diff options
| author | Adrian Holovaty <adrian@holovaty.com> | 2006-06-07 06:08:23 +0000 |
|---|---|---|
| committer | Adrian Holovaty <adrian@holovaty.com> | 2006-06-07 06:08:23 +0000 |
| commit | a1c9c525330ae489c5ad124818ea42097b0d3b73 (patch) | |
| tree | 3873530887eef292db148ca1068d6d0479348ca3 /django/utils/text.py | |
| parent | 22da62f2391b88b524e33f79956db998b9522fb5 (diff) | |
Added django.utils.text.smart_split. Thanks, ckknight
git-svn-id: http://code.djangoproject.com/svn/django/trunk@3101 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/utils/text.py')
| -rw-r--r-- | django/utils/text.py | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/django/utils/text.py b/django/utils/text.py index 7b6e1182ab..08ac596836 100644 --- a/django/utils/text.py +++ b/django/utils/text.py @@ -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) |
