]>
git.saurik.com Git - apple/javascriptcore.git/blob - tests/mozilla/ecma_2/String/split-002.js
2 * File Name: String/split-002.js
3 * ECMA Section: 15.6.4.9
4 * Description: Based on ECMA 2 Draft 7 February 1999
6 * Author: christine@netscape.com
7 * Date: 19 February 1999
11 * Since regular expressions have been part of JavaScript since 1.2, there
12 * are already tests for regular expressions in the js1_2/regexp folder.
14 * These new tests try to supplement the existing tests, and verify that
15 * our implementation of RegExp conforms to the ECMA specification, but
16 * does not try to be as exhaustive as in previous tests.
18 * The [,limit] argument to String.split is new, and not covered in any
21 * String.split cases are covered in ecma/String/15.5.4.8-*.js.
22 * String.split where separator is a RegExp are in
23 * js1_2/regexp/string_split.js
27 var SECTION
= "ecma_2/String/split-002.js";
28 var VERSION
= "ECMA_2";
29 var TITLE
= "String.prototype.split( regexp, [,limit] )";
33 // the separator is not supplied
34 // separator is undefined
35 // separator is an empty string
37 // AddSplitCases( "splitme", "", "''", ["s", "p", "l", "i", "t", "m", "e"] );
38 // AddSplitCases( "splitme", new RegExp(), "new RegExp()", ["s", "p", "l", "i", "t", "m", "e"] );
40 // separator is an empty regexp
41 // separator is not supplied
43 CompareSplit( "hello", "ll" );
45 CompareSplit( "hello", "l" );
46 CompareSplit( "hello", "x" );
47 CompareSplit( "hello", "h" );
48 CompareSplit( "hello", "o" );
49 CompareSplit( "hello", "hello" );
50 CompareSplit( "hello", undefined );
52 CompareSplit( "hello", "");
53 CompareSplit( "hello", "hellothere" );
55 CompareSplit( new String("hello" ) );
58 Number
.prototype.split
= String
.prototype.split
;
60 CompareSplit( new Number(100111122133144155), 1 );
61 CompareSplitWithLimit(new Number(100111122133144155), 1, 1 );
63 CompareSplitWithLimit(new Number(100111122133144155), 1, 2 );
64 CompareSplitWithLimit(new Number(100111122133144155), 1, 0 );
65 CompareSplitWithLimit(new Number(100111122133144155), 1, 100 );
66 CompareSplitWithLimit(new Number(100111122133144155), 1, void 0 );
67 CompareSplitWithLimit(new Number(100111122133144155), 1, Math
.pow(2,32)-1 );
68 CompareSplitWithLimit(new Number(100111122133144155), 1, "boo" );
69 CompareSplitWithLimit(new Number(100111122133144155), 1, -(Math
.pow(2,32)-1) );
70 CompareSplitWithLimit( "hello", "l", NaN
);
71 CompareSplitWithLimit( "hello", "l", 0 );
72 CompareSplitWithLimit( "hello", "l", 1 );
73 CompareSplitWithLimit( "hello", "l", 2 );
74 CompareSplitWithLimit( "hello", "l", 3 );
75 CompareSplitWithLimit( "hello", "l", 4 );
79 CompareSplitWithLimit( "hello", "ll", 0 );
80 CompareSplitWithLimit( "hello", "ll", 1 );
81 CompareSplitWithLimit( "hello", "ll", 2 );
82 CompareSplit( "", " " );
86 // separartor is a regexp
87 // separator regexp value global setting is set
88 // string is an empty string
89 // if separator is an empty string, split each by character
91 // this is not a String object
93 // limit is not a number
95 // limit is larger than 2^32-1
96 // limit is a negative number
100 function CompareSplit( string
, separator
) {
101 split_1
= string
.split( separator
);
102 split_2
= string_split( string
, separator
);
105 "( " + string
+".split(" + separator
+ ") ).length" ,
109 var limit
= split_1
.length
> split_2
.length
?
110 split_1
.length : split_2
.length
;
112 for ( var split_item
= 0; split_item
< limit
; split_item
++ ) {
114 string
+ ".split(" + separator
+ ")["+split_item
+"]",
116 split_1
[split_item
] );
120 function CompareSplitWithLimit( string
, separator
, splitlimit
) {
121 split_1
= string
.split( separator
, splitlimit
);
122 split_2
= string_split( string
, separator
, splitlimit
);
125 "( " + string
+".split(" + separator
+ ", " + splitlimit
+") ).length" ,
129 var limit
= split_1
.length
> split_2
.length
?
130 split_1
.length : split_2
.length
;
132 for ( var split_item
= 0; split_item
< limit
; split_item
++ ) {
134 string
+ ".split(" + separator
+ ", " + splitlimit
+")["+split_item
+"]",
136 split_1
[split_item
] );
140 function string_split ( __this
, separator
, limit
) {
141 var S
= String(__this
); // 1
143 var A
= new Array(); // 2
145 if ( limit
== undefined ) { // 3
146 lim
= Math
.pow(2, 31 ) -1;
148 lim
= ToUint32( limit
);
151 var s
= S
.length
; // 4
154 if ( separator
== undefined ) { // 8
159 if ( separator
.constructor == RegExp
) // 6
162 R
= separator
.toString();
164 if (lim
== 0) return A
; // 7
166 if ( separator
== undefined ) { // 8
172 z
= SplitMatch(R
, S
, 0);
173 if (z
!= false) return A
;
182 if ( q
== s
) break; // 11
184 z
= SplitMatch(R
, S
, q
); // 12
186 //print("Returned ", z);
188 if (z
!= false) { // 13
189 e
= z
.endIndex
; // 14
190 cap
= z
.captures
; // 14
192 //print("S = ", S, ", p = ", p, ", q = ", q);
193 T
= S
.slice(p
, q
); // 16
195 A
[A
.length
] = T
; // 17
196 if (A
.length
== lim
) return A
; // 18
200 if (i
== cap
.length
) { // 21
205 A
[A
.length
] = cap
[i
] // 23
206 if (A
.length
== lim
) return A
; // 24
219 function SplitMatch(R
, S
, q
)
221 if (R
.constructor == RegExp
) { // 1
222 var reResult
= R
.match(S
, q
); // 8
223 if (reResult
== undefined)
226 a
= new Array(reResult
.length
- 1);
227 for (var i
= 1; i
< reResult
.length
; i
++)
228 a
[a
.length
] = reResult
[i
];
229 return { endIndex : reResult
.index
+ reResult
[0].length
, captures : cap
};
233 var r
= R
.length
; // 2
235 if ((q
+ r
) > s
) return false; // 4
236 for (var i
= 0; i
< r
; i
++) {
237 //print("S.charAt(", q + i, ") = ", S.charAt(q + i), ", R.charAt(", i, ") = ", R.charAt(i));
238 if (S
.charAt(q
+ i
) != R
.charAt(i
)) // 5
241 cap
= new Array(); // 6
242 return { endIndex : q
+ r
, captures : cap
}; // 7
246 function ToUint32( n
) {
248 var sign
= ( n
< 0 ) ? -1 : 1;
250 if ( Math
.abs( n
) == 0
251 || Math
.abs( n
) == Number
.POSITIVE_INFINITY
255 n
= sign
* Math
.floor( Math
.abs(n
) )
257 n
= n
% Math
.pow(2,32);