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

Side by Side Diff: src/string.js

Issue 518059: Make String.prototype.replace a tiny bit faster by avoiding... (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: Created 10 years, 11 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | 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-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 178 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 // This has the same size as the lastMatchInfo array, and can be used for 189 // This has the same size as the lastMatchInfo array, and can be used for
190 // functions that expect that structure to be returned. It is used when the 190 // functions that expect that structure to be returned. It is used when the
191 // needle is a string rather than a regexp. In this case we can't update 191 // needle is a string rather than a regexp. In this case we can't update
192 // lastMatchArray without erroneously affecting the properties on the global 192 // lastMatchArray without erroneously affecting the properties on the global
193 // RegExp object. 193 // RegExp object.
194 var reusableMatchInfo = [2, "", "", -1, -1]; 194 var reusableMatchInfo = [2, "", "", -1, -1];
195 195
196 196
197 // ECMA-262, section 15.5.4.11 197 // ECMA-262, section 15.5.4.11
198 function StringReplace(search, replace) { 198 function StringReplace(search, replace) {
199 var subject = ToString(this); 199 var subject = IS_STRING(this) ? this : ToString(this);
200 200
201 // Delegate to one of the regular expression variants if necessary. 201 // Delegate to one of the regular expression variants if necessary.
202 if (IS_REGEXP(search)) { 202 if (IS_REGEXP(search)) {
203 %_Log('regexp', 'regexp-replace,%0r,%1S', [search, subject]); 203 %_Log('regexp', 'regexp-replace,%0r,%1S', [search, subject]);
204 if (IS_FUNCTION(replace)) { 204 if (IS_FUNCTION(replace)) {
205 return StringReplaceRegExpWithFunction(subject, search, replace); 205 return StringReplaceRegExpWithFunction(subject, search, replace);
206 } else { 206 } else {
207 return StringReplaceRegExp(subject, search, replace); 207 return StringReplaceRegExp(subject, search, replace);
208 } 208 }
209 } 209 }
210 210
211 // Convert the search argument to a string and search for it. 211 // Convert the search argument to a string and search for it.
212 search = ToString(search); 212 search = IS_STRING(search) ? search : ToString(search);
213 var start = %StringIndexOf(subject, search, 0); 213 var start = %StringIndexOf(subject, search, 0);
214 if (start < 0) return subject; 214 if (start < 0) return subject;
215 var end = start + search.length; 215 var end = start + search.length;
216 216
217 var builder = new ReplaceResultBuilder(subject); 217 var builder = new ReplaceResultBuilder(subject);
218 // prefix 218 // prefix
219 builder.addSpecialSlice(0, start); 219 builder.addSpecialSlice(0, start);
220 220
221 // Compute the string to replace with. 221 // Compute the string to replace with.
222 if (IS_FUNCTION(replace)) { 222 if (IS_FUNCTION(replace)) {
223 builder.add(replace.call(null, search, start, subject)); 223 builder.add(replace.call(null, search, start, subject));
224 } else { 224 } else {
225 reusableMatchInfo[CAPTURE0] = start; 225 reusableMatchInfo[CAPTURE0] = start;
226 reusableMatchInfo[CAPTURE1] = end; 226 reusableMatchInfo[CAPTURE1] = end;
227 ExpandReplacement(ToString(replace), subject, reusableMatchInfo, builder); 227 if (!IS_STRING(replace)) replace = ToString(replace);
228 ExpandReplacement(replace, subject, reusableMatchInfo, builder);
228 } 229 }
229 230
230 // suffix 231 // suffix
231 builder.addSpecialSlice(end, subject.length); 232 builder.addSpecialSlice(end, subject.length);
232 233
233 return builder.generate(); 234 return builder.generate();
234 } 235 }
235 236
236 237
237 // Helper function for regular expressions in String.prototype.replace. 238 // Helper function for regular expressions in String.prototype.replace.
(...skipping 653 matching lines...) Expand 10 before | Expand all | Expand 10 after
891 "small", StringSmall, 892 "small", StringSmall,
892 "strike", StringStrike, 893 "strike", StringStrike,
893 "sub", StringSub, 894 "sub", StringSub,
894 "sup", StringSup, 895 "sup", StringSup,
895 "toJSON", StringToJSON 896 "toJSON", StringToJSON
896 )); 897 ));
897 } 898 }
898 899
899 900
900 SetupString(); 901 SetupString();
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698