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