string-slices-regexp.js revision 69a99ed0b2b2ef69d393c371b03db3a98aaf880e
1// Copyright 2009 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// Flags: --string-slices
29
30//assertEquals('345"12345 6"1234567"123',
31//             '12345""12345 6""1234567""1234'.slice(2,-1).replace(/""/g, '"'));
32
33var foo = "lsdfj sldkfj sdklfj læsdfjl sdkfjlsdk fjsdl fjsdljskdj flsj flsdkj flskd regexp: /foobar/\nldkfj sdlkfj sdkl";
34for(var i = 0; i < 1000; i++) {
35  assertTrue(/^([a-z]+): (.*)/.test(foo.substring(foo.indexOf("regexp:"))));
36  assertEquals("regexp", RegExp.$1, "RegExp.$1");
37}
38
39var re = /^(((N({)?)|(R)|(U)|(V)|(B)|(H)|(n((n)|(r)|(v)|(h))?)|(r(r)?)|(v)|(b((n)|(b))?)|(h))|((Y)|(A)|(E)|(o(u)?)|(p(u)?)|(q(u)?)|(s)|(t)|(u)|(w)|(x(u)?)|(y)|(z)|(a((T)|(A)|(L))?)|(c)|(e)|(f(u)?)|(g(u)?)|(i)|(j)|(l)|(m(u)?)))+/;
40var r = new RegExp(re)
41var str = "_Avtnennan gunzvmu pubExnY nEvln vaTxh rmuhguhaTxnY_".slice(1,-1);
42str = str + str;
43assertTrue(r.test(str));
44assertTrue(r.test(str));
45var re = /x/;
46assertEquals("a.yb", "_axyb_".slice(1,-1).replace(re, "."));
47re.compile("y");
48assertEquals("ax.b", "_axyb_".slice(1,-1).replace(re, "."));
49re.compile("(x)");
50assertEquals(["x", "x"], re.exec("_axyb_".slice(1,-1)));
51re.compile("(y)");
52assertEquals(["y", "y"], re.exec("_axyb_".slice(1,-1)));
53
54for(var i = 0; i < 100; i++) {
55  var a = "aaaaaaaaaaaaaaaaaaaaaaaabbaacabbabaaaaabbaaaabbac".slice(24,-1);
56  var b = "bbaacabbabaaaaabbaaaabba" + a;
57  // The first time, the cons string will be flattened and handled by the
58  // runtime system.
59  assertEquals(["bbaa", "a", "", "a"], /((\3|b)\2(a)){2,}/.exec(b));
60  // The second time, the cons string is already flattened and will be
61  // handled by generated code.
62  assertEquals(["bbaa", "a", "", "a"], /((\3|b)\2(a)){2,}/.exec(b));
63  assertEquals(["bbaa", "a", "", "a"], /((\3|b)\2(a)){2,}/.exec(a));
64  assertEquals(["bbaa", "a", "", "a"], /((\3|b)\2(a)){2,}/.exec(a));
65}
66
67var c = "ABCDEFGHIJKLMN".slice(2,-2);
68var d = "ABCDEF\u1234GHIJKLMN".slice(2,-2);
69var e = "ABCDEFGHIJKLMN".slice(0,-2);
70assertTrue(/^C.*L$/.test(c));
71assertTrue(/^C.*L$/.test(c));
72assertTrue(/^C.*L$/.test(d));
73assertTrue(/^C.*L$/.test(d));
74assertTrue(/^A\w{10}L$/.test(e));
75assertTrue(/^A\w{10}L$/.test(e));
76
77var e = "qui-opIasd-fghjklzx-cvbn-mqwer-tyuio-pasdf-ghIjkl-zx".slice(6,-6);
78var e_split = e.split("-");
79assertEquals(e_split[0], "Iasd");
80assertEquals(e_split[1], "fghjklzx");
81assertEquals(e_split[6], "ghI");
82