Added django.utils.text.smart_split. Thanks, ckknight

git-svn-id: http://code.djangoproject.com/svn/django/trunk@3101 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2006-06-07 06:08:23 +00:00
parent 22da62f239
commit a1c9c52533
1 changed files with 10 additions and 0 deletions

View File

@ -109,3 +109,13 @@ def javascript_quote(s):
s = s.replace("'", "\\'")
return str(ustring_re.sub(fix, s))
smart_split_re = re.compile('("(?:[^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'(?:[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*)\'|[^\\s]+)')
def smart_split(text):
for bit in smart_split_re.finditer(text):
bit = bit.group(0)
if bit[0] == '"':
yield (bit[1:-1].replace('\\"', '"').replace('\\\\', '\\'), True)
elif bit[0] == "'":
yield (bit[1:-1].replace("\\'", "'").replace("\\\\", "\\"), True)
else:
yield (bit, False)