Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(568)

Unified Diff: src/harmony-string.js

Issue 657863002: Improve String.repeat. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: addressed comment Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « AUTHORS ('k') | test/mjsunit/harmony/string-repeat.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/harmony-string.js
diff --git a/src/harmony-string.js b/src/harmony-string.js
index ae13745cdbf55989ad78b288843396ee5fab36e9..1c477e2fa5c996fc279a3b8bac500286da8eb9c0 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 (true) {
+ if (n & 1) r += s;
+ n >>= 1;
+ if (n === 0) return r;
+ s += s;
}
-
- return %StringBuilderConcat(elements, n, "");
}
« no previous file with comments | « AUTHORS ('k') | test/mjsunit/harmony/string-repeat.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698