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

Unified Diff: src/js/harmony-string-padding.js

Issue 1700003002: [esnext] implement String padding proposal (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Rebase on top of another 1000 commits Created 4 years, 9 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/flag-definitions.h ('k') | test/mjsunit/harmony/harmony-string-pad-end.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/js/harmony-string-padding.js
diff --git a/src/js/harmony-string-padding.js b/src/js/harmony-string-padding.js
new file mode 100644
index 0000000000000000000000000000000000000000..97b76981802f202535e5217438e5e1a71ad0f6ad
--- /dev/null
+++ b/src/js/harmony-string-padding.js
@@ -0,0 +1,75 @@
+// Copyright 2016 the V8 project authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+(function(global, utils) {
+
+%CheckIsBootstrapping();
+
+// -------------------------------------------------------------------
+// Imports
+
+var GlobalString = global.String;
+var MakeTypeError;
+
+utils.Import(function(from) {
+ MakeTypeError = from.MakeTypeError;
+});
+
+// -------------------------------------------------------------------
+// http://tc39.github.io/proposal-string-pad-start-end/
+
+macro STRING_PAD_COMMON(NAME, MAXLENGTH, FILLSTRING, S, FILLER)
Dan Ehrenberg 2016/03/23 15:59:50 Can you use a function rather than a macro here?
caitp (gmail) 2016/03/23 17:00:41 I've made a function for the "repeat the filler" s
Dan Ehrenberg 2016/03/23 17:30:17 I think there could be a middle point here, which
caitp (gmail) 2016/03/23 20:45:45 done
+ CHECK_OBJECT_COERCIBLE(this, "String.prototype.NAME")
+
+ var S = TO_STRING(TO_OBJECT(this));
+ MAXLENGTH = TO_LENGTH(maxLength);
+ var stringLength = S.length;
+
+ if (MAXLENGTH <= stringLength) return S;
+
+ if (IS_UNDEFINED(FILLSTRING)) {
+ FILLSTRING = " ";
+ } else {
+ FILLSTRING = TO_STRING(FILLSTRING);
+ if (FILLSTRING === "") {
+ FILLSTRING = " ";
+ }
+ }
+
+ var fillLength = MAXLENGTH - stringLength;
+
+ var repetitions = (fillLength / fillString.length) | 0;
+ var remainingChars = (fillLength - fillString.length * repetitions) | 0;
+
+ var FILLER = "";
+ while (true) {
+ if (repetitions & 1) FILLER += FILLSTRING;
+ repetitions >>= 1;
+ if (repetitions === 0) break;
+ FILLSTRING += FILLSTRING;
+ }
+
+ if (remainingChars) {
+ FILLER += %_SubString(FILLSTRING, 0, remainingChars);
+ }
+endmacro
+
+function StringPadStart(maxLength, fillString) {
+ STRING_PAD_COMMON(padStart, maxLength, fillString, S, filler)
+ return filler + S;
+}
+%FunctionSetLength(StringPadStart, 1);
+
+function StringPadEnd(maxLength, fillString) {
+ STRING_PAD_COMMON(padEnd, maxLength, fillString, S, filler)
+ return S + filler;
+}
+%FunctionSetLength(StringPadEnd, 1);
+
+utils.InstallFunctions(GlobalString.prototype, DONT_ENUM, [
+ "padStart", StringPadStart,
+ "padEnd", StringPadEnd
+]);
+
+});
« no previous file with comments | « src/flag-definitions.h ('k') | test/mjsunit/harmony/harmony-string-pad-end.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698