Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1 | // Copyright 2006-2009 the V8 project authors. All rights reserved. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 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 | |
| 29 | // This file relies on the fact that the following declaration has been made |
| 30 | // in runtime.js: |
| 31 | // const $String = global.String; |
| 32 | // const $NaN = 0/0; |
| 33 | |
| 34 | |
| 35 | // Set the String function and constructor. |
| 36 | %SetCode($String, function(x) { |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 37 | var value = %_ArgumentsLength() == 0 ? '' : TO_STRING_INLINE(x); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 38 | if (%_IsConstructCall()) { |
| 39 | %_SetValueOf(this, value); |
| 40 | } else { |
| 41 | return value; |
| 42 | } |
| 43 | }); |
| 44 | |
| 45 | %FunctionSetPrototype($String, new $String()); |
| 46 | |
| 47 | // ECMA-262 section 15.5.4.2 |
| 48 | function StringToString() { |
| 49 | if (!IS_STRING(this) && !IS_STRING_WRAPPER(this)) |
| 50 | throw new $TypeError('String.prototype.toString is not generic'); |
| 51 | return %_ValueOf(this); |
| 52 | } |
| 53 | |
| 54 | |
| 55 | // ECMA-262 section 15.5.4.3 |
| 56 | function StringValueOf() { |
| 57 | if (!IS_STRING(this) && !IS_STRING_WRAPPER(this)) |
| 58 | throw new $TypeError('String.prototype.valueOf is not generic'); |
| 59 | return %_ValueOf(this); |
| 60 | } |
| 61 | |
| 62 | |
| 63 | // ECMA-262, section 15.5.4.4 |
| 64 | function StringCharAt(pos) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 65 | if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { |
| 66 | throw MakeTypeError("called_on_null_or_undefined", |
| 67 | ["String.prototype.charAt"]); |
| 68 | } |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 69 | var result = %_StringCharAt(this, pos); |
| 70 | if (%_IsSmi(result)) { |
| 71 | result = %_StringCharAt(TO_STRING_INLINE(this), TO_INTEGER(pos)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 72 | } |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 73 | return result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | |
| 77 | // ECMA-262 section 15.5.4.5 |
| 78 | function StringCharCodeAt(pos) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 79 | if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { |
| 80 | throw MakeTypeError("called_on_null_or_undefined", |
| 81 | ["String.prototype.charCodeAt"]); |
| 82 | } |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 83 | var result = %_StringCharCodeAt(this, pos); |
| 84 | if (!%_IsSmi(result)) { |
| 85 | result = %_StringCharCodeAt(TO_STRING_INLINE(this), TO_INTEGER(pos)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 86 | } |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 87 | return result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | |
| 91 | // ECMA-262, section 15.5.4.6 |
| 92 | function StringConcat() { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 93 | if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { |
| 94 | throw MakeTypeError("called_on_null_or_undefined", ["String.prototype.concat"]); |
| 95 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 96 | var len = %_ArgumentsLength(); |
| 97 | var this_as_string = TO_STRING_INLINE(this); |
| 98 | if (len === 1) { |
| 99 | return this_as_string + %_Arguments(0); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 100 | } |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 101 | var parts = new InternalArray(len + 1); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 102 | parts[0] = this_as_string; |
| 103 | for (var i = 0; i < len; i++) { |
| 104 | var part = %_Arguments(i); |
| 105 | parts[i + 1] = TO_STRING_INLINE(part); |
| 106 | } |
| 107 | return %StringBuilderConcat(parts, len + 1, ""); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | // Match ES3 and Safari |
| 111 | %FunctionSetLength(StringConcat, 1); |
| 112 | |
| 113 | |
| 114 | // ECMA-262 section 15.5.4.7 |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 115 | function StringIndexOf(pattern /* position */) { // length == 1 |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 116 | if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { |
| 117 | throw MakeTypeError("called_on_null_or_undefined", |
| 118 | ["String.prototype.indexOf"]); |
| 119 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 120 | var subject = TO_STRING_INLINE(this); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 121 | pattern = TO_STRING_INLINE(pattern); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 122 | var index = 0; |
| 123 | if (%_ArgumentsLength() > 1) { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 124 | index = %_Arguments(1); // position |
| 125 | index = TO_INTEGER(index); |
| 126 | if (index < 0) index = 0; |
| 127 | if (index > subject.length) index = subject.length; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 128 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 129 | return %StringIndexOf(subject, pattern, index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | |
| 133 | // ECMA-262 section 15.5.4.8 |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 134 | function StringLastIndexOf(pat /* position */) { // length == 1 |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 135 | if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { |
| 136 | throw MakeTypeError("called_on_null_or_undefined", |
| 137 | ["String.prototype.lastIndexOf"]); |
| 138 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 139 | var sub = TO_STRING_INLINE(this); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 140 | var subLength = sub.length; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 141 | var pat = TO_STRING_INLINE(pat); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 142 | var patLength = pat.length; |
| 143 | var index = subLength - patLength; |
| 144 | if (%_ArgumentsLength() > 1) { |
| 145 | var position = ToNumber(%_Arguments(1)); |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 146 | if (!NUMBER_IS_NAN(position)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 147 | position = TO_INTEGER(position); |
| 148 | if (position < 0) { |
| 149 | position = 0; |
| 150 | } |
| 151 | if (position + patLength < subLength) { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame^] | 152 | index = position; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | } |
| 156 | if (index < 0) { |
| 157 | return -1; |
| 158 | } |
| 159 | return %StringLastIndexOf(sub, pat, index); |
| 160 | } |
| 161 | |
| 162 | |
| 163 | // ECMA-262 section 15.5.4.9 |
| 164 | // |
| 165 | // This function is implementation specific. For now, we do not |
| 166 | // do anything locale specific. |
| 167 | function StringLocaleCompare(other) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 168 | if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { |
| 169 | throw MakeTypeError("called_on_null_or_undefined", |
| 170 | ["String.prototype.localeCompare"]); |
| 171 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 172 | if (%_ArgumentsLength() === 0) return 0; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame^] | 173 | return %StringLocaleCompare(TO_STRING_INLINE(this), |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 174 | TO_STRING_INLINE(other)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | |
| 178 | // ECMA-262 section 15.5.4.10 |
| 179 | function StringMatch(regexp) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 180 | if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { |
| 181 | throw MakeTypeError("called_on_null_or_undefined", |
| 182 | ["String.prototype.match"]); |
| 183 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 184 | var subject = TO_STRING_INLINE(this); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 185 | if (IS_REGEXP(regexp)) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 186 | if (!regexp.global) return RegExpExecNoTests(regexp, subject, 0); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 187 | %_Log('regexp', 'regexp-match,%0S,%1r', [subject, regexp]); |
| 188 | // lastMatchInfo is defined in regexp.js. |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 189 | return %StringMatch(subject, regexp, lastMatchInfo); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 190 | } |
| 191 | // Non-regexp argument. |
| 192 | regexp = new $RegExp(regexp); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 193 | return RegExpExecNoTests(regexp, subject, 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | |
| 197 | // SubString is an internal function that returns the sub string of 'string'. |
| 198 | // If resulting string is of length 1, we use the one character cache |
| 199 | // otherwise we call the runtime system. |
| 200 | function SubString(string, start, end) { |
| 201 | // Use the one character string cache. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 202 | if (start + 1 == end) return %_StringCharAt(string, start); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 203 | return %_SubString(string, start, end); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | |
| 207 | // This has the same size as the lastMatchInfo array, and can be used for |
| 208 | // functions that expect that structure to be returned. It is used when the |
| 209 | // needle is a string rather than a regexp. In this case we can't update |
| 210 | // lastMatchArray without erroneously affecting the properties on the global |
| 211 | // RegExp object. |
| 212 | var reusableMatchInfo = [2, "", "", -1, -1]; |
| 213 | |
| 214 | |
| 215 | // ECMA-262, section 15.5.4.11 |
| 216 | function StringReplace(search, replace) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 217 | if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { |
| 218 | throw MakeTypeError("called_on_null_or_undefined", |
| 219 | ["String.prototype.replace"]); |
| 220 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 221 | var subject = TO_STRING_INLINE(this); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 222 | |
| 223 | // Delegate to one of the regular expression variants if necessary. |
| 224 | if (IS_REGEXP(search)) { |
| 225 | %_Log('regexp', 'regexp-replace,%0r,%1S', [search, subject]); |
| 226 | if (IS_FUNCTION(replace)) { |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 227 | if (search.global) { |
| 228 | return StringReplaceGlobalRegExpWithFunction(subject, search, replace); |
| 229 | } else { |
| 230 | return StringReplaceNonGlobalRegExpWithFunction(subject, |
| 231 | search, |
| 232 | replace); |
| 233 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 234 | } else { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 235 | return %StringReplaceRegExpWithString(subject, |
| 236 | search, |
| 237 | TO_STRING_INLINE(replace), |
| 238 | lastMatchInfo); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | |
| 242 | // Convert the search argument to a string and search for it. |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 243 | search = TO_STRING_INLINE(search); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 244 | var start = %StringIndexOf(subject, search, 0); |
| 245 | if (start < 0) return subject; |
| 246 | var end = start + search.length; |
| 247 | |
| 248 | var builder = new ReplaceResultBuilder(subject); |
| 249 | // prefix |
| 250 | builder.addSpecialSlice(0, start); |
| 251 | |
| 252 | // Compute the string to replace with. |
| 253 | if (IS_FUNCTION(replace)) { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame^] | 254 | var receiver = %GetDefaultReceiver(replace); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 255 | builder.add(%_CallFunction(receiver, |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 256 | search, |
| 257 | start, |
| 258 | subject, |
| 259 | replace)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 260 | } else { |
| 261 | reusableMatchInfo[CAPTURE0] = start; |
| 262 | reusableMatchInfo[CAPTURE1] = end; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 263 | replace = TO_STRING_INLINE(replace); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 264 | ExpandReplacement(replace, subject, reusableMatchInfo, builder); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | // suffix |
| 268 | builder.addSpecialSlice(end, subject.length); |
| 269 | |
| 270 | return builder.generate(); |
| 271 | } |
| 272 | |
| 273 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 274 | // Expand the $-expressions in the string and return a new string with |
| 275 | // the result. |
| 276 | function ExpandReplacement(string, subject, matchInfo, builder) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 277 | var length = string.length; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 278 | var builder_elements = builder.elements; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 279 | var next = %StringIndexOf(string, '$', 0); |
| 280 | if (next < 0) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 281 | if (length > 0) builder_elements.push(string); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 282 | return; |
| 283 | } |
| 284 | |
| 285 | // Compute the number of captures; see ECMA-262, 15.5.4.11, p. 102. |
| 286 | var m = NUMBER_OF_CAPTURES(matchInfo) >> 1; // Includes the match. |
| 287 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 288 | if (next > 0) builder_elements.push(SubString(string, 0, next)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 289 | |
| 290 | while (true) { |
| 291 | var expansion = '$'; |
| 292 | var position = next + 1; |
| 293 | if (position < length) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 294 | var peek = %_StringCharCodeAt(string, position); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 295 | if (peek == 36) { // $$ |
| 296 | ++position; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 297 | builder_elements.push('$'); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 298 | } else if (peek == 38) { // $& - match |
| 299 | ++position; |
| 300 | builder.addSpecialSlice(matchInfo[CAPTURE0], |
| 301 | matchInfo[CAPTURE1]); |
| 302 | } else if (peek == 96) { // $` - prefix |
| 303 | ++position; |
| 304 | builder.addSpecialSlice(0, matchInfo[CAPTURE0]); |
| 305 | } else if (peek == 39) { // $' - suffix |
| 306 | ++position; |
| 307 | builder.addSpecialSlice(matchInfo[CAPTURE1], subject.length); |
| 308 | } else if (peek >= 48 && peek <= 57) { // $n, 0 <= n <= 9 |
| 309 | ++position; |
| 310 | var n = peek - 48; |
| 311 | if (position < length) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 312 | peek = %_StringCharCodeAt(string, position); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 313 | // $nn, 01 <= nn <= 99 |
| 314 | if (n != 0 && peek == 48 || peek >= 49 && peek <= 57) { |
| 315 | var nn = n * 10 + (peek - 48); |
| 316 | if (nn < m) { |
| 317 | // If the two digit capture reference is within range of |
| 318 | // the captures, we use it instead of the single digit |
| 319 | // one. Otherwise, we fall back to using the single |
| 320 | // digit reference. This matches the behavior of |
| 321 | // SpiderMonkey. |
| 322 | ++position; |
| 323 | n = nn; |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | if (0 < n && n < m) { |
| 328 | addCaptureString(builder, matchInfo, n); |
| 329 | } else { |
| 330 | // Because of the captures range check in the parsing of two |
| 331 | // digit capture references, we can only enter here when a |
| 332 | // single digit capture reference is outside the range of |
| 333 | // captures. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 334 | builder_elements.push('$'); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 335 | --position; |
| 336 | } |
| 337 | } else { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 338 | builder_elements.push('$'); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 339 | } |
| 340 | } else { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 341 | builder_elements.push('$'); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | // Go the the next $ in the string. |
| 345 | next = %StringIndexOf(string, '$', position); |
| 346 | |
| 347 | // Return if there are no more $ characters in the string. If we |
| 348 | // haven't reached the end, we need to append the suffix. |
| 349 | if (next < 0) { |
| 350 | if (position < length) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 351 | builder_elements.push(SubString(string, position, length)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 352 | } |
| 353 | return; |
| 354 | } |
| 355 | |
| 356 | // Append substring between the previous and the next $ character. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 357 | if (next > position) { |
| 358 | builder_elements.push(SubString(string, position, next)); |
| 359 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 360 | } |
| 361 | }; |
| 362 | |
| 363 | |
| 364 | // Compute the string of a given regular expression capture. |
| 365 | function CaptureString(string, lastCaptureInfo, index) { |
| 366 | // Scale the index. |
| 367 | var scaled = index << 1; |
| 368 | // Compute start and end. |
| 369 | var start = lastCaptureInfo[CAPTURE(scaled)]; |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 370 | // If start isn't valid, return undefined. |
| 371 | if (start < 0) return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 372 | var end = lastCaptureInfo[CAPTURE(scaled + 1)]; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 373 | return SubString(string, start, end); |
| 374 | }; |
| 375 | |
| 376 | |
| 377 | // Add the string of a given regular expression capture to the |
| 378 | // ReplaceResultBuilder |
| 379 | function addCaptureString(builder, matchInfo, index) { |
| 380 | // Scale the index. |
| 381 | var scaled = index << 1; |
| 382 | // Compute start and end. |
| 383 | var start = matchInfo[CAPTURE(scaled)]; |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 384 | if (start < 0) return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 385 | var end = matchInfo[CAPTURE(scaled + 1)]; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 386 | builder.addSpecialSlice(start, end); |
| 387 | }; |
| 388 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 389 | // TODO(lrn): This array will survive indefinitely if replace is never |
| 390 | // called again. However, it will be empty, since the contents are cleared |
| 391 | // in the finally block. |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 392 | var reusableReplaceArray = new InternalArray(16); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 393 | |
| 394 | // Helper function for replacing regular expressions with the result of a |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 395 | // function application in String.prototype.replace. |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 396 | function StringReplaceGlobalRegExpWithFunction(subject, regexp, replace) { |
| 397 | var resultArray = reusableReplaceArray; |
| 398 | if (resultArray) { |
| 399 | reusableReplaceArray = null; |
| 400 | } else { |
| 401 | // Inside a nested replace (replace called from the replacement function |
| 402 | // of another replace) or we have failed to set the reusable array |
| 403 | // back due to an exception in a replacement function. Create a new |
| 404 | // array to use in the future, or until the original is written back. |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 405 | resultArray = new InternalArray(16); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 406 | } |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 407 | var res = %RegExpExecMultiple(regexp, |
| 408 | subject, |
| 409 | lastMatchInfo, |
| 410 | resultArray); |
| 411 | regexp.lastIndex = 0; |
| 412 | if (IS_NULL(res)) { |
| 413 | // No matches at all. |
| 414 | reusableReplaceArray = resultArray; |
| 415 | return subject; |
| 416 | } |
| 417 | var len = res.length; |
| 418 | var i = 0; |
| 419 | if (NUMBER_OF_CAPTURES(lastMatchInfo) == 2) { |
| 420 | var match_start = 0; |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 421 | var override = new InternalArray(null, 0, subject); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame^] | 422 | var receiver = %GetDefaultReceiver(replace); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 423 | while (i < len) { |
| 424 | var elem = res[i]; |
| 425 | if (%_IsSmi(elem)) { |
| 426 | if (elem > 0) { |
| 427 | match_start = (elem >> 11) + (elem & 0x7ff); |
| 428 | } else { |
| 429 | match_start = res[++i] - elem; |
| 430 | } |
| 431 | } else { |
| 432 | override[0] = elem; |
| 433 | override[1] = match_start; |
| 434 | lastMatchInfoOverride = override; |
| 435 | var func_result = |
| 436 | %_CallFunction(receiver, elem, match_start, subject, replace); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 437 | res[i] = TO_STRING_INLINE(func_result); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 438 | match_start += elem.length; |
| 439 | } |
| 440 | i++; |
| 441 | } |
| 442 | } else { |
| 443 | while (i < len) { |
| 444 | var elem = res[i]; |
| 445 | if (!%_IsSmi(elem)) { |
| 446 | // elem must be an Array. |
| 447 | // Use the apply argument as backing for global RegExp properties. |
| 448 | lastMatchInfoOverride = elem; |
| 449 | var func_result = replace.apply(null, elem); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 450 | res[i] = TO_STRING_INLINE(func_result); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 451 | } |
| 452 | i++; |
| 453 | } |
| 454 | } |
| 455 | var resultBuilder = new ReplaceResultBuilder(subject, res); |
| 456 | var result = resultBuilder.generate(); |
| 457 | resultArray.length = 0; |
| 458 | reusableReplaceArray = resultArray; |
| 459 | return result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 463 | function StringReplaceNonGlobalRegExpWithFunction(subject, regexp, replace) { |
| 464 | var matchInfo = DoRegExpExec(regexp, subject, 0); |
| 465 | if (IS_NULL(matchInfo)) return subject; |
| 466 | var result = new ReplaceResultBuilder(subject); |
| 467 | var index = matchInfo[CAPTURE0]; |
| 468 | result.addSpecialSlice(0, index); |
| 469 | var endOfMatch = matchInfo[CAPTURE1]; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 470 | // Compute the parameter list consisting of the match, captures, index, |
| 471 | // and subject for the replace function invocation. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 472 | // The number of captures plus one for the match. |
| 473 | var m = NUMBER_OF_CAPTURES(matchInfo) >> 1; |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 474 | var replacement; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 475 | if (m == 1) { |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 476 | // No captures, only the match, which is always valid. |
| 477 | var s = SubString(subject, index, endOfMatch); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 478 | // Don't call directly to avoid exposing the built-in global object. |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame^] | 479 | var receiver = %GetDefaultReceiver(replace); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 480 | replacement = |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 481 | %_CallFunction(receiver, s, index, subject, replace); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 482 | } else { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 483 | var parameters = new InternalArray(m + 2); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 484 | for (var j = 0; j < m; j++) { |
| 485 | parameters[j] = CaptureString(subject, matchInfo, j); |
| 486 | } |
| 487 | parameters[j] = index; |
| 488 | parameters[j + 1] = subject; |
| 489 | |
| 490 | replacement = replace.apply(null, parameters); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 491 | } |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 492 | |
| 493 | result.add(replacement); // The add method converts to string if necessary. |
| 494 | // Can't use matchInfo any more from here, since the function could |
| 495 | // overwrite it. |
| 496 | result.addSpecialSlice(endOfMatch, subject.length); |
| 497 | return result.generate(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 500 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 501 | // ECMA-262 section 15.5.4.12 |
| 502 | function StringSearch(re) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 503 | if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { |
| 504 | throw MakeTypeError("called_on_null_or_undefined", |
| 505 | ["String.prototype.search"]); |
| 506 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 507 | var regexp; |
| 508 | if (IS_STRING(re)) { |
| 509 | regexp = %_GetFromCache(STRING_TO_REGEXP_CACHE_ID, re); |
| 510 | } else if (IS_REGEXP(re)) { |
| 511 | regexp = re; |
| 512 | } else { |
| 513 | regexp = new $RegExp(re); |
| 514 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 515 | var match = DoRegExpExec(regexp, TO_STRING_INLINE(this), 0); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 516 | if (match) { |
| 517 | return match[CAPTURE0]; |
| 518 | } |
| 519 | return -1; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | |
| 523 | // ECMA-262 section 15.5.4.13 |
| 524 | function StringSlice(start, end) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 525 | if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { |
| 526 | throw MakeTypeError("called_on_null_or_undefined", |
| 527 | ["String.prototype.slice"]); |
| 528 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 529 | var s = TO_STRING_INLINE(this); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 530 | var s_len = s.length; |
| 531 | var start_i = TO_INTEGER(start); |
| 532 | var end_i = s_len; |
| 533 | if (end !== void 0) |
| 534 | end_i = TO_INTEGER(end); |
| 535 | |
| 536 | if (start_i < 0) { |
| 537 | start_i += s_len; |
| 538 | if (start_i < 0) |
| 539 | start_i = 0; |
| 540 | } else { |
| 541 | if (start_i > s_len) |
| 542 | start_i = s_len; |
| 543 | } |
| 544 | |
| 545 | if (end_i < 0) { |
| 546 | end_i += s_len; |
| 547 | if (end_i < 0) |
| 548 | end_i = 0; |
| 549 | } else { |
| 550 | if (end_i > s_len) |
| 551 | end_i = s_len; |
| 552 | } |
| 553 | |
| 554 | var num_c = end_i - start_i; |
| 555 | if (num_c < 0) |
| 556 | num_c = 0; |
| 557 | |
| 558 | return SubString(s, start_i, start_i + num_c); |
| 559 | } |
| 560 | |
| 561 | |
| 562 | // ECMA-262 section 15.5.4.14 |
| 563 | function StringSplit(separator, limit) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 564 | if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { |
| 565 | throw MakeTypeError("called_on_null_or_undefined", |
| 566 | ["String.prototype.split"]); |
| 567 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 568 | var subject = TO_STRING_INLINE(this); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 569 | limit = (IS_UNDEFINED(limit)) ? 0xffffffff : TO_UINT32(limit); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 570 | if (limit === 0) return []; |
| 571 | |
| 572 | // ECMA-262 says that if separator is undefined, the result should |
| 573 | // be an array of size 1 containing the entire string. SpiderMonkey |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 574 | // and KJS have this behavior only when no separator is given. If |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 575 | // undefined is explicitly given, they convert it to a string and |
| 576 | // use that. We do as SpiderMonkey and KJS. |
| 577 | if (%_ArgumentsLength() === 0) { |
| 578 | return [subject]; |
| 579 | } |
| 580 | |
| 581 | var length = subject.length; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 582 | if (!IS_REGEXP(separator)) { |
| 583 | separator = TO_STRING_INLINE(separator); |
| 584 | var separator_length = separator.length; |
| 585 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 586 | // If the separator string is empty then return the elements in the subject. |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 587 | if (separator_length === 0) return %StringToArray(subject, limit); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 588 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 589 | var result = %StringSplit(subject, separator, limit); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 590 | |
| 591 | return result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 594 | %_Log('regexp', 'regexp-split,%0S,%1r', [subject, separator]); |
| 595 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 596 | if (length === 0) { |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 597 | if (DoRegExpExec(separator, subject, 0, 0) != null) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 598 | return []; |
| 599 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 600 | return [subject]; |
| 601 | } |
| 602 | |
| 603 | var currentIndex = 0; |
| 604 | var startIndex = 0; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 605 | var startMatch = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 606 | var result = []; |
| 607 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 608 | outer_loop: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 609 | while (true) { |
| 610 | |
| 611 | if (startIndex === length) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 612 | result.push(SubString(subject, currentIndex, length)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 613 | break; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 614 | } |
| 615 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 616 | var matchInfo = DoRegExpExec(separator, subject, startIndex); |
| 617 | if (matchInfo == null || length === (startMatch = matchInfo[CAPTURE0])) { |
| 618 | result.push(SubString(subject, currentIndex, length)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 619 | break; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 620 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 621 | var endIndex = matchInfo[CAPTURE1]; |
| 622 | |
| 623 | // We ignore a zero-length match at the currentIndex. |
| 624 | if (startIndex === endIndex && endIndex === currentIndex) { |
| 625 | startIndex++; |
| 626 | continue; |
| 627 | } |
| 628 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 629 | if (currentIndex + 1 == startMatch) { |
| 630 | result.push(%_StringCharAt(subject, currentIndex)); |
| 631 | } else { |
| 632 | result.push(%_SubString(subject, currentIndex, startMatch)); |
| 633 | } |
| 634 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 635 | if (result.length === limit) break; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 636 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 637 | var matchinfo_len = NUMBER_OF_CAPTURES(matchInfo) + REGEXP_FIRST_CAPTURE; |
| 638 | for (var i = REGEXP_FIRST_CAPTURE + 2; i < matchinfo_len; ) { |
| 639 | var start = matchInfo[i++]; |
| 640 | var end = matchInfo[i++]; |
| 641 | if (end != -1) { |
| 642 | if (start + 1 == end) { |
| 643 | result.push(%_StringCharAt(subject, start)); |
| 644 | } else { |
| 645 | result.push(%_SubString(subject, start, end)); |
| 646 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 647 | } else { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 648 | result.push(void 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 649 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 650 | if (result.length === limit) break outer_loop; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | startIndex = currentIndex = endIndex; |
| 654 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 655 | return result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 659 | // ECMA-262 section 15.5.4.15 |
| 660 | function StringSubstring(start, end) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 661 | if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { |
| 662 | throw MakeTypeError("called_on_null_or_undefined", |
| 663 | ["String.prototype.subString"]); |
| 664 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 665 | var s = TO_STRING_INLINE(this); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 666 | var s_len = s.length; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 667 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 668 | var start_i = TO_INTEGER(start); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 669 | if (start_i < 0) { |
| 670 | start_i = 0; |
| 671 | } else if (start_i > s_len) { |
| 672 | start_i = s_len; |
| 673 | } |
| 674 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 675 | var end_i = s_len; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 676 | if (!IS_UNDEFINED(end)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 677 | end_i = TO_INTEGER(end); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 678 | if (end_i > s_len) { |
| 679 | end_i = s_len; |
| 680 | } else { |
| 681 | if (end_i < 0) end_i = 0; |
| 682 | if (start_i > end_i) { |
| 683 | var tmp = end_i; |
| 684 | end_i = start_i; |
| 685 | start_i = tmp; |
| 686 | } |
| 687 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 688 | } |
| 689 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 690 | return (start_i + 1 == end_i |
| 691 | ? %_StringCharAt(s, start_i) |
| 692 | : %_SubString(s, start_i, end_i)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | |
| 696 | // This is not a part of ECMA-262. |
| 697 | function StringSubstr(start, n) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 698 | if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { |
| 699 | throw MakeTypeError("called_on_null_or_undefined", |
| 700 | ["String.prototype.substr"]); |
| 701 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 702 | var s = TO_STRING_INLINE(this); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 703 | var len; |
| 704 | |
| 705 | // Correct n: If not given, set to string length; if explicitly |
| 706 | // set to undefined, zero, or negative, returns empty string. |
| 707 | if (n === void 0) { |
| 708 | len = s.length; |
| 709 | } else { |
| 710 | len = TO_INTEGER(n); |
| 711 | if (len <= 0) return ''; |
| 712 | } |
| 713 | |
| 714 | // Correct start: If not given (or undefined), set to zero; otherwise |
| 715 | // convert to integer and handle negative case. |
| 716 | if (start === void 0) { |
| 717 | start = 0; |
| 718 | } else { |
| 719 | start = TO_INTEGER(start); |
| 720 | // If positive, and greater than or equal to the string length, |
| 721 | // return empty string. |
| 722 | if (start >= s.length) return ''; |
| 723 | // If negative and absolute value is larger than the string length, |
| 724 | // use zero. |
| 725 | if (start < 0) { |
| 726 | start += s.length; |
| 727 | if (start < 0) start = 0; |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | var end = start + len; |
| 732 | if (end > s.length) end = s.length; |
| 733 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 734 | return (start + 1 == end |
| 735 | ? %_StringCharAt(s, start) |
| 736 | : %_SubString(s, start, end)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | |
| 740 | // ECMA-262, 15.5.4.16 |
| 741 | function StringToLowerCase() { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 742 | if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { |
| 743 | throw MakeTypeError("called_on_null_or_undefined", |
| 744 | ["String.prototype.toLowerCase"]); |
| 745 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 746 | return %StringToLowerCase(TO_STRING_INLINE(this)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | |
| 750 | // ECMA-262, 15.5.4.17 |
| 751 | function StringToLocaleLowerCase() { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 752 | if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { |
| 753 | throw MakeTypeError("called_on_null_or_undefined", |
| 754 | ["String.prototype.toLocaleLowerCase"]); |
| 755 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 756 | return %StringToLowerCase(TO_STRING_INLINE(this)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | |
| 760 | // ECMA-262, 15.5.4.18 |
| 761 | function StringToUpperCase() { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 762 | if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { |
| 763 | throw MakeTypeError("called_on_null_or_undefined", |
| 764 | ["String.prototype.toUpperCase"]); |
| 765 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 766 | return %StringToUpperCase(TO_STRING_INLINE(this)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | |
| 770 | // ECMA-262, 15.5.4.19 |
| 771 | function StringToLocaleUpperCase() { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 772 | if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { |
| 773 | throw MakeTypeError("called_on_null_or_undefined", |
| 774 | ["String.prototype.toLocaleUpperCase"]); |
| 775 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 776 | return %StringToUpperCase(TO_STRING_INLINE(this)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 777 | } |
| 778 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 779 | // ES5, 15.5.4.20 |
| 780 | function StringTrim() { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 781 | if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { |
| 782 | throw MakeTypeError("called_on_null_or_undefined", |
| 783 | ["String.prototype.trim"]); |
| 784 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 785 | return %StringTrim(TO_STRING_INLINE(this), true, true); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | function StringTrimLeft() { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 789 | if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { |
| 790 | throw MakeTypeError("called_on_null_or_undefined", |
| 791 | ["String.prototype.trimLeft"]); |
| 792 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 793 | return %StringTrim(TO_STRING_INLINE(this), true, false); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | function StringTrimRight() { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 797 | if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { |
| 798 | throw MakeTypeError("called_on_null_or_undefined", |
| 799 | ["String.prototype.trimRight"]); |
| 800 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 801 | return %StringTrim(TO_STRING_INLINE(this), false, true); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 802 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 803 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 804 | var static_charcode_array = new InternalArray(4); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 805 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 806 | // ECMA-262, section 15.5.3.2 |
| 807 | function StringFromCharCode(code) { |
| 808 | var n = %_ArgumentsLength(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 809 | if (n == 1) { |
| 810 | if (!%_IsSmi(code)) code = ToNumber(code); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 811 | return %_StringCharFromCode(code & 0xffff); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 812 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 813 | |
| 814 | // NOTE: This is not super-efficient, but it is necessary because we |
| 815 | // want to avoid converting to numbers from within the virtual |
| 816 | // machine. Maybe we can find another way of doing this? |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 817 | var codes = static_charcode_array; |
| 818 | for (var i = 0; i < n; i++) { |
| 819 | var code = %_Arguments(i); |
| 820 | if (!%_IsSmi(code)) code = ToNumber(code); |
| 821 | codes[i] = code; |
| 822 | } |
| 823 | codes.length = n; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 824 | return %StringFromCharCodeArray(codes); |
| 825 | } |
| 826 | |
| 827 | |
| 828 | // Helper function for very basic XSS protection. |
| 829 | function HtmlEscape(str) { |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 830 | return TO_STRING_INLINE(str).replace(/</g, "<") |
| 831 | .replace(/>/g, ">") |
| 832 | .replace(/"/g, """) |
| 833 | .replace(/'/g, "'"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 834 | }; |
| 835 | |
| 836 | |
| 837 | // Compatibility support for KJS. |
| 838 | // Tested by mozilla/js/tests/js1_5/Regress/regress-276103.js. |
| 839 | function StringLink(s) { |
| 840 | return "<a href=\"" + HtmlEscape(s) + "\">" + this + "</a>"; |
| 841 | } |
| 842 | |
| 843 | |
| 844 | function StringAnchor(name) { |
| 845 | return "<a name=\"" + HtmlEscape(name) + "\">" + this + "</a>"; |
| 846 | } |
| 847 | |
| 848 | |
| 849 | function StringFontcolor(color) { |
| 850 | return "<font color=\"" + HtmlEscape(color) + "\">" + this + "</font>"; |
| 851 | } |
| 852 | |
| 853 | |
| 854 | function StringFontsize(size) { |
| 855 | return "<font size=\"" + HtmlEscape(size) + "\">" + this + "</font>"; |
| 856 | } |
| 857 | |
| 858 | |
| 859 | function StringBig() { |
| 860 | return "<big>" + this + "</big>"; |
| 861 | } |
| 862 | |
| 863 | |
| 864 | function StringBlink() { |
| 865 | return "<blink>" + this + "</blink>"; |
| 866 | } |
| 867 | |
| 868 | |
| 869 | function StringBold() { |
| 870 | return "<b>" + this + "</b>"; |
| 871 | } |
| 872 | |
| 873 | |
| 874 | function StringFixed() { |
| 875 | return "<tt>" + this + "</tt>"; |
| 876 | } |
| 877 | |
| 878 | |
| 879 | function StringItalics() { |
| 880 | return "<i>" + this + "</i>"; |
| 881 | } |
| 882 | |
| 883 | |
| 884 | function StringSmall() { |
| 885 | return "<small>" + this + "</small>"; |
| 886 | } |
| 887 | |
| 888 | |
| 889 | function StringStrike() { |
| 890 | return "<strike>" + this + "</strike>"; |
| 891 | } |
| 892 | |
| 893 | |
| 894 | function StringSub() { |
| 895 | return "<sub>" + this + "</sub>"; |
| 896 | } |
| 897 | |
| 898 | |
| 899 | function StringSup() { |
| 900 | return "<sup>" + this + "</sup>"; |
| 901 | } |
| 902 | |
| 903 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 904 | // ReplaceResultBuilder support. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 905 | function ReplaceResultBuilder(str) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 906 | if (%_ArgumentsLength() > 1) { |
| 907 | this.elements = %_Arguments(1); |
| 908 | } else { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 909 | this.elements = new InternalArray(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 910 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 911 | this.special_string = str; |
| 912 | } |
| 913 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 914 | ReplaceResultBuilder.prototype.__proto__ = null; |
| 915 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 916 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 917 | ReplaceResultBuilder.prototype.add = function(str) { |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 918 | str = TO_STRING_INLINE(str); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 919 | if (str.length > 0) this.elements.push(str); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | |
| 923 | ReplaceResultBuilder.prototype.addSpecialSlice = function(start, end) { |
| 924 | var len = end - start; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 925 | if (start < 0 || len <= 0) return; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 926 | if (start < 0x80000 && len < 0x800) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 927 | this.elements.push((start << 11) | len); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 928 | } else { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 929 | // 0 < len <= String::kMaxLength and Smi::kMaxValue >= String::kMaxLength, |
| 930 | // so -len is a smi. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 931 | var elements = this.elements; |
| 932 | elements.push(-len); |
| 933 | elements.push(start); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 934 | } |
| 935 | } |
| 936 | |
| 937 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 938 | ReplaceResultBuilder.prototype.generate = function() { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 939 | var elements = this.elements; |
| 940 | return %StringBuilderConcat(elements, elements.length, this.special_string); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 944 | // ------------------------------------------------------------------- |
| 945 | |
| 946 | function SetupString() { |
| 947 | // Setup the constructor property on the String prototype object. |
| 948 | %SetProperty($String.prototype, "constructor", $String, DONT_ENUM); |
| 949 | |
| 950 | |
| 951 | // Setup the non-enumerable functions on the String object. |
| 952 | InstallFunctions($String, DONT_ENUM, $Array( |
| 953 | "fromCharCode", StringFromCharCode |
| 954 | )); |
| 955 | |
| 956 | |
| 957 | // Setup the non-enumerable functions on the String prototype object. |
| 958 | InstallFunctionsOnHiddenPrototype($String.prototype, DONT_ENUM, $Array( |
| 959 | "valueOf", StringValueOf, |
| 960 | "toString", StringToString, |
| 961 | "charAt", StringCharAt, |
| 962 | "charCodeAt", StringCharCodeAt, |
| 963 | "concat", StringConcat, |
| 964 | "indexOf", StringIndexOf, |
| 965 | "lastIndexOf", StringLastIndexOf, |
| 966 | "localeCompare", StringLocaleCompare, |
| 967 | "match", StringMatch, |
| 968 | "replace", StringReplace, |
| 969 | "search", StringSearch, |
| 970 | "slice", StringSlice, |
| 971 | "split", StringSplit, |
| 972 | "substring", StringSubstring, |
| 973 | "substr", StringSubstr, |
| 974 | "toLowerCase", StringToLowerCase, |
| 975 | "toLocaleLowerCase", StringToLocaleLowerCase, |
| 976 | "toUpperCase", StringToUpperCase, |
| 977 | "toLocaleUpperCase", StringToLocaleUpperCase, |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 978 | "trim", StringTrim, |
| 979 | "trimLeft", StringTrimLeft, |
| 980 | "trimRight", StringTrimRight, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 981 | "link", StringLink, |
| 982 | "anchor", StringAnchor, |
| 983 | "fontcolor", StringFontcolor, |
| 984 | "fontsize", StringFontsize, |
| 985 | "big", StringBig, |
| 986 | "blink", StringBlink, |
| 987 | "bold", StringBold, |
| 988 | "fixed", StringFixed, |
| 989 | "italics", StringItalics, |
| 990 | "small", StringSmall, |
| 991 | "strike", StringStrike, |
| 992 | "sub", StringSub, |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 993 | "sup", StringSup |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 994 | )); |
| 995 | } |
| 996 | |
| 997 | |
| 998 | SetupString(); |