changeset 3405:6f5e09f14525

parse_quoted_separated_ext: allow multiple/different quoting characters
author Johannes Berg <johannes AT sipsolutions DOT net>
date Sat, 22 Mar 2008 05:11:04 +0100
parents e979e3cad972
children ccdd2bbf76f7
files MoinMoin/_tests/test_wikiutil.py MoinMoin/wikiutil.py
diffstat 2 files changed, 30 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/_tests/test_wikiutil.py	Sat Mar 22 03:47:47 2008 +0100
+++ b/MoinMoin/_tests/test_wikiutil.py	Sat Mar 22 05:11:04 2008 +0100
@@ -383,6 +383,22 @@
         for test in tests:
             yield [_check] + list(test)
 
+    def testExtendedParserQuoting(self):
+        tests = [
+            (u'"a b" -a b-', u'"', [u'a b', u'-a', u'b-']),
+            (u'"a b" -a b-', u"-", [u'"a', u'b"', u'a b']),
+            (u'"a b" -a b-', u'"-', [u'a b', u'a b']),
+            (u'"a- b" -a b-', u'"-', [u'a- b', u'a b']),
+            (u'"a- b" -a" b-', u'"-', [u'a- b', u'a" b']),
+        ]
+
+        def _check(args, quotes, expected):
+            res = wikiutil.parse_quoted_separated_ext(args, quotes=quotes)
+            assert res == expected
+
+        for test in tests:
+            yield [_check] + list(test)
+
     def testExtendedParserMultikey(self):
         tests = [
             (u'"a", "b", "c"', u',', None, [u'a', u'b', u'c']),
--- a/MoinMoin/wikiutil.py	Sat Mar 22 03:47:47 2008 +0100
+++ b/MoinMoin/wikiutil.py	Sat Mar 22 05:11:04 2008 +0100
@@ -1282,14 +1282,14 @@
 
 def parse_quoted_separated_ext(args, separator=None, name_value_separator=None,
                                brackets=None, seplimit=0, multikey=False,
-                               prefixes=None):
+                               prefixes=None, quotes='"'):
     """
     Parses the given string according to the other parameters.
 
-    Items can be quoted with a double-quote ('"') and the quote can
-    be escaped by doubling it, the separator and name_value_separator
-    can both be quoted, when name_value_separator is set then the
-    name can also be quoted.
+    Items can be quoted with any character from the quotes parameter
+    and each quote can be escaped by doubling it, the separator and
+    name_value_separator can both be quoted, when name_value_separator
+    is set then the name can also be quoted.
 
     Values that are not given are returned as None, while the
     empty string as a value can be achieved by quoting it.
@@ -1349,7 +1349,7 @@
     max = len(args)
     result = []         # result list
     cur = [None]        # current item
-    quoted = False      # we're inside quotes
+    quoted = None       # we're inside quotes, indicates quote character used
     skipquote = 0       # next quote is a quoted quote
     noquote = False     # no quotes expected because word didn't start with one
     seplimit_reached = False # number of separators exhausted
@@ -1429,16 +1429,16 @@
         elif not quoted and not seplimit_reached and char in separators:
             (cur, noquote, separator_count, seplimit_reached,
              nextitemsep) = additem(result, cur, separator_count, nextitemsep)
-        elif not quoted and not noquote and char == '"':
+        elif not quoted and not noquote and char in quotes:
             if len(cur) and cur[-1] is None:
                 del cur[-1]
             cur.append(u'')
-            quoted = True
-        elif quoted and not skipquote and char == '"':
-            if next == '"':
+            quoted = char
+        elif char == quoted and not skipquote:
+            if next == quoted:
                 skipquote = 2 # will be decremented right away
             else:
-                quoted = False
+                quoted = None
         elif not quoted and char in opening:
             while len(cur) and cur[-1] is None:
                 del cur[-1]
@@ -1481,11 +1481,11 @@
     if quoted:
         if len(cur):
             if cur[-1] is None:
-                cur[-1] = '"'
+                cur[-1] = quoted
             else:
-                cur[-1] = '"' + cur[-1]
+                cur[-1] = quoted + cur[-1]
         else:
-            cur.append('"')
+            cur.append(quoted)
 
     additem(result, cur, separator_count, nextitemsep)