@@ -48,21 +48,29 @@ def maxsize():
48
48
import sys
49
49
return sys .maxsize
50
50
51
+
51
52
class _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
54
55
self .isMatch = isMatch
55
56
self .groupCount = groupCount
56
57
self .start = start
57
58
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
+
59
66
60
67
def _str_to_bytes (arg ):
61
68
buffer = bytearray (len (arg ))
62
69
for i , c in enumerate (arg ):
63
70
buffer [i ] = ord (c )
64
71
return bytes (buffer )
65
72
73
+
66
74
def setup (sre_compiler , error_class , flags_table ):
67
75
global error
68
76
error = error_class
@@ -82,22 +90,36 @@ def fallback_compiler(pattern, flags):
82
90
bit_flags = bit_flags | FLAGS [flag ]
83
91
84
92
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 ()
101
123
102
124
return fallback_compiler
103
125
0 commit comments