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