blob: 4829b711bf26a1b6c036a66a4880b13e6bbbe506 [file] [log] [blame]
Howard Hinnantc1198c32010-07-16 19:08:36 +00001//===----------------------------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10// <regex>
11
12// template <class BidirectionalIterator, class Allocator, class charT, class traits>
13// bool
14// regex_search(BidirectionalIterator first, BidirectionalIterator last,
15// match_results<BidirectionalIterator, Allocator>& m,
16// const basic_regex<charT, traits>& e,
17// regex_constants::match_flag_type flags = regex_constants::match_default);
18
19#include <regex>
20#include <cassert>
21
22#include "../../iterators.h"
23
24int main()
25{
26 {
27 std::cmatch m;
28 const char s[] = "a";
29 assert(std::regex_search(s, m, std::regex("a", std::regex_constants::extended)));
30 assert(m.size() == 1);
31 assert(!m.empty());
32 assert(!m.prefix().matched);
33 assert(m.prefix().first == s);
34 assert(m.prefix().second == m[0].first);
35 assert(!m.suffix().matched);
36 assert(m.suffix().first == m[0].second);
37 assert(m.suffix().second == s+1);
38 assert(m.length(0) == 1);
39 assert(m.position(0) == 0);
40 assert(m.str(0) == "a");
41 }
42 {
43 std::cmatch m;
44 const char s[] = "ab";
45 assert(std::regex_search(s, m, std::regex("ab", std::regex_constants::extended)));
46 assert(m.size() == 1);
47 assert(!m.prefix().matched);
48 assert(m.prefix().first == s);
49 assert(m.prefix().second == m[0].first);
50 assert(!m.suffix().matched);
51 assert(m.suffix().first == m[0].second);
52 assert(m.suffix().second == s+2);
53 assert(m.length(0) == 2);
54 assert(m.position(0) == 0);
55 assert(m.str(0) == "ab");
56 }
57 {
58 std::cmatch m;
59 const char s[] = "ab";
60 assert(!std::regex_search(s, m, std::regex("ba", std::regex_constants::extended)));
61 assert(m.size() == 0);
62 assert(m.empty());
63 }
64 {
65 std::cmatch m;
66 const char s[] = "aab";
67 assert(std::regex_search(s, m, std::regex("ab", std::regex_constants::extended)));
68 assert(m.size() == 1);
69 assert(m.prefix().matched);
70 assert(m.prefix().first == s);
71 assert(m.prefix().second == m[0].first);
72 assert(!m.suffix().matched);
73 assert(m.suffix().first == m[0].second);
74 assert(m.suffix().second == s+3);
75 assert(m.length(0) == 2);
76 assert(m.position(0) == 1);
77 assert(m.str(0) == "ab");
78 }
79 {
80 std::cmatch m;
81 const char s[] = "aab";
82 assert(!std::regex_search(s, m, std::regex("ab", std::regex_constants::extended),
83 std::regex_constants::match_continuous));
84 assert(m.size() == 0);
85 }
86 {
87 std::cmatch m;
88 const char s[] = "abcd";
89 assert(std::regex_search(s, m, std::regex("bc", std::regex_constants::extended)));
90 assert(m.size() == 1);
91 assert(m.prefix().matched);
92 assert(m.prefix().first == s);
93 assert(m.prefix().second == m[0].first);
94 assert(m.suffix().matched);
95 assert(m.suffix().first == m[0].second);
96 assert(m.suffix().second == s+4);
97 assert(m.length(0) == 2);
98 assert(m.position(0) == 1);
99 assert(m.str(0) == "bc");
100 }
101 {
102 std::cmatch m;
103 const char s[] = "abbc";
104 assert(std::regex_search(s, m, std::regex("ab*c", std::regex_constants::extended)));
105 assert(m.size() == 1);
106 assert(!m.prefix().matched);
107 assert(m.prefix().first == s);
108 assert(m.prefix().second == m[0].first);
109 assert(!m.suffix().matched);
110 assert(m.suffix().first == m[0].second);
111 assert(m.suffix().second == s+4);
112 assert(m.length(0) == 4);
113 assert(m.position(0) == 0);
114 assert(m.str(0) == s);
115 }
116 {
117 std::cmatch m;
118 const char s[] = "ababc";
119 assert(std::regex_search(s, m, std::regex("(ab)*c", std::regex_constants::extended)));
120 assert(m.size() == 2);
121 assert(!m.prefix().matched);
122 assert(m.prefix().first == s);
123 assert(m.prefix().second == m[0].first);
124 assert(!m.suffix().matched);
125 assert(m.suffix().first == m[0].second);
126 assert(m.suffix().second == s+5);
127 assert(m.length(0) == 5);
128 assert(m.position(0) == 0);
129 assert(m.str(0) == s);
130 assert(m.length(1) == 2);
131 assert(m.position(1) == 2);
132 assert(m.str(1) == "ab");
133 }
134 {
135 std::cmatch m;
136 const char s[] = "abcdefghijk";
137 assert(std::regex_search(s, m, std::regex("cd((e)fg)hi",
138 std::regex_constants::extended)));
139 assert(m.size() == 3);
140 assert(m.prefix().matched);
141 assert(m.prefix().first == s);
142 assert(m.prefix().second == m[0].first);
143 assert(m.suffix().matched);
144 assert(m.suffix().first == m[0].second);
145 assert(m.suffix().second == s+std::regex_traits<char>::length(s));
146 assert(m.length(0) == 7);
147 assert(m.position(0) == 2);
148 assert(m.str(0) == "cdefghi");
149 assert(m.length(1) == 3);
150 assert(m.position(1) == 4);
151 assert(m.str(1) == "efg");
152 assert(m.length(2) == 1);
153 assert(m.position(2) == 4);
154 assert(m.str(2) == "e");
155 }
156 {
157 std::cmatch m;
158 const char s[] = "abc";
159 assert(std::regex_search(s, m, std::regex("^abc", std::regex_constants::extended)));
160 assert(m.size() == 1);
161 assert(!m.prefix().matched);
162 assert(m.prefix().first == s);
163 assert(m.prefix().second == m[0].first);
164 assert(!m.suffix().matched);
165 assert(m.suffix().first == m[0].second);
166 assert(m.suffix().second == s+3);
167 assert(m.length(0) == 3);
168 assert(m.position(0) == 0);
169 assert(m.str(0) == s);
170 }
171 {
172 std::cmatch m;
173 const char s[] = "abcd";
174 assert(std::regex_search(s, m, std::regex("^abc", std::regex_constants::extended)));
175 assert(m.size() == 1);
176 assert(!m.prefix().matched);
177 assert(m.prefix().first == s);
178 assert(m.prefix().second == m[0].first);
179 assert(m.suffix().matched);
180 assert(m.suffix().first == m[0].second);
181 assert(m.suffix().second == s+4);
182 assert(m.length(0) == 3);
183 assert(m.position(0) == 0);
184 assert(m.str(0) == "abc");
185 }
186 {
187 std::cmatch m;
188 const char s[] = "aabc";
189 assert(!std::regex_search(s, m, std::regex("^abc", std::regex_constants::extended)));
190 assert(m.size() == 0);
191 }
192 {
193 std::cmatch m;
194 const char s[] = "abc";
195 assert(std::regex_search(s, m, std::regex("abc$", std::regex_constants::extended)));
196 assert(m.size() == 1);
197 assert(!m.prefix().matched);
198 assert(m.prefix().first == s);
199 assert(m.prefix().second == m[0].first);
200 assert(!m.suffix().matched);
201 assert(m.suffix().first == m[0].second);
202 assert(m.suffix().second == s+3);
203 assert(m.length(0) == 3);
204 assert(m.position(0) == 0);
205 assert(m.str(0) == s);
206 }
207 {
208 std::cmatch m;
209 const char s[] = "efabc";
210 assert(std::regex_search(s, m, std::regex("abc$", std::regex_constants::extended)));
211 assert(m.size() == 1);
212 assert(m.prefix().matched);
213 assert(m.prefix().first == s);
214 assert(m.prefix().second == m[0].first);
215 assert(!m.suffix().matched);
216 assert(m.suffix().first == m[0].second);
217 assert(m.suffix().second == s+5);
218 assert(m.length(0) == 3);
219 assert(m.position(0) == 2);
220 assert(m.str(0) == s+2);
221 }
222 {
223 std::cmatch m;
224 const char s[] = "efabcg";
225 assert(!std::regex_search(s, m, std::regex("abc$", std::regex_constants::extended)));
226 assert(m.size() == 0);
227 }
228 {
229 std::cmatch m;
230 const char s[] = "abc";
231 assert(std::regex_search(s, m, std::regex("a.c", std::regex_constants::extended)));
232 assert(m.size() == 1);
233 assert(!m.prefix().matched);
234 assert(m.prefix().first == s);
235 assert(m.prefix().second == m[0].first);
236 assert(!m.suffix().matched);
237 assert(m.suffix().first == m[0].second);
238 assert(m.suffix().second == s+3);
239 assert(m.length(0) == 3);
240 assert(m.position(0) == 0);
241 assert(m.str(0) == s);
242 }
243 {
244 std::cmatch m;
245 const char s[] = "acc";
246 assert(std::regex_search(s, m, std::regex("a.c", std::regex_constants::extended)));
247 assert(m.size() == 1);
248 assert(!m.prefix().matched);
249 assert(m.prefix().first == s);
250 assert(m.prefix().second == m[0].first);
251 assert(!m.suffix().matched);
252 assert(m.suffix().first == m[0].second);
253 assert(m.suffix().second == s+3);
254 assert(m.length(0) == 3);
255 assert(m.position(0) == 0);
256 assert(m.str(0) == s);
257 }
258 {
259 std::cmatch m;
260 const char s[] = "acc";
261 assert(std::regex_search(s, m, std::regex("a.c", std::regex_constants::extended)));
262 assert(m.size() == 1);
263 assert(!m.prefix().matched);
264 assert(m.prefix().first == s);
265 assert(m.prefix().second == m[0].first);
266 assert(!m.suffix().matched);
267 assert(m.suffix().first == m[0].second);
268 assert(m.suffix().second == s+3);
269 assert(m.length(0) == 3);
270 assert(m.position(0) == 0);
271 assert(m.str(0) == s);
272 }
273 {
274 std::cmatch m;
275 const char s[] = "abcdef";
276 assert(std::regex_search(s, m, std::regex("(.*).*", std::regex_constants::extended)));
277 assert(m.size() == 2);
278 assert(!m.prefix().matched);
279 assert(m.prefix().first == s);
280 assert(m.prefix().second == m[0].first);
281 assert(!m.suffix().matched);
282 assert(m.suffix().first == m[0].second);
283 assert(m.suffix().second == s+6);
284 assert(m.length(0) == 6);
285 assert(m.position(0) == 0);
286 assert(m.str(0) == s);
287 assert(m.length(1) == 6);
288 assert(m.position(1) == 0);
289 assert(m.str(1) == s);
290 }
291 {
292 std::cmatch m;
293 const char s[] = "bc";
294 assert(std::regex_search(s, m, std::regex("(a*)*", std::regex_constants::extended)));
295 assert(m.size() == 2);
296 assert(!m.prefix().matched);
297 assert(m.prefix().first == s);
298 assert(m.prefix().second == m[0].first);
299 assert(m.suffix().matched);
300 assert(m.suffix().first == m[0].second);
301 assert(m.suffix().second == s+2);
302 assert(m.length(0) == 0);
303 assert(m.position(0) == 0);
304 assert(m.str(0) == "");
305 assert(m.length(1) == 0);
306 assert(m.position(1) == 0);
307 assert(m.str(1) == "");
308 }
309 {
310 std::cmatch m;
311 const char s[] = "abbc";
312 assert(!std::regex_search(s, m, std::regex("ab{3,5}c", std::regex_constants::extended)));
313 assert(m.size() == 0);
314 }
315 {
316 std::cmatch m;
317 const char s[] = "abbbc";
318 assert(std::regex_search(s, m, std::regex("ab{3,5}c", std::regex_constants::extended)));
319 assert(m.size() == 1);
320 assert(!m.prefix().matched);
321 assert(m.prefix().first == s);
322 assert(m.prefix().second == m[0].first);
323 assert(!m.suffix().matched);
324 assert(m.suffix().first == m[0].second);
325 assert(m.suffix().second == m[0].second);
Howard Hinnantb762bea2010-07-22 14:12:20 +0000326 assert(m.length(0) == std::char_traits<char>::length(s));
Howard Hinnantc1198c32010-07-16 19:08:36 +0000327 assert(m.position(0) == 0);
328 assert(m.str(0) == s);
329 }
330 {
331 std::cmatch m;
332 const char s[] = "abbbbc";
333 assert(std::regex_search(s, m, std::regex("ab{3,5}c", std::regex_constants::extended)));
334 assert(m.size() == 1);
335 assert(!m.prefix().matched);
336 assert(m.prefix().first == s);
337 assert(m.prefix().second == m[0].first);
338 assert(!m.suffix().matched);
339 assert(m.suffix().first == m[0].second);
340 assert(m.suffix().second == m[0].second);
Howard Hinnantb762bea2010-07-22 14:12:20 +0000341 assert(m.length(0) == std::char_traits<char>::length(s));
Howard Hinnantc1198c32010-07-16 19:08:36 +0000342 assert(m.position(0) == 0);
343 assert(m.str(0) == s);
344 }
345 {
346 std::cmatch m;
347 const char s[] = "abbbbbc";
348 assert(std::regex_search(s, m, std::regex("ab{3,5}c", std::regex_constants::extended)));
349 assert(m.size() == 1);
350 assert(!m.prefix().matched);
351 assert(m.prefix().first == s);
352 assert(m.prefix().second == m[0].first);
353 assert(!m.suffix().matched);
354 assert(m.suffix().first == m[0].second);
355 assert(m.suffix().second == m[0].second);
Howard Hinnantb762bea2010-07-22 14:12:20 +0000356 assert(m.length(0) == std::char_traits<char>::length(s));
Howard Hinnantc1198c32010-07-16 19:08:36 +0000357 assert(m.position(0) == 0);
358 assert(m.str(0) == s);
359 }
360 {
361 std::cmatch m;
362 const char s[] = "adefc";
363 assert(!std::regex_search(s, m, std::regex("ab{3,5}c", std::regex_constants::extended)));
364 assert(m.size() == 0);
365 }
366 {
367 std::cmatch m;
368 const char s[] = "abbbbbbc";
369 assert(!std::regex_search(s, m, std::regex("ab{3,5}c", std::regex_constants::extended)));
370 assert(m.size() == 0);
371 }
372 {
373 std::cmatch m;
374 const char s[] = "adec";
375 assert(!std::regex_search(s, m, std::regex("a.{3,5}c", std::regex_constants::extended)));
376 assert(m.size() == 0);
377 }
378 {
379 std::cmatch m;
380 const char s[] = "adefc";
381 assert(std::regex_search(s, m, std::regex("a.{3,5}c", std::regex_constants::extended)));
382 assert(m.size() == 1);
383 assert(!m.prefix().matched);
384 assert(m.prefix().first == s);
385 assert(m.prefix().second == m[0].first);
386 assert(!m.suffix().matched);
387 assert(m.suffix().first == m[0].second);
388 assert(m.suffix().second == m[0].second);
Howard Hinnantb762bea2010-07-22 14:12:20 +0000389 assert(m.length(0) == std::char_traits<char>::length(s));
Howard Hinnantc1198c32010-07-16 19:08:36 +0000390 assert(m.position(0) == 0);
391 assert(m.str(0) == s);
392 }
393 {
394 std::cmatch m;
395 const char s[] = "adefgc";
396 assert(std::regex_search(s, m, std::regex("a.{3,5}c", std::regex_constants::extended)));
397 assert(m.size() == 1);
398 assert(!m.prefix().matched);
399 assert(m.prefix().first == s);
400 assert(m.prefix().second == m[0].first);
401 assert(!m.suffix().matched);
402 assert(m.suffix().first == m[0].second);
403 assert(m.suffix().second == m[0].second);
Howard Hinnantb762bea2010-07-22 14:12:20 +0000404 assert(m.length(0) == std::char_traits<char>::length(s));
Howard Hinnantc1198c32010-07-16 19:08:36 +0000405 assert(m.position(0) == 0);
406 assert(m.str(0) == s);
407 }
408 {
409 std::cmatch m;
410 const char s[] = "adefghc";
411 assert(std::regex_search(s, m, std::regex("a.{3,5}c", std::regex_constants::extended)));
412 assert(m.size() == 1);
413 assert(!m.prefix().matched);
414 assert(m.prefix().first == s);
415 assert(m.prefix().second == m[0].first);
416 assert(!m.suffix().matched);
417 assert(m.suffix().first == m[0].second);
418 assert(m.suffix().second == m[0].second);
Howard Hinnantb762bea2010-07-22 14:12:20 +0000419 assert(m.length(0) == std::char_traits<char>::length(s));
Howard Hinnantc1198c32010-07-16 19:08:36 +0000420 assert(m.position(0) == 0);
421 assert(m.str(0) == s);
422 }
423 {
424 std::cmatch m;
425 const char s[] = "adefghic";
426 assert(!std::regex_search(s, m, std::regex("a.{3,5}c", std::regex_constants::extended)));
427 assert(m.size() == 0);
428 }
429 {
430 std::cmatch m;
431 const char s[] = "tournament";
432 assert(std::regex_search(s, m, std::regex("tour|to|tournament",
433 std::regex_constants::extended)));
434 assert(m.size() == 1);
435 assert(!m.prefix().matched);
436 assert(m.prefix().first == s);
437 assert(m.prefix().second == m[0].first);
438 assert(!m.suffix().matched);
439 assert(m.suffix().first == m[0].second);
440 assert(m.suffix().second == m[0].second);
Howard Hinnantb762bea2010-07-22 14:12:20 +0000441 assert(m.length(0) == std::char_traits<char>::length(s));
Howard Hinnantc1198c32010-07-16 19:08:36 +0000442 assert(m.position(0) == 0);
443 assert(m.str(0) == s);
444 }
Howard Hinnantb762bea2010-07-22 14:12:20 +0000445 {
446 std::cmatch m;
447 const char s[] = "tournamenttotour";
448 assert(std::regex_search(s, m, std::regex("(tour|to|tournament)+",
449 std::regex_constants::extended | std::regex_constants::nosubs)));
450 assert(m.size() == 1);
451 assert(!m.prefix().matched);
452 assert(m.prefix().first == s);
453 assert(m.prefix().second == m[0].first);
454 assert(!m.suffix().matched);
455 assert(m.suffix().first == m[0].second);
456 assert(m.suffix().second == m[0].second);
457 assert(m.length(0) == std::char_traits<char>::length(s));
458 assert(m.position(0) == 0);
459 assert(m.str(0) == s);
460 }
461 {
462 std::cmatch m;
463 const char s[] = "ttotour";
464 assert(std::regex_search(s, m, std::regex("(tour|to|t)+",
465 std::regex_constants::extended)));
466 assert(m.size() == 2);
467 assert(!m.prefix().matched);
468 assert(m.prefix().first == s);
469 assert(m.prefix().second == m[0].first);
470 assert(!m.suffix().matched);
471 assert(m.suffix().first == m[0].second);
472 assert(m.suffix().second == m[0].second);
473 assert(m.length(0) == std::char_traits<char>::length(s));
474 assert(m.position(0) == 0);
475 assert(m.str(0) == s);
476 assert(m.length(1) == 4);
477 assert(m.position(1) == 3);
478 assert(m.str(1) == "tour");
479 }
Howard Hinnantc1198c32010-07-16 19:08:36 +0000480}