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

Side by Side 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: remove ToObject 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 unified diff | 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 »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 (function(global, utils) {
6
7 %CheckIsBootstrapping();
8
9 // -------------------------------------------------------------------
10 // Imports
11
12 var GlobalString = global.String;
13 var MakeTypeError;
14
15 utils.Import(function(from) {
16 MakeTypeError = from.MakeTypeError;
17 });
18
19 // -------------------------------------------------------------------
20 // http://tc39.github.io/proposal-string-pad-start-end/
21
22 function StringPad(thisString, maxLength, fillString) {
23 maxLength = TO_LENGTH(maxLength);
24 var stringLength = thisString.length;
25
26 if (maxLength <= stringLength) return "";
27
28 if (IS_UNDEFINED(fillString)) {
29 fillString = " ";
30 } else {
31 fillString = TO_STRING(fillString);
32 if (fillString === "") {
33 fillString = " ";
34 }
35 }
36
37 var fillLength = maxLength - stringLength;
38 var repetitions = (fillLength / fillString.length) | 0;
39 var remainingChars = (fillLength - fillString.length * repetitions) | 0;
40
41 var filler = "";
42 while (true) {
43 if (repetitions & 1) filler += fillString;
44 repetitions >>= 1;
45 if (repetitions === 0) break;
46 fillString += fillString;
47 }
48
49 if (remainingChars) {
50 filler += %_SubString(fillString, 0, remainingChars);
51 }
52
53 return filler;
54 }
55
56 function StringPadStart(maxLength, fillString) {
57 CHECK_OBJECT_COERCIBLE(this, "String.prototype.padStart")
58 var thisString = TO_STRING(this);
59
60 return StringPad(thisString, maxLength, fillString) + thisString;
61 }
62 %FunctionSetLength(StringPadStart, 1);
63
64 function StringPadEnd(maxLength, fillString) {
65 CHECK_OBJECT_COERCIBLE(this, "String.prototype.padEnd")
66 var thisString = TO_STRING(this);
67
68 return thisString + StringPad(thisString, maxLength, fillString);
69 }
70 %FunctionSetLength(StringPadEnd, 1);
71
72 utils.InstallFunctions(GlobalString.prototype, DONT_ENUM, [
73 "padStart", StringPadStart,
74 "padEnd", StringPadEnd
75 ]);
76
77 });
OLDNEW
« 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