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 12var GlobalString = global.String; 13var MakeTypeError; 14 15utils.Import(function(from) { 16 MakeTypeError = from.MakeTypeError; 17}); 18 19// ------------------------------------------------------------------- 20// http://tc39.github.io/proposal-string-pad-start-end/ 21 22function 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 // If filler is the empty String, return S. 34 return ""; 35 } 36 } 37 38 var fillLength = maxLength - stringLength; 39 var repetitions = (fillLength / fillString.length) | 0; 40 var remainingChars = (fillLength - fillString.length * repetitions) | 0; 41 42 var filler = ""; 43 while (true) { 44 if (repetitions & 1) filler += fillString; 45 repetitions >>= 1; 46 if (repetitions === 0) break; 47 fillString += fillString; 48 } 49 50 if (remainingChars) { 51 filler += %_SubString(fillString, 0, remainingChars); 52 } 53 54 return filler; 55} 56 57function StringPadStart(maxLength, fillString) { 58 CHECK_OBJECT_COERCIBLE(this, "String.prototype.padStart") 59 var thisString = TO_STRING(this); 60 61 return StringPad(thisString, maxLength, fillString) + thisString; 62} 63%FunctionSetLength(StringPadStart, 1); 64 65function StringPadEnd(maxLength, fillString) { 66 CHECK_OBJECT_COERCIBLE(this, "String.prototype.padEnd") 67 var thisString = TO_STRING(this); 68 69 return thisString + StringPad(thisString, maxLength, fillString); 70} 71%FunctionSetLength(StringPadEnd, 1); 72 73utils.InstallFunctions(GlobalString.prototype, DONT_ENUM, [ 74 "padStart", StringPadStart, 75 "padEnd", StringPadEnd 76]); 77 78}); 79