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