OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 (function(global, utils) { | 5 (function(global, utils) { |
6 | 6 |
7 %CheckIsBootstrapping(); | 7 %CheckIsBootstrapping(); |
8 | 8 |
9 // ------------------------------------------------------------------- | 9 // ------------------------------------------------------------------- |
10 // Imports | 10 // Imports |
(...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
311 return %StringReplaceOneCharWithString(subject, search, replace); | 311 return %StringReplaceOneCharWithString(subject, search, replace); |
312 } | 312 } |
313 var start = %StringIndexOf(subject, search, 0); | 313 var start = %StringIndexOf(subject, search, 0); |
314 if (start < 0) return subject; | 314 if (start < 0) return subject; |
315 var end = start + search.length; | 315 var end = start + search.length; |
316 | 316 |
317 var result = %_SubString(subject, 0, start); | 317 var result = %_SubString(subject, 0, start); |
318 | 318 |
319 // Compute the string to replace with. | 319 // Compute the string to replace with. |
320 if (IS_CALLABLE(replace)) { | 320 if (IS_CALLABLE(replace)) { |
321 var receiver = UNDEFINED; | 321 result += replace(search, start, subject); |
322 result += %_CallFunction(receiver, search, start, subject, replace); | |
323 } else { | 322 } else { |
324 reusableMatchInfo[CAPTURE0] = start; | 323 reusableMatchInfo[CAPTURE0] = start; |
325 reusableMatchInfo[CAPTURE1] = end; | 324 reusableMatchInfo[CAPTURE1] = end; |
326 result = ExpandReplacement(TO_STRING_INLINE(replace), | 325 result = ExpandReplacement(TO_STRING_INLINE(replace), |
327 subject, | 326 subject, |
328 reusableMatchInfo, | 327 reusableMatchInfo, |
329 result); | 328 result); |
330 } | 329 } |
331 | 330 |
332 return result + %_SubString(subject, end, subject.length); | 331 return result + %_SubString(subject, end, subject.length); |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
475 // RegExp.leftContext work during the callback function. | 474 // RegExp.leftContext work during the callback function. |
476 if (elem > 0) { | 475 if (elem > 0) { |
477 match_start = (elem >> 11) + (elem & 0x7ff); | 476 match_start = (elem >> 11) + (elem & 0x7ff); |
478 } else { | 477 } else { |
479 match_start = res[++i] - elem; | 478 match_start = res[++i] - elem; |
480 } | 479 } |
481 } else { | 480 } else { |
482 override[0] = elem; | 481 override[0] = elem; |
483 override[1] = match_start; | 482 override[1] = match_start; |
484 $regexpLastMatchInfoOverride = override; | 483 $regexpLastMatchInfoOverride = override; |
485 var func_result = | 484 var func_result = replace(elem, match_start, subject); |
486 %_CallFunction(UNDEFINED, elem, match_start, subject, replace); | |
487 // Overwrite the i'th element in the results with the string we got | 485 // Overwrite the i'th element in the results with the string we got |
488 // back from the callback function. | 486 // back from the callback function. |
489 res[i] = TO_STRING_INLINE(func_result); | 487 res[i] = TO_STRING_INLINE(func_result); |
490 match_start += elem.length; | 488 match_start += elem.length; |
491 } | 489 } |
492 } | 490 } |
493 } else { | 491 } else { |
494 for (var i = 0; i < len; i++) { | 492 for (var i = 0; i < len; i++) { |
495 var elem = res[i]; | 493 var elem = res[i]; |
496 if (!%_IsSmi(elem)) { | 494 if (!%_IsSmi(elem)) { |
(...skipping 25 matching lines...) Expand all Loading... |
522 var endOfMatch = matchInfo[CAPTURE1]; | 520 var endOfMatch = matchInfo[CAPTURE1]; |
523 // Compute the parameter list consisting of the match, captures, index, | 521 // Compute the parameter list consisting of the match, captures, index, |
524 // and subject for the replace function invocation. | 522 // and subject for the replace function invocation. |
525 // The number of captures plus one for the match. | 523 // The number of captures plus one for the match. |
526 var m = NUMBER_OF_CAPTURES(matchInfo) >> 1; | 524 var m = NUMBER_OF_CAPTURES(matchInfo) >> 1; |
527 var replacement; | 525 var replacement; |
528 if (m == 1) { | 526 if (m == 1) { |
529 // No captures, only the match, which is always valid. | 527 // No captures, only the match, which is always valid. |
530 var s = %_SubString(subject, index, endOfMatch); | 528 var s = %_SubString(subject, index, endOfMatch); |
531 // Don't call directly to avoid exposing the built-in global object. | 529 // Don't call directly to avoid exposing the built-in global object. |
532 replacement = %_CallFunction(UNDEFINED, s, index, subject, replace); | 530 replacement = replace(s, index, subject); |
533 } else { | 531 } else { |
534 var parameters = new InternalArray(m + 2); | 532 var parameters = new InternalArray(m + 2); |
535 for (var j = 0; j < m; j++) { | 533 for (var j = 0; j < m; j++) { |
536 parameters[j] = CaptureString(subject, matchInfo, j); | 534 parameters[j] = CaptureString(subject, matchInfo, j); |
537 } | 535 } |
538 parameters[j] = index; | 536 parameters[j] = index; |
539 parameters[j + 1] = subject; | 537 parameters[j + 1] = subject; |
540 | 538 |
541 replacement = %Apply(replace, UNDEFINED, parameters, 0, j + 2); | 539 replacement = %Apply(replace, UNDEFINED, parameters, 0, j + 2); |
542 } | 540 } |
(...skipping 663 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1206 to.StringLastIndexOf = StringLastIndexOfJS; | 1204 to.StringLastIndexOf = StringLastIndexOfJS; |
1207 to.StringMatch = StringMatchJS; | 1205 to.StringMatch = StringMatchJS; |
1208 to.StringReplace = StringReplace; | 1206 to.StringReplace = StringReplace; |
1209 to.StringSlice = StringSlice; | 1207 to.StringSlice = StringSlice; |
1210 to.StringSplit = StringSplitJS; | 1208 to.StringSplit = StringSplitJS; |
1211 to.StringSubstr = StringSubstr; | 1209 to.StringSubstr = StringSubstr; |
1212 to.StringSubstring = StringSubstring; | 1210 to.StringSubstring = StringSubstring; |
1213 }); | 1211 }); |
1214 | 1212 |
1215 }) | 1213 }) |
OLD | NEW |