Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 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 | |
| 28 | expected = ["A", undefined, "B", "bold", "/", "B", "and", undefined, "CODE", "coded", "/", "CODE", ""]; |
| 29 | result = "A<B>bold</B>and<CODE>coded</CODE>".split(/<(\/)?([^<>]+)>/); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 30 | assertArrayEquals(expected, result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 31 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 32 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 33 | assertArrayEquals(["a", "b"], "ab".split(/a*?/)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 34 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 35 | assertArrayEquals(["", "b"], "ab".split(/a*/)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 36 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 37 | assertArrayEquals(["a"], "ab".split(/a*?/, 1)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 38 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 39 | assertArrayEquals([""], "ab".split(/a*/, 1)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 40 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 41 | assertArrayEquals(["as","fas","fas","f"], "asdfasdfasdf".split("d")); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 42 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 43 | assertArrayEquals(["as","fas","fas","f"], "asdfasdfasdf".split("d", -1)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 44 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 45 | assertArrayEquals(["as", "fas"], "asdfasdfasdf".split("d", 2)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 46 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 47 | assertArrayEquals([], "asdfasdfasdf".split("d", 0)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 48 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 49 | assertArrayEquals(["as","fas","fas",""], "asdfasdfasd".split("d")); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 50 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 51 | assertArrayEquals([], "".split("")); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 52 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 53 | assertArrayEquals([""], "".split("a")); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 54 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 55 | assertArrayEquals(["a","b"], "axxb".split(/x*/)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 56 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 57 | assertArrayEquals(["a","b"], "axxb".split(/x+/)); |
| 58 | |
| 59 | assertArrayEquals(["a","","b"], "axxb".split(/x/)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 60 | |
| 61 | // This was http://b/issue?id=1151354 |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 62 | assertArrayEquals(["div", "#id", ".class"], "div#id.class".split(/(?=[#.])/)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 63 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 64 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 65 | assertArrayEquals(["div", "#i", "d", ".class"], "div#id.class".split(/(?=[d#.])/)); |
| 66 | |
| 67 | assertArrayEquals(["a", "b", "c"], "abc".split(/(?=.)/)); |
| 68 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 69 | assertArrayEquals(["Wenige", "sind", "auserwählt."], |
| 70 | "Wenige sind auserwählt.".split(" ")); |
| 71 | |
| 72 | assertArrayEquals([], "Wenige sind auserwählt.".split(" ", 0)); |
| 73 | |
| 74 | assertArrayEquals(["Wenige"], "Wenige sind auserwählt.".split(" ", 1)); |
| 75 | |
| 76 | assertArrayEquals(["Wenige", "sind"], "Wenige sind auserwählt.".split(" ", 2)); |
| 77 | |
| 78 | assertArrayEquals(["Wenige", "sind", "auserwählt."], |
| 79 | "Wenige sind auserwählt.".split(" ", 3)); |
| 80 | |
| 81 | assertArrayEquals(["Wenige sind auserw", "hlt."], |
| 82 | "Wenige sind auserwählt.".split("ä")); |
| 83 | |
| 84 | assertArrayEquals(["Wenige sind ", "."], |
| 85 | "Wenige sind auserwählt.".split("auserwählt")); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 86 | |
| 87 | /* "ab".split(/((?=.))/) |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 88 | * |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 89 | * KJS: ,a,,b |
| 90 | * SM: a,,b, |
| 91 | * IE: a,b |
| 92 | * Opera: a,,b |
| 93 | * V8: a,,b |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 94 | * |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 95 | * Opera seems to have this right. The others make no sense. |
| 96 | */ |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 97 | assertArrayEquals(["a", "", "b"], "ab".split(/((?=.))/)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 98 | |
| 99 | /* "ab".split(/(?=)/) |
| 100 | * |
| 101 | * KJS: a,b |
| 102 | * SM: ab |
| 103 | * IE: a,b |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 104 | * Opera: a,bb |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 105 | * V8: a,b |
| 106 | */ |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 107 | assertArrayEquals(["a", "b"], "ab".split(/(?=)/)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 108 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 109 | |
| 110 | // For issue http://code.google.com/p/v8/issues/detail?id=924 |
| 111 | // Splitting the empty string is a special case. |
| 112 | assertEquals([""], ''.split()); |
| 113 | assertEquals([""], ''.split(/./)); |
| 114 | assertEquals([], ''.split(/.?/)); |
| 115 | assertEquals([], ''.split(/.??/)); |
| 116 | assertEquals([], ''.split(/()()/)); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 117 | |
| 118 | |
| 119 | // Issue http://code.google.com/p/v8/issues/detail?id=929 |
| 120 | // (Splitting with empty separator and a limit.) |
| 121 | |
| 122 | function numberObj(num) { |
| 123 | return {valueOf: function() { return num; }}; |
| 124 | } |
| 125 | |
| 126 | assertEquals([], "abc".split("", 0)); |
| 127 | assertEquals([], "abc".split("", numberObj(0))); |
| 128 | assertEquals(["a"], "abc".split("", 1)); |
| 129 | assertEquals(["a"], "abc".split("", numberObj(1))); |
| 130 | assertEquals(["a", "b"], "abc".split("", 2)); |
| 131 | assertEquals(["a", "b"], "abc".split("", numberObj(2))); |
| 132 | assertEquals(["a", "b", "c"], "abc".split("", 3)); |
| 133 | assertEquals(["a", "b", "c"], "abc".split("", numberObj(3))); |
| 134 | assertEquals(["a", "b", "c"], "abc".split("", 4)); |
| 135 | assertEquals(["a", "b", "c"], "abc".split("", numberObj(4))); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 136 | |
| 137 | |
| 138 | var all_ascii_codes = []; |
| 139 | for (var i = 0; i < 128; i++) all_ascii_codes[i] = i; |
| 140 | var all_ascii_string = String.fromCharCode.apply(String, all_ascii_codes); |
| 141 | |
| 142 | var split_chars = all_ascii_string.split(""); |
| 143 | assertEquals(128, split_chars.length); |
| 144 | for (var i = 0; i < 128; i++) { |
| 145 | assertEquals(1, split_chars[i].length); |
| 146 | assertEquals(i, split_chars[i].charCodeAt(0)); |
| 147 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 148 | |
| 149 | // Check that the separator is converted to string before returning due to |
| 150 | // limit == 0. |
| 151 | var counter = 0; |
| 152 | var separator = { toString: function() { counter++; return "b"; }}; |
| 153 | assertEquals([], "abc".split(separator, 0)); |
| 154 | assertEquals(1, counter); |
| 155 | |
| 156 | // Check that the subject is converted to string before the separator. |
| 157 | counter = 0; |
| 158 | var subject = { toString: function() { assertEquals(0, counter); |
| 159 | counter++; |
| 160 | return "abc"; }}; |
| 161 | separator = { toString: function() { assertEquals(1, counter); |
| 162 | counter++; |
| 163 | return "b"; }}; |
| 164 | |
| 165 | assertEquals(["a", "c"], String.prototype.split.call(subject, separator)); |
| 166 | assertEquals(2, counter); |