Fixed #3733 -- Fixed up quote parsing in smart_split(). Thanks, Ivan Chelubeev.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@4870 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Malcolm Tredinnick 2007-03-30 11:57:50 +00:00
parent 63a629bb8d
commit 5e739219de
5 changed files with 23 additions and 4 deletions

View File

@ -64,6 +64,7 @@ answer newbie questions, and generally made Django that much better:
Chris Chamberlin <dja@cdc.msbx.net> Chris Chamberlin <dja@cdc.msbx.net>
Amit Chakradeo <http://amit.chakradeo.net/> Amit Chakradeo <http://amit.chakradeo.net/>
ChaosKCW ChaosKCW
ivan.chelubeev@gmail.com
Ian Clelland <clelland@gmail.com> Ian Clelland <clelland@gmail.com>
crankycoder@gmail.com crankycoder@gmail.com
Matt Croydon <http://www.postneo.com/> Matt Croydon <http://www.postneo.com/>

View File

@ -191,14 +191,15 @@ def smart_split(text):
Supports both single and double quotes, and supports escaping quotes with Supports both single and double quotes, and supports escaping quotes with
backslashes. In the output, strings will keep their initial and trailing backslashes. In the output, strings will keep their initial and trailing
quote marks. quote marks.
>>> list(smart_split('This is "a person\'s" test.')) >>> list(smart_split('This is "a person\'s" test.'))
['This', 'is', '"a person\'s"', 'test.'] ['This', 'is', '"a person\'s"', 'test.']
""" """
for bit in smart_split_re.finditer(text): for bit in smart_split_re.finditer(text):
bit = bit.group(0) bit = bit.group(0)
if bit[0] == '"': if bit[0] == '"' and bit[-1] == '"':
yield '"' + bit[1:-1].replace('\\"', '"').replace('\\\\', '\\') + '"' yield '"' + bit[1:-1].replace('\\"', '"').replace('\\\\', '\\') + '"'
elif bit[0] == "'": elif bit[0] == "'" and bit[-1] == "'":
yield "'" + bit[1:-1].replace("\\'", "'").replace("\\\\", "\\") + "'" yield "'" + bit[1:-1].replace("\\'", "'").replace("\\\\", "\\") + "'"
else: else:
yield bit yield bit

View File

View File

View File

@ -0,0 +1,17 @@
"""
# Tests for stuff in django.utils.text.
>>> from django.utils.text import *
### smart_split ###########################################################
>>> list(smart_split(r'''This is "a person" test.'''))
['This', 'is', '"a person"', 'test.']
>>> print list(smart_split(r'''This is "a person's" test.'''))[2]
"a person's"
>>> print list(smart_split(r'''This is "a person\\"s" test.'''))[2]
"a person"s"
>>> list(smart_split('''"a 'one'''))
['"a', "'one"]
>>> print list(smart_split(r'''all friends' tests'''))[1]
friends'
"""