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