comparison mercurial/utils/stringutil.py @ 38276:fbb2eddea4d2

stringutil: fix prettyrepr() to not orphan foo=<...> line
author Yuya Nishihara <yuya@tcha.org>
date Sun, 10 Jun 2018 12:24:53 +0900
parents f3033692ccef
children 96f65bdf0bf4
comparison
equal deleted inserted replaced
38275:dbf31732ef64 38276:fbb2eddea4d2
47 47
48 def prettyrepr(o): 48 def prettyrepr(o):
49 """Pretty print a representation of a possibly-nested object""" 49 """Pretty print a representation of a possibly-nested object"""
50 lines = [] 50 lines = []
51 rs = pycompat.byterepr(o) 51 rs = pycompat.byterepr(o)
52 p = 0 52 p0 = p1 = 0
53 while p < len(rs): 53 while p0 < len(rs):
54 q = rs.find('<', p + 1) 54 # '... field=<type ... field=<type ...'
55 if q < 0: 55 # ~~~~~~~~~~~~~~~~
56 q = len(rs) 56 # p0 p1 q0 q1
57 l = rs.count('<', 0, p) - rs.count('>', 0, p) 57 q0 = -1
58 q1 = rs.find('<', p1 + 1)
59 if q1 < 0:
60 q1 = len(rs)
61 elif q1 > p1 + 1 and rs.startswith('=', q1 - 1):
62 # backtrack for ' field=<'
63 q0 = rs.rfind(' ', p1 + 1, q1 - 1)
64 if q0 < 0:
65 q0 = q1
66 else:
67 q0 += 1 # skip ' '
68 l = rs.count('<', 0, p0) - rs.count('>', 0, p0)
58 assert l >= 0 69 assert l >= 0
59 lines.append((l, rs[p:q].rstrip())) 70 lines.append((l, rs[p0:q0].rstrip()))
60 p = q 71 p0, p1 = q0, q1
61 return '\n'.join(' ' * l + s for l, s in lines) 72 return '\n'.join(' ' * l + s for l, s in lines)
62 73
63 def binary(s): 74 def binary(s):
64 """return true if a string is binary data""" 75 """return true if a string is binary data"""
65 return bool(s and '\0' in s) 76 return bool(s and '\0' in s)