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

Side by Side 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 unified diff | Download patch
« src/runtime.cc ('K') | « src/runtime.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. 1 // Copyright 2006-2008 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 219 matching lines...) Expand 10 before | Expand all | Expand 10 after
230 return builder.generate(); 230 return builder.generate();
231 } 231 }
232 232
233 233
234 // This has the same size as the lastMatchInfo array, and can be used for 234 // This has the same size as the lastMatchInfo array, and can be used for
235 // functions that expect that structure to be returned. It is used when the 235 // functions that expect that structure to be returned. It is used when the
236 // needle is a string rather than a regexp. In this case we can't update 236 // needle is a string rather than a regexp. In this case we can't update
237 // lastMatchArray without erroneously affecting the properties on the global 237 // lastMatchArray without erroneously affecting the properties on the global
238 // RegExp object. 238 // RegExp object.
239 var reusableMatchInfo = [2, -1, -1, "", ""]; 239 var reusableMatchInfo = [2, -1, -1, "", ""];
240 var reusableMatchArray = [ void 0 ];
241 240
242 241
243 // Helper function for regular expressions in String.prototype.replace. 242 // Helper function for regular expressions in String.prototype.replace.
244 function StringReplaceRegExp(subject, regexp, replace) { 243 function StringReplaceRegExp(subject, regexp, replace) {
245 // Compute an array of matches; each match is really a list of
246 // captures - pairs of (start, end) indexes into the subject string.
247 var matches;
248 if (regexp.global) {
249 matches = DoRegExpExecGlobal(regexp, subject);
250 if (matches.length == 0) return subject;
251 } else {
252 var lastMatchInfo = DoRegExpExec(regexp, subject, 0);
253 if (IS_NULL(lastMatchInfo)) return subject;
254 reusableMatchArray[0] = lastMatchInfo;
255 matches = reusableMatchArray;
256 }
257
258 // Determine the number of matches.
259 var length = matches.length;
260
261 // Build the resulting string of subject slices and replacements.
262 var result = new ReplaceResultBuilder(subject);
263 var previous = 0;
264 // The caller of StringReplaceRegExp must ensure that replace is not a
265 // function.
266 replace = ToString(replace); 244 replace = ToString(replace);
267 if (%StringIndexOf(replace, "$", 0) < 0) { 245 return %StringReplaceRegExpWithString(subject,
268 for (var i = 0; i < length; i++) { 246 regexp,
269 var matchInfo = matches[i]; 247 replace,
270 result.addSpecialSlice(previous, matchInfo[CAPTURE0]); 248 lastMatchInfo);
271 result.add(replace);
272 previous = matchInfo[CAPTURE1]; // continue after match
273 }
274 } else {
275 for (var i = 0; i < length; i++) {
276 var matchInfo = matches[i];
277 result.addSpecialSlice(previous, matchInfo[CAPTURE0]);
278 ExpandReplacement(replace, subject, matchInfo, result);
279 previous = matchInfo[CAPTURE1]; // continue after match
280 }
281 }
282 result.addSpecialSlice(previous, subject.length);
283 return result.generate();
284 }; 249 };
285 250
286 251
287 // Expand the $-expressions in the string and return a new string with 252 // Expand the $-expressions in the string and return a new string with
288 // the result. 253 // the result.
289 function ExpandReplacement(string, subject, matchInfo, builder) { 254 function ExpandReplacement(string, subject, matchInfo, builder) {
290 var next = %StringIndexOf(string, '$', 0); 255 var next = %StringIndexOf(string, '$', 0);
291 if (next < 0) { 256 if (next < 0) {
292 builder.add(string); 257 builder.add(string);
293 return; 258 return;
(...skipping 609 matching lines...) Expand 10 before | Expand all | Expand 10 after
903 "italics", StringItalics, 868 "italics", StringItalics,
904 "small", StringSmall, 869 "small", StringSmall,
905 "strike", StringStrike, 870 "strike", StringStrike,
906 "sub", StringSub, 871 "sub", StringSub,
907 "sup", StringSup 872 "sup", StringSup
908 )); 873 ));
909 } 874 }
910 875
911 876
912 SetupString(); 877 SetupString();
OLDNEW
« 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