Skip to content
This repository was archived by the owner on Mar 23, 2023. It is now read-only.

Commit d52d231

Browse files
S-YOUtrotterdylan
authored andcommitted
Fix str split with whitespace (#182)
1 parent 9beb7e8 commit d52d231

3 files changed

Lines changed: 28 additions & 0 deletions

File tree

runtime/str.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,12 @@ func strSplit(f *Frame, args Args, kwargs KWArgs) (*Object, *BaseException) {
590590
s := toStrUnsafe(args[0]).Value()
591591
var parts []string
592592
if sep == "" {
593+
s = strings.TrimLeft(s, string(strASCIISpaces))
593594
parts = whitespaceSplitRegexp.Split(s, maxSplit)
595+
l := len(parts)
596+
if l > 0 && strings.Trim(parts[l-1], string(strASCIISpaces)) == "" {
597+
parts = parts[:l-1]
598+
}
594599
} else {
595600
parts = strings.SplitN(s, sep, maxSplit)
596601
}

runtime/str_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,16 @@ func TestStrMethods(t *testing.T) {
358358
{"split", wrapArgs("a \tb\nc", None, 1), newTestList("a", "b\nc").ToObject(), nil},
359359
{"split", wrapArgs("foo", 1), nil, mustCreateException(TypeErrorType, "expected a str separator")},
360360
{"split", wrapArgs("foo", ""), nil, mustCreateException(ValueErrorType, "empty separator")},
361+
{"split", wrapArgs(""), newTestList().ToObject(), nil},
362+
{"split", wrapArgs(" "), newTestList().ToObject(), nil},
363+
{"split", wrapArgs("", "x"), newTestList("").ToObject(), nil},
364+
{"split", wrapArgs(" ", " ", 1), newTestList("", "").ToObject(), nil},
365+
{"split", wrapArgs("aa", "a", 2), newTestList("", "", "").ToObject(), nil},
366+
{"split", wrapArgs(" a ", "a"), newTestList(" ", " ").ToObject(), nil},
367+
{"split", wrapArgs("a b c d", None, 1), newTestList("a", "b c d").ToObject(), nil},
368+
{"split", wrapArgs("a b c d "), newTestList("a", "b", "c", "d").ToObject(), nil},
369+
{"split", wrapArgs(" a b c d ", None, 1), newTestList("a", "b c d ").ToObject(), nil},
370+
{"split", wrapArgs(" a b c d ", None, 0), newTestList("a b c d ").ToObject(), nil},
361371
{"splitlines", wrapArgs(""), NewList().ToObject(), nil},
362372
{"splitlines", wrapArgs("\n"), newTestList("").ToObject(), nil},
363373
{"splitlines", wrapArgs("foo"), newTestList("foo").ToObject(), nil},

testing/str_test.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,19 @@ def __index__(self):
243243
except TypeError:
244244
pass
245245

246+
# Test split
247+
assert "".split() == []
248+
assert " ".split() == []
249+
assert "".split('x') == ['']
250+
assert "a".split() == ['a']
251+
assert " ".split(" ", 1) == ['', '']
252+
assert "aa".split("a", 2) == ['', '', '']
253+
assert " a ".split() == ['a']
254+
assert 'a b c d'.split(None, 1) == ['a', 'b c d']
255+
assert 'a b c d '.split() == ['a', 'b', 'c', 'd']
256+
assert ' a b c d '.split(None, 1) == ['a', 'b c d ']
257+
assert ' a b c d'.split(None, 0) == ['a b c d']
258+
246259
# Test zfill
247260
assert '123'.zfill(2) == '123'
248261
assert '123'.zfill(3) == '123'

0 commit comments

Comments
 (0)