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 539 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
550 | 550 |
551 | 551 |
552 // ECMA-262 section 15.5.4.14 | 552 // ECMA-262 section 15.5.4.14 |
553 function StringSplit(separator, limit) { | 553 function StringSplit(separator, limit) { |
554 var subject = TO_STRING_INLINE(this); | 554 var subject = TO_STRING_INLINE(this); |
555 limit = (IS_UNDEFINED(limit)) ? 0xffffffff : TO_UINT32(limit); | 555 limit = (IS_UNDEFINED(limit)) ? 0xffffffff : TO_UINT32(limit); |
556 if (limit === 0) return []; | 556 if (limit === 0) return []; |
557 | 557 |
558 // ECMA-262 says that if separator is undefined, the result should | 558 // ECMA-262 says that if separator is undefined, the result should |
559 // be an array of size 1 containing the entire string. SpiderMonkey | 559 // be an array of size 1 containing the entire string. SpiderMonkey |
560 // and KJS have this behaviour only when no separator is given. If | 560 // and KJS have this behavior only when no separator is given. If |
Erik Corry
2010/03/15 12:57:48
!@*%$
| |
561 // undefined is explicitly given, they convert it to a string and | 561 // undefined is explicitly given, they convert it to a string and |
562 // use that. We do as SpiderMonkey and KJS. | 562 // use that. We do as SpiderMonkey and KJS. |
563 if (%_ArgumentsLength() === 0) { | 563 if (%_ArgumentsLength() === 0) { |
564 return [subject]; | 564 return [subject]; |
565 } | 565 } |
566 | 566 |
567 var length = subject.length; | 567 var length = subject.length; |
568 if (!IS_REGEXP(separator)) { | 568 if (!IS_REGEXP(separator)) { |
569 separator = TO_STRING_INLINE(separator); | 569 separator = TO_STRING_INLINE(separator); |
570 var separator_length = separator.length; | 570 var separator_length = separator.length; |
571 | 571 |
572 // If the separator string is empty then return the elements in the subject. | 572 // If the separator string is empty then return the elements in the subject. |
573 if (separator_length === 0) return %StringToArray(subject); | 573 if (separator_length === 0) return %StringToArray(subject); |
574 | 574 |
575 var result = []; | 575 var result = %StringSplit(subject, separator, limit); |
576 var start_index = 0; | |
577 var index; | |
578 while (true) { | |
579 if (start_index + separator_length > length || | |
580 (index = %StringIndexOf(subject, separator, start_index)) === -1) { | |
581 result.push(SubString(subject, start_index, length)); | |
582 break; | |
583 } | |
584 if (result.push(SubString(subject, start_index, index)) === limit) break; | |
585 start_index = index + separator_length; | |
586 } | |
587 | 576 |
588 return result; | 577 return result; |
589 } | 578 } |
590 | 579 |
591 %_Log('regexp', 'regexp-split,%0S,%1r', [subject, separator]); | 580 %_Log('regexp', 'regexp-split,%0S,%1r', [subject, separator]); |
592 | 581 |
593 if (length === 0) { | 582 if (length === 0) { |
594 if (splitMatch(separator, subject, 0, 0) != null) return []; | 583 if (splitMatch(separator, subject, 0, 0) != null) return []; |
595 return [subject]; | 584 return [subject]; |
596 } | 585 } |
(...skipping 353 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
950 "small", StringSmall, | 939 "small", StringSmall, |
951 "strike", StringStrike, | 940 "strike", StringStrike, |
952 "sub", StringSub, | 941 "sub", StringSub, |
953 "sup", StringSup, | 942 "sup", StringSup, |
954 "toJSON", StringToJSON | 943 "toJSON", StringToJSON |
955 )); | 944 )); |
956 } | 945 } |
957 | 946 |
958 | 947 |
959 SetupString(); | 948 SetupString(); |
OLD | NEW |