Revision: 5806
Author: [email protected]
Date: Wed Nov 10 04:34:28 2010
Log: Make String.prototype.split honor limit when separator is empty.
BUG=929
Review URL: http://codereview.chromium.org/4750003
http://code.google.com/p/v8/source/detail?r=5806
Modified:
/branches/bleeding_edge/src/runtime.cc
/branches/bleeding_edge/src/runtime.h
/branches/bleeding_edge/src/string.js
/branches/bleeding_edge/test/mjsunit/string-split.js
=======================================
--- /branches/bleeding_edge/src/runtime.cc Tue Nov 2 06:37:59 2010
+++ /branches/bleeding_edge/src/runtime.cc Wed Nov 10 04:34:28 2010
@@ -5020,11 +5020,12 @@
// 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()) {
=======================================
--- /branches/bleeding_edge/src/runtime.h Tue Nov 2 06:37:59 2010
+++ /branches/bleeding_edge/src/runtime.h Wed Nov 10 04:34:28 2010
@@ -175,7 +175,7 @@
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 */ \
=======================================
--- /branches/bleeding_edge/src/string.js Fri Nov 5 06:33:12 2010
+++ /branches/bleeding_edge/src/string.js Wed Nov 10 04:34:28 2010
@@ -552,7 +552,7 @@
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);
=======================================
--- /branches/bleeding_edge/test/mjsunit/string-split.js Thu Nov 4
03:24:17 2010
+++ /branches/bleeding_edge/test/mjsunit/string-split.js Wed Nov 10
04:34:28 2010
@@ -97,3 +97,22 @@
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)));
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev