Fix splitting with split=False and maxsplit=0.

Since re.split behaves differently from str.split with maxsplit=0,
4e9b9baeab broke things like ":open foo bar".
This commit is contained in:
Florian Bruhin 2014-11-10 10:38:19 +01:00
parent 96c72724ce
commit 398da22b54
2 changed files with 17 additions and 1 deletions

View File

@ -157,6 +157,12 @@ class SimpleSplitTests(unittest.TestCase):
self.assertEqual(split.simple_split(string, maxsplit=1),
string.split(maxsplit=1))
def test_str_split_maxsplit_0(self):
"""Test if the behaviour matches str.split with maxsplit=0."""
string = " foo bar baz "
self.assertEqual(split.simple_split(string, maxsplit=0),
string.split(maxsplit=0))
def test_split_keep(self):
"""Test splitting with keep=True."""
for test, expected in self.TESTS.items():

View File

@ -155,7 +155,7 @@ def split(s, keep=False):
return out
def simple_split(s, keep=False, maxsplit=0):
def simple_split(s, keep=False, maxsplit=None):
"""Split a string on whitespace, optionally keeping the whitespace.
Args:
@ -167,6 +167,16 @@ def simple_split(s, keep=False, maxsplit=0):
A list of split strings.
"""
whitespace = '\n\t '
if maxsplit == 0:
# re.split with maxsplit=0 splits everything, while str.split splits
# nothing (which is the behaviour we want).
if keep:
return [s]
else:
return [s.lstrip(whitespace)]
elif maxsplit is None:
maxsplit = 0
if keep:
pattern = '([' + whitespace + '])'
else: