| OLD | NEW |
| 1 // Copyright 2006-2009 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2009 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 388 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 399 var scaled = index << 1; | 399 var scaled = index << 1; |
| 400 // Compute start and end. | 400 // Compute start and end. |
| 401 var start = matchInfo[CAPTURE(scaled)]; | 401 var start = matchInfo[CAPTURE(scaled)]; |
| 402 var end = matchInfo[CAPTURE(scaled + 1)]; | 402 var end = matchInfo[CAPTURE(scaled + 1)]; |
| 403 // If either start or end is missing return. | 403 // If either start or end is missing return. |
| 404 if (start < 0 || end <= start) return; | 404 if (start < 0 || end <= start) return; |
| 405 builder.addSpecialSlice(start, end); | 405 builder.addSpecialSlice(start, end); |
| 406 }; | 406 }; |
| 407 | 407 |
| 408 // TODO(lrn): This array will survive indefinitely if replace is never | 408 // TODO(lrn): This array will survive indefinitely if replace is never |
| 409 // called again. However, it will be empty, since the contents are cleared | 409 // called again. However, it will be empty, since the contents are cleared |
| 410 // in the finally block. | 410 // in the finally block. |
| 411 var reusableReplaceArray = $Array(16); | 411 var reusableReplaceArray = $Array(16); |
| 412 | 412 |
| 413 // Helper function for replacing regular expressions with the result of a | 413 // Helper function for replacing regular expressions with the result of a |
| 414 // function application in String.prototype.replace. | 414 // function application in String.prototype.replace. |
| 415 function StringReplaceRegExpWithFunction(subject, regexp, replace) { | 415 function StringReplaceRegExpWithFunction(subject, regexp, replace) { |
| 416 if (regexp.global) { | 416 if (regexp.global) { |
| 417 var resultArray = reusableReplaceArray; | 417 var resultArray = reusableReplaceArray; |
| 418 if (resultArray) { | 418 if (resultArray) { |
| 419 reusableReplaceArray = null; | 419 reusableReplaceArray = null; |
| 420 } else { | 420 } else { |
| 421 // Inside a nested replace (replace called from the replacement function | 421 // Inside a nested replace (replace called from the replacement function |
| 422 // of another replace) or we have failed to set the reusable array | 422 // of another replace) or we have failed to set the reusable array |
| 423 // back due to an exception in a replacement function. Create a new | 423 // back due to an exception in a replacement function. Create a new |
| 424 // array to use in the future, or until the original is written back. | 424 // array to use in the future, or until the original is written back. |
| 425 resultArray = $Array(16); | 425 resultArray = $Array(16); |
| 426 } | 426 } |
| 427 try { | 427 try { |
| 428 // Must handle exceptions thrown by the replace functions correctly, | 428 // Must handle exceptions thrown by the replace functions correctly, |
| 429 // including unregistering global regexps. | 429 // including unregistering global regexps. |
| 430 var res = %RegExpExecMultiple(regexp, | 430 var res = %RegExpExecMultiple(regexp, |
| 431 subject, | 431 subject, |
| 432 lastMatchInfo, | 432 lastMatchInfo, |
| 433 resultArray); | 433 resultArray); |
| 434 regexp.lastIndex = 0; | 434 regexp.lastIndex = 0; |
| 435 if (IS_NULL(res)) { | 435 if (IS_NULL(res)) { |
| 436 // No matches at all. | 436 // No matches at all. |
| 437 return subject; | 437 return subject; |
| 438 } | 438 } |
| 439 var len = res.length; | 439 var len = res.length; |
| 440 var i = 0; | 440 var i = 0; |
| 441 if (NUMBER_OF_CAPTURES(lastMatchInfo) == 2) { | 441 if (NUMBER_OF_CAPTURES(lastMatchInfo) == 2) { |
| 442 var match_start = 0; | 442 var match_start = 0; |
| 443 while (i < len) { | 443 while (i < len) { |
| 444 var elem = res[i]; | 444 var elem = res[i]; |
| 445 if (%_IsSmi(elem)) { | 445 if (%_IsSmi(elem)) { |
| 446 if (elem > 0) { | 446 if (elem > 0) { |
| 447 match_start = (elem >> 11) + (elem & 0x7ff); | 447 match_start = (elem >> 11) + (elem & 0x7ff); |
| 448 } else { | 448 } else { |
| 449 match_start = res[++i] - elem; | 449 match_start = res[++i] - elem; |
| 450 } | 450 } |
| 451 } else { | 451 } else { |
| 452 var func_result = replace.call(null, elem, match_start, subject); | 452 var func_result = replace.call(null, elem, match_start, subject); |
| 453 if (!IS_STRING(func_result)) { | 453 if (!IS_STRING(func_result)) { |
| 454 func_result = NonStringToString(func_result); | 454 func_result = NonStringToString(func_result); |
| 455 } | 455 } |
| 456 res[i] = func_result; | 456 res[i] = func_result; |
| 457 match_start += elem.length; | 457 match_start += elem.length; |
| 458 } | 458 } |
| 459 i++; | 459 i++; |
| 460 } | 460 } |
| 461 } else { | 461 } else { |
| 462 while (i < len) { | 462 while (i < len) { |
| 463 var elem = res[i]; | 463 var elem = res[i]; |
| 464 if (!%_IsSmi(elem)) { | 464 if (!%_IsSmi(elem)) { |
| 465 // elem must be an Array. | 465 // elem must be an Array. |
| 466 // Use the apply argument as backing for global RegExp properties. | 466 // Use the apply argument as backing for global RegExp properties. |
| 467 lastMatchInfoOverride = elem; | 467 lastMatchInfoOverride = elem; |
| 468 var func_result = replace.apply(null, elem); | 468 var func_result = replace.apply(null, elem); |
| 469 if (!IS_STRING(func_result)) { | 469 if (!IS_STRING(func_result)) { |
| 470 func_result = NonStringToString(func_result); | 470 func_result = NonStringToString(func_result); |
| 471 } | 471 } |
| 472 res[i] = func_result; | 472 res[i] = func_result; |
| 473 } | 473 } |
| 474 i++; | 474 i++; |
| 475 } | 475 } |
| 476 } | 476 } |
| 477 var result = new ReplaceResultBuilder(subject, res); | 477 var result = new ReplaceResultBuilder(subject, res); |
| 478 return result.generate(); | 478 return result.generate(); |
| 479 } finally { | 479 } finally { |
| 480 lastMatchInfoOverride = null; | 480 lastMatchInfoOverride = null; |
| 481 resultArray.length = 0; | 481 resultArray.length = 0; |
| 482 reusableReplaceArray = resultArray; | 482 reusableReplaceArray = resultArray; |
| 483 } | 483 } |
| 484 } else { // Not a global regexp, no need to loop. | 484 } else { // Not a global regexp, no need to loop. |
| 485 var matchInfo = DoRegExpExec(regexp, subject, 0); | 485 var matchInfo = DoRegExpExec(regexp, subject, 0); |
| 486 if (IS_NULL(matchInfo)) return subject; | 486 if (IS_NULL(matchInfo)) return subject; |
| 487 | 487 |
| 488 var result = new ReplaceResultBuilder(subject); | 488 var result = new ReplaceResultBuilder(subject); |
| 489 result.addSpecialSlice(0, matchInfo[CAPTURE0]); | 489 result.addSpecialSlice(0, matchInfo[CAPTURE0]); |
| 490 var endOfMatch = matchInfo[CAPTURE1]; | 490 var endOfMatch = matchInfo[CAPTURE1]; |
| 491 result.add(ApplyReplacementFunction(replace, matchInfo, subject)); | 491 result.add(ApplyReplacementFunction(replace, matchInfo, subject)); |
| 492 // Can't use matchInfo any more from here, since the function could | 492 // Can't use matchInfo any more from here, since the function could |
| 493 // overwrite it. | 493 // overwrite it. |
| 494 result.addSpecialSlice(endOfMatch, subject.length); | 494 result.addSpecialSlice(endOfMatch, subject.length); |
| 495 return result.generate(); | 495 return result.generate(); |
| 496 } | 496 } |
| 497 } | 497 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 513 for (var j = 0; j < m; j++) { | 513 for (var j = 0; j < m; j++) { |
| 514 parameters[j] = CaptureString(subject, matchInfo, j); | 514 parameters[j] = CaptureString(subject, matchInfo, j); |
| 515 } | 515 } |
| 516 parameters[j] = index; | 516 parameters[j] = index; |
| 517 parameters[j + 1] = subject; | 517 parameters[j + 1] = subject; |
| 518 return replace.apply(null, parameters); | 518 return replace.apply(null, parameters); |
| 519 } | 519 } |
| 520 | 520 |
| 521 | 521 |
| 522 // ECMA-262 section 15.5.4.12 | 522 // ECMA-262 section 15.5.4.12 |
| 523 function StringSearch(re) { | 523 function StringSearch(re) { |
| 524 var regexp = new $RegExp(re); | 524 var regexp = new $RegExp(re); |
| 525 var s = TO_STRING_INLINE(this); | 525 var s = TO_STRING_INLINE(this); |
| 526 var match = DoRegExpExec(regexp, s, 0); | 526 var match = DoRegExpExec(regexp, s, 0); |
| 527 if (match) { | 527 if (match) { |
| 528 lastMatchInfo = match; | 528 lastMatchInfo = match; |
| 529 return match[CAPTURE0]; | 529 return match[CAPTURE0]; |
| 530 } | 530 } |
| 531 return -1; | 531 return -1; |
| 532 } | 532 } |
| 533 | 533 |
| (...skipping 451 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 985 "small", StringSmall, | 985 "small", StringSmall, |
| 986 "strike", StringStrike, | 986 "strike", StringStrike, |
| 987 "sub", StringSub, | 987 "sub", StringSub, |
| 988 "sup", StringSup, | 988 "sup", StringSup, |
| 989 "toJSON", StringToJSON | 989 "toJSON", StringToJSON |
| 990 )); | 990 )); |
| 991 } | 991 } |
| 992 | 992 |
| 993 | 993 |
| 994 SetupString(); | 994 SetupString(); |
| OLD | NEW |