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