ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1 | // Copyright 2008 the V8 project authors. All rights reserved. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 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(/<(\/)?([^<>]+)>/); |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 30 | assertArrayEquals(expected, result); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 31 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 32 | |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 33 | assertArrayEquals(["a", "b"], "ab".split(/a*?/)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 34 | |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 35 | assertArrayEquals(["", "b"], "ab".split(/a*/)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 36 | |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 37 | assertArrayEquals(["a"], "ab".split(/a*?/, 1)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 38 | |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 39 | assertArrayEquals([""], "ab".split(/a*/, 1)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 40 | |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 41 | assertArrayEquals(["as","fas","fas","f"], "asdfasdfasdf".split("d")); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 42 | |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 43 | assertArrayEquals(["as","fas","fas","f"], "asdfasdfasdf".split("d", -1)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 44 | |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 45 | assertArrayEquals(["as", "fas"], "asdfasdfasdf".split("d", 2)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 46 | |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 47 | assertArrayEquals([], "asdfasdfasdf".split("d", 0)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 48 | |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 49 | assertArrayEquals(["as","fas","fas",""], "asdfasdfasd".split("d")); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 50 | |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 51 | assertArrayEquals([], "".split("")); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 52 | |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 53 | assertArrayEquals([""], "".split("a")); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 54 | |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 55 | assertArrayEquals(["a","b"], "axxb".split(/x*/)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 56 | |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 57 | assertArrayEquals(["a","b"], "axxb".split(/x+/)); |
| 58 | |
| 59 | assertArrayEquals(["a","","b"], "axxb".split(/x/)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 60 | |
| 61 | // This was http://b/issue?id=1151354 |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 62 | assertArrayEquals(["div", "#id", ".class"], "div#id.class".split(/(?=[#.])/)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 63 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 64 | |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 65 | assertArrayEquals(["div", "#i", "d", ".class"], "div#id.class".split(/(?=[d#.])/)); |
| 66 | |
| 67 | assertArrayEquals(["a", "b", "c"], "abc".split(/(?=.)/)); |
| 68 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 69 | |
| 70 | /* "ab".split(/((?=.))/) |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 71 | * |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 72 | * KJS: ,a,,b |
| 73 | * SM: a,,b, |
| 74 | * IE: a,b |
| 75 | * Opera: a,,b |
| 76 | * V8: a,,b |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 77 | * |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 78 | * Opera seems to have this right. The others make no sense. |
| 79 | */ |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 80 | assertArrayEquals(["a", "", "b"], "ab".split(/((?=.))/)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 81 | |
| 82 | /* "ab".split(/(?=)/) |
| 83 | * |
| 84 | * KJS: a,b |
| 85 | * SM: ab |
| 86 | * IE: a,b |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 87 | * Opera: a,bb |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 88 | * V8: a,b |
| 89 | */ |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 90 | assertArrayEquals(["a", "b"], "ab".split(/(?=)/)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 91 | |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 92 | |
| 93 | // For issue http://code.google.com/p/v8/issues/detail?id=924 |
| 94 | // Splitting the empty string is a special case. |
| 95 | assertEquals([""], ''.split()); |
| 96 | assertEquals([""], ''.split(/./)); |
| 97 | assertEquals([], ''.split(/.?/)); |
| 98 | assertEquals([], ''.split(/.??/)); |
| 99 | assertEquals([], ''.split(/()()/)); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 100 | |
| 101 | |
| 102 | // Issue http://code.google.com/p/v8/issues/detail?id=929 |
| 103 | // (Splitting with empty separator and a limit.) |
| 104 | |
| 105 | function numberObj(num) { |
| 106 | return {valueOf: function() { return num; }}; |
| 107 | } |
| 108 | |
| 109 | assertEquals([], "abc".split("", 0)); |
| 110 | assertEquals([], "abc".split("", numberObj(0))); |
| 111 | assertEquals(["a"], "abc".split("", 1)); |
| 112 | assertEquals(["a"], "abc".split("", numberObj(1))); |
| 113 | assertEquals(["a", "b"], "abc".split("", 2)); |
| 114 | assertEquals(["a", "b"], "abc".split("", numberObj(2))); |
| 115 | assertEquals(["a", "b", "c"], "abc".split("", 3)); |
| 116 | assertEquals(["a", "b", "c"], "abc".split("", numberObj(3))); |
| 117 | assertEquals(["a", "b", "c"], "abc".split("", 4)); |
| 118 | assertEquals(["a", "b", "c"], "abc".split("", numberObj(4))); |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 119 | |
| 120 | |
| 121 | var all_ascii_codes = []; |
| 122 | for (var i = 0; i < 128; i++) all_ascii_codes[i] = i; |
| 123 | var all_ascii_string = String.fromCharCode.apply(String, all_ascii_codes); |
| 124 | |
| 125 | var split_chars = all_ascii_string.split(""); |
| 126 | assertEquals(128, split_chars.length); |
| 127 | for (var i = 0; i < 128; i++) { |
| 128 | assertEquals(1, split_chars[i].length); |
| 129 | assertEquals(i, split_chars[i].charCodeAt(0)); |
| 130 | } |