Ignore:
Timestamp:
Nov 1, 2008, 5:38:01 PM (15 years ago)
Author:
y_z
Message:

fixed string literal parsing; broder will test

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/packages/invirt-dns/invirt-dns

    r1490 r1511  
    122122    quoted phrase, unless the double quote is escaped by a backslash
    123123    """
    124     # Grab everything up to the first whitespace character or
    125     # quotation mark not proceeded by a backslash
    126     whitespace_re = re.compile(r'(.*?)([\t\n\x0b\x0c\r ]+|(?<!\\)")')
     124    # Match either a quoted or unquoted string literal followed by
     125    # whitespace or the end of line.  This yields two groups, one of
     126    # which has a match, and the other of which is None, depending on
     127    # whether the string literal was quoted or unquoted; this is what
     128    # necessitates the subsequent filtering out of groups that are
     129    # None.
     130    string_pat = \
     131            re.compile(r'"((?:[^"\\]|\\.)*)"|((?:[^\\\s]|\\.)+)(?:\s+|\s*$)')
     132
     133    # For interpreting escapes.
     134    escape_pat = re.compile(r'\\(.)')
     135
    127136    def collapseContinuations(self, lines):
    128137        L = []
     
    143152        lines = L
    144153        L = []
     154
    145155        for line in lines:
    146156            in_quote = False
    147157            split_line = []
    148             while len(line) > 0:
    149                 match = self.whitespace_re.match(line)
    150                 if match is None:
    151                     # If there's no match, that means that there's no
    152                     # whitespace in the rest of the line, so it should
    153                     # be treated as a single entity, quoted or not
    154                     #
    155                     # This also means that a closing quote isn't
    156                     # strictly necessary if the line ends the quote
    157                     substr = line
    158                     end = ''
    159                 else:
    160                     substr, end = match.groups()
    161                
    162                 if in_quote:
    163                     # If we're in the middle of the quote, the string
    164                     # we just grabbed belongs at the end of the
    165                     # previous string
    166                     #
    167                     # Including the whitespace! Unless it's not
    168                     # whitespace and is actually a closequote instead
    169                     split_line[-1] += substr + (end if end != '"' else '')
    170                 else:
    171                     # If we're not in the middle of a quote, than this
    172                     # is the next new string
    173                     split_line.append(substr)
    174                
    175                 if end == '"':
    176                     in_quote = not in_quote
    177                
    178                 # Then strip off what we just processed
    179                 line = line[len(substr + end):]
     158            for m in string_pat.finditer(line):
     159                [x] = [x for x in m.groups() if x is not None]
     160                split_line.append(escape_pat.sub(r'\1', x))
    180161            L.append(split_line)
    181162        return filter(None, L)
Note: See TracChangeset for help on using the changeset viewer.