Commit 9e2c15e4 authored by sandholm@chromium.org's avatar sandholm@chromium.org

Moved one check outside of the loop and moved another one under an if statement

Review URL: http://codereview.chromium.org/821001

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@4082 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent e9484a07
...@@ -387,19 +387,44 @@ function StringReplaceRegExpWithFunction(subject, regexp, replace) { ...@@ -387,19 +387,44 @@ function StringReplaceRegExpWithFunction(subject, regexp, replace) {
// Unfortunately, that means this code is nearly duplicated, here and in // Unfortunately, that means this code is nearly duplicated, here and in
// jsregexp.cc. // jsregexp.cc.
if (regexp.global) { if (regexp.global) {
var numberOfCaptures = NUMBER_OF_CAPTURES(matchInfo) >> 1;
var previous = 0; var previous = 0;
var startOfMatch;
if (NUMBER_OF_CAPTURES(matchInfo) == 2) {
// Both branches contain essentially the same loop except for the call
// to the replace function. The branch is put outside of the loop for
// speed
do { do {
var startOfMatch = matchInfo[CAPTURE0]; startOfMatch = matchInfo[CAPTURE0];
result.addSpecialSlice(previous, startOfMatch); result.addSpecialSlice(previous, startOfMatch);
previous = matchInfo[CAPTURE1]; previous = matchInfo[CAPTURE1];
if (numberOfCaptures == 1) {
var match = SubString(subject, startOfMatch, previous); var match = SubString(subject, startOfMatch, previous);
// Don't call directly to avoid exposing the built-in global object. // Don't call directly to avoid exposing the built-in global object.
result.add(replace.call(null, match, startOfMatch, subject)); result.add(replace.call(null, match, startOfMatch, subject));
// Can't use matchInfo any more from here, since the function could
// overwrite it.
// Continue with the next match.
// Increment previous if we matched an empty string, as per ECMA-262
// 15.5.4.10.
if (previous == startOfMatch) {
// Add the skipped character to the output, if any.
if (previous < subject.length) {
result.addSpecialSlice(previous, previous + 1);
}
previous++;
// Per ECMA-262 15.10.6.2, if the previous index is greater than the
// string length, there is no match
if (previous > subject.length) {
return result.generate();
}
}
matchInfo = DoRegExpExec(regexp, subject, previous);
} while (!IS_NULL(matchInfo));
} else { } else {
do {
startOfMatch = matchInfo[CAPTURE0];
result.addSpecialSlice(previous, startOfMatch);
previous = matchInfo[CAPTURE1];
result.add(ApplyReplacementFunction(replace, matchInfo, subject)); result.add(ApplyReplacementFunction(replace, matchInfo, subject));
}
// Can't use matchInfo any more from here, since the function could // Can't use matchInfo any more from here, since the function could
// overwrite it. // overwrite it.
// Continue with the next match. // Continue with the next match.
...@@ -411,19 +436,19 @@ function StringReplaceRegExpWithFunction(subject, regexp, replace) { ...@@ -411,19 +436,19 @@ function StringReplaceRegExpWithFunction(subject, regexp, replace) {
result.addSpecialSlice(previous, previous + 1); result.addSpecialSlice(previous, previous + 1);
} }
previous++; previous++;
}
// Per ECMA-262 15.10.6.2, if the previous index is greater than the // Per ECMA-262 15.10.6.2, if the previous index is greater than the
// string length, there is no match // string length, there is no match
matchInfo = (previous > subject.length) if (previous > subject.length) {
? null return result.generate();
: DoRegExpExec(regexp, subject, previous); }
}
matchInfo = DoRegExpExec(regexp, subject, previous);
} while (!IS_NULL(matchInfo)); } while (!IS_NULL(matchInfo));
}
// Tack on the final right substring after the last match, if necessary. // Tack on the final right substring after the last match.
if (previous < subject.length) {
result.addSpecialSlice(previous, subject.length); result.addSpecialSlice(previous, subject.length);
}
} else { // Not a global regexp, no need to loop. } else { // Not a global regexp, no need to loop.
result.addSpecialSlice(0, matchInfo[CAPTURE0]); result.addSpecialSlice(0, matchInfo[CAPTURE0]);
var endOfMatch = matchInfo[CAPTURE1]; var endOfMatch = matchInfo[CAPTURE1];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment