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