From b1681fb2a2f511ab1f5d2614c6c66092ed7d0c8f Mon Sep 17 00:00:00 2001 From: Dmitry Panov Date: Sun, 7 Jul 2024 17:33:29 +0100 Subject: [PATCH] Fixed String.prototype.split() when string is empty and limit is > 0. Fixes #588. --- builtin_string.go | 7 +------ builtin_string_test.go | 10 ++++++++++ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/builtin_string.go b/builtin_string.go index b2a0e4ea..43369c41 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 c740cc7e..2c217f0a 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) +}