mercurial/utils/stringutil.py
changeset 38474 96f65bdf0bf4
parent 38264 fbb2eddea4d2
child 38477 de275ab362cb
--- a/mercurial/utils/stringutil.py	Tue Jun 26 16:14:02 2018 +0530
+++ b/mercurial/utils/stringutil.py	Tue Jun 26 10:33:52 2018 -0400
@@ -23,6 +23,25 @@
     pycompat,
 )
 
+# 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}
+
+def reescape(pat):
+    """Drop-in replacement for re.escape."""
+    # NOTE: it is intentional that this works on unicodes and not
+    # bytes, as it's only possible to do the escaping with
+    # unicode.translate, not bytes.translate. Sigh.
+    wantuni = True
+    if isinstance(pat, bytes):
+        wantuni = False
+        pat = pat.decode('latin1')
+    pat = pat.translate(_regexescapemap)
+    if wantuni:
+        return pat
+    return pat.encode('latin1')
+
 def pprint(o, bprefix=False):
     """Pretty print an object."""
     if isinstance(o, bytes):