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