OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 var subject = TO_STRING_INLINE(this); | 187 var subject = TO_STRING_INLINE(this); |
188 if (IS_REGEXP(regexp)) { | 188 if (IS_REGEXP(regexp)) { |
189 // Emulate RegExp.prototype.exec's side effect in step 5, even though | 189 // Emulate RegExp.prototype.exec's side effect in step 5, even though |
190 // value is discarded. | 190 // value is discarded. |
191 ToInteger(regexp.lastIndex); | 191 ToInteger(regexp.lastIndex); |
192 if (!regexp.global) return RegExpExecNoTests(regexp, subject, 0); | 192 if (!regexp.global) return RegExpExecNoTests(regexp, subject, 0); |
193 %_Log('regexp', 'regexp-match,%0S,%1r', [subject, regexp]); | 193 %_Log('regexp', 'regexp-match,%0S,%1r', [subject, regexp]); |
194 // lastMatchInfo is defined in regexp.js. | 194 // lastMatchInfo is defined in regexp.js. |
195 var result = %StringMatch(subject, regexp, lastMatchInfo); | 195 var result = %StringMatch(subject, regexp, lastMatchInfo); |
196 if (result !== null) lastMatchInfoOverride = null; | 196 if (result !== null) lastMatchInfoOverride = null; |
| 197 regexp.lastIndex = 0; |
197 return result; | 198 return result; |
198 } | 199 } |
199 // Non-regexp argument. | 200 // Non-regexp argument. |
200 regexp = new $RegExp(regexp); | 201 regexp = new $RegExp(regexp); |
201 return RegExpExecNoTests(regexp, subject, 0); | 202 return RegExpExecNoTests(regexp, subject, 0); |
202 } | 203 } |
203 | 204 |
204 | 205 |
205 // SubString is an internal function that returns the sub string of 'string'. | 206 // SubString is an internal function that returns the sub string of 'string'. |
206 // If resulting string is of length 1, we use the one character cache | 207 // If resulting string is of length 1, we use the one character cache |
(...skipping 30 matching lines...) Expand all Loading... |
237 if (IS_SPEC_FUNCTION(replace)) { | 238 if (IS_SPEC_FUNCTION(replace)) { |
238 if (search.global) { | 239 if (search.global) { |
239 return StringReplaceGlobalRegExpWithFunction(subject, search, replace); | 240 return StringReplaceGlobalRegExpWithFunction(subject, search, replace); |
240 } else { | 241 } else { |
241 return StringReplaceNonGlobalRegExpWithFunction(subject, | 242 return StringReplaceNonGlobalRegExpWithFunction(subject, |
242 search, | 243 search, |
243 replace); | 244 replace); |
244 } | 245 } |
245 } else { | 246 } else { |
246 if (lastMatchInfoOverride == null) { | 247 if (lastMatchInfoOverride == null) { |
247 return %StringReplaceRegExpWithString(subject, | 248 var answer = %StringReplaceRegExpWithString(subject, |
248 search, | 249 search, |
249 TO_STRING_INLINE(replace), | 250 TO_STRING_INLINE(replace), |
250 lastMatchInfo); | 251 lastMatchInfo); |
| 252 if (IS_UNDEFINED(answer)) { // No match. Return subject string. |
| 253 search.lastIndex = 0; |
| 254 return subject; |
| 255 } |
| 256 if (search.global) search.lastIndex = 0; |
| 257 return answer; |
251 } else { | 258 } else { |
252 // We use this hack to detect whether StringReplaceRegExpWithString | 259 // We use this hack to detect whether StringReplaceRegExpWithString |
253 // found at least one hit. In that case we need to remove any | 260 // found at least one hit. In that case we need to remove any |
254 // override. | 261 // override. |
255 var saved_subject = lastMatchInfo[LAST_SUBJECT_INDEX]; | 262 var saved_subject = lastMatchInfo[LAST_SUBJECT_INDEX]; |
256 lastMatchInfo[LAST_SUBJECT_INDEX] = 0; | 263 lastMatchInfo[LAST_SUBJECT_INDEX] = 0; |
257 var answer = %StringReplaceRegExpWithString(subject, | 264 var answer = %StringReplaceRegExpWithString(subject, |
258 search, | 265 search, |
259 TO_STRING_INLINE(replace), | 266 TO_STRING_INLINE(replace), |
260 lastMatchInfo); | 267 lastMatchInfo); |
| 268 if (IS_UNDEFINED(answer)) { // No match. Return subject string. |
| 269 search.lastIndex = 0; |
| 270 lastMatchInfo[LAST_SUBJECT_INDEX] = saved_subject; |
| 271 return subject; |
| 272 } |
261 if (%_IsSmi(lastMatchInfo[LAST_SUBJECT_INDEX])) { | 273 if (%_IsSmi(lastMatchInfo[LAST_SUBJECT_INDEX])) { |
262 lastMatchInfo[LAST_SUBJECT_INDEX] = saved_subject; | 274 lastMatchInfo[LAST_SUBJECT_INDEX] = saved_subject; |
263 } else { | 275 } else { |
264 lastMatchInfoOverride = null; | 276 lastMatchInfoOverride = null; |
265 } | 277 } |
| 278 if (search.global) search.lastIndex = 0; |
266 return answer; | 279 return answer; |
267 } | 280 } |
268 } | 281 } |
269 } | 282 } |
270 | 283 |
271 // Convert the search argument to a string and search for it. | 284 // Convert the search argument to a string and search for it. |
272 search = TO_STRING_INLINE(search); | 285 search = TO_STRING_INLINE(search); |
273 if (search.length == 1 && | 286 if (search.length == 1 && |
274 subject.length > 0xFF && | 287 subject.length > 0xFF && |
275 IS_STRING(replace) && | 288 IS_STRING(replace) && |
(...skipping 726 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1002 "fixed", StringFixed, | 1015 "fixed", StringFixed, |
1003 "italics", StringItalics, | 1016 "italics", StringItalics, |
1004 "small", StringSmall, | 1017 "small", StringSmall, |
1005 "strike", StringStrike, | 1018 "strike", StringStrike, |
1006 "sub", StringSub, | 1019 "sub", StringSub, |
1007 "sup", StringSup | 1020 "sup", StringSup |
1008 )); | 1021 )); |
1009 } | 1022 } |
1010 | 1023 |
1011 SetUpString(); | 1024 SetUpString(); |
OLD | NEW |