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

Side by Side Diff: src/regexp.js

Issue 5959002: Improve regexp split, replace and test. (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: '' Created 10 years 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 | src/string.js » ('j') | 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 14 matching lines...) Expand all
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 27
28 // Expect $Object = global.Object; 28 // Expect $Object = global.Object;
29 // Expect $Array = global.Array; 29 // Expect $Array = global.Array;
30 30
31 const $RegExp = global.RegExp; 31 const $RegExp = global.RegExp;
32 32
33 // A recursive descent parser for Patterns according to the grammar of 33 // A recursive descent parser for Patterns according to the grammar of
34 // ECMA-262 15.10.1, with deviations noted below. 34 // ECMA-262 15.10.1, with deviations noted below.
35 function DoConstructRegExp(object, pattern, flags, isConstructorCall) { 35 function DoConstructRegExp(object, pattern, flags) {
36 // RegExp : Called as constructor; see ECMA-262, section 15.10.4. 36 // RegExp : Called as constructor; see ECMA-262, section 15.10.4.
37 if (IS_REGEXP(pattern)) { 37 if (IS_REGEXP(pattern)) {
38 if (!IS_UNDEFINED(flags)) { 38 if (!IS_UNDEFINED(flags)) {
39 throw MakeTypeError('regexp_flags', []); 39 throw MakeTypeError('regexp_flags', []);
40 } 40 }
41 flags = (pattern.global ? 'g' : '') 41 flags = (pattern.global ? 'g' : '')
42 + (pattern.ignoreCase ? 'i' : '') 42 + (pattern.ignoreCase ? 'i' : '')
43 + (pattern.multiline ? 'm' : ''); 43 + (pattern.multiline ? 'm' : '');
44 pattern = pattern.source; 44 pattern = pattern.source;
45 } 45 }
(...skipping 27 matching lines...) Expand all
73 73
74 %RegExpInitializeObject(object, pattern, global, ignoreCase, multiline); 74 %RegExpInitializeObject(object, pattern, global, ignoreCase, multiline);
75 75
76 // Call internal function to compile the pattern. 76 // Call internal function to compile the pattern.
77 %RegExpCompile(object, pattern, flags); 77 %RegExpCompile(object, pattern, flags);
78 } 78 }
79 79
80 80
81 function RegExpConstructor(pattern, flags) { 81 function RegExpConstructor(pattern, flags) {
82 if (%_IsConstructCall()) { 82 if (%_IsConstructCall()) {
83 DoConstructRegExp(this, pattern, flags, true); 83 DoConstructRegExp(this, pattern, flags);
84 } else { 84 } else {
85 // RegExp : Called as function; see ECMA-262, section 15.10.3.1. 85 // RegExp : Called as function; see ECMA-262, section 15.10.3.1.
86 if (IS_REGEXP(pattern) && IS_UNDEFINED(flags)) { 86 if (IS_REGEXP(pattern) && IS_UNDEFINED(flags)) {
87 return pattern; 87 return pattern;
88 } 88 }
89 return new $RegExp(pattern, flags); 89 return new $RegExp(pattern, flags);
90 } 90 }
91 } 91 }
92 92
93 93
94 // Deprecated RegExp.prototype.compile method. We behave like the constructor 94 // Deprecated RegExp.prototype.compile method. We behave like the constructor
95 // were called again. In SpiderMonkey, this method returns the regexp object. 95 // were called again. In SpiderMonkey, this method returns the regexp object.
96 // In JSC, it returns undefined. For compatibility with JSC, we match their 96 // In JSC, it returns undefined. For compatibility with JSC, we match their
97 // behavior. 97 // behavior.
98 function CompileRegExp(pattern, flags) { 98 function CompileRegExp(pattern, flags) {
99 // Both JSC and SpiderMonkey treat a missing pattern argument as the 99 // Both JSC and SpiderMonkey treat a missing pattern argument as the
100 // empty subject string, and an actual undefined value passed as the 100 // empty subject string, and an actual undefined value passed as the
101 // pattern as the string 'undefined'. Note that JSC is inconsistent 101 // pattern as the string 'undefined'. Note that JSC is inconsistent
102 // here, treating undefined values differently in 102 // here, treating undefined values differently in
103 // RegExp.prototype.compile and in the constructor, where they are 103 // RegExp.prototype.compile and in the constructor, where they are
104 // the empty string. For compatibility with JSC, we match their 104 // the empty string. For compatibility with JSC, we match their
105 // behavior. 105 // behavior.
106 if (IS_UNDEFINED(pattern) && %_ArgumentsLength() != 0) { 106 if (IS_UNDEFINED(pattern) && %_ArgumentsLength() != 0) {
107 DoConstructRegExp(this, 'undefined', flags, false); 107 DoConstructRegExp(this, 'undefined', flags);
108 } else { 108 } else {
109 DoConstructRegExp(this, pattern, flags, false); 109 DoConstructRegExp(this, pattern, flags);
110 } 110 }
111 } 111 }
112 112
113 113
114 function DoRegExpExec(regexp, string, index) { 114 function DoRegExpExec(regexp, string, index) {
115 var result = %_RegExpExec(regexp, string, index, lastMatchInfo); 115 var result = %_RegExpExec(regexp, string, index, lastMatchInfo);
116 if (result !== null) lastMatchInfoOverride = null; 116 if (result !== null) lastMatchInfoOverride = null;
117 return result; 117 return result;
118 } 118 }
119 119
(...skipping 23 matching lines...) Expand all
143 // property from the global object since this may change. 143 // property from the global object since this may change.
144 result[i] = void 0; 144 result[i] = void 0;
145 } 145 }
146 } 146 }
147 return result; 147 return result;
148 } 148 }
149 149
150 150
151 function RegExpExecNoTests(regexp, string, start) { 151 function RegExpExecNoTests(regexp, string, start) {
152 // Must be called with RegExp, string and positive integer as arguments. 152 // Must be called with RegExp, string and positive integer as arguments.
153 var matchInfo = DoRegExpExec(regexp, string, start); 153 var matchInfo = %_RegExpExec(regexp, string, start, lastMatchInfo);
154 var result = null;
155 if (matchInfo !== null) { 154 if (matchInfo !== null) {
156 result = BuildResultFromMatchInfo(matchInfo, string); 155 lastMatchInfoOverride = null;
156 return BuildResultFromMatchInfo(matchInfo, string);
157 } 157 }
158 return result; 158 return null;
159 } 159 }
160 160
161 161
162 function RegExpExec(string) { 162 function RegExpExec(string) {
163 if (!IS_REGEXP(this)) { 163 if (!IS_REGEXP(this)) {
164 throw MakeTypeError('incompatible_method_receiver', 164 throw MakeTypeError('incompatible_method_receiver',
165 ['RegExp.prototype.exec', this]); 165 ['RegExp.prototype.exec', this]);
166 } 166 }
167 167
168 if (%_ArgumentsLength() === 0) { 168 if (%_ArgumentsLength() === 0) {
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
254 } else { 254 } else {
255 // Non-global regexp. 255 // Non-global regexp.
256 // Remove irrelevant preceeding '.*' in a non-global test regexp. 256 // Remove irrelevant preceeding '.*' in a non-global test regexp.
257 // The expression checks whether this.source starts with '.*' and 257 // The expression checks whether this.source starts with '.*' and
258 // that the third char is not a '?'. 258 // that the third char is not a '?'.
259 if (%_StringCharCodeAt(this.source, 0) == 46 && // '.' 259 if (%_StringCharCodeAt(this.source, 0) == 46 && // '.'
260 %_StringCharCodeAt(this.source, 1) == 42 && // '*' 260 %_StringCharCodeAt(this.source, 1) == 42 && // '*'
261 %_StringCharCodeAt(this.source, 2) != 63) { // '?' 261 %_StringCharCodeAt(this.source, 2) != 63) { // '?'
262 if (!%_ObjectEquals(regexp_key, this)) { 262 if (!%_ObjectEquals(regexp_key, this)) {
263 regexp_key = this; 263 regexp_key = this;
264 regexp_val = new $RegExp(this.source.substring(2, this.source.length), 264 regexp_val = new $RegExp(SubString(this.source, 2, this.source.length),
265 (this.ignoreCase ? 'i' : '') 265 (!this.ignoreCase
266 + (this.multiline ? 'm' : '')); 266 ? !this.multiline ? "" : "m"
267 : !this.multiline ? "i" : "im"));
267 } 268 }
268 if (!regexp_val.test(string)) return false; 269 if (%_RegExpExec(regexp_val, string, 0, lastMatchInfo) === null) {
270 return false;
271 }
269 } 272 }
270 %_Log('regexp', 'regexp-exec,%0r,%1S,%2i', [this, string, lastIndex]); 273 %_Log('regexp', 'regexp-exec,%0r,%1S,%2i', [this, string, lastIndex]);
271 // matchIndices is either null or the lastMatchInfo array. 274 // matchIndices is either null or the lastMatchInfo array.
272 var matchIndices = %_RegExpExec(this, string, 0, lastMatchInfo); 275 var matchIndices = %_RegExpExec(this, string, 0, lastMatchInfo);
273 if (matchIndices === null) return false; 276 if (matchIndices === null) return false;
274 lastMatchInfoOverride = null; 277 lastMatchInfoOverride = null;
275 return true; 278 return true;
276 } 279 }
277 } 280 }
278 281
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after
471 %DefineAccessor($RegExp, "$'", SETTER, NoOpSetter, DONT_ENUM | DONT_DELETE); 474 %DefineAccessor($RegExp, "$'", SETTER, NoOpSetter, DONT_ENUM | DONT_DELETE);
472 475
473 for (var i = 1; i < 10; ++i) { 476 for (var i = 1; i < 10; ++i) {
474 %DefineAccessor($RegExp, '$' + i, GETTER, RegExpMakeCaptureGetter(i), DONT_D ELETE); 477 %DefineAccessor($RegExp, '$' + i, GETTER, RegExpMakeCaptureGetter(i), DONT_D ELETE);
475 %DefineAccessor($RegExp, '$' + i, SETTER, NoOpSetter, DONT_DELETE); 478 %DefineAccessor($RegExp, '$' + i, SETTER, NoOpSetter, DONT_DELETE);
476 } 479 }
477 } 480 }
478 481
479 482
480 SetupRegExp(); 483 SetupRegExp();
OLDNEW
« no previous file with comments | « no previous file | src/string.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698