|
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: String/split-002.js |
|
9 * ECMA Section: 15.6.4.9 |
|
10 * Description: Based on ECMA 2 Draft 7 February 1999 |
|
11 * |
|
12 * Author: christine@netscape.com |
|
13 * Date: 19 February 1999 |
|
14 */ |
|
15 |
|
16 /* |
|
17 * Since regular expressions have been part of JavaScript since 1.2, there |
|
18 * are already tests for regular expressions in the js1_2/regexp folder. |
|
19 * |
|
20 * These new tests try to supplement the existing tests, and verify that |
|
21 * our implementation of RegExp conforms to the ECMA specification, but |
|
22 * does not try to be as exhaustive as in previous tests. |
|
23 * |
|
24 * The [,limit] argument to String.split is new, and not covered in any |
|
25 * existing tests. |
|
26 * |
|
27 * String.split cases are covered in ecma/String/15.5.4.8-*.js. |
|
28 * String.split where separator is a RegExp are in |
|
29 * js1_2/regexp/string_split.js |
|
30 * |
|
31 */ |
|
32 |
|
33 var SECTION = "ecma_2/String/split-002.js"; |
|
34 var VERSION = "ECMA_2"; |
|
35 var TITLE = "String.prototype.split( regexp, [,limit] )"; |
|
36 |
|
37 startTest(); |
|
38 |
|
39 // the separator is not supplied |
|
40 // separator is undefined |
|
41 // separator is an empty string |
|
42 |
|
43 // AddSplitCases( "splitme", "", "''", ["s", "p", "l", "i", "t", "m", "e"] ); |
|
44 // AddSplitCases( "splitme", new RegExp(), "new RegExp()", ["s", "p", "l", "i", "t", "m", "e"] ); |
|
45 |
|
46 // separator is an empty regexp |
|
47 // separator is not supplied |
|
48 |
|
49 CompareSplit( "hello", "ll" ); |
|
50 |
|
51 CompareSplit( "hello", "l" ); |
|
52 CompareSplit( "hello", "x" ); |
|
53 CompareSplit( "hello", "h" ); |
|
54 CompareSplit( "hello", "o" ); |
|
55 CompareSplit( "hello", "hello" ); |
|
56 CompareSplit( "hello", undefined ); |
|
57 |
|
58 CompareSplit( "hello", ""); |
|
59 CompareSplit( "hello", "hellothere" ); |
|
60 |
|
61 CompareSplit( new String("hello" ) ); |
|
62 |
|
63 |
|
64 Number.prototype.split = String.prototype.split; |
|
65 |
|
66 CompareSplit( new Number(100111122133144155), 1 ); |
|
67 CompareSplitWithLimit(new Number(100111122133144155), 1, 1 ); |
|
68 |
|
69 CompareSplitWithLimit(new Number(100111122133144155), 1, 2 ); |
|
70 CompareSplitWithLimit(new Number(100111122133144155), 1, 0 ); |
|
71 CompareSplitWithLimit(new Number(100111122133144155), 1, 100 ); |
|
72 CompareSplitWithLimit(new Number(100111122133144155), 1, void 0 ); |
|
73 CompareSplitWithLimit(new Number(100111122133144155), 1, Math.pow(2,32)-1 ); |
|
74 CompareSplitWithLimit(new Number(100111122133144155), 1, "boo" ); |
|
75 CompareSplitWithLimit(new Number(100111122133144155), 1, -(Math.pow(2,32)-1) ); |
|
76 CompareSplitWithLimit( "hello", "l", NaN ); |
|
77 CompareSplitWithLimit( "hello", "l", 0 ); |
|
78 CompareSplitWithLimit( "hello", "l", 1 ); |
|
79 CompareSplitWithLimit( "hello", "l", 2 ); |
|
80 CompareSplitWithLimit( "hello", "l", 3 ); |
|
81 CompareSplitWithLimit( "hello", "l", 4 ); |
|
82 |
|
83 |
|
84 /* |
|
85 CompareSplitWithLimit( "hello", "ll", 0 ); |
|
86 CompareSplitWithLimit( "hello", "ll", 1 ); |
|
87 CompareSplitWithLimit( "hello", "ll", 2 ); |
|
88 CompareSplit( "", " " ); |
|
89 CompareSplit( "" ); |
|
90 */ |
|
91 |
|
92 // separartor is a regexp |
|
93 // separator regexp value global setting is set |
|
94 // string is an empty string |
|
95 // if separator is an empty string, split each by character |
|
96 |
|
97 // this is not a String object |
|
98 |
|
99 // limit is not a number |
|
100 // limit is undefined |
|
101 // limit is larger than 2^32-1 |
|
102 // limit is a negative number |
|
103 |
|
104 test(); |
|
105 |
|
106 function CompareSplit( string, separator ) { |
|
107 split_1 = string.split( separator ); |
|
108 split_2 = string_split( string, separator ); |
|
109 |
|
110 AddTestCase( |
|
111 "( " + string +".split(" + separator + ") ).length" , |
|
112 split_2.length, |
|
113 split_1.length ); |
|
114 |
|
115 var limit = split_1.length > split_2.length ? |
|
116 split_1.length : split_2.length; |
|
117 |
|
118 for ( var split_item = 0; split_item < limit; split_item++ ) { |
|
119 AddTestCase( |
|
120 string + ".split(" + separator + ")["+split_item+"]", |
|
121 split_2[split_item], |
|
122 split_1[split_item] ); |
|
123 } |
|
124 } |
|
125 |
|
126 function CompareSplitWithLimit( string, separator, splitlimit ) { |
|
127 split_1 = string.split( separator, splitlimit ); |
|
128 split_2 = string_split( string, separator, splitlimit ); |
|
129 |
|
130 AddTestCase( |
|
131 "( " + string +".split(" + separator + ", " + splitlimit+") ).length" , |
|
132 split_2.length, |
|
133 split_1.length ); |
|
134 |
|
135 var limit = split_1.length > split_2.length ? |
|
136 split_1.length : split_2.length; |
|
137 |
|
138 for ( var split_item = 0; split_item < limit; split_item++ ) { |
|
139 AddTestCase( |
|
140 string + ".split(" + separator + ", " + splitlimit+")["+split_item+"]", |
|
141 split_2[split_item], |
|
142 split_1[split_item] ); |
|
143 } |
|
144 } |
|
145 |
|
146 function string_split ( __this, separator, limit ) { |
|
147 var S = String(__this ); // 1 |
|
148 |
|
149 var A = new Array(); // 2 |
|
150 |
|
151 if ( limit == undefined ) { // 3 |
|
152 lim = Math.pow(2, 31 ) -1; |
|
153 } else { |
|
154 lim = ToUint32( limit ); |
|
155 } |
|
156 |
|
157 var s = S.length; // 4 |
|
158 var p = 0; // 5 |
|
159 |
|
160 if ( separator == undefined ) { // 8 |
|
161 A[0] = S; |
|
162 return A; |
|
163 } |
|
164 |
|
165 if ( separator.constructor == RegExp ) // 6 |
|
166 R = separator; |
|
167 else |
|
168 R = separator.toString(); |
|
169 |
|
170 if (lim == 0) return A; // 7 |
|
171 |
|
172 if ( separator == undefined ) { // 8 |
|
173 A[0] = S; |
|
174 return A; |
|
175 } |
|
176 |
|
177 if (s == 0) { // 9 |
|
178 z = SplitMatch(R, S, 0); |
|
179 if (z != false) return A; |
|
180 A[0] = S; |
|
181 return A; |
|
182 } |
|
183 |
|
184 var q = p; // 10 |
|
185 loop: |
|
186 while (true ) { |
|
187 |
|
188 if ( q == s ) break; // 11 |
|
189 |
|
190 z = SplitMatch(R, S, q); // 12 |
|
191 |
|
192 //print("Returned ", z); |
|
193 |
|
194 if (z != false) { // 13 |
|
195 e = z.endIndex; // 14 |
|
196 cap = z.captures; // 14 |
|
197 if (e != p) { // 15 |
|
198 //print("S = ", S, ", p = ", p, ", q = ", q); |
|
199 T = S.slice(p, q); // 16 |
|
200 //print("T = ", T); |
|
201 A[A.length] = T; // 17 |
|
202 if (A.length == lim) return A; // 18 |
|
203 p = e; // 19 |
|
204 i = 0; // 20 |
|
205 while (true) { // 25 |
|
206 if (i == cap.length) { // 21 |
|
207 q = p; // 10 |
|
208 continue loop; |
|
209 } |
|
210 i = i + 1; // 22 |
|
211 A[A.length] = cap[i] // 23 |
|
212 if (A.length == lim) return A; // 24 |
|
213 } |
|
214 } |
|
215 } |
|
216 |
|
217 q = q + 1; // 26 |
|
218 } |
|
219 |
|
220 T = S.slice(p, q); |
|
221 A[A.length] = T; |
|
222 return A; |
|
223 } |
|
224 |
|
225 function SplitMatch(R, S, q) |
|
226 { |
|
227 if (R.constructor == RegExp) { // 1 |
|
228 var reResult = R.match(S, q); // 8 |
|
229 if (reResult == undefined) |
|
230 return false; |
|
231 else { |
|
232 a = new Array(reResult.length - 1); |
|
233 for (var i = 1; i < reResult.length; i++) |
|
234 a[a.length] = reResult[i]; |
|
235 return { endIndex : reResult.index + reResult[0].length, captures : cap }; |
|
236 } |
|
237 } |
|
238 else { |
|
239 var r = R.length; // 2 |
|
240 s = S.length; // 3 |
|
241 if ((q + r) > s) return false; // 4 |
|
242 for (var i = 0; i < r; i++) { |
|
243 //print("S.charAt(", q + i, ") = ", S.charAt(q + i), ", R.charAt(", i, ") = ", R.charAt(i)); |
|
244 if (S.charAt(q + i) != R.charAt(i)) // 5 |
|
245 return false; |
|
246 } |
|
247 cap = new Array(); // 6 |
|
248 return { endIndex : q + r, captures : cap }; // 7 |
|
249 } |
|
250 } |
|
251 |
|
252 function ToUint32( n ) { |
|
253 n = Number( n ); |
|
254 var sign = ( n < 0 ) ? -1 : 1; |
|
255 |
|
256 if ( Math.abs( n ) == 0 |
|
257 || Math.abs( n ) == Number.POSITIVE_INFINITY |
|
258 || n != n) { |
|
259 return 0; |
|
260 } |
|
261 n = sign * Math.floor( Math.abs(n) ) |
|
262 |
|
263 n = n % Math.pow(2,32); |
|
264 |
|
265 if ( n < 0 ){ |
|
266 n += Math.pow(2,32); |
|
267 } |
|
268 |
|
269 return ( n ); |
|
270 } |