Chromium Code Reviews| 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..f76f0fefddaa642fa7bc74e866f861a68686c4b1 |
| --- /dev/null |
| +++ b/src/js/harmony-string-padding.js |
| @@ -0,0 +1,79 @@ |
| +// 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/ |
| + |
| +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.
|
| + 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.
|
| + |
| + 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); |
| + } |
| + |
| + if (fillerAtStart) return filler + S; |
| + 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
|
| +} |
| + |
| +function StringPadStart(maxLength, fillString) { |
| + CHECK_OBJECT_COERCIBLE(this, "String.prototype.padStart") |
| + var S = TO_STRING(TO_OBJECT(this)); |
| + |
| + return StringPadCommon(this, maxLength, fillString, true); |
| +} |
| +%FunctionSetLength(StringPadStart, 1); |
| + |
| +function StringPadEnd(maxLength, fillString) { |
| + CHECK_OBJECT_COERCIBLE(this, "String.prototype.padEnd") |
| + |
| + return StringPadCommon(this, maxLength, fillString, false); |
| +} |
| +%FunctionSetLength(StringPadEnd, 1); |
| + |
| +utils.InstallFunctions(GlobalString.prototype, DONT_ENUM, [ |
| + "padStart", StringPadStart, |
| + "padEnd", StringPadEnd |
| +]); |
| + |
| +}); |