blob: ad79f06659cd442ea05871e6ff7ddc41dd356e82 [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 Hinnantac303862010-07-12 15:51:17 +0000720// temporary!
Howard Hinnante77aa5e2010-07-08 17:43:58 +0000721#include <sstream>
722#include <cassert>
723
Howard Hinnant3257c982010-06-17 00:34:59 +0000724#include <__config>
725#include <stdexcept>
726#include <__locale>
Howard Hinnant8c2c18d2010-06-24 21:28:00 +0000727#include <initializer_list>
Howard Hinnantcd85b9e2010-06-29 18:37:43 +0000728#include <utility>
729#include <iterator>
730#include <string>
Howard Hinnant7e9d84b2010-06-30 00:21:42 +0000731#include <memory>
732#include <vector>
Howard Hinnantac303862010-07-12 15:51:17 +0000733#include <deque>
Howard Hinnant3257c982010-06-17 00:34:59 +0000734
735#pragma GCC system_header
736
737_LIBCPP_BEGIN_NAMESPACE_STD
738
739namespace regex_constants
740{
741
742// syntax_option_type
743
744enum syntax_option_type
745{
746 icase = 1 << 0,
747 nosubs = 1 << 1,
748 optimize = 1 << 2,
749 collate = 1 << 3,
750 ECMAScript = 1 << 4,
751 basic = 1 << 5,
752 extended = 1 << 6,
753 awk = 1 << 7,
754 grep = 1 << 8,
755 egrep = 1 << 9
756};
757
758inline
759/*constexpr*/
760syntax_option_type
761operator~(syntax_option_type __x)
762{
763 return syntax_option_type(~int(__x));
764}
765
766inline
767/*constexpr*/
768syntax_option_type
769operator&(syntax_option_type __x, syntax_option_type __y)
770{
771 return syntax_option_type(int(__x) & int(__y));
772}
773
774inline
775/*constexpr*/
776syntax_option_type
777operator|(syntax_option_type __x, syntax_option_type __y)
778{
779 return syntax_option_type(int(__x) | int(__y));
780}
781
782inline
783/*constexpr*/
784syntax_option_type
785operator^(syntax_option_type __x, syntax_option_type __y)
786{
787 return syntax_option_type(int(__x) ^ int(__y));
788}
789
790inline
791/*constexpr*/
792syntax_option_type&
793operator&=(syntax_option_type& __x, syntax_option_type __y)
794{
795 __x = __x & __y;
796 return __x;
797}
798
799inline
800/*constexpr*/
801syntax_option_type&
802operator|=(syntax_option_type& __x, syntax_option_type __y)
803{
804 __x = __x | __y;
805 return __x;
806}
807
808inline
809/*constexpr*/
810syntax_option_type&
811operator^=(syntax_option_type& __x, syntax_option_type __y)
812{
813 __x = __x ^ __y;
814 return __x;
815}
816
817// match_flag_type
818
819enum match_flag_type
820{
821 match_default = 0,
822 match_not_bol = 1 << 0,
823 match_not_eol = 1 << 1,
824 match_not_bow = 1 << 2,
825 match_not_eow = 1 << 3,
826 match_any = 1 << 4,
827 match_not_null = 1 << 5,
828 match_continuous = 1 << 6,
829 match_prev_avail = 1 << 7,
830 format_default = 0,
831 format_sed = 1 << 8,
832 format_no_copy = 1 << 9,
833 format_first_only = 1 << 10
834};
835
836inline
837/*constexpr*/
838match_flag_type
839operator~(match_flag_type __x)
840{
841 return match_flag_type(~int(__x));
842}
843
844inline
845/*constexpr*/
846match_flag_type
847operator&(match_flag_type __x, match_flag_type __y)
848{
849 return match_flag_type(int(__x) & int(__y));
850}
851
852inline
853/*constexpr*/
854match_flag_type
855operator|(match_flag_type __x, match_flag_type __y)
856{
857 return match_flag_type(int(__x) | int(__y));
858}
859
860inline
861/*constexpr*/
862match_flag_type
863operator^(match_flag_type __x, match_flag_type __y)
864{
865 return match_flag_type(int(__x) ^ int(__y));
866}
867
868inline
869/*constexpr*/
870match_flag_type&
871operator&=(match_flag_type& __x, match_flag_type __y)
872{
873 __x = __x & __y;
874 return __x;
875}
876
877inline
878/*constexpr*/
879match_flag_type&
880operator|=(match_flag_type& __x, match_flag_type __y)
881{
882 __x = __x | __y;
883 return __x;
884}
885
886inline
887/*constexpr*/
888match_flag_type&
889operator^=(match_flag_type& __x, match_flag_type __y)
890{
891 __x = __x ^ __y;
892 return __x;
893}
894
895enum error_type
896{
897 error_collate = 1,
898 error_ctype,
899 error_escape,
900 error_backref,
901 error_brack,
902 error_paren,
903 error_brace,
904 error_badbrace,
905 error_range,
906 error_space,
907 error_badrepeat,
908 error_complexity,
Howard Hinnant8c2c18d2010-06-24 21:28:00 +0000909 error_stack,
910 error_temp
Howard Hinnant3257c982010-06-17 00:34:59 +0000911};
912
913} // regex_constants
914
915class _LIBCPP_EXCEPTION_ABI regex_error
916 : public runtime_error
917{
918 regex_constants::error_type __code_;
919public:
920 explicit regex_error(regex_constants::error_type __ecode);
921 virtual ~regex_error() throw();
922 regex_constants::error_type code() const {return __code_;}
923};
924
925template <class _CharT>
926struct regex_traits
927{
928public:
929 typedef _CharT char_type;
930 typedef basic_string<char_type> string_type;
931 typedef locale locale_type;
Howard Hinnantf409d2f2010-06-21 21:01:43 +0000932 typedef ctype_base::mask char_class_type;
Howard Hinnant3257c982010-06-17 00:34:59 +0000933
Howard Hinnantf409d2f2010-06-21 21:01:43 +0000934 static const char_class_type __regex_word = 0x80;
Howard Hinnant3257c982010-06-17 00:34:59 +0000935private:
936 locale __loc_;
937 const ctype<char_type>* __ct_;
938 const collate<char_type>* __col_;
939
940public:
941 regex_traits();
942
943 static size_t length(const char_type* __p)
944 {return char_traits<char_type>::length(__p);}
945 char_type translate(char_type __c) const {return __c;}
946 char_type translate_nocase(char_type __c) const;
947 template <class _ForwardIterator>
948 string_type
949 transform(_ForwardIterator __f, _ForwardIterator __l) const;
950 template <class _ForwardIterator>
951 string_type
952 transform_primary( _ForwardIterator __f, _ForwardIterator __l) const
953 {return __transform_primary(__f, __l, char_type());}
954 template <class _ForwardIterator>
955 string_type
956 lookup_collatename(_ForwardIterator __f, _ForwardIterator __l) const
957 {return __lookup_collatename(__f, __l, char_type());}
958 template <class _ForwardIterator>
959 char_class_type
960 lookup_classname(_ForwardIterator __f, _ForwardIterator __l,
Howard Hinnantf409d2f2010-06-21 21:01:43 +0000961 bool __icase = false) const
962 {return __lookup_classname(__f, __l, __icase, char_type());}
963 bool isctype(char_type __c, char_class_type __m) const;
964 int value(char_type __ch, int __radix) const
965 {return __value(__ch, __radix);}
Howard Hinnant3257c982010-06-17 00:34:59 +0000966 locale_type imbue(locale_type __l);
967 locale_type getloc()const {return __loc_;}
968
969private:
970 void __init();
971
972 template <class _ForwardIterator>
973 string_type
974 __transform_primary(_ForwardIterator __f, _ForwardIterator __l, char) const;
975 template <class _ForwardIterator>
976 string_type
977 __transform_primary(_ForwardIterator __f, _ForwardIterator __l, wchar_t) const;
978
979 template <class _ForwardIterator>
980 string_type
981 __lookup_collatename(_ForwardIterator __f, _ForwardIterator __l, char) const;
982 template <class _ForwardIterator>
983 string_type
984 __lookup_collatename(_ForwardIterator __f, _ForwardIterator __l, wchar_t) const;
Howard Hinnantf409d2f2010-06-21 21:01:43 +0000985
986 template <class _ForwardIterator>
987 char_class_type
988 __lookup_classname(_ForwardIterator __f, _ForwardIterator __l,
989 bool __icase, char) const;
990 template <class _ForwardIterator>
991 char_class_type
992 __lookup_classname(_ForwardIterator __f, _ForwardIterator __l,
993 bool __icase, wchar_t) const;
994
995 static int __value(unsigned char __ch, int __radix);
996 int __value(char __ch, int __radix) const
997 {return __value(static_cast<unsigned char>(__ch), __radix);}
998 int __value(wchar_t __ch, int __radix) const;
Howard Hinnant3257c982010-06-17 00:34:59 +0000999};
1000
1001template <class _CharT>
1002regex_traits<_CharT>::regex_traits()
1003{
1004 __init();
1005}
1006
1007template <class _CharT>
1008typename regex_traits<_CharT>::char_type
1009regex_traits<_CharT>::translate_nocase(char_type __c) const
1010{
1011 return __ct_->tolower(__c);
1012}
1013
1014template <class _CharT>
1015template <class _ForwardIterator>
1016typename regex_traits<_CharT>::string_type
1017regex_traits<_CharT>::transform(_ForwardIterator __f, _ForwardIterator __l) const
1018{
1019 string_type __s(__f, __l);
1020 return __col_->transform(__s.data(), __s.data() + __s.size());
1021}
1022
1023template <class _CharT>
1024void
1025regex_traits<_CharT>::__init()
1026{
1027 __ct_ = &use_facet<ctype<char_type> >(__loc_);
1028 __col_ = &use_facet<collate<char_type> >(__loc_);
1029}
1030
1031template <class _CharT>
1032typename regex_traits<_CharT>::locale_type
1033regex_traits<_CharT>::imbue(locale_type __l)
1034{
1035 locale __r = __loc_;
1036 __loc_ = __l;
1037 __init();
1038 return __r;
1039}
1040
1041// transform_primary is very FreeBSD-specific
1042
1043template <class _CharT>
1044template <class _ForwardIterator>
1045typename regex_traits<_CharT>::string_type
1046regex_traits<_CharT>::__transform_primary(_ForwardIterator __f,
1047 _ForwardIterator __l, char) const
1048{
1049 const string_type __s(__f, __l);
1050 string_type __d = __col_->transform(__s.data(), __s.data() + __s.size());
1051 switch (__d.size())
1052 {
1053 case 1:
1054 break;
1055 case 12:
1056 __d[11] = __d[3];
1057 break;
1058 default:
1059 __d.clear();
1060 break;
1061 }
1062 return __d;
1063}
1064
1065template <class _CharT>
1066template <class _ForwardIterator>
1067typename regex_traits<_CharT>::string_type
1068regex_traits<_CharT>::__transform_primary(_ForwardIterator __f,
1069 _ForwardIterator __l, wchar_t) const
1070{
1071 const string_type __s(__f, __l);
1072 string_type __d = __col_->transform(__s.data(), __s.data() + __s.size());
1073 switch (__d.size())
1074 {
1075 case 1:
1076 break;
1077 case 3:
1078 __d[2] = __d[0];
1079 break;
1080 default:
1081 __d.clear();
1082 break;
1083 }
1084 return __d;
1085}
1086
1087// lookup_collatename is very FreeBSD-specific
1088
Howard Hinnantf409d2f2010-06-21 21:01:43 +00001089string __get_collation_name(const char* __s);
Howard Hinnant3257c982010-06-17 00:34:59 +00001090
1091template <class _CharT>
1092template <class _ForwardIterator>
1093typename regex_traits<_CharT>::string_type
1094regex_traits<_CharT>::__lookup_collatename(_ForwardIterator __f,
1095 _ForwardIterator __l, char) const
1096{
1097 string_type __s(__f, __l);
1098 string_type __r;
1099 if (!__s.empty())
1100 {
1101 __r = __get_collation_name(__s.c_str());
1102 if (__r.empty() && __s.size() <= 2)
1103 {
1104 __r = __col_->transform(__s.data(), __s.data() + __s.size());
1105 if (__r.size() == 1 || __r.size() == 12)
1106 __r = __s;
1107 else
1108 __r.clear();
1109 }
1110 }
1111 return __r;
1112}
1113
1114template <class _CharT>
1115template <class _ForwardIterator>
1116typename regex_traits<_CharT>::string_type
1117regex_traits<_CharT>::__lookup_collatename(_ForwardIterator __f,
1118 _ForwardIterator __l, wchar_t) const
1119{
1120 string_type __s(__f, __l);
1121 string __n;
1122 __n.reserve(__s.size());
1123 for (typename string_type::const_iterator __i = __s.begin(), __e = __s.end();
1124 __i != __e; ++__i)
1125 {
1126 if (static_cast<unsigned>(*__i) >= 127)
1127 return string_type();
1128 __n.push_back(char(*__i));
1129 }
1130 string_type __r;
1131 if (!__s.empty())
1132 {
1133 __n = __get_collation_name(__n.c_str());
1134 if (!__n.empty())
1135 __r.assign(__n.begin(), __n.end());
1136 else if (__s.size() <= 2)
1137 {
1138 __r = __col_->transform(__s.data(), __s.data() + __s.size());
1139 if (__r.size() == 1 || __r.size() == 3)
1140 __r = __s;
1141 else
1142 __r.clear();
1143 }
1144 }
1145 return __r;
1146}
1147
Howard Hinnantf409d2f2010-06-21 21:01:43 +00001148// lookup_classname
1149
1150ctype_base::mask __get_classname(const char* __s, bool __icase);
1151
1152template <class _CharT>
1153template <class _ForwardIterator>
1154typename regex_traits<_CharT>::char_class_type
1155regex_traits<_CharT>::__lookup_classname(_ForwardIterator __f,
1156 _ForwardIterator __l,
1157 bool __icase, char) const
1158{
1159 string_type __s(__f, __l);
1160 __ct_->tolower(&__s[0], &__s[0] + __s.size());
1161 return __get_classname(__s.c_str(), __icase);
1162}
1163
1164template <class _CharT>
1165template <class _ForwardIterator>
1166typename regex_traits<_CharT>::char_class_type
1167regex_traits<_CharT>::__lookup_classname(_ForwardIterator __f,
1168 _ForwardIterator __l,
1169 bool __icase, wchar_t) const
1170{
1171 string_type __s(__f, __l);
1172 __ct_->tolower(&__s[0], &__s[0] + __s.size());
1173 string __n;
1174 __n.reserve(__s.size());
1175 for (typename string_type::const_iterator __i = __s.begin(), __e = __s.end();
1176 __i != __e; ++__i)
1177 {
1178 if (static_cast<unsigned>(*__i) >= 127)
1179 return char_class_type();
1180 __n.push_back(char(*__i));
1181 }
1182 return __get_classname(__n.c_str(), __icase);
1183}
1184
1185template <class _CharT>
1186bool
1187regex_traits<_CharT>::isctype(char_type __c, char_class_type __m) const
1188{
1189 if (__ct_->is(__m, __c))
1190 return true;
1191 return (__c == '_' && (__m & __regex_word));
1192}
1193
1194template <class _CharT>
1195int
1196regex_traits<_CharT>::__value(unsigned char __ch, int __radix)
1197{
1198 if ((__ch & 0xF8u) == 0x30) // '0' <= __ch && __ch <= '7'
1199 return __ch - '0';
1200 if (__radix != 8)
1201 {
1202 if ((__ch & 0xFEu) == 0x38) // '8' <= __ch && __ch <= '9'
1203 return __ch - '0';
1204 if (__radix == 16)
1205 {
1206 __ch |= 0x20; // tolower
1207 if ('a' <= __ch && __ch <= 'f')
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00001208 return __ch - ('a' - 10);
Howard Hinnantf409d2f2010-06-21 21:01:43 +00001209 }
1210 }
1211 return -1;
1212}
1213
1214template <class _CharT>
1215inline
1216int
1217regex_traits<_CharT>::__value(wchar_t __ch, int __radix) const
1218{
1219 return __value(static_cast<unsigned char>(__ct_->narrow(__ch, char_type())), __radix);
1220}
1221
Howard Hinnantac303862010-07-12 15:51:17 +00001222template <class _CharT> class __node;
1223
1224template <class _BidirectionalIterator> class sub_match;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001225
1226template <class _CharT>
Howard Hinnantac303862010-07-12 15:51:17 +00001227struct __state
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001228{
1229 enum
1230 {
1231 __end_state = -1000,
1232 __consume_input, // -999
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001233 __begin_marked_expr, // -998
1234 __end_marked_expr, // -997
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001235 __pop_state, // -996
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001236 __accept_and_consume, // -995
1237 __accept_but_not_consume, // -994
1238 __reject, // -993
Howard Hinnantac303862010-07-12 15:51:17 +00001239 __split,
1240 __repeat
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001241 };
1242
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001243 int __do_;
Howard Hinnantac303862010-07-12 15:51:17 +00001244 const _CharT* __first_;
1245 const _CharT* __current_;
1246 const _CharT* __last_;
1247 vector<sub_match<const _CharT*> > __sub_matches_;
1248 vector<pair<size_t, const _CharT*> > __loop_data_;
1249 const __node<_CharT>* __node_;
1250 regex_constants::match_flag_type __flags_;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001251
Howard Hinnantac303862010-07-12 15:51:17 +00001252 __state()
1253 : __do_(0), __first_(nullptr), __current_(nullptr), __last_(nullptr),
1254 __node_(nullptr), __flags_() {}
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001255};
1256
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001257template <class _CharT>
1258ostream&
Howard Hinnantac303862010-07-12 15:51:17 +00001259operator<<(ostream& os, const __state<_CharT>& c)
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001260{
1261 os << c.__do_;
Howard Hinnantac303862010-07-12 15:51:17 +00001262 if (c.__node_)
1263 os << ", " << c.__node_->speak();
1264else
1265 os << ", null";
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001266 return os;
1267}
1268
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001269
Howard Hinnantac303862010-07-12 15:51:17 +00001270// __node
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001271
1272template <class _CharT>
Howard Hinnantac303862010-07-12 15:51:17 +00001273class __node
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001274{
Howard Hinnantac303862010-07-12 15:51:17 +00001275 __node(const __node&);
1276 __node& operator=(const __node&);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001277public:
Howard Hinnantac303862010-07-12 15:51:17 +00001278 typedef _STD::__state<_CharT> __state;
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001279
Howard Hinnantac303862010-07-12 15:51:17 +00001280 __node() {}
1281 virtual ~__node() {}
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001282
Howard Hinnantac303862010-07-12 15:51:17 +00001283 virtual void __exec(__state&) const {};
1284 virtual void __exec_split(bool, __state&) const {};
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001285
Howard Hinnantac303862010-07-12 15:51:17 +00001286 virtual string speak() const {return "__node";}
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001287};
1288
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001289// __end_state
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001290
1291template <class _CharT>
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001292class __end_state
Howard Hinnantac303862010-07-12 15:51:17 +00001293 : public __node<_CharT>
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001294{
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001295public:
Howard Hinnantac303862010-07-12 15:51:17 +00001296 typedef _STD::__state<_CharT> __state;
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001297
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001298 __end_state() {}
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00001299
Howard Hinnantac303862010-07-12 15:51:17 +00001300 virtual void __exec(__state&) const;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001301
1302 virtual string speak() const {return "end state";}
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001303};
1304
1305template <class _CharT>
Howard Hinnantac303862010-07-12 15:51:17 +00001306void
1307__end_state<_CharT>::__exec(__state& __s) const
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00001308{
Howard Hinnantac303862010-07-12 15:51:17 +00001309 __s.__do_ = __state::__end_state;
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00001310}
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001311
1312// __has_one_state
1313
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00001314template <class _CharT>
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001315class __has_one_state
Howard Hinnantac303862010-07-12 15:51:17 +00001316 : public __node<_CharT>
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00001317{
Howard Hinnantac303862010-07-12 15:51:17 +00001318 __node<_CharT>* __first_;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001319
1320public:
Howard Hinnantac303862010-07-12 15:51:17 +00001321 explicit __has_one_state(__node<_CharT>* __s)
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001322 : __first_(__s) {}
1323
Howard Hinnantac303862010-07-12 15:51:17 +00001324 __node<_CharT>* first() const {return __first_;}
1325 __node<_CharT>*& first() {return __first_;}
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001326};
1327
1328// __owns_one_state
1329
1330template <class _CharT>
1331class __owns_one_state
1332 : public __has_one_state<_CharT>
1333{
1334 typedef __has_one_state<_CharT> base;
1335
1336public:
Howard Hinnantac303862010-07-12 15:51:17 +00001337 explicit __owns_one_state(__node<_CharT>* __s)
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001338 : base(__s) {}
1339
1340 virtual ~__owns_one_state();
1341};
1342
1343template <class _CharT>
1344__owns_one_state<_CharT>::~__owns_one_state()
1345{
1346 delete this->first();
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00001347}
1348
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001349// __empty_state
1350
1351template <class _CharT>
1352class __empty_state
1353 : public __owns_one_state<_CharT>
1354{
1355 typedef __owns_one_state<_CharT> base;
1356
1357public:
Howard Hinnantac303862010-07-12 15:51:17 +00001358 typedef _STD::__state<_CharT> __state;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001359
Howard Hinnantac303862010-07-12 15:51:17 +00001360 explicit __empty_state(__node<_CharT>* __s)
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001361 : base(__s) {}
1362
Howard Hinnantac303862010-07-12 15:51:17 +00001363 virtual void __exec(__state&) const;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001364
1365 virtual string speak() const {return "empty state";}
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001366};
1367
1368template <class _CharT>
Howard Hinnantac303862010-07-12 15:51:17 +00001369void
1370__empty_state<_CharT>::__exec(__state& __s) const
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001371{
Howard Hinnantac303862010-07-12 15:51:17 +00001372 __s.__do_ = __state::__accept_but_not_consume;
1373 __s.__node_ = this->first();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001374}
1375
1376// __empty_non_own_state
1377
1378template <class _CharT>
1379class __empty_non_own_state
1380 : public __has_one_state<_CharT>
1381{
1382 typedef __has_one_state<_CharT> base;
1383
1384public:
Howard Hinnantac303862010-07-12 15:51:17 +00001385 typedef _STD::__state<_CharT> __state;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001386
Howard Hinnantac303862010-07-12 15:51:17 +00001387 explicit __empty_non_own_state(__node<_CharT>* __s)
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001388 : base(__s) {}
1389
Howard Hinnantac303862010-07-12 15:51:17 +00001390 virtual void __exec(__state&) const;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001391
1392 virtual string speak() const {return "empty non-owning state";}
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001393};
1394
1395template <class _CharT>
Howard Hinnantac303862010-07-12 15:51:17 +00001396void
1397__empty_non_own_state<_CharT>::__exec(__state& __s) const
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001398{
Howard Hinnantac303862010-07-12 15:51:17 +00001399 __s.__do_ = __state::__accept_but_not_consume;
1400 __s.__node_ = this->first();
1401}
1402
1403// __repeat_one_loop
1404
1405template <class _CharT>
1406class __repeat_one_loop
1407 : public __has_one_state<_CharT>
1408{
1409 typedef __has_one_state<_CharT> base;
1410
1411public:
1412 typedef _STD::__state<_CharT> __state;
1413
1414 explicit __repeat_one_loop(__node<_CharT>* __s)
1415 : base(__s) {}
1416
1417 virtual void __exec(__state&) const;
1418
1419 virtual string speak() const {return "repeat loop";}
1420};
1421
1422template <class _CharT>
1423void
1424__repeat_one_loop<_CharT>::__exec(__state& __s) const
1425{
1426 __s.__do_ = __state::__repeat;
1427 __s.__node_ = this->first();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001428}
1429
1430// __owns_two_states
1431
1432template <class _CharT>
1433class __owns_two_states
1434 : public __owns_one_state<_CharT>
1435{
1436 typedef __owns_one_state<_CharT> base;
1437
1438 base* __second_;
1439
1440public:
Howard Hinnantac303862010-07-12 15:51:17 +00001441 explicit __owns_two_states(__node<_CharT>* __s1, base* __s2)
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001442 : base(__s1), __second_(__s2) {}
1443
1444 virtual ~__owns_two_states();
1445
1446 base* second() const {return __second_;}
1447 base*& second() {return __second_;}
1448};
1449
1450template <class _CharT>
1451__owns_two_states<_CharT>::~__owns_two_states()
1452{
1453 delete __second_;
1454}
1455
1456// __loop
1457
1458template <class _CharT>
1459class __loop
1460 : public __owns_two_states<_CharT>
1461{
1462 typedef __owns_two_states<_CharT> base;
1463
1464 size_t __min_;
1465 size_t __max_;
1466 unsigned __loop_id_;
Howard Hinnantac303862010-07-12 15:51:17 +00001467 unsigned __mexp_begin_;
1468 unsigned __mexp_end_;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001469 bool __greedy_;
1470
1471public:
Howard Hinnantac303862010-07-12 15:51:17 +00001472 typedef _STD::__state<_CharT> __state;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001473
1474 explicit __loop(unsigned __loop_id,
Howard Hinnantac303862010-07-12 15:51:17 +00001475 __node<_CharT>* __s1, __owns_one_state<_CharT>* __s2,
1476 unsigned __mexp_begin, unsigned __mexp_end,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001477 bool __greedy = true,
1478 size_t __min = 0,
1479 size_t __max = numeric_limits<size_t>::max())
1480 : base(__s1, __s2), __min_(__min), __max_(__max), __loop_id_(__loop_id),
Howard Hinnantac303862010-07-12 15:51:17 +00001481 __mexp_begin_(__mexp_begin), __mexp_end_(__mexp_end),
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001482 __greedy_(__greedy) {}
1483
Howard Hinnantac303862010-07-12 15:51:17 +00001484 virtual void __exec(__state& __s) const;
1485 virtual void __exec_split(bool __second, __state& __s) const;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001486
1487 virtual string speak() const
1488 {
1489 ostringstream os;
Howard Hinnantac303862010-07-12 15:51:17 +00001490 os << "loop "<< __loop_id_ << " {" << __min_ << ',' << __max_ << "}";
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001491 if (!__greedy_)
1492 os << " not";
1493 os << " greedy";
1494 return os.str();
1495 }
Howard Hinnantac303862010-07-12 15:51:17 +00001496
1497private:
1498 void __init_repeat(__state& __s) const
1499 {
1500 __s.__loop_data_[__loop_id_].second = __s.__current_;
1501 for (size_t __i = __mexp_begin_-1; __i != __mexp_end_-1; ++__i)
1502 {
1503 __s.__sub_matches_[__i].first = __s.__last_;
1504 __s.__sub_matches_[__i].second = __s.__last_;
1505 __s.__sub_matches_[__i].matched = false;
1506 }
1507 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001508};
1509
1510template <class _CharT>
Howard Hinnantac303862010-07-12 15:51:17 +00001511void
1512__loop<_CharT>::__exec(__state& __s) const
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001513{
Howard Hinnantac303862010-07-12 15:51:17 +00001514 if (__s.__do_ == __state::__repeat)
1515 {
1516 bool __do_repeat = ++__s.__loop_data_[__loop_id_].first < __max_;
1517 bool __do_alt = __s.__loop_data_[__loop_id_].first >= __min_;
1518 if (__do_repeat && __do_alt &&
1519 __s.__loop_data_[__loop_id_].second == __s.__current_)
1520 __do_repeat = false;
1521 if (__do_repeat && __do_alt)
1522 __s.__do_ = __state::__split;
1523 else if (__do_repeat)
1524 {
1525 __s.__do_ = __state::__accept_but_not_consume;
1526 __s.__node_ = this->first();
1527 __init_repeat(__s);
1528 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001529 else
Howard Hinnantac303862010-07-12 15:51:17 +00001530 {
1531 __s.__do_ = __state::__accept_but_not_consume;
1532 __s.__node_ = this->second();
1533 }
1534 }
1535 else
1536 {
1537 if (__max_ > 0)
1538 __s.__do_ = __state::__split;
1539 else
1540 {
1541 __s.__do_ = __state::__accept_but_not_consume;
1542 __s.__node_ = this->second();
1543 }
1544 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001545}
1546
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001547template <class _CharT>
Howard Hinnantac303862010-07-12 15:51:17 +00001548void
1549__loop<_CharT>::__exec_split(bool __second, __state& __s) const
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001550{
Howard Hinnantac303862010-07-12 15:51:17 +00001551 __s.__do_ = __state::__accept_but_not_consume;
1552 if (__greedy_ != __second)
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001553 {
Howard Hinnantac303862010-07-12 15:51:17 +00001554 __s.__node_ = this->first();
1555 __init_repeat(__s);
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001556 }
Howard Hinnantac303862010-07-12 15:51:17 +00001557 else
1558 __s.__node_ = this->second();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001559}
1560
1561// __begin_marked_subexpression
1562
1563template <class _CharT>
1564class __begin_marked_subexpression
1565 : public __owns_one_state<_CharT>
1566{
1567 typedef __owns_one_state<_CharT> base;
1568
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001569 unsigned __mexp_;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001570public:
Howard Hinnantac303862010-07-12 15:51:17 +00001571 typedef _STD::__state<_CharT> __state;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001572
Howard Hinnantac303862010-07-12 15:51:17 +00001573 explicit __begin_marked_subexpression(unsigned __mexp, __node<_CharT>* __s)
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001574 : base(__s), __mexp_(__mexp) {}
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001575
Howard Hinnantac303862010-07-12 15:51:17 +00001576 virtual void __exec(__state&) const;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001577
1578 virtual string speak() const
1579 {
1580 ostringstream os;
1581 os << "begin marked expr " << __mexp_;
1582 return os.str();
1583 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001584};
1585
1586template <class _CharT>
Howard Hinnantac303862010-07-12 15:51:17 +00001587void
1588__begin_marked_subexpression<_CharT>::__exec(__state& __s) const
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001589{
Howard Hinnantac303862010-07-12 15:51:17 +00001590 __s.__do_ = __state::__accept_but_not_consume;
1591 __s.__sub_matches_[__mexp_-1].first = __s.__current_;
1592 __s.__node_ = this->first();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001593}
1594
1595// __end_marked_subexpression
1596
1597template <class _CharT>
1598class __end_marked_subexpression
1599 : public __owns_one_state<_CharT>
1600{
1601 typedef __owns_one_state<_CharT> base;
1602
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001603 unsigned __mexp_;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001604public:
Howard Hinnantac303862010-07-12 15:51:17 +00001605 typedef _STD::__state<_CharT> __state;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001606
Howard Hinnantac303862010-07-12 15:51:17 +00001607 explicit __end_marked_subexpression(unsigned __mexp, __node<_CharT>* __s)
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001608 : base(__s), __mexp_(__mexp) {}
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001609
Howard Hinnantac303862010-07-12 15:51:17 +00001610 virtual void __exec(__state&) const;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001611
1612 virtual string speak() const
1613 {
1614 ostringstream os;
1615 os << "end marked expr " << __mexp_;
1616 return os.str();
1617 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001618};
1619
1620template <class _CharT>
Howard Hinnantac303862010-07-12 15:51:17 +00001621void
1622__end_marked_subexpression<_CharT>::__exec(__state& __s) const
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001623{
Howard Hinnantac303862010-07-12 15:51:17 +00001624 __s.__do_ = __state::__accept_but_not_consume;
1625 __s.__sub_matches_[__mexp_-1].second = __s.__current_;
1626 __s.__sub_matches_[__mexp_-1].matched = true;
1627 __s.__node_ = this->first();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001628}
1629
Howard Hinnantcba352d2010-07-12 18:16:05 +00001630// __back_ref
1631
1632template <class _CharT>
1633class __back_ref
1634 : public __owns_one_state<_CharT>
1635{
1636 typedef __owns_one_state<_CharT> base;
1637
1638 unsigned __mexp_;
1639public:
1640 typedef _STD::__state<_CharT> __state;
1641
1642 explicit __back_ref(unsigned __mexp, __node<_CharT>* __s)
1643 : base(__s), __mexp_(__mexp) {}
1644
1645 virtual void __exec(__state&) const;
1646
1647 virtual string speak() const
1648 {
1649 ostringstream os;
1650 os << "__back_ref " << __mexp_;
1651 return os.str();
1652 }
1653};
1654
1655template <class _CharT>
1656void
1657__back_ref<_CharT>::__exec(__state& __s) const
1658{
1659 sub_match<const _CharT*>& __sm = __s.__sub_matches_[__mexp_-1];
1660 if (__sm.matched)
1661 {
1662 ptrdiff_t __len = __sm.second - __sm.first;
1663 if (__s.__last_ - __s.__current_ >= __len &&
1664 _STD::equal(__sm.first, __sm.second, __s.__current_))
1665 {
1666 __s.__do_ = __state::__accept_but_not_consume;
1667 __s.__current_ += __len;
1668 __s.__node_ = this->first();
1669 }
1670 else
1671 {
1672 __s.__do_ = __state::__reject;
1673 __s.__node_ = nullptr;
1674 }
1675 }
1676 else
1677 {
1678 __s.__do_ = __state::__reject;
1679 __s.__node_ = nullptr;
1680 }
1681}
1682
Howard Hinnante34f17d2010-07-12 19:11:27 +00001683// __back_ref_icase
1684
1685template <class _CharT, class _Traits>
1686class __back_ref_icase
1687 : public __owns_one_state<_CharT>
1688{
1689 typedef __owns_one_state<_CharT> base;
1690
1691 _Traits __traits_;
1692 unsigned __mexp_;
1693public:
1694 typedef _STD::__state<_CharT> __state;
1695
1696 explicit __back_ref_icase(const _Traits& __traits, unsigned __mexp,
1697 __node<_CharT>* __s)
1698 : base(__s), __traits_(__traits), __mexp_(__mexp) {}
1699
1700 virtual void __exec(__state&) const;
1701
1702 virtual string speak() const
1703 {
1704 ostringstream os;
1705 os << "__back_ref_icase " << __mexp_;
1706 return os.str();
1707 }
1708};
1709
1710template <class _CharT, class _Traits>
1711void
1712__back_ref_icase<_CharT, _Traits>::__exec(__state& __s) const
1713{
1714 sub_match<const _CharT*>& __sm = __s.__sub_matches_[__mexp_-1];
1715 if (__sm.matched)
1716 {
1717 ptrdiff_t __len = __sm.second - __sm.first;
1718 if (__s.__last_ - __s.__current_ >= __len)
1719 {
1720 for (ptrdiff_t __i = 0; __i < __len; ++__i)
1721 {
1722 if (__traits_.translate_nocase(__sm.first[__i]) !=
1723 __traits_.translate_nocase(__s.__current_[__i]))
1724 goto __not_equal;
1725 }
1726 __s.__do_ = __state::__accept_but_not_consume;
1727 __s.__current_ += __len;
1728 __s.__node_ = this->first();
1729 }
1730 else
1731 {
1732 __s.__do_ = __state::__reject;
1733 __s.__node_ = nullptr;
1734 }
1735 }
1736 else
1737 {
1738__not_equal:
1739 __s.__do_ = __state::__reject;
1740 __s.__node_ = nullptr;
1741 }
1742}
1743
1744// __back_ref_collate
1745
1746template <class _CharT, class _Traits>
1747class __back_ref_collate
1748 : public __owns_one_state<_CharT>
1749{
1750 typedef __owns_one_state<_CharT> base;
1751
1752 _Traits __traits_;
1753 unsigned __mexp_;
1754public:
1755 typedef _STD::__state<_CharT> __state;
1756
1757 explicit __back_ref_collate(const _Traits& __traits, unsigned __mexp,
1758 __node<_CharT>* __s)
1759 : base(__s), __traits_(__traits), __mexp_(__mexp) {}
1760
1761 virtual void __exec(__state&) const;
1762
1763 virtual string speak() const
1764 {
1765 ostringstream os;
1766 os << "__back_ref_collate " << __mexp_;
1767 return os.str();
1768 }
1769};
1770
1771template <class _CharT, class _Traits>
1772void
1773__back_ref_collate<_CharT, _Traits>::__exec(__state& __s) const
1774{
1775 sub_match<const _CharT*>& __sm = __s.__sub_matches_[__mexp_-1];
1776 if (__sm.matched)
1777 {
1778 ptrdiff_t __len = __sm.second - __sm.first;
1779 if (__s.__last_ - __s.__current_ >= __len)
1780 {
1781 for (ptrdiff_t __i = 0; __i < __len; ++__i)
1782 {
1783 if (__traits_.translate(__sm.first[__i]) !=
1784 __traits_.translate(__s.__current_[__i]))
1785 goto __not_equal;
1786 }
1787 __s.__do_ = __state::__accept_but_not_consume;
1788 __s.__current_ += __len;
1789 __s.__node_ = this->first();
1790 }
1791 else
1792 {
1793 __s.__do_ = __state::__reject;
1794 __s.__node_ = nullptr;
1795 }
1796 }
1797 else
1798 {
1799__not_equal:
1800 __s.__do_ = __state::__reject;
1801 __s.__node_ = nullptr;
1802 }
1803}
1804
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001805// __r_anchor
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001806
1807template <class _CharT>
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001808class __r_anchor
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001809 : public __owns_one_state<_CharT>
1810{
1811 typedef __owns_one_state<_CharT> base;
1812
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001813public:
Howard Hinnantac303862010-07-12 15:51:17 +00001814 typedef _STD::__state<_CharT> __state;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001815
Howard Hinnantac303862010-07-12 15:51:17 +00001816 __r_anchor(__node<_CharT>* __s)
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001817 : base(__s) {}
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001818
Howard Hinnantac303862010-07-12 15:51:17 +00001819 virtual void __exec(__state&) const;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001820
1821 virtual string speak() const
1822 {
1823 ostringstream os;
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001824 os << "right anchor";
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001825 return os.str();
1826 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001827};
1828
1829template <class _CharT>
Howard Hinnantac303862010-07-12 15:51:17 +00001830void
1831__r_anchor<_CharT>::__exec(__state& __s) const
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001832{
Howard Hinnantac303862010-07-12 15:51:17 +00001833 if (__s.__current_ == __s.__last_)
1834 {
1835 __s.__do_ = __state::__accept_but_not_consume;
1836 __s.__node_ = this->first();
1837 }
1838 else
1839 {
1840 __s.__do_ = __state::__reject;
1841 __s.__node_ = nullptr;
1842 }
1843}
1844
1845// __match_any
1846
1847template <class _CharT>
1848class __match_any
1849 : public __owns_one_state<_CharT>
1850{
1851 typedef __owns_one_state<_CharT> base;
1852
1853public:
1854 typedef _STD::__state<_CharT> __state;
1855
1856 __match_any(__node<_CharT>* __s)
1857 : base(__s) {}
1858
1859 virtual void __exec(__state&) const;
1860
1861 virtual string speak() const
1862 {
1863 ostringstream os;
1864 os << "match any";
1865 return os.str();
1866 }
1867};
1868
1869template <class _CharT>
1870void
1871__match_any<_CharT>::__exec(__state& __s) const
1872{
1873 if (__s.__current_ != __s.__last_ && *__s.__current_ != 0)
1874 {
1875 __s.__do_ = __state::__accept_and_consume;
1876 ++__s.__current_;
1877 __s.__node_ = this->first();
1878 }
1879 else
1880 {
1881 __s.__do_ = __state::__reject;
1882 __s.__node_ = nullptr;
1883 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001884}
1885
1886// __match_char
1887
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00001888template <class _CharT>
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001889class __match_char
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001890 : public __owns_one_state<_CharT>
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001891{
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001892 typedef __owns_one_state<_CharT> base;
1893
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001894 _CharT __c_;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001895
1896 __match_char(const __match_char&);
1897 __match_char& operator=(const __match_char&);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001898public:
Howard Hinnantac303862010-07-12 15:51:17 +00001899 typedef _STD::__state<_CharT> __state;
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00001900
Howard Hinnantac303862010-07-12 15:51:17 +00001901 __match_char(_CharT __c, __node<_CharT>* __s)
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001902 : base(__s), __c_(__c) {}
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001903
Howard Hinnantac303862010-07-12 15:51:17 +00001904 virtual void __exec(__state&) const;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001905
1906 virtual string speak() const
1907 {
1908 ostringstream os;
1909 os << "match char " << __c_;
1910 return os.str();
1911 }
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001912};
1913
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00001914template <class _CharT>
Howard Hinnantac303862010-07-12 15:51:17 +00001915void
1916__match_char<_CharT>::__exec(__state& __s) const
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00001917{
Howard Hinnantac303862010-07-12 15:51:17 +00001918 if (__s.__current_ != __s.__last_ && *__s.__current_ == __c_)
1919 {
1920 __s.__do_ = __state::__accept_and_consume;
1921 ++__s.__current_;
1922 __s.__node_ = this->first();
1923 }
1924 else
1925 {
1926 __s.__do_ = __state::__reject;
1927 __s.__node_ = nullptr;
1928 }
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00001929}
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00001930
Howard Hinnante34f17d2010-07-12 19:11:27 +00001931// __match_char_icase
1932
1933template <class _CharT, class _Traits>
1934class __match_char_icase
1935 : public __owns_one_state<_CharT>
1936{
1937 typedef __owns_one_state<_CharT> base;
1938
1939 _Traits __traits_;
1940 _CharT __c_;
1941
1942 __match_char_icase(const __match_char_icase&);
1943 __match_char_icase& operator=(const __match_char_icase&);
1944public:
1945 typedef _STD::__state<_CharT> __state;
1946
1947 __match_char_icase(const _Traits& __traits, _CharT __c, __node<_CharT>* __s)
1948 : base(__s), __traits_(__traits), __c_(__traits.translate_nocase(__c)) {}
1949
1950 virtual void __exec(__state&) const;
1951
1952 virtual string speak() const
1953 {
1954 ostringstream os;
1955 os << "match char icase " << __c_;
1956 return os.str();
1957 }
1958};
1959
1960template <class _CharT, class _Traits>
1961void
1962__match_char_icase<_CharT, _Traits>::__exec(__state& __s) const
1963{
1964 if (__s.__current_ != __s.__last_ &&
1965 __traits_.translate_nocase(*__s.__current_) == __c_)
1966 {
1967 __s.__do_ = __state::__accept_and_consume;
1968 ++__s.__current_;
1969 __s.__node_ = this->first();
1970 }
1971 else
1972 {
1973 __s.__do_ = __state::__reject;
1974 __s.__node_ = nullptr;
1975 }
1976}
1977
1978// __match_char_collate
1979
1980template <class _CharT, class _Traits>
1981class __match_char_collate
1982 : public __owns_one_state<_CharT>
1983{
1984 typedef __owns_one_state<_CharT> base;
1985
1986 _Traits __traits_;
1987 _CharT __c_;
1988
1989 __match_char_collate(const __match_char_collate&);
1990 __match_char_collate& operator=(const __match_char_collate&);
1991public:
1992 typedef _STD::__state<_CharT> __state;
1993
1994 __match_char_collate(const _Traits& __traits, _CharT __c, __node<_CharT>* __s)
1995 : base(__s), __traits_(__traits), __c_(__traits.translate(__c)) {}
1996
1997 virtual void __exec(__state&) const;
1998
1999 virtual string speak() const
2000 {
2001 ostringstream os;
2002 os << "match char icase " << __c_;
2003 return os.str();
2004 }
2005};
2006
2007template <class _CharT, class _Traits>
2008void
2009__match_char_collate<_CharT, _Traits>::__exec(__state& __s) const
2010{
2011 if (__s.__current_ != __s.__last_ &&
2012 __traits_.translate(*__s.__current_) == __c_)
2013 {
2014 __s.__do_ = __state::__accept_and_consume;
2015 ++__s.__current_;
2016 __s.__node_ = this->first();
2017 }
2018 else
2019 {
2020 __s.__do_ = __state::__reject;
2021 __s.__node_ = nullptr;
2022 }
2023}
2024
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00002025template <class, class> class match_results;
2026
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002027template <class _CharT, class _Traits = regex_traits<_CharT> >
2028class basic_regex
2029{
2030public:
2031 // types:
2032 typedef _CharT value_type;
2033 typedef regex_constants::syntax_option_type flag_type;
2034 typedef typename _Traits::locale_type locale_type;
2035
2036private:
2037 _Traits __traits_;
2038 flag_type __flags_;
2039 unsigned __marked_count_;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002040 unsigned __loop_count_;
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002041 int __open_count_;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002042 shared_ptr<__empty_state<_CharT> > __start_;
2043 __owns_one_state<_CharT>* __end_;
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00002044 bool __left_anchor_;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002045
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00002046 typedef _STD::__state<_CharT> __state;
Howard Hinnantac303862010-07-12 15:51:17 +00002047 typedef _STD::__node<_CharT> __node;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002048
2049public:
2050 // constants:
2051 static const/*expr*/ regex_constants::syntax_option_type icase = regex_constants::icase;
2052 static const/*expr*/ regex_constants::syntax_option_type nosubs = regex_constants::nosubs;
2053 static const/*expr*/ regex_constants::syntax_option_type optimize = regex_constants::optimize;
2054 static const/*expr*/ regex_constants::syntax_option_type collate = regex_constants::collate;
2055 static const/*expr*/ regex_constants::syntax_option_type ECMAScript = regex_constants::ECMAScript;
2056 static const/*expr*/ regex_constants::syntax_option_type basic = regex_constants::basic;
2057 static const/*expr*/ regex_constants::syntax_option_type extended = regex_constants::extended;
2058 static const/*expr*/ regex_constants::syntax_option_type awk = regex_constants::awk;
2059 static const/*expr*/ regex_constants::syntax_option_type grep = regex_constants::grep;
2060 static const/*expr*/ regex_constants::syntax_option_type egrep = regex_constants::egrep;
2061
2062 // construct/copy/destroy:
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002063 basic_regex()
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00002064 : __flags_(), __marked_count_(0), __loop_count_(0), __open_count_(0),
2065 __end_(0), __left_anchor_(false)
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002066 {}
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002067 explicit basic_regex(const value_type* __p, flag_type __f = regex_constants::ECMAScript)
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00002068 : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
2069 __end_(0), __left_anchor_(false)
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002070 {__parse(__p, __p + __traits_.length(__p));}
2071 basic_regex(const value_type* __p, size_t __len, flag_type __f)
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00002072 : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
2073 __end_(0), __left_anchor_(false)
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002074 {__parse(__p, __p + __len);}
2075 basic_regex(const basic_regex&);
2076#ifdef _LIBCPP_MOVE
2077 basic_regex(basic_regex&&);
2078#endif
2079 template <class _ST, class _SA>
2080 explicit basic_regex(const basic_string<value_type, _ST, _SA>& __p,
2081 flag_type __f = regex_constants::ECMAScript)
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00002082 : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
2083 __end_(0), __left_anchor_(false)
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002084 {__parse(__p.begin(), __p.end());}
2085 template <class _ForwardIterator>
2086 basic_regex(_ForwardIterator __first, _ForwardIterator __last,
2087 flag_type __f = regex_constants::ECMAScript)
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00002088 : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
2089 __end_(0), __left_anchor_(false)
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002090 {__parse(__first, __last);}
2091 basic_regex(initializer_list<value_type> __il,
2092 flag_type __f = regex_constants::ECMAScript)
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00002093 : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
2094 __end_(0), __left_anchor_(false)
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002095 {__parse(__il.begin(), __il.end());}
2096
2097 ~basic_regex();
2098
2099 basic_regex& operator=(const basic_regex&);
2100#ifdef _LIBCPP_MOVE
2101 basic_regex& operator=(basic_regex&&);
2102#endif
2103 basic_regex& operator=(const value_type* __p);
2104 basic_regex& operator=(initializer_list<value_type> __il);
2105 template <class _ST, class _SA>
2106 basic_regex& operator=(const basic_string<value_type, _ST, _SA>& __p);
2107
2108 // assign:
2109 basic_regex& assign(const basic_regex& __that);
2110#ifdef _LIBCPP_MOVE
2111 basic_regex& assign(basic_regex&& __that);
2112#endif
2113 basic_regex& assign(const value_type* __p, flag_type __f = regex_constants::ECMAScript);
2114 basic_regex& assign(const value_type* __p, size_t __len, flag_type __f);
2115 template <class _ST, class _SA>
2116 basic_regex& assign(const basic_string<value_type, _ST, _SA>& __s,
2117 flag_type __f = regex_constants::ECMAScript);
2118 template <class _InputIterator>
2119 basic_regex& assign(_InputIterator __first, _InputIterator __last,
2120 flag_type __f = regex_constants::ECMAScript);
2121 basic_regex& assign(initializer_list<value_type> __il,
2122 flag_type = regex_constants::ECMAScript);
2123
2124 // const operations:
2125 unsigned mark_count() const {return __marked_count_;}
2126 flag_type flags() const {return __flags_;}
2127
2128 // locale:
2129 locale_type imbue(locale_type __loc) {return __traits_.imbue(__loc);}
2130 locale_type getloc() const {return __traits_.getloc();}
2131
2132 // swap:
2133 void swap(basic_regex&);
2134
2135private:
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002136 unsigned __loop_count() const {return __loop_count_;}
2137
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002138 template <class _ForwardIterator>
2139 void __parse(_ForwardIterator __first, _ForwardIterator __last);
2140 template <class _ForwardIterator>
2141 _ForwardIterator
2142 __parse_basic_reg_exp(_ForwardIterator __first, _ForwardIterator __last);
2143 template <class _ForwardIterator>
2144 _ForwardIterator
2145 __parse_RE_expression(_ForwardIterator __first, _ForwardIterator __last);
2146 template <class _ForwardIterator>
2147 _ForwardIterator
2148 __parse_simple_RE(_ForwardIterator __first, _ForwardIterator __last);
2149 template <class _ForwardIterator>
2150 _ForwardIterator
2151 __parse_nondupl_RE(_ForwardIterator __first, _ForwardIterator __last);
2152 template <class _ForwardIterator>
2153 _ForwardIterator
2154 __parse_one_char_or_coll_elem_RE(_ForwardIterator __first, _ForwardIterator __last);
2155 template <class _ForwardIterator>
2156 _ForwardIterator
2157 __parse_Back_open_paren(_ForwardIterator __first, _ForwardIterator __last);
2158 template <class _ForwardIterator>
2159 _ForwardIterator
2160 __parse_Back_close_paren(_ForwardIterator __first, _ForwardIterator __last);
2161 template <class _ForwardIterator>
2162 _ForwardIterator
2163 __parse_Back_open_brace(_ForwardIterator __first, _ForwardIterator __last);
2164 template <class _ForwardIterator>
2165 _ForwardIterator
2166 __parse_Back_close_brace(_ForwardIterator __first, _ForwardIterator __last);
2167 template <class _ForwardIterator>
2168 _ForwardIterator
2169 __parse_BACKREF(_ForwardIterator __first, _ForwardIterator __last);
2170 template <class _ForwardIterator>
2171 _ForwardIterator
2172 __parse_ORD_CHAR(_ForwardIterator __first, _ForwardIterator __last);
2173 template <class _ForwardIterator>
2174 _ForwardIterator
2175 __parse_QUOTED_CHAR(_ForwardIterator __first, _ForwardIterator __last);
2176 template <class _ForwardIterator>
2177 _ForwardIterator
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002178 __parse_RE_dupl_symbol(_ForwardIterator __first, _ForwardIterator __last,
Howard Hinnante77aa5e2010-07-08 17:43:58 +00002179 __owns_one_state<_CharT>* __s,
2180 unsigned __mexp_begin, unsigned __mexp_end);
Howard Hinnant0de86b62010-06-25 20:56:08 +00002181 template <class _ForwardIterator>
2182 _ForwardIterator
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002183 __parse_ERE_dupl_symbol(_ForwardIterator __first, _ForwardIterator __last);
2184 template <class _ForwardIterator>
2185 _ForwardIterator
Howard Hinnant0de86b62010-06-25 20:56:08 +00002186 __parse_bracket_expression(_ForwardIterator __first, _ForwardIterator __last);
2187 template <class _ForwardIterator>
2188 _ForwardIterator
2189 __parse_follow_list(_ForwardIterator __first, _ForwardIterator __last);
2190 template <class _ForwardIterator>
2191 _ForwardIterator
2192 __parse_expression_term(_ForwardIterator __first, _ForwardIterator __last);
2193 template <class _ForwardIterator>
2194 _ForwardIterator
2195 __parse_equivalence_class(_ForwardIterator __first, _ForwardIterator __last);
2196 template <class _ForwardIterator>
2197 _ForwardIterator
2198 __parse_character_class(_ForwardIterator __first, _ForwardIterator __last);
2199 template <class _ForwardIterator>
2200 _ForwardIterator
2201 __parse_collating_symbol(_ForwardIterator __first, _ForwardIterator __last);
2202 template <class _ForwardIterator>
2203 _ForwardIterator
2204 __parse_DUP_COUNT(_ForwardIterator __first, _ForwardIterator __last, int& __c);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002205 template <class _ForwardIterator>
2206 _ForwardIterator
2207 __parse_extended_reg_exp(_ForwardIterator __first, _ForwardIterator __last);
2208 template <class _ForwardIterator>
2209 _ForwardIterator
2210 __parse_ERE_branch(_ForwardIterator __first, _ForwardIterator __last);
2211 template <class _ForwardIterator>
2212 _ForwardIterator
2213 __parse_ERE_expression(_ForwardIterator __first, _ForwardIterator __last);
2214 template <class _ForwardIterator>
2215 _ForwardIterator
2216 __parse_one_char_or_coll_elem_ERE(_ForwardIterator __first, _ForwardIterator __last);
2217 template <class _ForwardIterator>
2218 _ForwardIterator
2219 __parse_ORD_CHAR_ERE(_ForwardIterator __first, _ForwardIterator __last);
2220 template <class _ForwardIterator>
2221 _ForwardIterator
2222 __parse_QUOTED_CHAR_ERE(_ForwardIterator __first, _ForwardIterator __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002223
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00002224 void __push_l_anchor() {__left_anchor_ = true;}
2225 void __push_r_anchor();
Howard Hinnantac303862010-07-12 15:51:17 +00002226 void __push_match_any();
Howard Hinnante77aa5e2010-07-08 17:43:58 +00002227 void __push_greedy_inf_repeat(size_t __min, __owns_one_state<_CharT>* __s,
2228 unsigned __mexp_begin = 0, unsigned __mexp_end = 0)
2229 {__push_loop(__min, numeric_limits<size_t>::max(), __s,
2230 __mexp_begin, __mexp_end);}
Howard Hinnant0de86b62010-06-25 20:56:08 +00002231 void __push_exact_repeat(int __count) {}
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002232 void __push_loop(size_t __min, size_t __max, __owns_one_state<_CharT>* __s,
2233 size_t __mexp_begin = 0, size_t __mexp_end = 0,
2234 bool __greedy = true);
Howard Hinnant0de86b62010-06-25 20:56:08 +00002235 void __start_nonmatching_list() {}
2236 void __start_matching_list() {}
2237 void __end_nonmatching_list() {}
2238 void __end_matching_list() {}
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002239 void __push_char(value_type __c);
Howard Hinnant0de86b62010-06-25 20:56:08 +00002240 void __push_char(const typename _Traits::string_type& __c) {}
2241 void __push_range() {}
2242 void __push_class_type(typename _Traits::char_class_type) {}
Howard Hinnantcba352d2010-07-12 18:16:05 +00002243 void __push_back_ref(int __i);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002244 void __push_alternation() {}
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00002245 void __push_begin_marked_subexpression();
2246 void __push_end_marked_subexpression(unsigned);
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00002247
2248 template <class _BidirectionalIterator, class _Allocator>
2249 bool
2250 __search(_BidirectionalIterator __first, _BidirectionalIterator __last,
2251 match_results<_BidirectionalIterator, _Allocator>& __m,
2252 regex_constants::match_flag_type __flags) const;
2253
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002254 template <class _BidirectionalIterator, class _Allocator>
2255 bool
2256 __match_at_start(_BidirectionalIterator __first, _BidirectionalIterator __last,
2257 match_results<_BidirectionalIterator, _Allocator>& __m,
2258 vector<size_t>& __lc,
2259 regex_constants::match_flag_type __flags) const;
2260 template <class _BidirectionalIterator, class _Allocator>
2261 bool
2262 __match_at_start_ecma(_BidirectionalIterator __first, _BidirectionalIterator __last,
2263 match_results<_BidirectionalIterator, _Allocator>& __m,
2264 regex_constants::match_flag_type __flags) const;
2265 template <class _BidirectionalIterator, class _Allocator>
2266 bool
2267 __match_at_start_posix_nosubs(const _CharT* __first, const _CharT* __last,
2268 match_results<_BidirectionalIterator, _Allocator>& __m,
2269 vector<size_t>& __lc,
2270 regex_constants::match_flag_type __flags) const;
2271 template <class _BidirectionalIterator, class _Allocator>
2272 bool
2273 __match_at_start_posix_subs(_BidirectionalIterator __first, _BidirectionalIterator __last,
2274 match_results<_BidirectionalIterator, _Allocator>& __m,
Howard Hinnante77aa5e2010-07-08 17:43:58 +00002275 vector<size_t>& __lc,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002276 regex_constants::match_flag_type __flags) const;
2277
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00002278 template <class _B, class _A, class _C, class _T>
2279 friend
2280 bool
2281 regex_search(_B, _B, match_results<_B, _A>&, const basic_regex<_C, _T>&,
2282 regex_constants::match_flag_type);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002283
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002284};
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002285
2286template <class _CharT, class _Traits>
2287basic_regex<_CharT, _Traits>::~basic_regex()
2288{
2289}
2290
2291template <class _CharT, class _Traits>
2292template <class _ForwardIterator>
2293void
2294basic_regex<_CharT, _Traits>::__parse(_ForwardIterator __first,
2295 _ForwardIterator __last)
2296{
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002297 {
Howard Hinnantac303862010-07-12 15:51:17 +00002298 unique_ptr<__node> __h(new __end_state<_CharT>);
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002299 __start_.reset(new __empty_state<_CharT>(__h.get()));
2300 __h.release();
2301 __end_ = __start_.get();
2302 }
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002303 switch (__flags_ & 0x3F0)
2304 {
2305 case ECMAScript:
2306 break;
2307 case basic:
2308 __parse_basic_reg_exp(__first, __last);
2309 break;
2310 case extended:
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002311 __parse_extended_reg_exp(__first, __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002312 break;
2313 case awk:
2314 break;
2315 case grep:
2316 break;
2317 case egrep:
2318 break;
2319 default:
2320 throw regex_error(regex_constants::error_temp);
2321 }
2322}
2323
2324template <class _CharT, class _Traits>
2325template <class _ForwardIterator>
2326_ForwardIterator
2327basic_regex<_CharT, _Traits>::__parse_basic_reg_exp(_ForwardIterator __first,
2328 _ForwardIterator __last)
2329{
2330 if (__first != __last)
2331 {
2332 if (*__first == '^')
2333 {
2334 __push_l_anchor();
2335 ++__first;
2336 }
2337 if (__first != __last)
2338 {
2339 __first = __parse_RE_expression(__first, __last);
2340 if (__first != __last)
2341 {
2342 _ForwardIterator __temp = next(__first);
2343 if (__temp == __last && *__first == '$')
2344 {
2345 __push_r_anchor();
2346 ++__first;
2347 }
2348 }
2349 }
2350 if (__first != __last)
2351 throw regex_error(regex_constants::error_temp);
2352 }
2353 return __first;
2354}
2355
2356template <class _CharT, class _Traits>
2357template <class _ForwardIterator>
2358_ForwardIterator
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002359basic_regex<_CharT, _Traits>::__parse_extended_reg_exp(_ForwardIterator __first,
2360 _ForwardIterator __last)
2361{
2362 while (true)
2363 {
2364 _ForwardIterator __temp = __parse_ERE_branch(__first, __last);
2365 if (__temp == __first)
2366 throw regex_error(regex_constants::error_temp);
2367 __first = __temp;
2368 if (__first == __last)
2369 break;
2370 if (*__first != '|')
2371 throw regex_error(regex_constants::error_temp);
2372 __push_alternation();
2373 ++__first;
2374 }
2375 return __first;
2376}
2377
2378template <class _CharT, class _Traits>
2379template <class _ForwardIterator>
2380_ForwardIterator
2381basic_regex<_CharT, _Traits>::__parse_ERE_branch(_ForwardIterator __first,
2382 _ForwardIterator __last)
2383{
2384 _ForwardIterator __temp = __parse_ERE_expression(__first, __last);
2385 if (__temp == __first)
2386 throw regex_error(regex_constants::error_temp);
2387 do
2388 {
2389 __first = __temp;
2390 __temp = __parse_ERE_expression(__first, __last);
2391 } while (__temp != __first);
2392 return __first;
2393}
2394
2395template <class _CharT, class _Traits>
2396template <class _ForwardIterator>
2397_ForwardIterator
2398basic_regex<_CharT, _Traits>::__parse_ERE_expression(_ForwardIterator __first,
2399 _ForwardIterator __last)
2400{
2401 _ForwardIterator __temp = __parse_one_char_or_coll_elem_ERE(__first, __last);
2402 if (__temp == __first && __temp != __last)
2403 {
2404 switch (*__temp)
2405 {
2406 case '^':
2407 __push_l_anchor();
2408 ++__temp;
2409 break;
2410 case '$':
2411 __push_r_anchor();
2412 ++__temp;
2413 break;
2414 case '(':
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00002415 __push_begin_marked_subexpression();
2416 unsigned __temp_count = __marked_count_;
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002417 ++__open_count_;
2418 __temp = __parse_extended_reg_exp(++__temp, __last);
2419 if (__temp == __last || *__temp != ')')
2420 throw regex_error(regex_constants::error_paren);
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00002421 __push_end_marked_subexpression(__temp_count);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002422 --__open_count_;
2423 ++__temp;
2424 break;
2425 }
2426 }
2427 if (__temp != __first)
2428 __temp = __parse_ERE_dupl_symbol(__temp, __last);
2429 __first = __temp;
2430 return __first;
2431}
2432
2433template <class _CharT, class _Traits>
2434template <class _ForwardIterator>
2435_ForwardIterator
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002436basic_regex<_CharT, _Traits>::__parse_RE_expression(_ForwardIterator __first,
2437 _ForwardIterator __last)
2438{
2439 while (true)
2440 {
2441 _ForwardIterator __temp = __parse_simple_RE(__first, __last);
2442 if (__temp == __first)
2443 break;
2444 __first = __temp;
2445 }
2446 return __first;
2447}
2448
2449template <class _CharT, class _Traits>
2450template <class _ForwardIterator>
2451_ForwardIterator
2452basic_regex<_CharT, _Traits>::__parse_simple_RE(_ForwardIterator __first,
2453 _ForwardIterator __last)
2454{
2455 if (__first != __last)
2456 {
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002457 __owns_one_state<_CharT>* __e = __end_;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00002458 unsigned __mexp_begin = __marked_count_;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002459 _ForwardIterator __temp = __parse_nondupl_RE(__first, __last);
2460 if (__temp != __first)
Howard Hinnante77aa5e2010-07-08 17:43:58 +00002461 __first = __parse_RE_dupl_symbol(__temp, __last, __e,
2462 __mexp_begin+1, __marked_count_+1);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002463 }
2464 return __first;
2465}
2466
2467template <class _CharT, class _Traits>
2468template <class _ForwardIterator>
2469_ForwardIterator
2470basic_regex<_CharT, _Traits>::__parse_nondupl_RE(_ForwardIterator __first,
2471 _ForwardIterator __last)
2472{
2473 _ForwardIterator __temp = __first;
2474 __first = __parse_one_char_or_coll_elem_RE(__first, __last);
2475 if (__temp == __first)
2476 {
2477 __temp = __parse_Back_open_paren(__first, __last);
2478 if (__temp != __first)
2479 {
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00002480 __push_begin_marked_subexpression();
2481 unsigned __temp_count = __marked_count_;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002482 __first = __parse_RE_expression(__temp, __last);
2483 __temp = __parse_Back_close_paren(__first, __last);
2484 if (__temp == __first)
2485 throw regex_error(regex_constants::error_paren);
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00002486 __push_end_marked_subexpression(__temp_count);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002487 __first = __temp;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002488 }
2489 else
2490 __first = __parse_BACKREF(__first, __last);
2491 }
2492 return __first;
2493}
2494
2495template <class _CharT, class _Traits>
2496template <class _ForwardIterator>
2497_ForwardIterator
2498basic_regex<_CharT, _Traits>::__parse_one_char_or_coll_elem_RE(
2499 _ForwardIterator __first,
2500 _ForwardIterator __last)
2501{
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002502 _ForwardIterator __temp = __parse_ORD_CHAR(__first, __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002503 if (__temp == __first)
2504 {
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002505 __temp = __parse_QUOTED_CHAR(__first, __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002506 if (__temp == __first)
2507 {
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002508 if (__temp != __last && *__temp == '.')
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002509 {
2510 __push_match_any();
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002511 ++__temp;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002512 }
2513 else
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002514 __temp = __parse_bracket_expression(__first, __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002515 }
2516 }
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002517 __first = __temp;
2518 return __first;
2519}
2520
2521template <class _CharT, class _Traits>
2522template <class _ForwardIterator>
2523_ForwardIterator
2524basic_regex<_CharT, _Traits>::__parse_one_char_or_coll_elem_ERE(
2525 _ForwardIterator __first,
2526 _ForwardIterator __last)
2527{
2528 _ForwardIterator __temp = __parse_ORD_CHAR_ERE(__first, __last);
2529 if (__temp == __first)
2530 {
2531 __temp = __parse_QUOTED_CHAR_ERE(__first, __last);
2532 if (__temp == __first)
2533 {
2534 if (__temp != __last && *__temp == '.')
2535 {
2536 __push_match_any();
2537 ++__temp;
2538 }
2539 else
2540 __temp = __parse_bracket_expression(__first, __last);
2541 }
2542 }
2543 __first = __temp;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002544 return __first;
2545}
2546
2547template <class _CharT, class _Traits>
2548template <class _ForwardIterator>
2549_ForwardIterator
2550basic_regex<_CharT, _Traits>::__parse_Back_open_paren(_ForwardIterator __first,
2551 _ForwardIterator __last)
2552{
2553 if (__first != __last)
2554 {
2555 _ForwardIterator __temp = next(__first);
2556 if (__temp != __last)
2557 {
2558 if (*__first == '\\' && *__temp == '(')
2559 __first = ++__temp;
2560 }
2561 }
2562 return __first;
2563}
2564
2565template <class _CharT, class _Traits>
2566template <class _ForwardIterator>
2567_ForwardIterator
2568basic_regex<_CharT, _Traits>::__parse_Back_close_paren(_ForwardIterator __first,
2569 _ForwardIterator __last)
2570{
2571 if (__first != __last)
2572 {
2573 _ForwardIterator __temp = next(__first);
2574 if (__temp != __last)
2575 {
2576 if (*__first == '\\' && *__temp == ')')
2577 __first = ++__temp;
2578 }
2579 }
2580 return __first;
2581}
2582
2583template <class _CharT, class _Traits>
2584template <class _ForwardIterator>
2585_ForwardIterator
2586basic_regex<_CharT, _Traits>::__parse_Back_open_brace(_ForwardIterator __first,
2587 _ForwardIterator __last)
2588{
2589 if (__first != __last)
2590 {
2591 _ForwardIterator __temp = next(__first);
2592 if (__temp != __last)
2593 {
2594 if (*__first == '\\' && *__temp == '{')
2595 __first = ++__temp;
2596 }
2597 }
2598 return __first;
2599}
2600
2601template <class _CharT, class _Traits>
2602template <class _ForwardIterator>
2603_ForwardIterator
2604basic_regex<_CharT, _Traits>::__parse_Back_close_brace(_ForwardIterator __first,
2605 _ForwardIterator __last)
2606{
2607 if (__first != __last)
2608 {
2609 _ForwardIterator __temp = next(__first);
2610 if (__temp != __last)
2611 {
2612 if (*__first == '\\' && *__temp == '}')
2613 __first = ++__temp;
2614 }
2615 }
2616 return __first;
2617}
2618
2619template <class _CharT, class _Traits>
2620template <class _ForwardIterator>
2621_ForwardIterator
2622basic_regex<_CharT, _Traits>::__parse_BACKREF(_ForwardIterator __first,
2623 _ForwardIterator __last)
2624{
2625 if (__first != __last)
2626 {
2627 _ForwardIterator __temp = next(__first);
2628 if (__temp != __last)
2629 {
2630 if (*__first == '\\' && '1' <= *__temp && *__temp <= '9')
2631 {
2632 __push_back_ref(*__temp - '0');
2633 __first = ++__temp;
2634 }
2635 }
2636 }
2637 return __first;
2638}
2639
2640template <class _CharT, class _Traits>
2641template <class _ForwardIterator>
2642_ForwardIterator
2643basic_regex<_CharT, _Traits>::__parse_ORD_CHAR(_ForwardIterator __first,
2644 _ForwardIterator __last)
2645{
2646 if (__first != __last)
2647 {
2648 _ForwardIterator __temp = next(__first);
2649 if (__temp == __last && *__first == '$')
2650 return __first;
2651 // Not called inside a bracket
2652 if (*__first == '.' || *__first == '\\' || *__first == '[')
2653 return __first;
Howard Hinnant0de86b62010-06-25 20:56:08 +00002654 __push_char(*__first);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002655 ++__first;
2656 }
2657 return __first;
2658}
2659
2660template <class _CharT, class _Traits>
2661template <class _ForwardIterator>
2662_ForwardIterator
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002663basic_regex<_CharT, _Traits>::__parse_ORD_CHAR_ERE(_ForwardIterator __first,
2664 _ForwardIterator __last)
2665{
2666 if (__first != __last)
2667 {
2668 switch (*__first)
2669 {
2670 case '^':
2671 case '.':
2672 case '[':
2673 case '$':
2674 case '(':
2675 case '|':
2676 case '*':
2677 case '+':
2678 case '?':
2679 case '{':
2680 case '\\':
2681 break;
2682 case ')':
2683 if (__open_count_ == 0)
2684 {
2685 __push_char(*__first);
2686 ++__first;
2687 }
2688 break;
2689 default:
2690 __push_char(*__first);
2691 ++__first;
2692 break;
2693 }
2694 }
2695 return __first;
2696}
2697
2698template <class _CharT, class _Traits>
2699template <class _ForwardIterator>
2700_ForwardIterator
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002701basic_regex<_CharT, _Traits>::__parse_QUOTED_CHAR(_ForwardIterator __first,
2702 _ForwardIterator __last)
2703{
2704 if (__first != __last)
2705 {
2706 _ForwardIterator __temp = next(__first);
2707 if (__temp != __last)
2708 {
2709 if (*__first == '\\')
2710 {
2711 switch (*__temp)
2712 {
2713 case '^':
2714 case '.':
2715 case '*':
2716 case '[':
2717 case '$':
2718 case '\\':
Howard Hinnant0de86b62010-06-25 20:56:08 +00002719 __push_char(*__temp);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002720 __first = ++__temp;
2721 break;
2722 }
2723 }
2724 }
2725 }
2726 return __first;
2727}
2728
2729template <class _CharT, class _Traits>
2730template <class _ForwardIterator>
2731_ForwardIterator
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002732basic_regex<_CharT, _Traits>::__parse_QUOTED_CHAR_ERE(_ForwardIterator __first,
2733 _ForwardIterator __last)
2734{
2735 if (__first != __last)
2736 {
2737 _ForwardIterator __temp = next(__first);
2738 if (__temp != __last)
2739 {
2740 if (*__first == '\\')
2741 {
2742 switch (*__temp)
2743 {
2744 case '^':
2745 case '.':
2746 case '*':
2747 case '[':
2748 case '$':
2749 case '\\':
2750 case '(':
2751 case ')':
2752 case '|':
2753 case '+':
2754 case '?':
2755 case '{':
2756 __push_char(*__temp);
2757 __first = ++__temp;
2758 break;
2759 }
2760 }
2761 }
2762 }
2763 return __first;
2764}
2765
2766template <class _CharT, class _Traits>
2767template <class _ForwardIterator>
2768_ForwardIterator
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002769basic_regex<_CharT, _Traits>::__parse_RE_dupl_symbol(_ForwardIterator __first,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002770 _ForwardIterator __last,
Howard Hinnante77aa5e2010-07-08 17:43:58 +00002771 __owns_one_state<_CharT>* __s,
2772 unsigned __mexp_begin,
2773 unsigned __mexp_end)
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002774{
2775 if (__first != __last)
2776 {
Howard Hinnant0de86b62010-06-25 20:56:08 +00002777 if (*__first == '*')
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002778 {
Howard Hinnante77aa5e2010-07-08 17:43:58 +00002779 __push_greedy_inf_repeat(0, __s, __mexp_begin, __mexp_end);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002780 ++__first;
2781 }
2782 else
2783 {
2784 _ForwardIterator __temp = __parse_Back_open_brace(__first, __last);
2785 if (__temp != __first)
2786 {
2787 int __min = 0;
2788 __first = __temp;
2789 __temp = __parse_DUP_COUNT(__first, __last, __min);
2790 if (__temp == __first)
2791 throw regex_error(regex_constants::error_badbrace);
2792 __first = __temp;
2793 if (__first == __last)
2794 throw regex_error(regex_constants::error_brace);
2795 if (*__first != ',')
2796 {
2797 __temp = __parse_Back_close_brace(__first, __last);
2798 if (__temp == __first)
2799 throw regex_error(regex_constants::error_brace);
Howard Hinnantcba352d2010-07-12 18:16:05 +00002800 __push_loop(__min, __min, __s, __mexp_begin, __mexp_end,
2801 true);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002802 __first = __temp;
2803 }
2804 else
2805 {
2806 ++__first; // consume ','
2807 int __max = -1;
2808 __first = __parse_DUP_COUNT(__first, __last, __max);
2809 __temp = __parse_Back_close_brace(__first, __last);
2810 if (__temp == __first)
2811 throw regex_error(regex_constants::error_brace);
2812 if (__max == -1)
Howard Hinnante77aa5e2010-07-08 17:43:58 +00002813 __push_greedy_inf_repeat(__min, __s, __mexp_end, __mexp_end);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002814 else
2815 {
2816 if (__max < __min)
2817 throw regex_error(regex_constants::error_badbrace);
Howard Hinnantcba352d2010-07-12 18:16:05 +00002818 __push_loop(__min, __max, __s, __mexp_begin, __mexp_end,
2819 true);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002820 }
2821 __first = __temp;
2822 }
2823 }
2824 }
2825 }
2826 return __first;
2827}
2828
Howard Hinnant0de86b62010-06-25 20:56:08 +00002829template <class _CharT, class _Traits>
2830template <class _ForwardIterator>
2831_ForwardIterator
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002832basic_regex<_CharT, _Traits>::__parse_ERE_dupl_symbol(_ForwardIterator __first,
2833 _ForwardIterator __last)
2834{
2835 if (__first != __last)
2836 {
2837 switch (*__first)
2838 {
2839 case '*':
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002840 __push_greedy_inf_repeat(0, nullptr);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002841 ++__first;
2842 break;
2843 case '+':
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002844 __push_greedy_inf_repeat(1, nullptr);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002845 ++__first;
2846 break;
2847 case '?':
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002848 __push_loop(0, 1, nullptr);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002849 ++__first;
2850 break;
2851 case '{':
2852 {
2853 int __min;
2854 _ForwardIterator __temp = __parse_DUP_COUNT(__first, __last, __min);
2855 if (__temp == __first)
2856 throw regex_error(regex_constants::error_badbrace);
2857 __first = __temp;
2858 if (__first == __last)
2859 throw regex_error(regex_constants::error_brace);
2860 switch (*__first)
2861 {
2862 case '}':
2863 __push_exact_repeat(__min);
2864 ++__first;
2865 break;
2866 case ',':
2867 if (++__first == __last)
2868 throw regex_error(regex_constants::error_badbrace);
2869 if (*__first == '}')
2870 {
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002871 __push_greedy_inf_repeat(__min, nullptr);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002872 ++__first;
2873 }
2874 else
2875 {
2876 int __max;
2877 __temp = __parse_DUP_COUNT(__first, __last, __max);
2878 if (__temp == __first)
2879 throw regex_error(regex_constants::error_brace);
2880 __first = __temp;
2881 if (__first == __last || *__first != '}')
2882 throw regex_error(regex_constants::error_brace);
2883 ++__first;
2884 if (__max < __min)
2885 throw regex_error(regex_constants::error_badbrace);
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002886 __push_loop(__min, __max, nullptr);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002887 }
2888 default:
2889 throw regex_error(regex_constants::error_badbrace);
2890 }
2891 }
2892 break;
2893 }
2894 }
2895 return __first;
2896}
2897
2898template <class _CharT, class _Traits>
2899template <class _ForwardIterator>
2900_ForwardIterator
Howard Hinnant0de86b62010-06-25 20:56:08 +00002901basic_regex<_CharT, _Traits>::__parse_bracket_expression(_ForwardIterator __first,
2902 _ForwardIterator __last)
2903{
2904 if (__first != __last && *__first == '[')
2905 {
2906 if (++__first == __last)
2907 throw regex_error(regex_constants::error_brack);
2908 bool __non_matching = false;
2909 if (*__first == '^')
2910 {
2911 ++__first;
2912 __non_matching = true;
2913 __start_nonmatching_list();
2914 }
2915 else
2916 __start_matching_list();
2917 if (__first == __last)
2918 throw regex_error(regex_constants::error_brack);
2919 if (*__first == ']')
2920 {
2921 __push_char(']');
2922 ++__first;
2923 }
2924 __first = __parse_follow_list(__first, __last);
2925 if (__first == __last)
2926 throw regex_error(regex_constants::error_brack);
2927 if (*__first == '-')
2928 {
2929 __push_char('-');
2930 ++__first;
2931 }
2932 if (__first == __last || *__first != ']')
2933 throw regex_error(regex_constants::error_brack);
2934 if (__non_matching)
2935 __end_nonmatching_list();
2936 else
2937 __end_matching_list();
2938 ++__first;
2939 }
2940 return __first;
2941}
2942
2943template <class _CharT, class _Traits>
2944template <class _ForwardIterator>
2945_ForwardIterator
2946basic_regex<_CharT, _Traits>::__parse_follow_list(_ForwardIterator __first,
2947 _ForwardIterator __last)
2948{
2949 if (__first != __last)
2950 {
2951 while (true)
2952 {
2953 _ForwardIterator __temp = __parse_expression_term(__first, __last);
2954 if (__temp == __first)
2955 break;
2956 __first = __temp;
2957 }
2958 }
2959 return __first;
2960}
2961
2962template <class _CharT, class _Traits>
2963template <class _ForwardIterator>
2964_ForwardIterator
2965basic_regex<_CharT, _Traits>::__parse_expression_term(_ForwardIterator __first,
2966 _ForwardIterator __last)
2967{
2968 if (__first != __last && *__first != ']')
2969 {
2970 bool __parsed_one = false;
2971 _ForwardIterator __temp = next(__first);
2972 if (__temp != __last && *__first == '[')
2973 {
2974 if (*__temp == '=')
2975 return __parse_equivalence_class(++__temp, __last);
2976 else if (*__temp == ':')
2977 return __parse_character_class(++__temp, __last);
2978 else if (*__temp == '.')
2979 {
2980 __first = __parse_collating_symbol(++__temp, __last);
2981 __parsed_one = true;
2982 }
2983 }
2984 if (!__parsed_one)
2985 {
2986 __push_char(*__first);
2987 ++__first;
2988 }
2989 if (__first != __last && *__first != ']')
2990 {
2991 __temp = next(__first);
2992 if (__temp != __last && *__first == '-' && *__temp != ']')
2993 {
2994 // parse a range
2995 __first = __temp;
2996 ++__temp;
2997 if (__temp != __last && *__first == '[' && *__temp == '.')
2998 __first = __parse_collating_symbol(++__temp, __last);
2999 else
3000 {
3001 __push_char(*__first);
3002 ++__first;
3003 }
3004 __push_range();
3005 }
3006 }
3007 }
3008 return __first;
3009}
3010
3011template <class _CharT, class _Traits>
3012template <class _ForwardIterator>
3013_ForwardIterator
3014basic_regex<_CharT, _Traits>::__parse_equivalence_class(_ForwardIterator __first,
3015 _ForwardIterator __last)
3016{
3017 // Found [=
3018 // This means =] must exist
3019 value_type _Equal_close[2] = {'=', ']'};
3020 _ForwardIterator __temp = _STD::search(__first, __last, _Equal_close,
3021 _Equal_close+2);
3022 if (__temp == __last)
3023 throw regex_error(regex_constants::error_brack);
3024 // [__first, __temp) contains all text in [= ... =]
3025 typedef typename _Traits::string_type string_type;
3026 string_type __collate_name =
3027 __traits_.lookup_collatename(__first, __temp);
3028 if (__collate_name.empty())
3029 throw regex_error(regex_constants::error_brack);
3030 string_type __equiv_name =
3031 __traits_.transform_primary(__collate_name.begin(),
3032 __collate_name.end());
3033 if (!__equiv_name.empty())
3034 __push_char(__equiv_name);
3035 else
3036 __push_char(__collate_name);
3037 __first = next(__temp, 2);
3038 return __first;
3039}
3040
3041template <class _CharT, class _Traits>
3042template <class _ForwardIterator>
3043_ForwardIterator
3044basic_regex<_CharT, _Traits>::__parse_character_class(_ForwardIterator __first,
3045 _ForwardIterator __last)
3046{
3047 // Found [:
3048 // This means :] must exist
3049 value_type _Colon_close[2] = {':', ']'};
3050 _ForwardIterator __temp = _STD::search(__first, __last, _Colon_close,
3051 _Colon_close+2);
3052 if (__temp == __last)
3053 throw regex_error(regex_constants::error_brack);
3054 // [__first, __temp) contains all text in [: ... :]
3055 typedef typename _Traits::char_class_type char_class_type;
3056 char_class_type __class_type =
3057 __traits_.lookup_classname(__first, __temp, __flags_ & icase);
3058 if (__class_type == 0)
3059 throw regex_error(regex_constants::error_brack);
3060 __push_class_type(__class_type);
3061 __first = next(__temp, 2);
3062 return __first;
3063}
3064
3065template <class _CharT, class _Traits>
3066template <class _ForwardIterator>
3067_ForwardIterator
3068basic_regex<_CharT, _Traits>::__parse_collating_symbol(_ForwardIterator __first,
3069 _ForwardIterator __last)
3070{
3071 // Found [.
3072 // This means .] must exist
3073 value_type _Dot_close[2] = {'.', ']'};
3074 _ForwardIterator __temp = _STD::search(__first, __last, _Dot_close,
3075 _Dot_close+2);
3076 if (__temp == __last)
3077 throw regex_error(regex_constants::error_brack);
3078 // [__first, __temp) contains all text in [. ... .]
3079 typedef typename _Traits::string_type string_type;
3080 string_type __collate_name =
3081 __traits_.lookup_collatename(__first, __temp);
3082 if (__collate_name.empty())
3083 throw regex_error(regex_constants::error_brack);
3084 __push_char(__collate_name);
3085 __first = next(__temp, 2);
3086 return __first;
3087}
3088
3089template <class _CharT, class _Traits>
3090template <class _ForwardIterator>
3091_ForwardIterator
3092basic_regex<_CharT, _Traits>::__parse_DUP_COUNT(_ForwardIterator __first,
3093 _ForwardIterator __last,
3094 int& __c)
3095{
3096 if (__first != __last && '0' <= *__first && *__first <= '9')
3097 {
3098 __c = *__first - '0';
3099 for (++__first; __first != __last && '0' <= *__first && *__first <= '9';
3100 ++__first)
3101 {
3102 __c *= 10;
3103 __c += *__first - '0';
3104 }
3105 }
3106 return __first;
3107}
3108
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003109template <class _CharT, class _Traits>
3110void
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003111basic_regex<_CharT, _Traits>::__push_loop(size_t __min, size_t __max,
3112 __owns_one_state<_CharT>* __s, size_t __mexp_begin, size_t __mexp_end,
3113 bool __greedy)
3114{
3115 unique_ptr<__empty_state<_CharT> > __e1(new __empty_state<_CharT>(__end_->first()));
3116 __end_->first() = nullptr;
Howard Hinnantac303862010-07-12 15:51:17 +00003117 unique_ptr<__loop<_CharT> > __e2(new __loop<_CharT>(__loop_count_,
3118 __s->first(), __e1.get(), __mexp_begin, __mexp_end, __greedy,
3119 __min, __max));
3120 __s->first() = nullptr;
3121 __e1.release();
3122 __end_->first() = new __repeat_one_loop<_CharT>(__e2.get());
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003123 __end_ = __e2->second();
Howard Hinnantac303862010-07-12 15:51:17 +00003124 __s->first() = __e2.release();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003125 ++__loop_count_;
3126}
3127
3128template <class _CharT, class _Traits>
3129void
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003130basic_regex<_CharT, _Traits>::__push_char(value_type __c)
3131{
Howard Hinnante34f17d2010-07-12 19:11:27 +00003132 if (flags() & regex_constants::icase)
3133 __end_->first() = new __match_char_icase<_CharT, _Traits>
3134 (__traits_, __c, __end_->first());
3135 else if (flags() & regex_constants::collate)
3136 __end_->first() = new __match_char_collate<_CharT, _Traits>
3137 (__traits_, __c, __end_->first());
3138 else
3139 __end_->first() = new __match_char<_CharT>(__c, __end_->first());
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003140 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003141}
3142
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00003143template <class _CharT, class _Traits>
3144void
3145basic_regex<_CharT, _Traits>::__push_begin_marked_subexpression()
3146{
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003147 __end_->first() = new __begin_marked_subexpression<_CharT>(++__marked_count_,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003148 __end_->first());
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003149 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00003150}
3151
3152template <class _CharT, class _Traits>
3153void
3154basic_regex<_CharT, _Traits>::__push_end_marked_subexpression(unsigned __sub)
3155{
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003156 __end_->first() = new __end_marked_subexpression<_CharT>(__sub,
3157 __end_->first());
3158 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00003159}
3160
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00003161template <class _CharT, class _Traits>
3162void
3163basic_regex<_CharT, _Traits>::__push_r_anchor()
3164{
3165 __end_->first() = new __r_anchor<_CharT>(__end_->first());
3166 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
3167}
3168
Howard Hinnantac303862010-07-12 15:51:17 +00003169template <class _CharT, class _Traits>
3170void
3171basic_regex<_CharT, _Traits>::__push_match_any()
3172{
3173 __end_->first() = new __match_any<_CharT>(__end_->first());
3174 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
3175}
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00003176
Howard Hinnantcba352d2010-07-12 18:16:05 +00003177template <class _CharT, class _Traits>
3178void
3179basic_regex<_CharT, _Traits>::__push_back_ref(int __i)
3180{
Howard Hinnante34f17d2010-07-12 19:11:27 +00003181 if (flags() & regex_constants::icase)
3182 __end_->first() = new __back_ref_icase<_CharT, _Traits>
3183 (__traits_, __i, __end_->first());
3184 else if (flags() & regex_constants::collate)
3185 __end_->first() = new __back_ref_collate<_CharT, _Traits>
3186 (__traits_, __i, __end_->first());
3187 else
3188 __end_->first() = new __back_ref<_CharT>(__i, __end_->first());
Howard Hinnantcba352d2010-07-12 18:16:05 +00003189 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
3190}
3191
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003192typedef basic_regex<char> regex;
3193typedef basic_regex<wchar_t> wregex;
3194
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003195// sub_match
3196
3197template <class _BidirectionalIterator>
3198class sub_match
3199 : public pair<_BidirectionalIterator, _BidirectionalIterator>
3200{
3201public:
3202 typedef _BidirectionalIterator iterator;
3203 typedef typename iterator_traits<iterator>::value_type value_type;
3204 typedef typename iterator_traits<iterator>::difference_type difference_type;
3205 typedef basic_string<value_type> string_type;
3206
3207 bool matched;
3208
3209 difference_type length() const
3210 {return matched ? _STD::distance(this->first, this->second) : 0;}
3211 string_type str() const
3212 {return matched ? string_type(this->first, this->second) : string_type();}
3213 operator string_type() const
3214 {return str();}
3215
3216 int compare(const sub_match& __s) const
3217 {return str().compare(__s.str());}
3218 int compare(const string_type& __s) const
3219 {return str().compare(__s);}
3220 int compare(const value_type* __s) const
3221 {return str().compare(__s);}
3222};
3223
3224typedef sub_match<const char*> csub_match;
3225typedef sub_match<const wchar_t*> wcsub_match;
3226typedef sub_match<string::const_iterator> ssub_match;
3227typedef sub_match<wstring::const_iterator> wssub_match;
3228
3229template <class _BiIter>
3230inline _LIBCPP_INLINE_VISIBILITY
3231bool
3232operator==(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
3233{
3234 return __x.compare(__y) == 0;
3235}
3236
3237template <class _BiIter>
3238inline _LIBCPP_INLINE_VISIBILITY
3239bool
3240operator!=(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
3241{
3242 return !(__x == __y);
3243}
3244
3245template <class _BiIter>
3246inline _LIBCPP_INLINE_VISIBILITY
3247bool
3248operator<(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
3249{
3250 return __x.compare(__y) < 0;
3251}
3252
3253template <class _BiIter>
3254inline _LIBCPP_INLINE_VISIBILITY
3255bool
3256operator<=(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
3257{
3258 return !(__y < __x);
3259}
3260
3261template <class _BiIter>
3262inline _LIBCPP_INLINE_VISIBILITY
3263bool
3264operator>=(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
3265{
3266 return !(__x < __y);
3267}
3268
3269template <class _BiIter>
3270inline _LIBCPP_INLINE_VISIBILITY
3271bool
3272operator>(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
3273{
3274 return __y < __x;
3275}
3276
3277template <class _BiIter, class _ST, class _SA>
3278inline _LIBCPP_INLINE_VISIBILITY
3279bool
3280operator==(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
3281 const sub_match<_BiIter>& __y)
3282{
3283 return __y.compare(__x.c_str()) == 0;
3284}
3285
3286template <class _BiIter, class _ST, class _SA>
3287inline _LIBCPP_INLINE_VISIBILITY
3288bool
3289operator!=(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
3290 const sub_match<_BiIter>& __y)
3291{
3292 return !(__x == __y);
3293}
3294
3295template <class _BiIter, class _ST, class _SA>
3296inline _LIBCPP_INLINE_VISIBILITY
3297bool
3298operator<(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
3299 const sub_match<_BiIter>& __y)
3300{
3301 return __y.compare(__x.c_str()) > 0;
3302}
3303
3304template <class _BiIter, class _ST, class _SA>
3305inline _LIBCPP_INLINE_VISIBILITY
3306bool
3307operator>(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
3308 const sub_match<_BiIter>& __y)
3309{
3310 return __y < __x;
3311}
3312
3313template <class _BiIter, class _ST, class _SA>
3314inline _LIBCPP_INLINE_VISIBILITY
3315bool operator>=(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
3316 const sub_match<_BiIter>& __y)
3317{
3318 return !(__x < __y);
3319}
3320
3321template <class _BiIter, class _ST, class _SA>
3322inline _LIBCPP_INLINE_VISIBILITY
3323bool
3324operator<=(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
3325 const sub_match<_BiIter>& __y)
3326{
3327 return !(__y < __x);
3328}
3329
3330template <class _BiIter, class _ST, class _SA>
3331inline _LIBCPP_INLINE_VISIBILITY
3332bool
3333operator==(const sub_match<_BiIter>& __x,
3334 const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
3335{
3336 return __x.compare(__y.c_str()) == 0;
3337}
3338
3339template <class _BiIter, class _ST, class _SA>
3340inline _LIBCPP_INLINE_VISIBILITY
3341bool
3342operator!=(const sub_match<_BiIter>& __x,
3343 const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
3344{
3345 return !(__x == __y);
3346}
3347
3348template <class _BiIter, class _ST, class _SA>
3349inline _LIBCPP_INLINE_VISIBILITY
3350bool
3351operator<(const sub_match<_BiIter>& __x,
3352 const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
3353{
3354 return __x.compare(__y.c_str()) < 0;
3355}
3356
3357template <class _BiIter, class _ST, class _SA>
3358inline _LIBCPP_INLINE_VISIBILITY
3359bool operator>(const sub_match<_BiIter>& __x,
3360 const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
3361{
3362 return __y < __x;
3363}
3364
3365template <class _BiIter, class _ST, class _SA>
3366inline _LIBCPP_INLINE_VISIBILITY
3367bool
3368operator>=(const sub_match<_BiIter>& __x,
3369 const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
3370{
3371 return !(__x < __y);
3372}
3373
3374template <class _BiIter, class _ST, class _SA>
3375inline _LIBCPP_INLINE_VISIBILITY
3376bool
3377operator<=(const sub_match<_BiIter>& __x,
3378 const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
3379{
3380 return !(__y < __x);
3381}
3382
3383template <class _BiIter>
3384inline _LIBCPP_INLINE_VISIBILITY
3385bool
3386operator==(typename iterator_traits<_BiIter>::value_type const* __x,
3387 const sub_match<_BiIter>& __y)
3388{
3389 return __y.compare(__x) == 0;
3390}
3391
3392template <class _BiIter>
3393inline _LIBCPP_INLINE_VISIBILITY
3394bool
3395operator!=(typename iterator_traits<_BiIter>::value_type const* __x,
3396 const sub_match<_BiIter>& __y)
3397{
3398 return !(__x == __y);
3399}
3400
3401template <class _BiIter>
3402inline _LIBCPP_INLINE_VISIBILITY
3403bool
3404operator<(typename iterator_traits<_BiIter>::value_type const* __x,
3405 const sub_match<_BiIter>& __y)
3406{
3407 return __y.compare(__x) > 0;
3408}
3409
3410template <class _BiIter>
3411inline _LIBCPP_INLINE_VISIBILITY
3412bool
3413operator>(typename iterator_traits<_BiIter>::value_type const* __x,
3414 const sub_match<_BiIter>& __y)
3415{
3416 return __y < __x;
3417}
3418
3419template <class _BiIter>
3420inline _LIBCPP_INLINE_VISIBILITY
3421bool
3422operator>=(typename iterator_traits<_BiIter>::value_type const* __x,
3423 const sub_match<_BiIter>& __y)
3424{
3425 return !(__x < __y);
3426}
3427
3428template <class _BiIter>
3429inline _LIBCPP_INLINE_VISIBILITY
3430bool
3431operator<=(typename iterator_traits<_BiIter>::value_type const* __x,
3432 const sub_match<_BiIter>& __y)
3433{
3434 return !(__y < __x);
3435}
3436
3437template <class _BiIter>
3438inline _LIBCPP_INLINE_VISIBILITY
3439bool
3440operator==(const sub_match<_BiIter>& __x,
3441 typename iterator_traits<_BiIter>::value_type const* __y)
3442{
3443 return __x.compare(__y) == 0;
3444}
3445
3446template <class _BiIter>
3447inline _LIBCPP_INLINE_VISIBILITY
3448bool
3449operator!=(const sub_match<_BiIter>& __x,
3450 typename iterator_traits<_BiIter>::value_type const* __y)
3451{
3452 return !(__x == __y);
3453}
3454
3455template <class _BiIter>
3456inline _LIBCPP_INLINE_VISIBILITY
3457bool
3458operator<(const sub_match<_BiIter>& __x,
3459 typename iterator_traits<_BiIter>::value_type const* __y)
3460{
3461 return __x.compare(__y) < 0;
3462}
3463
3464template <class _BiIter>
3465inline _LIBCPP_INLINE_VISIBILITY
3466bool
3467operator>(const sub_match<_BiIter>& __x,
3468 typename iterator_traits<_BiIter>::value_type const* __y)
3469{
3470 return __y < __x;
3471}
3472
3473template <class _BiIter>
3474inline _LIBCPP_INLINE_VISIBILITY
3475bool
3476operator>=(const sub_match<_BiIter>& __x,
3477 typename iterator_traits<_BiIter>::value_type const* __y)
3478{
3479 return !(__x < __y);
3480}
3481
3482template <class _BiIter>
3483inline _LIBCPP_INLINE_VISIBILITY
3484bool
3485operator<=(const sub_match<_BiIter>& __x,
3486 typename iterator_traits<_BiIter>::value_type const* __y)
3487{
3488 return !(__y < __x);
3489}
3490
3491template <class _BiIter>
3492inline _LIBCPP_INLINE_VISIBILITY
3493bool
3494operator==(typename iterator_traits<_BiIter>::value_type const& __x,
3495 const sub_match<_BiIter>& __y)
3496{
3497 typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
3498 return __y.compare(string_type(1, __x)) == 0;
3499}
3500
3501template <class _BiIter>
3502inline _LIBCPP_INLINE_VISIBILITY
3503bool
3504operator!=(typename iterator_traits<_BiIter>::value_type const& __x,
3505 const sub_match<_BiIter>& __y)
3506{
3507 return !(__x == __y);
3508}
3509
3510template <class _BiIter>
3511inline _LIBCPP_INLINE_VISIBILITY
3512bool
3513operator<(typename iterator_traits<_BiIter>::value_type const& __x,
3514 const sub_match<_BiIter>& __y)
3515{
3516 typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
3517 return __y.compare(string_type(1, __x)) > 0;
3518}
3519
3520template <class _BiIter>
3521inline _LIBCPP_INLINE_VISIBILITY
3522bool
3523operator>(typename iterator_traits<_BiIter>::value_type const& __x,
3524 const sub_match<_BiIter>& __y)
3525{
3526 return __y < __x;
3527}
3528
3529template <class _BiIter>
3530inline _LIBCPP_INLINE_VISIBILITY
3531bool
3532operator>=(typename iterator_traits<_BiIter>::value_type const& __x,
3533 const sub_match<_BiIter>& __y)
3534{
3535 return !(__x < __y);
3536}
3537
3538template <class _BiIter>
3539inline _LIBCPP_INLINE_VISIBILITY
3540bool
3541operator<=(typename iterator_traits<_BiIter>::value_type const& __x,
3542 const sub_match<_BiIter>& __y)
3543{
3544 return !(__y < __x);
3545}
3546
3547template <class _BiIter>
3548inline _LIBCPP_INLINE_VISIBILITY
3549bool
3550operator==(const sub_match<_BiIter>& __x,
3551 typename iterator_traits<_BiIter>::value_type const& __y)
3552{
3553 typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
3554 return __x.compare(string_type(1, __y)) == 0;
3555}
3556
3557template <class _BiIter>
3558inline _LIBCPP_INLINE_VISIBILITY
3559bool
3560operator!=(const sub_match<_BiIter>& __x,
3561 typename iterator_traits<_BiIter>::value_type const& __y)
3562{
3563 return !(__x == __y);
3564}
3565
3566template <class _BiIter>
3567inline _LIBCPP_INLINE_VISIBILITY
3568bool
3569operator<(const sub_match<_BiIter>& __x,
3570 typename iterator_traits<_BiIter>::value_type const& __y)
3571{
3572 typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
3573 return __x.compare(string_type(1, __y)) < 0;
3574}
3575
3576template <class _BiIter>
3577inline _LIBCPP_INLINE_VISIBILITY
3578bool
3579operator>(const sub_match<_BiIter>& __x,
3580 typename iterator_traits<_BiIter>::value_type const& __y)
3581{
3582 return __y < __x;
3583}
3584
3585template <class _BiIter>
3586inline _LIBCPP_INLINE_VISIBILITY
3587bool
3588operator>=(const sub_match<_BiIter>& __x,
3589 typename iterator_traits<_BiIter>::value_type const& __y)
3590{
3591 return !(__x < __y);
3592}
3593
3594template <class _BiIter>
3595inline _LIBCPP_INLINE_VISIBILITY
3596bool
3597operator<=(const sub_match<_BiIter>& __x,
3598 typename iterator_traits<_BiIter>::value_type const& __y)
3599{
3600 return !(__y < __x);
3601}
3602
3603template <class _CharT, class _ST, class _BiIter>
3604inline _LIBCPP_INLINE_VISIBILITY
3605basic_ostream<_CharT, _ST>&
3606operator<<(basic_ostream<_CharT, _ST>& __os, const sub_match<_BiIter>& __m)
3607{
3608 return __os << __m.str();
3609}
3610
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00003611template <class _BidirectionalIterator,
3612 class _Allocator = allocator<sub_match<_BidirectionalIterator> > >
3613class match_results
3614{
3615public:
3616 typedef _Allocator allocator_type;
3617 typedef sub_match<_BidirectionalIterator> value_type;
3618private:
3619 typedef vector<value_type, allocator_type> __container_type;
3620
3621 __container_type __matches_;
3622 value_type __unmatched_;
3623 value_type __prefix_;
3624 value_type __suffix_;
3625public:
3626 typedef const value_type& const_reference;
3627 typedef const_reference reference;
3628 typedef typename __container_type::const_iterator const_iterator;
3629 typedef const_iterator iterator;
3630 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
3631 typedef typename allocator_traits<allocator_type>::size_type size_type;
3632 typedef typename iterator_traits<_BidirectionalIterator>::value_type char_type;
3633 typedef basic_string<char_type> string_type;
3634
3635 // construct/copy/destroy:
3636 explicit match_results(const allocator_type& __a = allocator_type());
3637// match_results(const match_results&) = default;
3638// match_results& operator=(const match_results&) = default;
3639#ifdef _LIBCPP_MOVE
3640// match_results(match_results&& __m) = default;
3641// match_results& operator=(match_results&& __m) = default;
3642#endif
3643// ~match_results() = default;
3644
3645 // size:
3646 size_type size() const {return __matches_.size();}
3647 size_type max_size() const {return __matches_.max_size();}
3648 bool empty() const {return size() == 0;}
3649
3650 // element access:
3651 difference_type length(size_type __sub = 0) const
3652 {return (*this)[__sub].length();}
3653 difference_type position(size_type __sub = 0) const
3654 {return _STD::distance(__prefix_.first, (*this)[__sub].first);}
3655 string_type str(size_type __sub = 0) const
3656 {return (*this)[__sub].str();}
3657 const_reference operator[](size_type __n) const
3658 {return __n < __matches_.size() ? __matches_[__n] : __unmatched_;}
3659
3660 const_reference prefix() const {return __prefix_;}
3661 const_reference suffix() const {return __suffix_;}
3662
3663 const_iterator begin() const {return empty() ? __matches_.end() : __matches_.begin() + 1;}
3664 const_iterator end() const {return __matches_.end();}
3665 const_iterator cbegin() const {return empty() ? __matches_.end() : __matches_.begin() + 1;}
3666 const_iterator cend() const {return __matches_.end();}
3667
3668 // format:
3669 template <class _OutputIter>
3670 _OutputIter
3671 format(_OutputIter __out, const char_type* __fmt_first,
3672 const char_type* __fmt_last,
3673 regex_constants::match_flag_type __flags = regex_constants::format_default) const;
3674 template <class _OutputIter, class _ST, class _SA>
3675 _OutputIter
3676 format(_OutputIter __out, const basic_string<char_type, _ST, _SA>& __fmt,
3677 regex_constants::match_flag_type __flags = regex_constants::format_default) const;
3678 template <class _ST, class _SA>
3679 basic_string<char_type, _ST, _SA>
3680 format(const basic_string<char_type, _ST, _SA>& __fmt,
3681 regex_constants::match_flag_type __flags = regex_constants::format_default) const;
3682 string_type
3683 format(const char_type* __fmt,
3684 regex_constants::match_flag_type __flags = regex_constants::format_default) const;
3685
3686 // allocator:
3687 allocator_type get_allocator() const {return __matches_.get_allocator();}
3688
3689 // swap:
3690 void swap(match_results& __m);
3691
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00003692private:
3693 void __init(unsigned __s,
3694 _BidirectionalIterator __f, _BidirectionalIterator __l);
3695
3696 template <class, class> friend class basic_regex;
3697
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00003698 template <class _B, class _A, class _C, class _T>
3699 friend
3700 bool
3701 regex_match(_B, _B, match_results<_B, _A>&, const basic_regex<_C, _T>&,
3702 regex_constants::match_flag_type);
3703};
3704
3705template <class _BidirectionalIterator, class _Allocator>
3706match_results<_BidirectionalIterator, _Allocator>::match_results(
3707 const allocator_type& __a)
3708 : __matches_(__a),
3709 __unmatched_(),
3710 __prefix_(),
3711 __suffix_()
3712{
3713}
3714
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00003715template <class _BidirectionalIterator, class _Allocator>
3716void
3717match_results<_BidirectionalIterator, _Allocator>::__init(unsigned __s,
3718 _BidirectionalIterator __f, _BidirectionalIterator __l)
3719{
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00003720 __unmatched_.first = __l;
3721 __unmatched_.second = __l;
3722 __unmatched_.matched = false;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003723 __matches_.assign(__s, __unmatched_);
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00003724 __prefix_.first = __f;
3725 __prefix_.second = __f;
3726 __prefix_.matched = false;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003727 __suffix_ = __unmatched_;
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00003728}
3729
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00003730typedef match_results<const char*> cmatch;
3731typedef match_results<const wchar_t*> wcmatch;
3732typedef match_results<string::const_iterator> smatch;
3733typedef match_results<wstring::const_iterator> wsmatch;
3734
3735template <class _BidirectionalIterator, class _Allocator>
3736 bool
3737 operator==(const match_results<_BidirectionalIterator, _Allocator>& __x,
3738 const match_results<_BidirectionalIterator, _Allocator>& __y);
3739
3740template <class _BidirectionalIterator, class _Allocator>
3741 bool
3742 operator!=(const match_results<_BidirectionalIterator, _Allocator>& __x,
3743 const match_results<_BidirectionalIterator, _Allocator>& __y);
3744
3745template <class _BidirectionalIterator, class _Allocator>
3746 void
3747 swap(match_results<_BidirectionalIterator, _Allocator>& __x,
3748 match_results<_BidirectionalIterator, _Allocator>& __y);
3749
3750// regex_search
3751
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00003752template <class _CharT, class _Traits>
3753template <class _BidirectionalIterator, class _Allocator>
3754bool
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003755basic_regex<_CharT, _Traits>::__match_at_start_ecma(
3756 _BidirectionalIterator __first, _BidirectionalIterator __last,
3757 match_results<_BidirectionalIterator, _Allocator>& __m,
3758 regex_constants::match_flag_type __flags) const
3759{
3760 return false;
3761}
3762
3763template <class _CharT, class _Traits>
3764template <class _BidirectionalIterator, class _Allocator>
3765bool
3766basic_regex<_CharT, _Traits>::__match_at_start_posix_nosubs(
3767 const _CharT* __first, const _CharT* __last,
3768 match_results<_BidirectionalIterator, _Allocator>& __m,
3769 vector<size_t>& __lc,
3770 regex_constants::match_flag_type __flags) const
3771{
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003772 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
Howard Hinnantac303862010-07-12 15:51:17 +00003773 deque<__state> __states;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003774 difference_type __j = 0;
3775 difference_type __highest_j = 0;
3776 difference_type _N = _STD::distance(__first, __last);
Howard Hinnantac303862010-07-12 15:51:17 +00003777 __node* __st = __start_.get();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003778 if (__st)
3779 {
Howard Hinnantac303862010-07-12 15:51:17 +00003780 __states.push_back(__state());
3781 __states.back().__do_ = __state::__consume_input;
3782 __states.push_back(__state());
3783 __states.back().__do_ = 0;
3784 __states.back().__first_ = __first;
3785 __states.back().__current_ = __first;
3786 __states.back().__last_ = __last;
3787 __states.back().__loop_data_.resize(__loop_count());
3788 __states.back().__node_ = __st;
3789 __states.back().__flags_ = __flags;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003790 _BidirectionalIterator __current = __first;
3791 do
3792 {
Howard Hinnantac303862010-07-12 15:51:17 +00003793 __state& __s = __states.back();
3794 if (__s.__node_)
3795 __s.__node_->__exec(__s);
3796 switch (__s.__do_)
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003797 {
Howard Hinnantac303862010-07-12 15:51:17 +00003798 case __state::__end_state:
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003799 __highest_j = _STD::max(__highest_j, __j);
Howard Hinnantac303862010-07-12 15:51:17 +00003800 if (__highest_j == _N)
3801 __states.clear();
3802 else
3803 __states.pop_back();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003804 break;
Howard Hinnantac303862010-07-12 15:51:17 +00003805 case __state::__consume_input:
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003806 if (__j == _N)
3807 return false;
3808 ++__current;
Howard Hinnantac303862010-07-12 15:51:17 +00003809 if (++__j != _N && __states.size() > 1)
3810 __states.push_front(_STD::move(__s));
3811 __states.pop_back();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003812 break;
Howard Hinnantac303862010-07-12 15:51:17 +00003813 case __state::__accept_and_consume:
3814 // needs to be changed for the case that this state
3815 // consumed more than one character. This will scan
3816 // down the deque and insert extra __consume_input
3817 // states as necessary
3818 __states.push_front(_STD::move(__s));
3819 __states.pop_back();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003820 break;
Howard Hinnantac303862010-07-12 15:51:17 +00003821 case __state::__repeat:
3822 case __state::__accept_but_not_consume:
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003823 break;
Howard Hinnantac303862010-07-12 15:51:17 +00003824 case __state::__split:
3825 {
3826 __state __snext = __s;
3827 __s.__node_->__exec_split(true, __s);
3828 __snext.__node_->__exec_split(false, __snext);
3829 __states.push_back(_STD::move(__snext));
3830 }
3831 break;
3832 case __state::__reject:
3833 __states.pop_back();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003834 break;
3835 default:
3836 throw regex_error(regex_constants::error_temp);
3837 break;
3838 }
Howard Hinnantac303862010-07-12 15:51:17 +00003839 } while (!__states.empty());
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003840 if (__highest_j != 0)
3841 {
3842 __m.__matches_[0].first = __first;
3843 __m.__matches_[0].second = _STD::next(__first, __highest_j);
3844 __m.__matches_[0].matched = true;
3845 return true;
3846 }
3847 }
3848 return false;
3849}
3850
3851template <class _CharT, class _Traits>
3852template <class _BidirectionalIterator, class _Allocator>
3853bool
3854basic_regex<_CharT, _Traits>::__match_at_start_posix_subs(
3855 _BidirectionalIterator __first, _BidirectionalIterator __last,
3856 match_results<_BidirectionalIterator, _Allocator>& __m,
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003857 vector<size_t>& __lc,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003858 regex_constants::match_flag_type __flags) const
3859{
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003860 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
Howard Hinnantac303862010-07-12 15:51:17 +00003861 vector<__state> __states;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003862 vector<_BidirectionalIterator> __current_stack;
3863 vector<sub_match<_BidirectionalIterator> > __saved_matches;
Howard Hinnantac303862010-07-12 15:51:17 +00003864 __state __best_state;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003865 difference_type __j = 0;
3866 difference_type __highest_j = 0;
3867 difference_type _N = _STD::distance(__first, __last);
Howard Hinnantac303862010-07-12 15:51:17 +00003868 __node* __st = __start_.get();
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003869 if (__st)
3870 {
Howard Hinnantac303862010-07-12 15:51:17 +00003871 __states.push_back(__state());
3872 __states.back().__do_ = 0;
3873 __states.back().__first_ = __first;
3874 __states.back().__current_ = __first;
3875 __states.back().__last_ = __last;
3876 __states.back().__sub_matches_.resize(mark_count());
3877 __states.back().__loop_data_.resize(__loop_count());
3878 __states.back().__node_ = __st;
3879 __states.back().__flags_ = __flags;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003880 _BidirectionalIterator __current = __first;
Howard Hinnantac303862010-07-12 15:51:17 +00003881 bool __matched = false;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003882 do
3883 {
Howard Hinnantac303862010-07-12 15:51:17 +00003884 __state& __s = __states.back();
3885 if (__s.__node_)
3886 __s.__node_->__exec(__s);
3887 switch (__s.__do_)
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003888 {
Howard Hinnantac303862010-07-12 15:51:17 +00003889 case __state::__end_state:
3890 if (__j == 0 || __highest_j < __j)
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003891 {
Howard Hinnantac303862010-07-12 15:51:17 +00003892 __matched = true;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003893 __highest_j = __j;
Howard Hinnantac303862010-07-12 15:51:17 +00003894 __best_state = __s;
3895 if (__highest_j == _N || __highest_j == 0)
3896 __states.clear();
3897 else
3898 __states.pop_back();
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003899 }
3900 break;
Howard Hinnantac303862010-07-12 15:51:17 +00003901 case __state::__accept_and_consume:
Howard Hinnantcba352d2010-07-12 18:16:05 +00003902 __j += __s.__current_ - __current;
3903 __current = __s.__current_;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003904 break;
Howard Hinnantac303862010-07-12 15:51:17 +00003905 case __state::__repeat:
3906 case __state::__accept_but_not_consume:
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003907 break;
Howard Hinnantac303862010-07-12 15:51:17 +00003908 case __state::__split:
3909 {
3910 __state __snext = __s;
3911 __s.__node_->__exec_split(true, __s);
3912 __snext.__node_->__exec_split(false, __snext);
3913 __states.push_back(_STD::move(__snext));
3914 }
3915 break;
3916 case __state::__reject:
3917 __states.pop_back();
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003918 break;
3919 default:
3920 throw regex_error(regex_constants::error_temp);
3921 break;
3922 }
Howard Hinnantac303862010-07-12 15:51:17 +00003923 } while (!__states.empty());
3924 if (__matched)
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003925 {
3926 __m.__matches_[0].first = __first;
3927 __m.__matches_[0].second = _STD::next(__first, __highest_j);
3928 __m.__matches_[0].matched = true;
Howard Hinnantac303862010-07-12 15:51:17 +00003929 for (unsigned __i = 0; __i < __best_state.__sub_matches_.size(); ++__i)
3930 __m.__matches_[__i+1] = __best_state.__sub_matches_[__i];
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003931 return true;
3932 }
3933 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003934 return false;
3935}
3936
3937template <class _CharT, class _Traits>
3938template <class _BidirectionalIterator, class _Allocator>
3939bool
3940basic_regex<_CharT, _Traits>::__match_at_start(
3941 _BidirectionalIterator __first, _BidirectionalIterator __last,
3942 match_results<_BidirectionalIterator, _Allocator>& __m,
3943 vector<size_t>& __lc,
3944 regex_constants::match_flag_type __flags) const
3945{
3946 if (__flags_ & ECMAScript)
3947 return __match_at_start_ecma(__first, __last, __m, __flags);
3948 if (mark_count() == 0)
3949 return __match_at_start_posix_nosubs(__first, __last, __m, __lc, __flags);
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003950 return __match_at_start_posix_subs(__first, __last, __m, __lc, __flags);
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003951}
3952
3953template <class _CharT, class _Traits>
3954template <class _BidirectionalIterator, class _Allocator>
3955bool
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00003956basic_regex<_CharT, _Traits>::__search(
3957 _BidirectionalIterator __first, _BidirectionalIterator __last,
3958 match_results<_BidirectionalIterator, _Allocator>& __m,
3959 regex_constants::match_flag_type __flags) const
3960{
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00003961 if (__left_anchor_)
3962 __flags |= regex_constants::match_continuous;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003963 __m.__init(1 + mark_count(), __first, __last);
3964 vector<size_t> __lc(__loop_count());
3965 if (__match_at_start(__first, __last, __m, __lc, __flags))
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00003966 {
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003967 __m.__prefix_.second = __m[0].first;
3968 __m.__prefix_.matched = __m.__prefix_.first != __m.__prefix_.second;
3969 __m.__suffix_.first = __m[0].second;
3970 __m.__suffix_.matched = __m.__suffix_.first != __m.__suffix_.second;
3971 return true;
3972 }
3973 if (!(__flags & regex_constants::match_continuous))
3974 {
3975 __m.__matches_.assign(__m.size(), __m.__unmatched_);
3976 for (++__first; __first != __last; ++__first)
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00003977 {
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003978 if (__match_at_start(__first, __last, __m, __lc, __flags))
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00003979 {
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003980 __m.__prefix_.second = __m[0].first;
3981 __m.__prefix_.matched = __m.__prefix_.first != __m.__prefix_.second;
3982 __m.__suffix_.first = __m[0].second;
3983 __m.__suffix_.matched = __m.__suffix_.first != __m.__suffix_.second;
3984 return true;
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00003985 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003986 __m.__matches_.assign(__m.size(), __m.__unmatched_);
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00003987 }
3988 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003989 __m.__matches_.clear();
3990 return false;
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00003991}
3992
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00003993template <class _BidirectionalIterator, class _Allocator, class _CharT, class _Traits>
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00003994inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00003995bool
3996regex_search(_BidirectionalIterator __first, _BidirectionalIterator __last,
3997 match_results<_BidirectionalIterator, _Allocator>& __m,
3998 const basic_regex<_CharT, _Traits>& __e,
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00003999 regex_constants::match_flag_type __flags = regex_constants::match_default)
4000{
4001 return __e.__search(__first, __last, __m, __flags);
4002}
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00004003
4004template <class _BidirectionalIterator, class _CharT, class _Traits>
4005inline _LIBCPP_INLINE_VISIBILITY
4006bool
4007regex_search(_BidirectionalIterator __first, _BidirectionalIterator __last,
4008 const basic_regex<_CharT, _Traits>& __e,
4009 regex_constants::match_flag_type __flags = regex_constants::match_default)
4010{
4011 match_results<_BidirectionalIterator> __m;
4012 return _STD::regex_search(__first, __last, __m, __e, __flags);
4013}
4014
4015template <class _CharT, class _Allocator, class _Traits>
4016inline _LIBCPP_INLINE_VISIBILITY
4017bool
4018regex_search(const _CharT* __str, match_results<const _CharT*, _Allocator>& __m,
4019 const basic_regex<_CharT, _Traits>& __e,
4020 regex_constants::match_flag_type __flags = regex_constants::match_default)
4021{
4022 return _STD::regex_search(__str, __str + _Traits::length(__str), __m, __e, __flags);
4023}
4024
4025template <class _CharT, class _Traits>
4026inline _LIBCPP_INLINE_VISIBILITY
4027bool
4028regex_search(const _CharT* __str, const basic_regex<_CharT, _Traits>& __e,
4029 regex_constants::match_flag_type __flags = regex_constants::match_default)
4030{
4031 return _STD::regex_search(__str, __str + _Traits::length(__str), __e, __flags);
4032}
4033
4034template <class _ST, class _SA, class _CharT, class _Traits>
4035inline _LIBCPP_INLINE_VISIBILITY
4036bool
4037regex_search(const basic_string<_CharT, _ST, _SA>& __s,
4038 const basic_regex<_CharT, _Traits>& __e,
4039 regex_constants::match_flag_type __flags = regex_constants::match_default)
4040{
4041 return _STD::regex_search(__s.begin(), __s.end(), __e, __flags);
4042}
4043
4044template <class _ST, class _SA, class _Allocator, class _CharT, class _Traits>
4045inline _LIBCPP_INLINE_VISIBILITY
4046bool
4047regex_search(const basic_string<_CharT, _ST, _SA>& __s,
4048 match_results<typename basic_string<_CharT, _ST, _SA>::const_iterator, _Allocator>& __m,
4049 const basic_regex<_CharT, _Traits>& __e,
4050 regex_constants::match_flag_type __flags = regex_constants::match_default)
4051{
4052 return _STD::regex_search(__s.begin(), __s.end(), __m, __e, __flags);
4053}
4054
4055// regex_match
4056
4057template <class _BidirectionalIterator, class _Allocator, class _CharT, class _Traits>
4058bool
4059regex_match(_BidirectionalIterator __first, _BidirectionalIterator __last,
4060 match_results<_BidirectionalIterator, _Allocator>& __m,
4061 const basic_regex<_CharT, _Traits>& __e,
4062 regex_constants::match_flag_type __flags = regex_constants::match_default)
4063{
4064 bool __r = _STD::regex_search(__first, __last, __m, __e,
4065 __flags | regex_constants::match_continuous);
4066 if (__r)
4067 {
4068 __r = !__m.suffix().matched;
4069 if (!__r)
4070 __m.__matches_.clear();
4071 }
4072 return __r;
4073}
4074
4075template <class _BidirectionalIterator, class _CharT, class _Traits>
4076inline _LIBCPP_INLINE_VISIBILITY
4077bool
4078regex_match(_BidirectionalIterator __first, _BidirectionalIterator __last,
4079 const basic_regex<_CharT, _Traits>& __e,
4080 regex_constants::match_flag_type __flags = regex_constants::match_default)
4081{
4082 match_results<_BidirectionalIterator> __m;
4083 return _STD::regex_match(__first, __last, __m, __e, __flags);
4084}
4085
4086template <class _CharT, class _Allocator, class _Traits>
4087inline _LIBCPP_INLINE_VISIBILITY
4088bool
4089regex_match(const _CharT* __str, match_results<const _CharT*, _Allocator>& __m,
4090 const basic_regex<_CharT, _Traits>& __e,
4091 regex_constants::match_flag_type __flags = regex_constants::match_default)
4092{
4093 return _STD::regex_match(__str, __str + _Traits::length(__str), __m, __e, __flags);
4094}
4095
4096template <class _ST, class _SA, class _Allocator, class _CharT, class _Traits>
4097inline _LIBCPP_INLINE_VISIBILITY
4098bool
4099regex_match(const basic_string<_CharT, _ST, _SA>& __s,
4100 match_results<typename basic_string<_CharT, _ST, _SA>::const_iterator, _Allocator>& __m,
4101 const basic_regex<_CharT, _Traits>& __e,
4102 regex_constants::match_flag_type __flags = regex_constants::match_default)
4103{
4104 return _STD::regex_match(__s.begin(), __s.end(), __m, __e, __flags);
4105}
4106
4107template <class _CharT, class _Traits>
4108inline _LIBCPP_INLINE_VISIBILITY
4109bool
4110regex_match(const _CharT* __str, const basic_regex<_CharT, _Traits>& __e,
4111 regex_constants::match_flag_type __flags = regex_constants::match_default)
4112{
4113 return _STD::regex_match(__str, __str + _Traits::length(__str), __e, __flags);
4114}
4115
4116template <class _ST, class _SA, class _CharT, class _Traits>
4117inline _LIBCPP_INLINE_VISIBILITY
4118bool
4119regex_match(const basic_string<_CharT, _ST, _SA>& __s,
4120 const basic_regex<_CharT, _Traits>& __e,
4121 regex_constants::match_flag_type __flags = regex_constants::match_default)
4122{
4123 return _STD::regex_match(__s.begin(), __s.end(), __e, __flags);
4124}
4125
Howard Hinnant3257c982010-06-17 00:34:59 +00004126_LIBCPP_END_NAMESPACE_STD
4127
4128#endif // _LIBCPP_REGEX