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