Skip to content

Commit

Permalink
Fixed String.prototype.split() when string is empty and limit is > 0. F…
Browse files Browse the repository at this point in the history
…ixes #588.
  • Loading branch information
dop251 committed Jul 7, 2024
1 parent eb1f15e commit b1681fb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
7 changes: 1 addition & 6 deletions builtin_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -825,21 +825,16 @@ func (r *Runtime) stringproto_split(call FunctionCall) Value {

separator := separatorValue.String()

excess := false
str := s.String()
if limit > len(str) {
limit = len(str)
}
splitLimit := limit
if limit > 0 {
splitLimit = limit + 1
excess = true
}

// TODO handle invalid UTF-16
split := strings.SplitN(str, separator, splitLimit)

if excess && len(split) > limit {
if limit > 0 && len(split) > limit {
split = split[:limit]
}

Expand Down
10 changes: 10 additions & 0 deletions builtin_string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,3 +276,13 @@ func TestValueStringBuilder(t *testing.T) {
})

}

func TestStringSplit(t *testing.T) {
const SCRIPT = `
assert(compareArray("".split("#",2), [""]));
assert(compareArray("".split("#"), [""]));
assert(compareArray("".split("",2), []));
assert(compareArray("".split(""), []));
`
testScriptWithTestLib(SCRIPT, _undefined, t)
}

0 comments on commit b1681fb

Please sign in to comment.