OLD | NEW |
---|---|
(Empty) | |
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 | |
4 // met: | |
5 // | |
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. | |
15 // | |
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. | |
27 | |
28 'use strict'; | |
29 | |
30 // This file relies on the fact that the following declaration has been made | |
31 // in runtime.js: | |
32 // var $String = global.String; | |
33 // var $Array = global.Array; | |
34 | |
Michael Starzinger
2013/07/30 10:58:39
nit: Please add the separation marker as present i
| |
35 // ES6 draft 07-15-13, section 15.5.3.21 | |
36 function StringRepeat(count) { | |
37 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { | |
38 throw MakeTypeError("called_on_null_or_undefined", | |
39 ["String.prototype.repeat"]); | |
40 } | |
41 | |
42 var s = TO_STRING_INLINE(this); | |
43 var n = ToInteger(count); | |
44 if (n < 0 || !NUMBER_IS_FINITE(n)) { | |
45 throw MakeRangeError("invalid_count_value", []); | |
46 } | |
47 | |
48 var elements = new InternalArray(n); | |
49 var i; | |
Michael Starzinger
2013/07/30 10:58:39
nit: Please move the variable declaration of "i" i
| |
50 for (i = 0; i < n; i++) { | |
51 elements[i] = s; | |
52 } | |
53 | |
54 return %StringBuilderConcat(elements, n, ""); | |
55 } | |
56 | |
57 | |
58 // ES6 draft 07-15-13, section 15.5.3.22 | |
59 function StringStartsWith(searchString /* position */) { // length == 1 | |
60 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { | |
61 throw MakeTypeError("called_on_null_or_undefined", | |
62 ["String.prototype.startsWith"]); | |
63 } | |
64 | |
65 var s = TO_STRING_INLINE(this); | |
66 var ss = TO_STRING_INLINE(searchString); | |
67 var pos = 0; | |
68 if (%_ArgumentsLength() > 1) { | |
69 pos = %_Arguments(1); // position | |
70 pos = ToInteger(pos); | |
71 } | |
72 | |
73 var s_len = s.length; | |
74 var start = MathMin(MathMax(pos, 0), s_len); | |
75 var ss_len = ss.length; | |
76 if (ss_len + start > s_len) { | |
77 return false; | |
78 } | |
79 | |
80 return %StringIndexOf(s, ss, start) === start; | |
81 } | |
82 | |
83 | |
84 // ES6 draft 07-15-13, section 15.5.3.23 | |
85 function StringEndsWith(searchString /* position */) { // length == 1 | |
86 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { | |
87 throw MakeTypeError("called_on_null_or_undefined", | |
88 ["String.prototype.endsWith"]); | |
89 } | |
90 | |
91 var s = TO_STRING_INLINE(this); | |
92 var ss = TO_STRING_INLINE(searchString); | |
93 var s_len = s.length; | |
94 var pos = s_len; | |
95 if (%_ArgumentsLength() > 1) { | |
96 var arg = %_Arguments(1); // position | |
97 if (!IS_UNDEFINED(arg)) { | |
98 pos = ToInteger(arg); | |
99 } | |
100 } | |
101 | |
102 var end = MathMin(MathMax(pos, 0), s_len); | |
103 var ss_len = ss.length; | |
104 var start = end - ss_len; | |
105 if (start < 0) { | |
106 return false; | |
107 } | |
108 | |
109 return %StringLastIndexOf(s, ss, start) === end - ss_len; | |
Michael Starzinger
2013/07/30 10:58:39
nit: s/end - ss_len/start/ here, makes it easier t
| |
110 } | |
111 | |
112 | |
113 // ES6 draft 07-15-13, section 15.5.3.24 | |
114 function StringContains(searchString /* position */) { // length == 1 | |
115 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { | |
116 throw MakeTypeError("called_on_null_or_undefined", | |
117 ["String.prototype.contains"]); | |
118 } | |
119 | |
120 var s = TO_STRING_INLINE(this); | |
121 var ss = TO_STRING_INLINE(searchString); | |
122 var pos = 0; | |
123 if (%_ArgumentsLength() > 1) { | |
124 pos = %_Arguments(1); // position | |
125 pos = ToInteger(pos); | |
126 } | |
127 | |
128 var s_len = s.length; | |
129 var start = MathMin(MathMax(pos, 0), s_len); | |
130 var ss_len = ss.length; | |
131 if (ss_len + start > s_len) { | |
132 return false; | |
133 } | |
134 | |
135 return %StringIndexOf(s, ss, start) !== -1; | |
136 } | |
137 | |
138 | |
Michael Starzinger
2013/07/30 10:58:39
nit: Please add the separation marker as present i
| |
139 function ExtendStringPrototype() { | |
140 %CheckIsBootstrapping(); | |
141 | |
142 // Set up the non-enumerable functions on the String prototype object. | |
143 InstallFunctions($String.prototype, DONT_ENUM, $Array( | |
144 "repeat", StringRepeat, | |
145 "startsWith", StringStartsWith, | |
146 "endsWith", StringEndsWith, | |
147 "contains", StringContains | |
148 )); | |
149 } | |
150 | |
151 ExtendStringPrototype(); | |
OLD | NEW |