|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 |
|
7 /** |
|
8 File Name: 15.5.4.8-3.js |
|
9 ECMA Section: 15.5.4.8 String.prototype.split( separator ) |
|
10 Description: |
|
11 |
|
12 Returns an Array object into which substrings of the result of converting |
|
13 this object to a string have been stored. The substrings are determined by |
|
14 searching from left to right for occurrences of the given separator; these |
|
15 occurrences are not part of any substring in the returned array, but serve |
|
16 to divide up this string value. The separator may be a string of any length. |
|
17 |
|
18 As a special case, if the separator is the empty string, the string is split |
|
19 up into individual characters; the length of the result array equals the |
|
20 length of the string, and each substring contains one character. |
|
21 |
|
22 If the separator is not supplied, then the result array contains just one |
|
23 string, which is the string. |
|
24 |
|
25 When the split method is called with one argument separator, the following steps are taken: |
|
26 |
|
27 1. Call ToString, giving it the this value as its argument. |
|
28 2. Create a new Array object of length 0 and call it A. |
|
29 3. If separator is not supplied, call the [[Put]] method of A with 0 and |
|
30 Result(1) as arguments, and then return A. |
|
31 4. Call ToString(separator). |
|
32 5. Compute the number of characters in Result(1). |
|
33 6. Compute the number of characters in the string that is Result(4). |
|
34 7. Let p be 0. |
|
35 8. If Result(6) is zero (the separator string is empty), go to step 17. |
|
36 9. Compute the smallest possible integer k not smaller than p such that |
|
37 k+Result(6) is not greater than Result(5), and for all nonnegative |
|
38 integers j less than Result(6), the character at position k+j of |
|
39 Result(1) is the same as the character at position j of Result(2); |
|
40 but if there is no such integer k, then go to step 14. |
|
41 10. Compute a string value equal to the substring of Result(1), consisting |
|
42 of the characters at positions p through k1, inclusive. |
|
43 11. Call the [[Put]] method of A with A.length and Result(10) as arguments. |
|
44 12. Let p be k+Result(6). |
|
45 13. Go to step 9. |
|
46 14. Compute a string value equal to the substring of Result(1), consisting |
|
47 of the characters from position p to the end of Result(1). |
|
48 15. Call the [[Put]] method of A with A.length and Result(14) as arguments. |
|
49 16. Return A. |
|
50 17. If p equals Result(5), return A. |
|
51 18. Compute a string value equal to the substring of Result(1), consisting of |
|
52 the single character at position p. |
|
53 19. Call the [[Put]] method of A with A.length and Result(18) as arguments. |
|
54 20. Increase p by 1. |
|
55 21. Go to step 17. |
|
56 |
|
57 Note that the split function is intentionally generic; it does not require that its this value be a String |
|
58 object. Therefore it can be transferred to other kinds of objects for use as a method. |
|
59 |
|
60 Author: christine@netscape.com |
|
61 Date: 12 november 1997 |
|
62 */ |
|
63 |
|
64 var SECTION = "15.5.4.8-3"; |
|
65 var VERSION = "ECMA_1"; |
|
66 startTest(); |
|
67 var TITLE = "String.prototype.split"; |
|
68 |
|
69 writeHeaderToLog( SECTION + " "+ TITLE); |
|
70 |
|
71 var TEST_STRING = ""; |
|
72 var EXPECT = new Array(); |
|
73 |
|
74 // this.toString is the empty string. |
|
75 |
|
76 new TestCase( SECTION, |
|
77 "var s = new String(); s.split().length", |
|
78 1, |
|
79 eval("var s = new String(); s.split().length") ); |
|
80 |
|
81 new TestCase( SECTION, |
|
82 "var s = new String(); s.split()[0]", |
|
83 "", |
|
84 eval("var s = new String(); s.split()[0]") ); |
|
85 |
|
86 // this.toString() is the empty string, separator is specified. |
|
87 |
|
88 new TestCase( SECTION, |
|
89 "var s = new String(); s.split('').length", |
|
90 0, |
|
91 eval("var s = new String(); s.split('').length") ); |
|
92 |
|
93 new TestCase( SECTION, |
|
94 "var s = new String(); s.split(' ').length", |
|
95 1, |
|
96 eval("var s = new String(); s.split(' ').length") ); |
|
97 |
|
98 // this to string is " " |
|
99 new TestCase( SECTION, |
|
100 "var s = new String(' '); s.split().length", |
|
101 1, |
|
102 eval("var s = new String(' '); s.split().length") ); |
|
103 |
|
104 new TestCase( SECTION, |
|
105 "var s = new String(' '); s.split()[0]", |
|
106 " ", |
|
107 eval("var s = new String(' '); s.split()[0]") ); |
|
108 |
|
109 new TestCase( SECTION, |
|
110 "var s = new String(' '); s.split('').length", |
|
111 1, |
|
112 eval("var s = new String(' '); s.split('').length") ); |
|
113 |
|
114 new TestCase( SECTION, |
|
115 "var s = new String(' '); s.split('')[0]", |
|
116 " ", |
|
117 eval("var s = new String(' '); s.split('')[0]") ); |
|
118 |
|
119 new TestCase( SECTION, |
|
120 "var s = new String(' '); s.split(' ').length", |
|
121 2, |
|
122 eval("var s = new String(' '); s.split(' ').length") ); |
|
123 |
|
124 new TestCase( SECTION, |
|
125 "var s = new String(' '); s.split(' ')[0]", |
|
126 "", |
|
127 eval("var s = new String(' '); s.split(' ')[0]") ); |
|
128 |
|
129 new TestCase( SECTION, |
|
130 "\"\".split(\"\").length", |
|
131 0, |
|
132 ("".split("")).length ); |
|
133 |
|
134 new TestCase( SECTION, |
|
135 "\"\".split(\"x\").length", |
|
136 1, |
|
137 ("".split("x")).length ); |
|
138 |
|
139 new TestCase( SECTION, |
|
140 "\"\".split(\"x\")[0]", |
|
141 "", |
|
142 ("".split("x"))[0] ); |
|
143 |
|
144 test(); |
|
145 |
|
146 function Split( string, separator ) { |
|
147 string = String( string ); |
|
148 |
|
149 var A = new Array(); |
|
150 |
|
151 if ( arguments.length < 2 ) { |
|
152 A[0] = string; |
|
153 return A; |
|
154 } |
|
155 |
|
156 separator = String( separator ); |
|
157 |
|
158 var str_len = String( string ).length; |
|
159 var sep_len = String( separator ).length; |
|
160 |
|
161 var p = 0; |
|
162 var k = 0; |
|
163 |
|
164 if ( sep_len == 0 ) { |
|
165 for ( ; p < str_len; p++ ) { |
|
166 A[A.length] = String( string.charAt(p) ); |
|
167 } |
|
168 } |
|
169 return A; |
|
170 } |