Browse Source

Fixed String.prototype.split() when string is empty and limit is > 0. Fixes #588.

Dmitry Panov 1 year ago
parent
commit
b1681fb2a2
2 changed files with 11 additions and 6 deletions
  1. 1 6
      builtin_string.go
  2. 10 0
      builtin_string_test.go

+ 1 - 6
builtin_string.go

@@ -825,21 +825,16 @@ func (r *Runtime) stringproto_split(call FunctionCall) Value {
 
 
 	separator := separatorValue.String()
 	separator := separatorValue.String()
 
 
-	excess := false
 	str := s.String()
 	str := s.String()
-	if limit > len(str) {
-		limit = len(str)
-	}
 	splitLimit := limit
 	splitLimit := limit
 	if limit > 0 {
 	if limit > 0 {
 		splitLimit = limit + 1
 		splitLimit = limit + 1
-		excess = true
 	}
 	}
 
 
 	// TODO handle invalid UTF-16
 	// TODO handle invalid UTF-16
 	split := strings.SplitN(str, separator, splitLimit)
 	split := strings.SplitN(str, separator, splitLimit)
 
 
-	if excess && len(split) > limit {
+	if limit > 0 && len(split) > limit {
 		split = split[:limit]
 		split = split[:limit]
 	}
 	}
 
 

+ 10 - 0
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)
+}