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