summaryrefslogtreecommitdiff
path: root/deps/v8/src/js/harmony-string-padding.js
blob: 1af2359def000d7c1f688c28eafd86777a8ed554 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// 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;

// -------------------------------------------------------------------
// http://tc39.github.io/proposal-string-pad-start-end/

function StringPad(thisString, maxLength, fillString) {
  maxLength = TO_LENGTH(maxLength);
  var stringLength = thisString.length;

  if (maxLength <= stringLength) return "";

  if (IS_UNDEFINED(fillString)) {
    fillString = " ";
  } else {
    fillString = TO_STRING(fillString);
    if (fillString === "") {
      // If filler is the empty String, return S.
      return "";
    }
  }

  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);
  }

  return filler;
}

function StringPadStart(maxLength, fillString) {
  CHECK_OBJECT_COERCIBLE(this, "String.prototype.padStart")
  var thisString = TO_STRING(this);

  return StringPad(thisString, maxLength, fillString) + thisString;
}
%FunctionSetLength(StringPadStart, 1);

function StringPadEnd(maxLength, fillString) {
  CHECK_OBJECT_COERCIBLE(this, "String.prototype.padEnd")
  var thisString = TO_STRING(this);

  return thisString + StringPad(thisString, maxLength, fillString);
}
%FunctionSetLength(StringPadEnd, 1);

utils.InstallFunctions(GlobalString.prototype, DONT_ENUM, [
  "padStart", StringPadStart,
  "padEnd", StringPadEnd
]);

});