blob: dc53498858fd10d13c220e0cc969cd20d0c8e9f6 [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,
Howard Hinnantad2a7ab2010-07-27 17:24:17 +0000750 ECMAScript = 0,
751 basic = 1 << 4,
752 extended = 1 << 5,
753 awk = 1 << 6,
754 grep = 1 << 7,
755 egrep = 1 << 8
Howard Hinnant3257c982010-06-17 00:34:59 +0000756};
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,
Howard Hinnanta712c722010-08-16 20:21:16 +0000833 format_first_only = 1 << 10,
834 __no_update_pos = 1 << 11
Howard Hinnant3257c982010-06-17 00:34:59 +0000835};
836
837inline
838/*constexpr*/
839match_flag_type
840operator~(match_flag_type __x)
841{
842 return match_flag_type(~int(__x));
843}
844
845inline
846/*constexpr*/
847match_flag_type
848operator&(match_flag_type __x, match_flag_type __y)
849{
850 return match_flag_type(int(__x) & int(__y));
851}
852
853inline
854/*constexpr*/
855match_flag_type
856operator|(match_flag_type __x, match_flag_type __y)
857{
858 return match_flag_type(int(__x) | int(__y));
859}
860
861inline
862/*constexpr*/
863match_flag_type
864operator^(match_flag_type __x, match_flag_type __y)
865{
866 return match_flag_type(int(__x) ^ int(__y));
867}
868
869inline
870/*constexpr*/
871match_flag_type&
872operator&=(match_flag_type& __x, match_flag_type __y)
873{
874 __x = __x & __y;
875 return __x;
876}
877
878inline
879/*constexpr*/
880match_flag_type&
881operator|=(match_flag_type& __x, match_flag_type __y)
882{
883 __x = __x | __y;
884 return __x;
885}
886
887inline
888/*constexpr*/
889match_flag_type&
890operator^=(match_flag_type& __x, match_flag_type __y)
891{
892 __x = __x ^ __y;
893 return __x;
894}
895
896enum error_type
897{
898 error_collate = 1,
899 error_ctype,
900 error_escape,
901 error_backref,
902 error_brack,
903 error_paren,
904 error_brace,
905 error_badbrace,
906 error_range,
907 error_space,
908 error_badrepeat,
909 error_complexity,
Howard Hinnant8c2c18d2010-06-24 21:28:00 +0000910 error_stack,
Howard Hinnantad2a7ab2010-07-27 17:24:17 +0000911 __re_err_grammar,
912 __re_err_empty,
913 __re_err_unknown
Howard Hinnant3257c982010-06-17 00:34:59 +0000914};
915
916} // regex_constants
917
918class _LIBCPP_EXCEPTION_ABI regex_error
919 : public runtime_error
920{
921 regex_constants::error_type __code_;
922public:
923 explicit regex_error(regex_constants::error_type __ecode);
924 virtual ~regex_error() throw();
925 regex_constants::error_type code() const {return __code_;}
926};
927
928template <class _CharT>
929struct regex_traits
930{
931public:
932 typedef _CharT char_type;
933 typedef basic_string<char_type> string_type;
934 typedef locale locale_type;
Howard Hinnantf409d2f2010-06-21 21:01:43 +0000935 typedef ctype_base::mask char_class_type;
Howard Hinnant3257c982010-06-17 00:34:59 +0000936
Howard Hinnantf409d2f2010-06-21 21:01:43 +0000937 static const char_class_type __regex_word = 0x80;
Howard Hinnant3257c982010-06-17 00:34:59 +0000938private:
939 locale __loc_;
940 const ctype<char_type>* __ct_;
941 const collate<char_type>* __col_;
942
943public:
944 regex_traits();
945
946 static size_t length(const char_type* __p)
947 {return char_traits<char_type>::length(__p);}
948 char_type translate(char_type __c) const {return __c;}
949 char_type translate_nocase(char_type __c) const;
950 template <class _ForwardIterator>
951 string_type
952 transform(_ForwardIterator __f, _ForwardIterator __l) const;
953 template <class _ForwardIterator>
954 string_type
955 transform_primary( _ForwardIterator __f, _ForwardIterator __l) const
956 {return __transform_primary(__f, __l, char_type());}
957 template <class _ForwardIterator>
958 string_type
959 lookup_collatename(_ForwardIterator __f, _ForwardIterator __l) const
960 {return __lookup_collatename(__f, __l, char_type());}
961 template <class _ForwardIterator>
962 char_class_type
963 lookup_classname(_ForwardIterator __f, _ForwardIterator __l,
Howard Hinnantf409d2f2010-06-21 21:01:43 +0000964 bool __icase = false) const
965 {return __lookup_classname(__f, __l, __icase, char_type());}
966 bool isctype(char_type __c, char_class_type __m) const;
967 int value(char_type __ch, int __radix) const
968 {return __value(__ch, __radix);}
Howard Hinnant3257c982010-06-17 00:34:59 +0000969 locale_type imbue(locale_type __l);
970 locale_type getloc()const {return __loc_;}
971
972private:
973 void __init();
974
975 template <class _ForwardIterator>
976 string_type
977 __transform_primary(_ForwardIterator __f, _ForwardIterator __l, char) const;
978 template <class _ForwardIterator>
979 string_type
980 __transform_primary(_ForwardIterator __f, _ForwardIterator __l, wchar_t) const;
981
982 template <class _ForwardIterator>
983 string_type
984 __lookup_collatename(_ForwardIterator __f, _ForwardIterator __l, char) const;
985 template <class _ForwardIterator>
986 string_type
987 __lookup_collatename(_ForwardIterator __f, _ForwardIterator __l, wchar_t) const;
Howard Hinnantf409d2f2010-06-21 21:01:43 +0000988
989 template <class _ForwardIterator>
990 char_class_type
991 __lookup_classname(_ForwardIterator __f, _ForwardIterator __l,
992 bool __icase, char) const;
993 template <class _ForwardIterator>
994 char_class_type
995 __lookup_classname(_ForwardIterator __f, _ForwardIterator __l,
996 bool __icase, wchar_t) const;
997
998 static int __value(unsigned char __ch, int __radix);
999 int __value(char __ch, int __radix) const
1000 {return __value(static_cast<unsigned char>(__ch), __radix);}
1001 int __value(wchar_t __ch, int __radix) const;
Howard Hinnant3257c982010-06-17 00:34:59 +00001002};
1003
1004template <class _CharT>
1005regex_traits<_CharT>::regex_traits()
1006{
1007 __init();
1008}
1009
1010template <class _CharT>
1011typename regex_traits<_CharT>::char_type
1012regex_traits<_CharT>::translate_nocase(char_type __c) const
1013{
1014 return __ct_->tolower(__c);
1015}
1016
1017template <class _CharT>
1018template <class _ForwardIterator>
1019typename regex_traits<_CharT>::string_type
1020regex_traits<_CharT>::transform(_ForwardIterator __f, _ForwardIterator __l) const
1021{
1022 string_type __s(__f, __l);
1023 return __col_->transform(__s.data(), __s.data() + __s.size());
1024}
1025
1026template <class _CharT>
1027void
1028regex_traits<_CharT>::__init()
1029{
1030 __ct_ = &use_facet<ctype<char_type> >(__loc_);
1031 __col_ = &use_facet<collate<char_type> >(__loc_);
1032}
1033
1034template <class _CharT>
1035typename regex_traits<_CharT>::locale_type
1036regex_traits<_CharT>::imbue(locale_type __l)
1037{
1038 locale __r = __loc_;
1039 __loc_ = __l;
1040 __init();
1041 return __r;
1042}
1043
1044// transform_primary is very FreeBSD-specific
1045
1046template <class _CharT>
1047template <class _ForwardIterator>
1048typename regex_traits<_CharT>::string_type
1049regex_traits<_CharT>::__transform_primary(_ForwardIterator __f,
1050 _ForwardIterator __l, char) const
1051{
1052 const string_type __s(__f, __l);
1053 string_type __d = __col_->transform(__s.data(), __s.data() + __s.size());
1054 switch (__d.size())
1055 {
1056 case 1:
1057 break;
1058 case 12:
1059 __d[11] = __d[3];
1060 break;
1061 default:
1062 __d.clear();
1063 break;
1064 }
1065 return __d;
1066}
1067
1068template <class _CharT>
1069template <class _ForwardIterator>
1070typename regex_traits<_CharT>::string_type
1071regex_traits<_CharT>::__transform_primary(_ForwardIterator __f,
1072 _ForwardIterator __l, wchar_t) const
1073{
1074 const string_type __s(__f, __l);
1075 string_type __d = __col_->transform(__s.data(), __s.data() + __s.size());
1076 switch (__d.size())
1077 {
1078 case 1:
1079 break;
1080 case 3:
1081 __d[2] = __d[0];
1082 break;
1083 default:
1084 __d.clear();
1085 break;
1086 }
1087 return __d;
1088}
1089
1090// lookup_collatename is very FreeBSD-specific
1091
Howard Hinnantf409d2f2010-06-21 21:01:43 +00001092string __get_collation_name(const char* __s);
Howard Hinnant3257c982010-06-17 00:34:59 +00001093
1094template <class _CharT>
1095template <class _ForwardIterator>
1096typename regex_traits<_CharT>::string_type
1097regex_traits<_CharT>::__lookup_collatename(_ForwardIterator __f,
1098 _ForwardIterator __l, char) const
1099{
1100 string_type __s(__f, __l);
1101 string_type __r;
1102 if (!__s.empty())
1103 {
1104 __r = __get_collation_name(__s.c_str());
1105 if (__r.empty() && __s.size() <= 2)
1106 {
1107 __r = __col_->transform(__s.data(), __s.data() + __s.size());
1108 if (__r.size() == 1 || __r.size() == 12)
1109 __r = __s;
1110 else
1111 __r.clear();
1112 }
1113 }
1114 return __r;
1115}
1116
1117template <class _CharT>
1118template <class _ForwardIterator>
1119typename regex_traits<_CharT>::string_type
1120regex_traits<_CharT>::__lookup_collatename(_ForwardIterator __f,
1121 _ForwardIterator __l, wchar_t) const
1122{
1123 string_type __s(__f, __l);
1124 string __n;
1125 __n.reserve(__s.size());
1126 for (typename string_type::const_iterator __i = __s.begin(), __e = __s.end();
1127 __i != __e; ++__i)
1128 {
1129 if (static_cast<unsigned>(*__i) >= 127)
1130 return string_type();
1131 __n.push_back(char(*__i));
1132 }
1133 string_type __r;
1134 if (!__s.empty())
1135 {
1136 __n = __get_collation_name(__n.c_str());
1137 if (!__n.empty())
1138 __r.assign(__n.begin(), __n.end());
1139 else if (__s.size() <= 2)
1140 {
1141 __r = __col_->transform(__s.data(), __s.data() + __s.size());
1142 if (__r.size() == 1 || __r.size() == 3)
1143 __r = __s;
1144 else
1145 __r.clear();
1146 }
1147 }
1148 return __r;
1149}
1150
Howard Hinnantf409d2f2010-06-21 21:01:43 +00001151// lookup_classname
1152
1153ctype_base::mask __get_classname(const char* __s, bool __icase);
1154
1155template <class _CharT>
1156template <class _ForwardIterator>
1157typename regex_traits<_CharT>::char_class_type
1158regex_traits<_CharT>::__lookup_classname(_ForwardIterator __f,
1159 _ForwardIterator __l,
1160 bool __icase, char) const
1161{
1162 string_type __s(__f, __l);
1163 __ct_->tolower(&__s[0], &__s[0] + __s.size());
1164 return __get_classname(__s.c_str(), __icase);
1165}
1166
1167template <class _CharT>
1168template <class _ForwardIterator>
1169typename regex_traits<_CharT>::char_class_type
1170regex_traits<_CharT>::__lookup_classname(_ForwardIterator __f,
1171 _ForwardIterator __l,
1172 bool __icase, wchar_t) const
1173{
1174 string_type __s(__f, __l);
1175 __ct_->tolower(&__s[0], &__s[0] + __s.size());
1176 string __n;
1177 __n.reserve(__s.size());
1178 for (typename string_type::const_iterator __i = __s.begin(), __e = __s.end();
1179 __i != __e; ++__i)
1180 {
1181 if (static_cast<unsigned>(*__i) >= 127)
1182 return char_class_type();
1183 __n.push_back(char(*__i));
1184 }
1185 return __get_classname(__n.c_str(), __icase);
1186}
1187
1188template <class _CharT>
1189bool
1190regex_traits<_CharT>::isctype(char_type __c, char_class_type __m) const
1191{
1192 if (__ct_->is(__m, __c))
1193 return true;
1194 return (__c == '_' && (__m & __regex_word));
1195}
1196
1197template <class _CharT>
1198int
1199regex_traits<_CharT>::__value(unsigned char __ch, int __radix)
1200{
1201 if ((__ch & 0xF8u) == 0x30) // '0' <= __ch && __ch <= '7'
1202 return __ch - '0';
1203 if (__radix != 8)
1204 {
1205 if ((__ch & 0xFEu) == 0x38) // '8' <= __ch && __ch <= '9'
1206 return __ch - '0';
1207 if (__radix == 16)
1208 {
1209 __ch |= 0x20; // tolower
1210 if ('a' <= __ch && __ch <= 'f')
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00001211 return __ch - ('a' - 10);
Howard Hinnantf409d2f2010-06-21 21:01:43 +00001212 }
1213 }
1214 return -1;
1215}
1216
1217template <class _CharT>
1218inline
1219int
1220regex_traits<_CharT>::__value(wchar_t __ch, int __radix) const
1221{
1222 return __value(static_cast<unsigned char>(__ct_->narrow(__ch, char_type())), __radix);
1223}
1224
Howard Hinnantac303862010-07-12 15:51:17 +00001225template <class _CharT> class __node;
1226
1227template <class _BidirectionalIterator> class sub_match;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001228
Howard Hinnant17615b02010-07-27 01:25:38 +00001229template <class _BidirectionalIterator,
1230 class _Allocator = allocator<sub_match<_BidirectionalIterator> > >
1231class match_results;
1232
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001233template <class _CharT>
Howard Hinnantac303862010-07-12 15:51:17 +00001234struct __state
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001235{
1236 enum
1237 {
1238 __end_state = -1000,
1239 __consume_input, // -999
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001240 __begin_marked_expr, // -998
1241 __end_marked_expr, // -997
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001242 __pop_state, // -996
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001243 __accept_and_consume, // -995
1244 __accept_but_not_consume, // -994
1245 __reject, // -993
Howard Hinnantac303862010-07-12 15:51:17 +00001246 __split,
1247 __repeat
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001248 };
1249
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001250 int __do_;
Howard Hinnantac303862010-07-12 15:51:17 +00001251 const _CharT* __first_;
1252 const _CharT* __current_;
1253 const _CharT* __last_;
1254 vector<sub_match<const _CharT*> > __sub_matches_;
1255 vector<pair<size_t, const _CharT*> > __loop_data_;
1256 const __node<_CharT>* __node_;
1257 regex_constants::match_flag_type __flags_;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001258
Howard Hinnantac303862010-07-12 15:51:17 +00001259 __state()
1260 : __do_(0), __first_(nullptr), __current_(nullptr), __last_(nullptr),
1261 __node_(nullptr), __flags_() {}
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001262};
1263
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001264template <class _CharT>
1265ostream&
Howard Hinnantac303862010-07-12 15:51:17 +00001266operator<<(ostream& os, const __state<_CharT>& c)
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001267{
1268 os << c.__do_;
Howard Hinnantac303862010-07-12 15:51:17 +00001269 if (c.__node_)
1270 os << ", " << c.__node_->speak();
1271else
1272 os << ", null";
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001273 return os;
1274}
1275
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001276
Howard Hinnantac303862010-07-12 15:51:17 +00001277// __node
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001278
1279template <class _CharT>
Howard Hinnantac303862010-07-12 15:51:17 +00001280class __node
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001281{
Howard Hinnantac303862010-07-12 15:51:17 +00001282 __node(const __node&);
1283 __node& operator=(const __node&);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001284public:
Howard Hinnantac303862010-07-12 15:51:17 +00001285 typedef _STD::__state<_CharT> __state;
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001286
Howard Hinnantac303862010-07-12 15:51:17 +00001287 __node() {}
1288 virtual ~__node() {}
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001289
Howard Hinnantac303862010-07-12 15:51:17 +00001290 virtual void __exec(__state&) const {};
1291 virtual void __exec_split(bool, __state&) const {};
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001292
Howard Hinnantac303862010-07-12 15:51:17 +00001293 virtual string speak() const {return "__node";}
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001294};
1295
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001296// __end_state
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001297
1298template <class _CharT>
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001299class __end_state
Howard Hinnantac303862010-07-12 15:51:17 +00001300 : public __node<_CharT>
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001301{
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001302public:
Howard Hinnantac303862010-07-12 15:51:17 +00001303 typedef _STD::__state<_CharT> __state;
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001304
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001305 __end_state() {}
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00001306
Howard Hinnantac303862010-07-12 15:51:17 +00001307 virtual void __exec(__state&) const;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001308
1309 virtual string speak() const {return "end state";}
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001310};
1311
1312template <class _CharT>
Howard Hinnantac303862010-07-12 15:51:17 +00001313void
1314__end_state<_CharT>::__exec(__state& __s) const
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00001315{
Howard Hinnantac303862010-07-12 15:51:17 +00001316 __s.__do_ = __state::__end_state;
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00001317}
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001318
1319// __has_one_state
1320
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00001321template <class _CharT>
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001322class __has_one_state
Howard Hinnantac303862010-07-12 15:51:17 +00001323 : public __node<_CharT>
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00001324{
Howard Hinnantac303862010-07-12 15:51:17 +00001325 __node<_CharT>* __first_;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001326
1327public:
Howard Hinnantac303862010-07-12 15:51:17 +00001328 explicit __has_one_state(__node<_CharT>* __s)
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001329 : __first_(__s) {}
1330
Howard Hinnantac303862010-07-12 15:51:17 +00001331 __node<_CharT>* first() const {return __first_;}
1332 __node<_CharT>*& first() {return __first_;}
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001333};
1334
1335// __owns_one_state
1336
1337template <class _CharT>
1338class __owns_one_state
1339 : public __has_one_state<_CharT>
1340{
1341 typedef __has_one_state<_CharT> base;
1342
1343public:
Howard Hinnantac303862010-07-12 15:51:17 +00001344 explicit __owns_one_state(__node<_CharT>* __s)
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001345 : base(__s) {}
1346
1347 virtual ~__owns_one_state();
1348};
1349
1350template <class _CharT>
1351__owns_one_state<_CharT>::~__owns_one_state()
1352{
1353 delete this->first();
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00001354}
1355
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001356// __empty_state
1357
1358template <class _CharT>
1359class __empty_state
1360 : public __owns_one_state<_CharT>
1361{
1362 typedef __owns_one_state<_CharT> base;
1363
1364public:
Howard Hinnantac303862010-07-12 15:51:17 +00001365 typedef _STD::__state<_CharT> __state;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001366
Howard Hinnantac303862010-07-12 15:51:17 +00001367 explicit __empty_state(__node<_CharT>* __s)
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001368 : base(__s) {}
1369
Howard Hinnantac303862010-07-12 15:51:17 +00001370 virtual void __exec(__state&) const;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001371
1372 virtual string speak() const {return "empty state";}
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001373};
1374
1375template <class _CharT>
Howard Hinnantac303862010-07-12 15:51:17 +00001376void
1377__empty_state<_CharT>::__exec(__state& __s) const
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001378{
Howard Hinnantac303862010-07-12 15:51:17 +00001379 __s.__do_ = __state::__accept_but_not_consume;
1380 __s.__node_ = this->first();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001381}
1382
1383// __empty_non_own_state
1384
1385template <class _CharT>
1386class __empty_non_own_state
1387 : public __has_one_state<_CharT>
1388{
1389 typedef __has_one_state<_CharT> base;
1390
1391public:
Howard Hinnantac303862010-07-12 15:51:17 +00001392 typedef _STD::__state<_CharT> __state;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001393
Howard Hinnantac303862010-07-12 15:51:17 +00001394 explicit __empty_non_own_state(__node<_CharT>* __s)
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001395 : base(__s) {}
1396
Howard Hinnantac303862010-07-12 15:51:17 +00001397 virtual void __exec(__state&) const;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001398
1399 virtual string speak() const {return "empty non-owning state";}
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001400};
1401
1402template <class _CharT>
Howard Hinnantac303862010-07-12 15:51:17 +00001403void
1404__empty_non_own_state<_CharT>::__exec(__state& __s) const
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001405{
Howard Hinnantac303862010-07-12 15:51:17 +00001406 __s.__do_ = __state::__accept_but_not_consume;
1407 __s.__node_ = this->first();
1408}
1409
1410// __repeat_one_loop
1411
1412template <class _CharT>
1413class __repeat_one_loop
1414 : public __has_one_state<_CharT>
1415{
1416 typedef __has_one_state<_CharT> base;
1417
1418public:
1419 typedef _STD::__state<_CharT> __state;
1420
1421 explicit __repeat_one_loop(__node<_CharT>* __s)
1422 : base(__s) {}
1423
1424 virtual void __exec(__state&) const;
1425
1426 virtual string speak() const {return "repeat loop";}
1427};
1428
1429template <class _CharT>
1430void
1431__repeat_one_loop<_CharT>::__exec(__state& __s) const
1432{
1433 __s.__do_ = __state::__repeat;
1434 __s.__node_ = this->first();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001435}
1436
1437// __owns_two_states
1438
1439template <class _CharT>
1440class __owns_two_states
1441 : public __owns_one_state<_CharT>
1442{
1443 typedef __owns_one_state<_CharT> base;
1444
1445 base* __second_;
1446
1447public:
Howard Hinnantac303862010-07-12 15:51:17 +00001448 explicit __owns_two_states(__node<_CharT>* __s1, base* __s2)
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001449 : base(__s1), __second_(__s2) {}
1450
1451 virtual ~__owns_two_states();
1452
1453 base* second() const {return __second_;}
1454 base*& second() {return __second_;}
1455};
1456
1457template <class _CharT>
1458__owns_two_states<_CharT>::~__owns_two_states()
1459{
1460 delete __second_;
1461}
1462
1463// __loop
1464
1465template <class _CharT>
1466class __loop
1467 : public __owns_two_states<_CharT>
1468{
1469 typedef __owns_two_states<_CharT> base;
1470
1471 size_t __min_;
1472 size_t __max_;
1473 unsigned __loop_id_;
Howard Hinnantac303862010-07-12 15:51:17 +00001474 unsigned __mexp_begin_;
1475 unsigned __mexp_end_;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001476 bool __greedy_;
1477
1478public:
Howard Hinnantac303862010-07-12 15:51:17 +00001479 typedef _STD::__state<_CharT> __state;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001480
1481 explicit __loop(unsigned __loop_id,
Howard Hinnantac303862010-07-12 15:51:17 +00001482 __node<_CharT>* __s1, __owns_one_state<_CharT>* __s2,
1483 unsigned __mexp_begin, unsigned __mexp_end,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001484 bool __greedy = true,
1485 size_t __min = 0,
1486 size_t __max = numeric_limits<size_t>::max())
1487 : base(__s1, __s2), __min_(__min), __max_(__max), __loop_id_(__loop_id),
Howard Hinnantac303862010-07-12 15:51:17 +00001488 __mexp_begin_(__mexp_begin), __mexp_end_(__mexp_end),
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001489 __greedy_(__greedy) {}
1490
Howard Hinnantac303862010-07-12 15:51:17 +00001491 virtual void __exec(__state& __s) const;
1492 virtual void __exec_split(bool __second, __state& __s) const;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001493
1494 virtual string speak() const
1495 {
1496 ostringstream os;
Howard Hinnantac303862010-07-12 15:51:17 +00001497 os << "loop "<< __loop_id_ << " {" << __min_ << ',' << __max_ << "}";
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001498 if (!__greedy_)
1499 os << " not";
1500 os << " greedy";
1501 return os.str();
1502 }
Howard Hinnantac303862010-07-12 15:51:17 +00001503
1504private:
1505 void __init_repeat(__state& __s) const
1506 {
1507 __s.__loop_data_[__loop_id_].second = __s.__current_;
1508 for (size_t __i = __mexp_begin_-1; __i != __mexp_end_-1; ++__i)
1509 {
1510 __s.__sub_matches_[__i].first = __s.__last_;
1511 __s.__sub_matches_[__i].second = __s.__last_;
1512 __s.__sub_matches_[__i].matched = false;
1513 }
1514 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001515};
1516
1517template <class _CharT>
Howard Hinnantac303862010-07-12 15:51:17 +00001518void
1519__loop<_CharT>::__exec(__state& __s) const
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001520{
Howard Hinnantac303862010-07-12 15:51:17 +00001521 if (__s.__do_ == __state::__repeat)
1522 {
1523 bool __do_repeat = ++__s.__loop_data_[__loop_id_].first < __max_;
1524 bool __do_alt = __s.__loop_data_[__loop_id_].first >= __min_;
1525 if (__do_repeat && __do_alt &&
1526 __s.__loop_data_[__loop_id_].second == __s.__current_)
1527 __do_repeat = false;
1528 if (__do_repeat && __do_alt)
1529 __s.__do_ = __state::__split;
1530 else if (__do_repeat)
1531 {
1532 __s.__do_ = __state::__accept_but_not_consume;
1533 __s.__node_ = this->first();
1534 __init_repeat(__s);
1535 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001536 else
Howard Hinnantac303862010-07-12 15:51:17 +00001537 {
1538 __s.__do_ = __state::__accept_but_not_consume;
1539 __s.__node_ = this->second();
1540 }
1541 }
1542 else
1543 {
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00001544 __s.__loop_data_[__loop_id_].first = 0;
1545 bool __do_repeat = 0 < __max_;
1546 bool __do_alt = 0 >= __min_;
1547 if (__do_repeat && __do_alt)
Howard Hinnantac303862010-07-12 15:51:17 +00001548 __s.__do_ = __state::__split;
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00001549 else if (__do_repeat)
1550 {
1551 __s.__do_ = __state::__accept_but_not_consume;
1552 __s.__node_ = this->first();
1553 __init_repeat(__s);
1554 }
Howard Hinnantac303862010-07-12 15:51:17 +00001555 else
1556 {
1557 __s.__do_ = __state::__accept_but_not_consume;
1558 __s.__node_ = this->second();
1559 }
1560 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001561}
1562
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001563template <class _CharT>
Howard Hinnantac303862010-07-12 15:51:17 +00001564void
1565__loop<_CharT>::__exec_split(bool __second, __state& __s) const
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001566{
Howard Hinnantac303862010-07-12 15:51:17 +00001567 __s.__do_ = __state::__accept_but_not_consume;
1568 if (__greedy_ != __second)
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001569 {
Howard Hinnantac303862010-07-12 15:51:17 +00001570 __s.__node_ = this->first();
1571 __init_repeat(__s);
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001572 }
Howard Hinnantac303862010-07-12 15:51:17 +00001573 else
1574 __s.__node_ = this->second();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001575}
1576
Howard Hinnantaa698082010-07-16 19:08:36 +00001577// __alternate
1578
1579template <class _CharT>
1580class __alternate
1581 : public __owns_two_states<_CharT>
1582{
1583 typedef __owns_two_states<_CharT> base;
1584
1585public:
1586 typedef _STD::__state<_CharT> __state;
1587
1588 explicit __alternate(__owns_one_state<_CharT>* __s1,
1589 __owns_one_state<_CharT>* __s2)
1590 : base(__s1, __s2) {}
1591
1592 virtual void __exec(__state& __s) const;
1593 virtual void __exec_split(bool __second, __state& __s) const;
1594
1595 virtual string speak() const
1596 {
1597 ostringstream os;
1598 os << "__alternate";
1599 return os.str();
1600 }
1601};
1602
1603template <class _CharT>
1604void
1605__alternate<_CharT>::__exec(__state& __s) const
1606{
1607 __s.__do_ = __state::__split;
1608}
1609
1610template <class _CharT>
1611void
1612__alternate<_CharT>::__exec_split(bool __second, __state& __s) const
1613{
1614 __s.__do_ = __state::__accept_but_not_consume;
Howard Hinnant1371b2e2010-07-22 14:12:20 +00001615 if (__second)
Howard Hinnantaa698082010-07-16 19:08:36 +00001616 __s.__node_ = this->second();
Howard Hinnant1371b2e2010-07-22 14:12:20 +00001617 else
1618 __s.__node_ = this->first();
Howard Hinnantaa698082010-07-16 19:08:36 +00001619}
1620
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001621// __begin_marked_subexpression
1622
1623template <class _CharT>
1624class __begin_marked_subexpression
1625 : public __owns_one_state<_CharT>
1626{
1627 typedef __owns_one_state<_CharT> base;
1628
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001629 unsigned __mexp_;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001630public:
Howard Hinnantac303862010-07-12 15:51:17 +00001631 typedef _STD::__state<_CharT> __state;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001632
Howard Hinnantac303862010-07-12 15:51:17 +00001633 explicit __begin_marked_subexpression(unsigned __mexp, __node<_CharT>* __s)
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001634 : base(__s), __mexp_(__mexp) {}
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001635
Howard Hinnantac303862010-07-12 15:51:17 +00001636 virtual void __exec(__state&) const;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001637
1638 virtual string speak() const
1639 {
1640 ostringstream os;
1641 os << "begin marked expr " << __mexp_;
1642 return os.str();
1643 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001644};
1645
1646template <class _CharT>
Howard Hinnantac303862010-07-12 15:51:17 +00001647void
1648__begin_marked_subexpression<_CharT>::__exec(__state& __s) const
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001649{
Howard Hinnantac303862010-07-12 15:51:17 +00001650 __s.__do_ = __state::__accept_but_not_consume;
1651 __s.__sub_matches_[__mexp_-1].first = __s.__current_;
1652 __s.__node_ = this->first();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001653}
1654
1655// __end_marked_subexpression
1656
1657template <class _CharT>
1658class __end_marked_subexpression
1659 : public __owns_one_state<_CharT>
1660{
1661 typedef __owns_one_state<_CharT> base;
1662
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001663 unsigned __mexp_;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001664public:
Howard Hinnantac303862010-07-12 15:51:17 +00001665 typedef _STD::__state<_CharT> __state;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001666
Howard Hinnantac303862010-07-12 15:51:17 +00001667 explicit __end_marked_subexpression(unsigned __mexp, __node<_CharT>* __s)
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001668 : base(__s), __mexp_(__mexp) {}
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001669
Howard Hinnantac303862010-07-12 15:51:17 +00001670 virtual void __exec(__state&) const;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001671
1672 virtual string speak() const
1673 {
1674 ostringstream os;
1675 os << "end marked expr " << __mexp_;
1676 return os.str();
1677 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001678};
1679
1680template <class _CharT>
Howard Hinnantac303862010-07-12 15:51:17 +00001681void
1682__end_marked_subexpression<_CharT>::__exec(__state& __s) const
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001683{
Howard Hinnantac303862010-07-12 15:51:17 +00001684 __s.__do_ = __state::__accept_but_not_consume;
1685 __s.__sub_matches_[__mexp_-1].second = __s.__current_;
1686 __s.__sub_matches_[__mexp_-1].matched = true;
1687 __s.__node_ = this->first();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001688}
1689
Howard Hinnantcba352d2010-07-12 18:16:05 +00001690// __back_ref
1691
1692template <class _CharT>
1693class __back_ref
1694 : public __owns_one_state<_CharT>
1695{
1696 typedef __owns_one_state<_CharT> base;
1697
1698 unsigned __mexp_;
1699public:
1700 typedef _STD::__state<_CharT> __state;
1701
1702 explicit __back_ref(unsigned __mexp, __node<_CharT>* __s)
1703 : base(__s), __mexp_(__mexp) {}
1704
1705 virtual void __exec(__state&) const;
1706
1707 virtual string speak() const
1708 {
1709 ostringstream os;
1710 os << "__back_ref " << __mexp_;
1711 return os.str();
1712 }
1713};
1714
1715template <class _CharT>
1716void
1717__back_ref<_CharT>::__exec(__state& __s) const
1718{
1719 sub_match<const _CharT*>& __sm = __s.__sub_matches_[__mexp_-1];
1720 if (__sm.matched)
1721 {
1722 ptrdiff_t __len = __sm.second - __sm.first;
1723 if (__s.__last_ - __s.__current_ >= __len &&
1724 _STD::equal(__sm.first, __sm.second, __s.__current_))
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 __s.__do_ = __state::__reject;
1739 __s.__node_ = nullptr;
1740 }
1741}
1742
Howard Hinnante34f17d2010-07-12 19:11:27 +00001743// __back_ref_icase
1744
1745template <class _CharT, class _Traits>
1746class __back_ref_icase
1747 : public __owns_one_state<_CharT>
1748{
1749 typedef __owns_one_state<_CharT> base;
1750
1751 _Traits __traits_;
1752 unsigned __mexp_;
1753public:
1754 typedef _STD::__state<_CharT> __state;
1755
1756 explicit __back_ref_icase(const _Traits& __traits, unsigned __mexp,
1757 __node<_CharT>* __s)
1758 : base(__s), __traits_(__traits), __mexp_(__mexp) {}
1759
1760 virtual void __exec(__state&) const;
1761
1762 virtual string speak() const
1763 {
1764 ostringstream os;
1765 os << "__back_ref_icase " << __mexp_;
1766 return os.str();
1767 }
1768};
1769
1770template <class _CharT, class _Traits>
1771void
1772__back_ref_icase<_CharT, _Traits>::__exec(__state& __s) const
1773{
1774 sub_match<const _CharT*>& __sm = __s.__sub_matches_[__mexp_-1];
1775 if (__sm.matched)
1776 {
1777 ptrdiff_t __len = __sm.second - __sm.first;
1778 if (__s.__last_ - __s.__current_ >= __len)
1779 {
1780 for (ptrdiff_t __i = 0; __i < __len; ++__i)
1781 {
1782 if (__traits_.translate_nocase(__sm.first[__i]) !=
1783 __traits_.translate_nocase(__s.__current_[__i]))
1784 goto __not_equal;
1785 }
1786 __s.__do_ = __state::__accept_but_not_consume;
1787 __s.__current_ += __len;
1788 __s.__node_ = this->first();
1789 }
1790 else
1791 {
1792 __s.__do_ = __state::__reject;
1793 __s.__node_ = nullptr;
1794 }
1795 }
1796 else
1797 {
1798__not_equal:
1799 __s.__do_ = __state::__reject;
1800 __s.__node_ = nullptr;
1801 }
1802}
1803
1804// __back_ref_collate
1805
1806template <class _CharT, class _Traits>
1807class __back_ref_collate
1808 : public __owns_one_state<_CharT>
1809{
1810 typedef __owns_one_state<_CharT> base;
1811
1812 _Traits __traits_;
1813 unsigned __mexp_;
1814public:
1815 typedef _STD::__state<_CharT> __state;
1816
1817 explicit __back_ref_collate(const _Traits& __traits, unsigned __mexp,
1818 __node<_CharT>* __s)
1819 : base(__s), __traits_(__traits), __mexp_(__mexp) {}
1820
1821 virtual void __exec(__state&) const;
1822
1823 virtual string speak() const
1824 {
1825 ostringstream os;
1826 os << "__back_ref_collate " << __mexp_;
1827 return os.str();
1828 }
1829};
1830
1831template <class _CharT, class _Traits>
1832void
1833__back_ref_collate<_CharT, _Traits>::__exec(__state& __s) const
1834{
1835 sub_match<const _CharT*>& __sm = __s.__sub_matches_[__mexp_-1];
1836 if (__sm.matched)
1837 {
1838 ptrdiff_t __len = __sm.second - __sm.first;
1839 if (__s.__last_ - __s.__current_ >= __len)
1840 {
1841 for (ptrdiff_t __i = 0; __i < __len; ++__i)
1842 {
1843 if (__traits_.translate(__sm.first[__i]) !=
1844 __traits_.translate(__s.__current_[__i]))
1845 goto __not_equal;
1846 }
1847 __s.__do_ = __state::__accept_but_not_consume;
1848 __s.__current_ += __len;
1849 __s.__node_ = this->first();
1850 }
1851 else
1852 {
1853 __s.__do_ = __state::__reject;
1854 __s.__node_ = nullptr;
1855 }
1856 }
1857 else
1858 {
1859__not_equal:
1860 __s.__do_ = __state::__reject;
1861 __s.__node_ = nullptr;
1862 }
1863}
1864
Howard Hinnant17615b02010-07-27 01:25:38 +00001865// __word_boundary
1866
1867template <class _CharT, class _Traits>
1868class __word_boundary
1869 : public __owns_one_state<_CharT>
1870{
1871 typedef __owns_one_state<_CharT> base;
1872
1873 _Traits __traits_;
1874 bool __invert_;
1875public:
1876 typedef _STD::__state<_CharT> __state;
1877
1878 explicit __word_boundary(const _Traits& __traits, bool __invert,
1879 __node<_CharT>* __s)
1880 : base(__s), __traits_(__traits), __invert_(__invert) {}
1881
1882 virtual void __exec(__state&) const;
1883
1884 virtual string speak() const
1885 {
1886 ostringstream os;
Howard Hinnant8daa7332010-07-29 01:15:27 +00001887 if (!__invert_)
Howard Hinnant17615b02010-07-27 01:25:38 +00001888 os << "__word_boundary";
1889 else
1890 os << "not __word_boundary";
1891 return os.str();
1892 }
1893};
1894
1895template <class _CharT, class _Traits>
1896void
1897__word_boundary<_CharT, _Traits>::__exec(__state& __s) const
1898{
1899 bool __is_word_b = false;
1900 if (__s.__first_ != __s.__last_)
1901 {
1902 if (__s.__current_ == __s.__last_)
1903 {
1904 if (!(__s.__flags_ & regex_constants::match_not_eow))
1905 {
1906 _CharT __c = __s.__current_[-1];
1907 __is_word_b = __c == '_' ||
1908 __traits_.isctype(__c, ctype_base::alnum);
1909 }
1910 }
Howard Hinnantf3dcca02010-07-29 15:17:28 +00001911 else if (__s.__current_ == __s.__first_ &&
1912 !(__s.__flags_ & regex_constants::match_prev_avail))
Howard Hinnant17615b02010-07-27 01:25:38 +00001913 {
1914 if (!(__s.__flags_ & regex_constants::match_not_bow))
1915 {
1916 _CharT __c = *__s.__current_;
1917 __is_word_b = __c == '_' ||
1918 __traits_.isctype(__c, ctype_base::alnum);
1919 }
1920 }
1921 else
1922 {
1923 _CharT __c1 = __s.__current_[-1];
1924 _CharT __c2 = *__s.__current_;
1925 bool __is_c1_b = __c1 == '_' ||
1926 __traits_.isctype(__c1, ctype_base::alnum);
1927 bool __is_c2_b = __c2 == '_' ||
1928 __traits_.isctype(__c2, ctype_base::alnum);
1929 __is_word_b = __is_c1_b != __is_c2_b;
1930 }
1931 }
1932 if (__is_word_b != __invert_)
1933 {
1934 __s.__do_ = __state::__accept_but_not_consume;
1935 __s.__node_ = this->first();
1936 }
1937 else
1938 {
1939 __s.__do_ = __state::__reject;
1940 __s.__node_ = nullptr;
1941 }
1942}
1943
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001944// __r_anchor
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001945
1946template <class _CharT>
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001947class __r_anchor
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001948 : public __owns_one_state<_CharT>
1949{
1950 typedef __owns_one_state<_CharT> base;
1951
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001952public:
Howard Hinnantac303862010-07-12 15:51:17 +00001953 typedef _STD::__state<_CharT> __state;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001954
Howard Hinnantac303862010-07-12 15:51:17 +00001955 __r_anchor(__node<_CharT>* __s)
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001956 : base(__s) {}
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001957
Howard Hinnantac303862010-07-12 15:51:17 +00001958 virtual void __exec(__state&) const;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001959
1960 virtual string speak() const
1961 {
1962 ostringstream os;
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001963 os << "right anchor";
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001964 return os.str();
1965 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001966};
1967
1968template <class _CharT>
Howard Hinnantac303862010-07-12 15:51:17 +00001969void
1970__r_anchor<_CharT>::__exec(__state& __s) const
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001971{
Howard Hinnantac303862010-07-12 15:51:17 +00001972 if (__s.__current_ == __s.__last_)
1973 {
1974 __s.__do_ = __state::__accept_but_not_consume;
1975 __s.__node_ = this->first();
1976 }
1977 else
1978 {
1979 __s.__do_ = __state::__reject;
1980 __s.__node_ = nullptr;
1981 }
1982}
1983
1984// __match_any
1985
1986template <class _CharT>
1987class __match_any
1988 : public __owns_one_state<_CharT>
1989{
1990 typedef __owns_one_state<_CharT> base;
1991
1992public:
1993 typedef _STD::__state<_CharT> __state;
1994
1995 __match_any(__node<_CharT>* __s)
1996 : base(__s) {}
1997
1998 virtual void __exec(__state&) const;
1999
2000 virtual string speak() const
2001 {
2002 ostringstream os;
2003 os << "match any";
2004 return os.str();
2005 }
2006};
2007
2008template <class _CharT>
2009void
2010__match_any<_CharT>::__exec(__state& __s) const
2011{
2012 if (__s.__current_ != __s.__last_ && *__s.__current_ != 0)
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 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002023}
2024
Howard Hinnant17615b02010-07-27 01:25:38 +00002025// __match_any_but_newline
2026
2027template <class _CharT>
2028class __match_any_but_newline
2029 : public __owns_one_state<_CharT>
2030{
2031 typedef __owns_one_state<_CharT> base;
2032
2033public:
2034 typedef _STD::__state<_CharT> __state;
2035
2036 __match_any_but_newline(__node<_CharT>* __s)
2037 : base(__s) {}
2038
2039 virtual void __exec(__state&) const;
2040
2041 virtual string speak() const
2042 {
2043 ostringstream os;
2044 os << "match any but newline";
2045 return os.str();
2046 }
2047};
2048
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002049// __match_char
2050
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00002051template <class _CharT>
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002052class __match_char
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002053 : public __owns_one_state<_CharT>
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002054{
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002055 typedef __owns_one_state<_CharT> base;
2056
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002057 _CharT __c_;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002058
2059 __match_char(const __match_char&);
2060 __match_char& operator=(const __match_char&);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002061public:
Howard Hinnantac303862010-07-12 15:51:17 +00002062 typedef _STD::__state<_CharT> __state;
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00002063
Howard Hinnantac303862010-07-12 15:51:17 +00002064 __match_char(_CharT __c, __node<_CharT>* __s)
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002065 : base(__s), __c_(__c) {}
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002066
Howard Hinnantac303862010-07-12 15:51:17 +00002067 virtual void __exec(__state&) const;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00002068
2069 virtual string speak() const
2070 {
2071 ostringstream os;
2072 os << "match char " << __c_;
2073 return os.str();
2074 }
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002075};
2076
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00002077template <class _CharT>
Howard Hinnantac303862010-07-12 15:51:17 +00002078void
2079__match_char<_CharT>::__exec(__state& __s) const
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00002080{
Howard Hinnantac303862010-07-12 15:51:17 +00002081 if (__s.__current_ != __s.__last_ && *__s.__current_ == __c_)
2082 {
2083 __s.__do_ = __state::__accept_and_consume;
2084 ++__s.__current_;
2085 __s.__node_ = this->first();
2086 }
2087 else
2088 {
2089 __s.__do_ = __state::__reject;
2090 __s.__node_ = nullptr;
2091 }
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00002092}
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00002093
Howard Hinnante34f17d2010-07-12 19:11:27 +00002094// __match_char_icase
2095
2096template <class _CharT, class _Traits>
2097class __match_char_icase
2098 : public __owns_one_state<_CharT>
2099{
2100 typedef __owns_one_state<_CharT> base;
2101
2102 _Traits __traits_;
2103 _CharT __c_;
2104
2105 __match_char_icase(const __match_char_icase&);
2106 __match_char_icase& operator=(const __match_char_icase&);
2107public:
2108 typedef _STD::__state<_CharT> __state;
2109
2110 __match_char_icase(const _Traits& __traits, _CharT __c, __node<_CharT>* __s)
2111 : base(__s), __traits_(__traits), __c_(__traits.translate_nocase(__c)) {}
2112
2113 virtual void __exec(__state&) const;
2114
2115 virtual string speak() const
2116 {
2117 ostringstream os;
2118 os << "match char icase " << __c_;
2119 return os.str();
2120 }
2121};
2122
2123template <class _CharT, class _Traits>
2124void
2125__match_char_icase<_CharT, _Traits>::__exec(__state& __s) const
2126{
2127 if (__s.__current_ != __s.__last_ &&
2128 __traits_.translate_nocase(*__s.__current_) == __c_)
2129 {
2130 __s.__do_ = __state::__accept_and_consume;
2131 ++__s.__current_;
2132 __s.__node_ = this->first();
2133 }
2134 else
2135 {
2136 __s.__do_ = __state::__reject;
2137 __s.__node_ = nullptr;
2138 }
2139}
2140
2141// __match_char_collate
2142
2143template <class _CharT, class _Traits>
2144class __match_char_collate
2145 : public __owns_one_state<_CharT>
2146{
2147 typedef __owns_one_state<_CharT> base;
2148
2149 _Traits __traits_;
2150 _CharT __c_;
2151
2152 __match_char_collate(const __match_char_collate&);
2153 __match_char_collate& operator=(const __match_char_collate&);
2154public:
2155 typedef _STD::__state<_CharT> __state;
2156
2157 __match_char_collate(const _Traits& __traits, _CharT __c, __node<_CharT>* __s)
2158 : base(__s), __traits_(__traits), __c_(__traits.translate(__c)) {}
2159
2160 virtual void __exec(__state&) const;
2161
2162 virtual string speak() const
2163 {
2164 ostringstream os;
2165 os << "match char icase " << __c_;
2166 return os.str();
2167 }
2168};
2169
2170template <class _CharT, class _Traits>
2171void
2172__match_char_collate<_CharT, _Traits>::__exec(__state& __s) const
2173{
2174 if (__s.__current_ != __s.__last_ &&
2175 __traits_.translate(*__s.__current_) == __c_)
2176 {
2177 __s.__do_ = __state::__accept_and_consume;
2178 ++__s.__current_;
2179 __s.__node_ = this->first();
2180 }
2181 else
2182 {
2183 __s.__do_ = __state::__reject;
2184 __s.__node_ = nullptr;
2185 }
2186}
2187
Howard Hinnant173968a2010-07-13 21:48:06 +00002188// __bracket_expression
2189
2190template <class _CharT, class _Traits>
2191class __bracket_expression
2192 : public __owns_one_state<_CharT>
2193{
2194 typedef __owns_one_state<_CharT> base;
2195 typedef typename _Traits::string_type string_type;
2196
2197 _Traits __traits_;
2198 vector<_CharT> __chars_;
Howard Hinnant15476f32010-07-28 17:35:27 +00002199 vector<_CharT> __neg_chars_;
Howard Hinnant173968a2010-07-13 21:48:06 +00002200 vector<pair<string_type, string_type> > __ranges_;
2201 vector<pair<_CharT, _CharT> > __digraphs_;
2202 vector<string_type> __equivalences_;
2203 ctype_base::mask __mask_;
Howard Hinnant15476f32010-07-28 17:35:27 +00002204 ctype_base::mask __neg_mask_;
Howard Hinnant173968a2010-07-13 21:48:06 +00002205 bool __negate_;
2206 bool __icase_;
2207 bool __collate_;
Howard Hinnant68025ed2010-07-14 15:45:11 +00002208 bool __might_have_digraph_;
Howard Hinnant173968a2010-07-13 21:48:06 +00002209
2210 __bracket_expression(const __bracket_expression&);
2211 __bracket_expression& operator=(const __bracket_expression&);
2212public:
2213 typedef _STD::__state<_CharT> __state;
2214
2215 __bracket_expression(const _Traits& __traits, __node<_CharT>* __s,
2216 bool __negate, bool __icase, bool __collate)
Howard Hinnant15476f32010-07-28 17:35:27 +00002217 : base(__s), __traits_(__traits), __mask_(), __neg_mask_(),
2218 __negate_(__negate), __icase_(__icase), __collate_(__collate),
Howard Hinnant68025ed2010-07-14 15:45:11 +00002219 __might_have_digraph_(__traits_.getloc().name() != "C") {}
Howard Hinnant173968a2010-07-13 21:48:06 +00002220
2221 virtual void __exec(__state&) const;
2222
Howard Hinnant15476f32010-07-28 17:35:27 +00002223 bool __negated() const {return __negate_;}
2224
Howard Hinnant173968a2010-07-13 21:48:06 +00002225 void __add_char(_CharT __c)
2226 {
2227 if (__icase_)
2228 __chars_.push_back(__traits_.translate_nocase(__c));
2229 else if (__collate_)
2230 __chars_.push_back(__traits_.translate(__c));
2231 else
2232 __chars_.push_back(__c);
2233 }
Howard Hinnant15476f32010-07-28 17:35:27 +00002234 void __add_neg_char(_CharT __c)
2235 {
2236 if (__icase_)
2237 __neg_chars_.push_back(__traits_.translate_nocase(__c));
2238 else if (__collate_)
2239 __neg_chars_.push_back(__traits_.translate(__c));
2240 else
2241 __neg_chars_.push_back(__c);
2242 }
Howard Hinnant173968a2010-07-13 21:48:06 +00002243 void __add_range(string_type __b, string_type __e)
2244 {
2245 if (__collate_)
2246 {
2247 if (__icase_)
2248 {
2249 for (size_t __i = 0; __i < __b.size(); ++__i)
2250 __b[__i] = __traits_.translate_nocase(__b[__i]);
2251 for (size_t __i = 0; __i < __e.size(); ++__i)
2252 __e[__i] = __traits_.translate_nocase(__e[__i]);
2253 }
2254 else
2255 {
2256 for (size_t __i = 0; __i < __b.size(); ++__i)
2257 __b[__i] = __traits_.translate(__b[__i]);
2258 for (size_t __i = 0; __i < __e.size(); ++__i)
2259 __e[__i] = __traits_.translate(__e[__i]);
2260 }
2261 __ranges_.push_back(make_pair(
2262 __traits_.transform(__b.begin(), __b.end()),
2263 __traits_.transform(__e.begin(), __e.end())));
2264 }
2265 else
2266 {
Howard Hinnantd4444702010-08-11 17:04:31 +00002267#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant173968a2010-07-13 21:48:06 +00002268 if (__b.size() != 1 || __e.size() != 1)
2269 throw regex_error(regex_constants::error_collate);
Howard Hinnantd4444702010-08-11 17:04:31 +00002270#endif
Howard Hinnant173968a2010-07-13 21:48:06 +00002271 if (__icase_)
2272 {
2273 __b[0] = __traits_.translate_nocase(__b[0]);
2274 __e[0] = __traits_.translate_nocase(__e[0]);
2275 }
2276 __ranges_.push_back(make_pair(_STD::move(__b), _STD::move(__e)));
2277 }
2278 }
2279 void __add_digraph(_CharT __c1, _CharT __c2)
2280 {
2281 if (__icase_)
2282 __digraphs_.push_back(make_pair(__traits_.translate_nocase(__c1),
2283 __traits_.translate_nocase(__c2)));
2284 else if (__collate_)
2285 __digraphs_.push_back(make_pair(__traits_.translate(__c1),
2286 __traits_.translate(__c2)));
2287 else
2288 __digraphs_.push_back(make_pair(__c1, __c2));
2289 }
2290 void __add_equivalence(const string_type& __s)
2291 {__equivalences_.push_back(__s);}
2292 void __add_class(ctype_base::mask __mask)
2293 {__mask_ |= __mask;}
Howard Hinnant15476f32010-07-28 17:35:27 +00002294 void __add_neg_class(ctype_base::mask __mask)
2295 {__neg_mask_ |= __mask;}
Howard Hinnant173968a2010-07-13 21:48:06 +00002296
2297 virtual string speak() const
2298 {
2299 ostringstream os;
2300 os << "__bracket_expression ";
2301 return os.str();
2302 }
2303};
2304
2305template <class _CharT, class _Traits>
2306void
2307__bracket_expression<_CharT, _Traits>::__exec(__state& __s) const
2308{
2309 bool __found = false;
2310 unsigned __consumed = 0;
2311 if (__s.__current_ != __s.__last_)
2312 {
2313 ++__consumed;
Howard Hinnant68025ed2010-07-14 15:45:11 +00002314 if (__might_have_digraph_)
Howard Hinnant173968a2010-07-13 21:48:06 +00002315 {
Howard Hinnant68025ed2010-07-14 15:45:11 +00002316 const _CharT* __next = next(__s.__current_);
2317 if (__next != __s.__last_)
Howard Hinnant173968a2010-07-13 21:48:06 +00002318 {
Howard Hinnant68025ed2010-07-14 15:45:11 +00002319 pair<_CharT, _CharT> __ch2(*__s.__current_, *__next);
2320 if (__icase_)
Howard Hinnant173968a2010-07-13 21:48:06 +00002321 {
Howard Hinnant68025ed2010-07-14 15:45:11 +00002322 __ch2.first = __traits_.translate_nocase(__ch2.first);
2323 __ch2.second = __traits_.translate_nocase(__ch2.second);
2324 }
2325 else if (__collate_)
2326 {
2327 __ch2.first = __traits_.translate(__ch2.first);
2328 __ch2.second = __traits_.translate(__ch2.second);
2329 }
2330 if (!__traits_.lookup_collatename(&__ch2.first, &__ch2.first+2).empty())
2331 {
2332 // __ch2 is a digraph in this locale
2333 ++__consumed;
2334 for (size_t __i = 0; __i < __digraphs_.size(); ++__i)
2335 {
2336 if (__ch2 == __digraphs_[__i])
2337 {
2338 __found = true;
2339 goto __exit;
2340 }
2341 }
2342 if (__collate_ && !__ranges_.empty())
2343 {
2344 string_type __s2 = __traits_.transform(&__ch2.first,
2345 &__ch2.first + 2);
2346 for (size_t __i = 0; __i < __ranges_.size(); ++__i)
2347 {
2348 if (__ranges_[__i].first <= __s2 &&
2349 __s2 <= __ranges_[__i].second)
2350 {
2351 __found = true;
2352 goto __exit;
2353 }
2354 }
2355 }
2356 if (!__equivalences_.empty())
2357 {
2358 string_type __s2 = __traits_.transform_primary(&__ch2.first,
2359 &__ch2.first + 2);
2360 for (size_t __i = 0; __i < __equivalences_.size(); ++__i)
2361 {
2362 if (__s2 == __equivalences_[__i])
2363 {
2364 __found = true;
2365 goto __exit;
2366 }
2367 }
2368 }
2369 if (__traits_.isctype(__ch2.first, __mask_) &&
2370 __traits_.isctype(__ch2.second, __mask_))
Howard Hinnant173968a2010-07-13 21:48:06 +00002371 {
2372 __found = true;
2373 goto __exit;
2374 }
Howard Hinnant15476f32010-07-28 17:35:27 +00002375 if (!__traits_.isctype(__ch2.first, __neg_mask_) &&
2376 !__traits_.isctype(__ch2.second, __neg_mask_))
2377 {
2378 __found = true;
2379 goto __exit;
2380 }
Howard Hinnant173968a2010-07-13 21:48:06 +00002381 goto __exit;
2382 }
Howard Hinnant173968a2010-07-13 21:48:06 +00002383 }
2384 }
2385 // test *__s.__current_ as not a digraph
2386 _CharT __ch = *__s.__current_;
2387 if (__icase_)
2388 __ch = __traits_.translate_nocase(__ch);
2389 else if (__collate_)
2390 __ch = __traits_.translate(__ch);
2391 for (size_t __i = 0; __i < __chars_.size(); ++__i)
2392 {
2393 if (__ch == __chars_[__i])
2394 {
2395 __found = true;
2396 goto __exit;
2397 }
2398 }
Howard Hinnant15476f32010-07-28 17:35:27 +00002399 if (!__neg_chars_.empty())
2400 {
2401 for (size_t __i = 0; __i < __neg_chars_.size(); ++__i)
2402 {
2403 if (__ch == __neg_chars_[__i])
2404 goto __is_neg_char;
2405 }
2406 __found = true;
2407 goto __exit;
2408 }
2409__is_neg_char:
Howard Hinnant173968a2010-07-13 21:48:06 +00002410 if (!__ranges_.empty())
2411 {
2412 string_type __s2 = __collate_ ?
2413 __traits_.transform(&__ch, &__ch + 1) :
2414 string_type(1, __ch);
2415 for (size_t __i = 0; __i < __ranges_.size(); ++__i)
2416 {
2417 if (__ranges_[__i].first <= __s2 && __s2 <= __ranges_[__i].second)
2418 {
2419 __found = true;
2420 goto __exit;
2421 }
2422 }
2423 }
2424 if (!__equivalences_.empty())
2425 {
2426 string_type __s2 = __traits_.transform_primary(&__ch, &__ch + 1);
2427 for (size_t __i = 0; __i < __equivalences_.size(); ++__i)
2428 {
2429 if (__s2 == __equivalences_[__i])
2430 {
2431 __found = true;
2432 goto __exit;
2433 }
2434 }
2435 }
2436 if (__traits_.isctype(__ch, __mask_))
Howard Hinnant15476f32010-07-28 17:35:27 +00002437 {
Howard Hinnant173968a2010-07-13 21:48:06 +00002438 __found = true;
Howard Hinnant15476f32010-07-28 17:35:27 +00002439 goto __exit;
2440 }
2441 if (__neg_mask_ && !__traits_.isctype(__ch, __neg_mask_))
2442 {
2443 __found = true;
2444 goto __exit;
2445 }
Howard Hinnant173968a2010-07-13 21:48:06 +00002446 }
2447 else
2448 __found = __negate_; // force reject
2449__exit:
2450 if (__found != __negate_)
2451 {
Howard Hinnant173968a2010-07-13 21:48:06 +00002452 __s.__do_ = __state::__accept_and_consume;
2453 __s.__current_ += __consumed;
2454 __s.__node_ = this->first();
2455 }
2456 else
2457 {
2458 __s.__do_ = __state::__reject;
2459 __s.__node_ = nullptr;
2460 }
2461}
2462
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002463template <class, class> class __lookahead;
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00002464
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002465template <class _CharT, class _Traits = regex_traits<_CharT> >
2466class basic_regex
2467{
2468public:
2469 // types:
2470 typedef _CharT value_type;
2471 typedef regex_constants::syntax_option_type flag_type;
2472 typedef typename _Traits::locale_type locale_type;
2473
2474private:
2475 _Traits __traits_;
2476 flag_type __flags_;
2477 unsigned __marked_count_;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002478 unsigned __loop_count_;
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002479 int __open_count_;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002480 shared_ptr<__empty_state<_CharT> > __start_;
2481 __owns_one_state<_CharT>* __end_;
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00002482 bool __left_anchor_;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002483
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00002484 typedef _STD::__state<_CharT> __state;
Howard Hinnantac303862010-07-12 15:51:17 +00002485 typedef _STD::__node<_CharT> __node;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002486
2487public:
2488 // constants:
2489 static const/*expr*/ regex_constants::syntax_option_type icase = regex_constants::icase;
2490 static const/*expr*/ regex_constants::syntax_option_type nosubs = regex_constants::nosubs;
2491 static const/*expr*/ regex_constants::syntax_option_type optimize = regex_constants::optimize;
2492 static const/*expr*/ regex_constants::syntax_option_type collate = regex_constants::collate;
2493 static const/*expr*/ regex_constants::syntax_option_type ECMAScript = regex_constants::ECMAScript;
2494 static const/*expr*/ regex_constants::syntax_option_type basic = regex_constants::basic;
2495 static const/*expr*/ regex_constants::syntax_option_type extended = regex_constants::extended;
2496 static const/*expr*/ regex_constants::syntax_option_type awk = regex_constants::awk;
2497 static const/*expr*/ regex_constants::syntax_option_type grep = regex_constants::grep;
2498 static const/*expr*/ regex_constants::syntax_option_type egrep = regex_constants::egrep;
2499
2500 // construct/copy/destroy:
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002501 basic_regex()
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00002502 : __flags_(), __marked_count_(0), __loop_count_(0), __open_count_(0),
2503 __end_(0), __left_anchor_(false)
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002504 {}
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002505 explicit basic_regex(const value_type* __p, flag_type __f = regex_constants::ECMAScript)
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00002506 : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
2507 __end_(0), __left_anchor_(false)
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002508 {__parse(__p, __p + __traits_.length(__p));}
2509 basic_regex(const value_type* __p, size_t __len, flag_type __f)
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00002510 : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
2511 __end_(0), __left_anchor_(false)
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002512 {__parse(__p, __p + __len);}
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002513// basic_regex(const basic_regex&) = default;
2514// basic_regex(basic_regex&&) = default;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002515 template <class _ST, class _SA>
2516 explicit basic_regex(const basic_string<value_type, _ST, _SA>& __p,
2517 flag_type __f = regex_constants::ECMAScript)
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00002518 : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
2519 __end_(0), __left_anchor_(false)
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002520 {__parse(__p.begin(), __p.end());}
2521 template <class _ForwardIterator>
2522 basic_regex(_ForwardIterator __first, _ForwardIterator __last,
2523 flag_type __f = regex_constants::ECMAScript)
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00002524 : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
2525 __end_(0), __left_anchor_(false)
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002526 {__parse(__first, __last);}
2527 basic_regex(initializer_list<value_type> __il,
2528 flag_type __f = regex_constants::ECMAScript)
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00002529 : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
2530 __end_(0), __left_anchor_(false)
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002531 {__parse(__il.begin(), __il.end());}
2532
Howard Hinnant7026a172010-08-13 18:11:23 +00002533// ~basic_regex() = default;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002534
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002535// basic_regex& operator=(const basic_regex&) = default;
2536// basic_regex& operator=(basic_regex&&) = default;
Howard Hinnant7026a172010-08-13 18:11:23 +00002537 basic_regex& operator=(const value_type* __p)
2538 {return assign(__p);}
2539 basic_regex& operator=(initializer_list<value_type> __il)
2540 {return assign(__il);}
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002541 template <class _ST, class _SA>
Howard Hinnant7026a172010-08-13 18:11:23 +00002542 basic_regex& operator=(const basic_string<value_type, _ST, _SA>& __p)
2543 {return assign(__p);}
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002544
2545 // assign:
Howard Hinnant7026a172010-08-13 18:11:23 +00002546 basic_regex& assign(const basic_regex& __that)
2547 {return *this = __that;}
2548 basic_regex& assign(const value_type* __p, flag_type __f = regex_constants::ECMAScript)
2549 {return assign(__p, __p + __traits_.length(__p), __f);}
2550 basic_regex& assign(const value_type* __p, size_t __len, flag_type __f)
2551 {return assign(__p, __p + __len, __f);}
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002552 template <class _ST, class _SA>
2553 basic_regex& assign(const basic_string<value_type, _ST, _SA>& __s,
Howard Hinnant7026a172010-08-13 18:11:23 +00002554 flag_type __f = regex_constants::ECMAScript)
2555 {return assign(__s.begin(), __s.end(), __f);}
2556
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002557 template <class _InputIterator>
Howard Hinnant7026a172010-08-13 18:11:23 +00002558 typename enable_if
2559 <
2560 __is_input_iterator <_InputIterator>::value &&
2561 !__is_forward_iterator<_InputIterator>::value,
2562 basic_regex&
2563 >::type
2564 assign(_InputIterator __first, _InputIterator __last,
2565 flag_type __f = regex_constants::ECMAScript)
2566 {
2567 basic_string<_CharT> __t(__first, __last);
2568 return assign(__t.begin(), __t.end(), __f);
2569 }
2570
2571private:
2572 void __member_init(flag_type __f)
2573 {
2574 __flags_ = __f;
2575 __marked_count_ = 0;
2576 __loop_count_ = 0;
2577 __open_count_ = 0;
2578 __end_ = nullptr;
2579 __left_anchor_ = false;
2580 }
2581public:
2582
2583 template <class _ForwardIterator>
2584 typename enable_if
2585 <
2586 __is_forward_iterator<_ForwardIterator>::value,
2587 basic_regex&
2588 >::type
2589 assign(_ForwardIterator __first, _ForwardIterator __last,
2590 flag_type __f = regex_constants::ECMAScript)
2591 {
2592 __member_init(__f);
2593 __parse(__first, __last);
2594 }
2595
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002596 basic_regex& assign(initializer_list<value_type> __il,
Howard Hinnant7026a172010-08-13 18:11:23 +00002597 flag_type __f = regex_constants::ECMAScript)
2598 {return assign(__il.begin(), __il.end(), __f);}
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002599
2600 // const operations:
2601 unsigned mark_count() const {return __marked_count_;}
2602 flag_type flags() const {return __flags_;}
2603
2604 // locale:
Howard Hinnant7026a172010-08-13 18:11:23 +00002605 locale_type imbue(locale_type __loc)
2606 {
2607 __member_init(ECMAScript);
2608 __start_.reset();
2609 return __traits_.imbue(__loc);
2610 }
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002611 locale_type getloc() const {return __traits_.getloc();}
2612
2613 // swap:
Howard Hinnant7026a172010-08-13 18:11:23 +00002614 void swap(basic_regex& __r);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002615
2616private:
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002617 unsigned __loop_count() const {return __loop_count_;}
2618
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002619 template <class _ForwardIterator>
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002620 _ForwardIterator
2621 __parse(_ForwardIterator __first, _ForwardIterator __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002622 template <class _ForwardIterator>
2623 _ForwardIterator
2624 __parse_basic_reg_exp(_ForwardIterator __first, _ForwardIterator __last);
2625 template <class _ForwardIterator>
2626 _ForwardIterator
2627 __parse_RE_expression(_ForwardIterator __first, _ForwardIterator __last);
2628 template <class _ForwardIterator>
2629 _ForwardIterator
2630 __parse_simple_RE(_ForwardIterator __first, _ForwardIterator __last);
2631 template <class _ForwardIterator>
2632 _ForwardIterator
2633 __parse_nondupl_RE(_ForwardIterator __first, _ForwardIterator __last);
2634 template <class _ForwardIterator>
2635 _ForwardIterator
2636 __parse_one_char_or_coll_elem_RE(_ForwardIterator __first, _ForwardIterator __last);
2637 template <class _ForwardIterator>
2638 _ForwardIterator
2639 __parse_Back_open_paren(_ForwardIterator __first, _ForwardIterator __last);
2640 template <class _ForwardIterator>
2641 _ForwardIterator
2642 __parse_Back_close_paren(_ForwardIterator __first, _ForwardIterator __last);
2643 template <class _ForwardIterator>
2644 _ForwardIterator
2645 __parse_Back_open_brace(_ForwardIterator __first, _ForwardIterator __last);
2646 template <class _ForwardIterator>
2647 _ForwardIterator
2648 __parse_Back_close_brace(_ForwardIterator __first, _ForwardIterator __last);
2649 template <class _ForwardIterator>
2650 _ForwardIterator
2651 __parse_BACKREF(_ForwardIterator __first, _ForwardIterator __last);
2652 template <class _ForwardIterator>
2653 _ForwardIterator
2654 __parse_ORD_CHAR(_ForwardIterator __first, _ForwardIterator __last);
2655 template <class _ForwardIterator>
2656 _ForwardIterator
2657 __parse_QUOTED_CHAR(_ForwardIterator __first, _ForwardIterator __last);
2658 template <class _ForwardIterator>
2659 _ForwardIterator
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002660 __parse_RE_dupl_symbol(_ForwardIterator __first, _ForwardIterator __last,
Howard Hinnante77aa5e2010-07-08 17:43:58 +00002661 __owns_one_state<_CharT>* __s,
2662 unsigned __mexp_begin, unsigned __mexp_end);
Howard Hinnant0de86b62010-06-25 20:56:08 +00002663 template <class _ForwardIterator>
2664 _ForwardIterator
Howard Hinnantaa698082010-07-16 19:08:36 +00002665 __parse_ERE_dupl_symbol(_ForwardIterator __first, _ForwardIterator __last,
2666 __owns_one_state<_CharT>* __s,
2667 unsigned __mexp_begin, unsigned __mexp_end);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002668 template <class _ForwardIterator>
2669 _ForwardIterator
Howard Hinnant0de86b62010-06-25 20:56:08 +00002670 __parse_bracket_expression(_ForwardIterator __first, _ForwardIterator __last);
2671 template <class _ForwardIterator>
2672 _ForwardIterator
Howard Hinnant173968a2010-07-13 21:48:06 +00002673 __parse_follow_list(_ForwardIterator __first, _ForwardIterator __last,
2674 __bracket_expression<_CharT, _Traits>* __ml);
Howard Hinnant0de86b62010-06-25 20:56:08 +00002675 template <class _ForwardIterator>
2676 _ForwardIterator
Howard Hinnant173968a2010-07-13 21:48:06 +00002677 __parse_expression_term(_ForwardIterator __first, _ForwardIterator __last,
2678 __bracket_expression<_CharT, _Traits>* __ml);
Howard Hinnant0de86b62010-06-25 20:56:08 +00002679 template <class _ForwardIterator>
2680 _ForwardIterator
Howard Hinnant173968a2010-07-13 21:48:06 +00002681 __parse_equivalence_class(_ForwardIterator __first, _ForwardIterator __last,
2682 __bracket_expression<_CharT, _Traits>* __ml);
Howard Hinnant0de86b62010-06-25 20:56:08 +00002683 template <class _ForwardIterator>
2684 _ForwardIterator
Howard Hinnant173968a2010-07-13 21:48:06 +00002685 __parse_character_class(_ForwardIterator __first, _ForwardIterator __last,
2686 __bracket_expression<_CharT, _Traits>* __ml);
Howard Hinnant0de86b62010-06-25 20:56:08 +00002687 template <class _ForwardIterator>
2688 _ForwardIterator
Howard Hinnant173968a2010-07-13 21:48:06 +00002689 __parse_collating_symbol(_ForwardIterator __first, _ForwardIterator __last,
2690 basic_string<_CharT>& __col_sym);
Howard Hinnant0de86b62010-06-25 20:56:08 +00002691 template <class _ForwardIterator>
2692 _ForwardIterator
2693 __parse_DUP_COUNT(_ForwardIterator __first, _ForwardIterator __last, int& __c);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002694 template <class _ForwardIterator>
2695 _ForwardIterator
2696 __parse_extended_reg_exp(_ForwardIterator __first, _ForwardIterator __last);
2697 template <class _ForwardIterator>
2698 _ForwardIterator
2699 __parse_ERE_branch(_ForwardIterator __first, _ForwardIterator __last);
2700 template <class _ForwardIterator>
2701 _ForwardIterator
2702 __parse_ERE_expression(_ForwardIterator __first, _ForwardIterator __last);
2703 template <class _ForwardIterator>
2704 _ForwardIterator
2705 __parse_one_char_or_coll_elem_ERE(_ForwardIterator __first, _ForwardIterator __last);
2706 template <class _ForwardIterator>
2707 _ForwardIterator
2708 __parse_ORD_CHAR_ERE(_ForwardIterator __first, _ForwardIterator __last);
2709 template <class _ForwardIterator>
2710 _ForwardIterator
2711 __parse_QUOTED_CHAR_ERE(_ForwardIterator __first, _ForwardIterator __last);
Howard Hinnant2ade7c22010-07-22 17:53:24 +00002712 template <class _ForwardIterator>
2713 _ForwardIterator
2714 __parse_ecma_exp(_ForwardIterator __first, _ForwardIterator __last);
2715 template <class _ForwardIterator>
2716 _ForwardIterator
2717 __parse_alternative(_ForwardIterator __first, _ForwardIterator __last);
2718 template <class _ForwardIterator>
2719 _ForwardIterator
2720 __parse_term(_ForwardIterator __first, _ForwardIterator __last);
2721 template <class _ForwardIterator>
2722 _ForwardIterator
2723 __parse_assertion(_ForwardIterator __first, _ForwardIterator __last);
2724 template <class _ForwardIterator>
2725 _ForwardIterator
2726 __parse_atom(_ForwardIterator __first, _ForwardIterator __last);
2727 template <class _ForwardIterator>
2728 _ForwardIterator
Howard Hinnant17615b02010-07-27 01:25:38 +00002729 __parse_atom_escape(_ForwardIterator __first, _ForwardIterator __last);
2730 template <class _ForwardIterator>
2731 _ForwardIterator
2732 __parse_decimal_escape(_ForwardIterator __first, _ForwardIterator __last);
2733 template <class _ForwardIterator>
2734 _ForwardIterator
2735 __parse_character_class_escape(_ForwardIterator __first, _ForwardIterator __last);
2736 template <class _ForwardIterator>
2737 _ForwardIterator
Howard Hinnant15476f32010-07-28 17:35:27 +00002738 __parse_character_escape(_ForwardIterator __first, _ForwardIterator __last,
2739 basic_string<_CharT>* __str = nullptr);
Howard Hinnant17615b02010-07-27 01:25:38 +00002740 template <class _ForwardIterator>
2741 _ForwardIterator
2742 __parse_pattern_character(_ForwardIterator __first, _ForwardIterator __last);
Howard Hinnant856846b2010-07-27 19:53:10 +00002743 template <class _ForwardIterator>
2744 _ForwardIterator
2745 __parse_grep(_ForwardIterator __first, _ForwardIterator __last);
2746 template <class _ForwardIterator>
2747 _ForwardIterator
2748 __parse_egrep(_ForwardIterator __first, _ForwardIterator __last);
Howard Hinnant15476f32010-07-28 17:35:27 +00002749 template <class _ForwardIterator>
2750 _ForwardIterator
2751 __parse_class_escape(_ForwardIterator __first, _ForwardIterator __last,
2752 basic_string<_CharT>& __str,
2753 __bracket_expression<_CharT, _Traits>* __ml);
2754 template <class _ForwardIterator>
2755 _ForwardIterator
2756 __parse_awk_escape(_ForwardIterator __first, _ForwardIterator __last,
2757 basic_string<_CharT>* __str = nullptr);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002758
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00002759 void __push_l_anchor() {__left_anchor_ = true;}
2760 void __push_r_anchor();
Howard Hinnantac303862010-07-12 15:51:17 +00002761 void __push_match_any();
Howard Hinnant17615b02010-07-27 01:25:38 +00002762 void __push_match_any_but_newline();
Howard Hinnante77aa5e2010-07-08 17:43:58 +00002763 void __push_greedy_inf_repeat(size_t __min, __owns_one_state<_CharT>* __s,
2764 unsigned __mexp_begin = 0, unsigned __mexp_end = 0)
2765 {__push_loop(__min, numeric_limits<size_t>::max(), __s,
2766 __mexp_begin, __mexp_end);}
Howard Hinnant17615b02010-07-27 01:25:38 +00002767 void __push_nongreedy_inf_repeat(size_t __min, __owns_one_state<_CharT>* __s,
2768 unsigned __mexp_begin = 0, unsigned __mexp_end = 0)
2769 {__push_loop(__min, numeric_limits<size_t>::max(), __s,
2770 __mexp_begin, __mexp_end, false);}
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002771 void __push_loop(size_t __min, size_t __max, __owns_one_state<_CharT>* __s,
2772 size_t __mexp_begin = 0, size_t __mexp_end = 0,
2773 bool __greedy = true);
Howard Hinnant173968a2010-07-13 21:48:06 +00002774 __bracket_expression<_CharT, _Traits>* __start_matching_list(bool __negate);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002775 void __push_char(value_type __c);
Howard Hinnantcba352d2010-07-12 18:16:05 +00002776 void __push_back_ref(int __i);
Howard Hinnantaa698082010-07-16 19:08:36 +00002777 void __push_alternation(__owns_one_state<_CharT>* __sa,
2778 __owns_one_state<_CharT>* __sb);
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00002779 void __push_begin_marked_subexpression();
2780 void __push_end_marked_subexpression(unsigned);
Howard Hinnant2ade7c22010-07-22 17:53:24 +00002781 void __push_empty();
Howard Hinnant17615b02010-07-27 01:25:38 +00002782 void __push_word_boundary(bool);
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002783 void __push_lookahead(const basic_regex&, bool);
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00002784
Howard Hinnant22ce0b42010-07-14 21:14:52 +00002785 template <class _Allocator>
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00002786 bool
Howard Hinnant22ce0b42010-07-14 21:14:52 +00002787 __search(const _CharT* __first, const _CharT* __last,
2788 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00002789 regex_constants::match_flag_type __flags) const;
2790
Howard Hinnant22ce0b42010-07-14 21:14:52 +00002791 template <class _Allocator>
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002792 bool
Howard Hinnant22ce0b42010-07-14 21:14:52 +00002793 __match_at_start(const _CharT* __first, const _CharT* __last,
2794 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002795 regex_constants::match_flag_type __flags) const;
Howard Hinnant17615b02010-07-27 01:25:38 +00002796 template <class _Allocator>
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002797 bool
Howard Hinnant17615b02010-07-27 01:25:38 +00002798 __match_at_start_ecma(const _CharT* __first, const _CharT* __last,
2799 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002800 regex_constants::match_flag_type __flags) const;
Howard Hinnant22ce0b42010-07-14 21:14:52 +00002801 template <class _Allocator>
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002802 bool
2803 __match_at_start_posix_nosubs(const _CharT* __first, const _CharT* __last,
Howard Hinnant22ce0b42010-07-14 21:14:52 +00002804 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002805 regex_constants::match_flag_type __flags) const;
Howard Hinnant22ce0b42010-07-14 21:14:52 +00002806 template <class _Allocator>
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002807 bool
Howard Hinnant22ce0b42010-07-14 21:14:52 +00002808 __match_at_start_posix_subs(const _CharT* __first, const _CharT* __last,
2809 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002810 regex_constants::match_flag_type __flags) const;
2811
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00002812 template <class _B, class _A, class _C, class _T>
2813 friend
2814 bool
2815 regex_search(_B, _B, match_results<_B, _A>&, const basic_regex<_C, _T>&,
2816 regex_constants::match_flag_type);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002817
Howard Hinnant22ce0b42010-07-14 21:14:52 +00002818 template <class _A, class _C, class _T>
2819 friend
2820 bool
2821 regex_search(const _C*, const _C*, match_results<const _C*, _A>&,
2822 const basic_regex<_C, _T>&, regex_constants::match_flag_type);
2823
2824 template <class _B, class _C, class _T>
2825 friend
2826 bool
2827 regex_search(_B, _B, const basic_regex<_C, _T>&,
2828 regex_constants::match_flag_type);
2829
2830 template <class _C, class _T>
2831 friend
2832 bool
2833 regex_search(const _C*, const _C*,
2834 const basic_regex<_C, _T>&, regex_constants::match_flag_type);
2835
2836 template <class _C, class _A, class _T>
2837 friend
2838 bool
2839 regex_search(const _C*, match_results<const _C*, _A>&, const basic_regex<_C, _T>&,
2840 regex_constants::match_flag_type);
2841
2842 template <class _ST, class _SA, class _C, class _T>
2843 friend
2844 bool
2845 regex_search(const basic_string<_C, _ST, _SA>& __s,
2846 const basic_regex<_C, _T>& __e,
2847 regex_constants::match_flag_type __flags);
2848
2849 template <class _ST, class _SA, class _A, class _C, class _T>
2850 friend
2851 bool
2852 regex_search(const basic_string<_C, _ST, _SA>& __s,
2853 match_results<typename basic_string<_C, _ST, _SA>::const_iterator, _A>&,
2854 const basic_regex<_C, _T>& __e,
2855 regex_constants::match_flag_type __flags);
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002856
2857 template <class, class> friend class __lookahead;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002858};
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002859
2860template <class _CharT, class _Traits>
Howard Hinnant7026a172010-08-13 18:11:23 +00002861void
2862basic_regex<_CharT, _Traits>::swap(basic_regex& __r)
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002863{
Howard Hinnant7026a172010-08-13 18:11:23 +00002864 using _STD::swap;
2865 swap(__traits_, __r.__traits_);
2866 swap(__flags_, __r.__flags_);
2867 swap(__marked_count_, __r.__marked_count_);
2868 swap(__loop_count_, __r.__loop_count_);
2869 swap(__open_count_, __r.__open_count_);
2870 swap(__start_, __r.__start_);
2871 swap(__end_, __r.__end_);
2872 swap(__left_anchor_, __r.__left_anchor_);
2873}
2874
2875template <class _CharT, class _Traits>
2876inline _LIBCPP_INLINE_VISIBILITY
2877void
2878swap(basic_regex<_CharT, _Traits>& __x, basic_regex<_CharT, _Traits>& __y)
2879{
2880 return __x.swap(__y);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002881}
2882
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002883// __lookahead
2884
2885template <class _CharT, class _Traits>
2886class __lookahead
2887 : public __owns_one_state<_CharT>
2888{
2889 typedef __owns_one_state<_CharT> base;
2890
2891 basic_regex<_CharT, _Traits> __exp_;
2892 bool __invert_;
2893
2894 __lookahead(const __lookahead&);
2895 __lookahead& operator=(const __lookahead&);
2896public:
2897 typedef _STD::__state<_CharT> __state;
2898
2899 __lookahead(const basic_regex<_CharT, _Traits>& __exp, bool __invert, __node<_CharT>* __s)
2900 : base(__s), __exp_(__exp), __invert_(__invert) {}
2901
2902 virtual void __exec(__state&) const;
2903
2904 virtual string speak() const
2905 {
2906 ostringstream os;
2907 if (__invert_)
2908 os << "not lookahead";
2909 else
2910 os << "lookahead";
2911 return os.str();
2912 }
2913};
2914
2915template <class _CharT, class _Traits>
2916void
2917__lookahead<_CharT, _Traits>::__exec(__state& __s) const
2918{
2919 match_results<const _CharT*> __m;
2920 __m.__init(1 + __exp_.mark_count(), __s.__current_, __s.__last_);
2921 bool __matched = __exp_.__match_at_start_ecma(__s.__current_, __s.__last_,
2922 __m, __s.__flags_);
2923 if (__matched != __invert_)
2924 {
2925 __s.__do_ = __state::__accept_but_not_consume;
2926 __s.__node_ = this->first();
2927 }
2928 else
2929 {
2930 __s.__do_ = __state::__reject;
2931 __s.__node_ = nullptr;
2932 }
2933}
2934
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002935template <class _CharT, class _Traits>
2936template <class _ForwardIterator>
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002937_ForwardIterator
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002938basic_regex<_CharT, _Traits>::__parse(_ForwardIterator __first,
2939 _ForwardIterator __last)
2940{
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002941 {
Howard Hinnantac303862010-07-12 15:51:17 +00002942 unique_ptr<__node> __h(new __end_state<_CharT>);
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002943 __start_.reset(new __empty_state<_CharT>(__h.get()));
2944 __h.release();
2945 __end_ = __start_.get();
2946 }
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00002947 switch (__flags_ & 0x1F0)
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002948 {
2949 case ECMAScript:
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002950 __first = __parse_ecma_exp(__first, __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002951 break;
2952 case basic:
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002953 __first = __parse_basic_reg_exp(__first, __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002954 break;
2955 case extended:
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002956 case awk:
Howard Hinnant15476f32010-07-28 17:35:27 +00002957 __first = __parse_extended_reg_exp(__first, __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002958 break;
2959 case grep:
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002960 __first = __parse_grep(__first, __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002961 break;
2962 case egrep:
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002963 __first = __parse_egrep(__first, __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002964 break;
Howard Hinnantd4444702010-08-11 17:04:31 +00002965#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002966 default:
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00002967 throw regex_error(regex_constants::__re_err_grammar);
Howard Hinnantd4444702010-08-11 17:04:31 +00002968#endif
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002969 }
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002970 return __first;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002971}
2972
2973template <class _CharT, class _Traits>
2974template <class _ForwardIterator>
2975_ForwardIterator
2976basic_regex<_CharT, _Traits>::__parse_basic_reg_exp(_ForwardIterator __first,
2977 _ForwardIterator __last)
2978{
2979 if (__first != __last)
2980 {
2981 if (*__first == '^')
2982 {
2983 __push_l_anchor();
2984 ++__first;
2985 }
2986 if (__first != __last)
2987 {
2988 __first = __parse_RE_expression(__first, __last);
2989 if (__first != __last)
2990 {
2991 _ForwardIterator __temp = next(__first);
2992 if (__temp == __last && *__first == '$')
2993 {
2994 __push_r_anchor();
2995 ++__first;
2996 }
2997 }
2998 }
Howard Hinnantd4444702010-08-11 17:04:31 +00002999#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003000 if (__first != __last)
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00003001 throw regex_error(regex_constants::__re_err_empty);
Howard Hinnantd4444702010-08-11 17:04:31 +00003002#endif
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003003 }
3004 return __first;
3005}
3006
3007template <class _CharT, class _Traits>
3008template <class _ForwardIterator>
3009_ForwardIterator
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003010basic_regex<_CharT, _Traits>::__parse_extended_reg_exp(_ForwardIterator __first,
3011 _ForwardIterator __last)
3012{
Howard Hinnantaa698082010-07-16 19:08:36 +00003013 __owns_one_state<_CharT>* __sa = __end_;
3014 _ForwardIterator __temp = __parse_ERE_branch(__first, __last);
Howard Hinnantd4444702010-08-11 17:04:31 +00003015#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantaa698082010-07-16 19:08:36 +00003016 if (__temp == __first)
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00003017 throw regex_error(regex_constants::__re_err_empty);
Howard Hinnantd4444702010-08-11 17:04:31 +00003018#endif
Howard Hinnantaa698082010-07-16 19:08:36 +00003019 __first = __temp;
3020 while (__first != __last && *__first == '|')
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003021 {
Howard Hinnantaa698082010-07-16 19:08:36 +00003022 __owns_one_state<_CharT>* __sb = __end_;
3023 __temp = __parse_ERE_branch(++__first, __last);
Howard Hinnantd4444702010-08-11 17:04:31 +00003024#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003025 if (__temp == __first)
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00003026 throw regex_error(regex_constants::__re_err_empty);
Howard Hinnantd4444702010-08-11 17:04:31 +00003027#endif
Howard Hinnantaa698082010-07-16 19:08:36 +00003028 __push_alternation(__sa, __sb);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003029 __first = __temp;
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003030 }
3031 return __first;
3032}
3033
3034template <class _CharT, class _Traits>
3035template <class _ForwardIterator>
3036_ForwardIterator
3037basic_regex<_CharT, _Traits>::__parse_ERE_branch(_ForwardIterator __first,
3038 _ForwardIterator __last)
3039{
3040 _ForwardIterator __temp = __parse_ERE_expression(__first, __last);
Howard Hinnantd4444702010-08-11 17:04:31 +00003041#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003042 if (__temp == __first)
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00003043 throw regex_error(regex_constants::__re_err_empty);
Howard Hinnantd4444702010-08-11 17:04:31 +00003044#endif
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003045 do
3046 {
3047 __first = __temp;
3048 __temp = __parse_ERE_expression(__first, __last);
3049 } while (__temp != __first);
3050 return __first;
3051}
3052
3053template <class _CharT, class _Traits>
3054template <class _ForwardIterator>
3055_ForwardIterator
3056basic_regex<_CharT, _Traits>::__parse_ERE_expression(_ForwardIterator __first,
3057 _ForwardIterator __last)
3058{
Howard Hinnantaa698082010-07-16 19:08:36 +00003059 __owns_one_state<_CharT>* __e = __end_;
3060 unsigned __mexp_begin = __marked_count_;
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003061 _ForwardIterator __temp = __parse_one_char_or_coll_elem_ERE(__first, __last);
3062 if (__temp == __first && __temp != __last)
3063 {
3064 switch (*__temp)
3065 {
3066 case '^':
3067 __push_l_anchor();
3068 ++__temp;
3069 break;
3070 case '$':
3071 __push_r_anchor();
3072 ++__temp;
3073 break;
3074 case '(':
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00003075 __push_begin_marked_subexpression();
3076 unsigned __temp_count = __marked_count_;
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003077 ++__open_count_;
3078 __temp = __parse_extended_reg_exp(++__temp, __last);
Howard Hinnantd4444702010-08-11 17:04:31 +00003079#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003080 if (__temp == __last || *__temp != ')')
3081 throw regex_error(regex_constants::error_paren);
Howard Hinnantd4444702010-08-11 17:04:31 +00003082#endif
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00003083 __push_end_marked_subexpression(__temp_count);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003084 --__open_count_;
3085 ++__temp;
3086 break;
3087 }
3088 }
3089 if (__temp != __first)
Howard Hinnantaa698082010-07-16 19:08:36 +00003090 __temp = __parse_ERE_dupl_symbol(__temp, __last, __e, __mexp_begin+1,
3091 __marked_count_+1);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003092 __first = __temp;
3093 return __first;
3094}
3095
3096template <class _CharT, class _Traits>
3097template <class _ForwardIterator>
3098_ForwardIterator
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003099basic_regex<_CharT, _Traits>::__parse_RE_expression(_ForwardIterator __first,
3100 _ForwardIterator __last)
3101{
3102 while (true)
3103 {
3104 _ForwardIterator __temp = __parse_simple_RE(__first, __last);
3105 if (__temp == __first)
3106 break;
3107 __first = __temp;
3108 }
3109 return __first;
3110}
3111
3112template <class _CharT, class _Traits>
3113template <class _ForwardIterator>
3114_ForwardIterator
3115basic_regex<_CharT, _Traits>::__parse_simple_RE(_ForwardIterator __first,
3116 _ForwardIterator __last)
3117{
3118 if (__first != __last)
3119 {
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003120 __owns_one_state<_CharT>* __e = __end_;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003121 unsigned __mexp_begin = __marked_count_;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003122 _ForwardIterator __temp = __parse_nondupl_RE(__first, __last);
3123 if (__temp != __first)
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003124 __first = __parse_RE_dupl_symbol(__temp, __last, __e,
3125 __mexp_begin+1, __marked_count_+1);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003126 }
3127 return __first;
3128}
3129
3130template <class _CharT, class _Traits>
3131template <class _ForwardIterator>
3132_ForwardIterator
3133basic_regex<_CharT, _Traits>::__parse_nondupl_RE(_ForwardIterator __first,
3134 _ForwardIterator __last)
3135{
3136 _ForwardIterator __temp = __first;
3137 __first = __parse_one_char_or_coll_elem_RE(__first, __last);
3138 if (__temp == __first)
3139 {
3140 __temp = __parse_Back_open_paren(__first, __last);
3141 if (__temp != __first)
3142 {
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00003143 __push_begin_marked_subexpression();
3144 unsigned __temp_count = __marked_count_;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003145 __first = __parse_RE_expression(__temp, __last);
3146 __temp = __parse_Back_close_paren(__first, __last);
Howard Hinnantd4444702010-08-11 17:04:31 +00003147#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003148 if (__temp == __first)
3149 throw regex_error(regex_constants::error_paren);
Howard Hinnantd4444702010-08-11 17:04:31 +00003150#endif
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00003151 __push_end_marked_subexpression(__temp_count);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003152 __first = __temp;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003153 }
3154 else
3155 __first = __parse_BACKREF(__first, __last);
3156 }
3157 return __first;
3158}
3159
3160template <class _CharT, class _Traits>
3161template <class _ForwardIterator>
3162_ForwardIterator
3163basic_regex<_CharT, _Traits>::__parse_one_char_or_coll_elem_RE(
3164 _ForwardIterator __first,
3165 _ForwardIterator __last)
3166{
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003167 _ForwardIterator __temp = __parse_ORD_CHAR(__first, __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003168 if (__temp == __first)
3169 {
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003170 __temp = __parse_QUOTED_CHAR(__first, __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003171 if (__temp == __first)
3172 {
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003173 if (__temp != __last && *__temp == '.')
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003174 {
3175 __push_match_any();
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003176 ++__temp;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003177 }
3178 else
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003179 __temp = __parse_bracket_expression(__first, __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003180 }
3181 }
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003182 __first = __temp;
3183 return __first;
3184}
3185
3186template <class _CharT, class _Traits>
3187template <class _ForwardIterator>
3188_ForwardIterator
3189basic_regex<_CharT, _Traits>::__parse_one_char_or_coll_elem_ERE(
3190 _ForwardIterator __first,
3191 _ForwardIterator __last)
3192{
3193 _ForwardIterator __temp = __parse_ORD_CHAR_ERE(__first, __last);
3194 if (__temp == __first)
3195 {
3196 __temp = __parse_QUOTED_CHAR_ERE(__first, __last);
3197 if (__temp == __first)
3198 {
3199 if (__temp != __last && *__temp == '.')
3200 {
3201 __push_match_any();
3202 ++__temp;
3203 }
3204 else
3205 __temp = __parse_bracket_expression(__first, __last);
3206 }
3207 }
3208 __first = __temp;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003209 return __first;
3210}
3211
3212template <class _CharT, class _Traits>
3213template <class _ForwardIterator>
3214_ForwardIterator
3215basic_regex<_CharT, _Traits>::__parse_Back_open_paren(_ForwardIterator __first,
3216 _ForwardIterator __last)
3217{
3218 if (__first != __last)
3219 {
3220 _ForwardIterator __temp = next(__first);
3221 if (__temp != __last)
3222 {
3223 if (*__first == '\\' && *__temp == '(')
3224 __first = ++__temp;
3225 }
3226 }
3227 return __first;
3228}
3229
3230template <class _CharT, class _Traits>
3231template <class _ForwardIterator>
3232_ForwardIterator
3233basic_regex<_CharT, _Traits>::__parse_Back_close_paren(_ForwardIterator __first,
3234 _ForwardIterator __last)
3235{
3236 if (__first != __last)
3237 {
3238 _ForwardIterator __temp = next(__first);
3239 if (__temp != __last)
3240 {
3241 if (*__first == '\\' && *__temp == ')')
3242 __first = ++__temp;
3243 }
3244 }
3245 return __first;
3246}
3247
3248template <class _CharT, class _Traits>
3249template <class _ForwardIterator>
3250_ForwardIterator
3251basic_regex<_CharT, _Traits>::__parse_Back_open_brace(_ForwardIterator __first,
3252 _ForwardIterator __last)
3253{
3254 if (__first != __last)
3255 {
3256 _ForwardIterator __temp = next(__first);
3257 if (__temp != __last)
3258 {
3259 if (*__first == '\\' && *__temp == '{')
3260 __first = ++__temp;
3261 }
3262 }
3263 return __first;
3264}
3265
3266template <class _CharT, class _Traits>
3267template <class _ForwardIterator>
3268_ForwardIterator
3269basic_regex<_CharT, _Traits>::__parse_Back_close_brace(_ForwardIterator __first,
3270 _ForwardIterator __last)
3271{
3272 if (__first != __last)
3273 {
3274 _ForwardIterator __temp = next(__first);
3275 if (__temp != __last)
3276 {
3277 if (*__first == '\\' && *__temp == '}')
3278 __first = ++__temp;
3279 }
3280 }
3281 return __first;
3282}
3283
3284template <class _CharT, class _Traits>
3285template <class _ForwardIterator>
3286_ForwardIterator
3287basic_regex<_CharT, _Traits>::__parse_BACKREF(_ForwardIterator __first,
3288 _ForwardIterator __last)
3289{
3290 if (__first != __last)
3291 {
3292 _ForwardIterator __temp = next(__first);
3293 if (__temp != __last)
3294 {
3295 if (*__first == '\\' && '1' <= *__temp && *__temp <= '9')
3296 {
3297 __push_back_ref(*__temp - '0');
3298 __first = ++__temp;
3299 }
3300 }
3301 }
3302 return __first;
3303}
3304
3305template <class _CharT, class _Traits>
3306template <class _ForwardIterator>
3307_ForwardIterator
3308basic_regex<_CharT, _Traits>::__parse_ORD_CHAR(_ForwardIterator __first,
3309 _ForwardIterator __last)
3310{
3311 if (__first != __last)
3312 {
3313 _ForwardIterator __temp = next(__first);
3314 if (__temp == __last && *__first == '$')
3315 return __first;
3316 // Not called inside a bracket
3317 if (*__first == '.' || *__first == '\\' || *__first == '[')
3318 return __first;
Howard Hinnant0de86b62010-06-25 20:56:08 +00003319 __push_char(*__first);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003320 ++__first;
3321 }
3322 return __first;
3323}
3324
3325template <class _CharT, class _Traits>
3326template <class _ForwardIterator>
3327_ForwardIterator
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003328basic_regex<_CharT, _Traits>::__parse_ORD_CHAR_ERE(_ForwardIterator __first,
3329 _ForwardIterator __last)
3330{
3331 if (__first != __last)
3332 {
3333 switch (*__first)
3334 {
3335 case '^':
3336 case '.':
3337 case '[':
3338 case '$':
3339 case '(':
3340 case '|':
3341 case '*':
3342 case '+':
3343 case '?':
3344 case '{':
3345 case '\\':
3346 break;
3347 case ')':
3348 if (__open_count_ == 0)
3349 {
3350 __push_char(*__first);
3351 ++__first;
3352 }
3353 break;
3354 default:
3355 __push_char(*__first);
3356 ++__first;
3357 break;
3358 }
3359 }
3360 return __first;
3361}
3362
3363template <class _CharT, class _Traits>
3364template <class _ForwardIterator>
3365_ForwardIterator
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003366basic_regex<_CharT, _Traits>::__parse_QUOTED_CHAR(_ForwardIterator __first,
3367 _ForwardIterator __last)
3368{
3369 if (__first != __last)
3370 {
3371 _ForwardIterator __temp = next(__first);
3372 if (__temp != __last)
3373 {
3374 if (*__first == '\\')
3375 {
3376 switch (*__temp)
3377 {
3378 case '^':
3379 case '.':
3380 case '*':
3381 case '[':
3382 case '$':
3383 case '\\':
Howard Hinnant0de86b62010-06-25 20:56:08 +00003384 __push_char(*__temp);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003385 __first = ++__temp;
3386 break;
3387 }
3388 }
3389 }
3390 }
3391 return __first;
3392}
3393
3394template <class _CharT, class _Traits>
3395template <class _ForwardIterator>
3396_ForwardIterator
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003397basic_regex<_CharT, _Traits>::__parse_QUOTED_CHAR_ERE(_ForwardIterator __first,
3398 _ForwardIterator __last)
3399{
3400 if (__first != __last)
3401 {
3402 _ForwardIterator __temp = next(__first);
3403 if (__temp != __last)
3404 {
3405 if (*__first == '\\')
3406 {
3407 switch (*__temp)
3408 {
3409 case '^':
3410 case '.':
3411 case '*':
3412 case '[':
3413 case '$':
3414 case '\\':
3415 case '(':
3416 case ')':
3417 case '|':
3418 case '+':
3419 case '?':
3420 case '{':
3421 __push_char(*__temp);
3422 __first = ++__temp;
3423 break;
Howard Hinnant15476f32010-07-28 17:35:27 +00003424 default:
3425 if ((__flags_ & 0x1F0) == awk)
3426 __first = __parse_awk_escape(++__first, __last);
3427 break;
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003428 }
3429 }
3430 }
3431 }
3432 return __first;
3433}
3434
3435template <class _CharT, class _Traits>
3436template <class _ForwardIterator>
3437_ForwardIterator
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003438basic_regex<_CharT, _Traits>::__parse_RE_dupl_symbol(_ForwardIterator __first,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003439 _ForwardIterator __last,
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003440 __owns_one_state<_CharT>* __s,
3441 unsigned __mexp_begin,
3442 unsigned __mexp_end)
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003443{
3444 if (__first != __last)
3445 {
Howard Hinnant0de86b62010-06-25 20:56:08 +00003446 if (*__first == '*')
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003447 {
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003448 __push_greedy_inf_repeat(0, __s, __mexp_begin, __mexp_end);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003449 ++__first;
3450 }
3451 else
3452 {
3453 _ForwardIterator __temp = __parse_Back_open_brace(__first, __last);
3454 if (__temp != __first)
3455 {
3456 int __min = 0;
3457 __first = __temp;
3458 __temp = __parse_DUP_COUNT(__first, __last, __min);
Howard Hinnantd4444702010-08-11 17:04:31 +00003459#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003460 if (__temp == __first)
3461 throw regex_error(regex_constants::error_badbrace);
Howard Hinnantd4444702010-08-11 17:04:31 +00003462#endif
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003463 __first = __temp;
Howard Hinnantd4444702010-08-11 17:04:31 +00003464#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003465 if (__first == __last)
3466 throw regex_error(regex_constants::error_brace);
Howard Hinnantd4444702010-08-11 17:04:31 +00003467#endif
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003468 if (*__first != ',')
3469 {
3470 __temp = __parse_Back_close_brace(__first, __last);
Howard Hinnantd4444702010-08-11 17:04:31 +00003471#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003472 if (__temp == __first)
3473 throw regex_error(regex_constants::error_brace);
Howard Hinnantd4444702010-08-11 17:04:31 +00003474#endif
Howard Hinnantcba352d2010-07-12 18:16:05 +00003475 __push_loop(__min, __min, __s, __mexp_begin, __mexp_end,
3476 true);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003477 __first = __temp;
3478 }
3479 else
3480 {
3481 ++__first; // consume ','
3482 int __max = -1;
3483 __first = __parse_DUP_COUNT(__first, __last, __max);
3484 __temp = __parse_Back_close_brace(__first, __last);
Howard Hinnantd4444702010-08-11 17:04:31 +00003485#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003486 if (__temp == __first)
3487 throw regex_error(regex_constants::error_brace);
Howard Hinnantd4444702010-08-11 17:04:31 +00003488#endif
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003489 if (__max == -1)
Howard Hinnantaa698082010-07-16 19:08:36 +00003490 __push_greedy_inf_repeat(__min, __s, __mexp_begin, __mexp_end);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003491 else
3492 {
Howard Hinnantd4444702010-08-11 17:04:31 +00003493#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003494 if (__max < __min)
3495 throw regex_error(regex_constants::error_badbrace);
Howard Hinnantd4444702010-08-11 17:04:31 +00003496#endif
Howard Hinnantcba352d2010-07-12 18:16:05 +00003497 __push_loop(__min, __max, __s, __mexp_begin, __mexp_end,
3498 true);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003499 }
3500 __first = __temp;
3501 }
3502 }
3503 }
3504 }
3505 return __first;
3506}
3507
Howard Hinnant0de86b62010-06-25 20:56:08 +00003508template <class _CharT, class _Traits>
3509template <class _ForwardIterator>
3510_ForwardIterator
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003511basic_regex<_CharT, _Traits>::__parse_ERE_dupl_symbol(_ForwardIterator __first,
Howard Hinnantaa698082010-07-16 19:08:36 +00003512 _ForwardIterator __last,
3513 __owns_one_state<_CharT>* __s,
3514 unsigned __mexp_begin,
3515 unsigned __mexp_end)
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003516{
3517 if (__first != __last)
3518 {
Howard Hinnanta0d045b2010-07-29 00:36:00 +00003519 unsigned __grammar = __flags_ & 0x1F0;
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003520 switch (*__first)
3521 {
3522 case '*':
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003523 ++__first;
Howard Hinnanta0d045b2010-07-29 00:36:00 +00003524 if (__grammar == ECMAScript && __first != __last && *__first == '?')
Howard Hinnant17615b02010-07-27 01:25:38 +00003525 {
3526 ++__first;
3527 __push_nongreedy_inf_repeat(0, __s, __mexp_begin, __mexp_end);
3528 }
3529 else
3530 __push_greedy_inf_repeat(0, __s, __mexp_begin, __mexp_end);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003531 break;
3532 case '+':
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003533 ++__first;
Howard Hinnanta0d045b2010-07-29 00:36:00 +00003534 if (__grammar == ECMAScript && __first != __last && *__first == '?')
Howard Hinnant17615b02010-07-27 01:25:38 +00003535 {
3536 ++__first;
3537 __push_nongreedy_inf_repeat(1, __s, __mexp_begin, __mexp_end);
3538 }
3539 else
3540 __push_greedy_inf_repeat(1, __s, __mexp_begin, __mexp_end);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003541 break;
3542 case '?':
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003543 ++__first;
Howard Hinnanta0d045b2010-07-29 00:36:00 +00003544 if (__grammar == ECMAScript && __first != __last && *__first == '?')
Howard Hinnant17615b02010-07-27 01:25:38 +00003545 {
3546 ++__first;
3547 __push_loop(0, 1, __s, __mexp_begin, __mexp_end, false);
3548 }
3549 else
3550 __push_loop(0, 1, __s, __mexp_begin, __mexp_end);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003551 break;
3552 case '{':
3553 {
3554 int __min;
Howard Hinnantaa698082010-07-16 19:08:36 +00003555 _ForwardIterator __temp = __parse_DUP_COUNT(++__first, __last, __min);
Howard Hinnantd4444702010-08-11 17:04:31 +00003556#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003557 if (__temp == __first)
3558 throw regex_error(regex_constants::error_badbrace);
Howard Hinnantd4444702010-08-11 17:04:31 +00003559#endif
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003560 __first = __temp;
Howard Hinnantd4444702010-08-11 17:04:31 +00003561#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003562 if (__first == __last)
3563 throw regex_error(regex_constants::error_brace);
Howard Hinnantd4444702010-08-11 17:04:31 +00003564#endif
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003565 switch (*__first)
3566 {
3567 case '}':
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003568 ++__first;
Howard Hinnanta0d045b2010-07-29 00:36:00 +00003569 if (__grammar == ECMAScript && __first != __last && *__first == '?')
Howard Hinnant17615b02010-07-27 01:25:38 +00003570 {
3571 ++__first;
3572 __push_loop(__min, __min, __s, __mexp_begin, __mexp_end, false);
3573 }
3574 else
3575 __push_loop(__min, __min, __s, __mexp_begin, __mexp_end);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003576 break;
3577 case ',':
Howard Hinnantd4444702010-08-11 17:04:31 +00003578 ++__first;
3579#ifndef _LIBCPP_NO_EXCEPTIONS
3580 if (__first == __last)
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003581 throw regex_error(regex_constants::error_badbrace);
Howard Hinnantd4444702010-08-11 17:04:31 +00003582#endif
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003583 if (*__first == '}')
3584 {
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003585 ++__first;
Howard Hinnanta0d045b2010-07-29 00:36:00 +00003586 if (__grammar == ECMAScript && __first != __last && *__first == '?')
Howard Hinnant17615b02010-07-27 01:25:38 +00003587 {
3588 ++__first;
3589 __push_nongreedy_inf_repeat(__min, __s, __mexp_begin, __mexp_end);
3590 }
3591 else
3592 __push_greedy_inf_repeat(__min, __s, __mexp_begin, __mexp_end);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003593 }
3594 else
3595 {
Howard Hinnantaa698082010-07-16 19:08:36 +00003596 int __max = -1;
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003597 __temp = __parse_DUP_COUNT(__first, __last, __max);
Howard Hinnantd4444702010-08-11 17:04:31 +00003598#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003599 if (__temp == __first)
3600 throw regex_error(regex_constants::error_brace);
Howard Hinnantd4444702010-08-11 17:04:31 +00003601#endif
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003602 __first = __temp;
Howard Hinnantd4444702010-08-11 17:04:31 +00003603#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003604 if (__first == __last || *__first != '}')
3605 throw regex_error(regex_constants::error_brace);
Howard Hinnantd4444702010-08-11 17:04:31 +00003606#endif
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003607 ++__first;
Howard Hinnantd4444702010-08-11 17:04:31 +00003608#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003609 if (__max < __min)
3610 throw regex_error(regex_constants::error_badbrace);
Howard Hinnantd4444702010-08-11 17:04:31 +00003611#endif
Howard Hinnanta0d045b2010-07-29 00:36:00 +00003612 if (__grammar == ECMAScript && __first != __last && *__first == '?')
Howard Hinnant17615b02010-07-27 01:25:38 +00003613 {
3614 ++__first;
3615 __push_loop(__min, __max, __s, __mexp_begin, __mexp_end, false);
3616 }
3617 else
3618 __push_loop(__min, __max, __s, __mexp_begin, __mexp_end);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003619 }
Howard Hinnantaa698082010-07-16 19:08:36 +00003620 break;
Howard Hinnantd4444702010-08-11 17:04:31 +00003621#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003622 default:
3623 throw regex_error(regex_constants::error_badbrace);
Howard Hinnantd4444702010-08-11 17:04:31 +00003624#endif
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003625 }
3626 }
3627 break;
3628 }
3629 }
3630 return __first;
3631}
3632
3633template <class _CharT, class _Traits>
3634template <class _ForwardIterator>
3635_ForwardIterator
Howard Hinnant0de86b62010-06-25 20:56:08 +00003636basic_regex<_CharT, _Traits>::__parse_bracket_expression(_ForwardIterator __first,
3637 _ForwardIterator __last)
3638{
3639 if (__first != __last && *__first == '[')
3640 {
Howard Hinnantd4444702010-08-11 17:04:31 +00003641 ++__first;
3642#ifndef _LIBCPP_NO_EXCEPTIONS
3643 if (__first == __last)
Howard Hinnant0de86b62010-06-25 20:56:08 +00003644 throw regex_error(regex_constants::error_brack);
Howard Hinnantd4444702010-08-11 17:04:31 +00003645#endif
Howard Hinnant173968a2010-07-13 21:48:06 +00003646 bool __negate = false;
Howard Hinnant0de86b62010-06-25 20:56:08 +00003647 if (*__first == '^')
3648 {
3649 ++__first;
Howard Hinnant173968a2010-07-13 21:48:06 +00003650 __negate = true;
Howard Hinnant0de86b62010-06-25 20:56:08 +00003651 }
Howard Hinnant173968a2010-07-13 21:48:06 +00003652 __bracket_expression<_CharT, _Traits>* __ml = __start_matching_list(__negate);
3653 // __ml owned by *this
Howard Hinnantd4444702010-08-11 17:04:31 +00003654#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0de86b62010-06-25 20:56:08 +00003655 if (__first == __last)
3656 throw regex_error(regex_constants::error_brack);
Howard Hinnantd4444702010-08-11 17:04:31 +00003657#endif
Howard Hinnant15476f32010-07-28 17:35:27 +00003658 if ((__flags_ & 0x1F0) != ECMAScript && *__first == ']')
Howard Hinnant0de86b62010-06-25 20:56:08 +00003659 {
Howard Hinnant173968a2010-07-13 21:48:06 +00003660 __ml->__add_char(']');
Howard Hinnant0de86b62010-06-25 20:56:08 +00003661 ++__first;
3662 }
Howard Hinnant173968a2010-07-13 21:48:06 +00003663 __first = __parse_follow_list(__first, __last, __ml);
Howard Hinnantd4444702010-08-11 17:04:31 +00003664#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0de86b62010-06-25 20:56:08 +00003665 if (__first == __last)
3666 throw regex_error(regex_constants::error_brack);
Howard Hinnantd4444702010-08-11 17:04:31 +00003667#endif
Howard Hinnant0de86b62010-06-25 20:56:08 +00003668 if (*__first == '-')
3669 {
Howard Hinnant173968a2010-07-13 21:48:06 +00003670 __ml->__add_char('-');
Howard Hinnant0de86b62010-06-25 20:56:08 +00003671 ++__first;
3672 }
Howard Hinnantd4444702010-08-11 17:04:31 +00003673#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0de86b62010-06-25 20:56:08 +00003674 if (__first == __last || *__first != ']')
3675 throw regex_error(regex_constants::error_brack);
Howard Hinnantd4444702010-08-11 17:04:31 +00003676#endif
Howard Hinnant0de86b62010-06-25 20:56:08 +00003677 ++__first;
3678 }
3679 return __first;
3680}
3681
3682template <class _CharT, class _Traits>
3683template <class _ForwardIterator>
3684_ForwardIterator
3685basic_regex<_CharT, _Traits>::__parse_follow_list(_ForwardIterator __first,
Howard Hinnant173968a2010-07-13 21:48:06 +00003686 _ForwardIterator __last,
3687 __bracket_expression<_CharT, _Traits>* __ml)
Howard Hinnant0de86b62010-06-25 20:56:08 +00003688{
3689 if (__first != __last)
3690 {
3691 while (true)
3692 {
Howard Hinnant173968a2010-07-13 21:48:06 +00003693 _ForwardIterator __temp = __parse_expression_term(__first, __last,
3694 __ml);
Howard Hinnant0de86b62010-06-25 20:56:08 +00003695 if (__temp == __first)
3696 break;
3697 __first = __temp;
3698 }
3699 }
3700 return __first;
3701}
3702
3703template <class _CharT, class _Traits>
3704template <class _ForwardIterator>
3705_ForwardIterator
3706basic_regex<_CharT, _Traits>::__parse_expression_term(_ForwardIterator __first,
Howard Hinnant173968a2010-07-13 21:48:06 +00003707 _ForwardIterator __last,
3708 __bracket_expression<_CharT, _Traits>* __ml)
Howard Hinnant0de86b62010-06-25 20:56:08 +00003709{
3710 if (__first != __last && *__first != ']')
3711 {
Howard Hinnant0de86b62010-06-25 20:56:08 +00003712 _ForwardIterator __temp = next(__first);
Howard Hinnant173968a2010-07-13 21:48:06 +00003713 basic_string<_CharT> __start_range;
Howard Hinnant0de86b62010-06-25 20:56:08 +00003714 if (__temp != __last && *__first == '[')
3715 {
3716 if (*__temp == '=')
Howard Hinnant173968a2010-07-13 21:48:06 +00003717 return __parse_equivalence_class(++__temp, __last, __ml);
Howard Hinnant0de86b62010-06-25 20:56:08 +00003718 else if (*__temp == ':')
Howard Hinnant173968a2010-07-13 21:48:06 +00003719 return __parse_character_class(++__temp, __last, __ml);
Howard Hinnant0de86b62010-06-25 20:56:08 +00003720 else if (*__temp == '.')
Howard Hinnant173968a2010-07-13 21:48:06 +00003721 __first = __parse_collating_symbol(++__temp, __last, __start_range);
Howard Hinnant0de86b62010-06-25 20:56:08 +00003722 }
Howard Hinnant15476f32010-07-28 17:35:27 +00003723 unsigned __grammar = __flags_ & 0x1F0;
3724 if (__start_range.empty())
Howard Hinnant0de86b62010-06-25 20:56:08 +00003725 {
Howard Hinnant15476f32010-07-28 17:35:27 +00003726 if ((__grammar == ECMAScript || __grammar == awk) && *__first == '\\')
3727 {
3728 if (__grammar == ECMAScript)
3729 __first = __parse_class_escape(++__first, __last, __start_range, __ml);
3730 else
3731 __first = __parse_awk_escape(++__first, __last, &__start_range);
3732 }
3733 else
3734 {
3735 __start_range = *__first;
3736 ++__first;
3737 }
Howard Hinnant0de86b62010-06-25 20:56:08 +00003738 }
3739 if (__first != __last && *__first != ']')
3740 {
3741 __temp = next(__first);
3742 if (__temp != __last && *__first == '-' && *__temp != ']')
3743 {
3744 // parse a range
Howard Hinnant173968a2010-07-13 21:48:06 +00003745 basic_string<_CharT> __end_range;
Howard Hinnant0de86b62010-06-25 20:56:08 +00003746 __first = __temp;
3747 ++__temp;
3748 if (__temp != __last && *__first == '[' && *__temp == '.')
Howard Hinnant173968a2010-07-13 21:48:06 +00003749 __first = __parse_collating_symbol(++__temp, __last, __end_range);
Howard Hinnant0de86b62010-06-25 20:56:08 +00003750 else
3751 {
Howard Hinnant15476f32010-07-28 17:35:27 +00003752 if ((__grammar == ECMAScript || __grammar == awk) && *__first == '\\')
3753 {
3754 if (__grammar == ECMAScript)
3755 __first = __parse_class_escape(++__first, __last,
3756 __end_range, __ml);
3757 else
3758 __first = __parse_awk_escape(++__first, __last,
3759 &__end_range);
3760 }
3761 else
3762 {
3763 __end_range = *__first;
3764 ++__first;
3765 }
Howard Hinnant0de86b62010-06-25 20:56:08 +00003766 }
Howard Hinnant173968a2010-07-13 21:48:06 +00003767 __ml->__add_range(_STD::move(__start_range), _STD::move(__end_range));
Howard Hinnant0de86b62010-06-25 20:56:08 +00003768 }
Howard Hinnant173968a2010-07-13 21:48:06 +00003769 else
3770 {
3771 if (__start_range.size() == 1)
3772 __ml->__add_char(__start_range[0]);
3773 else
3774 __ml->__add_digraph(__start_range[0], __start_range[1]);
3775 }
3776 }
3777 else
3778 {
3779 if (__start_range.size() == 1)
3780 __ml->__add_char(__start_range[0]);
3781 else
3782 __ml->__add_digraph(__start_range[0], __start_range[1]);
Howard Hinnant0de86b62010-06-25 20:56:08 +00003783 }
3784 }
3785 return __first;
3786}
3787
3788template <class _CharT, class _Traits>
3789template <class _ForwardIterator>
3790_ForwardIterator
Howard Hinnant15476f32010-07-28 17:35:27 +00003791basic_regex<_CharT, _Traits>::__parse_class_escape(_ForwardIterator __first,
3792 _ForwardIterator __last,
3793 basic_string<_CharT>& __str,
3794 __bracket_expression<_CharT, _Traits>* __ml)
3795{
Howard Hinnantd4444702010-08-11 17:04:31 +00003796#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant15476f32010-07-28 17:35:27 +00003797 if (__first == __last)
3798 throw regex_error(regex_constants::error_escape);
Howard Hinnantd4444702010-08-11 17:04:31 +00003799#endif
Howard Hinnant15476f32010-07-28 17:35:27 +00003800 switch (*__first)
3801 {
3802 case 0:
3803 __str = *__first;
3804 return ++__first;
3805 case 'b':
3806 __str = _CharT(8);
3807 return ++__first;
3808 case 'd':
3809 __ml->__add_class(ctype_base::digit);
3810 return ++__first;
3811 case 'D':
3812 __ml->__add_neg_class(ctype_base::digit);
3813 return ++__first;
3814 case 's':
3815 __ml->__add_class(ctype_base::space);
3816 return ++__first;
3817 case 'S':
3818 __ml->__add_neg_class(ctype_base::space);
3819 return ++__first;
3820 case 'w':
3821 __ml->__add_class(ctype_base::alnum);
3822 __ml->__add_char('_');
3823 return ++__first;
3824 case 'W':
3825 __ml->__add_neg_class(ctype_base::alnum);
3826 __ml->__add_neg_char('_');
3827 return ++__first;
3828 }
3829 __first = __parse_character_escape(__first, __last, &__str);
3830 return __first;
3831}
3832
3833template <class _CharT, class _Traits>
3834template <class _ForwardIterator>
3835_ForwardIterator
3836basic_regex<_CharT, _Traits>::__parse_awk_escape(_ForwardIterator __first,
3837 _ForwardIterator __last,
3838 basic_string<_CharT>* __str)
3839{
Howard Hinnantd4444702010-08-11 17:04:31 +00003840#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant15476f32010-07-28 17:35:27 +00003841 if (__first == __last)
3842 throw regex_error(regex_constants::error_escape);
Howard Hinnantd4444702010-08-11 17:04:31 +00003843#endif
Howard Hinnant15476f32010-07-28 17:35:27 +00003844 switch (*__first)
3845 {
3846 case '\\':
3847 case '"':
3848 case '/':
3849 if (__str)
3850 *__str = *__first;
3851 else
3852 __push_char(*__first);
3853 return ++__first;
3854 case 'a':
3855 if (__str)
3856 *__str = _CharT(7);
3857 else
3858 __push_char(_CharT(7));
3859 return ++__first;
3860 case 'b':
3861 if (__str)
3862 *__str = _CharT(8);
3863 else
3864 __push_char(_CharT(8));
3865 return ++__first;
3866 case 'f':
3867 if (__str)
3868 *__str = _CharT(0xC);
3869 else
3870 __push_char(_CharT(0xC));
3871 return ++__first;
3872 case 'n':
3873 if (__str)
3874 *__str = _CharT(0xA);
3875 else
3876 __push_char(_CharT(0xA));
3877 return ++__first;
3878 case 'r':
3879 if (__str)
3880 *__str = _CharT(0xD);
3881 else
3882 __push_char(_CharT(0xD));
3883 return ++__first;
3884 case 't':
3885 if (__str)
3886 *__str = _CharT(0x9);
3887 else
3888 __push_char(_CharT(0x9));
3889 return ++__first;
3890 case 'v':
3891 if (__str)
3892 *__str = _CharT(0xB);
3893 else
3894 __push_char(_CharT(0xB));
3895 return ++__first;
3896 }
3897 if ('0' <= *__first && *__first <= '7')
3898 {
3899 unsigned __val = *__first - '0';
3900 if (++__first != __last && ('0' <= *__first && *__first <= '7'))
3901 {
3902 __val = 8 * __val + *__first - '0';
3903 if (++__first != __last && ('0' <= *__first && *__first <= '7'))
3904 __val = 8 * __val + *__first - '0';
3905 }
3906 if (__str)
3907 *__str = _CharT(__val);
3908 else
3909 __push_char(_CharT(__val));
3910 }
Howard Hinnantd4444702010-08-11 17:04:31 +00003911#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant15476f32010-07-28 17:35:27 +00003912 else
3913 throw regex_error(regex_constants::error_escape);
Howard Hinnantd4444702010-08-11 17:04:31 +00003914#endif
Howard Hinnant15476f32010-07-28 17:35:27 +00003915 return __first;
3916}
3917
3918template <class _CharT, class _Traits>
3919template <class _ForwardIterator>
3920_ForwardIterator
Howard Hinnant0de86b62010-06-25 20:56:08 +00003921basic_regex<_CharT, _Traits>::__parse_equivalence_class(_ForwardIterator __first,
Howard Hinnant173968a2010-07-13 21:48:06 +00003922 _ForwardIterator __last,
3923 __bracket_expression<_CharT, _Traits>* __ml)
Howard Hinnant0de86b62010-06-25 20:56:08 +00003924{
3925 // Found [=
3926 // This means =] must exist
3927 value_type _Equal_close[2] = {'=', ']'};
3928 _ForwardIterator __temp = _STD::search(__first, __last, _Equal_close,
3929 _Equal_close+2);
Howard Hinnantd4444702010-08-11 17:04:31 +00003930#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0de86b62010-06-25 20:56:08 +00003931 if (__temp == __last)
3932 throw regex_error(regex_constants::error_brack);
Howard Hinnantd4444702010-08-11 17:04:31 +00003933#endif
Howard Hinnant0de86b62010-06-25 20:56:08 +00003934 // [__first, __temp) contains all text in [= ... =]
3935 typedef typename _Traits::string_type string_type;
3936 string_type __collate_name =
3937 __traits_.lookup_collatename(__first, __temp);
Howard Hinnantd4444702010-08-11 17:04:31 +00003938#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0de86b62010-06-25 20:56:08 +00003939 if (__collate_name.empty())
Howard Hinnant173968a2010-07-13 21:48:06 +00003940 throw regex_error(regex_constants::error_collate);
Howard Hinnantd4444702010-08-11 17:04:31 +00003941#endif
Howard Hinnant0de86b62010-06-25 20:56:08 +00003942 string_type __equiv_name =
3943 __traits_.transform_primary(__collate_name.begin(),
3944 __collate_name.end());
3945 if (!__equiv_name.empty())
Howard Hinnant173968a2010-07-13 21:48:06 +00003946 __ml->__add_equivalence(__equiv_name);
Howard Hinnant0de86b62010-06-25 20:56:08 +00003947 else
Howard Hinnant173968a2010-07-13 21:48:06 +00003948 {
3949 switch (__collate_name.size())
3950 {
3951 case 1:
3952 __ml->__add_char(__collate_name[0]);
3953 break;
3954 case 2:
3955 __ml->__add_digraph(__collate_name[0], __collate_name[1]);
3956 break;
Howard Hinnantd4444702010-08-11 17:04:31 +00003957#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant173968a2010-07-13 21:48:06 +00003958 default:
3959 throw regex_error(regex_constants::error_collate);
Howard Hinnantd4444702010-08-11 17:04:31 +00003960#endif
Howard Hinnant173968a2010-07-13 21:48:06 +00003961 }
3962 }
Howard Hinnant0de86b62010-06-25 20:56:08 +00003963 __first = next(__temp, 2);
3964 return __first;
3965}
3966
3967template <class _CharT, class _Traits>
3968template <class _ForwardIterator>
3969_ForwardIterator
3970basic_regex<_CharT, _Traits>::__parse_character_class(_ForwardIterator __first,
Howard Hinnant173968a2010-07-13 21:48:06 +00003971 _ForwardIterator __last,
3972 __bracket_expression<_CharT, _Traits>* __ml)
Howard Hinnant0de86b62010-06-25 20:56:08 +00003973{
3974 // Found [:
3975 // This means :] must exist
3976 value_type _Colon_close[2] = {':', ']'};
3977 _ForwardIterator __temp = _STD::search(__first, __last, _Colon_close,
3978 _Colon_close+2);
Howard Hinnantd4444702010-08-11 17:04:31 +00003979#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0de86b62010-06-25 20:56:08 +00003980 if (__temp == __last)
3981 throw regex_error(regex_constants::error_brack);
Howard Hinnantd4444702010-08-11 17:04:31 +00003982#endif
Howard Hinnant0de86b62010-06-25 20:56:08 +00003983 // [__first, __temp) contains all text in [: ... :]
3984 typedef typename _Traits::char_class_type char_class_type;
3985 char_class_type __class_type =
3986 __traits_.lookup_classname(__first, __temp, __flags_ & icase);
Howard Hinnantd4444702010-08-11 17:04:31 +00003987#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0de86b62010-06-25 20:56:08 +00003988 if (__class_type == 0)
3989 throw regex_error(regex_constants::error_brack);
Howard Hinnantd4444702010-08-11 17:04:31 +00003990#endif
Howard Hinnant173968a2010-07-13 21:48:06 +00003991 __ml->__add_class(__class_type);
Howard Hinnant0de86b62010-06-25 20:56:08 +00003992 __first = next(__temp, 2);
3993 return __first;
3994}
3995
3996template <class _CharT, class _Traits>
3997template <class _ForwardIterator>
3998_ForwardIterator
3999basic_regex<_CharT, _Traits>::__parse_collating_symbol(_ForwardIterator __first,
Howard Hinnant173968a2010-07-13 21:48:06 +00004000 _ForwardIterator __last,
4001 basic_string<_CharT>& __col_sym)
Howard Hinnant0de86b62010-06-25 20:56:08 +00004002{
4003 // Found [.
4004 // This means .] must exist
4005 value_type _Dot_close[2] = {'.', ']'};
4006 _ForwardIterator __temp = _STD::search(__first, __last, _Dot_close,
4007 _Dot_close+2);
Howard Hinnantd4444702010-08-11 17:04:31 +00004008#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0de86b62010-06-25 20:56:08 +00004009 if (__temp == __last)
4010 throw regex_error(regex_constants::error_brack);
Howard Hinnantd4444702010-08-11 17:04:31 +00004011#endif
Howard Hinnant0de86b62010-06-25 20:56:08 +00004012 // [__first, __temp) contains all text in [. ... .]
4013 typedef typename _Traits::string_type string_type;
Howard Hinnant173968a2010-07-13 21:48:06 +00004014 __col_sym = __traits_.lookup_collatename(__first, __temp);
4015 switch (__col_sym.size())
4016 {
4017 case 1:
4018 case 2:
4019 break;
Howard Hinnantd4444702010-08-11 17:04:31 +00004020#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant173968a2010-07-13 21:48:06 +00004021 default:
4022 throw regex_error(regex_constants::error_collate);
Howard Hinnantd4444702010-08-11 17:04:31 +00004023#endif
Howard Hinnant173968a2010-07-13 21:48:06 +00004024 }
Howard Hinnant0de86b62010-06-25 20:56:08 +00004025 __first = next(__temp, 2);
4026 return __first;
4027}
4028
4029template <class _CharT, class _Traits>
4030template <class _ForwardIterator>
4031_ForwardIterator
4032basic_regex<_CharT, _Traits>::__parse_DUP_COUNT(_ForwardIterator __first,
4033 _ForwardIterator __last,
4034 int& __c)
4035{
4036 if (__first != __last && '0' <= *__first && *__first <= '9')
4037 {
4038 __c = *__first - '0';
4039 for (++__first; __first != __last && '0' <= *__first && *__first <= '9';
4040 ++__first)
4041 {
4042 __c *= 10;
4043 __c += *__first - '0';
4044 }
4045 }
4046 return __first;
4047}
4048
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00004049template <class _CharT, class _Traits>
Howard Hinnant2ade7c22010-07-22 17:53:24 +00004050template <class _ForwardIterator>
4051_ForwardIterator
4052basic_regex<_CharT, _Traits>::__parse_ecma_exp(_ForwardIterator __first,
4053 _ForwardIterator __last)
4054{
4055 __owns_one_state<_CharT>* __sa = __end_;
4056 _ForwardIterator __temp = __parse_alternative(__first, __last);
4057 if (__temp == __first)
4058 __push_empty();
4059 __first = __temp;
4060 while (__first != __last && *__first == '|')
4061 {
4062 __owns_one_state<_CharT>* __sb = __end_;
4063 __temp = __parse_alternative(++__first, __last);
4064 if (__temp == __first)
4065 __push_empty();
4066 __push_alternation(__sa, __sb);
4067 __first = __temp;
4068 }
4069 return __first;
4070}
4071
4072template <class _CharT, class _Traits>
4073template <class _ForwardIterator>
4074_ForwardIterator
4075basic_regex<_CharT, _Traits>::__parse_alternative(_ForwardIterator __first,
4076 _ForwardIterator __last)
4077{
4078 while (true)
4079 {
4080 _ForwardIterator __temp = __parse_term(__first, __last);
4081 if (__temp == __first)
4082 break;
4083 __first = __temp;
4084 }
4085 return __first;
4086}
4087
4088template <class _CharT, class _Traits>
4089template <class _ForwardIterator>
4090_ForwardIterator
4091basic_regex<_CharT, _Traits>::__parse_term(_ForwardIterator __first,
4092 _ForwardIterator __last)
4093{
4094 _ForwardIterator __temp = __parse_assertion(__first, __last);
4095 if (__temp == __first)
4096 {
Howard Hinnant17615b02010-07-27 01:25:38 +00004097 __owns_one_state<_CharT>* __e = __end_;
4098 unsigned __mexp_begin = __marked_count_;
Howard Hinnant2ade7c22010-07-22 17:53:24 +00004099 __temp = __parse_atom(__first, __last);
4100 if (__temp != __first)
Howard Hinnant17615b02010-07-27 01:25:38 +00004101 __first = __parse_ERE_dupl_symbol(__temp, __last, __e,
4102 __mexp_begin+1, __marked_count_+1);
Howard Hinnant2ade7c22010-07-22 17:53:24 +00004103 }
Howard Hinnant17615b02010-07-27 01:25:38 +00004104 else
4105 __first = __temp;
Howard Hinnant2ade7c22010-07-22 17:53:24 +00004106 return __first;
4107}
4108
4109template <class _CharT, class _Traits>
4110template <class _ForwardIterator>
4111_ForwardIterator
4112basic_regex<_CharT, _Traits>::__parse_assertion(_ForwardIterator __first,
4113 _ForwardIterator __last)
4114{
4115 if (__first != __last)
4116 {
4117 switch (*__first)
4118 {
4119 case '^':
4120 __push_l_anchor();
4121 ++__first;
4122 break;
4123 case '$':
4124 __push_r_anchor();
4125 ++__first;
4126 break;
4127 case '\\':
4128 {
4129 _ForwardIterator __temp = _STD::next(__first);
4130 if (__temp != __last)
4131 {
4132 if (*__temp == 'b')
4133 {
Howard Hinnant17615b02010-07-27 01:25:38 +00004134 __push_word_boundary(false);
Howard Hinnant2ade7c22010-07-22 17:53:24 +00004135 __first = ++__temp;
4136 }
4137 else if (*__temp == 'B')
4138 {
Howard Hinnant17615b02010-07-27 01:25:38 +00004139 __push_word_boundary(true);
Howard Hinnant2ade7c22010-07-22 17:53:24 +00004140 __first = ++__temp;
4141 }
4142 }
4143 }
4144 break;
4145 case '(':
4146 {
4147 _ForwardIterator __temp = _STD::next(__first);
4148 if (__temp != __last && *__temp == '?')
4149 {
4150 if (++__temp != __last)
4151 {
4152 switch (*__temp)
4153 {
4154 case '=':
Howard Hinnante9de5ff2010-07-27 22:20:32 +00004155 {
4156 basic_regex __exp;
4157 __exp.__flags_ = __flags_;
4158 __temp = __exp.__parse(++__temp, __last);
4159 __exp.__push_l_anchor();
4160 __push_lookahead(_STD::move(__exp), false);
Howard Hinnantd4444702010-08-11 17:04:31 +00004161#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnante9de5ff2010-07-27 22:20:32 +00004162 if (__temp == __last || *__temp != ')')
4163 throw regex_error(regex_constants::error_paren);
Howard Hinnantd4444702010-08-11 17:04:31 +00004164#endif
Howard Hinnante9de5ff2010-07-27 22:20:32 +00004165 __first = ++__temp;
4166 }
Howard Hinnant2ade7c22010-07-22 17:53:24 +00004167 break;
4168 case '!':
Howard Hinnante9de5ff2010-07-27 22:20:32 +00004169 {
4170 basic_regex __exp;
4171 __exp.__flags_ = __flags_;
4172 __temp = __exp.__parse(++__temp, __last);
4173 __exp.__push_l_anchor();
4174 __push_lookahead(_STD::move(__exp), true);
Howard Hinnantd4444702010-08-11 17:04:31 +00004175#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnante9de5ff2010-07-27 22:20:32 +00004176 if (__temp == __last || *__temp != ')')
4177 throw regex_error(regex_constants::error_paren);
Howard Hinnantd4444702010-08-11 17:04:31 +00004178#endif
Howard Hinnante9de5ff2010-07-27 22:20:32 +00004179 __first = ++__temp;
4180 }
Howard Hinnant2ade7c22010-07-22 17:53:24 +00004181 break;
4182 }
4183 }
4184 }
4185 }
4186 break;
4187 }
4188 }
4189 return __first;
4190}
4191
4192template <class _CharT, class _Traits>
4193template <class _ForwardIterator>
4194_ForwardIterator
4195basic_regex<_CharT, _Traits>::__parse_atom(_ForwardIterator __first,
4196 _ForwardIterator __last)
4197{
Howard Hinnant17615b02010-07-27 01:25:38 +00004198 if (__first != __last)
4199 {
4200 switch (*__first)
4201 {
4202 case '.':
4203 __push_match_any_but_newline();
4204 ++__first;
4205 break;
4206 case '\\':
4207 __first = __parse_atom_escape(__first, __last);
4208 break;
4209 case '[':
4210 __first = __parse_bracket_expression(__first, __last);
4211 break;
4212 case '(':
4213 {
Howard Hinnantd4444702010-08-11 17:04:31 +00004214 ++__first;
4215#ifndef _LIBCPP_NO_EXCEPTIONS
4216 if (__first == __last)
Howard Hinnant17615b02010-07-27 01:25:38 +00004217 throw regex_error(regex_constants::error_paren);
Howard Hinnantd4444702010-08-11 17:04:31 +00004218#endif
Howard Hinnant17615b02010-07-27 01:25:38 +00004219 _ForwardIterator __temp = _STD::next(__first);
4220 if (__temp != __last && *__first == '?' && *__temp == ':')
4221 {
4222 ++__open_count_;
4223 __first = __parse_ecma_exp(++__temp, __last);
Howard Hinnantd4444702010-08-11 17:04:31 +00004224#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant17615b02010-07-27 01:25:38 +00004225 if (__first == __last || *__first != ')')
4226 throw regex_error(regex_constants::error_paren);
Howard Hinnantd4444702010-08-11 17:04:31 +00004227#endif
Howard Hinnant17615b02010-07-27 01:25:38 +00004228 --__open_count_;
4229 ++__first;
4230 }
4231 else
4232 {
4233 __push_begin_marked_subexpression();
4234 unsigned __temp_count = __marked_count_;
4235 ++__open_count_;
4236 __first = __parse_ecma_exp(__first, __last);
Howard Hinnantd4444702010-08-11 17:04:31 +00004237#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant17615b02010-07-27 01:25:38 +00004238 if (__first == __last || *__first != ')')
4239 throw regex_error(regex_constants::error_paren);
Howard Hinnantd4444702010-08-11 17:04:31 +00004240#endif
Howard Hinnant17615b02010-07-27 01:25:38 +00004241 __push_end_marked_subexpression(__temp_count);
4242 --__open_count_;
4243 ++__first;
4244 }
4245 }
4246 break;
4247 default:
4248 __first = __parse_pattern_character(__first, __last);
4249 break;
4250 }
4251 }
4252 return __first;
4253}
4254
4255template <class _CharT, class _Traits>
4256template <class _ForwardIterator>
4257_ForwardIterator
4258basic_regex<_CharT, _Traits>::__parse_atom_escape(_ForwardIterator __first,
4259 _ForwardIterator __last)
4260{
4261 if (__first != __last && *__first == '\\')
4262 {
4263 _ForwardIterator __t1 = _STD::next(__first);
4264 _ForwardIterator __t2 = __parse_decimal_escape(__t1, __last);
4265 if (__t2 != __t1)
4266 __first = __t2;
4267 else
4268 {
4269 __t2 = __parse_character_class_escape(__t1, __last);
4270 if (__t2 != __t1)
4271 __first = __t2;
4272 else
4273 {
4274 __t2 = __parse_character_escape(__t1, __last);
4275 if (__t2 != __t1)
4276 __first = __t2;
4277 }
4278 }
4279 }
4280 return __first;
4281}
4282
4283template <class _CharT, class _Traits>
4284template <class _ForwardIterator>
4285_ForwardIterator
4286basic_regex<_CharT, _Traits>::__parse_decimal_escape(_ForwardIterator __first,
4287 _ForwardIterator __last)
4288{
4289 if (__first != __last)
4290 {
4291 if (*__first == '0')
4292 {
4293 __push_char(_CharT());
4294 ++__first;
4295 }
4296 else if ('1' <= *__first && *__first <= '9')
4297 {
4298 unsigned __v = *__first - '0';
4299 for (++__first; '0' <= *__first && *__first <= '9'; ++__first)
4300 __v = 10 * __v + *__first - '0';
Howard Hinnantd4444702010-08-11 17:04:31 +00004301#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant17615b02010-07-27 01:25:38 +00004302 if (__v > mark_count())
4303 throw regex_error(regex_constants::error_backref);
Howard Hinnantd4444702010-08-11 17:04:31 +00004304#endif
Howard Hinnant17615b02010-07-27 01:25:38 +00004305 __push_back_ref(__v);
4306 }
4307 }
4308 return __first;
4309}
4310
4311template <class _CharT, class _Traits>
4312template <class _ForwardIterator>
4313_ForwardIterator
4314basic_regex<_CharT, _Traits>::__parse_character_class_escape(_ForwardIterator __first,
4315 _ForwardIterator __last)
4316{
4317 if (__first != __last)
4318 {
4319 __bracket_expression<_CharT, _Traits>* __ml;
4320 switch (*__first)
4321 {
4322 case 'd':
4323 __ml = __start_matching_list(false);
4324 __ml->__add_class(ctype_base::digit);
4325 ++__first;
4326 break;
4327 case 'D':
4328 __ml = __start_matching_list(true);
4329 __ml->__add_class(ctype_base::digit);
4330 ++__first;
4331 break;
4332 case 's':
4333 __ml = __start_matching_list(false);
4334 __ml->__add_class(ctype_base::space);
4335 ++__first;
4336 break;
4337 case 'S':
4338 __ml = __start_matching_list(true);
4339 __ml->__add_class(ctype_base::space);
4340 ++__first;
4341 break;
4342 case 'w':
4343 __ml = __start_matching_list(false);
4344 __ml->__add_class(ctype_base::alnum);
4345 __ml->__add_char('_');
4346 ++__first;
4347 break;
4348 case 'W':
4349 __ml = __start_matching_list(true);
4350 __ml->__add_class(ctype_base::alnum);
4351 __ml->__add_char('_');
4352 ++__first;
4353 break;
4354 }
4355 }
4356 return __first;
4357}
4358
4359template <class _CharT, class _Traits>
4360template <class _ForwardIterator>
4361_ForwardIterator
4362basic_regex<_CharT, _Traits>::__parse_character_escape(_ForwardIterator __first,
Howard Hinnant15476f32010-07-28 17:35:27 +00004363 _ForwardIterator __last,
4364 basic_string<_CharT>* __str)
Howard Hinnant17615b02010-07-27 01:25:38 +00004365{
4366 if (__first != __last)
4367 {
4368 _ForwardIterator __t;
4369 unsigned __sum = 0;
4370 int __hd;
4371 switch (*__first)
4372 {
4373 case 'f':
Howard Hinnant15476f32010-07-28 17:35:27 +00004374 if (__str)
4375 *__str = _CharT(0xC);
4376 else
4377 __push_char(_CharT(0xC));
Howard Hinnant17615b02010-07-27 01:25:38 +00004378 ++__first;
4379 break;
4380 case 'n':
Howard Hinnant15476f32010-07-28 17:35:27 +00004381 if (__str)
4382 *__str = _CharT(0xA);
4383 else
4384 __push_char(_CharT(0xA));
Howard Hinnant17615b02010-07-27 01:25:38 +00004385 ++__first;
4386 break;
4387 case 'r':
Howard Hinnant15476f32010-07-28 17:35:27 +00004388 if (__str)
4389 *__str = _CharT(0xD);
4390 else
4391 __push_char(_CharT(0xD));
Howard Hinnant17615b02010-07-27 01:25:38 +00004392 ++__first;
4393 break;
4394 case 't':
Howard Hinnant15476f32010-07-28 17:35:27 +00004395 if (__str)
4396 *__str = _CharT(0x9);
4397 else
4398 __push_char(_CharT(0x9));
Howard Hinnant17615b02010-07-27 01:25:38 +00004399 ++__first;
4400 break;
4401 case 'v':
Howard Hinnant15476f32010-07-28 17:35:27 +00004402 if (__str)
4403 *__str = _CharT(0xB);
4404 else
4405 __push_char(_CharT(0xB));
Howard Hinnant17615b02010-07-27 01:25:38 +00004406 ++__first;
4407 break;
4408 case 'c':
4409 if ((__t = _STD::next(__first)) != __last)
4410 {
4411 if ('A' <= *__t <= 'Z' || 'a' <= *__t <= 'z')
4412 {
Howard Hinnant15476f32010-07-28 17:35:27 +00004413 if (__str)
4414 *__str = _CharT(*__t % 32);
4415 else
4416 __push_char(_CharT(*__t % 32));
Howard Hinnant17615b02010-07-27 01:25:38 +00004417 __first = ++__t;
4418 }
4419 }
4420 break;
4421 case 'u':
Howard Hinnantd4444702010-08-11 17:04:31 +00004422 ++__first;
4423#ifndef _LIBCPP_NO_EXCEPTIONS
4424 if (__first == __last)
Howard Hinnant17615b02010-07-27 01:25:38 +00004425 throw regex_error(regex_constants::error_escape);
Howard Hinnantd4444702010-08-11 17:04:31 +00004426#endif
Howard Hinnant17615b02010-07-27 01:25:38 +00004427 __hd = __traits_.value(*__first, 16);
Howard Hinnantd4444702010-08-11 17:04:31 +00004428#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant17615b02010-07-27 01:25:38 +00004429 if (__hd == -1)
4430 throw regex_error(regex_constants::error_escape);
Howard Hinnantd4444702010-08-11 17:04:31 +00004431#endif
Howard Hinnant17615b02010-07-27 01:25:38 +00004432 __sum = 16 * __sum + __hd;
Howard Hinnantd4444702010-08-11 17:04:31 +00004433 ++__first;
4434#ifndef _LIBCPP_NO_EXCEPTIONS
4435 if (__first == __last)
Howard Hinnant17615b02010-07-27 01:25:38 +00004436 throw regex_error(regex_constants::error_escape);
Howard Hinnantd4444702010-08-11 17:04:31 +00004437#endif
Howard Hinnant17615b02010-07-27 01:25:38 +00004438 __hd = __traits_.value(*__first, 16);
Howard Hinnantd4444702010-08-11 17:04:31 +00004439#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant17615b02010-07-27 01:25:38 +00004440 if (__hd == -1)
4441 throw regex_error(regex_constants::error_escape);
Howard Hinnantd4444702010-08-11 17:04:31 +00004442#endif
Howard Hinnant17615b02010-07-27 01:25:38 +00004443 __sum = 16 * __sum + __hd;
4444 // drop through
4445 case 'x':
Howard Hinnantd4444702010-08-11 17:04:31 +00004446 ++__first;
4447#ifndef _LIBCPP_NO_EXCEPTIONS
4448 if (__first == __last)
Howard Hinnant17615b02010-07-27 01:25:38 +00004449 throw regex_error(regex_constants::error_escape);
Howard Hinnantd4444702010-08-11 17:04:31 +00004450#endif
Howard Hinnant17615b02010-07-27 01:25:38 +00004451 __hd = __traits_.value(*__first, 16);
Howard Hinnantd4444702010-08-11 17:04:31 +00004452#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant17615b02010-07-27 01:25:38 +00004453 if (__hd == -1)
4454 throw regex_error(regex_constants::error_escape);
Howard Hinnantd4444702010-08-11 17:04:31 +00004455#endif
Howard Hinnant17615b02010-07-27 01:25:38 +00004456 __sum = 16 * __sum + __hd;
Howard Hinnantd4444702010-08-11 17:04:31 +00004457 ++__first;
4458#ifndef _LIBCPP_NO_EXCEPTIONS
4459 if (__first == __last)
Howard Hinnant17615b02010-07-27 01:25:38 +00004460 throw regex_error(regex_constants::error_escape);
Howard Hinnantd4444702010-08-11 17:04:31 +00004461#endif
Howard Hinnant17615b02010-07-27 01:25:38 +00004462 __hd = __traits_.value(*__first, 16);
Howard Hinnantd4444702010-08-11 17:04:31 +00004463#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant17615b02010-07-27 01:25:38 +00004464 if (__hd == -1)
4465 throw regex_error(regex_constants::error_escape);
Howard Hinnantd4444702010-08-11 17:04:31 +00004466#endif
Howard Hinnant17615b02010-07-27 01:25:38 +00004467 __sum = 16 * __sum + __hd;
Howard Hinnant15476f32010-07-28 17:35:27 +00004468 if (__str)
4469 *__str = _CharT(__sum);
4470 else
4471 __push_char(_CharT(__sum));
Howard Hinnant17615b02010-07-27 01:25:38 +00004472 ++__first;
4473 break;
4474 default:
4475 if (*__first != '_' && !__traits_.isctype(*__first, ctype_base::alnum))
4476 {
Howard Hinnant15476f32010-07-28 17:35:27 +00004477 if (__str)
4478 *__str = *__first;
4479 else
4480 __push_char(*__first);
Howard Hinnant17615b02010-07-27 01:25:38 +00004481 ++__first;
4482 }
Howard Hinnantd4444702010-08-11 17:04:31 +00004483#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant15476f32010-07-28 17:35:27 +00004484 else if (__str)
4485 throw regex_error(regex_constants::error_escape);
Howard Hinnantd4444702010-08-11 17:04:31 +00004486#endif
Howard Hinnant17615b02010-07-27 01:25:38 +00004487 break;
4488 }
4489 }
4490 return __first;
4491}
4492
4493template <class _CharT, class _Traits>
4494template <class _ForwardIterator>
4495_ForwardIterator
4496basic_regex<_CharT, _Traits>::__parse_pattern_character(_ForwardIterator __first,
4497 _ForwardIterator __last)
4498{
4499 if (__first != __last)
4500 {
4501 switch (*__first)
4502 {
4503 case '^':
4504 case '$':
4505 case '\\':
4506 case '.':
4507 case '*':
4508 case '+':
4509 case '?':
4510 case '(':
4511 case ')':
4512 case '[':
4513 case ']':
4514 case '{':
4515 case '}':
4516 case '|':
4517 break;
4518 default:
4519 __push_char(*__first);
4520 ++__first;
4521 break;
4522 }
4523 }
4524 return __first;
Howard Hinnant2ade7c22010-07-22 17:53:24 +00004525}
4526
4527template <class _CharT, class _Traits>
Howard Hinnant856846b2010-07-27 19:53:10 +00004528template <class _ForwardIterator>
4529_ForwardIterator
4530basic_regex<_CharT, _Traits>::__parse_grep(_ForwardIterator __first,
4531 _ForwardIterator __last)
4532{
4533 __owns_one_state<_CharT>* __sa = __end_;
4534 _ForwardIterator __t1 = _STD::find(__first, __last, _CharT('\n'));
4535 if (__t1 != __first)
4536 __parse_basic_reg_exp(__first, __t1);
4537 else
4538 __push_empty();
4539 __first = __t1;
4540 if (__first != __last)
4541 ++__first;
4542 while (__first != __last)
4543 {
4544 __t1 = _STD::find(__first, __last, _CharT('\n'));
4545 __owns_one_state<_CharT>* __sb = __end_;
4546 if (__t1 != __first)
4547 __parse_basic_reg_exp(__first, __t1);
4548 else
4549 __push_empty();
4550 __push_alternation(__sa, __sb);
4551 __first = __t1;
4552 if (__first != __last)
4553 ++__first;
4554 }
4555 return __first;
4556}
4557
4558template <class _CharT, class _Traits>
4559template <class _ForwardIterator>
4560_ForwardIterator
4561basic_regex<_CharT, _Traits>::__parse_egrep(_ForwardIterator __first,
4562 _ForwardIterator __last)
4563{
4564 __owns_one_state<_CharT>* __sa = __end_;
4565 _ForwardIterator __t1 = _STD::find(__first, __last, _CharT('\n'));
4566 if (__t1 != __first)
4567 __parse_extended_reg_exp(__first, __t1);
4568 else
4569 __push_empty();
4570 __first = __t1;
4571 if (__first != __last)
4572 ++__first;
4573 while (__first != __last)
4574 {
4575 __t1 = _STD::find(__first, __last, _CharT('\n'));
4576 __owns_one_state<_CharT>* __sb = __end_;
4577 if (__t1 != __first)
4578 __parse_extended_reg_exp(__first, __t1);
4579 else
4580 __push_empty();
4581 __push_alternation(__sa, __sb);
4582 __first = __t1;
4583 if (__first != __last)
4584 ++__first;
4585 }
4586 return __first;
4587}
4588
4589template <class _CharT, class _Traits>
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00004590void
Howard Hinnantf8ce4592010-07-07 19:14:52 +00004591basic_regex<_CharT, _Traits>::__push_loop(size_t __min, size_t __max,
4592 __owns_one_state<_CharT>* __s, size_t __mexp_begin, size_t __mexp_end,
4593 bool __greedy)
4594{
4595 unique_ptr<__empty_state<_CharT> > __e1(new __empty_state<_CharT>(__end_->first()));
4596 __end_->first() = nullptr;
Howard Hinnantac303862010-07-12 15:51:17 +00004597 unique_ptr<__loop<_CharT> > __e2(new __loop<_CharT>(__loop_count_,
4598 __s->first(), __e1.get(), __mexp_begin, __mexp_end, __greedy,
4599 __min, __max));
4600 __s->first() = nullptr;
4601 __e1.release();
4602 __end_->first() = new __repeat_one_loop<_CharT>(__e2.get());
Howard Hinnantf8ce4592010-07-07 19:14:52 +00004603 __end_ = __e2->second();
Howard Hinnantac303862010-07-12 15:51:17 +00004604 __s->first() = __e2.release();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00004605 ++__loop_count_;
4606}
4607
4608template <class _CharT, class _Traits>
4609void
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00004610basic_regex<_CharT, _Traits>::__push_char(value_type __c)
4611{
Howard Hinnant173968a2010-07-13 21:48:06 +00004612 if (flags() & icase)
Howard Hinnante34f17d2010-07-12 19:11:27 +00004613 __end_->first() = new __match_char_icase<_CharT, _Traits>
4614 (__traits_, __c, __end_->first());
Howard Hinnant173968a2010-07-13 21:48:06 +00004615 else if (flags() & collate)
Howard Hinnante34f17d2010-07-12 19:11:27 +00004616 __end_->first() = new __match_char_collate<_CharT, _Traits>
4617 (__traits_, __c, __end_->first());
4618 else
4619 __end_->first() = new __match_char<_CharT>(__c, __end_->first());
Howard Hinnante77aa5e2010-07-08 17:43:58 +00004620 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00004621}
4622
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00004623template <class _CharT, class _Traits>
4624void
4625basic_regex<_CharT, _Traits>::__push_begin_marked_subexpression()
4626{
Howard Hinnant68025ed2010-07-14 15:45:11 +00004627 if (!(__flags_ & nosubs))
4628 {
4629 __end_->first() =
4630 new __begin_marked_subexpression<_CharT>(++__marked_count_,
4631 __end_->first());
4632 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4633 }
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00004634}
4635
4636template <class _CharT, class _Traits>
4637void
4638basic_regex<_CharT, _Traits>::__push_end_marked_subexpression(unsigned __sub)
4639{
Howard Hinnant68025ed2010-07-14 15:45:11 +00004640 if (!(__flags_ & nosubs))
4641 {
4642 __end_->first() =
4643 new __end_marked_subexpression<_CharT>(__sub, __end_->first());
4644 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4645 }
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00004646}
4647
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00004648template <class _CharT, class _Traits>
4649void
4650basic_regex<_CharT, _Traits>::__push_r_anchor()
4651{
4652 __end_->first() = new __r_anchor<_CharT>(__end_->first());
4653 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4654}
4655
Howard Hinnantac303862010-07-12 15:51:17 +00004656template <class _CharT, class _Traits>
4657void
4658basic_regex<_CharT, _Traits>::__push_match_any()
4659{
4660 __end_->first() = new __match_any<_CharT>(__end_->first());
4661 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4662}
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00004663
Howard Hinnantcba352d2010-07-12 18:16:05 +00004664template <class _CharT, class _Traits>
4665void
Howard Hinnant17615b02010-07-27 01:25:38 +00004666basic_regex<_CharT, _Traits>::__push_match_any_but_newline()
4667{
4668 __end_->first() = new __match_any_but_newline<_CharT>(__end_->first());
4669 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4670}
4671
4672template <class _CharT, class _Traits>
4673void
Howard Hinnant2ade7c22010-07-22 17:53:24 +00004674basic_regex<_CharT, _Traits>::__push_empty()
4675{
4676 __end_->first() = new __empty_state<_CharT>(__end_->first());
4677 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4678}
4679
4680template <class _CharT, class _Traits>
4681void
Howard Hinnant17615b02010-07-27 01:25:38 +00004682basic_regex<_CharT, _Traits>::__push_word_boundary(bool __invert)
4683{
4684 __end_->first() = new __word_boundary<_CharT, _Traits>(__traits_, __invert,
4685 __end_->first());
4686 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4687}
4688
4689template <class _CharT, class _Traits>
4690void
Howard Hinnantcba352d2010-07-12 18:16:05 +00004691basic_regex<_CharT, _Traits>::__push_back_ref(int __i)
4692{
Howard Hinnant173968a2010-07-13 21:48:06 +00004693 if (flags() & icase)
Howard Hinnante34f17d2010-07-12 19:11:27 +00004694 __end_->first() = new __back_ref_icase<_CharT, _Traits>
4695 (__traits_, __i, __end_->first());
Howard Hinnant173968a2010-07-13 21:48:06 +00004696 else if (flags() & collate)
Howard Hinnante34f17d2010-07-12 19:11:27 +00004697 __end_->first() = new __back_ref_collate<_CharT, _Traits>
4698 (__traits_, __i, __end_->first());
4699 else
4700 __end_->first() = new __back_ref<_CharT>(__i, __end_->first());
Howard Hinnantcba352d2010-07-12 18:16:05 +00004701 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4702}
4703
Howard Hinnant173968a2010-07-13 21:48:06 +00004704template <class _CharT, class _Traits>
Howard Hinnantaa698082010-07-16 19:08:36 +00004705void
4706basic_regex<_CharT, _Traits>::__push_alternation(__owns_one_state<_CharT>* __sa,
4707 __owns_one_state<_CharT>* __ea)
4708{
4709 __sa->first() = new __alternate<_CharT>(
4710 static_cast<__owns_one_state<_CharT>*>(__sa->first()),
4711 static_cast<__owns_one_state<_CharT>*>(__ea->first()));
4712 __ea->first() = nullptr;
4713 __ea->first() = new __empty_state<_CharT>(__end_->first());
4714 __end_->first() = nullptr;
4715 __end_->first() = new __empty_non_own_state<_CharT>(__ea->first());
4716 __end_ = static_cast<__owns_one_state<_CharT>*>(__ea->first());
4717}
4718
4719template <class _CharT, class _Traits>
Howard Hinnant173968a2010-07-13 21:48:06 +00004720__bracket_expression<_CharT, _Traits>*
4721basic_regex<_CharT, _Traits>::__start_matching_list(bool __negate)
4722{
4723 __bracket_expression<_CharT, _Traits>* __r =
4724 new __bracket_expression<_CharT, _Traits>(__traits_, __end_->first(),
4725 __negate, __flags_ & icase,
4726 __flags_ & collate);
4727 __end_->first() = __r;
4728 __end_ = __r;
4729 return __r;
4730}
4731
Howard Hinnante9de5ff2010-07-27 22:20:32 +00004732template <class _CharT, class _Traits>
4733void
4734basic_regex<_CharT, _Traits>::__push_lookahead(const basic_regex& __exp,
4735 bool __invert)
4736{
4737 __end_->first() = new __lookahead<_CharT, _Traits>(__exp, __invert,
4738 __end_->first());
4739 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4740}
4741
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00004742typedef basic_regex<char> regex;
4743typedef basic_regex<wchar_t> wregex;
4744
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00004745// sub_match
4746
4747template <class _BidirectionalIterator>
4748class sub_match
4749 : public pair<_BidirectionalIterator, _BidirectionalIterator>
4750{
4751public:
4752 typedef _BidirectionalIterator iterator;
4753 typedef typename iterator_traits<iterator>::value_type value_type;
4754 typedef typename iterator_traits<iterator>::difference_type difference_type;
4755 typedef basic_string<value_type> string_type;
4756
4757 bool matched;
4758
4759 difference_type length() const
4760 {return matched ? _STD::distance(this->first, this->second) : 0;}
4761 string_type str() const
4762 {return matched ? string_type(this->first, this->second) : string_type();}
4763 operator string_type() const
4764 {return str();}
4765
4766 int compare(const sub_match& __s) const
4767 {return str().compare(__s.str());}
4768 int compare(const string_type& __s) const
4769 {return str().compare(__s);}
4770 int compare(const value_type* __s) const
4771 {return str().compare(__s);}
4772};
4773
4774typedef sub_match<const char*> csub_match;
4775typedef sub_match<const wchar_t*> wcsub_match;
4776typedef sub_match<string::const_iterator> ssub_match;
4777typedef sub_match<wstring::const_iterator> wssub_match;
4778
4779template <class _BiIter>
4780inline _LIBCPP_INLINE_VISIBILITY
4781bool
4782operator==(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
4783{
4784 return __x.compare(__y) == 0;
4785}
4786
4787template <class _BiIter>
4788inline _LIBCPP_INLINE_VISIBILITY
4789bool
4790operator!=(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
4791{
4792 return !(__x == __y);
4793}
4794
4795template <class _BiIter>
4796inline _LIBCPP_INLINE_VISIBILITY
4797bool
4798operator<(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
4799{
4800 return __x.compare(__y) < 0;
4801}
4802
4803template <class _BiIter>
4804inline _LIBCPP_INLINE_VISIBILITY
4805bool
4806operator<=(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
4807{
4808 return !(__y < __x);
4809}
4810
4811template <class _BiIter>
4812inline _LIBCPP_INLINE_VISIBILITY
4813bool
4814operator>=(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
4815{
4816 return !(__x < __y);
4817}
4818
4819template <class _BiIter>
4820inline _LIBCPP_INLINE_VISIBILITY
4821bool
4822operator>(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
4823{
4824 return __y < __x;
4825}
4826
4827template <class _BiIter, class _ST, class _SA>
4828inline _LIBCPP_INLINE_VISIBILITY
4829bool
4830operator==(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4831 const sub_match<_BiIter>& __y)
4832{
4833 return __y.compare(__x.c_str()) == 0;
4834}
4835
4836template <class _BiIter, class _ST, class _SA>
4837inline _LIBCPP_INLINE_VISIBILITY
4838bool
4839operator!=(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4840 const sub_match<_BiIter>& __y)
4841{
4842 return !(__x == __y);
4843}
4844
4845template <class _BiIter, class _ST, class _SA>
4846inline _LIBCPP_INLINE_VISIBILITY
4847bool
4848operator<(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4849 const sub_match<_BiIter>& __y)
4850{
4851 return __y.compare(__x.c_str()) > 0;
4852}
4853
4854template <class _BiIter, class _ST, class _SA>
4855inline _LIBCPP_INLINE_VISIBILITY
4856bool
4857operator>(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4858 const sub_match<_BiIter>& __y)
4859{
4860 return __y < __x;
4861}
4862
4863template <class _BiIter, class _ST, class _SA>
4864inline _LIBCPP_INLINE_VISIBILITY
4865bool operator>=(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4866 const sub_match<_BiIter>& __y)
4867{
4868 return !(__x < __y);
4869}
4870
4871template <class _BiIter, class _ST, class _SA>
4872inline _LIBCPP_INLINE_VISIBILITY
4873bool
4874operator<=(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4875 const sub_match<_BiIter>& __y)
4876{
4877 return !(__y < __x);
4878}
4879
4880template <class _BiIter, class _ST, class _SA>
4881inline _LIBCPP_INLINE_VISIBILITY
4882bool
4883operator==(const sub_match<_BiIter>& __x,
4884 const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
4885{
4886 return __x.compare(__y.c_str()) == 0;
4887}
4888
4889template <class _BiIter, class _ST, class _SA>
4890inline _LIBCPP_INLINE_VISIBILITY
4891bool
4892operator!=(const sub_match<_BiIter>& __x,
4893 const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
4894{
4895 return !(__x == __y);
4896}
4897
4898template <class _BiIter, class _ST, class _SA>
4899inline _LIBCPP_INLINE_VISIBILITY
4900bool
4901operator<(const sub_match<_BiIter>& __x,
4902 const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
4903{
4904 return __x.compare(__y.c_str()) < 0;
4905}
4906
4907template <class _BiIter, class _ST, class _SA>
4908inline _LIBCPP_INLINE_VISIBILITY
4909bool operator>(const sub_match<_BiIter>& __x,
4910 const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
4911{
4912 return __y < __x;
4913}
4914
4915template <class _BiIter, class _ST, class _SA>
4916inline _LIBCPP_INLINE_VISIBILITY
4917bool
4918operator>=(const sub_match<_BiIter>& __x,
4919 const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
4920{
4921 return !(__x < __y);
4922}
4923
4924template <class _BiIter, class _ST, class _SA>
4925inline _LIBCPP_INLINE_VISIBILITY
4926bool
4927operator<=(const sub_match<_BiIter>& __x,
4928 const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
4929{
4930 return !(__y < __x);
4931}
4932
4933template <class _BiIter>
4934inline _LIBCPP_INLINE_VISIBILITY
4935bool
4936operator==(typename iterator_traits<_BiIter>::value_type const* __x,
4937 const sub_match<_BiIter>& __y)
4938{
4939 return __y.compare(__x) == 0;
4940}
4941
4942template <class _BiIter>
4943inline _LIBCPP_INLINE_VISIBILITY
4944bool
4945operator!=(typename iterator_traits<_BiIter>::value_type const* __x,
4946 const sub_match<_BiIter>& __y)
4947{
4948 return !(__x == __y);
4949}
4950
4951template <class _BiIter>
4952inline _LIBCPP_INLINE_VISIBILITY
4953bool
4954operator<(typename iterator_traits<_BiIter>::value_type const* __x,
4955 const sub_match<_BiIter>& __y)
4956{
4957 return __y.compare(__x) > 0;
4958}
4959
4960template <class _BiIter>
4961inline _LIBCPP_INLINE_VISIBILITY
4962bool
4963operator>(typename iterator_traits<_BiIter>::value_type const* __x,
4964 const sub_match<_BiIter>& __y)
4965{
4966 return __y < __x;
4967}
4968
4969template <class _BiIter>
4970inline _LIBCPP_INLINE_VISIBILITY
4971bool
4972operator>=(typename iterator_traits<_BiIter>::value_type const* __x,
4973 const sub_match<_BiIter>& __y)
4974{
4975 return !(__x < __y);
4976}
4977
4978template <class _BiIter>
4979inline _LIBCPP_INLINE_VISIBILITY
4980bool
4981operator<=(typename iterator_traits<_BiIter>::value_type const* __x,
4982 const sub_match<_BiIter>& __y)
4983{
4984 return !(__y < __x);
4985}
4986
4987template <class _BiIter>
4988inline _LIBCPP_INLINE_VISIBILITY
4989bool
4990operator==(const sub_match<_BiIter>& __x,
4991 typename iterator_traits<_BiIter>::value_type const* __y)
4992{
4993 return __x.compare(__y) == 0;
4994}
4995
4996template <class _BiIter>
4997inline _LIBCPP_INLINE_VISIBILITY
4998bool
4999operator!=(const sub_match<_BiIter>& __x,
5000 typename iterator_traits<_BiIter>::value_type const* __y)
5001{
5002 return !(__x == __y);
5003}
5004
5005template <class _BiIter>
5006inline _LIBCPP_INLINE_VISIBILITY
5007bool
5008operator<(const sub_match<_BiIter>& __x,
5009 typename iterator_traits<_BiIter>::value_type const* __y)
5010{
5011 return __x.compare(__y) < 0;
5012}
5013
5014template <class _BiIter>
5015inline _LIBCPP_INLINE_VISIBILITY
5016bool
5017operator>(const sub_match<_BiIter>& __x,
5018 typename iterator_traits<_BiIter>::value_type const* __y)
5019{
5020 return __y < __x;
5021}
5022
5023template <class _BiIter>
5024inline _LIBCPP_INLINE_VISIBILITY
5025bool
5026operator>=(const sub_match<_BiIter>& __x,
5027 typename iterator_traits<_BiIter>::value_type const* __y)
5028{
5029 return !(__x < __y);
5030}
5031
5032template <class _BiIter>
5033inline _LIBCPP_INLINE_VISIBILITY
5034bool
5035operator<=(const sub_match<_BiIter>& __x,
5036 typename iterator_traits<_BiIter>::value_type const* __y)
5037{
5038 return !(__y < __x);
5039}
5040
5041template <class _BiIter>
5042inline _LIBCPP_INLINE_VISIBILITY
5043bool
5044operator==(typename iterator_traits<_BiIter>::value_type const& __x,
5045 const sub_match<_BiIter>& __y)
5046{
5047 typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
5048 return __y.compare(string_type(1, __x)) == 0;
5049}
5050
5051template <class _BiIter>
5052inline _LIBCPP_INLINE_VISIBILITY
5053bool
5054operator!=(typename iterator_traits<_BiIter>::value_type const& __x,
5055 const sub_match<_BiIter>& __y)
5056{
5057 return !(__x == __y);
5058}
5059
5060template <class _BiIter>
5061inline _LIBCPP_INLINE_VISIBILITY
5062bool
5063operator<(typename iterator_traits<_BiIter>::value_type const& __x,
5064 const sub_match<_BiIter>& __y)
5065{
5066 typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
5067 return __y.compare(string_type(1, __x)) > 0;
5068}
5069
5070template <class _BiIter>
5071inline _LIBCPP_INLINE_VISIBILITY
5072bool
5073operator>(typename iterator_traits<_BiIter>::value_type const& __x,
5074 const sub_match<_BiIter>& __y)
5075{
5076 return __y < __x;
5077}
5078
5079template <class _BiIter>
5080inline _LIBCPP_INLINE_VISIBILITY
5081bool
5082operator>=(typename iterator_traits<_BiIter>::value_type const& __x,
5083 const sub_match<_BiIter>& __y)
5084{
5085 return !(__x < __y);
5086}
5087
5088template <class _BiIter>
5089inline _LIBCPP_INLINE_VISIBILITY
5090bool
5091operator<=(typename iterator_traits<_BiIter>::value_type const& __x,
5092 const sub_match<_BiIter>& __y)
5093{
5094 return !(__y < __x);
5095}
5096
5097template <class _BiIter>
5098inline _LIBCPP_INLINE_VISIBILITY
5099bool
5100operator==(const sub_match<_BiIter>& __x,
5101 typename iterator_traits<_BiIter>::value_type const& __y)
5102{
5103 typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
5104 return __x.compare(string_type(1, __y)) == 0;
5105}
5106
5107template <class _BiIter>
5108inline _LIBCPP_INLINE_VISIBILITY
5109bool
5110operator!=(const sub_match<_BiIter>& __x,
5111 typename iterator_traits<_BiIter>::value_type const& __y)
5112{
5113 return !(__x == __y);
5114}
5115
5116template <class _BiIter>
5117inline _LIBCPP_INLINE_VISIBILITY
5118bool
5119operator<(const sub_match<_BiIter>& __x,
5120 typename iterator_traits<_BiIter>::value_type const& __y)
5121{
5122 typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
5123 return __x.compare(string_type(1, __y)) < 0;
5124}
5125
5126template <class _BiIter>
5127inline _LIBCPP_INLINE_VISIBILITY
5128bool
5129operator>(const sub_match<_BiIter>& __x,
5130 typename iterator_traits<_BiIter>::value_type const& __y)
5131{
5132 return __y < __x;
5133}
5134
5135template <class _BiIter>
5136inline _LIBCPP_INLINE_VISIBILITY
5137bool
5138operator>=(const sub_match<_BiIter>& __x,
5139 typename iterator_traits<_BiIter>::value_type const& __y)
5140{
5141 return !(__x < __y);
5142}
5143
5144template <class _BiIter>
5145inline _LIBCPP_INLINE_VISIBILITY
5146bool
5147operator<=(const sub_match<_BiIter>& __x,
5148 typename iterator_traits<_BiIter>::value_type const& __y)
5149{
5150 return !(__y < __x);
5151}
5152
5153template <class _CharT, class _ST, class _BiIter>
5154inline _LIBCPP_INLINE_VISIBILITY
5155basic_ostream<_CharT, _ST>&
5156operator<<(basic_ostream<_CharT, _ST>& __os, const sub_match<_BiIter>& __m)
5157{
5158 return __os << __m.str();
5159}
5160
Howard Hinnant17615b02010-07-27 01:25:38 +00005161template <class _BidirectionalIterator, class _Allocator>
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005162class match_results
5163{
5164public:
5165 typedef _Allocator allocator_type;
5166 typedef sub_match<_BidirectionalIterator> value_type;
5167private:
5168 typedef vector<value_type, allocator_type> __container_type;
5169
5170 __container_type __matches_;
5171 value_type __unmatched_;
5172 value_type __prefix_;
5173 value_type __suffix_;
5174public:
Howard Hinnanta712c722010-08-16 20:21:16 +00005175 _BidirectionalIterator __position_start_;
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005176 typedef const value_type& const_reference;
5177 typedef const_reference reference;
5178 typedef typename __container_type::const_iterator const_iterator;
5179 typedef const_iterator iterator;
5180 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
5181 typedef typename allocator_traits<allocator_type>::size_type size_type;
5182 typedef typename iterator_traits<_BidirectionalIterator>::value_type char_type;
5183 typedef basic_string<char_type> string_type;
5184
5185 // construct/copy/destroy:
5186 explicit match_results(const allocator_type& __a = allocator_type());
5187// match_results(const match_results&) = default;
5188// match_results& operator=(const match_results&) = default;
5189#ifdef _LIBCPP_MOVE
5190// match_results(match_results&& __m) = default;
5191// match_results& operator=(match_results&& __m) = default;
5192#endif
5193// ~match_results() = default;
5194
5195 // size:
5196 size_type size() const {return __matches_.size();}
5197 size_type max_size() const {return __matches_.max_size();}
5198 bool empty() const {return size() == 0;}
5199
5200 // element access:
5201 difference_type length(size_type __sub = 0) const
5202 {return (*this)[__sub].length();}
5203 difference_type position(size_type __sub = 0) const
Howard Hinnanta712c722010-08-16 20:21:16 +00005204 {return _STD::distance(__position_start_, (*this)[__sub].first);}
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005205 string_type str(size_type __sub = 0) const
5206 {return (*this)[__sub].str();}
5207 const_reference operator[](size_type __n) const
5208 {return __n < __matches_.size() ? __matches_[__n] : __unmatched_;}
5209
5210 const_reference prefix() const {return __prefix_;}
5211 const_reference suffix() const {return __suffix_;}
5212
5213 const_iterator begin() const {return empty() ? __matches_.end() : __matches_.begin() + 1;}
5214 const_iterator end() const {return __matches_.end();}
5215 const_iterator cbegin() const {return empty() ? __matches_.end() : __matches_.begin() + 1;}
5216 const_iterator cend() const {return __matches_.end();}
5217
5218 // format:
5219 template <class _OutputIter>
5220 _OutputIter
5221 format(_OutputIter __out, const char_type* __fmt_first,
5222 const char_type* __fmt_last,
5223 regex_constants::match_flag_type __flags = regex_constants::format_default) const;
5224 template <class _OutputIter, class _ST, class _SA>
5225 _OutputIter
5226 format(_OutputIter __out, const basic_string<char_type, _ST, _SA>& __fmt,
Howard Hinnant27405f92010-08-14 18:14:02 +00005227 regex_constants::match_flag_type __flags = regex_constants::format_default) const
5228 {return format(__out, __fmt.data(), __fmt.data() + __fmt.size(), __flags);}
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005229 template <class _ST, class _SA>
5230 basic_string<char_type, _ST, _SA>
5231 format(const basic_string<char_type, _ST, _SA>& __fmt,
Howard Hinnant27405f92010-08-14 18:14:02 +00005232 regex_constants::match_flag_type __flags = regex_constants::format_default) const
5233 {
5234 basic_string<char_type, _ST, _SA> __r;
5235 format(back_inserter(__r), __fmt.data(), __fmt.data() + __fmt.size(),
5236 __flags);
5237 return __r;
5238 }
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005239 string_type
5240 format(const char_type* __fmt,
Howard Hinnant27405f92010-08-14 18:14:02 +00005241 regex_constants::match_flag_type __flags = regex_constants::format_default) const
5242 {
5243 string_type __r;
5244 format(back_inserter(__r), __fmt,
5245 __fmt + char_traits<char_type>::length(__fmt), __flags);
5246 return __r;
5247 }
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005248
5249 // allocator:
5250 allocator_type get_allocator() const {return __matches_.get_allocator();}
5251
5252 // swap:
5253 void swap(match_results& __m);
5254
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005255 template <class _B, class _A>
5256 void __assign(_BidirectionalIterator __f, _BidirectionalIterator __l,
Howard Hinnanta712c722010-08-16 20:21:16 +00005257 const match_results<_B, _A>& __m, bool __no_update_pos)
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005258 {
5259 _B __mf = __m.prefix().first;
5260 __matches_.resize(__m.size());
5261 for (size_type __i = 0; __i < __matches_.size(); ++__i)
5262 {
5263 __matches_[__i].first = next(__f, _STD::distance(__mf, __m[__i].first));
5264 __matches_[__i].second = next(__f, _STD::distance(__mf, __m[__i].second));
5265 __matches_[__i].matched = __m[__i].matched;
5266 }
5267 __unmatched_.first = __l;
5268 __unmatched_.second = __l;
5269 __unmatched_.matched = false;
5270 __prefix_.first = next(__f, _STD::distance(__mf, __m.prefix().first));
5271 __prefix_.second = next(__f, _STD::distance(__mf, __m.prefix().second));
5272 __prefix_.matched = __m.prefix().matched;
5273 __suffix_.first = next(__f, _STD::distance(__mf, __m.suffix().first));
5274 __suffix_.second = next(__f, _STD::distance(__mf, __m.suffix().second));
5275 __suffix_.matched = __m.suffix().matched;
Howard Hinnanta712c722010-08-16 20:21:16 +00005276 if (!__no_update_pos)
5277 __position_start_ = __prefix_.first;
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005278 }
5279
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005280private:
5281 void __init(unsigned __s,
Howard Hinnanta712c722010-08-16 20:21:16 +00005282 _BidirectionalIterator __f, _BidirectionalIterator __l,
5283 bool __no_update_pos = false);
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005284
5285 template <class, class> friend class basic_regex;
5286
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005287 template <class _B, class _A, class _C, class _T>
5288 friend
5289 bool
5290 regex_match(_B, _B, match_results<_B, _A>&, const basic_regex<_C, _T>&,
5291 regex_constants::match_flag_type);
Howard Hinnante9de5ff2010-07-27 22:20:32 +00005292
Howard Hinnant27405f92010-08-14 18:14:02 +00005293 template <class _B, class _A>
5294 friend
5295 bool
5296 operator==(const match_results<_B, _A>&, const match_results<_B, _A>&);
5297
Howard Hinnante9de5ff2010-07-27 22:20:32 +00005298 template <class, class> friend class __lookahead;
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005299};
5300
5301template <class _BidirectionalIterator, class _Allocator>
5302match_results<_BidirectionalIterator, _Allocator>::match_results(
5303 const allocator_type& __a)
5304 : __matches_(__a),
5305 __unmatched_(),
5306 __prefix_(),
Howard Hinnanta712c722010-08-16 20:21:16 +00005307 __suffix_(),
5308 __position_start_()
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005309{
5310}
5311
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005312template <class _BidirectionalIterator, class _Allocator>
5313void
5314match_results<_BidirectionalIterator, _Allocator>::__init(unsigned __s,
Howard Hinnanta712c722010-08-16 20:21:16 +00005315 _BidirectionalIterator __f, _BidirectionalIterator __l,
5316 bool __no_update_pos)
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005317{
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005318 __unmatched_.first = __l;
5319 __unmatched_.second = __l;
5320 __unmatched_.matched = false;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005321 __matches_.assign(__s, __unmatched_);
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005322 __prefix_.first = __f;
5323 __prefix_.second = __f;
5324 __prefix_.matched = false;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005325 __suffix_ = __unmatched_;
Howard Hinnanta712c722010-08-16 20:21:16 +00005326 if (!__no_update_pos)
5327 __position_start_ = __prefix_.first;
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005328}
5329
Howard Hinnant27405f92010-08-14 18:14:02 +00005330template <class _BidirectionalIterator, class _Allocator>
5331template <class _OutputIter>
5332_OutputIter
5333match_results<_BidirectionalIterator, _Allocator>::format(_OutputIter __out,
5334 const char_type* __fmt_first, const char_type* __fmt_last,
5335 regex_constants::match_flag_type __flags) const
5336{
5337 if (__flags & regex_constants::format_sed)
5338 {
5339 for (; __fmt_first != __fmt_last; ++__fmt_first)
5340 {
5341 if (*__fmt_first == '&')
5342 __out = _STD::copy(__matches_[0].first, __matches_[0].second,
5343 __out);
5344 else if (*__fmt_first == '\\' && __fmt_first + 1 != __fmt_last)
5345 {
5346 ++__fmt_first;
5347 if ('0' <= *__fmt_first && *__fmt_first <= '9')
5348 {
5349 size_t __i = *__fmt_first - '0';
5350 __out = _STD::copy(__matches_[__i].first,
5351 __matches_[__i].second, __out);
5352 }
5353 else
5354 {
5355 *__out = *__fmt_first;
5356 ++__out;
5357 }
5358 }
5359 else
5360 {
5361 *__out = *__fmt_first;
5362 ++__out;
5363 }
5364 }
5365 }
5366 else
5367 {
5368 for (; __fmt_first != __fmt_last; ++__fmt_first)
5369 {
5370 if (*__fmt_first == '$' && __fmt_first + 1 != __fmt_last)
5371 {
5372 switch (__fmt_first[1])
5373 {
5374 case '$':
5375 *__out = *++__fmt_first;
5376 ++__out;
5377 break;
5378 case '&':
5379 ++__fmt_first;
5380 __out = _STD::copy(__matches_[0].first, __matches_[0].second,
5381 __out);
5382 break;
5383 case '`':
5384 ++__fmt_first;
5385 __out = _STD::copy(__prefix_.first, __prefix_.second, __out);
5386 break;
5387 case '\'':
5388 ++__fmt_first;
5389 __out = _STD::copy(__suffix_.first, __suffix_.second, __out);
5390 break;
5391 default:
5392 if ('0' <= __fmt_first[1] && __fmt_first[1] <= '9')
5393 {
5394 ++__fmt_first;
5395 size_t __i = *__fmt_first - '0';
5396 if (__fmt_first + 1 != __fmt_last &&
5397 '0' <= __fmt_first[1] && __fmt_first[1] <= '9')
5398 {
5399 ++__fmt_first;
5400 __i = 10 * __i + *__fmt_first - '0';
5401 }
5402 __out = _STD::copy(__matches_[__i].first,
5403 __matches_[__i].second, __out);
5404 }
5405 else
5406 {
5407 *__out = *__fmt_first;
5408 ++__out;
5409 }
5410 break;
5411 }
5412 }
5413 else
5414 {
5415 *__out = *__fmt_first;
5416 ++__out;
5417 }
5418 }
5419 }
5420 return __out;
5421}
5422
5423template <class _BidirectionalIterator, class _Allocator>
5424void
5425match_results<_BidirectionalIterator, _Allocator>::swap(match_results& __m)
5426{
5427 using _STD::swap;
5428 swap(__matches_, __m.__matches_);
5429 swap(__unmatched_, __m.__unmatched_);
5430 swap(__prefix_, __m.__prefix_);
5431 swap(__suffix_, __m.__suffix_);
Howard Hinnanta712c722010-08-16 20:21:16 +00005432 swap(__position_start_, __m.__position_start_);
Howard Hinnant27405f92010-08-14 18:14:02 +00005433}
5434
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005435typedef match_results<const char*> cmatch;
5436typedef match_results<const wchar_t*> wcmatch;
5437typedef match_results<string::const_iterator> smatch;
5438typedef match_results<wstring::const_iterator> wsmatch;
5439
5440template <class _BidirectionalIterator, class _Allocator>
Howard Hinnant27405f92010-08-14 18:14:02 +00005441bool
5442operator==(const match_results<_BidirectionalIterator, _Allocator>& __x,
5443 const match_results<_BidirectionalIterator, _Allocator>& __y)
5444{
5445 return __x.__matches_ == __y.__matches_ &&
5446 __x.__prefix_ == __y.__prefix_ &&
Howard Hinnanta712c722010-08-16 20:21:16 +00005447 __x.__suffix_ == __y.__suffix_ &&
5448 __x.__position_start_ == __y.__position_start_;
Howard Hinnant27405f92010-08-14 18:14:02 +00005449}
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005450
5451template <class _BidirectionalIterator, class _Allocator>
Howard Hinnant27405f92010-08-14 18:14:02 +00005452inline _LIBCPP_INLINE_VISIBILITY
5453bool
5454operator!=(const match_results<_BidirectionalIterator, _Allocator>& __x,
5455 const match_results<_BidirectionalIterator, _Allocator>& __y)
5456{
5457 return !(__x == __y);
5458}
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005459
5460template <class _BidirectionalIterator, class _Allocator>
Howard Hinnant27405f92010-08-14 18:14:02 +00005461inline _LIBCPP_INLINE_VISIBILITY
5462void
5463swap(match_results<_BidirectionalIterator, _Allocator>& __x,
5464 match_results<_BidirectionalIterator, _Allocator>& __y)
5465{
5466 __x.swap(__y);
5467}
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005468
5469// regex_search
5470
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005471template <class _CharT, class _Traits>
Howard Hinnant17615b02010-07-27 01:25:38 +00005472template <class _Allocator>
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005473bool
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005474basic_regex<_CharT, _Traits>::__match_at_start_ecma(
Howard Hinnant17615b02010-07-27 01:25:38 +00005475 const _CharT* __first, const _CharT* __last,
5476 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005477 regex_constants::match_flag_type __flags) const
5478{
Howard Hinnant17615b02010-07-27 01:25:38 +00005479 vector<__state> __states;
5480 ptrdiff_t __j = 0;
5481 ptrdiff_t _N = _STD::distance(__first, __last);
5482 __node* __st = __start_.get();
5483 if (__st)
5484 {
5485 __states.push_back(__state());
5486 __states.back().__do_ = 0;
5487 __states.back().__first_ = __first;
5488 __states.back().__current_ = __first;
5489 __states.back().__last_ = __last;
5490 __states.back().__sub_matches_.resize(mark_count());
5491 __states.back().__loop_data_.resize(__loop_count());
5492 __states.back().__node_ = __st;
5493 __states.back().__flags_ = __flags;
5494 bool __matched = false;
5495 do
5496 {
5497 __state& __s = __states.back();
5498 if (__s.__node_)
5499 __s.__node_->__exec(__s);
5500 switch (__s.__do_)
5501 {
5502 case __state::__end_state:
5503 __m.__matches_[0].first = __first;
5504 __m.__matches_[0].second = _STD::next(__first, __s.__current_ - __first);
5505 __m.__matches_[0].matched = true;
5506 for (unsigned __i = 0; __i < __s.__sub_matches_.size(); ++__i)
5507 __m.__matches_[__i+1] = __s.__sub_matches_[__i];
5508 return true;
5509 case __state::__accept_and_consume:
5510 case __state::__repeat:
5511 case __state::__accept_but_not_consume:
5512 break;
5513 case __state::__split:
5514 {
5515 __state __snext = __s;
5516 __s.__node_->__exec_split(true, __s);
5517 __snext.__node_->__exec_split(false, __snext);
5518 __states.push_back(_STD::move(__snext));
5519 }
5520 break;
5521 case __state::__reject:
5522 __states.pop_back();
5523 break;
5524 default:
Howard Hinnantd4444702010-08-11 17:04:31 +00005525#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005526 throw regex_error(regex_constants::__re_err_unknown);
Howard Hinnantd4444702010-08-11 17:04:31 +00005527#endif
Howard Hinnant17615b02010-07-27 01:25:38 +00005528 break;
Howard Hinnantd4444702010-08-11 17:04:31 +00005529
Howard Hinnant17615b02010-07-27 01:25:38 +00005530 }
5531 } while (!__states.empty());
5532 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005533 return false;
5534}
5535
5536template <class _CharT, class _Traits>
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005537template <class _Allocator>
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005538bool
5539basic_regex<_CharT, _Traits>::__match_at_start_posix_nosubs(
5540 const _CharT* __first, const _CharT* __last,
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005541 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005542 regex_constants::match_flag_type __flags) const
5543{
Howard Hinnantac303862010-07-12 15:51:17 +00005544 deque<__state> __states;
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005545 ptrdiff_t __highest_j = 0;
5546 ptrdiff_t _N = _STD::distance(__first, __last);
Howard Hinnantac303862010-07-12 15:51:17 +00005547 __node* __st = __start_.get();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005548 if (__st)
5549 {
Howard Hinnantac303862010-07-12 15:51:17 +00005550 __states.push_back(__state());
Howard Hinnantac303862010-07-12 15:51:17 +00005551 __states.back().__do_ = 0;
5552 __states.back().__first_ = __first;
5553 __states.back().__current_ = __first;
5554 __states.back().__last_ = __last;
5555 __states.back().__loop_data_.resize(__loop_count());
5556 __states.back().__node_ = __st;
5557 __states.back().__flags_ = __flags;
Howard Hinnant68025ed2010-07-14 15:45:11 +00005558 bool __matched = false;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005559 do
5560 {
Howard Hinnantac303862010-07-12 15:51:17 +00005561 __state& __s = __states.back();
5562 if (__s.__node_)
5563 __s.__node_->__exec(__s);
5564 switch (__s.__do_)
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005565 {
Howard Hinnantac303862010-07-12 15:51:17 +00005566 case __state::__end_state:
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005567 if (!__matched || __highest_j < __s.__current_ - __s.__first_)
Howard Hinnant68025ed2010-07-14 15:45:11 +00005568 __highest_j = __s.__current_ - __s.__first_;
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005569 __matched = true;
Howard Hinnantac303862010-07-12 15:51:17 +00005570 if (__highest_j == _N)
5571 __states.clear();
5572 else
5573 __states.pop_back();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005574 break;
Howard Hinnantac303862010-07-12 15:51:17 +00005575 case __state::__consume_input:
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005576 break;
Howard Hinnantac303862010-07-12 15:51:17 +00005577 case __state::__accept_and_consume:
Howard Hinnantac303862010-07-12 15:51:17 +00005578 __states.push_front(_STD::move(__s));
5579 __states.pop_back();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005580 break;
Howard Hinnantac303862010-07-12 15:51:17 +00005581 case __state::__repeat:
5582 case __state::__accept_but_not_consume:
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005583 break;
Howard Hinnantac303862010-07-12 15:51:17 +00005584 case __state::__split:
5585 {
5586 __state __snext = __s;
5587 __s.__node_->__exec_split(true, __s);
5588 __snext.__node_->__exec_split(false, __snext);
5589 __states.push_back(_STD::move(__snext));
5590 }
5591 break;
5592 case __state::__reject:
5593 __states.pop_back();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005594 break;
5595 default:
Howard Hinnantd4444702010-08-11 17:04:31 +00005596#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005597 throw regex_error(regex_constants::__re_err_unknown);
Howard Hinnantd4444702010-08-11 17:04:31 +00005598#endif
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005599 break;
5600 }
Howard Hinnantac303862010-07-12 15:51:17 +00005601 } while (!__states.empty());
Howard Hinnant68025ed2010-07-14 15:45:11 +00005602 if (__matched)
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005603 {
5604 __m.__matches_[0].first = __first;
5605 __m.__matches_[0].second = _STD::next(__first, __highest_j);
5606 __m.__matches_[0].matched = true;
5607 return true;
5608 }
5609 }
5610 return false;
5611}
5612
5613template <class _CharT, class _Traits>
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005614template <class _Allocator>
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005615bool
5616basic_regex<_CharT, _Traits>::__match_at_start_posix_subs(
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005617 const _CharT* __first, const _CharT* __last,
5618 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005619 regex_constants::match_flag_type __flags) const
5620{
Howard Hinnantac303862010-07-12 15:51:17 +00005621 vector<__state> __states;
Howard Hinnantac303862010-07-12 15:51:17 +00005622 __state __best_state;
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005623 ptrdiff_t __j = 0;
5624 ptrdiff_t __highest_j = 0;
5625 ptrdiff_t _N = _STD::distance(__first, __last);
Howard Hinnantac303862010-07-12 15:51:17 +00005626 __node* __st = __start_.get();
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005627 if (__st)
5628 {
Howard Hinnantac303862010-07-12 15:51:17 +00005629 __states.push_back(__state());
5630 __states.back().__do_ = 0;
5631 __states.back().__first_ = __first;
5632 __states.back().__current_ = __first;
5633 __states.back().__last_ = __last;
5634 __states.back().__sub_matches_.resize(mark_count());
5635 __states.back().__loop_data_.resize(__loop_count());
5636 __states.back().__node_ = __st;
5637 __states.back().__flags_ = __flags;
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005638 const _CharT* __current = __first;
Howard Hinnantac303862010-07-12 15:51:17 +00005639 bool __matched = false;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005640 do
5641 {
Howard Hinnantac303862010-07-12 15:51:17 +00005642 __state& __s = __states.back();
5643 if (__s.__node_)
5644 __s.__node_->__exec(__s);
5645 switch (__s.__do_)
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005646 {
Howard Hinnantac303862010-07-12 15:51:17 +00005647 case __state::__end_state:
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005648 if (!__matched || __highest_j < __s.__current_ - __s.__first_)
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005649 {
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005650 __highest_j = __s.__current_ - __s.__first_;
Howard Hinnantac303862010-07-12 15:51:17 +00005651 __best_state = __s;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005652 }
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005653 __matched = true;
5654 if (__highest_j == _N)
5655 __states.clear();
5656 else
5657 __states.pop_back();
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005658 break;
Howard Hinnantac303862010-07-12 15:51:17 +00005659 case __state::__accept_and_consume:
Howard Hinnantcba352d2010-07-12 18:16:05 +00005660 __j += __s.__current_ - __current;
5661 __current = __s.__current_;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005662 break;
Howard Hinnantac303862010-07-12 15:51:17 +00005663 case __state::__repeat:
5664 case __state::__accept_but_not_consume:
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005665 break;
Howard Hinnantac303862010-07-12 15:51:17 +00005666 case __state::__split:
5667 {
5668 __state __snext = __s;
5669 __s.__node_->__exec_split(true, __s);
5670 __snext.__node_->__exec_split(false, __snext);
5671 __states.push_back(_STD::move(__snext));
5672 }
5673 break;
5674 case __state::__reject:
5675 __states.pop_back();
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005676 break;
5677 default:
Howard Hinnantd4444702010-08-11 17:04:31 +00005678#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005679 throw regex_error(regex_constants::__re_err_unknown);
Howard Hinnantd4444702010-08-11 17:04:31 +00005680#endif
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005681 break;
5682 }
Howard Hinnantac303862010-07-12 15:51:17 +00005683 } while (!__states.empty());
5684 if (__matched)
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005685 {
5686 __m.__matches_[0].first = __first;
5687 __m.__matches_[0].second = _STD::next(__first, __highest_j);
5688 __m.__matches_[0].matched = true;
Howard Hinnantac303862010-07-12 15:51:17 +00005689 for (unsigned __i = 0; __i < __best_state.__sub_matches_.size(); ++__i)
5690 __m.__matches_[__i+1] = __best_state.__sub_matches_[__i];
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005691 return true;
5692 }
5693 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005694 return false;
5695}
5696
5697template <class _CharT, class _Traits>
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005698template <class _Allocator>
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005699bool
5700basic_regex<_CharT, _Traits>::__match_at_start(
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005701 const _CharT* __first, const _CharT* __last,
5702 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005703 regex_constants::match_flag_type __flags) const
5704{
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005705 if ((__flags_ & 0x1F0) == ECMAScript)
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005706 return __match_at_start_ecma(__first, __last, __m, __flags);
5707 if (mark_count() == 0)
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005708 return __match_at_start_posix_nosubs(__first, __last, __m, __flags);
5709 return __match_at_start_posix_subs(__first, __last, __m, __flags);
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005710}
5711
5712template <class _CharT, class _Traits>
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005713template <class _Allocator>
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005714bool
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005715basic_regex<_CharT, _Traits>::__search(
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005716 const _CharT* __first, const _CharT* __last,
5717 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005718 regex_constants::match_flag_type __flags) const
5719{
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00005720 if (__left_anchor_)
5721 __flags |= regex_constants::match_continuous;
Howard Hinnanta712c722010-08-16 20:21:16 +00005722 __m.__init(1 + mark_count(), __first, __last,
5723 __flags & regex_constants::__no_update_pos);
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005724 if (__match_at_start(__first, __last, __m, __flags))
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005725 {
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005726 __m.__prefix_.second = __m[0].first;
5727 __m.__prefix_.matched = __m.__prefix_.first != __m.__prefix_.second;
5728 __m.__suffix_.first = __m[0].second;
5729 __m.__suffix_.matched = __m.__suffix_.first != __m.__suffix_.second;
5730 return true;
5731 }
Howard Hinnant8daa7332010-07-29 01:15:27 +00005732 if (__first != __last && !(__flags & regex_constants::match_continuous))
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005733 {
Howard Hinnantf3dcca02010-07-29 15:17:28 +00005734 __flags |= regex_constants::match_prev_avail;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005735 for (++__first; __first != __last; ++__first)
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005736 {
Howard Hinnantf3dcca02010-07-29 15:17:28 +00005737 __m.__matches_.assign(__m.size(), __m.__unmatched_);
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005738 if (__match_at_start(__first, __last, __m, __flags))
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005739 {
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005740 __m.__prefix_.second = __m[0].first;
5741 __m.__prefix_.matched = __m.__prefix_.first != __m.__prefix_.second;
5742 __m.__suffix_.first = __m[0].second;
5743 __m.__suffix_.matched = __m.__suffix_.first != __m.__suffix_.second;
5744 return true;
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005745 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005746 __m.__matches_.assign(__m.size(), __m.__unmatched_);
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005747 }
5748 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005749 __m.__matches_.clear();
5750 return false;
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005751}
5752
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005753template <class _BidirectionalIterator, class _Allocator, class _CharT, class _Traits>
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005754inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005755bool
5756regex_search(_BidirectionalIterator __first, _BidirectionalIterator __last,
5757 match_results<_BidirectionalIterator, _Allocator>& __m,
5758 const basic_regex<_CharT, _Traits>& __e,
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005759 regex_constants::match_flag_type __flags = regex_constants::match_default)
5760{
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005761 basic_string<_CharT> __s(__first, __last);
5762 match_results<const _CharT*> __mc;
5763 bool __r = __e.__search(__s.data(), __s.data() + __s.size(), __mc, __flags);
Howard Hinnanta712c722010-08-16 20:21:16 +00005764 __m.__assign(__first, __last, __mc, __flags & regex_constants::__no_update_pos);
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005765 return __r;
5766}
5767
5768template <class _Allocator, class _CharT, class _Traits>
5769inline _LIBCPP_INLINE_VISIBILITY
5770bool
5771regex_search(const _CharT* __first, const _CharT* __last,
5772 match_results<const _CharT*, _Allocator>& __m,
5773 const basic_regex<_CharT, _Traits>& __e,
5774 regex_constants::match_flag_type __flags = regex_constants::match_default)
5775{
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005776 return __e.__search(__first, __last, __m, __flags);
5777}
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005778
5779template <class _BidirectionalIterator, class _CharT, class _Traits>
5780inline _LIBCPP_INLINE_VISIBILITY
5781bool
5782regex_search(_BidirectionalIterator __first, _BidirectionalIterator __last,
5783 const basic_regex<_CharT, _Traits>& __e,
5784 regex_constants::match_flag_type __flags = regex_constants::match_default)
5785{
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005786 basic_string<_CharT> __s(__first, __last);
5787 match_results<const _CharT*> __mc;
5788 return __e.__search(__s.data(), __s.data() + __s.size(), __mc, __flags);
5789}
5790
5791template <class _CharT, class _Traits>
5792inline _LIBCPP_INLINE_VISIBILITY
5793bool
5794regex_search(const _CharT* __first, const _CharT* __last,
5795 const basic_regex<_CharT, _Traits>& __e,
5796 regex_constants::match_flag_type __flags = regex_constants::match_default)
5797{
5798 match_results<const _CharT*> __mc;
5799 return __e.__search(__first, __last, __mc, __flags);
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005800}
5801
5802template <class _CharT, class _Allocator, class _Traits>
5803inline _LIBCPP_INLINE_VISIBILITY
5804bool
5805regex_search(const _CharT* __str, match_results<const _CharT*, _Allocator>& __m,
5806 const basic_regex<_CharT, _Traits>& __e,
5807 regex_constants::match_flag_type __flags = regex_constants::match_default)
5808{
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005809 return __e.__search(__str, __str + _Traits::length(__str), __m, __flags);
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005810}
5811
5812template <class _CharT, class _Traits>
5813inline _LIBCPP_INLINE_VISIBILITY
5814bool
5815regex_search(const _CharT* __str, const basic_regex<_CharT, _Traits>& __e,
5816 regex_constants::match_flag_type __flags = regex_constants::match_default)
5817{
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005818 match_results<const _CharT*> __m;
5819 return _STD::regex_search(__str, __m, __e, __flags);
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005820}
5821
5822template <class _ST, class _SA, class _CharT, class _Traits>
5823inline _LIBCPP_INLINE_VISIBILITY
5824bool
5825regex_search(const basic_string<_CharT, _ST, _SA>& __s,
5826 const basic_regex<_CharT, _Traits>& __e,
5827 regex_constants::match_flag_type __flags = regex_constants::match_default)
5828{
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005829 match_results<const _CharT*> __mc;
5830 return __e.__search(__s.data(), __s.data() + __s.size(), __mc, __flags);
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005831}
5832
5833template <class _ST, class _SA, class _Allocator, class _CharT, class _Traits>
5834inline _LIBCPP_INLINE_VISIBILITY
5835bool
5836regex_search(const basic_string<_CharT, _ST, _SA>& __s,
5837 match_results<typename basic_string<_CharT, _ST, _SA>::const_iterator, _Allocator>& __m,
5838 const basic_regex<_CharT, _Traits>& __e,
5839 regex_constants::match_flag_type __flags = regex_constants::match_default)
5840{
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005841 match_results<const _CharT*> __mc;
5842 bool __r = __e.__search(__s.data(), __s.data() + __s.size(), __mc, __flags);
Howard Hinnanta712c722010-08-16 20:21:16 +00005843 __m.__assign(__s.begin(), __s.end(), __mc, __flags & regex_constants::__no_update_pos);
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005844 return __r;
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005845}
5846
5847// regex_match
5848
5849template <class _BidirectionalIterator, class _Allocator, class _CharT, class _Traits>
5850bool
5851regex_match(_BidirectionalIterator __first, _BidirectionalIterator __last,
5852 match_results<_BidirectionalIterator, _Allocator>& __m,
5853 const basic_regex<_CharT, _Traits>& __e,
5854 regex_constants::match_flag_type __flags = regex_constants::match_default)
5855{
5856 bool __r = _STD::regex_search(__first, __last, __m, __e,
5857 __flags | regex_constants::match_continuous);
5858 if (__r)
5859 {
5860 __r = !__m.suffix().matched;
5861 if (!__r)
5862 __m.__matches_.clear();
5863 }
5864 return __r;
5865}
5866
5867template <class _BidirectionalIterator, class _CharT, class _Traits>
5868inline _LIBCPP_INLINE_VISIBILITY
5869bool
5870regex_match(_BidirectionalIterator __first, _BidirectionalIterator __last,
5871 const basic_regex<_CharT, _Traits>& __e,
5872 regex_constants::match_flag_type __flags = regex_constants::match_default)
5873{
5874 match_results<_BidirectionalIterator> __m;
5875 return _STD::regex_match(__first, __last, __m, __e, __flags);
5876}
5877
5878template <class _CharT, class _Allocator, class _Traits>
5879inline _LIBCPP_INLINE_VISIBILITY
5880bool
5881regex_match(const _CharT* __str, match_results<const _CharT*, _Allocator>& __m,
5882 const basic_regex<_CharT, _Traits>& __e,
5883 regex_constants::match_flag_type __flags = regex_constants::match_default)
5884{
5885 return _STD::regex_match(__str, __str + _Traits::length(__str), __m, __e, __flags);
5886}
5887
5888template <class _ST, class _SA, class _Allocator, class _CharT, class _Traits>
5889inline _LIBCPP_INLINE_VISIBILITY
5890bool
5891regex_match(const basic_string<_CharT, _ST, _SA>& __s,
5892 match_results<typename basic_string<_CharT, _ST, _SA>::const_iterator, _Allocator>& __m,
5893 const basic_regex<_CharT, _Traits>& __e,
5894 regex_constants::match_flag_type __flags = regex_constants::match_default)
5895{
5896 return _STD::regex_match(__s.begin(), __s.end(), __m, __e, __flags);
5897}
5898
5899template <class _CharT, class _Traits>
5900inline _LIBCPP_INLINE_VISIBILITY
5901bool
5902regex_match(const _CharT* __str, const basic_regex<_CharT, _Traits>& __e,
5903 regex_constants::match_flag_type __flags = regex_constants::match_default)
5904{
5905 return _STD::regex_match(__str, __str + _Traits::length(__str), __e, __flags);
5906}
5907
5908template <class _ST, class _SA, class _CharT, class _Traits>
5909inline _LIBCPP_INLINE_VISIBILITY
5910bool
5911regex_match(const basic_string<_CharT, _ST, _SA>& __s,
5912 const basic_regex<_CharT, _Traits>& __e,
5913 regex_constants::match_flag_type __flags = regex_constants::match_default)
5914{
5915 return _STD::regex_match(__s.begin(), __s.end(), __e, __flags);
5916}
5917
Howard Hinnanta712c722010-08-16 20:21:16 +00005918// regex_iterator
5919
5920template <class _BidirectionalIterator,
5921 class _CharT = typename iterator_traits<_BidirectionalIterator>::value_type,
5922 class _Traits = regex_traits<_CharT> >
5923class regex_iterator
5924{
5925public:
5926 typedef basic_regex<_CharT, _Traits> regex_type;
5927 typedef match_results<_BidirectionalIterator> value_type;
5928 typedef ptrdiff_t difference_type;
5929 typedef const value_type* pointer;
5930 typedef const value_type& reference;
5931 typedef forward_iterator_tag iterator_category;
5932
5933private:
5934 _BidirectionalIterator __begin_;
5935 _BidirectionalIterator __end_;
5936 const regex_type* __pregex_;
5937 regex_constants::match_flag_type __flags_;
5938 value_type __match_;
5939
5940public:
5941 regex_iterator();
5942 regex_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
5943 const regex_type& __re,
5944 regex_constants::match_flag_type __m = regex_constants::match_default);
5945
5946 bool operator==(const regex_iterator& __x) const;
5947 bool operator!=(const regex_iterator& __x) const {return !(*this == __x);}
5948
5949 reference operator*() const {return __match_;}
5950 pointer operator->() const {return &__match_;}
5951
5952 regex_iterator& operator++();
5953 regex_iterator operator++(int)
5954 {
5955 regex_iterator __t(*this);
5956 ++(*this);
5957 return __t;
5958 }
5959};
5960
5961template <class _BidirectionalIterator, class _CharT, class _Traits>
5962regex_iterator<_BidirectionalIterator, _CharT, _Traits>::regex_iterator()
5963 : __begin_(), __end_(), __pregex_(nullptr), __flags_(), __match_()
5964{
5965}
5966
5967template <class _BidirectionalIterator, class _CharT, class _Traits>
5968regex_iterator<_BidirectionalIterator, _CharT, _Traits>::
5969 regex_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
5970 const regex_type& __re, regex_constants::match_flag_type __m)
5971 : __begin_(__a),
5972 __end_(__b),
5973 __pregex_(&__re),
5974 __flags_(__m)
5975{
5976 _STD::regex_search(__begin_, __end_, __match_, *__pregex_, __flags_);
5977}
5978
5979template <class _BidirectionalIterator, class _CharT, class _Traits>
5980bool
5981regex_iterator<_BidirectionalIterator, _CharT, _Traits>::
5982 operator==(const regex_iterator& __x) const
5983{
5984 if (__match_.empty() && __x.__match_.empty())
5985 return true;
5986 if (__match_.empty() || __x.__match_.empty())
5987 return false;
5988 return __begin_ == __x.__begin_ &&
5989 __end_ == __x.__end_ &&
5990 __pregex_ == __x.__pregex_ &&
5991 __flags_ == __x.__flags_ &&
5992 __match_[0] == __x.__match_[0];
5993}
5994
5995template <class _BidirectionalIterator, class _CharT, class _Traits>
5996regex_iterator<_BidirectionalIterator, _CharT, _Traits>&
5997regex_iterator<_BidirectionalIterator, _CharT, _Traits>::operator++()
5998{
5999 __flags_ |= regex_constants::__no_update_pos;
6000 _BidirectionalIterator __start = __match_[0].second;
6001 if (__match_.length() == 0)
6002 {
6003 if (__start == __end_)
6004 {
6005 __match_ = value_type();
6006 return *this;
6007 }
6008 else if (_STD::regex_search(__start, __end_, __match_, *__pregex_,
6009 __flags_ | regex_constants::match_not_null |
6010 regex_constants::match_continuous))
6011 return *this;
6012 else
6013 ++__start;
6014 }
6015 __flags_ |= regex_constants::match_prev_avail;
6016 if (!_STD::regex_search(__start, __end_, __match_, *__pregex_, __flags_))
6017 __match_ = value_type();
6018 return *this;
6019}
6020
6021typedef regex_iterator<const char*> cregex_iterator;
6022typedef regex_iterator<const wchar_t*> wcregex_iterator;
6023typedef regex_iterator<string::const_iterator> sregex_iterator;
6024typedef regex_iterator<wstring::const_iterator> wsregex_iterator;
6025
6026// regex_token_iterator
6027
6028template <class _BidirectionalIterator,
6029 class _CharT = typename iterator_traits<_BidirectionalIterator>::value_type,
6030 class _Traits = regex_traits<_CharT> >
6031class regex_token_iterator
6032{
6033public:
6034 typedef basic_regex<_CharT, _Traits> regex_type;
6035 typedef sub_match<_BidirectionalIterator> value_type;
6036 typedef ptrdiff_t difference_type;
6037 typedef const value_type* pointer;
6038 typedef const value_type& reference;
6039 typedef forward_iterator_tag iterator_category;
6040
Howard Hinnant262b7792010-08-17 20:42:03 +00006041private:
6042 typedef regex_iterator<_BidirectionalIterator, _CharT, _Traits> _Position;
6043
6044 _Position __position_;
6045 const value_type* __result_;
6046 value_type __suffix_;
6047 ptrdiff_t _N_;
6048 vector<int> __subs_;
6049
6050public:
Howard Hinnanta712c722010-08-16 20:21:16 +00006051 regex_token_iterator();
6052 regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6053 const regex_type& __re, int __submatch = 0,
Howard Hinnant262b7792010-08-17 20:42:03 +00006054 regex_constants::match_flag_type __m =
6055 regex_constants::match_default);
Howard Hinnanta712c722010-08-16 20:21:16 +00006056 regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6057 const regex_type& __re, const vector<int>& __submatches,
Howard Hinnant262b7792010-08-17 20:42:03 +00006058 regex_constants::match_flag_type __m =
6059 regex_constants::match_default);
Howard Hinnanta712c722010-08-16 20:21:16 +00006060 regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
Howard Hinnant262b7792010-08-17 20:42:03 +00006061 const regex_type& __re,
6062 initializer_list<int> __submatches,
6063 regex_constants::match_flag_type __m =
6064 regex_constants::match_default);
Howard Hinnanta712c722010-08-16 20:21:16 +00006065 template <size_t _N>
Howard Hinnant262b7792010-08-17 20:42:03 +00006066 regex_token_iterator(_BidirectionalIterator __a,
6067 _BidirectionalIterator __b,
6068 const regex_type& __re,
6069 const int (&__submatches)[_N],
6070 regex_constants::match_flag_type __m =
6071 regex_constants::match_default);
Howard Hinnanta712c722010-08-16 20:21:16 +00006072 regex_token_iterator(const regex_token_iterator&);
6073 regex_token_iterator& operator=(const regex_token_iterator&);
6074
Howard Hinnant262b7792010-08-17 20:42:03 +00006075 bool operator==(const regex_token_iterator& __x) const;
6076 bool operator!=(const regex_token_iterator& __x) const {return !(*this == __x);}
Howard Hinnanta712c722010-08-16 20:21:16 +00006077
Howard Hinnant262b7792010-08-17 20:42:03 +00006078 const value_type& operator*() const {return *__result_;}
6079 const value_type* operator->() const {return __result_;}
Howard Hinnanta712c722010-08-16 20:21:16 +00006080
6081 regex_token_iterator& operator++();
Howard Hinnant262b7792010-08-17 20:42:03 +00006082 regex_token_iterator operator++(int)
6083 {
6084 regex_token_iterator __t(*this);
6085 ++(*this);
6086 return __t;
6087 }
6088
6089private:
6090 void __init(_BidirectionalIterator __a, _BidirectionalIterator __b);
Howard Hinnanta712c722010-08-16 20:21:16 +00006091};
6092
Howard Hinnant262b7792010-08-17 20:42:03 +00006093template <class _BidirectionalIterator, class _CharT, class _Traits>
6094regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6095 regex_token_iterator()
6096 : __result_(nullptr),
6097 __suffix_(),
6098 _N_(0)
6099{
6100}
6101
6102template <class _BidirectionalIterator, class _CharT, class _Traits>
6103void
6104regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6105 __init(_BidirectionalIterator __a, _BidirectionalIterator __b)
6106{
6107 if (__position_ != _Position())
6108 {
6109 if (__subs_[_N_] == -1)
6110 __result_ = &__position_->prefix();
6111 else
6112 __result_ = &(*__position_)[__subs_[_N_]];
6113 }
6114 else if (__subs_[_N_] == -1)
6115 {
6116 __suffix_.matched = true;
6117 __suffix_.first = __a;
6118 __suffix_.second = __b;
6119 __result_ = &__suffix_;
6120 }
6121 else
6122 __result_ = nullptr;
6123}
6124
6125template <class _BidirectionalIterator, class _CharT, class _Traits>
6126regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6127 regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6128 const regex_type& __re, int __submatch,
6129 regex_constants::match_flag_type __m)
6130 : __position_(__a, __b, __re, __m),
6131 _N_(0),
6132 __subs_(1, __submatch)
6133{
6134 __init(__a, __b);
6135}
6136
6137template <class _BidirectionalIterator, class _CharT, class _Traits>
6138regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6139 regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6140 const regex_type& __re, const vector<int>& __submatches,
6141 regex_constants::match_flag_type __m)
6142 : __position_(__a, __b, __re, __m),
6143 _N_(0),
6144 __subs_(__submatches)
6145{
6146 __init(__a, __b);
6147}
6148
6149template <class _BidirectionalIterator, class _CharT, class _Traits>
6150regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6151 regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6152 const regex_type& __re,
6153 initializer_list<int> __submatches,
6154 regex_constants::match_flag_type __m)
6155 : __position_(__a, __b, __re, __m),
6156 _N_(0),
6157 __subs_(__submatches)
6158{
6159 __init(__a, __b);
6160}
6161
6162template <class _BidirectionalIterator, class _CharT, class _Traits>
6163template <size_t _N>
6164regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6165 regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6166 const regex_type& __re,
6167 const int (&__submatches)[_N],
6168 regex_constants::match_flag_type __m)
6169 : __position_(__a, __b, __re, __m),
6170 _N_(0),
6171 __subs_(__submatches, __submatches + _N)
6172{
6173 __init(__a, __b);
6174}
6175
6176template <class _BidirectionalIterator, class _CharT, class _Traits>
6177regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6178 regex_token_iterator(const regex_token_iterator& __x)
6179 : __position_(__x.__position_),
6180 __result_(__x.__result_),
6181 __suffix_(__x.__suffix_),
6182 _N_(__x._N_),
6183 __subs_(__x.__subs_)
6184{
6185 if (__x.__result_ == &__x.__suffix_)
6186 __result_ == &__suffix_;
6187}
6188
6189template <class _BidirectionalIterator, class _CharT, class _Traits>
6190regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>&
6191regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6192 operator=(const regex_token_iterator& __x)
6193{
6194 if (this != &__x)
6195 {
6196 __position_ = __x.__position_;
6197 if (__x.__result_ == &__x.__suffix_)
6198 __result_ == &__suffix_;
6199 else
6200 __result_ = __x.__result_;
6201 __suffix_ = __x.__suffix_;
6202 _N_ = __x._N_;
6203 __subs_ = __x.__subs_;
6204 }
6205 return *this;
6206}
6207
6208template <class _BidirectionalIterator, class _CharT, class _Traits>
6209bool
6210regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6211 operator==(const regex_token_iterator& __x) const
6212{
6213 if (__result_ == nullptr && __x.__result_ == nullptr)
6214 return true;
6215 if (__result_ == &__suffix_ && __x.__result_ == &__x.__suffix_ &&
6216 __suffix_ == __x.__suffix_)
6217 return true;
6218 if (__result_ == nullptr || __x.__result_ == nullptr)
6219 return false;
6220 if (__result_ == &__suffix_ || __x.__result_ == &__x.__suffix_)
6221 return false;
6222 return __position_ == __x.__position_ && _N_ == __x._N_ &&
6223 __subs_ == __x.__subs_;
6224}
6225
6226template <class _BidirectionalIterator, class _CharT, class _Traits>
6227regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>&
6228regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::operator++()
6229{
6230 _Position __prev = __position_;
6231 if (__result_ == &__suffix_)
6232 __result_ = nullptr;
6233 else if (_N_ + 1 < __subs_.size())
6234 {
6235 ++_N_;
6236 if (__subs_[_N_] == -1)
6237 __result_ = &__position_->prefix();
6238 else
6239 __result_ = &(*__position_)[__subs_[_N_]];
6240 }
6241 else
6242 {
6243 _N_ = 0;
6244 ++__position_;
6245 if (__position_ != _Position())
6246 {
6247 if (__subs_[_N_] == -1)
6248 __result_ = &__position_->prefix();
6249 else
6250 __result_ = &(*__position_)[__subs_[_N_]];
6251 }
6252 else
6253 {
6254 if (_STD::find(__subs_.begin(), __subs_.end(), -1) != __subs_.end()
6255 && __prev->suffix().length() != 0)
6256 {
6257 __suffix_.matched = true;
6258 __suffix_.first = __prev->suffix().first;
6259 __suffix_.second = __prev->suffix().second;
6260 __result_ = &__suffix_;
6261 }
6262 else
6263 __result_ = nullptr;
6264 }
6265 }
6266 return *this;
6267}
6268
Howard Hinnanta712c722010-08-16 20:21:16 +00006269typedef regex_token_iterator<const char*> cregex_token_iterator;
6270typedef regex_token_iterator<const wchar_t*> wcregex_token_iterator;
6271typedef regex_token_iterator<string::const_iterator> sregex_token_iterator;
6272typedef regex_token_iterator<wstring::const_iterator> wsregex_token_iterator;
6273
Howard Hinnant3257c982010-06-17 00:34:59 +00006274_LIBCPP_END_NAMESPACE_STD
6275
6276#endif // _LIBCPP_REGEX