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

Side by Side Diff: src/regexp.js

Issue 1513004: Revert svn r4269. (Closed)
Patch Set: Created 10 years, 8 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
« no previous file with comments | « no previous file | src/runtime.h » ('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 326 matching lines...) Expand 10 before | Expand all | Expand 10 after
337 result += 'm'; 337 result += 'm';
338 return result; 338 return result;
339 } 339 }
340 340
341 341
342 // Getters for the static properties lastMatch, lastParen, leftContext, and 342 // Getters for the static properties lastMatch, lastParen, leftContext, and
343 // rightContext of the RegExp constructor. The properties are computed based 343 // rightContext of the RegExp constructor. The properties are computed based
344 // on the captures array of the last successful match and the subject string 344 // on the captures array of the last successful match and the subject string
345 // of the last successful match. 345 // of the last successful match.
346 function RegExpGetLastMatch() { 346 function RegExpGetLastMatch() {
347 if (lastMatchInfoOverride) { return lastMatchInfoOverride[0]; }
348 var regExpSubject = LAST_SUBJECT(lastMatchInfo); 347 var regExpSubject = LAST_SUBJECT(lastMatchInfo);
349 return SubString(regExpSubject, 348 return SubString(regExpSubject,
350 lastMatchInfo[CAPTURE0], 349 lastMatchInfo[CAPTURE0],
351 lastMatchInfo[CAPTURE1]); 350 lastMatchInfo[CAPTURE1]);
352 } 351 }
353 352
354 353
355 function RegExpGetLastParen() { 354 function RegExpGetLastParen() {
356 if (lastMatchInfoOverride) {
357 var override = lastMatchInfoOverride;
358 if (override.length <= 3) return '';
359 return override[override.length - 3];
360 }
361 var length = NUMBER_OF_CAPTURES(lastMatchInfo); 355 var length = NUMBER_OF_CAPTURES(lastMatchInfo);
362 if (length <= 2) return ''; // There were no captures. 356 if (length <= 2) return ''; // There were no captures.
363 // We match the SpiderMonkey behavior: return the substring defined by the 357 // We match the SpiderMonkey behavior: return the substring defined by the
364 // last pair (after the first pair) of elements of the capture array even if 358 // last pair (after the first pair) of elements of the capture array even if
365 // it is empty. 359 // it is empty.
366 var regExpSubject = LAST_SUBJECT(lastMatchInfo); 360 var regExpSubject = LAST_SUBJECT(lastMatchInfo);
367 var start = lastMatchInfo[CAPTURE(length - 2)]; 361 var start = lastMatchInfo[CAPTURE(length - 2)];
368 var end = lastMatchInfo[CAPTURE(length - 1)]; 362 var end = lastMatchInfo[CAPTURE(length - 1)];
369 if (start != -1 && end != -1) { 363 if (start != -1 && end != -1) {
370 return SubString(regExpSubject, start, end); 364 return SubString(regExpSubject, start, end);
371 } 365 }
372 return ""; 366 return "";
373 } 367 }
374 368
375 369
376 function RegExpGetLeftContext() { 370 function RegExpGetLeftContext() {
377 var start_index; 371 return SubString(LAST_SUBJECT(lastMatchInfo),
378 var subject; 372 0,
379 if (!lastMatchInfoOverride) { 373 lastMatchInfo[CAPTURE0]);
380 start_index = lastMatchInfo[CAPTURE0];
381 subject = LAST_SUBJECT(lastMatchInfo);
382 } else {
383 var override = lastMatchInfoOverride;
384 start_index = override[override.length - 2];
385 subject = override[override.length - 1];
386 }
387 return SubString(subject, 0, start_index);
388 } 374 }
389 375
390 376
391 function RegExpGetRightContext() { 377 function RegExpGetRightContext() {
392 var start_index; 378 var subject = LAST_SUBJECT(lastMatchInfo);
393 var subject; 379 return SubString(subject,
394 if (!lastMatchInfoOverride) { 380 lastMatchInfo[CAPTURE1],
395 start_index = lastMatchInfo[CAPTURE1]; 381 subject.length);
396 subject = LAST_SUBJECT(lastMatchInfo);
397 } else {
398 var override = lastMatchInfoOverride;
399 subject = override[override.length - 1];
400 start_index = override[override.length - 2] + subject.length;
401 }
402 return SubString(subject, start_index, subject.length);
403 } 382 }
404 383
405 384
406 // The properties $1..$9 are the first nine capturing substrings of the last 385 // The properties $1..$9 are the first nine capturing substrings of the last
407 // successful match, or ''. The function RegExpMakeCaptureGetter will be 386 // successful match, or ''. The function RegExpMakeCaptureGetter will be
408 // called with indices from 1 to 9. 387 // called with indices from 1 to 9.
409 function RegExpMakeCaptureGetter(n) { 388 function RegExpMakeCaptureGetter(n) {
410 return function() { 389 return function() {
411 if (lastMatchInfoOverride) {
412 if (n < lastMatchInfoOverride.length - 2) return lastMatchInfoOverride[n];
413 return '';
414 }
415 var index = n * 2; 390 var index = n * 2;
416 if (index >= NUMBER_OF_CAPTURES(lastMatchInfo)) return ''; 391 if (index >= NUMBER_OF_CAPTURES(lastMatchInfo)) return '';
417 var matchStart = lastMatchInfo[CAPTURE(index)]; 392 var matchStart = lastMatchInfo[CAPTURE(index)];
418 var matchEnd = lastMatchInfo[CAPTURE(index + 1)]; 393 var matchEnd = lastMatchInfo[CAPTURE(index + 1)];
419 if (matchStart == -1 || matchEnd == -1) return ''; 394 if (matchStart == -1 || matchEnd == -1) return '';
420 return SubString(LAST_SUBJECT(lastMatchInfo), matchStart, matchEnd); 395 return SubString(LAST_SUBJECT(lastMatchInfo), matchStart, matchEnd);
421 }; 396 };
422 } 397 }
423 398
424 399
425 // Property of the builtins object for recording the result of the last 400 // Property of the builtins object for recording the result of the last
426 // regexp match. The property lastMatchInfo includes the matchIndices 401 // regexp match. The property lastMatchInfo includes the matchIndices
427 // array of the last successful regexp match (an array of start/end index 402 // array of the last successful regexp match (an array of start/end index
428 // pairs for the match and all the captured substrings), the invariant is 403 // pairs for the match and all the captured substrings), the invariant is
429 // that there are at least two capture indeces. The array also contains 404 // that there are at least two capture indeces. The array also contains
430 // the subject string for the last successful match. 405 // the subject string for the last successful match.
431 var lastMatchInfo = [ 406 var lastMatchInfo = [
432 2, // REGEXP_NUMBER_OF_CAPTURES 407 2, // REGEXP_NUMBER_OF_CAPTURES
433 "", // Last subject. 408 "", // Last subject.
434 void 0, // Last input - settable with RegExpSetInput. 409 void 0, // Last input - settable with RegExpSetInput.
435 0, // REGEXP_FIRST_CAPTURE + 0 410 0, // REGEXP_FIRST_CAPTURE + 0
436 0, // REGEXP_FIRST_CAPTURE + 1 411 0, // REGEXP_FIRST_CAPTURE + 1
437 ]; 412 ];
438 413
439 // Override last match info with an array of actual substrings.
440 // Used internally by replace regexp with function.
441 // The array has the format of an "apply" argument for a replacement
442 // function.
443 var lastMatchInfoOverride = null;
444
445 // ------------------------------------------------------------------- 414 // -------------------------------------------------------------------
446 415
447 function SetupRegExp() { 416 function SetupRegExp() {
448 %FunctionSetInstanceClassName($RegExp, 'RegExp'); 417 %FunctionSetInstanceClassName($RegExp, 'RegExp');
449 %FunctionSetPrototype($RegExp, new $Object()); 418 %FunctionSetPrototype($RegExp, new $Object());
450 %SetProperty($RegExp.prototype, 'constructor', $RegExp, DONT_ENUM); 419 %SetProperty($RegExp.prototype, 'constructor', $RegExp, DONT_ENUM);
451 %SetCode($RegExp, RegExpConstructor); 420 %SetCode($RegExp, RegExpConstructor);
452 421
453 InstallFunctions($RegExp.prototype, DONT_ENUM, $Array( 422 InstallFunctions($RegExp.prototype, DONT_ENUM, $Array(
454 "exec", RegExpExec, 423 "exec", RegExpExec,
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
519 %DefineAccessor($RegExp, "$'", SETTER, NoOpSetter, DONT_ENUM | DONT_DELETE); 488 %DefineAccessor($RegExp, "$'", SETTER, NoOpSetter, DONT_ENUM | DONT_DELETE);
520 489
521 for (var i = 1; i < 10; ++i) { 490 for (var i = 1; i < 10; ++i) {
522 %DefineAccessor($RegExp, '$' + i, GETTER, RegExpMakeCaptureGetter(i), DONT_D ELETE); 491 %DefineAccessor($RegExp, '$' + i, GETTER, RegExpMakeCaptureGetter(i), DONT_D ELETE);
523 %DefineAccessor($RegExp, '$' + i, SETTER, NoOpSetter, DONT_DELETE); 492 %DefineAccessor($RegExp, '$' + i, SETTER, NoOpSetter, DONT_DELETE);
524 } 493 }
525 } 494 }
526 495
527 496
528 SetupRegExp(); 497 SetupRegExp();
OLDNEW
« no previous file with comments | « no previous file | src/runtime.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698