blob: 25aab82cb8ef0b2d7ecac310744b0599cdfcd7de [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 Hinnantac303862010-07-12 15:51:17 +00001276// __node
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001277
1278template <class _CharT>
Howard Hinnantac303862010-07-12 15:51:17 +00001279class __node
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001280{
Howard Hinnantac303862010-07-12 15:51:17 +00001281 __node(const __node&);
1282 __node& operator=(const __node&);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001283public:
Howard Hinnantac303862010-07-12 15:51:17 +00001284 typedef _STD::__state<_CharT> __state;
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001285
Howard Hinnantac303862010-07-12 15:51:17 +00001286 __node() {}
1287 virtual ~__node() {}
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001288
Howard Hinnantac303862010-07-12 15:51:17 +00001289 virtual void __exec(__state&) const {};
1290 virtual void __exec_split(bool, __state&) const {};
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001291
Howard Hinnantac303862010-07-12 15:51:17 +00001292 virtual string speak() const {return "__node";}
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001293};
1294
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001295// __end_state
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001296
1297template <class _CharT>
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001298class __end_state
Howard Hinnantac303862010-07-12 15:51:17 +00001299 : public __node<_CharT>
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001300{
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001301public:
Howard Hinnantac303862010-07-12 15:51:17 +00001302 typedef _STD::__state<_CharT> __state;
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001303
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001304 __end_state() {}
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00001305
Howard Hinnantac303862010-07-12 15:51:17 +00001306 virtual void __exec(__state&) const;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001307
1308 virtual string speak() const {return "end state";}
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001309};
1310
1311template <class _CharT>
Howard Hinnantac303862010-07-12 15:51:17 +00001312void
1313__end_state<_CharT>::__exec(__state& __s) const
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00001314{
Howard Hinnantac303862010-07-12 15:51:17 +00001315 __s.__do_ = __state::__end_state;
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00001316}
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001317
1318// __has_one_state
1319
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00001320template <class _CharT>
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001321class __has_one_state
Howard Hinnantac303862010-07-12 15:51:17 +00001322 : public __node<_CharT>
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00001323{
Howard Hinnantac303862010-07-12 15:51:17 +00001324 __node<_CharT>* __first_;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001325
1326public:
Howard Hinnantac303862010-07-12 15:51:17 +00001327 explicit __has_one_state(__node<_CharT>* __s)
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001328 : __first_(__s) {}
1329
Howard Hinnantac303862010-07-12 15:51:17 +00001330 __node<_CharT>* first() const {return __first_;}
1331 __node<_CharT>*& first() {return __first_;}
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001332};
1333
1334// __owns_one_state
1335
1336template <class _CharT>
1337class __owns_one_state
1338 : public __has_one_state<_CharT>
1339{
1340 typedef __has_one_state<_CharT> base;
1341
1342public:
Howard Hinnantac303862010-07-12 15:51:17 +00001343 explicit __owns_one_state(__node<_CharT>* __s)
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001344 : base(__s) {}
1345
1346 virtual ~__owns_one_state();
1347};
1348
1349template <class _CharT>
1350__owns_one_state<_CharT>::~__owns_one_state()
1351{
1352 delete this->first();
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00001353}
1354
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001355// __empty_state
1356
1357template <class _CharT>
1358class __empty_state
1359 : public __owns_one_state<_CharT>
1360{
1361 typedef __owns_one_state<_CharT> base;
1362
1363public:
Howard Hinnantac303862010-07-12 15:51:17 +00001364 typedef _STD::__state<_CharT> __state;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001365
Howard Hinnantac303862010-07-12 15:51:17 +00001366 explicit __empty_state(__node<_CharT>* __s)
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001367 : base(__s) {}
1368
Howard Hinnantac303862010-07-12 15:51:17 +00001369 virtual void __exec(__state&) const;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001370
1371 virtual string speak() const {return "empty state";}
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001372};
1373
1374template <class _CharT>
Howard Hinnantac303862010-07-12 15:51:17 +00001375void
1376__empty_state<_CharT>::__exec(__state& __s) const
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001377{
Howard Hinnantac303862010-07-12 15:51:17 +00001378 __s.__do_ = __state::__accept_but_not_consume;
1379 __s.__node_ = this->first();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001380}
1381
1382// __empty_non_own_state
1383
1384template <class _CharT>
1385class __empty_non_own_state
1386 : public __has_one_state<_CharT>
1387{
1388 typedef __has_one_state<_CharT> base;
1389
1390public:
Howard Hinnantac303862010-07-12 15:51:17 +00001391 typedef _STD::__state<_CharT> __state;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001392
Howard Hinnantac303862010-07-12 15:51:17 +00001393 explicit __empty_non_own_state(__node<_CharT>* __s)
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001394 : base(__s) {}
1395
Howard Hinnantac303862010-07-12 15:51:17 +00001396 virtual void __exec(__state&) const;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001397
1398 virtual string speak() const {return "empty non-owning state";}
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001399};
1400
1401template <class _CharT>
Howard Hinnantac303862010-07-12 15:51:17 +00001402void
1403__empty_non_own_state<_CharT>::__exec(__state& __s) const
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001404{
Howard Hinnantac303862010-07-12 15:51:17 +00001405 __s.__do_ = __state::__accept_but_not_consume;
1406 __s.__node_ = this->first();
1407}
1408
1409// __repeat_one_loop
1410
1411template <class _CharT>
1412class __repeat_one_loop
1413 : public __has_one_state<_CharT>
1414{
1415 typedef __has_one_state<_CharT> base;
1416
1417public:
1418 typedef _STD::__state<_CharT> __state;
1419
1420 explicit __repeat_one_loop(__node<_CharT>* __s)
1421 : base(__s) {}
1422
1423 virtual void __exec(__state&) const;
1424
1425 virtual string speak() const {return "repeat loop";}
1426};
1427
1428template <class _CharT>
1429void
1430__repeat_one_loop<_CharT>::__exec(__state& __s) const
1431{
1432 __s.__do_ = __state::__repeat;
1433 __s.__node_ = this->first();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001434}
1435
1436// __owns_two_states
1437
1438template <class _CharT>
1439class __owns_two_states
1440 : public __owns_one_state<_CharT>
1441{
1442 typedef __owns_one_state<_CharT> base;
1443
1444 base* __second_;
1445
1446public:
Howard Hinnantac303862010-07-12 15:51:17 +00001447 explicit __owns_two_states(__node<_CharT>* __s1, base* __s2)
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001448 : base(__s1), __second_(__s2) {}
1449
1450 virtual ~__owns_two_states();
1451
1452 base* second() const {return __second_;}
1453 base*& second() {return __second_;}
1454};
1455
1456template <class _CharT>
1457__owns_two_states<_CharT>::~__owns_two_states()
1458{
1459 delete __second_;
1460}
1461
1462// __loop
1463
1464template <class _CharT>
1465class __loop
1466 : public __owns_two_states<_CharT>
1467{
1468 typedef __owns_two_states<_CharT> base;
1469
1470 size_t __min_;
1471 size_t __max_;
1472 unsigned __loop_id_;
Howard Hinnantac303862010-07-12 15:51:17 +00001473 unsigned __mexp_begin_;
1474 unsigned __mexp_end_;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001475 bool __greedy_;
1476
1477public:
Howard Hinnantac303862010-07-12 15:51:17 +00001478 typedef _STD::__state<_CharT> __state;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001479
1480 explicit __loop(unsigned __loop_id,
Howard Hinnantac303862010-07-12 15:51:17 +00001481 __node<_CharT>* __s1, __owns_one_state<_CharT>* __s2,
1482 unsigned __mexp_begin, unsigned __mexp_end,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001483 bool __greedy = true,
1484 size_t __min = 0,
1485 size_t __max = numeric_limits<size_t>::max())
1486 : base(__s1, __s2), __min_(__min), __max_(__max), __loop_id_(__loop_id),
Howard Hinnantac303862010-07-12 15:51:17 +00001487 __mexp_begin_(__mexp_begin), __mexp_end_(__mexp_end),
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001488 __greedy_(__greedy) {}
1489
Howard Hinnantac303862010-07-12 15:51:17 +00001490 virtual void __exec(__state& __s) const;
1491 virtual void __exec_split(bool __second, __state& __s) const;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001492
1493 virtual string speak() const
1494 {
1495 ostringstream os;
Howard Hinnantac303862010-07-12 15:51:17 +00001496 os << "loop "<< __loop_id_ << " {" << __min_ << ',' << __max_ << "}";
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001497 if (!__greedy_)
1498 os << " not";
1499 os << " greedy";
1500 return os.str();
1501 }
Howard Hinnantac303862010-07-12 15:51:17 +00001502
1503private:
1504 void __init_repeat(__state& __s) const
1505 {
1506 __s.__loop_data_[__loop_id_].second = __s.__current_;
1507 for (size_t __i = __mexp_begin_-1; __i != __mexp_end_-1; ++__i)
1508 {
1509 __s.__sub_matches_[__i].first = __s.__last_;
1510 __s.__sub_matches_[__i].second = __s.__last_;
1511 __s.__sub_matches_[__i].matched = false;
1512 }
1513 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001514};
1515
1516template <class _CharT>
Howard Hinnantac303862010-07-12 15:51:17 +00001517void
1518__loop<_CharT>::__exec(__state& __s) const
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001519{
Howard Hinnantac303862010-07-12 15:51:17 +00001520 if (__s.__do_ == __state::__repeat)
1521 {
1522 bool __do_repeat = ++__s.__loop_data_[__loop_id_].first < __max_;
1523 bool __do_alt = __s.__loop_data_[__loop_id_].first >= __min_;
1524 if (__do_repeat && __do_alt &&
1525 __s.__loop_data_[__loop_id_].second == __s.__current_)
1526 __do_repeat = false;
1527 if (__do_repeat && __do_alt)
1528 __s.__do_ = __state::__split;
1529 else if (__do_repeat)
1530 {
1531 __s.__do_ = __state::__accept_but_not_consume;
1532 __s.__node_ = this->first();
1533 __init_repeat(__s);
1534 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001535 else
Howard Hinnantac303862010-07-12 15:51:17 +00001536 {
1537 __s.__do_ = __state::__accept_but_not_consume;
1538 __s.__node_ = this->second();
1539 }
1540 }
1541 else
1542 {
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00001543 __s.__loop_data_[__loop_id_].first = 0;
1544 bool __do_repeat = 0 < __max_;
1545 bool __do_alt = 0 >= __min_;
1546 if (__do_repeat && __do_alt)
Howard Hinnantac303862010-07-12 15:51:17 +00001547 __s.__do_ = __state::__split;
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00001548 else if (__do_repeat)
1549 {
1550 __s.__do_ = __state::__accept_but_not_consume;
1551 __s.__node_ = this->first();
1552 __init_repeat(__s);
1553 }
Howard Hinnantac303862010-07-12 15:51:17 +00001554 else
1555 {
1556 __s.__do_ = __state::__accept_but_not_consume;
1557 __s.__node_ = this->second();
1558 }
1559 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001560}
1561
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001562template <class _CharT>
Howard Hinnantac303862010-07-12 15:51:17 +00001563void
1564__loop<_CharT>::__exec_split(bool __second, __state& __s) const
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001565{
Howard Hinnantac303862010-07-12 15:51:17 +00001566 __s.__do_ = __state::__accept_but_not_consume;
1567 if (__greedy_ != __second)
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001568 {
Howard Hinnantac303862010-07-12 15:51:17 +00001569 __s.__node_ = this->first();
1570 __init_repeat(__s);
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001571 }
Howard Hinnantac303862010-07-12 15:51:17 +00001572 else
1573 __s.__node_ = this->second();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001574}
1575
Howard Hinnantaa698082010-07-16 19:08:36 +00001576// __alternate
1577
1578template <class _CharT>
1579class __alternate
1580 : public __owns_two_states<_CharT>
1581{
1582 typedef __owns_two_states<_CharT> base;
1583
1584public:
1585 typedef _STD::__state<_CharT> __state;
1586
1587 explicit __alternate(__owns_one_state<_CharT>* __s1,
1588 __owns_one_state<_CharT>* __s2)
1589 : base(__s1, __s2) {}
1590
1591 virtual void __exec(__state& __s) const;
1592 virtual void __exec_split(bool __second, __state& __s) const;
1593
1594 virtual string speak() const
1595 {
1596 ostringstream os;
1597 os << "__alternate";
1598 return os.str();
1599 }
1600};
1601
1602template <class _CharT>
1603void
1604__alternate<_CharT>::__exec(__state& __s) const
1605{
1606 __s.__do_ = __state::__split;
1607}
1608
1609template <class _CharT>
1610void
1611__alternate<_CharT>::__exec_split(bool __second, __state& __s) const
1612{
1613 __s.__do_ = __state::__accept_but_not_consume;
Howard Hinnant1371b2e2010-07-22 14:12:20 +00001614 if (__second)
Howard Hinnantaa698082010-07-16 19:08:36 +00001615 __s.__node_ = this->second();
Howard Hinnant1371b2e2010-07-22 14:12:20 +00001616 else
1617 __s.__node_ = this->first();
Howard Hinnantaa698082010-07-16 19:08:36 +00001618}
1619
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001620// __begin_marked_subexpression
1621
1622template <class _CharT>
1623class __begin_marked_subexpression
1624 : public __owns_one_state<_CharT>
1625{
1626 typedef __owns_one_state<_CharT> base;
1627
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001628 unsigned __mexp_;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001629public:
Howard Hinnantac303862010-07-12 15:51:17 +00001630 typedef _STD::__state<_CharT> __state;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001631
Howard Hinnantac303862010-07-12 15:51:17 +00001632 explicit __begin_marked_subexpression(unsigned __mexp, __node<_CharT>* __s)
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001633 : base(__s), __mexp_(__mexp) {}
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001634
Howard Hinnantac303862010-07-12 15:51:17 +00001635 virtual void __exec(__state&) const;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001636
1637 virtual string speak() const
1638 {
1639 ostringstream os;
1640 os << "begin marked expr " << __mexp_;
1641 return os.str();
1642 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001643};
1644
1645template <class _CharT>
Howard Hinnantac303862010-07-12 15:51:17 +00001646void
1647__begin_marked_subexpression<_CharT>::__exec(__state& __s) const
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001648{
Howard Hinnantac303862010-07-12 15:51:17 +00001649 __s.__do_ = __state::__accept_but_not_consume;
1650 __s.__sub_matches_[__mexp_-1].first = __s.__current_;
1651 __s.__node_ = this->first();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001652}
1653
1654// __end_marked_subexpression
1655
1656template <class _CharT>
1657class __end_marked_subexpression
1658 : public __owns_one_state<_CharT>
1659{
1660 typedef __owns_one_state<_CharT> base;
1661
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001662 unsigned __mexp_;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001663public:
Howard Hinnantac303862010-07-12 15:51:17 +00001664 typedef _STD::__state<_CharT> __state;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001665
Howard Hinnantac303862010-07-12 15:51:17 +00001666 explicit __end_marked_subexpression(unsigned __mexp, __node<_CharT>* __s)
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001667 : base(__s), __mexp_(__mexp) {}
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001668
Howard Hinnantac303862010-07-12 15:51:17 +00001669 virtual void __exec(__state&) const;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001670
1671 virtual string speak() const
1672 {
1673 ostringstream os;
1674 os << "end marked expr " << __mexp_;
1675 return os.str();
1676 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001677};
1678
1679template <class _CharT>
Howard Hinnantac303862010-07-12 15:51:17 +00001680void
1681__end_marked_subexpression<_CharT>::__exec(__state& __s) const
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001682{
Howard Hinnantac303862010-07-12 15:51:17 +00001683 __s.__do_ = __state::__accept_but_not_consume;
1684 __s.__sub_matches_[__mexp_-1].second = __s.__current_;
1685 __s.__sub_matches_[__mexp_-1].matched = true;
1686 __s.__node_ = this->first();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001687}
1688
Howard Hinnantcba352d2010-07-12 18:16:05 +00001689// __back_ref
1690
1691template <class _CharT>
1692class __back_ref
1693 : public __owns_one_state<_CharT>
1694{
1695 typedef __owns_one_state<_CharT> base;
1696
1697 unsigned __mexp_;
1698public:
1699 typedef _STD::__state<_CharT> __state;
1700
1701 explicit __back_ref(unsigned __mexp, __node<_CharT>* __s)
1702 : base(__s), __mexp_(__mexp) {}
1703
1704 virtual void __exec(__state&) const;
1705
1706 virtual string speak() const
1707 {
1708 ostringstream os;
1709 os << "__back_ref " << __mexp_;
1710 return os.str();
1711 }
1712};
1713
1714template <class _CharT>
1715void
1716__back_ref<_CharT>::__exec(__state& __s) const
1717{
1718 sub_match<const _CharT*>& __sm = __s.__sub_matches_[__mexp_-1];
1719 if (__sm.matched)
1720 {
1721 ptrdiff_t __len = __sm.second - __sm.first;
1722 if (__s.__last_ - __s.__current_ >= __len &&
1723 _STD::equal(__sm.first, __sm.second, __s.__current_))
1724 {
1725 __s.__do_ = __state::__accept_but_not_consume;
1726 __s.__current_ += __len;
1727 __s.__node_ = this->first();
1728 }
1729 else
1730 {
1731 __s.__do_ = __state::__reject;
1732 __s.__node_ = nullptr;
1733 }
1734 }
1735 else
1736 {
1737 __s.__do_ = __state::__reject;
1738 __s.__node_ = nullptr;
1739 }
1740}
1741
Howard Hinnante34f17d2010-07-12 19:11:27 +00001742// __back_ref_icase
1743
1744template <class _CharT, class _Traits>
1745class __back_ref_icase
1746 : public __owns_one_state<_CharT>
1747{
1748 typedef __owns_one_state<_CharT> base;
1749
1750 _Traits __traits_;
1751 unsigned __mexp_;
1752public:
1753 typedef _STD::__state<_CharT> __state;
1754
1755 explicit __back_ref_icase(const _Traits& __traits, unsigned __mexp,
1756 __node<_CharT>* __s)
1757 : base(__s), __traits_(__traits), __mexp_(__mexp) {}
1758
1759 virtual void __exec(__state&) const;
1760
1761 virtual string speak() const
1762 {
1763 ostringstream os;
1764 os << "__back_ref_icase " << __mexp_;
1765 return os.str();
1766 }
1767};
1768
1769template <class _CharT, class _Traits>
1770void
1771__back_ref_icase<_CharT, _Traits>::__exec(__state& __s) const
1772{
1773 sub_match<const _CharT*>& __sm = __s.__sub_matches_[__mexp_-1];
1774 if (__sm.matched)
1775 {
1776 ptrdiff_t __len = __sm.second - __sm.first;
1777 if (__s.__last_ - __s.__current_ >= __len)
1778 {
1779 for (ptrdiff_t __i = 0; __i < __len; ++__i)
1780 {
1781 if (__traits_.translate_nocase(__sm.first[__i]) !=
1782 __traits_.translate_nocase(__s.__current_[__i]))
1783 goto __not_equal;
1784 }
1785 __s.__do_ = __state::__accept_but_not_consume;
1786 __s.__current_ += __len;
1787 __s.__node_ = this->first();
1788 }
1789 else
1790 {
1791 __s.__do_ = __state::__reject;
1792 __s.__node_ = nullptr;
1793 }
1794 }
1795 else
1796 {
1797__not_equal:
1798 __s.__do_ = __state::__reject;
1799 __s.__node_ = nullptr;
1800 }
1801}
1802
1803// __back_ref_collate
1804
1805template <class _CharT, class _Traits>
1806class __back_ref_collate
1807 : public __owns_one_state<_CharT>
1808{
1809 typedef __owns_one_state<_CharT> base;
1810
1811 _Traits __traits_;
1812 unsigned __mexp_;
1813public:
1814 typedef _STD::__state<_CharT> __state;
1815
1816 explicit __back_ref_collate(const _Traits& __traits, unsigned __mexp,
1817 __node<_CharT>* __s)
1818 : base(__s), __traits_(__traits), __mexp_(__mexp) {}
1819
1820 virtual void __exec(__state&) const;
1821
1822 virtual string speak() const
1823 {
1824 ostringstream os;
1825 os << "__back_ref_collate " << __mexp_;
1826 return os.str();
1827 }
1828};
1829
1830template <class _CharT, class _Traits>
1831void
1832__back_ref_collate<_CharT, _Traits>::__exec(__state& __s) const
1833{
1834 sub_match<const _CharT*>& __sm = __s.__sub_matches_[__mexp_-1];
1835 if (__sm.matched)
1836 {
1837 ptrdiff_t __len = __sm.second - __sm.first;
1838 if (__s.__last_ - __s.__current_ >= __len)
1839 {
1840 for (ptrdiff_t __i = 0; __i < __len; ++__i)
1841 {
1842 if (__traits_.translate(__sm.first[__i]) !=
1843 __traits_.translate(__s.__current_[__i]))
1844 goto __not_equal;
1845 }
1846 __s.__do_ = __state::__accept_but_not_consume;
1847 __s.__current_ += __len;
1848 __s.__node_ = this->first();
1849 }
1850 else
1851 {
1852 __s.__do_ = __state::__reject;
1853 __s.__node_ = nullptr;
1854 }
1855 }
1856 else
1857 {
1858__not_equal:
1859 __s.__do_ = __state::__reject;
1860 __s.__node_ = nullptr;
1861 }
1862}
1863
Howard Hinnant17615b02010-07-27 01:25:38 +00001864// __word_boundary
1865
1866template <class _CharT, class _Traits>
1867class __word_boundary
1868 : public __owns_one_state<_CharT>
1869{
1870 typedef __owns_one_state<_CharT> base;
1871
1872 _Traits __traits_;
1873 bool __invert_;
1874public:
1875 typedef _STD::__state<_CharT> __state;
1876
1877 explicit __word_boundary(const _Traits& __traits, bool __invert,
1878 __node<_CharT>* __s)
1879 : base(__s), __traits_(__traits), __invert_(__invert) {}
1880
1881 virtual void __exec(__state&) const;
1882
1883 virtual string speak() const
1884 {
1885 ostringstream os;
Howard Hinnant8daa7332010-07-29 01:15:27 +00001886 if (!__invert_)
Howard Hinnant17615b02010-07-27 01:25:38 +00001887 os << "__word_boundary";
1888 else
1889 os << "not __word_boundary";
1890 return os.str();
1891 }
1892};
1893
1894template <class _CharT, class _Traits>
1895void
1896__word_boundary<_CharT, _Traits>::__exec(__state& __s) const
1897{
1898 bool __is_word_b = false;
1899 if (__s.__first_ != __s.__last_)
1900 {
1901 if (__s.__current_ == __s.__last_)
1902 {
1903 if (!(__s.__flags_ & regex_constants::match_not_eow))
1904 {
1905 _CharT __c = __s.__current_[-1];
1906 __is_word_b = __c == '_' ||
1907 __traits_.isctype(__c, ctype_base::alnum);
1908 }
1909 }
Howard Hinnantf3dcca02010-07-29 15:17:28 +00001910 else if (__s.__current_ == __s.__first_ &&
1911 !(__s.__flags_ & regex_constants::match_prev_avail))
Howard Hinnant17615b02010-07-27 01:25:38 +00001912 {
1913 if (!(__s.__flags_ & regex_constants::match_not_bow))
1914 {
1915 _CharT __c = *__s.__current_;
1916 __is_word_b = __c == '_' ||
1917 __traits_.isctype(__c, ctype_base::alnum);
1918 }
1919 }
1920 else
1921 {
1922 _CharT __c1 = __s.__current_[-1];
1923 _CharT __c2 = *__s.__current_;
1924 bool __is_c1_b = __c1 == '_' ||
1925 __traits_.isctype(__c1, ctype_base::alnum);
1926 bool __is_c2_b = __c2 == '_' ||
1927 __traits_.isctype(__c2, ctype_base::alnum);
1928 __is_word_b = __is_c1_b != __is_c2_b;
1929 }
1930 }
1931 if (__is_word_b != __invert_)
1932 {
1933 __s.__do_ = __state::__accept_but_not_consume;
1934 __s.__node_ = this->first();
1935 }
1936 else
1937 {
1938 __s.__do_ = __state::__reject;
1939 __s.__node_ = nullptr;
1940 }
1941}
1942
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001943// __r_anchor
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001944
1945template <class _CharT>
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001946class __r_anchor
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001947 : public __owns_one_state<_CharT>
1948{
1949 typedef __owns_one_state<_CharT> base;
1950
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001951public:
Howard Hinnantac303862010-07-12 15:51:17 +00001952 typedef _STD::__state<_CharT> __state;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001953
Howard Hinnantac303862010-07-12 15:51:17 +00001954 __r_anchor(__node<_CharT>* __s)
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001955 : base(__s) {}
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001956
Howard Hinnantac303862010-07-12 15:51:17 +00001957 virtual void __exec(__state&) const;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001958
1959 virtual string speak() const
1960 {
1961 ostringstream os;
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001962 os << "right anchor";
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001963 return os.str();
1964 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001965};
1966
1967template <class _CharT>
Howard Hinnantac303862010-07-12 15:51:17 +00001968void
1969__r_anchor<_CharT>::__exec(__state& __s) const
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001970{
Howard Hinnantac303862010-07-12 15:51:17 +00001971 if (__s.__current_ == __s.__last_)
1972 {
1973 __s.__do_ = __state::__accept_but_not_consume;
1974 __s.__node_ = this->first();
1975 }
1976 else
1977 {
1978 __s.__do_ = __state::__reject;
1979 __s.__node_ = nullptr;
1980 }
1981}
1982
1983// __match_any
1984
1985template <class _CharT>
1986class __match_any
1987 : public __owns_one_state<_CharT>
1988{
1989 typedef __owns_one_state<_CharT> base;
1990
1991public:
1992 typedef _STD::__state<_CharT> __state;
1993
1994 __match_any(__node<_CharT>* __s)
1995 : base(__s) {}
1996
1997 virtual void __exec(__state&) const;
1998
1999 virtual string speak() const
2000 {
2001 ostringstream os;
2002 os << "match any";
2003 return os.str();
2004 }
2005};
2006
2007template <class _CharT>
2008void
2009__match_any<_CharT>::__exec(__state& __s) const
2010{
2011 if (__s.__current_ != __s.__last_ && *__s.__current_ != 0)
2012 {
2013 __s.__do_ = __state::__accept_and_consume;
2014 ++__s.__current_;
2015 __s.__node_ = this->first();
2016 }
2017 else
2018 {
2019 __s.__do_ = __state::__reject;
2020 __s.__node_ = nullptr;
2021 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002022}
2023
Howard Hinnant17615b02010-07-27 01:25:38 +00002024// __match_any_but_newline
2025
2026template <class _CharT>
2027class __match_any_but_newline
2028 : public __owns_one_state<_CharT>
2029{
2030 typedef __owns_one_state<_CharT> base;
2031
2032public:
2033 typedef _STD::__state<_CharT> __state;
2034
2035 __match_any_but_newline(__node<_CharT>* __s)
2036 : base(__s) {}
2037
2038 virtual void __exec(__state&) const;
2039
2040 virtual string speak() const
2041 {
2042 ostringstream os;
2043 os << "match any but newline";
2044 return os.str();
2045 }
2046};
2047
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002048// __match_char
2049
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00002050template <class _CharT>
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002051class __match_char
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002052 : public __owns_one_state<_CharT>
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002053{
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002054 typedef __owns_one_state<_CharT> base;
2055
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002056 _CharT __c_;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002057
2058 __match_char(const __match_char&);
2059 __match_char& operator=(const __match_char&);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002060public:
Howard Hinnantac303862010-07-12 15:51:17 +00002061 typedef _STD::__state<_CharT> __state;
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00002062
Howard Hinnantac303862010-07-12 15:51:17 +00002063 __match_char(_CharT __c, __node<_CharT>* __s)
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002064 : base(__s), __c_(__c) {}
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002065
Howard Hinnantac303862010-07-12 15:51:17 +00002066 virtual void __exec(__state&) const;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00002067
2068 virtual string speak() const
2069 {
2070 ostringstream os;
2071 os << "match char " << __c_;
2072 return os.str();
2073 }
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002074};
2075
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00002076template <class _CharT>
Howard Hinnantac303862010-07-12 15:51:17 +00002077void
2078__match_char<_CharT>::__exec(__state& __s) const
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00002079{
Howard Hinnantac303862010-07-12 15:51:17 +00002080 if (__s.__current_ != __s.__last_ && *__s.__current_ == __c_)
2081 {
2082 __s.__do_ = __state::__accept_and_consume;
2083 ++__s.__current_;
2084 __s.__node_ = this->first();
2085 }
2086 else
2087 {
2088 __s.__do_ = __state::__reject;
2089 __s.__node_ = nullptr;
2090 }
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00002091}
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00002092
Howard Hinnante34f17d2010-07-12 19:11:27 +00002093// __match_char_icase
2094
2095template <class _CharT, class _Traits>
2096class __match_char_icase
2097 : public __owns_one_state<_CharT>
2098{
2099 typedef __owns_one_state<_CharT> base;
2100
2101 _Traits __traits_;
2102 _CharT __c_;
2103
2104 __match_char_icase(const __match_char_icase&);
2105 __match_char_icase& operator=(const __match_char_icase&);
2106public:
2107 typedef _STD::__state<_CharT> __state;
2108
2109 __match_char_icase(const _Traits& __traits, _CharT __c, __node<_CharT>* __s)
2110 : base(__s), __traits_(__traits), __c_(__traits.translate_nocase(__c)) {}
2111
2112 virtual void __exec(__state&) const;
2113
2114 virtual string speak() const
2115 {
2116 ostringstream os;
2117 os << "match char icase " << __c_;
2118 return os.str();
2119 }
2120};
2121
2122template <class _CharT, class _Traits>
2123void
2124__match_char_icase<_CharT, _Traits>::__exec(__state& __s) const
2125{
2126 if (__s.__current_ != __s.__last_ &&
2127 __traits_.translate_nocase(*__s.__current_) == __c_)
2128 {
2129 __s.__do_ = __state::__accept_and_consume;
2130 ++__s.__current_;
2131 __s.__node_ = this->first();
2132 }
2133 else
2134 {
2135 __s.__do_ = __state::__reject;
2136 __s.__node_ = nullptr;
2137 }
2138}
2139
2140// __match_char_collate
2141
2142template <class _CharT, class _Traits>
2143class __match_char_collate
2144 : public __owns_one_state<_CharT>
2145{
2146 typedef __owns_one_state<_CharT> base;
2147
2148 _Traits __traits_;
2149 _CharT __c_;
2150
2151 __match_char_collate(const __match_char_collate&);
2152 __match_char_collate& operator=(const __match_char_collate&);
2153public:
2154 typedef _STD::__state<_CharT> __state;
2155
2156 __match_char_collate(const _Traits& __traits, _CharT __c, __node<_CharT>* __s)
2157 : base(__s), __traits_(__traits), __c_(__traits.translate(__c)) {}
2158
2159 virtual void __exec(__state&) const;
2160
2161 virtual string speak() const
2162 {
2163 ostringstream os;
2164 os << "match char icase " << __c_;
2165 return os.str();
2166 }
2167};
2168
2169template <class _CharT, class _Traits>
2170void
2171__match_char_collate<_CharT, _Traits>::__exec(__state& __s) const
2172{
2173 if (__s.__current_ != __s.__last_ &&
2174 __traits_.translate(*__s.__current_) == __c_)
2175 {
2176 __s.__do_ = __state::__accept_and_consume;
2177 ++__s.__current_;
2178 __s.__node_ = this->first();
2179 }
2180 else
2181 {
2182 __s.__do_ = __state::__reject;
2183 __s.__node_ = nullptr;
2184 }
2185}
2186
Howard Hinnant173968a2010-07-13 21:48:06 +00002187// __bracket_expression
2188
2189template <class _CharT, class _Traits>
2190class __bracket_expression
2191 : public __owns_one_state<_CharT>
2192{
2193 typedef __owns_one_state<_CharT> base;
2194 typedef typename _Traits::string_type string_type;
2195
2196 _Traits __traits_;
2197 vector<_CharT> __chars_;
Howard Hinnant15476f32010-07-28 17:35:27 +00002198 vector<_CharT> __neg_chars_;
Howard Hinnant173968a2010-07-13 21:48:06 +00002199 vector<pair<string_type, string_type> > __ranges_;
2200 vector<pair<_CharT, _CharT> > __digraphs_;
2201 vector<string_type> __equivalences_;
2202 ctype_base::mask __mask_;
Howard Hinnant15476f32010-07-28 17:35:27 +00002203 ctype_base::mask __neg_mask_;
Howard Hinnant173968a2010-07-13 21:48:06 +00002204 bool __negate_;
2205 bool __icase_;
2206 bool __collate_;
Howard Hinnant68025ed2010-07-14 15:45:11 +00002207 bool __might_have_digraph_;
Howard Hinnant173968a2010-07-13 21:48:06 +00002208
2209 __bracket_expression(const __bracket_expression&);
2210 __bracket_expression& operator=(const __bracket_expression&);
2211public:
2212 typedef _STD::__state<_CharT> __state;
2213
2214 __bracket_expression(const _Traits& __traits, __node<_CharT>* __s,
2215 bool __negate, bool __icase, bool __collate)
Howard Hinnant15476f32010-07-28 17:35:27 +00002216 : base(__s), __traits_(__traits), __mask_(), __neg_mask_(),
2217 __negate_(__negate), __icase_(__icase), __collate_(__collate),
Howard Hinnant68025ed2010-07-14 15:45:11 +00002218 __might_have_digraph_(__traits_.getloc().name() != "C") {}
Howard Hinnant173968a2010-07-13 21:48:06 +00002219
2220 virtual void __exec(__state&) const;
2221
Howard Hinnant15476f32010-07-28 17:35:27 +00002222 bool __negated() const {return __negate_;}
2223
Howard Hinnant173968a2010-07-13 21:48:06 +00002224 void __add_char(_CharT __c)
2225 {
2226 if (__icase_)
2227 __chars_.push_back(__traits_.translate_nocase(__c));
2228 else if (__collate_)
2229 __chars_.push_back(__traits_.translate(__c));
2230 else
2231 __chars_.push_back(__c);
2232 }
Howard Hinnant15476f32010-07-28 17:35:27 +00002233 void __add_neg_char(_CharT __c)
2234 {
2235 if (__icase_)
2236 __neg_chars_.push_back(__traits_.translate_nocase(__c));
2237 else if (__collate_)
2238 __neg_chars_.push_back(__traits_.translate(__c));
2239 else
2240 __neg_chars_.push_back(__c);
2241 }
Howard Hinnant173968a2010-07-13 21:48:06 +00002242 void __add_range(string_type __b, string_type __e)
2243 {
2244 if (__collate_)
2245 {
2246 if (__icase_)
2247 {
2248 for (size_t __i = 0; __i < __b.size(); ++__i)
2249 __b[__i] = __traits_.translate_nocase(__b[__i]);
2250 for (size_t __i = 0; __i < __e.size(); ++__i)
2251 __e[__i] = __traits_.translate_nocase(__e[__i]);
2252 }
2253 else
2254 {
2255 for (size_t __i = 0; __i < __b.size(); ++__i)
2256 __b[__i] = __traits_.translate(__b[__i]);
2257 for (size_t __i = 0; __i < __e.size(); ++__i)
2258 __e[__i] = __traits_.translate(__e[__i]);
2259 }
2260 __ranges_.push_back(make_pair(
2261 __traits_.transform(__b.begin(), __b.end()),
2262 __traits_.transform(__e.begin(), __e.end())));
2263 }
2264 else
2265 {
Howard Hinnantd4444702010-08-11 17:04:31 +00002266#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant173968a2010-07-13 21:48:06 +00002267 if (__b.size() != 1 || __e.size() != 1)
2268 throw regex_error(regex_constants::error_collate);
Howard Hinnant324bb032010-08-22 00:02:43 +00002269#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant173968a2010-07-13 21:48:06 +00002270 if (__icase_)
2271 {
2272 __b[0] = __traits_.translate_nocase(__b[0]);
2273 __e[0] = __traits_.translate_nocase(__e[0]);
2274 }
2275 __ranges_.push_back(make_pair(_STD::move(__b), _STD::move(__e)));
2276 }
2277 }
2278 void __add_digraph(_CharT __c1, _CharT __c2)
2279 {
2280 if (__icase_)
2281 __digraphs_.push_back(make_pair(__traits_.translate_nocase(__c1),
2282 __traits_.translate_nocase(__c2)));
2283 else if (__collate_)
2284 __digraphs_.push_back(make_pair(__traits_.translate(__c1),
2285 __traits_.translate(__c2)));
2286 else
2287 __digraphs_.push_back(make_pair(__c1, __c2));
2288 }
2289 void __add_equivalence(const string_type& __s)
2290 {__equivalences_.push_back(__s);}
2291 void __add_class(ctype_base::mask __mask)
2292 {__mask_ |= __mask;}
Howard Hinnant15476f32010-07-28 17:35:27 +00002293 void __add_neg_class(ctype_base::mask __mask)
2294 {__neg_mask_ |= __mask;}
Howard Hinnant173968a2010-07-13 21:48:06 +00002295
2296 virtual string speak() const
2297 {
2298 ostringstream os;
2299 os << "__bracket_expression ";
2300 return os.str();
2301 }
2302};
2303
2304template <class _CharT, class _Traits>
2305void
2306__bracket_expression<_CharT, _Traits>::__exec(__state& __s) const
2307{
2308 bool __found = false;
2309 unsigned __consumed = 0;
2310 if (__s.__current_ != __s.__last_)
2311 {
2312 ++__consumed;
Howard Hinnant68025ed2010-07-14 15:45:11 +00002313 if (__might_have_digraph_)
Howard Hinnant173968a2010-07-13 21:48:06 +00002314 {
Howard Hinnant68025ed2010-07-14 15:45:11 +00002315 const _CharT* __next = next(__s.__current_);
2316 if (__next != __s.__last_)
Howard Hinnant173968a2010-07-13 21:48:06 +00002317 {
Howard Hinnant68025ed2010-07-14 15:45:11 +00002318 pair<_CharT, _CharT> __ch2(*__s.__current_, *__next);
2319 if (__icase_)
Howard Hinnant173968a2010-07-13 21:48:06 +00002320 {
Howard Hinnant68025ed2010-07-14 15:45:11 +00002321 __ch2.first = __traits_.translate_nocase(__ch2.first);
2322 __ch2.second = __traits_.translate_nocase(__ch2.second);
2323 }
2324 else if (__collate_)
2325 {
2326 __ch2.first = __traits_.translate(__ch2.first);
2327 __ch2.second = __traits_.translate(__ch2.second);
2328 }
2329 if (!__traits_.lookup_collatename(&__ch2.first, &__ch2.first+2).empty())
2330 {
2331 // __ch2 is a digraph in this locale
2332 ++__consumed;
2333 for (size_t __i = 0; __i < __digraphs_.size(); ++__i)
2334 {
2335 if (__ch2 == __digraphs_[__i])
2336 {
2337 __found = true;
2338 goto __exit;
2339 }
2340 }
2341 if (__collate_ && !__ranges_.empty())
2342 {
2343 string_type __s2 = __traits_.transform(&__ch2.first,
2344 &__ch2.first + 2);
2345 for (size_t __i = 0; __i < __ranges_.size(); ++__i)
2346 {
2347 if (__ranges_[__i].first <= __s2 &&
2348 __s2 <= __ranges_[__i].second)
2349 {
2350 __found = true;
2351 goto __exit;
2352 }
2353 }
2354 }
2355 if (!__equivalences_.empty())
2356 {
2357 string_type __s2 = __traits_.transform_primary(&__ch2.first,
2358 &__ch2.first + 2);
2359 for (size_t __i = 0; __i < __equivalences_.size(); ++__i)
2360 {
2361 if (__s2 == __equivalences_[__i])
2362 {
2363 __found = true;
2364 goto __exit;
2365 }
2366 }
2367 }
2368 if (__traits_.isctype(__ch2.first, __mask_) &&
2369 __traits_.isctype(__ch2.second, __mask_))
Howard Hinnant173968a2010-07-13 21:48:06 +00002370 {
2371 __found = true;
2372 goto __exit;
2373 }
Howard Hinnant15476f32010-07-28 17:35:27 +00002374 if (!__traits_.isctype(__ch2.first, __neg_mask_) &&
2375 !__traits_.isctype(__ch2.second, __neg_mask_))
2376 {
2377 __found = true;
2378 goto __exit;
2379 }
Howard Hinnant173968a2010-07-13 21:48:06 +00002380 goto __exit;
2381 }
Howard Hinnant173968a2010-07-13 21:48:06 +00002382 }
2383 }
2384 // test *__s.__current_ as not a digraph
2385 _CharT __ch = *__s.__current_;
2386 if (__icase_)
2387 __ch = __traits_.translate_nocase(__ch);
2388 else if (__collate_)
2389 __ch = __traits_.translate(__ch);
2390 for (size_t __i = 0; __i < __chars_.size(); ++__i)
2391 {
2392 if (__ch == __chars_[__i])
2393 {
2394 __found = true;
2395 goto __exit;
2396 }
2397 }
Howard Hinnant15476f32010-07-28 17:35:27 +00002398 if (!__neg_chars_.empty())
2399 {
2400 for (size_t __i = 0; __i < __neg_chars_.size(); ++__i)
2401 {
2402 if (__ch == __neg_chars_[__i])
2403 goto __is_neg_char;
2404 }
2405 __found = true;
2406 goto __exit;
2407 }
2408__is_neg_char:
Howard Hinnant173968a2010-07-13 21:48:06 +00002409 if (!__ranges_.empty())
2410 {
2411 string_type __s2 = __collate_ ?
2412 __traits_.transform(&__ch, &__ch + 1) :
2413 string_type(1, __ch);
2414 for (size_t __i = 0; __i < __ranges_.size(); ++__i)
2415 {
2416 if (__ranges_[__i].first <= __s2 && __s2 <= __ranges_[__i].second)
2417 {
2418 __found = true;
2419 goto __exit;
2420 }
2421 }
2422 }
2423 if (!__equivalences_.empty())
2424 {
2425 string_type __s2 = __traits_.transform_primary(&__ch, &__ch + 1);
2426 for (size_t __i = 0; __i < __equivalences_.size(); ++__i)
2427 {
2428 if (__s2 == __equivalences_[__i])
2429 {
2430 __found = true;
2431 goto __exit;
2432 }
2433 }
2434 }
2435 if (__traits_.isctype(__ch, __mask_))
Howard Hinnant15476f32010-07-28 17:35:27 +00002436 {
Howard Hinnant173968a2010-07-13 21:48:06 +00002437 __found = true;
Howard Hinnant15476f32010-07-28 17:35:27 +00002438 goto __exit;
2439 }
2440 if (__neg_mask_ && !__traits_.isctype(__ch, __neg_mask_))
2441 {
2442 __found = true;
2443 goto __exit;
2444 }
Howard Hinnant173968a2010-07-13 21:48:06 +00002445 }
2446 else
2447 __found = __negate_; // force reject
2448__exit:
2449 if (__found != __negate_)
2450 {
Howard Hinnant173968a2010-07-13 21:48:06 +00002451 __s.__do_ = __state::__accept_and_consume;
2452 __s.__current_ += __consumed;
2453 __s.__node_ = this->first();
2454 }
2455 else
2456 {
2457 __s.__do_ = __state::__reject;
2458 __s.__node_ = nullptr;
2459 }
2460}
2461
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002462template <class, class> class __lookahead;
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00002463
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002464template <class _CharT, class _Traits = regex_traits<_CharT> >
2465class basic_regex
2466{
2467public:
2468 // types:
2469 typedef _CharT value_type;
2470 typedef regex_constants::syntax_option_type flag_type;
2471 typedef typename _Traits::locale_type locale_type;
2472
2473private:
2474 _Traits __traits_;
2475 flag_type __flags_;
2476 unsigned __marked_count_;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002477 unsigned __loop_count_;
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002478 int __open_count_;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002479 shared_ptr<__empty_state<_CharT> > __start_;
2480 __owns_one_state<_CharT>* __end_;
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00002481 bool __left_anchor_;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002482
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00002483 typedef _STD::__state<_CharT> __state;
Howard Hinnantac303862010-07-12 15:51:17 +00002484 typedef _STD::__node<_CharT> __node;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002485
2486public:
2487 // constants:
2488 static const/*expr*/ regex_constants::syntax_option_type icase = regex_constants::icase;
2489 static const/*expr*/ regex_constants::syntax_option_type nosubs = regex_constants::nosubs;
2490 static const/*expr*/ regex_constants::syntax_option_type optimize = regex_constants::optimize;
2491 static const/*expr*/ regex_constants::syntax_option_type collate = regex_constants::collate;
2492 static const/*expr*/ regex_constants::syntax_option_type ECMAScript = regex_constants::ECMAScript;
2493 static const/*expr*/ regex_constants::syntax_option_type basic = regex_constants::basic;
2494 static const/*expr*/ regex_constants::syntax_option_type extended = regex_constants::extended;
2495 static const/*expr*/ regex_constants::syntax_option_type awk = regex_constants::awk;
2496 static const/*expr*/ regex_constants::syntax_option_type grep = regex_constants::grep;
2497 static const/*expr*/ regex_constants::syntax_option_type egrep = regex_constants::egrep;
2498
2499 // construct/copy/destroy:
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002500 basic_regex()
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00002501 : __flags_(), __marked_count_(0), __loop_count_(0), __open_count_(0),
2502 __end_(0), __left_anchor_(false)
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002503 {}
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002504 explicit basic_regex(const value_type* __p, flag_type __f = regex_constants::ECMAScript)
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00002505 : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
2506 __end_(0), __left_anchor_(false)
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002507 {__parse(__p, __p + __traits_.length(__p));}
2508 basic_regex(const value_type* __p, size_t __len, flag_type __f)
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00002509 : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
2510 __end_(0), __left_anchor_(false)
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002511 {__parse(__p, __p + __len);}
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002512// basic_regex(const basic_regex&) = default;
2513// basic_regex(basic_regex&&) = default;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002514 template <class _ST, class _SA>
2515 explicit basic_regex(const basic_string<value_type, _ST, _SA>& __p,
2516 flag_type __f = regex_constants::ECMAScript)
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00002517 : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
2518 __end_(0), __left_anchor_(false)
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002519 {__parse(__p.begin(), __p.end());}
2520 template <class _ForwardIterator>
2521 basic_regex(_ForwardIterator __first, _ForwardIterator __last,
2522 flag_type __f = regex_constants::ECMAScript)
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00002523 : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
2524 __end_(0), __left_anchor_(false)
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002525 {__parse(__first, __last);}
2526 basic_regex(initializer_list<value_type> __il,
2527 flag_type __f = regex_constants::ECMAScript)
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00002528 : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
2529 __end_(0), __left_anchor_(false)
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002530 {__parse(__il.begin(), __il.end());}
2531
Howard Hinnant7026a172010-08-13 18:11:23 +00002532// ~basic_regex() = default;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002533
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002534// basic_regex& operator=(const basic_regex&) = default;
2535// basic_regex& operator=(basic_regex&&) = default;
Howard Hinnant7026a172010-08-13 18:11:23 +00002536 basic_regex& operator=(const value_type* __p)
2537 {return assign(__p);}
2538 basic_regex& operator=(initializer_list<value_type> __il)
2539 {return assign(__il);}
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002540 template <class _ST, class _SA>
Howard Hinnant7026a172010-08-13 18:11:23 +00002541 basic_regex& operator=(const basic_string<value_type, _ST, _SA>& __p)
2542 {return assign(__p);}
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002543
2544 // assign:
Howard Hinnant7026a172010-08-13 18:11:23 +00002545 basic_regex& assign(const basic_regex& __that)
2546 {return *this = __that;}
2547 basic_regex& assign(const value_type* __p, flag_type __f = regex_constants::ECMAScript)
2548 {return assign(__p, __p + __traits_.length(__p), __f);}
2549 basic_regex& assign(const value_type* __p, size_t __len, flag_type __f)
2550 {return assign(__p, __p + __len, __f);}
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002551 template <class _ST, class _SA>
2552 basic_regex& assign(const basic_string<value_type, _ST, _SA>& __s,
Howard Hinnant7026a172010-08-13 18:11:23 +00002553 flag_type __f = regex_constants::ECMAScript)
2554 {return assign(__s.begin(), __s.end(), __f);}
2555
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002556 template <class _InputIterator>
Howard Hinnant7026a172010-08-13 18:11:23 +00002557 typename enable_if
2558 <
2559 __is_input_iterator <_InputIterator>::value &&
2560 !__is_forward_iterator<_InputIterator>::value,
2561 basic_regex&
2562 >::type
2563 assign(_InputIterator __first, _InputIterator __last,
2564 flag_type __f = regex_constants::ECMAScript)
2565 {
2566 basic_string<_CharT> __t(__first, __last);
2567 return assign(__t.begin(), __t.end(), __f);
2568 }
2569
2570private:
2571 void __member_init(flag_type __f)
2572 {
2573 __flags_ = __f;
2574 __marked_count_ = 0;
2575 __loop_count_ = 0;
2576 __open_count_ = 0;
2577 __end_ = nullptr;
2578 __left_anchor_ = false;
2579 }
2580public:
2581
2582 template <class _ForwardIterator>
2583 typename enable_if
2584 <
2585 __is_forward_iterator<_ForwardIterator>::value,
2586 basic_regex&
2587 >::type
2588 assign(_ForwardIterator __first, _ForwardIterator __last,
2589 flag_type __f = regex_constants::ECMAScript)
2590 {
2591 __member_init(__f);
2592 __parse(__first, __last);
2593 }
2594
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002595 basic_regex& assign(initializer_list<value_type> __il,
Howard Hinnant7026a172010-08-13 18:11:23 +00002596 flag_type __f = regex_constants::ECMAScript)
2597 {return assign(__il.begin(), __il.end(), __f);}
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002598
2599 // const operations:
2600 unsigned mark_count() const {return __marked_count_;}
2601 flag_type flags() const {return __flags_;}
2602
2603 // locale:
Howard Hinnant7026a172010-08-13 18:11:23 +00002604 locale_type imbue(locale_type __loc)
2605 {
2606 __member_init(ECMAScript);
2607 __start_.reset();
2608 return __traits_.imbue(__loc);
2609 }
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002610 locale_type getloc() const {return __traits_.getloc();}
2611
2612 // swap:
Howard Hinnant7026a172010-08-13 18:11:23 +00002613 void swap(basic_regex& __r);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002614
2615private:
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002616 unsigned __loop_count() const {return __loop_count_;}
2617
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002618 template <class _ForwardIterator>
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002619 _ForwardIterator
2620 __parse(_ForwardIterator __first, _ForwardIterator __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002621 template <class _ForwardIterator>
2622 _ForwardIterator
2623 __parse_basic_reg_exp(_ForwardIterator __first, _ForwardIterator __last);
2624 template <class _ForwardIterator>
2625 _ForwardIterator
2626 __parse_RE_expression(_ForwardIterator __first, _ForwardIterator __last);
2627 template <class _ForwardIterator>
2628 _ForwardIterator
2629 __parse_simple_RE(_ForwardIterator __first, _ForwardIterator __last);
2630 template <class _ForwardIterator>
2631 _ForwardIterator
2632 __parse_nondupl_RE(_ForwardIterator __first, _ForwardIterator __last);
2633 template <class _ForwardIterator>
2634 _ForwardIterator
2635 __parse_one_char_or_coll_elem_RE(_ForwardIterator __first, _ForwardIterator __last);
2636 template <class _ForwardIterator>
2637 _ForwardIterator
2638 __parse_Back_open_paren(_ForwardIterator __first, _ForwardIterator __last);
2639 template <class _ForwardIterator>
2640 _ForwardIterator
2641 __parse_Back_close_paren(_ForwardIterator __first, _ForwardIterator __last);
2642 template <class _ForwardIterator>
2643 _ForwardIterator
2644 __parse_Back_open_brace(_ForwardIterator __first, _ForwardIterator __last);
2645 template <class _ForwardIterator>
2646 _ForwardIterator
2647 __parse_Back_close_brace(_ForwardIterator __first, _ForwardIterator __last);
2648 template <class _ForwardIterator>
2649 _ForwardIterator
2650 __parse_BACKREF(_ForwardIterator __first, _ForwardIterator __last);
2651 template <class _ForwardIterator>
2652 _ForwardIterator
2653 __parse_ORD_CHAR(_ForwardIterator __first, _ForwardIterator __last);
2654 template <class _ForwardIterator>
2655 _ForwardIterator
2656 __parse_QUOTED_CHAR(_ForwardIterator __first, _ForwardIterator __last);
2657 template <class _ForwardIterator>
2658 _ForwardIterator
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002659 __parse_RE_dupl_symbol(_ForwardIterator __first, _ForwardIterator __last,
Howard Hinnante77aa5e2010-07-08 17:43:58 +00002660 __owns_one_state<_CharT>* __s,
2661 unsigned __mexp_begin, unsigned __mexp_end);
Howard Hinnant0de86b62010-06-25 20:56:08 +00002662 template <class _ForwardIterator>
2663 _ForwardIterator
Howard Hinnantaa698082010-07-16 19:08:36 +00002664 __parse_ERE_dupl_symbol(_ForwardIterator __first, _ForwardIterator __last,
2665 __owns_one_state<_CharT>* __s,
2666 unsigned __mexp_begin, unsigned __mexp_end);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002667 template <class _ForwardIterator>
2668 _ForwardIterator
Howard Hinnant0de86b62010-06-25 20:56:08 +00002669 __parse_bracket_expression(_ForwardIterator __first, _ForwardIterator __last);
2670 template <class _ForwardIterator>
2671 _ForwardIterator
Howard Hinnant173968a2010-07-13 21:48:06 +00002672 __parse_follow_list(_ForwardIterator __first, _ForwardIterator __last,
2673 __bracket_expression<_CharT, _Traits>* __ml);
Howard Hinnant0de86b62010-06-25 20:56:08 +00002674 template <class _ForwardIterator>
2675 _ForwardIterator
Howard Hinnant173968a2010-07-13 21:48:06 +00002676 __parse_expression_term(_ForwardIterator __first, _ForwardIterator __last,
2677 __bracket_expression<_CharT, _Traits>* __ml);
Howard Hinnant0de86b62010-06-25 20:56:08 +00002678 template <class _ForwardIterator>
2679 _ForwardIterator
Howard Hinnant173968a2010-07-13 21:48:06 +00002680 __parse_equivalence_class(_ForwardIterator __first, _ForwardIterator __last,
2681 __bracket_expression<_CharT, _Traits>* __ml);
Howard Hinnant0de86b62010-06-25 20:56:08 +00002682 template <class _ForwardIterator>
2683 _ForwardIterator
Howard Hinnant173968a2010-07-13 21:48:06 +00002684 __parse_character_class(_ForwardIterator __first, _ForwardIterator __last,
2685 __bracket_expression<_CharT, _Traits>* __ml);
Howard Hinnant0de86b62010-06-25 20:56:08 +00002686 template <class _ForwardIterator>
2687 _ForwardIterator
Howard Hinnant173968a2010-07-13 21:48:06 +00002688 __parse_collating_symbol(_ForwardIterator __first, _ForwardIterator __last,
2689 basic_string<_CharT>& __col_sym);
Howard Hinnant0de86b62010-06-25 20:56:08 +00002690 template <class _ForwardIterator>
2691 _ForwardIterator
2692 __parse_DUP_COUNT(_ForwardIterator __first, _ForwardIterator __last, int& __c);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002693 template <class _ForwardIterator>
2694 _ForwardIterator
2695 __parse_extended_reg_exp(_ForwardIterator __first, _ForwardIterator __last);
2696 template <class _ForwardIterator>
2697 _ForwardIterator
2698 __parse_ERE_branch(_ForwardIterator __first, _ForwardIterator __last);
2699 template <class _ForwardIterator>
2700 _ForwardIterator
2701 __parse_ERE_expression(_ForwardIterator __first, _ForwardIterator __last);
2702 template <class _ForwardIterator>
2703 _ForwardIterator
2704 __parse_one_char_or_coll_elem_ERE(_ForwardIterator __first, _ForwardIterator __last);
2705 template <class _ForwardIterator>
2706 _ForwardIterator
2707 __parse_ORD_CHAR_ERE(_ForwardIterator __first, _ForwardIterator __last);
2708 template <class _ForwardIterator>
2709 _ForwardIterator
2710 __parse_QUOTED_CHAR_ERE(_ForwardIterator __first, _ForwardIterator __last);
Howard Hinnant2ade7c22010-07-22 17:53:24 +00002711 template <class _ForwardIterator>
2712 _ForwardIterator
2713 __parse_ecma_exp(_ForwardIterator __first, _ForwardIterator __last);
2714 template <class _ForwardIterator>
2715 _ForwardIterator
2716 __parse_alternative(_ForwardIterator __first, _ForwardIterator __last);
2717 template <class _ForwardIterator>
2718 _ForwardIterator
2719 __parse_term(_ForwardIterator __first, _ForwardIterator __last);
2720 template <class _ForwardIterator>
2721 _ForwardIterator
2722 __parse_assertion(_ForwardIterator __first, _ForwardIterator __last);
2723 template <class _ForwardIterator>
2724 _ForwardIterator
2725 __parse_atom(_ForwardIterator __first, _ForwardIterator __last);
2726 template <class _ForwardIterator>
2727 _ForwardIterator
Howard Hinnant17615b02010-07-27 01:25:38 +00002728 __parse_atom_escape(_ForwardIterator __first, _ForwardIterator __last);
2729 template <class _ForwardIterator>
2730 _ForwardIterator
2731 __parse_decimal_escape(_ForwardIterator __first, _ForwardIterator __last);
2732 template <class _ForwardIterator>
2733 _ForwardIterator
2734 __parse_character_class_escape(_ForwardIterator __first, _ForwardIterator __last);
2735 template <class _ForwardIterator>
2736 _ForwardIterator
Howard Hinnant15476f32010-07-28 17:35:27 +00002737 __parse_character_escape(_ForwardIterator __first, _ForwardIterator __last,
2738 basic_string<_CharT>* __str = nullptr);
Howard Hinnant17615b02010-07-27 01:25:38 +00002739 template <class _ForwardIterator>
2740 _ForwardIterator
2741 __parse_pattern_character(_ForwardIterator __first, _ForwardIterator __last);
Howard Hinnant856846b2010-07-27 19:53:10 +00002742 template <class _ForwardIterator>
2743 _ForwardIterator
2744 __parse_grep(_ForwardIterator __first, _ForwardIterator __last);
2745 template <class _ForwardIterator>
2746 _ForwardIterator
2747 __parse_egrep(_ForwardIterator __first, _ForwardIterator __last);
Howard Hinnant15476f32010-07-28 17:35:27 +00002748 template <class _ForwardIterator>
2749 _ForwardIterator
2750 __parse_class_escape(_ForwardIterator __first, _ForwardIterator __last,
2751 basic_string<_CharT>& __str,
2752 __bracket_expression<_CharT, _Traits>* __ml);
2753 template <class _ForwardIterator>
2754 _ForwardIterator
2755 __parse_awk_escape(_ForwardIterator __first, _ForwardIterator __last,
2756 basic_string<_CharT>* __str = nullptr);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002757
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00002758 void __push_l_anchor() {__left_anchor_ = true;}
2759 void __push_r_anchor();
Howard Hinnantac303862010-07-12 15:51:17 +00002760 void __push_match_any();
Howard Hinnant17615b02010-07-27 01:25:38 +00002761 void __push_match_any_but_newline();
Howard Hinnante77aa5e2010-07-08 17:43:58 +00002762 void __push_greedy_inf_repeat(size_t __min, __owns_one_state<_CharT>* __s,
2763 unsigned __mexp_begin = 0, unsigned __mexp_end = 0)
2764 {__push_loop(__min, numeric_limits<size_t>::max(), __s,
2765 __mexp_begin, __mexp_end);}
Howard Hinnant17615b02010-07-27 01:25:38 +00002766 void __push_nongreedy_inf_repeat(size_t __min, __owns_one_state<_CharT>* __s,
2767 unsigned __mexp_begin = 0, unsigned __mexp_end = 0)
2768 {__push_loop(__min, numeric_limits<size_t>::max(), __s,
2769 __mexp_begin, __mexp_end, false);}
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002770 void __push_loop(size_t __min, size_t __max, __owns_one_state<_CharT>* __s,
2771 size_t __mexp_begin = 0, size_t __mexp_end = 0,
2772 bool __greedy = true);
Howard Hinnant173968a2010-07-13 21:48:06 +00002773 __bracket_expression<_CharT, _Traits>* __start_matching_list(bool __negate);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002774 void __push_char(value_type __c);
Howard Hinnantcba352d2010-07-12 18:16:05 +00002775 void __push_back_ref(int __i);
Howard Hinnantaa698082010-07-16 19:08:36 +00002776 void __push_alternation(__owns_one_state<_CharT>* __sa,
2777 __owns_one_state<_CharT>* __sb);
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00002778 void __push_begin_marked_subexpression();
2779 void __push_end_marked_subexpression(unsigned);
Howard Hinnant2ade7c22010-07-22 17:53:24 +00002780 void __push_empty();
Howard Hinnant17615b02010-07-27 01:25:38 +00002781 void __push_word_boundary(bool);
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002782 void __push_lookahead(const basic_regex&, bool);
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00002783
Howard Hinnant22ce0b42010-07-14 21:14:52 +00002784 template <class _Allocator>
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00002785 bool
Howard Hinnant22ce0b42010-07-14 21:14:52 +00002786 __search(const _CharT* __first, const _CharT* __last,
2787 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00002788 regex_constants::match_flag_type __flags) const;
2789
Howard Hinnant22ce0b42010-07-14 21:14:52 +00002790 template <class _Allocator>
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002791 bool
Howard Hinnant22ce0b42010-07-14 21:14:52 +00002792 __match_at_start(const _CharT* __first, const _CharT* __last,
2793 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002794 regex_constants::match_flag_type __flags) const;
Howard Hinnant17615b02010-07-27 01:25:38 +00002795 template <class _Allocator>
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002796 bool
Howard Hinnant17615b02010-07-27 01:25:38 +00002797 __match_at_start_ecma(const _CharT* __first, const _CharT* __last,
2798 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002799 regex_constants::match_flag_type __flags) const;
Howard Hinnant22ce0b42010-07-14 21:14:52 +00002800 template <class _Allocator>
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002801 bool
2802 __match_at_start_posix_nosubs(const _CharT* __first, const _CharT* __last,
Howard Hinnant22ce0b42010-07-14 21:14:52 +00002803 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002804 regex_constants::match_flag_type __flags) const;
Howard Hinnant22ce0b42010-07-14 21:14:52 +00002805 template <class _Allocator>
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002806 bool
Howard Hinnant22ce0b42010-07-14 21:14:52 +00002807 __match_at_start_posix_subs(const _CharT* __first, const _CharT* __last,
2808 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002809 regex_constants::match_flag_type __flags) const;
2810
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00002811 template <class _B, class _A, class _C, class _T>
2812 friend
2813 bool
2814 regex_search(_B, _B, match_results<_B, _A>&, const basic_regex<_C, _T>&,
2815 regex_constants::match_flag_type);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002816
Howard Hinnant22ce0b42010-07-14 21:14:52 +00002817 template <class _A, class _C, class _T>
2818 friend
2819 bool
2820 regex_search(const _C*, const _C*, match_results<const _C*, _A>&,
2821 const basic_regex<_C, _T>&, regex_constants::match_flag_type);
2822
2823 template <class _B, class _C, class _T>
2824 friend
2825 bool
2826 regex_search(_B, _B, const basic_regex<_C, _T>&,
2827 regex_constants::match_flag_type);
2828
2829 template <class _C, class _T>
2830 friend
2831 bool
2832 regex_search(const _C*, const _C*,
2833 const basic_regex<_C, _T>&, regex_constants::match_flag_type);
2834
2835 template <class _C, class _A, class _T>
2836 friend
2837 bool
2838 regex_search(const _C*, match_results<const _C*, _A>&, const basic_regex<_C, _T>&,
2839 regex_constants::match_flag_type);
2840
2841 template <class _ST, class _SA, class _C, class _T>
2842 friend
2843 bool
2844 regex_search(const basic_string<_C, _ST, _SA>& __s,
2845 const basic_regex<_C, _T>& __e,
2846 regex_constants::match_flag_type __flags);
2847
2848 template <class _ST, class _SA, class _A, class _C, class _T>
2849 friend
2850 bool
2851 regex_search(const basic_string<_C, _ST, _SA>& __s,
Howard Hinnant324bb032010-08-22 00:02:43 +00002852 match_results<typename basic_string<_C, _ST, _SA>::const_iterator, _A>&,
Howard Hinnant22ce0b42010-07-14 21:14:52 +00002853 const basic_regex<_C, _T>& __e,
2854 regex_constants::match_flag_type __flags);
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002855
2856 template <class, class> friend class __lookahead;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002857};
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002858
2859template <class _CharT, class _Traits>
Howard Hinnant7026a172010-08-13 18:11:23 +00002860void
2861basic_regex<_CharT, _Traits>::swap(basic_regex& __r)
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002862{
Howard Hinnant7026a172010-08-13 18:11:23 +00002863 using _STD::swap;
2864 swap(__traits_, __r.__traits_);
2865 swap(__flags_, __r.__flags_);
2866 swap(__marked_count_, __r.__marked_count_);
2867 swap(__loop_count_, __r.__loop_count_);
2868 swap(__open_count_, __r.__open_count_);
2869 swap(__start_, __r.__start_);
2870 swap(__end_, __r.__end_);
2871 swap(__left_anchor_, __r.__left_anchor_);
2872}
2873
2874template <class _CharT, class _Traits>
2875inline _LIBCPP_INLINE_VISIBILITY
2876void
2877swap(basic_regex<_CharT, _Traits>& __x, basic_regex<_CharT, _Traits>& __y)
2878{
2879 return __x.swap(__y);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002880}
2881
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002882// __lookahead
2883
2884template <class _CharT, class _Traits>
2885class __lookahead
2886 : public __owns_one_state<_CharT>
2887{
2888 typedef __owns_one_state<_CharT> base;
2889
2890 basic_regex<_CharT, _Traits> __exp_;
2891 bool __invert_;
2892
2893 __lookahead(const __lookahead&);
2894 __lookahead& operator=(const __lookahead&);
2895public:
2896 typedef _STD::__state<_CharT> __state;
2897
2898 __lookahead(const basic_regex<_CharT, _Traits>& __exp, bool __invert, __node<_CharT>* __s)
2899 : base(__s), __exp_(__exp), __invert_(__invert) {}
2900
2901 virtual void __exec(__state&) const;
2902
2903 virtual string speak() const
2904 {
2905 ostringstream os;
2906 if (__invert_)
2907 os << "not lookahead";
2908 else
2909 os << "lookahead";
2910 return os.str();
2911 }
2912};
2913
2914template <class _CharT, class _Traits>
2915void
2916__lookahead<_CharT, _Traits>::__exec(__state& __s) const
2917{
2918 match_results<const _CharT*> __m;
2919 __m.__init(1 + __exp_.mark_count(), __s.__current_, __s.__last_);
2920 bool __matched = __exp_.__match_at_start_ecma(__s.__current_, __s.__last_,
2921 __m, __s.__flags_);
2922 if (__matched != __invert_)
2923 {
2924 __s.__do_ = __state::__accept_but_not_consume;
2925 __s.__node_ = this->first();
2926 }
2927 else
2928 {
2929 __s.__do_ = __state::__reject;
2930 __s.__node_ = nullptr;
2931 }
2932}
2933
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002934template <class _CharT, class _Traits>
2935template <class _ForwardIterator>
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002936_ForwardIterator
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002937basic_regex<_CharT, _Traits>::__parse(_ForwardIterator __first,
2938 _ForwardIterator __last)
2939{
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002940 {
Howard Hinnantac303862010-07-12 15:51:17 +00002941 unique_ptr<__node> __h(new __end_state<_CharT>);
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002942 __start_.reset(new __empty_state<_CharT>(__h.get()));
2943 __h.release();
2944 __end_ = __start_.get();
2945 }
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00002946 switch (__flags_ & 0x1F0)
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002947 {
2948 case ECMAScript:
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002949 __first = __parse_ecma_exp(__first, __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002950 break;
2951 case basic:
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002952 __first = __parse_basic_reg_exp(__first, __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002953 break;
2954 case extended:
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002955 case awk:
Howard Hinnant15476f32010-07-28 17:35:27 +00002956 __first = __parse_extended_reg_exp(__first, __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002957 break;
2958 case grep:
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002959 __first = __parse_grep(__first, __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002960 break;
2961 case egrep:
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002962 __first = __parse_egrep(__first, __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002963 break;
Howard Hinnantd4444702010-08-11 17:04:31 +00002964#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002965 default:
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00002966 throw regex_error(regex_constants::__re_err_grammar);
Howard Hinnant324bb032010-08-22 00:02:43 +00002967#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002968 }
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002969 return __first;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002970}
2971
2972template <class _CharT, class _Traits>
2973template <class _ForwardIterator>
2974_ForwardIterator
2975basic_regex<_CharT, _Traits>::__parse_basic_reg_exp(_ForwardIterator __first,
2976 _ForwardIterator __last)
2977{
2978 if (__first != __last)
2979 {
2980 if (*__first == '^')
2981 {
2982 __push_l_anchor();
2983 ++__first;
2984 }
2985 if (__first != __last)
2986 {
2987 __first = __parse_RE_expression(__first, __last);
2988 if (__first != __last)
2989 {
2990 _ForwardIterator __temp = next(__first);
2991 if (__temp == __last && *__first == '$')
2992 {
2993 __push_r_anchor();
2994 ++__first;
2995 }
2996 }
2997 }
Howard Hinnantd4444702010-08-11 17:04:31 +00002998#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002999 if (__first != __last)
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00003000 throw regex_error(regex_constants::__re_err_empty);
Howard Hinnant324bb032010-08-22 00:02:43 +00003001#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003002 }
3003 return __first;
3004}
3005
3006template <class _CharT, class _Traits>
3007template <class _ForwardIterator>
3008_ForwardIterator
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003009basic_regex<_CharT, _Traits>::__parse_extended_reg_exp(_ForwardIterator __first,
3010 _ForwardIterator __last)
3011{
Howard Hinnantaa698082010-07-16 19:08:36 +00003012 __owns_one_state<_CharT>* __sa = __end_;
3013 _ForwardIterator __temp = __parse_ERE_branch(__first, __last);
Howard Hinnantd4444702010-08-11 17:04:31 +00003014#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantaa698082010-07-16 19:08:36 +00003015 if (__temp == __first)
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00003016 throw regex_error(regex_constants::__re_err_empty);
Howard Hinnant324bb032010-08-22 00:02:43 +00003017#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantaa698082010-07-16 19:08:36 +00003018 __first = __temp;
3019 while (__first != __last && *__first == '|')
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003020 {
Howard Hinnantaa698082010-07-16 19:08:36 +00003021 __owns_one_state<_CharT>* __sb = __end_;
3022 __temp = __parse_ERE_branch(++__first, __last);
Howard Hinnantd4444702010-08-11 17:04:31 +00003023#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003024 if (__temp == __first)
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00003025 throw regex_error(regex_constants::__re_err_empty);
Howard Hinnant324bb032010-08-22 00:02:43 +00003026#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantaa698082010-07-16 19:08:36 +00003027 __push_alternation(__sa, __sb);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003028 __first = __temp;
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003029 }
3030 return __first;
3031}
3032
3033template <class _CharT, class _Traits>
3034template <class _ForwardIterator>
3035_ForwardIterator
3036basic_regex<_CharT, _Traits>::__parse_ERE_branch(_ForwardIterator __first,
3037 _ForwardIterator __last)
3038{
3039 _ForwardIterator __temp = __parse_ERE_expression(__first, __last);
Howard Hinnantd4444702010-08-11 17:04:31 +00003040#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003041 if (__temp == __first)
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00003042 throw regex_error(regex_constants::__re_err_empty);
Howard Hinnant324bb032010-08-22 00:02:43 +00003043#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003044 do
3045 {
3046 __first = __temp;
3047 __temp = __parse_ERE_expression(__first, __last);
3048 } while (__temp != __first);
3049 return __first;
3050}
3051
3052template <class _CharT, class _Traits>
3053template <class _ForwardIterator>
3054_ForwardIterator
3055basic_regex<_CharT, _Traits>::__parse_ERE_expression(_ForwardIterator __first,
3056 _ForwardIterator __last)
3057{
Howard Hinnantaa698082010-07-16 19:08:36 +00003058 __owns_one_state<_CharT>* __e = __end_;
3059 unsigned __mexp_begin = __marked_count_;
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003060 _ForwardIterator __temp = __parse_one_char_or_coll_elem_ERE(__first, __last);
3061 if (__temp == __first && __temp != __last)
3062 {
3063 switch (*__temp)
3064 {
3065 case '^':
3066 __push_l_anchor();
3067 ++__temp;
3068 break;
3069 case '$':
3070 __push_r_anchor();
3071 ++__temp;
3072 break;
3073 case '(':
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00003074 __push_begin_marked_subexpression();
3075 unsigned __temp_count = __marked_count_;
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003076 ++__open_count_;
3077 __temp = __parse_extended_reg_exp(++__temp, __last);
Howard Hinnantd4444702010-08-11 17:04:31 +00003078#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003079 if (__temp == __last || *__temp != ')')
3080 throw regex_error(regex_constants::error_paren);
Howard Hinnant324bb032010-08-22 00:02:43 +00003081#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00003082 __push_end_marked_subexpression(__temp_count);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003083 --__open_count_;
3084 ++__temp;
3085 break;
3086 }
3087 }
3088 if (__temp != __first)
Howard Hinnantaa698082010-07-16 19:08:36 +00003089 __temp = __parse_ERE_dupl_symbol(__temp, __last, __e, __mexp_begin+1,
3090 __marked_count_+1);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003091 __first = __temp;
3092 return __first;
3093}
3094
3095template <class _CharT, class _Traits>
3096template <class _ForwardIterator>
3097_ForwardIterator
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003098basic_regex<_CharT, _Traits>::__parse_RE_expression(_ForwardIterator __first,
3099 _ForwardIterator __last)
3100{
3101 while (true)
3102 {
3103 _ForwardIterator __temp = __parse_simple_RE(__first, __last);
3104 if (__temp == __first)
3105 break;
3106 __first = __temp;
3107 }
3108 return __first;
3109}
3110
3111template <class _CharT, class _Traits>
3112template <class _ForwardIterator>
3113_ForwardIterator
3114basic_regex<_CharT, _Traits>::__parse_simple_RE(_ForwardIterator __first,
3115 _ForwardIterator __last)
3116{
3117 if (__first != __last)
3118 {
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003119 __owns_one_state<_CharT>* __e = __end_;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003120 unsigned __mexp_begin = __marked_count_;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003121 _ForwardIterator __temp = __parse_nondupl_RE(__first, __last);
3122 if (__temp != __first)
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003123 __first = __parse_RE_dupl_symbol(__temp, __last, __e,
3124 __mexp_begin+1, __marked_count_+1);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003125 }
3126 return __first;
3127}
3128
3129template <class _CharT, class _Traits>
3130template <class _ForwardIterator>
3131_ForwardIterator
3132basic_regex<_CharT, _Traits>::__parse_nondupl_RE(_ForwardIterator __first,
3133 _ForwardIterator __last)
3134{
3135 _ForwardIterator __temp = __first;
3136 __first = __parse_one_char_or_coll_elem_RE(__first, __last);
3137 if (__temp == __first)
3138 {
3139 __temp = __parse_Back_open_paren(__first, __last);
3140 if (__temp != __first)
3141 {
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00003142 __push_begin_marked_subexpression();
3143 unsigned __temp_count = __marked_count_;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003144 __first = __parse_RE_expression(__temp, __last);
3145 __temp = __parse_Back_close_paren(__first, __last);
Howard Hinnantd4444702010-08-11 17:04:31 +00003146#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003147 if (__temp == __first)
3148 throw regex_error(regex_constants::error_paren);
Howard Hinnant324bb032010-08-22 00:02:43 +00003149#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00003150 __push_end_marked_subexpression(__temp_count);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003151 __first = __temp;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003152 }
3153 else
3154 __first = __parse_BACKREF(__first, __last);
3155 }
3156 return __first;
3157}
3158
3159template <class _CharT, class _Traits>
3160template <class _ForwardIterator>
3161_ForwardIterator
3162basic_regex<_CharT, _Traits>::__parse_one_char_or_coll_elem_RE(
3163 _ForwardIterator __first,
3164 _ForwardIterator __last)
3165{
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003166 _ForwardIterator __temp = __parse_ORD_CHAR(__first, __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003167 if (__temp == __first)
3168 {
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003169 __temp = __parse_QUOTED_CHAR(__first, __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003170 if (__temp == __first)
3171 {
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003172 if (__temp != __last && *__temp == '.')
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003173 {
3174 __push_match_any();
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003175 ++__temp;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003176 }
3177 else
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003178 __temp = __parse_bracket_expression(__first, __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003179 }
3180 }
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003181 __first = __temp;
3182 return __first;
3183}
3184
3185template <class _CharT, class _Traits>
3186template <class _ForwardIterator>
3187_ForwardIterator
3188basic_regex<_CharT, _Traits>::__parse_one_char_or_coll_elem_ERE(
3189 _ForwardIterator __first,
3190 _ForwardIterator __last)
3191{
3192 _ForwardIterator __temp = __parse_ORD_CHAR_ERE(__first, __last);
3193 if (__temp == __first)
3194 {
3195 __temp = __parse_QUOTED_CHAR_ERE(__first, __last);
3196 if (__temp == __first)
3197 {
3198 if (__temp != __last && *__temp == '.')
3199 {
3200 __push_match_any();
3201 ++__temp;
3202 }
3203 else
3204 __temp = __parse_bracket_expression(__first, __last);
3205 }
3206 }
3207 __first = __temp;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003208 return __first;
3209}
3210
3211template <class _CharT, class _Traits>
3212template <class _ForwardIterator>
3213_ForwardIterator
3214basic_regex<_CharT, _Traits>::__parse_Back_open_paren(_ForwardIterator __first,
3215 _ForwardIterator __last)
3216{
3217 if (__first != __last)
3218 {
3219 _ForwardIterator __temp = next(__first);
3220 if (__temp != __last)
3221 {
3222 if (*__first == '\\' && *__temp == '(')
3223 __first = ++__temp;
3224 }
3225 }
3226 return __first;
3227}
3228
3229template <class _CharT, class _Traits>
3230template <class _ForwardIterator>
3231_ForwardIterator
3232basic_regex<_CharT, _Traits>::__parse_Back_close_paren(_ForwardIterator __first,
3233 _ForwardIterator __last)
3234{
3235 if (__first != __last)
3236 {
3237 _ForwardIterator __temp = next(__first);
3238 if (__temp != __last)
3239 {
3240 if (*__first == '\\' && *__temp == ')')
3241 __first = ++__temp;
3242 }
3243 }
3244 return __first;
3245}
3246
3247template <class _CharT, class _Traits>
3248template <class _ForwardIterator>
3249_ForwardIterator
3250basic_regex<_CharT, _Traits>::__parse_Back_open_brace(_ForwardIterator __first,
3251 _ForwardIterator __last)
3252{
3253 if (__first != __last)
3254 {
3255 _ForwardIterator __temp = next(__first);
3256 if (__temp != __last)
3257 {
3258 if (*__first == '\\' && *__temp == '{')
3259 __first = ++__temp;
3260 }
3261 }
3262 return __first;
3263}
3264
3265template <class _CharT, class _Traits>
3266template <class _ForwardIterator>
3267_ForwardIterator
3268basic_regex<_CharT, _Traits>::__parse_Back_close_brace(_ForwardIterator __first,
3269 _ForwardIterator __last)
3270{
3271 if (__first != __last)
3272 {
3273 _ForwardIterator __temp = next(__first);
3274 if (__temp != __last)
3275 {
3276 if (*__first == '\\' && *__temp == '}')
3277 __first = ++__temp;
3278 }
3279 }
3280 return __first;
3281}
3282
3283template <class _CharT, class _Traits>
3284template <class _ForwardIterator>
3285_ForwardIterator
3286basic_regex<_CharT, _Traits>::__parse_BACKREF(_ForwardIterator __first,
3287 _ForwardIterator __last)
3288{
3289 if (__first != __last)
3290 {
3291 _ForwardIterator __temp = next(__first);
3292 if (__temp != __last)
3293 {
3294 if (*__first == '\\' && '1' <= *__temp && *__temp <= '9')
3295 {
3296 __push_back_ref(*__temp - '0');
3297 __first = ++__temp;
3298 }
3299 }
3300 }
3301 return __first;
3302}
3303
3304template <class _CharT, class _Traits>
3305template <class _ForwardIterator>
3306_ForwardIterator
3307basic_regex<_CharT, _Traits>::__parse_ORD_CHAR(_ForwardIterator __first,
3308 _ForwardIterator __last)
3309{
3310 if (__first != __last)
3311 {
3312 _ForwardIterator __temp = next(__first);
3313 if (__temp == __last && *__first == '$')
3314 return __first;
3315 // Not called inside a bracket
3316 if (*__first == '.' || *__first == '\\' || *__first == '[')
3317 return __first;
Howard Hinnant0de86b62010-06-25 20:56:08 +00003318 __push_char(*__first);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003319 ++__first;
3320 }
3321 return __first;
3322}
3323
3324template <class _CharT, class _Traits>
3325template <class _ForwardIterator>
3326_ForwardIterator
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003327basic_regex<_CharT, _Traits>::__parse_ORD_CHAR_ERE(_ForwardIterator __first,
3328 _ForwardIterator __last)
3329{
3330 if (__first != __last)
3331 {
3332 switch (*__first)
3333 {
3334 case '^':
3335 case '.':
3336 case '[':
3337 case '$':
3338 case '(':
3339 case '|':
3340 case '*':
3341 case '+':
3342 case '?':
3343 case '{':
3344 case '\\':
3345 break;
3346 case ')':
3347 if (__open_count_ == 0)
3348 {
3349 __push_char(*__first);
3350 ++__first;
3351 }
3352 break;
3353 default:
3354 __push_char(*__first);
3355 ++__first;
3356 break;
3357 }
3358 }
3359 return __first;
3360}
3361
3362template <class _CharT, class _Traits>
3363template <class _ForwardIterator>
3364_ForwardIterator
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003365basic_regex<_CharT, _Traits>::__parse_QUOTED_CHAR(_ForwardIterator __first,
3366 _ForwardIterator __last)
3367{
3368 if (__first != __last)
3369 {
3370 _ForwardIterator __temp = next(__first);
3371 if (__temp != __last)
3372 {
3373 if (*__first == '\\')
3374 {
3375 switch (*__temp)
3376 {
3377 case '^':
3378 case '.':
3379 case '*':
3380 case '[':
3381 case '$':
3382 case '\\':
Howard Hinnant0de86b62010-06-25 20:56:08 +00003383 __push_char(*__temp);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003384 __first = ++__temp;
3385 break;
3386 }
3387 }
3388 }
3389 }
3390 return __first;
3391}
3392
3393template <class _CharT, class _Traits>
3394template <class _ForwardIterator>
3395_ForwardIterator
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003396basic_regex<_CharT, _Traits>::__parse_QUOTED_CHAR_ERE(_ForwardIterator __first,
3397 _ForwardIterator __last)
3398{
3399 if (__first != __last)
3400 {
3401 _ForwardIterator __temp = next(__first);
3402 if (__temp != __last)
3403 {
3404 if (*__first == '\\')
3405 {
3406 switch (*__temp)
3407 {
3408 case '^':
3409 case '.':
3410 case '*':
3411 case '[':
3412 case '$':
3413 case '\\':
3414 case '(':
3415 case ')':
3416 case '|':
3417 case '+':
3418 case '?':
3419 case '{':
3420 __push_char(*__temp);
3421 __first = ++__temp;
3422 break;
Howard Hinnant15476f32010-07-28 17:35:27 +00003423 default:
3424 if ((__flags_ & 0x1F0) == awk)
3425 __first = __parse_awk_escape(++__first, __last);
3426 break;
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003427 }
3428 }
3429 }
3430 }
3431 return __first;
3432}
3433
3434template <class _CharT, class _Traits>
3435template <class _ForwardIterator>
3436_ForwardIterator
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003437basic_regex<_CharT, _Traits>::__parse_RE_dupl_symbol(_ForwardIterator __first,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003438 _ForwardIterator __last,
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003439 __owns_one_state<_CharT>* __s,
3440 unsigned __mexp_begin,
3441 unsigned __mexp_end)
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003442{
3443 if (__first != __last)
3444 {
Howard Hinnant0de86b62010-06-25 20:56:08 +00003445 if (*__first == '*')
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003446 {
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003447 __push_greedy_inf_repeat(0, __s, __mexp_begin, __mexp_end);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003448 ++__first;
3449 }
3450 else
3451 {
3452 _ForwardIterator __temp = __parse_Back_open_brace(__first, __last);
3453 if (__temp != __first)
3454 {
3455 int __min = 0;
3456 __first = __temp;
3457 __temp = __parse_DUP_COUNT(__first, __last, __min);
Howard Hinnantd4444702010-08-11 17:04:31 +00003458#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003459 if (__temp == __first)
3460 throw regex_error(regex_constants::error_badbrace);
Howard Hinnant324bb032010-08-22 00:02:43 +00003461#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003462 __first = __temp;
Howard Hinnantd4444702010-08-11 17:04:31 +00003463#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003464 if (__first == __last)
3465 throw regex_error(regex_constants::error_brace);
Howard Hinnant324bb032010-08-22 00:02:43 +00003466#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003467 if (*__first != ',')
3468 {
3469 __temp = __parse_Back_close_brace(__first, __last);
Howard Hinnantd4444702010-08-11 17:04:31 +00003470#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003471 if (__temp == __first)
3472 throw regex_error(regex_constants::error_brace);
Howard Hinnant324bb032010-08-22 00:02:43 +00003473#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantcba352d2010-07-12 18:16:05 +00003474 __push_loop(__min, __min, __s, __mexp_begin, __mexp_end,
3475 true);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003476 __first = __temp;
3477 }
3478 else
3479 {
3480 ++__first; // consume ','
3481 int __max = -1;
3482 __first = __parse_DUP_COUNT(__first, __last, __max);
3483 __temp = __parse_Back_close_brace(__first, __last);
Howard Hinnantd4444702010-08-11 17:04:31 +00003484#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003485 if (__temp == __first)
3486 throw regex_error(regex_constants::error_brace);
Howard Hinnant324bb032010-08-22 00:02:43 +00003487#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003488 if (__max == -1)
Howard Hinnantaa698082010-07-16 19:08:36 +00003489 __push_greedy_inf_repeat(__min, __s, __mexp_begin, __mexp_end);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003490 else
3491 {
Howard Hinnantd4444702010-08-11 17:04:31 +00003492#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003493 if (__max < __min)
3494 throw regex_error(regex_constants::error_badbrace);
Howard Hinnant324bb032010-08-22 00:02:43 +00003495#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantcba352d2010-07-12 18:16:05 +00003496 __push_loop(__min, __max, __s, __mexp_begin, __mexp_end,
3497 true);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003498 }
3499 __first = __temp;
3500 }
3501 }
3502 }
3503 }
3504 return __first;
3505}
3506
Howard Hinnant0de86b62010-06-25 20:56:08 +00003507template <class _CharT, class _Traits>
3508template <class _ForwardIterator>
3509_ForwardIterator
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003510basic_regex<_CharT, _Traits>::__parse_ERE_dupl_symbol(_ForwardIterator __first,
Howard Hinnantaa698082010-07-16 19:08:36 +00003511 _ForwardIterator __last,
3512 __owns_one_state<_CharT>* __s,
3513 unsigned __mexp_begin,
3514 unsigned __mexp_end)
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003515{
3516 if (__first != __last)
3517 {
Howard Hinnanta0d045b2010-07-29 00:36:00 +00003518 unsigned __grammar = __flags_ & 0x1F0;
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003519 switch (*__first)
3520 {
3521 case '*':
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003522 ++__first;
Howard Hinnanta0d045b2010-07-29 00:36:00 +00003523 if (__grammar == ECMAScript && __first != __last && *__first == '?')
Howard Hinnant17615b02010-07-27 01:25:38 +00003524 {
3525 ++__first;
3526 __push_nongreedy_inf_repeat(0, __s, __mexp_begin, __mexp_end);
3527 }
3528 else
3529 __push_greedy_inf_repeat(0, __s, __mexp_begin, __mexp_end);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003530 break;
3531 case '+':
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003532 ++__first;
Howard Hinnanta0d045b2010-07-29 00:36:00 +00003533 if (__grammar == ECMAScript && __first != __last && *__first == '?')
Howard Hinnant17615b02010-07-27 01:25:38 +00003534 {
3535 ++__first;
3536 __push_nongreedy_inf_repeat(1, __s, __mexp_begin, __mexp_end);
3537 }
3538 else
3539 __push_greedy_inf_repeat(1, __s, __mexp_begin, __mexp_end);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003540 break;
3541 case '?':
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003542 ++__first;
Howard Hinnanta0d045b2010-07-29 00:36:00 +00003543 if (__grammar == ECMAScript && __first != __last && *__first == '?')
Howard Hinnant17615b02010-07-27 01:25:38 +00003544 {
3545 ++__first;
3546 __push_loop(0, 1, __s, __mexp_begin, __mexp_end, false);
3547 }
3548 else
3549 __push_loop(0, 1, __s, __mexp_begin, __mexp_end);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003550 break;
3551 case '{':
3552 {
3553 int __min;
Howard Hinnantaa698082010-07-16 19:08:36 +00003554 _ForwardIterator __temp = __parse_DUP_COUNT(++__first, __last, __min);
Howard Hinnantd4444702010-08-11 17:04:31 +00003555#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003556 if (__temp == __first)
3557 throw regex_error(regex_constants::error_badbrace);
Howard Hinnant324bb032010-08-22 00:02:43 +00003558#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003559 __first = __temp;
Howard Hinnantd4444702010-08-11 17:04:31 +00003560#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003561 if (__first == __last)
3562 throw regex_error(regex_constants::error_brace);
Howard Hinnant324bb032010-08-22 00:02:43 +00003563#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003564 switch (*__first)
3565 {
3566 case '}':
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003567 ++__first;
Howard Hinnanta0d045b2010-07-29 00:36:00 +00003568 if (__grammar == ECMAScript && __first != __last && *__first == '?')
Howard Hinnant17615b02010-07-27 01:25:38 +00003569 {
3570 ++__first;
3571 __push_loop(__min, __min, __s, __mexp_begin, __mexp_end, false);
3572 }
3573 else
3574 __push_loop(__min, __min, __s, __mexp_begin, __mexp_end);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003575 break;
3576 case ',':
Howard Hinnantd4444702010-08-11 17:04:31 +00003577 ++__first;
3578#ifndef _LIBCPP_NO_EXCEPTIONS
3579 if (__first == __last)
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003580 throw regex_error(regex_constants::error_badbrace);
Howard Hinnant324bb032010-08-22 00:02:43 +00003581#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003582 if (*__first == '}')
3583 {
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003584 ++__first;
Howard Hinnanta0d045b2010-07-29 00:36:00 +00003585 if (__grammar == ECMAScript && __first != __last && *__first == '?')
Howard Hinnant17615b02010-07-27 01:25:38 +00003586 {
3587 ++__first;
3588 __push_nongreedy_inf_repeat(__min, __s, __mexp_begin, __mexp_end);
3589 }
3590 else
3591 __push_greedy_inf_repeat(__min, __s, __mexp_begin, __mexp_end);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003592 }
3593 else
3594 {
Howard Hinnantaa698082010-07-16 19:08:36 +00003595 int __max = -1;
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003596 __temp = __parse_DUP_COUNT(__first, __last, __max);
Howard Hinnantd4444702010-08-11 17:04:31 +00003597#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003598 if (__temp == __first)
3599 throw regex_error(regex_constants::error_brace);
Howard Hinnant324bb032010-08-22 00:02:43 +00003600#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003601 __first = __temp;
Howard Hinnantd4444702010-08-11 17:04:31 +00003602#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003603 if (__first == __last || *__first != '}')
3604 throw regex_error(regex_constants::error_brace);
Howard Hinnant324bb032010-08-22 00:02:43 +00003605#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003606 ++__first;
Howard Hinnantd4444702010-08-11 17:04:31 +00003607#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003608 if (__max < __min)
3609 throw regex_error(regex_constants::error_badbrace);
Howard Hinnant324bb032010-08-22 00:02:43 +00003610#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnanta0d045b2010-07-29 00:36:00 +00003611 if (__grammar == ECMAScript && __first != __last && *__first == '?')
Howard Hinnant17615b02010-07-27 01:25:38 +00003612 {
3613 ++__first;
3614 __push_loop(__min, __max, __s, __mexp_begin, __mexp_end, false);
3615 }
3616 else
3617 __push_loop(__min, __max, __s, __mexp_begin, __mexp_end);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003618 }
Howard Hinnantaa698082010-07-16 19:08:36 +00003619 break;
Howard Hinnantd4444702010-08-11 17:04:31 +00003620#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003621 default:
3622 throw regex_error(regex_constants::error_badbrace);
Howard Hinnant324bb032010-08-22 00:02:43 +00003623#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003624 }
3625 }
3626 break;
3627 }
3628 }
3629 return __first;
3630}
3631
3632template <class _CharT, class _Traits>
3633template <class _ForwardIterator>
3634_ForwardIterator
Howard Hinnant0de86b62010-06-25 20:56:08 +00003635basic_regex<_CharT, _Traits>::__parse_bracket_expression(_ForwardIterator __first,
3636 _ForwardIterator __last)
3637{
3638 if (__first != __last && *__first == '[')
3639 {
Howard Hinnantd4444702010-08-11 17:04:31 +00003640 ++__first;
3641#ifndef _LIBCPP_NO_EXCEPTIONS
3642 if (__first == __last)
Howard Hinnant0de86b62010-06-25 20:56:08 +00003643 throw regex_error(regex_constants::error_brack);
Howard Hinnant324bb032010-08-22 00:02:43 +00003644#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant173968a2010-07-13 21:48:06 +00003645 bool __negate = false;
Howard Hinnant0de86b62010-06-25 20:56:08 +00003646 if (*__first == '^')
3647 {
3648 ++__first;
Howard Hinnant173968a2010-07-13 21:48:06 +00003649 __negate = true;
Howard Hinnant0de86b62010-06-25 20:56:08 +00003650 }
Howard Hinnant173968a2010-07-13 21:48:06 +00003651 __bracket_expression<_CharT, _Traits>* __ml = __start_matching_list(__negate);
3652 // __ml owned by *this
Howard Hinnantd4444702010-08-11 17:04:31 +00003653#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0de86b62010-06-25 20:56:08 +00003654 if (__first == __last)
3655 throw regex_error(regex_constants::error_brack);
Howard Hinnant324bb032010-08-22 00:02:43 +00003656#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant15476f32010-07-28 17:35:27 +00003657 if ((__flags_ & 0x1F0) != ECMAScript && *__first == ']')
Howard Hinnant0de86b62010-06-25 20:56:08 +00003658 {
Howard Hinnant173968a2010-07-13 21:48:06 +00003659 __ml->__add_char(']');
Howard Hinnant0de86b62010-06-25 20:56:08 +00003660 ++__first;
3661 }
Howard Hinnant173968a2010-07-13 21:48:06 +00003662 __first = __parse_follow_list(__first, __last, __ml);
Howard Hinnantd4444702010-08-11 17:04:31 +00003663#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0de86b62010-06-25 20:56:08 +00003664 if (__first == __last)
3665 throw regex_error(regex_constants::error_brack);
Howard Hinnant324bb032010-08-22 00:02:43 +00003666#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0de86b62010-06-25 20:56:08 +00003667 if (*__first == '-')
3668 {
Howard Hinnant173968a2010-07-13 21:48:06 +00003669 __ml->__add_char('-');
Howard Hinnant0de86b62010-06-25 20:56:08 +00003670 ++__first;
3671 }
Howard Hinnantd4444702010-08-11 17:04:31 +00003672#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0de86b62010-06-25 20:56:08 +00003673 if (__first == __last || *__first != ']')
3674 throw regex_error(regex_constants::error_brack);
Howard Hinnant324bb032010-08-22 00:02:43 +00003675#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0de86b62010-06-25 20:56:08 +00003676 ++__first;
3677 }
3678 return __first;
3679}
3680
3681template <class _CharT, class _Traits>
3682template <class _ForwardIterator>
3683_ForwardIterator
3684basic_regex<_CharT, _Traits>::__parse_follow_list(_ForwardIterator __first,
Howard Hinnant173968a2010-07-13 21:48:06 +00003685 _ForwardIterator __last,
3686 __bracket_expression<_CharT, _Traits>* __ml)
Howard Hinnant0de86b62010-06-25 20:56:08 +00003687{
3688 if (__first != __last)
3689 {
3690 while (true)
3691 {
Howard Hinnant173968a2010-07-13 21:48:06 +00003692 _ForwardIterator __temp = __parse_expression_term(__first, __last,
3693 __ml);
Howard Hinnant0de86b62010-06-25 20:56:08 +00003694 if (__temp == __first)
3695 break;
3696 __first = __temp;
3697 }
3698 }
3699 return __first;
3700}
3701
3702template <class _CharT, class _Traits>
3703template <class _ForwardIterator>
3704_ForwardIterator
3705basic_regex<_CharT, _Traits>::__parse_expression_term(_ForwardIterator __first,
Howard Hinnant173968a2010-07-13 21:48:06 +00003706 _ForwardIterator __last,
3707 __bracket_expression<_CharT, _Traits>* __ml)
Howard Hinnant0de86b62010-06-25 20:56:08 +00003708{
3709 if (__first != __last && *__first != ']')
3710 {
Howard Hinnant0de86b62010-06-25 20:56:08 +00003711 _ForwardIterator __temp = next(__first);
Howard Hinnant173968a2010-07-13 21:48:06 +00003712 basic_string<_CharT> __start_range;
Howard Hinnant0de86b62010-06-25 20:56:08 +00003713 if (__temp != __last && *__first == '[')
3714 {
3715 if (*__temp == '=')
Howard Hinnant173968a2010-07-13 21:48:06 +00003716 return __parse_equivalence_class(++__temp, __last, __ml);
Howard Hinnant0de86b62010-06-25 20:56:08 +00003717 else if (*__temp == ':')
Howard Hinnant173968a2010-07-13 21:48:06 +00003718 return __parse_character_class(++__temp, __last, __ml);
Howard Hinnant0de86b62010-06-25 20:56:08 +00003719 else if (*__temp == '.')
Howard Hinnant173968a2010-07-13 21:48:06 +00003720 __first = __parse_collating_symbol(++__temp, __last, __start_range);
Howard Hinnant0de86b62010-06-25 20:56:08 +00003721 }
Howard Hinnant15476f32010-07-28 17:35:27 +00003722 unsigned __grammar = __flags_ & 0x1F0;
3723 if (__start_range.empty())
Howard Hinnant0de86b62010-06-25 20:56:08 +00003724 {
Howard Hinnant15476f32010-07-28 17:35:27 +00003725 if ((__grammar == ECMAScript || __grammar == awk) && *__first == '\\')
3726 {
3727 if (__grammar == ECMAScript)
3728 __first = __parse_class_escape(++__first, __last, __start_range, __ml);
3729 else
3730 __first = __parse_awk_escape(++__first, __last, &__start_range);
3731 }
3732 else
3733 {
3734 __start_range = *__first;
3735 ++__first;
3736 }
Howard Hinnant0de86b62010-06-25 20:56:08 +00003737 }
3738 if (__first != __last && *__first != ']')
3739 {
3740 __temp = next(__first);
3741 if (__temp != __last && *__first == '-' && *__temp != ']')
3742 {
3743 // parse a range
Howard Hinnant173968a2010-07-13 21:48:06 +00003744 basic_string<_CharT> __end_range;
Howard Hinnant0de86b62010-06-25 20:56:08 +00003745 __first = __temp;
3746 ++__temp;
3747 if (__temp != __last && *__first == '[' && *__temp == '.')
Howard Hinnant173968a2010-07-13 21:48:06 +00003748 __first = __parse_collating_symbol(++__temp, __last, __end_range);
Howard Hinnant0de86b62010-06-25 20:56:08 +00003749 else
3750 {
Howard Hinnant15476f32010-07-28 17:35:27 +00003751 if ((__grammar == ECMAScript || __grammar == awk) && *__first == '\\')
3752 {
3753 if (__grammar == ECMAScript)
3754 __first = __parse_class_escape(++__first, __last,
3755 __end_range, __ml);
3756 else
3757 __first = __parse_awk_escape(++__first, __last,
3758 &__end_range);
3759 }
3760 else
3761 {
3762 __end_range = *__first;
3763 ++__first;
3764 }
Howard Hinnant0de86b62010-06-25 20:56:08 +00003765 }
Howard Hinnant173968a2010-07-13 21:48:06 +00003766 __ml->__add_range(_STD::move(__start_range), _STD::move(__end_range));
Howard Hinnant0de86b62010-06-25 20:56:08 +00003767 }
Howard Hinnant173968a2010-07-13 21:48:06 +00003768 else
3769 {
3770 if (__start_range.size() == 1)
3771 __ml->__add_char(__start_range[0]);
3772 else
3773 __ml->__add_digraph(__start_range[0], __start_range[1]);
3774 }
3775 }
3776 else
3777 {
3778 if (__start_range.size() == 1)
3779 __ml->__add_char(__start_range[0]);
3780 else
3781 __ml->__add_digraph(__start_range[0], __start_range[1]);
Howard Hinnant0de86b62010-06-25 20:56:08 +00003782 }
3783 }
3784 return __first;
3785}
3786
3787template <class _CharT, class _Traits>
3788template <class _ForwardIterator>
3789_ForwardIterator
Howard Hinnant15476f32010-07-28 17:35:27 +00003790basic_regex<_CharT, _Traits>::__parse_class_escape(_ForwardIterator __first,
3791 _ForwardIterator __last,
3792 basic_string<_CharT>& __str,
3793 __bracket_expression<_CharT, _Traits>* __ml)
3794{
Howard Hinnantd4444702010-08-11 17:04:31 +00003795#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant15476f32010-07-28 17:35:27 +00003796 if (__first == __last)
3797 throw regex_error(regex_constants::error_escape);
Howard Hinnant324bb032010-08-22 00:02:43 +00003798#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant15476f32010-07-28 17:35:27 +00003799 switch (*__first)
3800 {
3801 case 0:
3802 __str = *__first;
3803 return ++__first;
3804 case 'b':
3805 __str = _CharT(8);
3806 return ++__first;
3807 case 'd':
3808 __ml->__add_class(ctype_base::digit);
3809 return ++__first;
3810 case 'D':
3811 __ml->__add_neg_class(ctype_base::digit);
3812 return ++__first;
3813 case 's':
3814 __ml->__add_class(ctype_base::space);
3815 return ++__first;
3816 case 'S':
3817 __ml->__add_neg_class(ctype_base::space);
3818 return ++__first;
3819 case 'w':
3820 __ml->__add_class(ctype_base::alnum);
3821 __ml->__add_char('_');
3822 return ++__first;
3823 case 'W':
3824 __ml->__add_neg_class(ctype_base::alnum);
3825 __ml->__add_neg_char('_');
3826 return ++__first;
3827 }
3828 __first = __parse_character_escape(__first, __last, &__str);
3829 return __first;
3830}
3831
3832template <class _CharT, class _Traits>
3833template <class _ForwardIterator>
3834_ForwardIterator
3835basic_regex<_CharT, _Traits>::__parse_awk_escape(_ForwardIterator __first,
3836 _ForwardIterator __last,
3837 basic_string<_CharT>* __str)
3838{
Howard Hinnantd4444702010-08-11 17:04:31 +00003839#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant15476f32010-07-28 17:35:27 +00003840 if (__first == __last)
3841 throw regex_error(regex_constants::error_escape);
Howard Hinnant324bb032010-08-22 00:02:43 +00003842#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant15476f32010-07-28 17:35:27 +00003843 switch (*__first)
3844 {
3845 case '\\':
3846 case '"':
3847 case '/':
3848 if (__str)
3849 *__str = *__first;
3850 else
3851 __push_char(*__first);
3852 return ++__first;
3853 case 'a':
3854 if (__str)
3855 *__str = _CharT(7);
3856 else
3857 __push_char(_CharT(7));
3858 return ++__first;
3859 case 'b':
3860 if (__str)
3861 *__str = _CharT(8);
3862 else
3863 __push_char(_CharT(8));
3864 return ++__first;
3865 case 'f':
3866 if (__str)
3867 *__str = _CharT(0xC);
3868 else
3869 __push_char(_CharT(0xC));
3870 return ++__first;
3871 case 'n':
3872 if (__str)
3873 *__str = _CharT(0xA);
3874 else
3875 __push_char(_CharT(0xA));
3876 return ++__first;
3877 case 'r':
3878 if (__str)
3879 *__str = _CharT(0xD);
3880 else
3881 __push_char(_CharT(0xD));
3882 return ++__first;
3883 case 't':
3884 if (__str)
3885 *__str = _CharT(0x9);
3886 else
3887 __push_char(_CharT(0x9));
3888 return ++__first;
3889 case 'v':
3890 if (__str)
3891 *__str = _CharT(0xB);
3892 else
3893 __push_char(_CharT(0xB));
3894 return ++__first;
3895 }
3896 if ('0' <= *__first && *__first <= '7')
3897 {
3898 unsigned __val = *__first - '0';
3899 if (++__first != __last && ('0' <= *__first && *__first <= '7'))
3900 {
3901 __val = 8 * __val + *__first - '0';
3902 if (++__first != __last && ('0' <= *__first && *__first <= '7'))
3903 __val = 8 * __val + *__first - '0';
3904 }
3905 if (__str)
3906 *__str = _CharT(__val);
3907 else
3908 __push_char(_CharT(__val));
3909 }
Howard Hinnantd4444702010-08-11 17:04:31 +00003910#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant15476f32010-07-28 17:35:27 +00003911 else
3912 throw regex_error(regex_constants::error_escape);
Howard Hinnant324bb032010-08-22 00:02:43 +00003913#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant15476f32010-07-28 17:35:27 +00003914 return __first;
3915}
3916
3917template <class _CharT, class _Traits>
3918template <class _ForwardIterator>
3919_ForwardIterator
Howard Hinnant0de86b62010-06-25 20:56:08 +00003920basic_regex<_CharT, _Traits>::__parse_equivalence_class(_ForwardIterator __first,
Howard Hinnant173968a2010-07-13 21:48:06 +00003921 _ForwardIterator __last,
3922 __bracket_expression<_CharT, _Traits>* __ml)
Howard Hinnant0de86b62010-06-25 20:56:08 +00003923{
3924 // Found [=
3925 // This means =] must exist
3926 value_type _Equal_close[2] = {'=', ']'};
3927 _ForwardIterator __temp = _STD::search(__first, __last, _Equal_close,
3928 _Equal_close+2);
Howard Hinnantd4444702010-08-11 17:04:31 +00003929#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0de86b62010-06-25 20:56:08 +00003930 if (__temp == __last)
3931 throw regex_error(regex_constants::error_brack);
Howard Hinnant324bb032010-08-22 00:02:43 +00003932#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0de86b62010-06-25 20:56:08 +00003933 // [__first, __temp) contains all text in [= ... =]
3934 typedef typename _Traits::string_type string_type;
3935 string_type __collate_name =
3936 __traits_.lookup_collatename(__first, __temp);
Howard Hinnantd4444702010-08-11 17:04:31 +00003937#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0de86b62010-06-25 20:56:08 +00003938 if (__collate_name.empty())
Howard Hinnant173968a2010-07-13 21:48:06 +00003939 throw regex_error(regex_constants::error_collate);
Howard Hinnant324bb032010-08-22 00:02:43 +00003940#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0de86b62010-06-25 20:56:08 +00003941 string_type __equiv_name =
3942 __traits_.transform_primary(__collate_name.begin(),
3943 __collate_name.end());
3944 if (!__equiv_name.empty())
Howard Hinnant173968a2010-07-13 21:48:06 +00003945 __ml->__add_equivalence(__equiv_name);
Howard Hinnant0de86b62010-06-25 20:56:08 +00003946 else
Howard Hinnant173968a2010-07-13 21:48:06 +00003947 {
3948 switch (__collate_name.size())
3949 {
3950 case 1:
3951 __ml->__add_char(__collate_name[0]);
3952 break;
3953 case 2:
3954 __ml->__add_digraph(__collate_name[0], __collate_name[1]);
3955 break;
Howard Hinnantd4444702010-08-11 17:04:31 +00003956#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant173968a2010-07-13 21:48:06 +00003957 default:
3958 throw regex_error(regex_constants::error_collate);
Howard Hinnant324bb032010-08-22 00:02:43 +00003959#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant173968a2010-07-13 21:48:06 +00003960 }
3961 }
Howard Hinnant0de86b62010-06-25 20:56:08 +00003962 __first = next(__temp, 2);
3963 return __first;
3964}
3965
3966template <class _CharT, class _Traits>
3967template <class _ForwardIterator>
3968_ForwardIterator
3969basic_regex<_CharT, _Traits>::__parse_character_class(_ForwardIterator __first,
Howard Hinnant173968a2010-07-13 21:48:06 +00003970 _ForwardIterator __last,
3971 __bracket_expression<_CharT, _Traits>* __ml)
Howard Hinnant0de86b62010-06-25 20:56:08 +00003972{
3973 // Found [:
3974 // This means :] must exist
3975 value_type _Colon_close[2] = {':', ']'};
3976 _ForwardIterator __temp = _STD::search(__first, __last, _Colon_close,
3977 _Colon_close+2);
Howard Hinnantd4444702010-08-11 17:04:31 +00003978#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0de86b62010-06-25 20:56:08 +00003979 if (__temp == __last)
3980 throw regex_error(regex_constants::error_brack);
Howard Hinnant324bb032010-08-22 00:02:43 +00003981#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0de86b62010-06-25 20:56:08 +00003982 // [__first, __temp) contains all text in [: ... :]
3983 typedef typename _Traits::char_class_type char_class_type;
3984 char_class_type __class_type =
3985 __traits_.lookup_classname(__first, __temp, __flags_ & icase);
Howard Hinnantd4444702010-08-11 17:04:31 +00003986#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0de86b62010-06-25 20:56:08 +00003987 if (__class_type == 0)
3988 throw regex_error(regex_constants::error_brack);
Howard Hinnant324bb032010-08-22 00:02:43 +00003989#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant173968a2010-07-13 21:48:06 +00003990 __ml->__add_class(__class_type);
Howard Hinnant0de86b62010-06-25 20:56:08 +00003991 __first = next(__temp, 2);
3992 return __first;
3993}
3994
3995template <class _CharT, class _Traits>
3996template <class _ForwardIterator>
3997_ForwardIterator
3998basic_regex<_CharT, _Traits>::__parse_collating_symbol(_ForwardIterator __first,
Howard Hinnant173968a2010-07-13 21:48:06 +00003999 _ForwardIterator __last,
4000 basic_string<_CharT>& __col_sym)
Howard Hinnant0de86b62010-06-25 20:56:08 +00004001{
4002 // Found [.
4003 // This means .] must exist
4004 value_type _Dot_close[2] = {'.', ']'};
4005 _ForwardIterator __temp = _STD::search(__first, __last, _Dot_close,
4006 _Dot_close+2);
Howard Hinnantd4444702010-08-11 17:04:31 +00004007#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0de86b62010-06-25 20:56:08 +00004008 if (__temp == __last)
4009 throw regex_error(regex_constants::error_brack);
Howard Hinnant324bb032010-08-22 00:02:43 +00004010#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0de86b62010-06-25 20:56:08 +00004011 // [__first, __temp) contains all text in [. ... .]
4012 typedef typename _Traits::string_type string_type;
Howard Hinnant173968a2010-07-13 21:48:06 +00004013 __col_sym = __traits_.lookup_collatename(__first, __temp);
4014 switch (__col_sym.size())
4015 {
4016 case 1:
4017 case 2:
4018 break;
Howard Hinnantd4444702010-08-11 17:04:31 +00004019#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant173968a2010-07-13 21:48:06 +00004020 default:
4021 throw regex_error(regex_constants::error_collate);
Howard Hinnant324bb032010-08-22 00:02:43 +00004022#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant173968a2010-07-13 21:48:06 +00004023 }
Howard Hinnant0de86b62010-06-25 20:56:08 +00004024 __first = next(__temp, 2);
4025 return __first;
4026}
4027
4028template <class _CharT, class _Traits>
4029template <class _ForwardIterator>
4030_ForwardIterator
4031basic_regex<_CharT, _Traits>::__parse_DUP_COUNT(_ForwardIterator __first,
4032 _ForwardIterator __last,
4033 int& __c)
4034{
4035 if (__first != __last && '0' <= *__first && *__first <= '9')
4036 {
4037 __c = *__first - '0';
4038 for (++__first; __first != __last && '0' <= *__first && *__first <= '9';
4039 ++__first)
4040 {
4041 __c *= 10;
4042 __c += *__first - '0';
4043 }
4044 }
4045 return __first;
4046}
4047
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00004048template <class _CharT, class _Traits>
Howard Hinnant2ade7c22010-07-22 17:53:24 +00004049template <class _ForwardIterator>
4050_ForwardIterator
4051basic_regex<_CharT, _Traits>::__parse_ecma_exp(_ForwardIterator __first,
4052 _ForwardIterator __last)
4053{
4054 __owns_one_state<_CharT>* __sa = __end_;
4055 _ForwardIterator __temp = __parse_alternative(__first, __last);
4056 if (__temp == __first)
4057 __push_empty();
4058 __first = __temp;
4059 while (__first != __last && *__first == '|')
4060 {
4061 __owns_one_state<_CharT>* __sb = __end_;
4062 __temp = __parse_alternative(++__first, __last);
4063 if (__temp == __first)
4064 __push_empty();
4065 __push_alternation(__sa, __sb);
4066 __first = __temp;
4067 }
4068 return __first;
4069}
4070
4071template <class _CharT, class _Traits>
4072template <class _ForwardIterator>
4073_ForwardIterator
4074basic_regex<_CharT, _Traits>::__parse_alternative(_ForwardIterator __first,
4075 _ForwardIterator __last)
4076{
4077 while (true)
4078 {
4079 _ForwardIterator __temp = __parse_term(__first, __last);
4080 if (__temp == __first)
4081 break;
4082 __first = __temp;
4083 }
4084 return __first;
4085}
4086
4087template <class _CharT, class _Traits>
4088template <class _ForwardIterator>
4089_ForwardIterator
4090basic_regex<_CharT, _Traits>::__parse_term(_ForwardIterator __first,
4091 _ForwardIterator __last)
4092{
4093 _ForwardIterator __temp = __parse_assertion(__first, __last);
4094 if (__temp == __first)
4095 {
Howard Hinnant17615b02010-07-27 01:25:38 +00004096 __owns_one_state<_CharT>* __e = __end_;
4097 unsigned __mexp_begin = __marked_count_;
Howard Hinnant2ade7c22010-07-22 17:53:24 +00004098 __temp = __parse_atom(__first, __last);
4099 if (__temp != __first)
Howard Hinnant17615b02010-07-27 01:25:38 +00004100 __first = __parse_ERE_dupl_symbol(__temp, __last, __e,
4101 __mexp_begin+1, __marked_count_+1);
Howard Hinnant2ade7c22010-07-22 17:53:24 +00004102 }
Howard Hinnant17615b02010-07-27 01:25:38 +00004103 else
4104 __first = __temp;
Howard Hinnant2ade7c22010-07-22 17:53:24 +00004105 return __first;
4106}
4107
4108template <class _CharT, class _Traits>
4109template <class _ForwardIterator>
4110_ForwardIterator
4111basic_regex<_CharT, _Traits>::__parse_assertion(_ForwardIterator __first,
4112 _ForwardIterator __last)
4113{
4114 if (__first != __last)
4115 {
4116 switch (*__first)
4117 {
4118 case '^':
4119 __push_l_anchor();
4120 ++__first;
4121 break;
4122 case '$':
4123 __push_r_anchor();
4124 ++__first;
4125 break;
4126 case '\\':
4127 {
4128 _ForwardIterator __temp = _STD::next(__first);
4129 if (__temp != __last)
4130 {
4131 if (*__temp == 'b')
4132 {
Howard Hinnant17615b02010-07-27 01:25:38 +00004133 __push_word_boundary(false);
Howard Hinnant2ade7c22010-07-22 17:53:24 +00004134 __first = ++__temp;
4135 }
4136 else if (*__temp == 'B')
4137 {
Howard Hinnant17615b02010-07-27 01:25:38 +00004138 __push_word_boundary(true);
Howard Hinnant2ade7c22010-07-22 17:53:24 +00004139 __first = ++__temp;
4140 }
4141 }
4142 }
4143 break;
4144 case '(':
4145 {
4146 _ForwardIterator __temp = _STD::next(__first);
4147 if (__temp != __last && *__temp == '?')
4148 {
4149 if (++__temp != __last)
4150 {
4151 switch (*__temp)
4152 {
4153 case '=':
Howard Hinnante9de5ff2010-07-27 22:20:32 +00004154 {
4155 basic_regex __exp;
4156 __exp.__flags_ = __flags_;
4157 __temp = __exp.__parse(++__temp, __last);
4158 __exp.__push_l_anchor();
4159 __push_lookahead(_STD::move(__exp), false);
Howard Hinnantd4444702010-08-11 17:04:31 +00004160#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnante9de5ff2010-07-27 22:20:32 +00004161 if (__temp == __last || *__temp != ')')
4162 throw regex_error(regex_constants::error_paren);
Howard Hinnant324bb032010-08-22 00:02:43 +00004163#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnante9de5ff2010-07-27 22:20:32 +00004164 __first = ++__temp;
4165 }
Howard Hinnant2ade7c22010-07-22 17:53:24 +00004166 break;
4167 case '!':
Howard Hinnante9de5ff2010-07-27 22:20:32 +00004168 {
4169 basic_regex __exp;
4170 __exp.__flags_ = __flags_;
4171 __temp = __exp.__parse(++__temp, __last);
4172 __exp.__push_l_anchor();
4173 __push_lookahead(_STD::move(__exp), true);
Howard Hinnantd4444702010-08-11 17:04:31 +00004174#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnante9de5ff2010-07-27 22:20:32 +00004175 if (__temp == __last || *__temp != ')')
4176 throw regex_error(regex_constants::error_paren);
Howard Hinnant324bb032010-08-22 00:02:43 +00004177#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnante9de5ff2010-07-27 22:20:32 +00004178 __first = ++__temp;
4179 }
Howard Hinnant2ade7c22010-07-22 17:53:24 +00004180 break;
4181 }
4182 }
4183 }
4184 }
4185 break;
4186 }
4187 }
4188 return __first;
4189}
4190
4191template <class _CharT, class _Traits>
4192template <class _ForwardIterator>
4193_ForwardIterator
4194basic_regex<_CharT, _Traits>::__parse_atom(_ForwardIterator __first,
4195 _ForwardIterator __last)
4196{
Howard Hinnant17615b02010-07-27 01:25:38 +00004197 if (__first != __last)
4198 {
4199 switch (*__first)
4200 {
4201 case '.':
4202 __push_match_any_but_newline();
4203 ++__first;
4204 break;
4205 case '\\':
4206 __first = __parse_atom_escape(__first, __last);
4207 break;
4208 case '[':
4209 __first = __parse_bracket_expression(__first, __last);
4210 break;
4211 case '(':
4212 {
Howard Hinnantd4444702010-08-11 17:04:31 +00004213 ++__first;
4214#ifndef _LIBCPP_NO_EXCEPTIONS
4215 if (__first == __last)
Howard Hinnant17615b02010-07-27 01:25:38 +00004216 throw regex_error(regex_constants::error_paren);
Howard Hinnant324bb032010-08-22 00:02:43 +00004217#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant17615b02010-07-27 01:25:38 +00004218 _ForwardIterator __temp = _STD::next(__first);
4219 if (__temp != __last && *__first == '?' && *__temp == ':')
4220 {
4221 ++__open_count_;
4222 __first = __parse_ecma_exp(++__temp, __last);
Howard Hinnantd4444702010-08-11 17:04:31 +00004223#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant17615b02010-07-27 01:25:38 +00004224 if (__first == __last || *__first != ')')
4225 throw regex_error(regex_constants::error_paren);
Howard Hinnant324bb032010-08-22 00:02:43 +00004226#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant17615b02010-07-27 01:25:38 +00004227 --__open_count_;
4228 ++__first;
4229 }
4230 else
4231 {
4232 __push_begin_marked_subexpression();
4233 unsigned __temp_count = __marked_count_;
4234 ++__open_count_;
4235 __first = __parse_ecma_exp(__first, __last);
Howard Hinnantd4444702010-08-11 17:04:31 +00004236#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant17615b02010-07-27 01:25:38 +00004237 if (__first == __last || *__first != ')')
4238 throw regex_error(regex_constants::error_paren);
Howard Hinnant324bb032010-08-22 00:02:43 +00004239#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant17615b02010-07-27 01:25:38 +00004240 __push_end_marked_subexpression(__temp_count);
4241 --__open_count_;
4242 ++__first;
4243 }
4244 }
4245 break;
4246 default:
4247 __first = __parse_pattern_character(__first, __last);
4248 break;
4249 }
4250 }
4251 return __first;
4252}
4253
4254template <class _CharT, class _Traits>
4255template <class _ForwardIterator>
4256_ForwardIterator
4257basic_regex<_CharT, _Traits>::__parse_atom_escape(_ForwardIterator __first,
4258 _ForwardIterator __last)
4259{
4260 if (__first != __last && *__first == '\\')
4261 {
4262 _ForwardIterator __t1 = _STD::next(__first);
4263 _ForwardIterator __t2 = __parse_decimal_escape(__t1, __last);
4264 if (__t2 != __t1)
4265 __first = __t2;
4266 else
4267 {
4268 __t2 = __parse_character_class_escape(__t1, __last);
4269 if (__t2 != __t1)
4270 __first = __t2;
4271 else
4272 {
4273 __t2 = __parse_character_escape(__t1, __last);
4274 if (__t2 != __t1)
4275 __first = __t2;
4276 }
4277 }
4278 }
4279 return __first;
4280}
4281
4282template <class _CharT, class _Traits>
4283template <class _ForwardIterator>
4284_ForwardIterator
4285basic_regex<_CharT, _Traits>::__parse_decimal_escape(_ForwardIterator __first,
4286 _ForwardIterator __last)
4287{
4288 if (__first != __last)
4289 {
4290 if (*__first == '0')
4291 {
4292 __push_char(_CharT());
4293 ++__first;
4294 }
4295 else if ('1' <= *__first && *__first <= '9')
4296 {
4297 unsigned __v = *__first - '0';
4298 for (++__first; '0' <= *__first && *__first <= '9'; ++__first)
4299 __v = 10 * __v + *__first - '0';
Howard Hinnantd4444702010-08-11 17:04:31 +00004300#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant17615b02010-07-27 01:25:38 +00004301 if (__v > mark_count())
4302 throw regex_error(regex_constants::error_backref);
Howard Hinnant324bb032010-08-22 00:02:43 +00004303#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant17615b02010-07-27 01:25:38 +00004304 __push_back_ref(__v);
4305 }
4306 }
4307 return __first;
4308}
4309
4310template <class _CharT, class _Traits>
4311template <class _ForwardIterator>
4312_ForwardIterator
4313basic_regex<_CharT, _Traits>::__parse_character_class_escape(_ForwardIterator __first,
4314 _ForwardIterator __last)
4315{
4316 if (__first != __last)
4317 {
4318 __bracket_expression<_CharT, _Traits>* __ml;
4319 switch (*__first)
4320 {
4321 case 'd':
4322 __ml = __start_matching_list(false);
4323 __ml->__add_class(ctype_base::digit);
4324 ++__first;
4325 break;
4326 case 'D':
4327 __ml = __start_matching_list(true);
4328 __ml->__add_class(ctype_base::digit);
4329 ++__first;
4330 break;
4331 case 's':
4332 __ml = __start_matching_list(false);
4333 __ml->__add_class(ctype_base::space);
4334 ++__first;
4335 break;
4336 case 'S':
4337 __ml = __start_matching_list(true);
4338 __ml->__add_class(ctype_base::space);
4339 ++__first;
4340 break;
4341 case 'w':
4342 __ml = __start_matching_list(false);
4343 __ml->__add_class(ctype_base::alnum);
4344 __ml->__add_char('_');
4345 ++__first;
4346 break;
4347 case 'W':
4348 __ml = __start_matching_list(true);
4349 __ml->__add_class(ctype_base::alnum);
4350 __ml->__add_char('_');
4351 ++__first;
4352 break;
4353 }
4354 }
4355 return __first;
4356}
4357
4358template <class _CharT, class _Traits>
4359template <class _ForwardIterator>
4360_ForwardIterator
4361basic_regex<_CharT, _Traits>::__parse_character_escape(_ForwardIterator __first,
Howard Hinnant15476f32010-07-28 17:35:27 +00004362 _ForwardIterator __last,
4363 basic_string<_CharT>* __str)
Howard Hinnant17615b02010-07-27 01:25:38 +00004364{
4365 if (__first != __last)
4366 {
4367 _ForwardIterator __t;
4368 unsigned __sum = 0;
4369 int __hd;
4370 switch (*__first)
4371 {
4372 case 'f':
Howard Hinnant15476f32010-07-28 17:35:27 +00004373 if (__str)
4374 *__str = _CharT(0xC);
4375 else
4376 __push_char(_CharT(0xC));
Howard Hinnant17615b02010-07-27 01:25:38 +00004377 ++__first;
4378 break;
4379 case 'n':
Howard Hinnant15476f32010-07-28 17:35:27 +00004380 if (__str)
4381 *__str = _CharT(0xA);
4382 else
4383 __push_char(_CharT(0xA));
Howard Hinnant17615b02010-07-27 01:25:38 +00004384 ++__first;
4385 break;
4386 case 'r':
Howard Hinnant15476f32010-07-28 17:35:27 +00004387 if (__str)
4388 *__str = _CharT(0xD);
4389 else
4390 __push_char(_CharT(0xD));
Howard Hinnant17615b02010-07-27 01:25:38 +00004391 ++__first;
4392 break;
4393 case 't':
Howard Hinnant15476f32010-07-28 17:35:27 +00004394 if (__str)
4395 *__str = _CharT(0x9);
4396 else
4397 __push_char(_CharT(0x9));
Howard Hinnant17615b02010-07-27 01:25:38 +00004398 ++__first;
4399 break;
4400 case 'v':
Howard Hinnant15476f32010-07-28 17:35:27 +00004401 if (__str)
4402 *__str = _CharT(0xB);
4403 else
4404 __push_char(_CharT(0xB));
Howard Hinnant17615b02010-07-27 01:25:38 +00004405 ++__first;
4406 break;
4407 case 'c':
4408 if ((__t = _STD::next(__first)) != __last)
4409 {
4410 if ('A' <= *__t <= 'Z' || 'a' <= *__t <= 'z')
4411 {
Howard Hinnant15476f32010-07-28 17:35:27 +00004412 if (__str)
4413 *__str = _CharT(*__t % 32);
4414 else
4415 __push_char(_CharT(*__t % 32));
Howard Hinnant17615b02010-07-27 01:25:38 +00004416 __first = ++__t;
4417 }
4418 }
4419 break;
4420 case 'u':
Howard Hinnantd4444702010-08-11 17:04:31 +00004421 ++__first;
4422#ifndef _LIBCPP_NO_EXCEPTIONS
4423 if (__first == __last)
Howard Hinnant17615b02010-07-27 01:25:38 +00004424 throw regex_error(regex_constants::error_escape);
Howard Hinnant324bb032010-08-22 00:02:43 +00004425#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant17615b02010-07-27 01:25:38 +00004426 __hd = __traits_.value(*__first, 16);
Howard Hinnantd4444702010-08-11 17:04:31 +00004427#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant17615b02010-07-27 01:25:38 +00004428 if (__hd == -1)
4429 throw regex_error(regex_constants::error_escape);
Howard Hinnant324bb032010-08-22 00:02:43 +00004430#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant17615b02010-07-27 01:25:38 +00004431 __sum = 16 * __sum + __hd;
Howard Hinnantd4444702010-08-11 17:04:31 +00004432 ++__first;
4433#ifndef _LIBCPP_NO_EXCEPTIONS
4434 if (__first == __last)
Howard Hinnant17615b02010-07-27 01:25:38 +00004435 throw regex_error(regex_constants::error_escape);
Howard Hinnant324bb032010-08-22 00:02:43 +00004436#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant17615b02010-07-27 01:25:38 +00004437 __hd = __traits_.value(*__first, 16);
Howard Hinnantd4444702010-08-11 17:04:31 +00004438#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant17615b02010-07-27 01:25:38 +00004439 if (__hd == -1)
4440 throw regex_error(regex_constants::error_escape);
Howard Hinnant324bb032010-08-22 00:02:43 +00004441#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant17615b02010-07-27 01:25:38 +00004442 __sum = 16 * __sum + __hd;
4443 // drop through
4444 case 'x':
Howard Hinnantd4444702010-08-11 17:04:31 +00004445 ++__first;
4446#ifndef _LIBCPP_NO_EXCEPTIONS
4447 if (__first == __last)
Howard Hinnant17615b02010-07-27 01:25:38 +00004448 throw regex_error(regex_constants::error_escape);
Howard Hinnant324bb032010-08-22 00:02:43 +00004449#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant17615b02010-07-27 01:25:38 +00004450 __hd = __traits_.value(*__first, 16);
Howard Hinnantd4444702010-08-11 17:04:31 +00004451#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant17615b02010-07-27 01:25:38 +00004452 if (__hd == -1)
4453 throw regex_error(regex_constants::error_escape);
Howard Hinnant324bb032010-08-22 00:02:43 +00004454#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant17615b02010-07-27 01:25:38 +00004455 __sum = 16 * __sum + __hd;
Howard Hinnantd4444702010-08-11 17:04:31 +00004456 ++__first;
4457#ifndef _LIBCPP_NO_EXCEPTIONS
4458 if (__first == __last)
Howard Hinnant17615b02010-07-27 01:25:38 +00004459 throw regex_error(regex_constants::error_escape);
Howard Hinnant324bb032010-08-22 00:02:43 +00004460#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant17615b02010-07-27 01:25:38 +00004461 __hd = __traits_.value(*__first, 16);
Howard Hinnantd4444702010-08-11 17:04:31 +00004462#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant17615b02010-07-27 01:25:38 +00004463 if (__hd == -1)
4464 throw regex_error(regex_constants::error_escape);
Howard Hinnant324bb032010-08-22 00:02:43 +00004465#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant17615b02010-07-27 01:25:38 +00004466 __sum = 16 * __sum + __hd;
Howard Hinnant15476f32010-07-28 17:35:27 +00004467 if (__str)
4468 *__str = _CharT(__sum);
4469 else
4470 __push_char(_CharT(__sum));
Howard Hinnant17615b02010-07-27 01:25:38 +00004471 ++__first;
4472 break;
4473 default:
4474 if (*__first != '_' && !__traits_.isctype(*__first, ctype_base::alnum))
4475 {
Howard Hinnant15476f32010-07-28 17:35:27 +00004476 if (__str)
4477 *__str = *__first;
4478 else
4479 __push_char(*__first);
Howard Hinnant17615b02010-07-27 01:25:38 +00004480 ++__first;
4481 }
Howard Hinnantd4444702010-08-11 17:04:31 +00004482#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant15476f32010-07-28 17:35:27 +00004483 else if (__str)
4484 throw regex_error(regex_constants::error_escape);
Howard Hinnant324bb032010-08-22 00:02:43 +00004485#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant17615b02010-07-27 01:25:38 +00004486 break;
4487 }
4488 }
4489 return __first;
4490}
4491
4492template <class _CharT, class _Traits>
4493template <class _ForwardIterator>
4494_ForwardIterator
4495basic_regex<_CharT, _Traits>::__parse_pattern_character(_ForwardIterator __first,
4496 _ForwardIterator __last)
4497{
4498 if (__first != __last)
4499 {
4500 switch (*__first)
4501 {
4502 case '^':
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 break;
4517 default:
4518 __push_char(*__first);
4519 ++__first;
4520 break;
4521 }
4522 }
4523 return __first;
Howard Hinnant2ade7c22010-07-22 17:53:24 +00004524}
4525
4526template <class _CharT, class _Traits>
Howard Hinnant856846b2010-07-27 19:53:10 +00004527template <class _ForwardIterator>
4528_ForwardIterator
4529basic_regex<_CharT, _Traits>::__parse_grep(_ForwardIterator __first,
4530 _ForwardIterator __last)
4531{
4532 __owns_one_state<_CharT>* __sa = __end_;
4533 _ForwardIterator __t1 = _STD::find(__first, __last, _CharT('\n'));
4534 if (__t1 != __first)
4535 __parse_basic_reg_exp(__first, __t1);
4536 else
4537 __push_empty();
4538 __first = __t1;
4539 if (__first != __last)
4540 ++__first;
4541 while (__first != __last)
4542 {
4543 __t1 = _STD::find(__first, __last, _CharT('\n'));
4544 __owns_one_state<_CharT>* __sb = __end_;
4545 if (__t1 != __first)
4546 __parse_basic_reg_exp(__first, __t1);
4547 else
4548 __push_empty();
4549 __push_alternation(__sa, __sb);
4550 __first = __t1;
4551 if (__first != __last)
4552 ++__first;
4553 }
4554 return __first;
4555}
4556
4557template <class _CharT, class _Traits>
4558template <class _ForwardIterator>
4559_ForwardIterator
4560basic_regex<_CharT, _Traits>::__parse_egrep(_ForwardIterator __first,
4561 _ForwardIterator __last)
4562{
4563 __owns_one_state<_CharT>* __sa = __end_;
4564 _ForwardIterator __t1 = _STD::find(__first, __last, _CharT('\n'));
4565 if (__t1 != __first)
4566 __parse_extended_reg_exp(__first, __t1);
4567 else
4568 __push_empty();
4569 __first = __t1;
4570 if (__first != __last)
4571 ++__first;
4572 while (__first != __last)
4573 {
4574 __t1 = _STD::find(__first, __last, _CharT('\n'));
4575 __owns_one_state<_CharT>* __sb = __end_;
4576 if (__t1 != __first)
4577 __parse_extended_reg_exp(__first, __t1);
4578 else
4579 __push_empty();
4580 __push_alternation(__sa, __sb);
4581 __first = __t1;
4582 if (__first != __last)
4583 ++__first;
4584 }
4585 return __first;
4586}
4587
4588template <class _CharT, class _Traits>
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00004589void
Howard Hinnantf8ce4592010-07-07 19:14:52 +00004590basic_regex<_CharT, _Traits>::__push_loop(size_t __min, size_t __max,
4591 __owns_one_state<_CharT>* __s, size_t __mexp_begin, size_t __mexp_end,
4592 bool __greedy)
4593{
4594 unique_ptr<__empty_state<_CharT> > __e1(new __empty_state<_CharT>(__end_->first()));
4595 __end_->first() = nullptr;
Howard Hinnantac303862010-07-12 15:51:17 +00004596 unique_ptr<__loop<_CharT> > __e2(new __loop<_CharT>(__loop_count_,
4597 __s->first(), __e1.get(), __mexp_begin, __mexp_end, __greedy,
4598 __min, __max));
4599 __s->first() = nullptr;
4600 __e1.release();
4601 __end_->first() = new __repeat_one_loop<_CharT>(__e2.get());
Howard Hinnantf8ce4592010-07-07 19:14:52 +00004602 __end_ = __e2->second();
Howard Hinnantac303862010-07-12 15:51:17 +00004603 __s->first() = __e2.release();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00004604 ++__loop_count_;
4605}
4606
4607template <class _CharT, class _Traits>
4608void
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00004609basic_regex<_CharT, _Traits>::__push_char(value_type __c)
4610{
Howard Hinnant173968a2010-07-13 21:48:06 +00004611 if (flags() & icase)
Howard Hinnante34f17d2010-07-12 19:11:27 +00004612 __end_->first() = new __match_char_icase<_CharT, _Traits>
4613 (__traits_, __c, __end_->first());
Howard Hinnant173968a2010-07-13 21:48:06 +00004614 else if (flags() & collate)
Howard Hinnante34f17d2010-07-12 19:11:27 +00004615 __end_->first() = new __match_char_collate<_CharT, _Traits>
4616 (__traits_, __c, __end_->first());
4617 else
4618 __end_->first() = new __match_char<_CharT>(__c, __end_->first());
Howard Hinnante77aa5e2010-07-08 17:43:58 +00004619 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00004620}
4621
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00004622template <class _CharT, class _Traits>
4623void
4624basic_regex<_CharT, _Traits>::__push_begin_marked_subexpression()
4625{
Howard Hinnant68025ed2010-07-14 15:45:11 +00004626 if (!(__flags_ & nosubs))
4627 {
4628 __end_->first() =
4629 new __begin_marked_subexpression<_CharT>(++__marked_count_,
4630 __end_->first());
4631 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4632 }
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00004633}
4634
4635template <class _CharT, class _Traits>
4636void
4637basic_regex<_CharT, _Traits>::__push_end_marked_subexpression(unsigned __sub)
4638{
Howard Hinnant68025ed2010-07-14 15:45:11 +00004639 if (!(__flags_ & nosubs))
4640 {
4641 __end_->first() =
4642 new __end_marked_subexpression<_CharT>(__sub, __end_->first());
4643 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4644 }
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00004645}
4646
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00004647template <class _CharT, class _Traits>
4648void
4649basic_regex<_CharT, _Traits>::__push_r_anchor()
4650{
4651 __end_->first() = new __r_anchor<_CharT>(__end_->first());
4652 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4653}
4654
Howard Hinnantac303862010-07-12 15:51:17 +00004655template <class _CharT, class _Traits>
4656void
4657basic_regex<_CharT, _Traits>::__push_match_any()
4658{
4659 __end_->first() = new __match_any<_CharT>(__end_->first());
4660 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4661}
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00004662
Howard Hinnantcba352d2010-07-12 18:16:05 +00004663template <class _CharT, class _Traits>
4664void
Howard Hinnant17615b02010-07-27 01:25:38 +00004665basic_regex<_CharT, _Traits>::__push_match_any_but_newline()
4666{
4667 __end_->first() = new __match_any_but_newline<_CharT>(__end_->first());
4668 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4669}
4670
4671template <class _CharT, class _Traits>
4672void
Howard Hinnant2ade7c22010-07-22 17:53:24 +00004673basic_regex<_CharT, _Traits>::__push_empty()
4674{
4675 __end_->first() = new __empty_state<_CharT>(__end_->first());
4676 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4677}
4678
4679template <class _CharT, class _Traits>
4680void
Howard Hinnant17615b02010-07-27 01:25:38 +00004681basic_regex<_CharT, _Traits>::__push_word_boundary(bool __invert)
4682{
4683 __end_->first() = new __word_boundary<_CharT, _Traits>(__traits_, __invert,
4684 __end_->first());
4685 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4686}
4687
4688template <class _CharT, class _Traits>
4689void
Howard Hinnantcba352d2010-07-12 18:16:05 +00004690basic_regex<_CharT, _Traits>::__push_back_ref(int __i)
4691{
Howard Hinnant173968a2010-07-13 21:48:06 +00004692 if (flags() & icase)
Howard Hinnante34f17d2010-07-12 19:11:27 +00004693 __end_->first() = new __back_ref_icase<_CharT, _Traits>
4694 (__traits_, __i, __end_->first());
Howard Hinnant173968a2010-07-13 21:48:06 +00004695 else if (flags() & collate)
Howard Hinnante34f17d2010-07-12 19:11:27 +00004696 __end_->first() = new __back_ref_collate<_CharT, _Traits>
4697 (__traits_, __i, __end_->first());
4698 else
4699 __end_->first() = new __back_ref<_CharT>(__i, __end_->first());
Howard Hinnantcba352d2010-07-12 18:16:05 +00004700 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4701}
4702
Howard Hinnant173968a2010-07-13 21:48:06 +00004703template <class _CharT, class _Traits>
Howard Hinnantaa698082010-07-16 19:08:36 +00004704void
4705basic_regex<_CharT, _Traits>::__push_alternation(__owns_one_state<_CharT>* __sa,
4706 __owns_one_state<_CharT>* __ea)
4707{
4708 __sa->first() = new __alternate<_CharT>(
4709 static_cast<__owns_one_state<_CharT>*>(__sa->first()),
4710 static_cast<__owns_one_state<_CharT>*>(__ea->first()));
4711 __ea->first() = nullptr;
4712 __ea->first() = new __empty_state<_CharT>(__end_->first());
4713 __end_->first() = nullptr;
4714 __end_->first() = new __empty_non_own_state<_CharT>(__ea->first());
4715 __end_ = static_cast<__owns_one_state<_CharT>*>(__ea->first());
4716}
4717
4718template <class _CharT, class _Traits>
Howard Hinnant173968a2010-07-13 21:48:06 +00004719__bracket_expression<_CharT, _Traits>*
4720basic_regex<_CharT, _Traits>::__start_matching_list(bool __negate)
4721{
4722 __bracket_expression<_CharT, _Traits>* __r =
4723 new __bracket_expression<_CharT, _Traits>(__traits_, __end_->first(),
4724 __negate, __flags_ & icase,
4725 __flags_ & collate);
4726 __end_->first() = __r;
4727 __end_ = __r;
4728 return __r;
4729}
4730
Howard Hinnante9de5ff2010-07-27 22:20:32 +00004731template <class _CharT, class _Traits>
4732void
4733basic_regex<_CharT, _Traits>::__push_lookahead(const basic_regex& __exp,
4734 bool __invert)
4735{
4736 __end_->first() = new __lookahead<_CharT, _Traits>(__exp, __invert,
4737 __end_->first());
4738 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4739}
4740
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00004741typedef basic_regex<char> regex;
4742typedef basic_regex<wchar_t> wregex;
4743
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00004744// sub_match
4745
4746template <class _BidirectionalIterator>
4747class sub_match
4748 : public pair<_BidirectionalIterator, _BidirectionalIterator>
4749{
4750public:
4751 typedef _BidirectionalIterator iterator;
4752 typedef typename iterator_traits<iterator>::value_type value_type;
4753 typedef typename iterator_traits<iterator>::difference_type difference_type;
4754 typedef basic_string<value_type> string_type;
4755
4756 bool matched;
4757
4758 difference_type length() const
4759 {return matched ? _STD::distance(this->first, this->second) : 0;}
4760 string_type str() const
4761 {return matched ? string_type(this->first, this->second) : string_type();}
4762 operator string_type() const
4763 {return str();}
4764
4765 int compare(const sub_match& __s) const
4766 {return str().compare(__s.str());}
4767 int compare(const string_type& __s) const
4768 {return str().compare(__s);}
4769 int compare(const value_type* __s) const
4770 {return str().compare(__s);}
4771};
4772
4773typedef sub_match<const char*> csub_match;
4774typedef sub_match<const wchar_t*> wcsub_match;
4775typedef sub_match<string::const_iterator> ssub_match;
4776typedef sub_match<wstring::const_iterator> wssub_match;
4777
4778template <class _BiIter>
4779inline _LIBCPP_INLINE_VISIBILITY
4780bool
4781operator==(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
4782{
4783 return __x.compare(__y) == 0;
4784}
4785
4786template <class _BiIter>
4787inline _LIBCPP_INLINE_VISIBILITY
4788bool
4789operator!=(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
4790{
4791 return !(__x == __y);
4792}
4793
4794template <class _BiIter>
4795inline _LIBCPP_INLINE_VISIBILITY
4796bool
4797operator<(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
4798{
4799 return __x.compare(__y) < 0;
4800}
4801
4802template <class _BiIter>
4803inline _LIBCPP_INLINE_VISIBILITY
4804bool
4805operator<=(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
4806{
4807 return !(__y < __x);
4808}
4809
4810template <class _BiIter>
4811inline _LIBCPP_INLINE_VISIBILITY
4812bool
4813operator>=(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
4814{
4815 return !(__x < __y);
4816}
4817
4818template <class _BiIter>
4819inline _LIBCPP_INLINE_VISIBILITY
4820bool
4821operator>(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
4822{
4823 return __y < __x;
4824}
4825
4826template <class _BiIter, class _ST, class _SA>
4827inline _LIBCPP_INLINE_VISIBILITY
4828bool
4829operator==(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4830 const sub_match<_BiIter>& __y)
4831{
4832 return __y.compare(__x.c_str()) == 0;
4833}
4834
4835template <class _BiIter, class _ST, class _SA>
4836inline _LIBCPP_INLINE_VISIBILITY
4837bool
4838operator!=(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4839 const sub_match<_BiIter>& __y)
4840{
4841 return !(__x == __y);
4842}
4843
4844template <class _BiIter, class _ST, class _SA>
4845inline _LIBCPP_INLINE_VISIBILITY
4846bool
4847operator<(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4848 const sub_match<_BiIter>& __y)
4849{
4850 return __y.compare(__x.c_str()) > 0;
4851}
4852
4853template <class _BiIter, class _ST, class _SA>
4854inline _LIBCPP_INLINE_VISIBILITY
4855bool
4856operator>(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4857 const sub_match<_BiIter>& __y)
4858{
4859 return __y < __x;
4860}
4861
4862template <class _BiIter, class _ST, class _SA>
4863inline _LIBCPP_INLINE_VISIBILITY
4864bool operator>=(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4865 const sub_match<_BiIter>& __y)
4866{
4867 return !(__x < __y);
4868}
4869
4870template <class _BiIter, class _ST, class _SA>
4871inline _LIBCPP_INLINE_VISIBILITY
4872bool
4873operator<=(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4874 const sub_match<_BiIter>& __y)
4875{
4876 return !(__y < __x);
4877}
4878
4879template <class _BiIter, class _ST, class _SA>
4880inline _LIBCPP_INLINE_VISIBILITY
4881bool
4882operator==(const sub_match<_BiIter>& __x,
4883 const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
4884{
4885 return __x.compare(__y.c_str()) == 0;
4886}
4887
4888template <class _BiIter, class _ST, class _SA>
4889inline _LIBCPP_INLINE_VISIBILITY
4890bool
4891operator!=(const sub_match<_BiIter>& __x,
4892 const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
4893{
4894 return !(__x == __y);
4895}
4896
4897template <class _BiIter, class _ST, class _SA>
4898inline _LIBCPP_INLINE_VISIBILITY
4899bool
4900operator<(const sub_match<_BiIter>& __x,
4901 const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
4902{
4903 return __x.compare(__y.c_str()) < 0;
4904}
4905
4906template <class _BiIter, class _ST, class _SA>
4907inline _LIBCPP_INLINE_VISIBILITY
4908bool operator>(const sub_match<_BiIter>& __x,
4909 const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
4910{
4911 return __y < __x;
4912}
4913
4914template <class _BiIter, class _ST, class _SA>
4915inline _LIBCPP_INLINE_VISIBILITY
4916bool
4917operator>=(const sub_match<_BiIter>& __x,
4918 const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
4919{
4920 return !(__x < __y);
4921}
4922
4923template <class _BiIter, class _ST, class _SA>
4924inline _LIBCPP_INLINE_VISIBILITY
4925bool
4926operator<=(const sub_match<_BiIter>& __x,
4927 const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
4928{
4929 return !(__y < __x);
4930}
4931
4932template <class _BiIter>
4933inline _LIBCPP_INLINE_VISIBILITY
4934bool
4935operator==(typename iterator_traits<_BiIter>::value_type const* __x,
4936 const sub_match<_BiIter>& __y)
4937{
4938 return __y.compare(__x) == 0;
4939}
4940
4941template <class _BiIter>
4942inline _LIBCPP_INLINE_VISIBILITY
4943bool
4944operator!=(typename iterator_traits<_BiIter>::value_type const* __x,
4945 const sub_match<_BiIter>& __y)
4946{
4947 return !(__x == __y);
4948}
4949
4950template <class _BiIter>
4951inline _LIBCPP_INLINE_VISIBILITY
4952bool
4953operator<(typename iterator_traits<_BiIter>::value_type const* __x,
4954 const sub_match<_BiIter>& __y)
4955{
4956 return __y.compare(__x) > 0;
4957}
4958
4959template <class _BiIter>
4960inline _LIBCPP_INLINE_VISIBILITY
4961bool
4962operator>(typename iterator_traits<_BiIter>::value_type const* __x,
4963 const sub_match<_BiIter>& __y)
4964{
4965 return __y < __x;
4966}
4967
4968template <class _BiIter>
4969inline _LIBCPP_INLINE_VISIBILITY
4970bool
4971operator>=(typename iterator_traits<_BiIter>::value_type const* __x,
4972 const sub_match<_BiIter>& __y)
4973{
4974 return !(__x < __y);
4975}
4976
4977template <class _BiIter>
4978inline _LIBCPP_INLINE_VISIBILITY
4979bool
4980operator<=(typename iterator_traits<_BiIter>::value_type const* __x,
4981 const sub_match<_BiIter>& __y)
4982{
4983 return !(__y < __x);
4984}
4985
4986template <class _BiIter>
4987inline _LIBCPP_INLINE_VISIBILITY
4988bool
4989operator==(const sub_match<_BiIter>& __x,
4990 typename iterator_traits<_BiIter>::value_type const* __y)
4991{
4992 return __x.compare(__y) == 0;
4993}
4994
4995template <class _BiIter>
4996inline _LIBCPP_INLINE_VISIBILITY
4997bool
4998operator!=(const sub_match<_BiIter>& __x,
4999 typename iterator_traits<_BiIter>::value_type const* __y)
5000{
5001 return !(__x == __y);
5002}
5003
5004template <class _BiIter>
5005inline _LIBCPP_INLINE_VISIBILITY
5006bool
5007operator<(const sub_match<_BiIter>& __x,
5008 typename iterator_traits<_BiIter>::value_type const* __y)
5009{
5010 return __x.compare(__y) < 0;
5011}
5012
5013template <class _BiIter>
5014inline _LIBCPP_INLINE_VISIBILITY
5015bool
5016operator>(const sub_match<_BiIter>& __x,
5017 typename iterator_traits<_BiIter>::value_type const* __y)
5018{
5019 return __y < __x;
5020}
5021
5022template <class _BiIter>
5023inline _LIBCPP_INLINE_VISIBILITY
5024bool
5025operator>=(const sub_match<_BiIter>& __x,
5026 typename iterator_traits<_BiIter>::value_type const* __y)
5027{
5028 return !(__x < __y);
5029}
5030
5031template <class _BiIter>
5032inline _LIBCPP_INLINE_VISIBILITY
5033bool
5034operator<=(const sub_match<_BiIter>& __x,
5035 typename iterator_traits<_BiIter>::value_type const* __y)
5036{
5037 return !(__y < __x);
5038}
5039
5040template <class _BiIter>
5041inline _LIBCPP_INLINE_VISIBILITY
5042bool
5043operator==(typename iterator_traits<_BiIter>::value_type const& __x,
5044 const sub_match<_BiIter>& __y)
5045{
5046 typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
5047 return __y.compare(string_type(1, __x)) == 0;
5048}
5049
5050template <class _BiIter>
5051inline _LIBCPP_INLINE_VISIBILITY
5052bool
5053operator!=(typename iterator_traits<_BiIter>::value_type const& __x,
5054 const sub_match<_BiIter>& __y)
5055{
5056 return !(__x == __y);
5057}
5058
5059template <class _BiIter>
5060inline _LIBCPP_INLINE_VISIBILITY
5061bool
5062operator<(typename iterator_traits<_BiIter>::value_type const& __x,
5063 const sub_match<_BiIter>& __y)
5064{
5065 typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
5066 return __y.compare(string_type(1, __x)) > 0;
5067}
5068
5069template <class _BiIter>
5070inline _LIBCPP_INLINE_VISIBILITY
5071bool
5072operator>(typename iterator_traits<_BiIter>::value_type const& __x,
5073 const sub_match<_BiIter>& __y)
5074{
5075 return __y < __x;
5076}
5077
5078template <class _BiIter>
5079inline _LIBCPP_INLINE_VISIBILITY
5080bool
5081operator>=(typename iterator_traits<_BiIter>::value_type const& __x,
5082 const sub_match<_BiIter>& __y)
5083{
5084 return !(__x < __y);
5085}
5086
5087template <class _BiIter>
5088inline _LIBCPP_INLINE_VISIBILITY
5089bool
5090operator<=(typename iterator_traits<_BiIter>::value_type const& __x,
5091 const sub_match<_BiIter>& __y)
5092{
5093 return !(__y < __x);
5094}
5095
5096template <class _BiIter>
5097inline _LIBCPP_INLINE_VISIBILITY
5098bool
5099operator==(const sub_match<_BiIter>& __x,
5100 typename iterator_traits<_BiIter>::value_type const& __y)
5101{
5102 typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
5103 return __x.compare(string_type(1, __y)) == 0;
5104}
5105
5106template <class _BiIter>
5107inline _LIBCPP_INLINE_VISIBILITY
5108bool
5109operator!=(const sub_match<_BiIter>& __x,
5110 typename iterator_traits<_BiIter>::value_type const& __y)
5111{
5112 return !(__x == __y);
5113}
5114
5115template <class _BiIter>
5116inline _LIBCPP_INLINE_VISIBILITY
5117bool
5118operator<(const sub_match<_BiIter>& __x,
5119 typename iterator_traits<_BiIter>::value_type const& __y)
5120{
5121 typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
5122 return __x.compare(string_type(1, __y)) < 0;
5123}
5124
5125template <class _BiIter>
5126inline _LIBCPP_INLINE_VISIBILITY
5127bool
5128operator>(const sub_match<_BiIter>& __x,
5129 typename iterator_traits<_BiIter>::value_type const& __y)
5130{
5131 return __y < __x;
5132}
5133
5134template <class _BiIter>
5135inline _LIBCPP_INLINE_VISIBILITY
5136bool
5137operator>=(const sub_match<_BiIter>& __x,
5138 typename iterator_traits<_BiIter>::value_type const& __y)
5139{
5140 return !(__x < __y);
5141}
5142
5143template <class _BiIter>
5144inline _LIBCPP_INLINE_VISIBILITY
5145bool
5146operator<=(const sub_match<_BiIter>& __x,
5147 typename iterator_traits<_BiIter>::value_type const& __y)
5148{
5149 return !(__y < __x);
5150}
5151
5152template <class _CharT, class _ST, class _BiIter>
5153inline _LIBCPP_INLINE_VISIBILITY
5154basic_ostream<_CharT, _ST>&
5155operator<<(basic_ostream<_CharT, _ST>& __os, const sub_match<_BiIter>& __m)
5156{
5157 return __os << __m.str();
5158}
5159
Howard Hinnant17615b02010-07-27 01:25:38 +00005160template <class _BidirectionalIterator, class _Allocator>
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005161class match_results
5162{
5163public:
5164 typedef _Allocator allocator_type;
5165 typedef sub_match<_BidirectionalIterator> value_type;
5166private:
5167 typedef vector<value_type, allocator_type> __container_type;
5168
5169 __container_type __matches_;
5170 value_type __unmatched_;
5171 value_type __prefix_;
5172 value_type __suffix_;
5173public:
Howard Hinnanta712c722010-08-16 20:21:16 +00005174 _BidirectionalIterator __position_start_;
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005175 typedef const value_type& const_reference;
5176 typedef const_reference reference;
5177 typedef typename __container_type::const_iterator const_iterator;
5178 typedef const_iterator iterator;
5179 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
5180 typedef typename allocator_traits<allocator_type>::size_type size_type;
5181 typedef typename iterator_traits<_BidirectionalIterator>::value_type char_type;
5182 typedef basic_string<char_type> string_type;
5183
5184 // construct/copy/destroy:
5185 explicit match_results(const allocator_type& __a = allocator_type());
5186// match_results(const match_results&) = default;
5187// match_results& operator=(const match_results&) = default;
5188#ifdef _LIBCPP_MOVE
5189// match_results(match_results&& __m) = default;
5190// match_results& operator=(match_results&& __m) = default;
Howard Hinnant324bb032010-08-22 00:02:43 +00005191#endif // _LIBCPP_MOVE
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005192// ~match_results() = default;
5193
5194 // size:
5195 size_type size() const {return __matches_.size();}
5196 size_type max_size() const {return __matches_.max_size();}
5197 bool empty() const {return size() == 0;}
5198
5199 // element access:
5200 difference_type length(size_type __sub = 0) const
5201 {return (*this)[__sub].length();}
5202 difference_type position(size_type __sub = 0) const
Howard Hinnanta712c722010-08-16 20:21:16 +00005203 {return _STD::distance(__position_start_, (*this)[__sub].first);}
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005204 string_type str(size_type __sub = 0) const
5205 {return (*this)[__sub].str();}
5206 const_reference operator[](size_type __n) const
5207 {return __n < __matches_.size() ? __matches_[__n] : __unmatched_;}
5208
5209 const_reference prefix() const {return __prefix_;}
5210 const_reference suffix() const {return __suffix_;}
5211
5212 const_iterator begin() const {return empty() ? __matches_.end() : __matches_.begin() + 1;}
5213 const_iterator end() const {return __matches_.end();}
5214 const_iterator cbegin() const {return empty() ? __matches_.end() : __matches_.begin() + 1;}
5215 const_iterator cend() const {return __matches_.end();}
5216
5217 // format:
5218 template <class _OutputIter>
5219 _OutputIter
5220 format(_OutputIter __out, const char_type* __fmt_first,
5221 const char_type* __fmt_last,
5222 regex_constants::match_flag_type __flags = regex_constants::format_default) const;
5223 template <class _OutputIter, class _ST, class _SA>
5224 _OutputIter
5225 format(_OutputIter __out, const basic_string<char_type, _ST, _SA>& __fmt,
Howard Hinnant27405f92010-08-14 18:14:02 +00005226 regex_constants::match_flag_type __flags = regex_constants::format_default) const
5227 {return format(__out, __fmt.data(), __fmt.data() + __fmt.size(), __flags);}
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005228 template <class _ST, class _SA>
5229 basic_string<char_type, _ST, _SA>
5230 format(const basic_string<char_type, _ST, _SA>& __fmt,
Howard Hinnant27405f92010-08-14 18:14:02 +00005231 regex_constants::match_flag_type __flags = regex_constants::format_default) const
5232 {
5233 basic_string<char_type, _ST, _SA> __r;
5234 format(back_inserter(__r), __fmt.data(), __fmt.data() + __fmt.size(),
5235 __flags);
5236 return __r;
5237 }
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005238 string_type
5239 format(const char_type* __fmt,
Howard Hinnant27405f92010-08-14 18:14:02 +00005240 regex_constants::match_flag_type __flags = regex_constants::format_default) const
5241 {
5242 string_type __r;
5243 format(back_inserter(__r), __fmt,
5244 __fmt + char_traits<char_type>::length(__fmt), __flags);
5245 return __r;
5246 }
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005247
5248 // allocator:
5249 allocator_type get_allocator() const {return __matches_.get_allocator();}
5250
5251 // swap:
5252 void swap(match_results& __m);
5253
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005254 template <class _B, class _A>
5255 void __assign(_BidirectionalIterator __f, _BidirectionalIterator __l,
Howard Hinnanta712c722010-08-16 20:21:16 +00005256 const match_results<_B, _A>& __m, bool __no_update_pos)
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005257 {
5258 _B __mf = __m.prefix().first;
5259 __matches_.resize(__m.size());
5260 for (size_type __i = 0; __i < __matches_.size(); ++__i)
5261 {
5262 __matches_[__i].first = next(__f, _STD::distance(__mf, __m[__i].first));
5263 __matches_[__i].second = next(__f, _STD::distance(__mf, __m[__i].second));
5264 __matches_[__i].matched = __m[__i].matched;
5265 }
5266 __unmatched_.first = __l;
5267 __unmatched_.second = __l;
5268 __unmatched_.matched = false;
5269 __prefix_.first = next(__f, _STD::distance(__mf, __m.prefix().first));
5270 __prefix_.second = next(__f, _STD::distance(__mf, __m.prefix().second));
5271 __prefix_.matched = __m.prefix().matched;
5272 __suffix_.first = next(__f, _STD::distance(__mf, __m.suffix().first));
5273 __suffix_.second = next(__f, _STD::distance(__mf, __m.suffix().second));
5274 __suffix_.matched = __m.suffix().matched;
Howard Hinnanta712c722010-08-16 20:21:16 +00005275 if (!__no_update_pos)
5276 __position_start_ = __prefix_.first;
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005277 }
5278
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005279private:
5280 void __init(unsigned __s,
Howard Hinnanta712c722010-08-16 20:21:16 +00005281 _BidirectionalIterator __f, _BidirectionalIterator __l,
5282 bool __no_update_pos = false);
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005283
5284 template <class, class> friend class basic_regex;
5285
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005286 template <class _B, class _A, class _C, class _T>
5287 friend
5288 bool
5289 regex_match(_B, _B, match_results<_B, _A>&, const basic_regex<_C, _T>&,
5290 regex_constants::match_flag_type);
Howard Hinnante9de5ff2010-07-27 22:20:32 +00005291
Howard Hinnant27405f92010-08-14 18:14:02 +00005292 template <class _B, class _A>
5293 friend
5294 bool
5295 operator==(const match_results<_B, _A>&, const match_results<_B, _A>&);
5296
Howard Hinnante9de5ff2010-07-27 22:20:32 +00005297 template <class, class> friend class __lookahead;
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005298};
5299
5300template <class _BidirectionalIterator, class _Allocator>
5301match_results<_BidirectionalIterator, _Allocator>::match_results(
5302 const allocator_type& __a)
5303 : __matches_(__a),
5304 __unmatched_(),
5305 __prefix_(),
Howard Hinnanta712c722010-08-16 20:21:16 +00005306 __suffix_(),
5307 __position_start_()
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005308{
5309}
5310
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005311template <class _BidirectionalIterator, class _Allocator>
5312void
5313match_results<_BidirectionalIterator, _Allocator>::__init(unsigned __s,
Howard Hinnanta712c722010-08-16 20:21:16 +00005314 _BidirectionalIterator __f, _BidirectionalIterator __l,
5315 bool __no_update_pos)
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005316{
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005317 __unmatched_.first = __l;
5318 __unmatched_.second = __l;
5319 __unmatched_.matched = false;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005320 __matches_.assign(__s, __unmatched_);
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005321 __prefix_.first = __f;
5322 __prefix_.second = __f;
5323 __prefix_.matched = false;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005324 __suffix_ = __unmatched_;
Howard Hinnanta712c722010-08-16 20:21:16 +00005325 if (!__no_update_pos)
5326 __position_start_ = __prefix_.first;
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005327}
5328
Howard Hinnant27405f92010-08-14 18:14:02 +00005329template <class _BidirectionalIterator, class _Allocator>
5330template <class _OutputIter>
5331_OutputIter
5332match_results<_BidirectionalIterator, _Allocator>::format(_OutputIter __out,
5333 const char_type* __fmt_first, const char_type* __fmt_last,
5334 regex_constants::match_flag_type __flags) const
5335{
5336 if (__flags & regex_constants::format_sed)
5337 {
5338 for (; __fmt_first != __fmt_last; ++__fmt_first)
5339 {
5340 if (*__fmt_first == '&')
5341 __out = _STD::copy(__matches_[0].first, __matches_[0].second,
5342 __out);
5343 else if (*__fmt_first == '\\' && __fmt_first + 1 != __fmt_last)
5344 {
5345 ++__fmt_first;
5346 if ('0' <= *__fmt_first && *__fmt_first <= '9')
5347 {
5348 size_t __i = *__fmt_first - '0';
5349 __out = _STD::copy(__matches_[__i].first,
5350 __matches_[__i].second, __out);
5351 }
5352 else
5353 {
5354 *__out = *__fmt_first;
5355 ++__out;
5356 }
5357 }
5358 else
5359 {
5360 *__out = *__fmt_first;
5361 ++__out;
5362 }
5363 }
5364 }
5365 else
5366 {
5367 for (; __fmt_first != __fmt_last; ++__fmt_first)
5368 {
5369 if (*__fmt_first == '$' && __fmt_first + 1 != __fmt_last)
5370 {
5371 switch (__fmt_first[1])
5372 {
5373 case '$':
5374 *__out = *++__fmt_first;
5375 ++__out;
5376 break;
5377 case '&':
5378 ++__fmt_first;
5379 __out = _STD::copy(__matches_[0].first, __matches_[0].second,
5380 __out);
5381 break;
5382 case '`':
5383 ++__fmt_first;
5384 __out = _STD::copy(__prefix_.first, __prefix_.second, __out);
5385 break;
5386 case '\'':
5387 ++__fmt_first;
5388 __out = _STD::copy(__suffix_.first, __suffix_.second, __out);
5389 break;
5390 default:
5391 if ('0' <= __fmt_first[1] && __fmt_first[1] <= '9')
5392 {
5393 ++__fmt_first;
5394 size_t __i = *__fmt_first - '0';
5395 if (__fmt_first + 1 != __fmt_last &&
5396 '0' <= __fmt_first[1] && __fmt_first[1] <= '9')
5397 {
5398 ++__fmt_first;
5399 __i = 10 * __i + *__fmt_first - '0';
5400 }
5401 __out = _STD::copy(__matches_[__i].first,
5402 __matches_[__i].second, __out);
5403 }
5404 else
5405 {
5406 *__out = *__fmt_first;
5407 ++__out;
5408 }
5409 break;
5410 }
5411 }
5412 else
5413 {
5414 *__out = *__fmt_first;
5415 ++__out;
5416 }
5417 }
5418 }
5419 return __out;
5420}
5421
5422template <class _BidirectionalIterator, class _Allocator>
5423void
5424match_results<_BidirectionalIterator, _Allocator>::swap(match_results& __m)
5425{
5426 using _STD::swap;
5427 swap(__matches_, __m.__matches_);
5428 swap(__unmatched_, __m.__unmatched_);
5429 swap(__prefix_, __m.__prefix_);
5430 swap(__suffix_, __m.__suffix_);
Howard Hinnanta712c722010-08-16 20:21:16 +00005431 swap(__position_start_, __m.__position_start_);
Howard Hinnant27405f92010-08-14 18:14:02 +00005432}
5433
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005434typedef match_results<const char*> cmatch;
5435typedef match_results<const wchar_t*> wcmatch;
5436typedef match_results<string::const_iterator> smatch;
5437typedef match_results<wstring::const_iterator> wsmatch;
5438
5439template <class _BidirectionalIterator, class _Allocator>
Howard Hinnant27405f92010-08-14 18:14:02 +00005440bool
5441operator==(const match_results<_BidirectionalIterator, _Allocator>& __x,
5442 const match_results<_BidirectionalIterator, _Allocator>& __y)
5443{
5444 return __x.__matches_ == __y.__matches_ &&
5445 __x.__prefix_ == __y.__prefix_ &&
Howard Hinnanta712c722010-08-16 20:21:16 +00005446 __x.__suffix_ == __y.__suffix_ &&
5447 __x.__position_start_ == __y.__position_start_;
Howard Hinnant27405f92010-08-14 18:14:02 +00005448}
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005449
5450template <class _BidirectionalIterator, class _Allocator>
Howard Hinnant27405f92010-08-14 18:14:02 +00005451inline _LIBCPP_INLINE_VISIBILITY
5452bool
5453operator!=(const match_results<_BidirectionalIterator, _Allocator>& __x,
5454 const match_results<_BidirectionalIterator, _Allocator>& __y)
5455{
5456 return !(__x == __y);
5457}
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005458
5459template <class _BidirectionalIterator, class _Allocator>
Howard Hinnant27405f92010-08-14 18:14:02 +00005460inline _LIBCPP_INLINE_VISIBILITY
5461void
5462swap(match_results<_BidirectionalIterator, _Allocator>& __x,
5463 match_results<_BidirectionalIterator, _Allocator>& __y)
5464{
5465 __x.swap(__y);
5466}
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005467
5468// regex_search
5469
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005470template <class _CharT, class _Traits>
Howard Hinnant17615b02010-07-27 01:25:38 +00005471template <class _Allocator>
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005472bool
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005473basic_regex<_CharT, _Traits>::__match_at_start_ecma(
Howard Hinnant17615b02010-07-27 01:25:38 +00005474 const _CharT* __first, const _CharT* __last,
5475 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005476 regex_constants::match_flag_type __flags) const
5477{
Howard Hinnant17615b02010-07-27 01:25:38 +00005478 vector<__state> __states;
5479 ptrdiff_t __j = 0;
5480 ptrdiff_t _N = _STD::distance(__first, __last);
5481 __node* __st = __start_.get();
5482 if (__st)
5483 {
5484 __states.push_back(__state());
5485 __states.back().__do_ = 0;
5486 __states.back().__first_ = __first;
5487 __states.back().__current_ = __first;
5488 __states.back().__last_ = __last;
5489 __states.back().__sub_matches_.resize(mark_count());
5490 __states.back().__loop_data_.resize(__loop_count());
5491 __states.back().__node_ = __st;
5492 __states.back().__flags_ = __flags;
5493 bool __matched = false;
5494 do
5495 {
5496 __state& __s = __states.back();
5497 if (__s.__node_)
5498 __s.__node_->__exec(__s);
5499 switch (__s.__do_)
5500 {
5501 case __state::__end_state:
5502 __m.__matches_[0].first = __first;
5503 __m.__matches_[0].second = _STD::next(__first, __s.__current_ - __first);
5504 __m.__matches_[0].matched = true;
5505 for (unsigned __i = 0; __i < __s.__sub_matches_.size(); ++__i)
5506 __m.__matches_[__i+1] = __s.__sub_matches_[__i];
5507 return true;
5508 case __state::__accept_and_consume:
5509 case __state::__repeat:
5510 case __state::__accept_but_not_consume:
5511 break;
5512 case __state::__split:
5513 {
5514 __state __snext = __s;
5515 __s.__node_->__exec_split(true, __s);
5516 __snext.__node_->__exec_split(false, __snext);
5517 __states.push_back(_STD::move(__snext));
5518 }
5519 break;
5520 case __state::__reject:
5521 __states.pop_back();
5522 break;
5523 default:
Howard Hinnantd4444702010-08-11 17:04:31 +00005524#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005525 throw regex_error(regex_constants::__re_err_unknown);
Howard Hinnantd4444702010-08-11 17:04:31 +00005526#endif
Howard Hinnant17615b02010-07-27 01:25:38 +00005527 break;
Howard Hinnantd4444702010-08-11 17:04:31 +00005528
Howard Hinnant17615b02010-07-27 01:25:38 +00005529 }
5530 } while (!__states.empty());
5531 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005532 return false;
5533}
5534
5535template <class _CharT, class _Traits>
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005536template <class _Allocator>
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005537bool
5538basic_regex<_CharT, _Traits>::__match_at_start_posix_nosubs(
5539 const _CharT* __first, const _CharT* __last,
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005540 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005541 regex_constants::match_flag_type __flags) const
5542{
Howard Hinnantac303862010-07-12 15:51:17 +00005543 deque<__state> __states;
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005544 ptrdiff_t __highest_j = 0;
5545 ptrdiff_t _N = _STD::distance(__first, __last);
Howard Hinnantac303862010-07-12 15:51:17 +00005546 __node* __st = __start_.get();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005547 if (__st)
5548 {
Howard Hinnantac303862010-07-12 15:51:17 +00005549 __states.push_back(__state());
Howard Hinnantac303862010-07-12 15:51:17 +00005550 __states.back().__do_ = 0;
5551 __states.back().__first_ = __first;
5552 __states.back().__current_ = __first;
5553 __states.back().__last_ = __last;
5554 __states.back().__loop_data_.resize(__loop_count());
5555 __states.back().__node_ = __st;
5556 __states.back().__flags_ = __flags;
Howard Hinnant68025ed2010-07-14 15:45:11 +00005557 bool __matched = false;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005558 do
5559 {
Howard Hinnantac303862010-07-12 15:51:17 +00005560 __state& __s = __states.back();
5561 if (__s.__node_)
5562 __s.__node_->__exec(__s);
5563 switch (__s.__do_)
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005564 {
Howard Hinnantac303862010-07-12 15:51:17 +00005565 case __state::__end_state:
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005566 if (!__matched || __highest_j < __s.__current_ - __s.__first_)
Howard Hinnant68025ed2010-07-14 15:45:11 +00005567 __highest_j = __s.__current_ - __s.__first_;
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005568 __matched = true;
Howard Hinnantac303862010-07-12 15:51:17 +00005569 if (__highest_j == _N)
5570 __states.clear();
5571 else
5572 __states.pop_back();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005573 break;
Howard Hinnantac303862010-07-12 15:51:17 +00005574 case __state::__consume_input:
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005575 break;
Howard Hinnantac303862010-07-12 15:51:17 +00005576 case __state::__accept_and_consume:
Howard Hinnantac303862010-07-12 15:51:17 +00005577 __states.push_front(_STD::move(__s));
5578 __states.pop_back();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005579 break;
Howard Hinnantac303862010-07-12 15:51:17 +00005580 case __state::__repeat:
5581 case __state::__accept_but_not_consume:
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005582 break;
Howard Hinnantac303862010-07-12 15:51:17 +00005583 case __state::__split:
5584 {
5585 __state __snext = __s;
5586 __s.__node_->__exec_split(true, __s);
5587 __snext.__node_->__exec_split(false, __snext);
5588 __states.push_back(_STD::move(__snext));
5589 }
5590 break;
5591 case __state::__reject:
5592 __states.pop_back();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005593 break;
5594 default:
Howard Hinnantd4444702010-08-11 17:04:31 +00005595#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005596 throw regex_error(regex_constants::__re_err_unknown);
Howard Hinnantd4444702010-08-11 17:04:31 +00005597#endif
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005598 break;
5599 }
Howard Hinnantac303862010-07-12 15:51:17 +00005600 } while (!__states.empty());
Howard Hinnant68025ed2010-07-14 15:45:11 +00005601 if (__matched)
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005602 {
5603 __m.__matches_[0].first = __first;
5604 __m.__matches_[0].second = _STD::next(__first, __highest_j);
5605 __m.__matches_[0].matched = true;
5606 return true;
5607 }
5608 }
5609 return false;
5610}
5611
5612template <class _CharT, class _Traits>
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005613template <class _Allocator>
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005614bool
5615basic_regex<_CharT, _Traits>::__match_at_start_posix_subs(
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005616 const _CharT* __first, const _CharT* __last,
5617 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005618 regex_constants::match_flag_type __flags) const
5619{
Howard Hinnantac303862010-07-12 15:51:17 +00005620 vector<__state> __states;
Howard Hinnantac303862010-07-12 15:51:17 +00005621 __state __best_state;
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005622 ptrdiff_t __j = 0;
5623 ptrdiff_t __highest_j = 0;
5624 ptrdiff_t _N = _STD::distance(__first, __last);
Howard Hinnantac303862010-07-12 15:51:17 +00005625 __node* __st = __start_.get();
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005626 if (__st)
5627 {
Howard Hinnantac303862010-07-12 15:51:17 +00005628 __states.push_back(__state());
5629 __states.back().__do_ = 0;
5630 __states.back().__first_ = __first;
5631 __states.back().__current_ = __first;
5632 __states.back().__last_ = __last;
5633 __states.back().__sub_matches_.resize(mark_count());
5634 __states.back().__loop_data_.resize(__loop_count());
5635 __states.back().__node_ = __st;
5636 __states.back().__flags_ = __flags;
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005637 const _CharT* __current = __first;
Howard Hinnantac303862010-07-12 15:51:17 +00005638 bool __matched = false;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005639 do
5640 {
Howard Hinnantac303862010-07-12 15:51:17 +00005641 __state& __s = __states.back();
5642 if (__s.__node_)
5643 __s.__node_->__exec(__s);
5644 switch (__s.__do_)
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005645 {
Howard Hinnantac303862010-07-12 15:51:17 +00005646 case __state::__end_state:
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005647 if (!__matched || __highest_j < __s.__current_ - __s.__first_)
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005648 {
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005649 __highest_j = __s.__current_ - __s.__first_;
Howard Hinnantac303862010-07-12 15:51:17 +00005650 __best_state = __s;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005651 }
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005652 __matched = true;
5653 if (__highest_j == _N)
5654 __states.clear();
5655 else
5656 __states.pop_back();
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005657 break;
Howard Hinnantac303862010-07-12 15:51:17 +00005658 case __state::__accept_and_consume:
Howard Hinnantcba352d2010-07-12 18:16:05 +00005659 __j += __s.__current_ - __current;
5660 __current = __s.__current_;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005661 break;
Howard Hinnantac303862010-07-12 15:51:17 +00005662 case __state::__repeat:
5663 case __state::__accept_but_not_consume:
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005664 break;
Howard Hinnantac303862010-07-12 15:51:17 +00005665 case __state::__split:
5666 {
5667 __state __snext = __s;
5668 __s.__node_->__exec_split(true, __s);
5669 __snext.__node_->__exec_split(false, __snext);
5670 __states.push_back(_STD::move(__snext));
5671 }
5672 break;
5673 case __state::__reject:
5674 __states.pop_back();
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005675 break;
5676 default:
Howard Hinnantd4444702010-08-11 17:04:31 +00005677#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005678 throw regex_error(regex_constants::__re_err_unknown);
Howard Hinnantd4444702010-08-11 17:04:31 +00005679#endif
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005680 break;
5681 }
Howard Hinnantac303862010-07-12 15:51:17 +00005682 } while (!__states.empty());
5683 if (__matched)
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005684 {
5685 __m.__matches_[0].first = __first;
5686 __m.__matches_[0].second = _STD::next(__first, __highest_j);
5687 __m.__matches_[0].matched = true;
Howard Hinnantac303862010-07-12 15:51:17 +00005688 for (unsigned __i = 0; __i < __best_state.__sub_matches_.size(); ++__i)
5689 __m.__matches_[__i+1] = __best_state.__sub_matches_[__i];
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005690 return true;
5691 }
5692 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005693 return false;
5694}
5695
5696template <class _CharT, class _Traits>
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005697template <class _Allocator>
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005698bool
5699basic_regex<_CharT, _Traits>::__match_at_start(
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005700 const _CharT* __first, const _CharT* __last,
5701 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005702 regex_constants::match_flag_type __flags) const
5703{
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005704 if ((__flags_ & 0x1F0) == ECMAScript)
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005705 return __match_at_start_ecma(__first, __last, __m, __flags);
5706 if (mark_count() == 0)
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005707 return __match_at_start_posix_nosubs(__first, __last, __m, __flags);
5708 return __match_at_start_posix_subs(__first, __last, __m, __flags);
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005709}
5710
5711template <class _CharT, class _Traits>
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005712template <class _Allocator>
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005713bool
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005714basic_regex<_CharT, _Traits>::__search(
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005715 const _CharT* __first, const _CharT* __last,
5716 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005717 regex_constants::match_flag_type __flags) const
5718{
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00005719 if (__left_anchor_)
5720 __flags |= regex_constants::match_continuous;
Howard Hinnanta712c722010-08-16 20:21:16 +00005721 __m.__init(1 + mark_count(), __first, __last,
5722 __flags & regex_constants::__no_update_pos);
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005723 if (__match_at_start(__first, __last, __m, __flags))
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005724 {
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005725 __m.__prefix_.second = __m[0].first;
5726 __m.__prefix_.matched = __m.__prefix_.first != __m.__prefix_.second;
5727 __m.__suffix_.first = __m[0].second;
5728 __m.__suffix_.matched = __m.__suffix_.first != __m.__suffix_.second;
5729 return true;
5730 }
Howard Hinnant8daa7332010-07-29 01:15:27 +00005731 if (__first != __last && !(__flags & regex_constants::match_continuous))
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005732 {
Howard Hinnantf3dcca02010-07-29 15:17:28 +00005733 __flags |= regex_constants::match_prev_avail;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005734 for (++__first; __first != __last; ++__first)
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005735 {
Howard Hinnantf3dcca02010-07-29 15:17:28 +00005736 __m.__matches_.assign(__m.size(), __m.__unmatched_);
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005737 if (__match_at_start(__first, __last, __m, __flags))
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005738 {
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005739 __m.__prefix_.second = __m[0].first;
5740 __m.__prefix_.matched = __m.__prefix_.first != __m.__prefix_.second;
5741 __m.__suffix_.first = __m[0].second;
5742 __m.__suffix_.matched = __m.__suffix_.first != __m.__suffix_.second;
5743 return true;
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005744 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005745 __m.__matches_.assign(__m.size(), __m.__unmatched_);
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005746 }
5747 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005748 __m.__matches_.clear();
5749 return false;
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005750}
5751
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005752template <class _BidirectionalIterator, class _Allocator, class _CharT, class _Traits>
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005753inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005754bool
5755regex_search(_BidirectionalIterator __first, _BidirectionalIterator __last,
5756 match_results<_BidirectionalIterator, _Allocator>& __m,
5757 const basic_regex<_CharT, _Traits>& __e,
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005758 regex_constants::match_flag_type __flags = regex_constants::match_default)
5759{
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005760 basic_string<_CharT> __s(__first, __last);
5761 match_results<const _CharT*> __mc;
5762 bool __r = __e.__search(__s.data(), __s.data() + __s.size(), __mc, __flags);
Howard Hinnanta712c722010-08-16 20:21:16 +00005763 __m.__assign(__first, __last, __mc, __flags & regex_constants::__no_update_pos);
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005764 return __r;
5765}
5766
5767template <class _Allocator, class _CharT, class _Traits>
5768inline _LIBCPP_INLINE_VISIBILITY
5769bool
5770regex_search(const _CharT* __first, const _CharT* __last,
5771 match_results<const _CharT*, _Allocator>& __m,
5772 const basic_regex<_CharT, _Traits>& __e,
5773 regex_constants::match_flag_type __flags = regex_constants::match_default)
5774{
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005775 return __e.__search(__first, __last, __m, __flags);
5776}
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005777
5778template <class _BidirectionalIterator, class _CharT, class _Traits>
5779inline _LIBCPP_INLINE_VISIBILITY
5780bool
5781regex_search(_BidirectionalIterator __first, _BidirectionalIterator __last,
5782 const basic_regex<_CharT, _Traits>& __e,
5783 regex_constants::match_flag_type __flags = regex_constants::match_default)
5784{
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005785 basic_string<_CharT> __s(__first, __last);
5786 match_results<const _CharT*> __mc;
5787 return __e.__search(__s.data(), __s.data() + __s.size(), __mc, __flags);
5788}
5789
5790template <class _CharT, class _Traits>
5791inline _LIBCPP_INLINE_VISIBILITY
5792bool
5793regex_search(const _CharT* __first, const _CharT* __last,
5794 const basic_regex<_CharT, _Traits>& __e,
5795 regex_constants::match_flag_type __flags = regex_constants::match_default)
5796{
5797 match_results<const _CharT*> __mc;
5798 return __e.__search(__first, __last, __mc, __flags);
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005799}
5800
5801template <class _CharT, class _Allocator, class _Traits>
5802inline _LIBCPP_INLINE_VISIBILITY
5803bool
5804regex_search(const _CharT* __str, match_results<const _CharT*, _Allocator>& __m,
5805 const basic_regex<_CharT, _Traits>& __e,
5806 regex_constants::match_flag_type __flags = regex_constants::match_default)
5807{
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005808 return __e.__search(__str, __str + _Traits::length(__str), __m, __flags);
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005809}
5810
5811template <class _CharT, class _Traits>
5812inline _LIBCPP_INLINE_VISIBILITY
5813bool
5814regex_search(const _CharT* __str, const basic_regex<_CharT, _Traits>& __e,
5815 regex_constants::match_flag_type __flags = regex_constants::match_default)
5816{
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005817 match_results<const _CharT*> __m;
5818 return _STD::regex_search(__str, __m, __e, __flags);
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005819}
5820
5821template <class _ST, class _SA, class _CharT, class _Traits>
5822inline _LIBCPP_INLINE_VISIBILITY
5823bool
5824regex_search(const basic_string<_CharT, _ST, _SA>& __s,
5825 const basic_regex<_CharT, _Traits>& __e,
5826 regex_constants::match_flag_type __flags = regex_constants::match_default)
5827{
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005828 match_results<const _CharT*> __mc;
5829 return __e.__search(__s.data(), __s.data() + __s.size(), __mc, __flags);
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005830}
5831
5832template <class _ST, class _SA, class _Allocator, class _CharT, class _Traits>
5833inline _LIBCPP_INLINE_VISIBILITY
5834bool
5835regex_search(const basic_string<_CharT, _ST, _SA>& __s,
5836 match_results<typename basic_string<_CharT, _ST, _SA>::const_iterator, _Allocator>& __m,
5837 const basic_regex<_CharT, _Traits>& __e,
5838 regex_constants::match_flag_type __flags = regex_constants::match_default)
5839{
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005840 match_results<const _CharT*> __mc;
5841 bool __r = __e.__search(__s.data(), __s.data() + __s.size(), __mc, __flags);
Howard Hinnanta712c722010-08-16 20:21:16 +00005842 __m.__assign(__s.begin(), __s.end(), __mc, __flags & regex_constants::__no_update_pos);
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005843 return __r;
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005844}
5845
5846// regex_match
5847
5848template <class _BidirectionalIterator, class _Allocator, class _CharT, class _Traits>
5849bool
5850regex_match(_BidirectionalIterator __first, _BidirectionalIterator __last,
5851 match_results<_BidirectionalIterator, _Allocator>& __m,
5852 const basic_regex<_CharT, _Traits>& __e,
5853 regex_constants::match_flag_type __flags = regex_constants::match_default)
5854{
5855 bool __r = _STD::regex_search(__first, __last, __m, __e,
5856 __flags | regex_constants::match_continuous);
5857 if (__r)
5858 {
5859 __r = !__m.suffix().matched;
5860 if (!__r)
5861 __m.__matches_.clear();
5862 }
5863 return __r;
5864}
5865
5866template <class _BidirectionalIterator, class _CharT, class _Traits>
5867inline _LIBCPP_INLINE_VISIBILITY
5868bool
5869regex_match(_BidirectionalIterator __first, _BidirectionalIterator __last,
5870 const basic_regex<_CharT, _Traits>& __e,
5871 regex_constants::match_flag_type __flags = regex_constants::match_default)
5872{
5873 match_results<_BidirectionalIterator> __m;
5874 return _STD::regex_match(__first, __last, __m, __e, __flags);
5875}
5876
5877template <class _CharT, class _Allocator, class _Traits>
5878inline _LIBCPP_INLINE_VISIBILITY
5879bool
5880regex_match(const _CharT* __str, match_results<const _CharT*, _Allocator>& __m,
5881 const basic_regex<_CharT, _Traits>& __e,
5882 regex_constants::match_flag_type __flags = regex_constants::match_default)
5883{
5884 return _STD::regex_match(__str, __str + _Traits::length(__str), __m, __e, __flags);
5885}
5886
5887template <class _ST, class _SA, class _Allocator, class _CharT, class _Traits>
5888inline _LIBCPP_INLINE_VISIBILITY
5889bool
5890regex_match(const basic_string<_CharT, _ST, _SA>& __s,
5891 match_results<typename basic_string<_CharT, _ST, _SA>::const_iterator, _Allocator>& __m,
5892 const basic_regex<_CharT, _Traits>& __e,
5893 regex_constants::match_flag_type __flags = regex_constants::match_default)
5894{
5895 return _STD::regex_match(__s.begin(), __s.end(), __m, __e, __flags);
5896}
5897
5898template <class _CharT, class _Traits>
5899inline _LIBCPP_INLINE_VISIBILITY
5900bool
5901regex_match(const _CharT* __str, const basic_regex<_CharT, _Traits>& __e,
5902 regex_constants::match_flag_type __flags = regex_constants::match_default)
5903{
5904 return _STD::regex_match(__str, __str + _Traits::length(__str), __e, __flags);
5905}
5906
5907template <class _ST, class _SA, class _CharT, class _Traits>
5908inline _LIBCPP_INLINE_VISIBILITY
5909bool
5910regex_match(const basic_string<_CharT, _ST, _SA>& __s,
5911 const basic_regex<_CharT, _Traits>& __e,
5912 regex_constants::match_flag_type __flags = regex_constants::match_default)
5913{
5914 return _STD::regex_match(__s.begin(), __s.end(), __e, __flags);
5915}
5916
Howard Hinnanta712c722010-08-16 20:21:16 +00005917// regex_iterator
5918
5919template <class _BidirectionalIterator,
5920 class _CharT = typename iterator_traits<_BidirectionalIterator>::value_type,
5921 class _Traits = regex_traits<_CharT> >
5922class regex_iterator
5923{
5924public:
5925 typedef basic_regex<_CharT, _Traits> regex_type;
5926 typedef match_results<_BidirectionalIterator> value_type;
5927 typedef ptrdiff_t difference_type;
5928 typedef const value_type* pointer;
5929 typedef const value_type& reference;
5930 typedef forward_iterator_tag iterator_category;
5931
5932private:
5933 _BidirectionalIterator __begin_;
5934 _BidirectionalIterator __end_;
5935 const regex_type* __pregex_;
5936 regex_constants::match_flag_type __flags_;
5937 value_type __match_;
5938
5939public:
5940 regex_iterator();
5941 regex_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
5942 const regex_type& __re,
5943 regex_constants::match_flag_type __m = regex_constants::match_default);
5944
5945 bool operator==(const regex_iterator& __x) const;
5946 bool operator!=(const regex_iterator& __x) const {return !(*this == __x);}
5947
5948 reference operator*() const {return __match_;}
5949 pointer operator->() const {return &__match_;}
5950
5951 regex_iterator& operator++();
5952 regex_iterator operator++(int)
5953 {
5954 regex_iterator __t(*this);
5955 ++(*this);
5956 return __t;
5957 }
5958};
5959
5960template <class _BidirectionalIterator, class _CharT, class _Traits>
5961regex_iterator<_BidirectionalIterator, _CharT, _Traits>::regex_iterator()
5962 : __begin_(), __end_(), __pregex_(nullptr), __flags_(), __match_()
5963{
5964}
5965
5966template <class _BidirectionalIterator, class _CharT, class _Traits>
5967regex_iterator<_BidirectionalIterator, _CharT, _Traits>::
5968 regex_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
5969 const regex_type& __re, regex_constants::match_flag_type __m)
5970 : __begin_(__a),
5971 __end_(__b),
5972 __pregex_(&__re),
5973 __flags_(__m)
5974{
5975 _STD::regex_search(__begin_, __end_, __match_, *__pregex_, __flags_);
5976}
5977
5978template <class _BidirectionalIterator, class _CharT, class _Traits>
5979bool
5980regex_iterator<_BidirectionalIterator, _CharT, _Traits>::
5981 operator==(const regex_iterator& __x) const
5982{
5983 if (__match_.empty() && __x.__match_.empty())
5984 return true;
5985 if (__match_.empty() || __x.__match_.empty())
5986 return false;
5987 return __begin_ == __x.__begin_ &&
5988 __end_ == __x.__end_ &&
5989 __pregex_ == __x.__pregex_ &&
5990 __flags_ == __x.__flags_ &&
5991 __match_[0] == __x.__match_[0];
5992}
5993
5994template <class _BidirectionalIterator, class _CharT, class _Traits>
5995regex_iterator<_BidirectionalIterator, _CharT, _Traits>&
5996regex_iterator<_BidirectionalIterator, _CharT, _Traits>::operator++()
5997{
5998 __flags_ |= regex_constants::__no_update_pos;
5999 _BidirectionalIterator __start = __match_[0].second;
6000 if (__match_.length() == 0)
6001 {
6002 if (__start == __end_)
6003 {
6004 __match_ = value_type();
6005 return *this;
6006 }
6007 else if (_STD::regex_search(__start, __end_, __match_, *__pregex_,
6008 __flags_ | regex_constants::match_not_null |
6009 regex_constants::match_continuous))
6010 return *this;
6011 else
6012 ++__start;
6013 }
6014 __flags_ |= regex_constants::match_prev_avail;
6015 if (!_STD::regex_search(__start, __end_, __match_, *__pregex_, __flags_))
6016 __match_ = value_type();
6017 return *this;
6018}
6019
6020typedef regex_iterator<const char*> cregex_iterator;
6021typedef regex_iterator<const wchar_t*> wcregex_iterator;
6022typedef regex_iterator<string::const_iterator> sregex_iterator;
6023typedef regex_iterator<wstring::const_iterator> wsregex_iterator;
6024
6025// regex_token_iterator
6026
6027template <class _BidirectionalIterator,
6028 class _CharT = typename iterator_traits<_BidirectionalIterator>::value_type,
6029 class _Traits = regex_traits<_CharT> >
6030class regex_token_iterator
6031{
6032public:
6033 typedef basic_regex<_CharT, _Traits> regex_type;
6034 typedef sub_match<_BidirectionalIterator> value_type;
6035 typedef ptrdiff_t difference_type;
6036 typedef const value_type* pointer;
6037 typedef const value_type& reference;
6038 typedef forward_iterator_tag iterator_category;
6039
Howard Hinnant262b7792010-08-17 20:42:03 +00006040private:
6041 typedef regex_iterator<_BidirectionalIterator, _CharT, _Traits> _Position;
6042
6043 _Position __position_;
6044 const value_type* __result_;
6045 value_type __suffix_;
6046 ptrdiff_t _N_;
6047 vector<int> __subs_;
6048
6049public:
Howard Hinnanta712c722010-08-16 20:21:16 +00006050 regex_token_iterator();
6051 regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6052 const regex_type& __re, int __submatch = 0,
Howard Hinnant262b7792010-08-17 20:42:03 +00006053 regex_constants::match_flag_type __m =
6054 regex_constants::match_default);
Howard Hinnanta712c722010-08-16 20:21:16 +00006055 regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6056 const regex_type& __re, const vector<int>& __submatches,
Howard Hinnant262b7792010-08-17 20:42:03 +00006057 regex_constants::match_flag_type __m =
6058 regex_constants::match_default);
Howard Hinnanta712c722010-08-16 20:21:16 +00006059 regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
Howard Hinnant262b7792010-08-17 20:42:03 +00006060 const regex_type& __re,
6061 initializer_list<int> __submatches,
6062 regex_constants::match_flag_type __m =
6063 regex_constants::match_default);
Howard Hinnanta712c722010-08-16 20:21:16 +00006064 template <size_t _N>
Howard Hinnant262b7792010-08-17 20:42:03 +00006065 regex_token_iterator(_BidirectionalIterator __a,
6066 _BidirectionalIterator __b,
6067 const regex_type& __re,
6068 const int (&__submatches)[_N],
6069 regex_constants::match_flag_type __m =
6070 regex_constants::match_default);
Howard Hinnanta712c722010-08-16 20:21:16 +00006071 regex_token_iterator(const regex_token_iterator&);
6072 regex_token_iterator& operator=(const regex_token_iterator&);
6073
Howard Hinnant262b7792010-08-17 20:42:03 +00006074 bool operator==(const regex_token_iterator& __x) const;
6075 bool operator!=(const regex_token_iterator& __x) const {return !(*this == __x);}
Howard Hinnanta712c722010-08-16 20:21:16 +00006076
Howard Hinnant262b7792010-08-17 20:42:03 +00006077 const value_type& operator*() const {return *__result_;}
6078 const value_type* operator->() const {return __result_;}
Howard Hinnanta712c722010-08-16 20:21:16 +00006079
6080 regex_token_iterator& operator++();
Howard Hinnant262b7792010-08-17 20:42:03 +00006081 regex_token_iterator operator++(int)
6082 {
6083 regex_token_iterator __t(*this);
6084 ++(*this);
6085 return __t;
6086 }
6087
6088private:
6089 void __init(_BidirectionalIterator __a, _BidirectionalIterator __b);
Howard Hinnanta712c722010-08-16 20:21:16 +00006090};
6091
Howard Hinnant262b7792010-08-17 20:42:03 +00006092template <class _BidirectionalIterator, class _CharT, class _Traits>
6093regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6094 regex_token_iterator()
6095 : __result_(nullptr),
6096 __suffix_(),
6097 _N_(0)
6098{
6099}
6100
6101template <class _BidirectionalIterator, class _CharT, class _Traits>
6102void
6103regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6104 __init(_BidirectionalIterator __a, _BidirectionalIterator __b)
6105{
6106 if (__position_ != _Position())
6107 {
6108 if (__subs_[_N_] == -1)
6109 __result_ = &__position_->prefix();
6110 else
6111 __result_ = &(*__position_)[__subs_[_N_]];
6112 }
6113 else if (__subs_[_N_] == -1)
6114 {
6115 __suffix_.matched = true;
6116 __suffix_.first = __a;
6117 __suffix_.second = __b;
6118 __result_ = &__suffix_;
6119 }
6120 else
6121 __result_ = nullptr;
6122}
6123
6124template <class _BidirectionalIterator, class _CharT, class _Traits>
6125regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6126 regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6127 const regex_type& __re, int __submatch,
6128 regex_constants::match_flag_type __m)
6129 : __position_(__a, __b, __re, __m),
6130 _N_(0),
6131 __subs_(1, __submatch)
6132{
6133 __init(__a, __b);
6134}
6135
6136template <class _BidirectionalIterator, class _CharT, class _Traits>
6137regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6138 regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6139 const regex_type& __re, const vector<int>& __submatches,
6140 regex_constants::match_flag_type __m)
6141 : __position_(__a, __b, __re, __m),
6142 _N_(0),
6143 __subs_(__submatches)
6144{
6145 __init(__a, __b);
6146}
6147
6148template <class _BidirectionalIterator, class _CharT, class _Traits>
6149regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6150 regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6151 const regex_type& __re,
6152 initializer_list<int> __submatches,
6153 regex_constants::match_flag_type __m)
6154 : __position_(__a, __b, __re, __m),
6155 _N_(0),
6156 __subs_(__submatches)
6157{
6158 __init(__a, __b);
6159}
6160
6161template <class _BidirectionalIterator, class _CharT, class _Traits>
6162template <size_t _N>
6163regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6164 regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6165 const regex_type& __re,
6166 const int (&__submatches)[_N],
6167 regex_constants::match_flag_type __m)
6168 : __position_(__a, __b, __re, __m),
6169 _N_(0),
6170 __subs_(__submatches, __submatches + _N)
6171{
6172 __init(__a, __b);
6173}
6174
6175template <class _BidirectionalIterator, class _CharT, class _Traits>
6176regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6177 regex_token_iterator(const regex_token_iterator& __x)
6178 : __position_(__x.__position_),
6179 __result_(__x.__result_),
6180 __suffix_(__x.__suffix_),
6181 _N_(__x._N_),
6182 __subs_(__x.__subs_)
6183{
6184 if (__x.__result_ == &__x.__suffix_)
6185 __result_ == &__suffix_;
6186}
6187
6188template <class _BidirectionalIterator, class _CharT, class _Traits>
6189regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>&
6190regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6191 operator=(const regex_token_iterator& __x)
6192{
6193 if (this != &__x)
6194 {
6195 __position_ = __x.__position_;
6196 if (__x.__result_ == &__x.__suffix_)
6197 __result_ == &__suffix_;
6198 else
6199 __result_ = __x.__result_;
6200 __suffix_ = __x.__suffix_;
6201 _N_ = __x._N_;
6202 __subs_ = __x.__subs_;
6203 }
6204 return *this;
6205}
6206
6207template <class _BidirectionalIterator, class _CharT, class _Traits>
6208bool
6209regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6210 operator==(const regex_token_iterator& __x) const
6211{
6212 if (__result_ == nullptr && __x.__result_ == nullptr)
6213 return true;
6214 if (__result_ == &__suffix_ && __x.__result_ == &__x.__suffix_ &&
6215 __suffix_ == __x.__suffix_)
6216 return true;
6217 if (__result_ == nullptr || __x.__result_ == nullptr)
6218 return false;
6219 if (__result_ == &__suffix_ || __x.__result_ == &__x.__suffix_)
6220 return false;
6221 return __position_ == __x.__position_ && _N_ == __x._N_ &&
6222 __subs_ == __x.__subs_;
6223}
6224
6225template <class _BidirectionalIterator, class _CharT, class _Traits>
6226regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>&
6227regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::operator++()
6228{
6229 _Position __prev = __position_;
6230 if (__result_ == &__suffix_)
6231 __result_ = nullptr;
6232 else if (_N_ + 1 < __subs_.size())
6233 {
6234 ++_N_;
6235 if (__subs_[_N_] == -1)
6236 __result_ = &__position_->prefix();
6237 else
6238 __result_ = &(*__position_)[__subs_[_N_]];
6239 }
6240 else
6241 {
6242 _N_ = 0;
6243 ++__position_;
6244 if (__position_ != _Position())
6245 {
6246 if (__subs_[_N_] == -1)
6247 __result_ = &__position_->prefix();
6248 else
6249 __result_ = &(*__position_)[__subs_[_N_]];
6250 }
6251 else
6252 {
6253 if (_STD::find(__subs_.begin(), __subs_.end(), -1) != __subs_.end()
6254 && __prev->suffix().length() != 0)
6255 {
6256 __suffix_.matched = true;
6257 __suffix_.first = __prev->suffix().first;
6258 __suffix_.second = __prev->suffix().second;
6259 __result_ = &__suffix_;
6260 }
6261 else
6262 __result_ = nullptr;
6263 }
6264 }
6265 return *this;
6266}
6267
Howard Hinnanta712c722010-08-16 20:21:16 +00006268typedef regex_token_iterator<const char*> cregex_token_iterator;
6269typedef regex_token_iterator<const wchar_t*> wcregex_token_iterator;
6270typedef regex_token_iterator<string::const_iterator> sregex_token_iterator;
6271typedef regex_token_iterator<wstring::const_iterator> wsregex_token_iterator;
6272
Howard Hinnanta8d77592010-08-18 00:13:08 +00006273// regex_replace
6274
6275template <class _OutputIterator, class _BidirectionalIterator,
6276 class _Traits, class _CharT>
6277_OutputIterator
6278regex_replace(_OutputIterator __out,
6279 _BidirectionalIterator __first, _BidirectionalIterator __last,
6280 const basic_regex<_CharT, _Traits>& __e, const _CharT* __fmt,
6281 regex_constants::match_flag_type __flags = regex_constants::match_default)
6282{
6283 typedef regex_iterator<_BidirectionalIterator, _CharT, _Traits> _Iter;
6284 _Iter __i(__first, __last, __e, __flags);
6285 _Iter __eof;
6286 if (__i == __eof)
6287 {
6288 if (!(__flags & regex_constants::format_no_copy))
6289 __out = _STD::copy(__first, __last, __out);
6290 }
6291 else
6292 {
6293 sub_match<_BidirectionalIterator> __lm;
6294 for (size_t __len = char_traits<_CharT>::length(__fmt); __i != __eof; ++__i)
6295 {
6296 if (!(__flags & regex_constants::format_no_copy))
6297 __out = _STD::copy(__i->prefix().first, __i->prefix().second, __out);
6298 __out = __i->format(__out, __fmt, __fmt + __len, __flags);
6299 __lm = __i->suffix();
6300 if (__flags & regex_constants::format_first_only)
6301 break;
6302 }
6303 if (!(__flags & regex_constants::format_no_copy))
6304 __out = _STD::copy(__lm.first, __lm.second, __out);
6305 }
6306 return __out;
6307}
6308
6309template <class _OutputIterator, class _BidirectionalIterator,
6310 class _Traits, class _CharT, class _ST, class _SA>
6311inline _LIBCPP_INLINE_VISIBILITY
6312_OutputIterator
6313regex_replace(_OutputIterator __out,
6314 _BidirectionalIterator __first, _BidirectionalIterator __last,
6315 const basic_regex<_CharT, _Traits>& __e,
6316 const basic_string<_CharT, _ST, _SA>& __fmt,
6317 regex_constants::match_flag_type __flags = regex_constants::match_default)
6318{
6319 return _STD::regex_replace(__out, __first, __last, __e, __fmt.c_str(), __flags);
6320}
6321
6322template <class _Traits, class _CharT, class _ST, class _SA, class _FST,
6323 class _FSA>
6324inline _LIBCPP_INLINE_VISIBILITY
6325basic_string<_CharT, _ST, _SA>
6326regex_replace(const basic_string<_CharT, _ST, _SA>& __s,
6327 const basic_regex<_CharT, _Traits>& __e,
6328 const basic_string<_CharT, _FST, _FSA>& __fmt,
6329 regex_constants::match_flag_type __flags = regex_constants::match_default)
6330{
6331 basic_string<_CharT, _ST, _SA> __r;
6332 _STD::regex_replace(back_inserter(__r), __s.begin(), __s.end(), __e,
6333 __fmt.c_str(), __flags);
6334 return __r;
6335}
6336
6337template <class _Traits, class _CharT, class _ST, class _SA>
6338inline _LIBCPP_INLINE_VISIBILITY
6339basic_string<_CharT, _ST, _SA>
6340regex_replace(const basic_string<_CharT, _ST, _SA>& __s,
6341 const basic_regex<_CharT, _Traits>& __e, const _CharT* __fmt,
6342 regex_constants::match_flag_type __flags = regex_constants::match_default)
6343{
6344 basic_string<_CharT, _ST, _SA> __r;
6345 _STD::regex_replace(back_inserter(__r), __s.begin(), __s.end(), __e,
6346 __fmt, __flags);
6347 return __r;
6348}
6349
6350template <class _Traits, class _CharT, class _ST, class _SA>
6351inline _LIBCPP_INLINE_VISIBILITY
6352basic_string<_CharT>
6353regex_replace(const _CharT* __s,
6354 const basic_regex<_CharT, _Traits>& __e,
6355 const basic_string<_CharT, _ST, _SA>& __fmt,
6356 regex_constants::match_flag_type __flags = regex_constants::match_default)
6357{
6358 basic_string<_CharT> __r;
6359 _STD::regex_replace(back_inserter(__r), __s,
6360 __s + char_traits<_CharT>::length(__s), __e,
6361 __fmt.c_str(), __flags);
6362 return __r;
6363}
6364
6365template <class _Traits, class _CharT>
6366inline _LIBCPP_INLINE_VISIBILITY
6367basic_string<_CharT>
6368regex_replace(const _CharT* __s,
6369 const basic_regex<_CharT, _Traits>& __e,
6370 const _CharT* __fmt,
6371 regex_constants::match_flag_type __flags = regex_constants::match_default)
6372{
6373 basic_string<_CharT> __r;
6374 _STD::regex_replace(back_inserter(__r), __s,
6375 __s + char_traits<_CharT>::length(__s), __e,
6376 __fmt, __flags);
6377 return __r;
6378}
6379
Howard Hinnant3257c982010-06-17 00:34:59 +00006380_LIBCPP_END_NAMESPACE_STD
6381
6382#endif // _LIBCPP_REGEX