OLD | NEW |
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 352 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
363 | 363 |
364 | 364 |
365 // Helper function for replacing regular expressions with the result of a | 365 // Helper function for replacing regular expressions with the result of a |
366 // function application in String.prototype.replace. The function application | 366 // function application in String.prototype.replace. The function application |
367 // must be interleaved with the regexp matching (contrary to ECMA-262 | 367 // must be interleaved with the regexp matching (contrary to ECMA-262 |
368 // 15.5.4.11) to mimic SpiderMonkey and KJS behavior when the function uses | 368 // 15.5.4.11) to mimic SpiderMonkey and KJS behavior when the function uses |
369 // the static properties of the RegExp constructor. Example: | 369 // the static properties of the RegExp constructor. Example: |
370 // 'abcd'.replace(/(.)/g, function() { return RegExp.$1; } | 370 // 'abcd'.replace(/(.)/g, function() { return RegExp.$1; } |
371 // should be 'abcd' and not 'dddd' (or anything else). | 371 // should be 'abcd' and not 'dddd' (or anything else). |
372 function StringReplaceRegExpWithFunction(subject, regexp, replace) { | 372 function StringReplaceRegExpWithFunction(subject, regexp, replace) { |
373 var result = new ReplaceResultBuilder(subject); | |
374 var lastMatchInfo = DoRegExpExec(regexp, subject, 0); | 373 var lastMatchInfo = DoRegExpExec(regexp, subject, 0); |
375 if (IS_NULL(lastMatchInfo)) return subject; | 374 if (IS_NULL(lastMatchInfo)) return subject; |
376 | 375 |
| 376 var result = new ReplaceResultBuilder(subject); |
377 // There's at least one match. If the regexp is global, we have to loop | 377 // There's at least one match. If the regexp is global, we have to loop |
378 // over all matches. The loop is not in C++ code here like the one in | 378 // over all matches. The loop is not in C++ code here like the one in |
379 // RegExp.prototype.exec, because of the interleaved function application. | 379 // RegExp.prototype.exec, because of the interleaved function application. |
380 // Unfortunately, that means this code is nearly duplicated, here and in | 380 // Unfortunately, that means this code is nearly duplicated, here and in |
381 // jsregexp.cc. | 381 // jsregexp.cc. |
382 if (regexp.global) { | 382 if (regexp.global) { |
383 var previous = 0; | 383 var previous = 0; |
384 do { | 384 do { |
385 result.addSpecialSlice(previous, lastMatchInfo[CAPTURE0]); | 385 result.addSpecialSlice(previous, lastMatchInfo[CAPTURE0]); |
386 var startOfMatch = lastMatchInfo[CAPTURE0]; | 386 var startOfMatch = lastMatchInfo[CAPTURE0]; |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
491 if (num_c < 0) | 491 if (num_c < 0) |
492 num_c = 0; | 492 num_c = 0; |
493 | 493 |
494 return SubString(s, start_i, start_i + num_c); | 494 return SubString(s, start_i, start_i + num_c); |
495 } | 495 } |
496 | 496 |
497 | 497 |
498 // ECMA-262 section 15.5.4.14 | 498 // ECMA-262 section 15.5.4.14 |
499 function StringSplit(separator, limit) { | 499 function StringSplit(separator, limit) { |
500 var subject = ToString(this); | 500 var subject = ToString(this); |
501 var result = []; | 501 limit = (limit === void 0) ? 0xffffffff : ToUint32(limit); |
502 var lim = (limit === void 0) ? 0xffffffff : ToUint32(limit); | 502 if (limit === 0) return []; |
503 | |
504 if (lim === 0) return result; | |
505 | 503 |
506 // ECMA-262 says that if separator is undefined, the result should | 504 // ECMA-262 says that if separator is undefined, the result should |
507 // be an array of size 1 containing the entire string. SpiderMonkey | 505 // be an array of size 1 containing the entire string. SpiderMonkey |
508 // and KJS have this behaviour only when no separator is given. If | 506 // and KJS have this behaviour only when no separator is given. If |
509 // undefined is explicitly given, they convert it to a string and | 507 // undefined is explicitly given, they convert it to a string and |
510 // use that. We do as SpiderMonkey and KJS. | 508 // use that. We do as SpiderMonkey and KJS. |
511 if (%_ArgumentsLength() === 0) { | 509 if (%_ArgumentsLength() === 0) { |
512 result[result.length] = subject; | 510 return [subject]; |
513 return result; | |
514 } | 511 } |
515 | 512 |
516 var length = subject.length; | 513 var length = subject.length; |
517 var currentIndex = 0; | |
518 var startIndex = 0; | |
519 | |
520 var sep; | |
521 if (IS_REGEXP(separator)) { | 514 if (IS_REGEXP(separator)) { |
522 sep = separator; | 515 %_Log('regexp', 'regexp-split,%0S,%1r', [subject, separator]); |
523 %_Log('regexp', 'regexp-split,%0S,%1r', [subject, sep]); | |
524 } else { | 516 } else { |
525 sep = ToString(separator); | 517 separator = ToString(separator); |
| 518 // If the separator string is empty then return the elements in the subject. |
| 519 if (separator.length == 0) { |
| 520 var result = $Array(length); |
| 521 for (var i = 0; i < length; i++) result[i] = subject[i]; |
| 522 return result; |
| 523 } |
526 } | 524 } |
527 | 525 |
528 if (length === 0) { | 526 if (length === 0) { |
529 if (splitMatch(sep, subject, 0, 0) != null) return result; | 527 if (splitMatch(separator, subject, 0, 0) != null) return []; |
530 result[result.length] = subject; | 528 return [subject]; |
531 return result; | |
532 } | 529 } |
533 | 530 |
| 531 var currentIndex = 0; |
| 532 var startIndex = 0; |
| 533 var result = []; |
| 534 |
534 while (true) { | 535 while (true) { |
535 | 536 |
536 if (startIndex === length) { | 537 if (startIndex === length) { |
537 result[result.length] = subject.slice(currentIndex, length); | 538 result[result.length] = subject.slice(currentIndex, length); |
538 return result; | 539 return result; |
539 } | 540 } |
540 | 541 |
541 var lastMatchInfo = splitMatch(sep, subject, currentIndex, startIndex); | 542 var lastMatchInfo = splitMatch(separator, subject, currentIndex, startIndex)
; |
542 | 543 |
543 if (IS_NULL(lastMatchInfo)) { | 544 if (IS_NULL(lastMatchInfo)) { |
544 result[result.length] = subject.slice(currentIndex, length); | 545 result[result.length] = subject.slice(currentIndex, length); |
545 return result; | 546 return result; |
546 } | 547 } |
547 | 548 |
548 var endIndex = lastMatchInfo[CAPTURE1]; | 549 var endIndex = lastMatchInfo[CAPTURE1]; |
549 | 550 |
550 // We ignore a zero-length match at the currentIndex. | 551 // We ignore a zero-length match at the currentIndex. |
551 if (startIndex === endIndex && endIndex === currentIndex) { | 552 if (startIndex === endIndex && endIndex === currentIndex) { |
552 startIndex++; | 553 startIndex++; |
553 continue; | 554 continue; |
554 } | 555 } |
555 | 556 |
556 result[result.length] = | 557 result[result.length] = SubString(subject, currentIndex, lastMatchInfo[CAPTU
RE0]); |
557 SubString(subject, currentIndex, lastMatchInfo[CAPTURE0]); | 558 if (result.length === limit) return result; |
558 if (result.length === lim) return result; | |
559 | 559 |
560 for (var i = 2; i < NUMBER_OF_CAPTURES(lastMatchInfo); i += 2) { | 560 for (var i = 2; i < NUMBER_OF_CAPTURES(lastMatchInfo); i += 2) { |
561 var start = lastMatchInfo[CAPTURE(i)]; | 561 var start = lastMatchInfo[CAPTURE(i)]; |
562 var end = lastMatchInfo[CAPTURE(i + 1)]; | 562 var end = lastMatchInfo[CAPTURE(i + 1)]; |
563 if (start != -1 && end != -1) { | 563 if (start != -1 && end != -1) { |
564 result[result.length] = SubString(subject, | 564 result[result.length] = SubString(subject, start, end); |
565 lastMatchInfo[CAPTURE(i)], | |
566 lastMatchInfo[CAPTURE(i + 1)]); | |
567 } else { | 565 } else { |
568 result[result.length] = void 0; | 566 result[result.length] = void 0; |
569 } | 567 } |
570 if (result.length === lim) return result; | 568 if (result.length === limit) return result; |
571 } | 569 } |
572 | 570 |
573 startIndex = currentIndex = endIndex; | 571 startIndex = currentIndex = endIndex; |
574 } | 572 } |
575 } | 573 } |
576 | 574 |
577 | 575 |
578 // ECMA-262 section 15.5.4.14 | 576 // ECMA-262 section 15.5.4.14 |
579 // Helper function used by split. This version returns the lastMatchInfo | 577 // Helper function used by split. This version returns the lastMatchInfo |
580 // instead of allocating a new array with basically the same information. | 578 // instead of allocating a new array with basically the same information. |
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
869 "small", StringSmall, | 867 "small", StringSmall, |
870 "strike", StringStrike, | 868 "strike", StringStrike, |
871 "sub", StringSub, | 869 "sub", StringSub, |
872 "sup", StringSup, | 870 "sup", StringSup, |
873 "toJSON", StringToJSON | 871 "toJSON", StringToJSON |
874 )); | 872 )); |
875 } | 873 } |
876 | 874 |
877 | 875 |
878 SetupString(); | 876 SetupString(); |
OLD | NEW |