forked from v8/v8
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.js
More file actions
151 lines (118 loc) · 3.97 KB
/
string.js
File metadata and controls
151 lines (118 loc) · 3.97 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright 2012 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) {
"use strict";
%CheckIsBootstrapping();
// -------------------------------------------------------------------
// Imports
var GlobalString = global.String;
var matchSymbol = utils.ImportNow("match_symbol");
var searchSymbol = utils.ImportNow("search_symbol");
//-------------------------------------------------------------------
// Set up the non-enumerable functions on the String prototype object.
DEFINE_METHODS(
GlobalString.prototype,
{
/* ES#sec-string.prototype.match */
match(pattern) {
CHECK_OBJECT_COERCIBLE(this, "String.prototype.match");
if (!IS_NULL_OR_UNDEFINED(pattern)) {
var matcher = pattern[matchSymbol];
if (!IS_UNDEFINED(matcher)) {
return %_Call(matcher, pattern, this);
}
}
var subject = TO_STRING(this);
// Equivalent to RegExpCreate (ES#sec-regexpcreate)
var regexp = %RegExpCreate(pattern);
return regexp[matchSymbol](subject);
}
/* ES#sec-string.prototype.search */
search(pattern) {
CHECK_OBJECT_COERCIBLE(this, "String.prototype.search");
if (!IS_NULL_OR_UNDEFINED(pattern)) {
var searcher = pattern[searchSymbol];
if (!IS_UNDEFINED(searcher)) {
return %_Call(searcher, pattern, this);
}
}
var subject = TO_STRING(this);
// Equivalent to RegExpCreate (ES#sec-regexpcreate)
var regexp = %RegExpCreate(pattern);
return %_Call(regexp[searchSymbol], regexp, subject);
}
}
);
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;
}
DEFINE_METHODS_LEN(
GlobalString.prototype,
{
/* ES#sec-string.prototype.padstart */
/* String.prototype.padStart(maxLength [, fillString]) */
padStart(maxLength, fillString) {
CHECK_OBJECT_COERCIBLE(this, "String.prototype.padStart");
var thisString = TO_STRING(this);
return StringPad(thisString, maxLength, fillString) + thisString;
}
/* ES#sec-string.prototype.padend */
/* String.prototype.padEnd(maxLength [, fillString]) */
padEnd(maxLength, fillString) {
CHECK_OBJECT_COERCIBLE(this, "String.prototype.padEnd");
var thisString = TO_STRING(this);
return thisString + StringPad(thisString, maxLength, fillString);
}
},
1 /* Set functions length */
);
// -------------------------------------------------------------------
// String methods related to templates
// Set up the non-enumerable functions on the String object.
DEFINE_METHOD(
GlobalString,
/* ES#sec-string.raw */
raw(callSite) {
var numberOfSubstitutions = arguments.length;
var cooked = TO_OBJECT(callSite);
var raw = TO_OBJECT(cooked.raw);
var literalSegments = TO_LENGTH(raw.length);
if (literalSegments <= 0) return "";
var result = TO_STRING(raw[0]);
for (var i = 1; i < literalSegments; ++i) {
if (i < numberOfSubstitutions) {
result += TO_STRING(arguments[i]);
}
result += TO_STRING(raw[i]);
}
return result;
}
);
// -------------------------------------------------------------------
})