blob: 72dfa426b35d6e5bd72349462712dedac16005ff [file] [log] [blame]
Howard Hinnant237ee6f2010-06-30 17:22:19 +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
22int main()
23{
24 {
25 std::cmatch m;
26 assert(!std::regex_search("a", m, std::regex()));
27 assert(m.size() == 0);
28 assert(m.empty());
29 }
30 {
31 std::cmatch m;
32 const char s[] = "a";
33 assert(std::regex_search(s, m, std::regex("a", std::regex_constants::basic)));
34 assert(m.size() == 1);
35 assert(!m.empty());
36 assert(!m.prefix().matched);
37 assert(m.prefix().first == s);
38 assert(m.prefix().second == m[0].first);
39 assert(!m.suffix().matched);
40 assert(m.suffix().first == m[0].second);
41 assert(m.suffix().second == s+1);
42 assert(m.length(0) == 1);
43 assert(m.position(0) == 0);
44 assert(m.str(0) == "a");
45 }
46 {
47 std::cmatch m;
48 const char s[] = "ab";
49 assert(std::regex_search(s, m, std::regex("ab", std::regex_constants::basic)));
50 assert(m.size() == 1);
51 assert(!m.prefix().matched);
52 assert(m.prefix().first == s);
53 assert(m.prefix().second == m[0].first);
54 assert(!m.suffix().matched);
55 assert(m.suffix().first == m[0].second);
56 assert(m.suffix().second == s+2);
57 assert(m.length(0) == 2);
58 assert(m.position(0) == 0);
59 assert(m.str(0) == "ab");
60 }
61 {
62 std::cmatch m;
63 const char s[] = "ab";
64 assert(!std::regex_search(s, m, std::regex("ba", std::regex_constants::basic)));
65 assert(m.size() == 0);
66 assert(m.empty());
67 }
68 {
69 std::cmatch m;
70 const char s[] = "aab";
71 assert(std::regex_search(s, m, std::regex("ab", std::regex_constants::basic)));
72 assert(m.size() == 1);
73 assert(m.prefix().matched);
74 assert(m.prefix().first == s);
75 assert(m.prefix().second == m[0].first);
76 assert(!m.suffix().matched);
77 assert(m.suffix().first == m[0].second);
78 assert(m.suffix().second == s+3);
79 assert(m.length(0) == 2);
80 assert(m.position(0) == 1);
81 assert(m.str(0) == "ab");
82 }
Howard Hinnant928658c2010-06-30 20:30:19 +000083 {
84 std::cmatch m;
85 const char s[] = "aab";
86 assert(!std::regex_search(s, m, std::regex("ab", std::regex_constants::basic),
87 std::regex_constants::match_continuous));
88 assert(m.size() == 0);
89 }
90 {
91 std::cmatch m;
92 const char s[] = "abcd";
93 assert(std::regex_search(s, m, std::regex("bc", std::regex_constants::basic)));
94 assert(m.size() == 1);
95 assert(m.prefix().matched);
96 assert(m.prefix().first == s);
97 assert(m.prefix().second == m[0].first);
98 assert(m.suffix().matched);
99 assert(m.suffix().first == m[0].second);
100 assert(m.suffix().second == s+4);
101 assert(m.length(0) == 2);
102 assert(m.position(0) == 1);
103 assert(m.str(0) == "bc");
104 }
105 {
106 std::cmatch m;
Howard Hinnant189b2122010-07-07 19:14:52 +0000107 const char s[] = "abbc";
108 assert(std::regex_search(s, m, std::regex("ab*c", std::regex_constants::basic)));
109 assert(m.size() == 1);
110 assert(!m.prefix().matched);
Howard Hinnant928658c2010-06-30 20:30:19 +0000111 assert(m.prefix().first == s);
112 assert(m.prefix().second == m[0].first);
Howard Hinnant189b2122010-07-07 19:14:52 +0000113 assert(!m.suffix().matched);
Howard Hinnant928658c2010-06-30 20:30:19 +0000114 assert(m.suffix().first == m[0].second);
Howard Hinnant189b2122010-07-07 19:14:52 +0000115 assert(m.suffix().second == s+4);
116 assert(m.length(0) == 4);
117 assert(m.position(0) == 0);
118 assert(m.str(0) == s);
Howard Hinnant928658c2010-06-30 20:30:19 +0000119 }
Howard Hinnant8c459a12010-07-08 17:43:58 +0000120 {
121 std::cmatch m;
122 const char s[] = "ababc";
123 assert(std::regex_search(s, m, std::regex("\\(ab\\)*c", std::regex_constants::basic)));
124 assert(m.size() == 2);
125 assert(!m.prefix().matched);
126 assert(m.prefix().first == s);
127 assert(m.prefix().second == m[0].first);
128 assert(!m.suffix().matched);
129 assert(m.suffix().first == m[0].second);
130 assert(m.suffix().second == s+5);
131 assert(m.length(0) == 5);
132 assert(m.position(0) == 0);
133 assert(m.str(0) == s);
134 assert(m.length(1) == 2);
135 assert(m.position(1) == 2);
136 assert(m.str(1) == "ab");
137 }
138 {
139 std::cmatch m;
140 const char s[] = "abcdefghijk";
141 assert(std::regex_search(s, m, std::regex("cd\\(\\(e\\)fg\\)hi",
142 std::regex_constants::basic)));
143 assert(m.size() == 3);
144 assert(m.prefix().matched);
145 assert(m.prefix().first == s);
146 assert(m.prefix().second == m[0].first);
147 assert(m.suffix().matched);
148 assert(m.suffix().first == m[0].second);
149 assert(m.suffix().second == s+std::regex_traits<char>::length(s));
150 assert(m.length(0) == 7);
151 assert(m.position(0) == 2);
152 assert(m.str(0) == "cdefghi");
153 assert(m.length(1) == 3);
154 assert(m.position(1) == 4);
155 assert(m.str(1) == "efg");
156 assert(m.length(2) == 1);
157 assert(m.position(2) == 4);
158 assert(m.str(2) == "e");
159 }
Howard Hinnant87ec03a2010-07-09 00:15:26 +0000160 {
161 std::cmatch m;
162 const char s[] = "abc";
163 assert(std::regex_search(s, m, std::regex("^abc", std::regex_constants::basic)));
164 assert(m.size() == 1);
165 assert(!m.prefix().matched);
166 assert(m.prefix().first == s);
167 assert(m.prefix().second == m[0].first);
168 assert(!m.suffix().matched);
169 assert(m.suffix().first == m[0].second);
170 assert(m.suffix().second == s+3);
171 assert(m.length(0) == 3);
172 assert(m.position(0) == 0);
173 assert(m.str(0) == s);
174 }
175 {
176 std::cmatch m;
177 const char s[] = "abcd";
178 assert(std::regex_search(s, m, std::regex("^abc", std::regex_constants::basic)));
179 assert(m.size() == 1);
180 assert(!m.prefix().matched);
181 assert(m.prefix().first == s);
182 assert(m.prefix().second == m[0].first);
183 assert(m.suffix().matched);
184 assert(m.suffix().first == m[0].second);
185 assert(m.suffix().second == s+4);
186 assert(m.length(0) == 3);
187 assert(m.position(0) == 0);
188 assert(m.str(0) == "abc");
189 }
190 {
191 std::cmatch m;
192 const char s[] = "aabc";
193 assert(!std::regex_search(s, m, std::regex("^abc", std::regex_constants::basic)));
194 assert(m.size() == 0);
195 }
196 {
197 std::cmatch m;
198 const char s[] = "abc";
199 assert(std::regex_search(s, m, std::regex("abc$", std::regex_constants::basic)));
200 assert(m.size() == 1);
201 assert(!m.prefix().matched);
202 assert(m.prefix().first == s);
203 assert(m.prefix().second == m[0].first);
204 assert(!m.suffix().matched);
205 assert(m.suffix().first == m[0].second);
206 assert(m.suffix().second == s+3);
207 assert(m.length(0) == 3);
208 assert(m.position(0) == 0);
209 assert(m.str(0) == s);
210 }
211 {
212 std::cmatch m;
213 const char s[] = "efabc";
214 assert(std::regex_search(s, m, std::regex("abc$", std::regex_constants::basic)));
215 assert(m.size() == 1);
216 assert(m.prefix().matched);
217 assert(m.prefix().first == s);
218 assert(m.prefix().second == m[0].first);
219 assert(!m.suffix().matched);
220 assert(m.suffix().first == m[0].second);
221 assert(m.suffix().second == s+5);
222 assert(m.length(0) == 3);
223 assert(m.position(0) == 2);
224 assert(m.str(0) == s+2);
225 }
226 {
227 std::cmatch m;
228 const char s[] = "efabcg";
229 assert(!std::regex_search(s, m, std::regex("abc$", std::regex_constants::basic)));
230 assert(m.size() == 0);
231 }
Howard Hinnant0cbed7e2010-07-12 15:51:17 +0000232 {
233 std::cmatch m;
234 const char s[] = "abc";
235 assert(std::regex_search(s, m, std::regex("a.c", std::regex_constants::basic)));
236 assert(m.size() == 1);
237 assert(!m.prefix().matched);
238 assert(m.prefix().first == s);
239 assert(m.prefix().second == m[0].first);
240 assert(!m.suffix().matched);
241 assert(m.suffix().first == m[0].second);
242 assert(m.suffix().second == s+3);
243 assert(m.length(0) == 3);
244 assert(m.position(0) == 0);
245 assert(m.str(0) == s);
246 }
247 {
248 std::cmatch m;
249 const char s[] = "acc";
250 assert(std::regex_search(s, m, std::regex("a.c", std::regex_constants::basic)));
251 assert(m.size() == 1);
252 assert(!m.prefix().matched);
253 assert(m.prefix().first == s);
254 assert(m.prefix().second == m[0].first);
255 assert(!m.suffix().matched);
256 assert(m.suffix().first == m[0].second);
257 assert(m.suffix().second == s+3);
258 assert(m.length(0) == 3);
259 assert(m.position(0) == 0);
260 assert(m.str(0) == s);
261 }
262 {
263 std::cmatch m;
264 const char s[] = "acc";
265 assert(std::regex_search(s, m, std::regex("a.c", std::regex_constants::basic)));
266 assert(m.size() == 1);
267 assert(!m.prefix().matched);
268 assert(m.prefix().first == s);
269 assert(m.prefix().second == m[0].first);
270 assert(!m.suffix().matched);
271 assert(m.suffix().first == m[0].second);
272 assert(m.suffix().second == s+3);
273 assert(m.length(0) == 3);
274 assert(m.position(0) == 0);
275 assert(m.str(0) == s);
276 }
277 {
278 std::cmatch m;
279 const char s[] = "abcdef";
280 assert(std::regex_search(s, m, std::regex("\\(.*\\).*", std::regex_constants::basic)));
281 assert(m.size() == 2);
282 assert(!m.prefix().matched);
283 assert(m.prefix().first == s);
284 assert(m.prefix().second == m[0].first);
285 assert(!m.suffix().matched);
286 assert(m.suffix().first == m[0].second);
287 assert(m.suffix().second == s+6);
288 assert(m.length(0) == 6);
289 assert(m.position(0) == 0);
290 assert(m.str(0) == s);
291 assert(m.length(1) == 6);
292 assert(m.position(1) == 0);
293 assert(m.str(1) == s);
294 }
295 {
296 std::cmatch m;
297 const char s[] = "bc";
298 assert(std::regex_search(s, m, std::regex("\\(a*\\)*", std::regex_constants::basic)));
299 assert(m.size() == 2);
300 assert(!m.prefix().matched);
301 assert(m.prefix().first == s);
302 assert(m.prefix().second == m[0].first);
303 assert(m.suffix().matched);
304 assert(m.suffix().first == m[0].second);
305 assert(m.suffix().second == s+2);
306 assert(m.length(0) == 0);
307 assert(m.position(0) == 0);
308 assert(m.str(0) == "");
309 assert(m.length(1) == 0);
310 assert(m.position(1) == 0);
311 assert(m.str(1) == "");
312 }
Howard Hinnantaea2afe2010-07-12 18:16:05 +0000313 {
314 std::cmatch m;
315 const char s[] = "abbc";
316 assert(!std::regex_search(s, m, std::regex("ab\\{3,5\\}c", std::regex_constants::basic)));
317 assert(m.size() == 0);
318 }
319 {
320 std::cmatch m;
321 const char s[] = "abbbc";
322 assert(std::regex_search(s, m, std::regex("ab\\{3,5\\}c", std::regex_constants::basic)));
323 assert(m.size() == 1);
324 assert(!m.prefix().matched);
325 assert(m.prefix().first == s);
326 assert(m.prefix().second == m[0].first);
327 assert(!m.suffix().matched);
328 assert(m.suffix().first == m[0].second);
329 assert(m.suffix().second == m[0].second);
330 assert(m.length(0) == sizeof(s)-1);
331 assert(m.position(0) == 0);
332 assert(m.str(0) == s);
333 }
334 {
335 std::cmatch m;
336 const char s[] = "abbbbc";
337 assert(std::regex_search(s, m, std::regex("ab\\{3,5\\}c", std::regex_constants::basic)));
338 assert(m.size() == 1);
339 assert(!m.prefix().matched);
340 assert(m.prefix().first == s);
341 assert(m.prefix().second == m[0].first);
342 assert(!m.suffix().matched);
343 assert(m.suffix().first == m[0].second);
344 assert(m.suffix().second == m[0].second);
345 assert(m.length(0) == sizeof(s)-1);
346 assert(m.position(0) == 0);
347 assert(m.str(0) == s);
348 }
349 {
350 std::cmatch m;
351 const char s[] = "abbbbbc";
352 assert(std::regex_search(s, m, std::regex("ab\\{3,5\\}c", std::regex_constants::basic)));
353 assert(m.size() == 1);
354 assert(!m.prefix().matched);
355 assert(m.prefix().first == s);
356 assert(m.prefix().second == m[0].first);
357 assert(!m.suffix().matched);
358 assert(m.suffix().first == m[0].second);
359 assert(m.suffix().second == m[0].second);
360 assert(m.length(0) == sizeof(s)-1);
361 assert(m.position(0) == 0);
362 assert(m.str(0) == s);
363 }
364 {
365 std::cmatch m;
366 const char s[] = "adefc";
367 assert(!std::regex_search(s, m, std::regex("ab\\{3,5\\}c", std::regex_constants::basic)));
368 assert(m.size() == 0);
369 }
370 {
371 std::cmatch m;
372 const char s[] = "abbbbbbc";
373 assert(!std::regex_search(s, m, std::regex("ab\\{3,5\\}c", std::regex_constants::basic)));
374 assert(m.size() == 0);
375 }
376 {
377 std::cmatch m;
378 const char s[] = "adec";
379 assert(!std::regex_search(s, m, std::regex("a.\\{3,5\\}c", std::regex_constants::basic)));
380 assert(m.size() == 0);
381 }
382 {
383 std::cmatch m;
384 const char s[] = "adefc";
385 assert(std::regex_search(s, m, std::regex("a.\\{3,5\\}c", std::regex_constants::basic)));
386 assert(m.size() == 1);
387 assert(!m.prefix().matched);
388 assert(m.prefix().first == s);
389 assert(m.prefix().second == m[0].first);
390 assert(!m.suffix().matched);
391 assert(m.suffix().first == m[0].second);
392 assert(m.suffix().second == m[0].second);
393 assert(m.length(0) == sizeof(s)-1);
394 assert(m.position(0) == 0);
395 assert(m.str(0) == s);
396 }
397 {
398 std::cmatch m;
399 const char s[] = "adefgc";
400 assert(std::regex_search(s, m, std::regex("a.\\{3,5\\}c", std::regex_constants::basic)));
401 assert(m.size() == 1);
402 assert(!m.prefix().matched);
403 assert(m.prefix().first == s);
404 assert(m.prefix().second == m[0].first);
405 assert(!m.suffix().matched);
406 assert(m.suffix().first == m[0].second);
407 assert(m.suffix().second == m[0].second);
408 assert(m.length(0) == sizeof(s)-1);
409 assert(m.position(0) == 0);
410 assert(m.str(0) == s);
411 }
412 {
413 std::cmatch m;
414 const char s[] = "adefghc";
415 assert(std::regex_search(s, m, std::regex("a.\\{3,5\\}c", std::regex_constants::basic)));
416 assert(m.size() == 1);
417 assert(!m.prefix().matched);
418 assert(m.prefix().first == s);
419 assert(m.prefix().second == m[0].first);
420 assert(!m.suffix().matched);
421 assert(m.suffix().first == m[0].second);
422 assert(m.suffix().second == m[0].second);
423 assert(m.length(0) == sizeof(s)-1);
424 assert(m.position(0) == 0);
425 assert(m.str(0) == s);
426 }
427 {
428 std::cmatch m;
429 const char s[] = "adefghic";
430 assert(!std::regex_search(s, m, std::regex("a.\\{3,5\\}c", std::regex_constants::basic)));
431 assert(m.size() == 0);
432 }
433 {
434 std::cmatch m;
435 const char s[] = "-ab,ab-";
436 assert(std::regex_search(s, m, std::regex("-\\(.*\\),\\1-", std::regex_constants::basic)));
437 assert(m.size() == 2);
438 assert(!m.prefix().matched);
439 assert(m.prefix().first == s);
440 assert(m.prefix().second == m[0].first);
441 assert(!m.suffix().matched);
442 assert(m.suffix().first == m[0].second);
443 assert(m.suffix().second == m[0].second);
444 assert(m.length(0) == std::char_traits<char>::length(s));
445 assert(m.position(0) == 0);
446 assert(m.str(0) == s);
447 assert(m.length(1) == 2);
448 assert(m.position(1) == 1);
449 assert(m.str(1) == "ab");
450 }
451 {
452 std::cmatch m;
453 const char s[] = "ababbabb";
454 assert(std::regex_search(s, m, std::regex("^\\(ab*\\)*\\1$", std::regex_constants::basic)));
455 assert(m.size() == 2);
456 assert(!m.prefix().matched);
457 assert(m.prefix().first == s);
458 assert(m.prefix().second == m[0].first);
459 assert(!m.suffix().matched);
460 assert(m.suffix().first == m[0].second);
461 assert(m.suffix().second == m[0].second);
462 assert(m.length(0) == std::char_traits<char>::length(s));
463 assert(m.position(0) == 0);
464 assert(m.str(0) == s);
465 assert(m.length(1) == 3);
466 assert(m.position(1) == 2);
467 assert(m.str(1) == "abb");
468 }
469 {
470 std::cmatch m;
471 const char s[] = "ababbab";
472 assert(!std::regex_search(s, m, std::regex("^\\(ab*\\)*\\1$", std::regex_constants::basic)));
473 assert(m.size() == 0);
474 }
Howard Hinnant237ee6f2010-06-30 17:22:19 +0000475}