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

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: 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 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 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
23 CHECK_OBJECT_COERCIBLE(this, "String.prototype.NAME")
24
25 var S = TO_STRING(TO_OBJECT(this));
26 MAXLENGTH = TO_LENGTH(maxLength);
27 var stringLength = S.length;
28
29 if (MAXLENGTH <= stringLength) return S;
30
31 if (IS_UNDEFINED(FILLSTRING)) {
32 FILLSTRING = " ";
33 } else {
34 FILLSTRING = TO_STRING(FILLSTRING);
35 if (FILLSTRING === "") {
36 FILLSTRING = " ";
37 }
38 }
39
40 var fillLength = MAXLENGTH - stringLength;
41
42 var repetitions = (fillLength / fillString.length) | 0;
43 var remainingChars = (fillLength - fillString.length * repetitions) | 0;
44
45 var FILLER = "";
46 while (true) {
47 if (repetitions & 1) FILLER += FILLSTRING;
48 repetitions >>= 1;
49 if (repetitions === 0) break;
50 FILLSTRING += FILLSTRING;
51 }
52
53 if (remainingChars) {
54 FILLER += %_SubString(FILLSTRING, 0, remainingChars);
55 }
56 endmacro
57
58 function StringPadStart(maxLength, fillString) {
59 STRING_PAD_COMMON(padStart, maxLength, fillString, S, filler)
60 return filler + S;
61 }
62 %FunctionSetLength(StringPadStart, 1);
63
64 function StringPadEnd(maxLength, fillString) {
65 STRING_PAD_COMMON(padEnd, maxLength, fillString, S, filler)
66 return S + filler;
67 }
68 %FunctionSetLength(StringPadEnd, 1);
69
70 utils.InstallFunctions(GlobalString.prototype, DONT_ENUM, [
71 "padStart", StringPadStart,
72 "padEnd", StringPadEnd
73 ]);
74
75 });
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