OLD | NEW |
---|---|
(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 StringPadCommon(self, maxLength, fillString, fillerAtStart) { | |
Dan Ehrenberg
2016/03/23 23:34:24
Nit: Maybe call this StringPad or StringPadding?
caitp (gmail)
2016/03/23 23:36:34
Acknowledged.
| |
23 var S = TO_STRING(TO_OBJECT(self)); | |
Dan Ehrenberg
2016/03/23 23:34:24
Nit: we tend to use lower-case names, like string
caitp (gmail)
2016/03/23 23:36:34
Acknowledged.
| |
24 | |
25 maxLength = TO_LENGTH(maxLength); | |
26 var stringLength = S.length; | |
27 | |
28 if (maxLength <= stringLength) return S; | |
29 | |
30 if (IS_UNDEFINED(fillString)) { | |
31 fillString = " "; | |
32 } else { | |
33 fillString = TO_STRING(fillString); | |
34 if (fillString === "") { | |
35 fillString = " "; | |
36 } | |
37 } | |
38 | |
39 var fillLength = maxLength - stringLength; | |
40 var repetitions = (fillLength / fillString.length) | 0; | |
41 var remainingChars = (fillLength - fillString.length * repetitions) | 0; | |
42 | |
43 var filler = ""; | |
44 while (true) { | |
45 if (repetitions & 1) filler += fillString; | |
46 repetitions >>= 1; | |
47 if (repetitions === 0) break; | |
48 fillString += fillString; | |
49 } | |
50 | |
51 if (remainingChars) { | |
52 filler += %_SubString(fillString, 0, remainingChars); | |
53 } | |
54 | |
55 if (fillerAtStart) return filler + S; | |
56 else return S + filler; | |
Dan Ehrenberg
2016/03/23 23:34:24
Sorry, one last comment: What if you just had this
caitp (gmail)
2016/03/23 23:36:34
This presents a problem in the case on line 28, be
caitp (gmail)
2016/03/23 23:37:34
er, it would append the original string and the or
| |
57 } | |
58 | |
59 function StringPadStart(maxLength, fillString) { | |
60 CHECK_OBJECT_COERCIBLE(this, "String.prototype.padStart") | |
61 var S = TO_STRING(TO_OBJECT(this)); | |
62 | |
63 return StringPadCommon(this, maxLength, fillString, true); | |
64 } | |
65 %FunctionSetLength(StringPadStart, 1); | |
66 | |
67 function StringPadEnd(maxLength, fillString) { | |
68 CHECK_OBJECT_COERCIBLE(this, "String.prototype.padEnd") | |
69 | |
70 return StringPadCommon(this, maxLength, fillString, false); | |
71 } | |
72 %FunctionSetLength(StringPadEnd, 1); | |
73 | |
74 utils.InstallFunctions(GlobalString.prototype, DONT_ENUM, [ | |
75 "padStart", StringPadStart, | |
76 "padEnd", StringPadEnd | |
77 ]); | |
78 | |
79 }); | |
OLD | NEW |