|
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-2.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-2"; |
|
65 var VERSION = "ECMA_1"; |
|
66 startTest(); |
|
67 var TITLE = "String.prototype.split"; |
|
68 |
|
69 writeHeaderToLog( SECTION + " "+ TITLE); |
|
70 |
|
71 // case where separator is the empty string. |
|
72 |
|
73 var TEST_STRING = "this is a string object"; |
|
74 |
|
75 new TestCase( SECTION, |
|
76 "var s = new String( "+ TEST_STRING +" ); s.split('').length", |
|
77 TEST_STRING.length, |
|
78 eval("var s = new String( TEST_STRING ); s.split('').length") ); |
|
79 |
|
80 for ( var i = 0; i < TEST_STRING.length; i++ ) { |
|
81 |
|
82 new TestCase( SECTION, |
|
83 "var s = new String( "+TEST_STRING+" ); s.split('')["+i+"]", |
|
84 TEST_STRING.charAt(i), |
|
85 eval("var s = new String( TEST_STRING ); s.split('')["+i+"]") ); |
|
86 } |
|
87 |
|
88 // case where the value of the separator is undefined. in this case, |
|
89 // the this value is returned. |
|
90 |
|
91 var TEST_STRING = "thisundefinedisundefinedaundefinedstringundefinedobject"; |
|
92 var EXPECT_STRING = new Array( TEST_STRING ); |
|
93 |
|
94 new TestCase( SECTION, |
|
95 "var s = new String( "+ TEST_STRING +" ); s.split(void 0).length", |
|
96 EXPECT_STRING.length, |
|
97 eval("var s = new String( TEST_STRING ); s.split(void 0).length") ); |
|
98 |
|
99 for ( var i = 0; i < EXPECT_STRING.length; i++ ) { |
|
100 new TestCase( SECTION, |
|
101 "var s = new String( "+TEST_STRING+" ); s.split(void 0)["+i+"]", |
|
102 EXPECT_STRING[i], |
|
103 eval("var s = new String( TEST_STRING ); s.split(void 0)["+i+"]") ); |
|
104 } |
|
105 |
|
106 // case where the value of the separator is null. in this case the value of the separator is "null". |
|
107 TEST_STRING = "thisnullisnullanullstringnullobject"; |
|
108 var EXPECT_STRING = new Array( "this", "is", "a", "string", "object" ); |
|
109 |
|
110 new TestCase( SECTION, |
|
111 "var s = new String( "+ TEST_STRING +" ); s.split(null).length", |
|
112 EXPECT_STRING.length, |
|
113 eval("var s = new String( TEST_STRING ); s.split(null).length") ); |
|
114 |
|
115 for ( var i = 0; i < EXPECT_STRING.length; i++ ) { |
|
116 new TestCase( SECTION, |
|
117 "var s = new String( "+TEST_STRING+" ); s.split(null)["+i+"]", |
|
118 EXPECT_STRING[i], |
|
119 eval("var s = new String( TEST_STRING ); s.split(null)["+i+"]") ); |
|
120 } |
|
121 |
|
122 // case where the value of the separator is a boolean. |
|
123 TEST_STRING = "thistrueistrueatruestringtrueobject"; |
|
124 var EXPECT_STRING = new Array( "this", "is", "a", "string", "object" ); |
|
125 |
|
126 new TestCase( SECTION, |
|
127 "var s = new String( "+ TEST_STRING +" ); s.split(true).length", |
|
128 EXPECT_STRING.length, |
|
129 eval("var s = new String( TEST_STRING ); s.split(true).length") ); |
|
130 |
|
131 for ( var i = 0; i < EXPECT_STRING.length; i++ ) { |
|
132 new TestCase( SECTION, |
|
133 "var s = new String( "+TEST_STRING+" ); s.split(true)["+i+"]", |
|
134 EXPECT_STRING[i], |
|
135 eval("var s = new String( TEST_STRING ); s.split(true)["+i+"]") ); |
|
136 } |
|
137 |
|
138 // case where the value of the separator is a number |
|
139 TEST_STRING = "this123is123a123string123object"; |
|
140 var EXPECT_STRING = new Array( "this", "is", "a", "string", "object" ); |
|
141 |
|
142 new TestCase( SECTION, |
|
143 "var s = new String( "+ TEST_STRING +" ); s.split(123).length", |
|
144 EXPECT_STRING.length, |
|
145 eval("var s = new String( TEST_STRING ); s.split(123).length") ); |
|
146 |
|
147 for ( var i = 0; i < EXPECT_STRING.length; i++ ) { |
|
148 new TestCase( SECTION, |
|
149 "var s = new String( "+TEST_STRING+" ); s.split(123)["+i+"]", |
|
150 EXPECT_STRING[i], |
|
151 eval("var s = new String( TEST_STRING ); s.split(123)["+i+"]") ); |
|
152 } |
|
153 |
|
154 |
|
155 // case where the value of the separator is a number |
|
156 TEST_STRING = "this123is123a123string123object"; |
|
157 var EXPECT_STRING = new Array( "this", "is", "a", "string", "object" ); |
|
158 |
|
159 new TestCase( SECTION, |
|
160 "var s = new String( "+ TEST_STRING +" ); s.split(123).length", |
|
161 EXPECT_STRING.length, |
|
162 eval("var s = new String( TEST_STRING ); s.split(123).length") ); |
|
163 |
|
164 for ( var i = 0; i < EXPECT_STRING.length; i++ ) { |
|
165 new TestCase( SECTION, |
|
166 "var s = new String( "+TEST_STRING+" ); s.split(123)["+i+"]", |
|
167 EXPECT_STRING[i], |
|
168 eval("var s = new String( TEST_STRING ); s.split(123)["+i+"]") ); |
|
169 } |
|
170 |
|
171 // case where the separator is not in the string |
|
172 TEST_STRING = "this is a string"; |
|
173 EXPECT_STRING = new Array( "this is a string" ); |
|
174 |
|
175 new TestCase( SECTION, |
|
176 "var s = new String( " + TEST_STRING + " ); s.split(':').length", |
|
177 1, |
|
178 eval("var s = new String( TEST_STRING ); s.split(':').length") ); |
|
179 |
|
180 new TestCase( SECTION, |
|
181 "var s = new String( " + TEST_STRING + " ); s.split(':')[0]", |
|
182 TEST_STRING, |
|
183 eval("var s = new String( TEST_STRING ); s.split(':')[0]") ); |
|
184 |
|
185 // case where part but not all of separator is in the string. |
|
186 TEST_STRING = "this is a string"; |
|
187 EXPECT_STRING = new Array( "this is a string" ); |
|
188 new TestCase( SECTION, |
|
189 "var s = new String( " + TEST_STRING + " ); s.split('strings').length", |
|
190 1, |
|
191 eval("var s = new String( TEST_STRING ); s.split('strings').length") ); |
|
192 |
|
193 new TestCase( SECTION, |
|
194 "var s = new String( " + TEST_STRING + " ); s.split('strings')[0]", |
|
195 TEST_STRING, |
|
196 eval("var s = new String( TEST_STRING ); s.split('strings')[0]") ); |
|
197 |
|
198 // case where the separator is at the end of the string |
|
199 TEST_STRING = "this is a string"; |
|
200 EXPECT_STRING = new Array( "this is a " ); |
|
201 new TestCase( SECTION, |
|
202 "var s = new String( " + TEST_STRING + " ); s.split('string').length", |
|
203 2, |
|
204 eval("var s = new String( TEST_STRING ); s.split('string').length") ); |
|
205 |
|
206 for ( var i = 0; i < EXPECT_STRING.length; i++ ) { |
|
207 new TestCase( SECTION, |
|
208 "var s = new String( "+TEST_STRING+" ); s.split('string')["+i+"]", |
|
209 EXPECT_STRING[i], |
|
210 eval("var s = new String( TEST_STRING ); s.split('string')["+i+"]") ); |
|
211 } |
|
212 |
|
213 test(); |