##// END OF EJS Templates
stringutil: add a new function to do minimal regex escaping...
Augie Fackler -
r38493:96f65bdf default
parent child Browse files
Show More
@@ -23,6 +23,25 b' from .. import ('
23 pycompat,
23 pycompat,
24 )
24 )
25
25
26 # regex special chars pulled from https://bugs.python.org/issue29995
27 # which was part of Python 3.7.
28 _respecial = pycompat.bytestr(b'()[]{}?*+-|^$\\.# \t\n\r\v\f')
29 _regexescapemap = {ord(i): (b'\\' + i).decode('latin1') for i in _respecial}
30
31 def reescape(pat):
32 """Drop-in replacement for re.escape."""
33 # NOTE: it is intentional that this works on unicodes and not
34 # bytes, as it's only possible to do the escaping with
35 # unicode.translate, not bytes.translate. Sigh.
36 wantuni = True
37 if isinstance(pat, bytes):
38 wantuni = False
39 pat = pat.decode('latin1')
40 pat = pat.translate(_regexescapemap)
41 if wantuni:
42 return pat
43 return pat.encode('latin1')
44
26 def pprint(o, bprefix=False):
45 def pprint(o, bprefix=False):
27 """Pretty print an object."""
46 """Pretty print an object."""
28 if isinstance(o, bytes):
47 if isinstance(o, bytes):
General Comments 0
You need to be logged in to leave comments. Login now