1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 // This file relies on the fact that the following declaration has been made
32 // var $String = global.String;
33 // var $Array = global.Array;
35 // -------------------------------------------------------------------
37 // ES6 draft 07-15-13, section 15.5.3.21
38 function StringRepeat(count) {
39 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) {
40 throw MakeTypeError("called_on_null_or_undefined",
41 ["String.prototype.repeat"]);
44 var s = TO_STRING_INLINE(this);
45 var n = ToInteger(count);
46 if (n < 0 || !NUMBER_IS_FINITE(n)) {
47 throw MakeRangeError("invalid_count_value", []);
50 var elements = new InternalArray(n);
51 for (var i = 0; i < n; i++) {
55 return %StringBuilderConcat(elements, n, "");
59 // ES6 draft 07-15-13, section 15.5.3.22
60 function StringStartsWith(searchString /* position */) { // length == 1
61 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) {
62 throw MakeTypeError("called_on_null_or_undefined",
63 ["String.prototype.startsWith"]);
66 var s = TO_STRING_INLINE(this);
67 var ss = TO_STRING_INLINE(searchString);
69 if (%_ArgumentsLength() > 1) {
70 pos = %_Arguments(1); // position
75 var start = MathMin(MathMax(pos, 0), s_len);
76 var ss_len = ss.length;
77 if (ss_len + start > s_len) {
81 return %StringIndexOf(s, ss, start) === start;
85 // ES6 draft 07-15-13, section 15.5.3.23
86 function StringEndsWith(searchString /* position */) { // length == 1
87 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) {
88 throw MakeTypeError("called_on_null_or_undefined",
89 ["String.prototype.endsWith"]);
92 var s = TO_STRING_INLINE(this);
93 var ss = TO_STRING_INLINE(searchString);
96 if (%_ArgumentsLength() > 1) {
97 var arg = %_Arguments(1); // position
98 if (!IS_UNDEFINED(arg)) {
103 var end = MathMin(MathMax(pos, 0), s_len);
104 var ss_len = ss.length;
105 var start = end - ss_len;
110 return %StringLastIndexOf(s, ss, start) === start;
114 // ES6 draft 07-15-13, section 15.5.3.24
115 function StringContains(searchString /* position */) { // length == 1
116 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) {
117 throw MakeTypeError("called_on_null_or_undefined",
118 ["String.prototype.contains"]);
121 var s = TO_STRING_INLINE(this);
122 var ss = TO_STRING_INLINE(searchString);
124 if (%_ArgumentsLength() > 1) {
125 pos = %_Arguments(1); // position
126 pos = ToInteger(pos);
129 var s_len = s.length;
130 var start = MathMin(MathMax(pos, 0), s_len);
131 var ss_len = ss.length;
132 if (ss_len + start > s_len) {
136 return %StringIndexOf(s, ss, start) !== -1;
140 // -------------------------------------------------------------------
142 function ExtendStringPrototype() {
143 %CheckIsBootstrapping();
145 // Set up the non-enumerable functions on the String prototype object.
146 InstallFunctions($String.prototype, DONT_ENUM, $Array(
147 "repeat", StringRepeat,
148 "startsWith", StringStartsWith,
149 "endsWith", StringEndsWith,
150 "contains", StringContains
154 ExtendStringPrototype();