@@ -127,12 +127,13 @@ def new_compile(p, flags=0):
127
127
128
128
129
129
class SRE_Match ():
130
- def __init__ (self , pattern , pos , endpos , result ):
130
+ def __init__ (self , pattern , pos , endpos , result , input_str , compiled_regex ):
131
131
self .result = result
132
- self .compiled_regex = result . regex
132
+ self .compiled_regex = compiled_regex
133
133
self .re = pattern
134
134
self .pos = pos
135
135
self .endpos = endpos
136
+ self .input_str = input_str
136
137
137
138
def end (self , groupnum = 0 ):
138
139
return self .result .end [groupnum ]
@@ -166,7 +167,7 @@ def __group__(self, idx):
166
167
if start < 0 :
167
168
return None
168
169
else :
169
- return self .result . input [start :self .result .end [idxarg ]]
170
+ return self .input_str [start :self .result .end [idxarg ]]
170
171
171
172
def groupdict (self , default = None ):
172
173
d = {}
@@ -187,7 +188,7 @@ def start(self, groupnum=0):
187
188
188
189
@property
189
190
def string (self ):
190
- return self .result . input
191
+ return self .input_str
191
192
192
193
@property
193
194
def lastgroup (self ):
@@ -274,12 +275,14 @@ def __repr__(self):
274
275
275
276
def _search (self , pattern , string , pos , endpos , sticky = False ):
276
277
pattern = self .__tregex_compile (pattern , self .flags_str + ("y" if sticky else "" ))
278
+ input_str = string
277
279
if endpos == - 1 or endpos >= len (string ):
278
- result = tregex_call_exec (pattern .exec , string , min (pos , len (string ) + 1 ))
280
+ result = tregex_call_exec (pattern .exec , input_str , min (pos , len (string ) + 1 ))
279
281
else :
280
- result = tregex_call_exec (pattern .exec , string [:endpos ], min (pos , endpos % len (string ) + 1 ))
282
+ input_str = string [:endpos ]
283
+ result = tregex_call_exec (pattern .exec , input_str , min (pos , endpos % len (string ) + 1 ))
281
284
if result .isMatch :
282
- return SRE_Match (self , pos , endpos , result )
285
+ return SRE_Match (self , pos , endpos , result , input_str , pattern )
283
286
else :
284
287
return None
285
288
@@ -310,11 +313,12 @@ def finditer(self, string, pos=0, endpos=-1):
310
313
elif endpos < 0 :
311
314
endpos = endpos % len (string ) + 1
312
315
while pos < endpos :
313
- result = tregex_call_exec (self .__tregex_compile (self .pattern ).exec , string , pos )
316
+ compiled_regex = self .__tregex_compile (self .pattern )
317
+ result = tregex_call_exec (compiled_regex .exec , string , pos )
314
318
if not result .isMatch :
315
319
break
316
320
else :
317
- yield SRE_Match (self , pos , endpos , result )
321
+ yield SRE_Match (self , pos , endpos , result , string , compiled_regex )
318
322
no_progress = (result .start [0 ] == result .end [0 ])
319
323
pos = result .end [0 ] + no_progress
320
324
return
@@ -327,15 +331,16 @@ def findall(self, string, pos=0, endpos=-1):
327
331
endpos = endpos % len (string ) + 1
328
332
matchlist = []
329
333
while pos < endpos :
330
- result = tregex_call_exec (self .__tregex_compile (self .pattern ).exec , string , pos )
334
+ compiled_regex = self .__tregex_compile (self .pattern )
335
+ result = tregex_call_exec (compiled_regex .exec , string , pos )
331
336
if not result .isMatch :
332
337
break
333
338
elif result .groupCount == 1 :
334
339
matchlist .append (self .__sanitize_out_type (string [result .start [0 ]:result .end [0 ]]))
335
340
elif result .groupCount == 2 :
336
341
matchlist .append (self .__sanitize_out_type (string [result .start [1 ]:result .end [1 ]]))
337
342
else :
338
- matchlist .append (tuple (map (self .__sanitize_out_type , SRE_Match (self , pos , endpos , result ).groups ())))
343
+ matchlist .append (tuple (map (self .__sanitize_out_type , SRE_Match (self , pos , endpos , result , string , compiled_regex ).groups ())))
339
344
no_progress = (result .start [0 ] == result .end [0 ])
340
345
pos = result .end [0 ] + no_progress
341
346
return matchlist
@@ -421,7 +426,7 @@ def sub(self, repl, string, count=0):
421
426
if is_string_rep :
422
427
result .append (self .__replace_groups (repl , string , match_result , pattern ))
423
428
else :
424
- _srematch = SRE_Match (self , pos , - 1 , match_result )
429
+ _srematch = SRE_Match (self , pos , - 1 , match_result , string , pattern )
425
430
_repl = repl (_srematch )
426
431
result .append (_repl )
427
432
pos = end
0 commit comments