Reviewers: Jakob,
Description:
Improve String.repeat.
Adapted from patch contributed by Isiah Meadows <[email protected]>.
[email protected]
Please review this at https://codereview.chromium.org/657863002/
Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Affected files (+13, -6 lines):
M AUTHORS
M src/harmony-string.js
M test/mjsunit/harmony/string-repeat.js
Index: AUTHORS
diff --git a/AUTHORS b/AUTHORS
index
d406a6c2326f7f83eef3801e0d68391b9644fa3d..89caae648c628639afcf6323df2e1b5336bdc3f5
100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -39,6 +39,7 @@ Fedor Indutny <[email protected]>
Filipe David Manana <[email protected]>
Haitao Feng <[email protected]>
Ioseb Dzmanashvili <[email protected]>
+Isiah Meadows <[email protected]>
Jacob Bramley <[email protected]>
Jan de Mooij <[email protected]>
Jay Freeman <[email protected]>
Index: src/harmony-string.js
diff --git a/src/harmony-string.js b/src/harmony-string.js
index
ae13745cdbf55989ad78b288843396ee5fab36e9..e9b93d86a4f900503d3cae6fc8a8ed9575352ecd
100644
--- a/src/harmony-string.js
+++ b/src/harmony-string.js
@@ -17,16 +17,19 @@ function StringRepeat(count) {
var s = TO_STRING_INLINE(this);
var n = ToInteger(count);
- if (n < 0 || !NUMBER_IS_FINITE(n)) {
+ // The maximum string length is stored in a smi, so a longer repeat
+ // must result in a range error.
+ if (n < 0 || n > %_MaxSmi()) {
throw MakeRangeError("invalid_count_value", []);
}
- var elements = new InternalArray(n);
- for (var i = 0; i < n; i++) {
- elements[i] = s;
+ var r = "";
+ while (n > 0) {
+ if (n & 1) r += s;
+ n >>= 1;
+ s += s;
}
-
- return %StringBuilderConcat(elements, n, "");
+ return r;
}
Index: test/mjsunit/harmony/string-repeat.js
diff --git a/test/mjsunit/harmony/string-repeat.js
b/test/mjsunit/harmony/string-repeat.js
index
761089b1d03a45ef91c0dcf6aad8cee7a0f1095d..0af74483a0bbe58afd7aa25322eb3cec890ab4da
100644
--- a/test/mjsunit/harmony/string-repeat.js
+++ b/test/mjsunit/harmony/string-repeat.js
@@ -61,8 +61,11 @@ assertEquals("", "".repeat(5));
assertEquals("", "abc".repeat(0));
assertEquals("abcabc", "abc".repeat(2.0));
+assertEquals("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "a".repeat(37));
assertThrows('"a".repeat(-1)', RangeError);
assertThrows('"a".repeat(Number.POSITIVE_INFINITY)', RangeError);
+assertThrows('"a".repeat(Math.pow(2, 30))', RangeError);
+assertThrows('"a".repeat(Math.pow(2, 40))', RangeError);
var myobj = {
toString: function() {
--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
---
You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.