OLD | NEW |
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 487 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
498 if (num_c < 0) | 498 if (num_c < 0) |
499 num_c = 0; | 499 num_c = 0; |
500 | 500 |
501 return SubString(s, start_i, start_i + num_c); | 501 return SubString(s, start_i, start_i + num_c); |
502 } | 502 } |
503 | 503 |
504 | 504 |
505 // ECMA-262 section 15.5.4.14 | 505 // ECMA-262 section 15.5.4.14 |
506 function StringSplit(separator, limit) { | 506 function StringSplit(separator, limit) { |
507 var subject = ToString(this); | 507 var subject = ToString(this); |
508 limit = (limit === void 0) ? 0xffffffff : ToUint32(limit); | 508 limit = (IS_UNDEFINED(limit)) ? 0xffffffff : TO_UINT32(limit); |
509 if (limit === 0) return []; | 509 if (limit === 0) return []; |
510 | 510 |
511 // ECMA-262 says that if separator is undefined, the result should | 511 // ECMA-262 says that if separator is undefined, the result should |
512 // be an array of size 1 containing the entire string. SpiderMonkey | 512 // be an array of size 1 containing the entire string. SpiderMonkey |
513 // and KJS have this behaviour only when no separator is given. If | 513 // and KJS have this behaviour only when no separator is given. If |
514 // undefined is explicitly given, they convert it to a string and | 514 // undefined is explicitly given, they convert it to a string and |
515 // use that. We do as SpiderMonkey and KJS. | 515 // use that. We do as SpiderMonkey and KJS. |
516 if (%_ArgumentsLength() === 0) { | 516 if (%_ArgumentsLength() === 0) { |
517 return [subject]; | 517 return [subject]; |
518 } | 518 } |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
597 if (separatorIndex === -1) return null; | 597 if (separatorIndex === -1) return null; |
598 | 598 |
599 reusableMatchInfo[CAPTURE0] = separatorIndex; | 599 reusableMatchInfo[CAPTURE0] = separatorIndex; |
600 reusableMatchInfo[CAPTURE1] = separatorIndex + separator.length; | 600 reusableMatchInfo[CAPTURE1] = separatorIndex + separator.length; |
601 return reusableMatchInfo; | 601 return reusableMatchInfo; |
602 }; | 602 }; |
603 | 603 |
604 | 604 |
605 // ECMA-262 section 15.5.4.15 | 605 // ECMA-262 section 15.5.4.15 |
606 function StringSubstring(start, end) { | 606 function StringSubstring(start, end) { |
607 var s = ToString(this); | 607 var s = this; |
| 608 if (!IS_STRING(s)) s = ToString(s); |
608 var s_len = s.length; | 609 var s_len = s.length; |
| 610 |
609 var start_i = TO_INTEGER(start); | 611 var start_i = TO_INTEGER(start); |
| 612 if (start_i < 0) { |
| 613 start_i = 0; |
| 614 } else if (start_i > s_len) { |
| 615 start_i = s_len; |
| 616 } |
| 617 |
610 var end_i = s_len; | 618 var end_i = s_len; |
611 if (!IS_UNDEFINED(end)) | 619 if (!IS_UNDEFINED(end)) { |
612 end_i = TO_INTEGER(end); | 620 end_i = TO_INTEGER(end); |
613 | 621 if (end_i > s_len) { |
614 if (start_i < 0) start_i = 0; | 622 end_i = s_len; |
615 if (start_i > s_len) start_i = s_len; | 623 } else { |
616 if (end_i < 0) end_i = 0; | 624 if (end_i < 0) end_i = 0; |
617 if (end_i > s_len) end_i = s_len; | 625 if (start_i > end_i) { |
618 | 626 var tmp = end_i; |
619 if (start_i > end_i) { | 627 end_i = start_i; |
620 var tmp = end_i; | 628 start_i = tmp; |
621 end_i = start_i; | 629 } |
622 start_i = tmp; | 630 } |
623 } | 631 } |
624 | 632 |
625 return SubString(s, start_i, end_i); | 633 return SubString(s, start_i, end_i); |
626 } | 634 } |
627 | 635 |
628 | 636 |
629 // This is not a part of ECMA-262. | 637 // This is not a part of ECMA-262. |
630 function StringSubstr(start, n) { | 638 function StringSubstr(start, n) { |
631 var s = ToString(this); | 639 var s = ToString(this); |
632 var len; | 640 var len; |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
783 function StringSub() { | 791 function StringSub() { |
784 return "<sub>" + this + "</sub>"; | 792 return "<sub>" + this + "</sub>"; |
785 } | 793 } |
786 | 794 |
787 | 795 |
788 function StringSup() { | 796 function StringSup() { |
789 return "<sup>" + this + "</sup>"; | 797 return "<sup>" + this + "</sup>"; |
790 } | 798 } |
791 | 799 |
792 | 800 |
793 // StringBuilder support. | 801 // ReplaceResultBuilder support. |
794 | |
795 function StringBuilder() { | |
796 this.elements = new $Array(); | |
797 } | |
798 | |
799 | |
800 function ReplaceResultBuilder(str) { | 802 function ReplaceResultBuilder(str) { |
801 this.elements = new $Array(); | 803 this.elements = new $Array(); |
802 this.special_string = str; | 804 this.special_string = str; |
803 } | 805 } |
804 | 806 |
805 | 807 |
806 ReplaceResultBuilder.prototype.add = | 808 ReplaceResultBuilder.prototype.add = function(str) { |
807 StringBuilder.prototype.add = function(str) { | |
808 if (!IS_STRING(str)) str = ToString(str); | 809 if (!IS_STRING(str)) str = ToString(str); |
809 if (str.length > 0) { | 810 if (str.length > 0) { |
810 var elements = this.elements; | 811 var elements = this.elements; |
811 elements[elements.length] = str; | 812 elements[elements.length] = str; |
812 } | 813 } |
813 } | 814 } |
814 | 815 |
815 | 816 |
816 ReplaceResultBuilder.prototype.addSpecialSlice = function(start, end) { | 817 ReplaceResultBuilder.prototype.addSpecialSlice = function(start, end) { |
817 var len = end - start; | 818 var len = end - start; |
818 if (len == 0) return; | 819 if (len == 0) return; |
819 var elements = this.elements; | 820 var elements = this.elements; |
820 if (start < 0x80000 && len < 0x800) { | 821 if (start < 0x80000 && len < 0x800) { |
821 elements[elements.length] = (start << 11) + len; | 822 elements[elements.length] = (start << 11) + len; |
822 } else { | 823 } else { |
823 // 0 < len <= String::kMaxLength and Smi::kMaxValue >= String::kMaxLength, | 824 // 0 < len <= String::kMaxLength and Smi::kMaxValue >= String::kMaxLength, |
824 // so -len is a smi. | 825 // so -len is a smi. |
825 elements[elements.length] = -len; | 826 elements[elements.length] = -len; |
826 elements[elements.length] = start; | 827 elements[elements.length] = start; |
827 } | 828 } |
828 } | 829 } |
829 | 830 |
830 | 831 |
831 StringBuilder.prototype.generate = function() { | |
832 return %StringBuilderConcat(this.elements, ""); | |
833 } | |
834 | |
835 | |
836 ReplaceResultBuilder.prototype.generate = function() { | 832 ReplaceResultBuilder.prototype.generate = function() { |
837 return %StringBuilderConcat(this.elements, this.special_string); | 833 var elements = this.elements; |
| 834 return %StringBuilderConcat(elements, elements.length, this.special_string); |
838 } | 835 } |
839 | 836 |
840 | 837 |
841 function StringToJSON(key) { | 838 function StringToJSON(key) { |
842 return CheckJSONPrimitive(this.valueOf()); | 839 return CheckJSONPrimitive(this.valueOf()); |
843 } | 840 } |
844 | 841 |
845 | 842 |
846 // ------------------------------------------------------------------- | 843 // ------------------------------------------------------------------- |
847 | 844 |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
892 "small", StringSmall, | 889 "small", StringSmall, |
893 "strike", StringStrike, | 890 "strike", StringStrike, |
894 "sub", StringSub, | 891 "sub", StringSub, |
895 "sup", StringSup, | 892 "sup", StringSup, |
896 "toJSON", StringToJSON | 893 "toJSON", StringToJSON |
897 )); | 894 )); |
898 } | 895 } |
899 | 896 |
900 | 897 |
901 SetupString(); | 898 SetupString(); |
OLD | NEW |