Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1168)

Unified Diff: src/string.js

Issue 42115: Faster string.replace with regexp pattern. (Closed)
Patch Set: Addressed review comments Created 11 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« src/runtime.cc ('K') | « src/runtime.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/string.js
diff --git a/src/string.js b/src/string.js
index f8efab613f348513fd00648f425f4515b743e803..de35902d4d28d9cb11f33cf99fce19f24eac07a6 100644
--- a/src/string.js
+++ b/src/string.js
@@ -237,50 +237,15 @@ function StringReplace(search, replace) {
// lastMatchArray without erroneously affecting the properties on the global
// RegExp object.
var reusableMatchInfo = [2, -1, -1, "", ""];
-var reusableMatchArray = [ void 0 ];
// Helper function for regular expressions in String.prototype.replace.
function StringReplaceRegExp(subject, regexp, replace) {
- // Compute an array of matches; each match is really a list of
- // captures - pairs of (start, end) indexes into the subject string.
- var matches;
- if (regexp.global) {
- matches = DoRegExpExecGlobal(regexp, subject);
- if (matches.length == 0) return subject;
- } else {
- var lastMatchInfo = DoRegExpExec(regexp, subject, 0);
- if (IS_NULL(lastMatchInfo)) return subject;
- reusableMatchArray[0] = lastMatchInfo;
- matches = reusableMatchArray;
- }
-
- // Determine the number of matches.
- var length = matches.length;
-
- // Build the resulting string of subject slices and replacements.
- var result = new ReplaceResultBuilder(subject);
- var previous = 0;
- // The caller of StringReplaceRegExp must ensure that replace is not a
- // function.
replace = ToString(replace);
- if (%StringIndexOf(replace, "$", 0) < 0) {
- for (var i = 0; i < length; i++) {
- var matchInfo = matches[i];
- result.addSpecialSlice(previous, matchInfo[CAPTURE0]);
- result.add(replace);
- previous = matchInfo[CAPTURE1]; // continue after match
- }
- } else {
- for (var i = 0; i < length; i++) {
- var matchInfo = matches[i];
- result.addSpecialSlice(previous, matchInfo[CAPTURE0]);
- ExpandReplacement(replace, subject, matchInfo, result);
- previous = matchInfo[CAPTURE1]; // continue after match
- }
- }
- result.addSpecialSlice(previous, subject.length);
- return result.generate();
+ return %StringReplaceRegExpWithString(subject,
+ regexp,
+ replace,
+ lastMatchInfo);
};
« src/runtime.cc ('K') | « src/runtime.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698