diff --git a/builtin_string.go b/builtin_string.go index 2343ed2..5091c08 100644 --- a/builtin_string.go +++ b/builtin_string.go @@ -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] } diff --git a/builtin_string_test.go b/builtin_string_test.go index e1b80ca..32d0cc4 100644 --- a/builtin_string_test.go +++ b/builtin_string_test.go @@ -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) +}