]>
git.saurik.com Git - apple/javascriptcore.git/blob - tests/mozilla/ecma/String/15.5.4.8-3.js
1 /* The contents of this file are subject to the Netscape Public
2 * License Version 1.1 (the "License"); you may not use this file
3 * except in compliance with the License. You may obtain a copy of
4 * the License at http://www.mozilla.org/NPL/
6 * Software distributed under the License is distributed on an "AS
7 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
8 * implied. See the License for the specific language governing
9 * rights and limitations under the License.
11 * The Original Code is Mozilla Communicator client code, released March
14 * The Initial Developer of the Original Code is Netscape Communications
15 * Corporation. Portions created by Netscape are
16 * Copyright (C) 1998 Netscape Communications Corporation. All
23 File Name: 15.5.4.8-3.js
24 ECMA Section: 15.5.4.8 String.prototype.split( separator )
27 Returns an Array object into which substrings of the result of converting
28 this object to a string have been stored. The substrings are determined by
29 searching from left to right for occurrences of the given separator; these
30 occurrences are not part of any substring in the returned array, but serve
31 to divide up this string value. The separator may be a string of any length.
33 As a special case, if the separator is the empty string, the string is split
34 up into individual characters; the length of the result array equals the
35 length of the string, and each substring contains one character.
37 If the separator is not supplied, then the result array contains just one
38 string, which is the string.
40 When the split method is called with one argument separator, the following steps are taken:
42 1. Call ToString, giving it the this value as its argument.
43 2. Create a new Array object of length 0 and call it A.
44 3. If separator is not supplied, call the [[Put]] method of A with 0 and
45 Result(1) as arguments, and then return A.
46 4. Call ToString(separator).
47 5. Compute the number of characters in Result(1).
48 6. Compute the number of characters in the string that is Result(4).
50 8. If Result(6) is zero (the separator string is empty), go to step 17.
51 9. Compute the smallest possible integer k not smaller than p such that
52 k+Result(6) is not greater than Result(5), and for all nonnegative
53 integers j less than Result(6), the character at position k+j of
54 Result(1) is the same as the character at position j of Result(2);
55 but if there is no such integer k, then go to step 14.
56 10. Compute a string value equal to the substring of Result(1), consisting
57 of the characters at positions p through k1, inclusive.
58 11. Call the [[Put]] method of A with A.length and Result(10) as arguments.
59 12. Let p be k+Result(6).
61 14. Compute a string value equal to the substring of Result(1), consisting
62 of the characters from position p to the end of Result(1).
63 15. Call the [[Put]] method of A with A.length and Result(14) as arguments.
65 17. If p equals Result(5), return A.
66 18. Compute a string value equal to the substring of Result(1), consisting of
67 the single character at position p.
68 19. Call the [[Put]] method of A with A.length and Result(18) as arguments.
72 Note that the split function is intentionally generic; it does not require that its this value be a String
73 object. Therefore it can be transferred to other kinds of objects for use as a method.
75 Author: christine@netscape.com
76 Date: 12 november 1997
79 var SECTION
= "15.5.4.8-3";
80 var VERSION
= "ECMA_1";
82 var TITLE
= "String.prototype.split";
84 writeHeaderToLog( SECTION
+ " "+ TITLE
);
86 var testcases
= getTestCases();
89 function getTestCases() {
90 var array
= new Array();
94 var EXPECT
= new Array();
96 // this.toString is the empty string.
98 array
[item
++] = new TestCase( SECTION
,
99 "var s = new String(); s.split().length",
101 eval("var s = new String(); s.split().length") );
103 array
[item
++] = new TestCase( SECTION
,
104 "var s = new String(); s.split()[0]",
106 eval("var s = new String(); s.split()[0]") );
108 // this.toString() is the empty string, separator is specified.
110 array
[item
++] = new TestCase( SECTION
,
111 "var s = new String(); s.split('').length",
113 eval("var s = new String(); s.split('').length") );
115 array
[item
++] = new TestCase( SECTION
,
116 "var s = new String(); s.split(' ').length",
118 eval("var s = new String(); s.split(' ').length") );
120 // this to string is " "
121 array
[item
++] = new TestCase( SECTION
,
122 "var s = new String(' '); s.split().length",
124 eval("var s = new String(' '); s.split().length") );
126 array
[item
++] = new TestCase( SECTION
,
127 "var s = new String(' '); s.split()[0]",
129 eval("var s = new String(' '); s.split()[0]") );
131 array
[item
++] = new TestCase( SECTION
,
132 "var s = new String(' '); s.split('').length",
134 eval("var s = new String(' '); s.split('').length") );
136 array
[item
++] = new TestCase( SECTION
,
137 "var s = new String(' '); s.split('')[0]",
139 eval("var s = new String(' '); s.split('')[0]") );
141 array
[item
++] = new TestCase( SECTION
,
142 "var s = new String(' '); s.split(' ').length",
144 eval("var s = new String(' '); s.split(' ').length") );
146 array
[item
++] = new TestCase( SECTION
,
147 "var s = new String(' '); s.split(' ')[0]",
149 eval("var s = new String(' '); s.split(' ')[0]") );
151 array
[item
++] = new TestCase( SECTION
,
152 "\"\".split(\"\").length",
154 ("".split("")).length
);
156 array
[item
++] = new TestCase( SECTION
,
157 "\"\".split(\"x\").length",
159 ("".split("x")).length
);
161 array
[item
++] = new TestCase( SECTION
,
162 "\"\".split(\"x\")[0]",
164 ("".split("x"))[0] );
169 for ( tc
=0; tc
< testcases
.length
; tc
++ ) {
170 testcases
[tc
].passed
= writeTestCaseResult(
171 testcases
[tc
].expect
,
172 testcases
[tc
].actual
,
173 testcases
[tc
].description
+" = "+
174 testcases
[tc
].actual
);
176 testcases
[tc
].reason
+= ( testcases
[tc
].passed
) ? "" : "wrong value ";
179 return ( testcases
);
181 function Split( string
, separator
) {
182 string
= String( string
);
186 if ( arguments
.length
< 2 ) {
191 separator
= String( separator
);
193 var str_len
= String( string
).length
;
194 var sep_len
= String( separator
).length
;
199 if ( sep_len
== 0 ) {
200 for ( ; p
< str_len
; p
++ ) {
201 A
[A
.length
] = String( string
.charAt(p
) );