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