stringutil: fix prettyrepr() to not orphan foo=<...> line
authorYuya Nishihara <yuya@tcha.org>
Sun, 10 Jun 2018 12:24:53 +0900
changeset 38264 fbb2eddea4d2
parent 38263 dbf31732ef64
child 38265 41ae9b3cbfb9
stringutil: fix prettyrepr() to not orphan foo=<...> line
mercurial/utils/stringutil.py
tests/test-walk.t
--- a/mercurial/utils/stringutil.py	Sun Jun 10 11:55:52 2018 +0900
+++ b/mercurial/utils/stringutil.py	Sun Jun 10 12:24:53 2018 +0900
@@ -49,15 +49,26 @@
     """Pretty print a representation of a possibly-nested object"""
     lines = []
     rs = pycompat.byterepr(o)
-    p = 0
-    while p < len(rs):
-        q = rs.find('<', p + 1)
-        if q < 0:
-            q = len(rs)
-        l = rs.count('<', 0, p) - rs.count('>', 0, p)
+    p0 = p1 = 0
+    while p0 < len(rs):
+        # '... field=<type ... field=<type ...'
+        #      ~~~~~~~~~~~~~~~~
+        #      p0    p1        q0    q1
+        q0 = -1
+        q1 = rs.find('<', p1 + 1)
+        if q1 < 0:
+            q1 = len(rs)
+        elif q1 > p1 + 1 and rs.startswith('=', q1 - 1):
+            # backtrack for ' field=<'
+            q0 = rs.rfind(' ', p1 + 1, q1 - 1)
+        if q0 < 0:
+            q0 = q1
+        else:
+            q0 += 1  # skip ' '
+        l = rs.count('<', 0, p0) - rs.count('>', 0, p0)
         assert l >= 0
-        lines.append((l, rs[p:q].rstrip()))
-        p = q
+        lines.append((l, rs[p0:q0].rstrip()))
+        p0, p1 = q0, q1
     return '\n'.join('  ' * l + s for l, s in lines)
 
 def binary(s):
--- a/tests/test-walk.t	Sun Jun 10 11:55:52 2018 +0900
+++ b/tests/test-walk.t	Sun Jun 10 12:24:53 2018 +0900
@@ -80,9 +80,9 @@
   f  mammals/skunk                   skunk
   $ hg debugwalk -v -X ../beans
   * matcher:
-  <differencematcher m1=
-    <alwaysmatcher>, m2=
-    <includematcher includes='(?:beans(?:/|$))'>>
+  <differencematcher
+    m1=<alwaysmatcher>,
+    m2=<includematcher includes='(?:beans(?:/|$))'>>
   f  fennel                          ../fennel
   f  fenugreek                       ../fenugreek
   f  fiddlehead                      ../fiddlehead
@@ -106,9 +106,9 @@
   f  mammals/skunk  skunk
   $ hg debugwalk -v -I 'relglob:*k' .
   * matcher:
-  <intersectionmatcher m1=
-    <patternmatcher patterns='(?:mammals(?:/|$))'>, m2=
-    <includematcher includes='(?:(?:|.*/)[^/]*k(?:/|$))'>>
+  <intersectionmatcher
+    m1=<patternmatcher patterns='(?:mammals(?:/|$))'>,
+    m2=<includematcher includes='(?:(?:|.*/)[^/]*k(?:/|$))'>>
   f  mammals/skunk  skunk
   $ hg debugwalk -v -I 're:.*k$'
   * matcher:
@@ -167,9 +167,9 @@
   f  fiddlehead  ../fiddlehead
   $ hg debugwalk -v -X 'rootfilesin:'
   * matcher:
-  <differencematcher m1=
-    <alwaysmatcher>, m2=
-    <includematcher includes='(?:[^/]+$)'>>
+  <differencematcher
+    m1=<alwaysmatcher>,
+    m2=<includematcher includes='(?:[^/]+$)'>>
   f  beans/black                     ../beans/black
   f  beans/borlotti                  ../beans/borlotti
   f  beans/kidney                    ../beans/kidney
@@ -228,9 +228,9 @@
   f  mammals/skunk  skunk
   $ hg debugwalk -v -X 'rootfilesin:mammals'
   * matcher:
-  <differencematcher m1=
-    <alwaysmatcher>, m2=
-    <includematcher includes='(?:mammals/[^/]+$)'>>
+  <differencematcher
+    m1=<alwaysmatcher>,
+    m2=<includematcher includes='(?:mammals/[^/]+$)'>>
   f  beans/black                     ../beans/black
   f  beans/borlotti                  ../beans/borlotti
   f  beans/kidney                    ../beans/kidney
@@ -322,9 +322,9 @@
   f  mammals/skunk  mammals/skunk
   $ hg debugwalk -v -Ibeans mammals
   * matcher:
-  <intersectionmatcher m1=
-    <patternmatcher patterns='(?:mammals(?:/|$))'>, m2=
-    <includematcher includes='(?:beans(?:/|$))'>>
+  <intersectionmatcher
+    m1=<patternmatcher patterns='(?:mammals(?:/|$))'>,
+    m2=<includematcher includes='(?:beans(?:/|$))'>>
   $ hg debugwalk -v -Inon-existent
   * matcher:
   <includematcher includes='(?:non\\-existent(?:/|$))'>
@@ -334,21 +334,21 @@
   f  beans/black  beans/black
   $ hg debugwalk -v -Ibeans beans/black
   * matcher:
-  <intersectionmatcher m1=
-    <patternmatcher patterns='(?:beans\\/black(?:/|$))'>, m2=
-    <includematcher includes='(?:beans(?:/|$))'>>
+  <intersectionmatcher
+    m1=<patternmatcher patterns='(?:beans\\/black(?:/|$))'>,
+    m2=<includematcher includes='(?:beans(?:/|$))'>>
   f  beans/black  beans/black  exact
   $ hg debugwalk -v -Ibeans/black beans
   * matcher:
-  <intersectionmatcher m1=
-    <patternmatcher patterns='(?:beans(?:/|$))'>, m2=
-    <includematcher includes='(?:beans\\/black(?:/|$))'>>
+  <intersectionmatcher
+    m1=<patternmatcher patterns='(?:beans(?:/|$))'>,
+    m2=<includematcher includes='(?:beans\\/black(?:/|$))'>>
   f  beans/black  beans/black
   $ hg debugwalk -v -Xbeans/black beans
   * matcher:
-  <differencematcher m1=
-    <patternmatcher patterns='(?:beans(?:/|$))'>, m2=
-    <includematcher includes='(?:beans\\/black(?:/|$))'>>
+  <differencematcher
+    m1=<patternmatcher patterns='(?:beans(?:/|$))'>,
+    m2=<includematcher includes='(?:beans\\/black(?:/|$))'>>
   f  beans/borlotti  beans/borlotti
   f  beans/kidney    beans/kidney
   f  beans/navy      beans/navy
@@ -356,9 +356,9 @@
   f  beans/turtle    beans/turtle
   $ hg debugwalk -v -Xbeans/black -Ibeans
   * matcher:
-  <differencematcher m1=
-    <includematcher includes='(?:beans(?:/|$))'>, m2=
-    <includematcher includes='(?:beans\\/black(?:/|$))'>>
+  <differencematcher
+    m1=<includematcher includes='(?:beans(?:/|$))'>,
+    m2=<includematcher includes='(?:beans\\/black(?:/|$))'>>
   f  beans/borlotti  beans/borlotti
   f  beans/kidney    beans/kidney
   f  beans/navy      beans/navy
@@ -366,24 +366,24 @@
   f  beans/turtle    beans/turtle
   $ hg debugwalk -v -Xbeans/black beans/black
   * matcher:
-  <differencematcher m1=
-    <patternmatcher patterns='(?:beans\\/black(?:/|$))'>, m2=
-    <includematcher includes='(?:beans\\/black(?:/|$))'>>
+  <differencematcher
+    m1=<patternmatcher patterns='(?:beans\\/black(?:/|$))'>,
+    m2=<includematcher includes='(?:beans\\/black(?:/|$))'>>
   $ hg debugwalk -v -Xbeans/black -Ibeans/black
   * matcher:
-  <differencematcher m1=
-    <includematcher includes='(?:beans\\/black(?:/|$))'>, m2=
-    <includematcher includes='(?:beans\\/black(?:/|$))'>>
+  <differencematcher
+    m1=<includematcher includes='(?:beans\\/black(?:/|$))'>,
+    m2=<includematcher includes='(?:beans\\/black(?:/|$))'>>
   $ hg debugwalk -v -Xbeans beans/black
   * matcher:
-  <differencematcher m1=
-    <patternmatcher patterns='(?:beans\\/black(?:/|$))'>, m2=
-    <includematcher includes='(?:beans(?:/|$))'>>
+  <differencematcher
+    m1=<patternmatcher patterns='(?:beans\\/black(?:/|$))'>,
+    m2=<includematcher includes='(?:beans(?:/|$))'>>
   $ hg debugwalk -v -Xbeans -Ibeans/black
   * matcher:
-  <differencematcher m1=
-    <includematcher includes='(?:beans\\/black(?:/|$))'>, m2=
-    <includematcher includes='(?:beans(?:/|$))'>>
+  <differencematcher
+    m1=<includematcher includes='(?:beans\\/black(?:/|$))'>,
+    m2=<includematcher includes='(?:beans(?:/|$))'>>
   $ hg debugwalk -v 'glob:mammals/../beans/b*'
   * matcher:
   <patternmatcher patterns='(?:beans\\/b[^/]*$)'>
@@ -391,9 +391,9 @@
   f  beans/borlotti  beans/borlotti
   $ hg debugwalk -v '-X*/Procyonidae' mammals
   * matcher:
-  <differencematcher m1=
-    <patternmatcher patterns='(?:mammals(?:/|$))'>, m2=
-    <includematcher includes='(?:[^/]*\\/Procyonidae(?:/|$))'>>
+  <differencematcher
+    m1=<patternmatcher patterns='(?:mammals(?:/|$))'>,
+    m2=<includematcher includes='(?:[^/]*\\/Procyonidae(?:/|$))'>>
   f  mammals/skunk  mammals/skunk
   $ hg debugwalk -v path:mammals
   * matcher:
@@ -425,24 +425,24 @@
 
   $ hg debugwalk -v fennel -X fennel
   * matcher:
-  <differencematcher m1=
-    <patternmatcher patterns='(?:fennel(?:/|$))'>, m2=
-    <includematcher includes='(?:fennel(?:/|$))'>>
+  <differencematcher
+    m1=<patternmatcher patterns='(?:fennel(?:/|$))'>,
+    m2=<includematcher includes='(?:fennel(?:/|$))'>>
   $ hg debugwalk -v fennel -X 'f*'
   * matcher:
-  <differencematcher m1=
-    <patternmatcher patterns='(?:fennel(?:/|$))'>, m2=
-    <includematcher includes='(?:f[^/]*(?:/|$))'>>
+  <differencematcher
+    m1=<patternmatcher patterns='(?:fennel(?:/|$))'>,
+    m2=<includematcher includes='(?:f[^/]*(?:/|$))'>>
   $ hg debugwalk -v beans/black -X 'path:beans'
   * matcher:
-  <differencematcher m1=
-    <patternmatcher patterns='(?:beans\\/black(?:/|$))'>, m2=
-    <includematcher includes='(?:beans(?:/|$))'>>
+  <differencematcher
+    m1=<patternmatcher patterns='(?:beans\\/black(?:/|$))'>,
+    m2=<includematcher includes='(?:beans(?:/|$))'>>
   $ hg debugwalk -v -I 'path:beans/black' -X 'path:beans'
   * matcher:
-  <differencematcher m1=
-    <includematcher includes='(?:beans\\/black(?:/|$))'>, m2=
-    <includematcher includes='(?:beans(?:/|$))'>>
+  <differencematcher
+    m1=<includematcher includes='(?:beans\\/black(?:/|$))'>,
+    m2=<includematcher includes='(?:beans(?:/|$))'>>
 
 Test absolute paths: