Add the keepends parameter to common.splitlines.

This commit is contained in:
Dave Halter
2016-08-07 16:57:53 +02:00
parent 2ae3aee7d0
commit 721195157a

View File

@@ -148,14 +148,38 @@ def source_to_unicode(source, encoding=None):
return unicode(source, encoding, 'replace')
def splitlines(string):
def splitlines(string, keepends=False):
"""
A splitlines for Python code. In contrast to Python's ``str.splitlines``,
looks at form feeds and other special characters as normal text. Just
splits ``\n`` and ``\r\n``.
Also different: Returns ``['']`` for an empty string input.
In Python 2.7 form feeds are used as normal characters when using
str.splitlines. However in Python 3 somewhere there was a decision to split
also on form feeds.
"""
return re.split('\n|\r\n', string)
if keepends:
# If capturing parentheses are used in pattern, then the text of all
# groups in the pattern are also returned as part of the resulting
# list.
lst = re.split('(\n|\r\n)', string)
# Need to merge the new lines with the actual lines.
odd = False
lines = []
for string in lst:
if odd:
line += string
lines.append(line)
else:
line = string
odd = not odd
if odd:
lines.append(line)
return lines
else:
return re.split('\n|\r\n', string)
def unite(iterable):