summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/v8/test/mjsunit/regexp-capture-3.js
blob: 9bdd6005995172b2f2caf92d5a1de385e7505c5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Copyright 2012 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

function oneMatch(re) {
  "abcd".replace(re, function() { });
  assertEquals("abcd", RegExp.input);
  assertEquals("a", RegExp.leftContext);
  assertEquals("b", RegExp.lastMatch);
  assertEquals("", RegExp.lastParen);
  assertEquals(undefined, RegExp.lastIndex);
  assertEquals(undefined, RegExp.index);
  assertEquals("cd", RegExp.rightContext);
  for (var i = 1; i < 10; i++) {
    assertEquals("", RegExp['$' + i]);
  }
}

oneMatch(/b/);
oneMatch(/b/g);

"abcdabcd".replace(/b/g, function() { });
assertEquals("abcdabcd", RegExp.input);
assertEquals("abcda", RegExp.leftContext);
assertEquals("b", RegExp.lastMatch);
assertEquals("", RegExp.lastParen);
assertEquals(undefined, RegExp.lastIndex);
assertEquals(undefined, RegExp.index);
assertEquals("cd", RegExp.rightContext);
for (var i = 1; i < 10; i++) {
  assertEquals("", RegExp['$' + i]);
}

function captureMatch(re) {
  "abcd".replace(re, function() { });
  assertEquals("abcd", RegExp.input);
  assertEquals("a", RegExp.leftContext);
  assertEquals("bc", RegExp.lastMatch);
  assertEquals("c", RegExp.lastParen);
  assertEquals(undefined, RegExp.lastIndex);
  assertEquals(undefined, RegExp.index);
  assertEquals("d", RegExp.rightContext);
  assertEquals('b', RegExp.$1);
  assertEquals('c', RegExp.$2);
  for (var i = 3; i < 10; i++) {
    assertEquals("", RegExp['$' + i]);
  }
}

captureMatch(/(b)(c)/);
captureMatch(/(b)(c)/g);

"abcdabcd".replace(/(b)(c)/g, function() { });
assertEquals("abcdabcd", RegExp.input);
assertEquals("abcda", RegExp.leftContext);
assertEquals("bc", RegExp.lastMatch);
assertEquals("c", RegExp.lastParen);
assertEquals(undefined, RegExp.lastIndex);
assertEquals(undefined, RegExp.index);
assertEquals("d", RegExp.rightContext);
assertEquals('b', RegExp.$1);
assertEquals('c', RegExp.$2);
for (var i = 3; i < 10; i++) {
  assertEquals("", RegExp['$' + i]);
}


function Override() {
  // Set the internal lastMatchInfoOverride.  After calling this we do a normal
  // match and verify the override was cleared and that we record the new
  // captures.
  "abcdabcd".replace(/(b)(c)/g, function() { });
}


function TestOverride(input, expect, property, re_src) {
  var re = new RegExp(re_src);
  var re_g = new RegExp(re_src, "g");

  function OverrideCase(fn) {
    Override();
    fn();
    assertEquals(expect, RegExp[property]);
  }

  OverrideCase(function() { return input.replace(re, "x"); });
  OverrideCase(function() { return input.replace(re_g, "x"); });
  OverrideCase(function() { return input.replace(re, ""); });
  OverrideCase(function() { return input.replace(re_g, ""); });
  OverrideCase(function() { return input.match(re); });
  OverrideCase(function() { return input.match(re_g); });
  OverrideCase(function() { return re.test(input); });
  OverrideCase(function() { return re_g.test(input); });
}

var input = "bar.foo baz......";
var re_str = "(ba.).*?f";
TestOverride(input, "bar", "$1", re_str);

input = "foo bar baz";
var re_str = "bar";
TestOverride(input, "bar", "$&", re_str);


function no_last_match(fn) {
  fn();
  assertEquals("hestfisk", RegExp.$1);
}

/(hestfisk)/.test("There's no such thing as a hestfisk!");

no_last_match(function() { "foo".replace("f", ""); });
no_last_match(function() { "foo".replace("f", "f"); });
no_last_match(function() { "foo".split("o"); });

var base = "In the music.  In the music.  ";
var cons = base + base + base + base;
no_last_match(function() { cons.replace("x", "y"); });
no_last_match(function() { cons.replace("e", "E"); });


// Here's one that matches once, then tries to match again, but fails.
// Verify that the last match info is from the last match, not from the
// failure that came after.
"bar.foo baz......".replace(/(ba.).*?f/g, function() { return "x";});
assertEquals("bar", RegExp.$1);


// A test that initially does a zero width match, but later does a non-zero
// width match.
var a = "foo bar baz".replace(/^|bar/g, "");
assertEquals("foo  baz", a);

a = "foo bar baz".replace(/^|bar/g, "*");
assertEquals("*foo * baz", a);

// We test FilterASCII using regexps that will backtrack forever.  Since
// a regexp with a non-ASCII character in it can never match an ASCII
// string we can test that the relevant node is removed by verifying that
// there is no hang.
function NoHang(re) {
  print(re);
  "This is an ASCII string that could take forever".match(re);
}


NoHang(/(((.*)*)*x)å/);  // Continuation after loop is filtered, so is loop.
NoHang(/(((.*)*)*å)foo/);  // Body of loop filtered.
NoHang(/å(((.*)*)*x)/);   // Everything after a filtered character is filtered.
NoHang(/(((.*)*)*x)å/);   // Everything before a filtered character is filtered.
NoHang(/[æøå](((.*)*)*x)/);   // Everything after a filtered class is filtered.
NoHang(/(((.*)*)*x)[æøå]/);   // Everything before a filtered class is filtered.
NoHang(/[^\x00-\x7f](((.*)*)*x)/);   // After negated class.
NoHang(/(((.*)*)*x)[^\x00-\x7f]/);   // Before negated class.
NoHang(/(?!(((.*)*)*x)å)foo/);  // Negative lookahead is filtered.
NoHang(/(?!(((.*)*)*x))å/);  // Continuation branch of negative lookahead.
NoHang(/(?=(((.*)*)*x)å)foo/);  // Positive lookahead is filtered.
NoHang(/(?=(((.*)*)*x))å/);  // Continuation branch of positive lookahead.
NoHang(/(?=å)(((.*)*)*x)/);  // Positive lookahead also prunes continuation.
NoHang(/(æ|ø|å)(((.*)*)*x)/);  // All branches of alternation are filtered.
NoHang(/(a|b|(((.*)*)*x))å/);  // 1 out of 3 branches pruned.
NoHang(/(a|(((.*)*)*x)ø|(((.*)*)*x)å)/);  // 2 out of 3 branches pruned.

var s = "Don't prune based on a repetition of length 0";
assertEquals(null, s.match(/å{1,1}prune/));
assertEquals("prune", (s.match(/å{0,0}prune/)[0]));

// Some very deep regexps where FilterASCII gives up in order not to make the
// stack overflow.
var regex6 = /a*\u0100*\w/;
var input0 = "a";
regex6.exec(input0);

var re = "\u0100*\\w";

for (var i = 0; i < 200; i++) re = "a*" + re;

var regex7 = new RegExp(re);
regex7.exec(input0);

var regex8 = new RegExp(re, "i");
regex8.exec(input0);

re = "[\u0100]*\\w";
for (var i = 0; i < 200; i++) re = "a*" + re;

var regex9 = new RegExp(re);
regex9.exec(input0);

var regex10 = new RegExp(re, "i");
regex10.exec(input0);

var regex11 = /^(?:[^\u0000-\u0080]|[0-9a-z?,.!&\s#()])+$/i;
regex11.exec(input0);