xref: /llvm-project-15.0.7/libcxx/include/regex (revision bacf751a)
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
195template<class ForwardIterator>
196basic_regex(ForwardIterator, ForwardIterator,
197            regex_constants::syntax_option_type = regex_constants::ECMAScript)
198    -> basic_regex<typename iterator_traits<ForwardIterator>::value_type>; // C++17
199
200typedef basic_regex<char>    regex;
201typedef basic_regex<wchar_t> wregex;
202
203template <class charT, class traits>
204    void swap(basic_regex<charT, traits>& e1, basic_regex<charT, traits>& e2);
205
206template <class BidirectionalIterator>
207class sub_match
208    : public pair<BidirectionalIterator, BidirectionalIterator>
209{
210public:
211    typedef typename iterator_traits<BidirectionalIterator>::value_type value_type;
212    typedef typename iterator_traits<BidirectionalIterator>::difference_type difference_type;
213    typedef BidirectionalIterator                                      iterator;
214    typedef basic_string<value_type>                                string_type;
215
216    bool matched;
217
218    constexpr sub_match();
219
220    difference_type length() const;
221    operator string_type() const;
222    string_type str() const;
223
224    int compare(const sub_match& s) const;
225    int compare(const string_type& s) const;
226    int compare(const value_type* s) const;
227};
228
229typedef sub_match<const char*>             csub_match;
230typedef sub_match<const wchar_t*>          wcsub_match;
231typedef sub_match<string::const_iterator>  ssub_match;
232typedef sub_match<wstring::const_iterator> wssub_match;
233
234template <class BiIter>
235    bool
236    operator==(const sub_match<BiIter>& lhs, const sub_match<BiIter>& rhs);
237
238template <class BiIter>
239    bool
240    operator!=(const sub_match<BiIter>& lhs, const sub_match<BiIter>& rhs);
241
242template <class BiIter>
243    bool
244    operator<(const sub_match<BiIter>& lhs, const sub_match<BiIter>& rhs);
245
246template <class BiIter>
247    bool
248    operator<=(const sub_match<BiIter>& lhs, const sub_match<BiIter>& rhs);
249
250template <class BiIter>
251    bool
252    operator>=(const sub_match<BiIter>& lhs, const sub_match<BiIter>& rhs);
253
254template <class BiIter>
255    bool
256    operator>(const sub_match<BiIter>& lhs, 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
275    operator>(const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& lhs,
276              const sub_match<BiIter>& rhs);
277
278template <class BiIter, class ST, class SA>
279    bool 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 basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& lhs,
285               const sub_match<BiIter>& 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
299    operator<(const sub_match<BiIter>& lhs,
300              const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& rhs);
301
302template <class BiIter, class ST, class SA>
303    bool 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, class ST, class SA>
312    bool
313    operator<=(const sub_match<BiIter>& lhs,
314               const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& 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<=(typename iterator_traits<BiIter>::value_type const* lhs,
344               const sub_match<BiIter>& 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<=(const sub_match<BiIter>& lhs,
374               typename iterator_traits<BiIter>::value_type const* 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<=(typename iterator_traits<BiIter>::value_type const& lhs,
404               const sub_match<BiIter>& 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 BiIter>
432    bool
433    operator<=(const sub_match<BiIter>& lhs,
434               typename iterator_traits<BiIter>::value_type const& rhs);
435
436template <class charT, class ST, class BiIter>
437    basic_ostream<charT, ST>&
438    operator<<(basic_ostream<charT, ST>& os, const sub_match<BiIter>& m);
439
440template <class BidirectionalIterator,
441          class Allocator = allocator<sub_match<BidirectionalIterator>>>
442class match_results
443{
444public:
445    typedef sub_match<BidirectionalIterator>                  value_type;
446    typedef const value_type&                                 const_reference;
447    typedef value_type&                                       reference;
448    typedef /implementation-defined/                          const_iterator;
449    typedef const_iterator                                    iterator;
450    typedef typename iterator_traits<BidirectionalIterator>::difference_type difference_type;
451    typedef typename allocator_traits<Allocator>::size_type   size_type;
452    typedef Allocator                                         allocator_type;
453    typedef typename iterator_traits<BidirectionalIterator>::value_type char_type;
454    typedef basic_string<char_type>                           string_type;
455
456    // construct/copy/destroy:
457    explicit match_results(const Allocator& a = Allocator());
458    match_results(const match_results& m);
459    match_results(match_results&& m) noexcept;
460    match_results& operator=(const match_results& m);
461    match_results& operator=(match_results&& m);
462    ~match_results();
463
464    bool ready() const;
465
466    // size:
467    size_type size() const;
468    size_type max_size() const;
469    bool empty() const;
470
471    // element access:
472    difference_type length(size_type sub = 0) const;
473    difference_type position(size_type sub = 0) const;
474    string_type str(size_type sub = 0) const;
475    const_reference operator[](size_type n) const;
476
477    const_reference prefix() const;
478    const_reference suffix() const;
479
480    const_iterator begin() const;
481    const_iterator end() const;
482    const_iterator cbegin() const;
483    const_iterator cend() const;
484
485    // format:
486    template <class OutputIter>
487        OutputIter
488        format(OutputIter out, const char_type* fmt_first,
489               const char_type* fmt_last,
490               regex_constants::match_flag_type flags = regex_constants::format_default) const;
491    template <class OutputIter, class ST, class SA>
492        OutputIter
493        format(OutputIter out, const basic_string<char_type, ST, SA>& fmt,
494               regex_constants::match_flag_type flags = regex_constants::format_default) const;
495    template <class ST, class SA>
496        basic_string<char_type, ST, SA>
497        format(const basic_string<char_type, ST, SA>& fmt,
498               regex_constants::match_flag_type flags = regex_constants::format_default) const;
499    string_type
500        format(const char_type* fmt,
501               regex_constants::match_flag_type flags = regex_constants::format_default) const;
502
503    // allocator:
504    allocator_type get_allocator() const;
505
506    // swap:
507    void swap(match_results& that);
508};
509
510typedef match_results<const char*>             cmatch;
511typedef match_results<const wchar_t*>          wcmatch;
512typedef match_results<string::const_iterator>  smatch;
513typedef match_results<wstring::const_iterator> wsmatch;
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    bool
522    operator!=(const match_results<BidirectionalIterator, Allocator>& m1,
523               const match_results<BidirectionalIterator, Allocator>& m2);
524
525template <class BidirectionalIterator, class Allocator>
526    void
527    swap(match_results<BidirectionalIterator, Allocator>& m1,
528         match_results<BidirectionalIterator, Allocator>& m2);
529
530template <class BidirectionalIterator, class Allocator, class charT, class traits>
531    bool
532    regex_match(BidirectionalIterator first, BidirectionalIterator last,
533                match_results<BidirectionalIterator, Allocator>& m,
534                const basic_regex<charT, traits>& e,
535                regex_constants::match_flag_type flags = regex_constants::match_default);
536
537template <class BidirectionalIterator, class charT, class traits>
538    bool
539    regex_match(BidirectionalIterator first, BidirectionalIterator last,
540                const basic_regex<charT, traits>& e,
541                regex_constants::match_flag_type flags = regex_constants::match_default);
542
543template <class charT, class Allocator, class traits>
544    bool
545    regex_match(const charT* str, match_results<const charT*, Allocator>& m,
546                const basic_regex<charT, traits>& e,
547                regex_constants::match_flag_type flags = regex_constants::match_default);
548
549template <class ST, class SA, class Allocator, class charT, class traits>
550    bool
551    regex_match(const basic_string<charT, ST, SA>& s,
552                match_results<typename basic_string<charT, ST, SA>::const_iterator, Allocator>& m,
553                const basic_regex<charT, traits>& e,
554                regex_constants::match_flag_type flags = regex_constants::match_default);
555
556template <class ST, class SA, class Allocator, class charT, class traits>
557    bool
558    regex_match(const basic_string<charT, ST, SA>&& s,
559                match_results<typename basic_string<charT, ST, SA>::const_iterator, Allocator>& m,
560                const basic_regex<charT, traits>& e,
561                regex_constants::match_flag_type flags = regex_constants::match_default) = delete; // C++14
562
563template <class charT, class traits>
564    bool
565    regex_match(const charT* str, const basic_regex<charT, traits>& e,
566                regex_constants::match_flag_type flags = regex_constants::match_default);
567
568template <class ST, class SA, class charT, class traits>
569    bool
570    regex_match(const basic_string<charT, ST, SA>& s,
571                const basic_regex<charT, traits>& e,
572                regex_constants::match_flag_type flags = regex_constants::match_default);
573
574template <class BidirectionalIterator, class Allocator, class charT, class traits>
575    bool
576    regex_search(BidirectionalIterator first, BidirectionalIterator last,
577                 match_results<BidirectionalIterator, Allocator>& m,
578                 const basic_regex<charT, traits>& e,
579                 regex_constants::match_flag_type flags = regex_constants::match_default);
580
581template <class BidirectionalIterator, class charT, class traits>
582    bool
583    regex_search(BidirectionalIterator first, BidirectionalIterator last,
584                 const basic_regex<charT, traits>& e,
585                 regex_constants::match_flag_type flags = regex_constants::match_default);
586
587template <class charT, class Allocator, class traits>
588    bool
589    regex_search(const charT* str, match_results<const charT*, Allocator>& m,
590                 const basic_regex<charT, traits>& e,
591                 regex_constants::match_flag_type flags = regex_constants::match_default);
592
593template <class charT, class traits>
594    bool
595    regex_search(const charT* str, const basic_regex<charT, traits>& e,
596                 regex_constants::match_flag_type flags = regex_constants::match_default);
597
598template <class ST, class SA, class charT, class traits>
599    bool
600    regex_search(const basic_string<charT, ST, SA>& s,
601                 const basic_regex<charT, traits>& e,
602                 regex_constants::match_flag_type flags = regex_constants::match_default);
603
604template <class ST, class SA, class Allocator, class charT, class traits>
605    bool
606    regex_search(const basic_string<charT, ST, SA>& s,
607                 match_results<typename basic_string<charT, ST, SA>::const_iterator, Allocator>& m,
608                 const basic_regex<charT, traits>& e,
609                 regex_constants::match_flag_type flags = regex_constants::match_default);
610
611template <class ST, class SA, class Allocator, class charT, class traits>
612    bool
613    regex_search(const basic_string<charT, ST, SA>&& s,
614                 match_results<typename basic_string<charT, ST, SA>::const_iterator, Allocator>& m,
615                 const basic_regex<charT, traits>& e,
616                 regex_constants::match_flag_type flags = regex_constants::match_default) = delete; // C++14
617
618template <class OutputIterator, class BidirectionalIterator,
619          class traits, class charT, class ST, class SA>
620    OutputIterator
621    regex_replace(OutputIterator out,
622                  BidirectionalIterator first, BidirectionalIterator last,
623                  const basic_regex<charT, traits>& e,
624                  const basic_string<charT, ST, SA>& fmt,
625                  regex_constants::match_flag_type flags = regex_constants::match_default);
626
627template <class OutputIterator, class BidirectionalIterator,
628          class traits, class charT>
629    OutputIterator
630    regex_replace(OutputIterator out,
631                  BidirectionalIterator first, BidirectionalIterator last,
632                  const basic_regex<charT, traits>& e, const charT* fmt,
633                  regex_constants::match_flag_type flags = regex_constants::match_default);
634
635template <class traits, class charT, class ST, class SA, class FST, class FSA>>
636    basic_string<charT, ST, SA>
637    regex_replace(const basic_string<charT, ST, SA>& s,
638                  const basic_regex<charT, traits>& e,
639                  const basic_string<charT, FST, FSA>& fmt,
640                  regex_constants::match_flag_type flags = regex_constants::match_default);
641
642template <class traits, class charT, class ST, class SA>
643    basic_string<charT, ST, SA>
644    regex_replace(const basic_string<charT, ST, SA>& s,
645                  const basic_regex<charT, traits>& e, const charT* fmt,
646                  regex_constants::match_flag_type flags = regex_constants::match_default);
647
648template <class traits, class charT, class ST, class SA>
649    basic_string<charT>
650    regex_replace(const charT* s,
651                  const basic_regex<charT, traits>& e,
652                  const basic_string<charT, ST, SA>& fmt,
653                  regex_constants::match_flag_type flags = regex_constants::match_default);
654
655template <class traits, class charT>
656    basic_string<charT>
657    regex_replace(const charT* s,
658                  const basic_regex<charT, traits>& e,
659                  const charT* fmt,
660                  regex_constants::match_flag_type flags = regex_constants::match_default);
661
662template <class BidirectionalIterator,
663          class charT = typename iterator_traits< BidirectionalIterator>::value_type,
664          class traits = regex_traits<charT>>
665class regex_iterator
666{
667public:
668    typedef basic_regex<charT, traits>           regex_type;
669    typedef match_results<BidirectionalIterator> value_type;
670    typedef ptrdiff_t                            difference_type;
671    typedef const value_type*                    pointer;
672    typedef const value_type&                    reference;
673    typedef forward_iterator_tag                 iterator_category;
674
675    regex_iterator();
676    regex_iterator(BidirectionalIterator a, BidirectionalIterator b,
677                   const regex_type& re,
678                   regex_constants::match_flag_type m = regex_constants::match_default);
679    regex_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
680                   const regex_type&& __re,
681                   regex_constants::match_flag_type __m
682                                     = regex_constants::match_default) = delete; // C++14
683    regex_iterator(const regex_iterator&);
684    regex_iterator& operator=(const regex_iterator&);
685
686    bool operator==(const regex_iterator&) const;
687    bool operator!=(const regex_iterator&) const;
688
689    const value_type& operator*() const;
690    const value_type* operator->() const;
691
692    regex_iterator& operator++();
693    regex_iterator operator++(int);
694};
695
696typedef regex_iterator<const char*>             cregex_iterator;
697typedef regex_iterator<const wchar_t*>          wcregex_iterator;
698typedef regex_iterator<string::const_iterator>  sregex_iterator;
699typedef regex_iterator<wstring::const_iterator> wsregex_iterator;
700
701template <class BidirectionalIterator,
702          class charT = typename iterator_traits< BidirectionalIterator>::value_type,
703          class traits = regex_traits<charT>>
704class regex_token_iterator
705{
706public:
707    typedef basic_regex<charT, traits>       regex_type;
708    typedef sub_match<BidirectionalIterator> value_type;
709    typedef ptrdiff_t                        difference_type;
710    typedef const value_type*                pointer;
711    typedef const value_type&                reference;
712    typedef forward_iterator_tag             iterator_category;
713
714    regex_token_iterator();
715    regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b,
716                         const regex_type& re, int submatch = 0,
717                         regex_constants::match_flag_type m = regex_constants::match_default);
718    regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b,
719                         const regex_type&& re, int submatch = 0,
720                         regex_constants::match_flag_type m = regex_constants::match_default) = delete; // C++14
721    regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b,
722                         const regex_type& re, const vector<int>& submatches,
723                         regex_constants::match_flag_type m = regex_constants::match_default);
724    regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b,
725                         const regex_type&& re, const vector<int>& submatches,
726                         regex_constants::match_flag_type m = regex_constants::match_default) = delete; // C++14
727    regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b,
728                         const regex_type& re, initializer_list<int> submatches,
729                         regex_constants::match_flag_type m = regex_constants::match_default);
730    regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b,
731                         const regex_type&& re, initializer_list<int> submatches,
732                         regex_constants::match_flag_type m = regex_constants::match_default) = delete; // C++14
733    template <size_t N>
734        regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b,
735                             const regex_type& re, const int (&submatches)[N],
736                             regex_constants::match_flag_type m = regex_constants::match_default);
737    template <size_t N>
738        regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b,
739                             const regex_type& re, const int (&submatches)[N],
740                             regex_constants::match_flag_type m = regex_constants::match_default) = delete // C++14;
741    regex_token_iterator(const regex_token_iterator&);
742    regex_token_iterator& operator=(const regex_token_iterator&);
743
744    bool operator==(const regex_token_iterator&) const;
745    bool operator!=(const regex_token_iterator&) const;
746
747    const value_type& operator*() const;
748    const value_type* operator->() const;
749
750    regex_token_iterator& operator++();
751    regex_token_iterator operator++(int);
752};
753
754typedef regex_token_iterator<const char*>             cregex_token_iterator;
755typedef regex_token_iterator<const wchar_t*>          wcregex_token_iterator;
756typedef regex_token_iterator<string::const_iterator>  sregex_token_iterator;
757typedef regex_token_iterator<wstring::const_iterator> wsregex_token_iterator;
758
759} // std
760*/
761
762#include <__config>
763#include <stdexcept>
764#include <__locale>
765#include <initializer_list>
766#include <utility>
767#include <iterator>
768#include <string>
769#include <memory>
770#include <vector>
771#include <deque>
772#include <version>
773
774#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
775#pragma GCC system_header
776#endif
777
778_LIBCPP_PUSH_MACROS
779#include <__undef_macros>
780
781
782#define _LIBCPP_REGEX_COMPLEXITY_FACTOR 4096
783
784_LIBCPP_BEGIN_NAMESPACE_STD
785
786namespace regex_constants
787{
788
789// syntax_option_type
790
791enum syntax_option_type
792{
793    icase      = 1 << 0,
794    nosubs     = 1 << 1,
795    optimize   = 1 << 2,
796    collate    = 1 << 3,
797    ECMAScript = 0,
798    basic      = 1 << 4,
799    extended   = 1 << 5,
800    awk        = 1 << 6,
801    grep       = 1 << 7,
802    egrep      = 1 << 8
803};
804
805inline _LIBCPP_INLINE_VISIBILITY
806_LIBCPP_CONSTEXPR
807syntax_option_type
808operator~(syntax_option_type __x)
809{
810    return syntax_option_type(~int(__x) & 0x1FF);
811}
812
813inline _LIBCPP_INLINE_VISIBILITY
814_LIBCPP_CONSTEXPR
815syntax_option_type
816operator&(syntax_option_type __x, syntax_option_type __y)
817{
818    return syntax_option_type(int(__x) & int(__y));
819}
820
821inline _LIBCPP_INLINE_VISIBILITY
822_LIBCPP_CONSTEXPR
823syntax_option_type
824operator|(syntax_option_type __x, syntax_option_type __y)
825{
826    return syntax_option_type(int(__x) | int(__y));
827}
828
829inline _LIBCPP_INLINE_VISIBILITY
830_LIBCPP_CONSTEXPR
831syntax_option_type
832operator^(syntax_option_type __x, syntax_option_type __y)
833{
834    return syntax_option_type(int(__x) ^ int(__y));
835}
836
837inline _LIBCPP_INLINE_VISIBILITY
838syntax_option_type&
839operator&=(syntax_option_type& __x, syntax_option_type __y)
840{
841    __x = __x & __y;
842    return __x;
843}
844
845inline _LIBCPP_INLINE_VISIBILITY
846syntax_option_type&
847operator|=(syntax_option_type& __x, syntax_option_type __y)
848{
849    __x = __x | __y;
850    return __x;
851}
852
853inline _LIBCPP_INLINE_VISIBILITY
854syntax_option_type&
855operator^=(syntax_option_type& __x, syntax_option_type __y)
856{
857    __x = __x ^ __y;
858    return __x;
859}
860
861// match_flag_type
862
863enum match_flag_type
864{
865    match_default     = 0,
866    match_not_bol     = 1 << 0,
867    match_not_eol     = 1 << 1,
868    match_not_bow     = 1 << 2,
869    match_not_eow     = 1 << 3,
870    match_any         = 1 << 4,
871    match_not_null    = 1 << 5,
872    match_continuous  = 1 << 6,
873    match_prev_avail  = 1 << 7,
874    format_default    = 0,
875    format_sed        = 1 << 8,
876    format_no_copy    = 1 << 9,
877    format_first_only = 1 << 10,
878    __no_update_pos   = 1 << 11,
879    __full_match      = 1 << 12
880};
881
882inline _LIBCPP_INLINE_VISIBILITY
883_LIBCPP_CONSTEXPR
884match_flag_type
885operator~(match_flag_type __x)
886{
887    return match_flag_type(~int(__x) & 0x0FFF);
888}
889
890inline _LIBCPP_INLINE_VISIBILITY
891_LIBCPP_CONSTEXPR
892match_flag_type
893operator&(match_flag_type __x, match_flag_type __y)
894{
895    return match_flag_type(int(__x) & int(__y));
896}
897
898inline _LIBCPP_INLINE_VISIBILITY
899_LIBCPP_CONSTEXPR
900match_flag_type
901operator|(match_flag_type __x, match_flag_type __y)
902{
903    return match_flag_type(int(__x) | int(__y));
904}
905
906inline _LIBCPP_INLINE_VISIBILITY
907_LIBCPP_CONSTEXPR
908match_flag_type
909operator^(match_flag_type __x, match_flag_type __y)
910{
911    return match_flag_type(int(__x) ^ int(__y));
912}
913
914inline _LIBCPP_INLINE_VISIBILITY
915match_flag_type&
916operator&=(match_flag_type& __x, match_flag_type __y)
917{
918    __x = __x & __y;
919    return __x;
920}
921
922inline _LIBCPP_INLINE_VISIBILITY
923match_flag_type&
924operator|=(match_flag_type& __x, match_flag_type __y)
925{
926    __x = __x | __y;
927    return __x;
928}
929
930inline _LIBCPP_INLINE_VISIBILITY
931match_flag_type&
932operator^=(match_flag_type& __x, match_flag_type __y)
933{
934    __x = __x ^ __y;
935    return __x;
936}
937
938enum error_type
939{
940    error_collate = 1,
941    error_ctype,
942    error_escape,
943    error_backref,
944    error_brack,
945    error_paren,
946    error_brace,
947    error_badbrace,
948    error_range,
949    error_space,
950    error_badrepeat,
951    error_complexity,
952    error_stack,
953    __re_err_grammar,
954    __re_err_empty,
955    __re_err_unknown
956};
957
958}  // regex_constants
959
960class _LIBCPP_EXCEPTION_ABI regex_error
961    : public runtime_error
962{
963    regex_constants::error_type __code_;
964public:
965    explicit regex_error(regex_constants::error_type __ecode);
966    virtual ~regex_error() throw();
967     _LIBCPP_INLINE_VISIBILITY
968    regex_constants::error_type code() const {return __code_;}
969};
970
971template <regex_constants::error_type _Ev>
972_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
973void __throw_regex_error()
974{
975#ifndef _LIBCPP_NO_EXCEPTIONS
976    throw regex_error(_Ev);
977#else
978    _VSTD::abort();
979#endif
980}
981
982template <class _CharT>
983struct _LIBCPP_TEMPLATE_VIS regex_traits
984{
985public:
986    typedef _CharT                  char_type;
987    typedef basic_string<char_type> string_type;
988    typedef locale                  locale_type;
989    typedef ctype_base::mask        char_class_type;
990
991#if 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        // When there's at least one of __neg_chars_ and __neg_mask_, the set
2419        // of "__found" chars is
2420        //   union(complement(union(__neg_chars_, __neg_mask_)),
2421        //         other cases...)
2422        //
2423        // It doesn't make sense to check this when there are no __neg_chars_
2424        // and no __neg_mask_.
2425        if (!(__neg_mask_ == 0 && __neg_chars_.empty()))
2426        {
2427            const bool __in_neg_mask = __traits_.isctype(__ch, __neg_mask_);
2428          const bool __in_neg_chars =
2429              std::find(__neg_chars_.begin(), __neg_chars_.end(), __ch) !=
2430              __neg_chars_.end();
2431          if (!(__in_neg_mask || __in_neg_chars))
2432          {
2433            __found = true;
2434            goto __exit;
2435          }
2436        }
2437        if (!__ranges_.empty())
2438        {
2439            string_type __s2 = __collate_ ?
2440                                   __traits_.transform(&__ch, &__ch + 1) :
2441                                   string_type(1, __ch);
2442            for (size_t __i = 0; __i < __ranges_.size(); ++__i)
2443            {
2444                if (__ranges_[__i].first <= __s2 && __s2 <= __ranges_[__i].second)
2445                {
2446                    __found = true;
2447                    goto __exit;
2448                }
2449            }
2450        }
2451        if (!__equivalences_.empty())
2452        {
2453            string_type __s2 = __traits_.transform_primary(&__ch, &__ch + 1);
2454            for (size_t __i = 0; __i < __equivalences_.size(); ++__i)
2455            {
2456                if (__s2 == __equivalences_[__i])
2457                {
2458                    __found = true;
2459                    goto __exit;
2460                }
2461            }
2462        }
2463        if (__traits_.isctype(__ch, __mask_))
2464        {
2465            __found = true;
2466            goto __exit;
2467        }
2468    }
2469    else
2470        __found = __negate_;  // force reject
2471__exit:
2472    if (__found != __negate_)
2473    {
2474        __s.__do_ = __state::__accept_and_consume;
2475        __s.__current_ += __consumed;
2476        __s.__node_ = this->first();
2477    }
2478    else
2479    {
2480        __s.__do_ = __state::__reject;
2481        __s.__node_ = nullptr;
2482    }
2483}
2484
2485template <class _CharT, class _Traits> class __lookahead;
2486
2487template <class _CharT, class _Traits = regex_traits<_CharT> >
2488class _LIBCPP_TEMPLATE_VIS basic_regex
2489{
2490public:
2491    // types:
2492    typedef _CharT                              value_type;
2493    typedef _Traits                             traits_type;
2494    typedef typename _Traits::string_type       string_type;
2495    typedef regex_constants::syntax_option_type flag_type;
2496    typedef typename _Traits::locale_type       locale_type;
2497
2498private:
2499    _Traits   __traits_;
2500    flag_type __flags_;
2501    unsigned __marked_count_;
2502    unsigned __loop_count_;
2503    int __open_count_;
2504    shared_ptr<__empty_state<_CharT> > __start_;
2505    __owns_one_state<_CharT>* __end_;
2506
2507    typedef _VSTD::__state<_CharT> __state;
2508    typedef _VSTD::__node<_CharT> __node;
2509
2510public:
2511    // constants:
2512    static const regex_constants::syntax_option_type icase = regex_constants::icase;
2513    static const regex_constants::syntax_option_type nosubs = regex_constants::nosubs;
2514    static const regex_constants::syntax_option_type optimize = regex_constants::optimize;
2515    static const regex_constants::syntax_option_type collate = regex_constants::collate;
2516    static const regex_constants::syntax_option_type ECMAScript = regex_constants::ECMAScript;
2517    static const regex_constants::syntax_option_type basic = regex_constants::basic;
2518    static const regex_constants::syntax_option_type extended = regex_constants::extended;
2519    static const regex_constants::syntax_option_type awk = regex_constants::awk;
2520    static const regex_constants::syntax_option_type grep = regex_constants::grep;
2521    static const regex_constants::syntax_option_type egrep = regex_constants::egrep;
2522
2523    // construct/copy/destroy:
2524    _LIBCPP_INLINE_VISIBILITY
2525    basic_regex()
2526        : __flags_(), __marked_count_(0), __loop_count_(0), __open_count_(0),
2527          __end_(0)
2528        {}
2529    _LIBCPP_INLINE_VISIBILITY
2530    explicit basic_regex(const value_type* __p, flag_type __f = regex_constants::ECMAScript)
2531        : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
2532          __end_(0)
2533        {__parse(__p, __p + __traits_.length(__p));}
2534    _LIBCPP_INLINE_VISIBILITY
2535    basic_regex(const value_type* __p, size_t __len, flag_type __f = regex_constants::ECMAScript)
2536        : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
2537          __end_(0)
2538        {__parse(__p, __p + __len);}
2539//     basic_regex(const basic_regex&) = default;
2540//     basic_regex(basic_regex&&) = default;
2541    template <class _ST, class _SA>
2542        _LIBCPP_INLINE_VISIBILITY
2543        explicit basic_regex(const basic_string<value_type, _ST, _SA>& __p,
2544                             flag_type __f = regex_constants::ECMAScript)
2545        : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
2546          __end_(0)
2547        {__parse(__p.begin(), __p.end());}
2548    template <class _ForwardIterator>
2549        _LIBCPP_INLINE_VISIBILITY
2550        basic_regex(_ForwardIterator __first, _ForwardIterator __last,
2551                    flag_type __f = regex_constants::ECMAScript)
2552        : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
2553          __end_(0)
2554        {__parse(__first, __last);}
2555#ifndef _LIBCPP_CXX03_LANG
2556    _LIBCPP_INLINE_VISIBILITY
2557    basic_regex(initializer_list<value_type> __il,
2558                flag_type __f = regex_constants::ECMAScript)
2559        : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
2560          __end_(0)
2561        {__parse(__il.begin(), __il.end());}
2562#endif  // _LIBCPP_CXX03_LANG
2563
2564//    ~basic_regex() = default;
2565
2566//     basic_regex& operator=(const basic_regex&) = default;
2567//     basic_regex& operator=(basic_regex&&) = default;
2568    _LIBCPP_INLINE_VISIBILITY
2569    basic_regex& operator=(const value_type* __p)
2570        {return assign(__p);}
2571#ifndef _LIBCPP_CXX03_LANG
2572    _LIBCPP_INLINE_VISIBILITY
2573    basic_regex& operator=(initializer_list<value_type> __il)
2574        {return assign(__il);}
2575#endif  // _LIBCPP_CXX03_LANG
2576    template <class _ST, class _SA>
2577        _LIBCPP_INLINE_VISIBILITY
2578        basic_regex& operator=(const basic_string<value_type, _ST, _SA>& __p)
2579        {return assign(__p);}
2580
2581    // assign:
2582    _LIBCPP_INLINE_VISIBILITY
2583    basic_regex& assign(const basic_regex& __that)
2584        {return *this = __that;}
2585#ifndef _LIBCPP_CXX03_LANG
2586    _LIBCPP_INLINE_VISIBILITY
2587    basic_regex& assign(basic_regex&& __that) _NOEXCEPT
2588        {return *this = _VSTD::move(__that);}
2589#endif
2590    _LIBCPP_INLINE_VISIBILITY
2591    basic_regex& assign(const value_type* __p, flag_type __f = regex_constants::ECMAScript)
2592        {return assign(__p, __p + __traits_.length(__p), __f);}
2593    _LIBCPP_INLINE_VISIBILITY
2594    basic_regex& assign(const value_type* __p, size_t __len, flag_type __f)
2595        {return assign(__p, __p + __len, __f);}
2596    template <class _ST, class _SA>
2597        _LIBCPP_INLINE_VISIBILITY
2598        basic_regex& assign(const basic_string<value_type, _ST, _SA>& __s,
2599                            flag_type __f = regex_constants::ECMAScript)
2600            {return assign(__s.begin(), __s.end(), __f);}
2601
2602    template <class _InputIterator>
2603        _LIBCPP_INLINE_VISIBILITY
2604        typename enable_if
2605        <
2606             __is_input_iterator  <_InputIterator>::value &&
2607            !__is_forward_iterator<_InputIterator>::value,
2608            basic_regex&
2609        >::type
2610        assign(_InputIterator __first, _InputIterator __last,
2611                            flag_type __f = regex_constants::ECMAScript)
2612        {
2613            basic_string<_CharT> __t(__first, __last);
2614            return assign(__t.begin(), __t.end(), __f);
2615        }
2616
2617private:
2618    _LIBCPP_INLINE_VISIBILITY
2619    void __member_init(flag_type __f)
2620    {
2621        __flags_ = __f;
2622        __marked_count_ = 0;
2623        __loop_count_ = 0;
2624        __open_count_ = 0;
2625        __end_ = nullptr;
2626    }
2627public:
2628
2629    template <class _ForwardIterator>
2630        _LIBCPP_INLINE_VISIBILITY
2631        typename enable_if
2632        <
2633            __is_forward_iterator<_ForwardIterator>::value,
2634            basic_regex&
2635        >::type
2636        assign(_ForwardIterator __first, _ForwardIterator __last,
2637                            flag_type __f = regex_constants::ECMAScript)
2638        {
2639            return assign(basic_regex(__first, __last, __f));
2640        }
2641
2642#ifndef _LIBCPP_CXX03_LANG
2643
2644    _LIBCPP_INLINE_VISIBILITY
2645    basic_regex& assign(initializer_list<value_type> __il,
2646                        flag_type __f = regex_constants::ECMAScript)
2647        {return assign(__il.begin(), __il.end(), __f);}
2648
2649#endif  // _LIBCPP_CXX03_LANG
2650
2651    // const operations:
2652    _LIBCPP_INLINE_VISIBILITY
2653    unsigned mark_count() const {return __marked_count_;}
2654    _LIBCPP_INLINE_VISIBILITY
2655    flag_type flags() const {return __flags_;}
2656
2657    // locale:
2658    _LIBCPP_INLINE_VISIBILITY
2659    locale_type imbue(locale_type __loc)
2660    {
2661        __member_init(ECMAScript);
2662        __start_.reset();
2663        return __traits_.imbue(__loc);
2664    }
2665    _LIBCPP_INLINE_VISIBILITY
2666    locale_type getloc() const {return __traits_.getloc();}
2667
2668    // swap:
2669    void swap(basic_regex& __r);
2670
2671private:
2672    _LIBCPP_INLINE_VISIBILITY
2673    unsigned __loop_count() const {return __loop_count_;}
2674
2675    template <class _ForwardIterator>
2676        _ForwardIterator
2677        __parse(_ForwardIterator __first, _ForwardIterator __last);
2678    template <class _ForwardIterator>
2679        _ForwardIterator
2680        __parse_basic_reg_exp(_ForwardIterator __first, _ForwardIterator __last);
2681    template <class _ForwardIterator>
2682        _ForwardIterator
2683        __parse_RE_expression(_ForwardIterator __first, _ForwardIterator __last);
2684    template <class _ForwardIterator>
2685        _ForwardIterator
2686        __parse_simple_RE(_ForwardIterator __first, _ForwardIterator __last);
2687    template <class _ForwardIterator>
2688        _ForwardIterator
2689        __parse_nondupl_RE(_ForwardIterator __first, _ForwardIterator __last);
2690    template <class _ForwardIterator>
2691        _ForwardIterator
2692        __parse_one_char_or_coll_elem_RE(_ForwardIterator __first, _ForwardIterator __last);
2693    template <class _ForwardIterator>
2694        _ForwardIterator
2695        __parse_Back_open_paren(_ForwardIterator __first, _ForwardIterator __last);
2696    template <class _ForwardIterator>
2697        _ForwardIterator
2698        __parse_Back_close_paren(_ForwardIterator __first, _ForwardIterator __last);
2699    template <class _ForwardIterator>
2700        _ForwardIterator
2701        __parse_Back_open_brace(_ForwardIterator __first, _ForwardIterator __last);
2702    template <class _ForwardIterator>
2703        _ForwardIterator
2704        __parse_Back_close_brace(_ForwardIterator __first, _ForwardIterator __last);
2705    template <class _ForwardIterator>
2706        _ForwardIterator
2707        __parse_BACKREF(_ForwardIterator __first, _ForwardIterator __last);
2708    template <class _ForwardIterator>
2709        _ForwardIterator
2710        __parse_ORD_CHAR(_ForwardIterator __first, _ForwardIterator __last);
2711    template <class _ForwardIterator>
2712        _ForwardIterator
2713        __parse_QUOTED_CHAR(_ForwardIterator __first, _ForwardIterator __last);
2714    template <class _ForwardIterator>
2715        _ForwardIterator
2716        __parse_RE_dupl_symbol(_ForwardIterator __first, _ForwardIterator __last,
2717                               __owns_one_state<_CharT>* __s,
2718                               unsigned __mexp_begin, unsigned __mexp_end);
2719    template <class _ForwardIterator>
2720        _ForwardIterator
2721        __parse_ERE_dupl_symbol(_ForwardIterator __first, _ForwardIterator __last,
2722                                __owns_one_state<_CharT>* __s,
2723                                unsigned __mexp_begin, unsigned __mexp_end);
2724    template <class _ForwardIterator>
2725        _ForwardIterator
2726        __parse_bracket_expression(_ForwardIterator __first, _ForwardIterator __last);
2727    template <class _ForwardIterator>
2728        _ForwardIterator
2729        __parse_follow_list(_ForwardIterator __first, _ForwardIterator __last,
2730                            __bracket_expression<_CharT, _Traits>* __ml);
2731    template <class _ForwardIterator>
2732        _ForwardIterator
2733        __parse_expression_term(_ForwardIterator __first, _ForwardIterator __last,
2734                                __bracket_expression<_CharT, _Traits>* __ml);
2735    template <class _ForwardIterator>
2736        _ForwardIterator
2737        __parse_equivalence_class(_ForwardIterator __first, _ForwardIterator __last,
2738                                  __bracket_expression<_CharT, _Traits>* __ml);
2739    template <class _ForwardIterator>
2740        _ForwardIterator
2741        __parse_character_class(_ForwardIterator __first, _ForwardIterator __last,
2742                                __bracket_expression<_CharT, _Traits>* __ml);
2743    template <class _ForwardIterator>
2744        _ForwardIterator
2745        __parse_collating_symbol(_ForwardIterator __first, _ForwardIterator __last,
2746                                 basic_string<_CharT>& __col_sym);
2747    template <class _ForwardIterator>
2748        _ForwardIterator
2749        __parse_DUP_COUNT(_ForwardIterator __first, _ForwardIterator __last, int& __c);
2750    template <class _ForwardIterator>
2751        _ForwardIterator
2752        __parse_extended_reg_exp(_ForwardIterator __first, _ForwardIterator __last);
2753    template <class _ForwardIterator>
2754        _ForwardIterator
2755        __parse_ERE_branch(_ForwardIterator __first, _ForwardIterator __last);
2756    template <class _ForwardIterator>
2757        _ForwardIterator
2758        __parse_ERE_expression(_ForwardIterator __first, _ForwardIterator __last);
2759    template <class _ForwardIterator>
2760        _ForwardIterator
2761        __parse_one_char_or_coll_elem_ERE(_ForwardIterator __first, _ForwardIterator __last);
2762    template <class _ForwardIterator>
2763        _ForwardIterator
2764        __parse_ORD_CHAR_ERE(_ForwardIterator __first, _ForwardIterator __last);
2765    template <class _ForwardIterator>
2766        _ForwardIterator
2767        __parse_QUOTED_CHAR_ERE(_ForwardIterator __first, _ForwardIterator __last);
2768    template <class _ForwardIterator>
2769        _ForwardIterator
2770        __parse_ecma_exp(_ForwardIterator __first, _ForwardIterator __last);
2771    template <class _ForwardIterator>
2772        _ForwardIterator
2773        __parse_alternative(_ForwardIterator __first, _ForwardIterator __last);
2774    template <class _ForwardIterator>
2775        _ForwardIterator
2776        __parse_term(_ForwardIterator __first, _ForwardIterator __last);
2777    template <class _ForwardIterator>
2778        _ForwardIterator
2779        __parse_assertion(_ForwardIterator __first, _ForwardIterator __last);
2780    template <class _ForwardIterator>
2781        _ForwardIterator
2782        __parse_atom(_ForwardIterator __first, _ForwardIterator __last);
2783    template <class _ForwardIterator>
2784        _ForwardIterator
2785        __parse_atom_escape(_ForwardIterator __first, _ForwardIterator __last);
2786    template <class _ForwardIterator>
2787        _ForwardIterator
2788        __parse_decimal_escape(_ForwardIterator __first, _ForwardIterator __last);
2789    template <class _ForwardIterator>
2790        _ForwardIterator
2791        __parse_character_class_escape(_ForwardIterator __first, _ForwardIterator __last);
2792    template <class _ForwardIterator>
2793        _ForwardIterator
2794        __parse_character_escape(_ForwardIterator __first, _ForwardIterator __last,
2795                                 basic_string<_CharT>* __str = nullptr);
2796    template <class _ForwardIterator>
2797        _ForwardIterator
2798        __parse_pattern_character(_ForwardIterator __first, _ForwardIterator __last);
2799    template <class _ForwardIterator>
2800        _ForwardIterator
2801        __parse_grep(_ForwardIterator __first, _ForwardIterator __last);
2802    template <class _ForwardIterator>
2803        _ForwardIterator
2804        __parse_egrep(_ForwardIterator __first, _ForwardIterator __last);
2805    template <class _ForwardIterator>
2806        _ForwardIterator
2807        __parse_class_escape(_ForwardIterator __first, _ForwardIterator __last,
2808                          basic_string<_CharT>& __str,
2809                          __bracket_expression<_CharT, _Traits>* __ml);
2810    template <class _ForwardIterator>
2811        _ForwardIterator
2812        __parse_awk_escape(_ForwardIterator __first, _ForwardIterator __last,
2813                          basic_string<_CharT>* __str = nullptr);
2814
2815    _LIBCPP_INLINE_VISIBILITY
2816    void __push_l_anchor();
2817    void __push_r_anchor();
2818    void __push_match_any();
2819    void __push_match_any_but_newline();
2820    _LIBCPP_INLINE_VISIBILITY
2821    void __push_greedy_inf_repeat(size_t __min, __owns_one_state<_CharT>* __s,
2822                                  unsigned __mexp_begin = 0, unsigned __mexp_end = 0)
2823        {__push_loop(__min, numeric_limits<size_t>::max(), __s,
2824                     __mexp_begin, __mexp_end);}
2825    _LIBCPP_INLINE_VISIBILITY
2826    void __push_nongreedy_inf_repeat(size_t __min, __owns_one_state<_CharT>* __s,
2827                                  unsigned __mexp_begin = 0, unsigned __mexp_end = 0)
2828        {__push_loop(__min, numeric_limits<size_t>::max(), __s,
2829                     __mexp_begin, __mexp_end, false);}
2830    void __push_loop(size_t __min, size_t __max, __owns_one_state<_CharT>* __s,
2831                     size_t __mexp_begin = 0, size_t __mexp_end = 0,
2832                     bool __greedy = true);
2833    __bracket_expression<_CharT, _Traits>* __start_matching_list(bool __negate);
2834    void __push_char(value_type __c);
2835    void __push_back_ref(int __i);
2836    void __push_alternation(__owns_one_state<_CharT>* __sa,
2837                            __owns_one_state<_CharT>* __sb);
2838    void __push_begin_marked_subexpression();
2839    void __push_end_marked_subexpression(unsigned);
2840    void __push_empty();
2841    void __push_word_boundary(bool);
2842    void __push_lookahead(const basic_regex&, bool, unsigned);
2843
2844    template <class _Allocator>
2845        bool
2846        __search(const _CharT* __first, const _CharT* __last,
2847                 match_results<const _CharT*, _Allocator>& __m,
2848                 regex_constants::match_flag_type __flags) const;
2849
2850    template <class _Allocator>
2851        bool
2852        __match_at_start(const _CharT* __first, const _CharT* __last,
2853                 match_results<const _CharT*, _Allocator>& __m,
2854                 regex_constants::match_flag_type __flags, bool) const;
2855    template <class _Allocator>
2856        bool
2857        __match_at_start_ecma(const _CharT* __first, const _CharT* __last,
2858                 match_results<const _CharT*, _Allocator>& __m,
2859                 regex_constants::match_flag_type __flags, bool) const;
2860    template <class _Allocator>
2861        bool
2862        __match_at_start_posix_nosubs(const _CharT* __first, const _CharT* __last,
2863                 match_results<const _CharT*, _Allocator>& __m,
2864                 regex_constants::match_flag_type __flags, bool) const;
2865    template <class _Allocator>
2866        bool
2867        __match_at_start_posix_subs(const _CharT* __first, const _CharT* __last,
2868                 match_results<const _CharT*, _Allocator>& __m,
2869                 regex_constants::match_flag_type __flags, bool) const;
2870
2871    template <class _Bp, class _Ap, class _Cp, class _Tp>
2872    friend
2873    bool
2874    regex_search(_Bp, _Bp, match_results<_Bp, _Ap>&, const basic_regex<_Cp, _Tp>&,
2875                 regex_constants::match_flag_type);
2876
2877    template <class _Ap, class _Cp, class _Tp>
2878    friend
2879    bool
2880    regex_search(const _Cp*, const _Cp*, match_results<const _Cp*, _Ap>&,
2881                 const basic_regex<_Cp, _Tp>&, regex_constants::match_flag_type);
2882
2883    template <class _Bp, class _Cp, class _Tp>
2884    friend
2885    bool
2886    regex_search(_Bp, _Bp, const basic_regex<_Cp, _Tp>&,
2887                 regex_constants::match_flag_type);
2888
2889    template <class _Cp, class _Tp>
2890    friend
2891    bool
2892    regex_search(const _Cp*, const _Cp*,
2893                 const basic_regex<_Cp, _Tp>&, regex_constants::match_flag_type);
2894
2895    template <class _Cp, class _Ap, class _Tp>
2896    friend
2897    bool
2898    regex_search(const _Cp*, match_results<const _Cp*, _Ap>&, const basic_regex<_Cp, _Tp>&,
2899                 regex_constants::match_flag_type);
2900
2901    template <class _ST, class _SA, class _Cp, class _Tp>
2902    friend
2903    bool
2904    regex_search(const basic_string<_Cp, _ST, _SA>& __s,
2905                 const basic_regex<_Cp, _Tp>& __e,
2906                 regex_constants::match_flag_type __flags);
2907
2908    template <class _ST, class _SA, class _Ap, class _Cp, class _Tp>
2909    friend
2910    bool
2911    regex_search(const basic_string<_Cp, _ST, _SA>& __s,
2912                 match_results<typename basic_string<_Cp, _ST, _SA>::const_iterator, _Ap>&,
2913                 const basic_regex<_Cp, _Tp>& __e,
2914                 regex_constants::match_flag_type __flags);
2915
2916    template <class _Iter, class _Ap, class _Cp, class _Tp>
2917    friend
2918    bool
2919    regex_search(__wrap_iter<_Iter> __first,
2920                 __wrap_iter<_Iter> __last,
2921                 match_results<__wrap_iter<_Iter>, _Ap>& __m,
2922                 const basic_regex<_Cp, _Tp>& __e,
2923                 regex_constants::match_flag_type __flags);
2924
2925    template <class, class> friend class __lookahead;
2926};
2927
2928#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
2929template <class _ForwardIterator,
2930          class = typename enable_if<__is_forward_iterator<_ForwardIterator>::value, nullptr_t>::type
2931>
2932basic_regex(_ForwardIterator, _ForwardIterator,
2933            regex_constants::syntax_option_type = regex_constants::ECMAScript)
2934    -> basic_regex<typename iterator_traits<_ForwardIterator>::value_type>;
2935#endif
2936
2937template <class _CharT, class _Traits>
2938    const regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::icase;
2939template <class _CharT, class _Traits>
2940    const regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::nosubs;
2941template <class _CharT, class _Traits>
2942    const regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::optimize;
2943template <class _CharT, class _Traits>
2944    const regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::collate;
2945template <class _CharT, class _Traits>
2946    const regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::ECMAScript;
2947template <class _CharT, class _Traits>
2948    const regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::basic;
2949template <class _CharT, class _Traits>
2950    const regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::extended;
2951template <class _CharT, class _Traits>
2952    const regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::awk;
2953template <class _CharT, class _Traits>
2954    const regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::grep;
2955template <class _CharT, class _Traits>
2956    const regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::egrep;
2957
2958template <class _CharT, class _Traits>
2959void
2960basic_regex<_CharT, _Traits>::swap(basic_regex& __r)
2961{
2962    using _VSTD::swap;
2963    swap(__traits_, __r.__traits_);
2964    swap(__flags_, __r.__flags_);
2965    swap(__marked_count_, __r.__marked_count_);
2966    swap(__loop_count_, __r.__loop_count_);
2967    swap(__open_count_, __r.__open_count_);
2968    swap(__start_, __r.__start_);
2969    swap(__end_, __r.__end_);
2970}
2971
2972template <class _CharT, class _Traits>
2973inline _LIBCPP_INLINE_VISIBILITY
2974void
2975swap(basic_regex<_CharT, _Traits>& __x, basic_regex<_CharT, _Traits>& __y)
2976{
2977    return __x.swap(__y);
2978}
2979
2980// __lookahead
2981
2982template <class _CharT, class _Traits>
2983class __lookahead
2984    : public __owns_one_state<_CharT>
2985{
2986    typedef __owns_one_state<_CharT> base;
2987
2988    basic_regex<_CharT, _Traits> __exp_;
2989    unsigned __mexp_;
2990    bool __invert_;
2991
2992    __lookahead(const __lookahead&);
2993    __lookahead& operator=(const __lookahead&);
2994public:
2995    typedef _VSTD::__state<_CharT> __state;
2996
2997    _LIBCPP_INLINE_VISIBILITY
2998    __lookahead(const basic_regex<_CharT, _Traits>& __exp, bool __invert, __node<_CharT>* __s, unsigned __mexp)
2999        : base(__s), __exp_(__exp), __mexp_(__mexp), __invert_(__invert) {}
3000
3001    virtual void __exec(__state&) const;
3002};
3003
3004template <class _CharT, class _Traits>
3005void
3006__lookahead<_CharT, _Traits>::__exec(__state& __s) const
3007{
3008    match_results<const _CharT*> __m;
3009    __m.__init(1 + __exp_.mark_count(), __s.__current_, __s.__last_);
3010    bool __matched = __exp_.__match_at_start_ecma(
3011        __s.__current_, __s.__last_,
3012        __m,
3013        (__s.__flags_ | regex_constants::match_continuous) &
3014        ~regex_constants::__full_match,
3015        __s.__at_first_ && __s.__current_ == __s.__first_);
3016    if (__matched != __invert_)
3017    {
3018        __s.__do_ = __state::__accept_but_not_consume;
3019        __s.__node_ = this->first();
3020        for (unsigned __i = 1; __i < __m.size(); ++__i) {
3021            __s.__sub_matches_[__mexp_ + __i - 1] = __m.__matches_[__i];
3022        }
3023    }
3024    else
3025    {
3026        __s.__do_ = __state::__reject;
3027        __s.__node_ = nullptr;
3028    }
3029}
3030
3031template <class _CharT, class _Traits>
3032template <class _ForwardIterator>
3033_ForwardIterator
3034basic_regex<_CharT, _Traits>::__parse(_ForwardIterator __first,
3035                                      _ForwardIterator __last)
3036{
3037    {
3038        unique_ptr<__node> __h(new __end_state<_CharT>);
3039        __start_.reset(new __empty_state<_CharT>(__h.get()));
3040        __h.release();
3041        __end_ = __start_.get();
3042    }
3043    switch (__flags_ & 0x1F0)
3044    {
3045    case ECMAScript:
3046        __first = __parse_ecma_exp(__first, __last);
3047        break;
3048    case basic:
3049        __first = __parse_basic_reg_exp(__first, __last);
3050        break;
3051    case extended:
3052    case awk:
3053        __first = __parse_extended_reg_exp(__first, __last);
3054        break;
3055    case grep:
3056        __first = __parse_grep(__first, __last);
3057        break;
3058    case egrep:
3059        __first = __parse_egrep(__first, __last);
3060        break;
3061    default:
3062        __throw_regex_error<regex_constants::__re_err_grammar>();
3063    }
3064    return __first;
3065}
3066
3067template <class _CharT, class _Traits>
3068template <class _ForwardIterator>
3069_ForwardIterator
3070basic_regex<_CharT, _Traits>::__parse_basic_reg_exp(_ForwardIterator __first,
3071                                                    _ForwardIterator __last)
3072{
3073    if (__first != __last)
3074    {
3075        if (*__first == '^')
3076        {
3077            __push_l_anchor();
3078            ++__first;
3079        }
3080        if (__first != __last)
3081        {
3082            __first = __parse_RE_expression(__first, __last);
3083            if (__first != __last)
3084            {
3085                _ForwardIterator __temp = _VSTD::next(__first);
3086                if (__temp == __last && *__first == '$')
3087                {
3088                    __push_r_anchor();
3089                    ++__first;
3090                }
3091            }
3092        }
3093        if (__first != __last)
3094            __throw_regex_error<regex_constants::__re_err_empty>();
3095    }
3096    return __first;
3097}
3098
3099template <class _CharT, class _Traits>
3100template <class _ForwardIterator>
3101_ForwardIterator
3102basic_regex<_CharT, _Traits>::__parse_extended_reg_exp(_ForwardIterator __first,
3103                                                       _ForwardIterator __last)
3104{
3105    __owns_one_state<_CharT>* __sa = __end_;
3106    _ForwardIterator __temp = __parse_ERE_branch(__first, __last);
3107    if (__temp == __first)
3108        __throw_regex_error<regex_constants::__re_err_empty>();
3109    __first = __temp;
3110    while (__first != __last && *__first == '|')
3111    {
3112        __owns_one_state<_CharT>* __sb = __end_;
3113        __temp = __parse_ERE_branch(++__first, __last);
3114        if (__temp == __first)
3115            __throw_regex_error<regex_constants::__re_err_empty>();
3116        __push_alternation(__sa, __sb);
3117        __first = __temp;
3118    }
3119    return __first;
3120}
3121
3122template <class _CharT, class _Traits>
3123template <class _ForwardIterator>
3124_ForwardIterator
3125basic_regex<_CharT, _Traits>::__parse_ERE_branch(_ForwardIterator __first,
3126                                                 _ForwardIterator __last)
3127{
3128    _ForwardIterator __temp = __parse_ERE_expression(__first, __last);
3129    if (__temp == __first)
3130        __throw_regex_error<regex_constants::__re_err_empty>();
3131    do
3132    {
3133        __first = __temp;
3134        __temp = __parse_ERE_expression(__first, __last);
3135    } while (__temp != __first);
3136    return __first;
3137}
3138
3139template <class _CharT, class _Traits>
3140template <class _ForwardIterator>
3141_ForwardIterator
3142basic_regex<_CharT, _Traits>::__parse_ERE_expression(_ForwardIterator __first,
3143                                                     _ForwardIterator __last)
3144{
3145    __owns_one_state<_CharT>* __e = __end_;
3146    unsigned __mexp_begin = __marked_count_;
3147    _ForwardIterator __temp = __parse_one_char_or_coll_elem_ERE(__first, __last);
3148    if (__temp == __first && __temp != __last)
3149    {
3150        switch (*__temp)
3151        {
3152        case '^':
3153            __push_l_anchor();
3154            ++__temp;
3155            break;
3156        case '$':
3157            __push_r_anchor();
3158            ++__temp;
3159            break;
3160        case '(':
3161            __push_begin_marked_subexpression();
3162            unsigned __temp_count = __marked_count_;
3163            ++__open_count_;
3164            __temp = __parse_extended_reg_exp(++__temp, __last);
3165            if (__temp == __last || *__temp != ')')
3166                __throw_regex_error<regex_constants::error_paren>();
3167            __push_end_marked_subexpression(__temp_count);
3168            --__open_count_;
3169            ++__temp;
3170            break;
3171        }
3172    }
3173    if (__temp != __first)
3174        __temp = __parse_ERE_dupl_symbol(__temp, __last, __e, __mexp_begin+1,
3175                                         __marked_count_+1);
3176    __first = __temp;
3177    return __first;
3178}
3179
3180template <class _CharT, class _Traits>
3181template <class _ForwardIterator>
3182_ForwardIterator
3183basic_regex<_CharT, _Traits>::__parse_RE_expression(_ForwardIterator __first,
3184                                                    _ForwardIterator __last)
3185{
3186    while (true)
3187    {
3188        _ForwardIterator __temp = __parse_simple_RE(__first, __last);
3189        if (__temp == __first)
3190            break;
3191        __first = __temp;
3192    }
3193    return __first;
3194}
3195
3196template <class _CharT, class _Traits>
3197template <class _ForwardIterator>
3198_ForwardIterator
3199basic_regex<_CharT, _Traits>::__parse_simple_RE(_ForwardIterator __first,
3200                                                _ForwardIterator __last)
3201{
3202    if (__first != __last)
3203    {
3204        __owns_one_state<_CharT>* __e = __end_;
3205        unsigned __mexp_begin = __marked_count_;
3206        _ForwardIterator __temp = __parse_nondupl_RE(__first, __last);
3207        if (__temp != __first)
3208            __first = __parse_RE_dupl_symbol(__temp, __last, __e,
3209                                             __mexp_begin+1, __marked_count_+1);
3210    }
3211    return __first;
3212}
3213
3214template <class _CharT, class _Traits>
3215template <class _ForwardIterator>
3216_ForwardIterator
3217basic_regex<_CharT, _Traits>::__parse_nondupl_RE(_ForwardIterator __first,
3218                                                 _ForwardIterator __last)
3219{
3220    _ForwardIterator __temp = __first;
3221    __first = __parse_one_char_or_coll_elem_RE(__first, __last);
3222    if (__temp == __first)
3223    {
3224        __temp = __parse_Back_open_paren(__first, __last);
3225        if (__temp != __first)
3226        {
3227            __push_begin_marked_subexpression();
3228            unsigned __temp_count = __marked_count_;
3229            __first = __parse_RE_expression(__temp, __last);
3230            __temp = __parse_Back_close_paren(__first, __last);
3231            if (__temp == __first)
3232                __throw_regex_error<regex_constants::error_paren>();
3233            __push_end_marked_subexpression(__temp_count);
3234            __first = __temp;
3235        }
3236        else
3237            __first = __parse_BACKREF(__first, __last);
3238    }
3239    return __first;
3240}
3241
3242template <class _CharT, class _Traits>
3243template <class _ForwardIterator>
3244_ForwardIterator
3245basic_regex<_CharT, _Traits>::__parse_one_char_or_coll_elem_RE(
3246                                                       _ForwardIterator __first,
3247                                                       _ForwardIterator __last)
3248{
3249    _ForwardIterator __temp = __parse_ORD_CHAR(__first, __last);
3250    if (__temp == __first)
3251    {
3252        __temp = __parse_QUOTED_CHAR(__first, __last);
3253        if (__temp == __first)
3254        {
3255            if (__temp != __last && *__temp == '.')
3256            {
3257                __push_match_any();
3258                ++__temp;
3259            }
3260            else
3261                __temp = __parse_bracket_expression(__first, __last);
3262        }
3263    }
3264    __first = __temp;
3265    return __first;
3266}
3267
3268template <class _CharT, class _Traits>
3269template <class _ForwardIterator>
3270_ForwardIterator
3271basic_regex<_CharT, _Traits>::__parse_one_char_or_coll_elem_ERE(
3272                                                       _ForwardIterator __first,
3273                                                       _ForwardIterator __last)
3274{
3275    _ForwardIterator __temp = __parse_ORD_CHAR_ERE(__first, __last);
3276    if (__temp == __first)
3277    {
3278        __temp = __parse_QUOTED_CHAR_ERE(__first, __last);
3279        if (__temp == __first)
3280        {
3281            if (__temp != __last && *__temp == '.')
3282            {
3283                __push_match_any();
3284                ++__temp;
3285            }
3286            else
3287                __temp = __parse_bracket_expression(__first, __last);
3288        }
3289    }
3290    __first = __temp;
3291    return __first;
3292}
3293
3294template <class _CharT, class _Traits>
3295template <class _ForwardIterator>
3296_ForwardIterator
3297basic_regex<_CharT, _Traits>::__parse_Back_open_paren(_ForwardIterator __first,
3298                                                      _ForwardIterator __last)
3299{
3300    if (__first != __last)
3301    {
3302        _ForwardIterator __temp = _VSTD::next(__first);
3303        if (__temp != __last)
3304        {
3305            if (*__first == '\\' && *__temp == '(')
3306                __first = ++__temp;
3307        }
3308    }
3309    return __first;
3310}
3311
3312template <class _CharT, class _Traits>
3313template <class _ForwardIterator>
3314_ForwardIterator
3315basic_regex<_CharT, _Traits>::__parse_Back_close_paren(_ForwardIterator __first,
3316                                                       _ForwardIterator __last)
3317{
3318    if (__first != __last)
3319    {
3320        _ForwardIterator __temp = _VSTD::next(__first);
3321        if (__temp != __last)
3322        {
3323            if (*__first == '\\' && *__temp == ')')
3324                __first = ++__temp;
3325        }
3326    }
3327    return __first;
3328}
3329
3330template <class _CharT, class _Traits>
3331template <class _ForwardIterator>
3332_ForwardIterator
3333basic_regex<_CharT, _Traits>::__parse_Back_open_brace(_ForwardIterator __first,
3334                                                      _ForwardIterator __last)
3335{
3336    if (__first != __last)
3337    {
3338        _ForwardIterator __temp = _VSTD::next(__first);
3339        if (__temp != __last)
3340        {
3341            if (*__first == '\\' && *__temp == '{')
3342                __first = ++__temp;
3343        }
3344    }
3345    return __first;
3346}
3347
3348template <class _CharT, class _Traits>
3349template <class _ForwardIterator>
3350_ForwardIterator
3351basic_regex<_CharT, _Traits>::__parse_Back_close_brace(_ForwardIterator __first,
3352                                                       _ForwardIterator __last)
3353{
3354    if (__first != __last)
3355    {
3356        _ForwardIterator __temp = _VSTD::next(__first);
3357        if (__temp != __last)
3358        {
3359            if (*__first == '\\' && *__temp == '}')
3360                __first = ++__temp;
3361        }
3362    }
3363    return __first;
3364}
3365
3366template <class _CharT, class _Traits>
3367template <class _ForwardIterator>
3368_ForwardIterator
3369basic_regex<_CharT, _Traits>::__parse_BACKREF(_ForwardIterator __first,
3370                                              _ForwardIterator __last)
3371{
3372    if (__first != __last)
3373    {
3374        _ForwardIterator __temp = _VSTD::next(__first);
3375        if (__temp != __last)
3376        {
3377            if (*__first == '\\')
3378            {
3379                int __val = __traits_.value(*__temp, 10);
3380                if (__val >= 1 && __val <= 9)
3381                {
3382                    __push_back_ref(__val);
3383                    __first = ++__temp;
3384                }
3385            }
3386        }
3387    }
3388    return __first;
3389}
3390
3391template <class _CharT, class _Traits>
3392template <class _ForwardIterator>
3393_ForwardIterator
3394basic_regex<_CharT, _Traits>::__parse_ORD_CHAR(_ForwardIterator __first,
3395                                               _ForwardIterator __last)
3396{
3397    if (__first != __last)
3398    {
3399        _ForwardIterator __temp = _VSTD::next(__first);
3400        if (__temp == __last && *__first == '$')
3401            return __first;
3402        // Not called inside a bracket
3403        if (*__first == '.' || *__first == '\\' || *__first == '[')
3404            return __first;
3405        __push_char(*__first);
3406        ++__first;
3407    }
3408    return __first;
3409}
3410
3411template <class _CharT, class _Traits>
3412template <class _ForwardIterator>
3413_ForwardIterator
3414basic_regex<_CharT, _Traits>::__parse_ORD_CHAR_ERE(_ForwardIterator __first,
3415                                                   _ForwardIterator __last)
3416{
3417    if (__first != __last)
3418    {
3419        switch (*__first)
3420        {
3421        case '^':
3422        case '.':
3423        case '[':
3424        case '$':
3425        case '(':
3426        case '|':
3427        case '*':
3428        case '+':
3429        case '?':
3430        case '{':
3431        case '\\':
3432            break;
3433        case ')':
3434            if (__open_count_ == 0)
3435            {
3436                __push_char(*__first);
3437                ++__first;
3438            }
3439            break;
3440        default:
3441            __push_char(*__first);
3442            ++__first;
3443            break;
3444        }
3445    }
3446    return __first;
3447}
3448
3449template <class _CharT, class _Traits>
3450template <class _ForwardIterator>
3451_ForwardIterator
3452basic_regex<_CharT, _Traits>::__parse_QUOTED_CHAR(_ForwardIterator __first,
3453                                                  _ForwardIterator __last)
3454{
3455    if (__first != __last)
3456    {
3457        _ForwardIterator __temp = _VSTD::next(__first);
3458        if (__temp != __last)
3459        {
3460            if (*__first == '\\')
3461            {
3462                switch (*__temp)
3463                {
3464                case '^':
3465                case '.':
3466                case '*':
3467                case '[':
3468                case '$':
3469                case '\\':
3470                    __push_char(*__temp);
3471                    __first = ++__temp;
3472                    break;
3473                }
3474            }
3475        }
3476    }
3477    return __first;
3478}
3479
3480template <class _CharT, class _Traits>
3481template <class _ForwardIterator>
3482_ForwardIterator
3483basic_regex<_CharT, _Traits>::__parse_QUOTED_CHAR_ERE(_ForwardIterator __first,
3484                                                      _ForwardIterator __last)
3485{
3486    if (__first != __last)
3487    {
3488        _ForwardIterator __temp = _VSTD::next(__first);
3489        if (__temp != __last)
3490        {
3491            if (*__first == '\\')
3492            {
3493                switch (*__temp)
3494                {
3495                case '^':
3496                case '.':
3497                case '*':
3498                case '[':
3499                case '$':
3500                case '\\':
3501                case '(':
3502                case ')':
3503                case '|':
3504                case '+':
3505                case '?':
3506                case '{':
3507                case '}':
3508                    __push_char(*__temp);
3509                    __first = ++__temp;
3510                    break;
3511                default:
3512                    if ((__flags_ & 0x1F0) == awk)
3513                        __first = __parse_awk_escape(++__first, __last);
3514                    break;
3515                }
3516            }
3517        }
3518    }
3519    return __first;
3520}
3521
3522template <class _CharT, class _Traits>
3523template <class _ForwardIterator>
3524_ForwardIterator
3525basic_regex<_CharT, _Traits>::__parse_RE_dupl_symbol(_ForwardIterator __first,
3526                                                     _ForwardIterator __last,
3527                                                     __owns_one_state<_CharT>* __s,
3528                                                     unsigned __mexp_begin,
3529                                                     unsigned __mexp_end)
3530{
3531    if (__first != __last)
3532    {
3533        if (*__first == '*')
3534        {
3535            __push_greedy_inf_repeat(0, __s, __mexp_begin, __mexp_end);
3536            ++__first;
3537        }
3538        else
3539        {
3540            _ForwardIterator __temp = __parse_Back_open_brace(__first, __last);
3541            if (__temp != __first)
3542            {
3543                int __min = 0;
3544                __first = __temp;
3545                __temp = __parse_DUP_COUNT(__first, __last, __min);
3546                if (__temp == __first)
3547                    __throw_regex_error<regex_constants::error_badbrace>();
3548                __first = __temp;
3549                if (__first == __last)
3550                    __throw_regex_error<regex_constants::error_brace>();
3551                if (*__first != ',')
3552                {
3553                    __temp = __parse_Back_close_brace(__first, __last);
3554                    if (__temp == __first)
3555                        __throw_regex_error<regex_constants::error_brace>();
3556                    __push_loop(__min, __min, __s, __mexp_begin, __mexp_end,
3557                                    true);
3558                    __first = __temp;
3559                }
3560                else
3561                {
3562                    ++__first;  // consume ','
3563                    int __max = -1;
3564                    __first = __parse_DUP_COUNT(__first, __last, __max);
3565                    __temp = __parse_Back_close_brace(__first, __last);
3566                    if (__temp == __first)
3567                        __throw_regex_error<regex_constants::error_brace>();
3568                    if (__max == -1)
3569                        __push_greedy_inf_repeat(__min, __s, __mexp_begin, __mexp_end);
3570                    else
3571                    {
3572                        if (__max < __min)
3573                            __throw_regex_error<regex_constants::error_badbrace>();
3574                        __push_loop(__min, __max, __s, __mexp_begin, __mexp_end,
3575                                    true);
3576                    }
3577                    __first = __temp;
3578                }
3579            }
3580        }
3581    }
3582    return __first;
3583}
3584
3585template <class _CharT, class _Traits>
3586template <class _ForwardIterator>
3587_ForwardIterator
3588basic_regex<_CharT, _Traits>::__parse_ERE_dupl_symbol(_ForwardIterator __first,
3589                                                      _ForwardIterator __last,
3590                                                      __owns_one_state<_CharT>* __s,
3591                                                      unsigned __mexp_begin,
3592                                                      unsigned __mexp_end)
3593{
3594    if (__first != __last)
3595    {
3596        unsigned __grammar = __flags_ & 0x1F0;
3597        switch (*__first)
3598        {
3599        case '*':
3600            ++__first;
3601            if (__grammar == ECMAScript && __first != __last && *__first == '?')
3602            {
3603                ++__first;
3604                __push_nongreedy_inf_repeat(0, __s, __mexp_begin, __mexp_end);
3605            }
3606            else
3607                __push_greedy_inf_repeat(0, __s, __mexp_begin, __mexp_end);
3608            break;
3609        case '+':
3610            ++__first;
3611            if (__grammar == ECMAScript && __first != __last && *__first == '?')
3612            {
3613                ++__first;
3614                __push_nongreedy_inf_repeat(1, __s, __mexp_begin, __mexp_end);
3615            }
3616            else
3617                __push_greedy_inf_repeat(1, __s, __mexp_begin, __mexp_end);
3618            break;
3619        case '?':
3620            ++__first;
3621            if (__grammar == ECMAScript && __first != __last && *__first == '?')
3622            {
3623                ++__first;
3624                __push_loop(0, 1, __s, __mexp_begin, __mexp_end, false);
3625            }
3626            else
3627                __push_loop(0, 1, __s, __mexp_begin, __mexp_end);
3628            break;
3629        case '{':
3630            {
3631                int __min;
3632                _ForwardIterator __temp = __parse_DUP_COUNT(++__first, __last, __min);
3633                if (__temp == __first)
3634                    __throw_regex_error<regex_constants::error_badbrace>();
3635                __first = __temp;
3636                if (__first == __last)
3637                    __throw_regex_error<regex_constants::error_brace>();
3638                switch (*__first)
3639                {
3640                case '}':
3641                    ++__first;
3642                    if (__grammar == ECMAScript && __first != __last && *__first == '?')
3643                    {
3644                        ++__first;
3645                        __push_loop(__min, __min, __s, __mexp_begin, __mexp_end, false);
3646                    }
3647                    else
3648                        __push_loop(__min, __min, __s, __mexp_begin, __mexp_end);
3649                    break;
3650                case ',':
3651                    ++__first;
3652                    if (__first == __last)
3653                        __throw_regex_error<regex_constants::error_badbrace>();
3654                    if (*__first == '}')
3655                    {
3656                        ++__first;
3657                        if (__grammar == ECMAScript && __first != __last && *__first == '?')
3658                        {
3659                            ++__first;
3660                            __push_nongreedy_inf_repeat(__min, __s, __mexp_begin, __mexp_end);
3661                        }
3662                        else
3663                            __push_greedy_inf_repeat(__min, __s, __mexp_begin, __mexp_end);
3664                    }
3665                    else
3666                    {
3667                        int __max = -1;
3668                        __temp = __parse_DUP_COUNT(__first, __last, __max);
3669                        if (__temp == __first)
3670                            __throw_regex_error<regex_constants::error_brace>();
3671                        __first = __temp;
3672                        if (__first == __last || *__first != '}')
3673                            __throw_regex_error<regex_constants::error_brace>();
3674                        ++__first;
3675                        if (__max < __min)
3676                            __throw_regex_error<regex_constants::error_badbrace>();
3677                        if (__grammar == ECMAScript && __first != __last && *__first == '?')
3678                        {
3679                            ++__first;
3680                            __push_loop(__min, __max, __s, __mexp_begin, __mexp_end, false);
3681                        }
3682                        else
3683                            __push_loop(__min, __max, __s, __mexp_begin, __mexp_end);
3684                    }
3685                    break;
3686                default:
3687                    __throw_regex_error<regex_constants::error_badbrace>();
3688                }
3689            }
3690            break;
3691        }
3692    }
3693    return __first;
3694}
3695
3696template <class _CharT, class _Traits>
3697template <class _ForwardIterator>
3698_ForwardIterator
3699basic_regex<_CharT, _Traits>::__parse_bracket_expression(_ForwardIterator __first,
3700                                                         _ForwardIterator __last)
3701{
3702    if (__first != __last && *__first == '[')
3703    {
3704        ++__first;
3705        if (__first == __last)
3706            __throw_regex_error<regex_constants::error_brack>();
3707        bool __negate = false;
3708        if (*__first == '^')
3709        {
3710            ++__first;
3711            __negate = true;
3712        }
3713        __bracket_expression<_CharT, _Traits>* __ml = __start_matching_list(__negate);
3714        // __ml owned by *this
3715        if (__first == __last)
3716            __throw_regex_error<regex_constants::error_brack>();
3717        if ((__flags_ & 0x1F0) != ECMAScript && *__first == ']')
3718        {
3719            __ml->__add_char(']');
3720            ++__first;
3721        }
3722        __first = __parse_follow_list(__first, __last, __ml);
3723        if (__first == __last)
3724            __throw_regex_error<regex_constants::error_brack>();
3725        if (*__first == '-')
3726        {
3727            __ml->__add_char('-');
3728            ++__first;
3729        }
3730        if (__first == __last || *__first != ']')
3731            __throw_regex_error<regex_constants::error_brack>();
3732        ++__first;
3733    }
3734    return __first;
3735}
3736
3737template <class _CharT, class _Traits>
3738template <class _ForwardIterator>
3739_ForwardIterator
3740basic_regex<_CharT, _Traits>::__parse_follow_list(_ForwardIterator __first,
3741                                    _ForwardIterator __last,
3742                                    __bracket_expression<_CharT, _Traits>* __ml)
3743{
3744    if (__first != __last)
3745    {
3746        while (true)
3747        {
3748            _ForwardIterator __temp = __parse_expression_term(__first, __last,
3749                                                              __ml);
3750            if (__temp == __first)
3751                break;
3752            __first = __temp;
3753        }
3754    }
3755    return __first;
3756}
3757
3758template <class _CharT, class _Traits>
3759template <class _ForwardIterator>
3760_ForwardIterator
3761basic_regex<_CharT, _Traits>::__parse_expression_term(_ForwardIterator __first,
3762                                    _ForwardIterator __last,
3763                                    __bracket_expression<_CharT, _Traits>* __ml)
3764{
3765    if (__first != __last && *__first != ']')
3766    {
3767        _ForwardIterator __temp = _VSTD::next(__first);
3768        basic_string<_CharT> __start_range;
3769        if (__temp != __last && *__first == '[')
3770        {
3771            if (*__temp == '=')
3772                return __parse_equivalence_class(++__temp, __last, __ml);
3773            else if (*__temp == ':')
3774                return __parse_character_class(++__temp, __last, __ml);
3775            else if (*__temp == '.')
3776                __first = __parse_collating_symbol(++__temp, __last, __start_range);
3777        }
3778        unsigned __grammar = __flags_ & 0x1F0;
3779        if (__start_range.empty())
3780        {
3781            if ((__grammar == ECMAScript || __grammar == awk) && *__first == '\\')
3782            {
3783                if (__grammar == ECMAScript)
3784                    __first = __parse_class_escape(++__first, __last, __start_range, __ml);
3785                else
3786                    __first = __parse_awk_escape(++__first, __last, &__start_range);
3787            }
3788            else
3789            {
3790                __start_range = *__first;
3791                ++__first;
3792            }
3793        }
3794        if (__first != __last && *__first != ']')
3795        {
3796            __temp = _VSTD::next(__first);
3797            if (__temp != __last && *__first == '-' && *__temp != ']')
3798            {
3799                // parse a range
3800                basic_string<_CharT> __end_range;
3801                __first = __temp;
3802                ++__temp;
3803                if (__temp != __last && *__first == '[' && *__temp == '.')
3804                    __first = __parse_collating_symbol(++__temp, __last, __end_range);
3805                else
3806                {
3807                    if ((__grammar == ECMAScript || __grammar == awk) && *__first == '\\')
3808                    {
3809                        if (__grammar == ECMAScript)
3810                            __first = __parse_class_escape(++__first, __last,
3811                                                           __end_range, __ml);
3812                        else
3813                            __first = __parse_awk_escape(++__first, __last,
3814                                                         &__end_range);
3815                    }
3816                    else
3817                    {
3818                        __end_range = *__first;
3819                        ++__first;
3820                    }
3821                }
3822                __ml->__add_range(_VSTD::move(__start_range), _VSTD::move(__end_range));
3823            }
3824            else if (!__start_range.empty())
3825            {
3826                if (__start_range.size() == 1)
3827                    __ml->__add_char(__start_range[0]);
3828                else
3829                    __ml->__add_digraph(__start_range[0], __start_range[1]);
3830            }
3831        }
3832        else if (!__start_range.empty())
3833        {
3834            if (__start_range.size() == 1)
3835                __ml->__add_char(__start_range[0]);
3836            else
3837                __ml->__add_digraph(__start_range[0], __start_range[1]);
3838        }
3839    }
3840    return __first;
3841}
3842
3843template <class _CharT, class _Traits>
3844template <class _ForwardIterator>
3845_ForwardIterator
3846basic_regex<_CharT, _Traits>::__parse_class_escape(_ForwardIterator __first,
3847                          _ForwardIterator __last,
3848                          basic_string<_CharT>& __str,
3849                          __bracket_expression<_CharT, _Traits>* __ml)
3850{
3851    if (__first == __last)
3852        __throw_regex_error<regex_constants::error_escape>();
3853    switch (*__first)
3854    {
3855    case 0:
3856        __str = *__first;
3857        return ++__first;
3858    case 'b':
3859        __str = _CharT(8);
3860        return ++__first;
3861    case 'd':
3862        __ml->__add_class(ctype_base::digit);
3863        return ++__first;
3864    case 'D':
3865        __ml->__add_neg_class(ctype_base::digit);
3866        return ++__first;
3867    case 's':
3868        __ml->__add_class(ctype_base::space);
3869        return ++__first;
3870    case 'S':
3871        __ml->__add_neg_class(ctype_base::space);
3872        return ++__first;
3873    case 'w':
3874        __ml->__add_class(ctype_base::alnum);
3875        __ml->__add_char('_');
3876        return ++__first;
3877    case 'W':
3878        __ml->__add_neg_class(ctype_base::alnum);
3879        __ml->__add_neg_char('_');
3880        return ++__first;
3881    }
3882    __first = __parse_character_escape(__first, __last, &__str);
3883    return __first;
3884}
3885
3886template <class _CharT, class _Traits>
3887template <class _ForwardIterator>
3888_ForwardIterator
3889basic_regex<_CharT, _Traits>::__parse_awk_escape(_ForwardIterator __first,
3890                          _ForwardIterator __last,
3891                          basic_string<_CharT>* __str)
3892{
3893    if (__first == __last)
3894        __throw_regex_error<regex_constants::error_escape>();
3895    switch (*__first)
3896    {
3897    case '\\':
3898    case '"':
3899    case '/':
3900        if (__str)
3901            *__str = *__first;
3902        else
3903            __push_char(*__first);
3904        return ++__first;
3905    case 'a':
3906        if (__str)
3907            *__str = _CharT(7);
3908        else
3909            __push_char(_CharT(7));
3910        return ++__first;
3911    case 'b':
3912        if (__str)
3913            *__str = _CharT(8);
3914        else
3915            __push_char(_CharT(8));
3916        return ++__first;
3917    case 'f':
3918        if (__str)
3919            *__str = _CharT(0xC);
3920        else
3921            __push_char(_CharT(0xC));
3922        return ++__first;
3923    case 'n':
3924        if (__str)
3925            *__str = _CharT(0xA);
3926        else
3927            __push_char(_CharT(0xA));
3928        return ++__first;
3929    case 'r':
3930        if (__str)
3931            *__str = _CharT(0xD);
3932        else
3933            __push_char(_CharT(0xD));
3934        return ++__first;
3935    case 't':
3936        if (__str)
3937            *__str = _CharT(0x9);
3938        else
3939            __push_char(_CharT(0x9));
3940        return ++__first;
3941    case 'v':
3942        if (__str)
3943            *__str = _CharT(0xB);
3944        else
3945            __push_char(_CharT(0xB));
3946        return ++__first;
3947    }
3948    if ('0' <= *__first && *__first <= '7')
3949    {
3950        unsigned __val = *__first - '0';
3951        if (++__first != __last && ('0' <= *__first && *__first <= '7'))
3952        {
3953            __val = 8 * __val + *__first - '0';
3954            if (++__first != __last && ('0' <= *__first && *__first <= '7'))
3955                __val = 8 * __val + *__first++ - '0';
3956        }
3957        if (__str)
3958            *__str = _CharT(__val);
3959        else
3960            __push_char(_CharT(__val));
3961    }
3962    else
3963        __throw_regex_error<regex_constants::error_escape>();
3964    return __first;
3965}
3966
3967template <class _CharT, class _Traits>
3968template <class _ForwardIterator>
3969_ForwardIterator
3970basic_regex<_CharT, _Traits>::__parse_equivalence_class(_ForwardIterator __first,
3971                                    _ForwardIterator __last,
3972                                    __bracket_expression<_CharT, _Traits>* __ml)
3973{
3974    // Found [=
3975    //   This means =] must exist
3976    value_type _Equal_close[2] = {'=', ']'};
3977    _ForwardIterator __temp = _VSTD::search(__first, __last, _Equal_close,
3978                                                            _Equal_close+2);
3979    if (__temp == __last)
3980        __throw_regex_error<regex_constants::error_brack>();
3981    // [__first, __temp) contains all text in [= ... =]
3982    string_type __collate_name =
3983        __traits_.lookup_collatename(__first, __temp);
3984    if (__collate_name.empty())
3985        __throw_regex_error<regex_constants::error_collate>();
3986    string_type __equiv_name =
3987        __traits_.transform_primary(__collate_name.begin(),
3988                                    __collate_name.end());
3989    if (!__equiv_name.empty())
3990        __ml->__add_equivalence(__equiv_name);
3991    else
3992    {
3993        switch (__collate_name.size())
3994        {
3995        case 1:
3996            __ml->__add_char(__collate_name[0]);
3997            break;
3998        case 2:
3999            __ml->__add_digraph(__collate_name[0], __collate_name[1]);
4000            break;
4001        default:
4002            __throw_regex_error<regex_constants::error_collate>();
4003        }
4004    }
4005    __first = _VSTD::next(__temp, 2);
4006    return __first;
4007}
4008
4009template <class _CharT, class _Traits>
4010template <class _ForwardIterator>
4011_ForwardIterator
4012basic_regex<_CharT, _Traits>::__parse_character_class(_ForwardIterator __first,
4013                                    _ForwardIterator __last,
4014                                    __bracket_expression<_CharT, _Traits>* __ml)
4015{
4016    // Found [:
4017    //   This means :] must exist
4018    value_type _Colon_close[2] = {':', ']'};
4019    _ForwardIterator __temp = _VSTD::search(__first, __last, _Colon_close,
4020                                                            _Colon_close+2);
4021    if (__temp == __last)
4022        __throw_regex_error<regex_constants::error_brack>();
4023    // [__first, __temp) contains all text in [: ... :]
4024    typedef typename _Traits::char_class_type char_class_type;
4025    char_class_type __class_type =
4026        __traits_.lookup_classname(__first, __temp, __flags_ & icase);
4027    if (__class_type == 0)
4028        __throw_regex_error<regex_constants::error_ctype>();
4029    __ml->__add_class(__class_type);
4030    __first = _VSTD::next(__temp, 2);
4031    return __first;
4032}
4033
4034template <class _CharT, class _Traits>
4035template <class _ForwardIterator>
4036_ForwardIterator
4037basic_regex<_CharT, _Traits>::__parse_collating_symbol(_ForwardIterator __first,
4038                                                _ForwardIterator __last,
4039                                                basic_string<_CharT>& __col_sym)
4040{
4041    // Found [.
4042    //   This means .] must exist
4043    value_type _Dot_close[2] = {'.', ']'};
4044    _ForwardIterator __temp = _VSTD::search(__first, __last, _Dot_close,
4045                                                            _Dot_close+2);
4046    if (__temp == __last)
4047        __throw_regex_error<regex_constants::error_brack>();
4048    // [__first, __temp) contains all text in [. ... .]
4049    __col_sym = __traits_.lookup_collatename(__first, __temp);
4050    switch (__col_sym.size())
4051    {
4052    case 1:
4053    case 2:
4054        break;
4055    default:
4056        __throw_regex_error<regex_constants::error_collate>();
4057    }
4058    __first = _VSTD::next(__temp, 2);
4059    return __first;
4060}
4061
4062template <class _CharT, class _Traits>
4063template <class _ForwardIterator>
4064_ForwardIterator
4065basic_regex<_CharT, _Traits>::__parse_DUP_COUNT(_ForwardIterator __first,
4066                                                _ForwardIterator __last,
4067                                                int& __c)
4068{
4069    if (__first != __last )
4070    {
4071        int __val = __traits_.value(*__first, 10);
4072        if ( __val != -1 )
4073        {
4074            __c = __val;
4075            for (++__first;
4076                 __first != __last && ( __val = __traits_.value(*__first, 10)) != -1;
4077                 ++__first)
4078            {
4079                if (__c >= std::numeric_limits<int>::max() / 10)
4080                    __throw_regex_error<regex_constants::error_badbrace>();
4081                __c *= 10;
4082                __c += __val;
4083            }
4084        }
4085    }
4086    return __first;
4087}
4088
4089template <class _CharT, class _Traits>
4090template <class _ForwardIterator>
4091_ForwardIterator
4092basic_regex<_CharT, _Traits>::__parse_ecma_exp(_ForwardIterator __first,
4093                                               _ForwardIterator __last)
4094{
4095    __owns_one_state<_CharT>* __sa = __end_;
4096    _ForwardIterator __temp = __parse_alternative(__first, __last);
4097    if (__temp == __first)
4098        __push_empty();
4099    __first = __temp;
4100    while (__first != __last && *__first == '|')
4101    {
4102        __owns_one_state<_CharT>* __sb = __end_;
4103        __temp = __parse_alternative(++__first, __last);
4104        if (__temp == __first)
4105            __push_empty();
4106        __push_alternation(__sa, __sb);
4107        __first = __temp;
4108    }
4109    return __first;
4110}
4111
4112template <class _CharT, class _Traits>
4113template <class _ForwardIterator>
4114_ForwardIterator
4115basic_regex<_CharT, _Traits>::__parse_alternative(_ForwardIterator __first,
4116                                                  _ForwardIterator __last)
4117{
4118    while (true)
4119    {
4120        _ForwardIterator __temp = __parse_term(__first, __last);
4121        if (__temp == __first)
4122            break;
4123        __first = __temp;
4124    }
4125    return __first;
4126}
4127
4128template <class _CharT, class _Traits>
4129template <class _ForwardIterator>
4130_ForwardIterator
4131basic_regex<_CharT, _Traits>::__parse_term(_ForwardIterator __first,
4132                                           _ForwardIterator __last)
4133{
4134    _ForwardIterator __temp = __parse_assertion(__first, __last);
4135    if (__temp == __first)
4136    {
4137        __owns_one_state<_CharT>* __e = __end_;
4138        unsigned __mexp_begin = __marked_count_;
4139        __temp = __parse_atom(__first, __last);
4140        if (__temp != __first)
4141            __first = __parse_ERE_dupl_symbol(__temp, __last, __e,
4142                                              __mexp_begin+1, __marked_count_+1);
4143    }
4144    else
4145        __first = __temp;
4146    return __first;
4147}
4148
4149template <class _CharT, class _Traits>
4150template <class _ForwardIterator>
4151_ForwardIterator
4152basic_regex<_CharT, _Traits>::__parse_assertion(_ForwardIterator __first,
4153                                                _ForwardIterator __last)
4154{
4155    if (__first != __last)
4156    {
4157        switch (*__first)
4158        {
4159        case '^':
4160            __push_l_anchor();
4161            ++__first;
4162            break;
4163        case '$':
4164            __push_r_anchor();
4165            ++__first;
4166            break;
4167        case '\\':
4168            {
4169                _ForwardIterator __temp = _VSTD::next(__first);
4170                if (__temp != __last)
4171                {
4172                    if (*__temp == 'b')
4173                    {
4174                        __push_word_boundary(false);
4175                        __first = ++__temp;
4176                    }
4177                    else if (*__temp == 'B')
4178                    {
4179                        __push_word_boundary(true);
4180                        __first = ++__temp;
4181                    }
4182                }
4183            }
4184            break;
4185        case '(':
4186            {
4187                _ForwardIterator __temp = _VSTD::next(__first);
4188                if (__temp != __last && *__temp == '?')
4189                {
4190                    if (++__temp != __last)
4191                    {
4192                        switch (*__temp)
4193                        {
4194                        case '=':
4195                            {
4196                                basic_regex __exp;
4197                                __exp.__flags_ = __flags_;
4198                                __temp = __exp.__parse(++__temp, __last);
4199                                unsigned __mexp = __exp.__marked_count_;
4200                                __push_lookahead(_VSTD::move(__exp), false, __marked_count_);
4201                                __marked_count_ += __mexp;
4202                                if (__temp == __last || *__temp != ')')
4203                                    __throw_regex_error<regex_constants::error_paren>();
4204                                __first = ++__temp;
4205                            }
4206                            break;
4207                        case '!':
4208                            {
4209                                basic_regex __exp;
4210                                __exp.__flags_ = __flags_;
4211                                __temp = __exp.__parse(++__temp, __last);
4212                                unsigned __mexp = __exp.__marked_count_;
4213                                __push_lookahead(_VSTD::move(__exp), true, __marked_count_);
4214                                __marked_count_ += __mexp;
4215                                if (__temp == __last || *__temp != ')')
4216                                    __throw_regex_error<regex_constants::error_paren>();
4217                                __first = ++__temp;
4218                            }
4219                            break;
4220                        }
4221                    }
4222                }
4223            }
4224            break;
4225        }
4226    }
4227    return __first;
4228}
4229
4230template <class _CharT, class _Traits>
4231template <class _ForwardIterator>
4232_ForwardIterator
4233basic_regex<_CharT, _Traits>::__parse_atom(_ForwardIterator __first,
4234                                           _ForwardIterator __last)
4235{
4236    if (__first != __last)
4237    {
4238        switch (*__first)
4239        {
4240        case '.':
4241            __push_match_any_but_newline();
4242            ++__first;
4243            break;
4244        case '\\':
4245            __first = __parse_atom_escape(__first, __last);
4246            break;
4247        case '[':
4248            __first = __parse_bracket_expression(__first, __last);
4249            break;
4250        case '(':
4251            {
4252                ++__first;
4253                if (__first == __last)
4254                    __throw_regex_error<regex_constants::error_paren>();
4255                _ForwardIterator __temp = _VSTD::next(__first);
4256                if (__temp != __last && *__first == '?' && *__temp == ':')
4257                {
4258                    ++__open_count_;
4259                    __first = __parse_ecma_exp(++__temp, __last);
4260                    if (__first == __last || *__first != ')')
4261                        __throw_regex_error<regex_constants::error_paren>();
4262                    --__open_count_;
4263                    ++__first;
4264                }
4265                else
4266                {
4267                    __push_begin_marked_subexpression();
4268                    unsigned __temp_count = __marked_count_;
4269                    ++__open_count_;
4270                    __first = __parse_ecma_exp(__first, __last);
4271                    if (__first == __last || *__first != ')')
4272                        __throw_regex_error<regex_constants::error_paren>();
4273                    __push_end_marked_subexpression(__temp_count);
4274                    --__open_count_;
4275                    ++__first;
4276                }
4277            }
4278            break;
4279        case '*':
4280        case '+':
4281        case '?':
4282        case '{':
4283            __throw_regex_error<regex_constants::error_badrepeat>();
4284            break;
4285        default:
4286            __first = __parse_pattern_character(__first, __last);
4287            break;
4288        }
4289    }
4290    return __first;
4291}
4292
4293template <class _CharT, class _Traits>
4294template <class _ForwardIterator>
4295_ForwardIterator
4296basic_regex<_CharT, _Traits>::__parse_atom_escape(_ForwardIterator __first,
4297                                                  _ForwardIterator __last)
4298{
4299    if (__first != __last && *__first == '\\')
4300    {
4301        _ForwardIterator __t1 = _VSTD::next(__first);
4302        if (__t1 == __last)
4303            __throw_regex_error<regex_constants::error_escape>();
4304
4305        _ForwardIterator __t2 = __parse_decimal_escape(__t1, __last);
4306        if (__t2 != __t1)
4307            __first = __t2;
4308        else
4309        {
4310            __t2 = __parse_character_class_escape(__t1, __last);
4311            if (__t2 != __t1)
4312                __first = __t2;
4313            else
4314            {
4315                __t2 = __parse_character_escape(__t1, __last);
4316                if (__t2 != __t1)
4317                    __first = __t2;
4318            }
4319        }
4320    }
4321    return __first;
4322}
4323
4324template <class _CharT, class _Traits>
4325template <class _ForwardIterator>
4326_ForwardIterator
4327basic_regex<_CharT, _Traits>::__parse_decimal_escape(_ForwardIterator __first,
4328                                                     _ForwardIterator __last)
4329{
4330    if (__first != __last)
4331    {
4332        if (*__first == '0')
4333        {
4334            __push_char(_CharT());
4335            ++__first;
4336        }
4337        else if ('1' <= *__first && *__first <= '9')
4338        {
4339            unsigned __v = *__first - '0';
4340            for (++__first;
4341                    __first != __last && '0' <= *__first && *__first <= '9'; ++__first)
4342                {
4343                if (__v >= std::numeric_limits<unsigned>::max() / 10)
4344                    __throw_regex_error<regex_constants::error_backref>();
4345                __v = 10 * __v + *__first - '0';
4346                }
4347            if (__v == 0 || __v > mark_count())
4348                __throw_regex_error<regex_constants::error_backref>();
4349            __push_back_ref(__v);
4350        }
4351    }
4352    return __first;
4353}
4354
4355template <class _CharT, class _Traits>
4356template <class _ForwardIterator>
4357_ForwardIterator
4358basic_regex<_CharT, _Traits>::__parse_character_class_escape(_ForwardIterator __first,
4359                                                             _ForwardIterator __last)
4360{
4361    if (__first != __last)
4362    {
4363        __bracket_expression<_CharT, _Traits>* __ml;
4364        switch (*__first)
4365        {
4366        case 'd':
4367            __ml = __start_matching_list(false);
4368            __ml->__add_class(ctype_base::digit);
4369            ++__first;
4370            break;
4371        case 'D':
4372            __ml = __start_matching_list(true);
4373            __ml->__add_class(ctype_base::digit);
4374            ++__first;
4375            break;
4376        case 's':
4377            __ml = __start_matching_list(false);
4378            __ml->__add_class(ctype_base::space);
4379            ++__first;
4380            break;
4381        case 'S':
4382            __ml = __start_matching_list(true);
4383            __ml->__add_class(ctype_base::space);
4384            ++__first;
4385            break;
4386        case 'w':
4387            __ml = __start_matching_list(false);
4388            __ml->__add_class(ctype_base::alnum);
4389            __ml->__add_char('_');
4390            ++__first;
4391            break;
4392        case 'W':
4393            __ml = __start_matching_list(true);
4394            __ml->__add_class(ctype_base::alnum);
4395            __ml->__add_char('_');
4396            ++__first;
4397            break;
4398        }
4399    }
4400    return __first;
4401}
4402
4403template <class _CharT, class _Traits>
4404template <class _ForwardIterator>
4405_ForwardIterator
4406basic_regex<_CharT, _Traits>::__parse_character_escape(_ForwardIterator __first,
4407                                                    _ForwardIterator __last,
4408                                                    basic_string<_CharT>* __str)
4409{
4410    if (__first != __last)
4411    {
4412        _ForwardIterator __t;
4413        unsigned __sum = 0;
4414        int __hd;
4415        switch (*__first)
4416        {
4417        case 'f':
4418            if (__str)
4419                *__str = _CharT(0xC);
4420            else
4421                __push_char(_CharT(0xC));
4422            ++__first;
4423            break;
4424        case 'n':
4425            if (__str)
4426                *__str = _CharT(0xA);
4427            else
4428                __push_char(_CharT(0xA));
4429            ++__first;
4430            break;
4431        case 'r':
4432            if (__str)
4433                *__str = _CharT(0xD);
4434            else
4435                __push_char(_CharT(0xD));
4436            ++__first;
4437            break;
4438        case 't':
4439            if (__str)
4440                *__str = _CharT(0x9);
4441            else
4442                __push_char(_CharT(0x9));
4443            ++__first;
4444            break;
4445        case 'v':
4446            if (__str)
4447                *__str = _CharT(0xB);
4448            else
4449                __push_char(_CharT(0xB));
4450            ++__first;
4451            break;
4452        case 'c':
4453            if ((__t = _VSTD::next(__first)) != __last)
4454            {
4455                if (('A' <= *__t && *__t <= 'Z') ||
4456                    ('a' <= *__t && *__t <= 'z'))
4457                {
4458                    if (__str)
4459                        *__str = _CharT(*__t % 32);
4460                    else
4461                        __push_char(_CharT(*__t % 32));
4462                    __first = ++__t;
4463                }
4464                else
4465                    __throw_regex_error<regex_constants::error_escape>();
4466            }
4467            else
4468                __throw_regex_error<regex_constants::error_escape>();
4469            break;
4470        case 'u':
4471            ++__first;
4472            if (__first == __last)
4473                __throw_regex_error<regex_constants::error_escape>();
4474            __hd = __traits_.value(*__first, 16);
4475            if (__hd == -1)
4476                __throw_regex_error<regex_constants::error_escape>();
4477            __sum = 16 * __sum + static_cast<unsigned>(__hd);
4478            ++__first;
4479            if (__first == __last)
4480                __throw_regex_error<regex_constants::error_escape>();
4481            __hd = __traits_.value(*__first, 16);
4482            if (__hd == -1)
4483                __throw_regex_error<regex_constants::error_escape>();
4484            __sum = 16 * __sum + static_cast<unsigned>(__hd);
4485            // drop through
4486        case 'x':
4487            ++__first;
4488            if (__first == __last)
4489                __throw_regex_error<regex_constants::error_escape>();
4490            __hd = __traits_.value(*__first, 16);
4491            if (__hd == -1)
4492                __throw_regex_error<regex_constants::error_escape>();
4493            __sum = 16 * __sum + static_cast<unsigned>(__hd);
4494            ++__first;
4495            if (__first == __last)
4496                __throw_regex_error<regex_constants::error_escape>();
4497            __hd = __traits_.value(*__first, 16);
4498            if (__hd == -1)
4499                __throw_regex_error<regex_constants::error_escape>();
4500            __sum = 16 * __sum + static_cast<unsigned>(__hd);
4501            if (__str)
4502                *__str = _CharT(__sum);
4503            else
4504                __push_char(_CharT(__sum));
4505            ++__first;
4506            break;
4507        case '0':
4508            if (__str)
4509                *__str = _CharT(0);
4510            else
4511                __push_char(_CharT(0));
4512            ++__first;
4513            break;
4514        default:
4515            if (*__first != '_' && !__traits_.isctype(*__first, ctype_base::alnum))
4516            {
4517                if (__str)
4518                    *__str = *__first;
4519                else
4520                    __push_char(*__first);
4521                ++__first;
4522            }
4523            else
4524                __throw_regex_error<regex_constants::error_escape>();
4525            break;
4526        }
4527    }
4528    return __first;
4529}
4530
4531template <class _CharT, class _Traits>
4532template <class _ForwardIterator>
4533_ForwardIterator
4534basic_regex<_CharT, _Traits>::__parse_pattern_character(_ForwardIterator __first,
4535                                                        _ForwardIterator __last)
4536{
4537    if (__first != __last)
4538    {
4539        switch (*__first)
4540        {
4541        case '^':
4542        case '$':
4543        case '\\':
4544        case '.':
4545        case '*':
4546        case '+':
4547        case '?':
4548        case '(':
4549        case ')':
4550        case '[':
4551        case ']':
4552        case '{':
4553        case '}':
4554        case '|':
4555            break;
4556        default:
4557            __push_char(*__first);
4558            ++__first;
4559            break;
4560        }
4561    }
4562    return __first;
4563}
4564
4565template <class _CharT, class _Traits>
4566template <class _ForwardIterator>
4567_ForwardIterator
4568basic_regex<_CharT, _Traits>::__parse_grep(_ForwardIterator __first,
4569                                           _ForwardIterator __last)
4570{
4571    __owns_one_state<_CharT>* __sa = __end_;
4572    _ForwardIterator __t1 = _VSTD::find(__first, __last, _CharT('\n'));
4573    if (__t1 != __first)
4574        __parse_basic_reg_exp(__first, __t1);
4575    else
4576        __push_empty();
4577    __first = __t1;
4578    if (__first != __last)
4579        ++__first;
4580    while (__first != __last)
4581    {
4582        __t1 = _VSTD::find(__first, __last, _CharT('\n'));
4583        __owns_one_state<_CharT>* __sb = __end_;
4584        if (__t1 != __first)
4585            __parse_basic_reg_exp(__first, __t1);
4586        else
4587            __push_empty();
4588        __push_alternation(__sa, __sb);
4589        __first = __t1;
4590        if (__first != __last)
4591            ++__first;
4592    }
4593    return __first;
4594}
4595
4596template <class _CharT, class _Traits>
4597template <class _ForwardIterator>
4598_ForwardIterator
4599basic_regex<_CharT, _Traits>::__parse_egrep(_ForwardIterator __first,
4600                                            _ForwardIterator __last)
4601{
4602    __owns_one_state<_CharT>* __sa = __end_;
4603    _ForwardIterator __t1 = _VSTD::find(__first, __last, _CharT('\n'));
4604    if (__t1 != __first)
4605        __parse_extended_reg_exp(__first, __t1);
4606    else
4607        __push_empty();
4608    __first = __t1;
4609    if (__first != __last)
4610        ++__first;
4611    while (__first != __last)
4612    {
4613        __t1 = _VSTD::find(__first, __last, _CharT('\n'));
4614        __owns_one_state<_CharT>* __sb = __end_;
4615        if (__t1 != __first)
4616            __parse_extended_reg_exp(__first, __t1);
4617        else
4618            __push_empty();
4619        __push_alternation(__sa, __sb);
4620        __first = __t1;
4621        if (__first != __last)
4622            ++__first;
4623    }
4624    return __first;
4625}
4626
4627template <class _CharT, class _Traits>
4628void
4629basic_regex<_CharT, _Traits>::__push_loop(size_t __min, size_t __max,
4630        __owns_one_state<_CharT>* __s, size_t __mexp_begin, size_t __mexp_end,
4631        bool __greedy)
4632{
4633    unique_ptr<__empty_state<_CharT> > __e1(new __empty_state<_CharT>(__end_->first()));
4634    __end_->first() = nullptr;
4635    unique_ptr<__loop<_CharT> > __e2(new __loop<_CharT>(__loop_count_,
4636                __s->first(), __e1.get(), __mexp_begin, __mexp_end, __greedy,
4637                __min, __max));
4638    __s->first() = nullptr;
4639    __e1.release();
4640    __end_->first() = new __repeat_one_loop<_CharT>(__e2.get());
4641    __end_ = __e2->second();
4642    __s->first() = __e2.release();
4643    ++__loop_count_;
4644}
4645
4646template <class _CharT, class _Traits>
4647void
4648basic_regex<_CharT, _Traits>::__push_char(value_type __c)
4649{
4650    if (flags() & icase)
4651        __end_->first() = new __match_char_icase<_CharT, _Traits>
4652                                              (__traits_, __c, __end_->first());
4653    else if (flags() & collate)
4654        __end_->first() = new __match_char_collate<_CharT, _Traits>
4655                                              (__traits_, __c, __end_->first());
4656    else
4657        __end_->first() = new __match_char<_CharT>(__c, __end_->first());
4658    __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4659}
4660
4661template <class _CharT, class _Traits>
4662void
4663basic_regex<_CharT, _Traits>::__push_begin_marked_subexpression()
4664{
4665    if (!(__flags_ & nosubs))
4666    {
4667        __end_->first() =
4668                new __begin_marked_subexpression<_CharT>(++__marked_count_,
4669                                                         __end_->first());
4670        __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4671    }
4672}
4673
4674template <class _CharT, class _Traits>
4675void
4676basic_regex<_CharT, _Traits>::__push_end_marked_subexpression(unsigned __sub)
4677{
4678    if (!(__flags_ & nosubs))
4679    {
4680        __end_->first() =
4681                new __end_marked_subexpression<_CharT>(__sub, __end_->first());
4682        __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4683    }
4684}
4685
4686template <class _CharT, class _Traits>
4687void
4688basic_regex<_CharT, _Traits>::__push_l_anchor()
4689{
4690    __end_->first() = new __l_anchor<_CharT>(__end_->first());
4691    __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4692}
4693
4694template <class _CharT, class _Traits>
4695void
4696basic_regex<_CharT, _Traits>::__push_r_anchor()
4697{
4698    __end_->first() = new __r_anchor<_CharT>(__end_->first());
4699    __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4700}
4701
4702template <class _CharT, class _Traits>
4703void
4704basic_regex<_CharT, _Traits>::__push_match_any()
4705{
4706    __end_->first() = new __match_any<_CharT>(__end_->first());
4707    __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4708}
4709
4710template <class _CharT, class _Traits>
4711void
4712basic_regex<_CharT, _Traits>::__push_match_any_but_newline()
4713{
4714    __end_->first() = new __match_any_but_newline<_CharT>(__end_->first());
4715    __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4716}
4717
4718template <class _CharT, class _Traits>
4719void
4720basic_regex<_CharT, _Traits>::__push_empty()
4721{
4722    __end_->first() = new __empty_state<_CharT>(__end_->first());
4723    __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4724}
4725
4726template <class _CharT, class _Traits>
4727void
4728basic_regex<_CharT, _Traits>::__push_word_boundary(bool __invert)
4729{
4730    __end_->first() = new __word_boundary<_CharT, _Traits>(__traits_, __invert,
4731                                                           __end_->first());
4732    __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4733}
4734
4735template <class _CharT, class _Traits>
4736void
4737basic_regex<_CharT, _Traits>::__push_back_ref(int __i)
4738{
4739    if (flags() & icase)
4740        __end_->first() = new __back_ref_icase<_CharT, _Traits>
4741                                              (__traits_, __i, __end_->first());
4742    else if (flags() & collate)
4743        __end_->first() = new __back_ref_collate<_CharT, _Traits>
4744                                              (__traits_, __i, __end_->first());
4745    else
4746        __end_->first() = new __back_ref<_CharT>(__i, __end_->first());
4747    __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4748}
4749
4750template <class _CharT, class _Traits>
4751void
4752basic_regex<_CharT, _Traits>::__push_alternation(__owns_one_state<_CharT>* __sa,
4753                                                 __owns_one_state<_CharT>* __ea)
4754{
4755    __sa->first() = new __alternate<_CharT>(
4756                         static_cast<__owns_one_state<_CharT>*>(__sa->first()),
4757                         static_cast<__owns_one_state<_CharT>*>(__ea->first()));
4758    __ea->first() = nullptr;
4759    __ea->first() = new __empty_state<_CharT>(__end_->first());
4760    __end_->first() = nullptr;
4761    __end_->first() = new __empty_non_own_state<_CharT>(__ea->first());
4762    __end_ = static_cast<__owns_one_state<_CharT>*>(__ea->first());
4763}
4764
4765template <class _CharT, class _Traits>
4766__bracket_expression<_CharT, _Traits>*
4767basic_regex<_CharT, _Traits>::__start_matching_list(bool __negate)
4768{
4769    __bracket_expression<_CharT, _Traits>* __r =
4770        new __bracket_expression<_CharT, _Traits>(__traits_, __end_->first(),
4771                                                  __negate, __flags_ & icase,
4772                                                  __flags_ & collate);
4773    __end_->first() = __r;
4774    __end_ = __r;
4775    return __r;
4776}
4777
4778template <class _CharT, class _Traits>
4779void
4780basic_regex<_CharT, _Traits>::__push_lookahead(const basic_regex& __exp,
4781                                               bool __invert,
4782                                               unsigned __mexp)
4783{
4784    __end_->first() = new __lookahead<_CharT, _Traits>(__exp, __invert,
4785                                                           __end_->first(), __mexp);
4786    __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4787}
4788
4789typedef basic_regex<char>    regex;
4790typedef basic_regex<wchar_t> wregex;
4791
4792// sub_match
4793
4794template <class _BidirectionalIterator>
4795class _LIBCPP_TEMPLATE_VIS sub_match
4796    : public pair<_BidirectionalIterator, _BidirectionalIterator>
4797{
4798public:
4799    typedef _BidirectionalIterator                              iterator;
4800    typedef typename iterator_traits<iterator>::value_type      value_type;
4801    typedef typename iterator_traits<iterator>::difference_type difference_type;
4802    typedef basic_string<value_type>                            string_type;
4803
4804    bool matched;
4805
4806    _LIBCPP_INLINE_VISIBILITY
4807    _LIBCPP_CONSTEXPR sub_match() : matched() {}
4808
4809    _LIBCPP_INLINE_VISIBILITY
4810    difference_type length() const
4811        {return matched ? _VSTD::distance(this->first, this->second) : 0;}
4812    _LIBCPP_INLINE_VISIBILITY
4813    string_type str() const
4814        {return matched ? string_type(this->first, this->second) : string_type();}
4815    _LIBCPP_INLINE_VISIBILITY
4816    operator string_type() const
4817        {return str();}
4818
4819    _LIBCPP_INLINE_VISIBILITY
4820    int compare(const sub_match& __s) const
4821        {return str().compare(__s.str());}
4822    _LIBCPP_INLINE_VISIBILITY
4823    int compare(const string_type& __s) const
4824        {return str().compare(__s);}
4825    _LIBCPP_INLINE_VISIBILITY
4826    int compare(const value_type* __s) const
4827        {return str().compare(__s);}
4828};
4829
4830typedef sub_match<const char*>             csub_match;
4831typedef sub_match<const wchar_t*>          wcsub_match;
4832typedef sub_match<string::const_iterator>  ssub_match;
4833typedef sub_match<wstring::const_iterator> wssub_match;
4834
4835template <class _BiIter>
4836inline _LIBCPP_INLINE_VISIBILITY
4837bool
4838operator==(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
4839{
4840    return __x.compare(__y) == 0;
4841}
4842
4843template <class _BiIter>
4844inline _LIBCPP_INLINE_VISIBILITY
4845bool
4846operator!=(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
4847{
4848    return !(__x == __y);
4849}
4850
4851template <class _BiIter>
4852inline _LIBCPP_INLINE_VISIBILITY
4853bool
4854operator<(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
4855{
4856    return __x.compare(__y) < 0;
4857}
4858
4859template <class _BiIter>
4860inline _LIBCPP_INLINE_VISIBILITY
4861bool
4862operator<=(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
4863{
4864    return !(__y < __x);
4865}
4866
4867template <class _BiIter>
4868inline _LIBCPP_INLINE_VISIBILITY
4869bool
4870operator>=(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
4871{
4872    return !(__x < __y);
4873}
4874
4875template <class _BiIter>
4876inline _LIBCPP_INLINE_VISIBILITY
4877bool
4878operator>(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
4879{
4880    return __y < __x;
4881}
4882
4883template <class _BiIter, class _ST, class _SA>
4884inline _LIBCPP_INLINE_VISIBILITY
4885bool
4886operator==(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4887           const sub_match<_BiIter>& __y)
4888{
4889    return __y.compare(typename sub_match<_BiIter>::string_type(__x.data(), __x.size())) == 0;
4890}
4891
4892template <class _BiIter, class _ST, class _SA>
4893inline _LIBCPP_INLINE_VISIBILITY
4894bool
4895operator!=(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4896           const sub_match<_BiIter>& __y)
4897{
4898    return !(__x == __y);
4899}
4900
4901template <class _BiIter, class _ST, class _SA>
4902inline _LIBCPP_INLINE_VISIBILITY
4903bool
4904operator<(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4905          const sub_match<_BiIter>& __y)
4906{
4907    return __y.compare(typename sub_match<_BiIter>::string_type(__x.data(), __x.size())) > 0;
4908}
4909
4910template <class _BiIter, class _ST, class _SA>
4911inline _LIBCPP_INLINE_VISIBILITY
4912bool
4913operator>(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4914          const sub_match<_BiIter>& __y)
4915{
4916    return __y < __x;
4917}
4918
4919template <class _BiIter, class _ST, class _SA>
4920inline _LIBCPP_INLINE_VISIBILITY
4921bool operator>=(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4922                const sub_match<_BiIter>& __y)
4923{
4924    return !(__x < __y);
4925}
4926
4927template <class _BiIter, class _ST, class _SA>
4928inline _LIBCPP_INLINE_VISIBILITY
4929bool
4930operator<=(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4931           const sub_match<_BiIter>& __y)
4932{
4933    return !(__y < __x);
4934}
4935
4936template <class _BiIter, class _ST, class _SA>
4937inline _LIBCPP_INLINE_VISIBILITY
4938bool
4939operator==(const sub_match<_BiIter>& __x,
4940           const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
4941{
4942    return __x.compare(typename sub_match<_BiIter>::string_type(__y.data(), __y.size())) == 0;
4943}
4944
4945template <class _BiIter, class _ST, class _SA>
4946inline _LIBCPP_INLINE_VISIBILITY
4947bool
4948operator!=(const sub_match<_BiIter>& __x,
4949           const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
4950{
4951    return !(__x == __y);
4952}
4953
4954template <class _BiIter, class _ST, class _SA>
4955inline _LIBCPP_INLINE_VISIBILITY
4956bool
4957operator<(const sub_match<_BiIter>& __x,
4958          const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
4959{
4960    return __x.compare(typename sub_match<_BiIter>::string_type(__y.data(), __y.size())) < 0;
4961}
4962
4963template <class _BiIter, class _ST, class _SA>
4964inline _LIBCPP_INLINE_VISIBILITY
4965bool operator>(const sub_match<_BiIter>& __x,
4966               const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
4967{
4968    return __y < __x;
4969}
4970
4971template <class _BiIter, class _ST, class _SA>
4972inline _LIBCPP_INLINE_VISIBILITY
4973bool
4974operator>=(const sub_match<_BiIter>& __x,
4975           const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
4976{
4977    return !(__x < __y);
4978}
4979
4980template <class _BiIter, class _ST, class _SA>
4981inline _LIBCPP_INLINE_VISIBILITY
4982bool
4983operator<=(const sub_match<_BiIter>& __x,
4984           const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
4985{
4986    return !(__y < __x);
4987}
4988
4989template <class _BiIter>
4990inline _LIBCPP_INLINE_VISIBILITY
4991bool
4992operator==(typename iterator_traits<_BiIter>::value_type const* __x,
4993           const sub_match<_BiIter>& __y)
4994{
4995    return __y.compare(__x) == 0;
4996}
4997
4998template <class _BiIter>
4999inline _LIBCPP_INLINE_VISIBILITY
5000bool
5001operator!=(typename iterator_traits<_BiIter>::value_type const* __x,
5002           const sub_match<_BiIter>& __y)
5003{
5004    return !(__x == __y);
5005}
5006
5007template <class _BiIter>
5008inline _LIBCPP_INLINE_VISIBILITY
5009bool
5010operator<(typename iterator_traits<_BiIter>::value_type const* __x,
5011          const sub_match<_BiIter>& __y)
5012{
5013    return __y.compare(__x) > 0;
5014}
5015
5016template <class _BiIter>
5017inline _LIBCPP_INLINE_VISIBILITY
5018bool
5019operator>(typename iterator_traits<_BiIter>::value_type const* __x,
5020          const sub_match<_BiIter>& __y)
5021{
5022    return __y < __x;
5023}
5024
5025template <class _BiIter>
5026inline _LIBCPP_INLINE_VISIBILITY
5027bool
5028operator>=(typename iterator_traits<_BiIter>::value_type const* __x,
5029           const sub_match<_BiIter>& __y)
5030{
5031    return !(__x < __y);
5032}
5033
5034template <class _BiIter>
5035inline _LIBCPP_INLINE_VISIBILITY
5036bool
5037operator<=(typename iterator_traits<_BiIter>::value_type const* __x,
5038           const sub_match<_BiIter>& __y)
5039{
5040    return !(__y < __x);
5041}
5042
5043template <class _BiIter>
5044inline _LIBCPP_INLINE_VISIBILITY
5045bool
5046operator==(const sub_match<_BiIter>& __x,
5047           typename iterator_traits<_BiIter>::value_type const* __y)
5048{
5049    return __x.compare(__y) == 0;
5050}
5051
5052template <class _BiIter>
5053inline _LIBCPP_INLINE_VISIBILITY
5054bool
5055operator!=(const sub_match<_BiIter>& __x,
5056           typename iterator_traits<_BiIter>::value_type const* __y)
5057{
5058    return !(__x == __y);
5059}
5060
5061template <class _BiIter>
5062inline _LIBCPP_INLINE_VISIBILITY
5063bool
5064operator<(const sub_match<_BiIter>& __x,
5065          typename iterator_traits<_BiIter>::value_type const* __y)
5066{
5067    return __x.compare(__y) < 0;
5068}
5069
5070template <class _BiIter>
5071inline _LIBCPP_INLINE_VISIBILITY
5072bool
5073operator>(const sub_match<_BiIter>& __x,
5074          typename iterator_traits<_BiIter>::value_type const* __y)
5075{
5076    return __y < __x;
5077}
5078
5079template <class _BiIter>
5080inline _LIBCPP_INLINE_VISIBILITY
5081bool
5082operator>=(const sub_match<_BiIter>& __x,
5083           typename iterator_traits<_BiIter>::value_type const* __y)
5084{
5085    return !(__x < __y);
5086}
5087
5088template <class _BiIter>
5089inline _LIBCPP_INLINE_VISIBILITY
5090bool
5091operator<=(const sub_match<_BiIter>& __x,
5092           typename iterator_traits<_BiIter>::value_type const* __y)
5093{
5094    return !(__y < __x);
5095}
5096
5097template <class _BiIter>
5098inline _LIBCPP_INLINE_VISIBILITY
5099bool
5100operator==(typename iterator_traits<_BiIter>::value_type const& __x,
5101           const sub_match<_BiIter>& __y)
5102{
5103    typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
5104    return __y.compare(string_type(1, __x)) == 0;
5105}
5106
5107template <class _BiIter>
5108inline _LIBCPP_INLINE_VISIBILITY
5109bool
5110operator!=(typename iterator_traits<_BiIter>::value_type const& __x,
5111           const sub_match<_BiIter>& __y)
5112{
5113    return !(__x == __y);
5114}
5115
5116template <class _BiIter>
5117inline _LIBCPP_INLINE_VISIBILITY
5118bool
5119operator<(typename iterator_traits<_BiIter>::value_type const& __x,
5120          const sub_match<_BiIter>& __y)
5121{
5122    typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
5123    return __y.compare(string_type(1, __x)) > 0;
5124}
5125
5126template <class _BiIter>
5127inline _LIBCPP_INLINE_VISIBILITY
5128bool
5129operator>(typename iterator_traits<_BiIter>::value_type const& __x,
5130          const sub_match<_BiIter>& __y)
5131{
5132    return __y < __x;
5133}
5134
5135template <class _BiIter>
5136inline _LIBCPP_INLINE_VISIBILITY
5137bool
5138operator>=(typename iterator_traits<_BiIter>::value_type const& __x,
5139           const sub_match<_BiIter>& __y)
5140{
5141    return !(__x < __y);
5142}
5143
5144template <class _BiIter>
5145inline _LIBCPP_INLINE_VISIBILITY
5146bool
5147operator<=(typename iterator_traits<_BiIter>::value_type const& __x,
5148           const sub_match<_BiIter>& __y)
5149{
5150    return !(__y < __x);
5151}
5152
5153template <class _BiIter>
5154inline _LIBCPP_INLINE_VISIBILITY
5155bool
5156operator==(const sub_match<_BiIter>& __x,
5157           typename iterator_traits<_BiIter>::value_type const& __y)
5158{
5159    typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
5160    return __x.compare(string_type(1, __y)) == 0;
5161}
5162
5163template <class _BiIter>
5164inline _LIBCPP_INLINE_VISIBILITY
5165bool
5166operator!=(const sub_match<_BiIter>& __x,
5167           typename iterator_traits<_BiIter>::value_type const& __y)
5168{
5169    return !(__x == __y);
5170}
5171
5172template <class _BiIter>
5173inline _LIBCPP_INLINE_VISIBILITY
5174bool
5175operator<(const sub_match<_BiIter>& __x,
5176          typename iterator_traits<_BiIter>::value_type const& __y)
5177{
5178    typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
5179    return __x.compare(string_type(1, __y)) < 0;
5180}
5181
5182template <class _BiIter>
5183inline _LIBCPP_INLINE_VISIBILITY
5184bool
5185operator>(const sub_match<_BiIter>& __x,
5186          typename iterator_traits<_BiIter>::value_type const& __y)
5187{
5188    return __y < __x;
5189}
5190
5191template <class _BiIter>
5192inline _LIBCPP_INLINE_VISIBILITY
5193bool
5194operator>=(const sub_match<_BiIter>& __x,
5195           typename iterator_traits<_BiIter>::value_type const& __y)
5196{
5197    return !(__x < __y);
5198}
5199
5200template <class _BiIter>
5201inline _LIBCPP_INLINE_VISIBILITY
5202bool
5203operator<=(const sub_match<_BiIter>& __x,
5204           typename iterator_traits<_BiIter>::value_type const& __y)
5205{
5206    return !(__y < __x);
5207}
5208
5209template <class _CharT, class _ST, class _BiIter>
5210inline _LIBCPP_INLINE_VISIBILITY
5211basic_ostream<_CharT, _ST>&
5212operator<<(basic_ostream<_CharT, _ST>& __os, const sub_match<_BiIter>& __m)
5213{
5214    return __os << __m.str();
5215}
5216
5217template <class _BidirectionalIterator, class _Allocator>
5218class _LIBCPP_TEMPLATE_VIS match_results
5219{
5220public:
5221    typedef _Allocator                                        allocator_type;
5222    typedef sub_match<_BidirectionalIterator>                 value_type;
5223private:
5224    typedef vector<value_type, allocator_type>                __container_type;
5225
5226    __container_type  __matches_;
5227    value_type __unmatched_;
5228    value_type __prefix_;
5229    value_type __suffix_;
5230    bool       __ready_;
5231public:
5232    _BidirectionalIterator __position_start_;
5233    typedef const value_type&                                 const_reference;
5234    typedef value_type&                                       reference;
5235    typedef typename __container_type::const_iterator         const_iterator;
5236    typedef const_iterator                                    iterator;
5237    typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
5238    typedef typename allocator_traits<allocator_type>::size_type size_type;
5239    typedef typename iterator_traits<_BidirectionalIterator>::value_type char_type;
5240    typedef basic_string<char_type>                           string_type;
5241
5242    // construct/copy/destroy:
5243    explicit match_results(const allocator_type& __a = allocator_type());
5244//    match_results(const match_results&) = default;
5245//    match_results& operator=(const match_results&) = default;
5246//    match_results(match_results&& __m) = default;
5247//    match_results& operator=(match_results&& __m) = default;
5248//    ~match_results() = default;
5249
5250    _LIBCPP_INLINE_VISIBILITY
5251    bool ready() const {return __ready_;}
5252
5253    // size:
5254    _LIBCPP_INLINE_VISIBILITY
5255    size_type size() const _NOEXCEPT {return __matches_.size();}
5256    _LIBCPP_INLINE_VISIBILITY
5257    size_type max_size() const _NOEXCEPT {return __matches_.max_size();}
5258    _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
5259    bool empty() const _NOEXCEPT {return size() == 0;}
5260
5261    // element access:
5262    _LIBCPP_INLINE_VISIBILITY
5263    difference_type length(size_type __sub = 0) const
5264        {return (*this)[__sub].length();}
5265    _LIBCPP_INLINE_VISIBILITY
5266    difference_type position(size_type __sub = 0) const
5267        {return _VSTD::distance(__position_start_, (*this)[__sub].first);}
5268    _LIBCPP_INLINE_VISIBILITY
5269    string_type str(size_type __sub = 0) const
5270        {return (*this)[__sub].str();}
5271    _LIBCPP_INLINE_VISIBILITY
5272    const_reference operator[](size_type __n) const
5273        {return __n < __matches_.size() ? __matches_[__n] : __unmatched_;}
5274
5275    _LIBCPP_INLINE_VISIBILITY
5276    const_reference prefix() const {return __prefix_;}
5277    _LIBCPP_INLINE_VISIBILITY
5278    const_reference suffix() const {return __suffix_;}
5279
5280    _LIBCPP_INLINE_VISIBILITY
5281    const_iterator begin() const {return empty() ? __matches_.end() : __matches_.begin();}
5282    _LIBCPP_INLINE_VISIBILITY
5283    const_iterator end() const {return __matches_.end();}
5284    _LIBCPP_INLINE_VISIBILITY
5285    const_iterator cbegin() const {return empty() ? __matches_.end() : __matches_.begin();}
5286    _LIBCPP_INLINE_VISIBILITY
5287    const_iterator cend() const {return __matches_.end();}
5288
5289    // format:
5290    template <class _OutputIter>
5291        _OutputIter
5292        format(_OutputIter __output_iter, const char_type* __fmt_first,
5293               const char_type* __fmt_last,
5294               regex_constants::match_flag_type __flags = regex_constants::format_default) const;
5295    template <class _OutputIter, class _ST, class _SA>
5296        _LIBCPP_INLINE_VISIBILITY
5297        _OutputIter
5298        format(_OutputIter __output_iter, const basic_string<char_type, _ST, _SA>& __fmt,
5299               regex_constants::match_flag_type __flags = regex_constants::format_default) const
5300            {return format(__output_iter, __fmt.data(), __fmt.data() + __fmt.size(), __flags);}
5301    template <class _ST, class _SA>
5302        _LIBCPP_INLINE_VISIBILITY
5303        basic_string<char_type, _ST, _SA>
5304        format(const basic_string<char_type, _ST, _SA>& __fmt,
5305               regex_constants::match_flag_type __flags = regex_constants::format_default) const
5306        {
5307            basic_string<char_type, _ST, _SA> __r;
5308            format(back_inserter(__r), __fmt.data(), __fmt.data() + __fmt.size(),
5309                   __flags);
5310            return __r;
5311        }
5312    _LIBCPP_INLINE_VISIBILITY
5313    string_type
5314        format(const char_type* __fmt,
5315               regex_constants::match_flag_type __flags = regex_constants::format_default) const
5316        {
5317            string_type __r;
5318            format(back_inserter(__r), __fmt,
5319                   __fmt + char_traits<char_type>::length(__fmt), __flags);
5320            return __r;
5321        }
5322
5323    // allocator:
5324    _LIBCPP_INLINE_VISIBILITY
5325    allocator_type get_allocator() const {return __matches_.get_allocator();}
5326
5327    // swap:
5328    void swap(match_results& __m);
5329
5330    template <class _Bp, class _Ap>
5331        _LIBCPP_INLINE_VISIBILITY
5332        void __assign(_BidirectionalIterator __f, _BidirectionalIterator __l,
5333                      const match_results<_Bp, _Ap>& __m, bool __no_update_pos)
5334    {
5335        _Bp __mf = __m.prefix().first;
5336        __matches_.resize(__m.size());
5337        for (size_type __i = 0; __i < __matches_.size(); ++__i)
5338        {
5339            __matches_[__i].first = _VSTD::next(__f, _VSTD::distance(__mf, __m[__i].first));
5340            __matches_[__i].second = _VSTD::next(__f, _VSTD::distance(__mf, __m[__i].second));
5341            __matches_[__i].matched = __m[__i].matched;
5342        }
5343        __unmatched_.first   = __l;
5344        __unmatched_.second  = __l;
5345        __unmatched_.matched = false;
5346        __prefix_.first = _VSTD::next(__f, _VSTD::distance(__mf, __m.prefix().first));
5347        __prefix_.second = _VSTD::next(__f, _VSTD::distance(__mf, __m.prefix().second));
5348        __prefix_.matched = __m.prefix().matched;
5349        __suffix_.first = _VSTD::next(__f, _VSTD::distance(__mf, __m.suffix().first));
5350        __suffix_.second = _VSTD::next(__f, _VSTD::distance(__mf, __m.suffix().second));
5351        __suffix_.matched = __m.suffix().matched;
5352        if (!__no_update_pos)
5353            __position_start_ = __prefix_.first;
5354        __ready_ = __m.ready();
5355    }
5356
5357private:
5358    void __init(unsigned __s,
5359                _BidirectionalIterator __f, _BidirectionalIterator __l,
5360                bool __no_update_pos = false);
5361
5362    template <class, class> friend class basic_regex;
5363
5364    template <class _Bp, class _Ap, class _Cp, class _Tp>
5365    friend
5366    bool
5367    regex_match(_Bp, _Bp, match_results<_Bp, _Ap>&, const basic_regex<_Cp, _Tp>&,
5368                regex_constants::match_flag_type);
5369
5370    template <class _Bp, class _Ap>
5371    friend
5372    bool
5373    operator==(const match_results<_Bp, _Ap>&, const match_results<_Bp, _Ap>&);
5374
5375    template <class, class> friend class __lookahead;
5376};
5377
5378template <class _BidirectionalIterator, class _Allocator>
5379match_results<_BidirectionalIterator, _Allocator>::match_results(
5380        const allocator_type& __a)
5381    : __matches_(__a),
5382      __unmatched_(),
5383      __prefix_(),
5384      __suffix_(),
5385      __ready_(false),
5386      __position_start_()
5387{
5388}
5389
5390template <class _BidirectionalIterator, class _Allocator>
5391void
5392match_results<_BidirectionalIterator, _Allocator>::__init(unsigned __s,
5393                         _BidirectionalIterator __f, _BidirectionalIterator __l,
5394                         bool __no_update_pos)
5395{
5396    __unmatched_.first   = __l;
5397    __unmatched_.second  = __l;
5398    __unmatched_.matched = false;
5399    __matches_.assign(__s, __unmatched_);
5400    __prefix_.first      = __f;
5401    __prefix_.second     = __f;
5402    __prefix_.matched    = false;
5403    __suffix_ = __unmatched_;
5404    if (!__no_update_pos)
5405        __position_start_ = __prefix_.first;
5406    __ready_ = true;
5407}
5408
5409template <class _BidirectionalIterator, class _Allocator>
5410template <class _OutputIter>
5411_OutputIter
5412match_results<_BidirectionalIterator, _Allocator>::format(_OutputIter __output_iter,
5413        const char_type* __fmt_first, const char_type* __fmt_last,
5414        regex_constants::match_flag_type __flags) const
5415{
5416    if (__flags & regex_constants::format_sed)
5417    {
5418        for (; __fmt_first != __fmt_last; ++__fmt_first)
5419        {
5420            if (*__fmt_first == '&')
5421                __output_iter = _VSTD::copy(__matches_[0].first, __matches_[0].second,
5422                                   __output_iter);
5423            else if (*__fmt_first == '\\' && __fmt_first + 1 != __fmt_last)
5424            {
5425                ++__fmt_first;
5426                if ('0' <= *__fmt_first && *__fmt_first <= '9')
5427                {
5428                    size_t __i = *__fmt_first - '0';
5429                    __output_iter = _VSTD::copy((*this)[__i].first,
5430                                        (*this)[__i].second, __output_iter);
5431                }
5432                else
5433                {
5434                    *__output_iter = *__fmt_first;
5435                    ++__output_iter;
5436                }
5437            }
5438            else
5439            {
5440                *__output_iter = *__fmt_first;
5441                ++__output_iter;
5442            }
5443        }
5444    }
5445    else
5446    {
5447        for (; __fmt_first != __fmt_last; ++__fmt_first)
5448        {
5449            if (*__fmt_first == '$' && __fmt_first + 1 != __fmt_last)
5450            {
5451                switch (__fmt_first[1])
5452                {
5453                case '$':
5454                    *__output_iter = *++__fmt_first;
5455                    ++__output_iter;
5456                    break;
5457                case '&':
5458                    ++__fmt_first;
5459                    __output_iter = _VSTD::copy(__matches_[0].first, __matches_[0].second,
5460                                       __output_iter);
5461                    break;
5462                case '`':
5463                    ++__fmt_first;
5464                    __output_iter = _VSTD::copy(__prefix_.first, __prefix_.second, __output_iter);
5465                    break;
5466                case '\'':
5467                    ++__fmt_first;
5468                    __output_iter = _VSTD::copy(__suffix_.first, __suffix_.second, __output_iter);
5469                    break;
5470                default:
5471                    if ('0' <= __fmt_first[1] && __fmt_first[1] <= '9')
5472                    {
5473                        ++__fmt_first;
5474                        size_t __idx = *__fmt_first - '0';
5475                        if (__fmt_first + 1 != __fmt_last &&
5476                            '0' <= __fmt_first[1] && __fmt_first[1] <= '9')
5477                        {
5478                            ++__fmt_first;
5479                            if (__idx >= std::numeric_limits<size_t>::max() / 10)
5480                                __throw_regex_error<regex_constants::error_escape>();
5481                            __idx = 10 * __idx + *__fmt_first - '0';
5482                        }
5483                        __output_iter = _VSTD::copy((*this)[__idx].first,
5484                                            (*this)[__idx].second, __output_iter);
5485                    }
5486                    else
5487                    {
5488                        *__output_iter = *__fmt_first;
5489                        ++__output_iter;
5490                    }
5491                    break;
5492                }
5493            }
5494            else
5495            {
5496                *__output_iter = *__fmt_first;
5497                ++__output_iter;
5498            }
5499        }
5500    }
5501    return __output_iter;
5502}
5503
5504template <class _BidirectionalIterator, class _Allocator>
5505void
5506match_results<_BidirectionalIterator, _Allocator>::swap(match_results& __m)
5507{
5508    using _VSTD::swap;
5509    swap(__matches_, __m.__matches_);
5510    swap(__unmatched_, __m.__unmatched_);
5511    swap(__prefix_, __m.__prefix_);
5512    swap(__suffix_, __m.__suffix_);
5513    swap(__position_start_, __m.__position_start_);
5514    swap(__ready_, __m.__ready_);
5515}
5516
5517typedef match_results<const char*>             cmatch;
5518typedef match_results<const wchar_t*>          wcmatch;
5519typedef match_results<string::const_iterator>  smatch;
5520typedef match_results<wstring::const_iterator> wsmatch;
5521
5522template <class _BidirectionalIterator, class _Allocator>
5523bool
5524operator==(const match_results<_BidirectionalIterator, _Allocator>& __x,
5525           const match_results<_BidirectionalIterator, _Allocator>& __y)
5526{
5527    if (__x.__ready_ != __y.__ready_)
5528        return false;
5529    if (!__x.__ready_)
5530        return true;
5531    return __x.__matches_ == __y.__matches_ &&
5532           __x.__prefix_ == __y.__prefix_ &&
5533           __x.__suffix_ == __y.__suffix_;
5534}
5535
5536template <class _BidirectionalIterator, class _Allocator>
5537inline _LIBCPP_INLINE_VISIBILITY
5538bool
5539operator!=(const match_results<_BidirectionalIterator, _Allocator>& __x,
5540           const match_results<_BidirectionalIterator, _Allocator>& __y)
5541{
5542    return !(__x == __y);
5543}
5544
5545template <class _BidirectionalIterator, class _Allocator>
5546inline _LIBCPP_INLINE_VISIBILITY
5547void
5548swap(match_results<_BidirectionalIterator, _Allocator>& __x,
5549     match_results<_BidirectionalIterator, _Allocator>& __y)
5550{
5551    __x.swap(__y);
5552}
5553
5554// regex_search
5555
5556template <class _CharT, class _Traits>
5557template <class _Allocator>
5558bool
5559basic_regex<_CharT, _Traits>::__match_at_start_ecma(
5560        const _CharT* __first, const _CharT* __last,
5561        match_results<const _CharT*, _Allocator>& __m,
5562        regex_constants::match_flag_type __flags, bool __at_first) const
5563{
5564    vector<__state> __states;
5565    __node* __st = __start_.get();
5566    if (__st)
5567    {
5568        sub_match<const _CharT*> __unmatched;
5569        __unmatched.first   = __last;
5570        __unmatched.second  = __last;
5571        __unmatched.matched = false;
5572
5573        __states.push_back(__state());
5574        __states.back().__do_ = 0;
5575        __states.back().__first_ = __first;
5576        __states.back().__current_ = __first;
5577        __states.back().__last_ = __last;
5578        __states.back().__sub_matches_.resize(mark_count(), __unmatched);
5579        __states.back().__loop_data_.resize(__loop_count());
5580        __states.back().__node_ = __st;
5581        __states.back().__flags_ = __flags;
5582        __states.back().__at_first_ = __at_first;
5583        int __counter = 0;
5584        int __length = __last - __first;
5585        do
5586        {
5587            ++__counter;
5588            if (__counter % _LIBCPP_REGEX_COMPLEXITY_FACTOR == 0 &&
5589                __counter / _LIBCPP_REGEX_COMPLEXITY_FACTOR >= __length)
5590              __throw_regex_error<regex_constants::error_complexity>();
5591            __state& __s = __states.back();
5592            if (__s.__node_)
5593                __s.__node_->__exec(__s);
5594            switch (__s.__do_)
5595            {
5596            case __state::__end_state:
5597                if ((__flags & regex_constants::match_not_null) &&
5598                    __s.__current_ == __first)
5599                {
5600                  __states.pop_back();
5601                  break;
5602                }
5603                if ((__flags & regex_constants::__full_match) &&
5604                    __s.__current_ != __last)
5605                {
5606                  __states.pop_back();
5607                  break;
5608                }
5609                __m.__matches_[0].first = __first;
5610                __m.__matches_[0].second = _VSTD::next(__first, __s.__current_ - __first);
5611                __m.__matches_[0].matched = true;
5612                for (unsigned __i = 0; __i < __s.__sub_matches_.size(); ++__i)
5613                    __m.__matches_[__i+1] = __s.__sub_matches_[__i];
5614                return true;
5615            case __state::__accept_and_consume:
5616            case __state::__repeat:
5617            case __state::__accept_but_not_consume:
5618                break;
5619            case __state::__split:
5620                {
5621                __state __snext = __s;
5622                __s.__node_->__exec_split(true, __s);
5623                __snext.__node_->__exec_split(false, __snext);
5624                __states.push_back(_VSTD::move(__snext));
5625                }
5626                break;
5627            case __state::__reject:
5628                __states.pop_back();
5629                break;
5630            default:
5631                __throw_regex_error<regex_constants::__re_err_unknown>();
5632                break;
5633
5634            }
5635        } while (!__states.empty());
5636    }
5637    return false;
5638}
5639
5640template <class _CharT, class _Traits>
5641template <class _Allocator>
5642bool
5643basic_regex<_CharT, _Traits>::__match_at_start_posix_nosubs(
5644        const _CharT* __first, const _CharT* __last,
5645        match_results<const _CharT*, _Allocator>& __m,
5646        regex_constants::match_flag_type __flags, bool __at_first) const
5647{
5648    deque<__state> __states;
5649    ptrdiff_t __highest_j = 0;
5650    ptrdiff_t _Np = _VSTD::distance(__first, __last);
5651    __node* __st = __start_.get();
5652    if (__st)
5653    {
5654        __states.push_back(__state());
5655        __states.back().__do_ = 0;
5656        __states.back().__first_ = __first;
5657        __states.back().__current_ = __first;
5658        __states.back().__last_ = __last;
5659        __states.back().__loop_data_.resize(__loop_count());
5660        __states.back().__node_ = __st;
5661        __states.back().__flags_ = __flags;
5662        __states.back().__at_first_ = __at_first;
5663        bool __matched = false;
5664        int __counter = 0;
5665        int __length = __last - __first;
5666        do
5667        {
5668            ++__counter;
5669            if (__counter % _LIBCPP_REGEX_COMPLEXITY_FACTOR == 0 &&
5670                __counter / _LIBCPP_REGEX_COMPLEXITY_FACTOR >= __length)
5671              __throw_regex_error<regex_constants::error_complexity>();
5672            __state& __s = __states.back();
5673            if (__s.__node_)
5674                __s.__node_->__exec(__s);
5675            switch (__s.__do_)
5676            {
5677            case __state::__end_state:
5678                if ((__flags & regex_constants::match_not_null) &&
5679                    __s.__current_ == __first)
5680                {
5681                  __states.pop_back();
5682                  break;
5683                }
5684                if ((__flags & regex_constants::__full_match) &&
5685                    __s.__current_ != __last)
5686                {
5687                  __states.pop_back();
5688                  break;
5689                }
5690                if (!__matched || __highest_j < __s.__current_ - __s.__first_)
5691                    __highest_j = __s.__current_ - __s.__first_;
5692                __matched = true;
5693                if (__highest_j == _Np)
5694                    __states.clear();
5695                else
5696                    __states.pop_back();
5697                break;
5698            case __state::__consume_input:
5699                break;
5700            case __state::__accept_and_consume:
5701                __states.push_front(_VSTD::move(__s));
5702                __states.pop_back();
5703                break;
5704            case __state::__repeat:
5705            case __state::__accept_but_not_consume:
5706                break;
5707            case __state::__split:
5708                {
5709                __state __snext = __s;
5710                __s.__node_->__exec_split(true, __s);
5711                __snext.__node_->__exec_split(false, __snext);
5712                __states.push_back(_VSTD::move(__snext));
5713                }
5714                break;
5715            case __state::__reject:
5716                __states.pop_back();
5717                break;
5718            default:
5719                __throw_regex_error<regex_constants::__re_err_unknown>();
5720                break;
5721            }
5722        } while (!__states.empty());
5723        if (__matched)
5724        {
5725            __m.__matches_[0].first = __first;
5726            __m.__matches_[0].second = _VSTD::next(__first, __highest_j);
5727            __m.__matches_[0].matched = true;
5728            return true;
5729        }
5730    }
5731    return false;
5732}
5733
5734template <class _CharT, class _Traits>
5735template <class _Allocator>
5736bool
5737basic_regex<_CharT, _Traits>::__match_at_start_posix_subs(
5738        const _CharT* __first, const _CharT* __last,
5739        match_results<const _CharT*, _Allocator>& __m,
5740        regex_constants::match_flag_type __flags, bool __at_first) const
5741{
5742    vector<__state> __states;
5743    __state __best_state;
5744    ptrdiff_t __j = 0;
5745    ptrdiff_t __highest_j = 0;
5746    ptrdiff_t _Np = _VSTD::distance(__first, __last);
5747    __node* __st = __start_.get();
5748    if (__st)
5749    {
5750        sub_match<const _CharT*> __unmatched;
5751        __unmatched.first   = __last;
5752        __unmatched.second  = __last;
5753        __unmatched.matched = false;
5754
5755        __states.push_back(__state());
5756        __states.back().__do_ = 0;
5757        __states.back().__first_ = __first;
5758        __states.back().__current_ = __first;
5759        __states.back().__last_ = __last;
5760        __states.back().__sub_matches_.resize(mark_count(), __unmatched);
5761        __states.back().__loop_data_.resize(__loop_count());
5762        __states.back().__node_ = __st;
5763        __states.back().__flags_ = __flags;
5764        __states.back().__at_first_ = __at_first;
5765        const _CharT* __current = __first;
5766        bool __matched = false;
5767        int __counter = 0;
5768        int __length = __last - __first;
5769        do
5770        {
5771            ++__counter;
5772            if (__counter % _LIBCPP_REGEX_COMPLEXITY_FACTOR == 0 &&
5773                __counter / _LIBCPP_REGEX_COMPLEXITY_FACTOR >= __length)
5774              __throw_regex_error<regex_constants::error_complexity>();
5775            __state& __s = __states.back();
5776            if (__s.__node_)
5777                __s.__node_->__exec(__s);
5778            switch (__s.__do_)
5779            {
5780            case __state::__end_state:
5781                if ((__flags & regex_constants::match_not_null) &&
5782                    __s.__current_ == __first)
5783                {
5784                  __states.pop_back();
5785                  break;
5786                }
5787                if ((__flags & regex_constants::__full_match) &&
5788                    __s.__current_ != __last)
5789                {
5790                  __states.pop_back();
5791                  break;
5792                }
5793                if (!__matched || __highest_j < __s.__current_ - __s.__first_)
5794                {
5795                    __highest_j = __s.__current_ - __s.__first_;
5796                    __best_state = __s;
5797                }
5798                __matched = true;
5799                if (__highest_j == _Np)
5800                    __states.clear();
5801                else
5802                    __states.pop_back();
5803                break;
5804            case __state::__accept_and_consume:
5805                __j += __s.__current_ - __current;
5806                __current = __s.__current_;
5807                break;
5808            case __state::__repeat:
5809            case __state::__accept_but_not_consume:
5810                break;
5811            case __state::__split:
5812                {
5813                __state __snext = __s;
5814                __s.__node_->__exec_split(true, __s);
5815                __snext.__node_->__exec_split(false, __snext);
5816                __states.push_back(_VSTD::move(__snext));
5817                }
5818                break;
5819            case __state::__reject:
5820                __states.pop_back();
5821                break;
5822            default:
5823                __throw_regex_error<regex_constants::__re_err_unknown>();
5824                break;
5825            }
5826        } while (!__states.empty());
5827        if (__matched)
5828        {
5829            __m.__matches_[0].first = __first;
5830            __m.__matches_[0].second = _VSTD::next(__first, __highest_j);
5831            __m.__matches_[0].matched = true;
5832            for (unsigned __i = 0; __i < __best_state.__sub_matches_.size(); ++__i)
5833                __m.__matches_[__i+1] = __best_state.__sub_matches_[__i];
5834            return true;
5835        }
5836    }
5837    return false;
5838}
5839
5840template <class _CharT, class _Traits>
5841template <class _Allocator>
5842bool
5843basic_regex<_CharT, _Traits>::__match_at_start(
5844        const _CharT* __first, const _CharT* __last,
5845        match_results<const _CharT*, _Allocator>& __m,
5846        regex_constants::match_flag_type __flags, bool __at_first) const
5847{
5848    if ((__flags_ & 0x1F0) == ECMAScript)
5849        return __match_at_start_ecma(__first, __last, __m, __flags, __at_first);
5850    if (mark_count() == 0)
5851        return __match_at_start_posix_nosubs(__first, __last, __m, __flags, __at_first);
5852    return __match_at_start_posix_subs(__first, __last, __m, __flags, __at_first);
5853}
5854
5855template <class _CharT, class _Traits>
5856template <class _Allocator>
5857bool
5858basic_regex<_CharT, _Traits>::__search(
5859        const _CharT* __first, const _CharT* __last,
5860        match_results<const _CharT*, _Allocator>& __m,
5861        regex_constants::match_flag_type __flags) const
5862{
5863    __m.__init(1 + mark_count(), __first, __last,
5864                                    __flags & regex_constants::__no_update_pos);
5865    if (__match_at_start(__first, __last, __m, __flags,
5866                                    !(__flags & regex_constants::__no_update_pos)))
5867    {
5868        __m.__prefix_.second = __m[0].first;
5869        __m.__prefix_.matched = __m.__prefix_.first != __m.__prefix_.second;
5870        __m.__suffix_.first = __m[0].second;
5871        __m.__suffix_.matched = __m.__suffix_.first != __m.__suffix_.second;
5872        return true;
5873    }
5874    if (__first != __last && !(__flags & regex_constants::match_continuous))
5875    {
5876        __flags |= regex_constants::match_prev_avail;
5877        for (++__first; __first != __last; ++__first)
5878        {
5879            __m.__matches_.assign(__m.size(), __m.__unmatched_);
5880            if (__match_at_start(__first, __last, __m, __flags, false))
5881            {
5882                __m.__prefix_.second = __m[0].first;
5883                __m.__prefix_.matched = __m.__prefix_.first != __m.__prefix_.second;
5884                __m.__suffix_.first = __m[0].second;
5885                __m.__suffix_.matched = __m.__suffix_.first != __m.__suffix_.second;
5886                return true;
5887            }
5888            __m.__matches_.assign(__m.size(), __m.__unmatched_);
5889        }
5890    }
5891    __m.__matches_.clear();
5892    return false;
5893}
5894
5895template <class _BidirectionalIterator, class _Allocator, class _CharT, class _Traits>
5896inline _LIBCPP_INLINE_VISIBILITY
5897bool
5898regex_search(_BidirectionalIterator __first, _BidirectionalIterator __last,
5899             match_results<_BidirectionalIterator, _Allocator>& __m,
5900             const basic_regex<_CharT, _Traits>& __e,
5901             regex_constants::match_flag_type __flags = regex_constants::match_default)
5902{
5903    int __offset = (__flags & regex_constants::match_prev_avail) ? 1 : 0;
5904    basic_string<_CharT> __s(_VSTD::prev(__first, __offset), __last);
5905    match_results<const _CharT*> __mc;
5906    bool __r = __e.__search(__s.data() + __offset, __s.data() + __s.size(), __mc, __flags);
5907    __m.__assign(__first, __last, __mc, __flags & regex_constants::__no_update_pos);
5908    return __r;
5909}
5910
5911template <class _Iter, class _Allocator, class _CharT, class _Traits>
5912inline _LIBCPP_INLINE_VISIBILITY
5913bool
5914regex_search(__wrap_iter<_Iter> __first,
5915             __wrap_iter<_Iter> __last,
5916             match_results<__wrap_iter<_Iter>, _Allocator>& __m,
5917             const basic_regex<_CharT, _Traits>& __e,
5918             regex_constants::match_flag_type __flags = regex_constants::match_default)
5919{
5920    match_results<const _CharT*> __mc;
5921    bool __r = __e.__search(__first.base(), __last.base(), __mc, __flags);
5922    __m.__assign(__first, __last, __mc, __flags & regex_constants::__no_update_pos);
5923    return __r;
5924}
5925
5926template <class _Allocator, class _CharT, class _Traits>
5927inline _LIBCPP_INLINE_VISIBILITY
5928bool
5929regex_search(const _CharT* __first, const _CharT* __last,
5930             match_results<const _CharT*, _Allocator>& __m,
5931             const basic_regex<_CharT, _Traits>& __e,
5932             regex_constants::match_flag_type __flags = regex_constants::match_default)
5933{
5934    return __e.__search(__first, __last, __m, __flags);
5935}
5936
5937template <class _BidirectionalIterator, class _CharT, class _Traits>
5938inline _LIBCPP_INLINE_VISIBILITY
5939bool
5940regex_search(_BidirectionalIterator __first, _BidirectionalIterator __last,
5941             const basic_regex<_CharT, _Traits>& __e,
5942             regex_constants::match_flag_type __flags = regex_constants::match_default)
5943{
5944    basic_string<_CharT> __s(__first, __last);
5945    match_results<const _CharT*> __mc;
5946    return __e.__search(__s.data(), __s.data() + __s.size(), __mc, __flags);
5947}
5948
5949template <class _CharT, class _Traits>
5950inline _LIBCPP_INLINE_VISIBILITY
5951bool
5952regex_search(const _CharT* __first, const _CharT* __last,
5953             const basic_regex<_CharT, _Traits>& __e,
5954             regex_constants::match_flag_type __flags = regex_constants::match_default)
5955{
5956    match_results<const _CharT*> __mc;
5957    return __e.__search(__first, __last, __mc, __flags);
5958}
5959
5960template <class _CharT, class _Allocator, class _Traits>
5961inline _LIBCPP_INLINE_VISIBILITY
5962bool
5963regex_search(const _CharT* __str, match_results<const _CharT*, _Allocator>& __m,
5964             const basic_regex<_CharT, _Traits>& __e,
5965             regex_constants::match_flag_type __flags = regex_constants::match_default)
5966{
5967    return __e.__search(__str, __str + _Traits::length(__str), __m, __flags);
5968}
5969
5970template <class _CharT, class _Traits>
5971inline _LIBCPP_INLINE_VISIBILITY
5972bool
5973regex_search(const _CharT* __str, const basic_regex<_CharT, _Traits>& __e,
5974             regex_constants::match_flag_type __flags = regex_constants::match_default)
5975{
5976    match_results<const _CharT*> __m;
5977    return _VSTD::regex_search(__str, __m, __e, __flags);
5978}
5979
5980template <class _ST, class _SA, class _CharT, class _Traits>
5981inline _LIBCPP_INLINE_VISIBILITY
5982bool
5983regex_search(const basic_string<_CharT, _ST, _SA>& __s,
5984             const basic_regex<_CharT, _Traits>& __e,
5985             regex_constants::match_flag_type __flags = regex_constants::match_default)
5986{
5987    match_results<const _CharT*> __mc;
5988    return __e.__search(__s.data(), __s.data() + __s.size(), __mc, __flags);
5989}
5990
5991template <class _ST, class _SA, class _Allocator, class _CharT, class _Traits>
5992inline _LIBCPP_INLINE_VISIBILITY
5993bool
5994regex_search(const basic_string<_CharT, _ST, _SA>& __s,
5995             match_results<typename basic_string<_CharT, _ST, _SA>::const_iterator, _Allocator>& __m,
5996             const basic_regex<_CharT, _Traits>& __e,
5997             regex_constants::match_flag_type __flags = regex_constants::match_default)
5998{
5999    match_results<const _CharT*> __mc;
6000    bool __r = __e.__search(__s.data(), __s.data() + __s.size(), __mc, __flags);
6001    __m.__assign(__s.begin(), __s.end(), __mc, __flags & regex_constants::__no_update_pos);
6002    return __r;
6003}
6004
6005#if _LIBCPP_STD_VER > 11
6006template <class _ST, class _SA, class _Ap, class _Cp, class _Tp>
6007bool
6008regex_search(const basic_string<_Cp, _ST, _SA>&& __s,
6009             match_results<typename basic_string<_Cp, _ST, _SA>::const_iterator, _Ap>&,
6010             const basic_regex<_Cp, _Tp>& __e,
6011             regex_constants::match_flag_type __flags = regex_constants::match_default) = delete;
6012#endif
6013
6014// regex_match
6015
6016template <class _BidirectionalIterator, class _Allocator, class _CharT, class _Traits>
6017bool
6018regex_match(_BidirectionalIterator __first, _BidirectionalIterator __last,
6019            match_results<_BidirectionalIterator, _Allocator>& __m,
6020            const basic_regex<_CharT, _Traits>& __e,
6021            regex_constants::match_flag_type __flags = regex_constants::match_default)
6022{
6023    bool __r = _VSTD::regex_search(
6024        __first, __last, __m, __e,
6025        __flags | regex_constants::match_continuous |
6026        regex_constants::__full_match);
6027    if (__r)
6028    {
6029        __r = !__m.suffix().matched;
6030        if (!__r)
6031            __m.__matches_.clear();
6032    }
6033    return __r;
6034}
6035
6036template <class _BidirectionalIterator, class _CharT, class _Traits>
6037inline _LIBCPP_INLINE_VISIBILITY
6038bool
6039regex_match(_BidirectionalIterator __first, _BidirectionalIterator __last,
6040            const basic_regex<_CharT, _Traits>& __e,
6041            regex_constants::match_flag_type __flags = regex_constants::match_default)
6042{
6043    match_results<_BidirectionalIterator> __m;
6044    return _VSTD::regex_match(__first, __last, __m, __e, __flags);
6045}
6046
6047template <class _CharT, class _Allocator, class _Traits>
6048inline _LIBCPP_INLINE_VISIBILITY
6049bool
6050regex_match(const _CharT* __str, match_results<const _CharT*, _Allocator>& __m,
6051            const basic_regex<_CharT, _Traits>& __e,
6052            regex_constants::match_flag_type __flags = regex_constants::match_default)
6053{
6054    return _VSTD::regex_match(__str, __str + _Traits::length(__str), __m, __e, __flags);
6055}
6056
6057template <class _ST, class _SA, class _Allocator, class _CharT, class _Traits>
6058inline _LIBCPP_INLINE_VISIBILITY
6059bool
6060regex_match(const basic_string<_CharT, _ST, _SA>& __s,
6061            match_results<typename basic_string<_CharT, _ST, _SA>::const_iterator, _Allocator>& __m,
6062            const basic_regex<_CharT, _Traits>& __e,
6063            regex_constants::match_flag_type __flags = regex_constants::match_default)
6064{
6065    return _VSTD::regex_match(__s.begin(), __s.end(), __m, __e, __flags);
6066}
6067
6068#if _LIBCPP_STD_VER > 11
6069template <class _ST, class _SA, class _Allocator, class _CharT, class _Traits>
6070inline _LIBCPP_INLINE_VISIBILITY
6071bool
6072regex_match(const basic_string<_CharT, _ST, _SA>&& __s,
6073            match_results<typename basic_string<_CharT, _ST, _SA>::const_iterator, _Allocator>& __m,
6074            const basic_regex<_CharT, _Traits>& __e,
6075            regex_constants::match_flag_type __flags = regex_constants::match_default) = delete;
6076#endif
6077
6078template <class _CharT, class _Traits>
6079inline _LIBCPP_INLINE_VISIBILITY
6080bool
6081regex_match(const _CharT* __str, const basic_regex<_CharT, _Traits>& __e,
6082            regex_constants::match_flag_type __flags = regex_constants::match_default)
6083{
6084    return _VSTD::regex_match(__str, __str + _Traits::length(__str), __e, __flags);
6085}
6086
6087template <class _ST, class _SA, class _CharT, class _Traits>
6088inline _LIBCPP_INLINE_VISIBILITY
6089bool
6090regex_match(const basic_string<_CharT, _ST, _SA>& __s,
6091            const basic_regex<_CharT, _Traits>& __e,
6092            regex_constants::match_flag_type __flags = regex_constants::match_default)
6093{
6094    return _VSTD::regex_match(__s.begin(), __s.end(), __e, __flags);
6095}
6096
6097// regex_iterator
6098
6099template <class _BidirectionalIterator,
6100          class _CharT = typename iterator_traits<_BidirectionalIterator>::value_type,
6101          class _Traits = regex_traits<_CharT> >
6102class _LIBCPP_TEMPLATE_VIS regex_iterator
6103{
6104public:
6105    typedef basic_regex<_CharT, _Traits>          regex_type;
6106    typedef match_results<_BidirectionalIterator> value_type;
6107    typedef ptrdiff_t                             difference_type;
6108    typedef const value_type*                     pointer;
6109    typedef const value_type&                     reference;
6110    typedef forward_iterator_tag                  iterator_category;
6111
6112private:
6113    _BidirectionalIterator           __begin_;
6114    _BidirectionalIterator           __end_;
6115    const regex_type*                __pregex_;
6116    regex_constants::match_flag_type __flags_;
6117    value_type                       __match_;
6118
6119public:
6120    regex_iterator();
6121    regex_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6122                   const regex_type& __re,
6123                   regex_constants::match_flag_type __m
6124                                              = regex_constants::match_default);
6125#if _LIBCPP_STD_VER > 11
6126    regex_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6127                   const regex_type&& __re,
6128                   regex_constants::match_flag_type __m
6129                                     = regex_constants::match_default) = delete;
6130#endif
6131
6132    bool operator==(const regex_iterator& __x) const;
6133    _LIBCPP_INLINE_VISIBILITY
6134    bool operator!=(const regex_iterator& __x) const {return !(*this == __x);}
6135
6136    _LIBCPP_INLINE_VISIBILITY
6137    reference operator*() const {return  __match_;}
6138    _LIBCPP_INLINE_VISIBILITY
6139    pointer operator->() const  {return &__match_;}
6140
6141    regex_iterator& operator++();
6142    _LIBCPP_INLINE_VISIBILITY
6143    regex_iterator operator++(int)
6144    {
6145        regex_iterator __t(*this);
6146        ++(*this);
6147        return __t;
6148    }
6149};
6150
6151template <class _BidirectionalIterator, class _CharT, class _Traits>
6152regex_iterator<_BidirectionalIterator, _CharT, _Traits>::regex_iterator()
6153    : __begin_(), __end_(), __pregex_(nullptr), __flags_(), __match_()
6154{
6155}
6156
6157template <class _BidirectionalIterator, class _CharT, class _Traits>
6158regex_iterator<_BidirectionalIterator, _CharT, _Traits>::
6159    regex_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6160                   const regex_type& __re, regex_constants::match_flag_type __m)
6161    : __begin_(__a),
6162      __end_(__b),
6163      __pregex_(&__re),
6164      __flags_(__m)
6165{
6166    _VSTD::regex_search(__begin_, __end_, __match_, *__pregex_, __flags_);
6167}
6168
6169template <class _BidirectionalIterator, class _CharT, class _Traits>
6170bool
6171regex_iterator<_BidirectionalIterator, _CharT, _Traits>::
6172    operator==(const regex_iterator& __x) const
6173{
6174    if (__match_.empty() && __x.__match_.empty())
6175        return true;
6176    if (__match_.empty() || __x.__match_.empty())
6177        return false;
6178    return __begin_ == __x.__begin_       &&
6179           __end_ == __x.__end_           &&
6180           __pregex_ == __x.__pregex_     &&
6181           __flags_ == __x.__flags_       &&
6182           __match_[0] == __x.__match_[0];
6183}
6184
6185template <class _BidirectionalIterator, class _CharT, class _Traits>
6186regex_iterator<_BidirectionalIterator, _CharT, _Traits>&
6187regex_iterator<_BidirectionalIterator, _CharT, _Traits>::operator++()
6188{
6189    __flags_ |= regex_constants::__no_update_pos;
6190    _BidirectionalIterator __start = __match_[0].second;
6191    if (__match_[0].first == __match_[0].second)
6192    {
6193        if (__start == __end_)
6194        {
6195            __match_ = value_type();
6196            return *this;
6197        }
6198        else if (_VSTD::regex_search(__start, __end_, __match_, *__pregex_,
6199                                    __flags_ | regex_constants::match_not_null |
6200                                    regex_constants::match_continuous))
6201            return *this;
6202        else
6203            ++__start;
6204    }
6205    __flags_ |= regex_constants::match_prev_avail;
6206    if (!_VSTD::regex_search(__start, __end_, __match_, *__pregex_, __flags_))
6207        __match_ = value_type();
6208    return *this;
6209}
6210
6211typedef regex_iterator<const char*>             cregex_iterator;
6212typedef regex_iterator<const wchar_t*>          wcregex_iterator;
6213typedef regex_iterator<string::const_iterator>  sregex_iterator;
6214typedef regex_iterator<wstring::const_iterator> wsregex_iterator;
6215
6216// regex_token_iterator
6217
6218template <class _BidirectionalIterator,
6219          class _CharT = typename iterator_traits<_BidirectionalIterator>::value_type,
6220          class _Traits = regex_traits<_CharT> >
6221class _LIBCPP_TEMPLATE_VIS regex_token_iterator
6222{
6223public:
6224    typedef basic_regex<_CharT, _Traits>      regex_type;
6225    typedef sub_match<_BidirectionalIterator> value_type;
6226    typedef ptrdiff_t                         difference_type;
6227    typedef const value_type*                 pointer;
6228    typedef const value_type&                 reference;
6229    typedef forward_iterator_tag              iterator_category;
6230
6231private:
6232    typedef regex_iterator<_BidirectionalIterator, _CharT, _Traits> _Position;
6233
6234    _Position         __position_;
6235    const value_type* __result_;
6236    value_type        __suffix_;
6237    ptrdiff_t         __n_;
6238    vector<int>       __subs_;
6239
6240public:
6241    regex_token_iterator();
6242    regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6243                         const regex_type& __re, int __submatch = 0,
6244                         regex_constants::match_flag_type __m =
6245                                                regex_constants::match_default);
6246#if _LIBCPP_STD_VER > 11
6247    regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6248                         const regex_type&& __re, int __submatch = 0,
6249                         regex_constants::match_flag_type __m =
6250                                       regex_constants::match_default) = delete;
6251#endif
6252
6253    regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6254                         const regex_type& __re, const vector<int>& __submatches,
6255                         regex_constants::match_flag_type __m =
6256                                                regex_constants::match_default);
6257#if _LIBCPP_STD_VER > 11
6258    regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6259                         const regex_type&& __re, const vector<int>& __submatches,
6260                         regex_constants::match_flag_type __m =
6261                                     regex_constants::match_default) = delete;
6262#endif
6263
6264#ifndef _LIBCPP_CXX03_LANG
6265    regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6266                         const regex_type& __re,
6267                         initializer_list<int> __submatches,
6268                         regex_constants::match_flag_type __m =
6269                                                regex_constants::match_default);
6270
6271#if _LIBCPP_STD_VER > 11
6272    regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6273                         const regex_type&& __re,
6274                         initializer_list<int> __submatches,
6275                         regex_constants::match_flag_type __m =
6276                                       regex_constants::match_default) = delete;
6277#endif
6278#endif  // _LIBCPP_CXX03_LANG
6279    template <size_t _Np>
6280        regex_token_iterator(_BidirectionalIterator __a,
6281                             _BidirectionalIterator __b,
6282                             const regex_type& __re,
6283                             const int (&__submatches)[_Np],
6284                             regex_constants::match_flag_type __m =
6285                                                regex_constants::match_default);
6286#if _LIBCPP_STD_VER > 11
6287    template <std::size_t _Np>
6288        regex_token_iterator(_BidirectionalIterator __a,
6289                             _BidirectionalIterator __b,
6290                             const regex_type&& __re,
6291                             const int (&__submatches)[_Np],
6292                             regex_constants::match_flag_type __m =
6293                                      regex_constants::match_default) = delete;
6294#endif
6295
6296    regex_token_iterator(const regex_token_iterator&);
6297    regex_token_iterator& operator=(const regex_token_iterator&);
6298
6299    bool operator==(const regex_token_iterator& __x) const;
6300    _LIBCPP_INLINE_VISIBILITY
6301    bool operator!=(const regex_token_iterator& __x) const {return !(*this == __x);}
6302
6303    _LIBCPP_INLINE_VISIBILITY
6304    const value_type& operator*() const {return *__result_;}
6305    _LIBCPP_INLINE_VISIBILITY
6306    const value_type* operator->() const {return __result_;}
6307
6308    regex_token_iterator& operator++();
6309    _LIBCPP_INLINE_VISIBILITY
6310    regex_token_iterator operator++(int)
6311    {
6312        regex_token_iterator __t(*this);
6313        ++(*this);
6314        return __t;
6315    }
6316
6317private:
6318    void __init(_BidirectionalIterator __a, _BidirectionalIterator __b);
6319    void __establish_result () {
6320        if (__subs_[__n_] == -1)
6321            __result_ = &__position_->prefix();
6322        else
6323            __result_ = &(*__position_)[__subs_[__n_]];
6324        }
6325};
6326
6327template <class _BidirectionalIterator, class _CharT, class _Traits>
6328regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6329    regex_token_iterator()
6330    : __result_(nullptr),
6331      __suffix_(),
6332      __n_(0)
6333{
6334}
6335
6336template <class _BidirectionalIterator, class _CharT, class _Traits>
6337void
6338regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6339    __init(_BidirectionalIterator __a, _BidirectionalIterator __b)
6340{
6341    if (__position_ != _Position())
6342        __establish_result ();
6343    else if (__subs_[__n_] == -1)
6344    {
6345        __suffix_.matched = true;
6346        __suffix_.first = __a;
6347        __suffix_.second = __b;
6348        __result_ = &__suffix_;
6349    }
6350    else
6351        __result_ = nullptr;
6352}
6353
6354template <class _BidirectionalIterator, class _CharT, class _Traits>
6355regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6356    regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6357                         const regex_type& __re, int __submatch,
6358                         regex_constants::match_flag_type __m)
6359    : __position_(__a, __b, __re, __m),
6360      __n_(0),
6361      __subs_(1, __submatch)
6362{
6363    __init(__a, __b);
6364}
6365
6366template <class _BidirectionalIterator, class _CharT, class _Traits>
6367regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6368    regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6369                         const regex_type& __re, const vector<int>& __submatches,
6370                         regex_constants::match_flag_type __m)
6371    : __position_(__a, __b, __re, __m),
6372      __n_(0),
6373      __subs_(__submatches)
6374{
6375    __init(__a, __b);
6376}
6377
6378#ifndef _LIBCPP_CXX03_LANG
6379
6380template <class _BidirectionalIterator, class _CharT, class _Traits>
6381regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6382    regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6383                         const regex_type& __re,
6384                         initializer_list<int> __submatches,
6385                         regex_constants::match_flag_type __m)
6386    : __position_(__a, __b, __re, __m),
6387      __n_(0),
6388      __subs_(__submatches)
6389{
6390    __init(__a, __b);
6391}
6392
6393#endif  // _LIBCPP_CXX03_LANG
6394
6395template <class _BidirectionalIterator, class _CharT, class _Traits>
6396template <size_t _Np>
6397regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6398    regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6399                             const regex_type& __re,
6400                             const int (&__submatches)[_Np],
6401                             regex_constants::match_flag_type __m)
6402    : __position_(__a, __b, __re, __m),
6403      __n_(0),
6404      __subs_(__submatches, __submatches + _Np)
6405{
6406    __init(__a, __b);
6407}
6408
6409template <class _BidirectionalIterator, class _CharT, class _Traits>
6410regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6411    regex_token_iterator(const regex_token_iterator& __x)
6412    : __position_(__x.__position_),
6413      __result_(__x.__result_),
6414      __suffix_(__x.__suffix_),
6415      __n_(__x.__n_),
6416      __subs_(__x.__subs_)
6417{
6418    if (__x.__result_ == &__x.__suffix_)
6419        __result_ = &__suffix_;
6420    else if ( __result_ != nullptr )
6421        __establish_result ();
6422}
6423
6424template <class _BidirectionalIterator, class _CharT, class _Traits>
6425regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>&
6426regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6427    operator=(const regex_token_iterator& __x)
6428{
6429    if (this != &__x)
6430    {
6431        __position_ = __x.__position_;
6432        if (__x.__result_ == &__x.__suffix_)
6433            __result_ = &__suffix_;
6434        else
6435            __result_ = __x.__result_;
6436        __suffix_ = __x.__suffix_;
6437        __n_ = __x.__n_;
6438        __subs_ = __x.__subs_;
6439
6440        if ( __result_ != nullptr && __result_ != &__suffix_ )
6441            __establish_result();
6442    }
6443    return *this;
6444}
6445
6446template <class _BidirectionalIterator, class _CharT, class _Traits>
6447bool
6448regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6449    operator==(const regex_token_iterator& __x) const
6450{
6451    if (__result_ == nullptr && __x.__result_ == nullptr)
6452        return true;
6453    if (__result_ == &__suffix_ && __x.__result_ == &__x.__suffix_ &&
6454            __suffix_ == __x.__suffix_)
6455        return true;
6456    if (__result_ == nullptr || __x.__result_ == nullptr)
6457        return false;
6458    if (__result_ == &__suffix_ || __x.__result_ == &__x.__suffix_)
6459        return false;
6460    return __position_ == __x.__position_ && __n_ == __x.__n_ &&
6461           __subs_ == __x.__subs_;
6462}
6463
6464template <class _BidirectionalIterator, class _CharT, class _Traits>
6465regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>&
6466regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::operator++()
6467{
6468    _Position __prev = __position_;
6469    if (__result_ == &__suffix_)
6470        __result_ = nullptr;
6471    else if (static_cast<size_t>(__n_ + 1) < __subs_.size())
6472    {
6473        ++__n_;
6474        __establish_result();
6475    }
6476    else
6477    {
6478        __n_ = 0;
6479        ++__position_;
6480        if (__position_ != _Position())
6481            __establish_result();
6482        else
6483        {
6484            if (_VSTD::find(__subs_.begin(), __subs_.end(), -1) != __subs_.end()
6485                && __prev->suffix().length() != 0)
6486            {
6487                __suffix_.matched = true;
6488                __suffix_.first = __prev->suffix().first;
6489                __suffix_.second = __prev->suffix().second;
6490                __result_ = &__suffix_;
6491            }
6492            else
6493                __result_ = nullptr;
6494        }
6495    }
6496    return *this;
6497}
6498
6499typedef regex_token_iterator<const char*>             cregex_token_iterator;
6500typedef regex_token_iterator<const wchar_t*>          wcregex_token_iterator;
6501typedef regex_token_iterator<string::const_iterator>  sregex_token_iterator;
6502typedef regex_token_iterator<wstring::const_iterator> wsregex_token_iterator;
6503
6504// regex_replace
6505
6506template <class _OutputIterator, class _BidirectionalIterator,
6507          class _Traits, class _CharT>
6508_OutputIterator
6509regex_replace(_OutputIterator __output_iter,
6510              _BidirectionalIterator __first, _BidirectionalIterator __last,
6511              const basic_regex<_CharT, _Traits>& __e, const _CharT* __fmt,
6512              regex_constants::match_flag_type __flags = regex_constants::match_default)
6513{
6514    typedef regex_iterator<_BidirectionalIterator, _CharT, _Traits> _Iter;
6515    _Iter __i(__first, __last, __e, __flags);
6516    _Iter __eof;
6517    if (__i == __eof)
6518    {
6519        if (!(__flags & regex_constants::format_no_copy))
6520            __output_iter = _VSTD::copy(__first, __last, __output_iter);
6521    }
6522    else
6523    {
6524        sub_match<_BidirectionalIterator> __lm;
6525        for (size_t __len = char_traits<_CharT>::length(__fmt); __i != __eof; ++__i)
6526        {
6527            if (!(__flags & regex_constants::format_no_copy))
6528                __output_iter = _VSTD::copy(__i->prefix().first, __i->prefix().second, __output_iter);
6529            __output_iter = __i->format(__output_iter, __fmt, __fmt + __len, __flags);
6530            __lm = __i->suffix();
6531            if (__flags & regex_constants::format_first_only)
6532                break;
6533        }
6534        if (!(__flags & regex_constants::format_no_copy))
6535            __output_iter = _VSTD::copy(__lm.first, __lm.second, __output_iter);
6536    }
6537    return __output_iter;
6538}
6539
6540template <class _OutputIterator, class _BidirectionalIterator,
6541          class _Traits, class _CharT, class _ST, class _SA>
6542inline _LIBCPP_INLINE_VISIBILITY
6543_OutputIterator
6544regex_replace(_OutputIterator __output_iter,
6545              _BidirectionalIterator __first, _BidirectionalIterator __last,
6546              const basic_regex<_CharT, _Traits>& __e,
6547              const basic_string<_CharT, _ST, _SA>& __fmt,
6548              regex_constants::match_flag_type __flags = regex_constants::match_default)
6549{
6550    return _VSTD::regex_replace(__output_iter, __first, __last, __e, __fmt.c_str(), __flags);
6551}
6552
6553template <class _Traits, class _CharT, class _ST, class _SA, class _FST,
6554          class _FSA>
6555inline _LIBCPP_INLINE_VISIBILITY
6556basic_string<_CharT, _ST, _SA>
6557regex_replace(const basic_string<_CharT, _ST, _SA>& __s,
6558              const basic_regex<_CharT, _Traits>& __e,
6559              const basic_string<_CharT, _FST, _FSA>& __fmt,
6560              regex_constants::match_flag_type __flags = regex_constants::match_default)
6561{
6562    basic_string<_CharT, _ST, _SA> __r;
6563    _VSTD::regex_replace(back_inserter(__r), __s.begin(), __s.end(), __e,
6564                        __fmt.c_str(), __flags);
6565    return __r;
6566}
6567
6568template <class _Traits, class _CharT, class _ST, class _SA>
6569inline _LIBCPP_INLINE_VISIBILITY
6570basic_string<_CharT, _ST, _SA>
6571regex_replace(const basic_string<_CharT, _ST, _SA>& __s,
6572              const basic_regex<_CharT, _Traits>& __e, const _CharT* __fmt,
6573              regex_constants::match_flag_type __flags = regex_constants::match_default)
6574{
6575    basic_string<_CharT, _ST, _SA> __r;
6576    _VSTD::regex_replace(back_inserter(__r), __s.begin(), __s.end(), __e,
6577                        __fmt, __flags);
6578    return __r;
6579}
6580
6581template <class _Traits, class _CharT, class _ST, class _SA>
6582inline _LIBCPP_INLINE_VISIBILITY
6583basic_string<_CharT>
6584regex_replace(const _CharT* __s,
6585              const basic_regex<_CharT, _Traits>& __e,
6586              const basic_string<_CharT, _ST, _SA>& __fmt,
6587              regex_constants::match_flag_type __flags = regex_constants::match_default)
6588{
6589    basic_string<_CharT> __r;
6590    _VSTD::regex_replace(back_inserter(__r), __s,
6591                        __s + char_traits<_CharT>::length(__s), __e,
6592                        __fmt.c_str(), __flags);
6593    return __r;
6594}
6595
6596template <class _Traits, class _CharT>
6597inline _LIBCPP_INLINE_VISIBILITY
6598basic_string<_CharT>
6599regex_replace(const _CharT* __s,
6600              const basic_regex<_CharT, _Traits>& __e,
6601              const _CharT* __fmt,
6602              regex_constants::match_flag_type __flags = regex_constants::match_default)
6603{
6604    basic_string<_CharT> __r;
6605    _VSTD::regex_replace(back_inserter(__r), __s,
6606                        __s + char_traits<_CharT>::length(__s), __e,
6607                        __fmt, __flags);
6608    return __r;
6609}
6610
6611_LIBCPP_END_NAMESPACE_STD
6612
6613_LIBCPP_POP_MACROS
6614
6615#endif  // _LIBCPP_REGEX
6616