diff options
author | Joseph Kocherhans <joseph@jkocherhans.com> | 2006-06-19 15:23:57 +0000 |
---|---|---|
committer | Joseph Kocherhans <joseph@jkocherhans.com> | 2006-06-19 15:23:57 +0000 |
commit | adf4b9311d5d64a2bdd58da50271c121ea22e397 (patch) | |
tree | a69b3b023595cf1ce67a14c4c1ecd3290d94088e /django/utils/text.py | |
parent | e976ed1f7910fad03704f88853c5c5b36cbab134 (diff) | |
download | django-attic/multi-auth.tar.gz |
multi-auth: Merged to [3151]archive/attic/multi-authattic/multi-auth
git-svn-id: http://code.djangoproject.com/svn/django/branches/multi-auth@3152 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/utils/text.py')
-rw-r--r-- | django/utils/text.py | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/django/utils/text.py b/django/utils/text.py index 7b6e1182ab..7df9bc03b7 100644 --- a/django/utils/text.py +++ b/django/utils/text.py @@ -109,3 +109,21 @@ def javascript_quote(s): s = s.replace("'", "\\'") return str(ustring_re.sub(fix, s)) +smart_split_re = re.compile('("(?:[^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'(?:[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*)\'|[^\\s]+)') +def smart_split(text): + """ + Generator that splits a string by spaces, leaving quoted phrases together. + Supports both single and double quotes, and supports escaping quotes with + backslashes. In the output, strings will keep their initial and trailing + quote marks. + >>> list(smart_split('This is "a person\'s" test.')) + ['This', 'is', '"a person\'s"', 'test.'] + """ + for bit in smart_split_re.finditer(text): + bit = bit.group(0) + if bit[0] == '"': + yield '"' + bit[1:-1].replace('\\"', '"').replace('\\\\', '\\') + '"' + elif bit[0] == "'": + yield "'" + bit[1:-1].replace("\\'", "'").replace("\\\\", "\\") + "'" + else: + yield bit |