@@ -48,21 +48,29 @@ def maxsize():
4848 import sys
4949 return sys .maxsize
5050
51+
5152class _RegexResult :
52- def __init__ (self , input , isMatch , groupCount , start , end , regex ):
53- self .input = input
53+ def __init__ (self , pattern_input , isMatch , groupCount , start , end ):
54+ self .input = pattern_input
5455 self .isMatch = isMatch
5556 self .groupCount = groupCount
5657 self .start = start
5758 self .end = end
58- self .regex = regex
59+
60+ def getStart (self , grpidx ):
61+ return self .start [grpidx ]
62+
63+ def getEnd (self , grpidx ):
64+ return self .end [grpidx ]
65+
5966
6067def _str_to_bytes (arg ):
6168 buffer = bytearray (len (arg ))
6269 for i , c in enumerate (arg ):
6370 buffer [i ] = ord (c )
6471 return bytes (buffer )
6572
73+
6674def setup (sre_compiler , error_class , flags_table ):
6775 global error
6876 error = error_class
@@ -82,22 +90,36 @@ def fallback_compiler(pattern, flags):
8290 bit_flags = bit_flags | FLAGS [flag ]
8391
8492 compiled_pattern = sre_compiler (pattern if mode == "str" else _str_to_bytes (pattern ), bit_flags )
85-
86- def executable_pattern (regex_object , input , from_index ):
87- search_method = compiled_pattern .match if sticky else compiled_pattern .search
88- result = search_method (input , from_index )
89- is_match = result is not None
90- group_count = 1 + compiled_pattern .groups
91- return _RegexResult (
92- input = input ,
93- isMatch = is_match ,
94- groupCount = group_count if is_match else 0 ,
95- start = [result .start (i ) for i in range (group_count )] if is_match else [],
96- end = [result .end (i ) for i in range (group_count )] if is_match else [],
97- regex = regex_object
98- )
99-
100- return executable_pattern
93+
94+ # wraps a native 're.Pattern' object
95+ class ExecutablePattern :
96+ def __call__ (self , * args ):
97+ # deprecated
98+ return self .exec (* args )
99+
100+ def exec (self , * args ):
101+ nargs = len (args )
102+ if nargs == 2 :
103+ # new-style signature
104+ pattern_input , from_index = args
105+ elif nargs == 3 :
106+ # old-style signature; deprecated
107+ _ , pattern_input , from_index = args
108+ else :
109+ raise TypeError ("invalid arguments: " + repr (args ))
110+ search_method = compiled_pattern .match if sticky else compiled_pattern .search
111+ result = search_method (pattern_input , from_index )
112+ is_match = result is not None
113+ group_count = 1 + compiled_pattern .groups
114+ return _RegexResult (
115+ pattern_input = pattern_input ,
116+ isMatch = is_match ,
117+ groupCount = group_count if is_match else 0 ,
118+ start = [result .start (i ) for i in range (group_count )] if is_match else [],
119+ end = [result .end (i ) for i in range (group_count )] if is_match else []
120+ )
121+
122+ return ExecutablePattern ()
101123
102124 return fallback_compiler
103125
0 commit comments