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 ***** */
41 File Name: 15.5.4.8-3.js
42 ECMA Section: 15.5.4.8 String.prototype.split( separator )
45 Returns an Array object into which substrings of the result of converting
46 this object to a string have been stored. The substrings are determined by
47 searching from left to right for occurrences of the given separator; these
48 occurrences are not part of any substring in the returned array, but serve
49 to divide up this string value. The separator may be a string of any length.
51 As a special case, if the separator is the empty string, the string is split
52 up into individual characters; the length of the result array equals the
53 length of the string, and each substring contains one character.
55 If the separator is not supplied, then the result array contains just one
56 string, which is the string.
58 When the split method is called with one argument separator, the following steps are taken:
60 1. Call ToString, giving it the this value as its argument.
61 2. Create a new Array object of length 0 and call it A.
62 3. If separator is not supplied, call the [[Put]] method of A with 0 and
63 Result(1) as arguments, and then return A.
64 4. Call ToString(separator).
65 5. Compute the number of characters in Result(1).
66 6. Compute the number of characters in the string that is Result(4).
68 8. If Result(6) is zero (the separator string is empty), go to step 17.
69 9. Compute the smallest possible integer k not smaller than p such that
70 k+Result(6) is not greater than Result(5), and for all nonnegative
71 integers j less than Result(6), the character at position k+j of
72 Result(1) is the same as the character at position j of Result(2);
73 but if there is no such integer k, then go to step 14.
74 10. Compute a string value equal to the substring of Result(1), consisting
75 of the characters at positions p through k1, inclusive.
76 11. Call the [[Put]] method of A with A.length and Result(10) as arguments.
77 12. Let p be k+Result(6).
79 14. Compute a string value equal to the substring of Result(1), consisting
80 of the characters from position p to the end of Result(1).
81 15. Call the [[Put]] method of A with A.length and Result(14) as arguments.
83 17. If p equals Result(5), return A.
84 18. Compute a string value equal to the substring of Result(1), consisting of
85 the single character at position p.
86 19. Call the [[Put]] method of A with A.length and Result(18) as arguments.
90 Note that the split function is intentionally generic; it does not require that its this value be a String
91 object. Therefore it can be transferred to other kinds of objects for use as a method.
93 Author: christine@netscape.com
94 Date: 12 november 1997
97 var SECTION = "15.5.4.8-3";
98 var VERSION = "ECMA_1";
100 var TITLE = "String.prototype.split";
102 writeHeaderToLog( SECTION + " "+ TITLE);
104 var TEST_STRING = "";
105 var EXPECT = new Array();
107 // this.toString is the empty string.
109 new TestCase( SECTION,
110 "var s = new String(); s.split().length",
112 eval("var s = new String(); s.split().length") );
114 new TestCase( SECTION,
115 "var s = new String(); s.split()[0]",
117 eval("var s = new String(); s.split()[0]") );
119 // this.toString() is the empty string, separator is specified.
121 new TestCase( SECTION,
122 "var s = new String(); s.split('').length",
124 eval("var s = new String(); s.split('').length") );
126 new TestCase( SECTION,
127 "var s = new String(); s.split(' ').length",
129 eval("var s = new String(); s.split(' ').length") );
131 // this to string is " "
132 new TestCase( SECTION,
133 "var s = new String(' '); s.split().length",
135 eval("var s = new String(' '); s.split().length") );
137 new TestCase( SECTION,
138 "var s = new String(' '); s.split()[0]",
140 eval("var s = new String(' '); s.split()[0]") );
142 new TestCase( SECTION,
143 "var s = new String(' '); s.split('').length",
145 eval("var s = new String(' '); s.split('').length") );
147 new TestCase( SECTION,
148 "var s = new String(' '); s.split('')[0]",
150 eval("var s = new String(' '); s.split('')[0]") );
152 new TestCase( SECTION,
153 "var s = new String(' '); s.split(' ').length",
155 eval("var s = new String(' '); s.split(' ').length") );
157 new TestCase( SECTION,
158 "var s = new String(' '); s.split(' ')[0]",
160 eval("var s = new String(' '); s.split(' ')[0]") );
162 new TestCase( SECTION,
163 "\"\".split(\"\").length",
165 ("".split("")).length );
167 new TestCase( SECTION,
168 "\"\".split(\"x\").length",
170 ("".split("x")).length );
172 new TestCase( SECTION,
173 "\"\".split(\"x\")[0]",
175 ("".split("x"))[0] );
179 function Split( string, separator ) {
180 string = String( string );
184 if ( arguments.length < 2 ) {
189 separator = String( separator );
191 var str_len = String( string ).length;
192 var sep_len = String( separator ).length;
197 if ( sep_len == 0 ) {
198 for ( ; p < str_len; p++ ) {
199 A[A.length] = String( string.charAt(p) );