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 Mozilla Communicator client code, released
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 1998
21 * the Initial Developer. All Rights Reserved.
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 gTestfile = '15.5.4.8-3.js';
42 File Name: 15.5.4.8-3.js
43 ECMA Section: 15.5.4.8 String.prototype.split( separator )
46 Returns an Array object into which substrings of the result of converting
47 this object to a string have been stored. The substrings are determined by
48 searching from left to right for occurrences of the given separator; these
49 occurrences are not part of any substring in the returned array, but serve
50 to divide up this string value. The separator may be a string of any length.
52 As a special case, if the separator is the empty string, the string is split
53 up into individual characters; the length of the result array equals the
54 length of the string, and each substring contains one character.
56 If the separator is not supplied, then the result array contains just one
57 string, which is the string.
59 When the split method is called with one argument separator, the following steps are taken:
61 1. Call ToString, giving it the this value as its argument.
62 2. Create a new Array object of length 0 and call it A.
63 3. If separator is not supplied, call the [[Put]] method of A with 0 and
64 Result(1) as arguments, and then return A.
65 4. Call ToString(separator).
66 5. Compute the number of characters in Result(1).
67 6. Compute the number of characters in the string that is Result(4).
69 8. If Result(6) is zero (the separator string is empty), go to step 17.
70 9. Compute the smallest possible integer k not smaller than p such that
71 k+Result(6) is not greater than Result(5), and for all nonnegative
72 integers j less than Result(6), the character at position k+j of
73 Result(1) is the same as the character at position j of Result(2);
74 but if there is no such integer k, then go to step 14.
75 10. Compute a string value equal to the substring of Result(1), consisting
76 of the characters at positions p through k1, inclusive.
77 11. Call the [[Put]] method of A with A.length and Result(10) as arguments.
78 12. Let p be k+Result(6).
80 14. Compute a string value equal to the substring of Result(1), consisting
81 of the characters from position p to the end of Result(1).
82 15. Call the [[Put]] method of A with A.length and Result(14) as arguments.
84 17. If p equals Result(5), return A.
85 18. Compute a string value equal to the substring of Result(1), consisting of
86 the single character at position p.
87 19. Call the [[Put]] method of A with A.length and Result(18) as arguments.
91 Note that the split function is intentionally generic; it does not require that its this value be a String
92 object. Therefore it can be transferred to other kinds of objects for use as a method.
94 Author: christine@netscape.com
95 Date: 12 november 1997
98 var SECTION = "15.5.4.8-3";
99 var VERSION = "ECMA_1";
101 var TITLE = "String.prototype.split";
103 writeHeaderToLog( SECTION + " "+ TITLE);
105 var TEST_STRING = "";
106 var EXPECT = new Array();
108 // this.toString is the empty string.
110 new TestCase( SECTION,
111 "var s = new String(); s.split().length",
113 eval("var s = new String(); s.split().length") );
115 new TestCase( SECTION,
116 "var s = new String(); s.split()[0]",
118 eval("var s = new String(); s.split()[0]") );
120 // this.toString() is the empty string, separator is specified.
122 new TestCase( SECTION,
123 "var s = new String(); s.split('').length",
125 eval("var s = new String(); s.split('').length") );
127 new TestCase( SECTION,
128 "var s = new String(); s.split(' ').length",
130 eval("var s = new String(); s.split(' ').length") );
132 // this to string is " "
133 new TestCase( SECTION,
134 "var s = new String(' '); s.split().length",
136 eval("var s = new String(' '); s.split().length") );
138 new TestCase( SECTION,
139 "var s = new String(' '); s.split()[0]",
141 eval("var s = new String(' '); s.split()[0]") );
143 new TestCase( SECTION,
144 "var s = new String(' '); s.split('').length",
146 eval("var s = new String(' '); s.split('').length") );
148 new TestCase( SECTION,
149 "var s = new String(' '); s.split('')[0]",
151 eval("var s = new String(' '); s.split('')[0]") );
153 new TestCase( SECTION,
154 "var s = new String(' '); s.split(' ').length",
156 eval("var s = new String(' '); s.split(' ').length") );
158 new TestCase( SECTION,
159 "var s = new String(' '); s.split(' ')[0]",
161 eval("var s = new String(' '); s.split(' ')[0]") );
163 new TestCase( SECTION,
164 "\"\".split(\"\").length",
166 ("".split("")).length );
168 new TestCase( SECTION,
169 "\"\".split(\"x\").length",
171 ("".split("x")).length );
173 new TestCase( SECTION,
174 "\"\".split(\"x\")[0]",
176 ("".split("x"))[0] );
180 function Split( string, separator ) {
181 string = String( string );
185 if ( arguments.length < 2 ) {
190 separator = String( separator );
192 var str_len = String( string ).length;
193 var sep_len = String( separator ).length;
198 if ( sep_len == 0 ) {
199 for ( ; p < str_len; p++ ) {
200 A[A.length] = String( string.charAt(p) );