1// -*- C++ -*-
2//===--------------------------- regex ------------------------------------===//
3//
4//                     The LLVM Compiler Infrastructure
5//
6// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. 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 traits                              traits_type;
131    typedef typename traits::string_type        string_type;
132    typedef regex_constants::syntax_option_type flag_type;
133    typedef typename traits::locale_type        locale_type;
134
135    // constants:
136    static constexpr regex_constants::syntax_option_type icase = regex_constants::icase;
137    static constexpr regex_constants::syntax_option_type nosubs = regex_constants::nosubs;
138    static constexpr regex_constants::syntax_option_type optimize = regex_constants::optimize;
139    static constexpr regex_constants::syntax_option_type collate = regex_constants::collate;
140    static constexpr regex_constants::syntax_option_type ECMAScript = regex_constants::ECMAScript;
141    static constexpr regex_constants::syntax_option_type basic = regex_constants::basic;
142    static constexpr regex_constants::syntax_option_type extended = regex_constants::extended;
143    static constexpr regex_constants::syntax_option_type awk = regex_constants::awk;
144    static constexpr regex_constants::syntax_option_type grep = regex_constants::grep;
145    static constexpr regex_constants::syntax_option_type egrep = regex_constants::egrep;
146
147    // construct/copy/destroy:
148    basic_regex();
149    explicit basic_regex(const charT* p, flag_type f = regex_constants::ECMAScript);
150    basic_regex(const charT* p, size_t len, flag_type f = regex_constants::ECMAScript);
151    basic_regex(const basic_regex&);
152    basic_regex(basic_regex&&) noexcept;
153    template <class ST, class SA>
154        explicit basic_regex(const basic_string<charT, ST, SA>& p,
155                             flag_type f = regex_constants::ECMAScript);
156    template <class ForwardIterator>
157        basic_regex(ForwardIterator first, ForwardIterator last,
158                    flag_type f = regex_constants::ECMAScript);
159    basic_regex(initializer_list<charT>, flag_type = regex_constants::ECMAScript);
160
161    ~basic_regex();
162
163    basic_regex& operator=(const basic_regex&);
164    basic_regex& operator=(basic_regex&&) noexcept;
165    basic_regex& operator=(const charT* ptr);
166    basic_regex& operator=(initializer_list<charT> il);
167    template <class ST, class SA>
168        basic_regex& operator=(const basic_string<charT, ST, SA>& p);
169
170    // assign:
171    basic_regex& assign(const basic_regex& that);
172    basic_regex& assign(basic_regex&& that) noexcept;
173    basic_regex& assign(const charT* ptr, flag_type f = regex_constants::ECMAScript);
174    basic_regex& assign(const charT* p, size_t len, flag_type f);
175    template <class string_traits, class A>
176        basic_regex& assign(const basic_string<charT, string_traits, A>& s,
177                            flag_type f = regex_constants::ECMAScript);
178    template <class InputIterator>
179        basic_regex& assign(InputIterator first, InputIterator last,
180                            flag_type f = regex_constants::ECMAScript);
181    basic_regex& assign(initializer_list<charT>, flag_type = regex_constants::ECMAScript);
182
183    // const operations:
184    unsigned mark_count() const;
185    flag_type flags() const;
186
187    // locale:
188    locale_type imbue(locale_type loc);
189    locale_type getloc() const;
190
191    // swap:
192    void swap(basic_regex&);
193};
194
195typedef basic_regex<char>    regex;
196typedef basic_regex<wchar_t> wregex;
197
198template <class charT, class traits>
199    void swap(basic_regex<charT, traits>& e1, basic_regex<charT, traits>& e2);
200
201template <class BidirectionalIterator>
202class sub_match
203    : public pair<BidirectionalIterator, BidirectionalIterator>
204{
205public:
206    typedef typename iterator_traits<BidirectionalIterator>::value_type value_type;
207    typedef typename iterator_traits<BidirectionalIterator>::difference_type difference_type;
208    typedef BidirectionalIterator                                      iterator;
209    typedef basic_string<value_type>                                string_type;
210
211    bool matched;
212
213    constexpr sub_match();
214
215    difference_type length() const;
216    operator string_type() const;
217    string_type str() const;
218
219    int compare(const sub_match& s) const;
220    int compare(const string_type& s) const;
221    int compare(const value_type* s) const;
222};
223
224typedef sub_match<const char*>             csub_match;
225typedef sub_match<const wchar_t*>          wcsub_match;
226typedef sub_match<string::const_iterator>  ssub_match;
227typedef sub_match<wstring::const_iterator> wssub_match;
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>
250    bool
251    operator>(const sub_match<BiIter>& lhs, const sub_match<BiIter>& rhs);
252
253template <class BiIter, class ST, class SA>
254    bool
255    operator==(const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& lhs,
256               const sub_match<BiIter>& rhs);
257
258template <class BiIter, class ST, class SA>
259    bool
260    operator!=(const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& lhs,
261               const sub_match<BiIter>& rhs);
262
263template <class BiIter, class ST, class SA>
264    bool
265    operator<(const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& lhs,
266              const sub_match<BiIter>& rhs);
267
268template <class BiIter, class ST, class SA>
269    bool
270    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 operator>=(const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& lhs,
275                    const sub_match<BiIter>& rhs);
276
277template <class BiIter, class ST, class SA>
278    bool
279    operator<=(const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& lhs,
280               const sub_match<BiIter>& rhs);
281
282template <class BiIter, class ST, class SA>
283    bool
284    operator==(const sub_match<BiIter>& lhs,
285               const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& rhs);
286
287template <class BiIter, class ST, class SA>
288    bool
289    operator!=(const sub_match<BiIter>& lhs,
290               const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& rhs);
291
292template <class BiIter, class ST, class SA>
293    bool
294    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 operator>(const sub_match<BiIter>& lhs,
299                   const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& rhs);
300
301template <class BiIter, class ST, class SA>
302    bool
303    operator>=(const sub_match<BiIter>& lhs,
304               const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& rhs);
305
306template <class BiIter, class ST, class SA>
307    bool
308    operator<=(const sub_match<BiIter>& lhs,
309               const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& rhs);
310
311template <class BiIter>
312    bool
313    operator==(typename iterator_traits<BiIter>::value_type const* lhs,
314               const sub_match<BiIter>& rhs);
315
316template <class BiIter>
317    bool
318    operator!=(typename iterator_traits<BiIter>::value_type const* lhs,
319               const sub_match<BiIter>& rhs);
320
321template <class BiIter>
322    bool
323    operator<(typename iterator_traits<BiIter>::value_type const* lhs,
324              const sub_match<BiIter>& rhs);
325
326template <class BiIter>
327    bool
328    operator>(typename iterator_traits<BiIter>::value_type const* lhs,
329              const sub_match<BiIter>& rhs);
330
331template <class BiIter>
332    bool
333    operator>=(typename iterator_traits<BiIter>::value_type const* lhs,
334               const sub_match<BiIter>& rhs);
335
336template <class BiIter>
337    bool
338    operator<=(typename iterator_traits<BiIter>::value_type const* lhs,
339               const sub_match<BiIter>& rhs);
340
341template <class BiIter>
342    bool
343    operator==(const sub_match<BiIter>& lhs,
344               typename iterator_traits<BiIter>::value_type const* rhs);
345
346template <class BiIter>
347    bool
348    operator!=(const sub_match<BiIter>& lhs,
349               typename iterator_traits<BiIter>::value_type const* rhs);
350
351template <class BiIter>
352    bool
353    operator<(const sub_match<BiIter>& lhs,
354              typename iterator_traits<BiIter>::value_type const* rhs);
355
356template <class BiIter>
357    bool
358    operator>(const sub_match<BiIter>& lhs,
359              typename iterator_traits<BiIter>::value_type const* rhs);
360
361template <class BiIter>
362    bool
363    operator>=(const sub_match<BiIter>& lhs,
364               typename iterator_traits<BiIter>::value_type const* rhs);
365
366template <class BiIter>
367    bool
368    operator<=(const sub_match<BiIter>& lhs,
369               typename iterator_traits<BiIter>::value_type const* rhs);
370
371template <class BiIter>
372    bool
373    operator==(typename iterator_traits<BiIter>::value_type const& lhs,
374               const sub_match<BiIter>& rhs);
375
376template <class BiIter>
377    bool
378    operator!=(typename iterator_traits<BiIter>::value_type const& lhs,
379               const sub_match<BiIter>& rhs);
380
381template <class BiIter>
382    bool
383    operator<(typename iterator_traits<BiIter>::value_type const& lhs,
384              const sub_match<BiIter>& rhs);
385
386template <class BiIter>
387    bool
388    operator>(typename iterator_traits<BiIter>::value_type const& lhs,
389              const sub_match<BiIter>& rhs);
390
391template <class BiIter>
392    bool
393    operator>=(typename iterator_traits<BiIter>::value_type const& lhs,
394               const sub_match<BiIter>& rhs);
395
396template <class BiIter>
397    bool
398    operator<=(typename iterator_traits<BiIter>::value_type const& lhs,
399               const sub_match<BiIter>& rhs);
400
401template <class BiIter>
402    bool
403    operator==(const sub_match<BiIter>& lhs,
404               typename iterator_traits<BiIter>::value_type const& rhs);
405
406template <class BiIter>
407    bool
408    operator!=(const sub_match<BiIter>& lhs,
409               typename iterator_traits<BiIter>::value_type const& rhs);
410
411template <class BiIter>
412    bool
413    operator<(const sub_match<BiIter>& lhs,
414              typename iterator_traits<BiIter>::value_type const& rhs);
415
416template <class BiIter>
417    bool
418    operator>(const sub_match<BiIter>& lhs,
419              typename iterator_traits<BiIter>::value_type const& rhs);
420
421template <class BiIter>
422    bool
423    operator>=(const sub_match<BiIter>& lhs,
424               typename iterator_traits<BiIter>::value_type const& rhs);
425
426template <class BiIter>
427    bool
428    operator<=(const sub_match<BiIter>& lhs,
429               typename iterator_traits<BiIter>::value_type const& rhs);
430
431template <class charT, class ST, class BiIter>
432    basic_ostream<charT, ST>&
433    operator<<(basic_ostream<charT, ST>& os, const sub_match<BiIter>& m);
434
435template <class BidirectionalIterator,
436          class Allocator = allocator<sub_match<BidirectionalIterator>>>
437class match_results
438{
439public:
440    typedef sub_match<BidirectionalIterator>                  value_type;
441    typedef const value_type&                                 const_reference;
442    typedef value_type&                                       reference;
443    typedef /implementation-defined/                          const_iterator;
444    typedef const_iterator                                    iterator;
445    typedef typename iterator_traits<BidirectionalIterator>::difference_type difference_type;
446    typedef typename allocator_traits<Allocator>::size_type   size_type;
447    typedef Allocator                                         allocator_type;
448    typedef typename iterator_traits<BidirectionalIterator>::value_type char_type;
449    typedef basic_string<char_type>                           string_type;
450
451    // construct/copy/destroy:
452    explicit match_results(const Allocator& a = Allocator());
453    match_results(const match_results& m);
454    match_results(match_results&& m) noexcept;
455    match_results& operator=(const match_results& m);
456    match_results& operator=(match_results&& m);
457    ~match_results();
458
459    bool ready() const;
460
461    // size:
462    size_type size() const;
463    size_type max_size() const;
464    bool empty() const;
465
466    // element access:
467    difference_type length(size_type sub = 0) const;
468    difference_type position(size_type sub = 0) const;
469    string_type str(size_type sub = 0) const;
470    const_reference operator[](size_type n) const;
471
472    const_reference prefix() const;
473    const_reference suffix() const;
474
475    const_iterator begin() const;
476    const_iterator end() const;
477    const_iterator cbegin() const;
478    const_iterator cend() const;
479
480    // format:
481    template <class OutputIter>
482        OutputIter
483        format(OutputIter out, const char_type* fmt_first,
484               const char_type* fmt_last,
485               regex_constants::match_flag_type flags = regex_constants::format_default) const;
486    template <class OutputIter, class ST, class SA>
487        OutputIter
488        format(OutputIter out, const basic_string<char_type, ST, SA>& fmt,
489               regex_constants::match_flag_type flags = regex_constants::format_default) const;
490    template <class ST, class SA>
491        basic_string<char_type, ST, SA>
492        format(const basic_string<char_type, ST, SA>& fmt,
493               regex_constants::match_flag_type flags = regex_constants::format_default) const;
494    string_type
495        format(const char_type* fmt,
496               regex_constants::match_flag_type flags = regex_constants::format_default) const;
497
498    // allocator:
499    allocator_type get_allocator() const;
500
501    // swap:
502    void swap(match_results& that);
503};
504
505typedef match_results<const char*>             cmatch;
506typedef match_results<const wchar_t*>          wcmatch;
507typedef match_results<string::const_iterator>  smatch;
508typedef match_results<wstring::const_iterator> wsmatch;
509
510template <class BidirectionalIterator, class Allocator>
511    bool
512    operator==(const match_results<BidirectionalIterator, Allocator>& m1,
513               const match_results<BidirectionalIterator, Allocator>& m2);
514
515template <class BidirectionalIterator, class Allocator>
516    bool
517    operator!=(const match_results<BidirectionalIterator, Allocator>& m1,
518               const match_results<BidirectionalIterator, Allocator>& m2);
519
520template <class BidirectionalIterator, class Allocator>
521    void
522    swap(match_results<BidirectionalIterator, Allocator>& m1,
523         match_results<BidirectionalIterator, Allocator>& m2);
524
525template <class BidirectionalIterator, class Allocator, class charT, class traits>
526    bool
527    regex_match(BidirectionalIterator first, BidirectionalIterator last,
528                match_results<BidirectionalIterator, Allocator>& m,
529                const basic_regex<charT, traits>& e,
530                regex_constants::match_flag_type flags = regex_constants::match_default);
531
532template <class BidirectionalIterator, class charT, class traits>
533    bool
534    regex_match(BidirectionalIterator first, BidirectionalIterator last,
535                const basic_regex<charT, traits>& e,
536                regex_constants::match_flag_type flags = regex_constants::match_default);
537
538template <class charT, class Allocator, class traits>
539    bool
540    regex_match(const charT* str, match_results<const charT*, Allocator>& m,
541                const basic_regex<charT, traits>& e,
542                regex_constants::match_flag_type flags = regex_constants::match_default);
543
544template <class ST, class SA, class Allocator, class charT, class traits>
545    bool
546    regex_match(const basic_string<charT, ST, SA>& s,
547                match_results<typename basic_string<charT, ST, SA>::const_iterator, Allocator>& m,
548                const basic_regex<charT, traits>& e,
549                regex_constants::match_flag_type flags = regex_constants::match_default);
550
551template <class ST, class SA, class Allocator, class charT, class traits>
552    bool
553    regex_match(const basic_string<charT, ST, SA>&& s,
554                match_results<typename basic_string<charT, ST, SA>::const_iterator, Allocator>& m,
555                const basic_regex<charT, traits>& e,
556                regex_constants::match_flag_type flags = regex_constants::match_default) = delete; // C++14
557
558template <class charT, class traits>
559    bool
560    regex_match(const charT* str, const basic_regex<charT, traits>& e,
561                regex_constants::match_flag_type flags = regex_constants::match_default);
562
563template <class ST, class SA, class charT, class traits>
564    bool
565    regex_match(const basic_string<charT, ST, SA>& s,
566                const basic_regex<charT, traits>& e,
567                regex_constants::match_flag_type flags = regex_constants::match_default);
568
569template <class BidirectionalIterator, class Allocator, class charT, class traits>
570    bool
571    regex_search(BidirectionalIterator first, BidirectionalIterator last,
572                 match_results<BidirectionalIterator, Allocator>& m,
573                 const basic_regex<charT, traits>& e,
574                 regex_constants::match_flag_type flags = regex_constants::match_default);
575
576template <class BidirectionalIterator, class charT, class traits>
577    bool
578    regex_search(BidirectionalIterator first, BidirectionalIterator last,
579                 const basic_regex<charT, traits>& e,
580                 regex_constants::match_flag_type flags = regex_constants::match_default);
581
582template <class charT, class Allocator, class traits>
583    bool
584    regex_search(const charT* str, match_results<const charT*, Allocator>& m,
585                 const basic_regex<charT, traits>& e,
586                 regex_constants::match_flag_type flags = regex_constants::match_default);
587
588template <class charT, class traits>
589    bool
590    regex_search(const charT* str, const basic_regex<charT, traits>& e,
591                 regex_constants::match_flag_type flags = regex_constants::match_default);
592
593template <class ST, class SA, class charT, class traits>
594    bool
595    regex_search(const basic_string<charT, ST, SA>& s,
596                 const basic_regex<charT, traits>& e,
597                 regex_constants::match_flag_type flags = regex_constants::match_default);
598
599template <class ST, class SA, class Allocator, class charT, class traits>
600    bool
601    regex_search(const basic_string<charT, ST, SA>& s,
602                 match_results<typename basic_string<charT, ST, SA>::const_iterator, Allocator>& m,
603                 const basic_regex<charT, traits>& e,
604                 regex_constants::match_flag_type flags = regex_constants::match_default);
605
606template <class ST, class SA, class Allocator, class charT, class traits>
607    bool
608    regex_search(const basic_string<charT, ST, SA>&& s,
609                 match_results<typename basic_string<charT, ST, SA>::const_iterator, Allocator>& m,
610                 const basic_regex<charT, traits>& e,
611                 regex_constants::match_flag_type flags = regex_constants::match_default) = delete; // C++14
612
613template <class OutputIterator, class BidirectionalIterator,
614          class traits, class charT, class ST, class SA>
615    OutputIterator
616    regex_replace(OutputIterator out,
617                  BidirectionalIterator first, BidirectionalIterator last,
618                  const basic_regex<charT, traits>& e,
619                  const basic_string<charT, ST, SA>& fmt,
620                  regex_constants::match_flag_type flags = regex_constants::match_default);
621
622template <class OutputIterator, class BidirectionalIterator,
623          class traits, class charT>
624    OutputIterator
625    regex_replace(OutputIterator out,
626                  BidirectionalIterator first, BidirectionalIterator last,
627                  const basic_regex<charT, traits>& e, const charT* fmt,
628                  regex_constants::match_flag_type flags = regex_constants::match_default);
629
630template <class traits, class charT, class ST, class SA, class FST, class FSA>>
631    basic_string<charT, ST, SA>
632    regex_replace(const basic_string<charT, ST, SA>& s,
633                  const basic_regex<charT, traits>& e,
634                  const basic_string<charT, FST, FSA>& fmt,
635                  regex_constants::match_flag_type flags = regex_constants::match_default);
636
637template <class traits, class charT, class ST, class SA>
638    basic_string<charT, ST, SA>
639    regex_replace(const basic_string<charT, ST, SA>& s,
640                  const basic_regex<charT, traits>& e, const charT* fmt,
641                  regex_constants::match_flag_type flags = regex_constants::match_default);
642
643template <class traits, class charT, class ST, class SA>
644    basic_string<charT>
645    regex_replace(const charT* s,
646                  const basic_regex<charT, traits>& e,
647                  const basic_string<charT, ST, SA>& fmt,
648                  regex_constants::match_flag_type flags = regex_constants::match_default);
649
650template <class traits, class charT>
651    basic_string<charT>
652    regex_replace(const charT* s,
653                  const basic_regex<charT, traits>& e,
654                  const charT* fmt,
655                  regex_constants::match_flag_type flags = regex_constants::match_default);
656
657template <class BidirectionalIterator,
658          class charT = typename iterator_traits< BidirectionalIterator>::value_type,
659          class traits = regex_traits<charT>>
660class regex_iterator
661{
662public:
663    typedef basic_regex<charT, traits>           regex_type;
664    typedef match_results<BidirectionalIterator> value_type;
665    typedef ptrdiff_t                            difference_type;
666    typedef const value_type*                    pointer;
667    typedef const value_type&                    reference;
668    typedef forward_iterator_tag                 iterator_category;
669
670    regex_iterator();
671    regex_iterator(BidirectionalIterator a, BidirectionalIterator b,
672                   const regex_type& re,
673                   regex_constants::match_flag_type m = regex_constants::match_default);
674    regex_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
675                   const regex_type&& __re,
676                   regex_constants::match_flag_type __m
677                                     = regex_constants::match_default) = delete; // C++14
678    regex_iterator(const regex_iterator&);
679    regex_iterator& operator=(const regex_iterator&);
680
681    bool operator==(const regex_iterator&) const;
682    bool operator!=(const regex_iterator&) const;
683
684    const value_type& operator*() const;
685    const value_type* operator->() const;
686
687    regex_iterator& operator++();
688    regex_iterator operator++(int);
689};
690
691typedef regex_iterator<const char*>             cregex_iterator;
692typedef regex_iterator<const wchar_t*>          wcregex_iterator;
693typedef regex_iterator<string::const_iterator>  sregex_iterator;
694typedef regex_iterator<wstring::const_iterator> wsregex_iterator;
695
696template <class BidirectionalIterator,
697          class charT = typename iterator_traits< BidirectionalIterator>::value_type,
698          class traits = regex_traits<charT>>
699class regex_token_iterator
700{
701public:
702    typedef basic_regex<charT, traits>       regex_type;
703    typedef sub_match<BidirectionalIterator> value_type;
704    typedef ptrdiff_t                        difference_type;
705    typedef const value_type*                pointer;
706    typedef const value_type&                reference;
707    typedef forward_iterator_tag             iterator_category;
708
709    regex_token_iterator();
710    regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b,
711                         const regex_type& re, int submatch = 0,
712                         regex_constants::match_flag_type m = regex_constants::match_default);
713    regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b,
714                         const regex_type&& re, int submatch = 0,
715                         regex_constants::match_flag_type m = regex_constants::match_default) = delete; // C++14
716    regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b,
717                         const regex_type& re, const vector<int>& submatches,
718                         regex_constants::match_flag_type m = regex_constants::match_default);
719    regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b,
720                         const regex_type&& re, const vector<int>& submatches,
721                         regex_constants::match_flag_type m = regex_constants::match_default) = delete; // C++14
722    regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b,
723                         const regex_type& re, initializer_list<int> submatches,
724                         regex_constants::match_flag_type m = regex_constants::match_default);
725    regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b,
726                         const regex_type&& re, initializer_list<int> submatches,
727                         regex_constants::match_flag_type m = regex_constants::match_default) = delete; // C++14
728    template <size_t N>
729        regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b,
730                             const regex_type& re, const int (&submatches)[N],
731                             regex_constants::match_flag_type m = regex_constants::match_default);
732    template <size_t N>
733        regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b,
734                             const regex_type& re, const int (&submatches)[N],
735                             regex_constants::match_flag_type m = regex_constants::match_default) = delete // C++14;
736    regex_token_iterator(const regex_token_iterator&);
737    regex_token_iterator& operator=(const regex_token_iterator&);
738
739    bool operator==(const regex_token_iterator&) const;
740    bool operator!=(const regex_token_iterator&) const;
741
742    const value_type& operator*() const;
743    const value_type* operator->() const;
744
745    regex_token_iterator& operator++();
746    regex_token_iterator operator++(int);
747};
748
749typedef regex_token_iterator<const char*>             cregex_token_iterator;
750typedef regex_token_iterator<const wchar_t*>          wcregex_token_iterator;
751typedef regex_token_iterator<string::const_iterator>  sregex_token_iterator;
752typedef regex_token_iterator<wstring::const_iterator> wsregex_token_iterator;
753
754} // std
755*/
756
757#include <__config>
758#include <stdexcept>
759#include <__locale>
760#include <initializer_list>
761#include <utility>
762#include <iterator>
763#include <string>
764#include <memory>
765#include <vector>
766#include <deque>
767
768#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
769#pragma GCC system_header
770#endif
771
772_LIBCPP_PUSH_MACROS
773#include <__undef_macros>
774
775
776#define _LIBCPP_REGEX_COMPLEXITY_FACTOR 4096
777
778_LIBCPP_BEGIN_NAMESPACE_STD
779
780namespace regex_constants
781{
782
783// syntax_option_type
784
785enum syntax_option_type
786{
787    icase      = 1 << 0,
788    nosubs     = 1 << 1,
789    optimize   = 1 << 2,
790    collate    = 1 << 3,
791    ECMAScript = 0,
792    basic      = 1 << 4,
793    extended   = 1 << 5,
794    awk        = 1 << 6,
795    grep       = 1 << 7,
796    egrep      = 1 << 8
797};
798
799inline _LIBCPP_INLINE_VISIBILITY
800_LIBCPP_CONSTEXPR
801syntax_option_type
802operator~(syntax_option_type __x)
803{
804    return syntax_option_type(~int(__x) & 0x1FF);
805}
806
807inline _LIBCPP_INLINE_VISIBILITY
808_LIBCPP_CONSTEXPR
809syntax_option_type
810operator&(syntax_option_type __x, syntax_option_type __y)
811{
812    return syntax_option_type(int(__x) & int(__y));
813}
814
815inline _LIBCPP_INLINE_VISIBILITY
816_LIBCPP_CONSTEXPR
817syntax_option_type
818operator|(syntax_option_type __x, syntax_option_type __y)
819{
820    return syntax_option_type(int(__x) | int(__y));
821}
822
823inline _LIBCPP_INLINE_VISIBILITY
824_LIBCPP_CONSTEXPR
825syntax_option_type
826operator^(syntax_option_type __x, syntax_option_type __y)
827{
828    return syntax_option_type(int(__x) ^ int(__y));
829}
830
831inline _LIBCPP_INLINE_VISIBILITY
832syntax_option_type&
833operator&=(syntax_option_type& __x, syntax_option_type __y)
834{
835    __x = __x & __y;
836    return __x;
837}
838
839inline _LIBCPP_INLINE_VISIBILITY
840syntax_option_type&
841operator|=(syntax_option_type& __x, syntax_option_type __y)
842{
843    __x = __x | __y;
844    return __x;
845}
846
847inline _LIBCPP_INLINE_VISIBILITY
848syntax_option_type&
849operator^=(syntax_option_type& __x, syntax_option_type __y)
850{
851    __x = __x ^ __y;
852    return __x;
853}
854
855// match_flag_type
856
857enum match_flag_type
858{
859    match_default     = 0,
860    match_not_bol     = 1 << 0,
861    match_not_eol     = 1 << 1,
862    match_not_bow     = 1 << 2,
863    match_not_eow     = 1 << 3,
864    match_any         = 1 << 4,
865    match_not_null    = 1 << 5,
866    match_continuous  = 1 << 6,
867    match_prev_avail  = 1 << 7,
868    format_default    = 0,
869    format_sed        = 1 << 8,
870    format_no_copy    = 1 << 9,
871    format_first_only = 1 << 10,
872    __no_update_pos   = 1 << 11,
873    __full_match      = 1 << 12
874};
875
876inline _LIBCPP_INLINE_VISIBILITY
877_LIBCPP_CONSTEXPR
878match_flag_type
879operator~(match_flag_type __x)
880{
881    return match_flag_type(~int(__x) & 0x0FFF);
882}
883
884inline _LIBCPP_INLINE_VISIBILITY
885_LIBCPP_CONSTEXPR
886match_flag_type
887operator&(match_flag_type __x, match_flag_type __y)
888{
889    return match_flag_type(int(__x) & int(__y));
890}
891
892inline _LIBCPP_INLINE_VISIBILITY
893_LIBCPP_CONSTEXPR
894match_flag_type
895operator|(match_flag_type __x, match_flag_type __y)
896{
897    return match_flag_type(int(__x) | int(__y));
898}
899
900inline _LIBCPP_INLINE_VISIBILITY
901_LIBCPP_CONSTEXPR
902match_flag_type
903operator^(match_flag_type __x, match_flag_type __y)
904{
905    return match_flag_type(int(__x) ^ int(__y));
906}
907
908inline _LIBCPP_INLINE_VISIBILITY
909match_flag_type&
910operator&=(match_flag_type& __x, match_flag_type __y)
911{
912    __x = __x & __y;
913    return __x;
914}
915
916inline _LIBCPP_INLINE_VISIBILITY
917match_flag_type&
918operator|=(match_flag_type& __x, match_flag_type __y)
919{
920    __x = __x | __y;
921    return __x;
922}
923
924inline _LIBCPP_INLINE_VISIBILITY
925match_flag_type&
926operator^=(match_flag_type& __x, match_flag_type __y)
927{
928    __x = __x ^ __y;
929    return __x;
930}
931
932enum error_type
933{
934    error_collate = 1,
935    error_ctype,
936    error_escape,
937    error_backref,
938    error_brack,
939    error_paren,
940    error_brace,
941    error_badbrace,
942    error_range,
943    error_space,
944    error_badrepeat,
945    error_complexity,
946    error_stack,
947    __re_err_grammar,
948    __re_err_empty,
949    __re_err_unknown
950};
951
952}  // regex_constants
953
954class _LIBCPP_EXCEPTION_ABI regex_error
955    : public runtime_error
956{
957    regex_constants::error_type __code_;
958public:
959    explicit regex_error(regex_constants::error_type __ecode);
960    virtual ~regex_error() throw();
961     _LIBCPP_INLINE_VISIBILITY
962    regex_constants::error_type code() const {return __code_;}
963};
964
965template <regex_constants::error_type _Ev>
966_LIBCPP_NORETURN inline _LIBCPP_ALWAYS_INLINE
967void __throw_regex_error()
968{
969#ifndef _LIBCPP_NO_EXCEPTIONS
970    throw regex_error(_Ev);
971#else
972    _VSTD::abort();
973#endif
974}
975
976template <class _CharT>
977struct _LIBCPP_TEMPLATE_VIS regex_traits
978{
979public:
980    typedef _CharT                  char_type;
981    typedef basic_string<char_type> string_type;
982    typedef locale                  locale_type;
983#ifdef __BIONIC__
984    typedef uint16_t                char_class_type;
985#else
986    typedef ctype_base::mask        char_class_type;
987#endif
988
989#ifdef __BIONIC__
990    static const char_class_type __regex_word = 0x8000;
991#elif defined(__mips__) && defined(__GLIBC__)
992    static const char_class_type __regex_word = static_cast<char_class_type>(_ISbit(15));
993#else
994    static const char_class_type __regex_word = 0x80;
995#endif
996
997private:
998    locale __loc_;
999    const ctype<char_type>* __ct_;
1000    const collate<char_type>* __col_;
1001
1002public:
1003    regex_traits();
1004
1005    _LIBCPP_INLINE_VISIBILITY
1006    static size_t length(const char_type* __p)
1007        {return char_traits<char_type>::length(__p);}
1008    _LIBCPP_INLINE_VISIBILITY
1009    char_type translate(char_type __c) const {return __c;}
1010    char_type translate_nocase(char_type __c) const;
1011    template <class _ForwardIterator>
1012        string_type
1013        transform(_ForwardIterator __f, _ForwardIterator __l) const;
1014    template <class _ForwardIterator>
1015        _LIBCPP_INLINE_VISIBILITY
1016        string_type
1017        transform_primary( _ForwardIterator __f, _ForwardIterator __l) const
1018            {return __transform_primary(__f, __l, char_type());}
1019    template <class _ForwardIterator>
1020        _LIBCPP_INLINE_VISIBILITY
1021        string_type
1022        lookup_collatename(_ForwardIterator __f, _ForwardIterator __l) const
1023            {return __lookup_collatename(__f, __l, char_type());}
1024    template <class _ForwardIterator>
1025        _LIBCPP_INLINE_VISIBILITY
1026        char_class_type
1027        lookup_classname(_ForwardIterator __f, _ForwardIterator __l,
1028                         bool __icase = false) const
1029            {return __lookup_classname(__f, __l, __icase, char_type());}
1030    bool isctype(char_type __c, char_class_type __m) const;
1031    _LIBCPP_INLINE_VISIBILITY
1032    int value(char_type __ch, int __radix) const
1033        {return __regex_traits_value(__ch, __radix);}
1034    locale_type imbue(locale_type __l);
1035    _LIBCPP_INLINE_VISIBILITY
1036    locale_type getloc()const {return __loc_;}
1037
1038private:
1039    void __init();
1040
1041    template <class _ForwardIterator>
1042        string_type
1043        __transform_primary(_ForwardIterator __f, _ForwardIterator __l, char) const;
1044    template <class _ForwardIterator>
1045        string_type
1046        __transform_primary(_ForwardIterator __f, _ForwardIterator __l, wchar_t) const;
1047
1048    template <class _ForwardIterator>
1049        string_type
1050        __lookup_collatename(_ForwardIterator __f, _ForwardIterator __l, char) const;
1051    template <class _ForwardIterator>
1052        string_type
1053        __lookup_collatename(_ForwardIterator __f, _ForwardIterator __l, wchar_t) const;
1054
1055    template <class _ForwardIterator>
1056        char_class_type
1057        __lookup_classname(_ForwardIterator __f, _ForwardIterator __l,
1058                           bool __icase, char) const;
1059    template <class _ForwardIterator>
1060        char_class_type
1061        __lookup_classname(_ForwardIterator __f, _ForwardIterator __l,
1062                           bool __icase, wchar_t) const;
1063
1064    static int __regex_traits_value(unsigned char __ch, int __radix);
1065    _LIBCPP_INLINE_VISIBILITY
1066    int __regex_traits_value(char __ch, int __radix) const
1067        {return __regex_traits_value(static_cast<unsigned char>(__ch), __radix);}
1068    _LIBCPP_INLINE_VISIBILITY
1069    int __regex_traits_value(wchar_t __ch, int __radix) const;
1070};
1071
1072template <class _CharT>
1073const typename regex_traits<_CharT>::char_class_type
1074regex_traits<_CharT>::__regex_word;
1075
1076template <class _CharT>
1077regex_traits<_CharT>::regex_traits()
1078{
1079    __init();
1080}
1081
1082template <class _CharT>
1083typename regex_traits<_CharT>::char_type
1084regex_traits<_CharT>::translate_nocase(char_type __c) const
1085{
1086    return __ct_->tolower(__c);
1087}
1088
1089template <class _CharT>
1090template <class _ForwardIterator>
1091typename regex_traits<_CharT>::string_type
1092regex_traits<_CharT>::transform(_ForwardIterator __f, _ForwardIterator __l) const
1093{
1094    string_type __s(__f, __l);
1095    return __col_->transform(__s.data(), __s.data() + __s.size());
1096}
1097
1098template <class _CharT>
1099void
1100regex_traits<_CharT>::__init()
1101{
1102    __ct_ = &use_facet<ctype<char_type> >(__loc_);
1103    __col_ = &use_facet<collate<char_type> >(__loc_);
1104}
1105
1106template <class _CharT>
1107typename regex_traits<_CharT>::locale_type
1108regex_traits<_CharT>::imbue(locale_type __l)
1109{
1110    locale __r = __loc_;
1111    __loc_ = __l;
1112    __init();
1113    return __r;
1114}
1115
1116// transform_primary is very FreeBSD-specific
1117
1118template <class _CharT>
1119template <class _ForwardIterator>
1120typename regex_traits<_CharT>::string_type
1121regex_traits<_CharT>::__transform_primary(_ForwardIterator __f,
1122                                          _ForwardIterator __l, char) const
1123{
1124    const string_type __s(__f, __l);
1125    string_type __d = __col_->transform(__s.data(), __s.data() + __s.size());
1126    switch (__d.size())
1127    {
1128    case 1:
1129        break;
1130    case 12:
1131        __d[11] = __d[3];
1132        break;
1133    default:
1134        __d.clear();
1135        break;
1136    }
1137    return __d;
1138}
1139
1140template <class _CharT>
1141template <class _ForwardIterator>
1142typename regex_traits<_CharT>::string_type
1143regex_traits<_CharT>::__transform_primary(_ForwardIterator __f,
1144                                          _ForwardIterator __l, wchar_t) const
1145{
1146    const string_type __s(__f, __l);
1147    string_type __d = __col_->transform(__s.data(), __s.data() + __s.size());
1148    switch (__d.size())
1149    {
1150    case 1:
1151        break;
1152    case 3:
1153        __d[2] = __d[0];
1154        break;
1155    default:
1156        __d.clear();
1157        break;
1158    }
1159    return __d;
1160}
1161
1162// lookup_collatename is very FreeBSD-specific
1163
1164_LIBCPP_FUNC_VIS string __get_collation_name(const char* __s);
1165
1166template <class _CharT>
1167template <class _ForwardIterator>
1168typename regex_traits<_CharT>::string_type
1169regex_traits<_CharT>::__lookup_collatename(_ForwardIterator __f,
1170                                           _ForwardIterator __l, char) const
1171{
1172    string_type __s(__f, __l);
1173    string_type __r;
1174    if (!__s.empty())
1175    {
1176        __r = __get_collation_name(__s.c_str());
1177        if (__r.empty() && __s.size() <= 2)
1178        {
1179            __r = __col_->transform(__s.data(), __s.data() + __s.size());
1180            if (__r.size() == 1 || __r.size() == 12)
1181                __r = __s;
1182            else
1183                __r.clear();
1184        }
1185    }
1186    return __r;
1187}
1188
1189template <class _CharT>
1190template <class _ForwardIterator>
1191typename regex_traits<_CharT>::string_type
1192regex_traits<_CharT>::__lookup_collatename(_ForwardIterator __f,
1193                                           _ForwardIterator __l, wchar_t) const
1194{
1195    string_type __s(__f, __l);
1196    string __n;
1197    __n.reserve(__s.size());
1198    for (typename string_type::const_iterator __i = __s.begin(), __e = __s.end();
1199                                                              __i != __e; ++__i)
1200    {
1201        if (static_cast<unsigned>(*__i) >= 127)
1202            return string_type();
1203        __n.push_back(char(*__i));
1204    }
1205    string_type __r;
1206    if (!__s.empty())
1207    {
1208        __n = __get_collation_name(__n.c_str());
1209        if (!__n.empty())
1210            __r.assign(__n.begin(), __n.end());
1211        else if (__s.size() <= 2)
1212        {
1213            __r = __col_->transform(__s.data(), __s.data() + __s.size());
1214            if (__r.size() == 1 || __r.size() == 3)
1215                __r = __s;
1216            else
1217                __r.clear();
1218        }
1219    }
1220    return __r;
1221}
1222
1223// lookup_classname
1224
1225regex_traits<char>::char_class_type _LIBCPP_FUNC_VIS
1226__get_classname(const char* __s, bool __icase);
1227
1228template <class _CharT>
1229template <class _ForwardIterator>
1230typename regex_traits<_CharT>::char_class_type
1231regex_traits<_CharT>::__lookup_classname(_ForwardIterator __f,
1232                                         _ForwardIterator __l,
1233                                         bool __icase, char) const
1234{
1235    string_type __s(__f, __l);
1236    __ct_->tolower(&__s[0], &__s[0] + __s.size());
1237    return __get_classname(__s.c_str(), __icase);
1238}
1239
1240template <class _CharT>
1241template <class _ForwardIterator>
1242typename regex_traits<_CharT>::char_class_type
1243regex_traits<_CharT>::__lookup_classname(_ForwardIterator __f,
1244                                         _ForwardIterator __l,
1245                                         bool __icase, wchar_t) const
1246{
1247    string_type __s(__f, __l);
1248    __ct_->tolower(&__s[0], &__s[0] + __s.size());
1249    string __n;
1250    __n.reserve(__s.size());
1251    for (typename string_type::const_iterator __i = __s.begin(), __e = __s.end();
1252                                                              __i != __e; ++__i)
1253    {
1254        if (static_cast<unsigned>(*__i) >= 127)
1255            return char_class_type();
1256        __n.push_back(char(*__i));
1257    }
1258    return __get_classname(__n.c_str(), __icase);
1259}
1260
1261template <class _CharT>
1262bool
1263regex_traits<_CharT>::isctype(char_type __c, char_class_type __m) const
1264{
1265    if (__ct_->is(__m, __c))
1266        return true;
1267    return (__c == '_' && (__m & __regex_word));
1268}
1269
1270template <class _CharT>
1271int
1272regex_traits<_CharT>::__regex_traits_value(unsigned char __ch, int __radix)
1273{
1274    if ((__ch & 0xF8u) == 0x30)  // '0' <= __ch && __ch <= '7'
1275        return __ch - '0';
1276    if (__radix != 8)
1277    {
1278        if ((__ch & 0xFEu) == 0x38)  // '8' <= __ch && __ch <= '9'
1279            return __ch - '0';
1280        if (__radix == 16)
1281        {
1282            __ch |= 0x20;  // tolower
1283            if ('a' <= __ch && __ch <= 'f')
1284                return __ch - ('a' - 10);
1285        }
1286    }
1287    return -1;
1288}
1289
1290template <class _CharT>
1291inline
1292int
1293regex_traits<_CharT>::__regex_traits_value(wchar_t __ch, int __radix) const
1294{
1295    return __regex_traits_value(static_cast<unsigned char>(__ct_->narrow(__ch, char_type())), __radix);
1296}
1297
1298template <class _CharT> class __node;
1299
1300template <class _BidirectionalIterator> class _LIBCPP_TEMPLATE_VIS sub_match;
1301
1302template <class _BidirectionalIterator,
1303          class _Allocator = allocator<sub_match<_BidirectionalIterator> > >
1304class _LIBCPP_TEMPLATE_VIS match_results;
1305
1306template <class _CharT>
1307struct __state
1308{
1309    enum
1310    {
1311        __end_state = -1000,
1312        __consume_input,  // -999
1313        __begin_marked_expr, // -998
1314        __end_marked_expr,   // -997
1315        __pop_state,           // -996
1316        __accept_and_consume,  // -995
1317        __accept_but_not_consume,  // -994
1318        __reject,                  // -993
1319        __split,
1320        __repeat
1321    };
1322
1323    int __do_;
1324    const _CharT* __first_;
1325    const _CharT* __current_;
1326    const _CharT* __last_;
1327    vector<sub_match<const _CharT*> > __sub_matches_;
1328    vector<pair<size_t, const _CharT*> > __loop_data_;
1329    const __node<_CharT>* __node_;
1330    regex_constants::match_flag_type __flags_;
1331    bool __at_first_;
1332
1333    _LIBCPP_INLINE_VISIBILITY
1334    __state()
1335        : __do_(0), __first_(nullptr), __current_(nullptr), __last_(nullptr),
1336          __node_(nullptr), __flags_() {}
1337};
1338
1339// __node
1340
1341template <class _CharT>
1342class __node
1343{
1344    __node(const __node&);
1345    __node& operator=(const __node&);
1346public:
1347    typedef _VSTD::__state<_CharT> __state;
1348
1349    _LIBCPP_INLINE_VISIBILITY
1350    __node() {}
1351    _LIBCPP_INLINE_VISIBILITY
1352    virtual ~__node() {}
1353
1354    _LIBCPP_INLINE_VISIBILITY
1355    virtual void __exec(__state&) const {};
1356    _LIBCPP_INLINE_VISIBILITY
1357    virtual void __exec_split(bool, __state&) const {};
1358};
1359
1360// __end_state
1361
1362template <class _CharT>
1363class __end_state
1364    : public __node<_CharT>
1365{
1366public:
1367    typedef _VSTD::__state<_CharT> __state;
1368
1369    _LIBCPP_INLINE_VISIBILITY
1370    __end_state() {}
1371
1372    virtual void __exec(__state&) const;
1373};
1374
1375template <class _CharT>
1376void
1377__end_state<_CharT>::__exec(__state& __s) const
1378{
1379    __s.__do_ = __state::__end_state;
1380}
1381
1382// __has_one_state
1383
1384template <class _CharT>
1385class __has_one_state
1386    : public __node<_CharT>
1387{
1388    __node<_CharT>* __first_;
1389
1390public:
1391    _LIBCPP_INLINE_VISIBILITY
1392    explicit __has_one_state(__node<_CharT>* __s)
1393        : __first_(__s) {}
1394
1395    _LIBCPP_INLINE_VISIBILITY
1396    __node<_CharT>*  first() const {return __first_;}
1397    _LIBCPP_INLINE_VISIBILITY
1398    __node<_CharT>*& first()       {return __first_;}
1399};
1400
1401// __owns_one_state
1402
1403template <class _CharT>
1404class __owns_one_state
1405    : public __has_one_state<_CharT>
1406{
1407    typedef __has_one_state<_CharT> base;
1408
1409public:
1410    _LIBCPP_INLINE_VISIBILITY
1411    explicit __owns_one_state(__node<_CharT>* __s)
1412        : base(__s) {}
1413
1414    virtual ~__owns_one_state();
1415};
1416
1417template <class _CharT>
1418__owns_one_state<_CharT>::~__owns_one_state()
1419{
1420    delete this->first();
1421}
1422
1423// __empty_state
1424
1425template <class _CharT>
1426class __empty_state
1427    : public __owns_one_state<_CharT>
1428{
1429    typedef __owns_one_state<_CharT> base;
1430
1431public:
1432    typedef _VSTD::__state<_CharT> __state;
1433
1434    _LIBCPP_INLINE_VISIBILITY
1435    explicit __empty_state(__node<_CharT>* __s)
1436        : base(__s) {}
1437
1438    virtual void __exec(__state&) const;
1439};
1440
1441template <class _CharT>
1442void
1443__empty_state<_CharT>::__exec(__state& __s) const
1444{
1445    __s.__do_ = __state::__accept_but_not_consume;
1446    __s.__node_ = this->first();
1447}
1448
1449// __empty_non_own_state
1450
1451template <class _CharT>
1452class __empty_non_own_state
1453    : public __has_one_state<_CharT>
1454{
1455    typedef __has_one_state<_CharT> base;
1456
1457public:
1458    typedef _VSTD::__state<_CharT> __state;
1459
1460    _LIBCPP_INLINE_VISIBILITY
1461    explicit __empty_non_own_state(__node<_CharT>* __s)
1462        : base(__s) {}
1463
1464    virtual void __exec(__state&) const;
1465};
1466
1467template <class _CharT>
1468void
1469__empty_non_own_state<_CharT>::__exec(__state& __s) const
1470{
1471    __s.__do_ = __state::__accept_but_not_consume;
1472    __s.__node_ = this->first();
1473}
1474
1475// __repeat_one_loop
1476
1477template <class _CharT>
1478class __repeat_one_loop
1479    : public __has_one_state<_CharT>
1480{
1481    typedef __has_one_state<_CharT> base;
1482
1483public:
1484    typedef _VSTD::__state<_CharT> __state;
1485
1486    _LIBCPP_INLINE_VISIBILITY
1487    explicit __repeat_one_loop(__node<_CharT>* __s)
1488        : base(__s) {}
1489
1490    virtual void __exec(__state&) const;
1491};
1492
1493template <class _CharT>
1494void
1495__repeat_one_loop<_CharT>::__exec(__state& __s) const
1496{
1497    __s.__do_ = __state::__repeat;
1498    __s.__node_ = this->first();
1499}
1500
1501// __owns_two_states
1502
1503template <class _CharT>
1504class __owns_two_states
1505    : public __owns_one_state<_CharT>
1506{
1507    typedef __owns_one_state<_CharT> base;
1508
1509    base* __second_;
1510
1511public:
1512    _LIBCPP_INLINE_VISIBILITY
1513    explicit __owns_two_states(__node<_CharT>* __s1, base* __s2)
1514        : base(__s1), __second_(__s2) {}
1515
1516    virtual ~__owns_two_states();
1517
1518    _LIBCPP_INLINE_VISIBILITY
1519    base*  second() const {return __second_;}
1520    _LIBCPP_INLINE_VISIBILITY
1521    base*& second()       {return __second_;}
1522};
1523
1524template <class _CharT>
1525__owns_two_states<_CharT>::~__owns_two_states()
1526{
1527    delete __second_;
1528}
1529
1530// __loop
1531
1532template <class _CharT>
1533class __loop
1534    : public __owns_two_states<_CharT>
1535{
1536    typedef __owns_two_states<_CharT> base;
1537
1538    size_t __min_;
1539    size_t __max_;
1540    unsigned __loop_id_;
1541    unsigned __mexp_begin_;
1542    unsigned __mexp_end_;
1543    bool __greedy_;
1544
1545public:
1546    typedef _VSTD::__state<_CharT> __state;
1547
1548    _LIBCPP_INLINE_VISIBILITY
1549    explicit __loop(unsigned __loop_id,
1550                          __node<_CharT>* __s1, __owns_one_state<_CharT>* __s2,
1551                          unsigned __mexp_begin, unsigned __mexp_end,
1552                          bool __greedy = true,
1553                          size_t __min = 0,
1554                          size_t __max = numeric_limits<size_t>::max())
1555        : base(__s1, __s2), __min_(__min), __max_(__max), __loop_id_(__loop_id),
1556          __mexp_begin_(__mexp_begin), __mexp_end_(__mexp_end),
1557          __greedy_(__greedy) {}
1558
1559    virtual void __exec(__state& __s) const;
1560    virtual void __exec_split(bool __second, __state& __s) const;
1561
1562private:
1563    _LIBCPP_INLINE_VISIBILITY
1564    void __init_repeat(__state& __s) const
1565    {
1566        __s.__loop_data_[__loop_id_].second = __s.__current_;
1567        for (size_t __i = __mexp_begin_-1; __i != __mexp_end_-1; ++__i)
1568        {
1569            __s.__sub_matches_[__i].first = __s.__last_;
1570            __s.__sub_matches_[__i].second = __s.__last_;
1571            __s.__sub_matches_[__i].matched = false;
1572        }
1573    }
1574};
1575
1576template <class _CharT>
1577void
1578__loop<_CharT>::__exec(__state& __s) const
1579{
1580    if (__s.__do_ == __state::__repeat)
1581    {
1582        bool __do_repeat = ++__s.__loop_data_[__loop_id_].first < __max_;
1583        bool __do_alt = __s.__loop_data_[__loop_id_].first >= __min_;
1584        if (__do_repeat && __do_alt &&
1585                               __s.__loop_data_[__loop_id_].second == __s.__current_)
1586            __do_repeat = false;
1587        if (__do_repeat && __do_alt)
1588            __s.__do_ = __state::__split;
1589        else if (__do_repeat)
1590        {
1591            __s.__do_ = __state::__accept_but_not_consume;
1592            __s.__node_ = this->first();
1593            __init_repeat(__s);
1594        }
1595        else
1596        {
1597            __s.__do_ = __state::__accept_but_not_consume;
1598            __s.__node_ = this->second();
1599        }
1600    }
1601    else
1602    {
1603        __s.__loop_data_[__loop_id_].first = 0;
1604        bool __do_repeat = 0 < __max_;
1605        bool __do_alt = 0 >= __min_;
1606        if (__do_repeat && __do_alt)
1607            __s.__do_ = __state::__split;
1608        else if (__do_repeat)
1609        {
1610            __s.__do_ = __state::__accept_but_not_consume;
1611            __s.__node_ = this->first();
1612            __init_repeat(__s);
1613        }
1614        else
1615        {
1616            __s.__do_ = __state::__accept_but_not_consume;
1617            __s.__node_ = this->second();
1618        }
1619    }
1620}
1621
1622template <class _CharT>
1623void
1624__loop<_CharT>::__exec_split(bool __second, __state& __s) const
1625{
1626    __s.__do_ = __state::__accept_but_not_consume;
1627    if (__greedy_ != __second)
1628    {
1629        __s.__node_ = this->first();
1630        __init_repeat(__s);
1631    }
1632    else
1633        __s.__node_ = this->second();
1634}
1635
1636// __alternate
1637
1638template <class _CharT>
1639class __alternate
1640    : public __owns_two_states<_CharT>
1641{
1642    typedef __owns_two_states<_CharT> base;
1643
1644public:
1645    typedef _VSTD::__state<_CharT> __state;
1646
1647    _LIBCPP_INLINE_VISIBILITY
1648    explicit __alternate(__owns_one_state<_CharT>* __s1,
1649                         __owns_one_state<_CharT>* __s2)
1650        : base(__s1, __s2) {}
1651
1652    virtual void __exec(__state& __s) const;
1653    virtual void __exec_split(bool __second, __state& __s) const;
1654};
1655
1656template <class _CharT>
1657void
1658__alternate<_CharT>::__exec(__state& __s) const
1659{
1660    __s.__do_ = __state::__split;
1661}
1662
1663template <class _CharT>
1664void
1665__alternate<_CharT>::__exec_split(bool __second, __state& __s) const
1666{
1667    __s.__do_ = __state::__accept_but_not_consume;
1668    if (__second)
1669        __s.__node_ = this->second();
1670    else
1671        __s.__node_ = this->first();
1672}
1673
1674// __begin_marked_subexpression
1675
1676template <class _CharT>
1677class __begin_marked_subexpression
1678    : public __owns_one_state<_CharT>
1679{
1680    typedef __owns_one_state<_CharT> base;
1681
1682    unsigned __mexp_;
1683public:
1684    typedef _VSTD::__state<_CharT> __state;
1685
1686    _LIBCPP_INLINE_VISIBILITY
1687    explicit __begin_marked_subexpression(unsigned __mexp, __node<_CharT>* __s)
1688        : base(__s), __mexp_(__mexp) {}
1689
1690    virtual void __exec(__state&) const;
1691};
1692
1693template <class _CharT>
1694void
1695__begin_marked_subexpression<_CharT>::__exec(__state& __s) const
1696{
1697    __s.__do_ = __state::__accept_but_not_consume;
1698    __s.__sub_matches_[__mexp_-1].first = __s.__current_;
1699    __s.__node_ = this->first();
1700}
1701
1702// __end_marked_subexpression
1703
1704template <class _CharT>
1705class __end_marked_subexpression
1706    : public __owns_one_state<_CharT>
1707{
1708    typedef __owns_one_state<_CharT> base;
1709
1710    unsigned __mexp_;
1711public:
1712    typedef _VSTD::__state<_CharT> __state;
1713
1714    _LIBCPP_INLINE_VISIBILITY
1715    explicit __end_marked_subexpression(unsigned __mexp, __node<_CharT>* __s)
1716        : base(__s), __mexp_(__mexp) {}
1717
1718    virtual void __exec(__state&) const;
1719};
1720
1721template <class _CharT>
1722void
1723__end_marked_subexpression<_CharT>::__exec(__state& __s) const
1724{
1725    __s.__do_ = __state::__accept_but_not_consume;
1726    __s.__sub_matches_[__mexp_-1].second = __s.__current_;
1727    __s.__sub_matches_[__mexp_-1].matched = true;
1728    __s.__node_ = this->first();
1729}
1730
1731// __back_ref
1732
1733template <class _CharT>
1734class __back_ref
1735    : public __owns_one_state<_CharT>
1736{
1737    typedef __owns_one_state<_CharT> base;
1738
1739    unsigned __mexp_;
1740public:
1741    typedef _VSTD::__state<_CharT> __state;
1742
1743    _LIBCPP_INLINE_VISIBILITY
1744    explicit __back_ref(unsigned __mexp, __node<_CharT>* __s)
1745        : base(__s), __mexp_(__mexp) {}
1746
1747    virtual void __exec(__state&) const;
1748};
1749
1750template <class _CharT>
1751void
1752__back_ref<_CharT>::__exec(__state& __s) const
1753{
1754    if (__mexp_ > __s.__sub_matches_.size())
1755        __throw_regex_error<regex_constants::error_backref>();
1756    sub_match<const _CharT*>& __sm = __s.__sub_matches_[__mexp_-1];
1757    if (__sm.matched)
1758    {
1759        ptrdiff_t __len = __sm.second - __sm.first;
1760        if (__s.__last_ - __s.__current_ >= __len &&
1761            _VSTD::equal(__sm.first, __sm.second, __s.__current_))
1762        {
1763            __s.__do_ = __state::__accept_but_not_consume;
1764            __s.__current_ += __len;
1765            __s.__node_ = this->first();
1766        }
1767        else
1768        {
1769            __s.__do_ = __state::__reject;
1770            __s.__node_ = nullptr;
1771        }
1772    }
1773    else
1774    {
1775        __s.__do_ = __state::__reject;
1776        __s.__node_ = nullptr;
1777    }
1778}
1779
1780// __back_ref_icase
1781
1782template <class _CharT, class _Traits>
1783class __back_ref_icase
1784    : public __owns_one_state<_CharT>
1785{
1786    typedef __owns_one_state<_CharT> base;
1787
1788    _Traits __traits_;
1789    unsigned __mexp_;
1790public:
1791    typedef _VSTD::__state<_CharT> __state;
1792
1793    _LIBCPP_INLINE_VISIBILITY
1794    explicit __back_ref_icase(const _Traits& __traits, unsigned __mexp,
1795                              __node<_CharT>* __s)
1796        : base(__s), __traits_(__traits), __mexp_(__mexp) {}
1797
1798    virtual void __exec(__state&) const;
1799};
1800
1801template <class _CharT, class _Traits>
1802void
1803__back_ref_icase<_CharT, _Traits>::__exec(__state& __s) const
1804{
1805    sub_match<const _CharT*>& __sm = __s.__sub_matches_[__mexp_-1];
1806    if (__sm.matched)
1807    {
1808        ptrdiff_t __len = __sm.second - __sm.first;
1809        if (__s.__last_ - __s.__current_ >= __len)
1810        {
1811            for (ptrdiff_t __i = 0; __i < __len; ++__i)
1812            {
1813                if (__traits_.translate_nocase(__sm.first[__i]) !=
1814                                __traits_.translate_nocase(__s.__current_[__i]))
1815                    goto __not_equal;
1816            }
1817            __s.__do_ = __state::__accept_but_not_consume;
1818            __s.__current_ += __len;
1819            __s.__node_ = this->first();
1820        }
1821        else
1822        {
1823            __s.__do_ = __state::__reject;
1824            __s.__node_ = nullptr;
1825        }
1826    }
1827    else
1828    {
1829__not_equal:
1830        __s.__do_ = __state::__reject;
1831        __s.__node_ = nullptr;
1832    }
1833}
1834
1835// __back_ref_collate
1836
1837template <class _CharT, class _Traits>
1838class __back_ref_collate
1839    : public __owns_one_state<_CharT>
1840{
1841    typedef __owns_one_state<_CharT> base;
1842
1843    _Traits __traits_;
1844    unsigned __mexp_;
1845public:
1846    typedef _VSTD::__state<_CharT> __state;
1847
1848    _LIBCPP_INLINE_VISIBILITY
1849    explicit __back_ref_collate(const _Traits& __traits, unsigned __mexp,
1850                              __node<_CharT>* __s)
1851        : base(__s), __traits_(__traits), __mexp_(__mexp) {}
1852
1853    virtual void __exec(__state&) const;
1854};
1855
1856template <class _CharT, class _Traits>
1857void
1858__back_ref_collate<_CharT, _Traits>::__exec(__state& __s) const
1859{
1860    sub_match<const _CharT*>& __sm = __s.__sub_matches_[__mexp_-1];
1861    if (__sm.matched)
1862    {
1863        ptrdiff_t __len = __sm.second - __sm.first;
1864        if (__s.__last_ - __s.__current_ >= __len)
1865        {
1866            for (ptrdiff_t __i = 0; __i < __len; ++__i)
1867            {
1868                if (__traits_.translate(__sm.first[__i]) !=
1869                                       __traits_.translate(__s.__current_[__i]))
1870                    goto __not_equal;
1871            }
1872            __s.__do_ = __state::__accept_but_not_consume;
1873            __s.__current_ += __len;
1874            __s.__node_ = this->first();
1875        }
1876        else
1877        {
1878            __s.__do_ = __state::__reject;
1879            __s.__node_ = nullptr;
1880        }
1881    }
1882    else
1883    {
1884__not_equal:
1885        __s.__do_ = __state::__reject;
1886        __s.__node_ = nullptr;
1887    }
1888}
1889
1890// __word_boundary
1891
1892template <class _CharT, class _Traits>
1893class __word_boundary
1894    : public __owns_one_state<_CharT>
1895{
1896    typedef __owns_one_state<_CharT> base;
1897
1898    _Traits __traits_;
1899    bool __invert_;
1900public:
1901    typedef _VSTD::__state<_CharT> __state;
1902
1903    _LIBCPP_INLINE_VISIBILITY
1904    explicit __word_boundary(const _Traits& __traits, bool __invert,
1905                             __node<_CharT>* __s)
1906        : base(__s), __traits_(__traits), __invert_(__invert) {}
1907
1908    virtual void __exec(__state&) const;
1909};
1910
1911template <class _CharT, class _Traits>
1912void
1913__word_boundary<_CharT, _Traits>::__exec(__state& __s) const
1914{
1915    bool __is_word_b = false;
1916    if (__s.__first_ != __s.__last_)
1917    {
1918        if (__s.__current_ == __s.__last_)
1919        {
1920            if (!(__s.__flags_ & regex_constants::match_not_eow))
1921            {
1922                _CharT __c = __s.__current_[-1];
1923                __is_word_b = __c == '_' ||
1924                              __traits_.isctype(__c, ctype_base::alnum);
1925            }
1926        }
1927        else if (__s.__current_ == __s.__first_ &&
1928                !(__s.__flags_ & regex_constants::match_prev_avail))
1929        {
1930            if (!(__s.__flags_ & regex_constants::match_not_bow))
1931            {
1932                _CharT __c = *__s.__current_;
1933                __is_word_b = __c == '_' ||
1934                              __traits_.isctype(__c, ctype_base::alnum);
1935            }
1936        }
1937        else
1938        {
1939            _CharT __c1 = __s.__current_[-1];
1940            _CharT __c2 = *__s.__current_;
1941            bool __is_c1_b = __c1 == '_' ||
1942                             __traits_.isctype(__c1, ctype_base::alnum);
1943            bool __is_c2_b = __c2 == '_' ||
1944                             __traits_.isctype(__c2, ctype_base::alnum);
1945            __is_word_b = __is_c1_b != __is_c2_b;
1946        }
1947    }
1948    if (__is_word_b != __invert_)
1949    {
1950        __s.__do_ = __state::__accept_but_not_consume;
1951        __s.__node_ = this->first();
1952    }
1953    else
1954    {
1955        __s.__do_ = __state::__reject;
1956        __s.__node_ = nullptr;
1957    }
1958}
1959
1960// __l_anchor
1961
1962template <class _CharT>
1963class __l_anchor
1964    : public __owns_one_state<_CharT>
1965{
1966    typedef __owns_one_state<_CharT> base;
1967
1968public:
1969    typedef _VSTD::__state<_CharT> __state;
1970
1971    _LIBCPP_INLINE_VISIBILITY
1972    __l_anchor(__node<_CharT>* __s)
1973        : base(__s) {}
1974
1975    virtual void __exec(__state&) const;
1976};
1977
1978template <class _CharT>
1979void
1980__l_anchor<_CharT>::__exec(__state& __s) const
1981{
1982    if (__s.__at_first_ && __s.__current_ == __s.__first_ &&
1983        !(__s.__flags_ & regex_constants::match_not_bol))
1984    {
1985        __s.__do_ = __state::__accept_but_not_consume;
1986        __s.__node_ = this->first();
1987    }
1988    else
1989    {
1990        __s.__do_ = __state::__reject;
1991        __s.__node_ = nullptr;
1992    }
1993}
1994
1995// __r_anchor
1996
1997template <class _CharT>
1998class __r_anchor
1999    : public __owns_one_state<_CharT>
2000{
2001    typedef __owns_one_state<_CharT> base;
2002
2003public:
2004    typedef _VSTD::__state<_CharT> __state;
2005
2006    _LIBCPP_INLINE_VISIBILITY
2007    __r_anchor(__node<_CharT>* __s)
2008        : base(__s) {}
2009
2010    virtual void __exec(__state&) const;
2011};
2012
2013template <class _CharT>
2014void
2015__r_anchor<_CharT>::__exec(__state& __s) const
2016{
2017    if (__s.__current_ == __s.__last_ &&
2018        !(__s.__flags_ & regex_constants::match_not_eol))
2019    {
2020        __s.__do_ = __state::__accept_but_not_consume;
2021        __s.__node_ = this->first();
2022    }
2023    else
2024    {
2025        __s.__do_ = __state::__reject;
2026        __s.__node_ = nullptr;
2027    }
2028}
2029
2030// __match_any
2031
2032template <class _CharT>
2033class __match_any
2034    : public __owns_one_state<_CharT>
2035{
2036    typedef __owns_one_state<_CharT> base;
2037
2038public:
2039    typedef _VSTD::__state<_CharT> __state;
2040
2041    _LIBCPP_INLINE_VISIBILITY
2042    __match_any(__node<_CharT>* __s)
2043        : base(__s) {}
2044
2045    virtual void __exec(__state&) const;
2046};
2047
2048template <class _CharT>
2049void
2050__match_any<_CharT>::__exec(__state& __s) const
2051{
2052    if (__s.__current_ != __s.__last_ && *__s.__current_ != 0)
2053    {
2054        __s.__do_ = __state::__accept_and_consume;
2055        ++__s.__current_;
2056        __s.__node_ = this->first();
2057    }
2058    else
2059    {
2060        __s.__do_ = __state::__reject;
2061        __s.__node_ = nullptr;
2062    }
2063}
2064
2065// __match_any_but_newline
2066
2067template <class _CharT>
2068class __match_any_but_newline
2069    : public __owns_one_state<_CharT>
2070{
2071    typedef __owns_one_state<_CharT> base;
2072
2073public:
2074    typedef _VSTD::__state<_CharT> __state;
2075
2076    _LIBCPP_INLINE_VISIBILITY
2077    __match_any_but_newline(__node<_CharT>* __s)
2078        : base(__s) {}
2079
2080    virtual void __exec(__state&) const;
2081};
2082
2083template <> _LIBCPP_FUNC_VIS void __match_any_but_newline<char>::__exec(__state&) const;
2084template <> _LIBCPP_FUNC_VIS void __match_any_but_newline<wchar_t>::__exec(__state&) const;
2085
2086// __match_char
2087
2088template <class _CharT>
2089class __match_char
2090    : public __owns_one_state<_CharT>
2091{
2092    typedef __owns_one_state<_CharT> base;
2093
2094    _CharT __c_;
2095
2096    __match_char(const __match_char&);
2097    __match_char& operator=(const __match_char&);
2098public:
2099    typedef _VSTD::__state<_CharT> __state;
2100
2101    _LIBCPP_INLINE_VISIBILITY
2102    __match_char(_CharT __c, __node<_CharT>* __s)
2103        : base(__s), __c_(__c) {}
2104
2105    virtual void __exec(__state&) const;
2106};
2107
2108template <class _CharT>
2109void
2110__match_char<_CharT>::__exec(__state& __s) const
2111{
2112    if (__s.__current_ != __s.__last_ && *__s.__current_ == __c_)
2113    {
2114        __s.__do_ = __state::__accept_and_consume;
2115        ++__s.__current_;
2116        __s.__node_ = this->first();
2117    }
2118    else
2119    {
2120        __s.__do_ = __state::__reject;
2121        __s.__node_ = nullptr;
2122    }
2123}
2124
2125// __match_char_icase
2126
2127template <class _CharT, class _Traits>
2128class __match_char_icase
2129    : public __owns_one_state<_CharT>
2130{
2131    typedef __owns_one_state<_CharT> base;
2132
2133    _Traits __traits_;
2134    _CharT __c_;
2135
2136    __match_char_icase(const __match_char_icase&);
2137    __match_char_icase& operator=(const __match_char_icase&);
2138public:
2139    typedef _VSTD::__state<_CharT> __state;
2140
2141    _LIBCPP_INLINE_VISIBILITY
2142    __match_char_icase(const _Traits& __traits, _CharT __c, __node<_CharT>* __s)
2143        : base(__s), __traits_(__traits), __c_(__traits.translate_nocase(__c)) {}
2144
2145    virtual void __exec(__state&) const;
2146};
2147
2148template <class _CharT, class _Traits>
2149void
2150__match_char_icase<_CharT, _Traits>::__exec(__state& __s) const
2151{
2152    if (__s.__current_ != __s.__last_ &&
2153        __traits_.translate_nocase(*__s.__current_) == __c_)
2154    {
2155        __s.__do_ = __state::__accept_and_consume;
2156        ++__s.__current_;
2157        __s.__node_ = this->first();
2158    }
2159    else
2160    {
2161        __s.__do_ = __state::__reject;
2162        __s.__node_ = nullptr;
2163    }
2164}
2165
2166// __match_char_collate
2167
2168template <class _CharT, class _Traits>
2169class __match_char_collate
2170    : public __owns_one_state<_CharT>
2171{
2172    typedef __owns_one_state<_CharT> base;
2173
2174    _Traits __traits_;
2175    _CharT __c_;
2176
2177    __match_char_collate(const __match_char_collate&);
2178    __match_char_collate& operator=(const __match_char_collate&);
2179public:
2180    typedef _VSTD::__state<_CharT> __state;
2181
2182    _LIBCPP_INLINE_VISIBILITY
2183    __match_char_collate(const _Traits& __traits, _CharT __c, __node<_CharT>* __s)
2184        : base(__s), __traits_(__traits), __c_(__traits.translate(__c)) {}
2185
2186    virtual void __exec(__state&) const;
2187};
2188
2189template <class _CharT, class _Traits>
2190void
2191__match_char_collate<_CharT, _Traits>::__exec(__state& __s) const
2192{
2193    if (__s.__current_ != __s.__last_ &&
2194        __traits_.translate(*__s.__current_) == __c_)
2195    {
2196        __s.__do_ = __state::__accept_and_consume;
2197        ++__s.__current_;
2198        __s.__node_ = this->first();
2199    }
2200    else
2201    {
2202        __s.__do_ = __state::__reject;
2203        __s.__node_ = nullptr;
2204    }
2205}
2206
2207// __bracket_expression
2208
2209template <class _CharT, class _Traits>
2210class __bracket_expression
2211    : public __owns_one_state<_CharT>
2212{
2213    typedef __owns_one_state<_CharT> base;
2214    typedef typename _Traits::string_type string_type;
2215
2216    _Traits __traits_;
2217    vector<_CharT> __chars_;
2218    vector<_CharT> __neg_chars_;
2219    vector<pair<string_type, string_type> > __ranges_;
2220    vector<pair<_CharT, _CharT> > __digraphs_;
2221    vector<string_type> __equivalences_;
2222    typename regex_traits<_CharT>::char_class_type __mask_;
2223    typename regex_traits<_CharT>::char_class_type __neg_mask_;
2224    bool __negate_;
2225    bool __icase_;
2226    bool __collate_;
2227    bool __might_have_digraph_;
2228
2229    __bracket_expression(const __bracket_expression&);
2230    __bracket_expression& operator=(const __bracket_expression&);
2231public:
2232    typedef _VSTD::__state<_CharT> __state;
2233
2234    _LIBCPP_INLINE_VISIBILITY
2235    __bracket_expression(const _Traits& __traits, __node<_CharT>* __s,
2236                                 bool __negate, bool __icase, bool __collate)
2237        : base(__s), __traits_(__traits), __mask_(), __neg_mask_(),
2238          __negate_(__negate), __icase_(__icase), __collate_(__collate),
2239          __might_have_digraph_(__traits_.getloc().name() != "C") {}
2240
2241    virtual void __exec(__state&) const;
2242
2243    _LIBCPP_INLINE_VISIBILITY
2244    bool __negated() const {return __negate_;}
2245
2246    _LIBCPP_INLINE_VISIBILITY
2247    void __add_char(_CharT __c)
2248        {
2249            if (__icase_)
2250                __chars_.push_back(__traits_.translate_nocase(__c));
2251            else if (__collate_)
2252                __chars_.push_back(__traits_.translate(__c));
2253            else
2254                __chars_.push_back(__c);
2255        }
2256    _LIBCPP_INLINE_VISIBILITY
2257    void __add_neg_char(_CharT __c)
2258        {
2259            if (__icase_)
2260                __neg_chars_.push_back(__traits_.translate_nocase(__c));
2261            else if (__collate_)
2262                __neg_chars_.push_back(__traits_.translate(__c));
2263            else
2264                __neg_chars_.push_back(__c);
2265        }
2266    _LIBCPP_INLINE_VISIBILITY
2267    void __add_range(string_type __b, string_type __e)
2268        {
2269            if (__collate_)
2270            {
2271                if (__icase_)
2272                {
2273                    for (size_t __i = 0; __i < __b.size(); ++__i)
2274                        __b[__i] = __traits_.translate_nocase(__b[__i]);
2275                    for (size_t __i = 0; __i < __e.size(); ++__i)
2276                        __e[__i] = __traits_.translate_nocase(__e[__i]);
2277                }
2278                else
2279                {
2280                    for (size_t __i = 0; __i < __b.size(); ++__i)
2281                        __b[__i] = __traits_.translate(__b[__i]);
2282                    for (size_t __i = 0; __i < __e.size(); ++__i)
2283                        __e[__i] = __traits_.translate(__e[__i]);
2284                }
2285                __ranges_.push_back(make_pair(
2286                                  __traits_.transform(__b.begin(), __b.end()),
2287                                  __traits_.transform(__e.begin(), __e.end())));
2288            }
2289            else
2290            {
2291                if (__b.size() != 1 || __e.size() != 1)
2292                    __throw_regex_error<regex_constants::error_collate>();
2293                if (__icase_)
2294                {
2295                    __b[0] = __traits_.translate_nocase(__b[0]);
2296                    __e[0] = __traits_.translate_nocase(__e[0]);
2297                }
2298                __ranges_.push_back(make_pair(_VSTD::move(__b), _VSTD::move(__e)));
2299            }
2300        }
2301    _LIBCPP_INLINE_VISIBILITY
2302    void __add_digraph(_CharT __c1, _CharT __c2)
2303        {
2304            if (__icase_)
2305                __digraphs_.push_back(make_pair(__traits_.translate_nocase(__c1),
2306                                                __traits_.translate_nocase(__c2)));
2307            else if (__collate_)
2308                __digraphs_.push_back(make_pair(__traits_.translate(__c1),
2309                                                __traits_.translate(__c2)));
2310            else
2311                __digraphs_.push_back(make_pair(__c1, __c2));
2312        }
2313    _LIBCPP_INLINE_VISIBILITY
2314    void __add_equivalence(const string_type& __s)
2315        {__equivalences_.push_back(__s);}
2316    _LIBCPP_INLINE_VISIBILITY
2317    void __add_class(typename regex_traits<_CharT>::char_class_type __mask)
2318        {__mask_ |= __mask;}
2319    _LIBCPP_INLINE_VISIBILITY
2320    void __add_neg_class(typename regex_traits<_CharT>::char_class_type __mask)
2321        {__neg_mask_ |= __mask;}
2322};
2323
2324template <class _CharT, class _Traits>
2325void
2326__bracket_expression<_CharT, _Traits>::__exec(__state& __s) const
2327{
2328    bool __found = false;
2329    unsigned __consumed = 0;
2330    if (__s.__current_ != __s.__last_)
2331    {
2332        ++__consumed;
2333        if (__might_have_digraph_)
2334        {
2335            const _CharT* __next = _VSTD::next(__s.__current_);
2336            if (__next != __s.__last_)
2337            {
2338                pair<_CharT, _CharT> __ch2(*__s.__current_, *__next);
2339                if (__icase_)
2340                {
2341                    __ch2.first = __traits_.translate_nocase(__ch2.first);
2342                    __ch2.second = __traits_.translate_nocase(__ch2.second);
2343                }
2344                else if (__collate_)
2345                {
2346                    __ch2.first = __traits_.translate(__ch2.first);
2347                    __ch2.second = __traits_.translate(__ch2.second);
2348                }
2349                if (!__traits_.lookup_collatename(&__ch2.first, &__ch2.first+2).empty())
2350                {
2351                    // __ch2 is a digraph in this locale
2352                    ++__consumed;
2353                    for (size_t __i = 0; __i < __digraphs_.size(); ++__i)
2354                    {
2355                        if (__ch2 == __digraphs_[__i])
2356                        {
2357                            __found = true;
2358                            goto __exit;
2359                        }
2360                    }
2361                    if (__collate_ && !__ranges_.empty())
2362                    {
2363                        string_type __s2 = __traits_.transform(&__ch2.first,
2364                                                               &__ch2.first + 2);
2365                        for (size_t __i = 0; __i < __ranges_.size(); ++__i)
2366                        {
2367                            if (__ranges_[__i].first <= __s2 &&
2368                                __s2 <= __ranges_[__i].second)
2369                            {
2370                                __found = true;
2371                                goto __exit;
2372                            }
2373                        }
2374                    }
2375                    if (!__equivalences_.empty())
2376                    {
2377                        string_type __s2 = __traits_.transform_primary(&__ch2.first,
2378                                                                       &__ch2.first + 2);
2379                        for (size_t __i = 0; __i < __equivalences_.size(); ++__i)
2380                        {
2381                            if (__s2 == __equivalences_[__i])
2382                            {
2383                                __found = true;
2384                                goto __exit;
2385                            }
2386                        }
2387                    }
2388                    if (__traits_.isctype(__ch2.first, __mask_) &&
2389                        __traits_.isctype(__ch2.second, __mask_))
2390                    {
2391                        __found = true;
2392                        goto __exit;
2393                    }
2394                    if (!__traits_.isctype(__ch2.first, __neg_mask_) &&
2395                        !__traits_.isctype(__ch2.second, __neg_mask_))
2396                    {
2397                        __found = true;
2398                        goto __exit;
2399                    }
2400                    goto __exit;
2401                }
2402            }
2403        }
2404        // test *__s.__current_ as not a digraph
2405        _CharT __ch = *__s.__current_;
2406        if (__icase_)
2407            __ch = __traits_.translate_nocase(__ch);
2408        else if (__collate_)
2409            __ch = __traits_.translate(__ch);
2410        for (size_t __i = 0; __i < __chars_.size(); ++__i)
2411        {
2412            if (__ch == __chars_[__i])
2413            {
2414                __found = true;
2415                goto __exit;
2416            }
2417        }
2418        // set of "__found" chars =
2419        //   union(complement(union(__neg_chars_, __neg_mask_)),
2420        //         other cases...)
2421        //
2422        // __neg_chars_ and __neg_mask_'d better be handled together, as there
2423        // are no short circuit opportunities.
2424        //
2425        // In addition, when __neg_mask_/__neg_chars_ is empty, they should be
2426        // treated as all ones/all chars.
2427        {
2428          const bool __in_neg_mask = (__neg_mask_ == 0) ||
2429              __traits_.isctype(__ch, __neg_mask_);
2430          const bool __in_neg_chars =
2431              __neg_chars_.empty() ||
2432              std::find(__neg_chars_.begin(), __neg_chars_.end(), __ch) !=
2433              __neg_chars_.end();
2434          if (!(__in_neg_mask || __in_neg_chars))
2435          {
2436            __found = true;
2437            goto __exit;
2438          }
2439        }
2440        if (!__ranges_.empty())
2441        {
2442            string_type __s2 = __collate_ ?
2443                                   __traits_.transform(&__ch, &__ch + 1) :
2444                                   string_type(1, __ch);
2445            for (size_t __i = 0; __i < __ranges_.size(); ++__i)
2446            {
2447                if (__ranges_[__i].first <= __s2 && __s2 <= __ranges_[__i].second)
2448                {
2449                    __found = true;
2450                    goto __exit;
2451                }
2452            }
2453        }
2454        if (!__equivalences_.empty())
2455        {
2456            string_type __s2 = __traits_.transform_primary(&__ch, &__ch + 1);
2457            for (size_t __i = 0; __i < __equivalences_.size(); ++__i)
2458            {
2459                if (__s2 == __equivalences_[__i])
2460                {
2461                    __found = true;
2462                    goto __exit;
2463                }
2464            }
2465        }
2466        if (__traits_.isctype(__ch, __mask_))
2467        {
2468            __found = true;
2469            goto __exit;
2470        }
2471    }
2472    else
2473        __found = __negate_;  // force reject
2474__exit:
2475    if (__found != __negate_)
2476    {
2477        __s.__do_ = __state::__accept_and_consume;
2478        __s.__current_ += __consumed;
2479        __s.__node_ = this->first();
2480    }
2481    else
2482    {
2483        __s.__do_ = __state::__reject;
2484        __s.__node_ = nullptr;
2485    }
2486}
2487
2488template <class _CharT, class _Traits> class __lookahead;
2489
2490template <class _CharT, class _Traits = regex_traits<_CharT> >
2491class _LIBCPP_TEMPLATE_VIS basic_regex
2492{
2493public:
2494    // types:
2495    typedef _CharT                              value_type;
2496    typedef _Traits                             traits_type;
2497    typedef typename _Traits::string_type       string_type;
2498    typedef regex_constants::syntax_option_type flag_type;
2499    typedef typename _Traits::locale_type       locale_type;
2500
2501private:
2502    _Traits   __traits_;
2503    flag_type __flags_;
2504    unsigned __marked_count_;
2505    unsigned __loop_count_;
2506    int __open_count_;
2507    shared_ptr<__empty_state<_CharT> > __start_;
2508    __owns_one_state<_CharT>* __end_;
2509
2510    typedef _VSTD::__state<_CharT> __state;
2511    typedef _VSTD::__node<_CharT> __node;
2512
2513public:
2514    // constants:
2515    static const regex_constants::syntax_option_type icase = regex_constants::icase;
2516    static const regex_constants::syntax_option_type nosubs = regex_constants::nosubs;
2517    static const regex_constants::syntax_option_type optimize = regex_constants::optimize;
2518    static const regex_constants::syntax_option_type collate = regex_constants::collate;
2519    static const regex_constants::syntax_option_type ECMAScript = regex_constants::ECMAScript;
2520    static const regex_constants::syntax_option_type basic = regex_constants::basic;
2521    static const regex_constants::syntax_option_type extended = regex_constants::extended;
2522    static const regex_constants::syntax_option_type awk = regex_constants::awk;
2523    static const regex_constants::syntax_option_type grep = regex_constants::grep;
2524    static const regex_constants::syntax_option_type egrep = regex_constants::egrep;
2525
2526    // construct/copy/destroy:
2527    _LIBCPP_INLINE_VISIBILITY
2528    basic_regex()
2529        : __flags_(), __marked_count_(0), __loop_count_(0), __open_count_(0),
2530          __end_(0)
2531        {}
2532    _LIBCPP_INLINE_VISIBILITY
2533    explicit basic_regex(const value_type* __p, flag_type __f = regex_constants::ECMAScript)
2534        : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
2535          __end_(0)
2536        {__parse(__p, __p + __traits_.length(__p));}
2537    _LIBCPP_INLINE_VISIBILITY
2538    basic_regex(const value_type* __p, size_t __len, flag_type __f = regex_constants::ECMAScript)
2539        : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
2540          __end_(0)
2541        {__parse(__p, __p + __len);}
2542//     basic_regex(const basic_regex&) = default;
2543//     basic_regex(basic_regex&&) = default;
2544    template <class _ST, class _SA>
2545        _LIBCPP_INLINE_VISIBILITY
2546        explicit basic_regex(const basic_string<value_type, _ST, _SA>& __p,
2547                             flag_type __f = regex_constants::ECMAScript)
2548        : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
2549          __end_(0)
2550        {__parse(__p.begin(), __p.end());}
2551    template <class _ForwardIterator>
2552        _LIBCPP_INLINE_VISIBILITY
2553        basic_regex(_ForwardIterator __first, _ForwardIterator __last,
2554                    flag_type __f = regex_constants::ECMAScript)
2555        : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
2556          __end_(0)
2557        {__parse(__first, __last);}
2558#ifndef _LIBCPP_CXX03_LANG
2559    _LIBCPP_INLINE_VISIBILITY
2560    basic_regex(initializer_list<value_type> __il,
2561                flag_type __f = regex_constants::ECMAScript)
2562        : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
2563          __end_(0)
2564        {__parse(__il.begin(), __il.end());}
2565#endif  // _LIBCPP_CXX03_LANG
2566
2567//    ~basic_regex() = default;
2568
2569//     basic_regex& operator=(const basic_regex&) = default;
2570//     basic_regex& operator=(basic_regex&&) = default;
2571    _LIBCPP_INLINE_VISIBILITY
2572    basic_regex& operator=(const value_type* __p)
2573        {return assign(__p);}
2574#ifndef _LIBCPP_CXX03_LANG
2575    _LIBCPP_INLINE_VISIBILITY
2576    basic_regex& operator=(initializer_list<value_type> __il)
2577        {return assign(__il);}
2578#endif  // _LIBCPP_CXX03_LANG
2579    template <class _ST, class _SA>
2580        _LIBCPP_INLINE_VISIBILITY
2581        basic_regex& operator=(const basic_string<value_type, _ST, _SA>& __p)
2582        {return assign(__p);}
2583
2584    // assign:
2585    _LIBCPP_INLINE_VISIBILITY
2586    basic_regex& assign(const basic_regex& __that)
2587        {return *this = __that;}
2588#ifndef _LIBCPP_CXX03_LANG
2589    _LIBCPP_INLINE_VISIBILITY
2590    basic_regex& assign(basic_regex&& __that) _NOEXCEPT
2591        {return *this = _VSTD::move(__that);}
2592#endif
2593    _LIBCPP_INLINE_VISIBILITY
2594    basic_regex& assign(const value_type* __p, flag_type __f = regex_constants::ECMAScript)
2595        {return assign(__p, __p + __traits_.length(__p), __f);}
2596    _LIBCPP_INLINE_VISIBILITY
2597    basic_regex& assign(const value_type* __p, size_t __len, flag_type __f)
2598        {return assign(__p, __p + __len, __f);}
2599    template <class _ST, class _SA>
2600        _LIBCPP_INLINE_VISIBILITY
2601        basic_regex& assign(const basic_string<value_type, _ST, _SA>& __s,
2602                            flag_type __f = regex_constants::ECMAScript)
2603            {return assign(__s.begin(), __s.end(), __f);}
2604
2605    template <class _InputIterator>
2606        _LIBCPP_INLINE_VISIBILITY
2607        typename enable_if
2608        <
2609             __is_input_iterator  <_InputIterator>::value &&
2610            !__is_forward_iterator<_InputIterator>::value,
2611            basic_regex&
2612        >::type
2613        assign(_InputIterator __first, _InputIterator __last,
2614                            flag_type __f = regex_constants::ECMAScript)
2615        {
2616            basic_string<_CharT> __t(__first, __last);
2617            return assign(__t.begin(), __t.end(), __f);
2618        }
2619
2620private:
2621    _LIBCPP_INLINE_VISIBILITY
2622    void __member_init(flag_type __f)
2623    {
2624        __flags_ = __f;
2625        __marked_count_ = 0;
2626        __loop_count_ = 0;
2627        __open_count_ = 0;
2628        __end_ = nullptr;
2629    }
2630public:
2631
2632    template <class _ForwardIterator>
2633        _LIBCPP_INLINE_VISIBILITY
2634        typename enable_if
2635        <
2636            __is_forward_iterator<_ForwardIterator>::value,
2637            basic_regex&
2638        >::type
2639        assign(_ForwardIterator __first, _ForwardIterator __last,
2640                            flag_type __f = regex_constants::ECMAScript)
2641        {
2642            return assign(basic_regex(__first, __last, __f));
2643        }
2644
2645#ifndef _LIBCPP_CXX03_LANG
2646
2647    _LIBCPP_INLINE_VISIBILITY
2648    basic_regex& assign(initializer_list<value_type> __il,
2649                        flag_type __f = regex_constants::ECMAScript)
2650        {return assign(__il.begin(), __il.end(), __f);}
2651
2652#endif  // _LIBCPP_CXX03_LANG
2653
2654    // const operations:
2655    _LIBCPP_INLINE_VISIBILITY
2656    unsigned mark_count() const {return __marked_count_;}
2657    _LIBCPP_INLINE_VISIBILITY
2658    flag_type flags() const {return __flags_;}
2659
2660    // locale:
2661    _LIBCPP_INLINE_VISIBILITY
2662    locale_type imbue(locale_type __loc)
2663    {
2664        __member_init(ECMAScript);
2665        __start_.reset();
2666        return __traits_.imbue(__loc);
2667    }
2668    _LIBCPP_INLINE_VISIBILITY
2669    locale_type getloc() const {return __traits_.getloc();}
2670
2671    // swap:
2672    void swap(basic_regex& __r);
2673
2674private:
2675    _LIBCPP_INLINE_VISIBILITY
2676    unsigned __loop_count() const {return __loop_count_;}
2677
2678    template <class _ForwardIterator>
2679        _ForwardIterator
2680        __parse(_ForwardIterator __first, _ForwardIterator __last);
2681    template <class _ForwardIterator>
2682        _ForwardIterator
2683        __parse_basic_reg_exp(_ForwardIterator __first, _ForwardIterator __last);
2684    template <class _ForwardIterator>
2685        _ForwardIterator
2686        __parse_RE_expression(_ForwardIterator __first, _ForwardIterator __last);
2687    template <class _ForwardIterator>
2688        _ForwardIterator
2689        __parse_simple_RE(_ForwardIterator __first, _ForwardIterator __last);
2690    template <class _ForwardIterator>
2691        _ForwardIterator
2692        __parse_nondupl_RE(_ForwardIterator __first, _ForwardIterator __last);
2693    template <class _ForwardIterator>
2694        _ForwardIterator
2695        __parse_one_char_or_coll_elem_RE(_ForwardIterator __first, _ForwardIterator __last);
2696    template <class _ForwardIterator>
2697        _ForwardIterator
2698        __parse_Back_open_paren(_ForwardIterator __first, _ForwardIterator __last);
2699    template <class _ForwardIterator>
2700        _ForwardIterator
2701        __parse_Back_close_paren(_ForwardIterator __first, _ForwardIterator __last);
2702    template <class _ForwardIterator>
2703        _ForwardIterator
2704        __parse_Back_open_brace(_ForwardIterator __first, _ForwardIterator __last);
2705    template <class _ForwardIterator>
2706        _ForwardIterator
2707        __parse_Back_close_brace(_ForwardIterator __first, _ForwardIterator __last);
2708    template <class _ForwardIterator>
2709        _ForwardIterator
2710        __parse_BACKREF(_ForwardIterator __first, _ForwardIterator __last);
2711    template <class _ForwardIterator>
2712        _ForwardIterator
2713        __parse_ORD_CHAR(_ForwardIterator __first, _ForwardIterator __last);
2714    template <class _ForwardIterator>
2715        _ForwardIterator
2716        __parse_QUOTED_CHAR(_ForwardIterator __first, _ForwardIterator __last);
2717    template <class _ForwardIterator>
2718        _ForwardIterator
2719        __parse_RE_dupl_symbol(_ForwardIterator __first, _ForwardIterator __last,
2720                               __owns_one_state<_CharT>* __s,
2721                               unsigned __mexp_begin, unsigned __mexp_end);
2722    template <class _ForwardIterator>
2723        _ForwardIterator
2724        __parse_ERE_dupl_symbol(_ForwardIterator __first, _ForwardIterator __last,
2725                                __owns_one_state<_CharT>* __s,
2726                                unsigned __mexp_begin, unsigned __mexp_end);
2727    template <class _ForwardIterator>
2728        _ForwardIterator
2729        __parse_bracket_expression(_ForwardIterator __first, _ForwardIterator __last);
2730    template <class _ForwardIterator>
2731        _ForwardIterator
2732        __parse_follow_list(_ForwardIterator __first, _ForwardIterator __last,
2733                            __bracket_expression<_CharT, _Traits>* __ml);
2734    template <class _ForwardIterator>
2735        _ForwardIterator
2736        __parse_expression_term(_ForwardIterator __first, _ForwardIterator __last,
2737                                __bracket_expression<_CharT, _Traits>* __ml);
2738    template <class _ForwardIterator>
2739        _ForwardIterator
2740        __parse_equivalence_class(_ForwardIterator __first, _ForwardIterator __last,
2741                                  __bracket_expression<_CharT, _Traits>* __ml);
2742    template <class _ForwardIterator>
2743        _ForwardIterator
2744        __parse_character_class(_ForwardIterator __first, _ForwardIterator __last,
2745                                __bracket_expression<_CharT, _Traits>* __ml);
2746    template <class _ForwardIterator>
2747        _ForwardIterator
2748        __parse_collating_symbol(_ForwardIterator __first, _ForwardIterator __last,
2749                                 basic_string<_CharT>& __col_sym);
2750    template <class _ForwardIterator>
2751        _ForwardIterator
2752        __parse_DUP_COUNT(_ForwardIterator __first, _ForwardIterator __last, int& __c);
2753    template <class _ForwardIterator>
2754        _ForwardIterator
2755        __parse_extended_reg_exp(_ForwardIterator __first, _ForwardIterator __last);
2756    template <class _ForwardIterator>
2757        _ForwardIterator
2758        __parse_ERE_branch(_ForwardIterator __first, _ForwardIterator __last);
2759    template <class _ForwardIterator>
2760        _ForwardIterator
2761        __parse_ERE_expression(_ForwardIterator __first, _ForwardIterator __last);
2762    template <class _ForwardIterator>
2763        _ForwardIterator
2764        __parse_one_char_or_coll_elem_ERE(_ForwardIterator __first, _ForwardIterator __last);
2765    template <class _ForwardIterator>
2766        _ForwardIterator
2767        __parse_ORD_CHAR_ERE(_ForwardIterator __first, _ForwardIterator __last);
2768    template <class _ForwardIterator>
2769        _ForwardIterator
2770        __parse_QUOTED_CHAR_ERE(_ForwardIterator __first, _ForwardIterator __last);
2771    template <class _ForwardIterator>
2772        _ForwardIterator
2773        __parse_ecma_exp(_ForwardIterator __first, _ForwardIterator __last);
2774    template <class _ForwardIterator>
2775        _ForwardIterator
2776        __parse_alternative(_ForwardIterator __first, _ForwardIterator __last);
2777    template <class _ForwardIterator>
2778        _ForwardIterator
2779        __parse_term(_ForwardIterator __first, _ForwardIterator __last);
2780    template <class _ForwardIterator>
2781        _ForwardIterator
2782        __parse_assertion(_ForwardIterator __first, _ForwardIterator __last);
2783    template <class _ForwardIterator>
2784        _ForwardIterator
2785        __parse_atom(_ForwardIterator __first, _ForwardIterator __last);
2786    template <class _ForwardIterator>
2787        _ForwardIterator
2788        __parse_atom_escape(_ForwardIterator __first, _ForwardIterator __last);
2789    template <class _ForwardIterator>
2790        _ForwardIterator
2791        __parse_decimal_escape(_ForwardIterator __first, _ForwardIterator __last);
2792    template <class _ForwardIterator>
2793        _ForwardIterator
2794        __parse_character_class_escape(_ForwardIterator __first, _ForwardIterator __last);
2795    template <class _ForwardIterator>
2796        _ForwardIterator
2797        __parse_character_escape(_ForwardIterator __first, _ForwardIterator __last,
2798                                 basic_string<_CharT>* __str = nullptr);
2799    template <class _ForwardIterator>
2800        _ForwardIterator
2801        __parse_pattern_character(_ForwardIterator __first, _ForwardIterator __last);
2802    template <class _ForwardIterator>
2803        _ForwardIterator
2804        __parse_grep(_ForwardIterator __first, _ForwardIterator __last);
2805    template <class _ForwardIterator>
2806        _ForwardIterator
2807        __parse_egrep(_ForwardIterator __first, _ForwardIterator __last);
2808    template <class _ForwardIterator>
2809        _ForwardIterator
2810        __parse_class_escape(_ForwardIterator __first, _ForwardIterator __last,
2811                          basic_string<_CharT>& __str,
2812                          __bracket_expression<_CharT, _Traits>* __ml);
2813    template <class _ForwardIterator>
2814        _ForwardIterator
2815        __parse_awk_escape(_ForwardIterator __first, _ForwardIterator __last,
2816                          basic_string<_CharT>* __str = nullptr);
2817
2818    _LIBCPP_INLINE_VISIBILITY
2819    void __push_l_anchor();
2820    void __push_r_anchor();
2821    void __push_match_any();
2822    void __push_match_any_but_newline();
2823    _LIBCPP_INLINE_VISIBILITY
2824    void __push_greedy_inf_repeat(size_t __min, __owns_one_state<_CharT>* __s,
2825                                  unsigned __mexp_begin = 0, unsigned __mexp_end = 0)
2826        {__push_loop(__min, numeric_limits<size_t>::max(), __s,
2827                     __mexp_begin, __mexp_end);}
2828    _LIBCPP_INLINE_VISIBILITY
2829    void __push_nongreedy_inf_repeat(size_t __min, __owns_one_state<_CharT>* __s,
2830                                  unsigned __mexp_begin = 0, unsigned __mexp_end = 0)
2831        {__push_loop(__min, numeric_limits<size_t>::max(), __s,
2832                     __mexp_begin, __mexp_end, false);}
2833    void __push_loop(size_t __min, size_t __max, __owns_one_state<_CharT>* __s,
2834                     size_t __mexp_begin = 0, size_t __mexp_end = 0,
2835                     bool __greedy = true);
2836    __bracket_expression<_CharT, _Traits>* __start_matching_list(bool __negate);
2837    void __push_char(value_type __c);
2838    void __push_back_ref(int __i);
2839    void __push_alternation(__owns_one_state<_CharT>* __sa,
2840                            __owns_one_state<_CharT>* __sb);
2841    void __push_begin_marked_subexpression();
2842    void __push_end_marked_subexpression(unsigned);
2843    void __push_empty();
2844    void __push_word_boundary(bool);
2845    void __push_lookahead(const basic_regex&, bool, unsigned);
2846
2847    template <class _Allocator>
2848        bool
2849        __search(const _CharT* __first, const _CharT* __last,
2850                 match_results<const _CharT*, _Allocator>& __m,
2851                 regex_constants::match_flag_type __flags) const;
2852
2853    template <class _Allocator>
2854        bool
2855        __match_at_start(const _CharT* __first, const _CharT* __last,
2856                 match_results<const _CharT*, _Allocator>& __m,
2857                 regex_constants::match_flag_type __flags, bool) const;
2858    template <class _Allocator>
2859        bool
2860        __match_at_start_ecma(const _CharT* __first, const _CharT* __last,
2861                 match_results<const _CharT*, _Allocator>& __m,
2862                 regex_constants::match_flag_type __flags, bool) const;
2863    template <class _Allocator>
2864        bool
2865        __match_at_start_posix_nosubs(const _CharT* __first, const _CharT* __last,
2866                 match_results<const _CharT*, _Allocator>& __m,
2867                 regex_constants::match_flag_type __flags, bool) const;
2868    template <class _Allocator>
2869        bool
2870        __match_at_start_posix_subs(const _CharT* __first, const _CharT* __last,
2871                 match_results<const _CharT*, _Allocator>& __m,
2872                 regex_constants::match_flag_type __flags, bool) const;
2873
2874    template <class _Bp, class _Ap, class _Cp, class _Tp>
2875    friend
2876    bool
2877    regex_search(_Bp, _Bp, match_results<_Bp, _Ap>&, const basic_regex<_Cp, _Tp>&,
2878                 regex_constants::match_flag_type);
2879
2880    template <class _Ap, class _Cp, class _Tp>
2881    friend
2882    bool
2883    regex_search(const _Cp*, const _Cp*, match_results<const _Cp*, _Ap>&,
2884                 const basic_regex<_Cp, _Tp>&, regex_constants::match_flag_type);
2885
2886    template <class _Bp, class _Cp, class _Tp>
2887    friend
2888    bool
2889    regex_search(_Bp, _Bp, const basic_regex<_Cp, _Tp>&,
2890                 regex_constants::match_flag_type);
2891
2892    template <class _Cp, class _Tp>
2893    friend
2894    bool
2895    regex_search(const _Cp*, const _Cp*,
2896                 const basic_regex<_Cp, _Tp>&, regex_constants::match_flag_type);
2897
2898    template <class _Cp, class _Ap, class _Tp>
2899    friend
2900    bool
2901    regex_search(const _Cp*, match_results<const _Cp*, _Ap>&, const basic_regex<_Cp, _Tp>&,
2902                 regex_constants::match_flag_type);
2903
2904    template <class _ST, class _SA, class _Cp, class _Tp>
2905    friend
2906    bool
2907    regex_search(const basic_string<_Cp, _ST, _SA>& __s,
2908                 const basic_regex<_Cp, _Tp>& __e,
2909                 regex_constants::match_flag_type __flags);
2910
2911    template <class _ST, class _SA, class _Ap, class _Cp, class _Tp>
2912    friend
2913    bool
2914    regex_search(const basic_string<_Cp, _ST, _SA>& __s,
2915                 match_results<typename basic_string<_Cp, _ST, _SA>::const_iterator, _Ap>&,
2916                 const basic_regex<_Cp, _Tp>& __e,
2917                 regex_constants::match_flag_type __flags);
2918
2919    template <class _Iter, class _Ap, class _Cp, class _Tp>
2920    friend
2921    bool
2922    regex_search(__wrap_iter<_Iter> __first,
2923                 __wrap_iter<_Iter> __last,
2924                 match_results<__wrap_iter<_Iter>, _Ap>& __m,
2925                 const basic_regex<_Cp, _Tp>& __e,
2926                 regex_constants::match_flag_type __flags);
2927
2928    template <class, class> friend class __lookahead;
2929};
2930
2931template <class _CharT, class _Traits>
2932    const regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::icase;
2933template <class _CharT, class _Traits>
2934    const regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::nosubs;
2935template <class _CharT, class _Traits>
2936    const regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::optimize;
2937template <class _CharT, class _Traits>
2938    const regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::collate;
2939template <class _CharT, class _Traits>
2940    const regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::ECMAScript;
2941template <class _CharT, class _Traits>
2942    const regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::basic;
2943template <class _CharT, class _Traits>
2944    const regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::extended;
2945template <class _CharT, class _Traits>
2946    const regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::awk;
2947template <class _CharT, class _Traits>
2948    const regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::grep;
2949template <class _CharT, class _Traits>
2950    const regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::egrep;
2951
2952template <class _CharT, class _Traits>
2953void
2954basic_regex<_CharT, _Traits>::swap(basic_regex& __r)
2955{
2956    using _VSTD::swap;
2957    swap(__traits_, __r.__traits_);
2958    swap(__flags_, __r.__flags_);
2959    swap(__marked_count_, __r.__marked_count_);
2960    swap(__loop_count_, __r.__loop_count_);
2961    swap(__open_count_, __r.__open_count_);
2962    swap(__start_, __r.__start_);
2963    swap(__end_, __r.__end_);
2964}
2965
2966template <class _CharT, class _Traits>
2967inline _LIBCPP_INLINE_VISIBILITY
2968void
2969swap(basic_regex<_CharT, _Traits>& __x, basic_regex<_CharT, _Traits>& __y)
2970{
2971    return __x.swap(__y);
2972}
2973
2974// __lookahead
2975
2976template <class _CharT, class _Traits>
2977class __lookahead
2978    : public __owns_one_state<_CharT>
2979{
2980    typedef __owns_one_state<_CharT> base;
2981
2982    basic_regex<_CharT, _Traits> __exp_;
2983    unsigned __mexp_;
2984    bool __invert_;
2985
2986    __lookahead(const __lookahead&);
2987    __lookahead& operator=(const __lookahead&);
2988public:
2989    typedef _VSTD::__state<_CharT> __state;
2990
2991    _LIBCPP_INLINE_VISIBILITY
2992    __lookahead(const basic_regex<_CharT, _Traits>& __exp, bool __invert, __node<_CharT>* __s, unsigned __mexp)
2993        : base(__s), __exp_(__exp), __mexp_(__mexp), __invert_(__invert) {}
2994
2995    virtual void __exec(__state&) const;
2996};
2997
2998template <class _CharT, class _Traits>
2999void
3000__lookahead<_CharT, _Traits>::__exec(__state& __s) const
3001{
3002    match_results<const _CharT*> __m;
3003    __m.__init(1 + __exp_.mark_count(), __s.__current_, __s.__last_);
3004    bool __matched = __exp_.__match_at_start_ecma(
3005        __s.__current_, __s.__last_,
3006        __m,
3007        (__s.__flags_ | regex_constants::match_continuous) &
3008        ~regex_constants::__full_match,
3009        __s.__at_first_ && __s.__current_ == __s.__first_);
3010    if (__matched != __invert_)
3011    {
3012        __s.__do_ = __state::__accept_but_not_consume;
3013        __s.__node_ = this->first();
3014        for (unsigned __i = 1; __i < __m.size(); ++__i) {
3015            __s.__sub_matches_[__mexp_ + __i - 1] = __m.__matches_[__i];
3016        }
3017    }
3018    else
3019    {
3020        __s.__do_ = __state::__reject;
3021        __s.__node_ = nullptr;
3022    }
3023}
3024
3025template <class _CharT, class _Traits>
3026template <class _ForwardIterator>
3027_ForwardIterator
3028basic_regex<_CharT, _Traits>::__parse(_ForwardIterator __first,
3029                                      _ForwardIterator __last)
3030{
3031    {
3032        unique_ptr<__node> __h(new __end_state<_CharT>);
3033        __start_.reset(new __empty_state<_CharT>(__h.get()));
3034        __h.release();
3035        __end_ = __start_.get();
3036    }
3037    switch (__flags_ & 0x1F0)
3038    {
3039    case ECMAScript:
3040        __first = __parse_ecma_exp(__first, __last);
3041        break;
3042    case basic:
3043        __first = __parse_basic_reg_exp(__first, __last);
3044        break;
3045    case extended:
3046    case awk:
3047        __first = __parse_extended_reg_exp(__first, __last);
3048        break;
3049    case grep:
3050        __first = __parse_grep(__first, __last);
3051        break;
3052    case egrep:
3053        __first = __parse_egrep(__first, __last);
3054        break;
3055    default:
3056        __throw_regex_error<regex_constants::__re_err_grammar>();
3057    }
3058    return __first;
3059}
3060
3061template <class _CharT, class _Traits>
3062template <class _ForwardIterator>
3063_ForwardIterator
3064basic_regex<_CharT, _Traits>::__parse_basic_reg_exp(_ForwardIterator __first,
3065                                                    _ForwardIterator __last)
3066{
3067    if (__first != __last)
3068    {
3069        if (*__first == '^')
3070        {
3071            __push_l_anchor();
3072            ++__first;
3073        }
3074        if (__first != __last)
3075        {
3076            __first = __parse_RE_expression(__first, __last);
3077            if (__first != __last)
3078            {
3079                _ForwardIterator __temp = _VSTD::next(__first);
3080                if (__temp == __last && *__first == '$')
3081                {
3082                    __push_r_anchor();
3083                    ++__first;
3084                }
3085            }
3086        }
3087        if (__first != __last)
3088            __throw_regex_error<regex_constants::__re_err_empty>();
3089    }
3090    return __first;
3091}
3092
3093template <class _CharT, class _Traits>
3094template <class _ForwardIterator>
3095_ForwardIterator
3096basic_regex<_CharT, _Traits>::__parse_extended_reg_exp(_ForwardIterator __first,
3097                                                       _ForwardIterator __last)
3098{
3099    __owns_one_state<_CharT>* __sa = __end_;
3100    _ForwardIterator __temp = __parse_ERE_branch(__first, __last);
3101    if (__temp == __first)
3102        __throw_regex_error<regex_constants::__re_err_empty>();
3103    __first = __temp;
3104    while (__first != __last && *__first == '|')
3105    {
3106        __owns_one_state<_CharT>* __sb = __end_;
3107        __temp = __parse_ERE_branch(++__first, __last);
3108        if (__temp == __first)
3109            __throw_regex_error<regex_constants::__re_err_empty>();
3110        __push_alternation(__sa, __sb);
3111        __first = __temp;
3112    }
3113    return __first;
3114}
3115
3116template <class _CharT, class _Traits>
3117template <class _ForwardIterator>
3118_ForwardIterator
3119basic_regex<_CharT, _Traits>::__parse_ERE_branch(_ForwardIterator __first,
3120                                                 _ForwardIterator __last)
3121{
3122    _ForwardIterator __temp = __parse_ERE_expression(__first, __last);
3123    if (__temp == __first)
3124        __throw_regex_error<regex_constants::__re_err_empty>();
3125    do
3126    {
3127        __first = __temp;
3128        __temp = __parse_ERE_expression(__first, __last);
3129    } while (__temp != __first);
3130    return __first;
3131}
3132
3133template <class _CharT, class _Traits>
3134template <class _ForwardIterator>
3135_ForwardIterator
3136basic_regex<_CharT, _Traits>::__parse_ERE_expression(_ForwardIterator __first,
3137                                                     _ForwardIterator __last)
3138{
3139    __owns_one_state<_CharT>* __e = __end_;
3140    unsigned __mexp_begin = __marked_count_;
3141    _ForwardIterator __temp = __parse_one_char_or_coll_elem_ERE(__first, __last);
3142    if (__temp == __first && __temp != __last)
3143    {
3144        switch (*__temp)
3145        {
3146        case '^':
3147            __push_l_anchor();
3148            ++__temp;
3149            break;
3150        case '$':
3151            __push_r_anchor();
3152            ++__temp;
3153            break;
3154        case '(':
3155            __push_begin_marked_subexpression();
3156            unsigned __temp_count = __marked_count_;
3157            ++__open_count_;
3158            __temp = __parse_extended_reg_exp(++__temp, __last);
3159            if (__temp == __last || *__temp != ')')
3160                __throw_regex_error<regex_constants::error_paren>();
3161            __push_end_marked_subexpression(__temp_count);
3162            --__open_count_;
3163            ++__temp;
3164            break;
3165        }
3166    }
3167    if (__temp != __first)
3168        __temp = __parse_ERE_dupl_symbol(__temp, __last, __e, __mexp_begin+1,
3169                                         __marked_count_+1);
3170    __first = __temp;
3171    return __first;
3172}
3173
3174template <class _CharT, class _Traits>
3175template <class _ForwardIterator>
3176_ForwardIterator
3177basic_regex<_CharT, _Traits>::__parse_RE_expression(_ForwardIterator __first,
3178                                                    _ForwardIterator __last)
3179{
3180    while (true)
3181    {
3182        _ForwardIterator __temp = __parse_simple_RE(__first, __last);
3183        if (__temp == __first)
3184            break;
3185        __first = __temp;
3186    }
3187    return __first;
3188}
3189
3190template <class _CharT, class _Traits>
3191template <class _ForwardIterator>
3192_ForwardIterator
3193basic_regex<_CharT, _Traits>::__parse_simple_RE(_ForwardIterator __first,
3194                                                _ForwardIterator __last)
3195{
3196    if (__first != __last)
3197    {
3198        __owns_one_state<_CharT>* __e = __end_;
3199        unsigned __mexp_begin = __marked_count_;
3200        _ForwardIterator __temp = __parse_nondupl_RE(__first, __last);
3201        if (__temp != __first)
3202            __first = __parse_RE_dupl_symbol(__temp, __last, __e,
3203                                             __mexp_begin+1, __marked_count_+1);
3204    }
3205    return __first;
3206}
3207
3208template <class _CharT, class _Traits>
3209template <class _ForwardIterator>
3210_ForwardIterator
3211basic_regex<_CharT, _Traits>::__parse_nondupl_RE(_ForwardIterator __first,
3212                                                 _ForwardIterator __last)
3213{
3214    _ForwardIterator __temp = __first;
3215    __first = __parse_one_char_or_coll_elem_RE(__first, __last);
3216    if (__temp == __first)
3217    {
3218        __temp = __parse_Back_open_paren(__first, __last);
3219        if (__temp != __first)
3220        {
3221            __push_begin_marked_subexpression();
3222            unsigned __temp_count = __marked_count_;
3223            __first = __parse_RE_expression(__temp, __last);
3224            __temp = __parse_Back_close_paren(__first, __last);
3225            if (__temp == __first)
3226                __throw_regex_error<regex_constants::error_paren>();
3227            __push_end_marked_subexpression(__temp_count);
3228            __first = __temp;
3229        }
3230        else
3231            __first = __parse_BACKREF(__first, __last);
3232    }
3233    return __first;
3234}
3235
3236template <class _CharT, class _Traits>
3237template <class _ForwardIterator>
3238_ForwardIterator
3239basic_regex<_CharT, _Traits>::__parse_one_char_or_coll_elem_RE(
3240                                                       _ForwardIterator __first,
3241                                                       _ForwardIterator __last)
3242{
3243    _ForwardIterator __temp = __parse_ORD_CHAR(__first, __last);
3244    if (__temp == __first)
3245    {
3246        __temp = __parse_QUOTED_CHAR(__first, __last);
3247        if (__temp == __first)
3248        {
3249            if (__temp != __last && *__temp == '.')
3250            {
3251                __push_match_any();
3252                ++__temp;
3253            }
3254            else
3255                __temp = __parse_bracket_expression(__first, __last);
3256        }
3257    }
3258    __first = __temp;
3259    return __first;
3260}
3261
3262template <class _CharT, class _Traits>
3263template <class _ForwardIterator>
3264_ForwardIterator
3265basic_regex<_CharT, _Traits>::__parse_one_char_or_coll_elem_ERE(
3266                                                       _ForwardIterator __first,
3267                                                       _ForwardIterator __last)
3268{
3269    _ForwardIterator __temp = __parse_ORD_CHAR_ERE(__first, __last);
3270    if (__temp == __first)
3271    {
3272        __temp = __parse_QUOTED_CHAR_ERE(__first, __last);
3273        if (__temp == __first)
3274        {
3275            if (__temp != __last && *__temp == '.')
3276            {
3277                __push_match_any();
3278                ++__temp;
3279            }
3280            else
3281                __temp = __parse_bracket_expression(__first, __last);
3282        }
3283    }
3284    __first = __temp;
3285    return __first;
3286}
3287
3288template <class _CharT, class _Traits>
3289template <class _ForwardIterator>
3290_ForwardIterator
3291basic_regex<_CharT, _Traits>::__parse_Back_open_paren(_ForwardIterator __first,
3292                                                      _ForwardIterator __last)
3293{
3294    if (__first != __last)
3295    {
3296        _ForwardIterator __temp = _VSTD::next(__first);
3297        if (__temp != __last)
3298        {
3299            if (*__first == '\\' && *__temp == '(')
3300                __first = ++__temp;
3301        }
3302    }
3303    return __first;
3304}
3305
3306template <class _CharT, class _Traits>
3307template <class _ForwardIterator>
3308_ForwardIterator
3309basic_regex<_CharT, _Traits>::__parse_Back_close_paren(_ForwardIterator __first,
3310                                                       _ForwardIterator __last)
3311{
3312    if (__first != __last)
3313    {
3314        _ForwardIterator __temp = _VSTD::next(__first);
3315        if (__temp != __last)
3316        {
3317            if (*__first == '\\' && *__temp == ')')
3318                __first = ++__temp;
3319        }
3320    }
3321    return __first;
3322}
3323
3324template <class _CharT, class _Traits>
3325template <class _ForwardIterator>
3326_ForwardIterator
3327basic_regex<_CharT, _Traits>::__parse_Back_open_brace(_ForwardIterator __first,
3328                                                      _ForwardIterator __last)
3329{
3330    if (__first != __last)
3331    {
3332        _ForwardIterator __temp = _VSTD::next(__first);
3333        if (__temp != __last)
3334        {
3335            if (*__first == '\\' && *__temp == '{')
3336                __first = ++__temp;
3337        }
3338    }
3339    return __first;
3340}
3341
3342template <class _CharT, class _Traits>
3343template <class _ForwardIterator>
3344_ForwardIterator
3345basic_regex<_CharT, _Traits>::__parse_Back_close_brace(_ForwardIterator __first,
3346                                                       _ForwardIterator __last)
3347{
3348    if (__first != __last)
3349    {
3350        _ForwardIterator __temp = _VSTD::next(__first);
3351        if (__temp != __last)
3352        {
3353            if (*__first == '\\' && *__temp == '}')
3354                __first = ++__temp;
3355        }
3356    }
3357    return __first;
3358}
3359
3360template <class _CharT, class _Traits>
3361template <class _ForwardIterator>
3362_ForwardIterator
3363basic_regex<_CharT, _Traits>::__parse_BACKREF(_ForwardIterator __first,
3364                                              _ForwardIterator __last)
3365{
3366    if (__first != __last)
3367    {
3368        _ForwardIterator __temp = _VSTD::next(__first);
3369        if (__temp != __last)
3370        {
3371            if (*__first == '\\')
3372            {
3373                int __val = __traits_.value(*__temp, 10);
3374                if (__val >= 1 && __val <= 9)
3375                {
3376                    __push_back_ref(__val);
3377                    __first = ++__temp;
3378                }
3379            }
3380        }
3381    }
3382    return __first;
3383}
3384
3385template <class _CharT, class _Traits>
3386template <class _ForwardIterator>
3387_ForwardIterator
3388basic_regex<_CharT, _Traits>::__parse_ORD_CHAR(_ForwardIterator __first,
3389                                               _ForwardIterator __last)
3390{
3391    if (__first != __last)
3392    {
3393        _ForwardIterator __temp = _VSTD::next(__first);
3394        if (__temp == __last && *__first == '$')
3395            return __first;
3396        // Not called inside a bracket
3397        if (*__first == '.' || *__first == '\\' || *__first == '[')
3398            return __first;
3399        __push_char(*__first);
3400        ++__first;
3401    }
3402    return __first;
3403}
3404
3405template <class _CharT, class _Traits>
3406template <class _ForwardIterator>
3407_ForwardIterator
3408basic_regex<_CharT, _Traits>::__parse_ORD_CHAR_ERE(_ForwardIterator __first,
3409                                                   _ForwardIterator __last)
3410{
3411    if (__first != __last)
3412    {
3413        switch (*__first)
3414        {
3415        case '^':
3416        case '.':
3417        case '[':
3418        case '$':
3419        case '(':
3420        case '|':
3421        case '*':
3422        case '+':
3423        case '?':
3424        case '{':
3425        case '\\':
3426            break;
3427        case ')':
3428            if (__open_count_ == 0)
3429            {
3430                __push_char(*__first);
3431                ++__first;
3432            }
3433            break;
3434        default:
3435            __push_char(*__first);
3436            ++__first;
3437            break;
3438        }
3439    }
3440    return __first;
3441}
3442
3443template <class _CharT, class _Traits>
3444template <class _ForwardIterator>
3445_ForwardIterator
3446basic_regex<_CharT, _Traits>::__parse_QUOTED_CHAR(_ForwardIterator __first,
3447                                                  _ForwardIterator __last)
3448{
3449    if (__first != __last)
3450    {
3451        _ForwardIterator __temp = _VSTD::next(__first);
3452        if (__temp != __last)
3453        {
3454            if (*__first == '\\')
3455            {
3456                switch (*__temp)
3457                {
3458                case '^':
3459                case '.':
3460                case '*':
3461                case '[':
3462                case '$':
3463                case '\\':
3464                    __push_char(*__temp);
3465                    __first = ++__temp;
3466                    break;
3467                }
3468            }
3469        }
3470    }
3471    return __first;
3472}
3473
3474template <class _CharT, class _Traits>
3475template <class _ForwardIterator>
3476_ForwardIterator
3477basic_regex<_CharT, _Traits>::__parse_QUOTED_CHAR_ERE(_ForwardIterator __first,
3478                                                      _ForwardIterator __last)
3479{
3480    if (__first != __last)
3481    {
3482        _ForwardIterator __temp = _VSTD::next(__first);
3483        if (__temp != __last)
3484        {
3485            if (*__first == '\\')
3486            {
3487                switch (*__temp)
3488                {
3489                case '^':
3490                case '.':
3491                case '*':
3492                case '[':
3493                case '$':
3494                case '\\':
3495                case '(':
3496                case ')':
3497                case '|':
3498                case '+':
3499                case '?':
3500                case '{':
3501                case '}':
3502                    __push_char(*__temp);
3503                    __first = ++__temp;
3504                    break;
3505                default:
3506                    if ((__flags_ & 0x1F0) == awk)
3507                        __first = __parse_awk_escape(++__first, __last);
3508                    break;
3509                }
3510            }
3511        }
3512    }
3513    return __first;
3514}
3515
3516template <class _CharT, class _Traits>
3517template <class _ForwardIterator>
3518_ForwardIterator
3519basic_regex<_CharT, _Traits>::__parse_RE_dupl_symbol(_ForwardIterator __first,
3520                                                     _ForwardIterator __last,
3521                                                     __owns_one_state<_CharT>* __s,
3522                                                     unsigned __mexp_begin,
3523                                                     unsigned __mexp_end)
3524{
3525    if (__first != __last)
3526    {
3527        if (*__first == '*')
3528        {
3529            __push_greedy_inf_repeat(0, __s, __mexp_begin, __mexp_end);
3530            ++__first;
3531        }
3532        else
3533        {
3534            _ForwardIterator __temp = __parse_Back_open_brace(__first, __last);
3535            if (__temp != __first)
3536            {
3537                int __min = 0;
3538                __first = __temp;
3539                __temp = __parse_DUP_COUNT(__first, __last, __min);
3540                if (__temp == __first)
3541                    __throw_regex_error<regex_constants::error_badbrace>();
3542                __first = __temp;
3543                if (__first == __last)
3544                    __throw_regex_error<regex_constants::error_brace>();
3545                if (*__first != ',')
3546                {
3547                    __temp = __parse_Back_close_brace(__first, __last);
3548                    if (__temp == __first)
3549                        __throw_regex_error<regex_constants::error_brace>();
3550                    __push_loop(__min, __min, __s, __mexp_begin, __mexp_end,
3551                                    true);
3552                    __first = __temp;
3553                }
3554                else
3555                {
3556                    ++__first;  // consume ','
3557                    int __max = -1;
3558                    __first = __parse_DUP_COUNT(__first, __last, __max);
3559                    __temp = __parse_Back_close_brace(__first, __last);
3560                    if (__temp == __first)
3561                        __throw_regex_error<regex_constants::error_brace>();
3562                    if (__max == -1)
3563                        __push_greedy_inf_repeat(__min, __s, __mexp_begin, __mexp_end);
3564                    else
3565                    {
3566                        if (__max < __min)
3567                            __throw_regex_error<regex_constants::error_badbrace>();
3568                        __push_loop(__min, __max, __s, __mexp_begin, __mexp_end,
3569                                    true);
3570                    }
3571                    __first = __temp;
3572                }
3573            }
3574        }
3575    }
3576    return __first;
3577}
3578
3579template <class _CharT, class _Traits>
3580template <class _ForwardIterator>
3581_ForwardIterator
3582basic_regex<_CharT, _Traits>::__parse_ERE_dupl_symbol(_ForwardIterator __first,
3583                                                      _ForwardIterator __last,
3584                                                      __owns_one_state<_CharT>* __s,
3585                                                      unsigned __mexp_begin,
3586                                                      unsigned __mexp_end)
3587{
3588    if (__first != __last)
3589    {
3590        unsigned __grammar = __flags_ & 0x1F0;
3591        switch (*__first)
3592        {
3593        case '*':
3594            ++__first;
3595            if (__grammar == ECMAScript && __first != __last && *__first == '?')
3596            {
3597                ++__first;
3598                __push_nongreedy_inf_repeat(0, __s, __mexp_begin, __mexp_end);
3599            }
3600            else
3601                __push_greedy_inf_repeat(0, __s, __mexp_begin, __mexp_end);
3602            break;
3603        case '+':
3604            ++__first;
3605            if (__grammar == ECMAScript && __first != __last && *__first == '?')
3606            {
3607                ++__first;
3608                __push_nongreedy_inf_repeat(1, __s, __mexp_begin, __mexp_end);
3609            }
3610            else
3611                __push_greedy_inf_repeat(1, __s, __mexp_begin, __mexp_end);
3612            break;
3613        case '?':
3614            ++__first;
3615            if (__grammar == ECMAScript && __first != __last && *__first == '?')
3616            {
3617                ++__first;
3618                __push_loop(0, 1, __s, __mexp_begin, __mexp_end, false);
3619            }
3620            else
3621                __push_loop(0, 1, __s, __mexp_begin, __mexp_end);
3622            break;
3623        case '{':
3624            {
3625                int __min;
3626                _ForwardIterator __temp = __parse_DUP_COUNT(++__first, __last, __min);
3627                if (__temp == __first)
3628                    __throw_regex_error<regex_constants::error_badbrace>();
3629                __first = __temp;
3630                if (__first == __last)
3631                    __throw_regex_error<regex_constants::error_brace>();
3632                switch (*__first)
3633                {
3634                case '}':
3635                    ++__first;
3636                    if (__grammar == ECMAScript && __first != __last && *__first == '?')
3637                    {
3638                        ++__first;
3639                        __push_loop(__min, __min, __s, __mexp_begin, __mexp_end, false);
3640                    }
3641                    else
3642                        __push_loop(__min, __min, __s, __mexp_begin, __mexp_end);
3643                    break;
3644                case ',':
3645                    ++__first;
3646                    if (__first == __last)
3647                        __throw_regex_error<regex_constants::error_badbrace>();
3648                    if (*__first == '}')
3649                    {
3650                        ++__first;
3651                        if (__grammar == ECMAScript && __first != __last && *__first == '?')
3652                        {
3653                            ++__first;
3654                            __push_nongreedy_inf_repeat(__min, __s, __mexp_begin, __mexp_end);
3655                        }
3656                        else
3657                            __push_greedy_inf_repeat(__min, __s, __mexp_begin, __mexp_end);
3658                    }
3659                    else
3660                    {
3661                        int __max = -1;
3662                        __temp = __parse_DUP_COUNT(__first, __last, __max);
3663                        if (__temp == __first)
3664                            __throw_regex_error<regex_constants::error_brace>();
3665                        __first = __temp;
3666                        if (__first == __last || *__first != '}')
3667                            __throw_regex_error<regex_constants::error_brace>();
3668                        ++__first;
3669                        if (__max < __min)
3670                            __throw_regex_error<regex_constants::error_badbrace>();
3671                        if (__grammar == ECMAScript && __first != __last && *__first == '?')
3672                        {
3673                            ++__first;
3674                            __push_loop(__min, __max, __s, __mexp_begin, __mexp_end, false);
3675                        }
3676                        else
3677                            __push_loop(__min, __max, __s, __mexp_begin, __mexp_end);
3678                    }
3679                    break;
3680                default:
3681                    __throw_regex_error<regex_constants::error_badbrace>();
3682                }
3683            }
3684            break;
3685        }
3686    }
3687    return __first;
3688}
3689
3690template <class _CharT, class _Traits>
3691template <class _ForwardIterator>
3692_ForwardIterator
3693basic_regex<_CharT, _Traits>::__parse_bracket_expression(_ForwardIterator __first,
3694                                                         _ForwardIterator __last)
3695{
3696    if (__first != __last && *__first == '[')
3697    {
3698        ++__first;
3699        if (__first == __last)
3700            __throw_regex_error<regex_constants::error_brack>();
3701        bool __negate = false;
3702        if (*__first == '^')
3703        {
3704            ++__first;
3705            __negate = true;
3706        }
3707        __bracket_expression<_CharT, _Traits>* __ml = __start_matching_list(__negate);
3708        // __ml owned by *this
3709        if (__first == __last)
3710            __throw_regex_error<regex_constants::error_brack>();
3711        if ((__flags_ & 0x1F0) != ECMAScript && *__first == ']')
3712        {
3713            __ml->__add_char(']');
3714            ++__first;
3715        }
3716        __first = __parse_follow_list(__first, __last, __ml);
3717        if (__first == __last)
3718            __throw_regex_error<regex_constants::error_brack>();
3719        if (*__first == '-')
3720        {
3721            __ml->__add_char('-');
3722            ++__first;
3723        }
3724        if (__first == __last || *__first != ']')
3725            __throw_regex_error<regex_constants::error_brack>();
3726        ++__first;
3727    }
3728    return __first;
3729}
3730
3731template <class _CharT, class _Traits>
3732template <class _ForwardIterator>
3733_ForwardIterator
3734basic_regex<_CharT, _Traits>::__parse_follow_list(_ForwardIterator __first,
3735                                    _ForwardIterator __last,
3736                                    __bracket_expression<_CharT, _Traits>* __ml)
3737{
3738    if (__first != __last)
3739    {
3740        while (true)
3741        {
3742            _ForwardIterator __temp = __parse_expression_term(__first, __last,
3743                                                              __ml);
3744            if (__temp == __first)
3745                break;
3746            __first = __temp;
3747        }
3748    }
3749    return __first;
3750}
3751
3752template <class _CharT, class _Traits>
3753template <class _ForwardIterator>
3754_ForwardIterator
3755basic_regex<_CharT, _Traits>::__parse_expression_term(_ForwardIterator __first,
3756                                    _ForwardIterator __last,
3757                                    __bracket_expression<_CharT, _Traits>* __ml)
3758{
3759    if (__first != __last && *__first != ']')
3760    {
3761        _ForwardIterator __temp = _VSTD::next(__first);
3762        basic_string<_CharT> __start_range;
3763        if (__temp != __last && *__first == '[')
3764        {
3765            if (*__temp == '=')
3766                return __parse_equivalence_class(++__temp, __last, __ml);
3767            else if (*__temp == ':')
3768                return __parse_character_class(++__temp, __last, __ml);
3769            else if (*__temp == '.')
3770                __first = __parse_collating_symbol(++__temp, __last, __start_range);
3771        }
3772        unsigned __grammar = __flags_ & 0x1F0;
3773        if (__start_range.empty())
3774        {
3775            if ((__grammar == ECMAScript || __grammar == awk) && *__first == '\\')
3776            {
3777                if (__grammar == ECMAScript)
3778                    __first = __parse_class_escape(++__first, __last, __start_range, __ml);
3779                else
3780                    __first = __parse_awk_escape(++__first, __last, &__start_range);
3781            }
3782            else
3783            {
3784                __start_range = *__first;
3785                ++__first;
3786            }
3787        }
3788        if (__first != __last && *__first != ']')
3789        {
3790            __temp = _VSTD::next(__first);
3791            if (__temp != __last && *__first == '-' && *__temp != ']')
3792            {
3793                // parse a range
3794                basic_string<_CharT> __end_range;
3795                __first = __temp;
3796                ++__temp;
3797                if (__temp != __last && *__first == '[' && *__temp == '.')
3798                    __first = __parse_collating_symbol(++__temp, __last, __end_range);
3799                else
3800                {
3801                    if ((__grammar == ECMAScript || __grammar == awk) && *__first == '\\')
3802                    {
3803                        if (__grammar == ECMAScript)
3804                            __first = __parse_class_escape(++__first, __last,
3805                                                           __end_range, __ml);
3806                        else
3807                            __first = __parse_awk_escape(++__first, __last,
3808                                                         &__end_range);
3809                    }
3810                    else
3811                    {
3812                        __end_range = *__first;
3813                        ++__first;
3814                    }
3815                }
3816                __ml->__add_range(_VSTD::move(__start_range), _VSTD::move(__end_range));
3817            }
3818            else if (!__start_range.empty())
3819            {
3820                if (__start_range.size() == 1)
3821                    __ml->__add_char(__start_range[0]);
3822                else
3823                    __ml->__add_digraph(__start_range[0], __start_range[1]);
3824            }
3825        }
3826        else if (!__start_range.empty())
3827        {
3828            if (__start_range.size() == 1)
3829                __ml->__add_char(__start_range[0]);
3830            else
3831                __ml->__add_digraph(__start_range[0], __start_range[1]);
3832        }
3833    }
3834    return __first;
3835}
3836
3837template <class _CharT, class _Traits>
3838template <class _ForwardIterator>
3839_ForwardIterator
3840basic_regex<_CharT, _Traits>::__parse_class_escape(_ForwardIterator __first,
3841                          _ForwardIterator __last,
3842                          basic_string<_CharT>& __str,
3843                          __bracket_expression<_CharT, _Traits>* __ml)
3844{
3845    if (__first == __last)
3846        __throw_regex_error<regex_constants::error_escape>();
3847    switch (*__first)
3848    {
3849    case 0:
3850        __str = *__first;
3851        return ++__first;
3852    case 'b':
3853        __str = _CharT(8);
3854        return ++__first;
3855    case 'd':
3856        __ml->__add_class(ctype_base::digit);
3857        return ++__first;
3858    case 'D':
3859        __ml->__add_neg_class(ctype_base::digit);
3860        return ++__first;
3861    case 's':
3862        __ml->__add_class(ctype_base::space);
3863        return ++__first;
3864    case 'S':
3865        __ml->__add_neg_class(ctype_base::space);
3866        return ++__first;
3867    case 'w':
3868        __ml->__add_class(ctype_base::alnum);
3869        __ml->__add_char('_');
3870        return ++__first;
3871    case 'W':
3872        __ml->__add_neg_class(ctype_base::alnum);
3873        __ml->__add_neg_char('_');
3874        return ++__first;
3875    }
3876    __first = __parse_character_escape(__first, __last, &__str);
3877    return __first;
3878}
3879
3880template <class _CharT, class _Traits>
3881template <class _ForwardIterator>
3882_ForwardIterator
3883basic_regex<_CharT, _Traits>::__parse_awk_escape(_ForwardIterator __first,
3884                          _ForwardIterator __last,
3885                          basic_string<_CharT>* __str)
3886{
3887    if (__first == __last)
3888        __throw_regex_error<regex_constants::error_escape>();
3889    switch (*__first)
3890    {
3891    case '\\':
3892    case '"':
3893    case '/':
3894        if (__str)
3895            *__str = *__first;
3896        else
3897            __push_char(*__first);
3898        return ++__first;
3899    case 'a':
3900        if (__str)
3901            *__str = _CharT(7);
3902        else
3903            __push_char(_CharT(7));
3904        return ++__first;
3905    case 'b':
3906        if (__str)
3907            *__str = _CharT(8);
3908        else
3909            __push_char(_CharT(8));
3910        return ++__first;
3911    case 'f':
3912        if (__str)
3913            *__str = _CharT(0xC);
3914        else
3915            __push_char(_CharT(0xC));
3916        return ++__first;
3917    case 'n':
3918        if (__str)
3919            *__str = _CharT(0xA);
3920        else
3921            __push_char(_CharT(0xA));
3922        return ++__first;
3923    case 'r':
3924        if (__str)
3925            *__str = _CharT(0xD);
3926        else
3927            __push_char(_CharT(0xD));
3928        return ++__first;
3929    case 't':
3930        if (__str)
3931            *__str = _CharT(0x9);
3932        else
3933            __push_char(_CharT(0x9));
3934        return ++__first;
3935    case 'v':
3936        if (__str)
3937            *__str = _CharT(0xB);
3938        else
3939            __push_char(_CharT(0xB));
3940        return ++__first;
3941    }
3942    if ('0' <= *__first && *__first <= '7')
3943    {
3944        unsigned __val = *__first - '0';
3945        if (++__first != __last && ('0' <= *__first && *__first <= '7'))
3946        {
3947            __val = 8 * __val + *__first - '0';
3948            if (++__first != __last && ('0' <= *__first && *__first <= '7'))
3949                __val = 8 * __val + *__first++ - '0';
3950        }
3951        if (__str)
3952            *__str = _CharT(__val);
3953        else
3954            __push_char(_CharT(__val));
3955    }
3956    else
3957        __throw_regex_error<regex_constants::error_escape>();
3958    return __first;
3959}
3960
3961template <class _CharT, class _Traits>
3962template <class _ForwardIterator>
3963_ForwardIterator
3964basic_regex<_CharT, _Traits>::__parse_equivalence_class(_ForwardIterator __first,
3965                                    _ForwardIterator __last,
3966                                    __bracket_expression<_CharT, _Traits>* __ml)
3967{
3968    // Found [=
3969    //   This means =] must exist
3970    value_type _Equal_close[2] = {'=', ']'};
3971    _ForwardIterator __temp = _VSTD::search(__first, __last, _Equal_close,
3972                                                            _Equal_close+2);
3973    if (__temp == __last)
3974        __throw_regex_error<regex_constants::error_brack>();
3975    // [__first, __temp) contains all text in [= ... =]
3976    string_type __collate_name =
3977        __traits_.lookup_collatename(__first, __temp);
3978    if (__collate_name.empty())
3979        __throw_regex_error<regex_constants::error_collate>();
3980    string_type __equiv_name =
3981        __traits_.transform_primary(__collate_name.begin(),
3982                                    __collate_name.end());
3983    if (!__equiv_name.empty())
3984        __ml->__add_equivalence(__equiv_name);
3985    else
3986    {
3987        switch (__collate_name.size())
3988        {
3989        case 1:
3990            __ml->__add_char(__collate_name[0]);
3991            break;
3992        case 2:
3993            __ml->__add_digraph(__collate_name[0], __collate_name[1]);
3994            break;
3995        default:
3996            __throw_regex_error<regex_constants::error_collate>();
3997        }
3998    }
3999    __first = _VSTD::next(__temp, 2);
4000    return __first;
4001}
4002
4003template <class _CharT, class _Traits>
4004template <class _ForwardIterator>
4005_ForwardIterator
4006basic_regex<_CharT, _Traits>::__parse_character_class(_ForwardIterator __first,
4007                                    _ForwardIterator __last,
4008                                    __bracket_expression<_CharT, _Traits>* __ml)
4009{
4010    // Found [:
4011    //   This means :] must exist
4012    value_type _Colon_close[2] = {':', ']'};
4013    _ForwardIterator __temp = _VSTD::search(__first, __last, _Colon_close,
4014                                                            _Colon_close+2);
4015    if (__temp == __last)
4016        __throw_regex_error<regex_constants::error_brack>();
4017    // [__first, __temp) contains all text in [: ... :]
4018    typedef typename _Traits::char_class_type char_class_type;
4019    char_class_type __class_type =
4020        __traits_.lookup_classname(__first, __temp, __flags_ & icase);
4021    if (__class_type == 0)
4022        __throw_regex_error<regex_constants::error_ctype>();
4023    __ml->__add_class(__class_type);
4024    __first = _VSTD::next(__temp, 2);
4025    return __first;
4026}
4027
4028template <class _CharT, class _Traits>
4029template <class _ForwardIterator>
4030_ForwardIterator
4031basic_regex<_CharT, _Traits>::__parse_collating_symbol(_ForwardIterator __first,
4032                                                _ForwardIterator __last,
4033                                                basic_string<_CharT>& __col_sym)
4034{
4035    // Found [.
4036    //   This means .] must exist
4037    value_type _Dot_close[2] = {'.', ']'};
4038    _ForwardIterator __temp = _VSTD::search(__first, __last, _Dot_close,
4039                                                            _Dot_close+2);
4040    if (__temp == __last)
4041        __throw_regex_error<regex_constants::error_brack>();
4042    // [__first, __temp) contains all text in [. ... .]
4043    __col_sym = __traits_.lookup_collatename(__first, __temp);
4044    switch (__col_sym.size())
4045    {
4046    case 1:
4047    case 2:
4048        break;
4049    default:
4050        __throw_regex_error<regex_constants::error_collate>();
4051    }
4052    __first = _VSTD::next(__temp, 2);
4053    return __first;
4054}
4055
4056template <class _CharT, class _Traits>
4057template <class _ForwardIterator>
4058_ForwardIterator
4059basic_regex<_CharT, _Traits>::__parse_DUP_COUNT(_ForwardIterator __first,
4060                                                _ForwardIterator __last,
4061                                                int& __c)
4062{
4063    if (__first != __last )
4064    {
4065        int __val = __traits_.value(*__first, 10);
4066        if ( __val != -1 )
4067        {
4068            __c = __val;
4069            for (++__first;
4070                 __first != __last && ( __val = __traits_.value(*__first, 10)) != -1;
4071                 ++__first)
4072            {
4073                if (__c >= std::numeric_limits<int>::max() / 10)
4074                    __throw_regex_error<regex_constants::error_badbrace>();
4075                __c *= 10;
4076                __c += __val;
4077            }
4078        }
4079    }
4080    return __first;
4081}
4082
4083template <class _CharT, class _Traits>
4084template <class _ForwardIterator>
4085_ForwardIterator
4086basic_regex<_CharT, _Traits>::__parse_ecma_exp(_ForwardIterator __first,
4087                                               _ForwardIterator __last)
4088{
4089    __owns_one_state<_CharT>* __sa = __end_;
4090    _ForwardIterator __temp = __parse_alternative(__first, __last);
4091    if (__temp == __first)
4092        __push_empty();
4093    __first = __temp;
4094    while (__first != __last && *__first == '|')
4095    {
4096        __owns_one_state<_CharT>* __sb = __end_;
4097        __temp = __parse_alternative(++__first, __last);
4098        if (__temp == __first)
4099            __push_empty();
4100        __push_alternation(__sa, __sb);
4101        __first = __temp;
4102    }
4103    return __first;
4104}
4105
4106template <class _CharT, class _Traits>
4107template <class _ForwardIterator>
4108_ForwardIterator
4109basic_regex<_CharT, _Traits>::__parse_alternative(_ForwardIterator __first,
4110                                                  _ForwardIterator __last)
4111{
4112    while (true)
4113    {
4114        _ForwardIterator __temp = __parse_term(__first, __last);
4115        if (__temp == __first)
4116            break;
4117        __first = __temp;
4118    }
4119    return __first;
4120}
4121
4122template <class _CharT, class _Traits>
4123template <class _ForwardIterator>
4124_ForwardIterator
4125basic_regex<_CharT, _Traits>::__parse_term(_ForwardIterator __first,
4126                                           _ForwardIterator __last)
4127{
4128    _ForwardIterator __temp = __parse_assertion(__first, __last);
4129    if (__temp == __first)
4130    {
4131        __owns_one_state<_CharT>* __e = __end_;
4132        unsigned __mexp_begin = __marked_count_;
4133        __temp = __parse_atom(__first, __last);
4134        if (__temp != __first)
4135            __first = __parse_ERE_dupl_symbol(__temp, __last, __e,
4136                                              __mexp_begin+1, __marked_count_+1);
4137    }
4138    else
4139        __first = __temp;
4140    return __first;
4141}
4142
4143template <class _CharT, class _Traits>
4144template <class _ForwardIterator>
4145_ForwardIterator
4146basic_regex<_CharT, _Traits>::__parse_assertion(_ForwardIterator __first,
4147                                                _ForwardIterator __last)
4148{
4149    if (__first != __last)
4150    {
4151        switch (*__first)
4152        {
4153        case '^':
4154            __push_l_anchor();
4155            ++__first;
4156            break;
4157        case '$':
4158            __push_r_anchor();
4159            ++__first;
4160            break;
4161        case '\\':
4162            {
4163                _ForwardIterator __temp = _VSTD::next(__first);
4164                if (__temp != __last)
4165                {
4166                    if (*__temp == 'b')
4167                    {
4168                        __push_word_boundary(false);
4169                        __first = ++__temp;
4170                    }
4171                    else if (*__temp == 'B')
4172                    {
4173                        __push_word_boundary(true);
4174                        __first = ++__temp;
4175                    }
4176                }
4177            }
4178            break;
4179        case '(':
4180            {
4181                _ForwardIterator __temp = _VSTD::next(__first);
4182                if (__temp != __last && *__temp == '?')
4183                {
4184                    if (++__temp != __last)
4185                    {
4186                        switch (*__temp)
4187                        {
4188                        case '=':
4189                            {
4190                                basic_regex __exp;
4191                                __exp.__flags_ = __flags_;
4192                                __temp = __exp.__parse(++__temp, __last);
4193                                unsigned __mexp = __exp.__marked_count_;
4194                                __push_lookahead(_VSTD::move(__exp), false, __marked_count_);
4195                                __marked_count_ += __mexp;
4196                                if (__temp == __last || *__temp != ')')
4197                                    __throw_regex_error<regex_constants::error_paren>();
4198                                __first = ++__temp;
4199                            }
4200                            break;
4201                        case '!':
4202                            {
4203                                basic_regex __exp;
4204                                __exp.__flags_ = __flags_;
4205                                __temp = __exp.__parse(++__temp, __last);
4206                                unsigned __mexp = __exp.__marked_count_;
4207                                __push_lookahead(_VSTD::move(__exp), true, __marked_count_);
4208                                __marked_count_ += __mexp;
4209                                if (__temp == __last || *__temp != ')')
4210                                    __throw_regex_error<regex_constants::error_paren>();
4211                                __first = ++__temp;
4212                            }
4213                            break;
4214                        }
4215                    }
4216                }
4217            }
4218            break;
4219        }
4220    }
4221    return __first;
4222}
4223
4224template <class _CharT, class _Traits>
4225template <class _ForwardIterator>
4226_ForwardIterator
4227basic_regex<_CharT, _Traits>::__parse_atom(_ForwardIterator __first,
4228                                           _ForwardIterator __last)
4229{
4230    if (__first != __last)
4231    {
4232        switch (*__first)
4233        {
4234        case '.':
4235            __push_match_any_but_newline();
4236            ++__first;
4237            break;
4238        case '\\':
4239            __first = __parse_atom_escape(__first, __last);
4240            break;
4241        case '[':
4242            __first = __parse_bracket_expression(__first, __last);
4243            break;
4244        case '(':
4245            {
4246                ++__first;
4247                if (__first == __last)
4248                    __throw_regex_error<regex_constants::error_paren>();
4249                _ForwardIterator __temp = _VSTD::next(__first);
4250                if (__temp != __last && *__first == '?' && *__temp == ':')
4251                {
4252                    ++__open_count_;
4253                    __first = __parse_ecma_exp(++__temp, __last);
4254                    if (__first == __last || *__first != ')')
4255                        __throw_regex_error<regex_constants::error_paren>();
4256                    --__open_count_;
4257                    ++__first;
4258                }
4259                else
4260                {
4261                    __push_begin_marked_subexpression();
4262                    unsigned __temp_count = __marked_count_;
4263                    ++__open_count_;
4264                    __first = __parse_ecma_exp(__first, __last);
4265                    if (__first == __last || *__first != ')')
4266                        __throw_regex_error<regex_constants::error_paren>();
4267                    __push_end_marked_subexpression(__temp_count);
4268                    --__open_count_;
4269                    ++__first;
4270                }
4271            }
4272            break;
4273        case '*':
4274        case '+':
4275        case '?':
4276        case '{':
4277            __throw_regex_error<regex_constants::error_badrepeat>();
4278            break;
4279        default:
4280            __first = __parse_pattern_character(__first, __last);
4281            break;
4282        }
4283    }
4284    return __first;
4285}
4286
4287template <class _CharT, class _Traits>
4288template <class _ForwardIterator>
4289_ForwardIterator
4290basic_regex<_CharT, _Traits>::__parse_atom_escape(_ForwardIterator __first,
4291                                                  _ForwardIterator __last)
4292{
4293    if (__first != __last && *__first == '\\')
4294    {
4295        _ForwardIterator __t1 = _VSTD::next(__first);
4296        if (__t1 == __last)
4297            __throw_regex_error<regex_constants::error_escape>();
4298
4299        _ForwardIterator __t2 = __parse_decimal_escape(__t1, __last);
4300        if (__t2 != __t1)
4301            __first = __t2;
4302        else
4303        {
4304            __t2 = __parse_character_class_escape(__t1, __last);
4305            if (__t2 != __t1)
4306                __first = __t2;
4307            else
4308            {
4309                __t2 = __parse_character_escape(__t1, __last);
4310                if (__t2 != __t1)
4311                    __first = __t2;
4312            }
4313        }
4314    }
4315    return __first;
4316}
4317
4318template <class _CharT, class _Traits>
4319template <class _ForwardIterator>
4320_ForwardIterator
4321basic_regex<_CharT, _Traits>::__parse_decimal_escape(_ForwardIterator __first,
4322                                                     _ForwardIterator __last)
4323{
4324    if (__first != __last)
4325    {
4326        if (*__first == '0')
4327        {
4328            __push_char(_CharT());
4329            ++__first;
4330        }
4331        else if ('1' <= *__first && *__first <= '9')
4332        {
4333            unsigned __v = *__first - '0';
4334            for (++__first;
4335                    __first != __last && '0' <= *__first && *__first <= '9'; ++__first)
4336                {
4337                if (__v >= std::numeric_limits<unsigned>::max() / 10)
4338                    __throw_regex_error<regex_constants::error_backref>();
4339                __v = 10 * __v + *__first - '0';
4340                }
4341            if (__v == 0 || __v > mark_count())
4342                __throw_regex_error<regex_constants::error_backref>();
4343            __push_back_ref(__v);
4344        }
4345    }
4346    return __first;
4347}
4348
4349template <class _CharT, class _Traits>
4350template <class _ForwardIterator>
4351_ForwardIterator
4352basic_regex<_CharT, _Traits>::__parse_character_class_escape(_ForwardIterator __first,
4353                                                             _ForwardIterator __last)
4354{
4355    if (__first != __last)
4356    {
4357        __bracket_expression<_CharT, _Traits>* __ml;
4358        switch (*__first)
4359        {
4360        case 'd':
4361            __ml = __start_matching_list(false);
4362            __ml->__add_class(ctype_base::digit);
4363            ++__first;
4364            break;
4365        case 'D':
4366            __ml = __start_matching_list(true);
4367            __ml->__add_class(ctype_base::digit);
4368            ++__first;
4369            break;
4370        case 's':
4371            __ml = __start_matching_list(false);
4372            __ml->__add_class(ctype_base::space);
4373            ++__first;
4374            break;
4375        case 'S':
4376            __ml = __start_matching_list(true);
4377            __ml->__add_class(ctype_base::space);
4378            ++__first;
4379            break;
4380        case 'w':
4381            __ml = __start_matching_list(false);
4382            __ml->__add_class(ctype_base::alnum);
4383            __ml->__add_char('_');
4384            ++__first;
4385            break;
4386        case 'W':
4387            __ml = __start_matching_list(true);
4388            __ml->__add_class(ctype_base::alnum);
4389            __ml->__add_char('_');
4390            ++__first;
4391            break;
4392        }
4393    }
4394    return __first;
4395}
4396
4397template <class _CharT, class _Traits>
4398template <class _ForwardIterator>
4399_ForwardIterator
4400basic_regex<_CharT, _Traits>::__parse_character_escape(_ForwardIterator __first,
4401                                                    _ForwardIterator __last,
4402                                                    basic_string<_CharT>* __str)
4403{
4404    if (__first != __last)
4405    {
4406        _ForwardIterator __t;
4407        unsigned __sum = 0;
4408        int __hd;
4409        switch (*__first)
4410        {
4411        case 'f':
4412            if (__str)
4413                *__str = _CharT(0xC);
4414            else
4415                __push_char(_CharT(0xC));
4416            ++__first;
4417            break;
4418        case 'n':
4419            if (__str)
4420                *__str = _CharT(0xA);
4421            else
4422                __push_char(_CharT(0xA));
4423            ++__first;
4424            break;
4425        case 'r':
4426            if (__str)
4427                *__str = _CharT(0xD);
4428            else
4429                __push_char(_CharT(0xD));
4430            ++__first;
4431            break;
4432        case 't':
4433            if (__str)
4434                *__str = _CharT(0x9);
4435            else
4436                __push_char(_CharT(0x9));
4437            ++__first;
4438            break;
4439        case 'v':
4440            if (__str)
4441                *__str = _CharT(0xB);
4442            else
4443                __push_char(_CharT(0xB));
4444            ++__first;
4445            break;
4446        case 'c':
4447            if ((__t = _VSTD::next(__first)) != __last)
4448            {
4449                if (('A' <= *__t && *__t <= 'Z') ||
4450                    ('a' <= *__t && *__t <= 'z'))
4451                {
4452                    if (__str)
4453                        *__str = _CharT(*__t % 32);
4454                    else
4455                        __push_char(_CharT(*__t % 32));
4456                    __first = ++__t;
4457                }
4458                else
4459                    __throw_regex_error<regex_constants::error_escape>();
4460            }
4461            else
4462                __throw_regex_error<regex_constants::error_escape>();
4463            break;
4464        case 'u':
4465            ++__first;
4466            if (__first == __last)
4467                __throw_regex_error<regex_constants::error_escape>();
4468            __hd = __traits_.value(*__first, 16);
4469            if (__hd == -1)
4470                __throw_regex_error<regex_constants::error_escape>();
4471            __sum = 16 * __sum + static_cast<unsigned>(__hd);
4472            ++__first;
4473            if (__first == __last)
4474                __throw_regex_error<regex_constants::error_escape>();
4475            __hd = __traits_.value(*__first, 16);
4476            if (__hd == -1)
4477                __throw_regex_error<regex_constants::error_escape>();
4478            __sum = 16 * __sum + static_cast<unsigned>(__hd);
4479            // drop through
4480        case 'x':
4481            ++__first;
4482            if (__first == __last)
4483                __throw_regex_error<regex_constants::error_escape>();
4484            __hd = __traits_.value(*__first, 16);
4485            if (__hd == -1)
4486                __throw_regex_error<regex_constants::error_escape>();
4487            __sum = 16 * __sum + static_cast<unsigned>(__hd);
4488            ++__first;
4489            if (__first == __last)
4490                __throw_regex_error<regex_constants::error_escape>();
4491            __hd = __traits_.value(*__first, 16);
4492            if (__hd == -1)
4493                __throw_regex_error<regex_constants::error_escape>();
4494            __sum = 16 * __sum + static_cast<unsigned>(__hd);
4495            if (__str)
4496                *__str = _CharT(__sum);
4497            else
4498                __push_char(_CharT(__sum));
4499            ++__first;
4500            break;
4501        case '0':
4502            if (__str)
4503                *__str = _CharT(0);
4504            else
4505                __push_char(_CharT(0));
4506            ++__first;
4507            break;
4508        default:
4509            if (*__first != '_' && !__traits_.isctype(*__first, ctype_base::alnum))
4510            {
4511                if (__str)
4512                    *__str = *__first;
4513                else
4514                    __push_char(*__first);
4515                ++__first;
4516            }
4517            else
4518                __throw_regex_error<regex_constants::error_escape>();
4519            break;
4520        }
4521    }
4522    return __first;
4523}
4524
4525template <class _CharT, class _Traits>
4526template <class _ForwardIterator>
4527_ForwardIterator
4528basic_regex<_CharT, _Traits>::__parse_pattern_character(_ForwardIterator __first,
4529                                                        _ForwardIterator __last)
4530{
4531    if (__first != __last)
4532    {
4533        switch (*__first)
4534        {
4535        case '^':
4536        case '$':
4537        case '\\':
4538        case '.':
4539        case '*':
4540        case '+':
4541        case '?':
4542        case '(':
4543        case ')':
4544        case '[':
4545        case ']':
4546        case '{':
4547        case '}':
4548        case '|':
4549            break;
4550        default:
4551            __push_char(*__first);
4552            ++__first;
4553            break;
4554        }
4555    }
4556    return __first;
4557}
4558
4559template <class _CharT, class _Traits>
4560template <class _ForwardIterator>
4561_ForwardIterator
4562basic_regex<_CharT, _Traits>::__parse_grep(_ForwardIterator __first,
4563                                           _ForwardIterator __last)
4564{
4565    __owns_one_state<_CharT>* __sa = __end_;
4566    _ForwardIterator __t1 = _VSTD::find(__first, __last, _CharT('\n'));
4567    if (__t1 != __first)
4568        __parse_basic_reg_exp(__first, __t1);
4569    else
4570        __push_empty();
4571    __first = __t1;
4572    if (__first != __last)
4573        ++__first;
4574    while (__first != __last)
4575    {
4576        __t1 = _VSTD::find(__first, __last, _CharT('\n'));
4577        __owns_one_state<_CharT>* __sb = __end_;
4578        if (__t1 != __first)
4579            __parse_basic_reg_exp(__first, __t1);
4580        else
4581            __push_empty();
4582        __push_alternation(__sa, __sb);
4583        __first = __t1;
4584        if (__first != __last)
4585            ++__first;
4586    }
4587    return __first;
4588}
4589
4590template <class _CharT, class _Traits>
4591template <class _ForwardIterator>
4592_ForwardIterator
4593basic_regex<_CharT, _Traits>::__parse_egrep(_ForwardIterator __first,
4594                                            _ForwardIterator __last)
4595{
4596    __owns_one_state<_CharT>* __sa = __end_;
4597    _ForwardIterator __t1 = _VSTD::find(__first, __last, _CharT('\n'));
4598    if (__t1 != __first)
4599        __parse_extended_reg_exp(__first, __t1);
4600    else
4601        __push_empty();
4602    __first = __t1;
4603    if (__first != __last)
4604        ++__first;
4605    while (__first != __last)
4606    {
4607        __t1 = _VSTD::find(__first, __last, _CharT('\n'));
4608        __owns_one_state<_CharT>* __sb = __end_;
4609        if (__t1 != __first)
4610            __parse_extended_reg_exp(__first, __t1);
4611        else
4612            __push_empty();
4613        __push_alternation(__sa, __sb);
4614        __first = __t1;
4615        if (__first != __last)
4616            ++__first;
4617    }
4618    return __first;
4619}
4620
4621template <class _CharT, class _Traits>
4622void
4623basic_regex<_CharT, _Traits>::__push_loop(size_t __min, size_t __max,
4624        __owns_one_state<_CharT>* __s, size_t __mexp_begin, size_t __mexp_end,
4625        bool __greedy)
4626{
4627    unique_ptr<__empty_state<_CharT> > __e1(new __empty_state<_CharT>(__end_->first()));
4628    __end_->first() = nullptr;
4629    unique_ptr<__loop<_CharT> > __e2(new __loop<_CharT>(__loop_count_,
4630                __s->first(), __e1.get(), __mexp_begin, __mexp_end, __greedy,
4631                __min, __max));
4632    __s->first() = nullptr;
4633    __e1.release();
4634    __end_->first() = new __repeat_one_loop<_CharT>(__e2.get());
4635    __end_ = __e2->second();
4636    __s->first() = __e2.release();
4637    ++__loop_count_;
4638}
4639
4640template <class _CharT, class _Traits>
4641void
4642basic_regex<_CharT, _Traits>::__push_char(value_type __c)
4643{
4644    if (flags() & icase)
4645        __end_->first() = new __match_char_icase<_CharT, _Traits>
4646                                              (__traits_, __c, __end_->first());
4647    else if (flags() & collate)
4648        __end_->first() = new __match_char_collate<_CharT, _Traits>
4649                                              (__traits_, __c, __end_->first());
4650    else
4651        __end_->first() = new __match_char<_CharT>(__c, __end_->first());
4652    __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4653}
4654
4655template <class _CharT, class _Traits>
4656void
4657basic_regex<_CharT, _Traits>::__push_begin_marked_subexpression()
4658{
4659    if (!(__flags_ & nosubs))
4660    {
4661        __end_->first() =
4662                new __begin_marked_subexpression<_CharT>(++__marked_count_,
4663                                                         __end_->first());
4664        __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4665    }
4666}
4667
4668template <class _CharT, class _Traits>
4669void
4670basic_regex<_CharT, _Traits>::__push_end_marked_subexpression(unsigned __sub)
4671{
4672    if (!(__flags_ & nosubs))
4673    {
4674        __end_->first() =
4675                new __end_marked_subexpression<_CharT>(__sub, __end_->first());
4676        __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4677    }
4678}
4679
4680template <class _CharT, class _Traits>
4681void
4682basic_regex<_CharT, _Traits>::__push_l_anchor()
4683{
4684    __end_->first() = new __l_anchor<_CharT>(__end_->first());
4685    __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4686}
4687
4688template <class _CharT, class _Traits>
4689void
4690basic_regex<_CharT, _Traits>::__push_r_anchor()
4691{
4692    __end_->first() = new __r_anchor<_CharT>(__end_->first());
4693    __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4694}
4695
4696template <class _CharT, class _Traits>
4697void
4698basic_regex<_CharT, _Traits>::__push_match_any()
4699{
4700    __end_->first() = new __match_any<_CharT>(__end_->first());
4701    __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4702}
4703
4704template <class _CharT, class _Traits>
4705void
4706basic_regex<_CharT, _Traits>::__push_match_any_but_newline()
4707{
4708    __end_->first() = new __match_any_but_newline<_CharT>(__end_->first());
4709    __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4710}
4711
4712template <class _CharT, class _Traits>
4713void
4714basic_regex<_CharT, _Traits>::__push_empty()
4715{
4716    __end_->first() = new __empty_state<_CharT>(__end_->first());
4717    __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4718}
4719
4720template <class _CharT, class _Traits>
4721void
4722basic_regex<_CharT, _Traits>::__push_word_boundary(bool __invert)
4723{
4724    __end_->first() = new __word_boundary<_CharT, _Traits>(__traits_, __invert,
4725                                                           __end_->first());
4726    __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4727}
4728
4729template <class _CharT, class _Traits>
4730void
4731basic_regex<_CharT, _Traits>::__push_back_ref(int __i)
4732{
4733    if (flags() & icase)
4734        __end_->first() = new __back_ref_icase<_CharT, _Traits>
4735                                              (__traits_, __i, __end_->first());
4736    else if (flags() & collate)
4737        __end_->first() = new __back_ref_collate<_CharT, _Traits>
4738                                              (__traits_, __i, __end_->first());
4739    else
4740        __end_->first() = new __back_ref<_CharT>(__i, __end_->first());
4741    __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4742}
4743
4744template <class _CharT, class _Traits>
4745void
4746basic_regex<_CharT, _Traits>::__push_alternation(__owns_one_state<_CharT>* __sa,
4747                                                 __owns_one_state<_CharT>* __ea)
4748{
4749    __sa->first() = new __alternate<_CharT>(
4750                         static_cast<__owns_one_state<_CharT>*>(__sa->first()),
4751                         static_cast<__owns_one_state<_CharT>*>(__ea->first()));
4752    __ea->first() = nullptr;
4753    __ea->first() = new __empty_state<_CharT>(__end_->first());
4754    __end_->first() = nullptr;
4755    __end_->first() = new __empty_non_own_state<_CharT>(__ea->first());
4756    __end_ = static_cast<__owns_one_state<_CharT>*>(__ea->first());
4757}
4758
4759template <class _CharT, class _Traits>
4760__bracket_expression<_CharT, _Traits>*
4761basic_regex<_CharT, _Traits>::__start_matching_list(bool __negate)
4762{
4763    __bracket_expression<_CharT, _Traits>* __r =
4764        new __bracket_expression<_CharT, _Traits>(__traits_, __end_->first(),
4765                                                  __negate, __flags_ & icase,
4766                                                  __flags_ & collate);
4767    __end_->first() = __r;
4768    __end_ = __r;
4769    return __r;
4770}
4771
4772template <class _CharT, class _Traits>
4773void
4774basic_regex<_CharT, _Traits>::__push_lookahead(const basic_regex& __exp,
4775                                               bool __invert,
4776                                               unsigned __mexp)
4777{
4778    __end_->first() = new __lookahead<_CharT, _Traits>(__exp, __invert,
4779                                                           __end_->first(), __mexp);
4780    __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4781}
4782
4783typedef basic_regex<char>    regex;
4784typedef basic_regex<wchar_t> wregex;
4785
4786// sub_match
4787
4788template <class _BidirectionalIterator>
4789class _LIBCPP_TEMPLATE_VIS sub_match
4790    : public pair<_BidirectionalIterator, _BidirectionalIterator>
4791{
4792public:
4793    typedef _BidirectionalIterator                              iterator;
4794    typedef typename iterator_traits<iterator>::value_type      value_type;
4795    typedef typename iterator_traits<iterator>::difference_type difference_type;
4796    typedef basic_string<value_type>                            string_type;
4797
4798    bool matched;
4799
4800    _LIBCPP_INLINE_VISIBILITY
4801    _LIBCPP_CONSTEXPR sub_match() : matched() {}
4802
4803    _LIBCPP_INLINE_VISIBILITY
4804    difference_type length() const
4805        {return matched ? _VSTD::distance(this->first, this->second) : 0;}
4806    _LIBCPP_INLINE_VISIBILITY
4807    string_type str() const
4808        {return matched ? string_type(this->first, this->second) : string_type();}
4809    _LIBCPP_INLINE_VISIBILITY
4810    operator string_type() const
4811        {return str();}
4812
4813    _LIBCPP_INLINE_VISIBILITY
4814    int compare(const sub_match& __s) const
4815        {return str().compare(__s.str());}
4816    _LIBCPP_INLINE_VISIBILITY
4817    int compare(const string_type& __s) const
4818        {return str().compare(__s);}
4819    _LIBCPP_INLINE_VISIBILITY
4820    int compare(const value_type* __s) const
4821        {return str().compare(__s);}
4822};
4823
4824typedef sub_match<const char*>             csub_match;
4825typedef sub_match<const wchar_t*>          wcsub_match;
4826typedef sub_match<string::const_iterator>  ssub_match;
4827typedef sub_match<wstring::const_iterator> wssub_match;
4828
4829template <class _BiIter>
4830inline _LIBCPP_INLINE_VISIBILITY
4831bool
4832operator==(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
4833{
4834    return __x.compare(__y) == 0;
4835}
4836
4837template <class _BiIter>
4838inline _LIBCPP_INLINE_VISIBILITY
4839bool
4840operator!=(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
4841{
4842    return !(__x == __y);
4843}
4844
4845template <class _BiIter>
4846inline _LIBCPP_INLINE_VISIBILITY
4847bool
4848operator<(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
4849{
4850    return __x.compare(__y) < 0;
4851}
4852
4853template <class _BiIter>
4854inline _LIBCPP_INLINE_VISIBILITY
4855bool
4856operator<=(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
4857{
4858    return !(__y < __x);
4859}
4860
4861template <class _BiIter>
4862inline _LIBCPP_INLINE_VISIBILITY
4863bool
4864operator>=(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
4865{
4866    return !(__x < __y);
4867}
4868
4869template <class _BiIter>
4870inline _LIBCPP_INLINE_VISIBILITY
4871bool
4872operator>(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
4873{
4874    return __y < __x;
4875}
4876
4877template <class _BiIter, class _ST, class _SA>
4878inline _LIBCPP_INLINE_VISIBILITY
4879bool
4880operator==(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4881           const sub_match<_BiIter>& __y)
4882{
4883    return __y.compare(typename sub_match<_BiIter>::string_type(__x.data(), __x.size())) == 0;
4884}
4885
4886template <class _BiIter, class _ST, class _SA>
4887inline _LIBCPP_INLINE_VISIBILITY
4888bool
4889operator!=(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4890           const sub_match<_BiIter>& __y)
4891{
4892    return !(__x == __y);
4893}
4894
4895template <class _BiIter, class _ST, class _SA>
4896inline _LIBCPP_INLINE_VISIBILITY
4897bool
4898operator<(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4899          const sub_match<_BiIter>& __y)
4900{
4901    return __y.compare(typename sub_match<_BiIter>::string_type(__x.data(), __x.size())) > 0;
4902}
4903
4904template <class _BiIter, class _ST, class _SA>
4905inline _LIBCPP_INLINE_VISIBILITY
4906bool
4907operator>(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4908          const sub_match<_BiIter>& __y)
4909{
4910    return __y < __x;
4911}
4912
4913template <class _BiIter, class _ST, class _SA>
4914inline _LIBCPP_INLINE_VISIBILITY
4915bool operator>=(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4916                const sub_match<_BiIter>& __y)
4917{
4918    return !(__x < __y);
4919}
4920
4921template <class _BiIter, class _ST, class _SA>
4922inline _LIBCPP_INLINE_VISIBILITY
4923bool
4924operator<=(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4925           const sub_match<_BiIter>& __y)
4926{
4927    return !(__y < __x);
4928}
4929
4930template <class _BiIter, class _ST, class _SA>
4931inline _LIBCPP_INLINE_VISIBILITY
4932bool
4933operator==(const sub_match<_BiIter>& __x,
4934           const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
4935{
4936    return __x.compare(typename sub_match<_BiIter>::string_type(__y.data(), __y.size())) == 0;
4937}
4938
4939template <class _BiIter, class _ST, class _SA>
4940inline _LIBCPP_INLINE_VISIBILITY
4941bool
4942operator!=(const sub_match<_BiIter>& __x,
4943           const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
4944{
4945    return !(__x == __y);
4946}
4947
4948template <class _BiIter, class _ST, class _SA>
4949inline _LIBCPP_INLINE_VISIBILITY
4950bool
4951operator<(const sub_match<_BiIter>& __x,
4952          const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
4953{
4954    return __x.compare(typename sub_match<_BiIter>::string_type(__y.data(), __y.size())) < 0;
4955}
4956
4957template <class _BiIter, class _ST, class _SA>
4958inline _LIBCPP_INLINE_VISIBILITY
4959bool operator>(const sub_match<_BiIter>& __x,
4960               const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
4961{
4962    return __y < __x;
4963}
4964
4965template <class _BiIter, class _ST, class _SA>
4966inline _LIBCPP_INLINE_VISIBILITY
4967bool
4968operator>=(const sub_match<_BiIter>& __x,
4969           const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
4970{
4971    return !(__x < __y);
4972}
4973
4974template <class _BiIter, class _ST, class _SA>
4975inline _LIBCPP_INLINE_VISIBILITY
4976bool
4977operator<=(const sub_match<_BiIter>& __x,
4978           const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
4979{
4980    return !(__y < __x);
4981}
4982
4983template <class _BiIter>
4984inline _LIBCPP_INLINE_VISIBILITY
4985bool
4986operator==(typename iterator_traits<_BiIter>::value_type const* __x,
4987           const sub_match<_BiIter>& __y)
4988{
4989    return __y.compare(__x) == 0;
4990}
4991
4992template <class _BiIter>
4993inline _LIBCPP_INLINE_VISIBILITY
4994bool
4995operator!=(typename iterator_traits<_BiIter>::value_type const* __x,
4996           const sub_match<_BiIter>& __y)
4997{
4998    return !(__x == __y);
4999}
5000
5001template <class _BiIter>
5002inline _LIBCPP_INLINE_VISIBILITY
5003bool
5004operator<(typename iterator_traits<_BiIter>::value_type const* __x,
5005          const sub_match<_BiIter>& __y)
5006{
5007    return __y.compare(__x) > 0;
5008}
5009
5010template <class _BiIter>
5011inline _LIBCPP_INLINE_VISIBILITY
5012bool
5013operator>(typename iterator_traits<_BiIter>::value_type const* __x,
5014          const sub_match<_BiIter>& __y)
5015{
5016    return __y < __x;
5017}
5018
5019template <class _BiIter>
5020inline _LIBCPP_INLINE_VISIBILITY
5021bool
5022operator>=(typename iterator_traits<_BiIter>::value_type const* __x,
5023           const sub_match<_BiIter>& __y)
5024{
5025    return !(__x < __y);
5026}
5027
5028template <class _BiIter>
5029inline _LIBCPP_INLINE_VISIBILITY
5030bool
5031operator<=(typename iterator_traits<_BiIter>::value_type const* __x,
5032           const sub_match<_BiIter>& __y)
5033{
5034    return !(__y < __x);
5035}
5036
5037template <class _BiIter>
5038inline _LIBCPP_INLINE_VISIBILITY
5039bool
5040operator==(const sub_match<_BiIter>& __x,
5041           typename iterator_traits<_BiIter>::value_type const* __y)
5042{
5043    return __x.compare(__y) == 0;
5044}
5045
5046template <class _BiIter>
5047inline _LIBCPP_INLINE_VISIBILITY
5048bool
5049operator!=(const sub_match<_BiIter>& __x,
5050           typename iterator_traits<_BiIter>::value_type const* __y)
5051{
5052    return !(__x == __y);
5053}
5054
5055template <class _BiIter>
5056inline _LIBCPP_INLINE_VISIBILITY
5057bool
5058operator<(const sub_match<_BiIter>& __x,
5059          typename iterator_traits<_BiIter>::value_type const* __y)
5060{
5061    return __x.compare(__y) < 0;
5062}
5063
5064template <class _BiIter>
5065inline _LIBCPP_INLINE_VISIBILITY
5066bool
5067operator>(const sub_match<_BiIter>& __x,
5068          typename iterator_traits<_BiIter>::value_type const* __y)
5069{
5070    return __y < __x;
5071}
5072
5073template <class _BiIter>
5074inline _LIBCPP_INLINE_VISIBILITY
5075bool
5076operator>=(const sub_match<_BiIter>& __x,
5077           typename iterator_traits<_BiIter>::value_type const* __y)
5078{
5079    return !(__x < __y);
5080}
5081
5082template <class _BiIter>
5083inline _LIBCPP_INLINE_VISIBILITY
5084bool
5085operator<=(const sub_match<_BiIter>& __x,
5086           typename iterator_traits<_BiIter>::value_type const* __y)
5087{
5088    return !(__y < __x);
5089}
5090
5091template <class _BiIter>
5092inline _LIBCPP_INLINE_VISIBILITY
5093bool
5094operator==(typename iterator_traits<_BiIter>::value_type const& __x,
5095           const sub_match<_BiIter>& __y)
5096{
5097    typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
5098    return __y.compare(string_type(1, __x)) == 0;
5099}
5100
5101template <class _BiIter>
5102inline _LIBCPP_INLINE_VISIBILITY
5103bool
5104operator!=(typename iterator_traits<_BiIter>::value_type const& __x,
5105           const sub_match<_BiIter>& __y)
5106{
5107    return !(__x == __y);
5108}
5109
5110template <class _BiIter>
5111inline _LIBCPP_INLINE_VISIBILITY
5112bool
5113operator<(typename iterator_traits<_BiIter>::value_type const& __x,
5114          const sub_match<_BiIter>& __y)
5115{
5116    typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
5117    return __y.compare(string_type(1, __x)) > 0;
5118}
5119
5120template <class _BiIter>
5121inline _LIBCPP_INLINE_VISIBILITY
5122bool
5123operator>(typename iterator_traits<_BiIter>::value_type const& __x,
5124          const sub_match<_BiIter>& __y)
5125{
5126    return __y < __x;
5127}
5128
5129template <class _BiIter>
5130inline _LIBCPP_INLINE_VISIBILITY
5131bool
5132operator>=(typename iterator_traits<_BiIter>::value_type const& __x,
5133           const sub_match<_BiIter>& __y)
5134{
5135    return !(__x < __y);
5136}
5137
5138template <class _BiIter>
5139inline _LIBCPP_INLINE_VISIBILITY
5140bool
5141operator<=(typename iterator_traits<_BiIter>::value_type const& __x,
5142           const sub_match<_BiIter>& __y)
5143{
5144    return !(__y < __x);
5145}
5146
5147template <class _BiIter>
5148inline _LIBCPP_INLINE_VISIBILITY
5149bool
5150operator==(const sub_match<_BiIter>& __x,
5151           typename iterator_traits<_BiIter>::value_type const& __y)
5152{
5153    typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
5154    return __x.compare(string_type(1, __y)) == 0;
5155}
5156
5157template <class _BiIter>
5158inline _LIBCPP_INLINE_VISIBILITY
5159bool
5160operator!=(const sub_match<_BiIter>& __x,
5161           typename iterator_traits<_BiIter>::value_type const& __y)
5162{
5163    return !(__x == __y);
5164}
5165
5166template <class _BiIter>
5167inline _LIBCPP_INLINE_VISIBILITY
5168bool
5169operator<(const sub_match<_BiIter>& __x,
5170          typename iterator_traits<_BiIter>::value_type const& __y)
5171{
5172    typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
5173    return __x.compare(string_type(1, __y)) < 0;
5174}
5175
5176template <class _BiIter>
5177inline _LIBCPP_INLINE_VISIBILITY
5178bool
5179operator>(const sub_match<_BiIter>& __x,
5180          typename iterator_traits<_BiIter>::value_type const& __y)
5181{
5182    return __y < __x;
5183}
5184
5185template <class _BiIter>
5186inline _LIBCPP_INLINE_VISIBILITY
5187bool
5188operator>=(const sub_match<_BiIter>& __x,
5189           typename iterator_traits<_BiIter>::value_type const& __y)
5190{
5191    return !(__x < __y);
5192}
5193
5194template <class _BiIter>
5195inline _LIBCPP_INLINE_VISIBILITY
5196bool
5197operator<=(const sub_match<_BiIter>& __x,
5198           typename iterator_traits<_BiIter>::value_type const& __y)
5199{
5200    return !(__y < __x);
5201}
5202
5203template <class _CharT, class _ST, class _BiIter>
5204inline _LIBCPP_INLINE_VISIBILITY
5205basic_ostream<_CharT, _ST>&
5206operator<<(basic_ostream<_CharT, _ST>& __os, const sub_match<_BiIter>& __m)
5207{
5208    return __os << __m.str();
5209}
5210
5211template <class _BidirectionalIterator, class _Allocator>
5212class _LIBCPP_TEMPLATE_VIS match_results
5213{
5214public:
5215    typedef _Allocator                                        allocator_type;
5216    typedef sub_match<_BidirectionalIterator>                 value_type;
5217private:
5218    typedef vector<value_type, allocator_type>                __container_type;
5219
5220    __container_type  __matches_;
5221    value_type __unmatched_;
5222    value_type __prefix_;
5223    value_type __suffix_;
5224    bool       __ready_;
5225public:
5226    _BidirectionalIterator __position_start_;
5227    typedef const value_type&                                 const_reference;
5228    typedef value_type&                                       reference;
5229    typedef typename __container_type::const_iterator         const_iterator;
5230    typedef const_iterator                                    iterator;
5231    typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
5232    typedef typename allocator_traits<allocator_type>::size_type size_type;
5233    typedef typename iterator_traits<_BidirectionalIterator>::value_type char_type;
5234    typedef basic_string<char_type>                           string_type;
5235
5236    // construct/copy/destroy:
5237    explicit match_results(const allocator_type& __a = allocator_type());
5238//    match_results(const match_results&) = default;
5239//    match_results& operator=(const match_results&) = default;
5240//    match_results(match_results&& __m) = default;
5241//    match_results& operator=(match_results&& __m) = default;
5242//    ~match_results() = default;
5243
5244    _LIBCPP_INLINE_VISIBILITY
5245    bool ready() const {return __ready_;}
5246
5247    // size:
5248    _LIBCPP_INLINE_VISIBILITY
5249    size_type size() const _NOEXCEPT {return __matches_.size();}
5250    _LIBCPP_INLINE_VISIBILITY
5251    size_type max_size() const _NOEXCEPT {return __matches_.max_size();}
5252    _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
5253    bool empty() const _NOEXCEPT {return size() == 0;}
5254
5255    // element access:
5256    _LIBCPP_INLINE_VISIBILITY
5257    difference_type length(size_type __sub = 0) const
5258        {return (*this)[__sub].length();}
5259    _LIBCPP_INLINE_VISIBILITY
5260    difference_type position(size_type __sub = 0) const
5261        {return _VSTD::distance(__position_start_, (*this)[__sub].first);}
5262    _LIBCPP_INLINE_VISIBILITY
5263    string_type str(size_type __sub = 0) const
5264        {return (*this)[__sub].str();}
5265    _LIBCPP_INLINE_VISIBILITY
5266    const_reference operator[](size_type __n) const
5267        {return __n < __matches_.size() ? __matches_[__n] : __unmatched_;}
5268
5269    _LIBCPP_INLINE_VISIBILITY
5270    const_reference prefix() const {return __prefix_;}
5271    _LIBCPP_INLINE_VISIBILITY
5272    const_reference suffix() const {return __suffix_;}
5273
5274    _LIBCPP_INLINE_VISIBILITY
5275    const_iterator begin() const {return empty() ? __matches_.end() : __matches_.begin();}
5276    _LIBCPP_INLINE_VISIBILITY
5277    const_iterator end() const {return __matches_.end();}
5278    _LIBCPP_INLINE_VISIBILITY
5279    const_iterator cbegin() const {return empty() ? __matches_.end() : __matches_.begin();}
5280    _LIBCPP_INLINE_VISIBILITY
5281    const_iterator cend() const {return __matches_.end();}
5282
5283    // format:
5284    template <class _OutputIter>
5285        _OutputIter
5286        format(_OutputIter __output_iter, const char_type* __fmt_first,
5287               const char_type* __fmt_last,
5288               regex_constants::match_flag_type __flags = regex_constants::format_default) const;
5289    template <class _OutputIter, class _ST, class _SA>
5290        _LIBCPP_INLINE_VISIBILITY
5291        _OutputIter
5292        format(_OutputIter __output_iter, const basic_string<char_type, _ST, _SA>& __fmt,
5293               regex_constants::match_flag_type __flags = regex_constants::format_default) const
5294            {return format(__output_iter, __fmt.data(), __fmt.data() + __fmt.size(), __flags);}
5295    template <class _ST, class _SA>
5296        _LIBCPP_INLINE_VISIBILITY
5297        basic_string<char_type, _ST, _SA>
5298        format(const basic_string<char_type, _ST, _SA>& __fmt,
5299               regex_constants::match_flag_type __flags = regex_constants::format_default) const
5300        {
5301            basic_string<char_type, _ST, _SA> __r;
5302            format(back_inserter(__r), __fmt.data(), __fmt.data() + __fmt.size(),
5303                   __flags);
5304            return __r;
5305        }
5306    _LIBCPP_INLINE_VISIBILITY
5307    string_type
5308        format(const char_type* __fmt,
5309               regex_constants::match_flag_type __flags = regex_constants::format_default) const
5310        {
5311            string_type __r;
5312            format(back_inserter(__r), __fmt,
5313                   __fmt + char_traits<char_type>::length(__fmt), __flags);
5314            return __r;
5315        }
5316
5317    // allocator:
5318    _LIBCPP_INLINE_VISIBILITY
5319    allocator_type get_allocator() const {return __matches_.get_allocator();}
5320
5321    // swap:
5322    void swap(match_results& __m);
5323
5324    template <class _Bp, class _Ap>
5325        _LIBCPP_INLINE_VISIBILITY
5326        void __assign(_BidirectionalIterator __f, _BidirectionalIterator __l,
5327                      const match_results<_Bp, _Ap>& __m, bool __no_update_pos)
5328    {
5329        _Bp __mf = __m.prefix().first;
5330        __matches_.resize(__m.size());
5331        for (size_type __i = 0; __i < __matches_.size(); ++__i)
5332        {
5333            __matches_[__i].first = _VSTD::next(__f, _VSTD::distance(__mf, __m[__i].first));
5334            __matches_[__i].second = _VSTD::next(__f, _VSTD::distance(__mf, __m[__i].second));
5335            __matches_[__i].matched = __m[__i].matched;
5336        }
5337        __unmatched_.first   = __l;
5338        __unmatched_.second  = __l;
5339        __unmatched_.matched = false;
5340        __prefix_.first = _VSTD::next(__f, _VSTD::distance(__mf, __m.prefix().first));
5341        __prefix_.second = _VSTD::next(__f, _VSTD::distance(__mf, __m.prefix().second));
5342        __prefix_.matched = __m.prefix().matched;
5343        __suffix_.first = _VSTD::next(__f, _VSTD::distance(__mf, __m.suffix().first));
5344        __suffix_.second = _VSTD::next(__f, _VSTD::distance(__mf, __m.suffix().second));
5345        __suffix_.matched = __m.suffix().matched;
5346        if (!__no_update_pos)
5347            __position_start_ = __prefix_.first;
5348        __ready_ = __m.ready();
5349    }
5350
5351private:
5352    void __init(unsigned __s,
5353                _BidirectionalIterator __f, _BidirectionalIterator __l,
5354                bool __no_update_pos = false);
5355
5356    template <class, class> friend class basic_regex;
5357
5358    template <class _Bp, class _Ap, class _Cp, class _Tp>
5359    friend
5360    bool
5361    regex_match(_Bp, _Bp, match_results<_Bp, _Ap>&, const basic_regex<_Cp, _Tp>&,
5362                regex_constants::match_flag_type);
5363
5364    template <class _Bp, class _Ap>
5365    friend
5366    bool
5367    operator==(const match_results<_Bp, _Ap>&, const match_results<_Bp, _Ap>&);
5368
5369    template <class, class> friend class __lookahead;
5370};
5371
5372template <class _BidirectionalIterator, class _Allocator>
5373match_results<_BidirectionalIterator, _Allocator>::match_results(
5374        const allocator_type& __a)
5375    : __matches_(__a),
5376      __unmatched_(),
5377      __prefix_(),
5378      __suffix_(),
5379      __ready_(false),
5380      __position_start_()
5381{
5382}
5383
5384template <class _BidirectionalIterator, class _Allocator>
5385void
5386match_results<_BidirectionalIterator, _Allocator>::__init(unsigned __s,
5387                         _BidirectionalIterator __f, _BidirectionalIterator __l,
5388                         bool __no_update_pos)
5389{
5390    __unmatched_.first   = __l;
5391    __unmatched_.second  = __l;
5392    __unmatched_.matched = false;
5393    __matches_.assign(__s, __unmatched_);
5394    __prefix_.first      = __f;
5395    __prefix_.second     = __f;
5396    __prefix_.matched    = false;
5397    __suffix_ = __unmatched_;
5398    if (!__no_update_pos)
5399        __position_start_ = __prefix_.first;
5400    __ready_ = true;
5401}
5402
5403template <class _BidirectionalIterator, class _Allocator>
5404template <class _OutputIter>
5405_OutputIter
5406match_results<_BidirectionalIterator, _Allocator>::format(_OutputIter __output_iter,
5407        const char_type* __fmt_first, const char_type* __fmt_last,
5408        regex_constants::match_flag_type __flags) const
5409{
5410    if (__flags & regex_constants::format_sed)
5411    {
5412        for (; __fmt_first != __fmt_last; ++__fmt_first)
5413        {
5414            if (*__fmt_first == '&')
5415                __output_iter = _VSTD::copy(__matches_[0].first, __matches_[0].second,
5416                                   __output_iter);
5417            else if (*__fmt_first == '\\' && __fmt_first + 1 != __fmt_last)
5418            {
5419                ++__fmt_first;
5420                if ('0' <= *__fmt_first && *__fmt_first <= '9')
5421                {
5422                    size_t __i = *__fmt_first - '0';
5423                    __output_iter = _VSTD::copy((*this)[__i].first,
5424                                        (*this)[__i].second, __output_iter);
5425                }
5426                else
5427                {
5428                    *__output_iter = *__fmt_first;
5429                    ++__output_iter;
5430                }
5431            }
5432            else
5433            {
5434                *__output_iter = *__fmt_first;
5435                ++__output_iter;
5436            }
5437        }
5438    }
5439    else
5440    {
5441        for (; __fmt_first != __fmt_last; ++__fmt_first)
5442        {
5443            if (*__fmt_first == '$' && __fmt_first + 1 != __fmt_last)
5444            {
5445                switch (__fmt_first[1])
5446                {
5447                case '$':
5448                    *__output_iter = *++__fmt_first;
5449                    ++__output_iter;
5450                    break;
5451                case '&':
5452                    ++__fmt_first;
5453                    __output_iter = _VSTD::copy(__matches_[0].first, __matches_[0].second,
5454                                       __output_iter);
5455                    break;
5456                case '`':
5457                    ++__fmt_first;
5458                    __output_iter = _VSTD::copy(__prefix_.first, __prefix_.second, __output_iter);
5459                    break;
5460                case '\'':
5461                    ++__fmt_first;
5462                    __output_iter = _VSTD::copy(__suffix_.first, __suffix_.second, __output_iter);
5463                    break;
5464                default:
5465                    if ('0' <= __fmt_first[1] && __fmt_first[1] <= '9')
5466                    {
5467                        ++__fmt_first;
5468                        size_t __idx = *__fmt_first - '0';
5469                        if (__fmt_first + 1 != __fmt_last &&
5470                            '0' <= __fmt_first[1] && __fmt_first[1] <= '9')
5471                        {
5472                            ++__fmt_first;
5473                            if (__idx >= std::numeric_limits<size_t>::max() / 10)
5474                                __throw_regex_error<regex_constants::error_escape>();
5475                            __idx = 10 * __idx + *__fmt_first - '0';
5476                        }
5477                        __output_iter = _VSTD::copy((*this)[__idx].first,
5478                                            (*this)[__idx].second, __output_iter);
5479                    }
5480                    else
5481                    {
5482                        *__output_iter = *__fmt_first;
5483                        ++__output_iter;
5484                    }
5485                    break;
5486                }
5487            }
5488            else
5489            {
5490                *__output_iter = *__fmt_first;
5491                ++__output_iter;
5492            }
5493        }
5494    }
5495    return __output_iter;
5496}
5497
5498template <class _BidirectionalIterator, class _Allocator>
5499void
5500match_results<_BidirectionalIterator, _Allocator>::swap(match_results& __m)
5501{
5502    using _VSTD::swap;
5503    swap(__matches_, __m.__matches_);
5504    swap(__unmatched_, __m.__unmatched_);
5505    swap(__prefix_, __m.__prefix_);
5506    swap(__suffix_, __m.__suffix_);
5507    swap(__position_start_, __m.__position_start_);
5508    swap(__ready_, __m.__ready_);
5509}
5510
5511typedef match_results<const char*>             cmatch;
5512typedef match_results<const wchar_t*>          wcmatch;
5513typedef match_results<string::const_iterator>  smatch;
5514typedef match_results<wstring::const_iterator> wsmatch;
5515
5516template <class _BidirectionalIterator, class _Allocator>
5517bool
5518operator==(const match_results<_BidirectionalIterator, _Allocator>& __x,
5519           const match_results<_BidirectionalIterator, _Allocator>& __y)
5520{
5521    if (__x.__ready_ != __y.__ready_)
5522        return false;
5523    if (!__x.__ready_)
5524        return true;
5525    return __x.__matches_ == __y.__matches_ &&
5526           __x.__prefix_ == __y.__prefix_ &&
5527           __x.__suffix_ == __y.__suffix_;
5528}
5529
5530template <class _BidirectionalIterator, class _Allocator>
5531inline _LIBCPP_INLINE_VISIBILITY
5532bool
5533operator!=(const match_results<_BidirectionalIterator, _Allocator>& __x,
5534           const match_results<_BidirectionalIterator, _Allocator>& __y)
5535{
5536    return !(__x == __y);
5537}
5538
5539template <class _BidirectionalIterator, class _Allocator>
5540inline _LIBCPP_INLINE_VISIBILITY
5541void
5542swap(match_results<_BidirectionalIterator, _Allocator>& __x,
5543     match_results<_BidirectionalIterator, _Allocator>& __y)
5544{
5545    __x.swap(__y);
5546}
5547
5548// regex_search
5549
5550template <class _CharT, class _Traits>
5551template <class _Allocator>
5552bool
5553basic_regex<_CharT, _Traits>::__match_at_start_ecma(
5554        const _CharT* __first, const _CharT* __last,
5555        match_results<const _CharT*, _Allocator>& __m,
5556        regex_constants::match_flag_type __flags, bool __at_first) const
5557{
5558    vector<__state> __states;
5559    __node* __st = __start_.get();
5560    if (__st)
5561    {
5562        sub_match<const _CharT*> __unmatched;
5563        __unmatched.first   = __last;
5564        __unmatched.second  = __last;
5565        __unmatched.matched = false;
5566
5567        __states.push_back(__state());
5568        __states.back().__do_ = 0;
5569        __states.back().__first_ = __first;
5570        __states.back().__current_ = __first;
5571        __states.back().__last_ = __last;
5572        __states.back().__sub_matches_.resize(mark_count(), __unmatched);
5573        __states.back().__loop_data_.resize(__loop_count());
5574        __states.back().__node_ = __st;
5575        __states.back().__flags_ = __flags;
5576        __states.back().__at_first_ = __at_first;
5577        int __counter = 0;
5578        int __length = __last - __first;
5579        do
5580        {
5581            ++__counter;
5582            if (__counter % _LIBCPP_REGEX_COMPLEXITY_FACTOR == 0 &&
5583                __counter / _LIBCPP_REGEX_COMPLEXITY_FACTOR >= __length)
5584              __throw_regex_error<regex_constants::error_complexity>();
5585            __state& __s = __states.back();
5586            if (__s.__node_)
5587                __s.__node_->__exec(__s);
5588            switch (__s.__do_)
5589            {
5590            case __state::__end_state:
5591                if ((__flags & regex_constants::match_not_null) &&
5592                    __s.__current_ == __first)
5593                {
5594                  __states.pop_back();
5595                  break;
5596                }
5597                if ((__flags & regex_constants::__full_match) &&
5598                    __s.__current_ != __last)
5599                {
5600                  __states.pop_back();
5601                  break;
5602                }
5603                __m.__matches_[0].first = __first;
5604                __m.__matches_[0].second = _VSTD::next(__first, __s.__current_ - __first);
5605                __m.__matches_[0].matched = true;
5606                for (unsigned __i = 0; __i < __s.__sub_matches_.size(); ++__i)
5607                    __m.__matches_[__i+1] = __s.__sub_matches_[__i];
5608                return true;
5609            case __state::__accept_and_consume:
5610            case __state::__repeat:
5611            case __state::__accept_but_not_consume:
5612                break;
5613            case __state::__split:
5614                {
5615                __state __snext = __s;
5616                __s.__node_->__exec_split(true, __s);
5617                __snext.__node_->__exec_split(false, __snext);
5618                __states.push_back(_VSTD::move(__snext));
5619                }
5620                break;
5621            case __state::__reject:
5622                __states.pop_back();
5623                break;
5624            default:
5625                __throw_regex_error<regex_constants::__re_err_unknown>();
5626                break;
5627
5628            }
5629        } while (!__states.empty());
5630    }
5631    return false;
5632}
5633
5634template <class _CharT, class _Traits>
5635template <class _Allocator>
5636bool
5637basic_regex<_CharT, _Traits>::__match_at_start_posix_nosubs(
5638        const _CharT* __first, const _CharT* __last,
5639        match_results<const _CharT*, _Allocator>& __m,
5640        regex_constants::match_flag_type __flags, bool __at_first) const
5641{
5642    deque<__state> __states;
5643    ptrdiff_t __highest_j = 0;
5644    ptrdiff_t _Np = _VSTD::distance(__first, __last);
5645    __node* __st = __start_.get();
5646    if (__st)
5647    {
5648        __states.push_back(__state());
5649        __states.back().__do_ = 0;
5650        __states.back().__first_ = __first;
5651        __states.back().__current_ = __first;
5652        __states.back().__last_ = __last;
5653        __states.back().__loop_data_.resize(__loop_count());
5654        __states.back().__node_ = __st;
5655        __states.back().__flags_ = __flags;
5656        __states.back().__at_first_ = __at_first;
5657        bool __matched = false;
5658        int __counter = 0;
5659        int __length = __last - __first;
5660        do
5661        {
5662            ++__counter;
5663            if (__counter % _LIBCPP_REGEX_COMPLEXITY_FACTOR == 0 &&
5664                __counter / _LIBCPP_REGEX_COMPLEXITY_FACTOR >= __length)
5665              __throw_regex_error<regex_constants::error_complexity>();
5666            __state& __s = __states.back();
5667            if (__s.__node_)
5668                __s.__node_->__exec(__s);
5669            switch (__s.__do_)
5670            {
5671            case __state::__end_state:
5672                if ((__flags & regex_constants::match_not_null) &&
5673                    __s.__current_ == __first)
5674                {
5675                  __states.pop_back();
5676                  break;
5677                }
5678                if ((__flags & regex_constants::__full_match) &&
5679                    __s.__current_ != __last)
5680                {
5681                  __states.pop_back();
5682                  break;
5683                }
5684                if (!__matched || __highest_j < __s.__current_ - __s.__first_)
5685                    __highest_j = __s.__current_ - __s.__first_;
5686                __matched = true;
5687                if (__highest_j == _Np)
5688                    __states.clear();
5689                else
5690                    __states.pop_back();
5691                break;
5692            case __state::__consume_input:
5693                break;
5694            case __state::__accept_and_consume:
5695                __states.push_front(_VSTD::move(__s));
5696                __states.pop_back();
5697                break;
5698            case __state::__repeat:
5699            case __state::__accept_but_not_consume:
5700                break;
5701            case __state::__split:
5702                {
5703                __state __snext = __s;
5704                __s.__node_->__exec_split(true, __s);
5705                __snext.__node_->__exec_split(false, __snext);
5706                __states.push_back(_VSTD::move(__snext));
5707                }
5708                break;
5709            case __state::__reject:
5710                __states.pop_back();
5711                break;
5712            default:
5713                __throw_regex_error<regex_constants::__re_err_unknown>();
5714                break;
5715            }
5716        } while (!__states.empty());
5717        if (__matched)
5718        {
5719            __m.__matches_[0].first = __first;
5720            __m.__matches_[0].second = _VSTD::next(__first, __highest_j);
5721            __m.__matches_[0].matched = true;
5722            return true;
5723        }
5724    }
5725    return false;
5726}
5727
5728template <class _CharT, class _Traits>
5729template <class _Allocator>
5730bool
5731basic_regex<_CharT, _Traits>::__match_at_start_posix_subs(
5732        const _CharT* __first, const _CharT* __last,
5733        match_results<const _CharT*, _Allocator>& __m,
5734        regex_constants::match_flag_type __flags, bool __at_first) const
5735{
5736    vector<__state> __states;
5737    __state __best_state;
5738    ptrdiff_t __j = 0;
5739    ptrdiff_t __highest_j = 0;
5740    ptrdiff_t _Np = _VSTD::distance(__first, __last);
5741    __node* __st = __start_.get();
5742    if (__st)
5743    {
5744        sub_match<const _CharT*> __unmatched;
5745        __unmatched.first   = __last;
5746        __unmatched.second  = __last;
5747        __unmatched.matched = false;
5748
5749        __states.push_back(__state());
5750        __states.back().__do_ = 0;
5751        __states.back().__first_ = __first;
5752        __states.back().__current_ = __first;
5753        __states.back().__last_ = __last;
5754        __states.back().__sub_matches_.resize(mark_count(), __unmatched);
5755        __states.back().__loop_data_.resize(__loop_count());
5756        __states.back().__node_ = __st;
5757        __states.back().__flags_ = __flags;
5758        __states.back().__at_first_ = __at_first;
5759        const _CharT* __current = __first;
5760        bool __matched = false;
5761        int __counter = 0;
5762        int __length = __last - __first;
5763        do
5764        {
5765            ++__counter;
5766            if (__counter % _LIBCPP_REGEX_COMPLEXITY_FACTOR == 0 &&
5767                __counter / _LIBCPP_REGEX_COMPLEXITY_FACTOR >= __length)
5768              __throw_regex_error<regex_constants::error_complexity>();
5769            __state& __s = __states.back();
5770            if (__s.__node_)
5771                __s.__node_->__exec(__s);
5772            switch (__s.__do_)
5773            {
5774            case __state::__end_state:
5775                if ((__flags & regex_constants::match_not_null) &&
5776                    __s.__current_ == __first)
5777                {
5778                  __states.pop_back();
5779                  break;
5780                }
5781                if ((__flags & regex_constants::__full_match) &&
5782                    __s.__current_ != __last)
5783                {
5784                  __states.pop_back();
5785                  break;
5786                }
5787                if (!__matched || __highest_j < __s.__current_ - __s.__first_)
5788                {
5789                    __highest_j = __s.__current_ - __s.__first_;
5790                    __best_state = __s;
5791                }
5792                __matched = true;
5793                if (__highest_j == _Np)
5794                    __states.clear();
5795                else
5796                    __states.pop_back();
5797                break;
5798            case __state::__accept_and_consume:
5799                __j += __s.__current_ - __current;
5800                __current = __s.__current_;
5801                break;
5802            case __state::__repeat:
5803            case __state::__accept_but_not_consume:
5804                break;
5805            case __state::__split:
5806                {
5807                __state __snext = __s;
5808                __s.__node_->__exec_split(true, __s);
5809                __snext.__node_->__exec_split(false, __snext);
5810                __states.push_back(_VSTD::move(__snext));
5811                }
5812                break;
5813            case __state::__reject:
5814                __states.pop_back();
5815                break;
5816            default:
5817                __throw_regex_error<regex_constants::__re_err_unknown>();
5818                break;
5819            }
5820        } while (!__states.empty());
5821        if (__matched)
5822        {
5823            __m.__matches_[0].first = __first;
5824            __m.__matches_[0].second = _VSTD::next(__first, __highest_j);
5825            __m.__matches_[0].matched = true;
5826            for (unsigned __i = 0; __i < __best_state.__sub_matches_.size(); ++__i)
5827                __m.__matches_[__i+1] = __best_state.__sub_matches_[__i];
5828            return true;
5829        }
5830    }
5831    return false;
5832}
5833
5834template <class _CharT, class _Traits>
5835template <class _Allocator>
5836bool
5837basic_regex<_CharT, _Traits>::__match_at_start(
5838        const _CharT* __first, const _CharT* __last,
5839        match_results<const _CharT*, _Allocator>& __m,
5840        regex_constants::match_flag_type __flags, bool __at_first) const
5841{
5842    if ((__flags_ & 0x1F0) == ECMAScript)
5843        return __match_at_start_ecma(__first, __last, __m, __flags, __at_first);
5844    if (mark_count() == 0)
5845        return __match_at_start_posix_nosubs(__first, __last, __m, __flags, __at_first);
5846    return __match_at_start_posix_subs(__first, __last, __m, __flags, __at_first);
5847}
5848
5849template <class _CharT, class _Traits>
5850template <class _Allocator>
5851bool
5852basic_regex<_CharT, _Traits>::__search(
5853        const _CharT* __first, const _CharT* __last,
5854        match_results<const _CharT*, _Allocator>& __m,
5855        regex_constants::match_flag_type __flags) const
5856{
5857    __m.__init(1 + mark_count(), __first, __last,
5858                                    __flags & regex_constants::__no_update_pos);
5859    if (__match_at_start(__first, __last, __m, __flags,
5860                                    !(__flags & regex_constants::__no_update_pos)))
5861    {
5862        __m.__prefix_.second = __m[0].first;
5863        __m.__prefix_.matched = __m.__prefix_.first != __m.__prefix_.second;
5864        __m.__suffix_.first = __m[0].second;
5865        __m.__suffix_.matched = __m.__suffix_.first != __m.__suffix_.second;
5866        return true;
5867    }
5868    if (__first != __last && !(__flags & regex_constants::match_continuous))
5869    {
5870        __flags |= regex_constants::match_prev_avail;
5871        for (++__first; __first != __last; ++__first)
5872        {
5873            __m.__matches_.assign(__m.size(), __m.__unmatched_);
5874            if (__match_at_start(__first, __last, __m, __flags, false))
5875            {
5876                __m.__prefix_.second = __m[0].first;
5877                __m.__prefix_.matched = __m.__prefix_.first != __m.__prefix_.second;
5878                __m.__suffix_.first = __m[0].second;
5879                __m.__suffix_.matched = __m.__suffix_.first != __m.__suffix_.second;
5880                return true;
5881            }
5882            __m.__matches_.assign(__m.size(), __m.__unmatched_);
5883        }
5884    }
5885    __m.__matches_.clear();
5886    return false;
5887}
5888
5889template <class _BidirectionalIterator, class _Allocator, class _CharT, class _Traits>
5890inline _LIBCPP_INLINE_VISIBILITY
5891bool
5892regex_search(_BidirectionalIterator __first, _BidirectionalIterator __last,
5893             match_results<_BidirectionalIterator, _Allocator>& __m,
5894             const basic_regex<_CharT, _Traits>& __e,
5895             regex_constants::match_flag_type __flags = regex_constants::match_default)
5896{
5897    int __offset = (__flags & regex_constants::match_prev_avail) ? 1 : 0;
5898    basic_string<_CharT> __s(_VSTD::prev(__first, __offset), __last);
5899    match_results<const _CharT*> __mc;
5900    bool __r = __e.__search(__s.data() + __offset, __s.data() + __s.size(), __mc, __flags);
5901    __m.__assign(__first, __last, __mc, __flags & regex_constants::__no_update_pos);
5902    return __r;
5903}
5904
5905template <class _Iter, class _Allocator, class _CharT, class _Traits>
5906inline _LIBCPP_INLINE_VISIBILITY
5907bool
5908regex_search(__wrap_iter<_Iter> __first,
5909             __wrap_iter<_Iter> __last,
5910             match_results<__wrap_iter<_Iter>, _Allocator>& __m,
5911             const basic_regex<_CharT, _Traits>& __e,
5912             regex_constants::match_flag_type __flags = regex_constants::match_default)
5913{
5914    match_results<const _CharT*> __mc;
5915    bool __r = __e.__search(__first.base(), __last.base(), __mc, __flags);
5916    __m.__assign(__first, __last, __mc, __flags & regex_constants::__no_update_pos);
5917    return __r;
5918}
5919
5920template <class _Allocator, class _CharT, class _Traits>
5921inline _LIBCPP_INLINE_VISIBILITY
5922bool
5923regex_search(const _CharT* __first, const _CharT* __last,
5924             match_results<const _CharT*, _Allocator>& __m,
5925             const basic_regex<_CharT, _Traits>& __e,
5926             regex_constants::match_flag_type __flags = regex_constants::match_default)
5927{
5928    return __e.__search(__first, __last, __m, __flags);
5929}
5930
5931template <class _BidirectionalIterator, class _CharT, class _Traits>
5932inline _LIBCPP_INLINE_VISIBILITY
5933bool
5934regex_search(_BidirectionalIterator __first, _BidirectionalIterator __last,
5935             const basic_regex<_CharT, _Traits>& __e,
5936             regex_constants::match_flag_type __flags = regex_constants::match_default)
5937{
5938    basic_string<_CharT> __s(__first, __last);
5939    match_results<const _CharT*> __mc;
5940    return __e.__search(__s.data(), __s.data() + __s.size(), __mc, __flags);
5941}
5942
5943template <class _CharT, class _Traits>
5944inline _LIBCPP_INLINE_VISIBILITY
5945bool
5946regex_search(const _CharT* __first, const _CharT* __last,
5947             const basic_regex<_CharT, _Traits>& __e,
5948             regex_constants::match_flag_type __flags = regex_constants::match_default)
5949{
5950    match_results<const _CharT*> __mc;
5951    return __e.__search(__first, __last, __mc, __flags);
5952}
5953
5954template <class _CharT, class _Allocator, class _Traits>
5955inline _LIBCPP_INLINE_VISIBILITY
5956bool
5957regex_search(const _CharT* __str, match_results<const _CharT*, _Allocator>& __m,
5958             const basic_regex<_CharT, _Traits>& __e,
5959             regex_constants::match_flag_type __flags = regex_constants::match_default)
5960{
5961    return __e.__search(__str, __str + _Traits::length(__str), __m, __flags);
5962}
5963
5964template <class _CharT, class _Traits>
5965inline _LIBCPP_INLINE_VISIBILITY
5966bool
5967regex_search(const _CharT* __str, const basic_regex<_CharT, _Traits>& __e,
5968             regex_constants::match_flag_type __flags = regex_constants::match_default)
5969{
5970    match_results<const _CharT*> __m;
5971    return _VSTD::regex_search(__str, __m, __e, __flags);
5972}
5973
5974template <class _ST, class _SA, class _CharT, class _Traits>
5975inline _LIBCPP_INLINE_VISIBILITY
5976bool
5977regex_search(const basic_string<_CharT, _ST, _SA>& __s,
5978             const basic_regex<_CharT, _Traits>& __e,
5979             regex_constants::match_flag_type __flags = regex_constants::match_default)
5980{
5981    match_results<const _CharT*> __mc;
5982    return __e.__search(__s.data(), __s.data() + __s.size(), __mc, __flags);
5983}
5984
5985template <class _ST, class _SA, class _Allocator, class _CharT, class _Traits>
5986inline _LIBCPP_INLINE_VISIBILITY
5987bool
5988regex_search(const basic_string<_CharT, _ST, _SA>& __s,
5989             match_results<typename basic_string<_CharT, _ST, _SA>::const_iterator, _Allocator>& __m,
5990             const basic_regex<_CharT, _Traits>& __e,
5991             regex_constants::match_flag_type __flags = regex_constants::match_default)
5992{
5993    match_results<const _CharT*> __mc;
5994    bool __r = __e.__search(__s.data(), __s.data() + __s.size(), __mc, __flags);
5995    __m.__assign(__s.begin(), __s.end(), __mc, __flags & regex_constants::__no_update_pos);
5996    return __r;
5997}
5998
5999#if _LIBCPP_STD_VER > 11
6000template <class _ST, class _SA, class _Ap, class _Cp, class _Tp>
6001bool
6002regex_search(const basic_string<_Cp, _ST, _SA>&& __s,
6003             match_results<typename basic_string<_Cp, _ST, _SA>::const_iterator, _Ap>&,
6004             const basic_regex<_Cp, _Tp>& __e,
6005             regex_constants::match_flag_type __flags = regex_constants::match_default) = delete;
6006#endif
6007
6008// regex_match
6009
6010template <class _BidirectionalIterator, class _Allocator, class _CharT, class _Traits>
6011bool
6012regex_match(_BidirectionalIterator __first, _BidirectionalIterator __last,
6013            match_results<_BidirectionalIterator, _Allocator>& __m,
6014            const basic_regex<_CharT, _Traits>& __e,
6015            regex_constants::match_flag_type __flags = regex_constants::match_default)
6016{
6017    bool __r = _VSTD::regex_search(
6018        __first, __last, __m, __e,
6019        __flags | regex_constants::match_continuous |
6020        regex_constants::__full_match);
6021    if (__r)
6022    {
6023        __r = !__m.suffix().matched;
6024        if (!__r)
6025            __m.__matches_.clear();
6026    }
6027    return __r;
6028}
6029
6030template <class _BidirectionalIterator, class _CharT, class _Traits>
6031inline _LIBCPP_INLINE_VISIBILITY
6032bool
6033regex_match(_BidirectionalIterator __first, _BidirectionalIterator __last,
6034            const basic_regex<_CharT, _Traits>& __e,
6035            regex_constants::match_flag_type __flags = regex_constants::match_default)
6036{
6037    match_results<_BidirectionalIterator> __m;
6038    return _VSTD::regex_match(__first, __last, __m, __e, __flags);
6039}
6040
6041template <class _CharT, class _Allocator, class _Traits>
6042inline _LIBCPP_INLINE_VISIBILITY
6043bool
6044regex_match(const _CharT* __str, match_results<const _CharT*, _Allocator>& __m,
6045            const basic_regex<_CharT, _Traits>& __e,
6046            regex_constants::match_flag_type __flags = regex_constants::match_default)
6047{
6048    return _VSTD::regex_match(__str, __str + _Traits::length(__str), __m, __e, __flags);
6049}
6050
6051template <class _ST, class _SA, class _Allocator, class _CharT, class _Traits>
6052inline _LIBCPP_INLINE_VISIBILITY
6053bool
6054regex_match(const basic_string<_CharT, _ST, _SA>& __s,
6055            match_results<typename basic_string<_CharT, _ST, _SA>::const_iterator, _Allocator>& __m,
6056            const basic_regex<_CharT, _Traits>& __e,
6057            regex_constants::match_flag_type __flags = regex_constants::match_default)
6058{
6059    return _VSTD::regex_match(__s.begin(), __s.end(), __m, __e, __flags);
6060}
6061
6062#if _LIBCPP_STD_VER > 11
6063template <class _ST, class _SA, class _Allocator, class _CharT, class _Traits>
6064inline _LIBCPP_INLINE_VISIBILITY
6065bool
6066regex_match(const basic_string<_CharT, _ST, _SA>&& __s,
6067            match_results<typename basic_string<_CharT, _ST, _SA>::const_iterator, _Allocator>& __m,
6068            const basic_regex<_CharT, _Traits>& __e,
6069            regex_constants::match_flag_type __flags = regex_constants::match_default) = delete;
6070#endif
6071
6072template <class _CharT, class _Traits>
6073inline _LIBCPP_INLINE_VISIBILITY
6074bool
6075regex_match(const _CharT* __str, const basic_regex<_CharT, _Traits>& __e,
6076            regex_constants::match_flag_type __flags = regex_constants::match_default)
6077{
6078    return _VSTD::regex_match(__str, __str + _Traits::length(__str), __e, __flags);
6079}
6080
6081template <class _ST, class _SA, class _CharT, class _Traits>
6082inline _LIBCPP_INLINE_VISIBILITY
6083bool
6084regex_match(const basic_string<_CharT, _ST, _SA>& __s,
6085            const basic_regex<_CharT, _Traits>& __e,
6086            regex_constants::match_flag_type __flags = regex_constants::match_default)
6087{
6088    return _VSTD::regex_match(__s.begin(), __s.end(), __e, __flags);
6089}
6090
6091// regex_iterator
6092
6093template <class _BidirectionalIterator,
6094          class _CharT = typename iterator_traits<_BidirectionalIterator>::value_type,
6095          class _Traits = regex_traits<_CharT> >
6096class _LIBCPP_TEMPLATE_VIS regex_iterator
6097{
6098public:
6099    typedef basic_regex<_CharT, _Traits>          regex_type;
6100    typedef match_results<_BidirectionalIterator> value_type;
6101    typedef ptrdiff_t                             difference_type;
6102    typedef const value_type*                     pointer;
6103    typedef const value_type&                     reference;
6104    typedef forward_iterator_tag                  iterator_category;
6105
6106private:
6107    _BidirectionalIterator           __begin_;
6108    _BidirectionalIterator           __end_;
6109    const regex_type*                __pregex_;
6110    regex_constants::match_flag_type __flags_;
6111    value_type                       __match_;
6112
6113public:
6114    regex_iterator();
6115    regex_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6116                   const regex_type& __re,
6117                   regex_constants::match_flag_type __m
6118                                              = regex_constants::match_default);
6119#if _LIBCPP_STD_VER > 11
6120    regex_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6121                   const regex_type&& __re,
6122                   regex_constants::match_flag_type __m
6123                                     = regex_constants::match_default) = delete;
6124#endif
6125
6126    bool operator==(const regex_iterator& __x) const;
6127    _LIBCPP_INLINE_VISIBILITY
6128    bool operator!=(const regex_iterator& __x) const {return !(*this == __x);}
6129
6130    _LIBCPP_INLINE_VISIBILITY
6131    reference operator*() const {return  __match_;}
6132    _LIBCPP_INLINE_VISIBILITY
6133    pointer operator->() const  {return &__match_;}
6134
6135    regex_iterator& operator++();
6136    _LIBCPP_INLINE_VISIBILITY
6137    regex_iterator operator++(int)
6138    {
6139        regex_iterator __t(*this);
6140        ++(*this);
6141        return __t;
6142    }
6143};
6144
6145template <class _BidirectionalIterator, class _CharT, class _Traits>
6146regex_iterator<_BidirectionalIterator, _CharT, _Traits>::regex_iterator()
6147    : __begin_(), __end_(), __pregex_(nullptr), __flags_(), __match_()
6148{
6149}
6150
6151template <class _BidirectionalIterator, class _CharT, class _Traits>
6152regex_iterator<_BidirectionalIterator, _CharT, _Traits>::
6153    regex_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6154                   const regex_type& __re, regex_constants::match_flag_type __m)
6155    : __begin_(__a),
6156      __end_(__b),
6157      __pregex_(&__re),
6158      __flags_(__m)
6159{
6160    _VSTD::regex_search(__begin_, __end_, __match_, *__pregex_, __flags_);
6161}
6162
6163template <class _BidirectionalIterator, class _CharT, class _Traits>
6164bool
6165regex_iterator<_BidirectionalIterator, _CharT, _Traits>::
6166    operator==(const regex_iterator& __x) const
6167{
6168    if (__match_.empty() && __x.__match_.empty())
6169        return true;
6170    if (__match_.empty() || __x.__match_.empty())
6171        return false;
6172    return __begin_ == __x.__begin_       &&
6173           __end_ == __x.__end_           &&
6174           __pregex_ == __x.__pregex_     &&
6175           __flags_ == __x.__flags_       &&
6176           __match_[0] == __x.__match_[0];
6177}
6178
6179template <class _BidirectionalIterator, class _CharT, class _Traits>
6180regex_iterator<_BidirectionalIterator, _CharT, _Traits>&
6181regex_iterator<_BidirectionalIterator, _CharT, _Traits>::operator++()
6182{
6183    __flags_ |= regex_constants::__no_update_pos;
6184    _BidirectionalIterator __start = __match_[0].second;
6185    if (__match_[0].first == __match_[0].second)
6186    {
6187        if (__start == __end_)
6188        {
6189            __match_ = value_type();
6190            return *this;
6191        }
6192        else if (_VSTD::regex_search(__start, __end_, __match_, *__pregex_,
6193                                    __flags_ | regex_constants::match_not_null |
6194                                    regex_constants::match_continuous))
6195            return *this;
6196        else
6197            ++__start;
6198    }
6199    __flags_ |= regex_constants::match_prev_avail;
6200    if (!_VSTD::regex_search(__start, __end_, __match_, *__pregex_, __flags_))
6201        __match_ = value_type();
6202    return *this;
6203}
6204
6205typedef regex_iterator<const char*>             cregex_iterator;
6206typedef regex_iterator<const wchar_t*>          wcregex_iterator;
6207typedef regex_iterator<string::const_iterator>  sregex_iterator;
6208typedef regex_iterator<wstring::const_iterator> wsregex_iterator;
6209
6210// regex_token_iterator
6211
6212template <class _BidirectionalIterator,
6213          class _CharT = typename iterator_traits<_BidirectionalIterator>::value_type,
6214          class _Traits = regex_traits<_CharT> >
6215class _LIBCPP_TEMPLATE_VIS regex_token_iterator
6216{
6217public:
6218    typedef basic_regex<_CharT, _Traits>      regex_type;
6219    typedef sub_match<_BidirectionalIterator> value_type;
6220    typedef ptrdiff_t                         difference_type;
6221    typedef const value_type*                 pointer;
6222    typedef const value_type&                 reference;
6223    typedef forward_iterator_tag              iterator_category;
6224
6225private:
6226    typedef regex_iterator<_BidirectionalIterator, _CharT, _Traits> _Position;
6227
6228    _Position         __position_;
6229    const value_type* __result_;
6230    value_type        __suffix_;
6231    ptrdiff_t         __n_;
6232    vector<int>       __subs_;
6233
6234public:
6235    regex_token_iterator();
6236    regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6237                         const regex_type& __re, int __submatch = 0,
6238                         regex_constants::match_flag_type __m =
6239                                                regex_constants::match_default);
6240#if _LIBCPP_STD_VER > 11
6241    regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6242                         const regex_type&& __re, int __submatch = 0,
6243                         regex_constants::match_flag_type __m =
6244                                       regex_constants::match_default) = delete;
6245#endif
6246
6247    regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6248                         const regex_type& __re, const vector<int>& __submatches,
6249                         regex_constants::match_flag_type __m =
6250                                                regex_constants::match_default);
6251#if _LIBCPP_STD_VER > 11
6252    regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6253                         const regex_type&& __re, const vector<int>& __submatches,
6254                         regex_constants::match_flag_type __m =
6255                                     regex_constants::match_default) = delete;
6256#endif
6257
6258#ifndef _LIBCPP_CXX03_LANG
6259    regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6260                         const regex_type& __re,
6261                         initializer_list<int> __submatches,
6262                         regex_constants::match_flag_type __m =
6263                                                regex_constants::match_default);
6264
6265#if _LIBCPP_STD_VER > 11
6266    regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6267                         const regex_type&& __re,
6268                         initializer_list<int> __submatches,
6269                         regex_constants::match_flag_type __m =
6270                                       regex_constants::match_default) = delete;
6271#endif
6272#endif  // _LIBCPP_CXX03_LANG
6273    template <size_t _Np>
6274        regex_token_iterator(_BidirectionalIterator __a,
6275                             _BidirectionalIterator __b,
6276                             const regex_type& __re,
6277                             const int (&__submatches)[_Np],
6278                             regex_constants::match_flag_type __m =
6279                                                regex_constants::match_default);
6280#if _LIBCPP_STD_VER > 11
6281    template <std::size_t _Np>
6282        regex_token_iterator(_BidirectionalIterator __a,
6283                             _BidirectionalIterator __b,
6284                             const regex_type&& __re,
6285                             const int (&__submatches)[_Np],
6286                             regex_constants::match_flag_type __m =
6287                                      regex_constants::match_default) = delete;
6288#endif
6289
6290    regex_token_iterator(const regex_token_iterator&);
6291    regex_token_iterator& operator=(const regex_token_iterator&);
6292
6293    bool operator==(const regex_token_iterator& __x) const;
6294    _LIBCPP_INLINE_VISIBILITY
6295    bool operator!=(const regex_token_iterator& __x) const {return !(*this == __x);}
6296
6297    _LIBCPP_INLINE_VISIBILITY
6298    const value_type& operator*() const {return *__result_;}
6299    _LIBCPP_INLINE_VISIBILITY
6300    const value_type* operator->() const {return __result_;}
6301
6302    regex_token_iterator& operator++();
6303    _LIBCPP_INLINE_VISIBILITY
6304    regex_token_iterator operator++(int)
6305    {
6306        regex_token_iterator __t(*this);
6307        ++(*this);
6308        return __t;
6309    }
6310
6311private:
6312    void __init(_BidirectionalIterator __a, _BidirectionalIterator __b);
6313    void __establish_result () {
6314        if (__subs_[__n_] == -1)
6315            __result_ = &__position_->prefix();
6316        else
6317            __result_ = &(*__position_)[__subs_[__n_]];
6318        }
6319};
6320
6321template <class _BidirectionalIterator, class _CharT, class _Traits>
6322regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6323    regex_token_iterator()
6324    : __result_(nullptr),
6325      __suffix_(),
6326      __n_(0)
6327{
6328}
6329
6330template <class _BidirectionalIterator, class _CharT, class _Traits>
6331void
6332regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6333    __init(_BidirectionalIterator __a, _BidirectionalIterator __b)
6334{
6335    if (__position_ != _Position())
6336        __establish_result ();
6337    else if (__subs_[__n_] == -1)
6338    {
6339        __suffix_.matched = true;
6340        __suffix_.first = __a;
6341        __suffix_.second = __b;
6342        __result_ = &__suffix_;
6343    }
6344    else
6345        __result_ = nullptr;
6346}
6347
6348template <class _BidirectionalIterator, class _CharT, class _Traits>
6349regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6350    regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6351                         const regex_type& __re, int __submatch,
6352                         regex_constants::match_flag_type __m)
6353    : __position_(__a, __b, __re, __m),
6354      __n_(0),
6355      __subs_(1, __submatch)
6356{
6357    __init(__a, __b);
6358}
6359
6360template <class _BidirectionalIterator, class _CharT, class _Traits>
6361regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6362    regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6363                         const regex_type& __re, const vector<int>& __submatches,
6364                         regex_constants::match_flag_type __m)
6365    : __position_(__a, __b, __re, __m),
6366      __n_(0),
6367      __subs_(__submatches)
6368{
6369    __init(__a, __b);
6370}
6371
6372#ifndef _LIBCPP_CXX03_LANG
6373
6374template <class _BidirectionalIterator, class _CharT, class _Traits>
6375regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6376    regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6377                         const regex_type& __re,
6378                         initializer_list<int> __submatches,
6379                         regex_constants::match_flag_type __m)
6380    : __position_(__a, __b, __re, __m),
6381      __n_(0),
6382      __subs_(__submatches)
6383{
6384    __init(__a, __b);
6385}
6386
6387#endif  // _LIBCPP_CXX03_LANG
6388
6389template <class _BidirectionalIterator, class _CharT, class _Traits>
6390template <size_t _Np>
6391regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6392    regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6393                             const regex_type& __re,
6394                             const int (&__submatches)[_Np],
6395                             regex_constants::match_flag_type __m)
6396    : __position_(__a, __b, __re, __m),
6397      __n_(0),
6398      __subs_(__submatches, __submatches + _Np)
6399{
6400    __init(__a, __b);
6401}
6402
6403template <class _BidirectionalIterator, class _CharT, class _Traits>
6404regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6405    regex_token_iterator(const regex_token_iterator& __x)
6406    : __position_(__x.__position_),
6407      __result_(__x.__result_),
6408      __suffix_(__x.__suffix_),
6409      __n_(__x.__n_),
6410      __subs_(__x.__subs_)
6411{
6412    if (__x.__result_ == &__x.__suffix_)
6413        __result_ = &__suffix_;
6414    else if ( __result_ != nullptr )
6415        __establish_result ();
6416}
6417
6418template <class _BidirectionalIterator, class _CharT, class _Traits>
6419regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>&
6420regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6421    operator=(const regex_token_iterator& __x)
6422{
6423    if (this != &__x)
6424    {
6425        __position_ = __x.__position_;
6426        if (__x.__result_ == &__x.__suffix_)
6427            __result_ = &__suffix_;
6428        else
6429            __result_ = __x.__result_;
6430        __suffix_ = __x.__suffix_;
6431        __n_ = __x.__n_;
6432        __subs_ = __x.__subs_;
6433
6434        if ( __result_ != nullptr && __result_ != &__suffix_ )
6435            __establish_result();
6436    }
6437    return *this;
6438}
6439
6440template <class _BidirectionalIterator, class _CharT, class _Traits>
6441bool
6442regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6443    operator==(const regex_token_iterator& __x) const
6444{
6445    if (__result_ == nullptr && __x.__result_ == nullptr)
6446        return true;
6447    if (__result_ == &__suffix_ && __x.__result_ == &__x.__suffix_ &&
6448            __suffix_ == __x.__suffix_)
6449        return true;
6450    if (__result_ == nullptr || __x.__result_ == nullptr)
6451        return false;
6452    if (__result_ == &__suffix_ || __x.__result_ == &__x.__suffix_)
6453        return false;
6454    return __position_ == __x.__position_ && __n_ == __x.__n_ &&
6455           __subs_ == __x.__subs_;
6456}
6457
6458template <class _BidirectionalIterator, class _CharT, class _Traits>
6459regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>&
6460regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::operator++()
6461{
6462    _Position __prev = __position_;
6463    if (__result_ == &__suffix_)
6464        __result_ = nullptr;
6465    else if (static_cast<size_t>(__n_ + 1) < __subs_.size())
6466    {
6467        ++__n_;
6468        __establish_result();
6469    }
6470    else
6471    {
6472        __n_ = 0;
6473        ++__position_;
6474        if (__position_ != _Position())
6475            __establish_result();
6476        else
6477        {
6478            if (_VSTD::find(__subs_.begin(), __subs_.end(), -1) != __subs_.end()
6479                && __prev->suffix().length() != 0)
6480            {
6481                __suffix_.matched = true;
6482                __suffix_.first = __prev->suffix().first;
6483                __suffix_.second = __prev->suffix().second;
6484                __result_ = &__suffix_;
6485            }
6486            else
6487                __result_ = nullptr;
6488        }
6489    }
6490    return *this;
6491}
6492
6493typedef regex_token_iterator<const char*>             cregex_token_iterator;
6494typedef regex_token_iterator<const wchar_t*>          wcregex_token_iterator;
6495typedef regex_token_iterator<string::const_iterator>  sregex_token_iterator;
6496typedef regex_token_iterator<wstring::const_iterator> wsregex_token_iterator;
6497
6498// regex_replace
6499
6500template <class _OutputIterator, class _BidirectionalIterator,
6501          class _Traits, class _CharT>
6502_OutputIterator
6503regex_replace(_OutputIterator __output_iter,
6504              _BidirectionalIterator __first, _BidirectionalIterator __last,
6505              const basic_regex<_CharT, _Traits>& __e, const _CharT* __fmt,
6506              regex_constants::match_flag_type __flags = regex_constants::match_default)
6507{
6508    typedef regex_iterator<_BidirectionalIterator, _CharT, _Traits> _Iter;
6509    _Iter __i(__first, __last, __e, __flags);
6510    _Iter __eof;
6511    if (__i == __eof)
6512    {
6513        if (!(__flags & regex_constants::format_no_copy))
6514            __output_iter = _VSTD::copy(__first, __last, __output_iter);
6515    }
6516    else
6517    {
6518        sub_match<_BidirectionalIterator> __lm;
6519        for (size_t __len = char_traits<_CharT>::length(__fmt); __i != __eof; ++__i)
6520        {
6521            if (!(__flags & regex_constants::format_no_copy))
6522                __output_iter = _VSTD::copy(__i->prefix().first, __i->prefix().second, __output_iter);
6523            __output_iter = __i->format(__output_iter, __fmt, __fmt + __len, __flags);
6524            __lm = __i->suffix();
6525            if (__flags & regex_constants::format_first_only)
6526                break;
6527        }
6528        if (!(__flags & regex_constants::format_no_copy))
6529            __output_iter = _VSTD::copy(__lm.first, __lm.second, __output_iter);
6530    }
6531    return __output_iter;
6532}
6533
6534template <class _OutputIterator, class _BidirectionalIterator,
6535          class _Traits, class _CharT, class _ST, class _SA>
6536inline _LIBCPP_INLINE_VISIBILITY
6537_OutputIterator
6538regex_replace(_OutputIterator __output_iter,
6539              _BidirectionalIterator __first, _BidirectionalIterator __last,
6540              const basic_regex<_CharT, _Traits>& __e,
6541              const basic_string<_CharT, _ST, _SA>& __fmt,
6542              regex_constants::match_flag_type __flags = regex_constants::match_default)
6543{
6544    return _VSTD::regex_replace(__output_iter, __first, __last, __e, __fmt.c_str(), __flags);
6545}
6546
6547template <class _Traits, class _CharT, class _ST, class _SA, class _FST,
6548          class _FSA>
6549inline _LIBCPP_INLINE_VISIBILITY
6550basic_string<_CharT, _ST, _SA>
6551regex_replace(const basic_string<_CharT, _ST, _SA>& __s,
6552              const basic_regex<_CharT, _Traits>& __e,
6553              const basic_string<_CharT, _FST, _FSA>& __fmt,
6554              regex_constants::match_flag_type __flags = regex_constants::match_default)
6555{
6556    basic_string<_CharT, _ST, _SA> __r;
6557    _VSTD::regex_replace(back_inserter(__r), __s.begin(), __s.end(), __e,
6558                        __fmt.c_str(), __flags);
6559    return __r;
6560}
6561
6562template <class _Traits, class _CharT, class _ST, class _SA>
6563inline _LIBCPP_INLINE_VISIBILITY
6564basic_string<_CharT, _ST, _SA>
6565regex_replace(const basic_string<_CharT, _ST, _SA>& __s,
6566              const basic_regex<_CharT, _Traits>& __e, const _CharT* __fmt,
6567              regex_constants::match_flag_type __flags = regex_constants::match_default)
6568{
6569    basic_string<_CharT, _ST, _SA> __r;
6570    _VSTD::regex_replace(back_inserter(__r), __s.begin(), __s.end(), __e,
6571                        __fmt, __flags);
6572    return __r;
6573}
6574
6575template <class _Traits, class _CharT, class _ST, class _SA>
6576inline _LIBCPP_INLINE_VISIBILITY
6577basic_string<_CharT>
6578regex_replace(const _CharT* __s,
6579              const basic_regex<_CharT, _Traits>& __e,
6580              const basic_string<_CharT, _ST, _SA>& __fmt,
6581              regex_constants::match_flag_type __flags = regex_constants::match_default)
6582{
6583    basic_string<_CharT> __r;
6584    _VSTD::regex_replace(back_inserter(__r), __s,
6585                        __s + char_traits<_CharT>::length(__s), __e,
6586                        __fmt.c_str(), __flags);
6587    return __r;
6588}
6589
6590template <class _Traits, class _CharT>
6591inline _LIBCPP_INLINE_VISIBILITY
6592basic_string<_CharT>
6593regex_replace(const _CharT* __s,
6594              const basic_regex<_CharT, _Traits>& __e,
6595              const _CharT* __fmt,
6596              regex_constants::match_flag_type __flags = regex_constants::match_default)
6597{
6598    basic_string<_CharT> __r;
6599    _VSTD::regex_replace(back_inserter(__r), __s,
6600                        __s + char_traits<_CharT>::length(__s), __e,
6601                        __fmt, __flags);
6602    return __r;
6603}
6604
6605_LIBCPP_END_NAMESPACE_STD
6606
6607_LIBCPP_POP_MACROS
6608
6609#endif  // _LIBCPP_REGEX
6610