Loading django/utils/text.py +11 −3 Original line number Diff line number Diff line Loading @@ -111,11 +111,19 @@ def javascript_quote(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('\\\\', '\\'), True) yield '"' + bit[1:-1].replace('\\"', '"').replace('\\\\', '\\') + '"' elif bit[0] == "'": yield (bit[1:-1].replace("\\'", "'").replace("\\\\", "\\"), True) yield "'" + bit[1:-1].replace("\\'", "'").replace("\\\\", "\\") + "'" else: yield (bit, False) yield bit Loading
django/utils/text.py +11 −3 Original line number Diff line number Diff line Loading @@ -111,11 +111,19 @@ def javascript_quote(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('\\\\', '\\'), True) yield '"' + bit[1:-1].replace('\\"', '"').replace('\\\\', '\\') + '"' elif bit[0] == "'": yield (bit[1:-1].replace("\\'", "'").replace("\\\\", "\\"), True) yield "'" + bit[1:-1].replace("\\'", "'").replace("\\\\", "\\") + "'" else: yield (bit, False) yield bit