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

Unified Diff: src/string.js

Issue 20818005: Implemented String.prototype.repeat as per ES6 draft 07-15-13, section 15.5.3.21 (Closed) Base URL: git://github.com/v8/v8.git@master
Patch Set: Created 7 years, 5 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 | « src/messages.js ('k') | test/mjsunit/string-repeat.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/string.js
diff --git a/src/string.js b/src/string.js
index cb82c166346fc7ba5491a1e9e5d28e0e9b5a5108..96d68e249fe5749fa8ef2500e5694555c1122340 100644
--- a/src/string.js
+++ b/src/string.js
@@ -882,6 +882,43 @@ function StringFromCharCode(code) {
}
+// ES6 draft 07-15-13, section 15.5.3.21
+function StringRepeat(count) {
+ if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) {
+ throw MakeTypeError("called_on_null_or_undefined",
+ ["String.prototype.repeat"]);
+ }
+
Sven Panne 2013/07/29 13:27:53 We have to call TO_STRING_INLINE here, not later,
+ if (IS_NULL_OR_UNDEFINED(count) && !IS_UNDETECTABLE(count)) {
Sven Panne 2013/07/29 13:27:53 Why test this? What should really happen at that p
+ throw MakeTypeError("called_on_null_or_undefined",
+ ["String.prototype.repeat"]);
+ }
+
+ var s = TO_STRING_INLINE(this);
+ if (s.length === 0) {
Sven Panne 2013/07/29 13:27:53 This optimization is not allowed by the spec, at l
+ return s;
+ }
+
+ var n = ToNumber(count);
+ if (NUMBER_IS_NAN(n) || n < 0 || !NUMBER_IS_FINITE(n)) {
+ throw MakeRangeError("invalid_count_value", []);
+ }
+
+ n = TO_INTEGER(n);
+ if (n === 0) {
Sven Panne 2013/07/29 13:27:53 I am not sure if a zero count is a common case, we
+ return "";
+ }
+
+ var elements = new InternalArray(n);
+ var i = 0;
Sven Panne 2013/07/29 13:27:53 Style nit: Use a for loop for readability.
+ while (i < n) {
+ elements[i++] = s;
+ }
+
+ return %StringBuilderConcat(elements, n, "");
+}
+
+
// Helper function for very basic XSS protection.
function HtmlEscape(str) {
return TO_STRING_INLINE(str).replace(/</g, "&lt;")
@@ -1010,7 +1047,8 @@ function SetUpString() {
"small", StringSmall,
"strike", StringStrike,
"sub", StringSub,
- "sup", StringSup
+ "sup", StringSup,
+ "repeat", StringRepeat
));
}
« no previous file with comments | « src/messages.js ('k') | test/mjsunit/string-repeat.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698