diff --git a/mercurial/match.py b/mercurial/match.py --- a/mercurial/match.py +++ b/mercurial/match.py @@ -1057,7 +1057,7 @@ i, n = 0, len(pat) res = '' group = 0 - escape = util.stringutil.reescape + escape = util.stringutil.reescapechar def peek(): return i < n and pat[i:i + 1] while i < n: diff --git a/mercurial/utils/stringutil.py b/mercurial/utils/stringutil.py --- a/mercurial/utils/stringutil.py +++ b/mercurial/utils/stringutil.py @@ -27,7 +27,8 @@ # regex special chars pulled from https://bugs.python.org/issue29995 # which was part of Python 3.7. _respecial = pycompat.bytestr(b'()[]{}?*+-|^$\\.&~# \t\n\r\v\f') -_regexescapemap = {ord(i): (b'\\' + i).decode('latin1') for i in _respecial} +_regexescapemapb = {i: (b'\\' + i) for i in _respecial} +_regexescapemapu = {ord(i): (b'\\' + i).decode('latin1') for i in _respecial} def reescape(pat): """Drop-in replacement for re.escape.""" @@ -38,11 +39,15 @@ if isinstance(pat, bytes): wantuni = False pat = pat.decode('latin1') - pat = pat.translate(_regexescapemap) + pat = pat.translate(_regexescapemapu) if wantuni: return pat return pat.encode('latin1') +def reescapechar(pat): + """Fast specialized version of reescape that operates on a 1-byte bytes""" + return _regexescapemapb.get(pat, pat) + def pprint(o, bprefix=False, indent=0, level=0): """Pretty print an object.""" return b''.join(pprintgen(o, bprefix=bprefix, indent=indent, level=level))