OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 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 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 if (result !== null) lastMatchInfoOverride = null; | 125 if (result !== null) lastMatchInfoOverride = null; |
126 return result; | 126 return result; |
127 } | 127 } |
128 | 128 |
129 | 129 |
130 function BuildResultFromMatchInfo(lastMatchInfo, s) { | 130 function BuildResultFromMatchInfo(lastMatchInfo, s) { |
131 var numResults = NUMBER_OF_CAPTURES(lastMatchInfo) >> 1; | 131 var numResults = NUMBER_OF_CAPTURES(lastMatchInfo) >> 1; |
132 var start = lastMatchInfo[CAPTURE0]; | 132 var start = lastMatchInfo[CAPTURE0]; |
133 var end = lastMatchInfo[CAPTURE1]; | 133 var end = lastMatchInfo[CAPTURE1]; |
134 var result = %_RegExpConstructResult(numResults, start, s); | 134 var result = %_RegExpConstructResult(numResults, start, s); |
135 if (start + 1 == end) { | 135 result[0] = %_SubString(s, start, end); |
136 result[0] = %_StringCharAt(s, start); | |
137 } else { | |
138 result[0] = %_SubString(s, start, end); | |
139 } | |
140 var j = REGEXP_FIRST_CAPTURE + 2; | 136 var j = REGEXP_FIRST_CAPTURE + 2; |
141 for (var i = 1; i < numResults; i++) { | 137 for (var i = 1; i < numResults; i++) { |
142 start = lastMatchInfo[j++]; | 138 start = lastMatchInfo[j++]; |
143 if (start != -1) { | 139 if (start != -1) { |
144 end = lastMatchInfo[j]; | 140 end = lastMatchInfo[j]; |
145 if (start + 1 == end) { | 141 result[i] = %_SubString(s, start, end); |
146 result[i] = %_StringCharAt(s, start); | |
147 } else { | |
148 result[i] = %_SubString(s, start, end); | |
149 } | |
150 } | 142 } |
151 j++; | 143 j++; |
152 } | 144 } |
153 return result; | 145 return result; |
154 } | 146 } |
155 | 147 |
156 | 148 |
157 function RegExpExecNoTests(regexp, string, start) { | 149 function RegExpExecNoTests(regexp, string, start) { |
158 // Must be called with RegExp, string and positive integer as arguments. | 150 // Must be called with RegExp, string and positive integer as arguments. |
159 var matchInfo = %_RegExpExec(regexp, string, start, lastMatchInfo); | 151 var matchInfo = %_RegExpExec(regexp, string, start, lastMatchInfo); |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
263 } | 255 } |
264 lastMatchInfoOverride = null; | 256 lastMatchInfoOverride = null; |
265 return true; | 257 return true; |
266 } | 258 } |
267 } | 259 } |
268 | 260 |
269 function TrimRegExp(regexp) { | 261 function TrimRegExp(regexp) { |
270 if (!%_ObjectEquals(regexp_key, regexp)) { | 262 if (!%_ObjectEquals(regexp_key, regexp)) { |
271 regexp_key = regexp; | 263 regexp_key = regexp; |
272 regexp_val = | 264 regexp_val = |
273 new $RegExp(SubString(regexp.source, 2, regexp.source.length), | 265 new $RegExp(%_SubString(regexp.source, 2, regexp.source.length), |
274 (regexp.ignoreCase ? regexp.multiline ? "im" : "i" | 266 (regexp.ignoreCase ? regexp.multiline ? "im" : "i" |
275 : regexp.multiline ? "m" : "")); | 267 : regexp.multiline ? "m" : "")); |
276 } | 268 } |
277 return regexp_val; | 269 return regexp_val; |
278 } | 270 } |
279 | 271 |
280 | 272 |
281 function RegExpToString() { | 273 function RegExpToString() { |
282 if (!IS_REGEXP(this)) { | 274 if (!IS_REGEXP(this)) { |
283 throw MakeTypeError('incompatible_method_receiver', | 275 throw MakeTypeError('incompatible_method_receiver', |
284 ['RegExp.prototype.toString', this]); | 276 ['RegExp.prototype.toString', this]); |
285 } | 277 } |
286 var result = '/' + this.source + '/'; | 278 var result = '/' + this.source + '/'; |
287 if (this.global) result += 'g'; | 279 if (this.global) result += 'g'; |
288 if (this.ignoreCase) result += 'i'; | 280 if (this.ignoreCase) result += 'i'; |
289 if (this.multiline) result += 'm'; | 281 if (this.multiline) result += 'm'; |
290 return result; | 282 return result; |
291 } | 283 } |
292 | 284 |
293 | 285 |
294 // Getters for the static properties lastMatch, lastParen, leftContext, and | 286 // Getters for the static properties lastMatch, lastParen, leftContext, and |
295 // rightContext of the RegExp constructor. The properties are computed based | 287 // rightContext of the RegExp constructor. The properties are computed based |
296 // on the captures array of the last successful match and the subject string | 288 // on the captures array of the last successful match and the subject string |
297 // of the last successful match. | 289 // of the last successful match. |
298 function RegExpGetLastMatch() { | 290 function RegExpGetLastMatch() { |
299 if (lastMatchInfoOverride !== null) { | 291 if (lastMatchInfoOverride !== null) { |
300 return OVERRIDE_MATCH(lastMatchInfoOverride); | 292 return OVERRIDE_MATCH(lastMatchInfoOverride); |
301 } | 293 } |
302 var regExpSubject = LAST_SUBJECT(lastMatchInfo); | 294 var regExpSubject = LAST_SUBJECT(lastMatchInfo); |
303 return SubString(regExpSubject, | 295 return %_SubString(regExpSubject, |
304 lastMatchInfo[CAPTURE0], | 296 lastMatchInfo[CAPTURE0], |
305 lastMatchInfo[CAPTURE1]); | 297 lastMatchInfo[CAPTURE1]); |
306 } | 298 } |
307 | 299 |
308 | 300 |
309 function RegExpGetLastParen() { | 301 function RegExpGetLastParen() { |
310 if (lastMatchInfoOverride) { | 302 if (lastMatchInfoOverride) { |
311 var override = lastMatchInfoOverride; | 303 var override = lastMatchInfoOverride; |
312 if (override.length <= 3) return ''; | 304 if (override.length <= 3) return ''; |
313 return override[override.length - 3]; | 305 return override[override.length - 3]; |
314 } | 306 } |
315 var length = NUMBER_OF_CAPTURES(lastMatchInfo); | 307 var length = NUMBER_OF_CAPTURES(lastMatchInfo); |
316 if (length <= 2) return ''; // There were no captures. | 308 if (length <= 2) return ''; // There were no captures. |
317 // We match the SpiderMonkey behavior: return the substring defined by the | 309 // We match the SpiderMonkey behavior: return the substring defined by the |
318 // last pair (after the first pair) of elements of the capture array even if | 310 // last pair (after the first pair) of elements of the capture array even if |
319 // it is empty. | 311 // it is empty. |
320 var regExpSubject = LAST_SUBJECT(lastMatchInfo); | 312 var regExpSubject = LAST_SUBJECT(lastMatchInfo); |
321 var start = lastMatchInfo[CAPTURE(length - 2)]; | 313 var start = lastMatchInfo[CAPTURE(length - 2)]; |
322 var end = lastMatchInfo[CAPTURE(length - 1)]; | 314 var end = lastMatchInfo[CAPTURE(length - 1)]; |
323 if (start != -1 && end != -1) { | 315 if (start != -1 && end != -1) { |
324 return SubString(regExpSubject, start, end); | 316 return %_SubString(regExpSubject, start, end); |
325 } | 317 } |
326 return ""; | 318 return ""; |
327 } | 319 } |
328 | 320 |
329 | 321 |
330 function RegExpGetLeftContext() { | 322 function RegExpGetLeftContext() { |
331 var start_index; | 323 var start_index; |
332 var subject; | 324 var subject; |
333 if (!lastMatchInfoOverride) { | 325 if (!lastMatchInfoOverride) { |
334 start_index = lastMatchInfo[CAPTURE0]; | 326 start_index = lastMatchInfo[CAPTURE0]; |
335 subject = LAST_SUBJECT(lastMatchInfo); | 327 subject = LAST_SUBJECT(lastMatchInfo); |
336 } else { | 328 } else { |
337 var override = lastMatchInfoOverride; | 329 var override = lastMatchInfoOverride; |
338 start_index = OVERRIDE_POS(override); | 330 start_index = OVERRIDE_POS(override); |
339 subject = OVERRIDE_SUBJECT(override); | 331 subject = OVERRIDE_SUBJECT(override); |
340 } | 332 } |
341 return SubString(subject, 0, start_index); | 333 return %_SubString(subject, 0, start_index); |
342 } | 334 } |
343 | 335 |
344 | 336 |
345 function RegExpGetRightContext() { | 337 function RegExpGetRightContext() { |
346 var start_index; | 338 var start_index; |
347 var subject; | 339 var subject; |
348 if (!lastMatchInfoOverride) { | 340 if (!lastMatchInfoOverride) { |
349 start_index = lastMatchInfo[CAPTURE1]; | 341 start_index = lastMatchInfo[CAPTURE1]; |
350 subject = LAST_SUBJECT(lastMatchInfo); | 342 subject = LAST_SUBJECT(lastMatchInfo); |
351 } else { | 343 } else { |
352 var override = lastMatchInfoOverride; | 344 var override = lastMatchInfoOverride; |
353 subject = OVERRIDE_SUBJECT(override); | 345 subject = OVERRIDE_SUBJECT(override); |
354 var match = OVERRIDE_MATCH(override); | 346 var match = OVERRIDE_MATCH(override); |
355 start_index = OVERRIDE_POS(override) + match.length; | 347 start_index = OVERRIDE_POS(override) + match.length; |
356 } | 348 } |
357 return SubString(subject, start_index, subject.length); | 349 return %_SubString(subject, start_index, subject.length); |
358 } | 350 } |
359 | 351 |
360 | 352 |
361 // The properties $1..$9 are the first nine capturing substrings of the last | 353 // The properties $1..$9 are the first nine capturing substrings of the last |
362 // successful match, or ''. The function RegExpMakeCaptureGetter will be | 354 // successful match, or ''. The function RegExpMakeCaptureGetter will be |
363 // called with indices from 1 to 9. | 355 // called with indices from 1 to 9. |
364 function RegExpMakeCaptureGetter(n) { | 356 function RegExpMakeCaptureGetter(n) { |
365 return function() { | 357 return function() { |
366 if (lastMatchInfoOverride) { | 358 if (lastMatchInfoOverride) { |
367 if (n < lastMatchInfoOverride.length - 2) { | 359 if (n < lastMatchInfoOverride.length - 2) { |
368 return OVERRIDE_CAPTURE(lastMatchInfoOverride, n); | 360 return OVERRIDE_CAPTURE(lastMatchInfoOverride, n); |
369 } | 361 } |
370 return ''; | 362 return ''; |
371 } | 363 } |
372 var index = n * 2; | 364 var index = n * 2; |
373 if (index >= NUMBER_OF_CAPTURES(lastMatchInfo)) return ''; | 365 if (index >= NUMBER_OF_CAPTURES(lastMatchInfo)) return ''; |
374 var matchStart = lastMatchInfo[CAPTURE(index)]; | 366 var matchStart = lastMatchInfo[CAPTURE(index)]; |
375 var matchEnd = lastMatchInfo[CAPTURE(index + 1)]; | 367 var matchEnd = lastMatchInfo[CAPTURE(index + 1)]; |
376 if (matchStart == -1 || matchEnd == -1) return ''; | 368 if (matchStart == -1 || matchEnd == -1) return ''; |
377 return SubString(LAST_SUBJECT(lastMatchInfo), matchStart, matchEnd); | 369 return %_SubString(LAST_SUBJECT(lastMatchInfo), matchStart, matchEnd); |
378 }; | 370 }; |
379 } | 371 } |
380 | 372 |
381 | 373 |
382 // Property of the builtins object for recording the result of the last | 374 // Property of the builtins object for recording the result of the last |
383 // regexp match. The property lastMatchInfo includes the matchIndices | 375 // regexp match. The property lastMatchInfo includes the matchIndices |
384 // array of the last successful regexp match (an array of start/end index | 376 // array of the last successful regexp match (an array of start/end index |
385 // pairs for the match and all the captured substrings), the invariant is | 377 // pairs for the match and all the captured substrings), the invariant is |
386 // that there are at least two capture indeces. The array also contains | 378 // that there are at least two capture indeces. The array also contains |
387 // the subject string for the last successful match. | 379 // the subject string for the last successful match. |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
480 | 472 |
481 for (var i = 1; i < 10; ++i) { | 473 for (var i = 1; i < 10; ++i) { |
482 %DefineOrRedefineAccessorProperty($RegExp, '$' + i, | 474 %DefineOrRedefineAccessorProperty($RegExp, '$' + i, |
483 RegExpMakeCaptureGetter(i), NoOpSetter, | 475 RegExpMakeCaptureGetter(i), NoOpSetter, |
484 DONT_DELETE); | 476 DONT_DELETE); |
485 } | 477 } |
486 %ToFastProperties($RegExp); | 478 %ToFastProperties($RegExp); |
487 } | 479 } |
488 | 480 |
489 SetUpRegExp(); | 481 SetUpRegExp(); |
OLD | NEW |