<s> import re <EOL> class UnicodeLiteralBuilder ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . chars = [ ] <EOL> def append ( self , characters ) : <EOL> if isinstance ( characters , str ) : <EOL> characters = characters . decode ( "<STR_LIT>" ) <EOL> assert isinstance ( characters , unicode ) , str ( type ( characters ) ) <EOL> self . chars . append ( characters ) <EOL> def append_charval ( self , char_number ) : <EOL> self . chars . append ( unichr ( char_number ) ) <EOL> def getstring ( self ) : <EOL> return EncodedString ( u'<STR_LIT>' . join ( self . chars ) ) <EOL> class BytesLiteralBuilder ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , target_encoding ) : <EOL> self . chars = [ ] <EOL> self . target_encoding = target_encoding <EOL> def append ( self , characters ) : <EOL> if isinstance ( characters , unicode ) : <EOL> characters = characters . encode ( self . target_encoding ) <EOL> assert isinstance ( characters , str ) , str ( type ( characters ) ) <EOL> self . chars . append ( characters ) <EOL> def append_charval ( self , char_number ) : <EOL> self . chars . append ( chr ( char_number ) ) <EOL> def getstring ( self ) : <EOL> s = BytesLiteral ( '<STR_LIT>' . join ( self . chars ) ) <EOL> s . encoding = self . target_encoding <EOL> return s <EOL> def getchar ( self ) : <EOL> return self . getstring ( ) <EOL> class EncodedString ( unicode ) : <EOL> encoding = None <EOL> def byteencode ( self ) : <EOL> assert self . encoding is not None <EOL> return self . encode ( self . encoding ) <EOL> def utf8encode ( self ) : <EOL> assert self . encoding is None <EOL> return self . encode ( "<STR_LIT>" ) <EOL> def is_unicode ( self ) : <EOL> return self . encoding is None <EOL> is_unicode = property ( is_unicode ) <EOL> class BytesLiteral ( str ) : <EOL> encoding = None <EOL> def byteencode ( self ) : <EOL> return str ( self ) <EOL> def utf8encode ( self ) : <EOL> assert False , "<STR_LIT>" % self <EOL> is_unicode = False <EOL> char_from_escape_sequence = { <EOL> r'<STR_LIT>' : u'<STR_LIT>' , <EOL> r'<STR_LIT>' : u'<STR_LIT>' , <EOL> r'<STR_LIT>' : u'<STR_LIT>' , <EOL> r'<STR_LIT:\n>' : u'<STR_LIT:\n>' , <EOL> r'<STR_LIT:\r>' : u'<STR_LIT:\r>' , <EOL> r'<STR_LIT:\t>' : u'<STR_LIT:\t>' , <EOL> r'<STR_LIT>' : u'<STR_LIT>' , <EOL> } . get <EOL> def _to_escape_sequence ( s ) : <EOL> if s in '<STR_LIT>' : <EOL> return repr ( s ) [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> elif s == '<STR_LIT:">' : <EOL> return r'<STR_LIT>' <EOL> else : <EOL> return '<STR_LIT>' . join ( [ '<STR_LIT>' % ord ( c ) for c in s ] ) <EOL> _c_special = ( '<STR_LIT>' , '<STR_LIT:\n>' , '<STR_LIT:\r>' , '<STR_LIT:\t>' , '<STR_LIT>' , '<STR_LIT:">' ) <EOL> _c_special_replacements = zip ( _c_special , map ( _to_escape_sequence , _c_special ) ) <EOL> def _build_specials_test ( ) : <EOL> subexps = [ ] <EOL> for special in _c_special : <EOL> regexp = '<STR_LIT>' . join ( [ '<STR_LIT>' % c for c in special ] ) <EOL> subexps . append ( regexp ) <EOL> return re . compile ( '<STR_LIT:|>' . join ( subexps ) ) . search <EOL> _has_specials = _build_specials_test ( ) <EOL> def escape_character ( c ) : <EOL> if c in '<STR_LIT>' : <EOL> return repr ( c ) [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> elif c == "<STR_LIT:'>" : <EOL> return "<STR_LIT>" <EOL> n = ord ( c ) <EOL> if n < <NUM_LIT:32> or n > <NUM_LIT> : <EOL> return "<STR_LIT>" % n <EOL> else : <EOL> return c <EOL> def escape_byte_string ( s ) : <EOL> s = s . replace ( '<STR_LIT:\\>' , '<STR_LIT>' ) <EOL> if _has_specials ( s ) : <EOL> for special , replacement in _c_special_replacements : <EOL> s = s . replace ( special , replacement ) <EOL> try : <EOL> s . decode ( "<STR_LIT>" ) <EOL> return s <EOL> except UnicodeDecodeError : <EOL> pass <EOL> l = [ ] <EOL> append = l . append <EOL> for c in s : <EOL> o = ord ( c ) <EOL> if o >= <NUM_LIT> : <EOL> append ( '<STR_LIT>' % o ) <EOL> else : <EOL> append ( c ) <EOL> return '<STR_LIT>' . join ( l ) <EOL> def split_docstring ( s ) : <EOL> if len ( s ) < <NUM_LIT> : <EOL> return s <EOL> return '<STR_LIT>' . join ( s . split ( r'<STR_LIT:\n>' ) ) </s>
