Clean up simple_split.
This commit is contained in:
parent
c70f6f534b
commit
6310081ef5
@ -155,6 +155,31 @@ def split(s, keep=False):
|
|||||||
return out
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
def _combine_ws(parts, whitespace):
|
||||||
|
"""Combine whitespace in a list with the element following it.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
parts: A list of strings.
|
||||||
|
whitespace: A string containing what's considered whitespace.
|
||||||
|
|
||||||
|
Return:
|
||||||
|
The modified list.
|
||||||
|
"""
|
||||||
|
out = []
|
||||||
|
ws = ''
|
||||||
|
for part in parts:
|
||||||
|
if not part:
|
||||||
|
continue
|
||||||
|
elif part in whitespace:
|
||||||
|
ws += part
|
||||||
|
else:
|
||||||
|
out.append(ws + part)
|
||||||
|
ws = ''
|
||||||
|
if ws:
|
||||||
|
out.append(ws)
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
def simple_split(s, keep=False, maxsplit=None):
|
def simple_split(s, keep=False, maxsplit=None):
|
||||||
"""Split a string on whitespace, optionally keeping the whitespace.
|
"""Split a string on whitespace, optionally keeping the whitespace.
|
||||||
|
|
||||||
@ -179,22 +204,9 @@ def simple_split(s, keep=False, maxsplit=None):
|
|||||||
|
|
||||||
if keep:
|
if keep:
|
||||||
pattern = '([' + whitespace + '])'
|
pattern = '([' + whitespace + '])'
|
||||||
|
parts = re.split(pattern, s, maxsplit)
|
||||||
|
return _combine_ws(parts, whitespace)
|
||||||
else:
|
else:
|
||||||
pattern = '[' + whitespace + ']'
|
pattern = '[' + whitespace + ']'
|
||||||
parts = re.split(pattern, s, maxsplit)
|
parts = re.split(pattern, s, maxsplit)
|
||||||
if keep:
|
return [p for p in parts if p]
|
||||||
out = []
|
|
||||||
ws = ''
|
|
||||||
for part in parts:
|
|
||||||
if not part:
|
|
||||||
continue
|
|
||||||
elif part in whitespace:
|
|
||||||
ws += part
|
|
||||||
else:
|
|
||||||
out.append(ws + part)
|
|
||||||
ws = ''
|
|
||||||
if ws:
|
|
||||||
out.append(ws)
|
|
||||||
else:
|
|
||||||
out = [p for p in parts if p]
|
|
||||||
return out
|
|
||||||
|
Loading…
Reference in New Issue
Block a user