D5273: hgignore: conversion glob into regex faster

valentin.gatienbaron (Valentin Gatien-Baron) phabricator at mercurial-scm.org
Mon Nov 19 13:33:37 EST 2018


valentin.gatienbaron updated this revision to Diff 12564.
valentin.gatienbaron edited the summary of this revision.
valentin.gatienbaron retitled this revision from "hgignore: faster conversion from globs to regexp" to "hgignore: conversion glob into regex faster".

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D5273?vs=12541&id=12564

REVISION DETAIL
  https://phab.mercurial-scm.org/D5273

AFFECTED FILES
  mercurial/match.py
  mercurial/utils/stringutil.py

CHANGE DETAILS

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))
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:



To: valentin.gatienbaron, #hg-reviewers
Cc: yuja, mercurial-devel


More information about the Mercurial-devel mailing list