Ticket #9315: patch-9315-regex-greedy.diff
File patch-9315-regex-greedy.diff, 4.5 KB (added by , 16 years ago) |
---|
-
django/utils/text.py
197 197 return str(ustring_re.sub(fix, s)) 198 198 javascript_quote = allow_lazy(javascript_quote, unicode) 199 199 200 smart_split_re = re.compile('("(?:[^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'(?:[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*)\'|[^\\s]+)') 200 smart_split_re = re.compile(r"""([^\s"]*"(?:[^"\\]*(?:\\.[^"\\]*)*)"\S*| # matches '"value with spaces"' and 'anything_before="value with spaces"' 201 [^\s']*'(?:[^'\\]*(?:\\.[^'\\]*)*)'\S*| # same as above but with quotes swapped 202 \S+) # matches not whitespaces""", 203 re.VERBOSE) 201 204 def smart_split(text): 202 205 r""" 203 206 Generator that splits a string by spaces, leaving quoted phrases together. … … 206 209 quote marks and escaped quotes will remain escaped (the results can then 207 210 be further processed with unescape_string_literal()). 208 211 212 (See more doctests at tests/regressiontests/text/tests.py) 213 209 214 >>> list(smart_split(r'This is "a person\'s" test.')) 210 215 [u'This', u'is', u'"a person\\\'s"', u'test.'] 211 216 >>> list(smart_split(r"Another 'person\'s' test.")) -
django/template/__init__.py
199 199 self.contents[:20].replace('\n', '')) 200 200 201 201 def split_contents(self): 202 split = [] 203 bits = iter(smart_split(self.contents)) 204 for bit in bits: 205 # Handle translation-marked template pieces 206 if bit.startswith('_("') or bit.startswith("_('"): 207 sentinal = bit[2] + ')' 208 trans_bit = [bit] 209 while not bit.endswith(sentinal): 210 bit = bits.next() 211 trans_bit.append(bit) 212 bit = ' '.join(trans_bit) 213 split.append(bit) 214 return split 202 return list(smart_split(self.contents)) 215 203 216 204 class Lexer(object): 217 205 def __init__(self, template_string, origin): … … 480 468 'arg_sep': re.escape(FILTER_ARGUMENT_SEPARATOR), 481 469 } 482 470 483 filter_raw_string = filter_raw_string.replace("\n", "").replace(" ", "") 484 filter_re = re.compile(filter_raw_string, re.UNICODE) 471 filter_re = re.compile(filter_raw_string, re.UNICODE | re.VERBOSE) 485 472 486 473 class FilterExpression(object): 487 474 r""" -
django/template/defaulttags.py
1092 1092 1093 1093 The URL will look like ``/clients/client/123/``. 1094 1094 """ 1095 bits = token. contents.split(' ')1095 bits = token.split_contents() 1096 1096 if len(bits) < 2: 1097 1097 raise TemplateSyntaxError("'%s' takes at least one argument" 1098 1098 " (path to a view)" % bits[0]) -
tests/regressiontests/text/tests.py
15 15 [u'"a', u"'one"] 16 16 >>> print list(smart_split(r'''all friends' tests'''))[1] 17 17 friends' 18 >>> list(smart_split(u'url search_page words="something else"')) 19 [u'url', u'search_page', u'words="something else"'] 20 >>> list(smart_split(u"url search_page words='something else'")) 21 [u'url', u'search_page', u"words='something else'"] 22 >>> list(smart_split(u'url search_page words "something else"')) 23 [u'url', u'search_page', u'words', u'"something else"'] 24 >>> list(smart_split(u'url search_page words-"something else"')) 25 [u'url', u'search_page', u'words-"something else"'] 26 >>> list(smart_split(u'url search_page words=hello')) 27 [u'url', u'search_page', u'words=hello'] 28 >>> list(smart_split(u'url search_page words="something else')) 29 [u'url', u'search_page', u'words="something', u'else'] 30 >>> list(smart_split(u"cycle 'a' 'b' 'c' as abc")) 31 [u'cycle', u"'a'", u"'b'", u"'c'", u'as', u'abc'] 32 >>> list(smart_split(u'cycle "a" "b" "c" as abc')) 33 [u'cycle', u'"a"', u'"b"', u'"c"', u'as', u'abc'] 34 >>> list(smart_split(u'cycle "a" "b" "c" "d" as abc')) 35 [u'cycle', u'"a"', u'"b"', u'"c"', u'"d"', u'as', u'abc'] 18 36 19 37 ### urlquote ############################################################# 20 38 >>> from django.utils.http import urlquote, urlquote_plus