<s> from pypy . interpreter . baseobjspace import Wrappable <EOL> from pypy . interpreter . typedef import GetSetProperty , TypeDef <EOL> from pypy . interpreter . typedef import interp_attrproperty , interp_attrproperty_w <EOL> from pypy . interpreter . gateway import interp2app , ObjSpace , W_Root <EOL> from pypy . interpreter . error import OperationError <EOL> from pypy . rlib . rarithmetic import intmask <EOL> THREE_VERSIONS_OF_CORE = False <EOL> from pypy . rlib . rsre import rsre <EOL> from pypy . rlib . rsre . rsre_char import MAGIC , CODESIZE , getlower <EOL> copyright = "<STR_LIT>" <EOL> def w_getlower ( space , char_ord , flags ) : <EOL> return space . wrap ( getlower ( char_ord , flags ) ) <EOL> w_getlower . unwrap_spec = [ ObjSpace , int , int ] <EOL> def w_getcodesize ( space ) : <EOL> return space . wrap ( CODESIZE ) <EOL> from pypy . objspace . std . unicodeobject import unicodedb <EOL> rsre . set_unicode_db ( unicodedb ) <EOL> def make_state ( space , w_string , start , end , flags ) : <EOL> if space . is_true ( space . isinstance ( w_string , space . w_str ) ) : <EOL> cls = W_StringState <EOL> elif space . is_true ( space . isinstance ( w_string , space . w_unicode ) ) : <EOL> cls = W_UnicodeState <EOL> else : <EOL> cls = W_GenericState <EOL> return space . wrap ( cls ( space , w_string , start , end , flags ) ) <EOL> make_state . unwrap_spec = [ ObjSpace , W_Root , int , int , int ] <EOL> class W_State ( Wrappable ) : <EOL> if not THREE_VERSIONS_OF_CORE : <EOL> rsre . insert_sre_methods ( locals ( ) , '<STR_LIT:all>' ) <EOL> def __init__ ( self , space , w_string , start , end , flags ) : <EOL> self . space = space <EOL> self . w_string = w_string <EOL> length = self . unwrap_object ( ) <EOL> if start < <NUM_LIT:0> : <EOL> start = <NUM_LIT:0> <EOL> if end > length : <EOL> end = length <EOL> self . start = start <EOL> self . pos = start <EOL> self . end = end <EOL> self . flags = flags <EOL> self . reset ( ) <EOL> def lower ( self , char_ord ) : <EOL> return getlower ( char_ord , self . flags ) <EOL> def unwrap_object ( self ) : <EOL> raise NotImplementedError <EOL> if '<STR_LIT>' not in locals ( ) : <EOL> def reset ( self ) : <EOL> raise NotImplementedError <EOL> if '<STR_LIT>' not in locals ( ) : <EOL> def search ( self , pattern_codes ) : <EOL> raise NotImplementedError <EOL> if '<STR_LIT>' not in locals ( ) : <EOL> def match ( self , pattern_codes ) : <EOL> raise NotImplementedError <EOL> def w_reset ( self ) : <EOL> self . reset ( ) <EOL> def w_create_regs ( self , group_count ) : <EOL> """<STR_LIT>""" <EOL> space = self . space <EOL> return space . newtuple ( [ <EOL> space . newtuple ( [ space . wrap ( value1 ) , <EOL> space . wrap ( value2 ) ] ) <EOL> for value1 , value2 in self . create_regs ( group_count ) ] ) <EOL> w_create_regs . unwrap_spec = [ '<STR_LIT>' , int ] <EOL> def fget_start ( space , self ) : <EOL> return space . wrap ( self . start ) <EOL> def fset_start ( space , self , w_value ) : <EOL> self . start = space . int_w ( w_value ) <EOL> def fget_string_position ( space , self ) : <EOL> return space . wrap ( self . string_position ) <EOL> def fset_string_position ( space , self , w_value ) : <EOL> self . start = space . int_w ( w_value ) <EOL> def get_char_ord ( self , p ) : <EOL> raise NotImplementedError <EOL> getset_start = GetSetProperty ( W_State . fget_start , W_State . fset_start , cls = W_State ) <EOL> getset_string_position = GetSetProperty ( W_State . fget_string_position , <EOL> W_State . fset_string_position , cls = W_State ) <EOL> W_State . typedef = TypeDef ( "<STR_LIT>" , <EOL> string = interp_attrproperty_w ( "<STR_LIT>" , W_State ) , <EOL> start = getset_start , <EOL> end = interp_attrproperty ( "<STR_LIT:end>" , W_State ) , <EOL> string_position = getset_string_position , <EOL> pos = interp_attrproperty ( "<STR_LIT>" , W_State ) , <EOL> lastindex = interp_attrproperty ( "<STR_LIT>" , W_State ) , <EOL> reset = interp2app ( W_State . w_reset ) , <EOL> create_regs = interp2app ( W_State . w_create_regs ) , <EOL> ) <EOL> class W_StringState ( W_State ) : <EOL> if THREE_VERSIONS_OF_CORE : <EOL> rsre . insert_sre_methods ( locals ( ) , '<STR_LIT:str>' ) <EOL> def unwrap_object ( self ) : <EOL> self . string = self . space . str_w ( self . w_string ) <EOL> return len ( self . string ) <EOL> def get_char_ord ( self , p ) : <EOL> return ord ( self . string [ p ] ) <EOL> class W_UnicodeState ( W_State ) : <EOL> if THREE_VERSIONS_OF_CORE : <EOL> rsre . insert_sre_methods ( locals ( ) , '<STR_LIT>' ) <EOL> def unwrap_object ( self ) : <EOL> self . unicode = self . space . unicode_w ( self . w_string ) <EOL> return len ( self . unicode ) <EOL> def get_char_ord ( self , p ) : <EOL> return ord ( self . unicode [ p ] ) <EOL> class W_GenericState ( W_State ) : <EOL> if THREE_VERSIONS_OF_CORE : <EOL> rsre . insert_sre_methods ( locals ( ) , '<STR_LIT>' ) <EOL> def unwrap_object ( self ) : <EOL> self . buffer = self . space . buffer_w ( self . w_string ) <EOL> return self . buffer . getlength ( ) <EOL> def get_char_ord ( self , p ) : <EOL> return ord ( self . buffer . getitem ( p ) ) <EOL> def w_search ( space , w_state , w_pattern_codes ) : <EOL> state = space . interp_w ( W_State , w_state ) <EOL> pattern_codes = [ intmask ( space . uint_w ( code ) ) for code <EOL> in space . unpackiterable ( w_pattern_codes ) ] <EOL> return space . newbool ( state . search ( pattern_codes ) ) <EOL> def w_match ( space , w_state , w_pattern_codes ) : <EOL> state = space . interp_w ( W_State , w_state ) <EOL> pattern_codes = [ intmask ( space . uint_w ( code ) ) for code <EOL> in space . unpackiterable ( w_pattern_codes ) ] <EOL> return space . newbool ( state . match ( pattern_codes ) ) </s>
