Back to home page

EIC code displayed by LXR

 
 

    


Warning, /include/c++/v1/regex is written in an unsupported language. File is not indexed.

0001 // -*- C++ -*-
0002 //===----------------------------------------------------------------------===//
0003 //
0004 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0005 // See https://llvm.org/LICENSE.txt for license information.
0006 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0007 //
0008 //===----------------------------------------------------------------------===//
0009 
0010 #ifndef _LIBCPP_REGEX
0011 #define _LIBCPP_REGEX
0012 
0013 /*
0014     regex synopsis
0015 
0016 #include <compare>
0017 #include <initializer_list>
0018 
0019 namespace std
0020 {
0021 
0022 namespace regex_constants
0023 {
0024 
0025 enum syntax_option_type
0026 {
0027     icase      = unspecified,
0028     nosubs     = unspecified,
0029     optimize   = unspecified,
0030     collate    = unspecified,
0031     ECMAScript = unspecified,
0032     basic      = unspecified,
0033     extended   = unspecified,
0034     awk        = unspecified,
0035     grep       = unspecified,
0036     egrep      = unspecified,
0037     multiline  = unspecified
0038 };
0039 
0040 constexpr syntax_option_type operator~(syntax_option_type f);
0041 constexpr syntax_option_type operator&(syntax_option_type lhs, syntax_option_type rhs);
0042 constexpr syntax_option_type operator|(syntax_option_type lhs, syntax_option_type rhs);
0043 
0044 enum match_flag_type
0045 {
0046     match_default     = 0,
0047     match_not_bol     = unspecified,
0048     match_not_eol     = unspecified,
0049     match_not_bow     = unspecified,
0050     match_not_eow     = unspecified,
0051     match_any         = unspecified,
0052     match_not_null    = unspecified,
0053     match_continuous  = unspecified,
0054     match_prev_avail  = unspecified,
0055     format_default    = 0,
0056     format_sed        = unspecified,
0057     format_no_copy    = unspecified,
0058     format_first_only = unspecified
0059 };
0060 
0061 constexpr match_flag_type operator~(match_flag_type f);
0062 constexpr match_flag_type operator&(match_flag_type lhs, match_flag_type rhs);
0063 constexpr match_flag_type operator|(match_flag_type lhs, match_flag_type rhs);
0064 
0065 enum error_type
0066 {
0067     error_collate    = unspecified,
0068     error_ctype      = unspecified,
0069     error_escape     = unspecified,
0070     error_backref    = unspecified,
0071     error_brack      = unspecified,
0072     error_paren      = unspecified,
0073     error_brace      = unspecified,
0074     error_badbrace   = unspecified,
0075     error_range      = unspecified,
0076     error_space      = unspecified,
0077     error_badrepeat  = unspecified,
0078     error_complexity = unspecified,
0079     error_stack      = unspecified
0080 };
0081 
0082 }  // regex_constants
0083 
0084 class regex_error
0085     : public runtime_error
0086 {
0087 public:
0088     explicit regex_error(regex_constants::error_type ecode);
0089     regex_constants::error_type code() const;
0090 };
0091 
0092 template <class charT>
0093 struct regex_traits
0094 {
0095 public:
0096     typedef charT                   char_type;
0097     typedef basic_string<char_type> string_type;
0098     typedef locale                  locale_type;
0099     typedef /bitmask_type/          char_class_type;
0100 
0101     regex_traits();
0102 
0103     static size_t length(const char_type* p);
0104     charT translate(charT c) const;
0105     charT translate_nocase(charT c) const;
0106     template <class ForwardIterator>
0107         string_type
0108         transform(ForwardIterator first, ForwardIterator last) const;
0109     template <class ForwardIterator>
0110         string_type
0111         transform_primary( ForwardIterator first, ForwardIterator last) const;
0112     template <class ForwardIterator>
0113         string_type
0114         lookup_collatename(ForwardIterator first, ForwardIterator last) const;
0115     template <class ForwardIterator>
0116         char_class_type
0117         lookup_classname(ForwardIterator first, ForwardIterator last,
0118                          bool icase = false) const;
0119     bool isctype(charT c, char_class_type f) const;
0120     int value(charT ch, int radix) const;
0121     locale_type imbue(locale_type l);
0122     locale_type getloc()const;
0123 };
0124 
0125 template <class charT, class traits = regex_traits<charT>>
0126 class basic_regex
0127 {
0128 public:
0129     // types:
0130     typedef charT                               value_type;
0131     typedef traits                              traits_type;
0132     typedef typename traits::string_type        string_type;
0133     typedef regex_constants::syntax_option_type flag_type;
0134     typedef typename traits::locale_type        locale_type;
0135 
0136     // constants:
0137     static constexpr regex_constants::syntax_option_type icase = regex_constants::icase;
0138     static constexpr regex_constants::syntax_option_type nosubs = regex_constants::nosubs;
0139     static constexpr regex_constants::syntax_option_type optimize = regex_constants::optimize;
0140     static constexpr regex_constants::syntax_option_type collate = regex_constants::collate;
0141     static constexpr regex_constants::syntax_option_type ECMAScript = regex_constants::ECMAScript;
0142     static constexpr regex_constants::syntax_option_type basic = regex_constants::basic;
0143     static constexpr regex_constants::syntax_option_type extended = regex_constants::extended;
0144     static constexpr regex_constants::syntax_option_type awk = regex_constants::awk;
0145     static constexpr regex_constants::syntax_option_type grep = regex_constants::grep;
0146     static constexpr regex_constants::syntax_option_type egrep = regex_constants::egrep;
0147     static constexpr regex_constants::syntax_option_type multiline = regex_constants::multiline;
0148 
0149     // construct/copy/destroy:
0150     basic_regex();
0151     explicit basic_regex(const charT* p, flag_type f = regex_constants::ECMAScript);
0152     basic_regex(const charT* p, size_t len, flag_type f = regex_constants::ECMAScript);
0153     basic_regex(const basic_regex&);
0154     basic_regex(basic_regex&&) noexcept;
0155     template <class ST, class SA>
0156         explicit basic_regex(const basic_string<charT, ST, SA>& p,
0157                              flag_type f = regex_constants::ECMAScript);
0158     template <class ForwardIterator>
0159         basic_regex(ForwardIterator first, ForwardIterator last,
0160                     flag_type f = regex_constants::ECMAScript);
0161     basic_regex(initializer_list<charT>, flag_type = regex_constants::ECMAScript);
0162 
0163     ~basic_regex();
0164 
0165     basic_regex& operator=(const basic_regex&);
0166     basic_regex& operator=(basic_regex&&) noexcept;
0167     basic_regex& operator=(const charT* ptr);
0168     basic_regex& operator=(initializer_list<charT> il);
0169     template <class ST, class SA>
0170         basic_regex& operator=(const basic_string<charT, ST, SA>& p);
0171 
0172     // assign:
0173     basic_regex& assign(const basic_regex& that);
0174     basic_regex& assign(basic_regex&& that) noexcept;
0175     basic_regex& assign(const charT* ptr,           flag_type f = regex_constants::ECMAScript);
0176     basic_regex& assign(const charT* p, size_t len, flag_type f = regex_constants::ECMAScript);
0177     template <class string_traits, class A>
0178         basic_regex& assign(const basic_string<charT, string_traits, A>& s,
0179                                                     flag_type f = regex_constants::ECMAScript);
0180     template <class InputIterator>
0181         basic_regex& assign(InputIterator first, InputIterator last,
0182                                                     flag_type f = regex_constants::ECMAScript);
0183     basic_regex& assign(initializer_list<charT>,    flag_type f = regex_constants::ECMAScript);
0184 
0185     // const operations:
0186     unsigned mark_count() const;
0187     flag_type flags() const;
0188 
0189     // locale:
0190     locale_type imbue(locale_type loc);
0191     locale_type getloc() const;
0192 
0193     // swap:
0194     void swap(basic_regex&);
0195 };
0196 
0197 template<class ForwardIterator>
0198 basic_regex(ForwardIterator, ForwardIterator,
0199             regex_constants::syntax_option_type = regex_constants::ECMAScript)
0200     -> basic_regex<typename iterator_traits<ForwardIterator>::value_type>; // C++17
0201 
0202 typedef basic_regex<char>    regex;
0203 typedef basic_regex<wchar_t> wregex;
0204 
0205 template <class charT, class traits>
0206     void swap(basic_regex<charT, traits>& e1, basic_regex<charT, traits>& e2);
0207 
0208 template <class BidirectionalIterator>
0209 class sub_match
0210     : public pair<BidirectionalIterator, BidirectionalIterator>
0211 {
0212 public:
0213     typedef typename iterator_traits<BidirectionalIterator>::value_type value_type;
0214     typedef typename iterator_traits<BidirectionalIterator>::difference_type difference_type;
0215     typedef BidirectionalIterator                                      iterator;
0216     typedef basic_string<value_type>                                string_type;
0217 
0218     bool matched;
0219 
0220     constexpr sub_match();
0221 
0222     difference_type length() const;
0223     operator string_type() const;
0224     string_type str() const;
0225 
0226     int compare(const sub_match& s) const;
0227     int compare(const string_type& s) const;
0228     int compare(const value_type* s) const;
0229 
0230     void swap(sub_match& s) noexcept(see below);
0231 };
0232 
0233 typedef sub_match<const char*>             csub_match;
0234 typedef sub_match<const wchar_t*>          wcsub_match;
0235 typedef sub_match<string::const_iterator>  ssub_match;
0236 typedef sub_match<wstring::const_iterator> wssub_match;
0237 
0238 template <class BiIter>
0239     bool
0240     operator==(const sub_match<BiIter>& lhs, const sub_match<BiIter>& rhs);
0241 
0242 template <class BiIter>
0243     auto
0244     operator<=>(const sub_match<BiIter>& lhs, const sub_match<BiIter>& rhs); // Since C++20
0245 
0246  template <class BiIter>                                                     // Removed in C++20
0247     bool
0248     operator!=(const sub_match<BiIter>& lhs, const sub_match<BiIter>& rhs);
0249 
0250 template <class BiIter>                                                      // Removed in C++20
0251     bool
0252     operator<(const sub_match<BiIter>& lhs, const sub_match<BiIter>& rhs);
0253 
0254 template <class BiIter>                                                      // Removed in C++20
0255     bool
0256     operator<=(const sub_match<BiIter>& lhs, const sub_match<BiIter>& rhs);
0257 
0258 template <class BiIter>                                                      // Removed in C++20
0259     bool
0260     operator>=(const sub_match<BiIter>& lhs, const sub_match<BiIter>& rhs);
0261 
0262 template <class BiIter>                                                      // Removed in C++20
0263     bool
0264     operator>(const sub_match<BiIter>& lhs, const sub_match<BiIter>& rhs);
0265 
0266 template <class BiIter, class ST, class SA>                                  // Removed in C++20
0267     bool
0268     operator==(const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& lhs,
0269                const sub_match<BiIter>& rhs);
0270 
0271 template <class BiIter, class ST, class SA>                                  // Removed in C++20
0272     bool
0273     operator!=(const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& lhs,
0274                const sub_match<BiIter>& rhs);
0275 
0276 template <class BiIter, class ST, class SA>                                  // Removed in C++20
0277     bool
0278     operator<(const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& lhs,
0279               const sub_match<BiIter>& rhs);
0280 
0281 template <class BiIter, class ST, class SA>                                  // Removed in C++20
0282     bool
0283     operator>(const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& lhs,
0284               const sub_match<BiIter>& rhs);
0285 
0286 template <class BiIter, class ST, class SA>                                  // Removed in C++20
0287     bool operator>=(const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& lhs,
0288                     const sub_match<BiIter>& rhs);
0289 
0290 template <class BiIter, class ST, class SA>                                  // Removed in C++20
0291     bool
0292     operator<=(const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& lhs,
0293                const sub_match<BiIter>& rhs);
0294 
0295 template <class BiIter, class ST, class SA>
0296     bool
0297     operator==(const sub_match<BiIter>& lhs,
0298                const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& rhs);
0299 
0300 template <class BiIter, class ST, class SA>                                  // Since C++20
0301     auto
0302     operator<=>(const sub_match<BiIter>& lhs,
0303                 const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& rhs);
0304 
0305 template <class BiIter, class ST, class SA>                                  // Removed in C++20
0306     bool
0307     operator!=(const sub_match<BiIter>& lhs,
0308                const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& rhs);
0309 
0310 template <class BiIter, class ST, class SA>                                  // Removed in C++20
0311     bool
0312     operator<(const sub_match<BiIter>& lhs,
0313               const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& rhs);
0314 
0315 template <class BiIter, class ST, class SA>                                  // Removed in C++20
0316     bool
0317     operator>(const sub_match<BiIter>& lhs,
0318                    const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& rhs);
0319 
0320 template <class BiIter, class ST, class SA>                                  // Removed in C++20
0321     bool
0322     operator>=(const sub_match<BiIter>& lhs,
0323                const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& rhs);
0324 
0325 template <class BiIter, class ST, class SA>                                  // Removed in C++20
0326     bool
0327     operator<=(const sub_match<BiIter>& lhs,
0328                const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& rhs);
0329 
0330 template <class BiIter>                                                      // Removed in C++20
0331     bool
0332     operator==(typename iterator_traits<BiIter>::value_type const* lhs,
0333                const sub_match<BiIter>& rhs);
0334 
0335 template <class BiIter>                                                      // Removed in C++20
0336     bool
0337     operator!=(typename iterator_traits<BiIter>::value_type const* lhs,
0338                const sub_match<BiIter>& rhs);
0339 
0340 template <class BiIter>                                                      // Removed in C++20
0341     bool
0342     operator<(typename iterator_traits<BiIter>::value_type const* lhs,
0343               const sub_match<BiIter>& rhs);
0344 
0345 template <class BiIter>                                                      // Removed in C++20
0346     bool
0347     operator>(typename iterator_traits<BiIter>::value_type const* lhs,
0348               const sub_match<BiIter>& rhs);
0349 
0350 template <class BiIter>                                                      // Removed in C++20
0351     bool
0352     operator>=(typename iterator_traits<BiIter>::value_type const* lhs,
0353                const sub_match<BiIter>& rhs);
0354 
0355 template <class BiIter>                                                      // Removed in C++20
0356     bool
0357     operator<=(typename iterator_traits<BiIter>::value_type const* lhs,
0358                const sub_match<BiIter>& rhs);
0359 
0360 template <class BiIter>
0361     bool
0362     operator==(const sub_match<BiIter>& lhs,
0363                typename iterator_traits<BiIter>::value_type const* rhs);
0364 
0365 template <class BiIter>                                                      // Since C++20
0366     auto
0367     operator<=>(const sub_match<BiIter>& lhs,
0368                 typename iterator_traits<BiIter>::value_type const* rhs);
0369 
0370 template <class BiIter, class ST, class SA>                                  // Removed in C++20
0371     bool
0372     operator!=(const sub_match<BiIter>& lhs,
0373                typename iterator_traits<BiIter>::value_type const* rhs);
0374 
0375 template <class BiIter>                                                      // Removed in C++20
0376     bool
0377     operator<(const sub_match<BiIter>& lhs,
0378               typename iterator_traits<BiIter>::value_type const* rhs);
0379 
0380 template <class BiIter>                                                      // Removed in C++20
0381     bool
0382     operator>(const sub_match<BiIter>& lhs,
0383               typename iterator_traits<BiIter>::value_type const* rhs);
0384 
0385 template <class BiIter>                                                      // Removed in C++20
0386     bool
0387     operator>=(const sub_match<BiIter>& lhs,
0388                typename iterator_traits<BiIter>::value_type const* rhs);
0389 
0390 template <class BiIter>                                                      // Removed in C++20
0391     bool
0392     operator<=(const sub_match<BiIter>& lhs,
0393                typename iterator_traits<BiIter>::value_type const* rhs);
0394 
0395 template <class BiIter>                                                      // Removed in C++20
0396     bool
0397     operator==(typename iterator_traits<BiIter>::value_type const& lhs,
0398                const sub_match<BiIter>& rhs);
0399 
0400 template <class BiIter>                                                      // Removed in C++20
0401     bool
0402     operator!=(typename iterator_traits<BiIter>::value_type const& lhs,
0403                const sub_match<BiIter>& rhs);
0404 
0405 template <class BiIter>                                                      // Removed in C++20
0406     bool
0407     operator<(typename iterator_traits<BiIter>::value_type const& lhs,
0408               const sub_match<BiIter>& rhs);
0409 
0410 template <class BiIter>                                                      // Removed in C++20
0411     bool
0412     operator>(typename iterator_traits<BiIter>::value_type const& lhs,
0413               const sub_match<BiIter>& rhs);
0414 
0415 template <class BiIter>                                                      // Removed in C++20
0416     bool
0417     operator>=(typename iterator_traits<BiIter>::value_type const& lhs,
0418                const sub_match<BiIter>& rhs);
0419 
0420 template <class BiIter>                                                      // Removed in C++20
0421     bool
0422     operator<=(typename iterator_traits<BiIter>::value_type const& lhs,
0423                const sub_match<BiIter>& rhs);
0424 
0425 template <class BiIter>
0426     bool
0427     operator==(const sub_match<BiIter>& lhs,
0428                typename iterator_traits<BiIter>::value_type const& rhs);
0429 
0430 template <class BiIter>                                                      // Since C++20
0431     auto
0432     operator<=>(const sub_match<BiIter>& lhs,
0433                 typename iterator_traits<BiIter>::value_type const& rhs);
0434 
0435 template <class BiIter>                                                      // Removed in C++20
0436     bool
0437     operator!=(const sub_match<BiIter>& lhs,
0438                typename iterator_traits<BiIter>::value_type const& rhs);
0439 
0440 template <class BiIter>                                                      // Removed in C++20
0441     bool
0442     operator<(const sub_match<BiIter>& lhs,
0443               typename iterator_traits<BiIter>::value_type const& rhs);
0444 
0445 template <class BiIter>                                                      // Removed in C++20
0446     bool
0447     operator>(const sub_match<BiIter>& lhs,
0448               typename iterator_traits<BiIter>::value_type const& rhs);
0449 
0450 template <class BiIter>                                                      // Removed in C++20
0451     bool
0452     operator>=(const sub_match<BiIter>& lhs,
0453                typename iterator_traits<BiIter>::value_type const& rhs);
0454 
0455 template <class BiIter>                                                      // Removed in C++20
0456     bool
0457     operator<=(const sub_match<BiIter>& lhs,
0458                typename iterator_traits<BiIter>::value_type const& rhs);
0459 
0460 template <class charT, class ST, class BiIter>
0461     basic_ostream<charT, ST>&
0462     operator<<(basic_ostream<charT, ST>& os, const sub_match<BiIter>& m);
0463 
0464 template <class BidirectionalIterator,
0465           class Allocator = allocator<sub_match<BidirectionalIterator>>>
0466 class match_results
0467 {
0468 public:
0469     typedef sub_match<BidirectionalIterator>                  value_type;
0470     typedef const value_type&                                 const_reference;
0471     typedef value_type&                                       reference;
0472     typedef /implementation-defined/                          const_iterator;
0473     typedef const_iterator                                    iterator;
0474     typedef typename iterator_traits<BidirectionalIterator>::difference_type difference_type;
0475     typedef typename allocator_traits<Allocator>::size_type   size_type;
0476     typedef Allocator                                         allocator_type;
0477     typedef typename iterator_traits<BidirectionalIterator>::value_type char_type;
0478     typedef basic_string<char_type>                           string_type;
0479 
0480     // construct/copy/destroy:
0481     explicit match_results(const Allocator& a = Allocator()); // before C++20
0482     match_results() : match_results(Allocator()) {}           // C++20
0483     explicit match_results(const Allocator& a);               // C++20
0484     match_results(const match_results& m);
0485     match_results(match_results&& m) noexcept;
0486     match_results& operator=(const match_results& m);
0487     match_results& operator=(match_results&& m);
0488     ~match_results();
0489 
0490     bool ready() const;
0491 
0492     // size:
0493     size_type size() const;
0494     size_type max_size() const;
0495     bool empty() const;
0496 
0497     // element access:
0498     difference_type length(size_type sub = 0) const;
0499     difference_type position(size_type sub = 0) const;
0500     string_type str(size_type sub = 0) const;
0501     const_reference operator[](size_type n) const;
0502 
0503     const_reference prefix() const;
0504     const_reference suffix() const;
0505 
0506     const_iterator begin() const;
0507     const_iterator end() const;
0508     const_iterator cbegin() const;
0509     const_iterator cend() const;
0510 
0511     // format:
0512     template <class OutputIter>
0513         OutputIter
0514         format(OutputIter out, const char_type* fmt_first,
0515                const char_type* fmt_last,
0516                regex_constants::match_flag_type flags = regex_constants::format_default) const;
0517     template <class OutputIter, class ST, class SA>
0518         OutputIter
0519         format(OutputIter out, const basic_string<char_type, ST, SA>& fmt,
0520                regex_constants::match_flag_type flags = regex_constants::format_default) const;
0521     template <class ST, class SA>
0522         basic_string<char_type, ST, SA>
0523         format(const basic_string<char_type, ST, SA>& fmt,
0524                regex_constants::match_flag_type flags = regex_constants::format_default) const;
0525     string_type
0526         format(const char_type* fmt,
0527                regex_constants::match_flag_type flags = regex_constants::format_default) const;
0528 
0529     // allocator:
0530     allocator_type get_allocator() const;
0531 
0532     // swap:
0533     void swap(match_results& that);
0534 };
0535 
0536 typedef match_results<const char*>             cmatch;
0537 typedef match_results<const wchar_t*>          wcmatch;
0538 typedef match_results<string::const_iterator>  smatch;
0539 typedef match_results<wstring::const_iterator> wsmatch;
0540 
0541 template <class BidirectionalIterator, class Allocator>
0542     bool
0543     operator==(const match_results<BidirectionalIterator, Allocator>& m1,
0544                const match_results<BidirectionalIterator, Allocator>& m2);
0545 
0546 template <class BidirectionalIterator, class Allocator>                    // Removed in C++20
0547     bool
0548     operator!=(const match_results<BidirectionalIterator, Allocator>& m1,
0549                const match_results<BidirectionalIterator, Allocator>& m2);
0550 
0551 template <class BidirectionalIterator, class Allocator>
0552     void
0553     swap(match_results<BidirectionalIterator, Allocator>& m1,
0554          match_results<BidirectionalIterator, Allocator>& m2);
0555 
0556 template <class BidirectionalIterator, class Allocator, class charT, class traits>
0557     bool
0558     regex_match(BidirectionalIterator first, BidirectionalIterator last,
0559                 match_results<BidirectionalIterator, Allocator>& m,
0560                 const basic_regex<charT, traits>& e,
0561                 regex_constants::match_flag_type flags = regex_constants::match_default);
0562 
0563 template <class BidirectionalIterator, class charT, class traits>
0564     bool
0565     regex_match(BidirectionalIterator first, BidirectionalIterator last,
0566                 const basic_regex<charT, traits>& e,
0567                 regex_constants::match_flag_type flags = regex_constants::match_default);
0568 
0569 template <class charT, class Allocator, class traits>
0570     bool
0571     regex_match(const charT* str, match_results<const charT*, Allocator>& m,
0572                 const basic_regex<charT, traits>& e,
0573                 regex_constants::match_flag_type flags = regex_constants::match_default);
0574 
0575 template <class ST, class SA, class Allocator, class charT, class traits>
0576     bool
0577     regex_match(const basic_string<charT, ST, SA>& s,
0578                 match_results<typename basic_string<charT, ST, SA>::const_iterator, Allocator>& m,
0579                 const basic_regex<charT, traits>& e,
0580                 regex_constants::match_flag_type flags = regex_constants::match_default);
0581 
0582 template <class ST, class SA, class Allocator, class charT, class traits>
0583     bool
0584     regex_match(const basic_string<charT, ST, SA>&& s,
0585                 match_results<typename basic_string<charT, ST, SA>::const_iterator, Allocator>& m,
0586                 const basic_regex<charT, traits>& e,
0587                 regex_constants::match_flag_type flags = regex_constants::match_default) = delete; // C++14
0588 
0589 template <class charT, class traits>
0590     bool
0591     regex_match(const charT* str, const basic_regex<charT, traits>& e,
0592                 regex_constants::match_flag_type flags = regex_constants::match_default);
0593 
0594 template <class ST, class SA, class charT, class traits>
0595     bool
0596     regex_match(const basic_string<charT, ST, SA>& s,
0597                 const basic_regex<charT, traits>& e,
0598                 regex_constants::match_flag_type flags = regex_constants::match_default);
0599 
0600 template <class BidirectionalIterator, class Allocator, class charT, class traits>
0601     bool
0602     regex_search(BidirectionalIterator first, BidirectionalIterator last,
0603                  match_results<BidirectionalIterator, Allocator>& m,
0604                  const basic_regex<charT, traits>& e,
0605                  regex_constants::match_flag_type flags = regex_constants::match_default);
0606 
0607 template <class BidirectionalIterator, class charT, class traits>
0608     bool
0609     regex_search(BidirectionalIterator first, BidirectionalIterator last,
0610                  const basic_regex<charT, traits>& e,
0611                  regex_constants::match_flag_type flags = regex_constants::match_default);
0612 
0613 template <class charT, class Allocator, class traits>
0614     bool
0615     regex_search(const charT* str, match_results<const charT*, Allocator>& m,
0616                  const basic_regex<charT, traits>& e,
0617                  regex_constants::match_flag_type flags = regex_constants::match_default);
0618 
0619 template <class charT, class traits>
0620     bool
0621     regex_search(const charT* str, const basic_regex<charT, traits>& e,
0622                  regex_constants::match_flag_type flags = regex_constants::match_default);
0623 
0624 template <class ST, class SA, class charT, class traits>
0625     bool
0626     regex_search(const basic_string<charT, ST, SA>& s,
0627                  const basic_regex<charT, traits>& e,
0628                  regex_constants::match_flag_type flags = regex_constants::match_default);
0629 
0630 template <class ST, class SA, class Allocator, class charT, class traits>
0631     bool
0632     regex_search(const basic_string<charT, ST, SA>& s,
0633                  match_results<typename basic_string<charT, ST, SA>::const_iterator, Allocator>& m,
0634                  const basic_regex<charT, traits>& e,
0635                  regex_constants::match_flag_type flags = regex_constants::match_default);
0636 
0637 template <class ST, class SA, class Allocator, class charT, class traits>
0638     bool
0639     regex_search(const basic_string<charT, ST, SA>&& s,
0640                  match_results<typename basic_string<charT, ST, SA>::const_iterator, Allocator>& m,
0641                  const basic_regex<charT, traits>& e,
0642                  regex_constants::match_flag_type flags = regex_constants::match_default) = delete; // C++14
0643 
0644 template <class OutputIterator, class BidirectionalIterator,
0645           class traits, class charT, class ST, class SA>
0646     OutputIterator
0647     regex_replace(OutputIterator out,
0648                   BidirectionalIterator first, BidirectionalIterator last,
0649                   const basic_regex<charT, traits>& e,
0650                   const basic_string<charT, ST, SA>& fmt,
0651                   regex_constants::match_flag_type flags = regex_constants::match_default);
0652 
0653 template <class OutputIterator, class BidirectionalIterator,
0654           class traits, class charT>
0655     OutputIterator
0656     regex_replace(OutputIterator out,
0657                   BidirectionalIterator first, BidirectionalIterator last,
0658                   const basic_regex<charT, traits>& e, const charT* fmt,
0659                   regex_constants::match_flag_type flags = regex_constants::match_default);
0660 
0661 template <class traits, class charT, class ST, class SA, class FST, class FSA>
0662     basic_string<charT, ST, SA>
0663     regex_replace(const basic_string<charT, ST, SA>& s,
0664                   const basic_regex<charT, traits>& e,
0665                   const basic_string<charT, FST, FSA>& fmt,
0666                   regex_constants::match_flag_type flags = regex_constants::match_default);
0667 
0668 template <class traits, class charT, class ST, class SA>
0669     basic_string<charT, ST, SA>
0670     regex_replace(const basic_string<charT, ST, SA>& s,
0671                   const basic_regex<charT, traits>& e, const charT* fmt,
0672                   regex_constants::match_flag_type flags = regex_constants::match_default);
0673 
0674 template <class traits, class charT, class ST, class SA>
0675     basic_string<charT>
0676     regex_replace(const charT* s,
0677                   const basic_regex<charT, traits>& e,
0678                   const basic_string<charT, ST, SA>& fmt,
0679                   regex_constants::match_flag_type flags = regex_constants::match_default);
0680 
0681 template <class traits, class charT>
0682     basic_string<charT>
0683     regex_replace(const charT* s,
0684                   const basic_regex<charT, traits>& e,
0685                   const charT* fmt,
0686                   regex_constants::match_flag_type flags = regex_constants::match_default);
0687 
0688 template <class BidirectionalIterator,
0689           class charT = typename iterator_traits< BidirectionalIterator>::value_type,
0690           class traits = regex_traits<charT>>
0691 class regex_iterator
0692 {
0693 public:
0694     typedef basic_regex<charT, traits>           regex_type;
0695     typedef match_results<BidirectionalIterator> value_type;
0696     typedef ptrdiff_t                            difference_type;
0697     typedef const value_type*                    pointer;
0698     typedef const value_type&                    reference;
0699     typedef forward_iterator_tag                 iterator_category;
0700     typedef input_iterator_tag                   iterator_concept; // since C++20
0701 
0702     regex_iterator();
0703     regex_iterator(BidirectionalIterator a, BidirectionalIterator b,
0704                    const regex_type& re,
0705                    regex_constants::match_flag_type m = regex_constants::match_default);
0706     regex_iterator(BidirectionalIterator a, BidirectionalIterator b,
0707                    const regex_type&& re,
0708                    regex_constants::match_flag_type m
0709                                      = regex_constants::match_default) = delete; // C++14
0710     regex_iterator(const regex_iterator&);
0711     regex_iterator& operator=(const regex_iterator&);
0712 
0713     bool operator==(const regex_iterator&) const;
0714     bool operator==(default_sentinel_t) const { return *this == regex_iterator(); } // since C++20
0715     bool operator!=(const regex_iterator&) const;                                   // Removed in C++20
0716 
0717     const value_type& operator*() const;
0718     const value_type* operator->() const;
0719 
0720     regex_iterator& operator++();
0721     regex_iterator operator++(int);
0722 };
0723 
0724 typedef regex_iterator<const char*>             cregex_iterator;
0725 typedef regex_iterator<const wchar_t*>          wcregex_iterator;
0726 typedef regex_iterator<string::const_iterator>  sregex_iterator;
0727 typedef regex_iterator<wstring::const_iterator> wsregex_iterator;
0728 
0729 template <class BidirectionalIterator,
0730           class charT = typename iterator_traits<BidirectionalIterator>::value_type,
0731           class traits = regex_traits<charT>>
0732 class regex_token_iterator
0733 {
0734 public:
0735     typedef basic_regex<charT, traits>       regex_type;
0736     typedef sub_match<BidirectionalIterator> value_type;
0737     typedef ptrdiff_t                        difference_type;
0738     typedef const value_type*                pointer;
0739     typedef const value_type&                reference;
0740     typedef forward_iterator_tag             iterator_category;
0741     typedef input_iterator_tag               iterator_concept; // since C++20
0742 
0743     regex_token_iterator();
0744     regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b,
0745                          const regex_type& re, int submatch = 0,
0746                          regex_constants::match_flag_type m = regex_constants::match_default);
0747     regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b,
0748                          const regex_type&& re, int submatch = 0,
0749                          regex_constants::match_flag_type m = regex_constants::match_default) = delete; // C++14
0750     regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b,
0751                          const regex_type& re, const vector<int>& submatches,
0752                          regex_constants::match_flag_type m = regex_constants::match_default);
0753     regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b,
0754                          const regex_type&& re, const vector<int>& submatches,
0755                          regex_constants::match_flag_type m = regex_constants::match_default) = delete; // C++14
0756     regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b,
0757                          const regex_type& re, initializer_list<int> submatches,
0758                          regex_constants::match_flag_type m = regex_constants::match_default);
0759     regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b,
0760                          const regex_type&& re, initializer_list<int> submatches,
0761                          regex_constants::match_flag_type m = regex_constants::match_default) = delete; // C++14
0762     template <size_t N>
0763         regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b,
0764                              const regex_type& re, const int (&submatches)[N],
0765                              regex_constants::match_flag_type m = regex_constants::match_default);
0766     template <size_t N>
0767         regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b,
0768                              const regex_type&& re, const int (&submatches)[N],
0769                              regex_constants::match_flag_type m = regex_constants::match_default) = delete; // C++14
0770     regex_token_iterator(const regex_token_iterator&);
0771     regex_token_iterator& operator=(const regex_token_iterator&);
0772 
0773     bool operator==(const regex_token_iterator&) const;
0774     bool operator==(default_sentinel_t) const { return *this == regex_token_iterator(); } // since C++20
0775     bool operator!=(const regex_token_iterator&) const;                                   // Removed in C++20
0776 
0777     const value_type& operator*() const;
0778     const value_type* operator->() const;
0779 
0780     regex_token_iterator& operator++();
0781     regex_token_iterator operator++(int);
0782 };
0783 
0784 typedef regex_token_iterator<const char*>             cregex_token_iterator;
0785 typedef regex_token_iterator<const wchar_t*>          wcregex_token_iterator;
0786 typedef regex_token_iterator<string::const_iterator>  sregex_token_iterator;
0787 typedef regex_token_iterator<wstring::const_iterator> wsregex_token_iterator;
0788 
0789 } // std
0790 */
0791 
0792 #if __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
0793 #  include <__cxx03/regex>
0794 #else
0795 #  include <__config>
0796 
0797 #  if _LIBCPP_HAS_LOCALIZATION
0798 
0799 #    include <__algorithm/find.h>
0800 #    include <__algorithm/search.h>
0801 #    include <__iterator/back_insert_iterator.h>
0802 #    include <__iterator/default_sentinel.h>
0803 #    include <__iterator/wrap_iter.h>
0804 #    include <__locale>
0805 #    include <__memory/shared_ptr.h>
0806 #    include <__memory_resource/polymorphic_allocator.h>
0807 #    include <__type_traits/is_swappable.h>
0808 #    include <__utility/move.h>
0809 #    include <__utility/pair.h>
0810 #    include <__utility/swap.h>
0811 #    include <__verbose_abort>
0812 #    include <deque>
0813 #    include <stdexcept>
0814 #    include <string>
0815 #    include <vector>
0816 #    include <version>
0817 
0818 // standard-mandated includes
0819 
0820 // [iterator.range]
0821 #    include <__iterator/access.h>
0822 #    include <__iterator/data.h>
0823 #    include <__iterator/empty.h>
0824 #    include <__iterator/reverse_access.h>
0825 #    include <__iterator/size.h>
0826 
0827 // [re.syn]
0828 #    include <compare>
0829 #    include <initializer_list>
0830 
0831 #    if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0832 #      pragma GCC system_header
0833 #    endif
0834 
0835 _LIBCPP_PUSH_MACROS
0836 #    include <__undef_macros>
0837 
0838 #    define _LIBCPP_REGEX_COMPLEXITY_FACTOR 4096
0839 
0840 _LIBCPP_BEGIN_NAMESPACE_STD
0841 
0842 namespace regex_constants {
0843 
0844 // syntax_option_type
0845 
0846 enum syntax_option_type {
0847   icase    = 1 << 0,
0848   nosubs   = 1 << 1,
0849   optimize = 1 << 2,
0850   collate  = 1 << 3,
0851 #    ifdef _LIBCPP_ABI_REGEX_CONSTANTS_NONZERO
0852   ECMAScript = 1 << 9,
0853 #    else
0854   ECMAScript = 0,
0855 #    endif
0856   basic    = 1 << 4,
0857   extended = 1 << 5,
0858   awk      = 1 << 6,
0859   grep     = 1 << 7,
0860   egrep    = 1 << 8,
0861   // 1 << 9 may be used by ECMAScript
0862   multiline = 1 << 10
0863 };
0864 
0865 _LIBCPP_HIDE_FROM_ABI inline _LIBCPP_CONSTEXPR syntax_option_type __get_grammar(syntax_option_type __g) {
0866 #    ifdef _LIBCPP_ABI_REGEX_CONSTANTS_NONZERO
0867   return static_cast<syntax_option_type>(__g & 0x3F0);
0868 #    else
0869   return static_cast<syntax_option_type>(__g & 0x1F0);
0870 #    endif
0871 }
0872 
0873 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR syntax_option_type operator~(syntax_option_type __x) {
0874   return syntax_option_type(~int(__x) & 0x1FF);
0875 }
0876 
0877 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR syntax_option_type
0878 operator&(syntax_option_type __x, syntax_option_type __y) {
0879   return syntax_option_type(int(__x) & int(__y));
0880 }
0881 
0882 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR syntax_option_type
0883 operator|(syntax_option_type __x, syntax_option_type __y) {
0884   return syntax_option_type(int(__x) | int(__y));
0885 }
0886 
0887 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR syntax_option_type
0888 operator^(syntax_option_type __x, syntax_option_type __y) {
0889   return syntax_option_type(int(__x) ^ int(__y));
0890 }
0891 
0892 inline _LIBCPP_HIDE_FROM_ABI syntax_option_type& operator&=(syntax_option_type& __x, syntax_option_type __y) {
0893   __x = __x & __y;
0894   return __x;
0895 }
0896 
0897 inline _LIBCPP_HIDE_FROM_ABI syntax_option_type& operator|=(syntax_option_type& __x, syntax_option_type __y) {
0898   __x = __x | __y;
0899   return __x;
0900 }
0901 
0902 inline _LIBCPP_HIDE_FROM_ABI syntax_option_type& operator^=(syntax_option_type& __x, syntax_option_type __y) {
0903   __x = __x ^ __y;
0904   return __x;
0905 }
0906 
0907 // match_flag_type
0908 
0909 enum match_flag_type {
0910   match_default     = 0,
0911   match_not_bol     = 1 << 0,
0912   match_not_eol     = 1 << 1,
0913   match_not_bow     = 1 << 2,
0914   match_not_eow     = 1 << 3,
0915   match_any         = 1 << 4,
0916   match_not_null    = 1 << 5,
0917   match_continuous  = 1 << 6,
0918   match_prev_avail  = 1 << 7,
0919   format_default    = 0,
0920   format_sed        = 1 << 8,
0921   format_no_copy    = 1 << 9,
0922   format_first_only = 1 << 10,
0923   __no_update_pos   = 1 << 11,
0924   __full_match      = 1 << 12
0925 };
0926 
0927 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR match_flag_type operator~(match_flag_type __x) {
0928   return match_flag_type(~int(__x) & 0x0FFF);
0929 }
0930 
0931 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR match_flag_type operator&(match_flag_type __x, match_flag_type __y) {
0932   return match_flag_type(int(__x) & int(__y));
0933 }
0934 
0935 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR match_flag_type operator|(match_flag_type __x, match_flag_type __y) {
0936   return match_flag_type(int(__x) | int(__y));
0937 }
0938 
0939 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR match_flag_type operator^(match_flag_type __x, match_flag_type __y) {
0940   return match_flag_type(int(__x) ^ int(__y));
0941 }
0942 
0943 inline _LIBCPP_HIDE_FROM_ABI match_flag_type& operator&=(match_flag_type& __x, match_flag_type __y) {
0944   __x = __x & __y;
0945   return __x;
0946 }
0947 
0948 inline _LIBCPP_HIDE_FROM_ABI match_flag_type& operator|=(match_flag_type& __x, match_flag_type __y) {
0949   __x = __x | __y;
0950   return __x;
0951 }
0952 
0953 inline _LIBCPP_HIDE_FROM_ABI match_flag_type& operator^=(match_flag_type& __x, match_flag_type __y) {
0954   __x = __x ^ __y;
0955   return __x;
0956 }
0957 
0958 enum error_type {
0959   error_collate = 1,
0960   error_ctype,
0961   error_escape,
0962   error_backref,
0963   error_brack,
0964   error_paren,
0965   error_brace,
0966   error_badbrace,
0967   error_range,
0968   error_space,
0969   error_badrepeat,
0970   error_complexity,
0971   error_stack,
0972   __re_err_grammar,
0973   __re_err_empty,
0974   __re_err_unknown,
0975   __re_err_parse
0976 };
0977 
0978 } // namespace regex_constants
0979 
0980 class _LIBCPP_EXPORTED_FROM_ABI regex_error : public runtime_error {
0981   regex_constants::error_type __code_;
0982 
0983 public:
0984   explicit regex_error(regex_constants::error_type __ecode);
0985   _LIBCPP_HIDE_FROM_ABI regex_error(const regex_error&) _NOEXCEPT = default;
0986   ~regex_error() _NOEXCEPT override;
0987   _LIBCPP_HIDE_FROM_ABI regex_constants::error_type code() const { return __code_; }
0988 };
0989 
0990 template <regex_constants::error_type _Ev>
0991 [[__noreturn__]] inline _LIBCPP_HIDE_FROM_ABI void __throw_regex_error() {
0992 #    if _LIBCPP_HAS_EXCEPTIONS
0993   throw regex_error(_Ev);
0994 #    else
0995   _LIBCPP_VERBOSE_ABORT("regex_error was thrown in -fno-exceptions mode");
0996 #    endif
0997 }
0998 
0999 template <class _CharT>
1000 struct _LIBCPP_TEMPLATE_VIS regex_traits {
1001 public:
1002   typedef _CharT char_type;
1003   typedef basic_string<char_type> string_type;
1004   typedef locale locale_type;
1005 #    if defined(__BIONIC__) || defined(_NEWLIB_VERSION)
1006   // Originally bionic's ctype_base used its own ctype masks because the
1007   // builtin ctype implementation wasn't in libc++ yet. Bionic's ctype mask
1008   // was only 8 bits wide and already saturated, so it used a wider type here
1009   // to make room for __regex_word (then a part of this class rather than
1010   // ctype_base). Bionic has since moved to the builtin ctype_base
1011   // implementation, but this was not updated to match. Since then Android has
1012   // needed to maintain a stable libc++ ABI, and this can't be changed without
1013   // an ABI break.
1014   // We also need this workaround for newlib since _NEWLIB_VERSION is not
1015   // defined yet inside __config, so we can't set the
1016   // _LIBCPP_PROVIDES_DEFAULT_RUNE_TABLE macro. Additionally, newlib is
1017   // often used for space constrained environments, so it makes sense not to
1018   // duplicate the ctype table.
1019   typedef uint16_t char_class_type;
1020 #    else
1021   typedef ctype_base::mask char_class_type;
1022 #    endif
1023 
1024   static const char_class_type __regex_word = ctype_base::__regex_word;
1025 
1026 private:
1027   locale __loc_;
1028   const ctype<char_type>* __ct_;
1029   const collate<char_type>* __col_;
1030 
1031 public:
1032   regex_traits();
1033 
1034   _LIBCPP_HIDE_FROM_ABI static size_t length(const char_type* __p) { return char_traits<char_type>::length(__p); }
1035   _LIBCPP_HIDE_FROM_ABI char_type translate(char_type __c) const { return __c; }
1036   char_type translate_nocase(char_type __c) const;
1037   template <class _ForwardIterator>
1038   string_type transform(_ForwardIterator __f, _ForwardIterator __l) const;
1039   template <class _ForwardIterator>
1040   _LIBCPP_HIDE_FROM_ABI string_type transform_primary(_ForwardIterator __f, _ForwardIterator __l) const {
1041     return __transform_primary(__f, __l, char_type());
1042   }
1043   template <class _ForwardIterator>
1044   _LIBCPP_HIDE_FROM_ABI string_type lookup_collatename(_ForwardIterator __f, _ForwardIterator __l) const {
1045     return __lookup_collatename(__f, __l, char_type());
1046   }
1047   template <class _ForwardIterator>
1048   _LIBCPP_HIDE_FROM_ABI char_class_type
1049   lookup_classname(_ForwardIterator __f, _ForwardIterator __l, bool __icase = false) const {
1050     return __lookup_classname(__f, __l, __icase, char_type());
1051   }
1052   bool isctype(char_type __c, char_class_type __m) const;
1053   _LIBCPP_HIDE_FROM_ABI int value(char_type __ch, int __radix) const { return __regex_traits_value(__ch, __radix); }
1054   locale_type imbue(locale_type __l);
1055   _LIBCPP_HIDE_FROM_ABI locale_type getloc() const { return __loc_; }
1056 
1057 private:
1058   void __init();
1059 
1060   template <class _ForwardIterator>
1061   string_type __transform_primary(_ForwardIterator __f, _ForwardIterator __l, char) const;
1062 #    if _LIBCPP_HAS_WIDE_CHARACTERS
1063   template <class _ForwardIterator>
1064   string_type __transform_primary(_ForwardIterator __f, _ForwardIterator __l, wchar_t) const;
1065 #    endif
1066   template <class _ForwardIterator>
1067   string_type __lookup_collatename(_ForwardIterator __f, _ForwardIterator __l, char) const;
1068 #    if _LIBCPP_HAS_WIDE_CHARACTERS
1069   template <class _ForwardIterator>
1070   string_type __lookup_collatename(_ForwardIterator __f, _ForwardIterator __l, wchar_t) const;
1071 #    endif
1072   template <class _ForwardIterator>
1073   char_class_type __lookup_classname(_ForwardIterator __f, _ForwardIterator __l, bool __icase, char) const;
1074 #    if _LIBCPP_HAS_WIDE_CHARACTERS
1075   template <class _ForwardIterator>
1076   char_class_type __lookup_classname(_ForwardIterator __f, _ForwardIterator __l, bool __icase, wchar_t) const;
1077 #    endif
1078 
1079   static int __regex_traits_value(unsigned char __ch, int __radix);
1080   _LIBCPP_HIDE_FROM_ABI int __regex_traits_value(char __ch, int __radix) const {
1081     return __regex_traits_value(static_cast<unsigned char>(__ch), __radix);
1082   }
1083 #    if _LIBCPP_HAS_WIDE_CHARACTERS
1084   _LIBCPP_HIDE_FROM_ABI int __regex_traits_value(wchar_t __ch, int __radix) const;
1085 #    endif
1086 };
1087 
1088 template <class _CharT>
1089 const typename regex_traits<_CharT>::char_class_type regex_traits<_CharT>::__regex_word;
1090 
1091 template <class _CharT>
1092 regex_traits<_CharT>::regex_traits() {
1093   __init();
1094 }
1095 
1096 template <class _CharT>
1097 typename regex_traits<_CharT>::char_type regex_traits<_CharT>::translate_nocase(char_type __c) const {
1098   return __ct_->tolower(__c);
1099 }
1100 
1101 template <class _CharT>
1102 template <class _ForwardIterator>
1103 typename regex_traits<_CharT>::string_type
1104 regex_traits<_CharT>::transform(_ForwardIterator __f, _ForwardIterator __l) const {
1105   string_type __s(__f, __l);
1106   return __col_->transform(__s.data(), __s.data() + __s.size());
1107 }
1108 
1109 template <class _CharT>
1110 void regex_traits<_CharT>::__init() {
1111   __ct_  = &std::use_facet<ctype<char_type> >(__loc_);
1112   __col_ = &std::use_facet<collate<char_type> >(__loc_);
1113 }
1114 
1115 template <class _CharT>
1116 typename regex_traits<_CharT>::locale_type regex_traits<_CharT>::imbue(locale_type __l) {
1117   locale __r = __loc_;
1118   __loc_     = __l;
1119   __init();
1120   return __r;
1121 }
1122 
1123 // transform_primary is very FreeBSD-specific
1124 
1125 template <class _CharT>
1126 template <class _ForwardIterator>
1127 typename regex_traits<_CharT>::string_type
1128 regex_traits<_CharT>::__transform_primary(_ForwardIterator __f, _ForwardIterator __l, char) const {
1129   const string_type __s(__f, __l);
1130   string_type __d = __col_->transform(__s.data(), __s.data() + __s.size());
1131   switch (__d.size()) {
1132   case 1:
1133     break;
1134   case 12:
1135     __d[11] = __d[3];
1136     break;
1137   default:
1138     __d.clear();
1139     break;
1140   }
1141   return __d;
1142 }
1143 
1144 #    if _LIBCPP_HAS_WIDE_CHARACTERS
1145 template <class _CharT>
1146 template <class _ForwardIterator>
1147 typename regex_traits<_CharT>::string_type
1148 regex_traits<_CharT>::__transform_primary(_ForwardIterator __f, _ForwardIterator __l, wchar_t) const {
1149   const string_type __s(__f, __l);
1150   string_type __d = __col_->transform(__s.data(), __s.data() + __s.size());
1151   switch (__d.size()) {
1152   case 1:
1153     break;
1154   case 3:
1155     __d[2] = __d[0];
1156     break;
1157   default:
1158     __d.clear();
1159     break;
1160   }
1161   return __d;
1162 }
1163 #    endif
1164 
1165 // lookup_collatename is very FreeBSD-specific
1166 
1167 _LIBCPP_EXPORTED_FROM_ABI string __get_collation_name(const char* __s);
1168 
1169 template <class _CharT>
1170 template <class _ForwardIterator>
1171 typename regex_traits<_CharT>::string_type
1172 regex_traits<_CharT>::__lookup_collatename(_ForwardIterator __f, _ForwardIterator __l, char) const {
1173   string_type __s(__f, __l);
1174   string_type __r;
1175   if (!__s.empty()) {
1176     __r = std::__get_collation_name(__s.c_str());
1177     if (__r.empty() && __s.size() <= 2) {
1178       __r = __col_->transform(__s.data(), __s.data() + __s.size());
1179       if (__r.size() == 1 || __r.size() == 12)
1180         __r = __s;
1181       else
1182         __r.clear();
1183     }
1184   }
1185   return __r;
1186 }
1187 
1188 #    if _LIBCPP_HAS_WIDE_CHARACTERS
1189 template <class _CharT>
1190 template <class _ForwardIterator>
1191 typename regex_traits<_CharT>::string_type
1192 regex_traits<_CharT>::__lookup_collatename(_ForwardIterator __f, _ForwardIterator __l, wchar_t) const {
1193   string_type __s(__f, __l);
1194   string __n;
1195   __n.reserve(__s.size());
1196   for (typename string_type::const_iterator __i = __s.begin(), __e = __s.end(); __i != __e; ++__i) {
1197     if (static_cast<unsigned>(*__i) >= 127)
1198       return string_type();
1199     __n.push_back(char(*__i));
1200   }
1201   string_type __r;
1202   if (!__s.empty()) {
1203     __n = __get_collation_name(__n.c_str());
1204     if (!__n.empty())
1205       __r.assign(__n.begin(), __n.end());
1206     else if (__s.size() <= 2) {
1207       __r = __col_->transform(__s.data(), __s.data() + __s.size());
1208       if (__r.size() == 1 || __r.size() == 3)
1209         __r = __s;
1210       else
1211         __r.clear();
1212     }
1213   }
1214   return __r;
1215 }
1216 #    endif // _LIBCPP_HAS_WIDE_CHARACTERS
1217 
1218 // lookup_classname
1219 
1220 regex_traits<char>::char_class_type _LIBCPP_EXPORTED_FROM_ABI __get_classname(const char* __s, bool __icase);
1221 
1222 template <class _CharT>
1223 template <class _ForwardIterator>
1224 typename regex_traits<_CharT>::char_class_type
1225 regex_traits<_CharT>::__lookup_classname(_ForwardIterator __f, _ForwardIterator __l, bool __icase, char) const {
1226   string_type __s(__f, __l);
1227   __ct_->tolower(&__s[0], &__s[0] + __s.size());
1228   return std::__get_classname(__s.c_str(), __icase);
1229 }
1230 
1231 #    if _LIBCPP_HAS_WIDE_CHARACTERS
1232 template <class _CharT>
1233 template <class _ForwardIterator>
1234 typename regex_traits<_CharT>::char_class_type
1235 regex_traits<_CharT>::__lookup_classname(_ForwardIterator __f, _ForwardIterator __l, bool __icase, wchar_t) const {
1236   string_type __s(__f, __l);
1237   __ct_->tolower(&__s[0], &__s[0] + __s.size());
1238   string __n;
1239   __n.reserve(__s.size());
1240   for (typename string_type::const_iterator __i = __s.begin(), __e = __s.end(); __i != __e; ++__i) {
1241     if (static_cast<unsigned>(*__i) >= 127)
1242       return char_class_type();
1243     __n.push_back(char(*__i));
1244   }
1245   return __get_classname(__n.c_str(), __icase);
1246 }
1247 #    endif // _LIBCPP_HAS_WIDE_CHARACTERS
1248 
1249 template <class _CharT>
1250 bool regex_traits<_CharT>::isctype(char_type __c, char_class_type __m) const {
1251   if (__ct_->is(__m, __c))
1252     return true;
1253   return (__c == '_' && (__m & __regex_word));
1254 }
1255 
1256 inline _LIBCPP_HIDE_FROM_ABI bool __is_07(unsigned char __c) {
1257   return (__c & 0xF8u) ==
1258 #    if defined(__MVS__) && !defined(__NATIVE_ASCII_F)
1259          0xF0;
1260 #    else
1261          0x30;
1262 #    endif
1263 }
1264 
1265 inline _LIBCPP_HIDE_FROM_ABI bool __is_89(unsigned char __c) {
1266   return (__c & 0xFEu) ==
1267 #    if defined(__MVS__) && !defined(__NATIVE_ASCII_F)
1268          0xF8;
1269 #    else
1270          0x38;
1271 #    endif
1272 }
1273 
1274 inline _LIBCPP_HIDE_FROM_ABI unsigned char __to_lower(unsigned char __c) {
1275 #    if defined(__MVS__) && !defined(__NATIVE_ASCII_F)
1276   return __c & 0xBF;
1277 #    else
1278   return __c | 0x20;
1279 #    endif
1280 }
1281 
1282 template <class _CharT>
1283 int regex_traits<_CharT>::__regex_traits_value(unsigned char __ch, int __radix) {
1284   if (__is_07(__ch)) // '0' <= __ch && __ch <= '7'
1285     return __ch - '0';
1286   if (__radix != 8) {
1287     if (__is_89(__ch)) // '8' <= __ch && __ch <= '9'
1288       return __ch - '0';
1289     if (__radix == 16) {
1290       __ch = __to_lower(__ch); // tolower
1291       if ('a' <= __ch && __ch <= 'f')
1292         return __ch - ('a' - 10);
1293     }
1294   }
1295   return -1;
1296 }
1297 
1298 #    if _LIBCPP_HAS_WIDE_CHARACTERS
1299 template <class _CharT>
1300 inline int regex_traits<_CharT>::__regex_traits_value(wchar_t __ch, int __radix) const {
1301   return __regex_traits_value(static_cast<unsigned char>(__ct_->narrow(__ch, char_type())), __radix);
1302 }
1303 #    endif
1304 
1305 template <class _CharT>
1306 class __node;
1307 
1308 template <class _BidirectionalIterator>
1309 class _LIBCPP_TEMPLATE_VIS sub_match;
1310 
1311 template <class _BidirectionalIterator, class _Allocator = allocator<sub_match<_BidirectionalIterator> > >
1312 class _LIBCPP_TEMPLATE_VIS match_results;
1313 
1314 template <class _CharT>
1315 struct __state {
1316   enum {
1317     __end_state = -1000,
1318     __consume_input,          // -999
1319     __begin_marked_expr,      // -998
1320     __end_marked_expr,        // -997
1321     __pop_state,              // -996
1322     __accept_and_consume,     // -995
1323     __accept_but_not_consume, // -994
1324     __reject,                 // -993
1325     __split,
1326     __repeat
1327   };
1328 
1329   int __do_;
1330   const _CharT* __first_;
1331   const _CharT* __current_;
1332   const _CharT* __last_;
1333   vector<sub_match<const _CharT*> > __sub_matches_;
1334   vector<pair<size_t, const _CharT*> > __loop_data_;
1335   const __node<_CharT>* __node_;
1336   regex_constants::match_flag_type __flags_;
1337   bool __at_first_;
1338 
1339   _LIBCPP_HIDE_FROM_ABI __state()
1340       : __do_(0),
1341         __first_(nullptr),
1342         __current_(nullptr),
1343         __last_(nullptr),
1344         __node_(nullptr),
1345         __flags_(),
1346         __at_first_(false) {}
1347 };
1348 
1349 // __node
1350 
1351 template <class _CharT>
1352 class __node {
1353 public:
1354   typedef std::__state<_CharT> __state;
1355 
1356   _LIBCPP_HIDE_FROM_ABI __node() {}
1357   __node(const __node&)            = delete;
1358   __node& operator=(const __node&) = delete;
1359   _LIBCPP_HIDE_FROM_ABI_VIRTUAL
1360   virtual ~__node() {}
1361 
1362   _LIBCPP_HIDE_FROM_ABI_VIRTUAL
1363   virtual void __exec(__state&) const {}
1364   _LIBCPP_HIDE_FROM_ABI_VIRTUAL
1365   virtual void __exec_split(bool, __state&) const {}
1366 };
1367 
1368 // __end_state
1369 
1370 template <class _CharT>
1371 class __end_state : public __node<_CharT> {
1372 public:
1373   typedef std::__state<_CharT> __state;
1374 
1375   _LIBCPP_HIDE_FROM_ABI __end_state() {}
1376 
1377   _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
1378 };
1379 
1380 template <class _CharT>
1381 void __end_state<_CharT>::__exec(__state& __s) const {
1382   __s.__do_ = __state::__end_state;
1383 }
1384 
1385 // __has_one_state
1386 
1387 template <class _CharT>
1388 class __has_one_state : public __node<_CharT> {
1389   __node<_CharT>* __first_;
1390 
1391 public:
1392   _LIBCPP_HIDE_FROM_ABI explicit __has_one_state(__node<_CharT>* __s) : __first_(__s) {}
1393 
1394   _LIBCPP_HIDE_FROM_ABI __node<_CharT>* first() const { return __first_; }
1395   _LIBCPP_HIDE_FROM_ABI __node<_CharT>*& first() { return __first_; }
1396 };
1397 
1398 // __owns_one_state
1399 
1400 template <class _CharT>
1401 class __owns_one_state : public __has_one_state<_CharT> {
1402   typedef __has_one_state<_CharT> base;
1403 
1404 public:
1405   _LIBCPP_HIDE_FROM_ABI explicit __owns_one_state(__node<_CharT>* __s) : base(__s) {}
1406 
1407   ~__owns_one_state() override;
1408 };
1409 
1410 template <class _CharT>
1411 __owns_one_state<_CharT>::~__owns_one_state() {
1412   delete this->first();
1413 }
1414 
1415 // __empty_state
1416 
1417 template <class _CharT>
1418 class __empty_state : public __owns_one_state<_CharT> {
1419   typedef __owns_one_state<_CharT> base;
1420 
1421 public:
1422   typedef std::__state<_CharT> __state;
1423 
1424   _LIBCPP_HIDE_FROM_ABI explicit __empty_state(__node<_CharT>* __s) : base(__s) {}
1425 
1426   _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
1427 };
1428 
1429 template <class _CharT>
1430 void __empty_state<_CharT>::__exec(__state& __s) const {
1431   __s.__do_   = __state::__accept_but_not_consume;
1432   __s.__node_ = this->first();
1433 }
1434 
1435 // __empty_non_own_state
1436 
1437 template <class _CharT>
1438 class __empty_non_own_state : public __has_one_state<_CharT> {
1439   typedef __has_one_state<_CharT> base;
1440 
1441 public:
1442   typedef std::__state<_CharT> __state;
1443 
1444   _LIBCPP_HIDE_FROM_ABI explicit __empty_non_own_state(__node<_CharT>* __s) : base(__s) {}
1445 
1446   _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
1447 };
1448 
1449 template <class _CharT>
1450 void __empty_non_own_state<_CharT>::__exec(__state& __s) const {
1451   __s.__do_   = __state::__accept_but_not_consume;
1452   __s.__node_ = this->first();
1453 }
1454 
1455 // __repeat_one_loop
1456 
1457 template <class _CharT>
1458 class __repeat_one_loop : public __has_one_state<_CharT> {
1459   typedef __has_one_state<_CharT> base;
1460 
1461 public:
1462   typedef std::__state<_CharT> __state;
1463 
1464   _LIBCPP_HIDE_FROM_ABI explicit __repeat_one_loop(__node<_CharT>* __s) : base(__s) {}
1465 
1466   _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
1467 };
1468 
1469 template <class _CharT>
1470 void __repeat_one_loop<_CharT>::__exec(__state& __s) const {
1471   __s.__do_   = __state::__repeat;
1472   __s.__node_ = this->first();
1473 }
1474 
1475 // __owns_two_states
1476 
1477 template <class _CharT>
1478 class __owns_two_states : public __owns_one_state<_CharT> {
1479   typedef __owns_one_state<_CharT> base;
1480 
1481   base* __second_;
1482 
1483 public:
1484   _LIBCPP_HIDE_FROM_ABI explicit __owns_two_states(__node<_CharT>* __s1, base* __s2) : base(__s1), __second_(__s2) {}
1485 
1486   _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual ~__owns_two_states();
1487 
1488   _LIBCPP_HIDE_FROM_ABI base* second() const { return __second_; }
1489   _LIBCPP_HIDE_FROM_ABI base*& second() { return __second_; }
1490 };
1491 
1492 template <class _CharT>
1493 __owns_two_states<_CharT>::~__owns_two_states() {
1494   delete __second_;
1495 }
1496 
1497 // __loop
1498 
1499 template <class _CharT>
1500 class __loop : public __owns_two_states<_CharT> {
1501   typedef __owns_two_states<_CharT> base;
1502 
1503   size_t __min_;
1504   size_t __max_;
1505   unsigned __loop_id_;
1506   unsigned __mexp_begin_;
1507   unsigned __mexp_end_;
1508   bool __greedy_;
1509 
1510 public:
1511   typedef std::__state<_CharT> __state;
1512 
1513   _LIBCPP_HIDE_FROM_ABI explicit __loop(
1514       unsigned __loop_id,
1515       __node<_CharT>* __s1,
1516       __owns_one_state<_CharT>* __s2,
1517       unsigned __mexp_begin,
1518       unsigned __mexp_end,
1519       bool __greedy = true,
1520       size_t __min  = 0,
1521       size_t __max  = numeric_limits<size_t>::max())
1522       : base(__s1, __s2),
1523         __min_(__min),
1524         __max_(__max),
1525         __loop_id_(__loop_id),
1526         __mexp_begin_(__mexp_begin),
1527         __mexp_end_(__mexp_end),
1528         __greedy_(__greedy) {}
1529 
1530   _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state& __s) const;
1531   _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec_split(bool __second, __state& __s) const;
1532 
1533 private:
1534   _LIBCPP_HIDE_FROM_ABI void __init_repeat(__state& __s) const {
1535     __s.__loop_data_[__loop_id_].second = __s.__current_;
1536     for (size_t __i = __mexp_begin_ - 1; __i != __mexp_end_ - 1; ++__i) {
1537       __s.__sub_matches_[__i].first   = __s.__last_;
1538       __s.__sub_matches_[__i].second  = __s.__last_;
1539       __s.__sub_matches_[__i].matched = false;
1540     }
1541   }
1542 };
1543 
1544 template <class _CharT>
1545 void __loop<_CharT>::__exec(__state& __s) const {
1546   if (__s.__do_ == __state::__repeat) {
1547     bool __do_repeat = ++__s.__loop_data_[__loop_id_].first < __max_;
1548     bool __do_alt    = __s.__loop_data_[__loop_id_].first >= __min_;
1549     if (__do_repeat && __do_alt && __s.__loop_data_[__loop_id_].second == __s.__current_)
1550       __do_repeat = false;
1551     if (__do_repeat && __do_alt)
1552       __s.__do_ = __state::__split;
1553     else if (__do_repeat) {
1554       __s.__do_   = __state::__accept_but_not_consume;
1555       __s.__node_ = this->first();
1556       __init_repeat(__s);
1557     } else {
1558       __s.__do_   = __state::__accept_but_not_consume;
1559       __s.__node_ = this->second();
1560     }
1561   } else {
1562     __s.__loop_data_[__loop_id_].first = 0;
1563     bool __do_repeat                   = 0 < __max_;
1564     bool __do_alt                      = 0 >= __min_;
1565     if (__do_repeat && __do_alt)
1566       __s.__do_ = __state::__split;
1567     else if (__do_repeat) {
1568       __s.__do_   = __state::__accept_but_not_consume;
1569       __s.__node_ = this->first();
1570       __init_repeat(__s);
1571     } else {
1572       __s.__do_   = __state::__accept_but_not_consume;
1573       __s.__node_ = this->second();
1574     }
1575   }
1576 }
1577 
1578 template <class _CharT>
1579 void __loop<_CharT>::__exec_split(bool __second, __state& __s) const {
1580   __s.__do_ = __state::__accept_but_not_consume;
1581   if (__greedy_ != __second) {
1582     __s.__node_ = this->first();
1583     __init_repeat(__s);
1584   } else
1585     __s.__node_ = this->second();
1586 }
1587 
1588 // __alternate
1589 
1590 template <class _CharT>
1591 class __alternate : public __owns_two_states<_CharT> {
1592   typedef __owns_two_states<_CharT> base;
1593 
1594 public:
1595   typedef std::__state<_CharT> __state;
1596 
1597   _LIBCPP_HIDE_FROM_ABI explicit __alternate(__owns_one_state<_CharT>* __s1, __owns_one_state<_CharT>* __s2)
1598       : base(__s1, __s2) {}
1599 
1600   _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state& __s) const;
1601   _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec_split(bool __second, __state& __s) const;
1602 };
1603 
1604 template <class _CharT>
1605 void __alternate<_CharT>::__exec(__state& __s) const {
1606   __s.__do_ = __state::__split;
1607 }
1608 
1609 template <class _CharT>
1610 void __alternate<_CharT>::__exec_split(bool __second, __state& __s) const {
1611   __s.__do_ = __state::__accept_but_not_consume;
1612   if (__second)
1613     __s.__node_ = this->second();
1614   else
1615     __s.__node_ = this->first();
1616 }
1617 
1618 // __begin_marked_subexpression
1619 
1620 template <class _CharT>
1621 class __begin_marked_subexpression : public __owns_one_state<_CharT> {
1622   typedef __owns_one_state<_CharT> base;
1623 
1624   unsigned __mexp_;
1625 
1626 public:
1627   typedef std::__state<_CharT> __state;
1628 
1629   _LIBCPP_HIDE_FROM_ABI explicit __begin_marked_subexpression(unsigned __mexp, __node<_CharT>* __s)
1630       : base(__s), __mexp_(__mexp) {}
1631 
1632   _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
1633 };
1634 
1635 template <class _CharT>
1636 void __begin_marked_subexpression<_CharT>::__exec(__state& __s) const {
1637   __s.__do_                             = __state::__accept_but_not_consume;
1638   __s.__sub_matches_[__mexp_ - 1].first = __s.__current_;
1639   __s.__node_                           = this->first();
1640 }
1641 
1642 // __end_marked_subexpression
1643 
1644 template <class _CharT>
1645 class __end_marked_subexpression : public __owns_one_state<_CharT> {
1646   typedef __owns_one_state<_CharT> base;
1647 
1648   unsigned __mexp_;
1649 
1650 public:
1651   typedef std::__state<_CharT> __state;
1652 
1653   _LIBCPP_HIDE_FROM_ABI explicit __end_marked_subexpression(unsigned __mexp, __node<_CharT>* __s)
1654       : base(__s), __mexp_(__mexp) {}
1655 
1656   _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
1657 };
1658 
1659 template <class _CharT>
1660 void __end_marked_subexpression<_CharT>::__exec(__state& __s) const {
1661   __s.__do_                               = __state::__accept_but_not_consume;
1662   __s.__sub_matches_[__mexp_ - 1].second  = __s.__current_;
1663   __s.__sub_matches_[__mexp_ - 1].matched = true;
1664   __s.__node_                             = this->first();
1665 }
1666 
1667 // __back_ref
1668 
1669 template <class _CharT>
1670 class __back_ref : public __owns_one_state<_CharT> {
1671   typedef __owns_one_state<_CharT> base;
1672 
1673   unsigned __mexp_;
1674 
1675 public:
1676   typedef std::__state<_CharT> __state;
1677 
1678   _LIBCPP_HIDE_FROM_ABI explicit __back_ref(unsigned __mexp, __node<_CharT>* __s) : base(__s), __mexp_(__mexp) {}
1679 
1680   _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
1681 };
1682 
1683 template <class _CharT>
1684 void __back_ref<_CharT>::__exec(__state& __s) const {
1685   if (__mexp_ > __s.__sub_matches_.size())
1686     __throw_regex_error<regex_constants::error_backref>();
1687   sub_match<const _CharT*>& __sm = __s.__sub_matches_[__mexp_ - 1];
1688   if (__sm.matched) {
1689     ptrdiff_t __len = __sm.second - __sm.first;
1690     if (__s.__last_ - __s.__current_ >= __len && std::equal(__sm.first, __sm.second, __s.__current_)) {
1691       __s.__do_ = __state::__accept_but_not_consume;
1692       __s.__current_ += __len;
1693       __s.__node_ = this->first();
1694     } else {
1695       __s.__do_   = __state::__reject;
1696       __s.__node_ = nullptr;
1697     }
1698   } else {
1699     __s.__do_   = __state::__reject;
1700     __s.__node_ = nullptr;
1701   }
1702 }
1703 
1704 // __back_ref_icase
1705 
1706 template <class _CharT, class _Traits>
1707 class __back_ref_icase : public __owns_one_state<_CharT> {
1708   typedef __owns_one_state<_CharT> base;
1709 
1710   _Traits __traits_;
1711   unsigned __mexp_;
1712 
1713 public:
1714   typedef std::__state<_CharT> __state;
1715 
1716   _LIBCPP_HIDE_FROM_ABI explicit __back_ref_icase(const _Traits& __traits, unsigned __mexp, __node<_CharT>* __s)
1717       : base(__s), __traits_(__traits), __mexp_(__mexp) {}
1718 
1719   _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
1720 };
1721 
1722 template <class _CharT, class _Traits>
1723 void __back_ref_icase<_CharT, _Traits>::__exec(__state& __s) const {
1724   sub_match<const _CharT*>& __sm = __s.__sub_matches_[__mexp_ - 1];
1725   if (__sm.matched) {
1726     ptrdiff_t __len = __sm.second - __sm.first;
1727     if (__s.__last_ - __s.__current_ >= __len) {
1728       for (ptrdiff_t __i = 0; __i < __len; ++__i) {
1729         if (__traits_.translate_nocase(__sm.first[__i]) != __traits_.translate_nocase(__s.__current_[__i]))
1730           goto __not_equal;
1731       }
1732       __s.__do_ = __state::__accept_but_not_consume;
1733       __s.__current_ += __len;
1734       __s.__node_ = this->first();
1735     } else {
1736       __s.__do_   = __state::__reject;
1737       __s.__node_ = nullptr;
1738     }
1739   } else {
1740   __not_equal:
1741     __s.__do_   = __state::__reject;
1742     __s.__node_ = nullptr;
1743   }
1744 }
1745 
1746 // __back_ref_collate
1747 
1748 template <class _CharT, class _Traits>
1749 class __back_ref_collate : public __owns_one_state<_CharT> {
1750   typedef __owns_one_state<_CharT> base;
1751 
1752   _Traits __traits_;
1753   unsigned __mexp_;
1754 
1755 public:
1756   typedef std::__state<_CharT> __state;
1757 
1758   _LIBCPP_HIDE_FROM_ABI explicit __back_ref_collate(const _Traits& __traits, unsigned __mexp, __node<_CharT>* __s)
1759       : base(__s), __traits_(__traits), __mexp_(__mexp) {}
1760 
1761   _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
1762 };
1763 
1764 template <class _CharT, class _Traits>
1765 void __back_ref_collate<_CharT, _Traits>::__exec(__state& __s) const {
1766   sub_match<const _CharT*>& __sm = __s.__sub_matches_[__mexp_ - 1];
1767   if (__sm.matched) {
1768     ptrdiff_t __len = __sm.second - __sm.first;
1769     if (__s.__last_ - __s.__current_ >= __len) {
1770       for (ptrdiff_t __i = 0; __i < __len; ++__i) {
1771         if (__traits_.translate(__sm.first[__i]) != __traits_.translate(__s.__current_[__i]))
1772           goto __not_equal;
1773       }
1774       __s.__do_ = __state::__accept_but_not_consume;
1775       __s.__current_ += __len;
1776       __s.__node_ = this->first();
1777     } else {
1778       __s.__do_   = __state::__reject;
1779       __s.__node_ = nullptr;
1780     }
1781   } else {
1782   __not_equal:
1783     __s.__do_   = __state::__reject;
1784     __s.__node_ = nullptr;
1785   }
1786 }
1787 
1788 // __word_boundary
1789 
1790 template <class _CharT, class _Traits>
1791 class __word_boundary : public __owns_one_state<_CharT> {
1792   typedef __owns_one_state<_CharT> base;
1793 
1794   _Traits __traits_;
1795   bool __invert_;
1796 
1797 public:
1798   typedef std::__state<_CharT> __state;
1799 
1800   _LIBCPP_HIDE_FROM_ABI explicit __word_boundary(const _Traits& __traits, bool __invert, __node<_CharT>* __s)
1801       : base(__s), __traits_(__traits), __invert_(__invert) {}
1802 
1803   _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
1804 };
1805 
1806 template <class _CharT, class _Traits>
1807 void __word_boundary<_CharT, _Traits>::__exec(__state& __s) const {
1808   bool __is_word_b = false;
1809   if (__s.__first_ != __s.__last_) {
1810     if (__s.__current_ == __s.__last_) {
1811       if (!(__s.__flags_ & regex_constants::match_not_eow)) {
1812         _CharT __c  = __s.__current_[-1];
1813         __is_word_b = __c == '_' || __traits_.isctype(__c, ctype_base::alnum);
1814       }
1815     } else if (__s.__current_ == __s.__first_ && !(__s.__flags_ & regex_constants::match_prev_avail)) {
1816       if (!(__s.__flags_ & regex_constants::match_not_bow)) {
1817         _CharT __c  = *__s.__current_;
1818         __is_word_b = __c == '_' || __traits_.isctype(__c, ctype_base::alnum);
1819       }
1820     } else {
1821       _CharT __c1    = __s.__current_[-1];
1822       _CharT __c2    = *__s.__current_;
1823       bool __is_c1_b = __c1 == '_' || __traits_.isctype(__c1, ctype_base::alnum);
1824       bool __is_c2_b = __c2 == '_' || __traits_.isctype(__c2, ctype_base::alnum);
1825       __is_word_b    = __is_c1_b != __is_c2_b;
1826     }
1827   }
1828   if (__is_word_b != __invert_) {
1829     __s.__do_   = __state::__accept_but_not_consume;
1830     __s.__node_ = this->first();
1831   } else {
1832     __s.__do_   = __state::__reject;
1833     __s.__node_ = nullptr;
1834   }
1835 }
1836 
1837 // __l_anchor
1838 
1839 template <class _CharT>
1840 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool __is_eol(_CharT __c) {
1841   return __c == '\r' || __c == '\n';
1842 }
1843 
1844 template <class _CharT>
1845 class __l_anchor_multiline : public __owns_one_state<_CharT> {
1846   typedef __owns_one_state<_CharT> base;
1847 
1848   bool __multiline_;
1849 
1850 public:
1851   typedef std::__state<_CharT> __state;
1852 
1853   _LIBCPP_HIDE_FROM_ABI __l_anchor_multiline(bool __multiline, __node<_CharT>* __s)
1854       : base(__s), __multiline_(__multiline) {}
1855 
1856   _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
1857 };
1858 
1859 template <class _CharT>
1860 void __l_anchor_multiline<_CharT>::__exec(__state& __s) const {
1861   if (__s.__at_first_ && __s.__current_ == __s.__first_ && !(__s.__flags_ & regex_constants::match_not_bol)) {
1862     __s.__do_   = __state::__accept_but_not_consume;
1863     __s.__node_ = this->first();
1864   } else if (__multiline_ && !__s.__at_first_ && std::__is_eol(*std::prev(__s.__current_))) {
1865     __s.__do_   = __state::__accept_but_not_consume;
1866     __s.__node_ = this->first();
1867   } else {
1868     __s.__do_   = __state::__reject;
1869     __s.__node_ = nullptr;
1870   }
1871 }
1872 
1873 // __r_anchor
1874 
1875 template <class _CharT>
1876 class __r_anchor_multiline : public __owns_one_state<_CharT> {
1877   typedef __owns_one_state<_CharT> base;
1878 
1879   bool __multiline_;
1880 
1881 public:
1882   typedef std::__state<_CharT> __state;
1883 
1884   _LIBCPP_HIDE_FROM_ABI __r_anchor_multiline(bool __multiline, __node<_CharT>* __s)
1885       : base(__s), __multiline_(__multiline) {}
1886 
1887   _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
1888 };
1889 
1890 template <class _CharT>
1891 void __r_anchor_multiline<_CharT>::__exec(__state& __s) const {
1892   if (__s.__current_ == __s.__last_ && !(__s.__flags_ & regex_constants::match_not_eol)) {
1893     __s.__do_   = __state::__accept_but_not_consume;
1894     __s.__node_ = this->first();
1895   } else if (__multiline_ && std::__is_eol(*__s.__current_)) {
1896     __s.__do_   = __state::__accept_but_not_consume;
1897     __s.__node_ = this->first();
1898   } else {
1899     __s.__do_   = __state::__reject;
1900     __s.__node_ = nullptr;
1901   }
1902 }
1903 
1904 // __match_any
1905 
1906 template <class _CharT>
1907 class __match_any : public __owns_one_state<_CharT> {
1908   typedef __owns_one_state<_CharT> base;
1909 
1910 public:
1911   typedef std::__state<_CharT> __state;
1912 
1913   _LIBCPP_HIDE_FROM_ABI __match_any(__node<_CharT>* __s) : base(__s) {}
1914 
1915   _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
1916 };
1917 
1918 template <class _CharT>
1919 void __match_any<_CharT>::__exec(__state& __s) const {
1920   if (__s.__current_ != __s.__last_ && *__s.__current_ != 0) {
1921     __s.__do_ = __state::__accept_and_consume;
1922     ++__s.__current_;
1923     __s.__node_ = this->first();
1924   } else {
1925     __s.__do_   = __state::__reject;
1926     __s.__node_ = nullptr;
1927   }
1928 }
1929 
1930 // __match_any_but_newline
1931 
1932 template <class _CharT>
1933 class __match_any_but_newline : public __owns_one_state<_CharT> {
1934   typedef __owns_one_state<_CharT> base;
1935 
1936 public:
1937   typedef std::__state<_CharT> __state;
1938 
1939   _LIBCPP_HIDE_FROM_ABI __match_any_but_newline(__node<_CharT>* __s) : base(__s) {}
1940 
1941   void __exec(__state&) const override;
1942 };
1943 
1944 template <>
1945 _LIBCPP_EXPORTED_FROM_ABI void __match_any_but_newline<char>::__exec(__state&) const;
1946 #    if _LIBCPP_HAS_WIDE_CHARACTERS
1947 template <>
1948 _LIBCPP_EXPORTED_FROM_ABI void __match_any_but_newline<wchar_t>::__exec(__state&) const;
1949 #    endif
1950 
1951 // __match_char
1952 
1953 template <class _CharT>
1954 class __match_char : public __owns_one_state<_CharT> {
1955   typedef __owns_one_state<_CharT> base;
1956 
1957   _CharT __c_;
1958 
1959 public:
1960   typedef std::__state<_CharT> __state;
1961 
1962   _LIBCPP_HIDE_FROM_ABI __match_char(_CharT __c, __node<_CharT>* __s) : base(__s), __c_(__c) {}
1963 
1964   __match_char(const __match_char&)            = delete;
1965   __match_char& operator=(const __match_char&) = delete;
1966 
1967   _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
1968 };
1969 
1970 template <class _CharT>
1971 void __match_char<_CharT>::__exec(__state& __s) const {
1972   if (__s.__current_ != __s.__last_ && *__s.__current_ == __c_) {
1973     __s.__do_ = __state::__accept_and_consume;
1974     ++__s.__current_;
1975     __s.__node_ = this->first();
1976   } else {
1977     __s.__do_   = __state::__reject;
1978     __s.__node_ = nullptr;
1979   }
1980 }
1981 
1982 // __match_char_icase
1983 
1984 template <class _CharT, class _Traits>
1985 class __match_char_icase : public __owns_one_state<_CharT> {
1986   typedef __owns_one_state<_CharT> base;
1987 
1988   _Traits __traits_;
1989   _CharT __c_;
1990 
1991 public:
1992   typedef std::__state<_CharT> __state;
1993 
1994   _LIBCPP_HIDE_FROM_ABI __match_char_icase(const _Traits& __traits, _CharT __c, __node<_CharT>* __s)
1995       : base(__s), __traits_(__traits), __c_(__traits.translate_nocase(__c)) {}
1996 
1997   __match_char_icase(const __match_char_icase&)            = delete;
1998   __match_char_icase& operator=(const __match_char_icase&) = delete;
1999 
2000   _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
2001 };
2002 
2003 template <class _CharT, class _Traits>
2004 void __match_char_icase<_CharT, _Traits>::__exec(__state& __s) const {
2005   if (__s.__current_ != __s.__last_ && __traits_.translate_nocase(*__s.__current_) == __c_) {
2006     __s.__do_ = __state::__accept_and_consume;
2007     ++__s.__current_;
2008     __s.__node_ = this->first();
2009   } else {
2010     __s.__do_   = __state::__reject;
2011     __s.__node_ = nullptr;
2012   }
2013 }
2014 
2015 // __match_char_collate
2016 
2017 template <class _CharT, class _Traits>
2018 class __match_char_collate : public __owns_one_state<_CharT> {
2019   typedef __owns_one_state<_CharT> base;
2020 
2021   _Traits __traits_;
2022   _CharT __c_;
2023 
2024 public:
2025   typedef std::__state<_CharT> __state;
2026 
2027   _LIBCPP_HIDE_FROM_ABI __match_char_collate(const _Traits& __traits, _CharT __c, __node<_CharT>* __s)
2028       : base(__s), __traits_(__traits), __c_(__traits.translate(__c)) {}
2029 
2030   __match_char_collate(const __match_char_collate&)            = delete;
2031   __match_char_collate& operator=(const __match_char_collate&) = delete;
2032 
2033   _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
2034 };
2035 
2036 template <class _CharT, class _Traits>
2037 void __match_char_collate<_CharT, _Traits>::__exec(__state& __s) const {
2038   if (__s.__current_ != __s.__last_ && __traits_.translate(*__s.__current_) == __c_) {
2039     __s.__do_ = __state::__accept_and_consume;
2040     ++__s.__current_;
2041     __s.__node_ = this->first();
2042   } else {
2043     __s.__do_   = __state::__reject;
2044     __s.__node_ = nullptr;
2045   }
2046 }
2047 
2048 // __bracket_expression
2049 
2050 template <class _CharT, class _Traits>
2051 class __bracket_expression : public __owns_one_state<_CharT> {
2052   typedef __owns_one_state<_CharT> base;
2053   typedef typename _Traits::string_type string_type;
2054 
2055   _Traits __traits_;
2056   vector<_CharT> __chars_;
2057   vector<_CharT> __neg_chars_;
2058   vector<pair<string_type, string_type> > __ranges_;
2059   vector<pair<_CharT, _CharT> > __digraphs_;
2060   vector<string_type> __equivalences_;
2061   typename regex_traits<_CharT>::char_class_type __mask_;
2062   typename regex_traits<_CharT>::char_class_type __neg_mask_;
2063   bool __negate_;
2064   bool __icase_;
2065   bool __collate_;
2066   bool __might_have_digraph_;
2067 
2068 public:
2069   typedef std::__state<_CharT> __state;
2070 
2071   _LIBCPP_HIDE_FROM_ABI
2072   __bracket_expression(const _Traits& __traits, __node<_CharT>* __s, bool __negate, bool __icase, bool __collate)
2073       : base(__s),
2074         __traits_(__traits),
2075         __mask_(),
2076         __neg_mask_(),
2077         __negate_(__negate),
2078         __icase_(__icase),
2079         __collate_(__collate),
2080         __might_have_digraph_(__traits_.getloc().name() != "C") {}
2081 
2082   __bracket_expression(const __bracket_expression&)            = delete;
2083   __bracket_expression& operator=(const __bracket_expression&) = delete;
2084 
2085   _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
2086 
2087   _LIBCPP_HIDE_FROM_ABI bool __negated() const { return __negate_; }
2088 
2089   _LIBCPP_HIDE_FROM_ABI void __add_char(_CharT __c) {
2090     if (__icase_)
2091       __chars_.push_back(__traits_.translate_nocase(__c));
2092     else if (__collate_)
2093       __chars_.push_back(__traits_.translate(__c));
2094     else
2095       __chars_.push_back(__c);
2096   }
2097   _LIBCPP_HIDE_FROM_ABI void __add_neg_char(_CharT __c) {
2098     if (__icase_)
2099       __neg_chars_.push_back(__traits_.translate_nocase(__c));
2100     else if (__collate_)
2101       __neg_chars_.push_back(__traits_.translate(__c));
2102     else
2103       __neg_chars_.push_back(__c);
2104   }
2105   _LIBCPP_HIDE_FROM_ABI void __add_range(string_type __b, string_type __e) {
2106     if (__collate_) {
2107       if (__icase_) {
2108         for (size_t __i = 0; __i < __b.size(); ++__i)
2109           __b[__i] = __traits_.translate_nocase(__b[__i]);
2110         for (size_t __i = 0; __i < __e.size(); ++__i)
2111           __e[__i] = __traits_.translate_nocase(__e[__i]);
2112       } else {
2113         for (size_t __i = 0; __i < __b.size(); ++__i)
2114           __b[__i] = __traits_.translate(__b[__i]);
2115         for (size_t __i = 0; __i < __e.size(); ++__i)
2116           __e[__i] = __traits_.translate(__e[__i]);
2117       }
2118       __ranges_.push_back(
2119           std::make_pair(__traits_.transform(__b.begin(), __b.end()), __traits_.transform(__e.begin(), __e.end())));
2120     } else {
2121       if (__b.size() != 1 || __e.size() != 1)
2122         __throw_regex_error<regex_constants::error_range>();
2123       if (__icase_) {
2124         __b[0] = __traits_.translate_nocase(__b[0]);
2125         __e[0] = __traits_.translate_nocase(__e[0]);
2126       }
2127       __ranges_.push_back(std::make_pair(std::move(__b), std::move(__e)));
2128     }
2129   }
2130   _LIBCPP_HIDE_FROM_ABI void __add_digraph(_CharT __c1, _CharT __c2) {
2131     if (__icase_)
2132       __digraphs_.push_back(std::make_pair(__traits_.translate_nocase(__c1), __traits_.translate_nocase(__c2)));
2133     else if (__collate_)
2134       __digraphs_.push_back(std::make_pair(__traits_.translate(__c1), __traits_.translate(__c2)));
2135     else
2136       __digraphs_.push_back(std::make_pair(__c1, __c2));
2137   }
2138   _LIBCPP_HIDE_FROM_ABI void __add_equivalence(const string_type& __s) { __equivalences_.push_back(__s); }
2139   _LIBCPP_HIDE_FROM_ABI void __add_class(typename regex_traits<_CharT>::char_class_type __mask) { __mask_ |= __mask; }
2140   _LIBCPP_HIDE_FROM_ABI void __add_neg_class(typename regex_traits<_CharT>::char_class_type __mask) {
2141     __neg_mask_ |= __mask;
2142   }
2143 };
2144 
2145 template <class _CharT, class _Traits>
2146 void __bracket_expression<_CharT, _Traits>::__exec(__state& __s) const {
2147   bool __found        = false;
2148   unsigned __consumed = 0;
2149   if (__s.__current_ != __s.__last_) {
2150     ++__consumed;
2151     if (__might_have_digraph_) {
2152       const _CharT* __next = std::next(__s.__current_);
2153       if (__next != __s.__last_) {
2154         pair<_CharT, _CharT> __ch2(*__s.__current_, *__next);
2155         if (__icase_) {
2156           __ch2.first  = __traits_.translate_nocase(__ch2.first);
2157           __ch2.second = __traits_.translate_nocase(__ch2.second);
2158         } else if (__collate_) {
2159           __ch2.first  = __traits_.translate(__ch2.first);
2160           __ch2.second = __traits_.translate(__ch2.second);
2161         }
2162         if (!__traits_.lookup_collatename(&__ch2.first, &__ch2.first + 2).empty()) {
2163           // __ch2 is a digraph in this locale
2164           ++__consumed;
2165           for (size_t __i = 0; __i < __digraphs_.size(); ++__i) {
2166             if (__ch2 == __digraphs_[__i]) {
2167               __found = true;
2168               goto __exit;
2169             }
2170           }
2171           if (__collate_ && !__ranges_.empty()) {
2172             string_type __s2 = __traits_.transform(&__ch2.first, &__ch2.first + 2);
2173             for (size_t __i = 0; __i < __ranges_.size(); ++__i) {
2174               if (__ranges_[__i].first <= __s2 && __s2 <= __ranges_[__i].second) {
2175                 __found = true;
2176                 goto __exit;
2177               }
2178             }
2179           }
2180           if (!__equivalences_.empty()) {
2181             string_type __s2 = __traits_.transform_primary(&__ch2.first, &__ch2.first + 2);
2182             for (size_t __i = 0; __i < __equivalences_.size(); ++__i) {
2183               if (__s2 == __equivalences_[__i]) {
2184                 __found = true;
2185                 goto __exit;
2186               }
2187             }
2188           }
2189           if (__traits_.isctype(__ch2.first, __mask_) && __traits_.isctype(__ch2.second, __mask_)) {
2190             __found = true;
2191             goto __exit;
2192           }
2193           if (!__traits_.isctype(__ch2.first, __neg_mask_) && !__traits_.isctype(__ch2.second, __neg_mask_)) {
2194             __found = true;
2195             goto __exit;
2196           }
2197           goto __exit;
2198         }
2199       }
2200     }
2201     // test *__s.__current_ as not a digraph
2202     _CharT __ch = *__s.__current_;
2203     if (__icase_)
2204       __ch = __traits_.translate_nocase(__ch);
2205     else if (__collate_)
2206       __ch = __traits_.translate(__ch);
2207     for (size_t __i = 0; __i < __chars_.size(); ++__i) {
2208       if (__ch == __chars_[__i]) {
2209         __found = true;
2210         goto __exit;
2211       }
2212     }
2213     // When there's at least one of __neg_chars_ and __neg_mask_, the set
2214     // of "__found" chars is
2215     //   union(complement(union(__neg_chars_, __neg_mask_)),
2216     //         other cases...)
2217     //
2218     // It doesn't make sense to check this when there are no __neg_chars_
2219     // and no __neg_mask_.
2220     if (!(__neg_mask_ == 0 && __neg_chars_.empty())) {
2221       const bool __in_neg_mask  = __traits_.isctype(__ch, __neg_mask_);
2222       const bool __in_neg_chars = std::find(__neg_chars_.begin(), __neg_chars_.end(), __ch) != __neg_chars_.end();
2223       if (!(__in_neg_mask || __in_neg_chars)) {
2224         __found = true;
2225         goto __exit;
2226       }
2227     }
2228     if (!__ranges_.empty()) {
2229       string_type __s2 = __collate_ ? __traits_.transform(&__ch, &__ch + 1) : string_type(1, __ch);
2230       for (size_t __i = 0; __i < __ranges_.size(); ++__i) {
2231         if (__ranges_[__i].first <= __s2 && __s2 <= __ranges_[__i].second) {
2232           __found = true;
2233           goto __exit;
2234         }
2235       }
2236     }
2237     if (!__equivalences_.empty()) {
2238       string_type __s2 = __traits_.transform_primary(&__ch, &__ch + 1);
2239       for (size_t __i = 0; __i < __equivalences_.size(); ++__i) {
2240         if (__s2 == __equivalences_[__i]) {
2241           __found = true;
2242           goto __exit;
2243         }
2244       }
2245     }
2246     if (__traits_.isctype(__ch, __mask_)) {
2247       __found = true;
2248       goto __exit;
2249     }
2250   } else
2251     __found = __negate_; // force reject
2252 __exit:
2253   if (__found != __negate_) {
2254     __s.__do_ = __state::__accept_and_consume;
2255     __s.__current_ += __consumed;
2256     __s.__node_ = this->first();
2257   } else {
2258     __s.__do_   = __state::__reject;
2259     __s.__node_ = nullptr;
2260   }
2261 }
2262 
2263 template <class _CharT, class _Traits>
2264 class __lookahead;
2265 
2266 template <class _CharT, class _Traits = regex_traits<_CharT> >
2267 class _LIBCPP_TEMPLATE_VIS basic_regex;
2268 
2269 typedef basic_regex<char> regex;
2270 #    if _LIBCPP_HAS_WIDE_CHARACTERS
2271 typedef basic_regex<wchar_t> wregex;
2272 #    endif
2273 
2274 template <class _CharT, class _Traits>
2275 class _LIBCPP_TEMPLATE_VIS _LIBCPP_PREFERRED_NAME(regex) _LIBCPP_IF_WIDE_CHARACTERS(_LIBCPP_PREFERRED_NAME(wregex))
2276     basic_regex {
2277 public:
2278   // types:
2279   typedef _CharT value_type;
2280   typedef _Traits traits_type;
2281   typedef typename _Traits::string_type string_type;
2282   typedef regex_constants::syntax_option_type flag_type;
2283   typedef typename _Traits::locale_type locale_type;
2284 
2285 private:
2286   _Traits __traits_;
2287   flag_type __flags_;
2288   unsigned __marked_count_;
2289   unsigned __loop_count_;
2290   int __open_count_;
2291   shared_ptr<__empty_state<_CharT> > __start_;
2292   __owns_one_state<_CharT>* __end_;
2293 
2294   typedef std::__state<_CharT> __state;
2295   typedef std::__node<_CharT> __node;
2296 
2297 public:
2298   // constants:
2299   static const regex_constants::syntax_option_type icase      = regex_constants::icase;
2300   static const regex_constants::syntax_option_type nosubs     = regex_constants::nosubs;
2301   static const regex_constants::syntax_option_type optimize   = regex_constants::optimize;
2302   static const regex_constants::syntax_option_type collate    = regex_constants::collate;
2303   static const regex_constants::syntax_option_type ECMAScript = regex_constants::ECMAScript;
2304   static const regex_constants::syntax_option_type basic      = regex_constants::basic;
2305   static const regex_constants::syntax_option_type extended   = regex_constants::extended;
2306   static const regex_constants::syntax_option_type awk        = regex_constants::awk;
2307   static const regex_constants::syntax_option_type grep       = regex_constants::grep;
2308   static const regex_constants::syntax_option_type egrep      = regex_constants::egrep;
2309   static const regex_constants::syntax_option_type multiline  = regex_constants::multiline;
2310 
2311   // construct/copy/destroy:
2312   _LIBCPP_HIDE_FROM_ABI basic_regex()
2313       : __flags_(regex_constants::ECMAScript),
2314         __marked_count_(0),
2315         __loop_count_(0),
2316         __open_count_(0),
2317         __end_(nullptr) {}
2318   _LIBCPP_HIDE_FROM_ABI explicit basic_regex(const value_type* __p, flag_type __f = regex_constants::ECMAScript)
2319       : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0), __end_(nullptr) {
2320     __init(__p, __p + __traits_.length(__p));
2321   }
2322 
2323   _LIBCPP_HIDE_FROM_ABI basic_regex(const value_type* __p, size_t __len, flag_type __f = regex_constants::ECMAScript)
2324       : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0), __end_(nullptr) {
2325     __init(__p, __p + __len);
2326   }
2327 
2328   //     basic_regex(const basic_regex&) = default;
2329   //     basic_regex(basic_regex&&) = default;
2330   template <class _ST, class _SA>
2331   _LIBCPP_HIDE_FROM_ABI explicit basic_regex(const basic_string<value_type, _ST, _SA>& __p,
2332                                              flag_type __f = regex_constants::ECMAScript)
2333       : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0), __end_(nullptr) {
2334     __init(__p.begin(), __p.end());
2335   }
2336 
2337   template <class _ForwardIterator>
2338   _LIBCPP_HIDE_FROM_ABI
2339   basic_regex(_ForwardIterator __first, _ForwardIterator __last, flag_type __f = regex_constants::ECMAScript)
2340       : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0), __end_(nullptr) {
2341     __init(__first, __last);
2342   }
2343 #    ifndef _LIBCPP_CXX03_LANG
2344   _LIBCPP_HIDE_FROM_ABI basic_regex(initializer_list<value_type> __il, flag_type __f = regex_constants::ECMAScript)
2345       : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0), __end_(nullptr) {
2346     __init(__il.begin(), __il.end());
2347   }
2348 #    endif // _LIBCPP_CXX03_LANG
2349 
2350   //    ~basic_regex() = default;
2351 
2352   //     basic_regex& operator=(const basic_regex&) = default;
2353   //     basic_regex& operator=(basic_regex&&) = default;
2354   _LIBCPP_HIDE_FROM_ABI basic_regex& operator=(const value_type* __p) { return assign(__p); }
2355 #    ifndef _LIBCPP_CXX03_LANG
2356   _LIBCPP_HIDE_FROM_ABI basic_regex& operator=(initializer_list<value_type> __il) { return assign(__il); }
2357 #    endif // _LIBCPP_CXX03_LANG
2358   template <class _ST, class _SA>
2359   _LIBCPP_HIDE_FROM_ABI basic_regex& operator=(const basic_string<value_type, _ST, _SA>& __p) {
2360     return assign(__p);
2361   }
2362 
2363   // assign:
2364   _LIBCPP_HIDE_FROM_ABI basic_regex& assign(const basic_regex& __that) { return *this = __that; }
2365 #    ifndef _LIBCPP_CXX03_LANG
2366   _LIBCPP_HIDE_FROM_ABI basic_regex& assign(basic_regex&& __that) _NOEXCEPT { return *this = std::move(__that); }
2367 #    endif
2368   _LIBCPP_HIDE_FROM_ABI basic_regex& assign(const value_type* __p, flag_type __f = regex_constants::ECMAScript) {
2369     return assign(__p, __p + __traits_.length(__p), __f);
2370   }
2371   _LIBCPP_HIDE_FROM_ABI basic_regex&
2372   assign(const value_type* __p, size_t __len, flag_type __f = regex_constants::ECMAScript) {
2373     return assign(__p, __p + __len, __f);
2374   }
2375   template <class _ST, class _SA>
2376   _LIBCPP_HIDE_FROM_ABI basic_regex&
2377   assign(const basic_string<value_type, _ST, _SA>& __s, flag_type __f = regex_constants::ECMAScript) {
2378     return assign(__s.begin(), __s.end(), __f);
2379   }
2380 
2381   template <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value, int> = 0>
2382   _LIBCPP_HIDE_FROM_ABI basic_regex&
2383   assign(_InputIterator __first, _InputIterator __last, flag_type __f = regex_constants::ECMAScript) {
2384     basic_string<_CharT> __t(__first, __last);
2385     return assign(__t.begin(), __t.end(), __f);
2386   }
2387 
2388 private:
2389   _LIBCPP_HIDE_FROM_ABI void __member_init(flag_type __f) {
2390     __flags_        = __f;
2391     __marked_count_ = 0;
2392     __loop_count_   = 0;
2393     __open_count_   = 0;
2394     __end_          = nullptr;
2395   }
2396 
2397 public:
2398   template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> = 0>
2399   _LIBCPP_HIDE_FROM_ABI basic_regex&
2400   assign(_ForwardIterator __first, _ForwardIterator __last, flag_type __f = regex_constants::ECMAScript) {
2401     return assign(basic_regex(__first, __last, __f));
2402   }
2403 
2404 #    ifndef _LIBCPP_CXX03_LANG
2405 
2406   _LIBCPP_HIDE_FROM_ABI basic_regex&
2407   assign(initializer_list<value_type> __il, flag_type __f = regex_constants::ECMAScript) {
2408     return assign(__il.begin(), __il.end(), __f);
2409   }
2410 
2411 #    endif // _LIBCPP_CXX03_LANG
2412 
2413   // const operations:
2414   _LIBCPP_HIDE_FROM_ABI unsigned mark_count() const { return __marked_count_; }
2415   _LIBCPP_HIDE_FROM_ABI flag_type flags() const { return __flags_; }
2416 
2417   // locale:
2418   _LIBCPP_HIDE_FROM_ABI locale_type imbue(locale_type __loc) {
2419     __member_init(ECMAScript);
2420     __start_.reset();
2421     return __traits_.imbue(__loc);
2422   }
2423   _LIBCPP_HIDE_FROM_ABI locale_type getloc() const { return __traits_.getloc(); }
2424 
2425   // swap:
2426   void swap(basic_regex& __r);
2427 
2428 private:
2429   _LIBCPP_HIDE_FROM_ABI unsigned __loop_count() const { return __loop_count_; }
2430 
2431   _LIBCPP_HIDE_FROM_ABI bool __use_multiline() const {
2432     return __get_grammar(__flags_) == ECMAScript && (__flags_ & multiline);
2433   }
2434 
2435   template <class _ForwardIterator>
2436   void __init(_ForwardIterator __first, _ForwardIterator __last);
2437   template <class _ForwardIterator>
2438   _ForwardIterator __parse(_ForwardIterator __first, _ForwardIterator __last);
2439   template <class _ForwardIterator>
2440   _ForwardIterator __parse_basic_reg_exp(_ForwardIterator __first, _ForwardIterator __last);
2441   template <class _ForwardIterator>
2442   _ForwardIterator __parse_RE_expression(_ForwardIterator __first, _ForwardIterator __last);
2443   template <class _ForwardIterator>
2444   _ForwardIterator __parse_simple_RE(_ForwardIterator __first, _ForwardIterator __last);
2445   template <class _ForwardIterator>
2446   _ForwardIterator __parse_nondupl_RE(_ForwardIterator __first, _ForwardIterator __last);
2447   template <class _ForwardIterator>
2448   _ForwardIterator __parse_one_char_or_coll_elem_RE(_ForwardIterator __first, _ForwardIterator __last);
2449   template <class _ForwardIterator>
2450   _ForwardIterator __parse_Back_open_paren(_ForwardIterator __first, _ForwardIterator __last);
2451   template <class _ForwardIterator>
2452   _ForwardIterator __parse_Back_close_paren(_ForwardIterator __first, _ForwardIterator __last);
2453   template <class _ForwardIterator>
2454   _ForwardIterator __parse_Back_open_brace(_ForwardIterator __first, _ForwardIterator __last);
2455   template <class _ForwardIterator>
2456   _ForwardIterator __parse_Back_close_brace(_ForwardIterator __first, _ForwardIterator __last);
2457   template <class _ForwardIterator>
2458   _ForwardIterator __parse_BACKREF(_ForwardIterator __first, _ForwardIterator __last);
2459   template <class _ForwardIterator>
2460   _ForwardIterator __parse_ORD_CHAR(_ForwardIterator __first, _ForwardIterator __last);
2461   template <class _ForwardIterator>
2462   _ForwardIterator __parse_QUOTED_CHAR(_ForwardIterator __first, _ForwardIterator __last);
2463   template <class _ForwardIterator>
2464   _ForwardIterator __parse_RE_dupl_symbol(
2465       _ForwardIterator __first,
2466       _ForwardIterator __last,
2467       __owns_one_state<_CharT>* __s,
2468       unsigned __mexp_begin,
2469       unsigned __mexp_end);
2470   template <class _ForwardIterator>
2471   _ForwardIterator __parse_ERE_dupl_symbol(
2472       _ForwardIterator __first,
2473       _ForwardIterator __last,
2474       __owns_one_state<_CharT>* __s,
2475       unsigned __mexp_begin,
2476       unsigned __mexp_end);
2477   template <class _ForwardIterator>
2478   _ForwardIterator __parse_bracket_expression(_ForwardIterator __first, _ForwardIterator __last);
2479   template <class _ForwardIterator>
2480   _ForwardIterator
2481   __parse_follow_list(_ForwardIterator __first, _ForwardIterator __last, __bracket_expression<_CharT, _Traits>* __ml);
2482   template <class _ForwardIterator>
2483   _ForwardIterator __parse_expression_term(
2484       _ForwardIterator __first, _ForwardIterator __last, __bracket_expression<_CharT, _Traits>* __ml);
2485   template <class _ForwardIterator>
2486   _ForwardIterator __parse_equivalence_class(
2487       _ForwardIterator __first, _ForwardIterator __last, __bracket_expression<_CharT, _Traits>* __ml);
2488   template <class _ForwardIterator>
2489   _ForwardIterator __parse_character_class(
2490       _ForwardIterator __first, _ForwardIterator __last, __bracket_expression<_CharT, _Traits>* __ml);
2491   template <class _ForwardIterator>
2492   _ForwardIterator
2493   __parse_collating_symbol(_ForwardIterator __first, _ForwardIterator __last, basic_string<_CharT>& __col_sym);
2494   template <class _ForwardIterator>
2495   _ForwardIterator __parse_DUP_COUNT(_ForwardIterator __first, _ForwardIterator __last, int& __c);
2496   template <class _ForwardIterator>
2497   _ForwardIterator __parse_extended_reg_exp(_ForwardIterator __first, _ForwardIterator __last);
2498   template <class _ForwardIterator>
2499   _ForwardIterator __parse_ERE_branch(_ForwardIterator __first, _ForwardIterator __last);
2500   template <class _ForwardIterator>
2501   _ForwardIterator __parse_ERE_expression(_ForwardIterator __first, _ForwardIterator __last);
2502   template <class _ForwardIterator>
2503   _ForwardIterator __parse_one_char_or_coll_elem_ERE(_ForwardIterator __first, _ForwardIterator __last);
2504   template <class _ForwardIterator>
2505   _ForwardIterator __parse_ORD_CHAR_ERE(_ForwardIterator __first, _ForwardIterator __last);
2506   template <class _ForwardIterator>
2507   _ForwardIterator __parse_QUOTED_CHAR_ERE(_ForwardIterator __first, _ForwardIterator __last);
2508   template <class _ForwardIterator>
2509   _ForwardIterator __parse_ecma_exp(_ForwardIterator __first, _ForwardIterator __last);
2510   template <class _ForwardIterator>
2511   _ForwardIterator __parse_alternative(_ForwardIterator __first, _ForwardIterator __last);
2512   template <class _ForwardIterator>
2513   _ForwardIterator __parse_term(_ForwardIterator __first, _ForwardIterator __last);
2514   template <class _ForwardIterator>
2515   _ForwardIterator __parse_assertion(_ForwardIterator __first, _ForwardIterator __last);
2516   template <class _ForwardIterator>
2517   _ForwardIterator __parse_atom(_ForwardIterator __first, _ForwardIterator __last);
2518   template <class _ForwardIterator>
2519   _ForwardIterator __parse_atom_escape(_ForwardIterator __first, _ForwardIterator __last);
2520   template <class _ForwardIterator>
2521   _ForwardIterator __parse_decimal_escape(_ForwardIterator __first, _ForwardIterator __last);
2522   template <class _ForwardIterator>
2523   _ForwardIterator __parse_character_class_escape(_ForwardIterator __first, _ForwardIterator __last);
2524   template <class _ForwardIterator>
2525   _ForwardIterator
2526   __parse_character_escape(_ForwardIterator __first, _ForwardIterator __last, basic_string<_CharT>* __str = nullptr);
2527   template <class _ForwardIterator>
2528   _ForwardIterator __parse_pattern_character(_ForwardIterator __first, _ForwardIterator __last);
2529   template <class _ForwardIterator>
2530   _ForwardIterator __parse_grep(_ForwardIterator __first, _ForwardIterator __last);
2531   template <class _ForwardIterator>
2532   _ForwardIterator __parse_egrep(_ForwardIterator __first, _ForwardIterator __last);
2533   template <class _ForwardIterator>
2534   _ForwardIterator __parse_class_escape(
2535       _ForwardIterator __first,
2536       _ForwardIterator __last,
2537       basic_string<_CharT>& __str,
2538       __bracket_expression<_CharT, _Traits>* __ml);
2539   template <class _ForwardIterator>
2540   _ForwardIterator
2541   __parse_awk_escape(_ForwardIterator __first, _ForwardIterator __last, basic_string<_CharT>* __str = nullptr);
2542 
2543   bool __test_back_ref(_CharT);
2544 
2545   _LIBCPP_HIDE_FROM_ABI void __push_l_anchor();
2546   void __push_r_anchor();
2547   void __push_match_any();
2548   void __push_match_any_but_newline();
2549   _LIBCPP_HIDE_FROM_ABI void __push_greedy_inf_repeat(
2550       size_t __min, __owns_one_state<_CharT>* __s, unsigned __mexp_begin = 0, unsigned __mexp_end = 0) {
2551     __push_loop(__min, numeric_limits<size_t>::max(), __s, __mexp_begin, __mexp_end);
2552   }
2553   _LIBCPP_HIDE_FROM_ABI void __push_nongreedy_inf_repeat(
2554       size_t __min, __owns_one_state<_CharT>* __s, unsigned __mexp_begin = 0, unsigned __mexp_end = 0) {
2555     __push_loop(__min, numeric_limits<size_t>::max(), __s, __mexp_begin, __mexp_end, false);
2556   }
2557   void __push_loop(size_t __min,
2558                    size_t __max,
2559                    __owns_one_state<_CharT>* __s,
2560                    size_t __mexp_begin = 0,
2561                    size_t __mexp_end   = 0,
2562                    bool __greedy       = true);
2563   __bracket_expression<_CharT, _Traits>* __start_matching_list(bool __negate);
2564   void __push_char(value_type __c);
2565   void __push_back_ref(int __i);
2566   void __push_alternation(__owns_one_state<_CharT>* __sa, __owns_one_state<_CharT>* __sb);
2567   void __push_begin_marked_subexpression();
2568   void __push_end_marked_subexpression(unsigned);
2569   void __push_empty();
2570   void __push_word_boundary(bool);
2571   void __push_lookahead(const basic_regex&, bool, unsigned);
2572 
2573   template <class _Allocator>
2574   bool __search(const _CharT* __first,
2575                 const _CharT* __last,
2576                 match_results<const _CharT*, _Allocator>& __m,
2577                 regex_constants::match_flag_type __flags) const;
2578 
2579   template <class _Allocator>
2580   bool __match_at_start(const _CharT* __first,
2581                         const _CharT* __last,
2582                         match_results<const _CharT*, _Allocator>& __m,
2583                         regex_constants::match_flag_type __flags,
2584                         bool) const;
2585   template <class _Allocator>
2586   bool __match_at_start_ecma(
2587       const _CharT* __first,
2588       const _CharT* __last,
2589       match_results<const _CharT*, _Allocator>& __m,
2590       regex_constants::match_flag_type __flags,
2591       bool) const;
2592   template <class _Allocator>
2593   bool __match_at_start_posix_nosubs(
2594       const _CharT* __first,
2595       const _CharT* __last,
2596       match_results<const _CharT*, _Allocator>& __m,
2597       regex_constants::match_flag_type __flags,
2598       bool) const;
2599   template <class _Allocator>
2600   bool __match_at_start_posix_subs(
2601       const _CharT* __first,
2602       const _CharT* __last,
2603       match_results<const _CharT*, _Allocator>& __m,
2604       regex_constants::match_flag_type __flags,
2605       bool) const;
2606 
2607   template <class _Bp, class _Ap, class _Cp, class _Tp>
2608   friend bool
2609   regex_search(_Bp, _Bp, match_results<_Bp, _Ap>&, const basic_regex<_Cp, _Tp>&, regex_constants::match_flag_type);
2610 
2611   template <class _Ap, class _Cp, class _Tp>
2612   friend bool
2613   regex_search(const _Cp*,
2614                const _Cp*,
2615                match_results<const _Cp*, _Ap>&,
2616                const basic_regex<_Cp, _Tp>&,
2617                regex_constants::match_flag_type);
2618 
2619   template <class _Bp, class _Cp, class _Tp>
2620   friend bool regex_search(_Bp, _Bp, const basic_regex<_Cp, _Tp>&, regex_constants::match_flag_type);
2621 
2622   template <class _Cp, class _Tp>
2623   friend bool regex_search(const _Cp*, const _Cp*, const basic_regex<_Cp, _Tp>&, regex_constants::match_flag_type);
2624 
2625   template <class _Cp, class _Ap, class _Tp>
2626   friend bool regex_search(
2627       const _Cp*, match_results<const _Cp*, _Ap>&, const basic_regex<_Cp, _Tp>&, regex_constants::match_flag_type);
2628 
2629   template <class _ST, class _SA, class _Cp, class _Tp>
2630   friend bool regex_search(const basic_string<_Cp, _ST, _SA>& __s,
2631                            const basic_regex<_Cp, _Tp>& __e,
2632                            regex_constants::match_flag_type __flags);
2633 
2634   template <class _ST, class _SA, class _Ap, class _Cp, class _Tp>
2635   friend bool regex_search(const basic_string<_Cp, _ST, _SA>& __s,
2636                            match_results<typename basic_string<_Cp, _ST, _SA>::const_iterator, _Ap>&,
2637                            const basic_regex<_Cp, _Tp>& __e,
2638                            regex_constants::match_flag_type __flags);
2639 
2640   template <class _Iter, class _Ap, class _Cp, class _Tp>
2641   friend bool
2642   regex_search(__wrap_iter<_Iter> __first,
2643                __wrap_iter<_Iter> __last,
2644                match_results<__wrap_iter<_Iter>, _Ap>& __m,
2645                const basic_regex<_Cp, _Tp>& __e,
2646                regex_constants::match_flag_type __flags);
2647 
2648   template <class, class>
2649   friend class __lookahead;
2650 };
2651 
2652 #    if _LIBCPP_STD_VER >= 17
2653 template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> = 0>
2654 basic_regex(_ForwardIterator, _ForwardIterator, regex_constants::syntax_option_type = regex_constants::ECMAScript)
2655     -> basic_regex<typename iterator_traits<_ForwardIterator>::value_type>;
2656 #    endif
2657 
2658 template <class _CharT, class _Traits>
2659 const regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::icase;
2660 template <class _CharT, class _Traits>
2661 const regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::nosubs;
2662 template <class _CharT, class _Traits>
2663 const regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::optimize;
2664 template <class _CharT, class _Traits>
2665 const regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::collate;
2666 template <class _CharT, class _Traits>
2667 const regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::ECMAScript;
2668 template <class _CharT, class _Traits>
2669 const regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::basic;
2670 template <class _CharT, class _Traits>
2671 const regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::extended;
2672 template <class _CharT, class _Traits>
2673 const regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::awk;
2674 template <class _CharT, class _Traits>
2675 const regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::grep;
2676 template <class _CharT, class _Traits>
2677 const regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::egrep;
2678 
2679 template <class _CharT, class _Traits>
2680 void basic_regex<_CharT, _Traits>::swap(basic_regex& __r) {
2681   using std::swap;
2682   swap(__traits_, __r.__traits_);
2683   swap(__flags_, __r.__flags_);
2684   swap(__marked_count_, __r.__marked_count_);
2685   swap(__loop_count_, __r.__loop_count_);
2686   swap(__open_count_, __r.__open_count_);
2687   swap(__start_, __r.__start_);
2688   swap(__end_, __r.__end_);
2689 }
2690 
2691 template <class _CharT, class _Traits>
2692 inline _LIBCPP_HIDE_FROM_ABI void swap(basic_regex<_CharT, _Traits>& __x, basic_regex<_CharT, _Traits>& __y) {
2693   return __x.swap(__y);
2694 }
2695 
2696 // __lookahead
2697 
2698 template <class _CharT, class _Traits>
2699 class __lookahead : public __owns_one_state<_CharT> {
2700   typedef __owns_one_state<_CharT> base;
2701 
2702   basic_regex<_CharT, _Traits> __exp_;
2703   unsigned __mexp_;
2704   bool __invert_;
2705 
2706 public:
2707   typedef std::__state<_CharT> __state;
2708 
2709   _LIBCPP_HIDE_FROM_ABI
2710   __lookahead(const basic_regex<_CharT, _Traits>& __exp, bool __invert, __node<_CharT>* __s, unsigned __mexp)
2711       : base(__s), __exp_(__exp), __mexp_(__mexp), __invert_(__invert) {}
2712 
2713   __lookahead(const __lookahead&)            = delete;
2714   __lookahead& operator=(const __lookahead&) = delete;
2715 
2716   _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
2717 };
2718 
2719 template <class _CharT, class _Traits>
2720 void __lookahead<_CharT, _Traits>::__exec(__state& __s) const {
2721   match_results<const _CharT*> __m;
2722   __m.__init(1 + __exp_.mark_count(), __s.__current_, __s.__last_);
2723   bool __matched = __exp_.__match_at_start_ecma(
2724       __s.__current_,
2725       __s.__last_,
2726       __m,
2727       (__s.__flags_ | regex_constants::match_continuous) & ~regex_constants::__full_match,
2728       __s.__at_first_ && __s.__current_ == __s.__first_);
2729   if (__matched != __invert_) {
2730     __s.__do_   = __state::__accept_but_not_consume;
2731     __s.__node_ = this->first();
2732     for (unsigned __i = 1; __i < __m.size(); ++__i) {
2733       __s.__sub_matches_[__mexp_ + __i - 1] = __m.__matches_[__i];
2734     }
2735   } else {
2736     __s.__do_   = __state::__reject;
2737     __s.__node_ = nullptr;
2738   }
2739 }
2740 
2741 template <class _CharT, class _Traits>
2742 template <class _ForwardIterator>
2743 void basic_regex<_CharT, _Traits>::__init(_ForwardIterator __first, _ForwardIterator __last) {
2744   if (__get_grammar(__flags_) == 0)
2745     __flags_ |= regex_constants::ECMAScript;
2746   _ForwardIterator __temp = __parse(__first, __last);
2747   if (__temp != __last)
2748     __throw_regex_error<regex_constants::__re_err_parse>();
2749 }
2750 
2751 template <class _CharT, class _Traits>
2752 template <class _ForwardIterator>
2753 _ForwardIterator basic_regex<_CharT, _Traits>::__parse(_ForwardIterator __first, _ForwardIterator __last) {
2754   {
2755     unique_ptr<__node> __h(new __end_state<_CharT>);
2756     __start_.reset(new __empty_state<_CharT>(__h.get()));
2757     __h.release();
2758     __end_ = __start_.get();
2759   }
2760   switch (__get_grammar(__flags_)) {
2761   case ECMAScript:
2762     __first = __parse_ecma_exp(__first, __last);
2763     break;
2764   case basic:
2765     __first = __parse_basic_reg_exp(__first, __last);
2766     break;
2767   case extended:
2768   case awk:
2769     __first = __parse_extended_reg_exp(__first, __last);
2770     break;
2771   case grep:
2772     __first = __parse_grep(__first, __last);
2773     break;
2774   case egrep:
2775     __first = __parse_egrep(__first, __last);
2776     break;
2777   default:
2778     __throw_regex_error<regex_constants::__re_err_grammar>();
2779   }
2780   return __first;
2781 }
2782 
2783 template <class _CharT, class _Traits>
2784 template <class _ForwardIterator>
2785 _ForwardIterator
2786 basic_regex<_CharT, _Traits>::__parse_basic_reg_exp(_ForwardIterator __first, _ForwardIterator __last) {
2787   if (__first != __last) {
2788     if (*__first == '^') {
2789       __push_l_anchor();
2790       ++__first;
2791     }
2792     if (__first != __last) {
2793       __first = __parse_RE_expression(__first, __last);
2794       if (__first != __last) {
2795         _ForwardIterator __temp = std::next(__first);
2796         if (__temp == __last && *__first == '$') {
2797           __push_r_anchor();
2798           ++__first;
2799         }
2800       }
2801     }
2802     if (__first != __last)
2803       __throw_regex_error<regex_constants::__re_err_empty>();
2804   }
2805   return __first;
2806 }
2807 
2808 template <class _CharT, class _Traits>
2809 template <class _ForwardIterator>
2810 _ForwardIterator
2811 basic_regex<_CharT, _Traits>::__parse_extended_reg_exp(_ForwardIterator __first, _ForwardIterator __last) {
2812   __owns_one_state<_CharT>* __sa = __end_;
2813   _ForwardIterator __temp        = __parse_ERE_branch(__first, __last);
2814   if (__temp == __first)
2815     __throw_regex_error<regex_constants::__re_err_empty>();
2816   __first = __temp;
2817   while (__first != __last && *__first == '|') {
2818     __owns_one_state<_CharT>* __sb = __end_;
2819     __temp                         = __parse_ERE_branch(++__first, __last);
2820     if (__temp == __first)
2821       __throw_regex_error<regex_constants::__re_err_empty>();
2822     __push_alternation(__sa, __sb);
2823     __first = __temp;
2824   }
2825   return __first;
2826 }
2827 
2828 template <class _CharT, class _Traits>
2829 template <class _ForwardIterator>
2830 _ForwardIterator basic_regex<_CharT, _Traits>::__parse_ERE_branch(_ForwardIterator __first, _ForwardIterator __last) {
2831   _ForwardIterator __temp = __parse_ERE_expression(__first, __last);
2832   if (__temp == __first)
2833     __throw_regex_error<regex_constants::__re_err_empty>();
2834   do {
2835     __first = __temp;
2836     __temp  = __parse_ERE_expression(__first, __last);
2837   } while (__temp != __first);
2838   return __first;
2839 }
2840 
2841 template <class _CharT, class _Traits>
2842 template <class _ForwardIterator>
2843 _ForwardIterator
2844 basic_regex<_CharT, _Traits>::__parse_ERE_expression(_ForwardIterator __first, _ForwardIterator __last) {
2845   __owns_one_state<_CharT>* __e = __end_;
2846   unsigned __mexp_begin         = __marked_count_;
2847   _ForwardIterator __temp       = __parse_one_char_or_coll_elem_ERE(__first, __last);
2848   if (__temp == __first && __temp != __last) {
2849     switch (*__temp) {
2850     case '^':
2851       __push_l_anchor();
2852       ++__temp;
2853       break;
2854     case '$':
2855       __push_r_anchor();
2856       ++__temp;
2857       break;
2858     case '(':
2859       __push_begin_marked_subexpression();
2860       unsigned __temp_count = __marked_count_;
2861       ++__open_count_;
2862       __temp = __parse_extended_reg_exp(++__temp, __last);
2863       if (__temp == __last || *__temp != ')')
2864         __throw_regex_error<regex_constants::error_paren>();
2865       __push_end_marked_subexpression(__temp_count);
2866       --__open_count_;
2867       ++__temp;
2868       break;
2869     }
2870   }
2871   if (__temp != __first)
2872     __temp = __parse_ERE_dupl_symbol(__temp, __last, __e, __mexp_begin + 1, __marked_count_ + 1);
2873   __first = __temp;
2874   return __first;
2875 }
2876 
2877 template <class _CharT, class _Traits>
2878 template <class _ForwardIterator>
2879 _ForwardIterator
2880 basic_regex<_CharT, _Traits>::__parse_RE_expression(_ForwardIterator __first, _ForwardIterator __last) {
2881   while (true) {
2882     _ForwardIterator __temp = __parse_simple_RE(__first, __last);
2883     if (__temp == __first)
2884       break;
2885     __first = __temp;
2886   }
2887   return __first;
2888 }
2889 
2890 template <class _CharT, class _Traits>
2891 template <class _ForwardIterator>
2892 _ForwardIterator basic_regex<_CharT, _Traits>::__parse_simple_RE(_ForwardIterator __first, _ForwardIterator __last) {
2893   if (__first != __last) {
2894     __owns_one_state<_CharT>* __e = __end_;
2895     unsigned __mexp_begin         = __marked_count_;
2896     _ForwardIterator __temp       = __parse_nondupl_RE(__first, __last);
2897     if (__temp != __first)
2898       __first = __parse_RE_dupl_symbol(__temp, __last, __e, __mexp_begin + 1, __marked_count_ + 1);
2899   }
2900   return __first;
2901 }
2902 
2903 template <class _CharT, class _Traits>
2904 template <class _ForwardIterator>
2905 _ForwardIterator basic_regex<_CharT, _Traits>::__parse_nondupl_RE(_ForwardIterator __first, _ForwardIterator __last) {
2906   _ForwardIterator __temp = __first;
2907   __first                 = __parse_one_char_or_coll_elem_RE(__first, __last);
2908   if (__temp == __first) {
2909     __temp = __parse_Back_open_paren(__first, __last);
2910     if (__temp != __first) {
2911       __push_begin_marked_subexpression();
2912       unsigned __temp_count = __marked_count_;
2913       __first               = __parse_RE_expression(__temp, __last);
2914       __temp                = __parse_Back_close_paren(__first, __last);
2915       if (__temp == __first)
2916         __throw_regex_error<regex_constants::error_paren>();
2917       __push_end_marked_subexpression(__temp_count);
2918       __first = __temp;
2919     } else
2920       __first = __parse_BACKREF(__first, __last);
2921   }
2922   return __first;
2923 }
2924 
2925 template <class _CharT, class _Traits>
2926 template <class _ForwardIterator>
2927 _ForwardIterator
2928 basic_regex<_CharT, _Traits>::__parse_one_char_or_coll_elem_RE(_ForwardIterator __first, _ForwardIterator __last) {
2929   _ForwardIterator __temp = __parse_ORD_CHAR(__first, __last);
2930   if (__temp == __first) {
2931     __temp = __parse_QUOTED_CHAR(__first, __last);
2932     if (__temp == __first) {
2933       if (__temp != __last && *__temp == '.') {
2934         __push_match_any();
2935         ++__temp;
2936       } else
2937         __temp = __parse_bracket_expression(__first, __last);
2938     }
2939   }
2940   __first = __temp;
2941   return __first;
2942 }
2943 
2944 template <class _CharT, class _Traits>
2945 template <class _ForwardIterator>
2946 _ForwardIterator
2947 basic_regex<_CharT, _Traits>::__parse_one_char_or_coll_elem_ERE(_ForwardIterator __first, _ForwardIterator __last) {
2948   _ForwardIterator __temp = __parse_ORD_CHAR_ERE(__first, __last);
2949   if (__temp == __first) {
2950     __temp = __parse_QUOTED_CHAR_ERE(__first, __last);
2951     if (__temp == __first) {
2952       if (__temp != __last && *__temp == '.') {
2953         __push_match_any();
2954         ++__temp;
2955       } else
2956         __temp = __parse_bracket_expression(__first, __last);
2957     }
2958   }
2959   __first = __temp;
2960   return __first;
2961 }
2962 
2963 template <class _CharT, class _Traits>
2964 template <class _ForwardIterator>
2965 _ForwardIterator
2966 basic_regex<_CharT, _Traits>::__parse_Back_open_paren(_ForwardIterator __first, _ForwardIterator __last) {
2967   if (__first != __last) {
2968     _ForwardIterator __temp = std::next(__first);
2969     if (__temp != __last) {
2970       if (*__first == '\\' && *__temp == '(')
2971         __first = ++__temp;
2972     }
2973   }
2974   return __first;
2975 }
2976 
2977 template <class _CharT, class _Traits>
2978 template <class _ForwardIterator>
2979 _ForwardIterator
2980 basic_regex<_CharT, _Traits>::__parse_Back_close_paren(_ForwardIterator __first, _ForwardIterator __last) {
2981   if (__first != __last) {
2982     _ForwardIterator __temp = std::next(__first);
2983     if (__temp != __last) {
2984       if (*__first == '\\' && *__temp == ')')
2985         __first = ++__temp;
2986     }
2987   }
2988   return __first;
2989 }
2990 
2991 template <class _CharT, class _Traits>
2992 template <class _ForwardIterator>
2993 _ForwardIterator
2994 basic_regex<_CharT, _Traits>::__parse_Back_open_brace(_ForwardIterator __first, _ForwardIterator __last) {
2995   if (__first != __last) {
2996     _ForwardIterator __temp = std::next(__first);
2997     if (__temp != __last) {
2998       if (*__first == '\\' && *__temp == '{')
2999         __first = ++__temp;
3000     }
3001   }
3002   return __first;
3003 }
3004 
3005 template <class _CharT, class _Traits>
3006 template <class _ForwardIterator>
3007 _ForwardIterator
3008 basic_regex<_CharT, _Traits>::__parse_Back_close_brace(_ForwardIterator __first, _ForwardIterator __last) {
3009   if (__first != __last) {
3010     _ForwardIterator __temp = std::next(__first);
3011     if (__temp != __last) {
3012       if (*__first == '\\' && *__temp == '}')
3013         __first = ++__temp;
3014     }
3015   }
3016   return __first;
3017 }
3018 
3019 template <class _CharT, class _Traits>
3020 template <class _ForwardIterator>
3021 _ForwardIterator basic_regex<_CharT, _Traits>::__parse_BACKREF(_ForwardIterator __first, _ForwardIterator __last) {
3022   if (__first != __last) {
3023     _ForwardIterator __temp = std::next(__first);
3024     if (__temp != __last && *__first == '\\' && __test_back_ref(*__temp))
3025       __first = ++__temp;
3026   }
3027   return __first;
3028 }
3029 
3030 template <class _CharT, class _Traits>
3031 template <class _ForwardIterator>
3032 _ForwardIterator basic_regex<_CharT, _Traits>::__parse_ORD_CHAR(_ForwardIterator __first, _ForwardIterator __last) {
3033   if (__first != __last) {
3034     _ForwardIterator __temp = std::next(__first);
3035     if (__temp == __last && *__first == '$')
3036       return __first;
3037     // Not called inside a bracket
3038     if (*__first == '.' || *__first == '\\' || *__first == '[')
3039       return __first;
3040     __push_char(*__first);
3041     ++__first;
3042   }
3043   return __first;
3044 }
3045 
3046 template <class _CharT, class _Traits>
3047 template <class _ForwardIterator>
3048 _ForwardIterator basic_regex<_CharT, _Traits>::__parse_ORD_CHAR_ERE(_ForwardIterator __first, _ForwardIterator __last) {
3049   if (__first != __last) {
3050     switch (*__first) {
3051     case '^':
3052     case '.':
3053     case '[':
3054     case '$':
3055     case '(':
3056     case '|':
3057     case '*':
3058     case '+':
3059     case '?':
3060     case '{':
3061     case '\\':
3062       break;
3063     case ')':
3064       if (__open_count_ == 0) {
3065         __push_char(*__first);
3066         ++__first;
3067       }
3068       break;
3069     default:
3070       __push_char(*__first);
3071       ++__first;
3072       break;
3073     }
3074   }
3075   return __first;
3076 }
3077 
3078 template <class _CharT, class _Traits>
3079 template <class _ForwardIterator>
3080 _ForwardIterator basic_regex<_CharT, _Traits>::__parse_QUOTED_CHAR(_ForwardIterator __first, _ForwardIterator __last) {
3081   if (__first != __last) {
3082     _ForwardIterator __temp = std::next(__first);
3083     if (__temp != __last) {
3084       if (*__first == '\\') {
3085         switch (*__temp) {
3086         case '^':
3087         case '.':
3088         case '*':
3089         case '[':
3090         case '$':
3091         case '\\':
3092           __push_char(*__temp);
3093           __first = ++__temp;
3094           break;
3095         }
3096       }
3097     }
3098   }
3099   return __first;
3100 }
3101 
3102 template <class _CharT, class _Traits>
3103 template <class _ForwardIterator>
3104 _ForwardIterator
3105 basic_regex<_CharT, _Traits>::__parse_QUOTED_CHAR_ERE(_ForwardIterator __first, _ForwardIterator __last) {
3106   if (__first != __last) {
3107     _ForwardIterator __temp = std::next(__first);
3108     if (__temp != __last) {
3109       if (*__first == '\\') {
3110         switch (*__temp) {
3111         case '^':
3112         case '.':
3113         case '*':
3114         case '[':
3115         case '$':
3116         case '\\':
3117         case '(':
3118         case ')':
3119         case '|':
3120         case '+':
3121         case '?':
3122         case '{':
3123         case '}':
3124           __push_char(*__temp);
3125           __first = ++__temp;
3126           break;
3127         default:
3128           if (__get_grammar(__flags_) == awk)
3129             __first = __parse_awk_escape(++__first, __last);
3130           else if (__test_back_ref(*__temp))
3131             __first = ++__temp;
3132           break;
3133         }
3134       }
3135     }
3136   }
3137   return __first;
3138 }
3139 
3140 template <class _CharT, class _Traits>
3141 template <class _ForwardIterator>
3142 _ForwardIterator basic_regex<_CharT, _Traits>::__parse_RE_dupl_symbol(
3143     _ForwardIterator __first,
3144     _ForwardIterator __last,
3145     __owns_one_state<_CharT>* __s,
3146     unsigned __mexp_begin,
3147     unsigned __mexp_end) {
3148   if (__first != __last) {
3149     if (*__first == '*') {
3150       __push_greedy_inf_repeat(0, __s, __mexp_begin, __mexp_end);
3151       ++__first;
3152     } else {
3153       _ForwardIterator __temp = __parse_Back_open_brace(__first, __last);
3154       if (__temp != __first) {
3155         int __min = 0;
3156         __first   = __temp;
3157         __temp    = __parse_DUP_COUNT(__first, __last, __min);
3158         if (__temp == __first)
3159           __throw_regex_error<regex_constants::error_badbrace>();
3160         __first = __temp;
3161         if (__first == __last)
3162           __throw_regex_error<regex_constants::error_brace>();
3163         if (*__first != ',') {
3164           __temp = __parse_Back_close_brace(__first, __last);
3165           if (__temp == __first)
3166             __throw_regex_error<regex_constants::error_brace>();
3167           __push_loop(__min, __min, __s, __mexp_begin, __mexp_end, true);
3168           __first = __temp;
3169         } else {
3170           ++__first; // consume ','
3171           int __max = -1;
3172           __first   = __parse_DUP_COUNT(__first, __last, __max);
3173           __temp    = __parse_Back_close_brace(__first, __last);
3174           if (__temp == __first)
3175             __throw_regex_error<regex_constants::error_brace>();
3176           if (__max == -1)
3177             __push_greedy_inf_repeat(__min, __s, __mexp_begin, __mexp_end);
3178           else {
3179             if (__max < __min)
3180               __throw_regex_error<regex_constants::error_badbrace>();
3181             __push_loop(__min, __max, __s, __mexp_begin, __mexp_end, true);
3182           }
3183           __first = __temp;
3184         }
3185       }
3186     }
3187   }
3188   return __first;
3189 }
3190 
3191 template <class _CharT, class _Traits>
3192 template <class _ForwardIterator>
3193 _ForwardIterator basic_regex<_CharT, _Traits>::__parse_ERE_dupl_symbol(
3194     _ForwardIterator __first,
3195     _ForwardIterator __last,
3196     __owns_one_state<_CharT>* __s,
3197     unsigned __mexp_begin,
3198     unsigned __mexp_end) {
3199   if (__first != __last) {
3200     unsigned __grammar = __get_grammar(__flags_);
3201     switch (*__first) {
3202     case '*':
3203       ++__first;
3204       if (__grammar == ECMAScript && __first != __last && *__first == '?') {
3205         ++__first;
3206         __push_nongreedy_inf_repeat(0, __s, __mexp_begin, __mexp_end);
3207       } else
3208         __push_greedy_inf_repeat(0, __s, __mexp_begin, __mexp_end);
3209       break;
3210     case '+':
3211       ++__first;
3212       if (__grammar == ECMAScript && __first != __last && *__first == '?') {
3213         ++__first;
3214         __push_nongreedy_inf_repeat(1, __s, __mexp_begin, __mexp_end);
3215       } else
3216         __push_greedy_inf_repeat(1, __s, __mexp_begin, __mexp_end);
3217       break;
3218     case '?':
3219       ++__first;
3220       if (__grammar == ECMAScript && __first != __last && *__first == '?') {
3221         ++__first;
3222         __push_loop(0, 1, __s, __mexp_begin, __mexp_end, false);
3223       } else
3224         __push_loop(0, 1, __s, __mexp_begin, __mexp_end);
3225       break;
3226     case '{': {
3227       int __min;
3228       _ForwardIterator __temp = __parse_DUP_COUNT(++__first, __last, __min);
3229       if (__temp == __first)
3230         __throw_regex_error<regex_constants::error_badbrace>();
3231       __first = __temp;
3232       if (__first == __last)
3233         __throw_regex_error<regex_constants::error_brace>();
3234       switch (*__first) {
3235       case '}':
3236         ++__first;
3237         if (__grammar == ECMAScript && __first != __last && *__first == '?') {
3238           ++__first;
3239           __push_loop(__min, __min, __s, __mexp_begin, __mexp_end, false);
3240         } else
3241           __push_loop(__min, __min, __s, __mexp_begin, __mexp_end);
3242         break;
3243       case ',':
3244         ++__first;
3245         if (__first == __last)
3246           __throw_regex_error<regex_constants::error_badbrace>();
3247         if (*__first == '}') {
3248           ++__first;
3249           if (__grammar == ECMAScript && __first != __last && *__first == '?') {
3250             ++__first;
3251             __push_nongreedy_inf_repeat(__min, __s, __mexp_begin, __mexp_end);
3252           } else
3253             __push_greedy_inf_repeat(__min, __s, __mexp_begin, __mexp_end);
3254         } else {
3255           int __max = -1;
3256           __temp    = __parse_DUP_COUNT(__first, __last, __max);
3257           if (__temp == __first)
3258             __throw_regex_error<regex_constants::error_brace>();
3259           __first = __temp;
3260           if (__first == __last || *__first != '}')
3261             __throw_regex_error<regex_constants::error_brace>();
3262           ++__first;
3263           if (__max < __min)
3264             __throw_regex_error<regex_constants::error_badbrace>();
3265           if (__grammar == ECMAScript && __first != __last && *__first == '?') {
3266             ++__first;
3267             __push_loop(__min, __max, __s, __mexp_begin, __mexp_end, false);
3268           } else
3269             __push_loop(__min, __max, __s, __mexp_begin, __mexp_end);
3270         }
3271         break;
3272       default:
3273         __throw_regex_error<regex_constants::error_badbrace>();
3274       }
3275     } break;
3276     }
3277   }
3278   return __first;
3279 }
3280 
3281 template <class _CharT, class _Traits>
3282 template <class _ForwardIterator>
3283 _ForwardIterator
3284 basic_regex<_CharT, _Traits>::__parse_bracket_expression(_ForwardIterator __first, _ForwardIterator __last) {
3285   if (__first != __last && *__first == '[') {
3286     ++__first;
3287     if (__first == __last)
3288       __throw_regex_error<regex_constants::error_brack>();
3289     bool __negate = false;
3290     if (*__first == '^') {
3291       ++__first;
3292       __negate = true;
3293     }
3294     __bracket_expression<_CharT, _Traits>* __ml = __start_matching_list(__negate);
3295     // __ml owned by *this
3296     if (__first == __last)
3297       __throw_regex_error<regex_constants::error_brack>();
3298     if (__get_grammar(__flags_) != ECMAScript && *__first == ']') {
3299       __ml->__add_char(']');
3300       ++__first;
3301     }
3302     __first = __parse_follow_list(__first, __last, __ml);
3303     if (__first == __last)
3304       __throw_regex_error<regex_constants::error_brack>();
3305     if (*__first == '-') {
3306       __ml->__add_char('-');
3307       ++__first;
3308     }
3309     if (__first == __last || *__first != ']')
3310       __throw_regex_error<regex_constants::error_brack>();
3311     ++__first;
3312   }
3313   return __first;
3314 }
3315 
3316 template <class _CharT, class _Traits>
3317 template <class _ForwardIterator>
3318 _ForwardIterator basic_regex<_CharT, _Traits>::__parse_follow_list(
3319     _ForwardIterator __first, _ForwardIterator __last, __bracket_expression<_CharT, _Traits>* __ml) {
3320   if (__first != __last) {
3321     while (true) {
3322       _ForwardIterator __temp = __parse_expression_term(__first, __last, __ml);
3323       if (__temp == __first)
3324         break;
3325       __first = __temp;
3326     }
3327   }
3328   return __first;
3329 }
3330 
3331 template <class _CharT, class _Traits>
3332 template <class _ForwardIterator>
3333 _ForwardIterator basic_regex<_CharT, _Traits>::__parse_expression_term(
3334     _ForwardIterator __first, _ForwardIterator __last, __bracket_expression<_CharT, _Traits>* __ml) {
3335   if (__first != __last && *__first != ']') {
3336     _ForwardIterator __temp = std::next(__first);
3337     basic_string<_CharT> __start_range;
3338     if (__temp != __last && *__first == '[') {
3339       if (*__temp == '=')
3340         return __parse_equivalence_class(++__temp, __last, __ml);
3341       else if (*__temp == ':')
3342         return __parse_character_class(++__temp, __last, __ml);
3343       else if (*__temp == '.')
3344         __first = __parse_collating_symbol(++__temp, __last, __start_range);
3345     }
3346     unsigned __grammar = __get_grammar(__flags_);
3347     if (__start_range.empty()) {
3348       if ((__grammar == ECMAScript || __grammar == awk) && *__first == '\\') {
3349         if (__grammar == ECMAScript)
3350           __first = __parse_class_escape(++__first, __last, __start_range, __ml);
3351         else
3352           __first = __parse_awk_escape(++__first, __last, &__start_range);
3353       } else {
3354         __start_range = *__first;
3355         ++__first;
3356       }
3357     }
3358     if (__first != __last && *__first != ']') {
3359       __temp = std::next(__first);
3360       if (__temp != __last && *__first == '-' && *__temp != ']') {
3361         // parse a range
3362         basic_string<_CharT> __end_range;
3363         __first = __temp;
3364         ++__temp;
3365         if (__temp != __last && *__first == '[' && *__temp == '.')
3366           __first = __parse_collating_symbol(++__temp, __last, __end_range);
3367         else {
3368           if ((__grammar == ECMAScript || __grammar == awk) && *__first == '\\') {
3369             if (__grammar == ECMAScript)
3370               __first = __parse_class_escape(++__first, __last, __end_range, __ml);
3371             else
3372               __first = __parse_awk_escape(++__first, __last, &__end_range);
3373           } else {
3374             __end_range = *__first;
3375             ++__first;
3376           }
3377         }
3378         __ml->__add_range(std::move(__start_range), std::move(__end_range));
3379       } else if (!__start_range.empty()) {
3380         if (__start_range.size() == 1)
3381           __ml->__add_char(__start_range[0]);
3382         else
3383           __ml->__add_digraph(__start_range[0], __start_range[1]);
3384       }
3385     } else if (!__start_range.empty()) {
3386       if (__start_range.size() == 1)
3387         __ml->__add_char(__start_range[0]);
3388       else
3389         __ml->__add_digraph(__start_range[0], __start_range[1]);
3390     }
3391   }
3392   return __first;
3393 }
3394 
3395 template <class _CharT, class _Traits>
3396 template <class _ForwardIterator>
3397 _ForwardIterator basic_regex<_CharT, _Traits>::__parse_class_escape(
3398     _ForwardIterator __first,
3399     _ForwardIterator __last,
3400     basic_string<_CharT>& __str,
3401     __bracket_expression<_CharT, _Traits>* __ml) {
3402   if (__first == __last)
3403     __throw_regex_error<regex_constants::error_escape>();
3404   switch (*__first) {
3405   case 0:
3406     __str = *__first;
3407     return ++__first;
3408   case 'b':
3409     __str = _CharT(8);
3410     return ++__first;
3411   case 'd':
3412     __ml->__add_class(ctype_base::digit);
3413     return ++__first;
3414   case 'D':
3415     __ml->__add_neg_class(ctype_base::digit);
3416     return ++__first;
3417   case 's':
3418     __ml->__add_class(ctype_base::space);
3419     return ++__first;
3420   case 'S':
3421     __ml->__add_neg_class(ctype_base::space);
3422     return ++__first;
3423   case 'w':
3424     __ml->__add_class(ctype_base::alnum);
3425     __ml->__add_char('_');
3426     return ++__first;
3427   case 'W':
3428     __ml->__add_neg_class(ctype_base::alnum);
3429     __ml->__add_neg_char('_');
3430     return ++__first;
3431   }
3432   __first = __parse_character_escape(__first, __last, &__str);
3433   return __first;
3434 }
3435 
3436 template <class _CharT, class _Traits>
3437 template <class _ForwardIterator>
3438 _ForwardIterator basic_regex<_CharT, _Traits>::__parse_awk_escape(
3439     _ForwardIterator __first, _ForwardIterator __last, basic_string<_CharT>* __str) {
3440   if (__first == __last)
3441     __throw_regex_error<regex_constants::error_escape>();
3442   switch (*__first) {
3443   case '\\':
3444   case '"':
3445   case '/':
3446     if (__str)
3447       *__str = *__first;
3448     else
3449       __push_char(*__first);
3450     return ++__first;
3451   case 'a':
3452     if (__str)
3453       *__str = _CharT(7);
3454     else
3455       __push_char(_CharT(7));
3456     return ++__first;
3457   case 'b':
3458     if (__str)
3459       *__str = _CharT(8);
3460     else
3461       __push_char(_CharT(8));
3462     return ++__first;
3463   case 'f':
3464     if (__str)
3465       *__str = _CharT(0xC);
3466     else
3467       __push_char(_CharT(0xC));
3468     return ++__first;
3469   case 'n':
3470     if (__str)
3471       *__str = _CharT(0xA);
3472     else
3473       __push_char(_CharT(0xA));
3474     return ++__first;
3475   case 'r':
3476     if (__str)
3477       *__str = _CharT(0xD);
3478     else
3479       __push_char(_CharT(0xD));
3480     return ++__first;
3481   case 't':
3482     if (__str)
3483       *__str = _CharT(0x9);
3484     else
3485       __push_char(_CharT(0x9));
3486     return ++__first;
3487   case 'v':
3488     if (__str)
3489       *__str = _CharT(0xB);
3490     else
3491       __push_char(_CharT(0xB));
3492     return ++__first;
3493   }
3494   if ('0' <= *__first && *__first <= '7') {
3495     unsigned __val = *__first - '0';
3496     if (++__first != __last && ('0' <= *__first && *__first <= '7')) {
3497       __val = 8 * __val + *__first - '0';
3498       if (++__first != __last && ('0' <= *__first && *__first <= '7'))
3499         __val = 8 * __val + *__first++ - '0';
3500     }
3501     if (__str)
3502       *__str = _CharT(__val);
3503     else
3504       __push_char(_CharT(__val));
3505   } else
3506     __throw_regex_error<regex_constants::error_escape>();
3507   return __first;
3508 }
3509 
3510 template <class _CharT, class _Traits>
3511 template <class _ForwardIterator>
3512 _ForwardIterator basic_regex<_CharT, _Traits>::__parse_equivalence_class(
3513     _ForwardIterator __first, _ForwardIterator __last, __bracket_expression<_CharT, _Traits>* __ml) {
3514   // Found [=
3515   //   This means =] must exist
3516   value_type __equal_close[2] = {'=', ']'};
3517   _ForwardIterator __temp     = std::search(__first, __last, __equal_close, __equal_close + 2);
3518   if (__temp == __last)
3519     __throw_regex_error<regex_constants::error_brack>();
3520   // [__first, __temp) contains all text in [= ... =]
3521   string_type __collate_name = __traits_.lookup_collatename(__first, __temp);
3522   if (__collate_name.empty())
3523     __throw_regex_error<regex_constants::error_collate>();
3524   string_type __equiv_name = __traits_.transform_primary(__collate_name.begin(), __collate_name.end());
3525   if (!__equiv_name.empty())
3526     __ml->__add_equivalence(__equiv_name);
3527   else {
3528     switch (__collate_name.size()) {
3529     case 1:
3530       __ml->__add_char(__collate_name[0]);
3531       break;
3532     case 2:
3533       __ml->__add_digraph(__collate_name[0], __collate_name[1]);
3534       break;
3535     default:
3536       __throw_regex_error<regex_constants::error_collate>();
3537     }
3538   }
3539   __first = std::next(__temp, 2);
3540   return __first;
3541 }
3542 
3543 template <class _CharT, class _Traits>
3544 template <class _ForwardIterator>
3545 _ForwardIterator basic_regex<_CharT, _Traits>::__parse_character_class(
3546     _ForwardIterator __first, _ForwardIterator __last, __bracket_expression<_CharT, _Traits>* __ml) {
3547   // Found [:
3548   //   This means :] must exist
3549   value_type __colon_close[2] = {':', ']'};
3550   _ForwardIterator __temp     = std::search(__first, __last, __colon_close, __colon_close + 2);
3551   if (__temp == __last)
3552     __throw_regex_error<regex_constants::error_brack>();
3553   // [__first, __temp) contains all text in [: ... :]
3554   typedef typename _Traits::char_class_type char_class_type;
3555   char_class_type __class_type = __traits_.lookup_classname(__first, __temp, __flags_ & icase);
3556   if (__class_type == 0)
3557     __throw_regex_error<regex_constants::error_ctype>();
3558   __ml->__add_class(__class_type);
3559   __first = std::next(__temp, 2);
3560   return __first;
3561 }
3562 
3563 template <class _CharT, class _Traits>
3564 template <class _ForwardIterator>
3565 _ForwardIterator basic_regex<_CharT, _Traits>::__parse_collating_symbol(
3566     _ForwardIterator __first, _ForwardIterator __last, basic_string<_CharT>& __col_sym) {
3567   // Found [.
3568   //   This means .] must exist
3569   value_type __dot_close[2] = {'.', ']'};
3570   _ForwardIterator __temp   = std::search(__first, __last, __dot_close, __dot_close + 2);
3571   if (__temp == __last)
3572     __throw_regex_error<regex_constants::error_brack>();
3573   // [__first, __temp) contains all text in [. ... .]
3574   __col_sym = __traits_.lookup_collatename(__first, __temp);
3575   switch (__col_sym.size()) {
3576   case 1:
3577   case 2:
3578     break;
3579   default:
3580     __throw_regex_error<regex_constants::error_collate>();
3581   }
3582   __first = std::next(__temp, 2);
3583   return __first;
3584 }
3585 
3586 template <class _CharT, class _Traits>
3587 template <class _ForwardIterator>
3588 _ForwardIterator
3589 basic_regex<_CharT, _Traits>::__parse_DUP_COUNT(_ForwardIterator __first, _ForwardIterator __last, int& __c) {
3590   if (__first != __last) {
3591     int __val = __traits_.value(*__first, 10);
3592     if (__val != -1) {
3593       __c = __val;
3594       for (++__first; __first != __last && (__val = __traits_.value(*__first, 10)) != -1; ++__first) {
3595         if (__c >= numeric_limits<int>::max() / 10)
3596           __throw_regex_error<regex_constants::error_badbrace>();
3597         __c *= 10;
3598         __c += __val;
3599       }
3600     }
3601   }
3602   return __first;
3603 }
3604 
3605 template <class _CharT, class _Traits>
3606 template <class _ForwardIterator>
3607 _ForwardIterator basic_regex<_CharT, _Traits>::__parse_ecma_exp(_ForwardIterator __first, _ForwardIterator __last) {
3608   __owns_one_state<_CharT>* __sa = __end_;
3609   _ForwardIterator __temp        = __parse_alternative(__first, __last);
3610   if (__temp == __first)
3611     __push_empty();
3612   __first = __temp;
3613   while (__first != __last && *__first == '|') {
3614     __owns_one_state<_CharT>* __sb = __end_;
3615     __temp                         = __parse_alternative(++__first, __last);
3616     if (__temp == __first)
3617       __push_empty();
3618     __push_alternation(__sa, __sb);
3619     __first = __temp;
3620   }
3621   return __first;
3622 }
3623 
3624 template <class _CharT, class _Traits>
3625 template <class _ForwardIterator>
3626 _ForwardIterator basic_regex<_CharT, _Traits>::__parse_alternative(_ForwardIterator __first, _ForwardIterator __last) {
3627   while (true) {
3628     _ForwardIterator __temp = __parse_term(__first, __last);
3629     if (__temp == __first)
3630       break;
3631     __first = __temp;
3632   }
3633   return __first;
3634 }
3635 
3636 template <class _CharT, class _Traits>
3637 template <class _ForwardIterator>
3638 _ForwardIterator basic_regex<_CharT, _Traits>::__parse_term(_ForwardIterator __first, _ForwardIterator __last) {
3639   _ForwardIterator __temp = __parse_assertion(__first, __last);
3640   if (__temp == __first) {
3641     __owns_one_state<_CharT>* __e = __end_;
3642     unsigned __mexp_begin         = __marked_count_;
3643     __temp                        = __parse_atom(__first, __last);
3644     if (__temp != __first)
3645       __first = __parse_ERE_dupl_symbol(__temp, __last, __e, __mexp_begin + 1, __marked_count_ + 1);
3646   } else
3647     __first = __temp;
3648   return __first;
3649 }
3650 
3651 template <class _CharT, class _Traits>
3652 template <class _ForwardIterator>
3653 _ForwardIterator basic_regex<_CharT, _Traits>::__parse_assertion(_ForwardIterator __first, _ForwardIterator __last) {
3654   if (__first != __last) {
3655     switch (*__first) {
3656     case '^':
3657       __push_l_anchor();
3658       ++__first;
3659       break;
3660     case '$':
3661       __push_r_anchor();
3662       ++__first;
3663       break;
3664     case '\\': {
3665       _ForwardIterator __temp = std::next(__first);
3666       if (__temp != __last) {
3667         if (*__temp == 'b') {
3668           __push_word_boundary(false);
3669           __first = ++__temp;
3670         } else if (*__temp == 'B') {
3671           __push_word_boundary(true);
3672           __first = ++__temp;
3673         }
3674       }
3675     } break;
3676     case '(': {
3677       _ForwardIterator __temp = std::next(__first);
3678       if (__temp != __last && *__temp == '?') {
3679         if (++__temp != __last) {
3680           switch (*__temp) {
3681           case '=': {
3682             basic_regex __exp;
3683             __exp.__flags_  = __flags_;
3684             __temp          = __exp.__parse(++__temp, __last);
3685             unsigned __mexp = __exp.__marked_count_;
3686             __push_lookahead(std::move(__exp), false, __marked_count_);
3687             __marked_count_ += __mexp;
3688             if (__temp == __last || *__temp != ')')
3689               __throw_regex_error<regex_constants::error_paren>();
3690             __first = ++__temp;
3691           } break;
3692           case '!': {
3693             basic_regex __exp;
3694             __exp.__flags_  = __flags_;
3695             __temp          = __exp.__parse(++__temp, __last);
3696             unsigned __mexp = __exp.__marked_count_;
3697             __push_lookahead(std::move(__exp), true, __marked_count_);
3698             __marked_count_ += __mexp;
3699             if (__temp == __last || *__temp != ')')
3700               __throw_regex_error<regex_constants::error_paren>();
3701             __first = ++__temp;
3702           } break;
3703           }
3704         }
3705       }
3706     } break;
3707     }
3708   }
3709   return __first;
3710 }
3711 
3712 template <class _CharT, class _Traits>
3713 template <class _ForwardIterator>
3714 _ForwardIterator basic_regex<_CharT, _Traits>::__parse_atom(_ForwardIterator __first, _ForwardIterator __last) {
3715   if (__first != __last) {
3716     switch (*__first) {
3717     case '.':
3718       __push_match_any_but_newline();
3719       ++__first;
3720       break;
3721     case '\\':
3722       __first = __parse_atom_escape(__first, __last);
3723       break;
3724     case '[':
3725       __first = __parse_bracket_expression(__first, __last);
3726       break;
3727     case '(': {
3728       ++__first;
3729       if (__first == __last)
3730         __throw_regex_error<regex_constants::error_paren>();
3731       _ForwardIterator __temp = std::next(__first);
3732       if (__temp != __last && *__first == '?' && *__temp == ':') {
3733         ++__open_count_;
3734         __first = __parse_ecma_exp(++__temp, __last);
3735         if (__first == __last || *__first != ')')
3736           __throw_regex_error<regex_constants::error_paren>();
3737         --__open_count_;
3738         ++__first;
3739       } else {
3740         __push_begin_marked_subexpression();
3741         unsigned __temp_count = __marked_count_;
3742         ++__open_count_;
3743         __first = __parse_ecma_exp(__first, __last);
3744         if (__first == __last || *__first != ')')
3745           __throw_regex_error<regex_constants::error_paren>();
3746         __push_end_marked_subexpression(__temp_count);
3747         --__open_count_;
3748         ++__first;
3749       }
3750     } break;
3751     case '*':
3752     case '+':
3753     case '?':
3754     case '{':
3755       __throw_regex_error<regex_constants::error_badrepeat>();
3756       break;
3757     default:
3758       __first = __parse_pattern_character(__first, __last);
3759       break;
3760     }
3761   }
3762   return __first;
3763 }
3764 
3765 template <class _CharT, class _Traits>
3766 template <class _ForwardIterator>
3767 _ForwardIterator basic_regex<_CharT, _Traits>::__parse_atom_escape(_ForwardIterator __first, _ForwardIterator __last) {
3768   if (__first != __last && *__first == '\\') {
3769     _ForwardIterator __t1 = std::next(__first);
3770     if (__t1 == __last)
3771       __throw_regex_error<regex_constants::error_escape>();
3772 
3773     _ForwardIterator __t2 = __parse_decimal_escape(__t1, __last);
3774     if (__t2 != __t1)
3775       __first = __t2;
3776     else {
3777       __t2 = __parse_character_class_escape(__t1, __last);
3778       if (__t2 != __t1)
3779         __first = __t2;
3780       else {
3781         __t2 = __parse_character_escape(__t1, __last);
3782         if (__t2 != __t1)
3783           __first = __t2;
3784       }
3785     }
3786   }
3787   return __first;
3788 }
3789 
3790 template <class _CharT, class _Traits>
3791 template <class _ForwardIterator>
3792 _ForwardIterator
3793 basic_regex<_CharT, _Traits>::__parse_decimal_escape(_ForwardIterator __first, _ForwardIterator __last) {
3794   if (__first != __last) {
3795     if (*__first == '0') {
3796       __push_char(_CharT());
3797       ++__first;
3798     } else if ('1' <= *__first && *__first <= '9') {
3799       unsigned __v = *__first - '0';
3800       for (++__first; __first != __last && '0' <= *__first && *__first <= '9'; ++__first) {
3801         if (__v >= numeric_limits<unsigned>::max() / 10)
3802           __throw_regex_error<regex_constants::error_backref>();
3803         __v = 10 * __v + *__first - '0';
3804       }
3805       if (__v == 0 || __v > mark_count())
3806         __throw_regex_error<regex_constants::error_backref>();
3807       __push_back_ref(__v);
3808     }
3809   }
3810   return __first;
3811 }
3812 
3813 template <class _CharT, class _Traits>
3814 template <class _ForwardIterator>
3815 _ForwardIterator
3816 basic_regex<_CharT, _Traits>::__parse_character_class_escape(_ForwardIterator __first, _ForwardIterator __last) {
3817   if (__first != __last) {
3818     __bracket_expression<_CharT, _Traits>* __ml;
3819     switch (*__first) {
3820     case 'd':
3821       __ml = __start_matching_list(false);
3822       __ml->__add_class(ctype_base::digit);
3823       ++__first;
3824       break;
3825     case 'D':
3826       __ml = __start_matching_list(true);
3827       __ml->__add_class(ctype_base::digit);
3828       ++__first;
3829       break;
3830     case 's':
3831       __ml = __start_matching_list(false);
3832       __ml->__add_class(ctype_base::space);
3833       ++__first;
3834       break;
3835     case 'S':
3836       __ml = __start_matching_list(true);
3837       __ml->__add_class(ctype_base::space);
3838       ++__first;
3839       break;
3840     case 'w':
3841       __ml = __start_matching_list(false);
3842       __ml->__add_class(ctype_base::alnum);
3843       __ml->__add_char('_');
3844       ++__first;
3845       break;
3846     case 'W':
3847       __ml = __start_matching_list(true);
3848       __ml->__add_class(ctype_base::alnum);
3849       __ml->__add_char('_');
3850       ++__first;
3851       break;
3852     }
3853   }
3854   return __first;
3855 }
3856 
3857 template <class _CharT, class _Traits>
3858 template <class _ForwardIterator>
3859 _ForwardIterator basic_regex<_CharT, _Traits>::__parse_character_escape(
3860     _ForwardIterator __first, _ForwardIterator __last, basic_string<_CharT>* __str) {
3861   if (__first != __last) {
3862     _ForwardIterator __t;
3863     unsigned __sum = 0;
3864     int __hd;
3865     switch (*__first) {
3866     case 'f':
3867       if (__str)
3868         *__str = _CharT(0xC);
3869       else
3870         __push_char(_CharT(0xC));
3871       ++__first;
3872       break;
3873     case 'n':
3874       if (__str)
3875         *__str = _CharT(0xA);
3876       else
3877         __push_char(_CharT(0xA));
3878       ++__first;
3879       break;
3880     case 'r':
3881       if (__str)
3882         *__str = _CharT(0xD);
3883       else
3884         __push_char(_CharT(0xD));
3885       ++__first;
3886       break;
3887     case 't':
3888       if (__str)
3889         *__str = _CharT(0x9);
3890       else
3891         __push_char(_CharT(0x9));
3892       ++__first;
3893       break;
3894     case 'v':
3895       if (__str)
3896         *__str = _CharT(0xB);
3897       else
3898         __push_char(_CharT(0xB));
3899       ++__first;
3900       break;
3901     case 'c':
3902       if ((__t = std::next(__first)) != __last) {
3903         if (('A' <= *__t && *__t <= 'Z') || ('a' <= *__t && *__t <= 'z')) {
3904           if (__str)
3905             *__str = _CharT(*__t % 32);
3906           else
3907             __push_char(_CharT(*__t % 32));
3908           __first = ++__t;
3909         } else
3910           __throw_regex_error<regex_constants::error_escape>();
3911       } else
3912         __throw_regex_error<regex_constants::error_escape>();
3913       break;
3914     case 'u':
3915       ++__first;
3916       if (__first == __last)
3917         __throw_regex_error<regex_constants::error_escape>();
3918       __hd = __traits_.value(*__first, 16);
3919       if (__hd == -1)
3920         __throw_regex_error<regex_constants::error_escape>();
3921       __sum = 16 * __sum + static_cast<unsigned>(__hd);
3922       ++__first;
3923       if (__first == __last)
3924         __throw_regex_error<regex_constants::error_escape>();
3925       __hd = __traits_.value(*__first, 16);
3926       if (__hd == -1)
3927         __throw_regex_error<regex_constants::error_escape>();
3928       __sum = 16 * __sum + static_cast<unsigned>(__hd);
3929       _LIBCPP_FALLTHROUGH();
3930     case 'x':
3931       ++__first;
3932       if (__first == __last)
3933         __throw_regex_error<regex_constants::error_escape>();
3934       __hd = __traits_.value(*__first, 16);
3935       if (__hd == -1)
3936         __throw_regex_error<regex_constants::error_escape>();
3937       __sum = 16 * __sum + static_cast<unsigned>(__hd);
3938       ++__first;
3939       if (__first == __last)
3940         __throw_regex_error<regex_constants::error_escape>();
3941       __hd = __traits_.value(*__first, 16);
3942       if (__hd == -1)
3943         __throw_regex_error<regex_constants::error_escape>();
3944       __sum = 16 * __sum + static_cast<unsigned>(__hd);
3945       if (__str)
3946         *__str = _CharT(__sum);
3947       else
3948         __push_char(_CharT(__sum));
3949       ++__first;
3950       break;
3951     case '0':
3952       if (__str)
3953         *__str = _CharT(0);
3954       else
3955         __push_char(_CharT(0));
3956       ++__first;
3957       break;
3958     default:
3959       if (*__first != '_' && !__traits_.isctype(*__first, ctype_base::alnum)) {
3960         if (__str)
3961           *__str = *__first;
3962         else
3963           __push_char(*__first);
3964         ++__first;
3965       } else
3966         __throw_regex_error<regex_constants::error_escape>();
3967       break;
3968     }
3969   }
3970   return __first;
3971 }
3972 
3973 template <class _CharT, class _Traits>
3974 template <class _ForwardIterator>
3975 _ForwardIterator
3976 basic_regex<_CharT, _Traits>::__parse_pattern_character(_ForwardIterator __first, _ForwardIterator __last) {
3977   if (__first != __last) {
3978     switch (*__first) {
3979     case '^':
3980     case '$':
3981     case '\\':
3982     case '.':
3983     case '*':
3984     case '+':
3985     case '?':
3986     case '(':
3987     case ')':
3988     case '[':
3989     case ']':
3990     case '{':
3991     case '}':
3992     case '|':
3993       break;
3994     default:
3995       __push_char(*__first);
3996       ++__first;
3997       break;
3998     }
3999   }
4000   return __first;
4001 }
4002 
4003 template <class _CharT, class _Traits>
4004 template <class _ForwardIterator>
4005 _ForwardIterator basic_regex<_CharT, _Traits>::__parse_grep(_ForwardIterator __first, _ForwardIterator __last) {
4006   __owns_one_state<_CharT>* __sa = __end_;
4007   _ForwardIterator __t1          = std::find(__first, __last, _CharT('\n'));
4008   if (__t1 != __first)
4009     __parse_basic_reg_exp(__first, __t1);
4010   else
4011     __push_empty();
4012   __first = __t1;
4013   if (__first != __last)
4014     ++__first;
4015   while (__first != __last) {
4016     __t1                           = std::find(__first, __last, _CharT('\n'));
4017     __owns_one_state<_CharT>* __sb = __end_;
4018     if (__t1 != __first)
4019       __parse_basic_reg_exp(__first, __t1);
4020     else
4021       __push_empty();
4022     __push_alternation(__sa, __sb);
4023     __first = __t1;
4024     if (__first != __last)
4025       ++__first;
4026   }
4027   return __first;
4028 }
4029 
4030 template <class _CharT, class _Traits>
4031 template <class _ForwardIterator>
4032 _ForwardIterator basic_regex<_CharT, _Traits>::__parse_egrep(_ForwardIterator __first, _ForwardIterator __last) {
4033   __owns_one_state<_CharT>* __sa = __end_;
4034   _ForwardIterator __t1          = std::find(__first, __last, _CharT('\n'));
4035   if (__t1 != __first)
4036     __parse_extended_reg_exp(__first, __t1);
4037   else
4038     __push_empty();
4039   __first = __t1;
4040   if (__first != __last)
4041     ++__first;
4042   while (__first != __last) {
4043     __t1                           = std::find(__first, __last, _CharT('\n'));
4044     __owns_one_state<_CharT>* __sb = __end_;
4045     if (__t1 != __first)
4046       __parse_extended_reg_exp(__first, __t1);
4047     else
4048       __push_empty();
4049     __push_alternation(__sa, __sb);
4050     __first = __t1;
4051     if (__first != __last)
4052       ++__first;
4053   }
4054   return __first;
4055 }
4056 
4057 template <class _CharT, class _Traits>
4058 bool basic_regex<_CharT, _Traits>::__test_back_ref(_CharT __c) {
4059   unsigned __val = __traits_.value(__c, 10);
4060   if (__val >= 1 && __val <= 9) {
4061     if (__val > mark_count())
4062       __throw_regex_error<regex_constants::error_backref>();
4063     __push_back_ref(__val);
4064     return true;
4065   }
4066 
4067   return false;
4068 }
4069 
4070 template <class _CharT, class _Traits>
4071 void basic_regex<_CharT, _Traits>::__push_loop(
4072     size_t __min, size_t __max, __owns_one_state<_CharT>* __s, size_t __mexp_begin, size_t __mexp_end, bool __greedy) {
4073   unique_ptr<__empty_state<_CharT> > __e1(new __empty_state<_CharT>(__end_->first()));
4074   __end_->first() = nullptr;
4075   unique_ptr<__loop<_CharT> > __e2(
4076       new __loop<_CharT>(__loop_count_, __s->first(), __e1.get(), __mexp_begin, __mexp_end, __greedy, __min, __max));
4077   __s->first() = nullptr;
4078   __e1.release();
4079   __end_->first() = new __repeat_one_loop<_CharT>(__e2.get());
4080   __end_          = __e2->second();
4081   __s->first()    = __e2.release();
4082   ++__loop_count_;
4083 }
4084 
4085 template <class _CharT, class _Traits>
4086 void basic_regex<_CharT, _Traits>::__push_char(value_type __c) {
4087   if (flags() & icase)
4088     __end_->first() = new __match_char_icase<_CharT, _Traits>(__traits_, __c, __end_->first());
4089   else if (flags() & collate)
4090     __end_->first() = new __match_char_collate<_CharT, _Traits>(__traits_, __c, __end_->first());
4091   else
4092     __end_->first() = new __match_char<_CharT>(__c, __end_->first());
4093   __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4094 }
4095 
4096 template <class _CharT, class _Traits>
4097 void basic_regex<_CharT, _Traits>::__push_begin_marked_subexpression() {
4098   if (!(__flags_ & nosubs)) {
4099     __end_->first() = new __begin_marked_subexpression<_CharT>(++__marked_count_, __end_->first());
4100     __end_          = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4101   }
4102 }
4103 
4104 template <class _CharT, class _Traits>
4105 void basic_regex<_CharT, _Traits>::__push_end_marked_subexpression(unsigned __sub) {
4106   if (!(__flags_ & nosubs)) {
4107     __end_->first() = new __end_marked_subexpression<_CharT>(__sub, __end_->first());
4108     __end_          = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4109   }
4110 }
4111 
4112 template <class _CharT, class _Traits>
4113 void basic_regex<_CharT, _Traits>::__push_l_anchor() {
4114   __end_->first() = new __l_anchor_multiline<_CharT>(__use_multiline(), __end_->first());
4115   __end_          = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4116 }
4117 
4118 template <class _CharT, class _Traits>
4119 void basic_regex<_CharT, _Traits>::__push_r_anchor() {
4120   __end_->first() = new __r_anchor_multiline<_CharT>(__use_multiline(), __end_->first());
4121   __end_          = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4122 }
4123 
4124 template <class _CharT, class _Traits>
4125 void basic_regex<_CharT, _Traits>::__push_match_any() {
4126   __end_->first() = new __match_any<_CharT>(__end_->first());
4127   __end_          = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4128 }
4129 
4130 template <class _CharT, class _Traits>
4131 void basic_regex<_CharT, _Traits>::__push_match_any_but_newline() {
4132   __end_->first() = new __match_any_but_newline<_CharT>(__end_->first());
4133   __end_          = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4134 }
4135 
4136 template <class _CharT, class _Traits>
4137 void basic_regex<_CharT, _Traits>::__push_empty() {
4138   __end_->first() = new __empty_state<_CharT>(__end_->first());
4139   __end_          = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4140 }
4141 
4142 template <class _CharT, class _Traits>
4143 void basic_regex<_CharT, _Traits>::__push_word_boundary(bool __invert) {
4144   __end_->first() = new __word_boundary<_CharT, _Traits>(__traits_, __invert, __end_->first());
4145   __end_          = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4146 }
4147 
4148 template <class _CharT, class _Traits>
4149 void basic_regex<_CharT, _Traits>::__push_back_ref(int __i) {
4150   if (flags() & icase)
4151     __end_->first() = new __back_ref_icase<_CharT, _Traits>(__traits_, __i, __end_->first());
4152   else if (flags() & collate)
4153     __end_->first() = new __back_ref_collate<_CharT, _Traits>(__traits_, __i, __end_->first());
4154   else
4155     __end_->first() = new __back_ref<_CharT>(__i, __end_->first());
4156   __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4157 }
4158 
4159 template <class _CharT, class _Traits>
4160 void basic_regex<_CharT, _Traits>::__push_alternation(__owns_one_state<_CharT>* __sa, __owns_one_state<_CharT>* __ea) {
4161   __sa->first() = new __alternate<_CharT>(
4162       static_cast<__owns_one_state<_CharT>*>(__sa->first()), static_cast<__owns_one_state<_CharT>*>(__ea->first()));
4163   __ea->first()   = nullptr;
4164   __ea->first()   = new __empty_state<_CharT>(__end_->first());
4165   __end_->first() = nullptr;
4166   __end_->first() = new __empty_non_own_state<_CharT>(__ea->first());
4167   __end_          = static_cast<__owns_one_state<_CharT>*>(__ea->first());
4168 }
4169 
4170 template <class _CharT, class _Traits>
4171 __bracket_expression<_CharT, _Traits>* basic_regex<_CharT, _Traits>::__start_matching_list(bool __negate) {
4172   __bracket_expression<_CharT, _Traits>* __r = new __bracket_expression<_CharT, _Traits>(
4173       __traits_, __end_->first(), __negate, __flags_ & icase, __flags_ & collate);
4174   __end_->first() = __r;
4175   __end_          = __r;
4176   return __r;
4177 }
4178 
4179 template <class _CharT, class _Traits>
4180 void basic_regex<_CharT, _Traits>::__push_lookahead(const basic_regex& __exp, bool __invert, unsigned __mexp) {
4181   __end_->first() = new __lookahead<_CharT, _Traits>(__exp, __invert, __end_->first(), __mexp);
4182   __end_          = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4183 }
4184 
4185 // sub_match
4186 
4187 typedef sub_match<const char*> csub_match;
4188 typedef sub_match<string::const_iterator> ssub_match;
4189 #    if _LIBCPP_HAS_WIDE_CHARACTERS
4190 typedef sub_match<const wchar_t*> wcsub_match;
4191 typedef sub_match<wstring::const_iterator> wssub_match;
4192 #    endif
4193 
4194 template <class _BidirectionalIterator>
4195 class _LIBCPP_TEMPLATE_VIS _LIBCPP_PREFERRED_NAME(csub_match)
4196     _LIBCPP_IF_WIDE_CHARACTERS(_LIBCPP_PREFERRED_NAME(wcsub_match)) _LIBCPP_PREFERRED_NAME(ssub_match)
4197         _LIBCPP_IF_WIDE_CHARACTERS(_LIBCPP_PREFERRED_NAME(wssub_match)) sub_match
4198     : public pair<_BidirectionalIterator, _BidirectionalIterator> {
4199 public:
4200   typedef _BidirectionalIterator iterator;
4201   typedef typename iterator_traits<iterator>::value_type value_type;
4202   typedef typename iterator_traits<iterator>::difference_type difference_type;
4203   typedef basic_string<value_type> string_type;
4204 
4205   bool matched;
4206 
4207   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR sub_match() : matched() {}
4208 
4209   _LIBCPP_HIDE_FROM_ABI difference_type length() const {
4210     return matched ? std::distance(this->first, this->second) : 0;
4211   }
4212   _LIBCPP_HIDE_FROM_ABI string_type str() const {
4213     return matched ? string_type(this->first, this->second) : string_type();
4214   }
4215   _LIBCPP_HIDE_FROM_ABI operator string_type() const { return str(); }
4216 
4217   _LIBCPP_HIDE_FROM_ABI int compare(const sub_match& __s) const { return str().compare(__s.str()); }
4218   _LIBCPP_HIDE_FROM_ABI int compare(const string_type& __s) const { return str().compare(__s); }
4219   _LIBCPP_HIDE_FROM_ABI int compare(const value_type* __s) const { return str().compare(__s); }
4220 
4221   _LIBCPP_HIDE_FROM_ABI void swap(sub_match& __s) _NOEXCEPT_(__is_nothrow_swappable_v<_BidirectionalIterator>) {
4222     this->pair<_BidirectionalIterator, _BidirectionalIterator>::swap(__s);
4223     std::swap(matched, __s.matched);
4224   }
4225 };
4226 
4227 template <class _BiIter>
4228 inline _LIBCPP_HIDE_FROM_ABI bool operator==(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y) {
4229   return __x.compare(__y) == 0;
4230 }
4231 
4232 #    if _LIBCPP_STD_VER >= 20
4233 template <class _BiIter>
4234 using __sub_match_cat _LIBCPP_NODEBUG =
4235     compare_three_way_result_t<basic_string<typename iterator_traits<_BiIter>::value_type>>;
4236 
4237 template <class _BiIter>
4238 _LIBCPP_HIDE_FROM_ABI auto operator<=>(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y) {
4239   return static_cast<__sub_match_cat<_BiIter>>(__x.compare(__y) <=> 0);
4240 }
4241 #    else  // _LIBCPP_STD_VER >= 20
4242 template <class _BiIter>
4243 inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y) {
4244   return !(__x == __y);
4245 }
4246 
4247 template <class _BiIter>
4248 inline _LIBCPP_HIDE_FROM_ABI bool operator<(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y) {
4249   return __x.compare(__y) < 0;
4250 }
4251 
4252 template <class _BiIter>
4253 inline _LIBCPP_HIDE_FROM_ABI bool operator<=(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y) {
4254   return !(__y < __x);
4255 }
4256 
4257 template <class _BiIter>
4258 inline _LIBCPP_HIDE_FROM_ABI bool operator>=(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y) {
4259   return !(__x < __y);
4260 }
4261 
4262 template <class _BiIter>
4263 inline _LIBCPP_HIDE_FROM_ABI bool operator>(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y) {
4264   return __y < __x;
4265 }
4266 
4267 template <class _BiIter, class _ST, class _SA>
4268 inline _LIBCPP_HIDE_FROM_ABI bool
4269 operator==(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4270            const sub_match<_BiIter>& __y) {
4271   return __y.compare(typename sub_match<_BiIter>::string_type(__x.data(), __x.size())) == 0;
4272 }
4273 
4274 template <class _BiIter, class _ST, class _SA>
4275 inline _LIBCPP_HIDE_FROM_ABI bool
4276 operator!=(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4277            const sub_match<_BiIter>& __y) {
4278   return !(__x == __y);
4279 }
4280 
4281 template <class _BiIter, class _ST, class _SA>
4282 inline _LIBCPP_HIDE_FROM_ABI bool
4283 operator<(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4284           const sub_match<_BiIter>& __y) {
4285   return __y.compare(typename sub_match<_BiIter>::string_type(__x.data(), __x.size())) > 0;
4286 }
4287 
4288 template <class _BiIter, class _ST, class _SA>
4289 inline _LIBCPP_HIDE_FROM_ABI bool
4290 operator>(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4291           const sub_match<_BiIter>& __y) {
4292   return __y < __x;
4293 }
4294 
4295 template <class _BiIter, class _ST, class _SA>
4296 inline _LIBCPP_HIDE_FROM_ABI bool
4297 operator>=(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4298            const sub_match<_BiIter>& __y) {
4299   return !(__x < __y);
4300 }
4301 
4302 template <class _BiIter, class _ST, class _SA>
4303 inline _LIBCPP_HIDE_FROM_ABI bool
4304 operator<=(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4305            const sub_match<_BiIter>& __y) {
4306   return !(__y < __x);
4307 }
4308 #    endif // _LIBCPP_STD_VER >= 20
4309 
4310 template <class _BiIter, class _ST, class _SA>
4311 inline _LIBCPP_HIDE_FROM_ABI bool
4312 operator==(const sub_match<_BiIter>& __x,
4313            const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y) {
4314   return __x.compare(typename sub_match<_BiIter>::string_type(__y.data(), __y.size())) == 0;
4315 }
4316 
4317 #    if _LIBCPP_STD_VER >= 20
4318 template <class _BiIter, class _ST, class _SA>
4319 _LIBCPP_HIDE_FROM_ABI auto
4320 operator<=>(const sub_match<_BiIter>& __x,
4321             const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y) {
4322   return static_cast<__sub_match_cat<_BiIter>>(
4323       __x.compare(typename sub_match<_BiIter>::string_type(__y.data(), __y.size())) <=> 0);
4324 }
4325 #    else  // _LIBCPP_STD_VER >= 20
4326 template <class _BiIter, class _ST, class _SA>
4327 inline _LIBCPP_HIDE_FROM_ABI bool
4328 operator!=(const sub_match<_BiIter>& __x,
4329            const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y) {
4330   return !(__x == __y);
4331 }
4332 
4333 template <class _BiIter, class _ST, class _SA>
4334 inline _LIBCPP_HIDE_FROM_ABI bool
4335 operator<(const sub_match<_BiIter>& __x,
4336           const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y) {
4337   return __x.compare(typename sub_match<_BiIter>::string_type(__y.data(), __y.size())) < 0;
4338 }
4339 
4340 template <class _BiIter, class _ST, class _SA>
4341 inline _LIBCPP_HIDE_FROM_ABI bool
4342 operator>(const sub_match<_BiIter>& __x,
4343           const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y) {
4344   return __y < __x;
4345 }
4346 
4347 template <class _BiIter, class _ST, class _SA>
4348 inline _LIBCPP_HIDE_FROM_ABI bool
4349 operator>=(const sub_match<_BiIter>& __x,
4350            const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y) {
4351   return !(__x < __y);
4352 }
4353 
4354 template <class _BiIter, class _ST, class _SA>
4355 inline _LIBCPP_HIDE_FROM_ABI bool
4356 operator<=(const sub_match<_BiIter>& __x,
4357            const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y) {
4358   return !(__y < __x);
4359 }
4360 
4361 template <class _BiIter>
4362 inline _LIBCPP_HIDE_FROM_ABI bool
4363 operator==(typename iterator_traits<_BiIter>::value_type const* __x, const sub_match<_BiIter>& __y) {
4364   return __y.compare(__x) == 0;
4365 }
4366 
4367 template <class _BiIter>
4368 inline _LIBCPP_HIDE_FROM_ABI bool
4369 operator!=(typename iterator_traits<_BiIter>::value_type const* __x, const sub_match<_BiIter>& __y) {
4370   return !(__x == __y);
4371 }
4372 
4373 template <class _BiIter>
4374 inline _LIBCPP_HIDE_FROM_ABI bool
4375 operator<(typename iterator_traits<_BiIter>::value_type const* __x, const sub_match<_BiIter>& __y) {
4376   return __y.compare(__x) > 0;
4377 }
4378 
4379 template <class _BiIter>
4380 inline _LIBCPP_HIDE_FROM_ABI bool
4381 operator>(typename iterator_traits<_BiIter>::value_type const* __x, const sub_match<_BiIter>& __y) {
4382   return __y < __x;
4383 }
4384 
4385 template <class _BiIter>
4386 inline _LIBCPP_HIDE_FROM_ABI bool
4387 operator>=(typename iterator_traits<_BiIter>::value_type const* __x, const sub_match<_BiIter>& __y) {
4388   return !(__x < __y);
4389 }
4390 
4391 template <class _BiIter>
4392 inline _LIBCPP_HIDE_FROM_ABI bool
4393 operator<=(typename iterator_traits<_BiIter>::value_type const* __x, const sub_match<_BiIter>& __y) {
4394   return !(__y < __x);
4395 }
4396 #    endif // _LIBCPP_STD_VER >= 20
4397 
4398 template <class _BiIter>
4399 inline _LIBCPP_HIDE_FROM_ABI bool
4400 operator==(const sub_match<_BiIter>& __x, typename iterator_traits<_BiIter>::value_type const* __y) {
4401   return __x.compare(__y) == 0;
4402 }
4403 
4404 #    if _LIBCPP_STD_VER >= 20
4405 template <class _BiIter>
4406 _LIBCPP_HIDE_FROM_ABI auto
4407 operator<=>(const sub_match<_BiIter>& __x, typename iterator_traits<_BiIter>::value_type const* __y) {
4408   return static_cast<__sub_match_cat<_BiIter>>(__x.compare(__y) <=> 0);
4409 }
4410 #    else  // _LIBCPP_STD_VER >= 20
4411 template <class _BiIter>
4412 inline _LIBCPP_HIDE_FROM_ABI bool
4413 operator!=(const sub_match<_BiIter>& __x, typename iterator_traits<_BiIter>::value_type const* __y) {
4414   return !(__x == __y);
4415 }
4416 
4417 template <class _BiIter>
4418 inline _LIBCPP_HIDE_FROM_ABI bool
4419 operator<(const sub_match<_BiIter>& __x, typename iterator_traits<_BiIter>::value_type const* __y) {
4420   return __x.compare(__y) < 0;
4421 }
4422 
4423 template <class _BiIter>
4424 inline _LIBCPP_HIDE_FROM_ABI bool
4425 operator>(const sub_match<_BiIter>& __x, typename iterator_traits<_BiIter>::value_type const* __y) {
4426   return __y < __x;
4427 }
4428 
4429 template <class _BiIter>
4430 inline _LIBCPP_HIDE_FROM_ABI bool
4431 operator>=(const sub_match<_BiIter>& __x, typename iterator_traits<_BiIter>::value_type const* __y) {
4432   return !(__x < __y);
4433 }
4434 
4435 template <class _BiIter>
4436 inline _LIBCPP_HIDE_FROM_ABI bool
4437 operator<=(const sub_match<_BiIter>& __x, typename iterator_traits<_BiIter>::value_type const* __y) {
4438   return !(__y < __x);
4439 }
4440 
4441 template <class _BiIter>
4442 inline _LIBCPP_HIDE_FROM_ABI bool
4443 operator==(typename iterator_traits<_BiIter>::value_type const& __x, const sub_match<_BiIter>& __y) {
4444   typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
4445   return __y.compare(string_type(1, __x)) == 0;
4446 }
4447 
4448 template <class _BiIter>
4449 inline _LIBCPP_HIDE_FROM_ABI bool
4450 operator!=(typename iterator_traits<_BiIter>::value_type const& __x, const sub_match<_BiIter>& __y) {
4451   return !(__x == __y);
4452 }
4453 
4454 template <class _BiIter>
4455 inline _LIBCPP_HIDE_FROM_ABI bool
4456 operator<(typename iterator_traits<_BiIter>::value_type const& __x, const sub_match<_BiIter>& __y) {
4457   typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
4458   return __y.compare(string_type(1, __x)) > 0;
4459 }
4460 
4461 template <class _BiIter>
4462 inline _LIBCPP_HIDE_FROM_ABI bool
4463 operator>(typename iterator_traits<_BiIter>::value_type const& __x, const sub_match<_BiIter>& __y) {
4464   return __y < __x;
4465 }
4466 
4467 template <class _BiIter>
4468 inline _LIBCPP_HIDE_FROM_ABI bool
4469 operator>=(typename iterator_traits<_BiIter>::value_type const& __x, const sub_match<_BiIter>& __y) {
4470   return !(__x < __y);
4471 }
4472 
4473 template <class _BiIter>
4474 inline _LIBCPP_HIDE_FROM_ABI bool
4475 operator<=(typename iterator_traits<_BiIter>::value_type const& __x, const sub_match<_BiIter>& __y) {
4476   return !(__y < __x);
4477 }
4478 #    endif // _LIBCPP_STD_VER >= 20
4479 
4480 template <class _BiIter>
4481 inline _LIBCPP_HIDE_FROM_ABI bool
4482 operator==(const sub_match<_BiIter>& __x, typename iterator_traits<_BiIter>::value_type const& __y) {
4483   typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
4484   return __x.compare(string_type(1, __y)) == 0;
4485 }
4486 
4487 #    if _LIBCPP_STD_VER >= 20
4488 template <class _BiIter>
4489 _LIBCPP_HIDE_FROM_ABI auto
4490 operator<=>(const sub_match<_BiIter>& __x, typename iterator_traits<_BiIter>::value_type const& __y) {
4491   using string_type = basic_string<typename iterator_traits<_BiIter>::value_type>;
4492   return static_cast<__sub_match_cat<_BiIter>>(__x.compare(string_type(1, __y)) <=> 0);
4493 }
4494 #    else  // _LIBCPP_STD_VER >= 20
4495 template <class _BiIter>
4496 inline _LIBCPP_HIDE_FROM_ABI bool
4497 operator!=(const sub_match<_BiIter>& __x, typename iterator_traits<_BiIter>::value_type const& __y) {
4498   return !(__x == __y);
4499 }
4500 
4501 template <class _BiIter>
4502 inline _LIBCPP_HIDE_FROM_ABI bool
4503 operator<(const sub_match<_BiIter>& __x, typename iterator_traits<_BiIter>::value_type const& __y) {
4504   typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
4505   return __x.compare(string_type(1, __y)) < 0;
4506 }
4507 
4508 template <class _BiIter>
4509 inline _LIBCPP_HIDE_FROM_ABI bool
4510 operator>(const sub_match<_BiIter>& __x, typename iterator_traits<_BiIter>::value_type const& __y) {
4511   return __y < __x;
4512 }
4513 
4514 template <class _BiIter>
4515 inline _LIBCPP_HIDE_FROM_ABI bool
4516 operator>=(const sub_match<_BiIter>& __x, typename iterator_traits<_BiIter>::value_type const& __y) {
4517   return !(__x < __y);
4518 }
4519 
4520 template <class _BiIter>
4521 inline _LIBCPP_HIDE_FROM_ABI bool
4522 operator<=(const sub_match<_BiIter>& __x, typename iterator_traits<_BiIter>::value_type const& __y) {
4523   return !(__y < __x);
4524 }
4525 #    endif // _LIBCPP_STD_VER >= 20
4526 
4527 template <class _CharT, class _ST, class _BiIter>
4528 inline _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _ST>&
4529 operator<<(basic_ostream<_CharT, _ST>& __os, const sub_match<_BiIter>& __m) {
4530   return __os << __m.str();
4531 }
4532 
4533 typedef match_results<const char*> cmatch;
4534 typedef match_results<string::const_iterator> smatch;
4535 #    if _LIBCPP_HAS_WIDE_CHARACTERS
4536 typedef match_results<const wchar_t*> wcmatch;
4537 typedef match_results<wstring::const_iterator> wsmatch;
4538 #    endif
4539 
4540 template <class _BidirectionalIterator, class _Allocator>
4541 class _LIBCPP_TEMPLATE_VIS _LIBCPP_PREFERRED_NAME(cmatch) _LIBCPP_IF_WIDE_CHARACTERS(_LIBCPP_PREFERRED_NAME(wcmatch))
4542     _LIBCPP_PREFERRED_NAME(smatch) _LIBCPP_IF_WIDE_CHARACTERS(_LIBCPP_PREFERRED_NAME(wsmatch)) match_results {
4543 public:
4544   typedef _Allocator allocator_type;
4545   typedef sub_match<_BidirectionalIterator> value_type;
4546 
4547 private:
4548   typedef vector<value_type, allocator_type> __container_type;
4549 
4550   __container_type __matches_;
4551   value_type __unmatched_;
4552   value_type __prefix_;
4553   value_type __suffix_;
4554   bool __ready_;
4555 
4556 public:
4557   _BidirectionalIterator __position_start_;
4558   typedef const value_type& const_reference;
4559   typedef value_type& reference;
4560   typedef typename __container_type::const_iterator const_iterator;
4561   typedef const_iterator iterator;
4562   typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
4563   typedef typename allocator_traits<allocator_type>::size_type size_type;
4564   typedef typename iterator_traits<_BidirectionalIterator>::value_type char_type;
4565   typedef basic_string<char_type> string_type;
4566 
4567   // construct/copy/destroy:
4568 #    ifndef _LIBCPP_CXX03_LANG
4569   match_results() : match_results(allocator_type()) {}
4570   explicit match_results(const allocator_type& __a);
4571 #    else
4572   explicit match_results(const allocator_type& __a = allocator_type());
4573 #    endif
4574 
4575   //    match_results(const match_results&) = default;
4576   //    match_results& operator=(const match_results&) = default;
4577   //    match_results(match_results&& __m) = default;
4578   //    match_results& operator=(match_results&& __m) = default;
4579   //    ~match_results() = default;
4580 
4581   _LIBCPP_HIDE_FROM_ABI bool ready() const { return __ready_; }
4582 
4583   // size:
4584   _LIBCPP_HIDE_FROM_ABI size_type size() const _NOEXCEPT { return __matches_.size(); }
4585   _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT { return __matches_.max_size(); }
4586   [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool empty() const _NOEXCEPT { return size() == 0; }
4587 
4588   // element access:
4589   _LIBCPP_HIDE_FROM_ABI difference_type length(size_type __sub = 0) const {
4590     // If the match results are not ready, this will return `0`.
4591     _LIBCPP_ASSERT_PEDANTIC(ready(), "match_results::length() called when not ready");
4592     return (*this)[__sub].length();
4593   }
4594   _LIBCPP_HIDE_FROM_ABI difference_type position(size_type __sub = 0) const {
4595     // If the match results are not ready, this will return the result of subtracting two default-constructed iterators
4596     // (which is typically a well-defined operation).
4597     _LIBCPP_ASSERT_PEDANTIC(ready(), "match_results::position() called when not ready");
4598     return std::distance(__position_start_, (*this)[__sub].first);
4599   }
4600   _LIBCPP_HIDE_FROM_ABI string_type str(size_type __sub = 0) const {
4601     // If the match results are not ready, this will return an empty string.
4602     _LIBCPP_ASSERT_PEDANTIC(ready(), "match_results::str() called when not ready");
4603     return (*this)[__sub].str();
4604   }
4605   _LIBCPP_HIDE_FROM_ABI const_reference operator[](size_type __n) const {
4606     // If the match results are not ready, this call will be equivalent to calling this function with `__n >= size()`,
4607     // returning an empty subrange.
4608     _LIBCPP_ASSERT_PEDANTIC(ready(), "match_results::operator[]() called when not ready");
4609     return __n < __matches_.size() ? __matches_[__n] : __unmatched_;
4610   }
4611 
4612   _LIBCPP_HIDE_FROM_ABI const_reference prefix() const {
4613     // If the match results are not ready, this will return a default-constructed empty `__suffix_`.
4614     _LIBCPP_ASSERT_PEDANTIC(ready(), "match_results::prefix() called when not ready");
4615     return __prefix_;
4616   }
4617   _LIBCPP_HIDE_FROM_ABI const_reference suffix() const {
4618     // If the match results are not ready, this will return a default-constructed empty `__suffix_`.
4619     _LIBCPP_ASSERT_PEDANTIC(ready(), "match_results::suffix() called when not ready");
4620     return __suffix_;
4621   }
4622 
4623   _LIBCPP_HIDE_FROM_ABI const_iterator begin() const { return empty() ? __matches_.end() : __matches_.begin(); }
4624   _LIBCPP_HIDE_FROM_ABI const_iterator end() const { return __matches_.end(); }
4625   _LIBCPP_HIDE_FROM_ABI const_iterator cbegin() const { return empty() ? __matches_.end() : __matches_.begin(); }
4626   _LIBCPP_HIDE_FROM_ABI const_iterator cend() const { return __matches_.end(); }
4627 
4628   // format:
4629   template <class _OutputIter>
4630   _OutputIter format(_OutputIter __output_iter,
4631                      const char_type* __fmt_first,
4632                      const char_type* __fmt_last,
4633                      regex_constants::match_flag_type __flags = regex_constants::format_default) const;
4634   template <class _OutputIter, class _ST, class _SA>
4635   _LIBCPP_HIDE_FROM_ABI _OutputIter
4636   format(_OutputIter __output_iter,
4637          const basic_string<char_type, _ST, _SA>& __fmt,
4638          regex_constants::match_flag_type __flags = regex_constants::format_default) const {
4639     return format(__output_iter, __fmt.data(), __fmt.data() + __fmt.size(), __flags);
4640   }
4641   template <class _ST, class _SA>
4642   _LIBCPP_HIDE_FROM_ABI basic_string<char_type, _ST, _SA>
4643   format(const basic_string<char_type, _ST, _SA>& __fmt,
4644          regex_constants::match_flag_type __flags = regex_constants::format_default) const {
4645     basic_string<char_type, _ST, _SA> __r;
4646     format(std::back_inserter(__r), __fmt.data(), __fmt.data() + __fmt.size(), __flags);
4647     return __r;
4648   }
4649   _LIBCPP_HIDE_FROM_ABI string_type
4650   format(const char_type* __fmt, regex_constants::match_flag_type __flags = regex_constants::format_default) const {
4651     string_type __r;
4652     format(std::back_inserter(__r), __fmt, __fmt + char_traits<char_type>::length(__fmt), __flags);
4653     return __r;
4654   }
4655 
4656   // allocator:
4657   _LIBCPP_HIDE_FROM_ABI allocator_type get_allocator() const { return __matches_.get_allocator(); }
4658 
4659   // swap:
4660   void swap(match_results& __m);
4661 
4662   template <class _Bp, class _Ap>
4663   _LIBCPP_HIDE_FROM_ABI void
4664   __assign(_BidirectionalIterator __f,
4665            _BidirectionalIterator __l,
4666            const match_results<_Bp, _Ap>& __m,
4667            bool __no_update_pos) {
4668     _Bp __mf = __m.prefix().first;
4669     __matches_.resize(__m.size());
4670     for (size_type __i = 0; __i < __matches_.size(); ++__i) {
4671       __matches_[__i].first   = std::next(__f, std::distance(__mf, __m[__i].first));
4672       __matches_[__i].second  = std::next(__f, std::distance(__mf, __m[__i].second));
4673       __matches_[__i].matched = __m[__i].matched;
4674     }
4675     __unmatched_.first   = __l;
4676     __unmatched_.second  = __l;
4677     __unmatched_.matched = false;
4678     __prefix_.first      = std::next(__f, std::distance(__mf, __m.prefix().first));
4679     __prefix_.second     = std::next(__f, std::distance(__mf, __m.prefix().second));
4680     __prefix_.matched    = __m.prefix().matched;
4681     __suffix_.first      = std::next(__f, std::distance(__mf, __m.suffix().first));
4682     __suffix_.second     = std::next(__f, std::distance(__mf, __m.suffix().second));
4683     __suffix_.matched    = __m.suffix().matched;
4684     if (!__no_update_pos)
4685       __position_start_ = __prefix_.first;
4686     __ready_ = __m.ready();
4687   }
4688 
4689 private:
4690   void __init(unsigned __s, _BidirectionalIterator __f, _BidirectionalIterator __l, bool __no_update_pos = false);
4691 
4692   template <class, class>
4693   friend class basic_regex;
4694 
4695   template <class _Bp, class _Ap, class _Cp, class _Tp>
4696   friend bool
4697   regex_match(_Bp, _Bp, match_results<_Bp, _Ap>&, const basic_regex<_Cp, _Tp>&, regex_constants::match_flag_type);
4698 
4699   template <class _Bp, class _Ap>
4700   friend bool operator==(const match_results<_Bp, _Ap>&, const match_results<_Bp, _Ap>&);
4701 
4702   template <class, class>
4703   friend class __lookahead;
4704 
4705   template <class, class, class>
4706   friend class regex_iterator;
4707 };
4708 
4709 template <class _BidirectionalIterator, class _Allocator>
4710 match_results<_BidirectionalIterator, _Allocator>::match_results(const allocator_type& __a)
4711     : __matches_(__a), __unmatched_(), __prefix_(), __suffix_(), __ready_(false), __position_start_() {}
4712 
4713 template <class _BidirectionalIterator, class _Allocator>
4714 void match_results<_BidirectionalIterator, _Allocator>::__init(
4715     unsigned __s, _BidirectionalIterator __f, _BidirectionalIterator __l, bool __no_update_pos) {
4716   __unmatched_.first   = __l;
4717   __unmatched_.second  = __l;
4718   __unmatched_.matched = false;
4719   __matches_.assign(__s, __unmatched_);
4720   __prefix_.first   = __f;
4721   __prefix_.second  = __f;
4722   __prefix_.matched = false;
4723   __suffix_         = __unmatched_;
4724   if (!__no_update_pos)
4725     __position_start_ = __prefix_.first;
4726   __ready_ = true;
4727 }
4728 
4729 template <class _BidirectionalIterator, class _Allocator>
4730 template <class _OutputIter>
4731 _OutputIter match_results<_BidirectionalIterator, _Allocator>::format(
4732     _OutputIter __output_iter,
4733     const char_type* __fmt_first,
4734     const char_type* __fmt_last,
4735     regex_constants::match_flag_type __flags) const {
4736   // Note: this duplicates a check in `vector::operator[]` but provides a better error message.
4737   _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(ready(), "match_results::format() called when not ready");
4738   if (__flags & regex_constants::format_sed) {
4739     for (; __fmt_first != __fmt_last; ++__fmt_first) {
4740       if (*__fmt_first == '&')
4741         __output_iter = std::copy(__matches_[0].first, __matches_[0].second, __output_iter);
4742       else if (*__fmt_first == '\\' && __fmt_first + 1 != __fmt_last) {
4743         ++__fmt_first;
4744         if ('0' <= *__fmt_first && *__fmt_first <= '9') {
4745           size_t __i    = *__fmt_first - '0';
4746           __output_iter = std::copy((*this)[__i].first, (*this)[__i].second, __output_iter);
4747         } else {
4748           *__output_iter = *__fmt_first;
4749           ++__output_iter;
4750         }
4751       } else {
4752         *__output_iter = *__fmt_first;
4753         ++__output_iter;
4754       }
4755     }
4756   } else {
4757     for (; __fmt_first != __fmt_last; ++__fmt_first) {
4758       if (*__fmt_first == '$' && __fmt_first + 1 != __fmt_last) {
4759         switch (__fmt_first[1]) {
4760         case '$':
4761           *__output_iter = *++__fmt_first;
4762           ++__output_iter;
4763           break;
4764         case '&':
4765           ++__fmt_first;
4766           __output_iter = std::copy(__matches_[0].first, __matches_[0].second, __output_iter);
4767           break;
4768         case '`':
4769           ++__fmt_first;
4770           __output_iter = std::copy(__prefix_.first, __prefix_.second, __output_iter);
4771           break;
4772         case '\'':
4773           ++__fmt_first;
4774           __output_iter = std::copy(__suffix_.first, __suffix_.second, __output_iter);
4775           break;
4776         default:
4777           if ('0' <= __fmt_first[1] && __fmt_first[1] <= '9') {
4778             ++__fmt_first;
4779             size_t __idx = *__fmt_first - '0';
4780             if (__fmt_first + 1 != __fmt_last && '0' <= __fmt_first[1] && __fmt_first[1] <= '9') {
4781               ++__fmt_first;
4782               if (__idx >= numeric_limits<size_t>::max() / 10)
4783                 __throw_regex_error<regex_constants::error_escape>();
4784               __idx = 10 * __idx + *__fmt_first - '0';
4785             }
4786             __output_iter = std::copy((*this)[__idx].first, (*this)[__idx].second, __output_iter);
4787           } else {
4788             *__output_iter = *__fmt_first;
4789             ++__output_iter;
4790           }
4791           break;
4792         }
4793       } else {
4794         *__output_iter = *__fmt_first;
4795         ++__output_iter;
4796       }
4797     }
4798   }
4799   return __output_iter;
4800 }
4801 
4802 template <class _BidirectionalIterator, class _Allocator>
4803 void match_results<_BidirectionalIterator, _Allocator>::swap(match_results& __m) {
4804   using std::swap;
4805   swap(__matches_, __m.__matches_);
4806   swap(__unmatched_, __m.__unmatched_);
4807   swap(__prefix_, __m.__prefix_);
4808   swap(__suffix_, __m.__suffix_);
4809   swap(__position_start_, __m.__position_start_);
4810   swap(__ready_, __m.__ready_);
4811 }
4812 
4813 template <class _BidirectionalIterator, class _Allocator>
4814 _LIBCPP_HIDE_FROM_ABI bool operator==(const match_results<_BidirectionalIterator, _Allocator>& __x,
4815                                       const match_results<_BidirectionalIterator, _Allocator>& __y) {
4816   if (__x.__ready_ != __y.__ready_)
4817     return false;
4818   if (!__x.__ready_)
4819     return true;
4820   return __x.__matches_ == __y.__matches_ && __x.__prefix_ == __y.__prefix_ && __x.__suffix_ == __y.__suffix_;
4821 }
4822 
4823 #    if _LIBCPP_STD_VER < 20
4824 template <class _BidirectionalIterator, class _Allocator>
4825 inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const match_results<_BidirectionalIterator, _Allocator>& __x,
4826                                              const match_results<_BidirectionalIterator, _Allocator>& __y) {
4827   return !(__x == __y);
4828 }
4829 #    endif
4830 
4831 template <class _BidirectionalIterator, class _Allocator>
4832 inline _LIBCPP_HIDE_FROM_ABI void
4833 swap(match_results<_BidirectionalIterator, _Allocator>& __x, match_results<_BidirectionalIterator, _Allocator>& __y) {
4834   __x.swap(__y);
4835 }
4836 
4837 // regex_search
4838 
4839 template <class _CharT, class _Traits>
4840 template <class _Allocator>
4841 bool basic_regex<_CharT, _Traits>::__match_at_start_ecma(
4842     const _CharT* __first,
4843     const _CharT* __last,
4844     match_results<const _CharT*, _Allocator>& __m,
4845     regex_constants::match_flag_type __flags,
4846     bool __at_first) const {
4847   vector<__state> __states;
4848   __node* __st = __start_.get();
4849   if (__st) {
4850     sub_match<const _CharT*> __unmatched;
4851     __unmatched.first   = __last;
4852     __unmatched.second  = __last;
4853     __unmatched.matched = false;
4854 
4855     __states.push_back(__state());
4856     __states.back().__do_      = 0;
4857     __states.back().__first_   = __first;
4858     __states.back().__current_ = __first;
4859     __states.back().__last_    = __last;
4860     __states.back().__sub_matches_.resize(mark_count(), __unmatched);
4861     __states.back().__loop_data_.resize(__loop_count());
4862     __states.back().__node_     = __st;
4863     __states.back().__flags_    = __flags;
4864     __states.back().__at_first_ = __at_first;
4865     int __counter               = 0;
4866     int __length                = __last - __first;
4867     do {
4868       ++__counter;
4869       if (__counter % _LIBCPP_REGEX_COMPLEXITY_FACTOR == 0 && __counter / _LIBCPP_REGEX_COMPLEXITY_FACTOR >= __length)
4870         __throw_regex_error<regex_constants::error_complexity>();
4871       __state& __s = __states.back();
4872       if (__s.__node_)
4873         __s.__node_->__exec(__s);
4874       switch (__s.__do_) {
4875       case __state::__end_state:
4876         if ((__flags & regex_constants::match_not_null) && __s.__current_ == __first) {
4877           __states.pop_back();
4878           break;
4879         }
4880         if ((__flags & regex_constants::__full_match) && __s.__current_ != __last) {
4881           __states.pop_back();
4882           break;
4883         }
4884         __m.__matches_[0].first   = __first;
4885         __m.__matches_[0].second  = std::next(__first, __s.__current_ - __first);
4886         __m.__matches_[0].matched = true;
4887         for (unsigned __i = 0; __i < __s.__sub_matches_.size(); ++__i)
4888           __m.__matches_[__i + 1] = __s.__sub_matches_[__i];
4889         return true;
4890       case __state::__accept_and_consume:
4891       case __state::__repeat:
4892       case __state::__accept_but_not_consume:
4893         break;
4894       case __state::__split: {
4895         __state __snext = __s;
4896         __s.__node_->__exec_split(true, __s);
4897         __snext.__node_->__exec_split(false, __snext);
4898         __states.push_back(std::move(__snext));
4899       } break;
4900       case __state::__reject:
4901         __states.pop_back();
4902         break;
4903       default:
4904         __throw_regex_error<regex_constants::__re_err_unknown>();
4905         break;
4906       }
4907     } while (!__states.empty());
4908   }
4909   return false;
4910 }
4911 
4912 template <class _CharT, class _Traits>
4913 template <class _Allocator>
4914 bool basic_regex<_CharT, _Traits>::__match_at_start_posix_nosubs(
4915     const _CharT* __first,
4916     const _CharT* __last,
4917     match_results<const _CharT*, _Allocator>& __m,
4918     regex_constants::match_flag_type __flags,
4919     bool __at_first) const {
4920   deque<__state> __states;
4921   ptrdiff_t __highest_j = 0;
4922   ptrdiff_t __np        = std::distance(__first, __last);
4923   __node* __st          = __start_.get();
4924   if (__st) {
4925     __states.push_back(__state());
4926     __states.back().__do_      = 0;
4927     __states.back().__first_   = __first;
4928     __states.back().__current_ = __first;
4929     __states.back().__last_    = __last;
4930     __states.back().__loop_data_.resize(__loop_count());
4931     __states.back().__node_     = __st;
4932     __states.back().__flags_    = __flags;
4933     __states.back().__at_first_ = __at_first;
4934     bool __matched              = false;
4935     int __counter               = 0;
4936     int __length                = __last - __first;
4937     do {
4938       ++__counter;
4939       if (__counter % _LIBCPP_REGEX_COMPLEXITY_FACTOR == 0 && __counter / _LIBCPP_REGEX_COMPLEXITY_FACTOR >= __length)
4940         __throw_regex_error<regex_constants::error_complexity>();
4941       __state& __s = __states.back();
4942       if (__s.__node_)
4943         __s.__node_->__exec(__s);
4944       switch (__s.__do_) {
4945       case __state::__end_state:
4946         if ((__flags & regex_constants::match_not_null) && __s.__current_ == __first) {
4947           __states.pop_back();
4948           break;
4949         }
4950         if ((__flags & regex_constants::__full_match) && __s.__current_ != __last) {
4951           __states.pop_back();
4952           break;
4953         }
4954         if (!__matched || __highest_j < __s.__current_ - __s.__first_)
4955           __highest_j = __s.__current_ - __s.__first_;
4956         __matched = true;
4957         if (__highest_j == __np)
4958           __states.clear();
4959         else
4960           __states.pop_back();
4961         break;
4962       case __state::__consume_input:
4963         break;
4964       case __state::__accept_and_consume:
4965         __states.push_front(std::move(__s));
4966         __states.pop_back();
4967         break;
4968       case __state::__repeat:
4969       case __state::__accept_but_not_consume:
4970         break;
4971       case __state::__split: {
4972         __state __snext = __s;
4973         __s.__node_->__exec_split(true, __s);
4974         __snext.__node_->__exec_split(false, __snext);
4975         __states.push_back(std::move(__snext));
4976       } break;
4977       case __state::__reject:
4978         __states.pop_back();
4979         break;
4980       default:
4981         __throw_regex_error<regex_constants::__re_err_unknown>();
4982         break;
4983       }
4984     } while (!__states.empty());
4985     if (__matched) {
4986       __m.__matches_[0].first   = __first;
4987       __m.__matches_[0].second  = std::next(__first, __highest_j);
4988       __m.__matches_[0].matched = true;
4989       return true;
4990     }
4991   }
4992   return false;
4993 }
4994 
4995 template <class _CharT, class _Traits>
4996 template <class _Allocator>
4997 bool basic_regex<_CharT, _Traits>::__match_at_start_posix_subs(
4998     const _CharT* __first,
4999     const _CharT* __last,
5000     match_results<const _CharT*, _Allocator>& __m,
5001     regex_constants::match_flag_type __flags,
5002     bool __at_first) const {
5003   vector<__state> __states;
5004   __state __best_state;
5005   ptrdiff_t __highest_j = 0;
5006   ptrdiff_t __np        = std::distance(__first, __last);
5007   __node* __st          = __start_.get();
5008   if (__st) {
5009     sub_match<const _CharT*> __unmatched;
5010     __unmatched.first   = __last;
5011     __unmatched.second  = __last;
5012     __unmatched.matched = false;
5013 
5014     __states.push_back(__state());
5015     __states.back().__do_      = 0;
5016     __states.back().__first_   = __first;
5017     __states.back().__current_ = __first;
5018     __states.back().__last_    = __last;
5019     __states.back().__sub_matches_.resize(mark_count(), __unmatched);
5020     __states.back().__loop_data_.resize(__loop_count());
5021     __states.back().__node_     = __st;
5022     __states.back().__flags_    = __flags;
5023     __states.back().__at_first_ = __at_first;
5024     bool __matched              = false;
5025     int __counter               = 0;
5026     int __length                = __last - __first;
5027     do {
5028       ++__counter;
5029       if (__counter % _LIBCPP_REGEX_COMPLEXITY_FACTOR == 0 && __counter / _LIBCPP_REGEX_COMPLEXITY_FACTOR >= __length)
5030         __throw_regex_error<regex_constants::error_complexity>();
5031       __state& __s = __states.back();
5032       if (__s.__node_)
5033         __s.__node_->__exec(__s);
5034       switch (__s.__do_) {
5035       case __state::__end_state:
5036         if ((__flags & regex_constants::match_not_null) && __s.__current_ == __first) {
5037           __states.pop_back();
5038           break;
5039         }
5040         if ((__flags & regex_constants::__full_match) && __s.__current_ != __last) {
5041           __states.pop_back();
5042           break;
5043         }
5044         if (!__matched || __highest_j < __s.__current_ - __s.__first_) {
5045           __highest_j  = __s.__current_ - __s.__first_;
5046           __best_state = __s;
5047         }
5048         __matched = true;
5049         if (__highest_j == __np)
5050           __states.clear();
5051         else
5052           __states.pop_back();
5053         break;
5054       case __state::__accept_and_consume:
5055       case __state::__repeat:
5056       case __state::__accept_but_not_consume:
5057         break;
5058       case __state::__split: {
5059         __state __snext = __s;
5060         __s.__node_->__exec_split(true, __s);
5061         __snext.__node_->__exec_split(false, __snext);
5062         __states.push_back(std::move(__snext));
5063       } break;
5064       case __state::__reject:
5065         __states.pop_back();
5066         break;
5067       default:
5068         __throw_regex_error<regex_constants::__re_err_unknown>();
5069         break;
5070       }
5071     } while (!__states.empty());
5072     if (__matched) {
5073       __m.__matches_[0].first   = __first;
5074       __m.__matches_[0].second  = std::next(__first, __highest_j);
5075       __m.__matches_[0].matched = true;
5076       for (unsigned __i = 0; __i < __best_state.__sub_matches_.size(); ++__i)
5077         __m.__matches_[__i + 1] = __best_state.__sub_matches_[__i];
5078       return true;
5079     }
5080   }
5081   return false;
5082 }
5083 
5084 template <class _CharT, class _Traits>
5085 template <class _Allocator>
5086 bool basic_regex<_CharT, _Traits>::__match_at_start(
5087     const _CharT* __first,
5088     const _CharT* __last,
5089     match_results<const _CharT*, _Allocator>& __m,
5090     regex_constants::match_flag_type __flags,
5091     bool __at_first) const {
5092   if (__get_grammar(__flags_) == ECMAScript)
5093     return __match_at_start_ecma(__first, __last, __m, __flags, __at_first);
5094   if (mark_count() == 0)
5095     return __match_at_start_posix_nosubs(__first, __last, __m, __flags, __at_first);
5096   return __match_at_start_posix_subs(__first, __last, __m, __flags, __at_first);
5097 }
5098 
5099 template <class _CharT, class _Traits>
5100 template <class _Allocator>
5101 bool basic_regex<_CharT, _Traits>::__search(
5102     const _CharT* __first,
5103     const _CharT* __last,
5104     match_results<const _CharT*, _Allocator>& __m,
5105     regex_constants::match_flag_type __flags) const {
5106   if (__flags & regex_constants::match_prev_avail)
5107     __flags &= ~(regex_constants::match_not_bol | regex_constants::match_not_bow);
5108 
5109   __m.__init(1 + mark_count(), __first, __last, __flags & regex_constants::__no_update_pos);
5110   if (__match_at_start(__first, __last, __m, __flags, !(__flags & regex_constants::__no_update_pos))) {
5111     __m.__prefix_.second  = __m[0].first;
5112     __m.__prefix_.matched = __m.__prefix_.first != __m.__prefix_.second;
5113     __m.__suffix_.first   = __m[0].second;
5114     __m.__suffix_.matched = __m.__suffix_.first != __m.__suffix_.second;
5115     return true;
5116   }
5117   if (__first != __last && !(__flags & regex_constants::match_continuous)) {
5118     __flags |= regex_constants::match_prev_avail;
5119     for (++__first; __first != __last; ++__first) {
5120       __m.__matches_.assign(__m.size(), __m.__unmatched_);
5121       if (__match_at_start(__first, __last, __m, __flags, false)) {
5122         __m.__prefix_.second  = __m[0].first;
5123         __m.__prefix_.matched = __m.__prefix_.first != __m.__prefix_.second;
5124         __m.__suffix_.first   = __m[0].second;
5125         __m.__suffix_.matched = __m.__suffix_.first != __m.__suffix_.second;
5126         return true;
5127       }
5128       __m.__matches_.assign(__m.size(), __m.__unmatched_);
5129     }
5130     __m.__matches_.assign(__m.size(), __m.__unmatched_);
5131     if (__match_at_start(__first, __last, __m, __flags, false)) {
5132       __m.__prefix_.second  = __m[0].first;
5133       __m.__prefix_.matched = __m.__prefix_.first != __m.__prefix_.second;
5134       __m.__suffix_.first   = __m[0].second;
5135       __m.__suffix_.matched = __m.__suffix_.first != __m.__suffix_.second;
5136       return true;
5137     }
5138   }
5139   __m.__matches_.clear();
5140   return false;
5141 }
5142 
5143 template <class _BidirectionalIterator, class _Allocator, class _CharT, class _Traits>
5144 inline _LIBCPP_HIDE_FROM_ABI bool
5145 regex_search(_BidirectionalIterator __first,
5146              _BidirectionalIterator __last,
5147              match_results<_BidirectionalIterator, _Allocator>& __m,
5148              const basic_regex<_CharT, _Traits>& __e,
5149              regex_constants::match_flag_type __flags = regex_constants::match_default) {
5150   int __offset = (__flags & regex_constants::match_prev_avail) ? 1 : 0;
5151   basic_string<_CharT> __s(std::prev(__first, __offset), __last);
5152   match_results<const _CharT*> __mc;
5153   bool __r = __e.__search(__s.data() + __offset, __s.data() + __s.size(), __mc, __flags);
5154   __m.__assign(__first, __last, __mc, __flags & regex_constants::__no_update_pos);
5155   return __r;
5156 }
5157 
5158 template <class _Iter, class _Allocator, class _CharT, class _Traits>
5159 inline _LIBCPP_HIDE_FROM_ABI bool
5160 regex_search(__wrap_iter<_Iter> __first,
5161              __wrap_iter<_Iter> __last,
5162              match_results<__wrap_iter<_Iter>, _Allocator>& __m,
5163              const basic_regex<_CharT, _Traits>& __e,
5164              regex_constants::match_flag_type __flags = regex_constants::match_default) {
5165   match_results<const _CharT*> __mc;
5166   bool __r = __e.__search(__first.base(), __last.base(), __mc, __flags);
5167   __m.__assign(__first, __last, __mc, __flags & regex_constants::__no_update_pos);
5168   return __r;
5169 }
5170 
5171 template <class _Allocator, class _CharT, class _Traits>
5172 inline _LIBCPP_HIDE_FROM_ABI bool
5173 regex_search(const _CharT* __first,
5174              const _CharT* __last,
5175              match_results<const _CharT*, _Allocator>& __m,
5176              const basic_regex<_CharT, _Traits>& __e,
5177              regex_constants::match_flag_type __flags = regex_constants::match_default) {
5178   return __e.__search(__first, __last, __m, __flags);
5179 }
5180 
5181 template <class _BidirectionalIterator, class _CharT, class _Traits>
5182 inline _LIBCPP_HIDE_FROM_ABI bool
5183 regex_search(_BidirectionalIterator __first,
5184              _BidirectionalIterator __last,
5185              const basic_regex<_CharT, _Traits>& __e,
5186              regex_constants::match_flag_type __flags = regex_constants::match_default) {
5187   basic_string<_CharT> __s(__first, __last);
5188   match_results<const _CharT*> __mc;
5189   return __e.__search(__s.data(), __s.data() + __s.size(), __mc, __flags);
5190 }
5191 
5192 template <class _CharT, class _Traits>
5193 inline _LIBCPP_HIDE_FROM_ABI bool
5194 regex_search(const _CharT* __first,
5195              const _CharT* __last,
5196              const basic_regex<_CharT, _Traits>& __e,
5197              regex_constants::match_flag_type __flags = regex_constants::match_default) {
5198   match_results<const _CharT*> __mc;
5199   return __e.__search(__first, __last, __mc, __flags);
5200 }
5201 
5202 template <class _CharT, class _Allocator, class _Traits>
5203 inline _LIBCPP_HIDE_FROM_ABI bool
5204 regex_search(const _CharT* __str,
5205              match_results<const _CharT*, _Allocator>& __m,
5206              const basic_regex<_CharT, _Traits>& __e,
5207              regex_constants::match_flag_type __flags = regex_constants::match_default) {
5208   return __e.__search(__str, __str + _Traits::length(__str), __m, __flags);
5209 }
5210 
5211 template <class _CharT, class _Traits>
5212 inline _LIBCPP_HIDE_FROM_ABI bool
5213 regex_search(const _CharT* __str,
5214              const basic_regex<_CharT, _Traits>& __e,
5215              regex_constants::match_flag_type __flags = regex_constants::match_default) {
5216   match_results<const _CharT*> __m;
5217   return std::regex_search(__str, __m, __e, __flags);
5218 }
5219 
5220 template <class _ST, class _SA, class _CharT, class _Traits>
5221 inline _LIBCPP_HIDE_FROM_ABI bool
5222 regex_search(const basic_string<_CharT, _ST, _SA>& __s,
5223              const basic_regex<_CharT, _Traits>& __e,
5224              regex_constants::match_flag_type __flags = regex_constants::match_default) {
5225   match_results<const _CharT*> __mc;
5226   return __e.__search(__s.data(), __s.data() + __s.size(), __mc, __flags);
5227 }
5228 
5229 template <class _ST, class _SA, class _Allocator, class _CharT, class _Traits>
5230 inline _LIBCPP_HIDE_FROM_ABI bool
5231 regex_search(const basic_string<_CharT, _ST, _SA>& __s,
5232              match_results<typename basic_string<_CharT, _ST, _SA>::const_iterator, _Allocator>& __m,
5233              const basic_regex<_CharT, _Traits>& __e,
5234              regex_constants::match_flag_type __flags = regex_constants::match_default) {
5235   match_results<const _CharT*> __mc;
5236   bool __r = __e.__search(__s.data(), __s.data() + __s.size(), __mc, __flags);
5237   __m.__assign(__s.begin(), __s.end(), __mc, __flags & regex_constants::__no_update_pos);
5238   return __r;
5239 }
5240 
5241 #    if _LIBCPP_STD_VER >= 14
5242 template <class _ST, class _SA, class _Ap, class _Cp, class _Tp>
5243 bool regex_search(const basic_string<_Cp, _ST, _SA>&& __s,
5244                   match_results<typename basic_string<_Cp, _ST, _SA>::const_iterator, _Ap>&,
5245                   const basic_regex<_Cp, _Tp>& __e,
5246                   regex_constants::match_flag_type __flags = regex_constants::match_default) = delete;
5247 #    endif
5248 
5249 // regex_match
5250 
5251 template <class _BidirectionalIterator, class _Allocator, class _CharT, class _Traits>
5252 _LIBCPP_HIDE_FROM_ABI bool
5253 regex_match(_BidirectionalIterator __first,
5254             _BidirectionalIterator __last,
5255             match_results<_BidirectionalIterator, _Allocator>& __m,
5256             const basic_regex<_CharT, _Traits>& __e,
5257             regex_constants::match_flag_type __flags = regex_constants::match_default) {
5258   bool __r = std::regex_search(
5259       __first, __last, __m, __e, __flags | regex_constants::match_continuous | regex_constants::__full_match);
5260   if (__r) {
5261     __r = !__m.suffix().matched;
5262     if (!__r)
5263       __m.__matches_.clear();
5264   }
5265   return __r;
5266 }
5267 
5268 template <class _BidirectionalIterator, class _CharT, class _Traits>
5269 inline _LIBCPP_HIDE_FROM_ABI bool
5270 regex_match(_BidirectionalIterator __first,
5271             _BidirectionalIterator __last,
5272             const basic_regex<_CharT, _Traits>& __e,
5273             regex_constants::match_flag_type __flags = regex_constants::match_default) {
5274   match_results<_BidirectionalIterator> __m;
5275   return std::regex_match(__first, __last, __m, __e, __flags);
5276 }
5277 
5278 template <class _CharT, class _Allocator, class _Traits>
5279 inline _LIBCPP_HIDE_FROM_ABI bool
5280 regex_match(const _CharT* __str,
5281             match_results<const _CharT*, _Allocator>& __m,
5282             const basic_regex<_CharT, _Traits>& __e,
5283             regex_constants::match_flag_type __flags = regex_constants::match_default) {
5284   return std::regex_match(__str, __str + _Traits::length(__str), __m, __e, __flags);
5285 }
5286 
5287 template <class _ST, class _SA, class _Allocator, class _CharT, class _Traits>
5288 inline _LIBCPP_HIDE_FROM_ABI bool
5289 regex_match(const basic_string<_CharT, _ST, _SA>& __s,
5290             match_results<typename basic_string<_CharT, _ST, _SA>::const_iterator, _Allocator>& __m,
5291             const basic_regex<_CharT, _Traits>& __e,
5292             regex_constants::match_flag_type __flags = regex_constants::match_default) {
5293   return std::regex_match(__s.begin(), __s.end(), __m, __e, __flags);
5294 }
5295 
5296 #    if _LIBCPP_STD_VER >= 14
5297 template <class _ST, class _SA, class _Allocator, class _CharT, class _Traits>
5298 inline _LIBCPP_HIDE_FROM_ABI bool
5299 regex_match(const basic_string<_CharT, _ST, _SA>&& __s,
5300             match_results<typename basic_string<_CharT, _ST, _SA>::const_iterator, _Allocator>& __m,
5301             const basic_regex<_CharT, _Traits>& __e,
5302             regex_constants::match_flag_type __flags = regex_constants::match_default) = delete;
5303 #    endif
5304 
5305 template <class _CharT, class _Traits>
5306 inline _LIBCPP_HIDE_FROM_ABI bool
5307 regex_match(const _CharT* __str,
5308             const basic_regex<_CharT, _Traits>& __e,
5309             regex_constants::match_flag_type __flags = regex_constants::match_default) {
5310   return std::regex_match(__str, __str + _Traits::length(__str), __e, __flags);
5311 }
5312 
5313 template <class _ST, class _SA, class _CharT, class _Traits>
5314 inline _LIBCPP_HIDE_FROM_ABI bool
5315 regex_match(const basic_string<_CharT, _ST, _SA>& __s,
5316             const basic_regex<_CharT, _Traits>& __e,
5317             regex_constants::match_flag_type __flags = regex_constants::match_default) {
5318   return std::regex_match(__s.begin(), __s.end(), __e, __flags);
5319 }
5320 
5321 // regex_iterator
5322 
5323 template <class _BidirectionalIterator,
5324           class _CharT  = typename iterator_traits<_BidirectionalIterator>::value_type,
5325           class _Traits = regex_traits<_CharT> >
5326 class _LIBCPP_TEMPLATE_VIS regex_iterator;
5327 
5328 typedef regex_iterator<const char*> cregex_iterator;
5329 typedef regex_iterator<string::const_iterator> sregex_iterator;
5330 #    if _LIBCPP_HAS_WIDE_CHARACTERS
5331 typedef regex_iterator<const wchar_t*> wcregex_iterator;
5332 typedef regex_iterator<wstring::const_iterator> wsregex_iterator;
5333 #    endif
5334 
5335 template <class _BidirectionalIterator, class _CharT, class _Traits>
5336 class _LIBCPP_TEMPLATE_VIS _LIBCPP_PREFERRED_NAME(cregex_iterator)
5337     _LIBCPP_IF_WIDE_CHARACTERS(_LIBCPP_PREFERRED_NAME(wcregex_iterator)) _LIBCPP_PREFERRED_NAME(sregex_iterator)
5338         _LIBCPP_IF_WIDE_CHARACTERS(_LIBCPP_PREFERRED_NAME(wsregex_iterator)) regex_iterator {
5339 public:
5340   typedef basic_regex<_CharT, _Traits> regex_type;
5341   typedef match_results<_BidirectionalIterator> value_type;
5342   typedef ptrdiff_t difference_type;
5343   typedef const value_type* pointer;
5344   typedef const value_type& reference;
5345   typedef forward_iterator_tag iterator_category;
5346 #    if _LIBCPP_STD_VER >= 20
5347   typedef input_iterator_tag iterator_concept;
5348 #    endif
5349 
5350 private:
5351   _BidirectionalIterator __begin_;
5352   _BidirectionalIterator __end_;
5353   const regex_type* __pregex_;
5354   regex_constants::match_flag_type __flags_;
5355   value_type __match_;
5356 
5357 public:
5358   regex_iterator();
5359   regex_iterator(_BidirectionalIterator __a,
5360                  _BidirectionalIterator __b,
5361                  const regex_type& __re,
5362                  regex_constants::match_flag_type __m = regex_constants::match_default);
5363 #    if _LIBCPP_STD_VER >= 14
5364   regex_iterator(_BidirectionalIterator __a,
5365                  _BidirectionalIterator __b,
5366                  const regex_type&& __re,
5367                  regex_constants::match_flag_type __m = regex_constants::match_default) = delete;
5368 #    endif
5369 
5370   _LIBCPP_HIDE_FROM_ABI bool operator==(const regex_iterator& __x) const;
5371 #    if _LIBCPP_STD_VER >= 20
5372   _LIBCPP_HIDE_FROM_ABI bool operator==(default_sentinel_t) const { return *this == regex_iterator(); }
5373 #    endif
5374 #    if _LIBCPP_STD_VER < 20
5375   _LIBCPP_HIDE_FROM_ABI bool operator!=(const regex_iterator& __x) const { return !(*this == __x); }
5376 #    endif
5377 
5378   _LIBCPP_HIDE_FROM_ABI reference operator*() const { return __match_; }
5379   _LIBCPP_HIDE_FROM_ABI pointer operator->() const { return std::addressof(__match_); }
5380 
5381   regex_iterator& operator++();
5382   _LIBCPP_HIDE_FROM_ABI regex_iterator operator++(int) {
5383     regex_iterator __t(*this);
5384     ++(*this);
5385     return __t;
5386   }
5387 };
5388 
5389 template <class _BidirectionalIterator, class _CharT, class _Traits>
5390 regex_iterator<_BidirectionalIterator, _CharT, _Traits>::regex_iterator()
5391     : __begin_(), __end_(), __pregex_(nullptr), __flags_(), __match_() {}
5392 
5393 template <class _BidirectionalIterator, class _CharT, class _Traits>
5394 regex_iterator<_BidirectionalIterator, _CharT, _Traits>::regex_iterator(
5395     _BidirectionalIterator __a,
5396     _BidirectionalIterator __b,
5397     const regex_type& __re,
5398     regex_constants::match_flag_type __m)
5399     : __begin_(__a), __end_(__b), __pregex_(std::addressof(__re)), __flags_(__m) {
5400   std::regex_search(__begin_, __end_, __match_, *__pregex_, __flags_);
5401 }
5402 
5403 template <class _BidirectionalIterator, class _CharT, class _Traits>
5404 bool regex_iterator<_BidirectionalIterator, _CharT, _Traits>::operator==(const regex_iterator& __x) const {
5405   if (__match_.empty() && __x.__match_.empty())
5406     return true;
5407   if (__match_.empty() || __x.__match_.empty())
5408     return false;
5409   return __begin_ == __x.__begin_ && __end_ == __x.__end_ && __pregex_ == __x.__pregex_ && __flags_ == __x.__flags_ &&
5410          __match_[0] == __x.__match_[0];
5411 }
5412 
5413 template <class _BidirectionalIterator, class _CharT, class _Traits>
5414 regex_iterator<_BidirectionalIterator, _CharT, _Traits>&
5415 regex_iterator<_BidirectionalIterator, _CharT, _Traits>::operator++() {
5416   __flags_ |= regex_constants::__no_update_pos;
5417   _BidirectionalIterator __start        = __match_[0].second;
5418   _BidirectionalIterator __prefix_start = __start;
5419 
5420   if (__match_[0].first == __match_[0].second) {
5421     if (__start == __end_) {
5422       __match_ = value_type();
5423       return *this;
5424     } else if (std::regex_search(__start,
5425                                  __end_,
5426                                  __match_,
5427                                  *__pregex_,
5428                                  __flags_ | regex_constants::match_not_null | regex_constants::match_continuous))
5429       return *this;
5430     else
5431       ++__start;
5432   }
5433 
5434   __flags_ |= regex_constants::match_prev_avail;
5435   if (!std::regex_search(__start, __end_, __match_, *__pregex_, __flags_)) {
5436     __match_ = value_type();
5437 
5438   } else {
5439     // The Standard mandates that if `regex_search` returns true ([re.regiter.incr]), "`match.prefix().first` shall be
5440     // equal to the previous value of `match[0].second`... It is unspecified how the implementation makes these
5441     // adjustments." The adjustment is necessary if we incremented `__start` above (the branch that deals with
5442     // zero-length matches).
5443     auto& __prefix   = __match_.__prefix_;
5444     __prefix.first   = __prefix_start;
5445     __prefix.matched = __prefix.first != __prefix.second;
5446   }
5447 
5448   return *this;
5449 }
5450 
5451 // regex_token_iterator
5452 
5453 template <class _BidirectionalIterator,
5454           class _CharT  = typename iterator_traits<_BidirectionalIterator>::value_type,
5455           class _Traits = regex_traits<_CharT> >
5456 class _LIBCPP_TEMPLATE_VIS regex_token_iterator;
5457 
5458 typedef regex_token_iterator<const char*> cregex_token_iterator;
5459 typedef regex_token_iterator<string::const_iterator> sregex_token_iterator;
5460 #    if _LIBCPP_HAS_WIDE_CHARACTERS
5461 typedef regex_token_iterator<const wchar_t*> wcregex_token_iterator;
5462 typedef regex_token_iterator<wstring::const_iterator> wsregex_token_iterator;
5463 #    endif
5464 
5465 template <class _BidirectionalIterator, class _CharT, class _Traits>
5466 class _LIBCPP_TEMPLATE_VIS _LIBCPP_PREFERRED_NAME(cregex_token_iterator)
5467     _LIBCPP_IF_WIDE_CHARACTERS(_LIBCPP_PREFERRED_NAME(wcregex_token_iterator))
5468         _LIBCPP_PREFERRED_NAME(sregex_token_iterator)
5469             _LIBCPP_IF_WIDE_CHARACTERS(_LIBCPP_PREFERRED_NAME(wsregex_token_iterator)) regex_token_iterator {
5470 public:
5471   typedef basic_regex<_CharT, _Traits> regex_type;
5472   typedef sub_match<_BidirectionalIterator> value_type;
5473   typedef ptrdiff_t difference_type;
5474   typedef const value_type* pointer;
5475   typedef const value_type& reference;
5476   typedef forward_iterator_tag iterator_category;
5477 #    if _LIBCPP_STD_VER >= 20
5478   typedef input_iterator_tag iterator_concept;
5479 #    endif
5480 
5481 private:
5482   typedef regex_iterator<_BidirectionalIterator, _CharT, _Traits> _Position;
5483 
5484   _Position __position_;
5485   const value_type* __result_;
5486   value_type __suffix_;
5487   ptrdiff_t __n_;
5488   vector<int> __subs_;
5489 
5490 public:
5491   regex_token_iterator();
5492   regex_token_iterator(_BidirectionalIterator __a,
5493                        _BidirectionalIterator __b,
5494                        const regex_type& __re,
5495                        int __submatch                       = 0,
5496                        regex_constants::match_flag_type __m = regex_constants::match_default);
5497 #    if _LIBCPP_STD_VER >= 14
5498   regex_token_iterator(_BidirectionalIterator __a,
5499                        _BidirectionalIterator __b,
5500                        const regex_type&& __re,
5501                        int __submatch                       = 0,
5502                        regex_constants::match_flag_type __m = regex_constants::match_default) = delete;
5503 #    endif
5504 
5505   regex_token_iterator(_BidirectionalIterator __a,
5506                        _BidirectionalIterator __b,
5507                        const regex_type& __re,
5508                        const vector<int>& __submatches,
5509                        regex_constants::match_flag_type __m = regex_constants::match_default);
5510 #    if _LIBCPP_STD_VER >= 14
5511   regex_token_iterator(_BidirectionalIterator __a,
5512                        _BidirectionalIterator __b,
5513                        const regex_type&& __re,
5514                        const vector<int>& __submatches,
5515                        regex_constants::match_flag_type __m = regex_constants::match_default) = delete;
5516 #    endif
5517 
5518 #    ifndef _LIBCPP_CXX03_LANG
5519   regex_token_iterator(_BidirectionalIterator __a,
5520                        _BidirectionalIterator __b,
5521                        const regex_type& __re,
5522                        initializer_list<int> __submatches,
5523                        regex_constants::match_flag_type __m = regex_constants::match_default);
5524 
5525 #      if _LIBCPP_STD_VER >= 14
5526   regex_token_iterator(_BidirectionalIterator __a,
5527                        _BidirectionalIterator __b,
5528                        const regex_type&& __re,
5529                        initializer_list<int> __submatches,
5530                        regex_constants::match_flag_type __m = regex_constants::match_default) = delete;
5531 #      endif
5532 #    endif // _LIBCPP_CXX03_LANG
5533   template <size_t _Np>
5534   regex_token_iterator(_BidirectionalIterator __a,
5535                        _BidirectionalIterator __b,
5536                        const regex_type& __re,
5537                        const int (&__submatches)[_Np],
5538                        regex_constants::match_flag_type __m = regex_constants::match_default);
5539 #    if _LIBCPP_STD_VER >= 14
5540   template <size_t _Np>
5541   regex_token_iterator(_BidirectionalIterator __a,
5542                        _BidirectionalIterator __b,
5543                        const regex_type&& __re,
5544                        const int (&__submatches)[_Np],
5545                        regex_constants::match_flag_type __m = regex_constants::match_default) = delete;
5546 #    endif
5547 
5548   regex_token_iterator(const regex_token_iterator&);
5549   regex_token_iterator& operator=(const regex_token_iterator&);
5550 
5551   _LIBCPP_HIDE_FROM_ABI bool operator==(const regex_token_iterator& __x) const;
5552 #    if _LIBCPP_STD_VER >= 20
5553   _LIBCPP_HIDE_FROM_ABI bool operator==(default_sentinel_t) const { return *this == regex_token_iterator(); }
5554 #    endif
5555 #    if _LIBCPP_STD_VER < 20
5556   _LIBCPP_HIDE_FROM_ABI bool operator!=(const regex_token_iterator& __x) const { return !(*this == __x); }
5557 #    endif
5558 
5559   _LIBCPP_HIDE_FROM_ABI const value_type& operator*() const { return *__result_; }
5560   _LIBCPP_HIDE_FROM_ABI const value_type* operator->() const { return __result_; }
5561 
5562   regex_token_iterator& operator++();
5563   _LIBCPP_HIDE_FROM_ABI regex_token_iterator operator++(int) {
5564     regex_token_iterator __t(*this);
5565     ++(*this);
5566     return __t;
5567   }
5568 
5569 private:
5570   void __init(_BidirectionalIterator __a, _BidirectionalIterator __b);
5571   void __establish_result() {
5572     if (__subs_[__n_] == -1)
5573       __result_ = &__position_->prefix();
5574     else
5575       __result_ = &(*__position_)[__subs_[__n_]];
5576   }
5577 };
5578 
5579 template <class _BidirectionalIterator, class _CharT, class _Traits>
5580 regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::regex_token_iterator()
5581     : __result_(nullptr), __suffix_(), __n_(0) {}
5582 
5583 template <class _BidirectionalIterator, class _CharT, class _Traits>
5584 void regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::__init(
5585     _BidirectionalIterator __a, _BidirectionalIterator __b) {
5586   if (__position_ != _Position())
5587     __establish_result();
5588   else if (__subs_[__n_] == -1) {
5589     __suffix_.matched = true;
5590     __suffix_.first   = __a;
5591     __suffix_.second  = __b;
5592     __result_         = &__suffix_;
5593   } else
5594     __result_ = nullptr;
5595 }
5596 
5597 template <class _BidirectionalIterator, class _CharT, class _Traits>
5598 regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::regex_token_iterator(
5599     _BidirectionalIterator __a,
5600     _BidirectionalIterator __b,
5601     const regex_type& __re,
5602     int __submatch,
5603     regex_constants::match_flag_type __m)
5604     : __position_(__a, __b, __re, __m), __n_(0), __subs_(1, __submatch) {
5605   __init(__a, __b);
5606 }
5607 
5608 template <class _BidirectionalIterator, class _CharT, class _Traits>
5609 regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::regex_token_iterator(
5610     _BidirectionalIterator __a,
5611     _BidirectionalIterator __b,
5612     const regex_type& __re,
5613     const vector<int>& __submatches,
5614     regex_constants::match_flag_type __m)
5615     : __position_(__a, __b, __re, __m), __n_(0), __subs_(__submatches) {
5616   __init(__a, __b);
5617 }
5618 
5619 #    ifndef _LIBCPP_CXX03_LANG
5620 
5621 template <class _BidirectionalIterator, class _CharT, class _Traits>
5622 regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::regex_token_iterator(
5623     _BidirectionalIterator __a,
5624     _BidirectionalIterator __b,
5625     const regex_type& __re,
5626     initializer_list<int> __submatches,
5627     regex_constants::match_flag_type __m)
5628     : __position_(__a, __b, __re, __m), __n_(0), __subs_(__submatches) {
5629   __init(__a, __b);
5630 }
5631 
5632 #    endif // _LIBCPP_CXX03_LANG
5633 
5634 template <class _BidirectionalIterator, class _CharT, class _Traits>
5635 template <size_t _Np>
5636 regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::regex_token_iterator(
5637     _BidirectionalIterator __a,
5638     _BidirectionalIterator __b,
5639     const regex_type& __re,
5640     const int (&__submatches)[_Np],
5641     regex_constants::match_flag_type __m)
5642     : __position_(__a, __b, __re, __m), __n_(0), __subs_(begin(__submatches), end(__submatches)) {
5643   __init(__a, __b);
5644 }
5645 
5646 template <class _BidirectionalIterator, class _CharT, class _Traits>
5647 regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::regex_token_iterator(const regex_token_iterator& __x)
5648     : __position_(__x.__position_),
5649       __result_(__x.__result_),
5650       __suffix_(__x.__suffix_),
5651       __n_(__x.__n_),
5652       __subs_(__x.__subs_) {
5653   if (__x.__result_ == &__x.__suffix_)
5654     __result_ = &__suffix_;
5655   else if (__result_ != nullptr)
5656     __establish_result();
5657 }
5658 
5659 template <class _BidirectionalIterator, class _CharT, class _Traits>
5660 regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>&
5661 regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::operator=(const regex_token_iterator& __x) {
5662   if (this != &__x) {
5663     __position_ = __x.__position_;
5664     if (__x.__result_ == &__x.__suffix_)
5665       __result_ = &__suffix_;
5666     else
5667       __result_ = __x.__result_;
5668     __suffix_ = __x.__suffix_;
5669     __n_      = __x.__n_;
5670     __subs_   = __x.__subs_;
5671 
5672     if (__result_ != nullptr && __result_ != &__suffix_)
5673       __establish_result();
5674   }
5675   return *this;
5676 }
5677 
5678 template <class _BidirectionalIterator, class _CharT, class _Traits>
5679 bool regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::operator==(const regex_token_iterator& __x) const {
5680   if (__result_ == nullptr && __x.__result_ == nullptr)
5681     return true;
5682   if (__result_ == &__suffix_ && __x.__result_ == &__x.__suffix_ && __suffix_ == __x.__suffix_)
5683     return true;
5684   if (__result_ == nullptr || __x.__result_ == nullptr)
5685     return false;
5686   if (__result_ == &__suffix_ || __x.__result_ == &__x.__suffix_)
5687     return false;
5688   return __position_ == __x.__position_ && __n_ == __x.__n_ && __subs_ == __x.__subs_;
5689 }
5690 
5691 template <class _BidirectionalIterator, class _CharT, class _Traits>
5692 regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>&
5693 regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::operator++() {
5694   _Position __prev = __position_;
5695   if (__result_ == &__suffix_)
5696     __result_ = nullptr;
5697   else if (static_cast<size_t>(__n_ + 1) < __subs_.size()) {
5698     ++__n_;
5699     __establish_result();
5700   } else {
5701     __n_ = 0;
5702     ++__position_;
5703     if (__position_ != _Position())
5704       __establish_result();
5705     else {
5706       if (std::find(__subs_.begin(), __subs_.end(), -1) != __subs_.end() && __prev->suffix().length() != 0) {
5707         __suffix_.matched = true;
5708         __suffix_.first   = __prev->suffix().first;
5709         __suffix_.second  = __prev->suffix().second;
5710         __result_         = &__suffix_;
5711       } else
5712         __result_ = nullptr;
5713     }
5714   }
5715   return *this;
5716 }
5717 
5718 // regex_replace
5719 
5720 template <class _OutputIterator, class _BidirectionalIterator, class _Traits, class _CharT>
5721 _LIBCPP_HIDE_FROM_ABI _OutputIterator regex_replace(
5722     _OutputIterator __output_iter,
5723     _BidirectionalIterator __first,
5724     _BidirectionalIterator __last,
5725     const basic_regex<_CharT, _Traits>& __e,
5726     const _CharT* __fmt,
5727     regex_constants::match_flag_type __flags = regex_constants::match_default) {
5728   typedef regex_iterator<_BidirectionalIterator, _CharT, _Traits> _Iter;
5729   _Iter __i(__first, __last, __e, __flags);
5730   _Iter __eof;
5731   if (__i == __eof) {
5732     if (!(__flags & regex_constants::format_no_copy))
5733       __output_iter = std::copy(__first, __last, __output_iter);
5734   } else {
5735     sub_match<_BidirectionalIterator> __lm;
5736     for (size_t __len = char_traits<_CharT>::length(__fmt); __i != __eof; ++__i) {
5737       if (!(__flags & regex_constants::format_no_copy))
5738         __output_iter = std::copy(__i->prefix().first, __i->prefix().second, __output_iter);
5739       __output_iter = __i->format(__output_iter, __fmt, __fmt + __len, __flags);
5740       __lm          = __i->suffix();
5741       if (__flags & regex_constants::format_first_only)
5742         break;
5743     }
5744     if (!(__flags & regex_constants::format_no_copy))
5745       __output_iter = std::copy(__lm.first, __lm.second, __output_iter);
5746   }
5747   return __output_iter;
5748 }
5749 
5750 template <class _OutputIterator, class _BidirectionalIterator, class _Traits, class _CharT, class _ST, class _SA>
5751 inline _LIBCPP_HIDE_FROM_ABI _OutputIterator regex_replace(
5752     _OutputIterator __output_iter,
5753     _BidirectionalIterator __first,
5754     _BidirectionalIterator __last,
5755     const basic_regex<_CharT, _Traits>& __e,
5756     const basic_string<_CharT, _ST, _SA>& __fmt,
5757     regex_constants::match_flag_type __flags = regex_constants::match_default) {
5758   return std::regex_replace(__output_iter, __first, __last, __e, __fmt.c_str(), __flags);
5759 }
5760 
5761 template <class _Traits, class _CharT, class _ST, class _SA, class _FST, class _FSA>
5762 inline _LIBCPP_HIDE_FROM_ABI basic_string<_CharT, _ST, _SA>
5763 regex_replace(const basic_string<_CharT, _ST, _SA>& __s,
5764               const basic_regex<_CharT, _Traits>& __e,
5765               const basic_string<_CharT, _FST, _FSA>& __fmt,
5766               regex_constants::match_flag_type __flags = regex_constants::match_default) {
5767   basic_string<_CharT, _ST, _SA> __r;
5768   std::regex_replace(std::back_inserter(__r), __s.begin(), __s.end(), __e, __fmt.c_str(), __flags);
5769   return __r;
5770 }
5771 
5772 template <class _Traits, class _CharT, class _ST, class _SA>
5773 inline _LIBCPP_HIDE_FROM_ABI basic_string<_CharT, _ST, _SA>
5774 regex_replace(const basic_string<_CharT, _ST, _SA>& __s,
5775               const basic_regex<_CharT, _Traits>& __e,
5776               const _CharT* __fmt,
5777               regex_constants::match_flag_type __flags = regex_constants::match_default) {
5778   basic_string<_CharT, _ST, _SA> __r;
5779   std::regex_replace(std::back_inserter(__r), __s.begin(), __s.end(), __e, __fmt, __flags);
5780   return __r;
5781 }
5782 
5783 template <class _Traits, class _CharT, class _ST, class _SA>
5784 inline _LIBCPP_HIDE_FROM_ABI basic_string<_CharT>
5785 regex_replace(const _CharT* __s,
5786               const basic_regex<_CharT, _Traits>& __e,
5787               const basic_string<_CharT, _ST, _SA>& __fmt,
5788               regex_constants::match_flag_type __flags = regex_constants::match_default) {
5789   basic_string<_CharT> __r;
5790   std::regex_replace(std::back_inserter(__r), __s, __s + char_traits<_CharT>::length(__s), __e, __fmt.c_str(), __flags);
5791   return __r;
5792 }
5793 
5794 template <class _Traits, class _CharT>
5795 inline _LIBCPP_HIDE_FROM_ABI basic_string<_CharT>
5796 regex_replace(const _CharT* __s,
5797               const basic_regex<_CharT, _Traits>& __e,
5798               const _CharT* __fmt,
5799               regex_constants::match_flag_type __flags = regex_constants::match_default) {
5800   basic_string<_CharT> __r;
5801   std::regex_replace(std::back_inserter(__r), __s, __s + char_traits<_CharT>::length(__s), __e, __fmt, __flags);
5802   return __r;
5803 }
5804 
5805 _LIBCPP_END_NAMESPACE_STD
5806 
5807 #    if _LIBCPP_STD_VER >= 17
5808 _LIBCPP_BEGIN_NAMESPACE_STD
5809 namespace pmr {
5810 template <class _BidirT>
5811 using match_results _LIBCPP_AVAILABILITY_PMR =
5812     std::match_results<_BidirT, polymorphic_allocator<std::sub_match<_BidirT>>>;
5813 
5814 using cmatch _LIBCPP_AVAILABILITY_PMR = match_results<const char*>;
5815 using smatch _LIBCPP_AVAILABILITY_PMR = match_results<std::pmr::string::const_iterator>;
5816 
5817 #      if _LIBCPP_HAS_WIDE_CHARACTERS
5818 using wcmatch _LIBCPP_AVAILABILITY_PMR = match_results<const wchar_t*>;
5819 using wsmatch _LIBCPP_AVAILABILITY_PMR = match_results<std::pmr::wstring::const_iterator>;
5820 #      endif
5821 } // namespace pmr
5822 _LIBCPP_END_NAMESPACE_STD
5823 #    endif
5824 
5825 _LIBCPP_POP_MACROS
5826 
5827 #  endif // _LIBCPP_HAS_LOCALIZATION
5828 
5829 #  if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
5830 #    include <atomic>
5831 #    include <concepts>
5832 #    include <cstdlib>
5833 #    include <iosfwd>
5834 #    include <iterator>
5835 #    include <mutex>
5836 #    include <new>
5837 #    include <type_traits>
5838 #    include <typeinfo>
5839 #    include <utility>
5840 #  endif
5841 #endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
5842 
5843 #endif // _LIBCPP_REGEX