Debian Codegraph @

rhino/testsrc/tests/ecma/String/15.5.4.8-3.js

raw symbols SCIP
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 gTestfile = '15.5.4.8-3.js';
7
8 /**
9 File Name: 15.5.4.8-3.js
10 ECMA Section: 15.5.4.8 String.prototype.split( separator )
11 Description:
12
13 Returns an Array object into which substrings of the result of converting
14 this object to a string have been stored. The substrings are determined by
15 searching from left to right for occurrences of the given separator; these
16 occurrences are not part of any substring in the returned array, but serve
17 to divide up this string value. The separator may be a string of any length.
18
19 As a special case, if the separator is the empty string, the string is split
20 up into individual characters; the length of the result array equals the
21 length of the string, and each substring contains one character.
22
23 If the separator is not supplied, then the result array contains just one
24 string, which is the string.
25
26 When the split method is called with one argument separator, the following steps are taken:
27
28 1. Call ToString, giving it the this value as its argument.
29 2. Create a new Array object of length 0 and call it A.
30 3. If separator is not supplied, call the [[Put]] method of A with 0 and
31 Result(1) as arguments, and then return A.
32 4. Call ToString(separator).
33 5. Compute the number of characters in Result(1).
34 6. Compute the number of characters in the string that is Result(4).
35 7. Let p be 0.
36 8. If Result(6) is zero (the separator string is empty), go to step 17.
37 9. Compute the smallest possible integer k not smaller than p such that
38 k+Result(6) is not greater than Result(5), and for all nonnegative
39 integers j less than Result(6), the character at position k+j of
40 Result(1) is the same as the character at position j of Result(2);
41 but if there is no such integer k, then go to step 14.
42 10. Compute a string value equal to the substring of Result(1), consisting
43 of the characters at positions p through k1, inclusive.
44 11. Call the [[Put]] method of A with A.length and Result(10) as arguments.
45 12. Let p be k+Result(6).
46 13. Go to step 9.
47 14. Compute a string value equal to the substring of Result(1), consisting
48 of the characters from position p to the end of Result(1).
49 15. Call the [[Put]] method of A with A.length and Result(14) as arguments.
50 16. Return A.
51 17. If p equals Result(5), return A.
52 18. Compute a string value equal to the substring of Result(1), consisting of
53 the single character at position p.
54 19. Call the [[Put]] method of A with A.length and Result(18) as arguments.
55 20. Increase p by 1.
56 21. Go to step 17.
57
58 Note that the split function is intentionally generic; it does not require that its this value be a String
59 object. Therefore it can be transferred to other kinds of objects for use as a method.
60
61 Author: [email protected]
62 Date: 12 november 1997
63 */
64
65 var SECTION = "15.5.4.8-3";
66 var VERSION = "ECMA_1";
67 startTest();
68 var TITLE = "String.prototype.split";
69
70 writeHeaderToLog( SECTION + " "+ TITLE);
71
72 var TEST_STRING = "";
73 var EXPECT = new Array();
74
75 // this.toString is the empty string.
76
77 new TestCase( SECTION,
78 "var s = new String(); s.split().length",
79 1,
80 eval("var s = new String(); s.split().length") );
81
82 new TestCase( SECTION,
83 "var s = new String(); s.split()[0]",
84 "",
85 eval("var s = new String(); s.split()[0]") );
86
87 // this.toString() is the empty string, separator is specified.
88
89 new TestCase( SECTION,
90 "var s = new String(); s.split('').length",
91 0,
92 eval("var s = new String(); s.split('').length") );
93
94 new TestCase( SECTION,
95 "var s = new String(); s.split(' ').length",
96 1,
97 eval("var s = new String(); s.split(' ').length") );
98
99 // this to string is " "
100 new TestCase( SECTION,
101 "var s = new String(' '); s.split().length",
102 1,
103 eval("var s = new String(' '); s.split().length") );
104
105 new TestCase( SECTION,
106 "var s = new String(' '); s.split()[0]",
107 " ",
108 eval("var s = new String(' '); s.split()[0]") );
109
110 new TestCase( SECTION,
111 "var s = new String(' '); s.split('').length",
112 1,
113 eval("var s = new String(' '); s.split('').length") );
114
115 new TestCase( SECTION,
116 "var s = new String(' '); s.split('')[0]",
117 " ",
118 eval("var s = new String(' '); s.split('')[0]") );
119
120 new TestCase( SECTION,
121 "var s = new String(' '); s.split(' ').length",
122 2,
123 eval("var s = new String(' '); s.split(' ').length") );
124
125 new TestCase( SECTION,
126 "var s = new String(' '); s.split(' ')[0]",
127 "",
128 eval("var s = new String(' '); s.split(' ')[0]") );
129
130 new TestCase( SECTION,
131 "\"\".split(\"\").length",
132 0,
133 ("".split("")).length );
134
135 new TestCase( SECTION,
136 "\"\".split(\"x\").length",
137 1,
138 ("".split("x")).length );
139
140 new TestCase( SECTION,
141 "\"\".split(\"x\")[0]",
142 "",
143 ("".split("x"))[0] );
144
145 test();
146
147 function Split( string, separator ) {
148 string = String( string );
149
150 var A = new Array();
151
152 if ( arguments.length < 2 ) {
153 A[0] = string;
154 return A;
155 }
156
157 separator = String( separator );
158
159 var str_len = String( string ).length;
160 var sep_len = String( separator ).length;
161
162 var p = 0;
163 var k = 0;
164
165 if ( sep_len == 0 ) {
166 for ( ; p < str_len; p++ ) {
167 A[A.length] = String( string.charAt(p) );
168 }
169 }
170 return A;
171 }