[PATCH 1 of 1] ui: handle leading newlines in configlist

Henrik Stuart hg at hstuart.dk
Thu Jun 3 15:38:43 CDT 2010


# HG changeset patch
# User Henrik Stuart <hg at hstuart.dk>
# Date 1275597083 -7200
# Node ID f9c791b84bba1e68662d1a6e1362386fed773616
# Parent  e8a66a40474df0c70609ed16843f95183c1639c8
ui: handle leading newlines in configlist

Thanks to Greg Ward for supplying the test case.

diff --git a/mercurial/ui.py b/mercurial/ui.py
--- a/mercurial/ui.py
+++ b/mercurial/ui.py
@@ -155,14 +155,14 @@
     def configlist(self, section, name, default=None, untrusted=False):
         """Return a list of comma/space separated strings"""
 
-        def _parse_plain(parts, s, offset):
+        def _parse_plain(parts, s, offset, withlspace=False):
             whitespace = False
             while offset < len(s) and (s[offset].isspace() or s[offset] == ','):
                 whitespace = True
                 offset += 1
             if offset >= len(s):
                 return None, parts, offset
-            if whitespace:
+            if whitespace and (parts[-1] or withlspace):
                 parts.append('')
             if s[offset] == '"' and not parts[-1]:
                 return _parse_quote, parts, offset + 1
@@ -172,7 +172,7 @@
             parts[-1] += s[offset]
             return _parse_plain, parts, offset + 1
 
-        def _parse_quote(parts, s, offset):
+        def _parse_quote(parts, s, offset, withlspace):
             if offset < len(s) and s[offset] == '"': # ""
                 parts.append('')
                 offset += 1
@@ -191,7 +191,7 @@
                 offset += 1
 
             if offset >= len(s):
-                real_parts = _configlist(parts[-1])
+                real_parts = _configlist(parts[-1], withlspace=True)
                 if not real_parts:
                     parts[-1] = '"'
                 else:
@@ -215,13 +215,13 @@
 
             return _parse_plain, parts, offset
 
-        def _configlist(s):
+        def _configlist(s, withlspace=False):
             s = s.rstrip(' ,')
             if not s:
                 return None
             parser, parts, offset = _parse_plain, [''], 0
             while parser:
-                parser, parts, offset = parser(parts, s, offset)
+                parser, parts, offset = parser(parts, s, offset, withlspace)
             return parts
 
         result = self.config(section, name, untrusted=untrusted)
diff --git a/tests/test-ui-config.py b/tests/test-ui-config.py
--- a/tests/test-ui-config.py
+++ b/tests/test-ui-config.py
@@ -24,6 +24,7 @@
     'lists.list15=" just with starting quotation',
     'lists.list16="longer quotation" with "no ending quotation',
     'lists.list17=this is \\" "not a quotation mark"',
+    'lists.list18=\n\nding\ndong',
 ])
 
 print repr(testui.configitems('values'))
@@ -62,6 +63,7 @@
 print repr(testui.configlist('lists', 'list15'))
 print repr(testui.configlist('lists', 'list16'))
 print repr(testui.configlist('lists', 'list17'))
+print repr(testui.configlist('lists', 'list18'))
 print repr(testui.configlist('lists', 'unknown'))
 print repr(testui.configlist('lists', 'unknown', ''))
 print repr(testui.configlist('lists', 'unknown', 'foo'))
diff --git a/tests/test-ui-config.py.out b/tests/test-ui-config.py.out
--- a/tests/test-ui-config.py.out
+++ b/tests/test-ui-config.py.out
@@ -1,5 +1,5 @@
 [('string', 'string value'), ('bool1', 'true'), ('bool2', 'false')]
-[('list1', 'foo'), ('list2', 'foo bar baz'), ('list3', 'alice, bob'), ('list4', 'foo bar baz alice, bob'), ('list5', 'abc d"ef"g "hij def"'), ('list6', '"hello world", "how are you?"'), ('list7', 'Do"Not"Separate'), ('list8', '"Do"Separate'), ('list9', '"Do\\"NotSeparate"'), ('list10', 'string "with extraneous" quotation mark"'), ('list11', 'x, y'), ('list12', '"x", "y"'), ('list13', '""" key = "x", "y" """'), ('list14', ',,,,     '), ('list15', '" just with starting quotation'), ('list16', '"longer quotation" with "no ending quotation'), ('list17', 'this is \\" "not a quotation mark"')]
+[('list1', 'foo'), ('list2', 'foo bar baz'), ('list3', 'alice, bob'), ('list4', 'foo bar baz alice, bob'), ('list5', 'abc d"ef"g "hij def"'), ('list6', '"hello world", "how are you?"'), ('list7', 'Do"Not"Separate'), ('list8', '"Do"Separate'), ('list9', '"Do\\"NotSeparate"'), ('list10', 'string "with extraneous" quotation mark"'), ('list11', 'x, y'), ('list12', '"x", "y"'), ('list13', '""" key = "x", "y" """'), ('list14', ',,,,     '), ('list15', '" just with starting quotation'), ('list16', '"longer quotation" with "no ending quotation'), ('list17', 'this is \\" "not a quotation mark"'), ('list18', '\n\nding\ndong')]
 ---
 'string value'
 'true'
@@ -31,6 +31,7 @@
 ['"', 'just', 'with', 'starting', 'quotation']
 ['longer quotation', 'with', '"no', 'ending', 'quotation']
 ['this', 'is', '"', 'not a quotation mark']
+['ding', 'dong']
 []
 []
 ['foo']


More information about the Mercurial-devel mailing list