blob: efd0ef3eae3dc1e8843b33e69f7102e00dca4667 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2008 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28expected = ["A", undefined, "B", "bold", "/", "B", "and", undefined, "CODE", "coded", "/", "CODE", ""];
29result = "A<B>bold</B>and<CODE>coded</CODE>".split(/<(\/)?([^<>]+)>/);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -080030assertArrayEquals(expected, result);
Steve Blocka7e24c12009-10-30 11:49:00 +000031
Steve Blocka7e24c12009-10-30 11:49:00 +000032
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -080033assertArrayEquals(["a", "b"], "ab".split(/a*?/));
Steve Blocka7e24c12009-10-30 11:49:00 +000034
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -080035assertArrayEquals(["", "b"], "ab".split(/a*/));
Steve Blocka7e24c12009-10-30 11:49:00 +000036
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -080037assertArrayEquals(["a"], "ab".split(/a*?/, 1));
Steve Blocka7e24c12009-10-30 11:49:00 +000038
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -080039assertArrayEquals([""], "ab".split(/a*/, 1));
Steve Blocka7e24c12009-10-30 11:49:00 +000040
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -080041assertArrayEquals(["as","fas","fas","f"], "asdfasdfasdf".split("d"));
Steve Blocka7e24c12009-10-30 11:49:00 +000042
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -080043assertArrayEquals(["as","fas","fas","f"], "asdfasdfasdf".split("d", -1));
Steve Blocka7e24c12009-10-30 11:49:00 +000044
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -080045assertArrayEquals(["as", "fas"], "asdfasdfasdf".split("d", 2));
Steve Blocka7e24c12009-10-30 11:49:00 +000046
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -080047assertArrayEquals([], "asdfasdfasdf".split("d", 0));
Steve Blocka7e24c12009-10-30 11:49:00 +000048
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -080049assertArrayEquals(["as","fas","fas",""], "asdfasdfasd".split("d"));
Steve Blocka7e24c12009-10-30 11:49:00 +000050
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -080051assertArrayEquals([], "".split(""));
Steve Blocka7e24c12009-10-30 11:49:00 +000052
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -080053assertArrayEquals([""], "".split("a"));
Steve Blocka7e24c12009-10-30 11:49:00 +000054
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -080055assertArrayEquals(["a","b"], "axxb".split(/x*/));
Steve Blocka7e24c12009-10-30 11:49:00 +000056
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -080057assertArrayEquals(["a","b"], "axxb".split(/x+/));
58
59assertArrayEquals(["a","","b"], "axxb".split(/x/));
Steve Blocka7e24c12009-10-30 11:49:00 +000060
61// This was http://b/issue?id=1151354
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -080062assertArrayEquals(["div", "#id", ".class"], "div#id.class".split(/(?=[#.])/));
Steve Blocka7e24c12009-10-30 11:49:00 +000063
Steve Blocka7e24c12009-10-30 11:49:00 +000064
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -080065assertArrayEquals(["div", "#i", "d", ".class"], "div#id.class".split(/(?=[d#.])/));
66
67assertArrayEquals(["a", "b", "c"], "abc".split(/(?=.)/));
68
Ben Murdochb8a8cc12014-11-26 15:28:44 +000069assertArrayEquals(["Wenige", "sind", "auserwählt."],
70 "Wenige sind auserwählt.".split(" "));
71
72assertArrayEquals([], "Wenige sind auserwählt.".split(" ", 0));
73
74assertArrayEquals(["Wenige"], "Wenige sind auserwählt.".split(" ", 1));
75
76assertArrayEquals(["Wenige", "sind"], "Wenige sind auserwählt.".split(" ", 2));
77
78assertArrayEquals(["Wenige", "sind", "auserwählt."],
79 "Wenige sind auserwählt.".split(" ", 3));
80
81assertArrayEquals(["Wenige sind auserw", "hlt."],
82 "Wenige sind auserwählt.".split("ä"));
83
84assertArrayEquals(["Wenige sind ", "."],
85 "Wenige sind auserwählt.".split("auserwählt"));
Steve Blocka7e24c12009-10-30 11:49:00 +000086
87/* "ab".split(/((?=.))/)
Ben Murdoch589d6972011-11-30 16:04:58 +000088 *
Steve Blocka7e24c12009-10-30 11:49:00 +000089 * KJS: ,a,,b
90 * SM: a,,b,
91 * IE: a,b
92 * Opera: a,,b
93 * V8: a,,b
Ben Murdoch589d6972011-11-30 16:04:58 +000094 *
Steve Blocka7e24c12009-10-30 11:49:00 +000095 * Opera seems to have this right. The others make no sense.
96 */
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -080097assertArrayEquals(["a", "", "b"], "ab".split(/((?=.))/));
Steve Blocka7e24c12009-10-30 11:49:00 +000098
99/* "ab".split(/(?=)/)
100 *
101 * KJS: a,b
102 * SM: ab
103 * IE: a,b
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800104 * Opera: a,bb
Steve Blocka7e24c12009-10-30 11:49:00 +0000105 * V8: a,b
106 */
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800107assertArrayEquals(["a", "b"], "ab".split(/(?=)/));
Steve Blocka7e24c12009-10-30 11:49:00 +0000108
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800109
110// For issue http://code.google.com/p/v8/issues/detail?id=924
111// Splitting the empty string is a special case.
112assertEquals([""], ''.split());
113assertEquals([""], ''.split(/./));
114assertEquals([], ''.split(/.?/));
115assertEquals([], ''.split(/.??/));
116assertEquals([], ''.split(/()()/));
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800117
118
119// Issue http://code.google.com/p/v8/issues/detail?id=929
120// (Splitting with empty separator and a limit.)
121
122function numberObj(num) {
123 return {valueOf: function() { return num; }};
124}
125
126assertEquals([], "abc".split("", 0));
127assertEquals([], "abc".split("", numberObj(0)));
128assertEquals(["a"], "abc".split("", 1));
129assertEquals(["a"], "abc".split("", numberObj(1)));
130assertEquals(["a", "b"], "abc".split("", 2));
131assertEquals(["a", "b"], "abc".split("", numberObj(2)));
132assertEquals(["a", "b", "c"], "abc".split("", 3));
133assertEquals(["a", "b", "c"], "abc".split("", numberObj(3)));
134assertEquals(["a", "b", "c"], "abc".split("", 4));
135assertEquals(["a", "b", "c"], "abc".split("", numberObj(4)));
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000136
137
138var all_ascii_codes = [];
139for (var i = 0; i < 128; i++) all_ascii_codes[i] = i;
140var all_ascii_string = String.fromCharCode.apply(String, all_ascii_codes);
141
142var split_chars = all_ascii_string.split("");
143assertEquals(128, split_chars.length);
144for (var i = 0; i < 128; i++) {
145 assertEquals(1, split_chars[i].length);
146 assertEquals(i, split_chars[i].charCodeAt(0));
147}
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000148
149// Check that the separator is converted to string before returning due to
150// limit == 0.
151var counter = 0;
152var separator = { toString: function() { counter++; return "b"; }};
153assertEquals([], "abc".split(separator, 0));
154assertEquals(1, counter);
155
156// Check that the subject is converted to string before the separator.
157counter = 0;
158var subject = { toString: function() { assertEquals(0, counter);
159 counter++;
160 return "abc"; }};
161separator = { toString: function() { assertEquals(1, counter);
162 counter++;
163 return "b"; }};
164
165assertEquals(["a", "c"], String.prototype.split.call(subject, separator));
166assertEquals(2, counter);