1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is JavaScript Engine testing utilities.
17 * The Initial Developer of the Original Code is
18 * Netscape Communication Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
40 * File Name: String/split-002.js
41 * ECMA Section: 15.6.4.9
42 * Description: Based on ECMA 2 Draft 7 February 1999
44 * Author: christine@netscape.com
45 * Date: 19 February 1999
49 * Since regular expressions have been part of JavaScript since 1.2, there
50 * are already tests for regular expressions in the js1_2/regexp folder.
52 * These new tests try to supplement the existing tests, and verify that
53 * our implementation of RegExp conforms to the ECMA specification, but
54 * does not try to be as exhaustive as in previous tests.
56 * The [,limit] argument to String.split is new, and not covered in any
59 * String.split cases are covered in ecma/String/15.5.4.8-*.js.
60 * String.split where separator is a RegExp are in
61 * js1_2/regexp/string_split.js
65 var SECTION = "ecma_2/String/split-002.js";
66 var VERSION = "ECMA_2";
67 var TITLE = "String.prototype.split( regexp, [,limit] )";
71 // the separator is not supplied
72 // separator is undefined
73 // separator is an empty string
75 // AddSplitCases( "splitme", "", "''", ["s", "p", "l", "i", "t", "m", "e"] );
76 // AddSplitCases( "splitme", new RegExp(), "new RegExp()", ["s", "p", "l", "i", "t", "m", "e"] );
78 // separator is an empty regexp
79 // separator is not supplied
81 CompareSplit( "hello", "ll" );
83 CompareSplit( "hello", "l" );
84 CompareSplit( "hello", "x" );
85 CompareSplit( "hello", "h" );
86 CompareSplit( "hello", "o" );
87 CompareSplit( "hello", "hello" );
88 CompareSplit( "hello", undefined );
90 CompareSplit( "hello", "");
91 CompareSplit( "hello", "hellothere" );
93 CompareSplit( new String("hello" ) );
96 Number.prototype.split = String.prototype.split;
98 CompareSplit( new Number(100111122133144155), 1 );
99 CompareSplitWithLimit(new Number(100111122133144155), 1, 1 );
101 CompareSplitWithLimit(new Number(100111122133144155), 1, 2 );
102 CompareSplitWithLimit(new Number(100111122133144155), 1, 0 );
103 CompareSplitWithLimit(new Number(100111122133144155), 1, 100 );
104 CompareSplitWithLimit(new Number(100111122133144155), 1, void 0 );
105 CompareSplitWithLimit(new Number(100111122133144155), 1, Math.pow(2,32)-1 );
106 CompareSplitWithLimit(new Number(100111122133144155), 1, "boo" );
107 CompareSplitWithLimit(new Number(100111122133144155), 1, -(Math.pow(2,32)-1) );
108 CompareSplitWithLimit( "hello", "l", NaN );
109 CompareSplitWithLimit( "hello", "l", 0 );
110 CompareSplitWithLimit( "hello", "l", 1 );
111 CompareSplitWithLimit( "hello", "l", 2 );
112 CompareSplitWithLimit( "hello", "l", 3 );
113 CompareSplitWithLimit( "hello", "l", 4 );
117 CompareSplitWithLimit( "hello", "ll", 0 );
118 CompareSplitWithLimit( "hello", "ll", 1 );
119 CompareSplitWithLimit( "hello", "ll", 2 );
120 CompareSplit( "", " " );
124 // separartor is a regexp
125 // separator regexp value global setting is set
126 // string is an empty string
127 // if separator is an empty string, split each by character
129 // this is not a String object
131 // limit is not a number
132 // limit is undefined
133 // limit is larger than 2^32-1
134 // limit is a negative number
138 function CompareSplit( string, separator ) {
139 split_1 = string.split( separator );
140 split_2 = string_split( string, separator );
143 "( " + string +".split(" + separator + ") ).length" ,
147 var limit = split_1.length > split_2.length ?
148 split_1.length : split_2.length;
150 for ( var split_item = 0; split_item < limit; split_item++ ) {
152 string + ".split(" + separator + ")["+split_item+"]",
154 split_1[split_item] );
158 function CompareSplitWithLimit( string, separator, splitlimit ) {
159 split_1 = string.split( separator, splitlimit );
160 split_2 = string_split( string, separator, splitlimit );
163 "( " + string +".split(" + separator + ", " + splitlimit+") ).length" ,
167 var limit = split_1.length > split_2.length ?
168 split_1.length : split_2.length;
170 for ( var split_item = 0; split_item < limit; split_item++ ) {
172 string + ".split(" + separator + ", " + splitlimit+")["+split_item+"]",
174 split_1[split_item] );
178 function string_split ( __this, separator, limit ) {
179 var S = String(__this ); // 1
181 var A = new Array(); // 2
183 if ( limit == undefined ) { // 3
184 lim = Math.pow(2, 31 ) -1;
186 lim = ToUint32( limit );
189 var s = S.length; // 4
192 if ( separator == undefined ) { // 8
197 if ( separator.constructor == RegExp ) // 6
200 R = separator.toString();
202 if (lim == 0) return A; // 7
204 if ( separator == undefined ) { // 8
210 z = SplitMatch(R, S, 0);
211 if (z != false) return A;
220 if ( q == s ) break; // 11
222 z = SplitMatch(R, S, q); // 12
224 //print("Returned ", z);
226 if (z != false) { // 13
227 e = z.endIndex; // 14
228 cap = z.captures; // 14
230 //print("S = ", S, ", p = ", p, ", q = ", q);
231 T = S.slice(p, q); // 16
233 A[A.length] = T; // 17
234 if (A.length == lim) return A; // 18
238 if (i == cap.length) { // 21
243 A[A.length] = cap[i] // 23
244 if (A.length == lim) return A; // 24
257 function SplitMatch(R, S, q)
259 if (R.constructor == RegExp) { // 1
260 var reResult = R.match(S, q); // 8
261 if (reResult == undefined)
264 a = new Array(reResult.length - 1);
265 for (var i = 1; i < reResult.length; i++)
266 a[a.length] = reResult[i];
267 return { endIndex : reResult.index + reResult[0].length, captures : cap };
271 var r = R.length; // 2
273 if ((q + r) > s) return false; // 4
274 for (var i = 0; i < r; i++) {
275 //print("S.charAt(", q + i, ") = ", S.charAt(q + i), ", R.charAt(", i, ") = ", R.charAt(i));
276 if (S.charAt(q + i) != R.charAt(i)) // 5
279 cap = new Array(); // 6
280 return { endIndex : q + r, captures : cap }; // 7
284 function ToUint32( n ) {
286 var sign = ( n < 0 ) ? -1 : 1;
288 if ( Math.abs( n ) == 0
289 || Math.abs( n ) == Number.POSITIVE_INFINITY
293 n = sign * Math.floor( Math.abs(n) )
295 n = n % Math.pow(2,32);