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

Side by Side Diff: src/string.js

Issue 42115: Faster string.replace with regexp pattern. (Closed)
Patch Set: 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 218 matching lines...) Expand 10 before | Expand all | Expand 10 after
229 229
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, "", ""];
Erik Corry 2009/03/12 10:13:00 Do we still need both of these?
Lasse Reichstein 2009/03/13 08:36:51 No, well spotted, the reusabeMatchArray isn't used
240 var reusableMatchArray = [ void 0 ]; 240 var reusableMatchArray = [ void 0 ];
241 241
242 242
243 // Helper function for regular expressions in String.prototype.replace. 243 // Helper function for regular expressions in String.prototype.replace.
244 function StringReplaceRegExp(subject, regexp, replace) { 244 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); 245 replace = ToString(replace);
267 if (%StringIndexOf(replace, "$", 0) < 0) { 246 return %StringReplaceRegExpWithString(subject,
268 for (var i = 0; i < length; i++) { 247 regexp,
269 var matchInfo = matches[i]; 248 replace,
270 result.addSpecialSlice(previous, matchInfo[CAPTURE0]); 249 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 }; 250 };
285 251
286 252
287 // Expand the $-expressions in the string and return a new string with 253 // Expand the $-expressions in the string and return a new string with
288 // the result. 254 // the result.
Erik Corry 2009/03/12 10:13:00 Do we still need this?
Lasse Reichstein 2009/03/13 08:36:51 Sadly, yes. Replacing with a string pattern, inste
Erik Corry 2009/03/13 09:12:34 But knowing that number of captures is always zero
Lasse Reichstein 2011/06/14 11:51:17 True. I'll leave it as-is for now, expecting that
289 function ExpandReplacement(string, subject, matchInfo, builder) { 255 function ExpandReplacement(string, subject, matchInfo, builder) {
290 var next = %StringIndexOf(string, '$', 0); 256 var next = %StringIndexOf(string, '$', 0);
291 if (next < 0) { 257 if (next < 0) {
292 builder.add(string); 258 builder.add(string);
293 return; 259 return;
294 } 260 }
295 261
296 // Compute the number of captures; see ECMA-262, 15.5.4.11, p. 102. 262 // Compute the number of captures; see ECMA-262, 15.5.4.11, p. 102.
297 var m = NUMBER_OF_CAPTURES(matchInfo) >> 1; // Includes the match. 263 var m = NUMBER_OF_CAPTURES(matchInfo) >> 1; // Includes the match.
298 264
(...skipping 604 matching lines...) Expand 10 before | Expand all | Expand 10 after
903 "italics", StringItalics, 869 "italics", StringItalics,
904 "small", StringSmall, 870 "small", StringSmall,
905 "strike", StringStrike, 871 "strike", StringStrike,
906 "sub", StringSub, 872 "sub", StringSub,
907 "sup", StringSup 873 "sup", StringSup
908 )); 874 ));
909 } 875 }
910 876
911 877
912 SetupString(); 878 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