From: vitalyr@chromium.org Date: Wed, 10 Nov 2010 12:34:28 +0000 (+0000) Subject: Make String.prototype.split honor limit when separator is empty. X-Git-Tag: upstream/4.7.83~20973 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=87834f8cf02906d24c2eeee545d8c7ece93dd94d;p=platform%2Fupstream%2Fv8.git Make String.prototype.split honor limit when separator is empty. BUG=929 Review URL: http://codereview.chromium.org/4750003 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@5806 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- diff --git a/src/runtime.cc b/src/runtime.cc index 5534db5..3271148 100644 --- a/src/runtime.cc +++ b/src/runtime.cc @@ -5020,11 +5020,12 @@ static int CopyCachedAsciiCharsToArray(const char* chars, // For example, "foo" => ["f", "o", "o"]. static MaybeObject* Runtime_StringToArray(Arguments args) { HandleScope scope; - ASSERT(args.length() == 1); + ASSERT(args.length() == 2); CONVERT_ARG_CHECKED(String, s, 0); + CONVERT_NUMBER_CHECKED(uint32_t, limit, Uint32, args[1]); s->TryFlatten(); - const int length = s->length(); + const int length = static_cast(Min(s->length(), limit)); Handle elements; if (s->IsFlat() && s->IsAsciiRepresentation()) { diff --git a/src/runtime.h b/src/runtime.h index 756099b..76b3c77 100644 --- a/src/runtime.h +++ b/src/runtime.h @@ -175,7 +175,7 @@ namespace internal { F(StringReplaceRegExpWithString, 4, 1) \ F(StringMatch, 3, 1) \ F(StringTrim, 3, 1) \ - F(StringToArray, 1, 1) \ + F(StringToArray, 2, 1) \ F(NewStringWrapper, 1, 1) \ \ /* Numbers */ \ diff --git a/src/string.js b/src/string.js index d82ce05..3b3c82b 100644 --- a/src/string.js +++ b/src/string.js @@ -552,7 +552,7 @@ function StringSplit(separator, limit) { var separator_length = separator.length; // If the separator string is empty then return the elements in the subject. - if (separator_length === 0) return %StringToArray(subject); + if (separator_length === 0) return %StringToArray(subject, limit); var result = %StringSplit(subject, separator, limit); diff --git a/test/mjsunit/string-split.js b/test/mjsunit/string-split.js index c741f6a..6fcf557 100644 --- a/test/mjsunit/string-split.js +++ b/test/mjsunit/string-split.js @@ -97,3 +97,22 @@ assertEquals([""], ''.split(/./)); assertEquals([], ''.split(/.?/)); assertEquals([], ''.split(/.??/)); assertEquals([], ''.split(/()()/)); + + +// Issue http://code.google.com/p/v8/issues/detail?id=929 +// (Splitting with empty separator and a limit.) + +function numberObj(num) { + return {valueOf: function() { return num; }}; +} + +assertEquals([], "abc".split("", 0)); +assertEquals([], "abc".split("", numberObj(0))); +assertEquals(["a"], "abc".split("", 1)); +assertEquals(["a"], "abc".split("", numberObj(1))); +assertEquals(["a", "b"], "abc".split("", 2)); +assertEquals(["a", "b"], "abc".split("", numberObj(2))); +assertEquals(["a", "b", "c"], "abc".split("", 3)); +assertEquals(["a", "b", "c"], "abc".split("", numberObj(3))); +assertEquals(["a", "b", "c"], "abc".split("", 4)); +assertEquals(["a", "b", "c"], "abc".split("", numberObj(4)));