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
This commit is contained in:
vitalyr@chromium.org 2010-11-10 12:34:28 +00:00
parent 869017bf6a
commit 87834f8cf0
4 changed files with 24 additions and 4 deletions

View File

@ -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<int>(Min<uint32_t>(s->length(), limit));
Handle<FixedArray> elements;
if (s->IsFlat() && s->IsAsciiRepresentation()) {

View File

@ -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 */ \

View File

@ -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);

View File

@ -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)));