comparison mercurial/utils/stringutil.py @ 37322:a67fd1fe5109

stringutil: drop escapedata() in favor of escapestr() They are quite similar. Let's choose one that uses standard Python escape.
author Yuya Nishihara <yuya@tcha.org>
date Wed, 04 Apr 2018 23:26:49 +0900
parents 2f859ad7ed8c
children e9dea82ea1f3
comparison
equal deleted inserted replaced
37321:e826fe7a08c7 37322:a67fd1fe5109
21 encoding, 21 encoding,
22 error, 22 error,
23 pycompat, 23 pycompat,
24 ) 24 )
25 25
26 _DATA_ESCAPE_MAP = {pycompat.bytechr(i): br'\x%02x' % i for i in range(256)}
27 _DATA_ESCAPE_MAP.update({
28 b'\\': b'\\\\',
29 b'\r': br'\r',
30 b'\n': br'\n',
31 })
32 _DATA_ESCAPE_RE = remod.compile(br'[\x00-\x08\x0a-\x1f\\\x7f-\xff]')
33
34 def escapedata(s):
35 if isinstance(s, bytearray):
36 s = bytes(s)
37
38 return _DATA_ESCAPE_RE.sub(lambda m: _DATA_ESCAPE_MAP[m.group(0)], s)
39
40 def pprint(o): 26 def pprint(o):
41 """Pretty print an object.""" 27 """Pretty print an object."""
42 if isinstance(o, (bytes, bytearray)): 28 if isinstance(o, (bytes, bytearray)):
43 return "b'%s'" % escapedata(o) 29 return "b'%s'" % escapestr(o)
44 elif isinstance(o, list): 30 elif isinstance(o, list):
45 return '[%s]' % (b', '.join(pprint(a) for a in o)) 31 return '[%s]' % (b', '.join(pprint(a) for a in o))
46 elif isinstance(o, dict): 32 elif isinstance(o, dict):
47 return '{%s}' % (b', '.join( 33 return '{%s}' % (b', '.join(
48 '%s: %s' % (pprint(k), pprint(v)) for k, v in sorted(o.items()))) 34 '%s: %s' % (pprint(k), pprint(v)) for k, v in sorted(o.items())))