diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2022-10-23 22:57:30 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-23 22:57:30 (GMT) |
commit | 75a6fadf369315b27e12f670e6295cf2c2cf7d7e (patch) | |
tree | cbf2004f870017f90874dc721e928da9412bab50 /Lib/re/__init__.py | |
parent | 176b6c57be70fb70fd0563813a87822545eb4bbf (diff) | |
download | cpython-75a6fadf369315b27e12f670e6295cf2c2cf7d7e.zip cpython-75a6fadf369315b27e12f670e6295cf2c2cf7d7e.tar.gz cpython-75a6fadf369315b27e12f670e6295cf2c2cf7d7e.tar.bz2 |
gh-91524: Speed up the regular expression substitution (#91525)
Functions re.sub() and re.subn() and corresponding re.Pattern methods
are now 2-3 times faster for replacement strings containing group references.
Closes #91524
Primarily authored by serhiy-storchaka Serhiy Storchaka
Minor-cleanups-by: Gregory P. Smith [Google] <greg@krypto.org>
Diffstat (limited to 'Lib/re/__init__.py')
-rw-r--r-- | Lib/re/__init__.py | 22 |
1 files changed, 4 insertions, 18 deletions
diff --git a/Lib/re/__init__.py b/Lib/re/__init__.py index 8d6a4ef..4515650 100644 --- a/Lib/re/__init__.py +++ b/Lib/re/__init__.py @@ -124,6 +124,7 @@ This module also defines an exception 'error'. import enum from . import _compiler, _parser import functools +import _sre # public symbols @@ -230,7 +231,7 @@ def purge(): "Clear the regular expression caches" _cache.clear() _cache2.clear() - _compile_repl.cache_clear() + _compile_template.cache_clear() def template(pattern, flags=0): "Compile a template pattern, returning a Pattern object, deprecated" @@ -328,24 +329,9 @@ def _compile(pattern, flags): return p @functools.lru_cache(_MAXCACHE) -def _compile_repl(repl, pattern): +def _compile_template(pattern, repl): # internal: compile replacement pattern - return _parser.parse_template(repl, pattern) - -def _expand(pattern, match, template): - # internal: Match.expand implementation hook - template = _parser.parse_template(template, pattern) - return _parser.expand_template(template, match) - -def _subx(pattern, template): - # internal: Pattern.sub/subn implementation helper - template = _compile_repl(template, pattern) - if not template[0] and len(template[1]) == 1: - # literal replacement - return template[1][0] - def filter(match, template=template): - return _parser.expand_template(template, match) - return filter + return _sre.template(pattern, _parser.parse_template(repl, pattern)) # register myself for pickling |