@@ -349,12 +349,11 @@ def __check_input_type(self, input):
349
349
if self .__binary and isinstance (input , str ):
350
350
raise TypeError ("cannot use a bytes pattern on a string-like object" )
351
351
352
- def __tregex_compile (self , other_pattern = None , extra_flags = "" , extra_options = "" ):
353
- pattern = other_pattern or self .pattern
354
- flags = self .__flags_str + extra_flags
355
- if (pattern , flags , extra_options ) not in self .__compiled_regexes :
352
+ def __tregex_compile (self , method = "search" , must_advance = False ):
353
+ if (method , must_advance ) not in self .__compiled_regexes :
356
354
try :
357
- self .__compiled_regexes [(pattern , flags , extra_options )] = tregex_compile_internal (pattern , flags , extra_options , fallback_compiler )
355
+ extra_options = f"PythonMethod={ method } ,MustAdvance={ 'true' if must_advance else 'false' } "
356
+ self .__compiled_regexes [(method , must_advance )] = tregex_compile_internal (self .pattern , self .__flags_str , extra_options , fallback_compiler )
358
357
except ValueError as e :
359
358
if len (e .args ) == 2 :
360
359
msg = e .args [0 ]
@@ -367,7 +366,7 @@ def __tregex_compile(self, other_pattern=None, extra_flags="", extra_options="")
367
366
raise ValueError (msg ) from None
368
367
raise error (msg , self .pattern , e .args [1 ]) from None
369
368
raise
370
- return self .__compiled_regexes [(pattern , flags , extra_options )]
369
+ return self .__compiled_regexes [(method , must_advance )]
371
370
372
371
def __repr__ (self ):
373
372
flags = self .flags
@@ -405,25 +404,25 @@ def __copy__(self):
405
404
def __deepcopy__ (self , memo ):
406
405
return self
407
406
408
- def _search (self , string , pos , endpos , other_pattern = None , sticky = False , must_advance = False ):
407
+ def _search (self , string , pos , endpos , method = "search" , must_advance = False ):
409
408
_check_pos (pos )
410
409
self .__check_input_type (string )
411
410
substring , pos , endpos = _normalize_bounds (string , pos , endpos )
412
- compiled_regex = self .__tregex_compile (other_pattern = other_pattern , extra_flags = "y" if sticky else "" , extra_options = "MustAdvance=true" if must_advance else "" )
411
+ compiled_regex = self .__tregex_compile (method = method , must_advance = must_advance )
413
412
result = tregex_call_exec (compiled_regex .exec , substring , pos )
414
413
if result .isMatch :
415
414
return Match (self , pos , endpos , result , string , self .__indexgroup )
416
415
else :
417
416
return None
418
417
419
418
def search (self , string , pos = 0 , endpos = maxsize ):
420
- return self ._search (string , pos , endpos )
419
+ return self ._search (string , pos , endpos , method = "search" )
421
420
422
421
def match (self , string , pos = 0 , endpos = maxsize ):
423
- return self ._search (string , pos , endpos , sticky = True )
422
+ return self ._search (string , pos , endpos , method = "match" )
424
423
425
424
def fullmatch (self , string , pos = 0 , endpos = maxsize ):
426
- return self ._search (string , pos , endpos , sticky = True , other_pattern = _append_end_assert ( self . pattern ) )
425
+ return self ._search (string , pos , endpos , method = "fullmatch" )
427
426
428
427
def __sanitize_out_type (self , elem ):
429
428
"""Helper function for findall and split. Ensures that the type of the elements of the
@@ -444,7 +443,7 @@ def finditer(self, string, pos=0, endpos=maxsize):
444
443
def __finditer_gen (self , string , substring , pos , endpos ):
445
444
must_advance = False
446
445
while pos <= endpos :
447
- compiled_regex = self .__tregex_compile (extra_options = "MustAdvance=true" if must_advance else "" )
446
+ compiled_regex = self .__tregex_compile (must_advance = must_advance )
448
447
result = tregex_call_exec (compiled_regex .exec , substring , pos )
449
448
if not result .isMatch :
450
449
break
@@ -462,7 +461,7 @@ def findall(self, string, pos=0, endpos=maxsize):
462
461
group_count = self .__tregex_compile ().groupCount
463
462
must_advance = False
464
463
while pos <= endpos :
465
- compiled_regex = self .__tregex_compile (extra_options = "MustAdvance=true" if must_advance else "" )
464
+ compiled_regex = self .__tregex_compile (must_advance = must_advance )
466
465
result = tregex_call_exec (compiled_regex .exec , substring , pos )
467
466
if not result .isMatch :
468
467
break
@@ -499,7 +498,7 @@ def subn(self, repl, string, count=0):
499
498
literal = True
500
499
501
500
while (count == 0 or n < count ) and pos <= len (string ):
502
- compiled_regex = self .__tregex_compile (extra_options = "MustAdvance=true" if must_advance else "" )
501
+ compiled_regex = self .__tregex_compile (must_advance = must_advance )
503
502
match_result = tregex_call_exec (compiled_regex .exec , string , pos )
504
503
if not match_result .isMatch :
505
504
break
@@ -529,7 +528,7 @@ def split(self, string, maxsplit=0):
529
528
search_pos = 0
530
529
must_advance = False
531
530
while (maxsplit == 0 or n < maxsplit ) and search_pos <= len (string ):
532
- compiled_regex = self .__tregex_compile (extra_options = "MustAdvance=true" if must_advance else "" )
531
+ compiled_regex = self .__tregex_compile (must_advance = must_advance )
533
532
match_result = tregex_call_exec (compiled_regex .exec , string , search_pos )
534
533
if not match_result .isMatch :
535
534
break
@@ -562,10 +561,10 @@ def __init__(self, pattern, string, start, end):
562
561
self ._end = end
563
562
self ._must_advance = False
564
563
565
- def _match_search (self , sticky ):
564
+ def _match_search (self , method ):
566
565
if self ._start > len (self ._string ):
567
566
return None
568
- match = self .pattern ._search (self ._string , self ._start , self ._end , sticky = sticky , must_advance = self ._must_advance )
567
+ match = self .pattern ._search (self ._string , self ._start , self ._end , method = method , must_advance = self ._must_advance )
569
568
if match is None :
570
569
self ._start += 1
571
570
else :
@@ -574,10 +573,10 @@ def _match_search(self, sticky):
574
573
return match
575
574
576
575
def match (self ):
577
- return self ._match_search (True )
576
+ return self ._match_search ("match" )
578
577
579
578
def search (self ):
580
- return self ._match_search (False )
579
+ return self ._match_search ("search" )
581
580
582
581
583
582
_t_compile = Pattern
0 commit comments