1342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch// Copyright 2016 the V8 project authors. All rights reserved.
2342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch// Use of this source code is governed by a BSD-style license that can be
3342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch// found in the LICENSE file.
4342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch
521efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch// Flags: --harmony-regexp-lookbehind
6342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch
7342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochfunction execl(expectation, regexp, subject) {
8342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch  if (regexp instanceof String) regexp = new RegExp(regexp, "u");
9342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch  assertEquals(expectation, regexp.exec(subject));
10342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch}
11342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch
12342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochfunction execs(expectation, regexp_source, subject) {
13342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch  execl(expectation, new RegExp(regexp_source, "u"), subject);
14342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch}
15342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch
16342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch// Character ranges.
17342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecl(["A"], /[A-D]/u, "A");
18342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecs(["A"], "[A-D]", "A");
19342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecl(["ABCD"], /[A-D]+/u, "ZABCDEF");
20342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecs(["ABCD"], "[A-D]+", "ZABCDEF");
21342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch
22342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecl(["\u{12345}"], /[\u1234-\u{12345}]/u, "\u{12345}");
23342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecs(["\u{12345}"], "[\u1234-\u{12345}]", "\u{12345}");
24342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecl(null, /[^\u1234-\u{12345}]/u, "\u{12345}");
25342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecs(null, "[^\u1234-\u{12345}]", "\u{12345}");
26342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch
27342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecl(["\u{1234}"], /[\u1234-\u{12345}]/u, "\u{1234}");
28342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecs(["\u{1234}"], "[\u1234-\u{12345}]", "\u{1234}");
29342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecl(null, /[^\u1234-\u{12345}]/u, "\u{1234}");
30342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecs(null, "[^\u1234-\u{12345}]", "\u{1234}");
31342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch
32342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecl(null, /[\u1234-\u{12345}]/u, "\u{1233}");
33342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecs(null, "[\u1234-\u{12345}]", "\u{1233}");
34342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecl(["\u{1233}"], /[^\u1234-\u{12345}]/u, "\u{1233}");
35342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecs(["\u{1233}"], "[^\u1234-\u{12345}]", "\u{1233}");
36342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch
37342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecl(["\u{12346}"], /[^\u1234-\u{12345}]/u, "\u{12346}");
38342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecs(["\u{12346}"], "[^\u1234-\u{12345}]", "\u{12346}");
39342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecl(null, /[\u1234-\u{12345}]/u, "\u{12346}");
40342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecs(null, "[\u1234-\u{12345}]", "\u{12346}");
41342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch
42342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecl(["\u{12342}"], /[\u{12340}-\u{12345}]/u, "\u{12342}");
43342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecs(["\u{12342}"], "[\u{12340}-\u{12345}]", "\u{12342}");
44342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecl(["\u{12342}"], /[\ud808\udf40-\ud808\udf45]/u, "\u{12342}");
45342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecs(["\u{12342}"], "[\ud808\udf40-\ud808\udf45]", "\u{12342}");
46342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecl(null, /[^\u{12340}-\u{12345}]/u, "\u{12342}");
47342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecs(null, "[^\u{12340}-\u{12345}]", "\u{12342}");
48342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecl(null, /[^\ud808\udf40-\ud808\udf45]/u, "\u{12342}");
49342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecs(null, "[^\ud808\udf40-\ud808\udf45]", "\u{12342}");
50342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch
51342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecl(["\u{ffff}"], /[\u{ff80}-\u{12345}]/u, "\u{ffff}");
52342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecs(["\u{ffff}"], "[\u{ff80}-\u{12345}]", "\u{ffff}");
53342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecl(["\u{ffff}"], /[\u{ff80}-\ud808\udf45]/u, "\u{ffff}");
54342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecs(["\u{ffff}"], "[\u{ff80}-\ud808\udf45]", "\u{ffff}");
55342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecl(null, /[^\u{ff80}-\u{12345}]/u, "\u{ffff}");
56342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecs(null, "[^\u{ff80}-\u{12345}]", "\u{ffff}");
57342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecl(null, /[^\u{ff80}-\ud808\udf45]/u, "\u{ffff}");
58342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecs(null, "[^\u{ff80}-\ud808\udf45]", "\u{ffff}");
59342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch
60342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch// Lone surrogate
61342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecl(["\ud800"], /[^\u{ff80}-\u{12345}]/u, "\uff99\u{d800}A");
62342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecs(["\udc00"], "[^\u{ff80}-\u{12345}]", "\uff99\u{dc00}A");
63342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecl(["\udc01"], /[\u0100-\u{10ffff}]/u, "A\udc01");
64342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecl(["\udc03"], /[\udc01-\udc03]/u, "\ud801\udc02\udc03");
65342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecl(["\ud801"], /[\ud801-\ud803]/u, "\ud802\udc01\ud801");
66342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch
67342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch// Paired sorrogate.
68342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecl(null, /[^\u{ff80}-\u{12345}]/u, "\u{d800}\u{dc00}");
69342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecs(null, "[^\u{ff80}-\u{12345}]", "\u{d800}\u{dc00}");
70342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecl(["\ud800\udc00"], /[\u{ff80}-\u{12345}]/u, "\u{d800}\u{dc00}");
71342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecs(["\ud800\udc00"], "[\u{ff80}-\u{12345}]", "\u{d800}\u{dc00}");
72342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecl(["foo\u{10e6d}bar"], /foo\ud803\ude6dbar/u, "foo\u{10e6d}bar");
73342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch
74342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch// Lone surrogates
75342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecl(["\ud801\ud801"], /\ud801+/u, "\ud801\udc01\ud801\ud801");
76342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecl(["\udc01\udc01"], /\udc01+/u, "\ud801\ud801\udc01\udc01\udc01");
77342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch
78342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecl(["\udc02\udc03A"], /\W\WA/u, "\ud801\udc01A\udc02\udc03A");
79342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecl(["\ud801\ud802"], /\ud801./u, "\ud801\udc01\ud801\ud802");
80342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecl(["\udc02\udc03A"], /[\ud800-\udfff][\ud800-\udfff]A/u,
81342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch      "\ud801\udc01A\udc02\udc03A");
82342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch
83342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch// Character classes
84342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecl(null, /\w/u, "\ud801\udc01");
85342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecl(["\ud801"], /[^\w]/, "\ud801\udc01");
86342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecl(["\ud801\udc01"], /[^\w]/u, "\ud801\udc01");
87342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecl(["\ud801"], /\W/, "\ud801\udc01");
88342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecl(["\ud801\udc01"], /\W/u, "\ud801\udc01");
89342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch
90342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecl(["\ud800X"], /.X/u, "\ud800XaX");
91342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecl(["aX"], /.(?<!\ud800)X/u, "\ud800XaX");
92342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecl(["aX"], /.(?<![\ud800-\ud900])X/u, "\ud800XaX");
93342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch
94342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecl(null, /[]/u, "\u1234");
95342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecl(["0abc"], /[^]abc/u, "0abc");
96342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecl(["\u1234abc"], /[^]abc/u, "\u1234abc");
97342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecl(["\u{12345}abc"], /[^]abc/u, "\u{12345}abc");
98342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch
99342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecl(null, /[\u{0}-\u{1F444}]/u, "\ud83d\udfff");
100342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch
101342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch// Backward matches of lone surrogates.
102342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecl(["B", "\ud803A"], /(?<=([\ud800-\ud900]A))B/u,
103342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch      "\ud801\udc00AB\udc00AB\ud802\ud803AB");
104342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecl(["B", "\udc00A"], /(?<=([\ud800-\u{10300}]A))B/u,
105342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch       "\ud801\udc00AB\udc00AB\ud802\ud803AB");
106342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecl(["B", "\udc11A"], /(?<=([\udc00-\udd00]A))B/u,
107342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch      "\ud801\udc00AB\udc11AB\ud802\ud803AB");
108342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecl(["X", "\ud800C"], /(?<=(\ud800\w))X/u,
109342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch      "\ud800\udc00AX\udc11BX\ud800\ud800CX");
110342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecl(["C", "\ud800\ud800"], /(?<=(\ud800.))\w/u,
111342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch      "\ud800\udc00AX\udc11BX\ud800\ud800CX");
112342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecl(["X", "\udc01C"], /(?<=(\udc01\w))X/u,
113342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch      "\ud800\udc01AX\udc11BX\udc01\udc01CX");
114342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochexecl(["C", "\udc01\udc01"], /(?<=(\udc01.))./u,
115342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch      "\ud800\udc01AX\udc11BX\udc01\udc01CX");
116342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch
117342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochvar L = "\ud800";
118342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochvar T = "\udc00";
119342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochvar X = "X";
120342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch
121342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch// Test string contains only match.
122342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochfunction testw(expect, src, subject) {
123342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch  var re = new RegExp("^" + src + "$", "u");
124342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch  assertEquals(expect, re.test(subject));
125342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch}
126342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch
127342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch// Test string starts with match.
128342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochfunction tests(expect, src, subject) {
129342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch  var re = new RegExp("^" + src, "u");
130342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch  assertEquals(expect, re.test(subject));
131342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch}
132342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch
133342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochtestw(true, X, X);
134342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochtestw(true, L, L);
135342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochtestw(true, T, T);
136342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochtestw(true, L + T, L + T);
137342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochtestw(true, T + L, T + L);
138342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochtestw(false, T, L + T);
139342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochtestw(false, L, L + T);
140342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochtestw(true, ".(?<=" + L + ")", L);
141342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochtestw(true, ".(?<=" + T + ")", T);
142342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochtestw(true, ".(?<=" + L + T + ")", L + T);
143342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochtestw(true, ".(?<=" + L + T + ")", L + T);
144342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochtests(true, ".(?<=" + T + ")", T + L);
145342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochtests(false, ".(?<=" + L + ")", L + T);
146342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochtests(false, ".(?<=" + T + ")", L + T);
147342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochtests(true, "..(?<=" + T + ")", T + T + L);
148342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochtests(true, "..(?<=" + T + ")", X + T + L);
149342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochtests(true, "...(?<=" + L + ")", X + T + L);
150342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochtests(false, "...(?<=" + T + ")", X + L + T)
151342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochtests(true, "..(?<=" + L + T + ")", X + L + T)
152342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochtests(true, "..(?<=" + L + T + "(?<=" + L + T + "))", X + L + T);
153342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochtests(false, "..(?<=" + L + "(" + T + "))", X + L + T);
154342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochtests(false, ".*" + L, X + L + T);
155342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochtests(true, ".*" + L, X + L + L + T);
156342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochtests(false, ".*" + L, X + L + T + L + T);
157342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochtests(false, ".*" + T, X + L + T + L + T);
158342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochtests(true, ".*" + T, X + L + T + T + L + T);
159