Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bec95b9

Browse files
author
Fredrik Lundh
committed
rewrote the pattern.sub and pattern.subn methods in C
removed (conceptually flawed) getliteral helper; the new sub/subn code uses a faster code path for literal replacement strings, but doesn't (yet) look for literal patterns. added STATE_OFFSET macro, and use it to convert state.start/ptr to char indexes
1 parent 5b68ce3 commit bec95b9

2 files changed

Lines changed: 331 additions & 130 deletions

File tree

Lib/sre.py

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104
"U", "IGNORECASE", "LOCALE", "MULTILINE", "DOTALL", "VERBOSE",
105105
"UNICODE", "error" ]
106106

107-
__version__ = "2.2.0"
107+
__version__ = "2.2.1"
108108

109109
# this module works under 1.5.2 and later. don't use string methods
110110
import string
@@ -244,26 +244,33 @@ def _expand(pattern, match, template):
244244
template = sre_parse.parse_template(template, pattern)
245245
return sre_parse.expand_template(template, match)
246246

247-
def _sub(pattern, template, text, count=0):
248-
# internal: pattern.sub implementation hook
249-
return _subn(pattern, template, text, count, 1)[0]
250-
251-
def _subn(pattern, template, text, count=0, sub=0):
252-
# internal: pattern.subn implementation hook
247+
def _subx(pattern, template):
248+
# internal: pattern.sub/subn implementation helper
253249
if callable(template):
254250
filter = template
255251
else:
256252
template = _compile_repl(template, pattern)
257-
literals = template[1]
258-
if sub and not count:
259-
literal = pattern._getliteral()
260-
if literal and "\\" in literal:
261-
literal = None # may contain untranslated escapes
262-
if literal is not None and len(literals) == 1 and literals[0]:
263-
# shortcut: both pattern and string are literals
264-
return string.replace(text, pattern.pattern, literals[0]), 0
265-
def filter(match, template=template):
266-
return sre_parse.expand_template(template, match)
253+
if not template[0] and len(template[1]) == 1:
254+
# literal replacement
255+
filter = template[1][0]
256+
else:
257+
def filter(match, template=template):
258+
return sre_parse.expand_template(template, match)
259+
return filter
260+
261+
def _sub(pattern, template, text, count=0):
262+
# internal: pattern.sub implementation hook
263+
# FIXME: not used in SRE 2.2.1 and later; will be removed soon
264+
return _subn(pattern, template, text, count)[0]
265+
266+
def _subn(pattern, template, text, count=0):
267+
# internal: pattern.subn implementation hook
268+
# FIXME: not used in SRE 2.2.1 and later; will be removed soon
269+
filter = _subx(pattern, template)
270+
if not callable(filter):
271+
# literal replacement
272+
def filter(match, literal=filter):
273+
return literal
267274
n = i = 0
268275
s = []
269276
append = s.append
@@ -286,6 +293,7 @@ def filter(match, template=template):
286293

287294
def _split(pattern, text, maxsplit=0):
288295
# internal: pattern.split implementation hook
296+
# FIXME: not used in SRE 2.2.1 and later; will be removed soon
289297
n = i = 0
290298
s = []
291299
append = s.append

0 commit comments

Comments
 (0)