Commit a1c9c525 authored by Adrian Holovaty's avatar Adrian Holovaty
Browse files

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

git-svn-id: http://code.djangoproject.com/svn/django/trunk@3101 bcc190cf-cafb-0310-a4f2-bffc1f526a37
parent 22da62f2
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -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)