Skip to content

Commit 320caf3

Browse files
committed
implement SRE_Pattern.finditer as a generator
1 parent 0046365 commit 320caf3

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

graalpython/lib-graalpython/_sre.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,22 @@ def __sanitize_out_type(self, elem):
299299
else:
300300
return str(elem)
301301

302+
def finditer(self, string, pos=0, endpos=-1):
303+
self.__check_input_type(string)
304+
if endpos > len(string):
305+
endpos = len(string)
306+
elif endpos < 0:
307+
endpos = endpos % len(string) + 1
308+
while pos < endpos:
309+
result = tregex_call_exec(self.__tregex_compile(self.pattern).exec, string, pos)
310+
if not result.isMatch:
311+
break
312+
else:
313+
yield SRE_Match(self, pos, endpos, result)
314+
no_progress = (result.start[0] == result.end[0])
315+
pos = result.end[0] + no_progress
316+
return
317+
302318
def findall(self, string, pos=0, endpos=-1):
303319
self.__check_input_type(string)
304320
if endpos > len(string):
@@ -320,7 +336,6 @@ def findall(self, string, pos=0, endpos=-1):
320336
pos = result.end[0] + no_progress
321337
return matchlist
322338

323-
324339
def __replace_groups(self, repl, string, match_result, pattern):
325340
def group(match_result, group_nr, string):
326341
if group_nr >= match_result.groupCount:

0 commit comments

Comments
 (0)