diff --git a/qutebrowser/test/utils/test_split.py b/qutebrowser/test/utils/test_split.py index 5a1aa7516..6b4109587 100644 --- a/qutebrowser/test/utils/test_split.py +++ b/qutebrowser/test/utils/test_split.py @@ -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(): diff --git a/qutebrowser/utils/split.py b/qutebrowser/utils/split.py index 1cc02056f..0bcf933e4 100644 --- a/qutebrowser/utils/split.py +++ b/qutebrowser/utils/split.py @@ -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: