string-split.js revision a7e24c173cf37484693b9abb38e494fa7bd7baeb
1// 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(/<(\/)?([^<>]+)>/);
30assertArrayEquals(expected, result, 1);
31
32expected = ["a", "b"];
33result = "ab".split(/a*?/);
34assertArrayEquals(expected, result, 2);
35
36expected = ["", "b"];
37result = "ab".split(/a*/);
38assertArrayEquals(expected, result, 3);
39
40expected = ["a"];
41result = "ab".split(/a*?/, 1);
42assertArrayEquals(expected, result, 4);
43
44expected = [""];
45result = "ab".split(/a*/, 1);
46assertArrayEquals(expected, result, 5);
47
48expected = ["as","fas","fas","f"];
49result = "asdfasdfasdf".split("d");
50assertArrayEquals(expected, result, 6);
51
52expected = ["as","fas","fas","f"];
53result = "asdfasdfasdf".split("d", -1);
54assertArrayEquals(expected, result, 7);
55
56expected = ["as", "fas"];
57result = "asdfasdfasdf".split("d", 2);
58assertArrayEquals(expected, result, 8);
59
60expected = [];
61result = "asdfasdfasdf".split("d", 0);
62assertArrayEquals(expected, result, 9);
63
64expected = ["as","fas","fas",""];
65result = "asdfasdfasd".split("d");
66assertArrayEquals(expected, result, 10);
67
68expected = [];
69result = "".split("");
70assertArrayEquals(expected, result, 11);
71
72expected = [""]
73result = "".split("a");
74assertArrayEquals(expected, result, 12);
75
76expected = ["a","b"]
77result = "axxb".split(/x*/);
78assertArrayEquals(expected, result, 13);
79
80expected = ["a","b"]
81result = "axxb".split(/x+/);
82assertArrayEquals(expected, result, 14);
83
84expected = ["a","","b"]
85result = "axxb".split(/x/);
86assertArrayEquals(expected, result, 15);
87
88// This was http://b/issue?id=1151354
89expected = ["div", "#id", ".class"]
90result = "div#id.class".split(/(?=[#.])/);
91assertArrayEquals(expected, result, 16);
92
93expected = ["div", "#i", "d", ".class"]
94result = "div#id.class".split(/(?=[d#.])/);
95assertArrayEquals(expected, result, 17);
96
97expected = ["a", "b", "c"]
98result = "abc".split(/(?=.)/);
99assertArrayEquals(expected, result, 18);
100
101/* "ab".split(/((?=.))/)
102 *
103 * KJS:   ,a,,b
104 * SM:    a,,b,
105 * IE:    a,b
106 * Opera: a,,b
107 * V8:    a,,b
108 *
109 * Opera seems to have this right.  The others make no sense.
110 */
111expected = ["a", "", "b"]
112result = "ab".split(/((?=.))/);
113assertArrayEquals(expected, result, 19);
114
115/* "ab".split(/(?=)/)
116 *
117 * KJS:   a,b
118 * SM:    ab
119 * IE:    a,b
120 * Opera: a,b
121 * V8:    a,b
122 */
123expected = ["a", "b"]
124result = "ab".split(/(?=)/);
125assertArrayEquals(expected, result, 20);
126
127