blob: c9f509794dbd31fdca3a96f8076d97c665a6e64f [file] [log] [blame]
Howard Hinnant3257c982010-06-17 00:34:59 +00001// -*- C++ -*-
2//===--------------------------- regex ------------------------------------===//
3//
4// The LLVM Compiler Infrastructure
5//
6// This file is distributed under the University of Illinois Open Source
7// License. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_REGEX
12#define _LIBCPP_REGEX
13
14/*
15 regex synopsis
16
17#include <initializer_list>
18
19namespace std
20{
21
22namespace regex_constants
23{
24
25emum syntax_option_type
26{
27 icase = unspecified,
28 nosubs = unspecified,
29 optimize = unspecified,
30 collate = unspecified,
31 ECMAScript = unspecified,
32 basic = unspecified,
33 extended = unspecified,
34 awk = unspecified,
35 grep = unspecified,
36 egrep = unspecified
37};
38
39constexpr syntax_option_type operator~(syntax_option_type f);
40constexpr syntax_option_type operator&(syntax_option_type lhs, syntax_option_type rhs);
41constexpr syntax_option_type operator|(syntax_option_type lhs, syntax_option_type rhs);
42
43enum match_flag_type
44{
45 match_default = 0,
46 match_not_bol = unspecified,
47 match_not_eol = unspecified,
48 match_not_bow = unspecified,
49 match_not_eow = unspecified,
50 match_any = unspecified,
51 match_not_null = unspecified,
52 match_continuous = unspecified,
53 match_prev_avail = unspecified,
54 format_default = 0,
55 format_sed = unspecified,
56 format_no_copy = unspecified,
57 format_first_only = unspecified
58};
59
60constexpr match_flag_type operator~(match_flag_type f);
61constexpr match_flag_type operator&(match_flag_type lhs, match_flag_type rhs);
62constexpr match_flag_type operator|(match_flag_type lhs, match_flag_type rhs);
63
64enum error_type
65{
66 error_collate = unspecified,
67 error_ctype = unspecified,
68 error_escape = unspecified,
69 error_backref = unspecified,
70 error_brack = unspecified,
71 error_paren = unspecified,
72 error_brace = unspecified,
73 error_badbrace = unspecified,
74 error_range = unspecified,
75 error_space = unspecified,
76 error_badrepeat = unspecified,
77 error_complexity = unspecified,
78 error_stack = unspecified
79};
80
81} // regex_constants
82
83class regex_error
84 : public runtime_error
85{
86public:
87 explicit regex_error(regex_constants::error_type ecode);
88 regex_constants::error_type code() const;
89};
90
91template <class charT>
92struct regex_traits
93{
94public:
95 typedef charT char_type;
96 typedef basic_string<char_type> string_type;
97 typedef locale locale_type;
98 typedef /bitmask_type/ char_class_type;
99
100 regex_traits();
101
102 static size_t length(const char_type* p);
103 charT translate(charT c) const;
104 charT translate_nocase(charT c) const;
105 template <class ForwardIterator>
106 string_type
107 transform(ForwardIterator first, ForwardIterator last) const;
108 template <class ForwardIterator>
109 string_type
110 transform_primary( ForwardIterator first, ForwardIterator last) const;
111 template <class ForwardIterator>
112 string_type
113 lookup_collatename(ForwardIterator first, ForwardIterator last) const;
114 template <class ForwardIterator>
115 char_class_type
116 lookup_classname(ForwardIterator first, ForwardIterator last,
117 bool icase = false) const;
118 bool isctype(charT c, char_class_type f) const;
119 int value(charT ch, int radix) const;
120 locale_type imbue(locale_type l);
121 locale_type getloc()const;
122};
123
124template <class charT, class traits = regex_traits<charT>>
125class basic_regex
126{
127public:
128 // types:
129 typedef charT value_type;
130 typedef regex_constants::syntax_option_type flag_type;
131 typedef typename traits::locale_type locale_type;
132
133 // constants:
134 static constexpr regex_constants::syntax_option_type icase = regex_constants::icase;
135 static constexpr regex_constants::syntax_option_type nosubs = regex_constants::nosubs;
136 static constexpr regex_constants::syntax_option_type optimize = regex_constants::optimize;
137 static constexpr regex_constants::syntax_option_type collate = regex_constants::collate;
138 static constexpr regex_constants::syntax_option_type ECMAScript = regex_constants::ECMAScript;
139 static constexpr regex_constants::syntax_option_type basic = regex_constants::basic;
140 static constexpr regex_constants::syntax_option_type extended = regex_constants::extended;
141 static constexpr regex_constants::syntax_option_type awk = regex_constants::awk;
142 static constexpr regex_constants::syntax_option_type grep = regex_constants::grep;
143 static constexpr regex_constants::syntax_option_type egrep = regex_constants::egrep;
144
145 // construct/copy/destroy:
146 basic_regex();
147 explicit basic_regex(const charT* p, flag_type f = regex_constants::ECMAScript);
148 basic_regex(const charT* p, size_t len, flag_type f);
149 basic_regex(const basic_regex&);
150 basic_regex(basic_regex&&);
151 template <class ST, class SA>
152 explicit basic_regex(const basic_string<charT, ST, SA>& p,
153 flag_type f = regex_constants::ECMAScript);
154 template <class ForwardIterator>
155 basic_regex(ForwardIterator first, ForwardIterator last,
156 flag_type f = regex_constants::ECMAScript);
157 basic_regex(initializer_list<charT>, flag_type = regex_constants::ECMAScript);
158
159 ~basic_regex();
160
161 basic_regex& operator=(const basic_regex&);
162 basic_regex& operator=(basic_regex&&);
163 basic_regex& operator=(const charT* ptr);
164 basic_regex& operator=(initializer_list<charT> il);
165 template <class ST, class SA>
166 basic_regex& operator=(const basic_string<charT, ST, SA>& p);
167
168 // assign:
169 basic_regex& assign(const basic_regex& that);
170 basic_regex& assign(basic_regex&& that);
171 basic_regex& assign(const charT* ptr, flag_type f = regex_constants::ECMAScript);
172 basic_regex& assign(const charT* p, size_t len, flag_type f);
173 template <class string_traits, class A>
174 basic_regex& assign(const basic_string<charT, string_traits, A>& s,
175 flag_type f = regex_constants::ECMAScript);
176 template <class InputIterator>
177 basic_regex& assign(InputIterator first, InputIterator last,
178 flag_type f = regex_constants::ECMAScript);
179 basic_regex& assign(initializer_list<charT>, flag_type = regex_constants::ECMAScript);
180
181 // const operations:
182 unsigned mark_count() const;
183 flag_type flags() const;
184
185 // locale:
186 locale_type imbue(locale_type loc);
187 locale_type getloc() const;
188
189 // swap:
190 void swap(basic_regex&);
191};
192
193typedef basic_regex<char> regex;
194typedef basic_regex<wchar_t> wregex;
195
196template <class charT, class traits>
197 void swap(basic_regex<charT, traits>& e1, basic_regex<charT, traits>& e2);
198
199template <class BidirectionalIterator>
200class sub_match
201 : public pair<BidirectionalIterator, BidirectionalIterator>
202{
203public:
204 typedef typename iterator_traits<BidirectionalIterator>::value_type value_type;
205 typedef typename iterator_traits<BidirectionalIterator>::difference_type difference_type;
206 typedef BidirectionalIterator iterator;
207 typedef basic_string<value_type> string_type;
208
209 bool matched;
210
211 difference_type length() const;
212 operator string_type() const;
213 string_type str() const;
214
215 int compare(const sub_match& s) const;
216 int compare(const string_type& s) const;
217 int compare(const value_type* s) const;
218};
219
220typedef sub_match<const char*> csub_match;
221typedef sub_match<const wchar_t*> wcsub_match;
222typedef sub_match<string::const_iterator> ssub_match;
223typedef sub_match<wstring::const_iterator> wssub_match;
224
225template <class BiIter>
226 bool
227 operator==(const sub_match<BiIter>& lhs, const sub_match<BiIter>& rhs);
228
229template <class BiIter>
230 bool
231 operator!=(const sub_match<BiIter>& lhs, const sub_match<BiIter>& rhs);
232
233template <class BiIter>
234 bool
235 operator<(const sub_match<BiIter>& lhs, const sub_match<BiIter>& rhs);
236
237template <class BiIter>
238 bool
239 operator<=(const sub_match<BiIter>& lhs, const sub_match<BiIter>& rhs);
240
241template <class BiIter>
242 bool
243 operator>=(const sub_match<BiIter>& lhs, const sub_match<BiIter>& rhs);
244
245template <class BiIter>
246 bool
247 operator>(const sub_match<BiIter>& lhs, const sub_match<BiIter>& rhs);
248
249template <class BiIter, class ST, class SA>
250 bool
251 operator==(const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& lhs,
252 const sub_match<BiIter>& rhs);
253
254template <class BiIter, class ST, class SA>
255 bool
256 operator!=(const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& lhs,
257 const sub_match<BiIter>& rhs);
258
259template <class BiIter, class ST, class SA>
260 bool
261 operator<(const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& lhs,
262 const sub_match<BiIter>& rhs);
263
264template <class BiIter, class ST, class SA>
265 bool
266 operator>(const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& lhs,
267 const sub_match<BiIter>& rhs);
268
269template <class BiIter, class ST, class SA>
270 bool operator>=(const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& lhs,
271 const sub_match<BiIter>& rhs);
272
273template <class BiIter, class ST, class SA>
274 bool
275 operator<=(const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& lhs,
276 const sub_match<BiIter>& rhs);
277
278template <class BiIter, class ST, class SA>
279 bool
280 operator==(const sub_match<BiIter>& lhs,
281 const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& rhs);
282
283template <class BiIter, class ST, class SA>
284 bool
285 operator!=(const sub_match<BiIter>& lhs,
286 const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& rhs);
287
288template <class BiIter, class ST, class SA>
289 bool
290 operator<(const sub_match<BiIter>& lhs,
291 const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& rhs);
292
293template <class BiIter, class ST, class SA>
294 bool operator>(const sub_match<BiIter>& lhs,
295 const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& rhs);
296
297template <class BiIter, class ST, class SA>
298 bool
299 operator>=(const sub_match<BiIter>& lhs,
300 const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& rhs);
301
302template <class BiIter, class ST, class SA>
303 bool
304 operator<=(const sub_match<BiIter>& lhs,
305 const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& rhs);
306
307template <class BiIter>
308 bool
309 operator==(typename iterator_traits<BiIter>::value_type const* lhs,
310 const sub_match<BiIter>& rhs);
311
312template <class BiIter>
313 bool
314 operator!=(typename iterator_traits<BiIter>::value_type const* lhs,
315 const sub_match<BiIter>& rhs);
316
317template <class BiIter>
318 bool
319 operator<(typename iterator_traits<BiIter>::value_type const* lhs,
320 const sub_match<BiIter>& rhs);
321
322template <class BiIter>
323 bool
324 operator>(typename iterator_traits<BiIter>::value_type const* lhs,
325 const sub_match<BiIter>& rhs);
326
327template <class BiIter>
328 bool
329 operator>=(typename iterator_traits<BiIter>::value_type const* lhs,
330 const sub_match<BiIter>& rhs);
331
332template <class BiIter>
333 bool
334 operator<=(typename iterator_traits<BiIter>::value_type const* lhs,
335 const sub_match<BiIter>& rhs);
336
337template <class BiIter>
338 bool
339 operator==(const sub_match<BiIter>& lhs,
340 typename iterator_traits<BiIter>::value_type const* rhs);
341
342template <class BiIter>
343 bool
344 operator!=(const sub_match<BiIter>& lhs,
345 typename iterator_traits<BiIter>::value_type const* rhs);
346
347template <class BiIter>
348 bool
349 operator<(const sub_match<BiIter>& lhs,
350 typename iterator_traits<BiIter>::value_type const* rhs);
351
352template <class BiIter>
353 bool
354 operator>(const sub_match<BiIter>& lhs,
355 typename iterator_traits<BiIter>::value_type const* rhs);
356
357template <class BiIter>
358 bool
359 operator>=(const sub_match<BiIter>& lhs,
360 typename iterator_traits<BiIter>::value_type const* rhs);
361
362template <class BiIter>
363 bool
364 operator<=(const sub_match<BiIter>& lhs,
365 typename iterator_traits<BiIter>::value_type const* rhs);
366
367template <class BiIter>
368 bool
369 operator==(typename iterator_traits<BiIter>::value_type const& lhs,
370 const sub_match<BiIter>& rhs);
371
372template <class BiIter>
373 bool
374 operator!=(typename iterator_traits<BiIter>::value_type const& lhs,
375 const sub_match<BiIter>& rhs);
376
377template <class BiIter>
378 bool
379 operator<(typename iterator_traits<BiIter>::value_type const& lhs,
380 const sub_match<BiIter>& rhs);
381
382template <class BiIter>
383 bool
384 operator>(typename iterator_traits<BiIter>::value_type const& lhs,
385 const sub_match<BiIter>& rhs);
386
387template <class BiIter>
388 bool
389 operator>=(typename iterator_traits<BiIter>::value_type const& lhs,
390 const sub_match<BiIter>& rhs);
391
392template <class BiIter>
393 bool
394 operator<=(typename iterator_traits<BiIter>::value_type const& lhs,
395 const sub_match<BiIter>& rhs);
396
397template <class BiIter>
398 bool
399 operator==(const sub_match<BiIter>& lhs,
400 typename iterator_traits<BiIter>::value_type const& rhs);
401
402template <class BiIter>
403 bool
404 operator!=(const sub_match<BiIter>& lhs,
405 typename iterator_traits<BiIter>::value_type const& rhs);
406
407template <class BiIter>
408 bool
409 operator<(const sub_match<BiIter>& lhs,
410 typename iterator_traits<BiIter>::value_type const& rhs);
411
412template <class BiIter>
413 bool
414 operator>(const sub_match<BiIter>& lhs,
415 typename iterator_traits<BiIter>::value_type const& rhs);
416
417template <class BiIter>
418 bool
419 operator>=(const sub_match<BiIter>& lhs,
420 typename iterator_traits<BiIter>::value_type const& rhs);
421
422template <class BiIter>
423 bool
424 operator<=(const sub_match<BiIter>& lhs,
425 typename iterator_traits<BiIter>::value_type const& rhs);
426
427template <class charT, class ST, class BiIter>
428 basic_ostream<charT, ST>&
429 operator<<(basic_ostream<charT, ST>& os, const sub_match<BiIter>& m);
430
431template <class BidirectionalIterator,
432 class Allocator = allocator<sub_match<BidirectionalIterator>>>
433class match_results
434{
435public:
436 typedef sub_match<BidirectionalIterator> value_type;
437 typedef const value_type& const_reference;
438 typedef const_reference reference;
439 typedef /implementation-defined/ const_iterator;
440 typedef const_iterator iterator;
441 typedef typename iterator_traits<BidirectionalIterator>::difference_type difference_type;
442 typedef typename allocator_traits<Allocator>::size_type size_type;
443 typedef Allocator allocator_type;
444 typedef typename iterator_traits<BidirectionalIterator>::value_type char_type;
445 typedef basic_string<char_type> string_type;
446
447 // construct/copy/destroy:
448 explicit match_results(const Allocator& a = Allocator());
449 match_results(const match_results& m);
450 match_results(match_results&& m);
451 match_results& operator=(const match_results& m);
452 match_results& operator=(match_results&& m);
453 ~match_results();
454
455 // size:
456 size_type size() const;
457 size_type max_size() const;
458 bool empty() const;
459
460 // element access:
461 difference_type length(size_type sub = 0) const;
462 difference_type position(size_type sub = 0) const;
463 string_type str(size_type sub = 0) const;
464 const_reference operator[](size_type n) const;
465
466 const_reference prefix() const;
467 const_reference suffix() const;
468
469 const_iterator begin() const;
470 const_iterator end() const;
471 const_iterator cbegin() const;
472 const_iterator cend() const;
473
474 // format:
475 template <class OutputIter>
476 OutputIter
477 format(OutputIter out, const char_type* fmt_first,
478 const char_type* fmt_last,
479 regex_constants::match_flag_type flags = regex_constants::format_default) const;
480 template <class OutputIter, class ST, class SA>
481 OutputIter
482 format(OutputIter out, const basic_string<char_type, ST, SA>& fmt,
483 regex_constants::match_flag_type flags = regex_constants::format_default) const;
484 template <class ST, class SA>
485 basic_string<char_type, ST, SA>
486 format(const basic_string<char_type, ST, SA>& fmt,
487 regex_constants::match_flag_type flags = regex_constants::format_default) const;
488 string_type
489 format(const char_type* fmt,
490 regex_constants::match_flag_type flags = regex_constants::format_default) const;
491
492 // allocator:
493 allocator_type get_allocator() const;
494
495 // swap:
496 void swap(match_results& that);
497};
498
499typedef match_results<const char*> cmatch;
500typedef match_results<const wchar_t*> wcmatch;
501typedef match_results<string::const_iterator> smatch;
502typedef match_results<wstring::const_iterator> wsmatch;
503
504template <class BidirectionalIterator, class Allocator>
505 bool
506 operator==(const match_results<BidirectionalIterator, Allocator>& m1,
507 const match_results<BidirectionalIterator, Allocator>& m2);
508
509template <class BidirectionalIterator, class Allocator>
510 bool
511 operator!=(const match_results<BidirectionalIterator, Allocator>& m1,
512 const match_results<BidirectionalIterator, Allocator>& m2);
513
514template <class BidirectionalIterator, class Allocator>
515 void
516 swap(match_results<BidirectionalIterator, Allocator>& m1,
517 match_results<BidirectionalIterator, Allocator>& m2);
518
519template <class BidirectionalIterator, class Allocator, class charT, class traits>
520 bool
521 regex_match(BidirectionalIterator first, BidirectionalIterator last,
522 match_results<BidirectionalIterator, Allocator>& m,
523 const basic_regex<charT, traits>& e,
524 regex_constants::match_flag_type flags = regex_constants::match_default);
525
526template <class BidirectionalIterator, class charT, class traits>
527 bool
528 regex_match(BidirectionalIterator first, BidirectionalIterator last,
529 const basic_regex<charT, traits>& e,
530 regex_constants::match_flag_type flags = regex_constants::match_default);
531
532template <class charT, class Allocator, class traits>
533 bool
534 regex_match(const charT* str, match_results<const charT*, Allocator>& m,
535 const basic_regex<charT, traits>& e,
536 regex_constants::match_flag_type flags = regex_constants::match_default);
537
538template <class ST, class SA, class Allocator, class charT, class traits>
539 bool
540 regex_match(const basic_string<charT, ST, SA>& s,
541 match_results<typename basic_string<charT, ST, SA>::const_iterator, Allocator>& m,
542 const basic_regex<charT, traits>& e,
543 regex_constants::match_flag_type flags = regex_constants::match_default);
544
545template <class charT, class traits>
546 bool
547 regex_match(const charT* str, const basic_regex<charT, traits>& e,
548 regex_constants::match_flag_type flags = regex_constants::match_default);
549
550template <class ST, class SA, class charT, class traits>
551 bool
552 regex_match(const basic_string<charT, ST, SA>& s,
553 const basic_regex<charT, traits>& e,
554 regex_constants::match_flag_type flags = regex_constants::match_default);
555
556template <class BidirectionalIterator, class Allocator, class charT, class traits>
557 bool
558 regex_search(BidirectionalIterator first, BidirectionalIterator last,
559 match_results<BidirectionalIterator, Allocator>& m,
560 const basic_regex<charT, traits>& e,
561 regex_constants::match_flag_type flags = regex_constants::match_default);
562
563template <class BidirectionalIterator, class charT, class traits>
564 bool
565 regex_search(BidirectionalIterator first, BidirectionalIterator last,
566 const basic_regex<charT, traits>& e,
567 regex_constants::match_flag_type flags = regex_constants::match_default);
568
569template <class charT, class Allocator, class traits>
570 bool
571 regex_search(const charT* str, match_results<const charT*, Allocator>& m,
572 const basic_regex<charT, traits>& e,
573 regex_constants::match_flag_type flags = regex_constants::match_default);
574
575template <class charT, class traits>
576 bool
577 regex_search(const charT* str, const basic_regex<charT, traits>& e,
578 regex_constants::match_flag_type flags = regex_constants::match_default);
579
580template <class ST, class SA, class charT, class traits>
581 bool
582 regex_search(const basic_string<charT, ST, SA>& s,
583 const basic_regex<charT, traits>& e,
584 regex_constants::match_flag_type flags = regex_constants::match_default);
585
586template <class ST, class SA, class Allocator, class charT, class traits>
587 bool
588 regex_search(const basic_string<charT, ST, SA>& s,
589 match_results<typename basic_string<charT, ST, SA>::const_iterator, Allocator>& m,
590 const basic_regex<charT, traits>& e,
591 regex_constants::match_flag_type flags = regex_constants::match_default);
592
593template <class OutputIterator, class BidirectionalIterator,
594 class traits, class charT, class ST, class SA>
595 OutputIterator
596 regex_replace(OutputIterator out,
597 BidirectionalIterator first, BidirectionalIterator last,
598 const basic_regex<charT, traits>& e,
599 const basic_string<charT, ST, SA>& fmt,
600 regex_constants::match_flag_type flags = regex_constants::match_default);
601
602template <class OutputIterator, class BidirectionalIterator,
603 class traits, class charT>
604 OutputIterator
605 regex_replace(OutputIterator out,
606 BidirectionalIterator first, BidirectionalIterator last,
607 const basic_regex<charT, traits>& e, const charT* fmt,
608 regex_constants::match_flag_type flags = regex_constants::match_default);
609
610template <class traits, class charT, class ST, class SA, class FST, class FSA>>
611 basic_string<charT, ST, SA>
612 regex_replace(const basic_string<charT, ST, SA>& s,
613 const basic_regex<charT, traits>& e,
614 const basic_string<charT, FST, FSA>& fmt,
615 regex_constants::match_flag_type flags = regex_constants::match_default);
616
617template <class traits, class charT, class ST, class SA>
618 basic_string<charT, ST, SA>
619 regex_replace(const basic_string<charT, ST, SA>& s,
620 const basic_regex<charT, traits>& e, const charT* fmt,
621 regex_constants::match_flag_type flags = regex_constants::match_default);
622
623template <class traits, class charT, class ST, class SA>
624 basic_string<charT>
625 regex_replace(const charT* s,
626 const basic_regex<charT, traits>& e,
627 const basic_string<charT, ST, SA>& fmt,
628 regex_constants::match_flag_type flags = regex_constants::match_default);
629
630template <class traits, class charT>
631 basic_string<charT>
632 regex_replace(const charT* s,
633 const basic_regex<charT, traits>& e,
634 const charT* fmt,
635 regex_constants::match_flag_type flags = regex_constants::match_default);
636
637template <class BidirectionalIterator,
638 class charT = typename iterator_traits< BidirectionalIterator>::value_type,
639 class traits = regex_traits<charT>>
640class regex_iterator
641{
642public:
643 typedef basic_regex<charT, traits> regex_type;
644 typedef match_results<BidirectionalIterator> value_type;
645 typedef ptrdiff_t difference_type;
646 typedef const value_type* pointer;
647 typedef const value_type& reference;
648 typedef forward_iterator_tag iterator_category;
649
650 regex_iterator();
651 regex_iterator(BidirectionalIterator a, BidirectionalIterator b,
652 const regex_type& re,
653 regex_constants::match_flag_type m = regex_constants::match_default);
654 regex_iterator(const regex_iterator&);
655 regex_iterator& operator=(const regex_iterator&);
656
657 bool operator==(const regex_iterator&) const;
658 bool operator!=(const regex_iterator&) const;
659
660 const value_type& operator*() const;
661 const value_type* operator->() const;
662
663 regex_iterator& operator++();
664 regex_iterator operator++(int);
665};
666
667typedef regex_iterator<const char*> cregex_iterator;
668typedef regex_iterator<const wchar_t*> wcregex_iterator;
669typedef regex_iterator<string::const_iterator> sregex_iterator;
670typedef regex_iterator<wstring::const_iterator> wsregex_iterator;
671
672template <class BidirectionalIterator,
673 class charT = typename iterator_traits< BidirectionalIterator>::value_type,
674 class traits = regex_traits<charT>>
675class regex_token_iterator
676{
677public:
678 typedef basic_regex<charT, traits> regex_type;
679 typedef sub_match<BidirectionalIterator> value_type;
680 typedef ptrdiff_t difference_type;
681 typedef const value_type* pointer;
682 typedef const value_type& reference;
683 typedef forward_iterator_tag iterator_category;
684
685 regex_token_iterator();
686 regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b,
687 const regex_type& re, int submatch = 0,
688 regex_constants::match_flag_type m = regex_constants::match_default);
689 regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b,
690 const regex_type& re, const vector<int>& submatches,
691 regex_constants::match_flag_type m = regex_constants::match_default);
692 regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b,
693 const regex_type& re, initializer_list<int> submatches,
694 regex_constants::match_flag_type m = regex_constants::match_default);
695 template <size_t N>
696 regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b,
697 const regex_type& re, const int (&submatches)[N],
698 regex_constants::match_flag_type m = regex_constants::match_default);
699 regex_token_iterator(const regex_token_iterator&);
700 regex_token_iterator& operator=(const regex_token_iterator&);
701
702 bool operator==(const regex_token_iterator&) const;
703 bool operator!=(const regex_token_iterator&) const;
704
705 const value_type& operator*() const;
706 const value_type* operator->() const;
707
708 regex_token_iterator& operator++();
709 regex_token_iterator operator++(int);
710};
711
712typedef regex_token_iterator<const char*> cregex_token_iterator;
713typedef regex_token_iterator<const wchar_t*> wcregex_token_iterator;
714typedef regex_token_iterator<string::const_iterator> sregex_token_iterator;
715typedef regex_token_iterator<wstring::const_iterator> wsregex_token_iterator;
716
717} // std
718*/
719
Howard Hinnante77aa5e2010-07-08 17:43:58 +0000720#include <sstream>
721#include <cassert>
722
Howard Hinnant3257c982010-06-17 00:34:59 +0000723#include <__config>
724#include <stdexcept>
725#include <__locale>
Howard Hinnant8c2c18d2010-06-24 21:28:00 +0000726#include <initializer_list>
Howard Hinnantcd85b9e2010-06-29 18:37:43 +0000727#include <utility>
728#include <iterator>
729#include <string>
Howard Hinnant7e9d84b2010-06-30 00:21:42 +0000730#include <memory>
731#include <vector>
Howard Hinnantf8ce4592010-07-07 19:14:52 +0000732#include <__split_buffer>
Howard Hinnant3257c982010-06-17 00:34:59 +0000733
734#pragma GCC system_header
735
736_LIBCPP_BEGIN_NAMESPACE_STD
737
738namespace regex_constants
739{
740
741// syntax_option_type
742
743enum syntax_option_type
744{
745 icase = 1 << 0,
746 nosubs = 1 << 1,
747 optimize = 1 << 2,
748 collate = 1 << 3,
749 ECMAScript = 1 << 4,
750 basic = 1 << 5,
751 extended = 1 << 6,
752 awk = 1 << 7,
753 grep = 1 << 8,
754 egrep = 1 << 9
755};
756
757inline
758/*constexpr*/
759syntax_option_type
760operator~(syntax_option_type __x)
761{
762 return syntax_option_type(~int(__x));
763}
764
765inline
766/*constexpr*/
767syntax_option_type
768operator&(syntax_option_type __x, syntax_option_type __y)
769{
770 return syntax_option_type(int(__x) & int(__y));
771}
772
773inline
774/*constexpr*/
775syntax_option_type
776operator|(syntax_option_type __x, syntax_option_type __y)
777{
778 return syntax_option_type(int(__x) | int(__y));
779}
780
781inline
782/*constexpr*/
783syntax_option_type
784operator^(syntax_option_type __x, syntax_option_type __y)
785{
786 return syntax_option_type(int(__x) ^ int(__y));
787}
788
789inline
790/*constexpr*/
791syntax_option_type&
792operator&=(syntax_option_type& __x, syntax_option_type __y)
793{
794 __x = __x & __y;
795 return __x;
796}
797
798inline
799/*constexpr*/
800syntax_option_type&
801operator|=(syntax_option_type& __x, syntax_option_type __y)
802{
803 __x = __x | __y;
804 return __x;
805}
806
807inline
808/*constexpr*/
809syntax_option_type&
810operator^=(syntax_option_type& __x, syntax_option_type __y)
811{
812 __x = __x ^ __y;
813 return __x;
814}
815
816// match_flag_type
817
818enum match_flag_type
819{
820 match_default = 0,
821 match_not_bol = 1 << 0,
822 match_not_eol = 1 << 1,
823 match_not_bow = 1 << 2,
824 match_not_eow = 1 << 3,
825 match_any = 1 << 4,
826 match_not_null = 1 << 5,
827 match_continuous = 1 << 6,
828 match_prev_avail = 1 << 7,
829 format_default = 0,
830 format_sed = 1 << 8,
831 format_no_copy = 1 << 9,
832 format_first_only = 1 << 10
833};
834
835inline
836/*constexpr*/
837match_flag_type
838operator~(match_flag_type __x)
839{
840 return match_flag_type(~int(__x));
841}
842
843inline
844/*constexpr*/
845match_flag_type
846operator&(match_flag_type __x, match_flag_type __y)
847{
848 return match_flag_type(int(__x) & int(__y));
849}
850
851inline
852/*constexpr*/
853match_flag_type
854operator|(match_flag_type __x, match_flag_type __y)
855{
856 return match_flag_type(int(__x) | int(__y));
857}
858
859inline
860/*constexpr*/
861match_flag_type
862operator^(match_flag_type __x, match_flag_type __y)
863{
864 return match_flag_type(int(__x) ^ int(__y));
865}
866
867inline
868/*constexpr*/
869match_flag_type&
870operator&=(match_flag_type& __x, match_flag_type __y)
871{
872 __x = __x & __y;
873 return __x;
874}
875
876inline
877/*constexpr*/
878match_flag_type&
879operator|=(match_flag_type& __x, match_flag_type __y)
880{
881 __x = __x | __y;
882 return __x;
883}
884
885inline
886/*constexpr*/
887match_flag_type&
888operator^=(match_flag_type& __x, match_flag_type __y)
889{
890 __x = __x ^ __y;
891 return __x;
892}
893
894enum error_type
895{
896 error_collate = 1,
897 error_ctype,
898 error_escape,
899 error_backref,
900 error_brack,
901 error_paren,
902 error_brace,
903 error_badbrace,
904 error_range,
905 error_space,
906 error_badrepeat,
907 error_complexity,
Howard Hinnant8c2c18d2010-06-24 21:28:00 +0000908 error_stack,
909 error_temp
Howard Hinnant3257c982010-06-17 00:34:59 +0000910};
911
912} // regex_constants
913
914class _LIBCPP_EXCEPTION_ABI regex_error
915 : public runtime_error
916{
917 regex_constants::error_type __code_;
918public:
919 explicit regex_error(regex_constants::error_type __ecode);
920 virtual ~regex_error() throw();
921 regex_constants::error_type code() const {return __code_;}
922};
923
924template <class _CharT>
925struct regex_traits
926{
927public:
928 typedef _CharT char_type;
929 typedef basic_string<char_type> string_type;
930 typedef locale locale_type;
Howard Hinnantf409d2f2010-06-21 21:01:43 +0000931 typedef ctype_base::mask char_class_type;
Howard Hinnant3257c982010-06-17 00:34:59 +0000932
Howard Hinnantf409d2f2010-06-21 21:01:43 +0000933 static const char_class_type __regex_word = 0x80;
Howard Hinnant3257c982010-06-17 00:34:59 +0000934private:
935 locale __loc_;
936 const ctype<char_type>* __ct_;
937 const collate<char_type>* __col_;
938
939public:
940 regex_traits();
941
942 static size_t length(const char_type* __p)
943 {return char_traits<char_type>::length(__p);}
944 char_type translate(char_type __c) const {return __c;}
945 char_type translate_nocase(char_type __c) const;
946 template <class _ForwardIterator>
947 string_type
948 transform(_ForwardIterator __f, _ForwardIterator __l) const;
949 template <class _ForwardIterator>
950 string_type
951 transform_primary( _ForwardIterator __f, _ForwardIterator __l) const
952 {return __transform_primary(__f, __l, char_type());}
953 template <class _ForwardIterator>
954 string_type
955 lookup_collatename(_ForwardIterator __f, _ForwardIterator __l) const
956 {return __lookup_collatename(__f, __l, char_type());}
957 template <class _ForwardIterator>
958 char_class_type
959 lookup_classname(_ForwardIterator __f, _ForwardIterator __l,
Howard Hinnantf409d2f2010-06-21 21:01:43 +0000960 bool __icase = false) const
961 {return __lookup_classname(__f, __l, __icase, char_type());}
962 bool isctype(char_type __c, char_class_type __m) const;
963 int value(char_type __ch, int __radix) const
964 {return __value(__ch, __radix);}
Howard Hinnant3257c982010-06-17 00:34:59 +0000965 locale_type imbue(locale_type __l);
966 locale_type getloc()const {return __loc_;}
967
968private:
969 void __init();
970
971 template <class _ForwardIterator>
972 string_type
973 __transform_primary(_ForwardIterator __f, _ForwardIterator __l, char) const;
974 template <class _ForwardIterator>
975 string_type
976 __transform_primary(_ForwardIterator __f, _ForwardIterator __l, wchar_t) const;
977
978 template <class _ForwardIterator>
979 string_type
980 __lookup_collatename(_ForwardIterator __f, _ForwardIterator __l, char) const;
981 template <class _ForwardIterator>
982 string_type
983 __lookup_collatename(_ForwardIterator __f, _ForwardIterator __l, wchar_t) const;
Howard Hinnantf409d2f2010-06-21 21:01:43 +0000984
985 template <class _ForwardIterator>
986 char_class_type
987 __lookup_classname(_ForwardIterator __f, _ForwardIterator __l,
988 bool __icase, char) const;
989 template <class _ForwardIterator>
990 char_class_type
991 __lookup_classname(_ForwardIterator __f, _ForwardIterator __l,
992 bool __icase, wchar_t) const;
993
994 static int __value(unsigned char __ch, int __radix);
995 int __value(char __ch, int __radix) const
996 {return __value(static_cast<unsigned char>(__ch), __radix);}
997 int __value(wchar_t __ch, int __radix) const;
Howard Hinnant3257c982010-06-17 00:34:59 +0000998};
999
1000template <class _CharT>
1001regex_traits<_CharT>::regex_traits()
1002{
1003 __init();
1004}
1005
1006template <class _CharT>
1007typename regex_traits<_CharT>::char_type
1008regex_traits<_CharT>::translate_nocase(char_type __c) const
1009{
1010 return __ct_->tolower(__c);
1011}
1012
1013template <class _CharT>
1014template <class _ForwardIterator>
1015typename regex_traits<_CharT>::string_type
1016regex_traits<_CharT>::transform(_ForwardIterator __f, _ForwardIterator __l) const
1017{
1018 string_type __s(__f, __l);
1019 return __col_->transform(__s.data(), __s.data() + __s.size());
1020}
1021
1022template <class _CharT>
1023void
1024regex_traits<_CharT>::__init()
1025{
1026 __ct_ = &use_facet<ctype<char_type> >(__loc_);
1027 __col_ = &use_facet<collate<char_type> >(__loc_);
1028}
1029
1030template <class _CharT>
1031typename regex_traits<_CharT>::locale_type
1032regex_traits<_CharT>::imbue(locale_type __l)
1033{
1034 locale __r = __loc_;
1035 __loc_ = __l;
1036 __init();
1037 return __r;
1038}
1039
1040// transform_primary is very FreeBSD-specific
1041
1042template <class _CharT>
1043template <class _ForwardIterator>
1044typename regex_traits<_CharT>::string_type
1045regex_traits<_CharT>::__transform_primary(_ForwardIterator __f,
1046 _ForwardIterator __l, char) const
1047{
1048 const string_type __s(__f, __l);
1049 string_type __d = __col_->transform(__s.data(), __s.data() + __s.size());
1050 switch (__d.size())
1051 {
1052 case 1:
1053 break;
1054 case 12:
1055 __d[11] = __d[3];
1056 break;
1057 default:
1058 __d.clear();
1059 break;
1060 }
1061 return __d;
1062}
1063
1064template <class _CharT>
1065template <class _ForwardIterator>
1066typename regex_traits<_CharT>::string_type
1067regex_traits<_CharT>::__transform_primary(_ForwardIterator __f,
1068 _ForwardIterator __l, wchar_t) const
1069{
1070 const string_type __s(__f, __l);
1071 string_type __d = __col_->transform(__s.data(), __s.data() + __s.size());
1072 switch (__d.size())
1073 {
1074 case 1:
1075 break;
1076 case 3:
1077 __d[2] = __d[0];
1078 break;
1079 default:
1080 __d.clear();
1081 break;
1082 }
1083 return __d;
1084}
1085
1086// lookup_collatename is very FreeBSD-specific
1087
Howard Hinnantf409d2f2010-06-21 21:01:43 +00001088string __get_collation_name(const char* __s);
Howard Hinnant3257c982010-06-17 00:34:59 +00001089
1090template <class _CharT>
1091template <class _ForwardIterator>
1092typename regex_traits<_CharT>::string_type
1093regex_traits<_CharT>::__lookup_collatename(_ForwardIterator __f,
1094 _ForwardIterator __l, char) const
1095{
1096 string_type __s(__f, __l);
1097 string_type __r;
1098 if (!__s.empty())
1099 {
1100 __r = __get_collation_name(__s.c_str());
1101 if (__r.empty() && __s.size() <= 2)
1102 {
1103 __r = __col_->transform(__s.data(), __s.data() + __s.size());
1104 if (__r.size() == 1 || __r.size() == 12)
1105 __r = __s;
1106 else
1107 __r.clear();
1108 }
1109 }
1110 return __r;
1111}
1112
1113template <class _CharT>
1114template <class _ForwardIterator>
1115typename regex_traits<_CharT>::string_type
1116regex_traits<_CharT>::__lookup_collatename(_ForwardIterator __f,
1117 _ForwardIterator __l, wchar_t) const
1118{
1119 string_type __s(__f, __l);
1120 string __n;
1121 __n.reserve(__s.size());
1122 for (typename string_type::const_iterator __i = __s.begin(), __e = __s.end();
1123 __i != __e; ++__i)
1124 {
1125 if (static_cast<unsigned>(*__i) >= 127)
1126 return string_type();
1127 __n.push_back(char(*__i));
1128 }
1129 string_type __r;
1130 if (!__s.empty())
1131 {
1132 __n = __get_collation_name(__n.c_str());
1133 if (!__n.empty())
1134 __r.assign(__n.begin(), __n.end());
1135 else if (__s.size() <= 2)
1136 {
1137 __r = __col_->transform(__s.data(), __s.data() + __s.size());
1138 if (__r.size() == 1 || __r.size() == 3)
1139 __r = __s;
1140 else
1141 __r.clear();
1142 }
1143 }
1144 return __r;
1145}
1146
Howard Hinnantf409d2f2010-06-21 21:01:43 +00001147// lookup_classname
1148
1149ctype_base::mask __get_classname(const char* __s, bool __icase);
1150
1151template <class _CharT>
1152template <class _ForwardIterator>
1153typename regex_traits<_CharT>::char_class_type
1154regex_traits<_CharT>::__lookup_classname(_ForwardIterator __f,
1155 _ForwardIterator __l,
1156 bool __icase, char) const
1157{
1158 string_type __s(__f, __l);
1159 __ct_->tolower(&__s[0], &__s[0] + __s.size());
1160 return __get_classname(__s.c_str(), __icase);
1161}
1162
1163template <class _CharT>
1164template <class _ForwardIterator>
1165typename regex_traits<_CharT>::char_class_type
1166regex_traits<_CharT>::__lookup_classname(_ForwardIterator __f,
1167 _ForwardIterator __l,
1168 bool __icase, wchar_t) const
1169{
1170 string_type __s(__f, __l);
1171 __ct_->tolower(&__s[0], &__s[0] + __s.size());
1172 string __n;
1173 __n.reserve(__s.size());
1174 for (typename string_type::const_iterator __i = __s.begin(), __e = __s.end();
1175 __i != __e; ++__i)
1176 {
1177 if (static_cast<unsigned>(*__i) >= 127)
1178 return char_class_type();
1179 __n.push_back(char(*__i));
1180 }
1181 return __get_classname(__n.c_str(), __icase);
1182}
1183
1184template <class _CharT>
1185bool
1186regex_traits<_CharT>::isctype(char_type __c, char_class_type __m) const
1187{
1188 if (__ct_->is(__m, __c))
1189 return true;
1190 return (__c == '_' && (__m & __regex_word));
1191}
1192
1193template <class _CharT>
1194int
1195regex_traits<_CharT>::__value(unsigned char __ch, int __radix)
1196{
1197 if ((__ch & 0xF8u) == 0x30) // '0' <= __ch && __ch <= '7'
1198 return __ch - '0';
1199 if (__radix != 8)
1200 {
1201 if ((__ch & 0xFEu) == 0x38) // '8' <= __ch && __ch <= '9'
1202 return __ch - '0';
1203 if (__radix == 16)
1204 {
1205 __ch |= 0x20; // tolower
1206 if ('a' <= __ch && __ch <= 'f')
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00001207 return __ch - ('a' - 10);
Howard Hinnantf409d2f2010-06-21 21:01:43 +00001208 }
1209 }
1210 return -1;
1211}
1212
1213template <class _CharT>
1214inline
1215int
1216regex_traits<_CharT>::__value(wchar_t __ch, int __radix) const
1217{
1218 return __value(static_cast<unsigned char>(__ct_->narrow(__ch, char_type())), __radix);
1219}
1220
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001221template <class _CharT> class __state;
1222
1223template <class _CharT>
1224struct __command
1225{
1226 enum
1227 {
1228 __end_state = -1000,
1229 __consume_input, // -999
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001230 __begin_marked_expr, // -998
1231 __end_marked_expr, // -997
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001232 __pop_state, // -996
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001233 __accept_and_consume, // -995
1234 __accept_but_not_consume, // -994
1235 __reject, // -993
1236 __zero_loop_count,
1237 __increment_loop_count,
1238 __zero_marked_exprs,
1239 };
1240
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001241 typedef _STD::__state<_CharT> __state;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001242
1243 int __do_;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001244 const __state* first;
1245 const __state* second;
1246
1247 __command() : __do_(__reject), first(nullptr), second(nullptr) {}
1248 explicit __command(int __do)
1249 : __do_(__do), first(nullptr), second(nullptr) {}
1250 __command(int __do, const __state* __s1, const __state* __s2 = nullptr)
1251 : __do_(__do), first(__s1), second(__s2) {}
1252 explicit __command(const __state* __s1, const __state* __s2 = nullptr)
1253 : __do_(0), first(__s1), second(__s2) {}
1254};
1255
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001256template <class _CharT>
1257ostream&
1258operator<<(ostream& os, const __command<_CharT>& c)
1259{
1260 os << c.__do_;
1261 if (c.first)
1262 os << ", " << c.first->speak();
1263 if (c.second)
1264 os << ", " << c.second->speak();
1265 return os;
1266}
1267
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001268template <class _BidirectionalIterator> class sub_match;
1269
1270// __state
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001271
1272template <class _CharT>
1273class __state
1274{
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001275 __state(const __state&);
1276 __state& operator=(const __state&);
1277public:
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001278 typedef _STD::__command<_CharT> __command;
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001279
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001280 __state() {}
1281 virtual ~__state() {}
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001282
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001283 virtual __command __test(const _CharT* __first, const _CharT* __current,
1284 const _CharT* __last,
1285 vector<size_t>& __lc,
1286 sub_match<const _CharT*>* __m,
1287 regex_constants::match_flag_type __flags) const = 0;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001288
1289 virtual string speak() const = 0;
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001290};
1291
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001292// __end_state
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001293
1294template <class _CharT>
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001295class __end_state
1296 : public __state<_CharT>
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001297{
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001298public:
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001299 typedef _STD::__command<_CharT> __command;
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001300
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001301 __end_state() {}
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00001302
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001303 virtual __command __test(const _CharT*, const _CharT*,
1304 const _CharT*,
1305 vector<size_t>&,
1306 sub_match<const _CharT*>*,
1307 regex_constants::match_flag_type) const;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001308
1309 virtual string speak() const {return "end state";}
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001310};
1311
1312template <class _CharT>
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001313__command<_CharT>
1314__end_state<_CharT>::__test(const _CharT*, const _CharT*,
1315 const _CharT*,
1316 vector<size_t>&,
1317 sub_match<const _CharT*>*,
1318 regex_constants::match_flag_type) const
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00001319{
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001320 return __command(__command::__end_state);
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00001321}
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001322
1323// __has_one_state
1324
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00001325template <class _CharT>
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001326class __has_one_state
1327 : public __state<_CharT>
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00001328{
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001329 __state<_CharT>* __first_;
1330
1331public:
1332 explicit __has_one_state(__state<_CharT>* __s)
1333 : __first_(__s) {}
1334
1335 __state<_CharT>* first() const {return __first_;}
1336 __state<_CharT>*& first() {return __first_;}
1337};
1338
1339// __owns_one_state
1340
1341template <class _CharT>
1342class __owns_one_state
1343 : public __has_one_state<_CharT>
1344{
1345 typedef __has_one_state<_CharT> base;
1346
1347public:
1348 explicit __owns_one_state(__state<_CharT>* __s)
1349 : base(__s) {}
1350
1351 virtual ~__owns_one_state();
1352};
1353
1354template <class _CharT>
1355__owns_one_state<_CharT>::~__owns_one_state()
1356{
1357 delete this->first();
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00001358}
1359
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001360// __empty_state
1361
1362template <class _CharT>
1363class __empty_state
1364 : public __owns_one_state<_CharT>
1365{
1366 typedef __owns_one_state<_CharT> base;
1367
1368public:
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001369 typedef _STD::__command<_CharT> __command;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001370
1371 explicit __empty_state(__state<_CharT>* __s)
1372 : base(__s) {}
1373
1374 virtual __command __test(const _CharT*, const _CharT*,
1375 const _CharT*,
1376 vector<size_t>&,
1377 sub_match<const _CharT*>*,
1378 regex_constants::match_flag_type) const;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001379
1380 virtual string speak() const {return "empty state";}
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001381};
1382
1383template <class _CharT>
1384__command<_CharT>
1385__empty_state<_CharT>::__test(const _CharT*, const _CharT*, const _CharT*,
1386 vector<size_t>&,
1387 sub_match<const _CharT*>*,
1388 regex_constants::match_flag_type) const
1389{
1390 return __command(__command::__accept_but_not_consume, this->first());
1391}
1392
1393// __empty_non_own_state
1394
1395template <class _CharT>
1396class __empty_non_own_state
1397 : public __has_one_state<_CharT>
1398{
1399 typedef __has_one_state<_CharT> base;
1400
1401public:
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001402 typedef _STD::__command<_CharT> __command;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001403
1404 explicit __empty_non_own_state(__state<_CharT>* __s)
1405 : base(__s) {}
1406
1407 virtual __command __test(const _CharT*, const _CharT*,
1408 const _CharT*,
1409 vector<size_t>&,
1410 sub_match<const _CharT*>*,
1411 regex_constants::match_flag_type) const;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001412
1413 virtual string speak() const {return "empty non-owning state";}
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001414};
1415
1416template <class _CharT>
1417__command<_CharT>
1418__empty_non_own_state<_CharT>::__test(const _CharT*, const _CharT*, const _CharT*,
1419 vector<size_t>&,
1420 sub_match<const _CharT*>*,
1421 regex_constants::match_flag_type) const
1422{
1423 return __command(__command::__accept_but_not_consume, this->first());
1424}
1425
1426// __owns_two_states
1427
1428template <class _CharT>
1429class __owns_two_states
1430 : public __owns_one_state<_CharT>
1431{
1432 typedef __owns_one_state<_CharT> base;
1433
1434 base* __second_;
1435
1436public:
1437 explicit __owns_two_states(__state<_CharT>* __s1, base* __s2)
1438 : base(__s1), __second_(__s2) {}
1439
1440 virtual ~__owns_two_states();
1441
1442 base* second() const {return __second_;}
1443 base*& second() {return __second_;}
1444};
1445
1446template <class _CharT>
1447__owns_two_states<_CharT>::~__owns_two_states()
1448{
1449 delete __second_;
1450}
1451
1452// __loop
1453
1454template <class _CharT>
1455class __loop
1456 : public __owns_two_states<_CharT>
1457{
1458 typedef __owns_two_states<_CharT> base;
1459
1460 size_t __min_;
1461 size_t __max_;
1462 unsigned __loop_id_;
1463 bool __greedy_;
1464
1465public:
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001466 typedef _STD::__command<_CharT> __command;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001467
1468 explicit __loop(unsigned __loop_id,
1469 __state<_CharT>* __s1, __owns_one_state<_CharT>* __s2,
1470 bool __greedy = true,
1471 size_t __min = 0,
1472 size_t __max = numeric_limits<size_t>::max())
1473 : base(__s1, __s2), __min_(__min), __max_(__max), __loop_id_(__loop_id),
1474 __greedy_(__greedy) {}
1475
1476 virtual __command __test(const _CharT* __first, const _CharT* __current,
1477 const _CharT* __last,
1478 vector<size_t>&,
1479 sub_match<const _CharT*>*,
1480 regex_constants::match_flag_type __flags) const;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001481
1482 virtual string speak() const
1483 {
1484 ostringstream os;
1485 os << "loop {" << __min_ << ',' << __max_ << "}";
1486 if (!__greedy_)
1487 os << " not";
1488 os << " greedy";
1489 return os.str();
1490 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001491};
1492
1493template <class _CharT>
1494__command<_CharT>
1495__loop<_CharT>::__test(const _CharT* __first, const _CharT* __current,
1496 const _CharT* __last,
1497 vector<size_t>& __lc,
1498 sub_match<const _CharT*>* __m,
1499 regex_constants::match_flag_type __flags) const
1500{
1501 size_t __count = __lc[__loop_id_];
1502 if (__min_ <= __count && __count < __max_)
1503 if (__greedy_)
1504 return __command(__command::__accept_but_not_consume, this->first(),
1505 this->second());
1506 else
1507 return __command(__command::__accept_but_not_consume, this->second(),
1508 this->first());
1509 if (__min_ <= __count)
1510 return __command(__command::__accept_but_not_consume, this->second());
1511 if (__count < __max_)
1512 return __command(__command::__accept_but_not_consume, this->first());
1513 throw regex_error(regex_constants::error_temp);
1514}
1515
1516// __zero_loop_count
1517
1518template <class _CharT>
1519class __zero_loop_count
1520 : public __owns_one_state<_CharT>
1521{
1522 typedef __owns_one_state<_CharT> base;
1523
1524 size_t __loop_id_;
1525
1526public:
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001527 typedef _STD::__command<_CharT> __command;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001528
1529 explicit __zero_loop_count(size_t __loop_id,
1530 __state<_CharT>* __s1)
1531 : base(__s1), __loop_id_(__loop_id) {}
1532
1533 virtual __command __test(const _CharT*, const _CharT*, const _CharT*,
1534 vector<size_t>& __lc,
1535 sub_match<const _CharT*>*,
1536 regex_constants::match_flag_type) const;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001537
1538 virtual string speak() const
1539 {
1540 ostringstream os;
1541 os << "zero loop " << __loop_id_;
1542 return os.str();
1543 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001544};
1545
1546template <class _CharT>
1547__command<_CharT>
1548__zero_loop_count<_CharT>::__test(const _CharT*, const _CharT*, const _CharT*,
1549 vector<size_t>& __lc,
1550 sub_match<const _CharT*>*,
1551 regex_constants::match_flag_type) const
1552{
1553 __lc[__loop_id_] = 0;
1554 return __command(__command::__accept_but_not_consume, this->first());
1555}
1556
1557// __increment_loop_count
1558
1559template <class _CharT>
1560class __increment_loop_count
1561 : public __has_one_state<_CharT>
1562{
1563 typedef __has_one_state<_CharT> base;
1564
1565 size_t __loop_id_;
1566
1567public:
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001568 typedef _STD::__command<_CharT> __command;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001569
1570 explicit __increment_loop_count(size_t __loop_id,
1571 __state<_CharT>* __s1)
1572 : base(__s1), __loop_id_(__loop_id) {}
1573
1574 virtual __command __test(const _CharT*, const _CharT*, const _CharT*,
1575 vector<size_t>& __lc,
1576 sub_match<const _CharT*>*,
1577 regex_constants::match_flag_type) const;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001578
1579 virtual string speak() const
1580 {
1581 ostringstream os;
1582 os << "increment loop " << __loop_id_;
1583 return os.str();
1584 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001585};
1586
1587template <class _CharT>
1588__command<_CharT>
1589__increment_loop_count<_CharT>::__test(const _CharT*, const _CharT*, const _CharT*,
1590 vector<size_t>& __lc,
1591 sub_match<const _CharT*>*,
1592 regex_constants::match_flag_type) const
1593{
1594 ++__lc[__loop_id_];
1595 return __command(__command::__accept_but_not_consume, this->first());
1596}
1597
1598// __zero_marked_exprs
1599
1600template <class _CharT>
1601class __zero_marked_exprs
1602 : public __owns_one_state<_CharT>
1603{
1604 typedef __owns_one_state<_CharT> base;
1605
1606 size_t __begin_;
1607 size_t __end_;
1608
1609public:
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001610 typedef _STD::__command<_CharT> __command;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001611
1612 explicit __zero_marked_exprs(size_t __begin, size_t __end,
1613 __state<_CharT>* __s1)
1614 : base(__s1), __begin_(__begin), __end_(__end) {}
1615
1616 virtual __command __test(const _CharT*, const _CharT*, const _CharT*,
1617 vector<size_t>&,
1618 sub_match<const _CharT*>* __sm,
1619 regex_constants::match_flag_type) const;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001620
1621 virtual string speak() const
1622 {
1623 ostringstream os;
1624 os << "zero marked exprs [" << __begin_ << ',' << __end_ << ')';
1625 return os.str();
1626 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001627};
1628
1629template <class _CharT>
1630__command<_CharT>
1631__zero_marked_exprs<_CharT>::__test(const _CharT*, const _CharT*,
1632 const _CharT* __last,
1633 vector<size_t>&,
1634 sub_match<const _CharT*>* __sm,
1635 regex_constants::match_flag_type) const
1636{
1637 for (size_t __i = __begin_; __i != __end_; ++__i)
1638 {
1639 __sm[__i].first = __last;
1640 __sm[__i].second = __last;
1641 __sm[__i].matched = false;
1642 }
1643 return __command(__command::__accept_but_not_consume, this->first());
1644}
1645
1646// __begin_marked_subexpression
1647
1648template <class _CharT>
1649class __begin_marked_subexpression
1650 : public __owns_one_state<_CharT>
1651{
1652 typedef __owns_one_state<_CharT> base;
1653
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001654 unsigned __mexp_;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001655public:
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001656 typedef _STD::__command<_CharT> __command;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001657
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001658 explicit __begin_marked_subexpression(unsigned __mexp, __state<_CharT>* __s)
1659 : base(__s), __mexp_(__mexp) {}
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001660
1661 virtual __command __test(const _CharT*, const _CharT*,
1662 const _CharT*,
1663 vector<size_t>&,
1664 sub_match<const _CharT*>*,
1665 regex_constants::match_flag_type) const;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001666
1667 virtual string speak() const
1668 {
1669 ostringstream os;
1670 os << "begin marked expr " << __mexp_;
1671 return os.str();
1672 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001673};
1674
1675template <class _CharT>
1676__command<_CharT>
1677__begin_marked_subexpression<_CharT>::__test(const _CharT*, const _CharT* __c, const _CharT*,
1678 vector<size_t>&,
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001679 sub_match<const _CharT*>* __s,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001680 regex_constants::match_flag_type) const
1681{
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001682 __s[__mexp_].first = __c;
1683 return __command(__command::__accept_but_not_consume, this->first());
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001684}
1685
1686// __end_marked_subexpression
1687
1688template <class _CharT>
1689class __end_marked_subexpression
1690 : public __owns_one_state<_CharT>
1691{
1692 typedef __owns_one_state<_CharT> base;
1693
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001694 unsigned __mexp_;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001695public:
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001696 typedef _STD::__command<_CharT> __command;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001697
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001698 explicit __end_marked_subexpression(unsigned __mexp, __state<_CharT>* __s)
1699 : base(__s), __mexp_(__mexp) {}
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001700
1701 virtual __command __test(const _CharT*, const _CharT*,
1702 const _CharT*,
1703 vector<size_t>&,
1704 sub_match<const _CharT*>*,
1705 regex_constants::match_flag_type) const;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001706
1707 virtual string speak() const
1708 {
1709 ostringstream os;
1710 os << "end marked expr " << __mexp_;
1711 return os.str();
1712 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001713};
1714
1715template <class _CharT>
1716__command<_CharT>
1717__end_marked_subexpression<_CharT>::__test(const _CharT*, const _CharT* __c, const _CharT*,
1718 vector<size_t>&,
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001719 sub_match<const _CharT*>* __s,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001720 regex_constants::match_flag_type) const
1721{
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001722 __s[__mexp_].second = __c;
1723 __s[__mexp_].matched = true;
1724 return __command(__command::__accept_but_not_consume, this->first());
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001725}
1726
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001727// __r_anchor
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001728
1729template <class _CharT>
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001730class __r_anchor
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001731 : public __owns_one_state<_CharT>
1732{
1733 typedef __owns_one_state<_CharT> base;
1734
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001735public:
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001736 typedef _STD::__command<_CharT> __command;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001737
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001738 __r_anchor(__state<_CharT>* __s)
1739 : base(__s) {}
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001740
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001741 virtual __command __test(const _CharT*, const _CharT* __c,
1742 const _CharT* __l,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001743 vector<size_t>&,
1744 sub_match<const _CharT*>*,
1745 regex_constants::match_flag_type) const;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001746
1747 virtual string speak() const
1748 {
1749 ostringstream os;
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001750 os << "right anchor";
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001751 return os.str();
1752 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001753};
1754
1755template <class _CharT>
1756__command<_CharT>
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001757__r_anchor<_CharT>::__test(const _CharT*, const _CharT* __c, const _CharT* __l,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001758 vector<size_t>&,
1759 sub_match<const _CharT*>*,
1760 regex_constants::match_flag_type) const
1761{
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001762 return __c == __l ?
1763 __command(__command::__accept_but_not_consume, this->first()) : __command();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001764}
1765
1766// __match_char
1767
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00001768template <class _CharT>
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001769class __match_char
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001770 : public __owns_one_state<_CharT>
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001771{
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001772 typedef __owns_one_state<_CharT> base;
1773
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001774 _CharT __c_;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001775
1776 __match_char(const __match_char&);
1777 __match_char& operator=(const __match_char&);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001778public:
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001779 typedef _STD::__command<_CharT> __command;
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00001780
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001781 __match_char(_CharT __c, __state<_CharT>* __s)
1782 : base(__s), __c_(__c) {}
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001783
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001784 virtual __command __test(const _CharT*, const _CharT* __c,
1785 const _CharT*,
1786 vector<size_t>&,
1787 sub_match<const _CharT*>*,
1788 regex_constants::match_flag_type) const;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001789
1790 virtual string speak() const
1791 {
1792 ostringstream os;
1793 os << "match char " << __c_;
1794 return os.str();
1795 }
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001796};
1797
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00001798template <class _CharT>
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001799__command<_CharT>
1800__match_char<_CharT>::__test(const _CharT*, const _CharT* __c,
1801 const _CharT*,
1802 vector<size_t>&,
1803 sub_match<const _CharT*>*,
1804 regex_constants::match_flag_type) const
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00001805{
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001806 return __c_ == *__c ?
1807 __command(__command::__accept_and_consume, this->first()) : __command();
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00001808}
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00001809
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00001810template <class, class> class match_results;
1811
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00001812template <class _CharT, class _Traits = regex_traits<_CharT> >
1813class basic_regex
1814{
1815public:
1816 // types:
1817 typedef _CharT value_type;
1818 typedef regex_constants::syntax_option_type flag_type;
1819 typedef typename _Traits::locale_type locale_type;
1820
1821private:
1822 _Traits __traits_;
1823 flag_type __flags_;
1824 unsigned __marked_count_;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001825 unsigned __loop_count_;
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001826 int __open_count_;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001827 shared_ptr<__empty_state<_CharT> > __start_;
1828 __owns_one_state<_CharT>* __end_;
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001829 bool __left_anchor_;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001830
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001831 typedef _STD::__command<_CharT> __command;
1832 typedef _STD::__state<_CharT> __state;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00001833
1834public:
1835 // constants:
1836 static const/*expr*/ regex_constants::syntax_option_type icase = regex_constants::icase;
1837 static const/*expr*/ regex_constants::syntax_option_type nosubs = regex_constants::nosubs;
1838 static const/*expr*/ regex_constants::syntax_option_type optimize = regex_constants::optimize;
1839 static const/*expr*/ regex_constants::syntax_option_type collate = regex_constants::collate;
1840 static const/*expr*/ regex_constants::syntax_option_type ECMAScript = regex_constants::ECMAScript;
1841 static const/*expr*/ regex_constants::syntax_option_type basic = regex_constants::basic;
1842 static const/*expr*/ regex_constants::syntax_option_type extended = regex_constants::extended;
1843 static const/*expr*/ regex_constants::syntax_option_type awk = regex_constants::awk;
1844 static const/*expr*/ regex_constants::syntax_option_type grep = regex_constants::grep;
1845 static const/*expr*/ regex_constants::syntax_option_type egrep = regex_constants::egrep;
1846
1847 // construct/copy/destroy:
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001848 basic_regex()
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001849 : __flags_(), __marked_count_(0), __loop_count_(0), __open_count_(0),
1850 __end_(0), __left_anchor_(false)
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001851 {}
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00001852 explicit basic_regex(const value_type* __p, flag_type __f = regex_constants::ECMAScript)
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001853 : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
1854 __end_(0), __left_anchor_(false)
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00001855 {__parse(__p, __p + __traits_.length(__p));}
1856 basic_regex(const value_type* __p, size_t __len, flag_type __f)
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001857 : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
1858 __end_(0), __left_anchor_(false)
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00001859 {__parse(__p, __p + __len);}
1860 basic_regex(const basic_regex&);
1861#ifdef _LIBCPP_MOVE
1862 basic_regex(basic_regex&&);
1863#endif
1864 template <class _ST, class _SA>
1865 explicit basic_regex(const basic_string<value_type, _ST, _SA>& __p,
1866 flag_type __f = regex_constants::ECMAScript)
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001867 : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
1868 __end_(0), __left_anchor_(false)
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00001869 {__parse(__p.begin(), __p.end());}
1870 template <class _ForwardIterator>
1871 basic_regex(_ForwardIterator __first, _ForwardIterator __last,
1872 flag_type __f = regex_constants::ECMAScript)
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001873 : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
1874 __end_(0), __left_anchor_(false)
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00001875 {__parse(__first, __last);}
1876 basic_regex(initializer_list<value_type> __il,
1877 flag_type __f = regex_constants::ECMAScript)
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001878 : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
1879 __end_(0), __left_anchor_(false)
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00001880 {__parse(__il.begin(), __il.end());}
1881
1882 ~basic_regex();
1883
1884 basic_regex& operator=(const basic_regex&);
1885#ifdef _LIBCPP_MOVE
1886 basic_regex& operator=(basic_regex&&);
1887#endif
1888 basic_regex& operator=(const value_type* __p);
1889 basic_regex& operator=(initializer_list<value_type> __il);
1890 template <class _ST, class _SA>
1891 basic_regex& operator=(const basic_string<value_type, _ST, _SA>& __p);
1892
1893 // assign:
1894 basic_regex& assign(const basic_regex& __that);
1895#ifdef _LIBCPP_MOVE
1896 basic_regex& assign(basic_regex&& __that);
1897#endif
1898 basic_regex& assign(const value_type* __p, flag_type __f = regex_constants::ECMAScript);
1899 basic_regex& assign(const value_type* __p, size_t __len, flag_type __f);
1900 template <class _ST, class _SA>
1901 basic_regex& assign(const basic_string<value_type, _ST, _SA>& __s,
1902 flag_type __f = regex_constants::ECMAScript);
1903 template <class _InputIterator>
1904 basic_regex& assign(_InputIterator __first, _InputIterator __last,
1905 flag_type __f = regex_constants::ECMAScript);
1906 basic_regex& assign(initializer_list<value_type> __il,
1907 flag_type = regex_constants::ECMAScript);
1908
1909 // const operations:
1910 unsigned mark_count() const {return __marked_count_;}
1911 flag_type flags() const {return __flags_;}
1912
1913 // locale:
1914 locale_type imbue(locale_type __loc) {return __traits_.imbue(__loc);}
1915 locale_type getloc() const {return __traits_.getloc();}
1916
1917 // swap:
1918 void swap(basic_regex&);
1919
1920private:
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001921 unsigned __loop_count() const {return __loop_count_;}
1922
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00001923 template <class _ForwardIterator>
1924 void __parse(_ForwardIterator __first, _ForwardIterator __last);
1925 template <class _ForwardIterator>
1926 _ForwardIterator
1927 __parse_basic_reg_exp(_ForwardIterator __first, _ForwardIterator __last);
1928 template <class _ForwardIterator>
1929 _ForwardIterator
1930 __parse_RE_expression(_ForwardIterator __first, _ForwardIterator __last);
1931 template <class _ForwardIterator>
1932 _ForwardIterator
1933 __parse_simple_RE(_ForwardIterator __first, _ForwardIterator __last);
1934 template <class _ForwardIterator>
1935 _ForwardIterator
1936 __parse_nondupl_RE(_ForwardIterator __first, _ForwardIterator __last);
1937 template <class _ForwardIterator>
1938 _ForwardIterator
1939 __parse_one_char_or_coll_elem_RE(_ForwardIterator __first, _ForwardIterator __last);
1940 template <class _ForwardIterator>
1941 _ForwardIterator
1942 __parse_Back_open_paren(_ForwardIterator __first, _ForwardIterator __last);
1943 template <class _ForwardIterator>
1944 _ForwardIterator
1945 __parse_Back_close_paren(_ForwardIterator __first, _ForwardIterator __last);
1946 template <class _ForwardIterator>
1947 _ForwardIterator
1948 __parse_Back_open_brace(_ForwardIterator __first, _ForwardIterator __last);
1949 template <class _ForwardIterator>
1950 _ForwardIterator
1951 __parse_Back_close_brace(_ForwardIterator __first, _ForwardIterator __last);
1952 template <class _ForwardIterator>
1953 _ForwardIterator
1954 __parse_BACKREF(_ForwardIterator __first, _ForwardIterator __last);
1955 template <class _ForwardIterator>
1956 _ForwardIterator
1957 __parse_ORD_CHAR(_ForwardIterator __first, _ForwardIterator __last);
1958 template <class _ForwardIterator>
1959 _ForwardIterator
1960 __parse_QUOTED_CHAR(_ForwardIterator __first, _ForwardIterator __last);
1961 template <class _ForwardIterator>
1962 _ForwardIterator
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001963 __parse_RE_dupl_symbol(_ForwardIterator __first, _ForwardIterator __last,
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001964 __owns_one_state<_CharT>* __s,
1965 unsigned __mexp_begin, unsigned __mexp_end);
Howard Hinnant0de86b62010-06-25 20:56:08 +00001966 template <class _ForwardIterator>
1967 _ForwardIterator
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001968 __parse_ERE_dupl_symbol(_ForwardIterator __first, _ForwardIterator __last);
1969 template <class _ForwardIterator>
1970 _ForwardIterator
Howard Hinnant0de86b62010-06-25 20:56:08 +00001971 __parse_bracket_expression(_ForwardIterator __first, _ForwardIterator __last);
1972 template <class _ForwardIterator>
1973 _ForwardIterator
1974 __parse_follow_list(_ForwardIterator __first, _ForwardIterator __last);
1975 template <class _ForwardIterator>
1976 _ForwardIterator
1977 __parse_expression_term(_ForwardIterator __first, _ForwardIterator __last);
1978 template <class _ForwardIterator>
1979 _ForwardIterator
1980 __parse_equivalence_class(_ForwardIterator __first, _ForwardIterator __last);
1981 template <class _ForwardIterator>
1982 _ForwardIterator
1983 __parse_character_class(_ForwardIterator __first, _ForwardIterator __last);
1984 template <class _ForwardIterator>
1985 _ForwardIterator
1986 __parse_collating_symbol(_ForwardIterator __first, _ForwardIterator __last);
1987 template <class _ForwardIterator>
1988 _ForwardIterator
1989 __parse_DUP_COUNT(_ForwardIterator __first, _ForwardIterator __last, int& __c);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001990 template <class _ForwardIterator>
1991 _ForwardIterator
1992 __parse_extended_reg_exp(_ForwardIterator __first, _ForwardIterator __last);
1993 template <class _ForwardIterator>
1994 _ForwardIterator
1995 __parse_ERE_branch(_ForwardIterator __first, _ForwardIterator __last);
1996 template <class _ForwardIterator>
1997 _ForwardIterator
1998 __parse_ERE_expression(_ForwardIterator __first, _ForwardIterator __last);
1999 template <class _ForwardIterator>
2000 _ForwardIterator
2001 __parse_one_char_or_coll_elem_ERE(_ForwardIterator __first, _ForwardIterator __last);
2002 template <class _ForwardIterator>
2003 _ForwardIterator
2004 __parse_ORD_CHAR_ERE(_ForwardIterator __first, _ForwardIterator __last);
2005 template <class _ForwardIterator>
2006 _ForwardIterator
2007 __parse_QUOTED_CHAR_ERE(_ForwardIterator __first, _ForwardIterator __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002008
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00002009 void __push_l_anchor() {__left_anchor_ = true;}
2010 void __push_r_anchor();
Howard Hinnant0de86b62010-06-25 20:56:08 +00002011 void __push_match_any() {}
Howard Hinnante77aa5e2010-07-08 17:43:58 +00002012 void __push_greedy_inf_repeat(size_t __min, __owns_one_state<_CharT>* __s,
2013 unsigned __mexp_begin = 0, unsigned __mexp_end = 0)
2014 {__push_loop(__min, numeric_limits<size_t>::max(), __s,
2015 __mexp_begin, __mexp_end);}
Howard Hinnant0de86b62010-06-25 20:56:08 +00002016 void __push_exact_repeat(int __count) {}
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002017 void __push_loop(size_t __min, size_t __max, __owns_one_state<_CharT>* __s,
2018 size_t __mexp_begin = 0, size_t __mexp_end = 0,
2019 bool __greedy = true);
Howard Hinnant0de86b62010-06-25 20:56:08 +00002020 void __start_nonmatching_list() {}
2021 void __start_matching_list() {}
2022 void __end_nonmatching_list() {}
2023 void __end_matching_list() {}
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002024 void __push_char(value_type __c);
Howard Hinnant0de86b62010-06-25 20:56:08 +00002025 void __push_char(const typename _Traits::string_type& __c) {}
2026 void __push_range() {}
2027 void __push_class_type(typename _Traits::char_class_type) {}
2028 void __push_back_ref(int __i) {}
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002029 void __push_alternation() {}
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00002030 void __push_begin_marked_subexpression();
2031 void __push_end_marked_subexpression(unsigned);
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00002032
2033 template <class _BidirectionalIterator, class _Allocator>
2034 bool
2035 __search(_BidirectionalIterator __first, _BidirectionalIterator __last,
2036 match_results<_BidirectionalIterator, _Allocator>& __m,
2037 regex_constants::match_flag_type __flags) const;
2038
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002039 template <class _BidirectionalIterator, class _Allocator>
2040 bool
2041 __match_at_start(_BidirectionalIterator __first, _BidirectionalIterator __last,
2042 match_results<_BidirectionalIterator, _Allocator>& __m,
2043 vector<size_t>& __lc,
2044 regex_constants::match_flag_type __flags) const;
2045 template <class _BidirectionalIterator, class _Allocator>
2046 bool
2047 __match_at_start_ecma(_BidirectionalIterator __first, _BidirectionalIterator __last,
2048 match_results<_BidirectionalIterator, _Allocator>& __m,
2049 regex_constants::match_flag_type __flags) const;
2050 template <class _BidirectionalIterator, class _Allocator>
2051 bool
2052 __match_at_start_posix_nosubs(const _CharT* __first, const _CharT* __last,
2053 match_results<_BidirectionalIterator, _Allocator>& __m,
2054 vector<size_t>& __lc,
2055 regex_constants::match_flag_type __flags) const;
2056 template <class _BidirectionalIterator, class _Allocator>
2057 bool
2058 __match_at_start_posix_subs(_BidirectionalIterator __first, _BidirectionalIterator __last,
2059 match_results<_BidirectionalIterator, _Allocator>& __m,
Howard Hinnante77aa5e2010-07-08 17:43:58 +00002060 vector<size_t>& __lc,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002061 regex_constants::match_flag_type __flags) const;
2062
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00002063 template <class _B, class _A, class _C, class _T>
2064 friend
2065 bool
2066 regex_search(_B, _B, match_results<_B, _A>&, const basic_regex<_C, _T>&,
2067 regex_constants::match_flag_type);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002068
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002069};
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002070
2071template <class _CharT, class _Traits>
2072basic_regex<_CharT, _Traits>::~basic_regex()
2073{
2074}
2075
2076template <class _CharT, class _Traits>
2077template <class _ForwardIterator>
2078void
2079basic_regex<_CharT, _Traits>::__parse(_ForwardIterator __first,
2080 _ForwardIterator __last)
2081{
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002082 {
2083 unique_ptr<__state> __h(new __end_state<_CharT>);
2084 __start_.reset(new __empty_state<_CharT>(__h.get()));
2085 __h.release();
2086 __end_ = __start_.get();
2087 }
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002088 switch (__flags_ & 0x3F0)
2089 {
2090 case ECMAScript:
2091 break;
2092 case basic:
2093 __parse_basic_reg_exp(__first, __last);
2094 break;
2095 case extended:
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002096 __parse_extended_reg_exp(__first, __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002097 break;
2098 case awk:
2099 break;
2100 case grep:
2101 break;
2102 case egrep:
2103 break;
2104 default:
2105 throw regex_error(regex_constants::error_temp);
2106 }
2107}
2108
2109template <class _CharT, class _Traits>
2110template <class _ForwardIterator>
2111_ForwardIterator
2112basic_regex<_CharT, _Traits>::__parse_basic_reg_exp(_ForwardIterator __first,
2113 _ForwardIterator __last)
2114{
2115 if (__first != __last)
2116 {
2117 if (*__first == '^')
2118 {
2119 __push_l_anchor();
2120 ++__first;
2121 }
2122 if (__first != __last)
2123 {
2124 __first = __parse_RE_expression(__first, __last);
2125 if (__first != __last)
2126 {
2127 _ForwardIterator __temp = next(__first);
2128 if (__temp == __last && *__first == '$')
2129 {
2130 __push_r_anchor();
2131 ++__first;
2132 }
2133 }
2134 }
2135 if (__first != __last)
2136 throw regex_error(regex_constants::error_temp);
2137 }
2138 return __first;
2139}
2140
2141template <class _CharT, class _Traits>
2142template <class _ForwardIterator>
2143_ForwardIterator
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002144basic_regex<_CharT, _Traits>::__parse_extended_reg_exp(_ForwardIterator __first,
2145 _ForwardIterator __last)
2146{
2147 while (true)
2148 {
2149 _ForwardIterator __temp = __parse_ERE_branch(__first, __last);
2150 if (__temp == __first)
2151 throw regex_error(regex_constants::error_temp);
2152 __first = __temp;
2153 if (__first == __last)
2154 break;
2155 if (*__first != '|')
2156 throw regex_error(regex_constants::error_temp);
2157 __push_alternation();
2158 ++__first;
2159 }
2160 return __first;
2161}
2162
2163template <class _CharT, class _Traits>
2164template <class _ForwardIterator>
2165_ForwardIterator
2166basic_regex<_CharT, _Traits>::__parse_ERE_branch(_ForwardIterator __first,
2167 _ForwardIterator __last)
2168{
2169 _ForwardIterator __temp = __parse_ERE_expression(__first, __last);
2170 if (__temp == __first)
2171 throw regex_error(regex_constants::error_temp);
2172 do
2173 {
2174 __first = __temp;
2175 __temp = __parse_ERE_expression(__first, __last);
2176 } while (__temp != __first);
2177 return __first;
2178}
2179
2180template <class _CharT, class _Traits>
2181template <class _ForwardIterator>
2182_ForwardIterator
2183basic_regex<_CharT, _Traits>::__parse_ERE_expression(_ForwardIterator __first,
2184 _ForwardIterator __last)
2185{
2186 _ForwardIterator __temp = __parse_one_char_or_coll_elem_ERE(__first, __last);
2187 if (__temp == __first && __temp != __last)
2188 {
2189 switch (*__temp)
2190 {
2191 case '^':
2192 __push_l_anchor();
2193 ++__temp;
2194 break;
2195 case '$':
2196 __push_r_anchor();
2197 ++__temp;
2198 break;
2199 case '(':
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00002200 __push_begin_marked_subexpression();
2201 unsigned __temp_count = __marked_count_;
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002202 ++__open_count_;
2203 __temp = __parse_extended_reg_exp(++__temp, __last);
2204 if (__temp == __last || *__temp != ')')
2205 throw regex_error(regex_constants::error_paren);
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00002206 __push_end_marked_subexpression(__temp_count);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002207 --__open_count_;
2208 ++__temp;
2209 break;
2210 }
2211 }
2212 if (__temp != __first)
2213 __temp = __parse_ERE_dupl_symbol(__temp, __last);
2214 __first = __temp;
2215 return __first;
2216}
2217
2218template <class _CharT, class _Traits>
2219template <class _ForwardIterator>
2220_ForwardIterator
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002221basic_regex<_CharT, _Traits>::__parse_RE_expression(_ForwardIterator __first,
2222 _ForwardIterator __last)
2223{
2224 while (true)
2225 {
2226 _ForwardIterator __temp = __parse_simple_RE(__first, __last);
2227 if (__temp == __first)
2228 break;
2229 __first = __temp;
2230 }
2231 return __first;
2232}
2233
2234template <class _CharT, class _Traits>
2235template <class _ForwardIterator>
2236_ForwardIterator
2237basic_regex<_CharT, _Traits>::__parse_simple_RE(_ForwardIterator __first,
2238 _ForwardIterator __last)
2239{
2240 if (__first != __last)
2241 {
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002242 __owns_one_state<_CharT>* __e = __end_;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00002243 unsigned __mexp_begin = __marked_count_;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002244 _ForwardIterator __temp = __parse_nondupl_RE(__first, __last);
2245 if (__temp != __first)
Howard Hinnante77aa5e2010-07-08 17:43:58 +00002246 __first = __parse_RE_dupl_symbol(__temp, __last, __e,
2247 __mexp_begin+1, __marked_count_+1);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002248 }
2249 return __first;
2250}
2251
2252template <class _CharT, class _Traits>
2253template <class _ForwardIterator>
2254_ForwardIterator
2255basic_regex<_CharT, _Traits>::__parse_nondupl_RE(_ForwardIterator __first,
2256 _ForwardIterator __last)
2257{
2258 _ForwardIterator __temp = __first;
2259 __first = __parse_one_char_or_coll_elem_RE(__first, __last);
2260 if (__temp == __first)
2261 {
2262 __temp = __parse_Back_open_paren(__first, __last);
2263 if (__temp != __first)
2264 {
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00002265 __push_begin_marked_subexpression();
2266 unsigned __temp_count = __marked_count_;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002267 __first = __parse_RE_expression(__temp, __last);
2268 __temp = __parse_Back_close_paren(__first, __last);
2269 if (__temp == __first)
2270 throw regex_error(regex_constants::error_paren);
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00002271 __push_end_marked_subexpression(__temp_count);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002272 __first = __temp;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002273 }
2274 else
2275 __first = __parse_BACKREF(__first, __last);
2276 }
2277 return __first;
2278}
2279
2280template <class _CharT, class _Traits>
2281template <class _ForwardIterator>
2282_ForwardIterator
2283basic_regex<_CharT, _Traits>::__parse_one_char_or_coll_elem_RE(
2284 _ForwardIterator __first,
2285 _ForwardIterator __last)
2286{
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002287 _ForwardIterator __temp = __parse_ORD_CHAR(__first, __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002288 if (__temp == __first)
2289 {
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002290 __temp = __parse_QUOTED_CHAR(__first, __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002291 if (__temp == __first)
2292 {
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002293 if (__temp != __last && *__temp == '.')
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002294 {
2295 __push_match_any();
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002296 ++__temp;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002297 }
2298 else
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002299 __temp = __parse_bracket_expression(__first, __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002300 }
2301 }
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002302 __first = __temp;
2303 return __first;
2304}
2305
2306template <class _CharT, class _Traits>
2307template <class _ForwardIterator>
2308_ForwardIterator
2309basic_regex<_CharT, _Traits>::__parse_one_char_or_coll_elem_ERE(
2310 _ForwardIterator __first,
2311 _ForwardIterator __last)
2312{
2313 _ForwardIterator __temp = __parse_ORD_CHAR_ERE(__first, __last);
2314 if (__temp == __first)
2315 {
2316 __temp = __parse_QUOTED_CHAR_ERE(__first, __last);
2317 if (__temp == __first)
2318 {
2319 if (__temp != __last && *__temp == '.')
2320 {
2321 __push_match_any();
2322 ++__temp;
2323 }
2324 else
2325 __temp = __parse_bracket_expression(__first, __last);
2326 }
2327 }
2328 __first = __temp;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002329 return __first;
2330}
2331
2332template <class _CharT, class _Traits>
2333template <class _ForwardIterator>
2334_ForwardIterator
2335basic_regex<_CharT, _Traits>::__parse_Back_open_paren(_ForwardIterator __first,
2336 _ForwardIterator __last)
2337{
2338 if (__first != __last)
2339 {
2340 _ForwardIterator __temp = next(__first);
2341 if (__temp != __last)
2342 {
2343 if (*__first == '\\' && *__temp == '(')
2344 __first = ++__temp;
2345 }
2346 }
2347 return __first;
2348}
2349
2350template <class _CharT, class _Traits>
2351template <class _ForwardIterator>
2352_ForwardIterator
2353basic_regex<_CharT, _Traits>::__parse_Back_close_paren(_ForwardIterator __first,
2354 _ForwardIterator __last)
2355{
2356 if (__first != __last)
2357 {
2358 _ForwardIterator __temp = next(__first);
2359 if (__temp != __last)
2360 {
2361 if (*__first == '\\' && *__temp == ')')
2362 __first = ++__temp;
2363 }
2364 }
2365 return __first;
2366}
2367
2368template <class _CharT, class _Traits>
2369template <class _ForwardIterator>
2370_ForwardIterator
2371basic_regex<_CharT, _Traits>::__parse_Back_open_brace(_ForwardIterator __first,
2372 _ForwardIterator __last)
2373{
2374 if (__first != __last)
2375 {
2376 _ForwardIterator __temp = next(__first);
2377 if (__temp != __last)
2378 {
2379 if (*__first == '\\' && *__temp == '{')
2380 __first = ++__temp;
2381 }
2382 }
2383 return __first;
2384}
2385
2386template <class _CharT, class _Traits>
2387template <class _ForwardIterator>
2388_ForwardIterator
2389basic_regex<_CharT, _Traits>::__parse_Back_close_brace(_ForwardIterator __first,
2390 _ForwardIterator __last)
2391{
2392 if (__first != __last)
2393 {
2394 _ForwardIterator __temp = next(__first);
2395 if (__temp != __last)
2396 {
2397 if (*__first == '\\' && *__temp == '}')
2398 __first = ++__temp;
2399 }
2400 }
2401 return __first;
2402}
2403
2404template <class _CharT, class _Traits>
2405template <class _ForwardIterator>
2406_ForwardIterator
2407basic_regex<_CharT, _Traits>::__parse_BACKREF(_ForwardIterator __first,
2408 _ForwardIterator __last)
2409{
2410 if (__first != __last)
2411 {
2412 _ForwardIterator __temp = next(__first);
2413 if (__temp != __last)
2414 {
2415 if (*__first == '\\' && '1' <= *__temp && *__temp <= '9')
2416 {
2417 __push_back_ref(*__temp - '0');
2418 __first = ++__temp;
2419 }
2420 }
2421 }
2422 return __first;
2423}
2424
2425template <class _CharT, class _Traits>
2426template <class _ForwardIterator>
2427_ForwardIterator
2428basic_regex<_CharT, _Traits>::__parse_ORD_CHAR(_ForwardIterator __first,
2429 _ForwardIterator __last)
2430{
2431 if (__first != __last)
2432 {
2433 _ForwardIterator __temp = next(__first);
2434 if (__temp == __last && *__first == '$')
2435 return __first;
2436 // Not called inside a bracket
2437 if (*__first == '.' || *__first == '\\' || *__first == '[')
2438 return __first;
Howard Hinnant0de86b62010-06-25 20:56:08 +00002439 __push_char(*__first);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002440 ++__first;
2441 }
2442 return __first;
2443}
2444
2445template <class _CharT, class _Traits>
2446template <class _ForwardIterator>
2447_ForwardIterator
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002448basic_regex<_CharT, _Traits>::__parse_ORD_CHAR_ERE(_ForwardIterator __first,
2449 _ForwardIterator __last)
2450{
2451 if (__first != __last)
2452 {
2453 switch (*__first)
2454 {
2455 case '^':
2456 case '.':
2457 case '[':
2458 case '$':
2459 case '(':
2460 case '|':
2461 case '*':
2462 case '+':
2463 case '?':
2464 case '{':
2465 case '\\':
2466 break;
2467 case ')':
2468 if (__open_count_ == 0)
2469 {
2470 __push_char(*__first);
2471 ++__first;
2472 }
2473 break;
2474 default:
2475 __push_char(*__first);
2476 ++__first;
2477 break;
2478 }
2479 }
2480 return __first;
2481}
2482
2483template <class _CharT, class _Traits>
2484template <class _ForwardIterator>
2485_ForwardIterator
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002486basic_regex<_CharT, _Traits>::__parse_QUOTED_CHAR(_ForwardIterator __first,
2487 _ForwardIterator __last)
2488{
2489 if (__first != __last)
2490 {
2491 _ForwardIterator __temp = next(__first);
2492 if (__temp != __last)
2493 {
2494 if (*__first == '\\')
2495 {
2496 switch (*__temp)
2497 {
2498 case '^':
2499 case '.':
2500 case '*':
2501 case '[':
2502 case '$':
2503 case '\\':
Howard Hinnant0de86b62010-06-25 20:56:08 +00002504 __push_char(*__temp);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002505 __first = ++__temp;
2506 break;
2507 }
2508 }
2509 }
2510 }
2511 return __first;
2512}
2513
2514template <class _CharT, class _Traits>
2515template <class _ForwardIterator>
2516_ForwardIterator
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002517basic_regex<_CharT, _Traits>::__parse_QUOTED_CHAR_ERE(_ForwardIterator __first,
2518 _ForwardIterator __last)
2519{
2520 if (__first != __last)
2521 {
2522 _ForwardIterator __temp = next(__first);
2523 if (__temp != __last)
2524 {
2525 if (*__first == '\\')
2526 {
2527 switch (*__temp)
2528 {
2529 case '^':
2530 case '.':
2531 case '*':
2532 case '[':
2533 case '$':
2534 case '\\':
2535 case '(':
2536 case ')':
2537 case '|':
2538 case '+':
2539 case '?':
2540 case '{':
2541 __push_char(*__temp);
2542 __first = ++__temp;
2543 break;
2544 }
2545 }
2546 }
2547 }
2548 return __first;
2549}
2550
2551template <class _CharT, class _Traits>
2552template <class _ForwardIterator>
2553_ForwardIterator
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002554basic_regex<_CharT, _Traits>::__parse_RE_dupl_symbol(_ForwardIterator __first,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002555 _ForwardIterator __last,
Howard Hinnante77aa5e2010-07-08 17:43:58 +00002556 __owns_one_state<_CharT>* __s,
2557 unsigned __mexp_begin,
2558 unsigned __mexp_end)
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002559{
2560 if (__first != __last)
2561 {
Howard Hinnant0de86b62010-06-25 20:56:08 +00002562 if (*__first == '*')
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002563 {
Howard Hinnante77aa5e2010-07-08 17:43:58 +00002564 __push_greedy_inf_repeat(0, __s, __mexp_begin, __mexp_end);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002565 ++__first;
2566 }
2567 else
2568 {
2569 _ForwardIterator __temp = __parse_Back_open_brace(__first, __last);
2570 if (__temp != __first)
2571 {
2572 int __min = 0;
2573 __first = __temp;
2574 __temp = __parse_DUP_COUNT(__first, __last, __min);
2575 if (__temp == __first)
2576 throw regex_error(regex_constants::error_badbrace);
2577 __first = __temp;
2578 if (__first == __last)
2579 throw regex_error(regex_constants::error_brace);
2580 if (*__first != ',')
2581 {
2582 __temp = __parse_Back_close_brace(__first, __last);
2583 if (__temp == __first)
2584 throw regex_error(regex_constants::error_brace);
2585 __push_exact_repeat(__min);
2586 __first = __temp;
2587 }
2588 else
2589 {
2590 ++__first; // consume ','
2591 int __max = -1;
2592 __first = __parse_DUP_COUNT(__first, __last, __max);
2593 __temp = __parse_Back_close_brace(__first, __last);
2594 if (__temp == __first)
2595 throw regex_error(regex_constants::error_brace);
2596 if (__max == -1)
Howard Hinnante77aa5e2010-07-08 17:43:58 +00002597 __push_greedy_inf_repeat(__min, __s, __mexp_end, __mexp_end);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002598 else
2599 {
2600 if (__max < __min)
2601 throw regex_error(regex_constants::error_badbrace);
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002602 __push_loop(__min, __max, __s);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002603 }
2604 __first = __temp;
2605 }
2606 }
2607 }
2608 }
2609 return __first;
2610}
2611
Howard Hinnant0de86b62010-06-25 20:56:08 +00002612template <class _CharT, class _Traits>
2613template <class _ForwardIterator>
2614_ForwardIterator
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002615basic_regex<_CharT, _Traits>::__parse_ERE_dupl_symbol(_ForwardIterator __first,
2616 _ForwardIterator __last)
2617{
2618 if (__first != __last)
2619 {
2620 switch (*__first)
2621 {
2622 case '*':
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002623 __push_greedy_inf_repeat(0, nullptr);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002624 ++__first;
2625 break;
2626 case '+':
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002627 __push_greedy_inf_repeat(1, nullptr);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002628 ++__first;
2629 break;
2630 case '?':
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002631 __push_loop(0, 1, nullptr);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002632 ++__first;
2633 break;
2634 case '{':
2635 {
2636 int __min;
2637 _ForwardIterator __temp = __parse_DUP_COUNT(__first, __last, __min);
2638 if (__temp == __first)
2639 throw regex_error(regex_constants::error_badbrace);
2640 __first = __temp;
2641 if (__first == __last)
2642 throw regex_error(regex_constants::error_brace);
2643 switch (*__first)
2644 {
2645 case '}':
2646 __push_exact_repeat(__min);
2647 ++__first;
2648 break;
2649 case ',':
2650 if (++__first == __last)
2651 throw regex_error(regex_constants::error_badbrace);
2652 if (*__first == '}')
2653 {
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002654 __push_greedy_inf_repeat(__min, nullptr);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002655 ++__first;
2656 }
2657 else
2658 {
2659 int __max;
2660 __temp = __parse_DUP_COUNT(__first, __last, __max);
2661 if (__temp == __first)
2662 throw regex_error(regex_constants::error_brace);
2663 __first = __temp;
2664 if (__first == __last || *__first != '}')
2665 throw regex_error(regex_constants::error_brace);
2666 ++__first;
2667 if (__max < __min)
2668 throw regex_error(regex_constants::error_badbrace);
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002669 __push_loop(__min, __max, nullptr);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002670 }
2671 default:
2672 throw regex_error(regex_constants::error_badbrace);
2673 }
2674 }
2675 break;
2676 }
2677 }
2678 return __first;
2679}
2680
2681template <class _CharT, class _Traits>
2682template <class _ForwardIterator>
2683_ForwardIterator
Howard Hinnant0de86b62010-06-25 20:56:08 +00002684basic_regex<_CharT, _Traits>::__parse_bracket_expression(_ForwardIterator __first,
2685 _ForwardIterator __last)
2686{
2687 if (__first != __last && *__first == '[')
2688 {
2689 if (++__first == __last)
2690 throw regex_error(regex_constants::error_brack);
2691 bool __non_matching = false;
2692 if (*__first == '^')
2693 {
2694 ++__first;
2695 __non_matching = true;
2696 __start_nonmatching_list();
2697 }
2698 else
2699 __start_matching_list();
2700 if (__first == __last)
2701 throw regex_error(regex_constants::error_brack);
2702 if (*__first == ']')
2703 {
2704 __push_char(']');
2705 ++__first;
2706 }
2707 __first = __parse_follow_list(__first, __last);
2708 if (__first == __last)
2709 throw regex_error(regex_constants::error_brack);
2710 if (*__first == '-')
2711 {
2712 __push_char('-');
2713 ++__first;
2714 }
2715 if (__first == __last || *__first != ']')
2716 throw regex_error(regex_constants::error_brack);
2717 if (__non_matching)
2718 __end_nonmatching_list();
2719 else
2720 __end_matching_list();
2721 ++__first;
2722 }
2723 return __first;
2724}
2725
2726template <class _CharT, class _Traits>
2727template <class _ForwardIterator>
2728_ForwardIterator
2729basic_regex<_CharT, _Traits>::__parse_follow_list(_ForwardIterator __first,
2730 _ForwardIterator __last)
2731{
2732 if (__first != __last)
2733 {
2734 while (true)
2735 {
2736 _ForwardIterator __temp = __parse_expression_term(__first, __last);
2737 if (__temp == __first)
2738 break;
2739 __first = __temp;
2740 }
2741 }
2742 return __first;
2743}
2744
2745template <class _CharT, class _Traits>
2746template <class _ForwardIterator>
2747_ForwardIterator
2748basic_regex<_CharT, _Traits>::__parse_expression_term(_ForwardIterator __first,
2749 _ForwardIterator __last)
2750{
2751 if (__first != __last && *__first != ']')
2752 {
2753 bool __parsed_one = false;
2754 _ForwardIterator __temp = next(__first);
2755 if (__temp != __last && *__first == '[')
2756 {
2757 if (*__temp == '=')
2758 return __parse_equivalence_class(++__temp, __last);
2759 else if (*__temp == ':')
2760 return __parse_character_class(++__temp, __last);
2761 else if (*__temp == '.')
2762 {
2763 __first = __parse_collating_symbol(++__temp, __last);
2764 __parsed_one = true;
2765 }
2766 }
2767 if (!__parsed_one)
2768 {
2769 __push_char(*__first);
2770 ++__first;
2771 }
2772 if (__first != __last && *__first != ']')
2773 {
2774 __temp = next(__first);
2775 if (__temp != __last && *__first == '-' && *__temp != ']')
2776 {
2777 // parse a range
2778 __first = __temp;
2779 ++__temp;
2780 if (__temp != __last && *__first == '[' && *__temp == '.')
2781 __first = __parse_collating_symbol(++__temp, __last);
2782 else
2783 {
2784 __push_char(*__first);
2785 ++__first;
2786 }
2787 __push_range();
2788 }
2789 }
2790 }
2791 return __first;
2792}
2793
2794template <class _CharT, class _Traits>
2795template <class _ForwardIterator>
2796_ForwardIterator
2797basic_regex<_CharT, _Traits>::__parse_equivalence_class(_ForwardIterator __first,
2798 _ForwardIterator __last)
2799{
2800 // Found [=
2801 // This means =] must exist
2802 value_type _Equal_close[2] = {'=', ']'};
2803 _ForwardIterator __temp = _STD::search(__first, __last, _Equal_close,
2804 _Equal_close+2);
2805 if (__temp == __last)
2806 throw regex_error(regex_constants::error_brack);
2807 // [__first, __temp) contains all text in [= ... =]
2808 typedef typename _Traits::string_type string_type;
2809 string_type __collate_name =
2810 __traits_.lookup_collatename(__first, __temp);
2811 if (__collate_name.empty())
2812 throw regex_error(regex_constants::error_brack);
2813 string_type __equiv_name =
2814 __traits_.transform_primary(__collate_name.begin(),
2815 __collate_name.end());
2816 if (!__equiv_name.empty())
2817 __push_char(__equiv_name);
2818 else
2819 __push_char(__collate_name);
2820 __first = next(__temp, 2);
2821 return __first;
2822}
2823
2824template <class _CharT, class _Traits>
2825template <class _ForwardIterator>
2826_ForwardIterator
2827basic_regex<_CharT, _Traits>::__parse_character_class(_ForwardIterator __first,
2828 _ForwardIterator __last)
2829{
2830 // Found [:
2831 // This means :] must exist
2832 value_type _Colon_close[2] = {':', ']'};
2833 _ForwardIterator __temp = _STD::search(__first, __last, _Colon_close,
2834 _Colon_close+2);
2835 if (__temp == __last)
2836 throw regex_error(regex_constants::error_brack);
2837 // [__first, __temp) contains all text in [: ... :]
2838 typedef typename _Traits::char_class_type char_class_type;
2839 char_class_type __class_type =
2840 __traits_.lookup_classname(__first, __temp, __flags_ & icase);
2841 if (__class_type == 0)
2842 throw regex_error(regex_constants::error_brack);
2843 __push_class_type(__class_type);
2844 __first = next(__temp, 2);
2845 return __first;
2846}
2847
2848template <class _CharT, class _Traits>
2849template <class _ForwardIterator>
2850_ForwardIterator
2851basic_regex<_CharT, _Traits>::__parse_collating_symbol(_ForwardIterator __first,
2852 _ForwardIterator __last)
2853{
2854 // Found [.
2855 // This means .] must exist
2856 value_type _Dot_close[2] = {'.', ']'};
2857 _ForwardIterator __temp = _STD::search(__first, __last, _Dot_close,
2858 _Dot_close+2);
2859 if (__temp == __last)
2860 throw regex_error(regex_constants::error_brack);
2861 // [__first, __temp) contains all text in [. ... .]
2862 typedef typename _Traits::string_type string_type;
2863 string_type __collate_name =
2864 __traits_.lookup_collatename(__first, __temp);
2865 if (__collate_name.empty())
2866 throw regex_error(regex_constants::error_brack);
2867 __push_char(__collate_name);
2868 __first = next(__temp, 2);
2869 return __first;
2870}
2871
2872template <class _CharT, class _Traits>
2873template <class _ForwardIterator>
2874_ForwardIterator
2875basic_regex<_CharT, _Traits>::__parse_DUP_COUNT(_ForwardIterator __first,
2876 _ForwardIterator __last,
2877 int& __c)
2878{
2879 if (__first != __last && '0' <= *__first && *__first <= '9')
2880 {
2881 __c = *__first - '0';
2882 for (++__first; __first != __last && '0' <= *__first && *__first <= '9';
2883 ++__first)
2884 {
2885 __c *= 10;
2886 __c += *__first - '0';
2887 }
2888 }
2889 return __first;
2890}
2891
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002892template <class _CharT, class _Traits>
2893void
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002894basic_regex<_CharT, _Traits>::__push_loop(size_t __min, size_t __max,
2895 __owns_one_state<_CharT>* __s, size_t __mexp_begin, size_t __mexp_end,
2896 bool __greedy)
2897{
2898 unique_ptr<__empty_state<_CharT> > __e1(new __empty_state<_CharT>(__end_->first()));
2899 __end_->first() = nullptr;
2900 unique_ptr<__loop<_CharT> > __e2;
2901 if (__mexp_begin != __mexp_end)
2902 {
2903 unique_ptr<__zero_marked_exprs<_CharT> >
2904 __e3(new __zero_marked_exprs<_CharT>(__mexp_begin, __mexp_end,
2905 __s->first()));
2906 __s->first() = nullptr;
2907 __e2.reset(new __loop<_CharT>(__loop_count_, __e3.get(), __e1.get(),
2908 __greedy, __min, __max));
2909 __e3.release();
2910 __e1.release();
2911 }
2912 else
2913 {
2914 __e2.reset(new __loop<_CharT>(__loop_count_, __s->first(), __e1.get(),
2915 __greedy, __min, __max));
2916 __s->first() = nullptr;
2917 __e1.release();
2918 }
2919 __end_->first() = new __increment_loop_count<_CharT>(__loop_count_, __e2.get());
2920 __end_ = __e2->second();
2921 __s->first() = new __zero_loop_count<_CharT>(__loop_count_, __e2.get());
2922 __e2.release();
2923 ++__loop_count_;
2924}
2925
2926template <class _CharT, class _Traits>
2927void
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002928basic_regex<_CharT, _Traits>::__push_char(value_type __c)
2929{
Howard Hinnante77aa5e2010-07-08 17:43:58 +00002930 __end_->first() = new __match_char<_CharT>(__c, __end_->first());
2931 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002932}
2933
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00002934template <class _CharT, class _Traits>
2935void
2936basic_regex<_CharT, _Traits>::__push_begin_marked_subexpression()
2937{
Howard Hinnante77aa5e2010-07-08 17:43:58 +00002938 __end_->first() = new __begin_marked_subexpression<_CharT>(++__marked_count_,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002939 __end_->first());
Howard Hinnante77aa5e2010-07-08 17:43:58 +00002940 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00002941}
2942
2943template <class _CharT, class _Traits>
2944void
2945basic_regex<_CharT, _Traits>::__push_end_marked_subexpression(unsigned __sub)
2946{
Howard Hinnante77aa5e2010-07-08 17:43:58 +00002947 __end_->first() = new __end_marked_subexpression<_CharT>(__sub,
2948 __end_->first());
2949 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00002950}
2951
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00002952template <class _CharT, class _Traits>
2953void
2954basic_regex<_CharT, _Traits>::__push_r_anchor()
2955{
2956 __end_->first() = new __r_anchor<_CharT>(__end_->first());
2957 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
2958}
2959
2960
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002961typedef basic_regex<char> regex;
2962typedef basic_regex<wchar_t> wregex;
2963
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002964// sub_match
2965
2966template <class _BidirectionalIterator>
2967class sub_match
2968 : public pair<_BidirectionalIterator, _BidirectionalIterator>
2969{
2970public:
2971 typedef _BidirectionalIterator iterator;
2972 typedef typename iterator_traits<iterator>::value_type value_type;
2973 typedef typename iterator_traits<iterator>::difference_type difference_type;
2974 typedef basic_string<value_type> string_type;
2975
2976 bool matched;
2977
2978 difference_type length() const
2979 {return matched ? _STD::distance(this->first, this->second) : 0;}
2980 string_type str() const
2981 {return matched ? string_type(this->first, this->second) : string_type();}
2982 operator string_type() const
2983 {return str();}
2984
2985 int compare(const sub_match& __s) const
2986 {return str().compare(__s.str());}
2987 int compare(const string_type& __s) const
2988 {return str().compare(__s);}
2989 int compare(const value_type* __s) const
2990 {return str().compare(__s);}
2991};
2992
2993typedef sub_match<const char*> csub_match;
2994typedef sub_match<const wchar_t*> wcsub_match;
2995typedef sub_match<string::const_iterator> ssub_match;
2996typedef sub_match<wstring::const_iterator> wssub_match;
2997
2998template <class _BiIter>
2999inline _LIBCPP_INLINE_VISIBILITY
3000bool
3001operator==(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
3002{
3003 return __x.compare(__y) == 0;
3004}
3005
3006template <class _BiIter>
3007inline _LIBCPP_INLINE_VISIBILITY
3008bool
3009operator!=(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
3010{
3011 return !(__x == __y);
3012}
3013
3014template <class _BiIter>
3015inline _LIBCPP_INLINE_VISIBILITY
3016bool
3017operator<(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
3018{
3019 return __x.compare(__y) < 0;
3020}
3021
3022template <class _BiIter>
3023inline _LIBCPP_INLINE_VISIBILITY
3024bool
3025operator<=(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
3026{
3027 return !(__y < __x);
3028}
3029
3030template <class _BiIter>
3031inline _LIBCPP_INLINE_VISIBILITY
3032bool
3033operator>=(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
3034{
3035 return !(__x < __y);
3036}
3037
3038template <class _BiIter>
3039inline _LIBCPP_INLINE_VISIBILITY
3040bool
3041operator>(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
3042{
3043 return __y < __x;
3044}
3045
3046template <class _BiIter, class _ST, class _SA>
3047inline _LIBCPP_INLINE_VISIBILITY
3048bool
3049operator==(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
3050 const sub_match<_BiIter>& __y)
3051{
3052 return __y.compare(__x.c_str()) == 0;
3053}
3054
3055template <class _BiIter, class _ST, class _SA>
3056inline _LIBCPP_INLINE_VISIBILITY
3057bool
3058operator!=(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
3059 const sub_match<_BiIter>& __y)
3060{
3061 return !(__x == __y);
3062}
3063
3064template <class _BiIter, class _ST, class _SA>
3065inline _LIBCPP_INLINE_VISIBILITY
3066bool
3067operator<(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
3068 const sub_match<_BiIter>& __y)
3069{
3070 return __y.compare(__x.c_str()) > 0;
3071}
3072
3073template <class _BiIter, class _ST, class _SA>
3074inline _LIBCPP_INLINE_VISIBILITY
3075bool
3076operator>(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
3077 const sub_match<_BiIter>& __y)
3078{
3079 return __y < __x;
3080}
3081
3082template <class _BiIter, class _ST, class _SA>
3083inline _LIBCPP_INLINE_VISIBILITY
3084bool operator>=(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
3085 const sub_match<_BiIter>& __y)
3086{
3087 return !(__x < __y);
3088}
3089
3090template <class _BiIter, class _ST, class _SA>
3091inline _LIBCPP_INLINE_VISIBILITY
3092bool
3093operator<=(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
3094 const sub_match<_BiIter>& __y)
3095{
3096 return !(__y < __x);
3097}
3098
3099template <class _BiIter, class _ST, class _SA>
3100inline _LIBCPP_INLINE_VISIBILITY
3101bool
3102operator==(const sub_match<_BiIter>& __x,
3103 const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
3104{
3105 return __x.compare(__y.c_str()) == 0;
3106}
3107
3108template <class _BiIter, class _ST, class _SA>
3109inline _LIBCPP_INLINE_VISIBILITY
3110bool
3111operator!=(const sub_match<_BiIter>& __x,
3112 const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
3113{
3114 return !(__x == __y);
3115}
3116
3117template <class _BiIter, class _ST, class _SA>
3118inline _LIBCPP_INLINE_VISIBILITY
3119bool
3120operator<(const sub_match<_BiIter>& __x,
3121 const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
3122{
3123 return __x.compare(__y.c_str()) < 0;
3124}
3125
3126template <class _BiIter, class _ST, class _SA>
3127inline _LIBCPP_INLINE_VISIBILITY
3128bool operator>(const sub_match<_BiIter>& __x,
3129 const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
3130{
3131 return __y < __x;
3132}
3133
3134template <class _BiIter, class _ST, class _SA>
3135inline _LIBCPP_INLINE_VISIBILITY
3136bool
3137operator>=(const sub_match<_BiIter>& __x,
3138 const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
3139{
3140 return !(__x < __y);
3141}
3142
3143template <class _BiIter, class _ST, class _SA>
3144inline _LIBCPP_INLINE_VISIBILITY
3145bool
3146operator<=(const sub_match<_BiIter>& __x,
3147 const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
3148{
3149 return !(__y < __x);
3150}
3151
3152template <class _BiIter>
3153inline _LIBCPP_INLINE_VISIBILITY
3154bool
3155operator==(typename iterator_traits<_BiIter>::value_type const* __x,
3156 const sub_match<_BiIter>& __y)
3157{
3158 return __y.compare(__x) == 0;
3159}
3160
3161template <class _BiIter>
3162inline _LIBCPP_INLINE_VISIBILITY
3163bool
3164operator!=(typename iterator_traits<_BiIter>::value_type const* __x,
3165 const sub_match<_BiIter>& __y)
3166{
3167 return !(__x == __y);
3168}
3169
3170template <class _BiIter>
3171inline _LIBCPP_INLINE_VISIBILITY
3172bool
3173operator<(typename iterator_traits<_BiIter>::value_type const* __x,
3174 const sub_match<_BiIter>& __y)
3175{
3176 return __y.compare(__x) > 0;
3177}
3178
3179template <class _BiIter>
3180inline _LIBCPP_INLINE_VISIBILITY
3181bool
3182operator>(typename iterator_traits<_BiIter>::value_type const* __x,
3183 const sub_match<_BiIter>& __y)
3184{
3185 return __y < __x;
3186}
3187
3188template <class _BiIter>
3189inline _LIBCPP_INLINE_VISIBILITY
3190bool
3191operator>=(typename iterator_traits<_BiIter>::value_type const* __x,
3192 const sub_match<_BiIter>& __y)
3193{
3194 return !(__x < __y);
3195}
3196
3197template <class _BiIter>
3198inline _LIBCPP_INLINE_VISIBILITY
3199bool
3200operator<=(typename iterator_traits<_BiIter>::value_type const* __x,
3201 const sub_match<_BiIter>& __y)
3202{
3203 return !(__y < __x);
3204}
3205
3206template <class _BiIter>
3207inline _LIBCPP_INLINE_VISIBILITY
3208bool
3209operator==(const sub_match<_BiIter>& __x,
3210 typename iterator_traits<_BiIter>::value_type const* __y)
3211{
3212 return __x.compare(__y) == 0;
3213}
3214
3215template <class _BiIter>
3216inline _LIBCPP_INLINE_VISIBILITY
3217bool
3218operator!=(const sub_match<_BiIter>& __x,
3219 typename iterator_traits<_BiIter>::value_type const* __y)
3220{
3221 return !(__x == __y);
3222}
3223
3224template <class _BiIter>
3225inline _LIBCPP_INLINE_VISIBILITY
3226bool
3227operator<(const sub_match<_BiIter>& __x,
3228 typename iterator_traits<_BiIter>::value_type const* __y)
3229{
3230 return __x.compare(__y) < 0;
3231}
3232
3233template <class _BiIter>
3234inline _LIBCPP_INLINE_VISIBILITY
3235bool
3236operator>(const sub_match<_BiIter>& __x,
3237 typename iterator_traits<_BiIter>::value_type const* __y)
3238{
3239 return __y < __x;
3240}
3241
3242template <class _BiIter>
3243inline _LIBCPP_INLINE_VISIBILITY
3244bool
3245operator>=(const sub_match<_BiIter>& __x,
3246 typename iterator_traits<_BiIter>::value_type const* __y)
3247{
3248 return !(__x < __y);
3249}
3250
3251template <class _BiIter>
3252inline _LIBCPP_INLINE_VISIBILITY
3253bool
3254operator<=(const sub_match<_BiIter>& __x,
3255 typename iterator_traits<_BiIter>::value_type const* __y)
3256{
3257 return !(__y < __x);
3258}
3259
3260template <class _BiIter>
3261inline _LIBCPP_INLINE_VISIBILITY
3262bool
3263operator==(typename iterator_traits<_BiIter>::value_type const& __x,
3264 const sub_match<_BiIter>& __y)
3265{
3266 typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
3267 return __y.compare(string_type(1, __x)) == 0;
3268}
3269
3270template <class _BiIter>
3271inline _LIBCPP_INLINE_VISIBILITY
3272bool
3273operator!=(typename iterator_traits<_BiIter>::value_type const& __x,
3274 const sub_match<_BiIter>& __y)
3275{
3276 return !(__x == __y);
3277}
3278
3279template <class _BiIter>
3280inline _LIBCPP_INLINE_VISIBILITY
3281bool
3282operator<(typename iterator_traits<_BiIter>::value_type const& __x,
3283 const sub_match<_BiIter>& __y)
3284{
3285 typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
3286 return __y.compare(string_type(1, __x)) > 0;
3287}
3288
3289template <class _BiIter>
3290inline _LIBCPP_INLINE_VISIBILITY
3291bool
3292operator>(typename iterator_traits<_BiIter>::value_type const& __x,
3293 const sub_match<_BiIter>& __y)
3294{
3295 return __y < __x;
3296}
3297
3298template <class _BiIter>
3299inline _LIBCPP_INLINE_VISIBILITY
3300bool
3301operator>=(typename iterator_traits<_BiIter>::value_type const& __x,
3302 const sub_match<_BiIter>& __y)
3303{
3304 return !(__x < __y);
3305}
3306
3307template <class _BiIter>
3308inline _LIBCPP_INLINE_VISIBILITY
3309bool
3310operator<=(typename iterator_traits<_BiIter>::value_type const& __x,
3311 const sub_match<_BiIter>& __y)
3312{
3313 return !(__y < __x);
3314}
3315
3316template <class _BiIter>
3317inline _LIBCPP_INLINE_VISIBILITY
3318bool
3319operator==(const sub_match<_BiIter>& __x,
3320 typename iterator_traits<_BiIter>::value_type const& __y)
3321{
3322 typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
3323 return __x.compare(string_type(1, __y)) == 0;
3324}
3325
3326template <class _BiIter>
3327inline _LIBCPP_INLINE_VISIBILITY
3328bool
3329operator!=(const sub_match<_BiIter>& __x,
3330 typename iterator_traits<_BiIter>::value_type const& __y)
3331{
3332 return !(__x == __y);
3333}
3334
3335template <class _BiIter>
3336inline _LIBCPP_INLINE_VISIBILITY
3337bool
3338operator<(const sub_match<_BiIter>& __x,
3339 typename iterator_traits<_BiIter>::value_type const& __y)
3340{
3341 typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
3342 return __x.compare(string_type(1, __y)) < 0;
3343}
3344
3345template <class _BiIter>
3346inline _LIBCPP_INLINE_VISIBILITY
3347bool
3348operator>(const sub_match<_BiIter>& __x,
3349 typename iterator_traits<_BiIter>::value_type const& __y)
3350{
3351 return __y < __x;
3352}
3353
3354template <class _BiIter>
3355inline _LIBCPP_INLINE_VISIBILITY
3356bool
3357operator>=(const sub_match<_BiIter>& __x,
3358 typename iterator_traits<_BiIter>::value_type const& __y)
3359{
3360 return !(__x < __y);
3361}
3362
3363template <class _BiIter>
3364inline _LIBCPP_INLINE_VISIBILITY
3365bool
3366operator<=(const sub_match<_BiIter>& __x,
3367 typename iterator_traits<_BiIter>::value_type const& __y)
3368{
3369 return !(__y < __x);
3370}
3371
3372template <class _CharT, class _ST, class _BiIter>
3373inline _LIBCPP_INLINE_VISIBILITY
3374basic_ostream<_CharT, _ST>&
3375operator<<(basic_ostream<_CharT, _ST>& __os, const sub_match<_BiIter>& __m)
3376{
3377 return __os << __m.str();
3378}
3379
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00003380template <class _BidirectionalIterator,
3381 class _Allocator = allocator<sub_match<_BidirectionalIterator> > >
3382class match_results
3383{
3384public:
3385 typedef _Allocator allocator_type;
3386 typedef sub_match<_BidirectionalIterator> value_type;
3387private:
3388 typedef vector<value_type, allocator_type> __container_type;
3389
3390 __container_type __matches_;
3391 value_type __unmatched_;
3392 value_type __prefix_;
3393 value_type __suffix_;
3394public:
3395 typedef const value_type& const_reference;
3396 typedef const_reference reference;
3397 typedef typename __container_type::const_iterator const_iterator;
3398 typedef const_iterator iterator;
3399 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
3400 typedef typename allocator_traits<allocator_type>::size_type size_type;
3401 typedef typename iterator_traits<_BidirectionalIterator>::value_type char_type;
3402 typedef basic_string<char_type> string_type;
3403
3404 // construct/copy/destroy:
3405 explicit match_results(const allocator_type& __a = allocator_type());
3406// match_results(const match_results&) = default;
3407// match_results& operator=(const match_results&) = default;
3408#ifdef _LIBCPP_MOVE
3409// match_results(match_results&& __m) = default;
3410// match_results& operator=(match_results&& __m) = default;
3411#endif
3412// ~match_results() = default;
3413
3414 // size:
3415 size_type size() const {return __matches_.size();}
3416 size_type max_size() const {return __matches_.max_size();}
3417 bool empty() const {return size() == 0;}
3418
3419 // element access:
3420 difference_type length(size_type __sub = 0) const
3421 {return (*this)[__sub].length();}
3422 difference_type position(size_type __sub = 0) const
3423 {return _STD::distance(__prefix_.first, (*this)[__sub].first);}
3424 string_type str(size_type __sub = 0) const
3425 {return (*this)[__sub].str();}
3426 const_reference operator[](size_type __n) const
3427 {return __n < __matches_.size() ? __matches_[__n] : __unmatched_;}
3428
3429 const_reference prefix() const {return __prefix_;}
3430 const_reference suffix() const {return __suffix_;}
3431
3432 const_iterator begin() const {return empty() ? __matches_.end() : __matches_.begin() + 1;}
3433 const_iterator end() const {return __matches_.end();}
3434 const_iterator cbegin() const {return empty() ? __matches_.end() : __matches_.begin() + 1;}
3435 const_iterator cend() const {return __matches_.end();}
3436
3437 // format:
3438 template <class _OutputIter>
3439 _OutputIter
3440 format(_OutputIter __out, const char_type* __fmt_first,
3441 const char_type* __fmt_last,
3442 regex_constants::match_flag_type __flags = regex_constants::format_default) const;
3443 template <class _OutputIter, class _ST, class _SA>
3444 _OutputIter
3445 format(_OutputIter __out, const basic_string<char_type, _ST, _SA>& __fmt,
3446 regex_constants::match_flag_type __flags = regex_constants::format_default) const;
3447 template <class _ST, class _SA>
3448 basic_string<char_type, _ST, _SA>
3449 format(const basic_string<char_type, _ST, _SA>& __fmt,
3450 regex_constants::match_flag_type __flags = regex_constants::format_default) const;
3451 string_type
3452 format(const char_type* __fmt,
3453 regex_constants::match_flag_type __flags = regex_constants::format_default) const;
3454
3455 // allocator:
3456 allocator_type get_allocator() const {return __matches_.get_allocator();}
3457
3458 // swap:
3459 void swap(match_results& __m);
3460
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00003461private:
3462 void __init(unsigned __s,
3463 _BidirectionalIterator __f, _BidirectionalIterator __l);
3464
3465 template <class, class> friend class basic_regex;
3466
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00003467 template <class _B, class _A, class _C, class _T>
3468 friend
3469 bool
3470 regex_match(_B, _B, match_results<_B, _A>&, const basic_regex<_C, _T>&,
3471 regex_constants::match_flag_type);
3472};
3473
3474template <class _BidirectionalIterator, class _Allocator>
3475match_results<_BidirectionalIterator, _Allocator>::match_results(
3476 const allocator_type& __a)
3477 : __matches_(__a),
3478 __unmatched_(),
3479 __prefix_(),
3480 __suffix_()
3481{
3482}
3483
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00003484template <class _BidirectionalIterator, class _Allocator>
3485void
3486match_results<_BidirectionalIterator, _Allocator>::__init(unsigned __s,
3487 _BidirectionalIterator __f, _BidirectionalIterator __l)
3488{
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00003489 __unmatched_.first = __l;
3490 __unmatched_.second = __l;
3491 __unmatched_.matched = false;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003492 __matches_.assign(__s, __unmatched_);
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00003493 __prefix_.first = __f;
3494 __prefix_.second = __f;
3495 __prefix_.matched = false;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003496 __suffix_ = __unmatched_;
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00003497}
3498
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00003499typedef match_results<const char*> cmatch;
3500typedef match_results<const wchar_t*> wcmatch;
3501typedef match_results<string::const_iterator> smatch;
3502typedef match_results<wstring::const_iterator> wsmatch;
3503
3504template <class _BidirectionalIterator, class _Allocator>
3505 bool
3506 operator==(const match_results<_BidirectionalIterator, _Allocator>& __x,
3507 const match_results<_BidirectionalIterator, _Allocator>& __y);
3508
3509template <class _BidirectionalIterator, class _Allocator>
3510 bool
3511 operator!=(const match_results<_BidirectionalIterator, _Allocator>& __x,
3512 const match_results<_BidirectionalIterator, _Allocator>& __y);
3513
3514template <class _BidirectionalIterator, class _Allocator>
3515 void
3516 swap(match_results<_BidirectionalIterator, _Allocator>& __x,
3517 match_results<_BidirectionalIterator, _Allocator>& __y);
3518
3519// regex_search
3520
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00003521template <class _CharT, class _Traits>
3522template <class _BidirectionalIterator, class _Allocator>
3523bool
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003524basic_regex<_CharT, _Traits>::__match_at_start_ecma(
3525 _BidirectionalIterator __first, _BidirectionalIterator __last,
3526 match_results<_BidirectionalIterator, _Allocator>& __m,
3527 regex_constants::match_flag_type __flags) const
3528{
3529 return false;
3530}
3531
3532template <class _CharT, class _Traits>
3533template <class _BidirectionalIterator, class _Allocator>
3534bool
3535basic_regex<_CharT, _Traits>::__match_at_start_posix_nosubs(
3536 const _CharT* __first, const _CharT* __last,
3537 match_results<_BidirectionalIterator, _Allocator>& __m,
3538 vector<size_t>& __lc,
3539 regex_constants::match_flag_type __flags) const
3540{
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003541 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
3542 __split_buffer<__command> __commands;
3543 difference_type __j = 0;
3544 difference_type __highest_j = 0;
3545 difference_type _N = _STD::distance(__first, __last);
3546 __state* __st = __start_.get();
3547 if (__st)
3548 {
3549 __commands.push_front(__command(__st));
3550 __commands.push_front(__command(__command::__consume_input));
3551 _BidirectionalIterator __current = __first;
3552 do
3553 {
3554 __command __cmd = __commands.back();
3555 __commands.pop_back();
3556 if (__cmd.first != nullptr)
3557 __cmd = __cmd.first->__test(__first, __current, __last, __lc,
3558 __m.__matches_.data(), __flags);
3559 switch (__cmd.__do_)
3560 {
3561 case __command::__end_state:
3562 __highest_j = _STD::max(__highest_j, __j);
3563 break;
3564 case __command::__consume_input:
3565 if (__j == _N)
3566 return false;
3567 ++__current;
3568 if (++__j != _N && !__commands.empty())
3569 __commands.push_front(__command(__command::__consume_input));
3570 break;
3571 case __command::__accept_and_consume:
3572 __commands.push_front(__command(__cmd.first));
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003573 break;
3574 case __command::__accept_but_not_consume:
3575 __commands.push_back(__command(__cmd.first));
3576 if (__cmd.second != nullptr)
3577 __commands.push_back(__command(__cmd.second));
3578 break;
3579 case __command::__reject:
3580 break;
3581 default:
3582 throw regex_error(regex_constants::error_temp);
3583 break;
3584 }
3585 } while (!__commands.empty());
3586 if (__highest_j != 0)
3587 {
3588 __m.__matches_[0].first = __first;
3589 __m.__matches_[0].second = _STD::next(__first, __highest_j);
3590 __m.__matches_[0].matched = true;
3591 return true;
3592 }
3593 }
3594 return false;
3595}
3596
3597template <class _CharT, class _Traits>
3598template <class _BidirectionalIterator, class _Allocator>
3599bool
3600basic_regex<_CharT, _Traits>::__match_at_start_posix_subs(
3601 _BidirectionalIterator __first, _BidirectionalIterator __last,
3602 match_results<_BidirectionalIterator, _Allocator>& __m,
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003603 vector<size_t>& __lc,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003604 regex_constants::match_flag_type __flags) const
3605{
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003606 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
3607 vector<__command> __commands;
3608 vector<_BidirectionalIterator> __current_stack;
3609 vector<sub_match<_BidirectionalIterator> > __saved_matches;
3610 vector<sub_match<_BidirectionalIterator> > __best_matches;
3611 difference_type __j = 0;
3612 difference_type __highest_j = 0;
3613 difference_type _N = _STD::distance(__first, __last);
3614 __state* __st = __start_.get();
3615 if (__st)
3616 {
3617 __commands.push_back(__command(__st));
3618 _BidirectionalIterator __current = __first;
3619 do
3620 {
3621 __command __cmd = __commands.back();
3622 __commands.pop_back();
3623 if (__cmd.first != nullptr)
3624 __cmd = __cmd.first->__test(__first, __current, __last, __lc,
3625 __m.__matches_.data(), __flags);
3626 switch (__cmd.__do_)
3627 {
3628 case __command::__end_state:
3629 if (__highest_j < __j)
3630 {
3631 __highest_j = __j;
3632 for (unsigned __i = 1; __i < __m.__matches_.size(); ++__i)
3633 __best_matches.push_back(__m.__matches_[__i]);
3634 }
3635 break;
3636 case __command::__pop_state:
3637 for (unsigned __i = __m.__matches_.size(); __i > 1;)
3638 {
3639 assert(!__saved_matches.empty());
3640 __m.__matches_[--__i] = __saved_matches.back();
3641 __saved_matches.pop_back();
3642 }
3643 assert(!__current_stack.empty());
3644 __current = __current_stack.back();
3645 __current_stack.pop_back();
3646 break;
3647 case __command::__accept_and_consume:
3648 __commands.push_back(__command(__cmd.first));
3649 if (__current != __last)
3650 {
3651 ++__current;
3652 ++__j;
3653 }
3654 break;
3655 case __command::__accept_but_not_consume:
3656 if (__cmd.second != nullptr)
3657 {
3658 __commands.push_back(__command(__cmd.second));
3659 __commands.push_back(__command(__command::__pop_state));
3660 __current_stack.push_back(__current);
3661 for (unsigned __i = 1; __i < __m.__matches_.size(); ++__i)
3662 __saved_matches.push_back(__m.__matches_[__i]);
3663 }
3664 __commands.push_back(__command(__cmd.first));
3665 break;
3666 case __command::__reject:
3667 break;
3668 default:
3669 throw regex_error(regex_constants::error_temp);
3670 break;
3671 }
3672 } while (!__commands.empty());
3673 if (__highest_j != 0)
3674 {
3675 __m.__matches_[0].first = __first;
3676 __m.__matches_[0].second = _STD::next(__first, __highest_j);
3677 __m.__matches_[0].matched = true;
3678 for (unsigned __i = __m.__matches_.size(); __i > 1;)
3679 {
3680 assert(!__best_matches.empty());
3681 __m.__matches_[--__i] = __best_matches.back();
3682 __best_matches.pop_back();
3683 }
3684 return true;
3685 }
3686 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003687 return false;
3688}
3689
3690template <class _CharT, class _Traits>
3691template <class _BidirectionalIterator, class _Allocator>
3692bool
3693basic_regex<_CharT, _Traits>::__match_at_start(
3694 _BidirectionalIterator __first, _BidirectionalIterator __last,
3695 match_results<_BidirectionalIterator, _Allocator>& __m,
3696 vector<size_t>& __lc,
3697 regex_constants::match_flag_type __flags) const
3698{
3699 if (__flags_ & ECMAScript)
3700 return __match_at_start_ecma(__first, __last, __m, __flags);
3701 if (mark_count() == 0)
3702 return __match_at_start_posix_nosubs(__first, __last, __m, __lc, __flags);
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003703 return __match_at_start_posix_subs(__first, __last, __m, __lc, __flags);
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003704}
3705
3706template <class _CharT, class _Traits>
3707template <class _BidirectionalIterator, class _Allocator>
3708bool
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00003709basic_regex<_CharT, _Traits>::__search(
3710 _BidirectionalIterator __first, _BidirectionalIterator __last,
3711 match_results<_BidirectionalIterator, _Allocator>& __m,
3712 regex_constants::match_flag_type __flags) const
3713{
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00003714 if (__left_anchor_)
3715 __flags |= regex_constants::match_continuous;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003716 __m.__init(1 + mark_count(), __first, __last);
3717 vector<size_t> __lc(__loop_count());
3718 if (__match_at_start(__first, __last, __m, __lc, __flags))
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00003719 {
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003720 __m.__prefix_.second = __m[0].first;
3721 __m.__prefix_.matched = __m.__prefix_.first != __m.__prefix_.second;
3722 __m.__suffix_.first = __m[0].second;
3723 __m.__suffix_.matched = __m.__suffix_.first != __m.__suffix_.second;
3724 return true;
3725 }
3726 if (!(__flags & regex_constants::match_continuous))
3727 {
3728 __m.__matches_.assign(__m.size(), __m.__unmatched_);
3729 for (++__first; __first != __last; ++__first)
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00003730 {
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003731 if (__match_at_start(__first, __last, __m, __lc, __flags))
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00003732 {
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003733 __m.__prefix_.second = __m[0].first;
3734 __m.__prefix_.matched = __m.__prefix_.first != __m.__prefix_.second;
3735 __m.__suffix_.first = __m[0].second;
3736 __m.__suffix_.matched = __m.__suffix_.first != __m.__suffix_.second;
3737 return true;
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00003738 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003739 __m.__matches_.assign(__m.size(), __m.__unmatched_);
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00003740 }
3741 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003742 __m.__matches_.clear();
3743 return false;
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00003744}
3745
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00003746template <class _BidirectionalIterator, class _Allocator, class _CharT, class _Traits>
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00003747inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00003748bool
3749regex_search(_BidirectionalIterator __first, _BidirectionalIterator __last,
3750 match_results<_BidirectionalIterator, _Allocator>& __m,
3751 const basic_regex<_CharT, _Traits>& __e,
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00003752 regex_constants::match_flag_type __flags = regex_constants::match_default)
3753{
3754 return __e.__search(__first, __last, __m, __flags);
3755}
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00003756
3757template <class _BidirectionalIterator, class _CharT, class _Traits>
3758inline _LIBCPP_INLINE_VISIBILITY
3759bool
3760regex_search(_BidirectionalIterator __first, _BidirectionalIterator __last,
3761 const basic_regex<_CharT, _Traits>& __e,
3762 regex_constants::match_flag_type __flags = regex_constants::match_default)
3763{
3764 match_results<_BidirectionalIterator> __m;
3765 return _STD::regex_search(__first, __last, __m, __e, __flags);
3766}
3767
3768template <class _CharT, class _Allocator, class _Traits>
3769inline _LIBCPP_INLINE_VISIBILITY
3770bool
3771regex_search(const _CharT* __str, match_results<const _CharT*, _Allocator>& __m,
3772 const basic_regex<_CharT, _Traits>& __e,
3773 regex_constants::match_flag_type __flags = regex_constants::match_default)
3774{
3775 return _STD::regex_search(__str, __str + _Traits::length(__str), __m, __e, __flags);
3776}
3777
3778template <class _CharT, class _Traits>
3779inline _LIBCPP_INLINE_VISIBILITY
3780bool
3781regex_search(const _CharT* __str, const basic_regex<_CharT, _Traits>& __e,
3782 regex_constants::match_flag_type __flags = regex_constants::match_default)
3783{
3784 return _STD::regex_search(__str, __str + _Traits::length(__str), __e, __flags);
3785}
3786
3787template <class _ST, class _SA, class _CharT, class _Traits>
3788inline _LIBCPP_INLINE_VISIBILITY
3789bool
3790regex_search(const basic_string<_CharT, _ST, _SA>& __s,
3791 const basic_regex<_CharT, _Traits>& __e,
3792 regex_constants::match_flag_type __flags = regex_constants::match_default)
3793{
3794 return _STD::regex_search(__s.begin(), __s.end(), __e, __flags);
3795}
3796
3797template <class _ST, class _SA, class _Allocator, class _CharT, class _Traits>
3798inline _LIBCPP_INLINE_VISIBILITY
3799bool
3800regex_search(const basic_string<_CharT, _ST, _SA>& __s,
3801 match_results<typename basic_string<_CharT, _ST, _SA>::const_iterator, _Allocator>& __m,
3802 const basic_regex<_CharT, _Traits>& __e,
3803 regex_constants::match_flag_type __flags = regex_constants::match_default)
3804{
3805 return _STD::regex_search(__s.begin(), __s.end(), __m, __e, __flags);
3806}
3807
3808// regex_match
3809
3810template <class _BidirectionalIterator, class _Allocator, class _CharT, class _Traits>
3811bool
3812regex_match(_BidirectionalIterator __first, _BidirectionalIterator __last,
3813 match_results<_BidirectionalIterator, _Allocator>& __m,
3814 const basic_regex<_CharT, _Traits>& __e,
3815 regex_constants::match_flag_type __flags = regex_constants::match_default)
3816{
3817 bool __r = _STD::regex_search(__first, __last, __m, __e,
3818 __flags | regex_constants::match_continuous);
3819 if (__r)
3820 {
3821 __r = !__m.suffix().matched;
3822 if (!__r)
3823 __m.__matches_.clear();
3824 }
3825 return __r;
3826}
3827
3828template <class _BidirectionalIterator, class _CharT, class _Traits>
3829inline _LIBCPP_INLINE_VISIBILITY
3830bool
3831regex_match(_BidirectionalIterator __first, _BidirectionalIterator __last,
3832 const basic_regex<_CharT, _Traits>& __e,
3833 regex_constants::match_flag_type __flags = regex_constants::match_default)
3834{
3835 match_results<_BidirectionalIterator> __m;
3836 return _STD::regex_match(__first, __last, __m, __e, __flags);
3837}
3838
3839template <class _CharT, class _Allocator, class _Traits>
3840inline _LIBCPP_INLINE_VISIBILITY
3841bool
3842regex_match(const _CharT* __str, match_results<const _CharT*, _Allocator>& __m,
3843 const basic_regex<_CharT, _Traits>& __e,
3844 regex_constants::match_flag_type __flags = regex_constants::match_default)
3845{
3846 return _STD::regex_match(__str, __str + _Traits::length(__str), __m, __e, __flags);
3847}
3848
3849template <class _ST, class _SA, class _Allocator, class _CharT, class _Traits>
3850inline _LIBCPP_INLINE_VISIBILITY
3851bool
3852regex_match(const basic_string<_CharT, _ST, _SA>& __s,
3853 match_results<typename basic_string<_CharT, _ST, _SA>::const_iterator, _Allocator>& __m,
3854 const basic_regex<_CharT, _Traits>& __e,
3855 regex_constants::match_flag_type __flags = regex_constants::match_default)
3856{
3857 return _STD::regex_match(__s.begin(), __s.end(), __m, __e, __flags);
3858}
3859
3860template <class _CharT, class _Traits>
3861inline _LIBCPP_INLINE_VISIBILITY
3862bool
3863regex_match(const _CharT* __str, const basic_regex<_CharT, _Traits>& __e,
3864 regex_constants::match_flag_type __flags = regex_constants::match_default)
3865{
3866 return _STD::regex_match(__str, __str + _Traits::length(__str), __e, __flags);
3867}
3868
3869template <class _ST, class _SA, class _CharT, class _Traits>
3870inline _LIBCPP_INLINE_VISIBILITY
3871bool
3872regex_match(const basic_string<_CharT, _ST, _SA>& __s,
3873 const basic_regex<_CharT, _Traits>& __e,
3874 regex_constants::match_flag_type __flags = regex_constants::match_default)
3875{
3876 return _STD::regex_match(__s.begin(), __s.end(), __e, __flags);
3877}
3878
Howard Hinnant3257c982010-06-17 00:34:59 +00003879_LIBCPP_END_NAMESPACE_STD
3880
3881#endif // _LIBCPP_REGEX