]>
Commit | Line | Data |
---|---|---|
b37bf2e1 A |
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/ | |
5 | * | |
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. | |
10 | * | |
11 | * The Original Code is Mozilla Communicator client code, released March | |
12 | * 31, 1998. | |
13 | * | |
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 | |
17 | * Rights Reserved. | |
18 | * | |
19 | * Contributor(s): | |
20 | * | |
21 | */ | |
22 | /** | |
23 | File Name: 15.5.4.8-3.js | |
24 | ECMA Section: 15.5.4.8 String.prototype.split( separator ) | |
25 | Description: | |
26 | ||
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. | |
32 | ||
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. | |
36 | ||
37 | If the separator is not supplied, then the result array contains just one | |
38 | string, which is the string. | |
39 | ||
40 | When the split method is called with one argument separator, the following steps are taken: | |
41 | ||
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). | |
49 | 7. Let p be 0. | |
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). | |
60 | 13. Go to step 9. | |
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. | |
64 | 16. Return A. | |
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. | |
69 | 20. Increase p by 1. | |
70 | 21. Go to step 17. | |
71 | ||
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. | |
74 | ||
75 | Author: christine@netscape.com | |
76 | Date: 12 november 1997 | |
77 | */ | |
78 | ||
79 | var SECTION = "15.5.4.8-3"; | |
80 | var VERSION = "ECMA_1"; | |
81 | startTest(); | |
82 | var TITLE = "String.prototype.split"; | |
83 | ||
84 | writeHeaderToLog( SECTION + " "+ TITLE); | |
85 | ||
86 | var testcases = getTestCases(); | |
87 | test(); | |
88 | ||
89 | function getTestCases() { | |
90 | var array = new Array(); | |
91 | var item = 0; | |
92 | ||
93 | var TEST_STRING = ""; | |
94 | var EXPECT = new Array(); | |
95 | ||
96 | // this.toString is the empty string. | |
97 | ||
98 | array[item++] = new TestCase( SECTION, | |
99 | "var s = new String(); s.split().length", | |
100 | 1, | |
101 | eval("var s = new String(); s.split().length") ); | |
102 | ||
103 | array[item++] = new TestCase( SECTION, | |
104 | "var s = new String(); s.split()[0]", | |
105 | "", | |
106 | eval("var s = new String(); s.split()[0]") ); | |
107 | ||
108 | // this.toString() is the empty string, separator is specified. | |
109 | ||
110 | array[item++] = new TestCase( SECTION, | |
111 | "var s = new String(); s.split('').length", | |
112 | 0, | |
113 | eval("var s = new String(); s.split('').length") ); | |
114 | ||
115 | array[item++] = new TestCase( SECTION, | |
116 | "var s = new String(); s.split(' ').length", | |
117 | 1, | |
118 | eval("var s = new String(); s.split(' ').length") ); | |
119 | ||
120 | // this to string is " " | |
121 | array[item++] = new TestCase( SECTION, | |
122 | "var s = new String(' '); s.split().length", | |
123 | 1, | |
124 | eval("var s = new String(' '); s.split().length") ); | |
125 | ||
126 | array[item++] = new TestCase( SECTION, | |
127 | "var s = new String(' '); s.split()[0]", | |
128 | " ", | |
129 | eval("var s = new String(' '); s.split()[0]") ); | |
130 | ||
131 | array[item++] = new TestCase( SECTION, | |
132 | "var s = new String(' '); s.split('').length", | |
133 | 1, | |
134 | eval("var s = new String(' '); s.split('').length") ); | |
135 | ||
136 | array[item++] = new TestCase( SECTION, | |
137 | "var s = new String(' '); s.split('')[0]", | |
138 | " ", | |
139 | eval("var s = new String(' '); s.split('')[0]") ); | |
140 | ||
141 | array[item++] = new TestCase( SECTION, | |
142 | "var s = new String(' '); s.split(' ').length", | |
143 | 2, | |
144 | eval("var s = new String(' '); s.split(' ').length") ); | |
145 | ||
146 | array[item++] = new TestCase( SECTION, | |
147 | "var s = new String(' '); s.split(' ')[0]", | |
148 | "", | |
149 | eval("var s = new String(' '); s.split(' ')[0]") ); | |
150 | ||
151 | array[item++] = new TestCase( SECTION, | |
152 | "\"\".split(\"\").length", | |
153 | 0, | |
154 | ("".split("")).length ); | |
155 | ||
156 | array[item++] = new TestCase( SECTION, | |
157 | "\"\".split(\"x\").length", | |
158 | 1, | |
159 | ("".split("x")).length ); | |
160 | ||
161 | array[item++] = new TestCase( SECTION, | |
162 | "\"\".split(\"x\")[0]", | |
163 | "", | |
164 | ("".split("x"))[0] ); | |
165 | ||
166 | return array; | |
167 | } | |
168 | function test() { | |
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 ); | |
175 | ||
176 | testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value "; | |
177 | } | |
178 | stopTest(); | |
179 | return ( testcases ); | |
180 | } | |
181 | function Split( string, separator ) { | |
182 | string = String( string ); | |
183 | ||
184 | var A = new Array(); | |
185 | ||
186 | if ( arguments.length < 2 ) { | |
187 | A[0] = string; | |
188 | return A; | |
189 | } | |
190 | ||
191 | separator = String( separator ); | |
192 | ||
193 | var str_len = String( string ).length; | |
194 | var sep_len = String( separator ).length; | |
195 | ||
196 | var p = 0; | |
197 | var k = 0; | |
198 | ||
199 | if ( sep_len == 0 ) { | |
200 | for ( ; p < str_len; p++ ) { | |
201 | A[A.length] = String( string.charAt(p) ); | |
202 | } | |
203 | } | |
204 | return A; | |
205 | } |