mercurial/utils/stringutil.py
changeset 37322 a67fd1fe5109
parent 37300 2f859ad7ed8c
child 37476 e9dea82ea1f3
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())))