xref: /freebsd/contrib/llvm-project/libcxx/include/regex (revision 7a6dacaca14b62ca4b74406814becb87a3fefac0)
10b57cec5SDimitry Andric// -*- C++ -*-
2349cc55cSDimitry Andric//===----------------------------------------------------------------------===//
30b57cec5SDimitry Andric//
40b57cec5SDimitry Andric// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
50b57cec5SDimitry Andric// See https://llvm.org/LICENSE.txt for license information.
60b57cec5SDimitry Andric// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
70b57cec5SDimitry Andric//
80b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
90b57cec5SDimitry Andric
100b57cec5SDimitry Andric#ifndef _LIBCPP_REGEX
110b57cec5SDimitry Andric#define _LIBCPP_REGEX
120b57cec5SDimitry Andric
130b57cec5SDimitry Andric/*
140b57cec5SDimitry Andric    regex synopsis
150b57cec5SDimitry Andric
1606c3fb27SDimitry Andric#include <compare>
170b57cec5SDimitry Andric#include <initializer_list>
180b57cec5SDimitry Andric
190b57cec5SDimitry Andricnamespace std
200b57cec5SDimitry Andric{
210b57cec5SDimitry Andric
220b57cec5SDimitry Andricnamespace regex_constants
230b57cec5SDimitry Andric{
240b57cec5SDimitry Andric
255ffd83dbSDimitry Andricenum syntax_option_type
260b57cec5SDimitry Andric{
270b57cec5SDimitry Andric    icase      = unspecified,
280b57cec5SDimitry Andric    nosubs     = unspecified,
290b57cec5SDimitry Andric    optimize   = unspecified,
300b57cec5SDimitry Andric    collate    = unspecified,
310b57cec5SDimitry Andric    ECMAScript = unspecified,
320b57cec5SDimitry Andric    basic      = unspecified,
330b57cec5SDimitry Andric    extended   = unspecified,
340b57cec5SDimitry Andric    awk        = unspecified,
350b57cec5SDimitry Andric    grep       = unspecified,
36e8d8bef9SDimitry Andric    egrep      = unspecified,
37e8d8bef9SDimitry Andric    multiline  = unspecified
380b57cec5SDimitry Andric};
390b57cec5SDimitry Andric
400b57cec5SDimitry Andricconstexpr syntax_option_type operator~(syntax_option_type f);
410b57cec5SDimitry Andricconstexpr syntax_option_type operator&(syntax_option_type lhs, syntax_option_type rhs);
420b57cec5SDimitry Andricconstexpr syntax_option_type operator|(syntax_option_type lhs, syntax_option_type rhs);
430b57cec5SDimitry Andric
440b57cec5SDimitry Andricenum match_flag_type
450b57cec5SDimitry Andric{
460b57cec5SDimitry Andric    match_default     = 0,
470b57cec5SDimitry Andric    match_not_bol     = unspecified,
480b57cec5SDimitry Andric    match_not_eol     = unspecified,
490b57cec5SDimitry Andric    match_not_bow     = unspecified,
500b57cec5SDimitry Andric    match_not_eow     = unspecified,
510b57cec5SDimitry Andric    match_any         = unspecified,
520b57cec5SDimitry Andric    match_not_null    = unspecified,
530b57cec5SDimitry Andric    match_continuous  = unspecified,
540b57cec5SDimitry Andric    match_prev_avail  = unspecified,
550b57cec5SDimitry Andric    format_default    = 0,
560b57cec5SDimitry Andric    format_sed        = unspecified,
570b57cec5SDimitry Andric    format_no_copy    = unspecified,
580b57cec5SDimitry Andric    format_first_only = unspecified
590b57cec5SDimitry Andric};
600b57cec5SDimitry Andric
610b57cec5SDimitry Andricconstexpr match_flag_type operator~(match_flag_type f);
620b57cec5SDimitry Andricconstexpr match_flag_type operator&(match_flag_type lhs, match_flag_type rhs);
630b57cec5SDimitry Andricconstexpr match_flag_type operator|(match_flag_type lhs, match_flag_type rhs);
640b57cec5SDimitry Andric
650b57cec5SDimitry Andricenum error_type
660b57cec5SDimitry Andric{
670b57cec5SDimitry Andric    error_collate    = unspecified,
680b57cec5SDimitry Andric    error_ctype      = unspecified,
690b57cec5SDimitry Andric    error_escape     = unspecified,
700b57cec5SDimitry Andric    error_backref    = unspecified,
710b57cec5SDimitry Andric    error_brack      = unspecified,
720b57cec5SDimitry Andric    error_paren      = unspecified,
730b57cec5SDimitry Andric    error_brace      = unspecified,
740b57cec5SDimitry Andric    error_badbrace   = unspecified,
750b57cec5SDimitry Andric    error_range      = unspecified,
760b57cec5SDimitry Andric    error_space      = unspecified,
770b57cec5SDimitry Andric    error_badrepeat  = unspecified,
780b57cec5SDimitry Andric    error_complexity = unspecified,
790b57cec5SDimitry Andric    error_stack      = unspecified
800b57cec5SDimitry Andric};
810b57cec5SDimitry Andric
820b57cec5SDimitry Andric}  // regex_constants
830b57cec5SDimitry Andric
840b57cec5SDimitry Andricclass regex_error
850b57cec5SDimitry Andric    : public runtime_error
860b57cec5SDimitry Andric{
870b57cec5SDimitry Andricpublic:
880b57cec5SDimitry Andric    explicit regex_error(regex_constants::error_type ecode);
890b57cec5SDimitry Andric    regex_constants::error_type code() const;
900b57cec5SDimitry Andric};
910b57cec5SDimitry Andric
920b57cec5SDimitry Andrictemplate <class charT>
930b57cec5SDimitry Andricstruct regex_traits
940b57cec5SDimitry Andric{
950b57cec5SDimitry Andricpublic:
960b57cec5SDimitry Andric    typedef charT                   char_type;
970b57cec5SDimitry Andric    typedef basic_string<char_type> string_type;
980b57cec5SDimitry Andric    typedef locale                  locale_type;
990b57cec5SDimitry Andric    typedef /bitmask_type/          char_class_type;
1000b57cec5SDimitry Andric
1010b57cec5SDimitry Andric    regex_traits();
1020b57cec5SDimitry Andric
1030b57cec5SDimitry Andric    static size_t length(const char_type* p);
1040b57cec5SDimitry Andric    charT translate(charT c) const;
1050b57cec5SDimitry Andric    charT translate_nocase(charT c) const;
1060b57cec5SDimitry Andric    template <class ForwardIterator>
1070b57cec5SDimitry Andric        string_type
1080b57cec5SDimitry Andric        transform(ForwardIterator first, ForwardIterator last) const;
1090b57cec5SDimitry Andric    template <class ForwardIterator>
1100b57cec5SDimitry Andric        string_type
1110b57cec5SDimitry Andric        transform_primary( ForwardIterator first, ForwardIterator last) const;
1120b57cec5SDimitry Andric    template <class ForwardIterator>
1130b57cec5SDimitry Andric        string_type
1140b57cec5SDimitry Andric        lookup_collatename(ForwardIterator first, ForwardIterator last) const;
1150b57cec5SDimitry Andric    template <class ForwardIterator>
1160b57cec5SDimitry Andric        char_class_type
1170b57cec5SDimitry Andric        lookup_classname(ForwardIterator first, ForwardIterator last,
1180b57cec5SDimitry Andric                         bool icase = false) const;
1190b57cec5SDimitry Andric    bool isctype(charT c, char_class_type f) const;
1200b57cec5SDimitry Andric    int value(charT ch, int radix) const;
1210b57cec5SDimitry Andric    locale_type imbue(locale_type l);
1220b57cec5SDimitry Andric    locale_type getloc()const;
1230b57cec5SDimitry Andric};
1240b57cec5SDimitry Andric
1250b57cec5SDimitry Andrictemplate <class charT, class traits = regex_traits<charT>>
1260b57cec5SDimitry Andricclass basic_regex
1270b57cec5SDimitry Andric{
1280b57cec5SDimitry Andricpublic:
1290b57cec5SDimitry Andric    // types:
1300b57cec5SDimitry Andric    typedef charT                               value_type;
1310b57cec5SDimitry Andric    typedef traits                              traits_type;
1320b57cec5SDimitry Andric    typedef typename traits::string_type        string_type;
1330b57cec5SDimitry Andric    typedef regex_constants::syntax_option_type flag_type;
1340b57cec5SDimitry Andric    typedef typename traits::locale_type        locale_type;
1350b57cec5SDimitry Andric
1360b57cec5SDimitry Andric    // constants:
1370b57cec5SDimitry Andric    static constexpr regex_constants::syntax_option_type icase = regex_constants::icase;
1380b57cec5SDimitry Andric    static constexpr regex_constants::syntax_option_type nosubs = regex_constants::nosubs;
1390b57cec5SDimitry Andric    static constexpr regex_constants::syntax_option_type optimize = regex_constants::optimize;
1400b57cec5SDimitry Andric    static constexpr regex_constants::syntax_option_type collate = regex_constants::collate;
1410b57cec5SDimitry Andric    static constexpr regex_constants::syntax_option_type ECMAScript = regex_constants::ECMAScript;
1420b57cec5SDimitry Andric    static constexpr regex_constants::syntax_option_type basic = regex_constants::basic;
1430b57cec5SDimitry Andric    static constexpr regex_constants::syntax_option_type extended = regex_constants::extended;
1440b57cec5SDimitry Andric    static constexpr regex_constants::syntax_option_type awk = regex_constants::awk;
1450b57cec5SDimitry Andric    static constexpr regex_constants::syntax_option_type grep = regex_constants::grep;
1460b57cec5SDimitry Andric    static constexpr regex_constants::syntax_option_type egrep = regex_constants::egrep;
147e8d8bef9SDimitry Andric    static constexpr regex_constants::syntax_option_type multiline = regex_constants::multiline;
1480b57cec5SDimitry Andric
1490b57cec5SDimitry Andric    // construct/copy/destroy:
1500b57cec5SDimitry Andric    basic_regex();
1510b57cec5SDimitry Andric    explicit basic_regex(const charT* p, flag_type f = regex_constants::ECMAScript);
1520b57cec5SDimitry Andric    basic_regex(const charT* p, size_t len, flag_type f = regex_constants::ECMAScript);
1530b57cec5SDimitry Andric    basic_regex(const basic_regex&);
1540b57cec5SDimitry Andric    basic_regex(basic_regex&&) noexcept;
1550b57cec5SDimitry Andric    template <class ST, class SA>
1560b57cec5SDimitry Andric        explicit basic_regex(const basic_string<charT, ST, SA>& p,
1570b57cec5SDimitry Andric                             flag_type f = regex_constants::ECMAScript);
1580b57cec5SDimitry Andric    template <class ForwardIterator>
1590b57cec5SDimitry Andric        basic_regex(ForwardIterator first, ForwardIterator last,
1600b57cec5SDimitry Andric                    flag_type f = regex_constants::ECMAScript);
1610b57cec5SDimitry Andric    basic_regex(initializer_list<charT>, flag_type = regex_constants::ECMAScript);
1620b57cec5SDimitry Andric
1630b57cec5SDimitry Andric    ~basic_regex();
1640b57cec5SDimitry Andric
1650b57cec5SDimitry Andric    basic_regex& operator=(const basic_regex&);
1660b57cec5SDimitry Andric    basic_regex& operator=(basic_regex&&) noexcept;
1670b57cec5SDimitry Andric    basic_regex& operator=(const charT* ptr);
1680b57cec5SDimitry Andric    basic_regex& operator=(initializer_list<charT> il);
1690b57cec5SDimitry Andric    template <class ST, class SA>
1700b57cec5SDimitry Andric        basic_regex& operator=(const basic_string<charT, ST, SA>& p);
1710b57cec5SDimitry Andric
1720b57cec5SDimitry Andric    // assign:
1730b57cec5SDimitry Andric    basic_regex& assign(const basic_regex& that);
1740b57cec5SDimitry Andric    basic_regex& assign(basic_regex&& that) noexcept;
1750b57cec5SDimitry Andric    basic_regex& assign(const charT* ptr,           flag_type f = regex_constants::ECMAScript);
176e40139ffSDimitry Andric    basic_regex& assign(const charT* p, size_t len, flag_type f = regex_constants::ECMAScript);
1770b57cec5SDimitry Andric    template <class string_traits, class A>
1780b57cec5SDimitry Andric        basic_regex& assign(const basic_string<charT, string_traits, A>& s,
1790b57cec5SDimitry Andric                                                    flag_type f = regex_constants::ECMAScript);
1800b57cec5SDimitry Andric    template <class InputIterator>
1810b57cec5SDimitry Andric        basic_regex& assign(InputIterator first, InputIterator last,
1820b57cec5SDimitry Andric                                                    flag_type f = regex_constants::ECMAScript);
183e40139ffSDimitry Andric    basic_regex& assign(initializer_list<charT>,    flag_type f = regex_constants::ECMAScript);
1840b57cec5SDimitry Andric
1850b57cec5SDimitry Andric    // const operations:
1860b57cec5SDimitry Andric    unsigned mark_count() const;
1870b57cec5SDimitry Andric    flag_type flags() const;
1880b57cec5SDimitry Andric
1890b57cec5SDimitry Andric    // locale:
1900b57cec5SDimitry Andric    locale_type imbue(locale_type loc);
1910b57cec5SDimitry Andric    locale_type getloc() const;
1920b57cec5SDimitry Andric
1930b57cec5SDimitry Andric    // swap:
1940b57cec5SDimitry Andric    void swap(basic_regex&);
1950b57cec5SDimitry Andric};
1960b57cec5SDimitry Andric
1970b57cec5SDimitry Andrictemplate<class ForwardIterator>
1980b57cec5SDimitry Andricbasic_regex(ForwardIterator, ForwardIterator,
1990b57cec5SDimitry Andric            regex_constants::syntax_option_type = regex_constants::ECMAScript)
2000b57cec5SDimitry Andric    -> basic_regex<typename iterator_traits<ForwardIterator>::value_type>; // C++17
2010b57cec5SDimitry Andric
2020b57cec5SDimitry Andrictypedef basic_regex<char>    regex;
2030b57cec5SDimitry Andrictypedef basic_regex<wchar_t> wregex;
2040b57cec5SDimitry Andric
2050b57cec5SDimitry Andrictemplate <class charT, class traits>
2060b57cec5SDimitry Andric    void swap(basic_regex<charT, traits>& e1, basic_regex<charT, traits>& e2);
2070b57cec5SDimitry Andric
2080b57cec5SDimitry Andrictemplate <class BidirectionalIterator>
2090b57cec5SDimitry Andricclass sub_match
2100b57cec5SDimitry Andric    : public pair<BidirectionalIterator, BidirectionalIterator>
2110b57cec5SDimitry Andric{
2120b57cec5SDimitry Andricpublic:
2130b57cec5SDimitry Andric    typedef typename iterator_traits<BidirectionalIterator>::value_type value_type;
2140b57cec5SDimitry Andric    typedef typename iterator_traits<BidirectionalIterator>::difference_type difference_type;
2150b57cec5SDimitry Andric    typedef BidirectionalIterator                                      iterator;
2160b57cec5SDimitry Andric    typedef basic_string<value_type>                                string_type;
2170b57cec5SDimitry Andric
2180b57cec5SDimitry Andric    bool matched;
2190b57cec5SDimitry Andric
2200b57cec5SDimitry Andric    constexpr sub_match();
2210b57cec5SDimitry Andric
2220b57cec5SDimitry Andric    difference_type length() const;
2230b57cec5SDimitry Andric    operator string_type() const;
2240b57cec5SDimitry Andric    string_type str() const;
2250b57cec5SDimitry Andric
2260b57cec5SDimitry Andric    int compare(const sub_match& s) const;
2270b57cec5SDimitry Andric    int compare(const string_type& s) const;
2280b57cec5SDimitry Andric    int compare(const value_type* s) const;
22906c3fb27SDimitry Andric
23006c3fb27SDimitry Andric    void swap(sub_match& s) noexcept(see below);
2310b57cec5SDimitry Andric};
2320b57cec5SDimitry Andric
2330b57cec5SDimitry Andrictypedef sub_match<const char*>             csub_match;
2340b57cec5SDimitry Andrictypedef sub_match<const wchar_t*>          wcsub_match;
2350b57cec5SDimitry Andrictypedef sub_match<string::const_iterator>  ssub_match;
2360b57cec5SDimitry Andrictypedef sub_match<wstring::const_iterator> wssub_match;
2370b57cec5SDimitry Andric
2380b57cec5SDimitry Andrictemplate <class BiIter>
2390b57cec5SDimitry Andric    bool
2400b57cec5SDimitry Andric    operator==(const sub_match<BiIter>& lhs, const sub_match<BiIter>& rhs);
2410b57cec5SDimitry Andric
2420b57cec5SDimitry Andrictemplate <class BiIter>
24306c3fb27SDimitry Andric    auto
24406c3fb27SDimitry Andric    operator<=>(const sub_match<BiIter>& lhs, const sub_match<BiIter>& rhs); // Since C++20
24506c3fb27SDimitry Andric
24606c3fb27SDimitry Andric template <class BiIter>                                                     // Removed in C++20
2470b57cec5SDimitry Andric    bool
2480b57cec5SDimitry Andric    operator!=(const sub_match<BiIter>& lhs, const sub_match<BiIter>& rhs);
2490b57cec5SDimitry Andric
25006c3fb27SDimitry Andrictemplate <class BiIter>                                                      // Removed in C++20
2510b57cec5SDimitry Andric    bool
2520b57cec5SDimitry Andric    operator<(const sub_match<BiIter>& lhs, const sub_match<BiIter>& rhs);
2530b57cec5SDimitry Andric
25406c3fb27SDimitry Andrictemplate <class BiIter>                                                      // Removed in C++20
2550b57cec5SDimitry Andric    bool
2560b57cec5SDimitry Andric    operator<=(const sub_match<BiIter>& lhs, const sub_match<BiIter>& rhs);
2570b57cec5SDimitry Andric
25806c3fb27SDimitry Andrictemplate <class BiIter>                                                      // Removed in C++20
2590b57cec5SDimitry Andric    bool
2600b57cec5SDimitry Andric    operator>=(const sub_match<BiIter>& lhs, const sub_match<BiIter>& rhs);
2610b57cec5SDimitry Andric
26206c3fb27SDimitry Andrictemplate <class BiIter>                                                      // Removed in C++20
2630b57cec5SDimitry Andric    bool
2640b57cec5SDimitry Andric    operator>(const sub_match<BiIter>& lhs, const sub_match<BiIter>& rhs);
2650b57cec5SDimitry Andric
26606c3fb27SDimitry Andrictemplate <class BiIter, class ST, class SA>                                  // Removed in C++20
2670b57cec5SDimitry Andric    bool
2680b57cec5SDimitry Andric    operator==(const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& lhs,
2690b57cec5SDimitry Andric               const sub_match<BiIter>& rhs);
2700b57cec5SDimitry Andric
27106c3fb27SDimitry Andrictemplate <class BiIter, class ST, class SA>                                  // Removed in C++20
2720b57cec5SDimitry Andric    bool
2730b57cec5SDimitry Andric    operator!=(const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& lhs,
2740b57cec5SDimitry Andric               const sub_match<BiIter>& rhs);
2750b57cec5SDimitry Andric
27606c3fb27SDimitry Andrictemplate <class BiIter, class ST, class SA>                                  // Removed in C++20
2770b57cec5SDimitry Andric    bool
2780b57cec5SDimitry Andric    operator<(const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& lhs,
2790b57cec5SDimitry Andric              const sub_match<BiIter>& rhs);
2800b57cec5SDimitry Andric
28106c3fb27SDimitry Andrictemplate <class BiIter, class ST, class SA>                                  // Removed in C++20
2820b57cec5SDimitry Andric    bool
2830b57cec5SDimitry Andric    operator>(const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& lhs,
2840b57cec5SDimitry Andric              const sub_match<BiIter>& rhs);
2850b57cec5SDimitry Andric
28606c3fb27SDimitry Andrictemplate <class BiIter, class ST, class SA>                                  // Removed in C++20
2870b57cec5SDimitry Andric    bool operator>=(const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& lhs,
2880b57cec5SDimitry Andric                    const sub_match<BiIter>& rhs);
2890b57cec5SDimitry Andric
29006c3fb27SDimitry Andrictemplate <class BiIter, class ST, class SA>                                  // Removed in C++20
2910b57cec5SDimitry Andric    bool
2920b57cec5SDimitry Andric    operator<=(const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& lhs,
2930b57cec5SDimitry Andric               const sub_match<BiIter>& rhs);
2940b57cec5SDimitry Andric
2950b57cec5SDimitry Andrictemplate <class BiIter, class ST, class SA>
2960b57cec5SDimitry Andric    bool
2970b57cec5SDimitry Andric    operator==(const sub_match<BiIter>& lhs,
2980b57cec5SDimitry Andric               const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& rhs);
2990b57cec5SDimitry Andric
30006c3fb27SDimitry Andrictemplate <class BiIter, class ST, class SA>                                  // Since C++20
30106c3fb27SDimitry Andric    auto
30206c3fb27SDimitry Andric    operator<=>(const sub_match<BiIter>& lhs,
30306c3fb27SDimitry Andric                const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& rhs);
30406c3fb27SDimitry Andric
30506c3fb27SDimitry Andrictemplate <class BiIter, class ST, class SA>                                  // Removed in C++20
3060b57cec5SDimitry Andric    bool
3070b57cec5SDimitry Andric    operator!=(const sub_match<BiIter>& lhs,
3080b57cec5SDimitry Andric               const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& rhs);
3090b57cec5SDimitry Andric
31006c3fb27SDimitry Andrictemplate <class BiIter, class ST, class SA>                                  // Removed in C++20
3110b57cec5SDimitry Andric    bool
3120b57cec5SDimitry Andric    operator<(const sub_match<BiIter>& lhs,
3130b57cec5SDimitry Andric              const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& rhs);
3140b57cec5SDimitry Andric
31506c3fb27SDimitry Andrictemplate <class BiIter, class ST, class SA>                                  // Removed in C++20
31606c3fb27SDimitry Andric    bool
31706c3fb27SDimitry Andric    operator>(const sub_match<BiIter>& lhs,
3180b57cec5SDimitry Andric                   const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& rhs);
3190b57cec5SDimitry Andric
32006c3fb27SDimitry Andrictemplate <class BiIter, class ST, class SA>                                  // Removed in C++20
3210b57cec5SDimitry Andric    bool
3220b57cec5SDimitry Andric    operator>=(const sub_match<BiIter>& lhs,
3230b57cec5SDimitry Andric               const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& rhs);
3240b57cec5SDimitry Andric
32506c3fb27SDimitry Andrictemplate <class BiIter, class ST, class SA>                                  // Removed in C++20
3260b57cec5SDimitry Andric    bool
3270b57cec5SDimitry Andric    operator<=(const sub_match<BiIter>& lhs,
3280b57cec5SDimitry Andric               const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& rhs);
3290b57cec5SDimitry Andric
33006c3fb27SDimitry Andrictemplate <class BiIter>                                                      // Removed in C++20
3310b57cec5SDimitry Andric    bool
3320b57cec5SDimitry Andric    operator==(typename iterator_traits<BiIter>::value_type const* lhs,
3330b57cec5SDimitry Andric               const sub_match<BiIter>& rhs);
3340b57cec5SDimitry Andric
33506c3fb27SDimitry Andrictemplate <class BiIter>                                                      // Removed in C++20
3360b57cec5SDimitry Andric    bool
3370b57cec5SDimitry Andric    operator!=(typename iterator_traits<BiIter>::value_type const* lhs,
3380b57cec5SDimitry Andric               const sub_match<BiIter>& rhs);
3390b57cec5SDimitry Andric
34006c3fb27SDimitry Andrictemplate <class BiIter>                                                      // Removed in C++20
3410b57cec5SDimitry Andric    bool
3420b57cec5SDimitry Andric    operator<(typename iterator_traits<BiIter>::value_type const* lhs,
3430b57cec5SDimitry Andric              const sub_match<BiIter>& rhs);
3440b57cec5SDimitry Andric
34506c3fb27SDimitry Andrictemplate <class BiIter>                                                      // Removed in C++20
3460b57cec5SDimitry Andric    bool
3470b57cec5SDimitry Andric    operator>(typename iterator_traits<BiIter>::value_type const* lhs,
3480b57cec5SDimitry Andric              const sub_match<BiIter>& rhs);
3490b57cec5SDimitry Andric
35006c3fb27SDimitry Andrictemplate <class BiIter>                                                      // Removed in C++20
3510b57cec5SDimitry Andric    bool
3520b57cec5SDimitry Andric    operator>=(typename iterator_traits<BiIter>::value_type const* lhs,
3530b57cec5SDimitry Andric               const sub_match<BiIter>& rhs);
3540b57cec5SDimitry Andric
35506c3fb27SDimitry Andrictemplate <class BiIter>                                                      // Removed in C++20
3560b57cec5SDimitry Andric    bool
3570b57cec5SDimitry Andric    operator<=(typename iterator_traits<BiIter>::value_type const* lhs,
3580b57cec5SDimitry Andric               const sub_match<BiIter>& rhs);
3590b57cec5SDimitry Andric
3600b57cec5SDimitry Andrictemplate <class BiIter>
3610b57cec5SDimitry Andric    bool
3620b57cec5SDimitry Andric    operator==(const sub_match<BiIter>& lhs,
3630b57cec5SDimitry Andric               typename iterator_traits<BiIter>::value_type const* rhs);
3640b57cec5SDimitry Andric
36506c3fb27SDimitry Andrictemplate <class BiIter>                                                      // Since C++20
36606c3fb27SDimitry Andric    auto
36706c3fb27SDimitry Andric    operator<=>(const sub_match<BiIter>& lhs,
36806c3fb27SDimitry Andric                typename iterator_traits<BiIter>::value_type const* rhs);
36906c3fb27SDimitry Andric
37006c3fb27SDimitry Andrictemplate <class BiIter, class ST, class SA>                                  // Removed in C++20
3710b57cec5SDimitry Andric    bool
3720b57cec5SDimitry Andric    operator!=(const sub_match<BiIter>& lhs,
3730b57cec5SDimitry Andric               typename iterator_traits<BiIter>::value_type const* rhs);
3740b57cec5SDimitry Andric
37506c3fb27SDimitry Andrictemplate <class BiIter>                                                      // Removed in C++20
3760b57cec5SDimitry Andric    bool
3770b57cec5SDimitry Andric    operator<(const sub_match<BiIter>& lhs,
3780b57cec5SDimitry Andric              typename iterator_traits<BiIter>::value_type const* rhs);
3790b57cec5SDimitry Andric
38006c3fb27SDimitry Andrictemplate <class BiIter>                                                      // Removed in C++20
3810b57cec5SDimitry Andric    bool
3820b57cec5SDimitry Andric    operator>(const sub_match<BiIter>& lhs,
3830b57cec5SDimitry Andric              typename iterator_traits<BiIter>::value_type const* rhs);
3840b57cec5SDimitry Andric
38506c3fb27SDimitry Andrictemplate <class BiIter>                                                      // Removed in C++20
3860b57cec5SDimitry Andric    bool
3870b57cec5SDimitry Andric    operator>=(const sub_match<BiIter>& lhs,
3880b57cec5SDimitry Andric               typename iterator_traits<BiIter>::value_type const* rhs);
3890b57cec5SDimitry Andric
39006c3fb27SDimitry Andrictemplate <class BiIter>                                                      // Removed in C++20
3910b57cec5SDimitry Andric    bool
3920b57cec5SDimitry Andric    operator<=(const sub_match<BiIter>& lhs,
3930b57cec5SDimitry Andric               typename iterator_traits<BiIter>::value_type const* rhs);
3940b57cec5SDimitry Andric
39506c3fb27SDimitry Andrictemplate <class BiIter>                                                      // Removed in C++20
3960b57cec5SDimitry Andric    bool
3970b57cec5SDimitry Andric    operator==(typename iterator_traits<BiIter>::value_type const& lhs,
3980b57cec5SDimitry Andric               const sub_match<BiIter>& rhs);
3990b57cec5SDimitry Andric
40006c3fb27SDimitry Andrictemplate <class BiIter>                                                      // Removed in C++20
4010b57cec5SDimitry Andric    bool
4020b57cec5SDimitry Andric    operator!=(typename iterator_traits<BiIter>::value_type const& lhs,
4030b57cec5SDimitry Andric               const sub_match<BiIter>& rhs);
4040b57cec5SDimitry Andric
40506c3fb27SDimitry Andrictemplate <class BiIter>                                                      // Removed in C++20
4060b57cec5SDimitry Andric    bool
4070b57cec5SDimitry Andric    operator<(typename iterator_traits<BiIter>::value_type const& lhs,
4080b57cec5SDimitry Andric              const sub_match<BiIter>& rhs);
4090b57cec5SDimitry Andric
41006c3fb27SDimitry Andrictemplate <class BiIter>                                                      // Removed in C++20
4110b57cec5SDimitry Andric    bool
4120b57cec5SDimitry Andric    operator>(typename iterator_traits<BiIter>::value_type const& lhs,
4130b57cec5SDimitry Andric              const sub_match<BiIter>& rhs);
4140b57cec5SDimitry Andric
41506c3fb27SDimitry Andrictemplate <class BiIter>                                                      // Removed in C++20
4160b57cec5SDimitry Andric    bool
4170b57cec5SDimitry Andric    operator>=(typename iterator_traits<BiIter>::value_type const& lhs,
4180b57cec5SDimitry Andric               const sub_match<BiIter>& rhs);
4190b57cec5SDimitry Andric
42006c3fb27SDimitry Andrictemplate <class BiIter>                                                      // Removed in C++20
4210b57cec5SDimitry Andric    bool
4220b57cec5SDimitry Andric    operator<=(typename iterator_traits<BiIter>::value_type const& lhs,
4230b57cec5SDimitry Andric               const sub_match<BiIter>& rhs);
4240b57cec5SDimitry Andric
4250b57cec5SDimitry Andrictemplate <class BiIter>
4260b57cec5SDimitry Andric    bool
4270b57cec5SDimitry Andric    operator==(const sub_match<BiIter>& lhs,
4280b57cec5SDimitry Andric               typename iterator_traits<BiIter>::value_type const& rhs);
4290b57cec5SDimitry Andric
43006c3fb27SDimitry Andrictemplate <class BiIter>                                                      // Since C++20
43106c3fb27SDimitry Andric    auto
43206c3fb27SDimitry Andric    operator<=>(const sub_match<BiIter>& lhs,
43306c3fb27SDimitry Andric                typename iterator_traits<BiIter>::value_type const& rhs);
43406c3fb27SDimitry Andric
43506c3fb27SDimitry Andrictemplate <class BiIter>                                                      // Removed in C++20
4360b57cec5SDimitry Andric    bool
4370b57cec5SDimitry Andric    operator!=(const sub_match<BiIter>& lhs,
4380b57cec5SDimitry Andric               typename iterator_traits<BiIter>::value_type const& rhs);
4390b57cec5SDimitry Andric
44006c3fb27SDimitry Andrictemplate <class BiIter>                                                      // Removed in C++20
4410b57cec5SDimitry Andric    bool
4420b57cec5SDimitry Andric    operator<(const sub_match<BiIter>& lhs,
4430b57cec5SDimitry Andric              typename iterator_traits<BiIter>::value_type const& rhs);
4440b57cec5SDimitry Andric
44506c3fb27SDimitry Andrictemplate <class BiIter>                                                      // Removed in C++20
4460b57cec5SDimitry Andric    bool
4470b57cec5SDimitry Andric    operator>(const sub_match<BiIter>& lhs,
4480b57cec5SDimitry Andric              typename iterator_traits<BiIter>::value_type const& rhs);
4490b57cec5SDimitry Andric
45006c3fb27SDimitry Andrictemplate <class BiIter>                                                      // Removed in C++20
4510b57cec5SDimitry Andric    bool
4520b57cec5SDimitry Andric    operator>=(const sub_match<BiIter>& lhs,
4530b57cec5SDimitry Andric               typename iterator_traits<BiIter>::value_type const& rhs);
4540b57cec5SDimitry Andric
45506c3fb27SDimitry Andrictemplate <class BiIter>                                                      // Removed in C++20
4560b57cec5SDimitry Andric    bool
4570b57cec5SDimitry Andric    operator<=(const sub_match<BiIter>& lhs,
4580b57cec5SDimitry Andric               typename iterator_traits<BiIter>::value_type const& rhs);
4590b57cec5SDimitry Andric
4600b57cec5SDimitry Andrictemplate <class charT, class ST, class BiIter>
4610b57cec5SDimitry Andric    basic_ostream<charT, ST>&
4620b57cec5SDimitry Andric    operator<<(basic_ostream<charT, ST>& os, const sub_match<BiIter>& m);
4630b57cec5SDimitry Andric
4640b57cec5SDimitry Andrictemplate <class BidirectionalIterator,
4650b57cec5SDimitry Andric          class Allocator = allocator<sub_match<BidirectionalIterator>>>
4660b57cec5SDimitry Andricclass match_results
4670b57cec5SDimitry Andric{
4680b57cec5SDimitry Andricpublic:
4690b57cec5SDimitry Andric    typedef sub_match<BidirectionalIterator>                  value_type;
4700b57cec5SDimitry Andric    typedef const value_type&                                 const_reference;
4710b57cec5SDimitry Andric    typedef value_type&                                       reference;
4720b57cec5SDimitry Andric    typedef /implementation-defined/                          const_iterator;
4730b57cec5SDimitry Andric    typedef const_iterator                                    iterator;
4740b57cec5SDimitry Andric    typedef typename iterator_traits<BidirectionalIterator>::difference_type difference_type;
4750b57cec5SDimitry Andric    typedef typename allocator_traits<Allocator>::size_type   size_type;
4760b57cec5SDimitry Andric    typedef Allocator                                         allocator_type;
4770b57cec5SDimitry Andric    typedef typename iterator_traits<BidirectionalIterator>::value_type char_type;
4780b57cec5SDimitry Andric    typedef basic_string<char_type>                           string_type;
4790b57cec5SDimitry Andric
4800b57cec5SDimitry Andric    // construct/copy/destroy:
481e8d8bef9SDimitry Andric    explicit match_results(const Allocator& a = Allocator()); // before C++20
482e8d8bef9SDimitry Andric    match_results() : match_results(Allocator()) {}           // C++20
483e8d8bef9SDimitry Andric    explicit match_results(const Allocator& a);               // C++20
4840b57cec5SDimitry Andric    match_results(const match_results& m);
4850b57cec5SDimitry Andric    match_results(match_results&& m) noexcept;
4860b57cec5SDimitry Andric    match_results& operator=(const match_results& m);
4870b57cec5SDimitry Andric    match_results& operator=(match_results&& m);
4880b57cec5SDimitry Andric    ~match_results();
4890b57cec5SDimitry Andric
4900b57cec5SDimitry Andric    bool ready() const;
4910b57cec5SDimitry Andric
4920b57cec5SDimitry Andric    // size:
4930b57cec5SDimitry Andric    size_type size() const;
4940b57cec5SDimitry Andric    size_type max_size() const;
4950b57cec5SDimitry Andric    bool empty() const;
4960b57cec5SDimitry Andric
4970b57cec5SDimitry Andric    // element access:
4980b57cec5SDimitry Andric    difference_type length(size_type sub = 0) const;
4990b57cec5SDimitry Andric    difference_type position(size_type sub = 0) const;
5000b57cec5SDimitry Andric    string_type str(size_type sub = 0) const;
5010b57cec5SDimitry Andric    const_reference operator[](size_type n) const;
5020b57cec5SDimitry Andric
5030b57cec5SDimitry Andric    const_reference prefix() const;
5040b57cec5SDimitry Andric    const_reference suffix() const;
5050b57cec5SDimitry Andric
5060b57cec5SDimitry Andric    const_iterator begin() const;
5070b57cec5SDimitry Andric    const_iterator end() const;
5080b57cec5SDimitry Andric    const_iterator cbegin() const;
5090b57cec5SDimitry Andric    const_iterator cend() const;
5100b57cec5SDimitry Andric
5110b57cec5SDimitry Andric    // format:
5120b57cec5SDimitry Andric    template <class OutputIter>
5130b57cec5SDimitry Andric        OutputIter
5140b57cec5SDimitry Andric        format(OutputIter out, const char_type* fmt_first,
5150b57cec5SDimitry Andric               const char_type* fmt_last,
5160b57cec5SDimitry Andric               regex_constants::match_flag_type flags = regex_constants::format_default) const;
5170b57cec5SDimitry Andric    template <class OutputIter, class ST, class SA>
5180b57cec5SDimitry Andric        OutputIter
5190b57cec5SDimitry Andric        format(OutputIter out, const basic_string<char_type, ST, SA>& fmt,
5200b57cec5SDimitry Andric               regex_constants::match_flag_type flags = regex_constants::format_default) const;
5210b57cec5SDimitry Andric    template <class ST, class SA>
5220b57cec5SDimitry Andric        basic_string<char_type, ST, SA>
5230b57cec5SDimitry Andric        format(const basic_string<char_type, ST, SA>& fmt,
5240b57cec5SDimitry Andric               regex_constants::match_flag_type flags = regex_constants::format_default) const;
5250b57cec5SDimitry Andric    string_type
5260b57cec5SDimitry Andric        format(const char_type* fmt,
5270b57cec5SDimitry Andric               regex_constants::match_flag_type flags = regex_constants::format_default) const;
5280b57cec5SDimitry Andric
5290b57cec5SDimitry Andric    // allocator:
5300b57cec5SDimitry Andric    allocator_type get_allocator() const;
5310b57cec5SDimitry Andric
5320b57cec5SDimitry Andric    // swap:
5330b57cec5SDimitry Andric    void swap(match_results& that);
5340b57cec5SDimitry Andric};
5350b57cec5SDimitry Andric
5360b57cec5SDimitry Andrictypedef match_results<const char*>             cmatch;
5370b57cec5SDimitry Andrictypedef match_results<const wchar_t*>          wcmatch;
5380b57cec5SDimitry Andrictypedef match_results<string::const_iterator>  smatch;
5390b57cec5SDimitry Andrictypedef match_results<wstring::const_iterator> wsmatch;
5400b57cec5SDimitry Andric
5410b57cec5SDimitry Andrictemplate <class BidirectionalIterator, class Allocator>
5420b57cec5SDimitry Andric    bool
5430b57cec5SDimitry Andric    operator==(const match_results<BidirectionalIterator, Allocator>& m1,
5440b57cec5SDimitry Andric               const match_results<BidirectionalIterator, Allocator>& m2);
5450b57cec5SDimitry Andric
54606c3fb27SDimitry Andrictemplate <class BidirectionalIterator, class Allocator>                    // Removed in C++20
5470b57cec5SDimitry Andric    bool
5480b57cec5SDimitry Andric    operator!=(const match_results<BidirectionalIterator, Allocator>& m1,
5490b57cec5SDimitry Andric               const match_results<BidirectionalIterator, Allocator>& m2);
5500b57cec5SDimitry Andric
5510b57cec5SDimitry Andrictemplate <class BidirectionalIterator, class Allocator>
5520b57cec5SDimitry Andric    void
5530b57cec5SDimitry Andric    swap(match_results<BidirectionalIterator, Allocator>& m1,
5540b57cec5SDimitry Andric         match_results<BidirectionalIterator, Allocator>& m2);
5550b57cec5SDimitry Andric
5560b57cec5SDimitry Andrictemplate <class BidirectionalIterator, class Allocator, class charT, class traits>
5570b57cec5SDimitry Andric    bool
5580b57cec5SDimitry Andric    regex_match(BidirectionalIterator first, BidirectionalIterator last,
5590b57cec5SDimitry Andric                match_results<BidirectionalIterator, Allocator>& m,
5600b57cec5SDimitry Andric                const basic_regex<charT, traits>& e,
5610b57cec5SDimitry Andric                regex_constants::match_flag_type flags = regex_constants::match_default);
5620b57cec5SDimitry Andric
5630b57cec5SDimitry Andrictemplate <class BidirectionalIterator, class charT, class traits>
5640b57cec5SDimitry Andric    bool
5650b57cec5SDimitry Andric    regex_match(BidirectionalIterator first, BidirectionalIterator last,
5660b57cec5SDimitry Andric                const basic_regex<charT, traits>& e,
5670b57cec5SDimitry Andric                regex_constants::match_flag_type flags = regex_constants::match_default);
5680b57cec5SDimitry Andric
5690b57cec5SDimitry Andrictemplate <class charT, class Allocator, class traits>
5700b57cec5SDimitry Andric    bool
5710b57cec5SDimitry Andric    regex_match(const charT* str, match_results<const charT*, Allocator>& m,
5720b57cec5SDimitry Andric                const basic_regex<charT, traits>& e,
5730b57cec5SDimitry Andric                regex_constants::match_flag_type flags = regex_constants::match_default);
5740b57cec5SDimitry Andric
5750b57cec5SDimitry Andrictemplate <class ST, class SA, class Allocator, class charT, class traits>
5760b57cec5SDimitry Andric    bool
5770b57cec5SDimitry Andric    regex_match(const basic_string<charT, ST, SA>& s,
5780b57cec5SDimitry Andric                match_results<typename basic_string<charT, ST, SA>::const_iterator, Allocator>& m,
5790b57cec5SDimitry Andric                const basic_regex<charT, traits>& e,
5800b57cec5SDimitry Andric                regex_constants::match_flag_type flags = regex_constants::match_default);
5810b57cec5SDimitry Andric
5820b57cec5SDimitry Andrictemplate <class ST, class SA, class Allocator, class charT, class traits>
5830b57cec5SDimitry Andric    bool
5840b57cec5SDimitry Andric    regex_match(const basic_string<charT, ST, SA>&& s,
5850b57cec5SDimitry Andric                match_results<typename basic_string<charT, ST, SA>::const_iterator, Allocator>& m,
5860b57cec5SDimitry Andric                const basic_regex<charT, traits>& e,
5870b57cec5SDimitry Andric                regex_constants::match_flag_type flags = regex_constants::match_default) = delete; // C++14
5880b57cec5SDimitry Andric
5890b57cec5SDimitry Andrictemplate <class charT, class traits>
5900b57cec5SDimitry Andric    bool
5910b57cec5SDimitry Andric    regex_match(const charT* str, const basic_regex<charT, traits>& e,
5920b57cec5SDimitry Andric                regex_constants::match_flag_type flags = regex_constants::match_default);
5930b57cec5SDimitry Andric
5940b57cec5SDimitry Andrictemplate <class ST, class SA, class charT, class traits>
5950b57cec5SDimitry Andric    bool
5960b57cec5SDimitry Andric    regex_match(const basic_string<charT, ST, SA>& s,
5970b57cec5SDimitry Andric                const basic_regex<charT, traits>& e,
5980b57cec5SDimitry Andric                regex_constants::match_flag_type flags = regex_constants::match_default);
5990b57cec5SDimitry Andric
6000b57cec5SDimitry Andrictemplate <class BidirectionalIterator, class Allocator, class charT, class traits>
6010b57cec5SDimitry Andric    bool
6020b57cec5SDimitry Andric    regex_search(BidirectionalIterator first, BidirectionalIterator last,
6030b57cec5SDimitry Andric                 match_results<BidirectionalIterator, Allocator>& m,
6040b57cec5SDimitry Andric                 const basic_regex<charT, traits>& e,
6050b57cec5SDimitry Andric                 regex_constants::match_flag_type flags = regex_constants::match_default);
6060b57cec5SDimitry Andric
6070b57cec5SDimitry Andrictemplate <class BidirectionalIterator, class charT, class traits>
6080b57cec5SDimitry Andric    bool
6090b57cec5SDimitry Andric    regex_search(BidirectionalIterator first, BidirectionalIterator last,
6100b57cec5SDimitry Andric                 const basic_regex<charT, traits>& e,
6110b57cec5SDimitry Andric                 regex_constants::match_flag_type flags = regex_constants::match_default);
6120b57cec5SDimitry Andric
6130b57cec5SDimitry Andrictemplate <class charT, class Allocator, class traits>
6140b57cec5SDimitry Andric    bool
6150b57cec5SDimitry Andric    regex_search(const charT* str, match_results<const charT*, Allocator>& m,
6160b57cec5SDimitry Andric                 const basic_regex<charT, traits>& e,
6170b57cec5SDimitry Andric                 regex_constants::match_flag_type flags = regex_constants::match_default);
6180b57cec5SDimitry Andric
6190b57cec5SDimitry Andrictemplate <class charT, class traits>
6200b57cec5SDimitry Andric    bool
6210b57cec5SDimitry Andric    regex_search(const charT* str, const basic_regex<charT, traits>& e,
6220b57cec5SDimitry Andric                 regex_constants::match_flag_type flags = regex_constants::match_default);
6230b57cec5SDimitry Andric
6240b57cec5SDimitry Andrictemplate <class ST, class SA, class charT, class traits>
6250b57cec5SDimitry Andric    bool
6260b57cec5SDimitry Andric    regex_search(const basic_string<charT, ST, SA>& s,
6270b57cec5SDimitry Andric                 const basic_regex<charT, traits>& e,
6280b57cec5SDimitry Andric                 regex_constants::match_flag_type flags = regex_constants::match_default);
6290b57cec5SDimitry Andric
6300b57cec5SDimitry Andrictemplate <class ST, class SA, class Allocator, class charT, class traits>
6310b57cec5SDimitry Andric    bool
6320b57cec5SDimitry Andric    regex_search(const basic_string<charT, ST, SA>& s,
6330b57cec5SDimitry Andric                 match_results<typename basic_string<charT, ST, SA>::const_iterator, Allocator>& m,
6340b57cec5SDimitry Andric                 const basic_regex<charT, traits>& e,
6350b57cec5SDimitry Andric                 regex_constants::match_flag_type flags = regex_constants::match_default);
6360b57cec5SDimitry Andric
6370b57cec5SDimitry Andrictemplate <class ST, class SA, class Allocator, class charT, class traits>
6380b57cec5SDimitry Andric    bool
6390b57cec5SDimitry Andric    regex_search(const basic_string<charT, ST, SA>&& s,
6400b57cec5SDimitry Andric                 match_results<typename basic_string<charT, ST, SA>::const_iterator, Allocator>& m,
6410b57cec5SDimitry Andric                 const basic_regex<charT, traits>& e,
6420b57cec5SDimitry Andric                 regex_constants::match_flag_type flags = regex_constants::match_default) = delete; // C++14
6430b57cec5SDimitry Andric
6440b57cec5SDimitry Andrictemplate <class OutputIterator, class BidirectionalIterator,
6450b57cec5SDimitry Andric          class traits, class charT, class ST, class SA>
6460b57cec5SDimitry Andric    OutputIterator
6470b57cec5SDimitry Andric    regex_replace(OutputIterator out,
6480b57cec5SDimitry Andric                  BidirectionalIterator first, BidirectionalIterator last,
6490b57cec5SDimitry Andric                  const basic_regex<charT, traits>& e,
6500b57cec5SDimitry Andric                  const basic_string<charT, ST, SA>& fmt,
6510b57cec5SDimitry Andric                  regex_constants::match_flag_type flags = regex_constants::match_default);
6520b57cec5SDimitry Andric
6530b57cec5SDimitry Andrictemplate <class OutputIterator, class BidirectionalIterator,
6540b57cec5SDimitry Andric          class traits, class charT>
6550b57cec5SDimitry Andric    OutputIterator
6560b57cec5SDimitry Andric    regex_replace(OutputIterator out,
6570b57cec5SDimitry Andric                  BidirectionalIterator first, BidirectionalIterator last,
6580b57cec5SDimitry Andric                  const basic_regex<charT, traits>& e, const charT* fmt,
6590b57cec5SDimitry Andric                  regex_constants::match_flag_type flags = regex_constants::match_default);
6600b57cec5SDimitry Andric
6615ffd83dbSDimitry Andrictemplate <class traits, class charT, class ST, class SA, class FST, class FSA>
6620b57cec5SDimitry Andric    basic_string<charT, ST, SA>
6630b57cec5SDimitry Andric    regex_replace(const basic_string<charT, ST, SA>& s,
6640b57cec5SDimitry Andric                  const basic_regex<charT, traits>& e,
6650b57cec5SDimitry Andric                  const basic_string<charT, FST, FSA>& fmt,
6660b57cec5SDimitry Andric                  regex_constants::match_flag_type flags = regex_constants::match_default);
6670b57cec5SDimitry Andric
6680b57cec5SDimitry Andrictemplate <class traits, class charT, class ST, class SA>
6690b57cec5SDimitry Andric    basic_string<charT, ST, SA>
6700b57cec5SDimitry Andric    regex_replace(const basic_string<charT, ST, SA>& s,
6710b57cec5SDimitry Andric                  const basic_regex<charT, traits>& e, const charT* fmt,
6720b57cec5SDimitry Andric                  regex_constants::match_flag_type flags = regex_constants::match_default);
6730b57cec5SDimitry Andric
6740b57cec5SDimitry Andrictemplate <class traits, class charT, class ST, class SA>
6750b57cec5SDimitry Andric    basic_string<charT>
6760b57cec5SDimitry Andric    regex_replace(const charT* s,
6770b57cec5SDimitry Andric                  const basic_regex<charT, traits>& e,
6780b57cec5SDimitry Andric                  const basic_string<charT, ST, SA>& fmt,
6790b57cec5SDimitry Andric                  regex_constants::match_flag_type flags = regex_constants::match_default);
6800b57cec5SDimitry Andric
6810b57cec5SDimitry Andrictemplate <class traits, class charT>
6820b57cec5SDimitry Andric    basic_string<charT>
6830b57cec5SDimitry Andric    regex_replace(const charT* s,
6840b57cec5SDimitry Andric                  const basic_regex<charT, traits>& e,
6850b57cec5SDimitry Andric                  const charT* fmt,
6860b57cec5SDimitry Andric                  regex_constants::match_flag_type flags = regex_constants::match_default);
6870b57cec5SDimitry Andric
6880b57cec5SDimitry Andrictemplate <class BidirectionalIterator,
6890b57cec5SDimitry Andric          class charT = typename iterator_traits< BidirectionalIterator>::value_type,
6900b57cec5SDimitry Andric          class traits = regex_traits<charT>>
6910b57cec5SDimitry Andricclass regex_iterator
6920b57cec5SDimitry Andric{
6930b57cec5SDimitry Andricpublic:
6940b57cec5SDimitry Andric    typedef basic_regex<charT, traits>           regex_type;
6950b57cec5SDimitry Andric    typedef match_results<BidirectionalIterator> value_type;
6960b57cec5SDimitry Andric    typedef ptrdiff_t                            difference_type;
6970b57cec5SDimitry Andric    typedef const value_type*                    pointer;
6980b57cec5SDimitry Andric    typedef const value_type&                    reference;
6990b57cec5SDimitry Andric    typedef forward_iterator_tag                 iterator_category;
7005f757f3fSDimitry Andric    typedef input_iterator_tag                   iterator_concept; // since C++20
7010b57cec5SDimitry Andric
7020b57cec5SDimitry Andric    regex_iterator();
7030b57cec5SDimitry Andric    regex_iterator(BidirectionalIterator a, BidirectionalIterator b,
7040b57cec5SDimitry Andric                   const regex_type& re,
7050b57cec5SDimitry Andric                   regex_constants::match_flag_type m = regex_constants::match_default);
7065ffd83dbSDimitry Andric    regex_iterator(BidirectionalIterator a, BidirectionalIterator b,
7075ffd83dbSDimitry Andric                   const regex_type&& re,
7085ffd83dbSDimitry Andric                   regex_constants::match_flag_type m
7090b57cec5SDimitry Andric                                     = regex_constants::match_default) = delete; // C++14
7100b57cec5SDimitry Andric    regex_iterator(const regex_iterator&);
7110b57cec5SDimitry Andric    regex_iterator& operator=(const regex_iterator&);
7120b57cec5SDimitry Andric
7130b57cec5SDimitry Andric    bool operator==(const regex_iterator&) const;
71406c3fb27SDimitry Andric    bool operator==(default_sentinel_t) const { return *this == regex_iterator(); } // since C++20
71506c3fb27SDimitry Andric    bool operator!=(const regex_iterator&) const;                                   // Removed in C++20
7160b57cec5SDimitry Andric
7170b57cec5SDimitry Andric    const value_type& operator*() const;
7180b57cec5SDimitry Andric    const value_type* operator->() const;
7190b57cec5SDimitry Andric
7200b57cec5SDimitry Andric    regex_iterator& operator++();
7210b57cec5SDimitry Andric    regex_iterator operator++(int);
7220b57cec5SDimitry Andric};
7230b57cec5SDimitry Andric
7240b57cec5SDimitry Andrictypedef regex_iterator<const char*>             cregex_iterator;
7250b57cec5SDimitry Andrictypedef regex_iterator<const wchar_t*>          wcregex_iterator;
7260b57cec5SDimitry Andrictypedef regex_iterator<string::const_iterator>  sregex_iterator;
7270b57cec5SDimitry Andrictypedef regex_iterator<wstring::const_iterator> wsregex_iterator;
7280b57cec5SDimitry Andric
7290b57cec5SDimitry Andrictemplate <class BidirectionalIterator,
7300b57cec5SDimitry Andric          class charT = typename iterator_traits<BidirectionalIterator>::value_type,
7310b57cec5SDimitry Andric          class traits = regex_traits<charT>>
7320b57cec5SDimitry Andricclass regex_token_iterator
7330b57cec5SDimitry Andric{
7340b57cec5SDimitry Andricpublic:
7350b57cec5SDimitry Andric    typedef basic_regex<charT, traits>       regex_type;
7360b57cec5SDimitry Andric    typedef sub_match<BidirectionalIterator> value_type;
7370b57cec5SDimitry Andric    typedef ptrdiff_t                        difference_type;
7380b57cec5SDimitry Andric    typedef const value_type*                pointer;
7390b57cec5SDimitry Andric    typedef const value_type&                reference;
7400b57cec5SDimitry Andric    typedef forward_iterator_tag             iterator_category;
7415f757f3fSDimitry Andric    typedef input_iterator_tag               iterator_concept; // since C++20
7420b57cec5SDimitry Andric
7430b57cec5SDimitry Andric    regex_token_iterator();
7440b57cec5SDimitry Andric    regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b,
7450b57cec5SDimitry Andric                         const regex_type& re, int submatch = 0,
7460b57cec5SDimitry Andric                         regex_constants::match_flag_type m = regex_constants::match_default);
7470b57cec5SDimitry Andric    regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b,
7480b57cec5SDimitry Andric                         const regex_type&& re, int submatch = 0,
7490b57cec5SDimitry Andric                         regex_constants::match_flag_type m = regex_constants::match_default) = delete; // C++14
7500b57cec5SDimitry Andric    regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b,
7510b57cec5SDimitry Andric                         const regex_type& re, const vector<int>& submatches,
7520b57cec5SDimitry Andric                         regex_constants::match_flag_type m = regex_constants::match_default);
7530b57cec5SDimitry Andric    regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b,
7540b57cec5SDimitry Andric                         const regex_type&& re, const vector<int>& submatches,
7550b57cec5SDimitry Andric                         regex_constants::match_flag_type m = regex_constants::match_default) = delete; // C++14
7560b57cec5SDimitry Andric    regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b,
7570b57cec5SDimitry Andric                         const regex_type& re, initializer_list<int> submatches,
7580b57cec5SDimitry Andric                         regex_constants::match_flag_type m = regex_constants::match_default);
7590b57cec5SDimitry Andric    regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b,
7600b57cec5SDimitry Andric                         const regex_type&& re, initializer_list<int> submatches,
7610b57cec5SDimitry Andric                         regex_constants::match_flag_type m = regex_constants::match_default) = delete; // C++14
7620b57cec5SDimitry Andric    template <size_t N>
7630b57cec5SDimitry Andric        regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b,
7640b57cec5SDimitry Andric                             const regex_type& re, const int (&submatches)[N],
7650b57cec5SDimitry Andric                             regex_constants::match_flag_type m = regex_constants::match_default);
7660b57cec5SDimitry Andric    template <size_t N>
7670b57cec5SDimitry Andric        regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b,
7685ffd83dbSDimitry Andric                             const regex_type&& re, const int (&submatches)[N],
7695ffd83dbSDimitry Andric                             regex_constants::match_flag_type m = regex_constants::match_default) = delete; // C++14
7700b57cec5SDimitry Andric    regex_token_iterator(const regex_token_iterator&);
7710b57cec5SDimitry Andric    regex_token_iterator& operator=(const regex_token_iterator&);
7720b57cec5SDimitry Andric
7730b57cec5SDimitry Andric    bool operator==(const regex_token_iterator&) const;
77406c3fb27SDimitry Andric    bool operator==(default_sentinel_t) const { return *this == regex_token_iterator(); } // since C++20
77506c3fb27SDimitry Andric    bool operator!=(const regex_token_iterator&) const;                                   // Removed in C++20
7760b57cec5SDimitry Andric
7770b57cec5SDimitry Andric    const value_type& operator*() const;
7780b57cec5SDimitry Andric    const value_type* operator->() const;
7790b57cec5SDimitry Andric
7800b57cec5SDimitry Andric    regex_token_iterator& operator++();
7810b57cec5SDimitry Andric    regex_token_iterator operator++(int);
7820b57cec5SDimitry Andric};
7830b57cec5SDimitry Andric
7840b57cec5SDimitry Andrictypedef regex_token_iterator<const char*>             cregex_token_iterator;
7850b57cec5SDimitry Andrictypedef regex_token_iterator<const wchar_t*>          wcregex_token_iterator;
7860b57cec5SDimitry Andrictypedef regex_token_iterator<string::const_iterator>  sregex_token_iterator;
7870b57cec5SDimitry Andrictypedef regex_token_iterator<wstring::const_iterator> wsregex_token_iterator;
7880b57cec5SDimitry Andric
7890b57cec5SDimitry Andric} // std
7900b57cec5SDimitry Andric*/
7910b57cec5SDimitry Andric
79281ad6265SDimitry Andric#include <__algorithm/find.h>
79381ad6265SDimitry Andric#include <__algorithm/search.h>
79481ad6265SDimitry Andric#include <__assert> // all public C++ headers provide the assertion handler
79506c3fb27SDimitry Andric#include <__availability>
7960b57cec5SDimitry Andric#include <__config>
79781ad6265SDimitry Andric#include <__iterator/back_insert_iterator.h>
79806c3fb27SDimitry Andric#include <__iterator/default_sentinel.h>
799fe6060f1SDimitry Andric#include <__iterator/wrap_iter.h>
8000b57cec5SDimitry Andric#include <__locale>
80106c3fb27SDimitry Andric#include <__memory/shared_ptr.h>
802bdd1243dSDimitry Andric#include <__memory_resource/polymorphic_allocator.h>
80306c3fb27SDimitry Andric#include <__type_traits/is_swappable.h>
80481ad6265SDimitry Andric#include <__utility/move.h>
805bdd1243dSDimitry Andric#include <__utility/pair.h>
80681ad6265SDimitry Andric#include <__utility/swap.h>
80706c3fb27SDimitry Andric#include <__verbose_abort>
8080b57cec5SDimitry Andric#include <deque>
809fe6060f1SDimitry Andric#include <stdexcept>
810fe6060f1SDimitry Andric#include <string>
811fe6060f1SDimitry Andric#include <vector>
8120b57cec5SDimitry Andric#include <version>
8130b57cec5SDimitry Andric
81481ad6265SDimitry Andric// standard-mandated includes
81581ad6265SDimitry Andric
81681ad6265SDimitry Andric// [iterator.range]
81781ad6265SDimitry Andric#include <__iterator/access.h>
81881ad6265SDimitry Andric#include <__iterator/data.h>
81981ad6265SDimitry Andric#include <__iterator/empty.h>
82081ad6265SDimitry Andric#include <__iterator/reverse_access.h>
82181ad6265SDimitry Andric#include <__iterator/size.h>
82281ad6265SDimitry Andric
82381ad6265SDimitry Andric// [re.syn]
82481ad6265SDimitry Andric#include <compare>
82581ad6265SDimitry Andric#include <initializer_list>
82681ad6265SDimitry Andric
8270b57cec5SDimitry Andric#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
8280b57cec5SDimitry Andric#  pragma GCC system_header
8290b57cec5SDimitry Andric#endif
8300b57cec5SDimitry Andric
8310b57cec5SDimitry Andric_LIBCPP_PUSH_MACROS
8320b57cec5SDimitry Andric#include <__undef_macros>
8330b57cec5SDimitry Andric
8340b57cec5SDimitry Andric#define _LIBCPP_REGEX_COMPLEXITY_FACTOR 4096
8350b57cec5SDimitry Andric
8360b57cec5SDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD
8370b57cec5SDimitry Andric
838cb14a3feSDimitry Andricnamespace regex_constants {
8390b57cec5SDimitry Andric
8400b57cec5SDimitry Andric// syntax_option_type
8410b57cec5SDimitry Andric
842cb14a3feSDimitry Andricenum syntax_option_type {
8430b57cec5SDimitry Andric  icase    = 1 << 0,
8440b57cec5SDimitry Andric  nosubs   = 1 << 1,
8450b57cec5SDimitry Andric  optimize = 1 << 2,
8460b57cec5SDimitry Andric  collate  = 1 << 3,
8470b57cec5SDimitry Andric#ifdef _LIBCPP_ABI_REGEX_CONSTANTS_NONZERO
8480b57cec5SDimitry Andric  ECMAScript = 1 << 9,
8490b57cec5SDimitry Andric#else
8500b57cec5SDimitry Andric  ECMAScript = 0,
8510b57cec5SDimitry Andric#endif
8520b57cec5SDimitry Andric  basic    = 1 << 4,
8530b57cec5SDimitry Andric  extended = 1 << 5,
8540b57cec5SDimitry Andric  awk      = 1 << 6,
8550b57cec5SDimitry Andric  grep     = 1 << 7,
856e8d8bef9SDimitry Andric  egrep    = 1 << 8,
857e8d8bef9SDimitry Andric  // 1 << 9 may be used by ECMAScript
858e8d8bef9SDimitry Andric  multiline = 1 << 10
8590b57cec5SDimitry Andric};
8600b57cec5SDimitry Andric
861cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI inline _LIBCPP_CONSTEXPR syntax_option_type __get_grammar(syntax_option_type __g) {
8620b57cec5SDimitry Andric#ifdef _LIBCPP_ABI_REGEX_CONSTANTS_NONZERO
8630b57cec5SDimitry Andric  return static_cast<syntax_option_type>(__g & 0x3F0);
8640b57cec5SDimitry Andric#else
8650b57cec5SDimitry Andric  return static_cast<syntax_option_type>(__g & 0x1F0);
8660b57cec5SDimitry Andric#endif
8670b57cec5SDimitry Andric}
8680b57cec5SDimitry Andric
869cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR syntax_option_type operator~(syntax_option_type __x) {
8700b57cec5SDimitry Andric  return syntax_option_type(~int(__x) & 0x1FF);
8710b57cec5SDimitry Andric}
8720b57cec5SDimitry Andric
873cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR syntax_option_type
874cb14a3feSDimitry Andricoperator&(syntax_option_type __x, syntax_option_type __y) {
8750b57cec5SDimitry Andric  return syntax_option_type(int(__x) & int(__y));
8760b57cec5SDimitry Andric}
8770b57cec5SDimitry Andric
878cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR syntax_option_type
879cb14a3feSDimitry Andricoperator|(syntax_option_type __x, syntax_option_type __y) {
8800b57cec5SDimitry Andric  return syntax_option_type(int(__x) | int(__y));
8810b57cec5SDimitry Andric}
8820b57cec5SDimitry Andric
883cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR syntax_option_type
884cb14a3feSDimitry Andricoperator^(syntax_option_type __x, syntax_option_type __y) {
8850b57cec5SDimitry Andric  return syntax_option_type(int(__x) ^ int(__y));
8860b57cec5SDimitry Andric}
8870b57cec5SDimitry Andric
888cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI syntax_option_type& operator&=(syntax_option_type& __x, syntax_option_type __y) {
8890b57cec5SDimitry Andric  __x = __x & __y;
8900b57cec5SDimitry Andric  return __x;
8910b57cec5SDimitry Andric}
8920b57cec5SDimitry Andric
893cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI syntax_option_type& operator|=(syntax_option_type& __x, syntax_option_type __y) {
8940b57cec5SDimitry Andric  __x = __x | __y;
8950b57cec5SDimitry Andric  return __x;
8960b57cec5SDimitry Andric}
8970b57cec5SDimitry Andric
898cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI syntax_option_type& operator^=(syntax_option_type& __x, syntax_option_type __y) {
8990b57cec5SDimitry Andric  __x = __x ^ __y;
9000b57cec5SDimitry Andric  return __x;
9010b57cec5SDimitry Andric}
9020b57cec5SDimitry Andric
9030b57cec5SDimitry Andric// match_flag_type
9040b57cec5SDimitry Andric
905cb14a3feSDimitry Andricenum match_flag_type {
9060b57cec5SDimitry Andric  match_default     = 0,
9070b57cec5SDimitry Andric  match_not_bol     = 1 << 0,
9080b57cec5SDimitry Andric  match_not_eol     = 1 << 1,
9090b57cec5SDimitry Andric  match_not_bow     = 1 << 2,
9100b57cec5SDimitry Andric  match_not_eow     = 1 << 3,
9110b57cec5SDimitry Andric  match_any         = 1 << 4,
9120b57cec5SDimitry Andric  match_not_null    = 1 << 5,
9130b57cec5SDimitry Andric  match_continuous  = 1 << 6,
9140b57cec5SDimitry Andric  match_prev_avail  = 1 << 7,
9150b57cec5SDimitry Andric  format_default    = 0,
9160b57cec5SDimitry Andric  format_sed        = 1 << 8,
9170b57cec5SDimitry Andric  format_no_copy    = 1 << 9,
9180b57cec5SDimitry Andric  format_first_only = 1 << 10,
9190b57cec5SDimitry Andric  __no_update_pos   = 1 << 11,
9200b57cec5SDimitry Andric  __full_match      = 1 << 12
9210b57cec5SDimitry Andric};
9220b57cec5SDimitry Andric
923cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR match_flag_type operator~(match_flag_type __x) {
9240b57cec5SDimitry Andric  return match_flag_type(~int(__x) & 0x0FFF);
9250b57cec5SDimitry Andric}
9260b57cec5SDimitry Andric
927cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR match_flag_type operator&(match_flag_type __x, match_flag_type __y) {
9280b57cec5SDimitry Andric  return match_flag_type(int(__x) & int(__y));
9290b57cec5SDimitry Andric}
9300b57cec5SDimitry Andric
931cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR match_flag_type operator|(match_flag_type __x, match_flag_type __y) {
9320b57cec5SDimitry Andric  return match_flag_type(int(__x) | int(__y));
9330b57cec5SDimitry Andric}
9340b57cec5SDimitry Andric
935cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR match_flag_type operator^(match_flag_type __x, match_flag_type __y) {
9360b57cec5SDimitry Andric  return match_flag_type(int(__x) ^ int(__y));
9370b57cec5SDimitry Andric}
9380b57cec5SDimitry Andric
939cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI match_flag_type& operator&=(match_flag_type& __x, match_flag_type __y) {
9400b57cec5SDimitry Andric  __x = __x & __y;
9410b57cec5SDimitry Andric  return __x;
9420b57cec5SDimitry Andric}
9430b57cec5SDimitry Andric
944cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI match_flag_type& operator|=(match_flag_type& __x, match_flag_type __y) {
9450b57cec5SDimitry Andric  __x = __x | __y;
9460b57cec5SDimitry Andric  return __x;
9470b57cec5SDimitry Andric}
9480b57cec5SDimitry Andric
949cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI match_flag_type& operator^=(match_flag_type& __x, match_flag_type __y) {
9500b57cec5SDimitry Andric  __x = __x ^ __y;
9510b57cec5SDimitry Andric  return __x;
9520b57cec5SDimitry Andric}
9530b57cec5SDimitry Andric
954cb14a3feSDimitry Andricenum error_type {
9550b57cec5SDimitry Andric  error_collate = 1,
9560b57cec5SDimitry Andric  error_ctype,
9570b57cec5SDimitry Andric  error_escape,
9580b57cec5SDimitry Andric  error_backref,
9590b57cec5SDimitry Andric  error_brack,
9600b57cec5SDimitry Andric  error_paren,
9610b57cec5SDimitry Andric  error_brace,
9620b57cec5SDimitry Andric  error_badbrace,
9630b57cec5SDimitry Andric  error_range,
9640b57cec5SDimitry Andric  error_space,
9650b57cec5SDimitry Andric  error_badrepeat,
9660b57cec5SDimitry Andric  error_complexity,
9670b57cec5SDimitry Andric  error_stack,
9680b57cec5SDimitry Andric  __re_err_grammar,
9690b57cec5SDimitry Andric  __re_err_empty,
970480093f4SDimitry Andric  __re_err_unknown,
971480093f4SDimitry Andric  __re_err_parse
9720b57cec5SDimitry Andric};
9730b57cec5SDimitry Andric
9740eae32dcSDimitry Andric} // namespace regex_constants
9750b57cec5SDimitry Andric
976cb14a3feSDimitry Andricclass _LIBCPP_EXPORTED_FROM_ABI regex_error : public runtime_error {
9770b57cec5SDimitry Andric  regex_constants::error_type __code_;
978cb14a3feSDimitry Andric
9790b57cec5SDimitry Andricpublic:
9800b57cec5SDimitry Andric  explicit regex_error(regex_constants::error_type __ecode);
98106c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI regex_error(const regex_error&) _NOEXCEPT = default;
982bdd1243dSDimitry Andric  ~regex_error() _NOEXCEPT override;
983cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI regex_constants::error_type code() const { return __code_; }
9840b57cec5SDimitry Andric};
9850b57cec5SDimitry Andric
9860b57cec5SDimitry Andrictemplate <regex_constants::error_type _Ev>
987cb14a3feSDimitry Andric_LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI void __throw_regex_error() {
98806c3fb27SDimitry Andric#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
9890b57cec5SDimitry Andric  throw regex_error(_Ev);
9900b57cec5SDimitry Andric#else
99106c3fb27SDimitry Andric  _LIBCPP_VERBOSE_ABORT("regex_error was thrown in -fno-exceptions mode");
9920b57cec5SDimitry Andric#endif
9930b57cec5SDimitry Andric}
9940b57cec5SDimitry Andric
9950b57cec5SDimitry Andrictemplate <class _CharT>
996cb14a3feSDimitry Andricstruct _LIBCPP_TEMPLATE_VIS regex_traits {
9970b57cec5SDimitry Andricpublic:
9980b57cec5SDimitry Andric  typedef _CharT char_type;
9990b57cec5SDimitry Andric  typedef basic_string<char_type> string_type;
10000b57cec5SDimitry Andric  typedef locale locale_type;
1001bdd1243dSDimitry Andric#if defined(__BIONIC__) || defined(_NEWLIB_VERSION)
10025ffd83dbSDimitry Andric  // Originally bionic's ctype_base used its own ctype masks because the
10035ffd83dbSDimitry Andric  // builtin ctype implementation wasn't in libc++ yet. Bionic's ctype mask
10045ffd83dbSDimitry Andric  // was only 8 bits wide and already saturated, so it used a wider type here
10055ffd83dbSDimitry Andric  // to make room for __regex_word (then a part of this class rather than
10065ffd83dbSDimitry Andric  // ctype_base). Bionic has since moved to the builtin ctype_base
10075ffd83dbSDimitry Andric  // implementation, but this was not updated to match. Since then Android has
10085ffd83dbSDimitry Andric  // needed to maintain a stable libc++ ABI, and this can't be changed without
10095ffd83dbSDimitry Andric  // an ABI break.
1010bdd1243dSDimitry Andric  // We also need this workaround for newlib since _NEWLIB_VERSION is not
1011bdd1243dSDimitry Andric  // defined yet inside __config, so we can't set the
1012bdd1243dSDimitry Andric  // _LIBCPP_PROVIDES_DEFAULT_RUNE_TABLE macro. Additionally, newlib is
1013bdd1243dSDimitry Andric  // often used for space constrained environments, so it makes sense not to
1014bdd1243dSDimitry Andric  // duplicate the ctype table.
10155ffd83dbSDimitry Andric  typedef uint16_t char_class_type;
10165ffd83dbSDimitry Andric#else
10170b57cec5SDimitry Andric  typedef ctype_base::mask char_class_type;
10185ffd83dbSDimitry Andric#endif
10190b57cec5SDimitry Andric
10200b57cec5SDimitry Andric  static const char_class_type __regex_word = ctype_base::__regex_word;
1021cb14a3feSDimitry Andric
10220b57cec5SDimitry Andricprivate:
10230b57cec5SDimitry Andric  locale __loc_;
10240b57cec5SDimitry Andric  const ctype<char_type>* __ct_;
10250b57cec5SDimitry Andric  const collate<char_type>* __col_;
10260b57cec5SDimitry Andric
10270b57cec5SDimitry Andricpublic:
10280b57cec5SDimitry Andric  regex_traits();
10290b57cec5SDimitry Andric
1030cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI static size_t length(const char_type* __p) { return char_traits<char_type>::length(__p); }
1031cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI char_type translate(char_type __c) const { return __c; }
10320b57cec5SDimitry Andric  char_type translate_nocase(char_type __c) const;
10330b57cec5SDimitry Andric  template <class _ForwardIterator>
1034cb14a3feSDimitry Andric  string_type transform(_ForwardIterator __f, _ForwardIterator __l) const;
10350b57cec5SDimitry Andric  template <class _ForwardIterator>
1036cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI string_type transform_primary(_ForwardIterator __f, _ForwardIterator __l) const {
1037cb14a3feSDimitry Andric    return __transform_primary(__f, __l, char_type());
1038cb14a3feSDimitry Andric  }
10390b57cec5SDimitry Andric  template <class _ForwardIterator>
1040cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI string_type lookup_collatename(_ForwardIterator __f, _ForwardIterator __l) const {
1041cb14a3feSDimitry Andric    return __lookup_collatename(__f, __l, char_type());
1042cb14a3feSDimitry Andric  }
10430b57cec5SDimitry Andric  template <class _ForwardIterator>
1044cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI char_class_type
1045cb14a3feSDimitry Andric  lookup_classname(_ForwardIterator __f, _ForwardIterator __l, bool __icase = false) const {
1046cb14a3feSDimitry Andric    return __lookup_classname(__f, __l, __icase, char_type());
1047cb14a3feSDimitry Andric  }
10480b57cec5SDimitry Andric  bool isctype(char_type __c, char_class_type __m) const;
1049cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI int value(char_type __ch, int __radix) const { return __regex_traits_value(__ch, __radix); }
10500b57cec5SDimitry Andric  locale_type imbue(locale_type __l);
1051cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI locale_type getloc() const { return __loc_; }
10520b57cec5SDimitry Andric
10530b57cec5SDimitry Andricprivate:
10540b57cec5SDimitry Andric  void __init();
10550b57cec5SDimitry Andric
10560b57cec5SDimitry Andric  template <class _ForwardIterator>
1057cb14a3feSDimitry Andric  string_type __transform_primary(_ForwardIterator __f, _ForwardIterator __l, char) const;
1058349cc55cSDimitry Andric#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
10590b57cec5SDimitry Andric  template <class _ForwardIterator>
1060cb14a3feSDimitry Andric  string_type __transform_primary(_ForwardIterator __f, _ForwardIterator __l, wchar_t) const;
1061349cc55cSDimitry Andric#endif
10620b57cec5SDimitry Andric  template <class _ForwardIterator>
1063cb14a3feSDimitry Andric  string_type __lookup_collatename(_ForwardIterator __f, _ForwardIterator __l, char) const;
1064349cc55cSDimitry Andric#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
10650b57cec5SDimitry Andric  template <class _ForwardIterator>
1066cb14a3feSDimitry Andric  string_type __lookup_collatename(_ForwardIterator __f, _ForwardIterator __l, wchar_t) const;
1067349cc55cSDimitry Andric#endif
10680b57cec5SDimitry Andric  template <class _ForwardIterator>
1069cb14a3feSDimitry Andric  char_class_type __lookup_classname(_ForwardIterator __f, _ForwardIterator __l, bool __icase, char) const;
1070349cc55cSDimitry Andric#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
10710b57cec5SDimitry Andric  template <class _ForwardIterator>
1072cb14a3feSDimitry Andric  char_class_type __lookup_classname(_ForwardIterator __f, _ForwardIterator __l, bool __icase, wchar_t) const;
1073349cc55cSDimitry Andric#endif
10740b57cec5SDimitry Andric
10750b57cec5SDimitry Andric  static int __regex_traits_value(unsigned char __ch, int __radix);
1076cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI int __regex_traits_value(char __ch, int __radix) const {
1077cb14a3feSDimitry Andric    return __regex_traits_value(static_cast<unsigned char>(__ch), __radix);
1078cb14a3feSDimitry Andric  }
1079349cc55cSDimitry Andric#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1080cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI int __regex_traits_value(wchar_t __ch, int __radix) const;
1081349cc55cSDimitry Andric#endif
10820b57cec5SDimitry Andric};
10830b57cec5SDimitry Andric
10840b57cec5SDimitry Andrictemplate <class _CharT>
1085cb14a3feSDimitry Andricconst typename regex_traits<_CharT>::char_class_type regex_traits<_CharT>::__regex_word;
10860b57cec5SDimitry Andric
10870b57cec5SDimitry Andrictemplate <class _CharT>
1088cb14a3feSDimitry Andricregex_traits<_CharT>::regex_traits() {
10890b57cec5SDimitry Andric  __init();
10900b57cec5SDimitry Andric}
10910b57cec5SDimitry Andric
10920b57cec5SDimitry Andrictemplate <class _CharT>
1093cb14a3feSDimitry Andrictypename regex_traits<_CharT>::char_type regex_traits<_CharT>::translate_nocase(char_type __c) const {
10940b57cec5SDimitry Andric  return __ct_->tolower(__c);
10950b57cec5SDimitry Andric}
10960b57cec5SDimitry Andric
10970b57cec5SDimitry Andrictemplate <class _CharT>
10980b57cec5SDimitry Andrictemplate <class _ForwardIterator>
10990b57cec5SDimitry Andrictypename regex_traits<_CharT>::string_type
1100cb14a3feSDimitry Andricregex_traits<_CharT>::transform(_ForwardIterator __f, _ForwardIterator __l) const {
11010b57cec5SDimitry Andric  string_type __s(__f, __l);
11020b57cec5SDimitry Andric  return __col_->transform(__s.data(), __s.data() + __s.size());
11030b57cec5SDimitry Andric}
11040b57cec5SDimitry Andric
11050b57cec5SDimitry Andrictemplate <class _CharT>
1106cb14a3feSDimitry Andricvoid regex_traits<_CharT>::__init() {
1107bdd1243dSDimitry Andric  __ct_  = &std::use_facet<ctype<char_type> >(__loc_);
1108bdd1243dSDimitry Andric  __col_ = &std::use_facet<collate<char_type> >(__loc_);
11090b57cec5SDimitry Andric}
11100b57cec5SDimitry Andric
11110b57cec5SDimitry Andrictemplate <class _CharT>
1112cb14a3feSDimitry Andrictypename regex_traits<_CharT>::locale_type regex_traits<_CharT>::imbue(locale_type __l) {
11130b57cec5SDimitry Andric  locale __r = __loc_;
11140b57cec5SDimitry Andric  __loc_     = __l;
11150b57cec5SDimitry Andric  __init();
11160b57cec5SDimitry Andric  return __r;
11170b57cec5SDimitry Andric}
11180b57cec5SDimitry Andric
11190b57cec5SDimitry Andric// transform_primary is very FreeBSD-specific
11200b57cec5SDimitry Andric
11210b57cec5SDimitry Andrictemplate <class _CharT>
11220b57cec5SDimitry Andrictemplate <class _ForwardIterator>
11230b57cec5SDimitry Andrictypename regex_traits<_CharT>::string_type
1124cb14a3feSDimitry Andricregex_traits<_CharT>::__transform_primary(_ForwardIterator __f, _ForwardIterator __l, char) const {
11250b57cec5SDimitry Andric  const string_type __s(__f, __l);
11260b57cec5SDimitry Andric  string_type __d = __col_->transform(__s.data(), __s.data() + __s.size());
1127cb14a3feSDimitry Andric  switch (__d.size()) {
11280b57cec5SDimitry Andric  case 1:
11290b57cec5SDimitry Andric    break;
11300b57cec5SDimitry Andric  case 12:
11310b57cec5SDimitry Andric    __d[11] = __d[3];
11320b57cec5SDimitry Andric    break;
11330b57cec5SDimitry Andric  default:
11340b57cec5SDimitry Andric    __d.clear();
11350b57cec5SDimitry Andric    break;
11360b57cec5SDimitry Andric  }
11370b57cec5SDimitry Andric  return __d;
11380b57cec5SDimitry Andric}
11390b57cec5SDimitry Andric
1140349cc55cSDimitry Andric#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
11410b57cec5SDimitry Andrictemplate <class _CharT>
11420b57cec5SDimitry Andrictemplate <class _ForwardIterator>
11430b57cec5SDimitry Andrictypename regex_traits<_CharT>::string_type
1144cb14a3feSDimitry Andricregex_traits<_CharT>::__transform_primary(_ForwardIterator __f, _ForwardIterator __l, wchar_t) const {
11450b57cec5SDimitry Andric  const string_type __s(__f, __l);
11460b57cec5SDimitry Andric  string_type __d = __col_->transform(__s.data(), __s.data() + __s.size());
1147cb14a3feSDimitry Andric  switch (__d.size()) {
11480b57cec5SDimitry Andric  case 1:
11490b57cec5SDimitry Andric    break;
11500b57cec5SDimitry Andric  case 3:
11510b57cec5SDimitry Andric    __d[2] = __d[0];
11520b57cec5SDimitry Andric    break;
11530b57cec5SDimitry Andric  default:
11540b57cec5SDimitry Andric    __d.clear();
11550b57cec5SDimitry Andric    break;
11560b57cec5SDimitry Andric  }
11570b57cec5SDimitry Andric  return __d;
11580b57cec5SDimitry Andric}
1159349cc55cSDimitry Andric#endif
11600b57cec5SDimitry Andric
11610b57cec5SDimitry Andric// lookup_collatename is very FreeBSD-specific
11620b57cec5SDimitry Andric
116306c3fb27SDimitry Andric_LIBCPP_EXPORTED_FROM_ABI string __get_collation_name(const char* __s);
11640b57cec5SDimitry Andric
11650b57cec5SDimitry Andrictemplate <class _CharT>
11660b57cec5SDimitry Andrictemplate <class _ForwardIterator>
11670b57cec5SDimitry Andrictypename regex_traits<_CharT>::string_type
1168cb14a3feSDimitry Andricregex_traits<_CharT>::__lookup_collatename(_ForwardIterator __f, _ForwardIterator __l, char) const {
11690b57cec5SDimitry Andric  string_type __s(__f, __l);
11700b57cec5SDimitry Andric  string_type __r;
1171cb14a3feSDimitry Andric  if (!__s.empty()) {
1172bdd1243dSDimitry Andric    __r = std::__get_collation_name(__s.c_str());
1173cb14a3feSDimitry Andric    if (__r.empty() && __s.size() <= 2) {
11740b57cec5SDimitry Andric      __r = __col_->transform(__s.data(), __s.data() + __s.size());
11750b57cec5SDimitry Andric      if (__r.size() == 1 || __r.size() == 12)
11760b57cec5SDimitry Andric        __r = __s;
11770b57cec5SDimitry Andric      else
11780b57cec5SDimitry Andric        __r.clear();
11790b57cec5SDimitry Andric    }
11800b57cec5SDimitry Andric  }
11810b57cec5SDimitry Andric  return __r;
11820b57cec5SDimitry Andric}
11830b57cec5SDimitry Andric
1184349cc55cSDimitry Andric#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
11850b57cec5SDimitry Andrictemplate <class _CharT>
11860b57cec5SDimitry Andrictemplate <class _ForwardIterator>
11870b57cec5SDimitry Andrictypename regex_traits<_CharT>::string_type
1188cb14a3feSDimitry Andricregex_traits<_CharT>::__lookup_collatename(_ForwardIterator __f, _ForwardIterator __l, wchar_t) const {
11890b57cec5SDimitry Andric  string_type __s(__f, __l);
11900b57cec5SDimitry Andric  string __n;
11910b57cec5SDimitry Andric  __n.reserve(__s.size());
1192cb14a3feSDimitry Andric  for (typename string_type::const_iterator __i = __s.begin(), __e = __s.end(); __i != __e; ++__i) {
11930b57cec5SDimitry Andric    if (static_cast<unsigned>(*__i) >= 127)
11940b57cec5SDimitry Andric      return string_type();
11950b57cec5SDimitry Andric    __n.push_back(char(*__i));
11960b57cec5SDimitry Andric  }
11970b57cec5SDimitry Andric  string_type __r;
1198cb14a3feSDimitry Andric  if (!__s.empty()) {
11990b57cec5SDimitry Andric    __n = __get_collation_name(__n.c_str());
12000b57cec5SDimitry Andric    if (!__n.empty())
12010b57cec5SDimitry Andric      __r.assign(__n.begin(), __n.end());
1202cb14a3feSDimitry Andric    else if (__s.size() <= 2) {
12030b57cec5SDimitry Andric      __r = __col_->transform(__s.data(), __s.data() + __s.size());
12040b57cec5SDimitry Andric      if (__r.size() == 1 || __r.size() == 3)
12050b57cec5SDimitry Andric        __r = __s;
12060b57cec5SDimitry Andric      else
12070b57cec5SDimitry Andric        __r.clear();
12080b57cec5SDimitry Andric    }
12090b57cec5SDimitry Andric  }
12100b57cec5SDimitry Andric  return __r;
12110b57cec5SDimitry Andric}
1212349cc55cSDimitry Andric#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
12130b57cec5SDimitry Andric
12140b57cec5SDimitry Andric// lookup_classname
12150b57cec5SDimitry Andric
121606c3fb27SDimitry Andricregex_traits<char>::char_class_type _LIBCPP_EXPORTED_FROM_ABI __get_classname(const char* __s, bool __icase);
12170b57cec5SDimitry Andric
12180b57cec5SDimitry Andrictemplate <class _CharT>
12190b57cec5SDimitry Andrictemplate <class _ForwardIterator>
12200b57cec5SDimitry Andrictypename regex_traits<_CharT>::char_class_type
1221cb14a3feSDimitry Andricregex_traits<_CharT>::__lookup_classname(_ForwardIterator __f, _ForwardIterator __l, bool __icase, char) const {
12220b57cec5SDimitry Andric  string_type __s(__f, __l);
12230b57cec5SDimitry Andric  __ct_->tolower(&__s[0], &__s[0] + __s.size());
1224bdd1243dSDimitry Andric  return std::__get_classname(__s.c_str(), __icase);
12250b57cec5SDimitry Andric}
12260b57cec5SDimitry Andric
1227349cc55cSDimitry Andric#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
12280b57cec5SDimitry Andrictemplate <class _CharT>
12290b57cec5SDimitry Andrictemplate <class _ForwardIterator>
12300b57cec5SDimitry Andrictypename regex_traits<_CharT>::char_class_type
1231cb14a3feSDimitry Andricregex_traits<_CharT>::__lookup_classname(_ForwardIterator __f, _ForwardIterator __l, bool __icase, wchar_t) const {
12320b57cec5SDimitry Andric  string_type __s(__f, __l);
12330b57cec5SDimitry Andric  __ct_->tolower(&__s[0], &__s[0] + __s.size());
12340b57cec5SDimitry Andric  string __n;
12350b57cec5SDimitry Andric  __n.reserve(__s.size());
1236cb14a3feSDimitry Andric  for (typename string_type::const_iterator __i = __s.begin(), __e = __s.end(); __i != __e; ++__i) {
12370b57cec5SDimitry Andric    if (static_cast<unsigned>(*__i) >= 127)
12380b57cec5SDimitry Andric      return char_class_type();
12390b57cec5SDimitry Andric    __n.push_back(char(*__i));
12400b57cec5SDimitry Andric  }
12410b57cec5SDimitry Andric  return __get_classname(__n.c_str(), __icase);
12420b57cec5SDimitry Andric}
1243349cc55cSDimitry Andric#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
12440b57cec5SDimitry Andric
12450b57cec5SDimitry Andrictemplate <class _CharT>
1246cb14a3feSDimitry Andricbool regex_traits<_CharT>::isctype(char_type __c, char_class_type __m) const {
12470b57cec5SDimitry Andric  if (__ct_->is(__m, __c))
12480b57cec5SDimitry Andric    return true;
12490b57cec5SDimitry Andric  return (__c == '_' && (__m & __regex_word));
12500b57cec5SDimitry Andric}
12510b57cec5SDimitry Andric
1252cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool __is_07(unsigned char __c) {
1253753f127fSDimitry Andric  return (__c & 0xF8u) ==
125404eeddc0SDimitry Andric#if defined(__MVS__) && !defined(__NATIVE_ASCII_F)
125504eeddc0SDimitry Andric         0xF0;
125604eeddc0SDimitry Andric#else
125704eeddc0SDimitry Andric         0x30;
125804eeddc0SDimitry Andric#endif
125904eeddc0SDimitry Andric}
126004eeddc0SDimitry Andric
1261cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool __is_89(unsigned char __c) {
1262753f127fSDimitry Andric  return (__c & 0xFEu) ==
126304eeddc0SDimitry Andric#if defined(__MVS__) && !defined(__NATIVE_ASCII_F)
126404eeddc0SDimitry Andric         0xF8;
126504eeddc0SDimitry Andric#else
126604eeddc0SDimitry Andric         0x38;
126704eeddc0SDimitry Andric#endif
126804eeddc0SDimitry Andric}
126904eeddc0SDimitry Andric
1270cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI unsigned char __to_lower(unsigned char __c) {
127104eeddc0SDimitry Andric#if defined(__MVS__) && !defined(__NATIVE_ASCII_F)
1272a4a491e2SDimitry Andric  return __c & 0xBF;
127304eeddc0SDimitry Andric#else
1274753f127fSDimitry Andric  return __c | 0x20;
127504eeddc0SDimitry Andric#endif
127604eeddc0SDimitry Andric}
127704eeddc0SDimitry Andric
12780b57cec5SDimitry Andrictemplate <class _CharT>
1279cb14a3feSDimitry Andricint regex_traits<_CharT>::__regex_traits_value(unsigned char __ch, int __radix) {
128004eeddc0SDimitry Andric  if (__is_07(__ch)) // '0' <= __ch && __ch <= '7'
12810b57cec5SDimitry Andric    return __ch - '0';
1282cb14a3feSDimitry Andric  if (__radix != 8) {
128304eeddc0SDimitry Andric    if (__is_89(__ch)) // '8' <= __ch && __ch <= '9'
12840b57cec5SDimitry Andric      return __ch - '0';
1285cb14a3feSDimitry Andric    if (__radix == 16) {
128604eeddc0SDimitry Andric      __ch = __to_lower(__ch); // tolower
12870b57cec5SDimitry Andric      if ('a' <= __ch && __ch <= 'f')
12880b57cec5SDimitry Andric        return __ch - ('a' - 10);
12890b57cec5SDimitry Andric    }
12900b57cec5SDimitry Andric  }
12910b57cec5SDimitry Andric  return -1;
12920b57cec5SDimitry Andric}
12930b57cec5SDimitry Andric
1294349cc55cSDimitry Andric#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
12950b57cec5SDimitry Andrictemplate <class _CharT>
1296cb14a3feSDimitry Andricinline int regex_traits<_CharT>::__regex_traits_value(wchar_t __ch, int __radix) const {
12970b57cec5SDimitry Andric  return __regex_traits_value(static_cast<unsigned char>(__ct_->narrow(__ch, char_type())), __radix);
12980b57cec5SDimitry Andric}
1299349cc55cSDimitry Andric#endif
13000b57cec5SDimitry Andric
1301cb14a3feSDimitry Andrictemplate <class _CharT>
1302cb14a3feSDimitry Andricclass __node;
13030b57cec5SDimitry Andric
1304cb14a3feSDimitry Andrictemplate <class _BidirectionalIterator>
1305cb14a3feSDimitry Andricclass _LIBCPP_TEMPLATE_VIS sub_match;
13060b57cec5SDimitry Andric
1307cb14a3feSDimitry Andrictemplate <class _BidirectionalIterator, class _Allocator = allocator<sub_match<_BidirectionalIterator> > >
13080b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS match_results;
13090b57cec5SDimitry Andric
13100b57cec5SDimitry Andrictemplate <class _CharT>
1311cb14a3feSDimitry Andricstruct __state {
1312cb14a3feSDimitry Andric  enum {
13130b57cec5SDimitry Andric    __end_state = -1000,
13140b57cec5SDimitry Andric    __consume_input,          // -999
13150b57cec5SDimitry Andric    __begin_marked_expr,      // -998
13160b57cec5SDimitry Andric    __end_marked_expr,        // -997
13170b57cec5SDimitry Andric    __pop_state,              // -996
13180b57cec5SDimitry Andric    __accept_and_consume,     // -995
13190b57cec5SDimitry Andric    __accept_but_not_consume, // -994
13200b57cec5SDimitry Andric    __reject,                 // -993
13210b57cec5SDimitry Andric    __split,
13220b57cec5SDimitry Andric    __repeat
13230b57cec5SDimitry Andric  };
13240b57cec5SDimitry Andric
13250b57cec5SDimitry Andric  int __do_;
13260b57cec5SDimitry Andric  const _CharT* __first_;
13270b57cec5SDimitry Andric  const _CharT* __current_;
13280b57cec5SDimitry Andric  const _CharT* __last_;
13290b57cec5SDimitry Andric  vector<sub_match<const _CharT*> > __sub_matches_;
13300b57cec5SDimitry Andric  vector<pair<size_t, const _CharT*> > __loop_data_;
13310b57cec5SDimitry Andric  const __node<_CharT>* __node_;
13320b57cec5SDimitry Andric  regex_constants::match_flag_type __flags_;
13330b57cec5SDimitry Andric  bool __at_first_;
13340b57cec5SDimitry Andric
1335cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI __state()
1336cb14a3feSDimitry Andric      : __do_(0),
1337cb14a3feSDimitry Andric        __first_(nullptr),
1338cb14a3feSDimitry Andric        __current_(nullptr),
1339cb14a3feSDimitry Andric        __last_(nullptr),
1340cb14a3feSDimitry Andric        __node_(nullptr),
1341cb14a3feSDimitry Andric        __flags_(),
1342cb14a3feSDimitry Andric        __at_first_(false) {}
13430b57cec5SDimitry Andric};
13440b57cec5SDimitry Andric
13450b57cec5SDimitry Andric// __node
13460b57cec5SDimitry Andric
13470b57cec5SDimitry Andrictemplate <class _CharT>
1348cb14a3feSDimitry Andricclass __node {
13490b57cec5SDimitry Andric  __node(const __node&);
13500b57cec5SDimitry Andric  __node& operator=(const __node&);
1351cb14a3feSDimitry Andric
13520b57cec5SDimitry Andricpublic:
13535f757f3fSDimitry Andric  typedef std::__state<_CharT> __state;
13540b57cec5SDimitry Andric
1355cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI __node() {}
1356bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI_VIRTUAL
13570b57cec5SDimitry Andric  virtual ~__node() {}
13580b57cec5SDimitry Andric
1359bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI_VIRTUAL
13600b57cec5SDimitry Andric  virtual void __exec(__state&) const {}
1361bdd1243dSDimitry Andric  _LIBCPP_HIDE_FROM_ABI_VIRTUAL
13620b57cec5SDimitry Andric  virtual void __exec_split(bool, __state&) const {}
13630b57cec5SDimitry Andric};
13640b57cec5SDimitry Andric
13650b57cec5SDimitry Andric// __end_state
13660b57cec5SDimitry Andric
13670b57cec5SDimitry Andrictemplate <class _CharT>
1368cb14a3feSDimitry Andricclass __end_state : public __node<_CharT> {
13690b57cec5SDimitry Andricpublic:
13705f757f3fSDimitry Andric  typedef std::__state<_CharT> __state;
13710b57cec5SDimitry Andric
1372cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI __end_state() {}
13730b57cec5SDimitry Andric
137406c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
13750b57cec5SDimitry Andric};
13760b57cec5SDimitry Andric
13770b57cec5SDimitry Andrictemplate <class _CharT>
1378cb14a3feSDimitry Andricvoid __end_state<_CharT>::__exec(__state& __s) const {
13790b57cec5SDimitry Andric  __s.__do_ = __state::__end_state;
13800b57cec5SDimitry Andric}
13810b57cec5SDimitry Andric
13820b57cec5SDimitry Andric// __has_one_state
13830b57cec5SDimitry Andric
13840b57cec5SDimitry Andrictemplate <class _CharT>
1385cb14a3feSDimitry Andricclass __has_one_state : public __node<_CharT> {
13860b57cec5SDimitry Andric  __node<_CharT>* __first_;
13870b57cec5SDimitry Andric
13880b57cec5SDimitry Andricpublic:
1389cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI explicit __has_one_state(__node<_CharT>* __s) : __first_(__s) {}
13900b57cec5SDimitry Andric
1391cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI __node<_CharT>* first() const { return __first_; }
1392cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI __node<_CharT>*& first() { return __first_; }
13930b57cec5SDimitry Andric};
13940b57cec5SDimitry Andric
13950b57cec5SDimitry Andric// __owns_one_state
13960b57cec5SDimitry Andric
13970b57cec5SDimitry Andrictemplate <class _CharT>
1398cb14a3feSDimitry Andricclass __owns_one_state : public __has_one_state<_CharT> {
13990b57cec5SDimitry Andric  typedef __has_one_state<_CharT> base;
14000b57cec5SDimitry Andric
14010b57cec5SDimitry Andricpublic:
1402cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI explicit __owns_one_state(__node<_CharT>* __s) : base(__s) {}
14030b57cec5SDimitry Andric
1404bdd1243dSDimitry Andric  ~__owns_one_state() override;
14050b57cec5SDimitry Andric};
14060b57cec5SDimitry Andric
14070b57cec5SDimitry Andrictemplate <class _CharT>
1408cb14a3feSDimitry Andric__owns_one_state<_CharT>::~__owns_one_state() {
14090b57cec5SDimitry Andric  delete this->first();
14100b57cec5SDimitry Andric}
14110b57cec5SDimitry Andric
14120b57cec5SDimitry Andric// __empty_state
14130b57cec5SDimitry Andric
14140b57cec5SDimitry Andrictemplate <class _CharT>
1415cb14a3feSDimitry Andricclass __empty_state : public __owns_one_state<_CharT> {
14160b57cec5SDimitry Andric  typedef __owns_one_state<_CharT> base;
14170b57cec5SDimitry Andric
14180b57cec5SDimitry Andricpublic:
14195f757f3fSDimitry Andric  typedef std::__state<_CharT> __state;
14200b57cec5SDimitry Andric
1421cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI explicit __empty_state(__node<_CharT>* __s) : base(__s) {}
14220b57cec5SDimitry Andric
142306c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
14240b57cec5SDimitry Andric};
14250b57cec5SDimitry Andric
14260b57cec5SDimitry Andrictemplate <class _CharT>
1427cb14a3feSDimitry Andricvoid __empty_state<_CharT>::__exec(__state& __s) const {
14280b57cec5SDimitry Andric  __s.__do_   = __state::__accept_but_not_consume;
14290b57cec5SDimitry Andric  __s.__node_ = this->first();
14300b57cec5SDimitry Andric}
14310b57cec5SDimitry Andric
14320b57cec5SDimitry Andric// __empty_non_own_state
14330b57cec5SDimitry Andric
14340b57cec5SDimitry Andrictemplate <class _CharT>
1435cb14a3feSDimitry Andricclass __empty_non_own_state : public __has_one_state<_CharT> {
14360b57cec5SDimitry Andric  typedef __has_one_state<_CharT> base;
14370b57cec5SDimitry Andric
14380b57cec5SDimitry Andricpublic:
14395f757f3fSDimitry Andric  typedef std::__state<_CharT> __state;
14400b57cec5SDimitry Andric
1441cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI explicit __empty_non_own_state(__node<_CharT>* __s) : base(__s) {}
14420b57cec5SDimitry Andric
144306c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
14440b57cec5SDimitry Andric};
14450b57cec5SDimitry Andric
14460b57cec5SDimitry Andrictemplate <class _CharT>
1447cb14a3feSDimitry Andricvoid __empty_non_own_state<_CharT>::__exec(__state& __s) const {
14480b57cec5SDimitry Andric  __s.__do_   = __state::__accept_but_not_consume;
14490b57cec5SDimitry Andric  __s.__node_ = this->first();
14500b57cec5SDimitry Andric}
14510b57cec5SDimitry Andric
14520b57cec5SDimitry Andric// __repeat_one_loop
14530b57cec5SDimitry Andric
14540b57cec5SDimitry Andrictemplate <class _CharT>
1455cb14a3feSDimitry Andricclass __repeat_one_loop : public __has_one_state<_CharT> {
14560b57cec5SDimitry Andric  typedef __has_one_state<_CharT> base;
14570b57cec5SDimitry Andric
14580b57cec5SDimitry Andricpublic:
14595f757f3fSDimitry Andric  typedef std::__state<_CharT> __state;
14600b57cec5SDimitry Andric
1461cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI explicit __repeat_one_loop(__node<_CharT>* __s) : base(__s) {}
14620b57cec5SDimitry Andric
146306c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
14640b57cec5SDimitry Andric};
14650b57cec5SDimitry Andric
14660b57cec5SDimitry Andrictemplate <class _CharT>
1467cb14a3feSDimitry Andricvoid __repeat_one_loop<_CharT>::__exec(__state& __s) const {
14680b57cec5SDimitry Andric  __s.__do_   = __state::__repeat;
14690b57cec5SDimitry Andric  __s.__node_ = this->first();
14700b57cec5SDimitry Andric}
14710b57cec5SDimitry Andric
14720b57cec5SDimitry Andric// __owns_two_states
14730b57cec5SDimitry Andric
14740b57cec5SDimitry Andrictemplate <class _CharT>
1475cb14a3feSDimitry Andricclass __owns_two_states : public __owns_one_state<_CharT> {
14760b57cec5SDimitry Andric  typedef __owns_one_state<_CharT> base;
14770b57cec5SDimitry Andric
14780b57cec5SDimitry Andric  base* __second_;
14790b57cec5SDimitry Andric
14800b57cec5SDimitry Andricpublic:
1481cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI explicit __owns_two_states(__node<_CharT>* __s1, base* __s2) : base(__s1), __second_(__s2) {}
14820b57cec5SDimitry Andric
148306c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual ~__owns_two_states();
14840b57cec5SDimitry Andric
1485cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI base* second() const { return __second_; }
1486cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI base*& second() { return __second_; }
14870b57cec5SDimitry Andric};
14880b57cec5SDimitry Andric
14890b57cec5SDimitry Andrictemplate <class _CharT>
1490cb14a3feSDimitry Andric__owns_two_states<_CharT>::~__owns_two_states() {
14910b57cec5SDimitry Andric  delete __second_;
14920b57cec5SDimitry Andric}
14930b57cec5SDimitry Andric
14940b57cec5SDimitry Andric// __loop
14950b57cec5SDimitry Andric
14960b57cec5SDimitry Andrictemplate <class _CharT>
1497cb14a3feSDimitry Andricclass __loop : public __owns_two_states<_CharT> {
14980b57cec5SDimitry Andric  typedef __owns_two_states<_CharT> base;
14990b57cec5SDimitry Andric
15000b57cec5SDimitry Andric  size_t __min_;
15010b57cec5SDimitry Andric  size_t __max_;
15020b57cec5SDimitry Andric  unsigned __loop_id_;
15030b57cec5SDimitry Andric  unsigned __mexp_begin_;
15040b57cec5SDimitry Andric  unsigned __mexp_end_;
15050b57cec5SDimitry Andric  bool __greedy_;
15060b57cec5SDimitry Andric
15070b57cec5SDimitry Andricpublic:
15085f757f3fSDimitry Andric  typedef std::__state<_CharT> __state;
15090b57cec5SDimitry Andric
1510cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI explicit __loop(
1511cb14a3feSDimitry Andric      unsigned __loop_id,
1512cb14a3feSDimitry Andric      __node<_CharT>* __s1,
1513cb14a3feSDimitry Andric      __owns_one_state<_CharT>* __s2,
1514cb14a3feSDimitry Andric      unsigned __mexp_begin,
1515cb14a3feSDimitry Andric      unsigned __mexp_end,
15160b57cec5SDimitry Andric      bool __greedy = true,
15170b57cec5SDimitry Andric      size_t __min  = 0,
15180b57cec5SDimitry Andric      size_t __max  = numeric_limits<size_t>::max())
1519cb14a3feSDimitry Andric      : base(__s1, __s2),
1520cb14a3feSDimitry Andric        __min_(__min),
1521cb14a3feSDimitry Andric        __max_(__max),
1522cb14a3feSDimitry Andric        __loop_id_(__loop_id),
1523cb14a3feSDimitry Andric        __mexp_begin_(__mexp_begin),
1524cb14a3feSDimitry Andric        __mexp_end_(__mexp_end),
15250b57cec5SDimitry Andric        __greedy_(__greedy) {}
15260b57cec5SDimitry Andric
152706c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state& __s) const;
152806c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec_split(bool __second, __state& __s) const;
15290b57cec5SDimitry Andric
15300b57cec5SDimitry Andricprivate:
1531cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI void __init_repeat(__state& __s) const {
15320b57cec5SDimitry Andric    __s.__loop_data_[__loop_id_].second = __s.__current_;
1533cb14a3feSDimitry Andric    for (size_t __i = __mexp_begin_ - 1; __i != __mexp_end_ - 1; ++__i) {
15340b57cec5SDimitry Andric      __s.__sub_matches_[__i].first   = __s.__last_;
15350b57cec5SDimitry Andric      __s.__sub_matches_[__i].second  = __s.__last_;
15360b57cec5SDimitry Andric      __s.__sub_matches_[__i].matched = false;
15370b57cec5SDimitry Andric    }
15380b57cec5SDimitry Andric  }
15390b57cec5SDimitry Andric};
15400b57cec5SDimitry Andric
15410b57cec5SDimitry Andrictemplate <class _CharT>
1542cb14a3feSDimitry Andricvoid __loop<_CharT>::__exec(__state& __s) const {
1543cb14a3feSDimitry Andric  if (__s.__do_ == __state::__repeat) {
15440b57cec5SDimitry Andric    bool __do_repeat = ++__s.__loop_data_[__loop_id_].first < __max_;
15450b57cec5SDimitry Andric    bool __do_alt    = __s.__loop_data_[__loop_id_].first >= __min_;
1546cb14a3feSDimitry Andric    if (__do_repeat && __do_alt && __s.__loop_data_[__loop_id_].second == __s.__current_)
15470b57cec5SDimitry Andric      __do_repeat = false;
15480b57cec5SDimitry Andric    if (__do_repeat && __do_alt)
15490b57cec5SDimitry Andric      __s.__do_ = __state::__split;
1550cb14a3feSDimitry Andric    else if (__do_repeat) {
15510b57cec5SDimitry Andric      __s.__do_   = __state::__accept_but_not_consume;
15520b57cec5SDimitry Andric      __s.__node_ = this->first();
15530b57cec5SDimitry Andric      __init_repeat(__s);
1554cb14a3feSDimitry Andric    } else {
15550b57cec5SDimitry Andric      __s.__do_   = __state::__accept_but_not_consume;
15560b57cec5SDimitry Andric      __s.__node_ = this->second();
15570b57cec5SDimitry Andric    }
1558cb14a3feSDimitry Andric  } else {
15590b57cec5SDimitry Andric    __s.__loop_data_[__loop_id_].first = 0;
15600b57cec5SDimitry Andric    bool __do_repeat                   = 0 < __max_;
15610b57cec5SDimitry Andric    bool __do_alt                      = 0 >= __min_;
15620b57cec5SDimitry Andric    if (__do_repeat && __do_alt)
15630b57cec5SDimitry Andric      __s.__do_ = __state::__split;
1564cb14a3feSDimitry Andric    else if (__do_repeat) {
15650b57cec5SDimitry Andric      __s.__do_   = __state::__accept_but_not_consume;
15660b57cec5SDimitry Andric      __s.__node_ = this->first();
15670b57cec5SDimitry Andric      __init_repeat(__s);
1568cb14a3feSDimitry Andric    } else {
15690b57cec5SDimitry Andric      __s.__do_   = __state::__accept_but_not_consume;
15700b57cec5SDimitry Andric      __s.__node_ = this->second();
15710b57cec5SDimitry Andric    }
15720b57cec5SDimitry Andric  }
15730b57cec5SDimitry Andric}
15740b57cec5SDimitry Andric
15750b57cec5SDimitry Andrictemplate <class _CharT>
1576cb14a3feSDimitry Andricvoid __loop<_CharT>::__exec_split(bool __second, __state& __s) const {
15770b57cec5SDimitry Andric  __s.__do_ = __state::__accept_but_not_consume;
1578cb14a3feSDimitry Andric  if (__greedy_ != __second) {
15790b57cec5SDimitry Andric    __s.__node_ = this->first();
15800b57cec5SDimitry Andric    __init_repeat(__s);
1581cb14a3feSDimitry Andric  } else
15820b57cec5SDimitry Andric    __s.__node_ = this->second();
15830b57cec5SDimitry Andric}
15840b57cec5SDimitry Andric
15850b57cec5SDimitry Andric// __alternate
15860b57cec5SDimitry Andric
15870b57cec5SDimitry Andrictemplate <class _CharT>
1588cb14a3feSDimitry Andricclass __alternate : public __owns_two_states<_CharT> {
15890b57cec5SDimitry Andric  typedef __owns_two_states<_CharT> base;
15900b57cec5SDimitry Andric
15910b57cec5SDimitry Andricpublic:
15925f757f3fSDimitry Andric  typedef std::__state<_CharT> __state;
15930b57cec5SDimitry Andric
1594cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI explicit __alternate(__owns_one_state<_CharT>* __s1, __owns_one_state<_CharT>* __s2)
15950b57cec5SDimitry Andric      : base(__s1, __s2) {}
15960b57cec5SDimitry Andric
159706c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state& __s) const;
159806c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec_split(bool __second, __state& __s) const;
15990b57cec5SDimitry Andric};
16000b57cec5SDimitry Andric
16010b57cec5SDimitry Andrictemplate <class _CharT>
1602cb14a3feSDimitry Andricvoid __alternate<_CharT>::__exec(__state& __s) const {
16030b57cec5SDimitry Andric  __s.__do_ = __state::__split;
16040b57cec5SDimitry Andric}
16050b57cec5SDimitry Andric
16060b57cec5SDimitry Andrictemplate <class _CharT>
1607cb14a3feSDimitry Andricvoid __alternate<_CharT>::__exec_split(bool __second, __state& __s) const {
16080b57cec5SDimitry Andric  __s.__do_ = __state::__accept_but_not_consume;
16090b57cec5SDimitry Andric  if (__second)
16100b57cec5SDimitry Andric    __s.__node_ = this->second();
16110b57cec5SDimitry Andric  else
16120b57cec5SDimitry Andric    __s.__node_ = this->first();
16130b57cec5SDimitry Andric}
16140b57cec5SDimitry Andric
16150b57cec5SDimitry Andric// __begin_marked_subexpression
16160b57cec5SDimitry Andric
16170b57cec5SDimitry Andrictemplate <class _CharT>
1618cb14a3feSDimitry Andricclass __begin_marked_subexpression : public __owns_one_state<_CharT> {
16190b57cec5SDimitry Andric  typedef __owns_one_state<_CharT> base;
16200b57cec5SDimitry Andric
16210b57cec5SDimitry Andric  unsigned __mexp_;
1622cb14a3feSDimitry Andric
16230b57cec5SDimitry Andricpublic:
16245f757f3fSDimitry Andric  typedef std::__state<_CharT> __state;
16250b57cec5SDimitry Andric
1626cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI explicit __begin_marked_subexpression(unsigned __mexp, __node<_CharT>* __s)
16270b57cec5SDimitry Andric      : base(__s), __mexp_(__mexp) {}
16280b57cec5SDimitry Andric
162906c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
16300b57cec5SDimitry Andric};
16310b57cec5SDimitry Andric
16320b57cec5SDimitry Andrictemplate <class _CharT>
1633cb14a3feSDimitry Andricvoid __begin_marked_subexpression<_CharT>::__exec(__state& __s) const {
16340b57cec5SDimitry Andric  __s.__do_                             = __state::__accept_but_not_consume;
16350b57cec5SDimitry Andric  __s.__sub_matches_[__mexp_ - 1].first = __s.__current_;
16360b57cec5SDimitry Andric  __s.__node_                           = this->first();
16370b57cec5SDimitry Andric}
16380b57cec5SDimitry Andric
16390b57cec5SDimitry Andric// __end_marked_subexpression
16400b57cec5SDimitry Andric
16410b57cec5SDimitry Andrictemplate <class _CharT>
1642cb14a3feSDimitry Andricclass __end_marked_subexpression : public __owns_one_state<_CharT> {
16430b57cec5SDimitry Andric  typedef __owns_one_state<_CharT> base;
16440b57cec5SDimitry Andric
16450b57cec5SDimitry Andric  unsigned __mexp_;
1646cb14a3feSDimitry Andric
16470b57cec5SDimitry Andricpublic:
16485f757f3fSDimitry Andric  typedef std::__state<_CharT> __state;
16490b57cec5SDimitry Andric
1650cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI explicit __end_marked_subexpression(unsigned __mexp, __node<_CharT>* __s)
16510b57cec5SDimitry Andric      : base(__s), __mexp_(__mexp) {}
16520b57cec5SDimitry Andric
165306c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
16540b57cec5SDimitry Andric};
16550b57cec5SDimitry Andric
16560b57cec5SDimitry Andrictemplate <class _CharT>
1657cb14a3feSDimitry Andricvoid __end_marked_subexpression<_CharT>::__exec(__state& __s) const {
16580b57cec5SDimitry Andric  __s.__do_                               = __state::__accept_but_not_consume;
16590b57cec5SDimitry Andric  __s.__sub_matches_[__mexp_ - 1].second  = __s.__current_;
16600b57cec5SDimitry Andric  __s.__sub_matches_[__mexp_ - 1].matched = true;
16610b57cec5SDimitry Andric  __s.__node_                             = this->first();
16620b57cec5SDimitry Andric}
16630b57cec5SDimitry Andric
16640b57cec5SDimitry Andric// __back_ref
16650b57cec5SDimitry Andric
16660b57cec5SDimitry Andrictemplate <class _CharT>
1667cb14a3feSDimitry Andricclass __back_ref : public __owns_one_state<_CharT> {
16680b57cec5SDimitry Andric  typedef __owns_one_state<_CharT> base;
16690b57cec5SDimitry Andric
16700b57cec5SDimitry Andric  unsigned __mexp_;
1671cb14a3feSDimitry Andric
16720b57cec5SDimitry Andricpublic:
16735f757f3fSDimitry Andric  typedef std::__state<_CharT> __state;
16740b57cec5SDimitry Andric
1675cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI explicit __back_ref(unsigned __mexp, __node<_CharT>* __s) : base(__s), __mexp_(__mexp) {}
16760b57cec5SDimitry Andric
167706c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
16780b57cec5SDimitry Andric};
16790b57cec5SDimitry Andric
16800b57cec5SDimitry Andrictemplate <class _CharT>
1681cb14a3feSDimitry Andricvoid __back_ref<_CharT>::__exec(__state& __s) const {
16820b57cec5SDimitry Andric  if (__mexp_ > __s.__sub_matches_.size())
16830b57cec5SDimitry Andric    __throw_regex_error<regex_constants::error_backref>();
16840b57cec5SDimitry Andric  sub_match<const _CharT*>& __sm = __s.__sub_matches_[__mexp_ - 1];
1685cb14a3feSDimitry Andric  if (__sm.matched) {
16860b57cec5SDimitry Andric    ptrdiff_t __len = __sm.second - __sm.first;
1687cb14a3feSDimitry Andric    if (__s.__last_ - __s.__current_ >= __len && std::equal(__sm.first, __sm.second, __s.__current_)) {
16880b57cec5SDimitry Andric      __s.__do_ = __state::__accept_but_not_consume;
16890b57cec5SDimitry Andric      __s.__current_ += __len;
16900b57cec5SDimitry Andric      __s.__node_ = this->first();
1691cb14a3feSDimitry Andric    } else {
16920b57cec5SDimitry Andric      __s.__do_   = __state::__reject;
16930b57cec5SDimitry Andric      __s.__node_ = nullptr;
16940b57cec5SDimitry Andric    }
1695cb14a3feSDimitry Andric  } else {
16960b57cec5SDimitry Andric    __s.__do_   = __state::__reject;
16970b57cec5SDimitry Andric    __s.__node_ = nullptr;
16980b57cec5SDimitry Andric  }
16990b57cec5SDimitry Andric}
17000b57cec5SDimitry Andric
17010b57cec5SDimitry Andric// __back_ref_icase
17020b57cec5SDimitry Andric
17030b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
1704cb14a3feSDimitry Andricclass __back_ref_icase : public __owns_one_state<_CharT> {
17050b57cec5SDimitry Andric  typedef __owns_one_state<_CharT> base;
17060b57cec5SDimitry Andric
17070b57cec5SDimitry Andric  _Traits __traits_;
17080b57cec5SDimitry Andric  unsigned __mexp_;
1709cb14a3feSDimitry Andric
17100b57cec5SDimitry Andricpublic:
17115f757f3fSDimitry Andric  typedef std::__state<_CharT> __state;
17120b57cec5SDimitry Andric
1713cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI explicit __back_ref_icase(const _Traits& __traits, unsigned __mexp, __node<_CharT>* __s)
17140b57cec5SDimitry Andric      : base(__s), __traits_(__traits), __mexp_(__mexp) {}
17150b57cec5SDimitry Andric
171606c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
17170b57cec5SDimitry Andric};
17180b57cec5SDimitry Andric
17190b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
1720cb14a3feSDimitry Andricvoid __back_ref_icase<_CharT, _Traits>::__exec(__state& __s) const {
17210b57cec5SDimitry Andric  sub_match<const _CharT*>& __sm = __s.__sub_matches_[__mexp_ - 1];
1722cb14a3feSDimitry Andric  if (__sm.matched) {
17230b57cec5SDimitry Andric    ptrdiff_t __len = __sm.second - __sm.first;
1724cb14a3feSDimitry Andric    if (__s.__last_ - __s.__current_ >= __len) {
1725cb14a3feSDimitry Andric      for (ptrdiff_t __i = 0; __i < __len; ++__i) {
1726cb14a3feSDimitry Andric        if (__traits_.translate_nocase(__sm.first[__i]) != __traits_.translate_nocase(__s.__current_[__i]))
17270b57cec5SDimitry Andric          goto __not_equal;
17280b57cec5SDimitry Andric      }
17290b57cec5SDimitry Andric      __s.__do_ = __state::__accept_but_not_consume;
17300b57cec5SDimitry Andric      __s.__current_ += __len;
17310b57cec5SDimitry Andric      __s.__node_ = this->first();
1732cb14a3feSDimitry Andric    } else {
17330b57cec5SDimitry Andric      __s.__do_   = __state::__reject;
17340b57cec5SDimitry Andric      __s.__node_ = nullptr;
17350b57cec5SDimitry Andric    }
1736cb14a3feSDimitry Andric  } else {
17370b57cec5SDimitry Andric  __not_equal:
17380b57cec5SDimitry Andric    __s.__do_   = __state::__reject;
17390b57cec5SDimitry Andric    __s.__node_ = nullptr;
17400b57cec5SDimitry Andric  }
17410b57cec5SDimitry Andric}
17420b57cec5SDimitry Andric
17430b57cec5SDimitry Andric// __back_ref_collate
17440b57cec5SDimitry Andric
17450b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
1746cb14a3feSDimitry Andricclass __back_ref_collate : public __owns_one_state<_CharT> {
17470b57cec5SDimitry Andric  typedef __owns_one_state<_CharT> base;
17480b57cec5SDimitry Andric
17490b57cec5SDimitry Andric  _Traits __traits_;
17500b57cec5SDimitry Andric  unsigned __mexp_;
1751cb14a3feSDimitry Andric
17520b57cec5SDimitry Andricpublic:
17535f757f3fSDimitry Andric  typedef std::__state<_CharT> __state;
17540b57cec5SDimitry Andric
1755cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI explicit __back_ref_collate(const _Traits& __traits, unsigned __mexp, __node<_CharT>* __s)
17560b57cec5SDimitry Andric      : base(__s), __traits_(__traits), __mexp_(__mexp) {}
17570b57cec5SDimitry Andric
175806c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
17590b57cec5SDimitry Andric};
17600b57cec5SDimitry Andric
17610b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
1762cb14a3feSDimitry Andricvoid __back_ref_collate<_CharT, _Traits>::__exec(__state& __s) const {
17630b57cec5SDimitry Andric  sub_match<const _CharT*>& __sm = __s.__sub_matches_[__mexp_ - 1];
1764cb14a3feSDimitry Andric  if (__sm.matched) {
17650b57cec5SDimitry Andric    ptrdiff_t __len = __sm.second - __sm.first;
1766cb14a3feSDimitry Andric    if (__s.__last_ - __s.__current_ >= __len) {
1767cb14a3feSDimitry Andric      for (ptrdiff_t __i = 0; __i < __len; ++__i) {
1768cb14a3feSDimitry Andric        if (__traits_.translate(__sm.first[__i]) != __traits_.translate(__s.__current_[__i]))
17690b57cec5SDimitry Andric          goto __not_equal;
17700b57cec5SDimitry Andric      }
17710b57cec5SDimitry Andric      __s.__do_ = __state::__accept_but_not_consume;
17720b57cec5SDimitry Andric      __s.__current_ += __len;
17730b57cec5SDimitry Andric      __s.__node_ = this->first();
1774cb14a3feSDimitry Andric    } else {
17750b57cec5SDimitry Andric      __s.__do_   = __state::__reject;
17760b57cec5SDimitry Andric      __s.__node_ = nullptr;
17770b57cec5SDimitry Andric    }
1778cb14a3feSDimitry Andric  } else {
17790b57cec5SDimitry Andric  __not_equal:
17800b57cec5SDimitry Andric    __s.__do_   = __state::__reject;
17810b57cec5SDimitry Andric    __s.__node_ = nullptr;
17820b57cec5SDimitry Andric  }
17830b57cec5SDimitry Andric}
17840b57cec5SDimitry Andric
17850b57cec5SDimitry Andric// __word_boundary
17860b57cec5SDimitry Andric
17870b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
1788cb14a3feSDimitry Andricclass __word_boundary : public __owns_one_state<_CharT> {
17890b57cec5SDimitry Andric  typedef __owns_one_state<_CharT> base;
17900b57cec5SDimitry Andric
17910b57cec5SDimitry Andric  _Traits __traits_;
17920b57cec5SDimitry Andric  bool __invert_;
1793cb14a3feSDimitry Andric
17940b57cec5SDimitry Andricpublic:
17955f757f3fSDimitry Andric  typedef std::__state<_CharT> __state;
17960b57cec5SDimitry Andric
1797cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI explicit __word_boundary(const _Traits& __traits, bool __invert, __node<_CharT>* __s)
17980b57cec5SDimitry Andric      : base(__s), __traits_(__traits), __invert_(__invert) {}
17990b57cec5SDimitry Andric
180006c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
18010b57cec5SDimitry Andric};
18020b57cec5SDimitry Andric
18030b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
1804cb14a3feSDimitry Andricvoid __word_boundary<_CharT, _Traits>::__exec(__state& __s) const {
18050b57cec5SDimitry Andric  bool __is_word_b = false;
1806cb14a3feSDimitry Andric  if (__s.__first_ != __s.__last_) {
1807cb14a3feSDimitry Andric    if (__s.__current_ == __s.__last_) {
1808cb14a3feSDimitry Andric      if (!(__s.__flags_ & regex_constants::match_not_eow)) {
18090b57cec5SDimitry Andric        _CharT __c  = __s.__current_[-1];
1810cb14a3feSDimitry Andric        __is_word_b = __c == '_' || __traits_.isctype(__c, ctype_base::alnum);
18110b57cec5SDimitry Andric      }
1812cb14a3feSDimitry Andric    } else if (__s.__current_ == __s.__first_ && !(__s.__flags_ & regex_constants::match_prev_avail)) {
1813cb14a3feSDimitry Andric      if (!(__s.__flags_ & regex_constants::match_not_bow)) {
18140b57cec5SDimitry Andric        _CharT __c  = *__s.__current_;
1815cb14a3feSDimitry Andric        __is_word_b = __c == '_' || __traits_.isctype(__c, ctype_base::alnum);
18160b57cec5SDimitry Andric      }
1817cb14a3feSDimitry Andric    } else {
18180b57cec5SDimitry Andric      _CharT __c1    = __s.__current_[-1];
18190b57cec5SDimitry Andric      _CharT __c2    = *__s.__current_;
1820cb14a3feSDimitry Andric      bool __is_c1_b = __c1 == '_' || __traits_.isctype(__c1, ctype_base::alnum);
1821cb14a3feSDimitry Andric      bool __is_c2_b = __c2 == '_' || __traits_.isctype(__c2, ctype_base::alnum);
18220b57cec5SDimitry Andric      __is_word_b    = __is_c1_b != __is_c2_b;
18230b57cec5SDimitry Andric    }
18240b57cec5SDimitry Andric  }
1825cb14a3feSDimitry Andric  if (__is_word_b != __invert_) {
18260b57cec5SDimitry Andric    __s.__do_   = __state::__accept_but_not_consume;
18270b57cec5SDimitry Andric    __s.__node_ = this->first();
1828cb14a3feSDimitry Andric  } else {
18290b57cec5SDimitry Andric    __s.__do_   = __state::__reject;
18300b57cec5SDimitry Andric    __s.__node_ = nullptr;
18310b57cec5SDimitry Andric  }
18320b57cec5SDimitry Andric}
18330b57cec5SDimitry Andric
18340b57cec5SDimitry Andric// __l_anchor
18350b57cec5SDimitry Andric
18360b57cec5SDimitry Andrictemplate <class _CharT>
1837cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool __is_eol(_CharT __c) {
1838753f127fSDimitry Andric  return __c == '\r' || __c == '\n';
1839e8d8bef9SDimitry Andric}
1840e8d8bef9SDimitry Andric
1841e8d8bef9SDimitry Andrictemplate <class _CharT>
1842cb14a3feSDimitry Andricclass __l_anchor_multiline : public __owns_one_state<_CharT> {
18430b57cec5SDimitry Andric  typedef __owns_one_state<_CharT> base;
18440b57cec5SDimitry Andric
1845fe6060f1SDimitry Andric  bool __multiline_;
1846e8d8bef9SDimitry Andric
18470b57cec5SDimitry Andricpublic:
18485f757f3fSDimitry Andric  typedef std::__state<_CharT> __state;
18490b57cec5SDimitry Andric
1850cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI __l_anchor_multiline(bool __multiline, __node<_CharT>* __s)
1851fe6060f1SDimitry Andric      : base(__s), __multiline_(__multiline) {}
18520b57cec5SDimitry Andric
185306c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
18540b57cec5SDimitry Andric};
18550b57cec5SDimitry Andric
18560b57cec5SDimitry Andrictemplate <class _CharT>
1857cb14a3feSDimitry Andricvoid __l_anchor_multiline<_CharT>::__exec(__state& __s) const {
1858cb14a3feSDimitry Andric  if (__s.__at_first_ && __s.__current_ == __s.__first_ && !(__s.__flags_ & regex_constants::match_not_bol)) {
18590b57cec5SDimitry Andric    __s.__do_   = __state::__accept_but_not_consume;
18600b57cec5SDimitry Andric    __s.__node_ = this->first();
1861cb14a3feSDimitry Andric  } else if (__multiline_ && !__s.__at_first_ && std::__is_eol(*std::prev(__s.__current_))) {
1862e8d8bef9SDimitry Andric    __s.__do_   = __state::__accept_but_not_consume;
1863e8d8bef9SDimitry Andric    __s.__node_ = this->first();
1864cb14a3feSDimitry Andric  } else {
18650b57cec5SDimitry Andric    __s.__do_   = __state::__reject;
18660b57cec5SDimitry Andric    __s.__node_ = nullptr;
18670b57cec5SDimitry Andric  }
18680b57cec5SDimitry Andric}
18690b57cec5SDimitry Andric
18700b57cec5SDimitry Andric// __r_anchor
18710b57cec5SDimitry Andric
18720b57cec5SDimitry Andrictemplate <class _CharT>
1873cb14a3feSDimitry Andricclass __r_anchor_multiline : public __owns_one_state<_CharT> {
18740b57cec5SDimitry Andric  typedef __owns_one_state<_CharT> base;
18750b57cec5SDimitry Andric
187681ad6265SDimitry Andric  bool __multiline_;
1877e8d8bef9SDimitry Andric
18780b57cec5SDimitry Andricpublic:
18795f757f3fSDimitry Andric  typedef std::__state<_CharT> __state;
18800b57cec5SDimitry Andric
1881cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI __r_anchor_multiline(bool __multiline, __node<_CharT>* __s)
188281ad6265SDimitry Andric      : base(__s), __multiline_(__multiline) {}
18830b57cec5SDimitry Andric
188406c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
18850b57cec5SDimitry Andric};
18860b57cec5SDimitry Andric
18870b57cec5SDimitry Andrictemplate <class _CharT>
1888cb14a3feSDimitry Andricvoid __r_anchor_multiline<_CharT>::__exec(__state& __s) const {
1889cb14a3feSDimitry Andric  if (__s.__current_ == __s.__last_ && !(__s.__flags_ & regex_constants::match_not_eol)) {
18900b57cec5SDimitry Andric    __s.__do_   = __state::__accept_but_not_consume;
18910b57cec5SDimitry Andric    __s.__node_ = this->first();
1892cb14a3feSDimitry Andric  } else if (__multiline_ && std::__is_eol(*__s.__current_)) {
1893e8d8bef9SDimitry Andric    __s.__do_   = __state::__accept_but_not_consume;
1894e8d8bef9SDimitry Andric    __s.__node_ = this->first();
1895cb14a3feSDimitry Andric  } else {
18960b57cec5SDimitry Andric    __s.__do_   = __state::__reject;
18970b57cec5SDimitry Andric    __s.__node_ = nullptr;
18980b57cec5SDimitry Andric  }
18990b57cec5SDimitry Andric}
19000b57cec5SDimitry Andric
19010b57cec5SDimitry Andric// __match_any
19020b57cec5SDimitry Andric
19030b57cec5SDimitry Andrictemplate <class _CharT>
1904cb14a3feSDimitry Andricclass __match_any : public __owns_one_state<_CharT> {
19050b57cec5SDimitry Andric  typedef __owns_one_state<_CharT> base;
19060b57cec5SDimitry Andric
19070b57cec5SDimitry Andricpublic:
19085f757f3fSDimitry Andric  typedef std::__state<_CharT> __state;
19090b57cec5SDimitry Andric
1910cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI __match_any(__node<_CharT>* __s) : base(__s) {}
19110b57cec5SDimitry Andric
191206c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
19130b57cec5SDimitry Andric};
19140b57cec5SDimitry Andric
19150b57cec5SDimitry Andrictemplate <class _CharT>
1916cb14a3feSDimitry Andricvoid __match_any<_CharT>::__exec(__state& __s) const {
1917cb14a3feSDimitry Andric  if (__s.__current_ != __s.__last_ && *__s.__current_ != 0) {
19180b57cec5SDimitry Andric    __s.__do_ = __state::__accept_and_consume;
19190b57cec5SDimitry Andric    ++__s.__current_;
19200b57cec5SDimitry Andric    __s.__node_ = this->first();
1921cb14a3feSDimitry Andric  } else {
19220b57cec5SDimitry Andric    __s.__do_   = __state::__reject;
19230b57cec5SDimitry Andric    __s.__node_ = nullptr;
19240b57cec5SDimitry Andric  }
19250b57cec5SDimitry Andric}
19260b57cec5SDimitry Andric
19270b57cec5SDimitry Andric// __match_any_but_newline
19280b57cec5SDimitry Andric
19290b57cec5SDimitry Andrictemplate <class _CharT>
1930cb14a3feSDimitry Andricclass __match_any_but_newline : public __owns_one_state<_CharT> {
19310b57cec5SDimitry Andric  typedef __owns_one_state<_CharT> base;
19320b57cec5SDimitry Andric
19330b57cec5SDimitry Andricpublic:
19345f757f3fSDimitry Andric  typedef std::__state<_CharT> __state;
19350b57cec5SDimitry Andric
1936cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI __match_any_but_newline(__node<_CharT>* __s) : base(__s) {}
19370b57cec5SDimitry Andric
1938bdd1243dSDimitry Andric  void __exec(__state&) const override;
19390b57cec5SDimitry Andric};
19400b57cec5SDimitry Andric
1941cb14a3feSDimitry Andrictemplate <>
1942cb14a3feSDimitry Andric_LIBCPP_EXPORTED_FROM_ABI void __match_any_but_newline<char>::__exec(__state&) const;
1943349cc55cSDimitry Andric#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1944cb14a3feSDimitry Andrictemplate <>
1945cb14a3feSDimitry Andric_LIBCPP_EXPORTED_FROM_ABI void __match_any_but_newline<wchar_t>::__exec(__state&) const;
1946349cc55cSDimitry Andric#endif
19470b57cec5SDimitry Andric
19480b57cec5SDimitry Andric// __match_char
19490b57cec5SDimitry Andric
19500b57cec5SDimitry Andrictemplate <class _CharT>
1951cb14a3feSDimitry Andricclass __match_char : public __owns_one_state<_CharT> {
19520b57cec5SDimitry Andric  typedef __owns_one_state<_CharT> base;
19530b57cec5SDimitry Andric
19540b57cec5SDimitry Andric  _CharT __c_;
19550b57cec5SDimitry Andric
19560b57cec5SDimitry Andric  __match_char(const __match_char&);
19570b57cec5SDimitry Andric  __match_char& operator=(const __match_char&);
1958cb14a3feSDimitry Andric
19590b57cec5SDimitry Andricpublic:
19605f757f3fSDimitry Andric  typedef std::__state<_CharT> __state;
19610b57cec5SDimitry Andric
1962cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI __match_char(_CharT __c, __node<_CharT>* __s) : base(__s), __c_(__c) {}
19630b57cec5SDimitry Andric
196406c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
19650b57cec5SDimitry Andric};
19660b57cec5SDimitry Andric
19670b57cec5SDimitry Andrictemplate <class _CharT>
1968cb14a3feSDimitry Andricvoid __match_char<_CharT>::__exec(__state& __s) const {
1969cb14a3feSDimitry Andric  if (__s.__current_ != __s.__last_ && *__s.__current_ == __c_) {
19700b57cec5SDimitry Andric    __s.__do_ = __state::__accept_and_consume;
19710b57cec5SDimitry Andric    ++__s.__current_;
19720b57cec5SDimitry Andric    __s.__node_ = this->first();
1973cb14a3feSDimitry Andric  } else {
19740b57cec5SDimitry Andric    __s.__do_   = __state::__reject;
19750b57cec5SDimitry Andric    __s.__node_ = nullptr;
19760b57cec5SDimitry Andric  }
19770b57cec5SDimitry Andric}
19780b57cec5SDimitry Andric
19790b57cec5SDimitry Andric// __match_char_icase
19800b57cec5SDimitry Andric
19810b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
1982cb14a3feSDimitry Andricclass __match_char_icase : public __owns_one_state<_CharT> {
19830b57cec5SDimitry Andric  typedef __owns_one_state<_CharT> base;
19840b57cec5SDimitry Andric
19850b57cec5SDimitry Andric  _Traits __traits_;
19860b57cec5SDimitry Andric  _CharT __c_;
19870b57cec5SDimitry Andric
19880b57cec5SDimitry Andric  __match_char_icase(const __match_char_icase&);
19890b57cec5SDimitry Andric  __match_char_icase& operator=(const __match_char_icase&);
1990cb14a3feSDimitry Andric
19910b57cec5SDimitry Andricpublic:
19925f757f3fSDimitry Andric  typedef std::__state<_CharT> __state;
19930b57cec5SDimitry Andric
1994cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI __match_char_icase(const _Traits& __traits, _CharT __c, __node<_CharT>* __s)
19950b57cec5SDimitry Andric      : base(__s), __traits_(__traits), __c_(__traits.translate_nocase(__c)) {}
19960b57cec5SDimitry Andric
199706c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
19980b57cec5SDimitry Andric};
19990b57cec5SDimitry Andric
20000b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
2001cb14a3feSDimitry Andricvoid __match_char_icase<_CharT, _Traits>::__exec(__state& __s) const {
2002cb14a3feSDimitry Andric  if (__s.__current_ != __s.__last_ && __traits_.translate_nocase(*__s.__current_) == __c_) {
20030b57cec5SDimitry Andric    __s.__do_ = __state::__accept_and_consume;
20040b57cec5SDimitry Andric    ++__s.__current_;
20050b57cec5SDimitry Andric    __s.__node_ = this->first();
2006cb14a3feSDimitry Andric  } else {
20070b57cec5SDimitry Andric    __s.__do_   = __state::__reject;
20080b57cec5SDimitry Andric    __s.__node_ = nullptr;
20090b57cec5SDimitry Andric  }
20100b57cec5SDimitry Andric}
20110b57cec5SDimitry Andric
20120b57cec5SDimitry Andric// __match_char_collate
20130b57cec5SDimitry Andric
20140b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
2015cb14a3feSDimitry Andricclass __match_char_collate : public __owns_one_state<_CharT> {
20160b57cec5SDimitry Andric  typedef __owns_one_state<_CharT> base;
20170b57cec5SDimitry Andric
20180b57cec5SDimitry Andric  _Traits __traits_;
20190b57cec5SDimitry Andric  _CharT __c_;
20200b57cec5SDimitry Andric
20210b57cec5SDimitry Andric  __match_char_collate(const __match_char_collate&);
20220b57cec5SDimitry Andric  __match_char_collate& operator=(const __match_char_collate&);
2023cb14a3feSDimitry Andric
20240b57cec5SDimitry Andricpublic:
20255f757f3fSDimitry Andric  typedef std::__state<_CharT> __state;
20260b57cec5SDimitry Andric
2027cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI __match_char_collate(const _Traits& __traits, _CharT __c, __node<_CharT>* __s)
20280b57cec5SDimitry Andric      : base(__s), __traits_(__traits), __c_(__traits.translate(__c)) {}
20290b57cec5SDimitry Andric
203006c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
20310b57cec5SDimitry Andric};
20320b57cec5SDimitry Andric
20330b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
2034cb14a3feSDimitry Andricvoid __match_char_collate<_CharT, _Traits>::__exec(__state& __s) const {
2035cb14a3feSDimitry Andric  if (__s.__current_ != __s.__last_ && __traits_.translate(*__s.__current_) == __c_) {
20360b57cec5SDimitry Andric    __s.__do_ = __state::__accept_and_consume;
20370b57cec5SDimitry Andric    ++__s.__current_;
20380b57cec5SDimitry Andric    __s.__node_ = this->first();
2039cb14a3feSDimitry Andric  } else {
20400b57cec5SDimitry Andric    __s.__do_   = __state::__reject;
20410b57cec5SDimitry Andric    __s.__node_ = nullptr;
20420b57cec5SDimitry Andric  }
20430b57cec5SDimitry Andric}
20440b57cec5SDimitry Andric
20450b57cec5SDimitry Andric// __bracket_expression
20460b57cec5SDimitry Andric
20470b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
2048cb14a3feSDimitry Andricclass __bracket_expression : public __owns_one_state<_CharT> {
20490b57cec5SDimitry Andric  typedef __owns_one_state<_CharT> base;
20500b57cec5SDimitry Andric  typedef typename _Traits::string_type string_type;
20510b57cec5SDimitry Andric
20520b57cec5SDimitry Andric  _Traits __traits_;
20530b57cec5SDimitry Andric  vector<_CharT> __chars_;
20540b57cec5SDimitry Andric  vector<_CharT> __neg_chars_;
20550b57cec5SDimitry Andric  vector<pair<string_type, string_type> > __ranges_;
20560b57cec5SDimitry Andric  vector<pair<_CharT, _CharT> > __digraphs_;
20570b57cec5SDimitry Andric  vector<string_type> __equivalences_;
20580b57cec5SDimitry Andric  typename regex_traits<_CharT>::char_class_type __mask_;
20590b57cec5SDimitry Andric  typename regex_traits<_CharT>::char_class_type __neg_mask_;
20600b57cec5SDimitry Andric  bool __negate_;
20610b57cec5SDimitry Andric  bool __icase_;
20620b57cec5SDimitry Andric  bool __collate_;
20630b57cec5SDimitry Andric  bool __might_have_digraph_;
20640b57cec5SDimitry Andric
20650b57cec5SDimitry Andric  __bracket_expression(const __bracket_expression&);
20660b57cec5SDimitry Andric  __bracket_expression& operator=(const __bracket_expression&);
2067cb14a3feSDimitry Andric
20680b57cec5SDimitry Andricpublic:
20695f757f3fSDimitry Andric  typedef std::__state<_CharT> __state;
20700b57cec5SDimitry Andric
20715f757f3fSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
2072cb14a3feSDimitry Andric  __bracket_expression(const _Traits& __traits, __node<_CharT>* __s, bool __negate, bool __icase, bool __collate)
2073cb14a3feSDimitry Andric      : base(__s),
2074cb14a3feSDimitry Andric        __traits_(__traits),
2075cb14a3feSDimitry Andric        __mask_(),
2076cb14a3feSDimitry Andric        __neg_mask_(),
2077cb14a3feSDimitry Andric        __negate_(__negate),
2078cb14a3feSDimitry Andric        __icase_(__icase),
2079cb14a3feSDimitry Andric        __collate_(__collate),
20800b57cec5SDimitry Andric        __might_have_digraph_(__traits_.getloc().name() != "C") {}
20810b57cec5SDimitry Andric
208206c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
20830b57cec5SDimitry Andric
2084cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI bool __negated() const { return __negate_; }
20850b57cec5SDimitry Andric
2086cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI void __add_char(_CharT __c) {
20870b57cec5SDimitry Andric    if (__icase_)
20880b57cec5SDimitry Andric      __chars_.push_back(__traits_.translate_nocase(__c));
20890b57cec5SDimitry Andric    else if (__collate_)
20900b57cec5SDimitry Andric      __chars_.push_back(__traits_.translate(__c));
20910b57cec5SDimitry Andric    else
20920b57cec5SDimitry Andric      __chars_.push_back(__c);
20930b57cec5SDimitry Andric  }
2094cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI void __add_neg_char(_CharT __c) {
20950b57cec5SDimitry Andric    if (__icase_)
20960b57cec5SDimitry Andric      __neg_chars_.push_back(__traits_.translate_nocase(__c));
20970b57cec5SDimitry Andric    else if (__collate_)
20980b57cec5SDimitry Andric      __neg_chars_.push_back(__traits_.translate(__c));
20990b57cec5SDimitry Andric    else
21000b57cec5SDimitry Andric      __neg_chars_.push_back(__c);
21010b57cec5SDimitry Andric  }
2102cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI void __add_range(string_type __b, string_type __e) {
2103cb14a3feSDimitry Andric    if (__collate_) {
2104cb14a3feSDimitry Andric      if (__icase_) {
21050b57cec5SDimitry Andric        for (size_t __i = 0; __i < __b.size(); ++__i)
21060b57cec5SDimitry Andric          __b[__i] = __traits_.translate_nocase(__b[__i]);
21070b57cec5SDimitry Andric        for (size_t __i = 0; __i < __e.size(); ++__i)
21080b57cec5SDimitry Andric          __e[__i] = __traits_.translate_nocase(__e[__i]);
2109cb14a3feSDimitry Andric      } else {
21100b57cec5SDimitry Andric        for (size_t __i = 0; __i < __b.size(); ++__i)
21110b57cec5SDimitry Andric          __b[__i] = __traits_.translate(__b[__i]);
21120b57cec5SDimitry Andric        for (size_t __i = 0; __i < __e.size(); ++__i)
21130b57cec5SDimitry Andric          __e[__i] = __traits_.translate(__e[__i]);
21140b57cec5SDimitry Andric      }
2115cb14a3feSDimitry Andric      __ranges_.push_back(
2116cb14a3feSDimitry Andric          std::make_pair(__traits_.transform(__b.begin(), __b.end()), __traits_.transform(__e.begin(), __e.end())));
2117cb14a3feSDimitry Andric    } else {
21180b57cec5SDimitry Andric      if (__b.size() != 1 || __e.size() != 1)
21190b57cec5SDimitry Andric        __throw_regex_error<regex_constants::error_range>();
2120cb14a3feSDimitry Andric      if (__icase_) {
21210b57cec5SDimitry Andric        __b[0] = __traits_.translate_nocase(__b[0]);
21220b57cec5SDimitry Andric        __e[0] = __traits_.translate_nocase(__e[0]);
21230b57cec5SDimitry Andric      }
21245f757f3fSDimitry Andric      __ranges_.push_back(std::make_pair(std::move(__b), std::move(__e)));
21250b57cec5SDimitry Andric    }
21260b57cec5SDimitry Andric  }
2127cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI void __add_digraph(_CharT __c1, _CharT __c2) {
21280b57cec5SDimitry Andric    if (__icase_)
2129cb14a3feSDimitry Andric      __digraphs_.push_back(std::make_pair(__traits_.translate_nocase(__c1), __traits_.translate_nocase(__c2)));
21300b57cec5SDimitry Andric    else if (__collate_)
2131cb14a3feSDimitry Andric      __digraphs_.push_back(std::make_pair(__traits_.translate(__c1), __traits_.translate(__c2)));
21320b57cec5SDimitry Andric    else
2133bdd1243dSDimitry Andric      __digraphs_.push_back(std::make_pair(__c1, __c2));
21340b57cec5SDimitry Andric  }
2135cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI void __add_equivalence(const string_type& __s) { __equivalences_.push_back(__s); }
2136cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI void __add_class(typename regex_traits<_CharT>::char_class_type __mask) { __mask_ |= __mask; }
2137cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI void __add_neg_class(typename regex_traits<_CharT>::char_class_type __mask) {
2138cb14a3feSDimitry Andric    __neg_mask_ |= __mask;
2139cb14a3feSDimitry Andric  }
21400b57cec5SDimitry Andric};
21410b57cec5SDimitry Andric
21420b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
2143cb14a3feSDimitry Andricvoid __bracket_expression<_CharT, _Traits>::__exec(__state& __s) const {
21440b57cec5SDimitry Andric  bool __found        = false;
21450b57cec5SDimitry Andric  unsigned __consumed = 0;
2146cb14a3feSDimitry Andric  if (__s.__current_ != __s.__last_) {
21470b57cec5SDimitry Andric    ++__consumed;
2148cb14a3feSDimitry Andric    if (__might_have_digraph_) {
21495f757f3fSDimitry Andric      const _CharT* __next = std::next(__s.__current_);
2150cb14a3feSDimitry Andric      if (__next != __s.__last_) {
21510b57cec5SDimitry Andric        pair<_CharT, _CharT> __ch2(*__s.__current_, *__next);
2152cb14a3feSDimitry Andric        if (__icase_) {
21530b57cec5SDimitry Andric          __ch2.first  = __traits_.translate_nocase(__ch2.first);
21540b57cec5SDimitry Andric          __ch2.second = __traits_.translate_nocase(__ch2.second);
2155cb14a3feSDimitry Andric        } else if (__collate_) {
21560b57cec5SDimitry Andric          __ch2.first  = __traits_.translate(__ch2.first);
21570b57cec5SDimitry Andric          __ch2.second = __traits_.translate(__ch2.second);
21580b57cec5SDimitry Andric        }
2159cb14a3feSDimitry Andric        if (!__traits_.lookup_collatename(&__ch2.first, &__ch2.first + 2).empty()) {
21600b57cec5SDimitry Andric          // __ch2 is a digraph in this locale
21610b57cec5SDimitry Andric          ++__consumed;
2162cb14a3feSDimitry Andric          for (size_t __i = 0; __i < __digraphs_.size(); ++__i) {
2163cb14a3feSDimitry Andric            if (__ch2 == __digraphs_[__i]) {
21640b57cec5SDimitry Andric              __found = true;
21650b57cec5SDimitry Andric              goto __exit;
21660b57cec5SDimitry Andric            }
21670b57cec5SDimitry Andric          }
2168cb14a3feSDimitry Andric          if (__collate_ && !__ranges_.empty()) {
2169cb14a3feSDimitry Andric            string_type __s2 = __traits_.transform(&__ch2.first, &__ch2.first + 2);
2170cb14a3feSDimitry Andric            for (size_t __i = 0; __i < __ranges_.size(); ++__i) {
2171cb14a3feSDimitry Andric              if (__ranges_[__i].first <= __s2 && __s2 <= __ranges_[__i].second) {
21720b57cec5SDimitry Andric                __found = true;
21730b57cec5SDimitry Andric                goto __exit;
21740b57cec5SDimitry Andric              }
21750b57cec5SDimitry Andric            }
21760b57cec5SDimitry Andric          }
2177cb14a3feSDimitry Andric          if (!__equivalences_.empty()) {
2178cb14a3feSDimitry Andric            string_type __s2 = __traits_.transform_primary(&__ch2.first, &__ch2.first + 2);
2179cb14a3feSDimitry Andric            for (size_t __i = 0; __i < __equivalences_.size(); ++__i) {
2180cb14a3feSDimitry Andric              if (__s2 == __equivalences_[__i]) {
21810b57cec5SDimitry Andric                __found = true;
21820b57cec5SDimitry Andric                goto __exit;
21830b57cec5SDimitry Andric              }
21840b57cec5SDimitry Andric            }
21850b57cec5SDimitry Andric          }
2186cb14a3feSDimitry Andric          if (__traits_.isctype(__ch2.first, __mask_) && __traits_.isctype(__ch2.second, __mask_)) {
21870b57cec5SDimitry Andric            __found = true;
21880b57cec5SDimitry Andric            goto __exit;
21890b57cec5SDimitry Andric          }
2190cb14a3feSDimitry Andric          if (!__traits_.isctype(__ch2.first, __neg_mask_) && !__traits_.isctype(__ch2.second, __neg_mask_)) {
21910b57cec5SDimitry Andric            __found = true;
21920b57cec5SDimitry Andric            goto __exit;
21930b57cec5SDimitry Andric          }
21940b57cec5SDimitry Andric          goto __exit;
21950b57cec5SDimitry Andric        }
21960b57cec5SDimitry Andric      }
21970b57cec5SDimitry Andric    }
21980b57cec5SDimitry Andric    // test *__s.__current_ as not a digraph
21990b57cec5SDimitry Andric    _CharT __ch = *__s.__current_;
22000b57cec5SDimitry Andric    if (__icase_)
22010b57cec5SDimitry Andric      __ch = __traits_.translate_nocase(__ch);
22020b57cec5SDimitry Andric    else if (__collate_)
22030b57cec5SDimitry Andric      __ch = __traits_.translate(__ch);
2204cb14a3feSDimitry Andric    for (size_t __i = 0; __i < __chars_.size(); ++__i) {
2205cb14a3feSDimitry Andric      if (__ch == __chars_[__i]) {
22060b57cec5SDimitry Andric        __found = true;
22070b57cec5SDimitry Andric        goto __exit;
22080b57cec5SDimitry Andric      }
22090b57cec5SDimitry Andric    }
22100b57cec5SDimitry Andric    // When there's at least one of __neg_chars_ and __neg_mask_, the set
22110b57cec5SDimitry Andric    // of "__found" chars is
22120b57cec5SDimitry Andric    //   union(complement(union(__neg_chars_, __neg_mask_)),
22130b57cec5SDimitry Andric    //         other cases...)
22140b57cec5SDimitry Andric    //
22150b57cec5SDimitry Andric    // It doesn't make sense to check this when there are no __neg_chars_
22160b57cec5SDimitry Andric    // and no __neg_mask_.
2217cb14a3feSDimitry Andric    if (!(__neg_mask_ == 0 && __neg_chars_.empty())) {
22180b57cec5SDimitry Andric      const bool __in_neg_mask  = __traits_.isctype(__ch, __neg_mask_);
2219cb14a3feSDimitry Andric      const bool __in_neg_chars = std::find(__neg_chars_.begin(), __neg_chars_.end(), __ch) != __neg_chars_.end();
2220cb14a3feSDimitry Andric      if (!(__in_neg_mask || __in_neg_chars)) {
22210b57cec5SDimitry Andric        __found = true;
22220b57cec5SDimitry Andric        goto __exit;
22230b57cec5SDimitry Andric      }
22240b57cec5SDimitry Andric    }
2225cb14a3feSDimitry Andric    if (!__ranges_.empty()) {
2226cb14a3feSDimitry Andric      string_type __s2 = __collate_ ? __traits_.transform(&__ch, &__ch + 1) : string_type(1, __ch);
2227cb14a3feSDimitry Andric      for (size_t __i = 0; __i < __ranges_.size(); ++__i) {
2228cb14a3feSDimitry Andric        if (__ranges_[__i].first <= __s2 && __s2 <= __ranges_[__i].second) {
22290b57cec5SDimitry Andric          __found = true;
22300b57cec5SDimitry Andric          goto __exit;
22310b57cec5SDimitry Andric        }
22320b57cec5SDimitry Andric      }
22330b57cec5SDimitry Andric    }
2234cb14a3feSDimitry Andric    if (!__equivalences_.empty()) {
22350b57cec5SDimitry Andric      string_type __s2 = __traits_.transform_primary(&__ch, &__ch + 1);
2236cb14a3feSDimitry Andric      for (size_t __i = 0; __i < __equivalences_.size(); ++__i) {
2237cb14a3feSDimitry Andric        if (__s2 == __equivalences_[__i]) {
22380b57cec5SDimitry Andric          __found = true;
22390b57cec5SDimitry Andric          goto __exit;
22400b57cec5SDimitry Andric        }
22410b57cec5SDimitry Andric      }
22420b57cec5SDimitry Andric    }
2243cb14a3feSDimitry Andric    if (__traits_.isctype(__ch, __mask_)) {
22440b57cec5SDimitry Andric      __found = true;
22450b57cec5SDimitry Andric      goto __exit;
22460b57cec5SDimitry Andric    }
2247cb14a3feSDimitry Andric  } else
22480b57cec5SDimitry Andric    __found = __negate_; // force reject
22490b57cec5SDimitry Andric__exit:
2250cb14a3feSDimitry Andric  if (__found != __negate_) {
22510b57cec5SDimitry Andric    __s.__do_ = __state::__accept_and_consume;
22520b57cec5SDimitry Andric    __s.__current_ += __consumed;
22530b57cec5SDimitry Andric    __s.__node_ = this->first();
2254cb14a3feSDimitry Andric  } else {
22550b57cec5SDimitry Andric    __s.__do_   = __state::__reject;
22560b57cec5SDimitry Andric    __s.__node_ = nullptr;
22570b57cec5SDimitry Andric  }
22580b57cec5SDimitry Andric}
22590b57cec5SDimitry Andric
2260cb14a3feSDimitry Andrictemplate <class _CharT, class _Traits>
2261cb14a3feSDimitry Andricclass __lookahead;
22620b57cec5SDimitry Andric
22630b57cec5SDimitry Andrictemplate <class _CharT, class _Traits = regex_traits<_CharT> >
2264e8d8bef9SDimitry Andricclass _LIBCPP_TEMPLATE_VIS basic_regex;
2265e8d8bef9SDimitry Andric
2266e8d8bef9SDimitry Andrictypedef basic_regex<char> regex;
2267349cc55cSDimitry Andric#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
2268e8d8bef9SDimitry Andrictypedef basic_regex<wchar_t> wregex;
2269349cc55cSDimitry Andric#endif
2270e8d8bef9SDimitry Andric
2271e8d8bef9SDimitry Andrictemplate <class _CharT, class _Traits>
2272cb14a3feSDimitry Andricclass _LIBCPP_TEMPLATE_VIS _LIBCPP_PREFERRED_NAME(regex)
2273cb14a3feSDimitry Andric    _LIBCPP_IF_WIDE_CHARACTERS(_LIBCPP_PREFERRED_NAME(wregex)) basic_regex {
22740b57cec5SDimitry Andricpublic:
22750b57cec5SDimitry Andric  // types:
22760b57cec5SDimitry Andric  typedef _CharT value_type;
22770b57cec5SDimitry Andric  typedef _Traits traits_type;
22780b57cec5SDimitry Andric  typedef typename _Traits::string_type string_type;
22790b57cec5SDimitry Andric  typedef regex_constants::syntax_option_type flag_type;
22800b57cec5SDimitry Andric  typedef typename _Traits::locale_type locale_type;
22810b57cec5SDimitry Andric
22820b57cec5SDimitry Andricprivate:
22830b57cec5SDimitry Andric  _Traits __traits_;
22840b57cec5SDimitry Andric  flag_type __flags_;
22850b57cec5SDimitry Andric  unsigned __marked_count_;
22860b57cec5SDimitry Andric  unsigned __loop_count_;
22870b57cec5SDimitry Andric  int __open_count_;
22880b57cec5SDimitry Andric  shared_ptr<__empty_state<_CharT> > __start_;
22890b57cec5SDimitry Andric  __owns_one_state<_CharT>* __end_;
22900b57cec5SDimitry Andric
22915f757f3fSDimitry Andric  typedef std::__state<_CharT> __state;
22925f757f3fSDimitry Andric  typedef std::__node<_CharT> __node;
22930b57cec5SDimitry Andric
22940b57cec5SDimitry Andricpublic:
22950b57cec5SDimitry Andric  // constants:
22960b57cec5SDimitry Andric  static const regex_constants::syntax_option_type icase      = regex_constants::icase;
22970b57cec5SDimitry Andric  static const regex_constants::syntax_option_type nosubs     = regex_constants::nosubs;
22980b57cec5SDimitry Andric  static const regex_constants::syntax_option_type optimize   = regex_constants::optimize;
22990b57cec5SDimitry Andric  static const regex_constants::syntax_option_type collate    = regex_constants::collate;
23000b57cec5SDimitry Andric  static const regex_constants::syntax_option_type ECMAScript = regex_constants::ECMAScript;
23010b57cec5SDimitry Andric  static const regex_constants::syntax_option_type basic      = regex_constants::basic;
23020b57cec5SDimitry Andric  static const regex_constants::syntax_option_type extended   = regex_constants::extended;
23030b57cec5SDimitry Andric  static const regex_constants::syntax_option_type awk        = regex_constants::awk;
23040b57cec5SDimitry Andric  static const regex_constants::syntax_option_type grep       = regex_constants::grep;
23050b57cec5SDimitry Andric  static const regex_constants::syntax_option_type egrep      = regex_constants::egrep;
2306e8d8bef9SDimitry Andric  static const regex_constants::syntax_option_type multiline  = regex_constants::multiline;
23070b57cec5SDimitry Andric
23080b57cec5SDimitry Andric  // construct/copy/destroy:
2309cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI basic_regex()
2310cb14a3feSDimitry Andric      : __flags_(regex_constants::ECMAScript),
2311cb14a3feSDimitry Andric        __marked_count_(0),
2312cb14a3feSDimitry Andric        __loop_count_(0),
2313cb14a3feSDimitry Andric        __open_count_(0),
2314cb14a3feSDimitry Andric        __end_(nullptr) {}
2315cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI explicit basic_regex(const value_type* __p, flag_type __f = regex_constants::ECMAScript)
2316cb14a3feSDimitry Andric      : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0), __end_(nullptr) {
2317480093f4SDimitry Andric    __init(__p, __p + __traits_.length(__p));
23180b57cec5SDimitry Andric  }
23190b57cec5SDimitry Andric
2320cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI basic_regex(const value_type* __p, size_t __len, flag_type __f = regex_constants::ECMAScript)
2321cb14a3feSDimitry Andric      : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0), __end_(nullptr) {
2322480093f4SDimitry Andric    __init(__p, __p + __len);
23230b57cec5SDimitry Andric  }
23240b57cec5SDimitry Andric
23250b57cec5SDimitry Andric  //     basic_regex(const basic_regex&) = default;
23260b57cec5SDimitry Andric  //     basic_regex(basic_regex&&) = default;
23270b57cec5SDimitry Andric  template <class _ST, class _SA>
2328cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI explicit basic_regex(const basic_string<value_type, _ST, _SA>& __p,
23290b57cec5SDimitry Andric                                             flag_type __f = regex_constants::ECMAScript)
2330cb14a3feSDimitry Andric      : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0), __end_(nullptr) {
2331480093f4SDimitry Andric    __init(__p.begin(), __p.end());
23320b57cec5SDimitry Andric  }
23330b57cec5SDimitry Andric
23340b57cec5SDimitry Andric  template <class _ForwardIterator>
23355f757f3fSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
2336cb14a3feSDimitry Andric  basic_regex(_ForwardIterator __first, _ForwardIterator __last, flag_type __f = regex_constants::ECMAScript)
2337cb14a3feSDimitry Andric      : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0), __end_(nullptr) {
2338480093f4SDimitry Andric    __init(__first, __last);
23390b57cec5SDimitry Andric  }
23400b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
2341cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI basic_regex(initializer_list<value_type> __il, flag_type __f = regex_constants::ECMAScript)
2342cb14a3feSDimitry Andric      : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0), __end_(nullptr) {
2343480093f4SDimitry Andric    __init(__il.begin(), __il.end());
23440b57cec5SDimitry Andric  }
23450b57cec5SDimitry Andric#endif // _LIBCPP_CXX03_LANG
23460b57cec5SDimitry Andric
23470b57cec5SDimitry Andric  //    ~basic_regex() = default;
23480b57cec5SDimitry Andric
23490b57cec5SDimitry Andric  //     basic_regex& operator=(const basic_regex&) = default;
23500b57cec5SDimitry Andric  //     basic_regex& operator=(basic_regex&&) = default;
2351cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI basic_regex& operator=(const value_type* __p) { return assign(__p); }
23520b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
2353cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI basic_regex& operator=(initializer_list<value_type> __il) { return assign(__il); }
23540b57cec5SDimitry Andric#endif // _LIBCPP_CXX03_LANG
23550b57cec5SDimitry Andric  template <class _ST, class _SA>
2356cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI basic_regex& operator=(const basic_string<value_type, _ST, _SA>& __p) {
2357cb14a3feSDimitry Andric    return assign(__p);
2358cb14a3feSDimitry Andric  }
23590b57cec5SDimitry Andric
23600b57cec5SDimitry Andric  // assign:
2361cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI basic_regex& assign(const basic_regex& __that) { return *this = __that; }
23620b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
2363cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI basic_regex& assign(basic_regex&& __that) _NOEXCEPT { return *this = std::move(__that); }
23640b57cec5SDimitry Andric#endif
2365cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI basic_regex& assign(const value_type* __p, flag_type __f = regex_constants::ECMAScript) {
2366cb14a3feSDimitry Andric    return assign(__p, __p + __traits_.length(__p), __f);
2367cb14a3feSDimitry Andric  }
2368cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI basic_regex&
2369cb14a3feSDimitry Andric  assign(const value_type* __p, size_t __len, flag_type __f = regex_constants::ECMAScript) {
2370cb14a3feSDimitry Andric    return assign(__p, __p + __len, __f);
2371cb14a3feSDimitry Andric  }
23720b57cec5SDimitry Andric  template <class _ST, class _SA>
2373cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI basic_regex&
2374cb14a3feSDimitry Andric  assign(const basic_string<value_type, _ST, _SA>& __s, flag_type __f = regex_constants::ECMAScript) {
2375cb14a3feSDimitry Andric    return assign(__s.begin(), __s.end(), __f);
2376cb14a3feSDimitry Andric  }
23770b57cec5SDimitry Andric
23785f757f3fSDimitry Andric  template <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value, int> = 0>
2379cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI basic_regex&
2380cb14a3feSDimitry Andric  assign(_InputIterator __first, _InputIterator __last, flag_type __f = regex_constants::ECMAScript) {
23810b57cec5SDimitry Andric    basic_string<_CharT> __t(__first, __last);
23820b57cec5SDimitry Andric    return assign(__t.begin(), __t.end(), __f);
23830b57cec5SDimitry Andric  }
23840b57cec5SDimitry Andric
23850b57cec5SDimitry Andricprivate:
2386cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI void __member_init(flag_type __f) {
23870b57cec5SDimitry Andric    __flags_        = __f;
23880b57cec5SDimitry Andric    __marked_count_ = 0;
23890b57cec5SDimitry Andric    __loop_count_   = 0;
23900b57cec5SDimitry Andric    __open_count_   = 0;
23910b57cec5SDimitry Andric    __end_          = nullptr;
23920b57cec5SDimitry Andric  }
23930b57cec5SDimitry Andric
2394cb14a3feSDimitry Andricpublic:
23955f757f3fSDimitry Andric  template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> = 0>
2396cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI basic_regex&
2397cb14a3feSDimitry Andric  assign(_ForwardIterator __first, _ForwardIterator __last, flag_type __f = regex_constants::ECMAScript) {
23980b57cec5SDimitry Andric    return assign(basic_regex(__first, __last, __f));
23990b57cec5SDimitry Andric  }
24000b57cec5SDimitry Andric
24010b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
24020b57cec5SDimitry Andric
2403cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI basic_regex&
2404cb14a3feSDimitry Andric  assign(initializer_list<value_type> __il, flag_type __f = regex_constants::ECMAScript) {
2405cb14a3feSDimitry Andric    return assign(__il.begin(), __il.end(), __f);
2406cb14a3feSDimitry Andric  }
24070b57cec5SDimitry Andric
24080b57cec5SDimitry Andric#endif // _LIBCPP_CXX03_LANG
24090b57cec5SDimitry Andric
24100b57cec5SDimitry Andric  // const operations:
2411cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI unsigned mark_count() const { return __marked_count_; }
2412cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI flag_type flags() const { return __flags_; }
24130b57cec5SDimitry Andric
24140b57cec5SDimitry Andric  // locale:
2415cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI locale_type imbue(locale_type __loc) {
24160b57cec5SDimitry Andric    __member_init(ECMAScript);
24170b57cec5SDimitry Andric    __start_.reset();
24180b57cec5SDimitry Andric    return __traits_.imbue(__loc);
24190b57cec5SDimitry Andric  }
2420cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI locale_type getloc() const { return __traits_.getloc(); }
24210b57cec5SDimitry Andric
24220b57cec5SDimitry Andric  // swap:
24230b57cec5SDimitry Andric  void swap(basic_regex& __r);
24240b57cec5SDimitry Andric
24250b57cec5SDimitry Andricprivate:
2426cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI unsigned __loop_count() const { return __loop_count_; }
24270b57cec5SDimitry Andric
2428cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI bool __use_multiline() const {
2429e8d8bef9SDimitry Andric    return __get_grammar(__flags_) == ECMAScript && (__flags_ & multiline);
2430e8d8bef9SDimitry Andric  }
2431e8d8bef9SDimitry Andric
24320b57cec5SDimitry Andric  template <class _ForwardIterator>
2433cb14a3feSDimitry Andric  void __init(_ForwardIterator __first, _ForwardIterator __last);
2434480093f4SDimitry Andric  template <class _ForwardIterator>
2435cb14a3feSDimitry Andric  _ForwardIterator __parse(_ForwardIterator __first, _ForwardIterator __last);
24360b57cec5SDimitry Andric  template <class _ForwardIterator>
2437cb14a3feSDimitry Andric  _ForwardIterator __parse_basic_reg_exp(_ForwardIterator __first, _ForwardIterator __last);
24380b57cec5SDimitry Andric  template <class _ForwardIterator>
2439cb14a3feSDimitry Andric  _ForwardIterator __parse_RE_expression(_ForwardIterator __first, _ForwardIterator __last);
24400b57cec5SDimitry Andric  template <class _ForwardIterator>
2441cb14a3feSDimitry Andric  _ForwardIterator __parse_simple_RE(_ForwardIterator __first, _ForwardIterator __last);
24420b57cec5SDimitry Andric  template <class _ForwardIterator>
2443cb14a3feSDimitry Andric  _ForwardIterator __parse_nondupl_RE(_ForwardIterator __first, _ForwardIterator __last);
24440b57cec5SDimitry Andric  template <class _ForwardIterator>
2445cb14a3feSDimitry Andric  _ForwardIterator __parse_one_char_or_coll_elem_RE(_ForwardIterator __first, _ForwardIterator __last);
24460b57cec5SDimitry Andric  template <class _ForwardIterator>
2447cb14a3feSDimitry Andric  _ForwardIterator __parse_Back_open_paren(_ForwardIterator __first, _ForwardIterator __last);
24480b57cec5SDimitry Andric  template <class _ForwardIterator>
2449cb14a3feSDimitry Andric  _ForwardIterator __parse_Back_close_paren(_ForwardIterator __first, _ForwardIterator __last);
24500b57cec5SDimitry Andric  template <class _ForwardIterator>
2451cb14a3feSDimitry Andric  _ForwardIterator __parse_Back_open_brace(_ForwardIterator __first, _ForwardIterator __last);
24520b57cec5SDimitry Andric  template <class _ForwardIterator>
2453cb14a3feSDimitry Andric  _ForwardIterator __parse_Back_close_brace(_ForwardIterator __first, _ForwardIterator __last);
24540b57cec5SDimitry Andric  template <class _ForwardIterator>
2455cb14a3feSDimitry Andric  _ForwardIterator __parse_BACKREF(_ForwardIterator __first, _ForwardIterator __last);
24560b57cec5SDimitry Andric  template <class _ForwardIterator>
2457cb14a3feSDimitry Andric  _ForwardIterator __parse_ORD_CHAR(_ForwardIterator __first, _ForwardIterator __last);
24580b57cec5SDimitry Andric  template <class _ForwardIterator>
2459cb14a3feSDimitry Andric  _ForwardIterator __parse_QUOTED_CHAR(_ForwardIterator __first, _ForwardIterator __last);
24600b57cec5SDimitry Andric  template <class _ForwardIterator>
2461cb14a3feSDimitry Andric  _ForwardIterator __parse_RE_dupl_symbol(
2462cb14a3feSDimitry Andric      _ForwardIterator __first,
2463cb14a3feSDimitry Andric      _ForwardIterator __last,
24640b57cec5SDimitry Andric      __owns_one_state<_CharT>* __s,
2465cb14a3feSDimitry Andric      unsigned __mexp_begin,
2466cb14a3feSDimitry Andric      unsigned __mexp_end);
24670b57cec5SDimitry Andric  template <class _ForwardIterator>
2468cb14a3feSDimitry Andric  _ForwardIterator __parse_ERE_dupl_symbol(
2469cb14a3feSDimitry Andric      _ForwardIterator __first,
2470cb14a3feSDimitry Andric      _ForwardIterator __last,
24710b57cec5SDimitry Andric      __owns_one_state<_CharT>* __s,
2472cb14a3feSDimitry Andric      unsigned __mexp_begin,
2473cb14a3feSDimitry Andric      unsigned __mexp_end);
2474cb14a3feSDimitry Andric  template <class _ForwardIterator>
2475cb14a3feSDimitry Andric  _ForwardIterator __parse_bracket_expression(_ForwardIterator __first, _ForwardIterator __last);
24760b57cec5SDimitry Andric  template <class _ForwardIterator>
24770b57cec5SDimitry Andric  _ForwardIterator
2478cb14a3feSDimitry Andric  __parse_follow_list(_ForwardIterator __first, _ForwardIterator __last, __bracket_expression<_CharT, _Traits>* __ml);
2479cb14a3feSDimitry Andric  template <class _ForwardIterator>
2480cb14a3feSDimitry Andric  _ForwardIterator __parse_expression_term(
2481cb14a3feSDimitry Andric      _ForwardIterator __first, _ForwardIterator __last, __bracket_expression<_CharT, _Traits>* __ml);
2482cb14a3feSDimitry Andric  template <class _ForwardIterator>
2483cb14a3feSDimitry Andric  _ForwardIterator __parse_equivalence_class(
2484cb14a3feSDimitry Andric      _ForwardIterator __first, _ForwardIterator __last, __bracket_expression<_CharT, _Traits>* __ml);
2485cb14a3feSDimitry Andric  template <class _ForwardIterator>
2486cb14a3feSDimitry Andric  _ForwardIterator __parse_character_class(
2487cb14a3feSDimitry Andric      _ForwardIterator __first, _ForwardIterator __last, __bracket_expression<_CharT, _Traits>* __ml);
24880b57cec5SDimitry Andric  template <class _ForwardIterator>
24890b57cec5SDimitry Andric  _ForwardIterator
2490cb14a3feSDimitry Andric  __parse_collating_symbol(_ForwardIterator __first, _ForwardIterator __last, basic_string<_CharT>& __col_sym);
2491cb14a3feSDimitry Andric  template <class _ForwardIterator>
2492cb14a3feSDimitry Andric  _ForwardIterator __parse_DUP_COUNT(_ForwardIterator __first, _ForwardIterator __last, int& __c);
2493cb14a3feSDimitry Andric  template <class _ForwardIterator>
2494cb14a3feSDimitry Andric  _ForwardIterator __parse_extended_reg_exp(_ForwardIterator __first, _ForwardIterator __last);
2495cb14a3feSDimitry Andric  template <class _ForwardIterator>
2496cb14a3feSDimitry Andric  _ForwardIterator __parse_ERE_branch(_ForwardIterator __first, _ForwardIterator __last);
2497cb14a3feSDimitry Andric  template <class _ForwardIterator>
2498cb14a3feSDimitry Andric  _ForwardIterator __parse_ERE_expression(_ForwardIterator __first, _ForwardIterator __last);
2499cb14a3feSDimitry Andric  template <class _ForwardIterator>
2500cb14a3feSDimitry Andric  _ForwardIterator __parse_one_char_or_coll_elem_ERE(_ForwardIterator __first, _ForwardIterator __last);
2501cb14a3feSDimitry Andric  template <class _ForwardIterator>
2502cb14a3feSDimitry Andric  _ForwardIterator __parse_ORD_CHAR_ERE(_ForwardIterator __first, _ForwardIterator __last);
2503cb14a3feSDimitry Andric  template <class _ForwardIterator>
2504cb14a3feSDimitry Andric  _ForwardIterator __parse_QUOTED_CHAR_ERE(_ForwardIterator __first, _ForwardIterator __last);
2505cb14a3feSDimitry Andric  template <class _ForwardIterator>
2506cb14a3feSDimitry Andric  _ForwardIterator __parse_ecma_exp(_ForwardIterator __first, _ForwardIterator __last);
2507cb14a3feSDimitry Andric  template <class _ForwardIterator>
2508cb14a3feSDimitry Andric  _ForwardIterator __parse_alternative(_ForwardIterator __first, _ForwardIterator __last);
2509cb14a3feSDimitry Andric  template <class _ForwardIterator>
2510cb14a3feSDimitry Andric  _ForwardIterator __parse_term(_ForwardIterator __first, _ForwardIterator __last);
2511cb14a3feSDimitry Andric  template <class _ForwardIterator>
2512cb14a3feSDimitry Andric  _ForwardIterator __parse_assertion(_ForwardIterator __first, _ForwardIterator __last);
2513cb14a3feSDimitry Andric  template <class _ForwardIterator>
2514cb14a3feSDimitry Andric  _ForwardIterator __parse_atom(_ForwardIterator __first, _ForwardIterator __last);
2515cb14a3feSDimitry Andric  template <class _ForwardIterator>
2516cb14a3feSDimitry Andric  _ForwardIterator __parse_atom_escape(_ForwardIterator __first, _ForwardIterator __last);
2517cb14a3feSDimitry Andric  template <class _ForwardIterator>
2518cb14a3feSDimitry Andric  _ForwardIterator __parse_decimal_escape(_ForwardIterator __first, _ForwardIterator __last);
2519cb14a3feSDimitry Andric  template <class _ForwardIterator>
2520cb14a3feSDimitry Andric  _ForwardIterator __parse_character_class_escape(_ForwardIterator __first, _ForwardIterator __last);
25210b57cec5SDimitry Andric  template <class _ForwardIterator>
25220b57cec5SDimitry Andric  _ForwardIterator
2523cb14a3feSDimitry Andric  __parse_character_escape(_ForwardIterator __first, _ForwardIterator __last, basic_string<_CharT>* __str = nullptr);
25240b57cec5SDimitry Andric  template <class _ForwardIterator>
2525cb14a3feSDimitry Andric  _ForwardIterator __parse_pattern_character(_ForwardIterator __first, _ForwardIterator __last);
25260b57cec5SDimitry Andric  template <class _ForwardIterator>
2527cb14a3feSDimitry Andric  _ForwardIterator __parse_grep(_ForwardIterator __first, _ForwardIterator __last);
25280b57cec5SDimitry Andric  template <class _ForwardIterator>
2529cb14a3feSDimitry Andric  _ForwardIterator __parse_egrep(_ForwardIterator __first, _ForwardIterator __last);
25300b57cec5SDimitry Andric  template <class _ForwardIterator>
2531cb14a3feSDimitry Andric  _ForwardIterator __parse_class_escape(
2532cb14a3feSDimitry Andric      _ForwardIterator __first,
2533cb14a3feSDimitry Andric      _ForwardIterator __last,
25340b57cec5SDimitry Andric      basic_string<_CharT>& __str,
25350b57cec5SDimitry Andric      __bracket_expression<_CharT, _Traits>* __ml);
25360b57cec5SDimitry Andric  template <class _ForwardIterator>
25370b57cec5SDimitry Andric  _ForwardIterator
2538cb14a3feSDimitry Andric  __parse_awk_escape(_ForwardIterator __first, _ForwardIterator __last, basic_string<_CharT>* __str = nullptr);
25390b57cec5SDimitry Andric
2540753f127fSDimitry Andric  bool __test_back_ref(_CharT);
25415ffd83dbSDimitry Andric
2542cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI void __push_l_anchor();
25430b57cec5SDimitry Andric  void __push_r_anchor();
25440b57cec5SDimitry Andric  void __push_match_any();
25450b57cec5SDimitry Andric  void __push_match_any_but_newline();
2546cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI void __push_greedy_inf_repeat(
2547cb14a3feSDimitry Andric      size_t __min, __owns_one_state<_CharT>* __s, unsigned __mexp_begin = 0, unsigned __mexp_end = 0) {
2548cb14a3feSDimitry Andric    __push_loop(__min, numeric_limits<size_t>::max(), __s, __mexp_begin, __mexp_end);
2549cb14a3feSDimitry Andric  }
2550cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI void __push_nongreedy_inf_repeat(
2551cb14a3feSDimitry Andric      size_t __min, __owns_one_state<_CharT>* __s, unsigned __mexp_begin = 0, unsigned __mexp_end = 0) {
2552cb14a3feSDimitry Andric    __push_loop(__min, numeric_limits<size_t>::max(), __s, __mexp_begin, __mexp_end, false);
2553cb14a3feSDimitry Andric  }
2554cb14a3feSDimitry Andric  void __push_loop(size_t __min,
2555cb14a3feSDimitry Andric                   size_t __max,
2556cb14a3feSDimitry Andric                   __owns_one_state<_CharT>* __s,
2557cb14a3feSDimitry Andric                   size_t __mexp_begin = 0,
2558cb14a3feSDimitry Andric                   size_t __mexp_end   = 0,
25590b57cec5SDimitry Andric                   bool __greedy       = true);
25600b57cec5SDimitry Andric  __bracket_expression<_CharT, _Traits>* __start_matching_list(bool __negate);
25610b57cec5SDimitry Andric  void __push_char(value_type __c);
25620b57cec5SDimitry Andric  void __push_back_ref(int __i);
2563cb14a3feSDimitry Andric  void __push_alternation(__owns_one_state<_CharT>* __sa, __owns_one_state<_CharT>* __sb);
25640b57cec5SDimitry Andric  void __push_begin_marked_subexpression();
25650b57cec5SDimitry Andric  void __push_end_marked_subexpression(unsigned);
25660b57cec5SDimitry Andric  void __push_empty();
25670b57cec5SDimitry Andric  void __push_word_boundary(bool);
25680b57cec5SDimitry Andric  void __push_lookahead(const basic_regex&, bool, unsigned);
25690b57cec5SDimitry Andric
25700b57cec5SDimitry Andric  template <class _Allocator>
2571cb14a3feSDimitry Andric  bool __search(const _CharT* __first,
2572cb14a3feSDimitry Andric                const _CharT* __last,
25730b57cec5SDimitry Andric                match_results<const _CharT*, _Allocator>& __m,
25740b57cec5SDimitry Andric                regex_constants::match_flag_type __flags) const;
25750b57cec5SDimitry Andric
25760b57cec5SDimitry Andric  template <class _Allocator>
2577cb14a3feSDimitry Andric  bool __match_at_start(const _CharT* __first,
2578cb14a3feSDimitry Andric                        const _CharT* __last,
25790b57cec5SDimitry Andric                        match_results<const _CharT*, _Allocator>& __m,
2580cb14a3feSDimitry Andric                        regex_constants::match_flag_type __flags,
2581cb14a3feSDimitry Andric                        bool) const;
25820b57cec5SDimitry Andric  template <class _Allocator>
2583cb14a3feSDimitry Andric  bool __match_at_start_ecma(
2584cb14a3feSDimitry Andric      const _CharT* __first,
2585cb14a3feSDimitry Andric      const _CharT* __last,
25860b57cec5SDimitry Andric      match_results<const _CharT*, _Allocator>& __m,
2587cb14a3feSDimitry Andric      regex_constants::match_flag_type __flags,
2588cb14a3feSDimitry Andric      bool) const;
25890b57cec5SDimitry Andric  template <class _Allocator>
2590cb14a3feSDimitry Andric  bool __match_at_start_posix_nosubs(
2591cb14a3feSDimitry Andric      const _CharT* __first,
2592cb14a3feSDimitry Andric      const _CharT* __last,
25930b57cec5SDimitry Andric      match_results<const _CharT*, _Allocator>& __m,
2594cb14a3feSDimitry Andric      regex_constants::match_flag_type __flags,
2595cb14a3feSDimitry Andric      bool) const;
25960b57cec5SDimitry Andric  template <class _Allocator>
2597cb14a3feSDimitry Andric  bool __match_at_start_posix_subs(
2598cb14a3feSDimitry Andric      const _CharT* __first,
2599cb14a3feSDimitry Andric      const _CharT* __last,
26000b57cec5SDimitry Andric      match_results<const _CharT*, _Allocator>& __m,
2601cb14a3feSDimitry Andric      regex_constants::match_flag_type __flags,
2602cb14a3feSDimitry Andric      bool) const;
26030b57cec5SDimitry Andric
26040b57cec5SDimitry Andric  template <class _Bp, class _Ap, class _Cp, class _Tp>
2605cb14a3feSDimitry Andric  friend bool
2606cb14a3feSDimitry Andric  regex_search(_Bp, _Bp, match_results<_Bp, _Ap>&, const basic_regex<_Cp, _Tp>&, regex_constants::match_flag_type);
26070b57cec5SDimitry Andric
26080b57cec5SDimitry Andric  template <class _Ap, class _Cp, class _Tp>
2609cb14a3feSDimitry Andric  friend bool
2610cb14a3feSDimitry Andric  regex_search(const _Cp*,
2611cb14a3feSDimitry Andric               const _Cp*,
2612cb14a3feSDimitry Andric               match_results<const _Cp*, _Ap>&,
2613cb14a3feSDimitry Andric               const basic_regex<_Cp, _Tp>&,
2614cb14a3feSDimitry Andric               regex_constants::match_flag_type);
26150b57cec5SDimitry Andric
26160b57cec5SDimitry Andric  template <class _Bp, class _Cp, class _Tp>
2617cb14a3feSDimitry Andric  friend bool regex_search(_Bp, _Bp, const basic_regex<_Cp, _Tp>&, regex_constants::match_flag_type);
26180b57cec5SDimitry Andric
26190b57cec5SDimitry Andric  template <class _Cp, class _Tp>
2620cb14a3feSDimitry Andric  friend bool regex_search(const _Cp*, const _Cp*, const basic_regex<_Cp, _Tp>&, regex_constants::match_flag_type);
26210b57cec5SDimitry Andric
26220b57cec5SDimitry Andric  template <class _Cp, class _Ap, class _Tp>
2623cb14a3feSDimitry Andric  friend bool regex_search(
2624cb14a3feSDimitry Andric      const _Cp*, match_results<const _Cp*, _Ap>&, const basic_regex<_Cp, _Tp>&, regex_constants::match_flag_type);
26250b57cec5SDimitry Andric
26260b57cec5SDimitry Andric  template <class _ST, class _SA, class _Cp, class _Tp>
2627cb14a3feSDimitry Andric  friend bool regex_search(const basic_string<_Cp, _ST, _SA>& __s,
26280b57cec5SDimitry Andric                           const basic_regex<_Cp, _Tp>& __e,
26290b57cec5SDimitry Andric                           regex_constants::match_flag_type __flags);
26300b57cec5SDimitry Andric
26310b57cec5SDimitry Andric  template <class _ST, class _SA, class _Ap, class _Cp, class _Tp>
2632cb14a3feSDimitry Andric  friend bool regex_search(const basic_string<_Cp, _ST, _SA>& __s,
26330b57cec5SDimitry Andric                           match_results<typename basic_string<_Cp, _ST, _SA>::const_iterator, _Ap>&,
26340b57cec5SDimitry Andric                           const basic_regex<_Cp, _Tp>& __e,
26350b57cec5SDimitry Andric                           regex_constants::match_flag_type __flags);
26360b57cec5SDimitry Andric
26370b57cec5SDimitry Andric  template <class _Iter, class _Ap, class _Cp, class _Tp>
2638cb14a3feSDimitry Andric  friend bool
26390b57cec5SDimitry Andric  regex_search(__wrap_iter<_Iter> __first,
26400b57cec5SDimitry Andric               __wrap_iter<_Iter> __last,
26410b57cec5SDimitry Andric               match_results<__wrap_iter<_Iter>, _Ap>& __m,
26420b57cec5SDimitry Andric               const basic_regex<_Cp, _Tp>& __e,
26430b57cec5SDimitry Andric               regex_constants::match_flag_type __flags);
26440b57cec5SDimitry Andric
2645cb14a3feSDimitry Andric  template <class, class>
2646cb14a3feSDimitry Andric  friend class __lookahead;
26470b57cec5SDimitry Andric};
26480b57cec5SDimitry Andric
2649349cc55cSDimitry Andric#if _LIBCPP_STD_VER >= 17
2650cb14a3feSDimitry Andrictemplate <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> = 0>
2651cb14a3feSDimitry Andricbasic_regex(_ForwardIterator, _ForwardIterator, regex_constants::syntax_option_type = regex_constants::ECMAScript)
26520b57cec5SDimitry Andric    -> basic_regex<typename iterator_traits<_ForwardIterator>::value_type>;
26530b57cec5SDimitry Andric#endif
26540b57cec5SDimitry Andric
26550b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
26560b57cec5SDimitry Andricconst regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::icase;
26570b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
26580b57cec5SDimitry Andricconst regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::nosubs;
26590b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
26600b57cec5SDimitry Andricconst regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::optimize;
26610b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
26620b57cec5SDimitry Andricconst regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::collate;
26630b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
26640b57cec5SDimitry Andricconst regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::ECMAScript;
26650b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
26660b57cec5SDimitry Andricconst regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::basic;
26670b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
26680b57cec5SDimitry Andricconst regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::extended;
26690b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
26700b57cec5SDimitry Andricconst regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::awk;
26710b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
26720b57cec5SDimitry Andricconst regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::grep;
26730b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
26740b57cec5SDimitry Andricconst regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::egrep;
26750b57cec5SDimitry Andric
26760b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
2677cb14a3feSDimitry Andricvoid basic_regex<_CharT, _Traits>::swap(basic_regex& __r) {
26785f757f3fSDimitry Andric  using std::swap;
26790b57cec5SDimitry Andric  swap(__traits_, __r.__traits_);
26800b57cec5SDimitry Andric  swap(__flags_, __r.__flags_);
26810b57cec5SDimitry Andric  swap(__marked_count_, __r.__marked_count_);
26820b57cec5SDimitry Andric  swap(__loop_count_, __r.__loop_count_);
26830b57cec5SDimitry Andric  swap(__open_count_, __r.__open_count_);
26840b57cec5SDimitry Andric  swap(__start_, __r.__start_);
26850b57cec5SDimitry Andric  swap(__end_, __r.__end_);
26860b57cec5SDimitry Andric}
26870b57cec5SDimitry Andric
26880b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
2689cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI void swap(basic_regex<_CharT, _Traits>& __x, basic_regex<_CharT, _Traits>& __y) {
26900b57cec5SDimitry Andric  return __x.swap(__y);
26910b57cec5SDimitry Andric}
26920b57cec5SDimitry Andric
26930b57cec5SDimitry Andric// __lookahead
26940b57cec5SDimitry Andric
26950b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
2696cb14a3feSDimitry Andricclass __lookahead : public __owns_one_state<_CharT> {
26970b57cec5SDimitry Andric  typedef __owns_one_state<_CharT> base;
26980b57cec5SDimitry Andric
26990b57cec5SDimitry Andric  basic_regex<_CharT, _Traits> __exp_;
27000b57cec5SDimitry Andric  unsigned __mexp_;
27010b57cec5SDimitry Andric  bool __invert_;
27020b57cec5SDimitry Andric
27030b57cec5SDimitry Andric  __lookahead(const __lookahead&);
27040b57cec5SDimitry Andric  __lookahead& operator=(const __lookahead&);
2705cb14a3feSDimitry Andric
27060b57cec5SDimitry Andricpublic:
27075f757f3fSDimitry Andric  typedef std::__state<_CharT> __state;
27080b57cec5SDimitry Andric
27095f757f3fSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
27100b57cec5SDimitry Andric  __lookahead(const basic_regex<_CharT, _Traits>& __exp, bool __invert, __node<_CharT>* __s, unsigned __mexp)
27110b57cec5SDimitry Andric      : base(__s), __exp_(__exp), __mexp_(__mexp), __invert_(__invert) {}
27120b57cec5SDimitry Andric
271306c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
27140b57cec5SDimitry Andric};
27150b57cec5SDimitry Andric
27160b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
2717cb14a3feSDimitry Andricvoid __lookahead<_CharT, _Traits>::__exec(__state& __s) const {
27180b57cec5SDimitry Andric  match_results<const _CharT*> __m;
27190b57cec5SDimitry Andric  __m.__init(1 + __exp_.mark_count(), __s.__current_, __s.__last_);
27200b57cec5SDimitry Andric  bool __matched = __exp_.__match_at_start_ecma(
2721cb14a3feSDimitry Andric      __s.__current_,
2722cb14a3feSDimitry Andric      __s.__last_,
27230b57cec5SDimitry Andric      __m,
2724cb14a3feSDimitry Andric      (__s.__flags_ | regex_constants::match_continuous) & ~regex_constants::__full_match,
27250b57cec5SDimitry Andric      __s.__at_first_ && __s.__current_ == __s.__first_);
2726cb14a3feSDimitry Andric  if (__matched != __invert_) {
27270b57cec5SDimitry Andric    __s.__do_   = __state::__accept_but_not_consume;
27280b57cec5SDimitry Andric    __s.__node_ = this->first();
27290b57cec5SDimitry Andric    for (unsigned __i = 1; __i < __m.size(); ++__i) {
27300b57cec5SDimitry Andric      __s.__sub_matches_[__mexp_ + __i - 1] = __m.__matches_[__i];
27310b57cec5SDimitry Andric    }
2732cb14a3feSDimitry Andric  } else {
27330b57cec5SDimitry Andric    __s.__do_   = __state::__reject;
27340b57cec5SDimitry Andric    __s.__node_ = nullptr;
27350b57cec5SDimitry Andric  }
27360b57cec5SDimitry Andric}
27370b57cec5SDimitry Andric
27380b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
27390b57cec5SDimitry Andrictemplate <class _ForwardIterator>
2740cb14a3feSDimitry Andricvoid basic_regex<_CharT, _Traits>::__init(_ForwardIterator __first, _ForwardIterator __last) {
2741cb14a3feSDimitry Andric  if (__get_grammar(__flags_) == 0)
2742cb14a3feSDimitry Andric    __flags_ |= regex_constants::ECMAScript;
2743480093f4SDimitry Andric  _ForwardIterator __temp = __parse(__first, __last);
2744480093f4SDimitry Andric  if (__temp != __last)
2745480093f4SDimitry Andric    __throw_regex_error<regex_constants::__re_err_parse>();
2746480093f4SDimitry Andric}
2747480093f4SDimitry Andric
2748480093f4SDimitry Andrictemplate <class _CharT, class _Traits>
2749480093f4SDimitry Andrictemplate <class _ForwardIterator>
2750cb14a3feSDimitry Andric_ForwardIterator basic_regex<_CharT, _Traits>::__parse(_ForwardIterator __first, _ForwardIterator __last) {
27510b57cec5SDimitry Andric  {
27520b57cec5SDimitry Andric    unique_ptr<__node> __h(new __end_state<_CharT>);
27530b57cec5SDimitry Andric    __start_.reset(new __empty_state<_CharT>(__h.get()));
27540b57cec5SDimitry Andric    __h.release();
27550b57cec5SDimitry Andric    __end_ = __start_.get();
27560b57cec5SDimitry Andric  }
2757cb14a3feSDimitry Andric  switch (__get_grammar(__flags_)) {
27580b57cec5SDimitry Andric  case ECMAScript:
27590b57cec5SDimitry Andric    __first = __parse_ecma_exp(__first, __last);
27600b57cec5SDimitry Andric    break;
27610b57cec5SDimitry Andric  case basic:
27620b57cec5SDimitry Andric    __first = __parse_basic_reg_exp(__first, __last);
27630b57cec5SDimitry Andric    break;
27640b57cec5SDimitry Andric  case extended:
27650b57cec5SDimitry Andric  case awk:
27660b57cec5SDimitry Andric    __first = __parse_extended_reg_exp(__first, __last);
27670b57cec5SDimitry Andric    break;
27680b57cec5SDimitry Andric  case grep:
27690b57cec5SDimitry Andric    __first = __parse_grep(__first, __last);
27700b57cec5SDimitry Andric    break;
27710b57cec5SDimitry Andric  case egrep:
27720b57cec5SDimitry Andric    __first = __parse_egrep(__first, __last);
27730b57cec5SDimitry Andric    break;
27740b57cec5SDimitry Andric  default:
27750b57cec5SDimitry Andric    __throw_regex_error<regex_constants::__re_err_grammar>();
27760b57cec5SDimitry Andric  }
27770b57cec5SDimitry Andric  return __first;
27780b57cec5SDimitry Andric}
27790b57cec5SDimitry Andric
27800b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
27810b57cec5SDimitry Andrictemplate <class _ForwardIterator>
27820b57cec5SDimitry Andric_ForwardIterator
2783cb14a3feSDimitry Andricbasic_regex<_CharT, _Traits>::__parse_basic_reg_exp(_ForwardIterator __first, _ForwardIterator __last) {
2784cb14a3feSDimitry Andric  if (__first != __last) {
2785cb14a3feSDimitry Andric    if (*__first == '^') {
27860b57cec5SDimitry Andric      __push_l_anchor();
27870b57cec5SDimitry Andric      ++__first;
27880b57cec5SDimitry Andric    }
2789cb14a3feSDimitry Andric    if (__first != __last) {
27900b57cec5SDimitry Andric      __first = __parse_RE_expression(__first, __last);
2791cb14a3feSDimitry Andric      if (__first != __last) {
27925f757f3fSDimitry Andric        _ForwardIterator __temp = std::next(__first);
2793cb14a3feSDimitry Andric        if (__temp == __last && *__first == '$') {
27940b57cec5SDimitry Andric          __push_r_anchor();
27950b57cec5SDimitry Andric          ++__first;
27960b57cec5SDimitry Andric        }
27970b57cec5SDimitry Andric      }
27980b57cec5SDimitry Andric    }
27990b57cec5SDimitry Andric    if (__first != __last)
28000b57cec5SDimitry Andric      __throw_regex_error<regex_constants::__re_err_empty>();
28010b57cec5SDimitry Andric  }
28020b57cec5SDimitry Andric  return __first;
28030b57cec5SDimitry Andric}
28040b57cec5SDimitry Andric
28050b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
28060b57cec5SDimitry Andrictemplate <class _ForwardIterator>
28070b57cec5SDimitry Andric_ForwardIterator
2808cb14a3feSDimitry Andricbasic_regex<_CharT, _Traits>::__parse_extended_reg_exp(_ForwardIterator __first, _ForwardIterator __last) {
28090b57cec5SDimitry Andric  __owns_one_state<_CharT>* __sa = __end_;
28100b57cec5SDimitry Andric  _ForwardIterator __temp        = __parse_ERE_branch(__first, __last);
28110b57cec5SDimitry Andric  if (__temp == __first)
28120b57cec5SDimitry Andric    __throw_regex_error<regex_constants::__re_err_empty>();
28130b57cec5SDimitry Andric  __first = __temp;
2814cb14a3feSDimitry Andric  while (__first != __last && *__first == '|') {
28150b57cec5SDimitry Andric    __owns_one_state<_CharT>* __sb = __end_;
28160b57cec5SDimitry Andric    __temp                         = __parse_ERE_branch(++__first, __last);
28170b57cec5SDimitry Andric    if (__temp == __first)
28180b57cec5SDimitry Andric      __throw_regex_error<regex_constants::__re_err_empty>();
28190b57cec5SDimitry Andric    __push_alternation(__sa, __sb);
28200b57cec5SDimitry Andric    __first = __temp;
28210b57cec5SDimitry Andric  }
28220b57cec5SDimitry Andric  return __first;
28230b57cec5SDimitry Andric}
28240b57cec5SDimitry Andric
28250b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
28260b57cec5SDimitry Andrictemplate <class _ForwardIterator>
2827cb14a3feSDimitry Andric_ForwardIterator basic_regex<_CharT, _Traits>::__parse_ERE_branch(_ForwardIterator __first, _ForwardIterator __last) {
28280b57cec5SDimitry Andric  _ForwardIterator __temp = __parse_ERE_expression(__first, __last);
28290b57cec5SDimitry Andric  if (__temp == __first)
28300b57cec5SDimitry Andric    __throw_regex_error<regex_constants::__re_err_empty>();
2831cb14a3feSDimitry Andric  do {
28320b57cec5SDimitry Andric    __first = __temp;
28330b57cec5SDimitry Andric    __temp  = __parse_ERE_expression(__first, __last);
28340b57cec5SDimitry Andric  } while (__temp != __first);
28350b57cec5SDimitry Andric  return __first;
28360b57cec5SDimitry Andric}
28370b57cec5SDimitry Andric
28380b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
28390b57cec5SDimitry Andrictemplate <class _ForwardIterator>
28400b57cec5SDimitry Andric_ForwardIterator
2841cb14a3feSDimitry Andricbasic_regex<_CharT, _Traits>::__parse_ERE_expression(_ForwardIterator __first, _ForwardIterator __last) {
28420b57cec5SDimitry Andric  __owns_one_state<_CharT>* __e = __end_;
28430b57cec5SDimitry Andric  unsigned __mexp_begin         = __marked_count_;
28440b57cec5SDimitry Andric  _ForwardIterator __temp       = __parse_one_char_or_coll_elem_ERE(__first, __last);
2845cb14a3feSDimitry Andric  if (__temp == __first && __temp != __last) {
2846cb14a3feSDimitry Andric    switch (*__temp) {
28470b57cec5SDimitry Andric    case '^':
28480b57cec5SDimitry Andric      __push_l_anchor();
28490b57cec5SDimitry Andric      ++__temp;
28500b57cec5SDimitry Andric      break;
28510b57cec5SDimitry Andric    case '$':
28520b57cec5SDimitry Andric      __push_r_anchor();
28530b57cec5SDimitry Andric      ++__temp;
28540b57cec5SDimitry Andric      break;
28550b57cec5SDimitry Andric    case '(':
28560b57cec5SDimitry Andric      __push_begin_marked_subexpression();
28570b57cec5SDimitry Andric      unsigned __temp_count = __marked_count_;
28580b57cec5SDimitry Andric      ++__open_count_;
28590b57cec5SDimitry Andric      __temp = __parse_extended_reg_exp(++__temp, __last);
28600b57cec5SDimitry Andric      if (__temp == __last || *__temp != ')')
28610b57cec5SDimitry Andric        __throw_regex_error<regex_constants::error_paren>();
28620b57cec5SDimitry Andric      __push_end_marked_subexpression(__temp_count);
28630b57cec5SDimitry Andric      --__open_count_;
28640b57cec5SDimitry Andric      ++__temp;
28650b57cec5SDimitry Andric      break;
28660b57cec5SDimitry Andric    }
28670b57cec5SDimitry Andric  }
28680b57cec5SDimitry Andric  if (__temp != __first)
2869cb14a3feSDimitry Andric    __temp = __parse_ERE_dupl_symbol(__temp, __last, __e, __mexp_begin + 1, __marked_count_ + 1);
28700b57cec5SDimitry Andric  __first = __temp;
28710b57cec5SDimitry Andric  return __first;
28720b57cec5SDimitry Andric}
28730b57cec5SDimitry Andric
28740b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
28750b57cec5SDimitry Andrictemplate <class _ForwardIterator>
28760b57cec5SDimitry Andric_ForwardIterator
2877cb14a3feSDimitry Andricbasic_regex<_CharT, _Traits>::__parse_RE_expression(_ForwardIterator __first, _ForwardIterator __last) {
2878cb14a3feSDimitry Andric  while (true) {
28790b57cec5SDimitry Andric    _ForwardIterator __temp = __parse_simple_RE(__first, __last);
28800b57cec5SDimitry Andric    if (__temp == __first)
28810b57cec5SDimitry Andric      break;
28820b57cec5SDimitry Andric    __first = __temp;
28830b57cec5SDimitry Andric  }
28840b57cec5SDimitry Andric  return __first;
28850b57cec5SDimitry Andric}
28860b57cec5SDimitry Andric
28870b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
28880b57cec5SDimitry Andrictemplate <class _ForwardIterator>
2889cb14a3feSDimitry Andric_ForwardIterator basic_regex<_CharT, _Traits>::__parse_simple_RE(_ForwardIterator __first, _ForwardIterator __last) {
2890cb14a3feSDimitry Andric  if (__first != __last) {
28910b57cec5SDimitry Andric    __owns_one_state<_CharT>* __e = __end_;
28920b57cec5SDimitry Andric    unsigned __mexp_begin         = __marked_count_;
28930b57cec5SDimitry Andric    _ForwardIterator __temp       = __parse_nondupl_RE(__first, __last);
28940b57cec5SDimitry Andric    if (__temp != __first)
2895cb14a3feSDimitry Andric      __first = __parse_RE_dupl_symbol(__temp, __last, __e, __mexp_begin + 1, __marked_count_ + 1);
28960b57cec5SDimitry Andric  }
28970b57cec5SDimitry Andric  return __first;
28980b57cec5SDimitry Andric}
28990b57cec5SDimitry Andric
29000b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
29010b57cec5SDimitry Andrictemplate <class _ForwardIterator>
2902cb14a3feSDimitry Andric_ForwardIterator basic_regex<_CharT, _Traits>::__parse_nondupl_RE(_ForwardIterator __first, _ForwardIterator __last) {
29030b57cec5SDimitry Andric  _ForwardIterator __temp = __first;
29040b57cec5SDimitry Andric  __first                 = __parse_one_char_or_coll_elem_RE(__first, __last);
2905cb14a3feSDimitry Andric  if (__temp == __first) {
29060b57cec5SDimitry Andric    __temp = __parse_Back_open_paren(__first, __last);
2907cb14a3feSDimitry Andric    if (__temp != __first) {
29080b57cec5SDimitry Andric      __push_begin_marked_subexpression();
29090b57cec5SDimitry Andric      unsigned __temp_count = __marked_count_;
29100b57cec5SDimitry Andric      __first               = __parse_RE_expression(__temp, __last);
29110b57cec5SDimitry Andric      __temp                = __parse_Back_close_paren(__first, __last);
29120b57cec5SDimitry Andric      if (__temp == __first)
29130b57cec5SDimitry Andric        __throw_regex_error<regex_constants::error_paren>();
29140b57cec5SDimitry Andric      __push_end_marked_subexpression(__temp_count);
29150b57cec5SDimitry Andric      __first = __temp;
2916cb14a3feSDimitry Andric    } else
29170b57cec5SDimitry Andric      __first = __parse_BACKREF(__first, __last);
29180b57cec5SDimitry Andric  }
29190b57cec5SDimitry Andric  return __first;
29200b57cec5SDimitry Andric}
29210b57cec5SDimitry Andric
29220b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
29230b57cec5SDimitry Andrictemplate <class _ForwardIterator>
29240b57cec5SDimitry Andric_ForwardIterator
2925cb14a3feSDimitry Andricbasic_regex<_CharT, _Traits>::__parse_one_char_or_coll_elem_RE(_ForwardIterator __first, _ForwardIterator __last) {
29260b57cec5SDimitry Andric  _ForwardIterator __temp = __parse_ORD_CHAR(__first, __last);
2927cb14a3feSDimitry Andric  if (__temp == __first) {
29280b57cec5SDimitry Andric    __temp = __parse_QUOTED_CHAR(__first, __last);
2929cb14a3feSDimitry Andric    if (__temp == __first) {
2930cb14a3feSDimitry Andric      if (__temp != __last && *__temp == '.') {
29310b57cec5SDimitry Andric        __push_match_any();
29320b57cec5SDimitry Andric        ++__temp;
2933cb14a3feSDimitry Andric      } else
29340b57cec5SDimitry Andric        __temp = __parse_bracket_expression(__first, __last);
29350b57cec5SDimitry Andric    }
29360b57cec5SDimitry Andric  }
29370b57cec5SDimitry Andric  __first = __temp;
29380b57cec5SDimitry Andric  return __first;
29390b57cec5SDimitry Andric}
29400b57cec5SDimitry Andric
29410b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
29420b57cec5SDimitry Andrictemplate <class _ForwardIterator>
29430b57cec5SDimitry Andric_ForwardIterator
2944cb14a3feSDimitry Andricbasic_regex<_CharT, _Traits>::__parse_one_char_or_coll_elem_ERE(_ForwardIterator __first, _ForwardIterator __last) {
29450b57cec5SDimitry Andric  _ForwardIterator __temp = __parse_ORD_CHAR_ERE(__first, __last);
2946cb14a3feSDimitry Andric  if (__temp == __first) {
29470b57cec5SDimitry Andric    __temp = __parse_QUOTED_CHAR_ERE(__first, __last);
2948cb14a3feSDimitry Andric    if (__temp == __first) {
2949cb14a3feSDimitry Andric      if (__temp != __last && *__temp == '.') {
29500b57cec5SDimitry Andric        __push_match_any();
29510b57cec5SDimitry Andric        ++__temp;
2952cb14a3feSDimitry Andric      } else
29530b57cec5SDimitry Andric        __temp = __parse_bracket_expression(__first, __last);
29540b57cec5SDimitry Andric    }
29550b57cec5SDimitry Andric  }
29560b57cec5SDimitry Andric  __first = __temp;
29570b57cec5SDimitry Andric  return __first;
29580b57cec5SDimitry Andric}
29590b57cec5SDimitry Andric
29600b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
29610b57cec5SDimitry Andrictemplate <class _ForwardIterator>
29620b57cec5SDimitry Andric_ForwardIterator
2963cb14a3feSDimitry Andricbasic_regex<_CharT, _Traits>::__parse_Back_open_paren(_ForwardIterator __first, _ForwardIterator __last) {
2964cb14a3feSDimitry Andric  if (__first != __last) {
29655f757f3fSDimitry Andric    _ForwardIterator __temp = std::next(__first);
2966cb14a3feSDimitry Andric    if (__temp != __last) {
29670b57cec5SDimitry Andric      if (*__first == '\\' && *__temp == '(')
29680b57cec5SDimitry Andric        __first = ++__temp;
29690b57cec5SDimitry Andric    }
29700b57cec5SDimitry Andric  }
29710b57cec5SDimitry Andric  return __first;
29720b57cec5SDimitry Andric}
29730b57cec5SDimitry Andric
29740b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
29750b57cec5SDimitry Andrictemplate <class _ForwardIterator>
29760b57cec5SDimitry Andric_ForwardIterator
2977cb14a3feSDimitry Andricbasic_regex<_CharT, _Traits>::__parse_Back_close_paren(_ForwardIterator __first, _ForwardIterator __last) {
2978cb14a3feSDimitry Andric  if (__first != __last) {
29795f757f3fSDimitry Andric    _ForwardIterator __temp = std::next(__first);
2980cb14a3feSDimitry Andric    if (__temp != __last) {
29810b57cec5SDimitry Andric      if (*__first == '\\' && *__temp == ')')
29820b57cec5SDimitry Andric        __first = ++__temp;
29830b57cec5SDimitry Andric    }
29840b57cec5SDimitry Andric  }
29850b57cec5SDimitry Andric  return __first;
29860b57cec5SDimitry Andric}
29870b57cec5SDimitry Andric
29880b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
29890b57cec5SDimitry Andrictemplate <class _ForwardIterator>
29900b57cec5SDimitry Andric_ForwardIterator
2991cb14a3feSDimitry Andricbasic_regex<_CharT, _Traits>::__parse_Back_open_brace(_ForwardIterator __first, _ForwardIterator __last) {
2992cb14a3feSDimitry Andric  if (__first != __last) {
29935f757f3fSDimitry Andric    _ForwardIterator __temp = std::next(__first);
2994cb14a3feSDimitry Andric    if (__temp != __last) {
29950b57cec5SDimitry Andric      if (*__first == '\\' && *__temp == '{')
29960b57cec5SDimitry Andric        __first = ++__temp;
29970b57cec5SDimitry Andric    }
29980b57cec5SDimitry Andric  }
29990b57cec5SDimitry Andric  return __first;
30000b57cec5SDimitry Andric}
30010b57cec5SDimitry Andric
30020b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
30030b57cec5SDimitry Andrictemplate <class _ForwardIterator>
30040b57cec5SDimitry Andric_ForwardIterator
3005cb14a3feSDimitry Andricbasic_regex<_CharT, _Traits>::__parse_Back_close_brace(_ForwardIterator __first, _ForwardIterator __last) {
3006cb14a3feSDimitry Andric  if (__first != __last) {
30075f757f3fSDimitry Andric    _ForwardIterator __temp = std::next(__first);
3008cb14a3feSDimitry Andric    if (__temp != __last) {
30090b57cec5SDimitry Andric      if (*__first == '\\' && *__temp == '}')
30100b57cec5SDimitry Andric        __first = ++__temp;
30110b57cec5SDimitry Andric    }
30120b57cec5SDimitry Andric  }
30130b57cec5SDimitry Andric  return __first;
30140b57cec5SDimitry Andric}
30150b57cec5SDimitry Andric
30160b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
30170b57cec5SDimitry Andrictemplate <class _ForwardIterator>
3018cb14a3feSDimitry Andric_ForwardIterator basic_regex<_CharT, _Traits>::__parse_BACKREF(_ForwardIterator __first, _ForwardIterator __last) {
3019cb14a3feSDimitry Andric  if (__first != __last) {
30205f757f3fSDimitry Andric    _ForwardIterator __temp = std::next(__first);
30215ffd83dbSDimitry Andric    if (__temp != __last && *__first == '\\' && __test_back_ref(*__temp))
30220b57cec5SDimitry Andric      __first = ++__temp;
30230b57cec5SDimitry Andric  }
30240b57cec5SDimitry Andric  return __first;
30250b57cec5SDimitry Andric}
30260b57cec5SDimitry Andric
30270b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
30280b57cec5SDimitry Andrictemplate <class _ForwardIterator>
3029cb14a3feSDimitry Andric_ForwardIterator basic_regex<_CharT, _Traits>::__parse_ORD_CHAR(_ForwardIterator __first, _ForwardIterator __last) {
3030cb14a3feSDimitry Andric  if (__first != __last) {
30315f757f3fSDimitry Andric    _ForwardIterator __temp = std::next(__first);
30320b57cec5SDimitry Andric    if (__temp == __last && *__first == '$')
30330b57cec5SDimitry Andric      return __first;
30340b57cec5SDimitry Andric    // Not called inside a bracket
30350b57cec5SDimitry Andric    if (*__first == '.' || *__first == '\\' || *__first == '[')
30360b57cec5SDimitry Andric      return __first;
30370b57cec5SDimitry Andric    __push_char(*__first);
30380b57cec5SDimitry Andric    ++__first;
30390b57cec5SDimitry Andric  }
30400b57cec5SDimitry Andric  return __first;
30410b57cec5SDimitry Andric}
30420b57cec5SDimitry Andric
30430b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
30440b57cec5SDimitry Andrictemplate <class _ForwardIterator>
3045cb14a3feSDimitry Andric_ForwardIterator basic_regex<_CharT, _Traits>::__parse_ORD_CHAR_ERE(_ForwardIterator __first, _ForwardIterator __last) {
3046cb14a3feSDimitry Andric  if (__first != __last) {
3047cb14a3feSDimitry Andric    switch (*__first) {
30480b57cec5SDimitry Andric    case '^':
30490b57cec5SDimitry Andric    case '.':
30500b57cec5SDimitry Andric    case '[':
30510b57cec5SDimitry Andric    case '$':
30520b57cec5SDimitry Andric    case '(':
30530b57cec5SDimitry Andric    case '|':
30540b57cec5SDimitry Andric    case '*':
30550b57cec5SDimitry Andric    case '+':
30560b57cec5SDimitry Andric    case '?':
30570b57cec5SDimitry Andric    case '{':
30580b57cec5SDimitry Andric    case '\\':
30590b57cec5SDimitry Andric      break;
30600b57cec5SDimitry Andric    case ')':
3061cb14a3feSDimitry Andric      if (__open_count_ == 0) {
30620b57cec5SDimitry Andric        __push_char(*__first);
30630b57cec5SDimitry Andric        ++__first;
30640b57cec5SDimitry Andric      }
30650b57cec5SDimitry Andric      break;
30660b57cec5SDimitry Andric    default:
30670b57cec5SDimitry Andric      __push_char(*__first);
30680b57cec5SDimitry Andric      ++__first;
30690b57cec5SDimitry Andric      break;
30700b57cec5SDimitry Andric    }
30710b57cec5SDimitry Andric  }
30720b57cec5SDimitry Andric  return __first;
30730b57cec5SDimitry Andric}
30740b57cec5SDimitry Andric
30750b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
30760b57cec5SDimitry Andrictemplate <class _ForwardIterator>
3077cb14a3feSDimitry Andric_ForwardIterator basic_regex<_CharT, _Traits>::__parse_QUOTED_CHAR(_ForwardIterator __first, _ForwardIterator __last) {
3078cb14a3feSDimitry Andric  if (__first != __last) {
30795f757f3fSDimitry Andric    _ForwardIterator __temp = std::next(__first);
3080cb14a3feSDimitry Andric    if (__temp != __last) {
3081cb14a3feSDimitry Andric      if (*__first == '\\') {
3082cb14a3feSDimitry Andric        switch (*__temp) {
30830b57cec5SDimitry Andric        case '^':
30840b57cec5SDimitry Andric        case '.':
30850b57cec5SDimitry Andric        case '*':
30860b57cec5SDimitry Andric        case '[':
30870b57cec5SDimitry Andric        case '$':
30880b57cec5SDimitry Andric        case '\\':
30890b57cec5SDimitry Andric          __push_char(*__temp);
30900b57cec5SDimitry Andric          __first = ++__temp;
30910b57cec5SDimitry Andric          break;
30920b57cec5SDimitry Andric        }
30930b57cec5SDimitry Andric      }
30940b57cec5SDimitry Andric    }
30950b57cec5SDimitry Andric  }
30960b57cec5SDimitry Andric  return __first;
30970b57cec5SDimitry Andric}
30980b57cec5SDimitry Andric
30990b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
31000b57cec5SDimitry Andrictemplate <class _ForwardIterator>
31010b57cec5SDimitry Andric_ForwardIterator
3102cb14a3feSDimitry Andricbasic_regex<_CharT, _Traits>::__parse_QUOTED_CHAR_ERE(_ForwardIterator __first, _ForwardIterator __last) {
3103cb14a3feSDimitry Andric  if (__first != __last) {
31045f757f3fSDimitry Andric    _ForwardIterator __temp = std::next(__first);
3105cb14a3feSDimitry Andric    if (__temp != __last) {
3106cb14a3feSDimitry Andric      if (*__first == '\\') {
3107cb14a3feSDimitry Andric        switch (*__temp) {
31080b57cec5SDimitry Andric        case '^':
31090b57cec5SDimitry Andric        case '.':
31100b57cec5SDimitry Andric        case '*':
31110b57cec5SDimitry Andric        case '[':
31120b57cec5SDimitry Andric        case '$':
31130b57cec5SDimitry Andric        case '\\':
31140b57cec5SDimitry Andric        case '(':
31150b57cec5SDimitry Andric        case ')':
31160b57cec5SDimitry Andric        case '|':
31170b57cec5SDimitry Andric        case '+':
31180b57cec5SDimitry Andric        case '?':
31190b57cec5SDimitry Andric        case '{':
31200b57cec5SDimitry Andric        case '}':
31210b57cec5SDimitry Andric          __push_char(*__temp);
31220b57cec5SDimitry Andric          __first = ++__temp;
31230b57cec5SDimitry Andric          break;
31240b57cec5SDimitry Andric        default:
31250b57cec5SDimitry Andric          if (__get_grammar(__flags_) == awk)
31260b57cec5SDimitry Andric            __first = __parse_awk_escape(++__first, __last);
31275ffd83dbSDimitry Andric          else if (__test_back_ref(*__temp))
31285ffd83dbSDimitry Andric            __first = ++__temp;
31290b57cec5SDimitry Andric          break;
31300b57cec5SDimitry Andric        }
31310b57cec5SDimitry Andric      }
31320b57cec5SDimitry Andric    }
31330b57cec5SDimitry Andric  }
31340b57cec5SDimitry Andric  return __first;
31350b57cec5SDimitry Andric}
31360b57cec5SDimitry Andric
31370b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
31380b57cec5SDimitry Andrictemplate <class _ForwardIterator>
3139cb14a3feSDimitry Andric_ForwardIterator basic_regex<_CharT, _Traits>::__parse_RE_dupl_symbol(
3140cb14a3feSDimitry Andric    _ForwardIterator __first,
31410b57cec5SDimitry Andric    _ForwardIterator __last,
31420b57cec5SDimitry Andric    __owns_one_state<_CharT>* __s,
31430b57cec5SDimitry Andric    unsigned __mexp_begin,
3144cb14a3feSDimitry Andric    unsigned __mexp_end) {
3145cb14a3feSDimitry Andric  if (__first != __last) {
3146cb14a3feSDimitry Andric    if (*__first == '*') {
31470b57cec5SDimitry Andric      __push_greedy_inf_repeat(0, __s, __mexp_begin, __mexp_end);
31480b57cec5SDimitry Andric      ++__first;
3149cb14a3feSDimitry Andric    } else {
31500b57cec5SDimitry Andric      _ForwardIterator __temp = __parse_Back_open_brace(__first, __last);
3151cb14a3feSDimitry Andric      if (__temp != __first) {
31520b57cec5SDimitry Andric        int __min = 0;
31530b57cec5SDimitry Andric        __first   = __temp;
31540b57cec5SDimitry Andric        __temp    = __parse_DUP_COUNT(__first, __last, __min);
31550b57cec5SDimitry Andric        if (__temp == __first)
31560b57cec5SDimitry Andric          __throw_regex_error<regex_constants::error_badbrace>();
31570b57cec5SDimitry Andric        __first = __temp;
31580b57cec5SDimitry Andric        if (__first == __last)
31590b57cec5SDimitry Andric          __throw_regex_error<regex_constants::error_brace>();
3160cb14a3feSDimitry Andric        if (*__first != ',') {
31610b57cec5SDimitry Andric          __temp = __parse_Back_close_brace(__first, __last);
31620b57cec5SDimitry Andric          if (__temp == __first)
31630b57cec5SDimitry Andric            __throw_regex_error<regex_constants::error_brace>();
3164cb14a3feSDimitry Andric          __push_loop(__min, __min, __s, __mexp_begin, __mexp_end, true);
31650b57cec5SDimitry Andric          __first = __temp;
3166cb14a3feSDimitry Andric        } else {
31670b57cec5SDimitry Andric          ++__first; // consume ','
31680b57cec5SDimitry Andric          int __max = -1;
31690b57cec5SDimitry Andric          __first   = __parse_DUP_COUNT(__first, __last, __max);
31700b57cec5SDimitry Andric          __temp    = __parse_Back_close_brace(__first, __last);
31710b57cec5SDimitry Andric          if (__temp == __first)
31720b57cec5SDimitry Andric            __throw_regex_error<regex_constants::error_brace>();
31730b57cec5SDimitry Andric          if (__max == -1)
31740b57cec5SDimitry Andric            __push_greedy_inf_repeat(__min, __s, __mexp_begin, __mexp_end);
3175cb14a3feSDimitry Andric          else {
31760b57cec5SDimitry Andric            if (__max < __min)
31770b57cec5SDimitry Andric              __throw_regex_error<regex_constants::error_badbrace>();
3178cb14a3feSDimitry Andric            __push_loop(__min, __max, __s, __mexp_begin, __mexp_end, true);
31790b57cec5SDimitry Andric          }
31800b57cec5SDimitry Andric          __first = __temp;
31810b57cec5SDimitry Andric        }
31820b57cec5SDimitry Andric      }
31830b57cec5SDimitry Andric    }
31840b57cec5SDimitry Andric  }
31850b57cec5SDimitry Andric  return __first;
31860b57cec5SDimitry Andric}
31870b57cec5SDimitry Andric
31880b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
31890b57cec5SDimitry Andrictemplate <class _ForwardIterator>
3190cb14a3feSDimitry Andric_ForwardIterator basic_regex<_CharT, _Traits>::__parse_ERE_dupl_symbol(
3191cb14a3feSDimitry Andric    _ForwardIterator __first,
31920b57cec5SDimitry Andric    _ForwardIterator __last,
31930b57cec5SDimitry Andric    __owns_one_state<_CharT>* __s,
31940b57cec5SDimitry Andric    unsigned __mexp_begin,
3195cb14a3feSDimitry Andric    unsigned __mexp_end) {
3196cb14a3feSDimitry Andric  if (__first != __last) {
31970b57cec5SDimitry Andric    unsigned __grammar = __get_grammar(__flags_);
3198cb14a3feSDimitry Andric    switch (*__first) {
31990b57cec5SDimitry Andric    case '*':
32000b57cec5SDimitry Andric      ++__first;
3201cb14a3feSDimitry Andric      if (__grammar == ECMAScript && __first != __last && *__first == '?') {
32020b57cec5SDimitry Andric        ++__first;
32030b57cec5SDimitry Andric        __push_nongreedy_inf_repeat(0, __s, __mexp_begin, __mexp_end);
3204cb14a3feSDimitry Andric      } else
32050b57cec5SDimitry Andric        __push_greedy_inf_repeat(0, __s, __mexp_begin, __mexp_end);
32060b57cec5SDimitry Andric      break;
32070b57cec5SDimitry Andric    case '+':
32080b57cec5SDimitry Andric      ++__first;
3209cb14a3feSDimitry Andric      if (__grammar == ECMAScript && __first != __last && *__first == '?') {
32100b57cec5SDimitry Andric        ++__first;
32110b57cec5SDimitry Andric        __push_nongreedy_inf_repeat(1, __s, __mexp_begin, __mexp_end);
3212cb14a3feSDimitry Andric      } else
32130b57cec5SDimitry Andric        __push_greedy_inf_repeat(1, __s, __mexp_begin, __mexp_end);
32140b57cec5SDimitry Andric      break;
32150b57cec5SDimitry Andric    case '?':
32160b57cec5SDimitry Andric      ++__first;
3217cb14a3feSDimitry Andric      if (__grammar == ECMAScript && __first != __last && *__first == '?') {
32180b57cec5SDimitry Andric        ++__first;
32190b57cec5SDimitry Andric        __push_loop(0, 1, __s, __mexp_begin, __mexp_end, false);
3220cb14a3feSDimitry Andric      } else
32210b57cec5SDimitry Andric        __push_loop(0, 1, __s, __mexp_begin, __mexp_end);
32220b57cec5SDimitry Andric      break;
3223cb14a3feSDimitry Andric    case '{': {
32240b57cec5SDimitry Andric      int __min;
32250b57cec5SDimitry Andric      _ForwardIterator __temp = __parse_DUP_COUNT(++__first, __last, __min);
32260b57cec5SDimitry Andric      if (__temp == __first)
32270b57cec5SDimitry Andric        __throw_regex_error<regex_constants::error_badbrace>();
32280b57cec5SDimitry Andric      __first = __temp;
32290b57cec5SDimitry Andric      if (__first == __last)
32300b57cec5SDimitry Andric        __throw_regex_error<regex_constants::error_brace>();
3231cb14a3feSDimitry Andric      switch (*__first) {
32320b57cec5SDimitry Andric      case '}':
32330b57cec5SDimitry Andric        ++__first;
3234cb14a3feSDimitry Andric        if (__grammar == ECMAScript && __first != __last && *__first == '?') {
32350b57cec5SDimitry Andric          ++__first;
32360b57cec5SDimitry Andric          __push_loop(__min, __min, __s, __mexp_begin, __mexp_end, false);
3237cb14a3feSDimitry Andric        } else
32380b57cec5SDimitry Andric          __push_loop(__min, __min, __s, __mexp_begin, __mexp_end);
32390b57cec5SDimitry Andric        break;
32400b57cec5SDimitry Andric      case ',':
32410b57cec5SDimitry Andric        ++__first;
32420b57cec5SDimitry Andric        if (__first == __last)
32430b57cec5SDimitry Andric          __throw_regex_error<regex_constants::error_badbrace>();
3244cb14a3feSDimitry Andric        if (*__first == '}') {
32450b57cec5SDimitry Andric          ++__first;
3246cb14a3feSDimitry Andric          if (__grammar == ECMAScript && __first != __last && *__first == '?') {
32470b57cec5SDimitry Andric            ++__first;
32480b57cec5SDimitry Andric            __push_nongreedy_inf_repeat(__min, __s, __mexp_begin, __mexp_end);
3249cb14a3feSDimitry Andric          } else
32500b57cec5SDimitry Andric            __push_greedy_inf_repeat(__min, __s, __mexp_begin, __mexp_end);
3251cb14a3feSDimitry Andric        } else {
32520b57cec5SDimitry Andric          int __max = -1;
32530b57cec5SDimitry Andric          __temp    = __parse_DUP_COUNT(__first, __last, __max);
32540b57cec5SDimitry Andric          if (__temp == __first)
32550b57cec5SDimitry Andric            __throw_regex_error<regex_constants::error_brace>();
32560b57cec5SDimitry Andric          __first = __temp;
32570b57cec5SDimitry Andric          if (__first == __last || *__first != '}')
32580b57cec5SDimitry Andric            __throw_regex_error<regex_constants::error_brace>();
32590b57cec5SDimitry Andric          ++__first;
32600b57cec5SDimitry Andric          if (__max < __min)
32610b57cec5SDimitry Andric            __throw_regex_error<regex_constants::error_badbrace>();
3262cb14a3feSDimitry Andric          if (__grammar == ECMAScript && __first != __last && *__first == '?') {
32630b57cec5SDimitry Andric            ++__first;
32640b57cec5SDimitry Andric            __push_loop(__min, __max, __s, __mexp_begin, __mexp_end, false);
3265cb14a3feSDimitry Andric          } else
32660b57cec5SDimitry Andric            __push_loop(__min, __max, __s, __mexp_begin, __mexp_end);
32670b57cec5SDimitry Andric        }
32680b57cec5SDimitry Andric        break;
32690b57cec5SDimitry Andric      default:
32700b57cec5SDimitry Andric        __throw_regex_error<regex_constants::error_badbrace>();
32710b57cec5SDimitry Andric      }
3272cb14a3feSDimitry Andric    } break;
32730b57cec5SDimitry Andric    }
32740b57cec5SDimitry Andric  }
32750b57cec5SDimitry Andric  return __first;
32760b57cec5SDimitry Andric}
32770b57cec5SDimitry Andric
32780b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
32790b57cec5SDimitry Andrictemplate <class _ForwardIterator>
32800b57cec5SDimitry Andric_ForwardIterator
3281cb14a3feSDimitry Andricbasic_regex<_CharT, _Traits>::__parse_bracket_expression(_ForwardIterator __first, _ForwardIterator __last) {
3282cb14a3feSDimitry Andric  if (__first != __last && *__first == '[') {
32830b57cec5SDimitry Andric    ++__first;
32840b57cec5SDimitry Andric    if (__first == __last)
32850b57cec5SDimitry Andric      __throw_regex_error<regex_constants::error_brack>();
32860b57cec5SDimitry Andric    bool __negate = false;
3287cb14a3feSDimitry Andric    if (*__first == '^') {
32880b57cec5SDimitry Andric      ++__first;
32890b57cec5SDimitry Andric      __negate = true;
32900b57cec5SDimitry Andric    }
32910b57cec5SDimitry Andric    __bracket_expression<_CharT, _Traits>* __ml = __start_matching_list(__negate);
32920b57cec5SDimitry Andric    // __ml owned by *this
32930b57cec5SDimitry Andric    if (__first == __last)
32940b57cec5SDimitry Andric      __throw_regex_error<regex_constants::error_brack>();
3295cb14a3feSDimitry Andric    if (__get_grammar(__flags_) != ECMAScript && *__first == ']') {
32960b57cec5SDimitry Andric      __ml->__add_char(']');
32970b57cec5SDimitry Andric      ++__first;
32980b57cec5SDimitry Andric    }
32990b57cec5SDimitry Andric    __first = __parse_follow_list(__first, __last, __ml);
33000b57cec5SDimitry Andric    if (__first == __last)
33010b57cec5SDimitry Andric      __throw_regex_error<regex_constants::error_brack>();
3302cb14a3feSDimitry Andric    if (*__first == '-') {
33030b57cec5SDimitry Andric      __ml->__add_char('-');
33040b57cec5SDimitry Andric      ++__first;
33050b57cec5SDimitry Andric    }
33060b57cec5SDimitry Andric    if (__first == __last || *__first != ']')
33070b57cec5SDimitry Andric      __throw_regex_error<regex_constants::error_brack>();
33080b57cec5SDimitry Andric    ++__first;
33090b57cec5SDimitry Andric  }
33100b57cec5SDimitry Andric  return __first;
33110b57cec5SDimitry Andric}
33120b57cec5SDimitry Andric
33130b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
33140b57cec5SDimitry Andrictemplate <class _ForwardIterator>
3315cb14a3feSDimitry Andric_ForwardIterator basic_regex<_CharT, _Traits>::__parse_follow_list(
3316cb14a3feSDimitry Andric    _ForwardIterator __first, _ForwardIterator __last, __bracket_expression<_CharT, _Traits>* __ml) {
3317cb14a3feSDimitry Andric  if (__first != __last) {
3318cb14a3feSDimitry Andric    while (true) {
3319cb14a3feSDimitry Andric      _ForwardIterator __temp = __parse_expression_term(__first, __last, __ml);
33200b57cec5SDimitry Andric      if (__temp == __first)
33210b57cec5SDimitry Andric        break;
33220b57cec5SDimitry Andric      __first = __temp;
33230b57cec5SDimitry Andric    }
33240b57cec5SDimitry Andric  }
33250b57cec5SDimitry Andric  return __first;
33260b57cec5SDimitry Andric}
33270b57cec5SDimitry Andric
33280b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
33290b57cec5SDimitry Andrictemplate <class _ForwardIterator>
3330cb14a3feSDimitry Andric_ForwardIterator basic_regex<_CharT, _Traits>::__parse_expression_term(
3331cb14a3feSDimitry Andric    _ForwardIterator __first, _ForwardIterator __last, __bracket_expression<_CharT, _Traits>* __ml) {
3332cb14a3feSDimitry Andric  if (__first != __last && *__first != ']') {
33335f757f3fSDimitry Andric    _ForwardIterator __temp = std::next(__first);
33340b57cec5SDimitry Andric    basic_string<_CharT> __start_range;
3335cb14a3feSDimitry Andric    if (__temp != __last && *__first == '[') {
33360b57cec5SDimitry Andric      if (*__temp == '=')
33370b57cec5SDimitry Andric        return __parse_equivalence_class(++__temp, __last, __ml);
33380b57cec5SDimitry Andric      else if (*__temp == ':')
33390b57cec5SDimitry Andric        return __parse_character_class(++__temp, __last, __ml);
33400b57cec5SDimitry Andric      else if (*__temp == '.')
33410b57cec5SDimitry Andric        __first = __parse_collating_symbol(++__temp, __last, __start_range);
33420b57cec5SDimitry Andric    }
33430b57cec5SDimitry Andric    unsigned __grammar = __get_grammar(__flags_);
3344cb14a3feSDimitry Andric    if (__start_range.empty()) {
3345cb14a3feSDimitry Andric      if ((__grammar == ECMAScript || __grammar == awk) && *__first == '\\') {
33460b57cec5SDimitry Andric        if (__grammar == ECMAScript)
33470b57cec5SDimitry Andric          __first = __parse_class_escape(++__first, __last, __start_range, __ml);
33480b57cec5SDimitry Andric        else
33490b57cec5SDimitry Andric          __first = __parse_awk_escape(++__first, __last, &__start_range);
3350cb14a3feSDimitry Andric      } else {
33510b57cec5SDimitry Andric        __start_range = *__first;
33520b57cec5SDimitry Andric        ++__first;
33530b57cec5SDimitry Andric      }
33540b57cec5SDimitry Andric    }
3355cb14a3feSDimitry Andric    if (__first != __last && *__first != ']') {
33565f757f3fSDimitry Andric      __temp = std::next(__first);
3357cb14a3feSDimitry Andric      if (__temp != __last && *__first == '-' && *__temp != ']') {
33580b57cec5SDimitry Andric        // parse a range
33590b57cec5SDimitry Andric        basic_string<_CharT> __end_range;
33600b57cec5SDimitry Andric        __first = __temp;
33610b57cec5SDimitry Andric        ++__temp;
33620b57cec5SDimitry Andric        if (__temp != __last && *__first == '[' && *__temp == '.')
33630b57cec5SDimitry Andric          __first = __parse_collating_symbol(++__temp, __last, __end_range);
3364cb14a3feSDimitry Andric        else {
3365cb14a3feSDimitry Andric          if ((__grammar == ECMAScript || __grammar == awk) && *__first == '\\') {
33660b57cec5SDimitry Andric            if (__grammar == ECMAScript)
3367cb14a3feSDimitry Andric              __first = __parse_class_escape(++__first, __last, __end_range, __ml);
33680b57cec5SDimitry Andric            else
3369cb14a3feSDimitry Andric              __first = __parse_awk_escape(++__first, __last, &__end_range);
3370cb14a3feSDimitry Andric          } else {
33710b57cec5SDimitry Andric            __end_range = *__first;
33720b57cec5SDimitry Andric            ++__first;
33730b57cec5SDimitry Andric          }
33740b57cec5SDimitry Andric        }
33755f757f3fSDimitry Andric        __ml->__add_range(std::move(__start_range), std::move(__end_range));
3376cb14a3feSDimitry Andric      } else if (!__start_range.empty()) {
33770b57cec5SDimitry Andric        if (__start_range.size() == 1)
33780b57cec5SDimitry Andric          __ml->__add_char(__start_range[0]);
33790b57cec5SDimitry Andric        else
33800b57cec5SDimitry Andric          __ml->__add_digraph(__start_range[0], __start_range[1]);
33810b57cec5SDimitry Andric      }
3382cb14a3feSDimitry Andric    } else if (!__start_range.empty()) {
33830b57cec5SDimitry Andric      if (__start_range.size() == 1)
33840b57cec5SDimitry Andric        __ml->__add_char(__start_range[0]);
33850b57cec5SDimitry Andric      else
33860b57cec5SDimitry Andric        __ml->__add_digraph(__start_range[0], __start_range[1]);
33870b57cec5SDimitry Andric    }
33880b57cec5SDimitry Andric  }
33890b57cec5SDimitry Andric  return __first;
33900b57cec5SDimitry Andric}
33910b57cec5SDimitry Andric
33920b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
33930b57cec5SDimitry Andrictemplate <class _ForwardIterator>
3394cb14a3feSDimitry Andric_ForwardIterator basic_regex<_CharT, _Traits>::__parse_class_escape(
3395cb14a3feSDimitry Andric    _ForwardIterator __first,
33960b57cec5SDimitry Andric    _ForwardIterator __last,
33970b57cec5SDimitry Andric    basic_string<_CharT>& __str,
3398cb14a3feSDimitry Andric    __bracket_expression<_CharT, _Traits>* __ml) {
33990b57cec5SDimitry Andric  if (__first == __last)
34000b57cec5SDimitry Andric    __throw_regex_error<regex_constants::error_escape>();
3401cb14a3feSDimitry Andric  switch (*__first) {
34020b57cec5SDimitry Andric  case 0:
34030b57cec5SDimitry Andric    __str = *__first;
34040b57cec5SDimitry Andric    return ++__first;
34050b57cec5SDimitry Andric  case 'b':
34060b57cec5SDimitry Andric    __str = _CharT(8);
34070b57cec5SDimitry Andric    return ++__first;
34080b57cec5SDimitry Andric  case 'd':
34090b57cec5SDimitry Andric    __ml->__add_class(ctype_base::digit);
34100b57cec5SDimitry Andric    return ++__first;
34110b57cec5SDimitry Andric  case 'D':
34120b57cec5SDimitry Andric    __ml->__add_neg_class(ctype_base::digit);
34130b57cec5SDimitry Andric    return ++__first;
34140b57cec5SDimitry Andric  case 's':
34150b57cec5SDimitry Andric    __ml->__add_class(ctype_base::space);
34160b57cec5SDimitry Andric    return ++__first;
34170b57cec5SDimitry Andric  case 'S':
34180b57cec5SDimitry Andric    __ml->__add_neg_class(ctype_base::space);
34190b57cec5SDimitry Andric    return ++__first;
34200b57cec5SDimitry Andric  case 'w':
34210b57cec5SDimitry Andric    __ml->__add_class(ctype_base::alnum);
34220b57cec5SDimitry Andric    __ml->__add_char('_');
34230b57cec5SDimitry Andric    return ++__first;
34240b57cec5SDimitry Andric  case 'W':
34250b57cec5SDimitry Andric    __ml->__add_neg_class(ctype_base::alnum);
34260b57cec5SDimitry Andric    __ml->__add_neg_char('_');
34270b57cec5SDimitry Andric    return ++__first;
34280b57cec5SDimitry Andric  }
34290b57cec5SDimitry Andric  __first = __parse_character_escape(__first, __last, &__str);
34300b57cec5SDimitry Andric  return __first;
34310b57cec5SDimitry Andric}
34320b57cec5SDimitry Andric
34330b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
34340b57cec5SDimitry Andrictemplate <class _ForwardIterator>
3435cb14a3feSDimitry Andric_ForwardIterator basic_regex<_CharT, _Traits>::__parse_awk_escape(
3436cb14a3feSDimitry Andric    _ForwardIterator __first, _ForwardIterator __last, basic_string<_CharT>* __str) {
34370b57cec5SDimitry Andric  if (__first == __last)
34380b57cec5SDimitry Andric    __throw_regex_error<regex_constants::error_escape>();
3439cb14a3feSDimitry Andric  switch (*__first) {
34400b57cec5SDimitry Andric  case '\\':
34410b57cec5SDimitry Andric  case '"':
34420b57cec5SDimitry Andric  case '/':
34430b57cec5SDimitry Andric    if (__str)
34440b57cec5SDimitry Andric      *__str = *__first;
34450b57cec5SDimitry Andric    else
34460b57cec5SDimitry Andric      __push_char(*__first);
34470b57cec5SDimitry Andric    return ++__first;
34480b57cec5SDimitry Andric  case 'a':
34490b57cec5SDimitry Andric    if (__str)
34500b57cec5SDimitry Andric      *__str = _CharT(7);
34510b57cec5SDimitry Andric    else
34520b57cec5SDimitry Andric      __push_char(_CharT(7));
34530b57cec5SDimitry Andric    return ++__first;
34540b57cec5SDimitry Andric  case 'b':
34550b57cec5SDimitry Andric    if (__str)
34560b57cec5SDimitry Andric      *__str = _CharT(8);
34570b57cec5SDimitry Andric    else
34580b57cec5SDimitry Andric      __push_char(_CharT(8));
34590b57cec5SDimitry Andric    return ++__first;
34600b57cec5SDimitry Andric  case 'f':
34610b57cec5SDimitry Andric    if (__str)
34620b57cec5SDimitry Andric      *__str = _CharT(0xC);
34630b57cec5SDimitry Andric    else
34640b57cec5SDimitry Andric      __push_char(_CharT(0xC));
34650b57cec5SDimitry Andric    return ++__first;
34660b57cec5SDimitry Andric  case 'n':
34670b57cec5SDimitry Andric    if (__str)
34680b57cec5SDimitry Andric      *__str = _CharT(0xA);
34690b57cec5SDimitry Andric    else
34700b57cec5SDimitry Andric      __push_char(_CharT(0xA));
34710b57cec5SDimitry Andric    return ++__first;
34720b57cec5SDimitry Andric  case 'r':
34730b57cec5SDimitry Andric    if (__str)
34740b57cec5SDimitry Andric      *__str = _CharT(0xD);
34750b57cec5SDimitry Andric    else
34760b57cec5SDimitry Andric      __push_char(_CharT(0xD));
34770b57cec5SDimitry Andric    return ++__first;
34780b57cec5SDimitry Andric  case 't':
34790b57cec5SDimitry Andric    if (__str)
34800b57cec5SDimitry Andric      *__str = _CharT(0x9);
34810b57cec5SDimitry Andric    else
34820b57cec5SDimitry Andric      __push_char(_CharT(0x9));
34830b57cec5SDimitry Andric    return ++__first;
34840b57cec5SDimitry Andric  case 'v':
34850b57cec5SDimitry Andric    if (__str)
34860b57cec5SDimitry Andric      *__str = _CharT(0xB);
34870b57cec5SDimitry Andric    else
34880b57cec5SDimitry Andric      __push_char(_CharT(0xB));
34890b57cec5SDimitry Andric    return ++__first;
34900b57cec5SDimitry Andric  }
3491cb14a3feSDimitry Andric  if ('0' <= *__first && *__first <= '7') {
34920b57cec5SDimitry Andric    unsigned __val = *__first - '0';
3493cb14a3feSDimitry Andric    if (++__first != __last && ('0' <= *__first && *__first <= '7')) {
34940b57cec5SDimitry Andric      __val = 8 * __val + *__first - '0';
34950b57cec5SDimitry Andric      if (++__first != __last && ('0' <= *__first && *__first <= '7'))
34960b57cec5SDimitry Andric        __val = 8 * __val + *__first++ - '0';
34970b57cec5SDimitry Andric    }
34980b57cec5SDimitry Andric    if (__str)
34990b57cec5SDimitry Andric      *__str = _CharT(__val);
35000b57cec5SDimitry Andric    else
35010b57cec5SDimitry Andric      __push_char(_CharT(__val));
3502cb14a3feSDimitry Andric  } else
35030b57cec5SDimitry Andric    __throw_regex_error<regex_constants::error_escape>();
35040b57cec5SDimitry Andric  return __first;
35050b57cec5SDimitry Andric}
35060b57cec5SDimitry Andric
35070b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
35080b57cec5SDimitry Andrictemplate <class _ForwardIterator>
3509cb14a3feSDimitry Andric_ForwardIterator basic_regex<_CharT, _Traits>::__parse_equivalence_class(
3510cb14a3feSDimitry Andric    _ForwardIterator __first, _ForwardIterator __last, __bracket_expression<_CharT, _Traits>* __ml) {
35110b57cec5SDimitry Andric  // Found [=
35120b57cec5SDimitry Andric  //   This means =] must exist
351306c3fb27SDimitry Andric  value_type __equal_close[2] = {'=', ']'};
3514cb14a3feSDimitry Andric  _ForwardIterator __temp     = std::search(__first, __last, __equal_close, __equal_close + 2);
35150b57cec5SDimitry Andric  if (__temp == __last)
35160b57cec5SDimitry Andric    __throw_regex_error<regex_constants::error_brack>();
35170b57cec5SDimitry Andric  // [__first, __temp) contains all text in [= ... =]
3518cb14a3feSDimitry Andric  string_type __collate_name = __traits_.lookup_collatename(__first, __temp);
35190b57cec5SDimitry Andric  if (__collate_name.empty())
35200b57cec5SDimitry Andric    __throw_regex_error<regex_constants::error_collate>();
3521cb14a3feSDimitry Andric  string_type __equiv_name = __traits_.transform_primary(__collate_name.begin(), __collate_name.end());
35220b57cec5SDimitry Andric  if (!__equiv_name.empty())
35230b57cec5SDimitry Andric    __ml->__add_equivalence(__equiv_name);
3524cb14a3feSDimitry Andric  else {
3525cb14a3feSDimitry Andric    switch (__collate_name.size()) {
35260b57cec5SDimitry Andric    case 1:
35270b57cec5SDimitry Andric      __ml->__add_char(__collate_name[0]);
35280b57cec5SDimitry Andric      break;
35290b57cec5SDimitry Andric    case 2:
35300b57cec5SDimitry Andric      __ml->__add_digraph(__collate_name[0], __collate_name[1]);
35310b57cec5SDimitry Andric      break;
35320b57cec5SDimitry Andric    default:
35330b57cec5SDimitry Andric      __throw_regex_error<regex_constants::error_collate>();
35340b57cec5SDimitry Andric    }
35350b57cec5SDimitry Andric  }
35365f757f3fSDimitry Andric  __first = std::next(__temp, 2);
35370b57cec5SDimitry Andric  return __first;
35380b57cec5SDimitry Andric}
35390b57cec5SDimitry Andric
35400b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
35410b57cec5SDimitry Andrictemplate <class _ForwardIterator>
3542cb14a3feSDimitry Andric_ForwardIterator basic_regex<_CharT, _Traits>::__parse_character_class(
3543cb14a3feSDimitry Andric    _ForwardIterator __first, _ForwardIterator __last, __bracket_expression<_CharT, _Traits>* __ml) {
35440b57cec5SDimitry Andric  // Found [:
35450b57cec5SDimitry Andric  //   This means :] must exist
354606c3fb27SDimitry Andric  value_type __colon_close[2] = {':', ']'};
3547cb14a3feSDimitry Andric  _ForwardIterator __temp     = std::search(__first, __last, __colon_close, __colon_close + 2);
35480b57cec5SDimitry Andric  if (__temp == __last)
35490b57cec5SDimitry Andric    __throw_regex_error<regex_constants::error_brack>();
35500b57cec5SDimitry Andric  // [__first, __temp) contains all text in [: ... :]
35510b57cec5SDimitry Andric  typedef typename _Traits::char_class_type char_class_type;
3552cb14a3feSDimitry Andric  char_class_type __class_type = __traits_.lookup_classname(__first, __temp, __flags_ & icase);
35530b57cec5SDimitry Andric  if (__class_type == 0)
35540b57cec5SDimitry Andric    __throw_regex_error<regex_constants::error_ctype>();
35550b57cec5SDimitry Andric  __ml->__add_class(__class_type);
35565f757f3fSDimitry Andric  __first = std::next(__temp, 2);
35570b57cec5SDimitry Andric  return __first;
35580b57cec5SDimitry Andric}
35590b57cec5SDimitry Andric
35600b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
35610b57cec5SDimitry Andrictemplate <class _ForwardIterator>
3562cb14a3feSDimitry Andric_ForwardIterator basic_regex<_CharT, _Traits>::__parse_collating_symbol(
3563cb14a3feSDimitry Andric    _ForwardIterator __first, _ForwardIterator __last, basic_string<_CharT>& __col_sym) {
35640b57cec5SDimitry Andric  // Found [.
35650b57cec5SDimitry Andric  //   This means .] must exist
356606c3fb27SDimitry Andric  value_type __dot_close[2] = {'.', ']'};
3567cb14a3feSDimitry Andric  _ForwardIterator __temp   = std::search(__first, __last, __dot_close, __dot_close + 2);
35680b57cec5SDimitry Andric  if (__temp == __last)
35690b57cec5SDimitry Andric    __throw_regex_error<regex_constants::error_brack>();
35700b57cec5SDimitry Andric  // [__first, __temp) contains all text in [. ... .]
35710b57cec5SDimitry Andric  __col_sym = __traits_.lookup_collatename(__first, __temp);
3572cb14a3feSDimitry Andric  switch (__col_sym.size()) {
35730b57cec5SDimitry Andric  case 1:
35740b57cec5SDimitry Andric  case 2:
35750b57cec5SDimitry Andric    break;
35760b57cec5SDimitry Andric  default:
35770b57cec5SDimitry Andric    __throw_regex_error<regex_constants::error_collate>();
35780b57cec5SDimitry Andric  }
35795f757f3fSDimitry Andric  __first = std::next(__temp, 2);
35800b57cec5SDimitry Andric  return __first;
35810b57cec5SDimitry Andric}
35820b57cec5SDimitry Andric
35830b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
35840b57cec5SDimitry Andrictemplate <class _ForwardIterator>
35850b57cec5SDimitry Andric_ForwardIterator
3586cb14a3feSDimitry Andricbasic_regex<_CharT, _Traits>::__parse_DUP_COUNT(_ForwardIterator __first, _ForwardIterator __last, int& __c) {
3587cb14a3feSDimitry Andric  if (__first != __last) {
35880b57cec5SDimitry Andric    int __val = __traits_.value(*__first, 10);
3589cb14a3feSDimitry Andric    if (__val != -1) {
35900b57cec5SDimitry Andric      __c = __val;
3591cb14a3feSDimitry Andric      for (++__first; __first != __last && (__val = __traits_.value(*__first, 10)) != -1; ++__first) {
3592e8d8bef9SDimitry Andric        if (__c >= numeric_limits<int>::max() / 10)
35930b57cec5SDimitry Andric          __throw_regex_error<regex_constants::error_badbrace>();
35940b57cec5SDimitry Andric        __c *= 10;
35950b57cec5SDimitry Andric        __c += __val;
35960b57cec5SDimitry Andric      }
35970b57cec5SDimitry Andric    }
35980b57cec5SDimitry Andric  }
35990b57cec5SDimitry Andric  return __first;
36000b57cec5SDimitry Andric}
36010b57cec5SDimitry Andric
36020b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
36030b57cec5SDimitry Andrictemplate <class _ForwardIterator>
3604cb14a3feSDimitry Andric_ForwardIterator basic_regex<_CharT, _Traits>::__parse_ecma_exp(_ForwardIterator __first, _ForwardIterator __last) {
36050b57cec5SDimitry Andric  __owns_one_state<_CharT>* __sa = __end_;
36060b57cec5SDimitry Andric  _ForwardIterator __temp        = __parse_alternative(__first, __last);
36070b57cec5SDimitry Andric  if (__temp == __first)
36080b57cec5SDimitry Andric    __push_empty();
36090b57cec5SDimitry Andric  __first = __temp;
3610cb14a3feSDimitry Andric  while (__first != __last && *__first == '|') {
36110b57cec5SDimitry Andric    __owns_one_state<_CharT>* __sb = __end_;
36120b57cec5SDimitry Andric    __temp                         = __parse_alternative(++__first, __last);
36130b57cec5SDimitry Andric    if (__temp == __first)
36140b57cec5SDimitry Andric      __push_empty();
36150b57cec5SDimitry Andric    __push_alternation(__sa, __sb);
36160b57cec5SDimitry Andric    __first = __temp;
36170b57cec5SDimitry Andric  }
36180b57cec5SDimitry Andric  return __first;
36190b57cec5SDimitry Andric}
36200b57cec5SDimitry Andric
36210b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
36220b57cec5SDimitry Andrictemplate <class _ForwardIterator>
3623cb14a3feSDimitry Andric_ForwardIterator basic_regex<_CharT, _Traits>::__parse_alternative(_ForwardIterator __first, _ForwardIterator __last) {
3624cb14a3feSDimitry Andric  while (true) {
36250b57cec5SDimitry Andric    _ForwardIterator __temp = __parse_term(__first, __last);
36260b57cec5SDimitry Andric    if (__temp == __first)
36270b57cec5SDimitry Andric      break;
36280b57cec5SDimitry Andric    __first = __temp;
36290b57cec5SDimitry Andric  }
36300b57cec5SDimitry Andric  return __first;
36310b57cec5SDimitry Andric}
36320b57cec5SDimitry Andric
36330b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
36340b57cec5SDimitry Andrictemplate <class _ForwardIterator>
3635cb14a3feSDimitry Andric_ForwardIterator basic_regex<_CharT, _Traits>::__parse_term(_ForwardIterator __first, _ForwardIterator __last) {
36360b57cec5SDimitry Andric  _ForwardIterator __temp = __parse_assertion(__first, __last);
3637cb14a3feSDimitry Andric  if (__temp == __first) {
36380b57cec5SDimitry Andric    __owns_one_state<_CharT>* __e = __end_;
36390b57cec5SDimitry Andric    unsigned __mexp_begin         = __marked_count_;
36400b57cec5SDimitry Andric    __temp                        = __parse_atom(__first, __last);
36410b57cec5SDimitry Andric    if (__temp != __first)
3642cb14a3feSDimitry Andric      __first = __parse_ERE_dupl_symbol(__temp, __last, __e, __mexp_begin + 1, __marked_count_ + 1);
3643cb14a3feSDimitry Andric  } else
36440b57cec5SDimitry Andric    __first = __temp;
36450b57cec5SDimitry Andric  return __first;
36460b57cec5SDimitry Andric}
36470b57cec5SDimitry Andric
36480b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
36490b57cec5SDimitry Andrictemplate <class _ForwardIterator>
3650cb14a3feSDimitry Andric_ForwardIterator basic_regex<_CharT, _Traits>::__parse_assertion(_ForwardIterator __first, _ForwardIterator __last) {
3651cb14a3feSDimitry Andric  if (__first != __last) {
3652cb14a3feSDimitry Andric    switch (*__first) {
36530b57cec5SDimitry Andric    case '^':
36540b57cec5SDimitry Andric      __push_l_anchor();
36550b57cec5SDimitry Andric      ++__first;
36560b57cec5SDimitry Andric      break;
36570b57cec5SDimitry Andric    case '$':
36580b57cec5SDimitry Andric      __push_r_anchor();
36590b57cec5SDimitry Andric      ++__first;
36600b57cec5SDimitry Andric      break;
3661cb14a3feSDimitry Andric    case '\\': {
36625f757f3fSDimitry Andric      _ForwardIterator __temp = std::next(__first);
3663cb14a3feSDimitry Andric      if (__temp != __last) {
3664cb14a3feSDimitry Andric        if (*__temp == 'b') {
36650b57cec5SDimitry Andric          __push_word_boundary(false);
36660b57cec5SDimitry Andric          __first = ++__temp;
3667cb14a3feSDimitry Andric        } else if (*__temp == 'B') {
36680b57cec5SDimitry Andric          __push_word_boundary(true);
36690b57cec5SDimitry Andric          __first = ++__temp;
36700b57cec5SDimitry Andric        }
36710b57cec5SDimitry Andric      }
3672cb14a3feSDimitry Andric    } break;
3673cb14a3feSDimitry Andric    case '(': {
36745f757f3fSDimitry Andric      _ForwardIterator __temp = std::next(__first);
3675cb14a3feSDimitry Andric      if (__temp != __last && *__temp == '?') {
3676cb14a3feSDimitry Andric        if (++__temp != __last) {
3677cb14a3feSDimitry Andric          switch (*__temp) {
3678cb14a3feSDimitry Andric          case '=': {
36790b57cec5SDimitry Andric            basic_regex __exp;
36800b57cec5SDimitry Andric            __exp.__flags_  = __flags_;
36810b57cec5SDimitry Andric            __temp          = __exp.__parse(++__temp, __last);
36820b57cec5SDimitry Andric            unsigned __mexp = __exp.__marked_count_;
36835f757f3fSDimitry Andric            __push_lookahead(std::move(__exp), false, __marked_count_);
36840b57cec5SDimitry Andric            __marked_count_ += __mexp;
36850b57cec5SDimitry Andric            if (__temp == __last || *__temp != ')')
36860b57cec5SDimitry Andric              __throw_regex_error<regex_constants::error_paren>();
36870b57cec5SDimitry Andric            __first = ++__temp;
3688cb14a3feSDimitry Andric          } break;
3689cb14a3feSDimitry Andric          case '!': {
36900b57cec5SDimitry Andric            basic_regex __exp;
36910b57cec5SDimitry Andric            __exp.__flags_  = __flags_;
36920b57cec5SDimitry Andric            __temp          = __exp.__parse(++__temp, __last);
36930b57cec5SDimitry Andric            unsigned __mexp = __exp.__marked_count_;
36945f757f3fSDimitry Andric            __push_lookahead(std::move(__exp), true, __marked_count_);
36950b57cec5SDimitry Andric            __marked_count_ += __mexp;
36960b57cec5SDimitry Andric            if (__temp == __last || *__temp != ')')
36970b57cec5SDimitry Andric              __throw_regex_error<regex_constants::error_paren>();
36980b57cec5SDimitry Andric            __first = ++__temp;
3699cb14a3feSDimitry Andric          } break;
37000b57cec5SDimitry Andric          }
37010b57cec5SDimitry Andric        }
37020b57cec5SDimitry Andric      }
3703cb14a3feSDimitry Andric    } break;
37040b57cec5SDimitry Andric    }
37050b57cec5SDimitry Andric  }
37060b57cec5SDimitry Andric  return __first;
37070b57cec5SDimitry Andric}
37080b57cec5SDimitry Andric
37090b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
37100b57cec5SDimitry Andrictemplate <class _ForwardIterator>
3711cb14a3feSDimitry Andric_ForwardIterator basic_regex<_CharT, _Traits>::__parse_atom(_ForwardIterator __first, _ForwardIterator __last) {
3712cb14a3feSDimitry Andric  if (__first != __last) {
3713cb14a3feSDimitry Andric    switch (*__first) {
37140b57cec5SDimitry Andric    case '.':
37150b57cec5SDimitry Andric      __push_match_any_but_newline();
37160b57cec5SDimitry Andric      ++__first;
37170b57cec5SDimitry Andric      break;
37180b57cec5SDimitry Andric    case '\\':
37190b57cec5SDimitry Andric      __first = __parse_atom_escape(__first, __last);
37200b57cec5SDimitry Andric      break;
37210b57cec5SDimitry Andric    case '[':
37220b57cec5SDimitry Andric      __first = __parse_bracket_expression(__first, __last);
37230b57cec5SDimitry Andric      break;
3724cb14a3feSDimitry Andric    case '(': {
37250b57cec5SDimitry Andric      ++__first;
37260b57cec5SDimitry Andric      if (__first == __last)
37270b57cec5SDimitry Andric        __throw_regex_error<regex_constants::error_paren>();
37285f757f3fSDimitry Andric      _ForwardIterator __temp = std::next(__first);
3729cb14a3feSDimitry Andric      if (__temp != __last && *__first == '?' && *__temp == ':') {
37300b57cec5SDimitry Andric        ++__open_count_;
37310b57cec5SDimitry Andric        __first = __parse_ecma_exp(++__temp, __last);
37320b57cec5SDimitry Andric        if (__first == __last || *__first != ')')
37330b57cec5SDimitry Andric          __throw_regex_error<regex_constants::error_paren>();
37340b57cec5SDimitry Andric        --__open_count_;
37350b57cec5SDimitry Andric        ++__first;
3736cb14a3feSDimitry Andric      } else {
37370b57cec5SDimitry Andric        __push_begin_marked_subexpression();
37380b57cec5SDimitry Andric        unsigned __temp_count = __marked_count_;
37390b57cec5SDimitry Andric        ++__open_count_;
37400b57cec5SDimitry Andric        __first = __parse_ecma_exp(__first, __last);
37410b57cec5SDimitry Andric        if (__first == __last || *__first != ')')
37420b57cec5SDimitry Andric          __throw_regex_error<regex_constants::error_paren>();
37430b57cec5SDimitry Andric        __push_end_marked_subexpression(__temp_count);
37440b57cec5SDimitry Andric        --__open_count_;
37450b57cec5SDimitry Andric        ++__first;
37460b57cec5SDimitry Andric      }
3747cb14a3feSDimitry Andric    } break;
37480b57cec5SDimitry Andric    case '*':
37490b57cec5SDimitry Andric    case '+':
37500b57cec5SDimitry Andric    case '?':
37510b57cec5SDimitry Andric    case '{':
37520b57cec5SDimitry Andric      __throw_regex_error<regex_constants::error_badrepeat>();
37530b57cec5SDimitry Andric      break;
37540b57cec5SDimitry Andric    default:
37550b57cec5SDimitry Andric      __first = __parse_pattern_character(__first, __last);
37560b57cec5SDimitry Andric      break;
37570b57cec5SDimitry Andric    }
37580b57cec5SDimitry Andric  }
37590b57cec5SDimitry Andric  return __first;
37600b57cec5SDimitry Andric}
37610b57cec5SDimitry Andric
37620b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
37630b57cec5SDimitry Andrictemplate <class _ForwardIterator>
3764cb14a3feSDimitry Andric_ForwardIterator basic_regex<_CharT, _Traits>::__parse_atom_escape(_ForwardIterator __first, _ForwardIterator __last) {
3765cb14a3feSDimitry Andric  if (__first != __last && *__first == '\\') {
37665f757f3fSDimitry Andric    _ForwardIterator __t1 = std::next(__first);
37670b57cec5SDimitry Andric    if (__t1 == __last)
37680b57cec5SDimitry Andric      __throw_regex_error<regex_constants::error_escape>();
37690b57cec5SDimitry Andric
37700b57cec5SDimitry Andric    _ForwardIterator __t2 = __parse_decimal_escape(__t1, __last);
37710b57cec5SDimitry Andric    if (__t2 != __t1)
37720b57cec5SDimitry Andric      __first = __t2;
3773cb14a3feSDimitry Andric    else {
37740b57cec5SDimitry Andric      __t2 = __parse_character_class_escape(__t1, __last);
37750b57cec5SDimitry Andric      if (__t2 != __t1)
37760b57cec5SDimitry Andric        __first = __t2;
3777cb14a3feSDimitry Andric      else {
37780b57cec5SDimitry Andric        __t2 = __parse_character_escape(__t1, __last);
37790b57cec5SDimitry Andric        if (__t2 != __t1)
37800b57cec5SDimitry Andric          __first = __t2;
37810b57cec5SDimitry Andric      }
37820b57cec5SDimitry Andric    }
37830b57cec5SDimitry Andric  }
37840b57cec5SDimitry Andric  return __first;
37850b57cec5SDimitry Andric}
37860b57cec5SDimitry Andric
37870b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
37880b57cec5SDimitry Andrictemplate <class _ForwardIterator>
37890b57cec5SDimitry Andric_ForwardIterator
3790cb14a3feSDimitry Andricbasic_regex<_CharT, _Traits>::__parse_decimal_escape(_ForwardIterator __first, _ForwardIterator __last) {
3791cb14a3feSDimitry Andric  if (__first != __last) {
3792cb14a3feSDimitry Andric    if (*__first == '0') {
37930b57cec5SDimitry Andric      __push_char(_CharT());
37940b57cec5SDimitry Andric      ++__first;
3795cb14a3feSDimitry Andric    } else if ('1' <= *__first && *__first <= '9') {
37960b57cec5SDimitry Andric      unsigned __v = *__first - '0';
3797cb14a3feSDimitry Andric      for (++__first; __first != __last && '0' <= *__first && *__first <= '9'; ++__first) {
3798e8d8bef9SDimitry Andric        if (__v >= numeric_limits<unsigned>::max() / 10)
37990b57cec5SDimitry Andric          __throw_regex_error<regex_constants::error_backref>();
38000b57cec5SDimitry Andric        __v = 10 * __v + *__first - '0';
38010b57cec5SDimitry Andric      }
38020b57cec5SDimitry Andric      if (__v == 0 || __v > mark_count())
38030b57cec5SDimitry Andric        __throw_regex_error<regex_constants::error_backref>();
38040b57cec5SDimitry Andric      __push_back_ref(__v);
38050b57cec5SDimitry Andric    }
38060b57cec5SDimitry Andric  }
38070b57cec5SDimitry Andric  return __first;
38080b57cec5SDimitry Andric}
38090b57cec5SDimitry Andric
38100b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
38110b57cec5SDimitry Andrictemplate <class _ForwardIterator>
38120b57cec5SDimitry Andric_ForwardIterator
3813cb14a3feSDimitry Andricbasic_regex<_CharT, _Traits>::__parse_character_class_escape(_ForwardIterator __first, _ForwardIterator __last) {
3814cb14a3feSDimitry Andric  if (__first != __last) {
38150b57cec5SDimitry Andric    __bracket_expression<_CharT, _Traits>* __ml;
3816cb14a3feSDimitry Andric    switch (*__first) {
38170b57cec5SDimitry Andric    case 'd':
38180b57cec5SDimitry Andric      __ml = __start_matching_list(false);
38190b57cec5SDimitry Andric      __ml->__add_class(ctype_base::digit);
38200b57cec5SDimitry Andric      ++__first;
38210b57cec5SDimitry Andric      break;
38220b57cec5SDimitry Andric    case 'D':
38230b57cec5SDimitry Andric      __ml = __start_matching_list(true);
38240b57cec5SDimitry Andric      __ml->__add_class(ctype_base::digit);
38250b57cec5SDimitry Andric      ++__first;
38260b57cec5SDimitry Andric      break;
38270b57cec5SDimitry Andric    case 's':
38280b57cec5SDimitry Andric      __ml = __start_matching_list(false);
38290b57cec5SDimitry Andric      __ml->__add_class(ctype_base::space);
38300b57cec5SDimitry Andric      ++__first;
38310b57cec5SDimitry Andric      break;
38320b57cec5SDimitry Andric    case 'S':
38330b57cec5SDimitry Andric      __ml = __start_matching_list(true);
38340b57cec5SDimitry Andric      __ml->__add_class(ctype_base::space);
38350b57cec5SDimitry Andric      ++__first;
38360b57cec5SDimitry Andric      break;
38370b57cec5SDimitry Andric    case 'w':
38380b57cec5SDimitry Andric      __ml = __start_matching_list(false);
38390b57cec5SDimitry Andric      __ml->__add_class(ctype_base::alnum);
38400b57cec5SDimitry Andric      __ml->__add_char('_');
38410b57cec5SDimitry Andric      ++__first;
38420b57cec5SDimitry Andric      break;
38430b57cec5SDimitry Andric    case 'W':
38440b57cec5SDimitry Andric      __ml = __start_matching_list(true);
38450b57cec5SDimitry Andric      __ml->__add_class(ctype_base::alnum);
38460b57cec5SDimitry Andric      __ml->__add_char('_');
38470b57cec5SDimitry Andric      ++__first;
38480b57cec5SDimitry Andric      break;
38490b57cec5SDimitry Andric    }
38500b57cec5SDimitry Andric  }
38510b57cec5SDimitry Andric  return __first;
38520b57cec5SDimitry Andric}
38530b57cec5SDimitry Andric
38540b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
38550b57cec5SDimitry Andrictemplate <class _ForwardIterator>
3856cb14a3feSDimitry Andric_ForwardIterator basic_regex<_CharT, _Traits>::__parse_character_escape(
3857cb14a3feSDimitry Andric    _ForwardIterator __first, _ForwardIterator __last, basic_string<_CharT>* __str) {
3858cb14a3feSDimitry Andric  if (__first != __last) {
38590b57cec5SDimitry Andric    _ForwardIterator __t;
38600b57cec5SDimitry Andric    unsigned __sum = 0;
38610b57cec5SDimitry Andric    int __hd;
3862cb14a3feSDimitry Andric    switch (*__first) {
38630b57cec5SDimitry Andric    case 'f':
38640b57cec5SDimitry Andric      if (__str)
38650b57cec5SDimitry Andric        *__str = _CharT(0xC);
38660b57cec5SDimitry Andric      else
38670b57cec5SDimitry Andric        __push_char(_CharT(0xC));
38680b57cec5SDimitry Andric      ++__first;
38690b57cec5SDimitry Andric      break;
38700b57cec5SDimitry Andric    case 'n':
38710b57cec5SDimitry Andric      if (__str)
38720b57cec5SDimitry Andric        *__str = _CharT(0xA);
38730b57cec5SDimitry Andric      else
38740b57cec5SDimitry Andric        __push_char(_CharT(0xA));
38750b57cec5SDimitry Andric      ++__first;
38760b57cec5SDimitry Andric      break;
38770b57cec5SDimitry Andric    case 'r':
38780b57cec5SDimitry Andric      if (__str)
38790b57cec5SDimitry Andric        *__str = _CharT(0xD);
38800b57cec5SDimitry Andric      else
38810b57cec5SDimitry Andric        __push_char(_CharT(0xD));
38820b57cec5SDimitry Andric      ++__first;
38830b57cec5SDimitry Andric      break;
38840b57cec5SDimitry Andric    case 't':
38850b57cec5SDimitry Andric      if (__str)
38860b57cec5SDimitry Andric        *__str = _CharT(0x9);
38870b57cec5SDimitry Andric      else
38880b57cec5SDimitry Andric        __push_char(_CharT(0x9));
38890b57cec5SDimitry Andric      ++__first;
38900b57cec5SDimitry Andric      break;
38910b57cec5SDimitry Andric    case 'v':
38920b57cec5SDimitry Andric      if (__str)
38930b57cec5SDimitry Andric        *__str = _CharT(0xB);
38940b57cec5SDimitry Andric      else
38950b57cec5SDimitry Andric        __push_char(_CharT(0xB));
38960b57cec5SDimitry Andric      ++__first;
38970b57cec5SDimitry Andric      break;
38980b57cec5SDimitry Andric    case 'c':
3899cb14a3feSDimitry Andric      if ((__t = std::next(__first)) != __last) {
3900cb14a3feSDimitry Andric        if (('A' <= *__t && *__t <= 'Z') || ('a' <= *__t && *__t <= 'z')) {
39010b57cec5SDimitry Andric          if (__str)
39020b57cec5SDimitry Andric            *__str = _CharT(*__t % 32);
39030b57cec5SDimitry Andric          else
39040b57cec5SDimitry Andric            __push_char(_CharT(*__t % 32));
39050b57cec5SDimitry Andric          __first = ++__t;
3906cb14a3feSDimitry Andric        } else
39070b57cec5SDimitry Andric          __throw_regex_error<regex_constants::error_escape>();
3908cb14a3feSDimitry Andric      } else
39090b57cec5SDimitry Andric        __throw_regex_error<regex_constants::error_escape>();
39100b57cec5SDimitry Andric      break;
39110b57cec5SDimitry Andric    case 'u':
39120b57cec5SDimitry Andric      ++__first;
39130b57cec5SDimitry Andric      if (__first == __last)
39140b57cec5SDimitry Andric        __throw_regex_error<regex_constants::error_escape>();
39150b57cec5SDimitry Andric      __hd = __traits_.value(*__first, 16);
39160b57cec5SDimitry Andric      if (__hd == -1)
39170b57cec5SDimitry Andric        __throw_regex_error<regex_constants::error_escape>();
39180b57cec5SDimitry Andric      __sum = 16 * __sum + static_cast<unsigned>(__hd);
39190b57cec5SDimitry Andric      ++__first;
39200b57cec5SDimitry Andric      if (__first == __last)
39210b57cec5SDimitry Andric        __throw_regex_error<regex_constants::error_escape>();
39220b57cec5SDimitry Andric      __hd = __traits_.value(*__first, 16);
39230b57cec5SDimitry Andric      if (__hd == -1)
39240b57cec5SDimitry Andric        __throw_regex_error<regex_constants::error_escape>();
39250b57cec5SDimitry Andric      __sum = 16 * __sum + static_cast<unsigned>(__hd);
3926fe6060f1SDimitry Andric      // fallthrough
39270b57cec5SDimitry Andric    case 'x':
39280b57cec5SDimitry Andric      ++__first;
39290b57cec5SDimitry Andric      if (__first == __last)
39300b57cec5SDimitry Andric        __throw_regex_error<regex_constants::error_escape>();
39310b57cec5SDimitry Andric      __hd = __traits_.value(*__first, 16);
39320b57cec5SDimitry Andric      if (__hd == -1)
39330b57cec5SDimitry Andric        __throw_regex_error<regex_constants::error_escape>();
39340b57cec5SDimitry Andric      __sum = 16 * __sum + static_cast<unsigned>(__hd);
39350b57cec5SDimitry Andric      ++__first;
39360b57cec5SDimitry Andric      if (__first == __last)
39370b57cec5SDimitry Andric        __throw_regex_error<regex_constants::error_escape>();
39380b57cec5SDimitry Andric      __hd = __traits_.value(*__first, 16);
39390b57cec5SDimitry Andric      if (__hd == -1)
39400b57cec5SDimitry Andric        __throw_regex_error<regex_constants::error_escape>();
39410b57cec5SDimitry Andric      __sum = 16 * __sum + static_cast<unsigned>(__hd);
39420b57cec5SDimitry Andric      if (__str)
39430b57cec5SDimitry Andric        *__str = _CharT(__sum);
39440b57cec5SDimitry Andric      else
39450b57cec5SDimitry Andric        __push_char(_CharT(__sum));
39460b57cec5SDimitry Andric      ++__first;
39470b57cec5SDimitry Andric      break;
39480b57cec5SDimitry Andric    case '0':
39490b57cec5SDimitry Andric      if (__str)
39500b57cec5SDimitry Andric        *__str = _CharT(0);
39510b57cec5SDimitry Andric      else
39520b57cec5SDimitry Andric        __push_char(_CharT(0));
39530b57cec5SDimitry Andric      ++__first;
39540b57cec5SDimitry Andric      break;
39550b57cec5SDimitry Andric    default:
3956cb14a3feSDimitry Andric      if (*__first != '_' && !__traits_.isctype(*__first, ctype_base::alnum)) {
39570b57cec5SDimitry Andric        if (__str)
39580b57cec5SDimitry Andric          *__str = *__first;
39590b57cec5SDimitry Andric        else
39600b57cec5SDimitry Andric          __push_char(*__first);
39610b57cec5SDimitry Andric        ++__first;
3962cb14a3feSDimitry Andric      } else
39630b57cec5SDimitry Andric        __throw_regex_error<regex_constants::error_escape>();
39640b57cec5SDimitry Andric      break;
39650b57cec5SDimitry Andric    }
39660b57cec5SDimitry Andric  }
39670b57cec5SDimitry Andric  return __first;
39680b57cec5SDimitry Andric}
39690b57cec5SDimitry Andric
39700b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
39710b57cec5SDimitry Andrictemplate <class _ForwardIterator>
39720b57cec5SDimitry Andric_ForwardIterator
3973cb14a3feSDimitry Andricbasic_regex<_CharT, _Traits>::__parse_pattern_character(_ForwardIterator __first, _ForwardIterator __last) {
3974cb14a3feSDimitry Andric  if (__first != __last) {
3975cb14a3feSDimitry Andric    switch (*__first) {
39760b57cec5SDimitry Andric    case '^':
39770b57cec5SDimitry Andric    case '$':
39780b57cec5SDimitry Andric    case '\\':
39790b57cec5SDimitry Andric    case '.':
39800b57cec5SDimitry Andric    case '*':
39810b57cec5SDimitry Andric    case '+':
39820b57cec5SDimitry Andric    case '?':
39830b57cec5SDimitry Andric    case '(':
39840b57cec5SDimitry Andric    case ')':
39850b57cec5SDimitry Andric    case '[':
39860b57cec5SDimitry Andric    case ']':
39870b57cec5SDimitry Andric    case '{':
39880b57cec5SDimitry Andric    case '}':
39890b57cec5SDimitry Andric    case '|':
39900b57cec5SDimitry Andric      break;
39910b57cec5SDimitry Andric    default:
39920b57cec5SDimitry Andric      __push_char(*__first);
39930b57cec5SDimitry Andric      ++__first;
39940b57cec5SDimitry Andric      break;
39950b57cec5SDimitry Andric    }
39960b57cec5SDimitry Andric  }
39970b57cec5SDimitry Andric  return __first;
39980b57cec5SDimitry Andric}
39990b57cec5SDimitry Andric
40000b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
40010b57cec5SDimitry Andrictemplate <class _ForwardIterator>
4002cb14a3feSDimitry Andric_ForwardIterator basic_regex<_CharT, _Traits>::__parse_grep(_ForwardIterator __first, _ForwardIterator __last) {
40030b57cec5SDimitry Andric  __owns_one_state<_CharT>* __sa = __end_;
40045f757f3fSDimitry Andric  _ForwardIterator __t1          = std::find(__first, __last, _CharT('\n'));
40050b57cec5SDimitry Andric  if (__t1 != __first)
40060b57cec5SDimitry Andric    __parse_basic_reg_exp(__first, __t1);
40070b57cec5SDimitry Andric  else
40080b57cec5SDimitry Andric    __push_empty();
40090b57cec5SDimitry Andric  __first = __t1;
40100b57cec5SDimitry Andric  if (__first != __last)
40110b57cec5SDimitry Andric    ++__first;
4012cb14a3feSDimitry Andric  while (__first != __last) {
40135f757f3fSDimitry Andric    __t1                           = std::find(__first, __last, _CharT('\n'));
40140b57cec5SDimitry Andric    __owns_one_state<_CharT>* __sb = __end_;
40150b57cec5SDimitry Andric    if (__t1 != __first)
40160b57cec5SDimitry Andric      __parse_basic_reg_exp(__first, __t1);
40170b57cec5SDimitry Andric    else
40180b57cec5SDimitry Andric      __push_empty();
40190b57cec5SDimitry Andric    __push_alternation(__sa, __sb);
40200b57cec5SDimitry Andric    __first = __t1;
40210b57cec5SDimitry Andric    if (__first != __last)
40220b57cec5SDimitry Andric      ++__first;
40230b57cec5SDimitry Andric  }
40240b57cec5SDimitry Andric  return __first;
40250b57cec5SDimitry Andric}
40260b57cec5SDimitry Andric
40270b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
40280b57cec5SDimitry Andrictemplate <class _ForwardIterator>
4029cb14a3feSDimitry Andric_ForwardIterator basic_regex<_CharT, _Traits>::__parse_egrep(_ForwardIterator __first, _ForwardIterator __last) {
40300b57cec5SDimitry Andric  __owns_one_state<_CharT>* __sa = __end_;
40315f757f3fSDimitry Andric  _ForwardIterator __t1          = std::find(__first, __last, _CharT('\n'));
40320b57cec5SDimitry Andric  if (__t1 != __first)
40330b57cec5SDimitry Andric    __parse_extended_reg_exp(__first, __t1);
40340b57cec5SDimitry Andric  else
40350b57cec5SDimitry Andric    __push_empty();
40360b57cec5SDimitry Andric  __first = __t1;
40370b57cec5SDimitry Andric  if (__first != __last)
40380b57cec5SDimitry Andric    ++__first;
4039cb14a3feSDimitry Andric  while (__first != __last) {
40405f757f3fSDimitry Andric    __t1                           = std::find(__first, __last, _CharT('\n'));
40410b57cec5SDimitry Andric    __owns_one_state<_CharT>* __sb = __end_;
40420b57cec5SDimitry Andric    if (__t1 != __first)
40430b57cec5SDimitry Andric      __parse_extended_reg_exp(__first, __t1);
40440b57cec5SDimitry Andric    else
40450b57cec5SDimitry Andric      __push_empty();
40460b57cec5SDimitry Andric    __push_alternation(__sa, __sb);
40470b57cec5SDimitry Andric    __first = __t1;
40480b57cec5SDimitry Andric    if (__first != __last)
40490b57cec5SDimitry Andric      ++__first;
40500b57cec5SDimitry Andric  }
40510b57cec5SDimitry Andric  return __first;
40520b57cec5SDimitry Andric}
40530b57cec5SDimitry Andric
40540b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
4055cb14a3feSDimitry Andricbool basic_regex<_CharT, _Traits>::__test_back_ref(_CharT __c) {
4056753f127fSDimitry Andric  unsigned __val = __traits_.value(__c, 10);
4057cb14a3feSDimitry Andric  if (__val >= 1 && __val <= 9) {
40585ffd83dbSDimitry Andric    if (__val > mark_count())
40595ffd83dbSDimitry Andric      __throw_regex_error<regex_constants::error_backref>();
40605ffd83dbSDimitry Andric    __push_back_ref(__val);
40615ffd83dbSDimitry Andric    return true;
40625ffd83dbSDimitry Andric  }
40635ffd83dbSDimitry Andric
40645ffd83dbSDimitry Andric  return false;
40655ffd83dbSDimitry Andric}
40665ffd83dbSDimitry Andric
40675ffd83dbSDimitry Andrictemplate <class _CharT, class _Traits>
4068cb14a3feSDimitry Andricvoid basic_regex<_CharT, _Traits>::__push_loop(
4069cb14a3feSDimitry Andric    size_t __min, size_t __max, __owns_one_state<_CharT>* __s, size_t __mexp_begin, size_t __mexp_end, bool __greedy) {
40700b57cec5SDimitry Andric  unique_ptr<__empty_state<_CharT> > __e1(new __empty_state<_CharT>(__end_->first()));
40710b57cec5SDimitry Andric  __end_->first() = nullptr;
4072cb14a3feSDimitry Andric  unique_ptr<__loop<_CharT> > __e2(
4073cb14a3feSDimitry Andric      new __loop<_CharT>(__loop_count_, __s->first(), __e1.get(), __mexp_begin, __mexp_end, __greedy, __min, __max));
40740b57cec5SDimitry Andric  __s->first() = nullptr;
40750b57cec5SDimitry Andric  __e1.release();
40760b57cec5SDimitry Andric  __end_->first() = new __repeat_one_loop<_CharT>(__e2.get());
40770b57cec5SDimitry Andric  __end_          = __e2->second();
40780b57cec5SDimitry Andric  __s->first()    = __e2.release();
40790b57cec5SDimitry Andric  ++__loop_count_;
40800b57cec5SDimitry Andric}
40810b57cec5SDimitry Andric
40820b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
4083cb14a3feSDimitry Andricvoid basic_regex<_CharT, _Traits>::__push_char(value_type __c) {
40840b57cec5SDimitry Andric  if (flags() & icase)
4085cb14a3feSDimitry Andric    __end_->first() = new __match_char_icase<_CharT, _Traits>(__traits_, __c, __end_->first());
40860b57cec5SDimitry Andric  else if (flags() & collate)
4087cb14a3feSDimitry Andric    __end_->first() = new __match_char_collate<_CharT, _Traits>(__traits_, __c, __end_->first());
40880b57cec5SDimitry Andric  else
40890b57cec5SDimitry Andric    __end_->first() = new __match_char<_CharT>(__c, __end_->first());
40900b57cec5SDimitry Andric  __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
40910b57cec5SDimitry Andric}
40920b57cec5SDimitry Andric
40930b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
4094cb14a3feSDimitry Andricvoid basic_regex<_CharT, _Traits>::__push_begin_marked_subexpression() {
4095cb14a3feSDimitry Andric  if (!(__flags_ & nosubs)) {
4096cb14a3feSDimitry Andric    __end_->first() = new __begin_marked_subexpression<_CharT>(++__marked_count_, __end_->first());
40970b57cec5SDimitry Andric    __end_          = static_cast<__owns_one_state<_CharT>*>(__end_->first());
40980b57cec5SDimitry Andric  }
40990b57cec5SDimitry Andric}
41000b57cec5SDimitry Andric
41010b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
4102cb14a3feSDimitry Andricvoid basic_regex<_CharT, _Traits>::__push_end_marked_subexpression(unsigned __sub) {
4103cb14a3feSDimitry Andric  if (!(__flags_ & nosubs)) {
4104cb14a3feSDimitry Andric    __end_->first() = new __end_marked_subexpression<_CharT>(__sub, __end_->first());
41050b57cec5SDimitry Andric    __end_          = static_cast<__owns_one_state<_CharT>*>(__end_->first());
41060b57cec5SDimitry Andric  }
41070b57cec5SDimitry Andric}
41080b57cec5SDimitry Andric
41090b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
4110cb14a3feSDimitry Andricvoid basic_regex<_CharT, _Traits>::__push_l_anchor() {
4111e8d8bef9SDimitry Andric  __end_->first() = new __l_anchor_multiline<_CharT>(__use_multiline(), __end_->first());
41120b57cec5SDimitry Andric  __end_          = static_cast<__owns_one_state<_CharT>*>(__end_->first());
41130b57cec5SDimitry Andric}
41140b57cec5SDimitry Andric
41150b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
4116cb14a3feSDimitry Andricvoid basic_regex<_CharT, _Traits>::__push_r_anchor() {
4117e8d8bef9SDimitry Andric  __end_->first() = new __r_anchor_multiline<_CharT>(__use_multiline(), __end_->first());
41180b57cec5SDimitry Andric  __end_          = static_cast<__owns_one_state<_CharT>*>(__end_->first());
41190b57cec5SDimitry Andric}
41200b57cec5SDimitry Andric
41210b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
4122cb14a3feSDimitry Andricvoid basic_regex<_CharT, _Traits>::__push_match_any() {
41230b57cec5SDimitry Andric  __end_->first() = new __match_any<_CharT>(__end_->first());
41240b57cec5SDimitry Andric  __end_          = static_cast<__owns_one_state<_CharT>*>(__end_->first());
41250b57cec5SDimitry Andric}
41260b57cec5SDimitry Andric
41270b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
4128cb14a3feSDimitry Andricvoid basic_regex<_CharT, _Traits>::__push_match_any_but_newline() {
41290b57cec5SDimitry Andric  __end_->first() = new __match_any_but_newline<_CharT>(__end_->first());
41300b57cec5SDimitry Andric  __end_          = static_cast<__owns_one_state<_CharT>*>(__end_->first());
41310b57cec5SDimitry Andric}
41320b57cec5SDimitry Andric
41330b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
4134cb14a3feSDimitry Andricvoid basic_regex<_CharT, _Traits>::__push_empty() {
41350b57cec5SDimitry Andric  __end_->first() = new __empty_state<_CharT>(__end_->first());
41360b57cec5SDimitry Andric  __end_          = static_cast<__owns_one_state<_CharT>*>(__end_->first());
41370b57cec5SDimitry Andric}
41380b57cec5SDimitry Andric
41390b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
4140cb14a3feSDimitry Andricvoid basic_regex<_CharT, _Traits>::__push_word_boundary(bool __invert) {
4141cb14a3feSDimitry Andric  __end_->first() = new __word_boundary<_CharT, _Traits>(__traits_, __invert, __end_->first());
41420b57cec5SDimitry Andric  __end_          = static_cast<__owns_one_state<_CharT>*>(__end_->first());
41430b57cec5SDimitry Andric}
41440b57cec5SDimitry Andric
41450b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
4146cb14a3feSDimitry Andricvoid basic_regex<_CharT, _Traits>::__push_back_ref(int __i) {
41470b57cec5SDimitry Andric  if (flags() & icase)
4148cb14a3feSDimitry Andric    __end_->first() = new __back_ref_icase<_CharT, _Traits>(__traits_, __i, __end_->first());
41490b57cec5SDimitry Andric  else if (flags() & collate)
4150cb14a3feSDimitry Andric    __end_->first() = new __back_ref_collate<_CharT, _Traits>(__traits_, __i, __end_->first());
41510b57cec5SDimitry Andric  else
41520b57cec5SDimitry Andric    __end_->first() = new __back_ref<_CharT>(__i, __end_->first());
41530b57cec5SDimitry Andric  __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
41540b57cec5SDimitry Andric}
41550b57cec5SDimitry Andric
41560b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
4157cb14a3feSDimitry Andricvoid basic_regex<_CharT, _Traits>::__push_alternation(__owns_one_state<_CharT>* __sa, __owns_one_state<_CharT>* __ea) {
41580b57cec5SDimitry Andric  __sa->first() = new __alternate<_CharT>(
4159cb14a3feSDimitry Andric      static_cast<__owns_one_state<_CharT>*>(__sa->first()), static_cast<__owns_one_state<_CharT>*>(__ea->first()));
41600b57cec5SDimitry Andric  __ea->first()   = nullptr;
41610b57cec5SDimitry Andric  __ea->first()   = new __empty_state<_CharT>(__end_->first());
41620b57cec5SDimitry Andric  __end_->first() = nullptr;
41630b57cec5SDimitry Andric  __end_->first() = new __empty_non_own_state<_CharT>(__ea->first());
41640b57cec5SDimitry Andric  __end_          = static_cast<__owns_one_state<_CharT>*>(__ea->first());
41650b57cec5SDimitry Andric}
41660b57cec5SDimitry Andric
41670b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
4168cb14a3feSDimitry Andric__bracket_expression<_CharT, _Traits>* basic_regex<_CharT, _Traits>::__start_matching_list(bool __negate) {
4169cb14a3feSDimitry Andric  __bracket_expression<_CharT, _Traits>* __r = new __bracket_expression<_CharT, _Traits>(
4170cb14a3feSDimitry Andric      __traits_, __end_->first(), __negate, __flags_ & icase, __flags_ & collate);
41710b57cec5SDimitry Andric  __end_->first() = __r;
41720b57cec5SDimitry Andric  __end_          = __r;
41730b57cec5SDimitry Andric  return __r;
41740b57cec5SDimitry Andric}
41750b57cec5SDimitry Andric
41760b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
4177cb14a3feSDimitry Andricvoid basic_regex<_CharT, _Traits>::__push_lookahead(const basic_regex& __exp, bool __invert, unsigned __mexp) {
4178cb14a3feSDimitry Andric  __end_->first() = new __lookahead<_CharT, _Traits>(__exp, __invert, __end_->first(), __mexp);
41790b57cec5SDimitry Andric  __end_          = static_cast<__owns_one_state<_CharT>*>(__end_->first());
41800b57cec5SDimitry Andric}
41810b57cec5SDimitry Andric
41820b57cec5SDimitry Andric// sub_match
41830b57cec5SDimitry Andric
4184e8d8bef9SDimitry Andrictypedef sub_match<const char*> csub_match;
4185e8d8bef9SDimitry Andrictypedef sub_match<string::const_iterator> ssub_match;
4186349cc55cSDimitry Andric#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
4187349cc55cSDimitry Andrictypedef sub_match<const wchar_t*> wcsub_match;
4188e8d8bef9SDimitry Andrictypedef sub_match<wstring::const_iterator> wssub_match;
4189349cc55cSDimitry Andric#endif
4190e8d8bef9SDimitry Andric
41910b57cec5SDimitry Andrictemplate <class _BidirectionalIterator>
4192cb14a3feSDimitry Andricclass _LIBCPP_TEMPLATE_VIS _LIBCPP_PREFERRED_NAME(csub_match)
4193cb14a3feSDimitry Andric    _LIBCPP_IF_WIDE_CHARACTERS(_LIBCPP_PREFERRED_NAME(wcsub_match)) _LIBCPP_PREFERRED_NAME(ssub_match)
4194cb14a3feSDimitry Andric        _LIBCPP_IF_WIDE_CHARACTERS(_LIBCPP_PREFERRED_NAME(wssub_match)) sub_match
4195cb14a3feSDimitry Andric    : public pair<_BidirectionalIterator, _BidirectionalIterator> {
41960b57cec5SDimitry Andricpublic:
41970b57cec5SDimitry Andric  typedef _BidirectionalIterator iterator;
41980b57cec5SDimitry Andric  typedef typename iterator_traits<iterator>::value_type value_type;
41990b57cec5SDimitry Andric  typedef typename iterator_traits<iterator>::difference_type difference_type;
42000b57cec5SDimitry Andric  typedef basic_string<value_type> string_type;
42010b57cec5SDimitry Andric
42020b57cec5SDimitry Andric  bool matched;
42030b57cec5SDimitry Andric
4204cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR sub_match() : matched() {}
42050b57cec5SDimitry Andric
4206cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI difference_type length() const {
4207cb14a3feSDimitry Andric    return matched ? std::distance(this->first, this->second) : 0;
4208cb14a3feSDimitry Andric  }
4209cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI string_type str() const {
4210cb14a3feSDimitry Andric    return matched ? string_type(this->first, this->second) : string_type();
4211cb14a3feSDimitry Andric  }
4212cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI operator string_type() const { return str(); }
42130b57cec5SDimitry Andric
4214cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI int compare(const sub_match& __s) const { return str().compare(__s.str()); }
4215cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI int compare(const string_type& __s) const { return str().compare(__s); }
4216cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI int compare(const value_type* __s) const { return str().compare(__s); }
421706c3fb27SDimitry Andric
4218cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI void swap(sub_match& __s)
421906c3fb27SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
422006c3fb27SDimitry Andric      _NOEXCEPT(__is_nothrow_swappable<_BidirectionalIterator>::value)
422106c3fb27SDimitry Andric#endif // _LIBCPP_CXX03_LANG
422206c3fb27SDimitry Andric  {
422306c3fb27SDimitry Andric    this->pair<_BidirectionalIterator, _BidirectionalIterator>::swap(__s);
422406c3fb27SDimitry Andric    std::swap(matched, __s.matched);
422506c3fb27SDimitry Andric  }
42260b57cec5SDimitry Andric};
42270b57cec5SDimitry Andric
42280b57cec5SDimitry Andrictemplate <class _BiIter>
4229cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool operator==(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y) {
42300b57cec5SDimitry Andric  return __x.compare(__y) == 0;
42310b57cec5SDimitry Andric}
42320b57cec5SDimitry Andric
423306c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 20
423406c3fb27SDimitry Andrictemplate <class _BiIter>
423506c3fb27SDimitry Andricusing __sub_match_cat = compare_three_way_result_t<basic_string<typename iterator_traits<_BiIter>::value_type>>;
423606c3fb27SDimitry Andric
423706c3fb27SDimitry Andrictemplate <class _BiIter>
423806c3fb27SDimitry Andric_LIBCPP_HIDE_FROM_ABI auto operator<=>(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y) {
423906c3fb27SDimitry Andric  return static_cast<__sub_match_cat<_BiIter>>(__x.compare(__y) <=> 0);
424006c3fb27SDimitry Andric}
424106c3fb27SDimitry Andric#else  // _LIBCPP_STD_VER >= 20
42420b57cec5SDimitry Andrictemplate <class _BiIter>
4243cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool operator!=(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y) {
42440b57cec5SDimitry Andric  return !(__x == __y);
42450b57cec5SDimitry Andric}
42460b57cec5SDimitry Andric
42470b57cec5SDimitry Andrictemplate <class _BiIter>
4248cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool operator<(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y) {
42490b57cec5SDimitry Andric  return __x.compare(__y) < 0;
42500b57cec5SDimitry Andric}
42510b57cec5SDimitry Andric
42520b57cec5SDimitry Andrictemplate <class _BiIter>
4253cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool operator<=(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y) {
42540b57cec5SDimitry Andric  return !(__y < __x);
42550b57cec5SDimitry Andric}
42560b57cec5SDimitry Andric
42570b57cec5SDimitry Andrictemplate <class _BiIter>
4258cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool operator>=(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y) {
42590b57cec5SDimitry Andric  return !(__x < __y);
42600b57cec5SDimitry Andric}
42610b57cec5SDimitry Andric
42620b57cec5SDimitry Andrictemplate <class _BiIter>
4263cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool operator>(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y) {
42640b57cec5SDimitry Andric  return __y < __x;
42650b57cec5SDimitry Andric}
42660b57cec5SDimitry Andric
42670b57cec5SDimitry Andrictemplate <class _BiIter, class _ST, class _SA>
4268cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool
42690b57cec5SDimitry Andricoperator==(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4270cb14a3feSDimitry Andric           const sub_match<_BiIter>& __y) {
42710b57cec5SDimitry Andric  return __y.compare(typename sub_match<_BiIter>::string_type(__x.data(), __x.size())) == 0;
42720b57cec5SDimitry Andric}
42730b57cec5SDimitry Andric
42740b57cec5SDimitry Andrictemplate <class _BiIter, class _ST, class _SA>
4275cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool
42760b57cec5SDimitry Andricoperator!=(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4277cb14a3feSDimitry Andric           const sub_match<_BiIter>& __y) {
42780b57cec5SDimitry Andric  return !(__x == __y);
42790b57cec5SDimitry Andric}
42800b57cec5SDimitry Andric
42810b57cec5SDimitry Andrictemplate <class _BiIter, class _ST, class _SA>
4282cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool
42830b57cec5SDimitry Andricoperator<(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4284cb14a3feSDimitry Andric          const sub_match<_BiIter>& __y) {
42850b57cec5SDimitry Andric  return __y.compare(typename sub_match<_BiIter>::string_type(__x.data(), __x.size())) > 0;
42860b57cec5SDimitry Andric}
42870b57cec5SDimitry Andric
42880b57cec5SDimitry Andrictemplate <class _BiIter, class _ST, class _SA>
4289cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool
42900b57cec5SDimitry Andricoperator>(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4291cb14a3feSDimitry Andric          const sub_match<_BiIter>& __y) {
42920b57cec5SDimitry Andric  return __y < __x;
42930b57cec5SDimitry Andric}
42940b57cec5SDimitry Andric
42950b57cec5SDimitry Andrictemplate <class _BiIter, class _ST, class _SA>
4296cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool
4297cb14a3feSDimitry Andricoperator>=(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4298cb14a3feSDimitry Andric           const sub_match<_BiIter>& __y) {
42990b57cec5SDimitry Andric  return !(__x < __y);
43000b57cec5SDimitry Andric}
43010b57cec5SDimitry Andric
43020b57cec5SDimitry Andrictemplate <class _BiIter, class _ST, class _SA>
4303cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool
43040b57cec5SDimitry Andricoperator<=(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4305cb14a3feSDimitry Andric           const sub_match<_BiIter>& __y) {
43060b57cec5SDimitry Andric  return !(__y < __x);
43070b57cec5SDimitry Andric}
430806c3fb27SDimitry Andric#endif // _LIBCPP_STD_VER >= 20
43090b57cec5SDimitry Andric
43100b57cec5SDimitry Andrictemplate <class _BiIter, class _ST, class _SA>
4311cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool
43120b57cec5SDimitry Andricoperator==(const sub_match<_BiIter>& __x,
4313cb14a3feSDimitry Andric           const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y) {
43140b57cec5SDimitry Andric  return __x.compare(typename sub_match<_BiIter>::string_type(__y.data(), __y.size())) == 0;
43150b57cec5SDimitry Andric}
43160b57cec5SDimitry Andric
431706c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 20
431806c3fb27SDimitry Andrictemplate <class _BiIter, class _ST, class _SA>
4319cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI auto
4320cb14a3feSDimitry Andricoperator<=>(const sub_match<_BiIter>& __x,
4321cb14a3feSDimitry Andric            const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y) {
432206c3fb27SDimitry Andric  return static_cast<__sub_match_cat<_BiIter>>(
432306c3fb27SDimitry Andric      __x.compare(typename sub_match<_BiIter>::string_type(__y.data(), __y.size())) <=> 0);
432406c3fb27SDimitry Andric}
432506c3fb27SDimitry Andric#else  // _LIBCPP_STD_VER >= 20
43260b57cec5SDimitry Andrictemplate <class _BiIter, class _ST, class _SA>
4327cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool
43280b57cec5SDimitry Andricoperator!=(const sub_match<_BiIter>& __x,
4329cb14a3feSDimitry Andric           const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y) {
43300b57cec5SDimitry Andric  return !(__x == __y);
43310b57cec5SDimitry Andric}
43320b57cec5SDimitry Andric
43330b57cec5SDimitry Andrictemplate <class _BiIter, class _ST, class _SA>
4334cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool
43350b57cec5SDimitry Andricoperator<(const sub_match<_BiIter>& __x,
4336cb14a3feSDimitry Andric          const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y) {
43370b57cec5SDimitry Andric  return __x.compare(typename sub_match<_BiIter>::string_type(__y.data(), __y.size())) < 0;
43380b57cec5SDimitry Andric}
43390b57cec5SDimitry Andric
43400b57cec5SDimitry Andrictemplate <class _BiIter, class _ST, class _SA>
4341cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool
4342cb14a3feSDimitry Andricoperator>(const sub_match<_BiIter>& __x,
4343cb14a3feSDimitry Andric          const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y) {
43440b57cec5SDimitry Andric  return __y < __x;
43450b57cec5SDimitry Andric}
43460b57cec5SDimitry Andric
43470b57cec5SDimitry Andrictemplate <class _BiIter, class _ST, class _SA>
4348cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool
43490b57cec5SDimitry Andricoperator>=(const sub_match<_BiIter>& __x,
4350cb14a3feSDimitry Andric           const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y) {
43510b57cec5SDimitry Andric  return !(__x < __y);
43520b57cec5SDimitry Andric}
43530b57cec5SDimitry Andric
43540b57cec5SDimitry Andrictemplate <class _BiIter, class _ST, class _SA>
4355cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool
43560b57cec5SDimitry Andricoperator<=(const sub_match<_BiIter>& __x,
4357cb14a3feSDimitry Andric           const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y) {
43580b57cec5SDimitry Andric  return !(__y < __x);
43590b57cec5SDimitry Andric}
43600b57cec5SDimitry Andric
43610b57cec5SDimitry Andrictemplate <class _BiIter>
4362cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool
4363cb14a3feSDimitry Andricoperator==(typename iterator_traits<_BiIter>::value_type const* __x, const sub_match<_BiIter>& __y) {
43640b57cec5SDimitry Andric  return __y.compare(__x) == 0;
43650b57cec5SDimitry Andric}
43660b57cec5SDimitry Andric
43670b57cec5SDimitry Andrictemplate <class _BiIter>
4368cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool
4369cb14a3feSDimitry Andricoperator!=(typename iterator_traits<_BiIter>::value_type const* __x, const sub_match<_BiIter>& __y) {
43700b57cec5SDimitry Andric  return !(__x == __y);
43710b57cec5SDimitry Andric}
43720b57cec5SDimitry Andric
43730b57cec5SDimitry Andrictemplate <class _BiIter>
4374cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool
4375cb14a3feSDimitry Andricoperator<(typename iterator_traits<_BiIter>::value_type const* __x, const sub_match<_BiIter>& __y) {
43760b57cec5SDimitry Andric  return __y.compare(__x) > 0;
43770b57cec5SDimitry Andric}
43780b57cec5SDimitry Andric
43790b57cec5SDimitry Andrictemplate <class _BiIter>
4380cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool
4381cb14a3feSDimitry Andricoperator>(typename iterator_traits<_BiIter>::value_type const* __x, const sub_match<_BiIter>& __y) {
43820b57cec5SDimitry Andric  return __y < __x;
43830b57cec5SDimitry Andric}
43840b57cec5SDimitry Andric
43850b57cec5SDimitry Andrictemplate <class _BiIter>
4386cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool
4387cb14a3feSDimitry Andricoperator>=(typename iterator_traits<_BiIter>::value_type const* __x, const sub_match<_BiIter>& __y) {
43880b57cec5SDimitry Andric  return !(__x < __y);
43890b57cec5SDimitry Andric}
43900b57cec5SDimitry Andric
43910b57cec5SDimitry Andrictemplate <class _BiIter>
4392cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool
4393cb14a3feSDimitry Andricoperator<=(typename iterator_traits<_BiIter>::value_type const* __x, const sub_match<_BiIter>& __y) {
43940b57cec5SDimitry Andric  return !(__y < __x);
43950b57cec5SDimitry Andric}
439606c3fb27SDimitry Andric#endif // _LIBCPP_STD_VER >= 20
43970b57cec5SDimitry Andric
43980b57cec5SDimitry Andrictemplate <class _BiIter>
4399cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool
4400cb14a3feSDimitry Andricoperator==(const sub_match<_BiIter>& __x, typename iterator_traits<_BiIter>::value_type const* __y) {
44010b57cec5SDimitry Andric  return __x.compare(__y) == 0;
44020b57cec5SDimitry Andric}
44030b57cec5SDimitry Andric
440406c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 20
440506c3fb27SDimitry Andrictemplate <class _BiIter>
440606c3fb27SDimitry Andric_LIBCPP_HIDE_FROM_ABI auto
440706c3fb27SDimitry Andricoperator<=>(const sub_match<_BiIter>& __x, typename iterator_traits<_BiIter>::value_type const* __y) {
440806c3fb27SDimitry Andric  return static_cast<__sub_match_cat<_BiIter>>(__x.compare(__y) <=> 0);
440906c3fb27SDimitry Andric}
441006c3fb27SDimitry Andric#else  // _LIBCPP_STD_VER >= 20
44110b57cec5SDimitry Andrictemplate <class _BiIter>
4412cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool
4413cb14a3feSDimitry Andricoperator!=(const sub_match<_BiIter>& __x, typename iterator_traits<_BiIter>::value_type const* __y) {
44140b57cec5SDimitry Andric  return !(__x == __y);
44150b57cec5SDimitry Andric}
44160b57cec5SDimitry Andric
44170b57cec5SDimitry Andrictemplate <class _BiIter>
4418cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool
4419cb14a3feSDimitry Andricoperator<(const sub_match<_BiIter>& __x, typename iterator_traits<_BiIter>::value_type const* __y) {
44200b57cec5SDimitry Andric  return __x.compare(__y) < 0;
44210b57cec5SDimitry Andric}
44220b57cec5SDimitry Andric
44230b57cec5SDimitry Andrictemplate <class _BiIter>
4424cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool
4425cb14a3feSDimitry Andricoperator>(const sub_match<_BiIter>& __x, typename iterator_traits<_BiIter>::value_type const* __y) {
44260b57cec5SDimitry Andric  return __y < __x;
44270b57cec5SDimitry Andric}
44280b57cec5SDimitry Andric
44290b57cec5SDimitry Andrictemplate <class _BiIter>
4430cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool
4431cb14a3feSDimitry Andricoperator>=(const sub_match<_BiIter>& __x, typename iterator_traits<_BiIter>::value_type const* __y) {
44320b57cec5SDimitry Andric  return !(__x < __y);
44330b57cec5SDimitry Andric}
44340b57cec5SDimitry Andric
44350b57cec5SDimitry Andrictemplate <class _BiIter>
4436cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool
4437cb14a3feSDimitry Andricoperator<=(const sub_match<_BiIter>& __x, typename iterator_traits<_BiIter>::value_type const* __y) {
44380b57cec5SDimitry Andric  return !(__y < __x);
44390b57cec5SDimitry Andric}
44400b57cec5SDimitry Andric
44410b57cec5SDimitry Andrictemplate <class _BiIter>
4442cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool
4443cb14a3feSDimitry Andricoperator==(typename iterator_traits<_BiIter>::value_type const& __x, const sub_match<_BiIter>& __y) {
44440b57cec5SDimitry Andric  typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
44450b57cec5SDimitry Andric  return __y.compare(string_type(1, __x)) == 0;
44460b57cec5SDimitry Andric}
44470b57cec5SDimitry Andric
44480b57cec5SDimitry Andrictemplate <class _BiIter>
4449cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool
4450cb14a3feSDimitry Andricoperator!=(typename iterator_traits<_BiIter>::value_type const& __x, const sub_match<_BiIter>& __y) {
44510b57cec5SDimitry Andric  return !(__x == __y);
44520b57cec5SDimitry Andric}
44530b57cec5SDimitry Andric
44540b57cec5SDimitry Andrictemplate <class _BiIter>
4455cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool
4456cb14a3feSDimitry Andricoperator<(typename iterator_traits<_BiIter>::value_type const& __x, const sub_match<_BiIter>& __y) {
44570b57cec5SDimitry Andric  typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
44580b57cec5SDimitry Andric  return __y.compare(string_type(1, __x)) > 0;
44590b57cec5SDimitry Andric}
44600b57cec5SDimitry Andric
44610b57cec5SDimitry Andrictemplate <class _BiIter>
4462cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool
4463cb14a3feSDimitry Andricoperator>(typename iterator_traits<_BiIter>::value_type const& __x, const sub_match<_BiIter>& __y) {
44640b57cec5SDimitry Andric  return __y < __x;
44650b57cec5SDimitry Andric}
44660b57cec5SDimitry Andric
44670b57cec5SDimitry Andrictemplate <class _BiIter>
4468cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool
4469cb14a3feSDimitry Andricoperator>=(typename iterator_traits<_BiIter>::value_type const& __x, const sub_match<_BiIter>& __y) {
44700b57cec5SDimitry Andric  return !(__x < __y);
44710b57cec5SDimitry Andric}
44720b57cec5SDimitry Andric
44730b57cec5SDimitry Andrictemplate <class _BiIter>
4474cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool
4475cb14a3feSDimitry Andricoperator<=(typename iterator_traits<_BiIter>::value_type const& __x, const sub_match<_BiIter>& __y) {
44760b57cec5SDimitry Andric  return !(__y < __x);
44770b57cec5SDimitry Andric}
447806c3fb27SDimitry Andric#endif // _LIBCPP_STD_VER >= 20
44790b57cec5SDimitry Andric
44800b57cec5SDimitry Andrictemplate <class _BiIter>
4481cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool
4482cb14a3feSDimitry Andricoperator==(const sub_match<_BiIter>& __x, typename iterator_traits<_BiIter>::value_type const& __y) {
44830b57cec5SDimitry Andric  typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
44840b57cec5SDimitry Andric  return __x.compare(string_type(1, __y)) == 0;
44850b57cec5SDimitry Andric}
44860b57cec5SDimitry Andric
448706c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 20
448806c3fb27SDimitry Andrictemplate <class _BiIter>
448906c3fb27SDimitry Andric_LIBCPP_HIDE_FROM_ABI auto
449006c3fb27SDimitry Andricoperator<=>(const sub_match<_BiIter>& __x, typename iterator_traits<_BiIter>::value_type const& __y) {
449106c3fb27SDimitry Andric  using string_type = basic_string<typename iterator_traits<_BiIter>::value_type>;
449206c3fb27SDimitry Andric  return static_cast<__sub_match_cat<_BiIter>>(__x.compare(string_type(1, __y)) <=> 0);
449306c3fb27SDimitry Andric}
449406c3fb27SDimitry Andric#else  // _LIBCPP_STD_VER >= 20
44950b57cec5SDimitry Andrictemplate <class _BiIter>
4496cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool
4497cb14a3feSDimitry Andricoperator!=(const sub_match<_BiIter>& __x, typename iterator_traits<_BiIter>::value_type const& __y) {
44980b57cec5SDimitry Andric  return !(__x == __y);
44990b57cec5SDimitry Andric}
45000b57cec5SDimitry Andric
45010b57cec5SDimitry Andrictemplate <class _BiIter>
4502cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool
4503cb14a3feSDimitry Andricoperator<(const sub_match<_BiIter>& __x, typename iterator_traits<_BiIter>::value_type const& __y) {
45040b57cec5SDimitry Andric  typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
45050b57cec5SDimitry Andric  return __x.compare(string_type(1, __y)) < 0;
45060b57cec5SDimitry Andric}
45070b57cec5SDimitry Andric
45080b57cec5SDimitry Andrictemplate <class _BiIter>
4509cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool
4510cb14a3feSDimitry Andricoperator>(const sub_match<_BiIter>& __x, typename iterator_traits<_BiIter>::value_type const& __y) {
45110b57cec5SDimitry Andric  return __y < __x;
45120b57cec5SDimitry Andric}
45130b57cec5SDimitry Andric
45140b57cec5SDimitry Andrictemplate <class _BiIter>
4515cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool
4516cb14a3feSDimitry Andricoperator>=(const sub_match<_BiIter>& __x, typename iterator_traits<_BiIter>::value_type const& __y) {
45170b57cec5SDimitry Andric  return !(__x < __y);
45180b57cec5SDimitry Andric}
45190b57cec5SDimitry Andric
45200b57cec5SDimitry Andrictemplate <class _BiIter>
4521cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool
4522cb14a3feSDimitry Andricoperator<=(const sub_match<_BiIter>& __x, typename iterator_traits<_BiIter>::value_type const& __y) {
45230b57cec5SDimitry Andric  return !(__y < __x);
45240b57cec5SDimitry Andric}
452506c3fb27SDimitry Andric#endif // _LIBCPP_STD_VER >= 20
45260b57cec5SDimitry Andric
45270b57cec5SDimitry Andrictemplate <class _CharT, class _ST, class _BiIter>
4528cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _ST>&
4529cb14a3feSDimitry Andricoperator<<(basic_ostream<_CharT, _ST>& __os, const sub_match<_BiIter>& __m) {
45300b57cec5SDimitry Andric  return __os << __m.str();
45310b57cec5SDimitry Andric}
45320b57cec5SDimitry Andric
4533e8d8bef9SDimitry Andrictypedef match_results<const char*> cmatch;
4534e8d8bef9SDimitry Andrictypedef match_results<string::const_iterator> smatch;
4535349cc55cSDimitry Andric#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
4536349cc55cSDimitry Andrictypedef match_results<const wchar_t*> wcmatch;
4537e8d8bef9SDimitry Andrictypedef match_results<wstring::const_iterator> wsmatch;
4538349cc55cSDimitry Andric#endif
4539e8d8bef9SDimitry Andric
45400b57cec5SDimitry Andrictemplate <class _BidirectionalIterator, class _Allocator>
4541cb14a3feSDimitry Andricclass _LIBCPP_TEMPLATE_VIS _LIBCPP_PREFERRED_NAME(cmatch) _LIBCPP_IF_WIDE_CHARACTERS(_LIBCPP_PREFERRED_NAME(wcmatch))
4542cb14a3feSDimitry Andric    _LIBCPP_PREFERRED_NAME(smatch) _LIBCPP_IF_WIDE_CHARACTERS(_LIBCPP_PREFERRED_NAME(wsmatch)) match_results {
45430b57cec5SDimitry Andricpublic:
45440b57cec5SDimitry Andric  typedef _Allocator allocator_type;
45450b57cec5SDimitry Andric  typedef sub_match<_BidirectionalIterator> value_type;
4546cb14a3feSDimitry Andric
45470b57cec5SDimitry Andricprivate:
45480b57cec5SDimitry Andric  typedef vector<value_type, allocator_type> __container_type;
45490b57cec5SDimitry Andric
45500b57cec5SDimitry Andric  __container_type __matches_;
45510b57cec5SDimitry Andric  value_type __unmatched_;
45520b57cec5SDimitry Andric  value_type __prefix_;
45530b57cec5SDimitry Andric  value_type __suffix_;
45540b57cec5SDimitry Andric  bool __ready_;
4555cb14a3feSDimitry Andric
45560b57cec5SDimitry Andricpublic:
45570b57cec5SDimitry Andric  _BidirectionalIterator __position_start_;
45580b57cec5SDimitry Andric  typedef const value_type& const_reference;
45590b57cec5SDimitry Andric  typedef value_type& reference;
45600b57cec5SDimitry Andric  typedef typename __container_type::const_iterator const_iterator;
45610b57cec5SDimitry Andric  typedef const_iterator iterator;
45620b57cec5SDimitry Andric  typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
45630b57cec5SDimitry Andric  typedef typename allocator_traits<allocator_type>::size_type size_type;
45640b57cec5SDimitry Andric  typedef typename iterator_traits<_BidirectionalIterator>::value_type char_type;
45650b57cec5SDimitry Andric  typedef basic_string<char_type> string_type;
45660b57cec5SDimitry Andric
45670b57cec5SDimitry Andric  // construct/copy/destroy:
4568e8d8bef9SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
4569e8d8bef9SDimitry Andric  match_results() : match_results(allocator_type()) {}
4570e8d8bef9SDimitry Andric  explicit match_results(const allocator_type& __a);
4571e8d8bef9SDimitry Andric#else
45720b57cec5SDimitry Andric  explicit match_results(const allocator_type& __a = allocator_type());
4573e8d8bef9SDimitry Andric#endif
4574e8d8bef9SDimitry Andric
45750b57cec5SDimitry Andric  //    match_results(const match_results&) = default;
45760b57cec5SDimitry Andric  //    match_results& operator=(const match_results&) = default;
45770b57cec5SDimitry Andric  //    match_results(match_results&& __m) = default;
45780b57cec5SDimitry Andric  //    match_results& operator=(match_results&& __m) = default;
45790b57cec5SDimitry Andric  //    ~match_results() = default;
45800b57cec5SDimitry Andric
4581cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI bool ready() const { return __ready_; }
45820b57cec5SDimitry Andric
45830b57cec5SDimitry Andric  // size:
4584cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI size_type size() const _NOEXCEPT { return __matches_.size(); }
4585cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT { return __matches_.max_size(); }
4586cb14a3feSDimitry Andric  _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI bool empty() const _NOEXCEPT { return size() == 0; }
45870b57cec5SDimitry Andric
45880b57cec5SDimitry Andric  // element access:
4589cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI difference_type length(size_type __sub = 0) const {
45901db9f3b2SDimitry Andric    // If the match results are not ready, this will return `0`.
45911db9f3b2SDimitry Andric    _LIBCPP_ASSERT_PEDANTIC(ready(), "match_results::length() called when not ready");
45920b57cec5SDimitry Andric    return (*this)[__sub].length();
45930b57cec5SDimitry Andric  }
4594cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI difference_type position(size_type __sub = 0) const {
45951db9f3b2SDimitry Andric    // If the match results are not ready, this will return the result of subtracting two default-constructed iterators
45961db9f3b2SDimitry Andric    // (which is typically a well-defined operation).
45971db9f3b2SDimitry Andric    _LIBCPP_ASSERT_PEDANTIC(ready(), "match_results::position() called when not ready");
45985f757f3fSDimitry Andric    return std::distance(__position_start_, (*this)[__sub].first);
45990b57cec5SDimitry Andric  }
4600cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI string_type str(size_type __sub = 0) const {
46011db9f3b2SDimitry Andric    // If the match results are not ready, this will return an empty string.
46021db9f3b2SDimitry Andric    _LIBCPP_ASSERT_PEDANTIC(ready(), "match_results::str() called when not ready");
46030b57cec5SDimitry Andric    return (*this)[__sub].str();
46040b57cec5SDimitry Andric  }
4605cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI const_reference operator[](size_type __n) const {
46061db9f3b2SDimitry Andric    // If the match results are not ready, this call will be equivalent to calling this function with `__n >= size()`,
46071db9f3b2SDimitry Andric    // returning an empty subrange.
46081db9f3b2SDimitry Andric    _LIBCPP_ASSERT_PEDANTIC(ready(), "match_results::operator[]() called when not ready");
46090b57cec5SDimitry Andric    return __n < __matches_.size() ? __matches_[__n] : __unmatched_;
46100b57cec5SDimitry Andric  }
46110b57cec5SDimitry Andric
4612cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI const_reference prefix() const {
46131db9f3b2SDimitry Andric    // If the match results are not ready, this will return a default-constructed empty `__suffix_`.
46141db9f3b2SDimitry Andric    _LIBCPP_ASSERT_PEDANTIC(ready(), "match_results::prefix() called when not ready");
46150b57cec5SDimitry Andric    return __prefix_;
46160b57cec5SDimitry Andric  }
4617cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI const_reference suffix() const {
46181db9f3b2SDimitry Andric    // If the match results are not ready, this will return a default-constructed empty `__suffix_`.
46191db9f3b2SDimitry Andric    _LIBCPP_ASSERT_PEDANTIC(ready(), "match_results::suffix() called when not ready");
46200b57cec5SDimitry Andric    return __suffix_;
46210b57cec5SDimitry Andric  }
46220b57cec5SDimitry Andric
4623cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI const_iterator begin() const { return empty() ? __matches_.end() : __matches_.begin(); }
4624cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI const_iterator end() const { return __matches_.end(); }
4625cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI const_iterator cbegin() const { return empty() ? __matches_.end() : __matches_.begin(); }
4626cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI const_iterator cend() const { return __matches_.end(); }
46270b57cec5SDimitry Andric
46280b57cec5SDimitry Andric  // format:
46290b57cec5SDimitry Andric  template <class _OutputIter>
4630cb14a3feSDimitry Andric  _OutputIter format(_OutputIter __output_iter,
4631cb14a3feSDimitry Andric                     const char_type* __fmt_first,
46320b57cec5SDimitry Andric                     const char_type* __fmt_last,
46330b57cec5SDimitry Andric                     regex_constants::match_flag_type __flags = regex_constants::format_default) const;
46340b57cec5SDimitry Andric  template <class _OutputIter, class _ST, class _SA>
4635cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI _OutputIter
4636cb14a3feSDimitry Andric  format(_OutputIter __output_iter,
4637cb14a3feSDimitry Andric         const basic_string<char_type, _ST, _SA>& __fmt,
4638cb14a3feSDimitry Andric         regex_constants::match_flag_type __flags = regex_constants::format_default) const {
4639cb14a3feSDimitry Andric    return format(__output_iter, __fmt.data(), __fmt.data() + __fmt.size(), __flags);
4640cb14a3feSDimitry Andric  }
46410b57cec5SDimitry Andric  template <class _ST, class _SA>
4642cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI basic_string<char_type, _ST, _SA>
46430b57cec5SDimitry Andric  format(const basic_string<char_type, _ST, _SA>& __fmt,
4644cb14a3feSDimitry Andric         regex_constants::match_flag_type __flags = regex_constants::format_default) const {
46450b57cec5SDimitry Andric    basic_string<char_type, _ST, _SA> __r;
4646cb14a3feSDimitry Andric    format(std::back_inserter(__r), __fmt.data(), __fmt.data() + __fmt.size(), __flags);
46470b57cec5SDimitry Andric    return __r;
46480b57cec5SDimitry Andric  }
4649cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI string_type
4650cb14a3feSDimitry Andric  format(const char_type* __fmt, regex_constants::match_flag_type __flags = regex_constants::format_default) const {
46510b57cec5SDimitry Andric    string_type __r;
4652cb14a3feSDimitry Andric    format(std::back_inserter(__r), __fmt, __fmt + char_traits<char_type>::length(__fmt), __flags);
46530b57cec5SDimitry Andric    return __r;
46540b57cec5SDimitry Andric  }
46550b57cec5SDimitry Andric
46560b57cec5SDimitry Andric  // allocator:
4657cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI allocator_type get_allocator() const { return __matches_.get_allocator(); }
46580b57cec5SDimitry Andric
46590b57cec5SDimitry Andric  // swap:
46600b57cec5SDimitry Andric  void swap(match_results& __m);
46610b57cec5SDimitry Andric
46620b57cec5SDimitry Andric  template <class _Bp, class _Ap>
4663cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI void
4664cb14a3feSDimitry Andric  __assign(_BidirectionalIterator __f,
4665cb14a3feSDimitry Andric           _BidirectionalIterator __l,
4666cb14a3feSDimitry Andric           const match_results<_Bp, _Ap>& __m,
4667cb14a3feSDimitry Andric           bool __no_update_pos) {
46680b57cec5SDimitry Andric    _Bp __mf = __m.prefix().first;
46690b57cec5SDimitry Andric    __matches_.resize(__m.size());
4670cb14a3feSDimitry Andric    for (size_type __i = 0; __i < __matches_.size(); ++__i) {
46715f757f3fSDimitry Andric      __matches_[__i].first   = std::next(__f, std::distance(__mf, __m[__i].first));
46725f757f3fSDimitry Andric      __matches_[__i].second  = std::next(__f, std::distance(__mf, __m[__i].second));
46730b57cec5SDimitry Andric      __matches_[__i].matched = __m[__i].matched;
46740b57cec5SDimitry Andric    }
46750b57cec5SDimitry Andric    __unmatched_.first   = __l;
46760b57cec5SDimitry Andric    __unmatched_.second  = __l;
46770b57cec5SDimitry Andric    __unmatched_.matched = false;
46785f757f3fSDimitry Andric    __prefix_.first      = std::next(__f, std::distance(__mf, __m.prefix().first));
46795f757f3fSDimitry Andric    __prefix_.second     = std::next(__f, std::distance(__mf, __m.prefix().second));
46800b57cec5SDimitry Andric    __prefix_.matched    = __m.prefix().matched;
46815f757f3fSDimitry Andric    __suffix_.first      = std::next(__f, std::distance(__mf, __m.suffix().first));
46825f757f3fSDimitry Andric    __suffix_.second     = std::next(__f, std::distance(__mf, __m.suffix().second));
46830b57cec5SDimitry Andric    __suffix_.matched    = __m.suffix().matched;
46840b57cec5SDimitry Andric    if (!__no_update_pos)
46850b57cec5SDimitry Andric      __position_start_ = __prefix_.first;
46860b57cec5SDimitry Andric    __ready_ = __m.ready();
46870b57cec5SDimitry Andric  }
46880b57cec5SDimitry Andric
46890b57cec5SDimitry Andricprivate:
4690cb14a3feSDimitry Andric  void __init(unsigned __s, _BidirectionalIterator __f, _BidirectionalIterator __l, bool __no_update_pos = false);
46910b57cec5SDimitry Andric
4692cb14a3feSDimitry Andric  template <class, class>
4693cb14a3feSDimitry Andric  friend class basic_regex;
46940b57cec5SDimitry Andric
46950b57cec5SDimitry Andric  template <class _Bp, class _Ap, class _Cp, class _Tp>
4696cb14a3feSDimitry Andric  friend bool
4697cb14a3feSDimitry Andric  regex_match(_Bp, _Bp, match_results<_Bp, _Ap>&, const basic_regex<_Cp, _Tp>&, regex_constants::match_flag_type);
46980b57cec5SDimitry Andric
46990b57cec5SDimitry Andric  template <class _Bp, class _Ap>
4700cb14a3feSDimitry Andric  friend bool operator==(const match_results<_Bp, _Ap>&, const match_results<_Bp, _Ap>&);
47010b57cec5SDimitry Andric
4702cb14a3feSDimitry Andric  template <class, class>
4703cb14a3feSDimitry Andric  friend class __lookahead;
47040b57cec5SDimitry Andric};
47050b57cec5SDimitry Andric
47060b57cec5SDimitry Andrictemplate <class _BidirectionalIterator, class _Allocator>
4707cb14a3feSDimitry Andricmatch_results<_BidirectionalIterator, _Allocator>::match_results(const allocator_type& __a)
4708cb14a3feSDimitry Andric    : __matches_(__a), __unmatched_(), __prefix_(), __suffix_(), __ready_(false), __position_start_() {}
47090b57cec5SDimitry Andric
47100b57cec5SDimitry Andrictemplate <class _BidirectionalIterator, class _Allocator>
4711cb14a3feSDimitry Andricvoid match_results<_BidirectionalIterator, _Allocator>::__init(
4712cb14a3feSDimitry Andric    unsigned __s, _BidirectionalIterator __f, _BidirectionalIterator __l, bool __no_update_pos) {
47130b57cec5SDimitry Andric  __unmatched_.first   = __l;
47140b57cec5SDimitry Andric  __unmatched_.second  = __l;
47150b57cec5SDimitry Andric  __unmatched_.matched = false;
47160b57cec5SDimitry Andric  __matches_.assign(__s, __unmatched_);
47170b57cec5SDimitry Andric  __prefix_.first   = __f;
47180b57cec5SDimitry Andric  __prefix_.second  = __f;
47190b57cec5SDimitry Andric  __prefix_.matched = false;
47200b57cec5SDimitry Andric  __suffix_         = __unmatched_;
47210b57cec5SDimitry Andric  if (!__no_update_pos)
47220b57cec5SDimitry Andric    __position_start_ = __prefix_.first;
47230b57cec5SDimitry Andric  __ready_ = true;
47240b57cec5SDimitry Andric}
47250b57cec5SDimitry Andric
47260b57cec5SDimitry Andrictemplate <class _BidirectionalIterator, class _Allocator>
47270b57cec5SDimitry Andrictemplate <class _OutputIter>
4728cb14a3feSDimitry Andric_OutputIter match_results<_BidirectionalIterator, _Allocator>::format(
4729cb14a3feSDimitry Andric    _OutputIter __output_iter,
4730cb14a3feSDimitry Andric    const char_type* __fmt_first,
4731cb14a3feSDimitry Andric    const char_type* __fmt_last,
4732cb14a3feSDimitry Andric    regex_constants::match_flag_type __flags) const {
47331db9f3b2SDimitry Andric  // Note: this duplicates a check in `vector::operator[]` but provides a better error message.
47341db9f3b2SDimitry Andric  _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(ready(), "match_results::format() called when not ready");
4735cb14a3feSDimitry Andric  if (__flags & regex_constants::format_sed) {
4736cb14a3feSDimitry Andric    for (; __fmt_first != __fmt_last; ++__fmt_first) {
47370b57cec5SDimitry Andric      if (*__fmt_first == '&')
4738cb14a3feSDimitry Andric        __output_iter = std::copy(__matches_[0].first, __matches_[0].second, __output_iter);
4739cb14a3feSDimitry Andric      else if (*__fmt_first == '\\' && __fmt_first + 1 != __fmt_last) {
47400b57cec5SDimitry Andric        ++__fmt_first;
4741cb14a3feSDimitry Andric        if ('0' <= *__fmt_first && *__fmt_first <= '9') {
47420b57cec5SDimitry Andric          size_t __i    = *__fmt_first - '0';
4743cb14a3feSDimitry Andric          __output_iter = std::copy((*this)[__i].first, (*this)[__i].second, __output_iter);
4744cb14a3feSDimitry Andric        } else {
4745cb14a3feSDimitry Andric          *__output_iter = *__fmt_first;
4746cb14a3feSDimitry Andric          ++__output_iter;
47470b57cec5SDimitry Andric        }
4748cb14a3feSDimitry Andric      } else {
47490b57cec5SDimitry Andric        *__output_iter = *__fmt_first;
47500b57cec5SDimitry Andric        ++__output_iter;
47510b57cec5SDimitry Andric      }
47520b57cec5SDimitry Andric    }
4753cb14a3feSDimitry Andric  } else {
4754cb14a3feSDimitry Andric    for (; __fmt_first != __fmt_last; ++__fmt_first) {
4755cb14a3feSDimitry Andric      if (*__fmt_first == '$' && __fmt_first + 1 != __fmt_last) {
4756cb14a3feSDimitry Andric        switch (__fmt_first[1]) {
47570b57cec5SDimitry Andric        case '$':
47580b57cec5SDimitry Andric          *__output_iter = *++__fmt_first;
47590b57cec5SDimitry Andric          ++__output_iter;
47600b57cec5SDimitry Andric          break;
47610b57cec5SDimitry Andric        case '&':
47620b57cec5SDimitry Andric          ++__fmt_first;
4763cb14a3feSDimitry Andric          __output_iter = std::copy(__matches_[0].first, __matches_[0].second, __output_iter);
47640b57cec5SDimitry Andric          break;
47650b57cec5SDimitry Andric        case '`':
47660b57cec5SDimitry Andric          ++__fmt_first;
47675f757f3fSDimitry Andric          __output_iter = std::copy(__prefix_.first, __prefix_.second, __output_iter);
47680b57cec5SDimitry Andric          break;
47690b57cec5SDimitry Andric        case '\'':
47700b57cec5SDimitry Andric          ++__fmt_first;
47715f757f3fSDimitry Andric          __output_iter = std::copy(__suffix_.first, __suffix_.second, __output_iter);
47720b57cec5SDimitry Andric          break;
47730b57cec5SDimitry Andric        default:
4774cb14a3feSDimitry Andric          if ('0' <= __fmt_first[1] && __fmt_first[1] <= '9') {
47750b57cec5SDimitry Andric            ++__fmt_first;
47760b57cec5SDimitry Andric            size_t __idx = *__fmt_first - '0';
4777cb14a3feSDimitry Andric            if (__fmt_first + 1 != __fmt_last && '0' <= __fmt_first[1] && __fmt_first[1] <= '9') {
47780b57cec5SDimitry Andric              ++__fmt_first;
4779e8d8bef9SDimitry Andric              if (__idx >= numeric_limits<size_t>::max() / 10)
47800b57cec5SDimitry Andric                __throw_regex_error<regex_constants::error_escape>();
47810b57cec5SDimitry Andric              __idx = 10 * __idx + *__fmt_first - '0';
47820b57cec5SDimitry Andric            }
4783cb14a3feSDimitry Andric            __output_iter = std::copy((*this)[__idx].first, (*this)[__idx].second, __output_iter);
4784cb14a3feSDimitry Andric          } else {
47850b57cec5SDimitry Andric            *__output_iter = *__fmt_first;
47860b57cec5SDimitry Andric            ++__output_iter;
47870b57cec5SDimitry Andric          }
47880b57cec5SDimitry Andric          break;
47890b57cec5SDimitry Andric        }
4790cb14a3feSDimitry Andric      } else {
47910b57cec5SDimitry Andric        *__output_iter = *__fmt_first;
47920b57cec5SDimitry Andric        ++__output_iter;
47930b57cec5SDimitry Andric      }
47940b57cec5SDimitry Andric    }
47950b57cec5SDimitry Andric  }
47960b57cec5SDimitry Andric  return __output_iter;
47970b57cec5SDimitry Andric}
47980b57cec5SDimitry Andric
47990b57cec5SDimitry Andrictemplate <class _BidirectionalIterator, class _Allocator>
4800cb14a3feSDimitry Andricvoid match_results<_BidirectionalIterator, _Allocator>::swap(match_results& __m) {
48015f757f3fSDimitry Andric  using std::swap;
48020b57cec5SDimitry Andric  swap(__matches_, __m.__matches_);
48030b57cec5SDimitry Andric  swap(__unmatched_, __m.__unmatched_);
48040b57cec5SDimitry Andric  swap(__prefix_, __m.__prefix_);
48050b57cec5SDimitry Andric  swap(__suffix_, __m.__suffix_);
48060b57cec5SDimitry Andric  swap(__position_start_, __m.__position_start_);
48070b57cec5SDimitry Andric  swap(__ready_, __m.__ready_);
48080b57cec5SDimitry Andric}
48090b57cec5SDimitry Andric
48100b57cec5SDimitry Andrictemplate <class _BidirectionalIterator, class _Allocator>
4811cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI bool operator==(const match_results<_BidirectionalIterator, _Allocator>& __x,
4812cb14a3feSDimitry Andric                                      const match_results<_BidirectionalIterator, _Allocator>& __y) {
48130b57cec5SDimitry Andric  if (__x.__ready_ != __y.__ready_)
48140b57cec5SDimitry Andric    return false;
48150b57cec5SDimitry Andric  if (!__x.__ready_)
48160b57cec5SDimitry Andric    return true;
4817cb14a3feSDimitry Andric  return __x.__matches_ == __y.__matches_ && __x.__prefix_ == __y.__prefix_ && __x.__suffix_ == __y.__suffix_;
48180b57cec5SDimitry Andric}
48190b57cec5SDimitry Andric
482006c3fb27SDimitry Andric#if _LIBCPP_STD_VER < 20
48210b57cec5SDimitry Andrictemplate <class _BidirectionalIterator, class _Allocator>
4822cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool operator!=(const match_results<_BidirectionalIterator, _Allocator>& __x,
4823cb14a3feSDimitry Andric                                             const match_results<_BidirectionalIterator, _Allocator>& __y) {
48240b57cec5SDimitry Andric  return !(__x == __y);
48250b57cec5SDimitry Andric}
482606c3fb27SDimitry Andric#endif
48270b57cec5SDimitry Andric
48280b57cec5SDimitry Andrictemplate <class _BidirectionalIterator, class _Allocator>
4829cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI void
4830cb14a3feSDimitry Andricswap(match_results<_BidirectionalIterator, _Allocator>& __x, match_results<_BidirectionalIterator, _Allocator>& __y) {
48310b57cec5SDimitry Andric  __x.swap(__y);
48320b57cec5SDimitry Andric}
48330b57cec5SDimitry Andric
48340b57cec5SDimitry Andric// regex_search
48350b57cec5SDimitry Andric
48360b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
48370b57cec5SDimitry Andrictemplate <class _Allocator>
4838cb14a3feSDimitry Andricbool basic_regex<_CharT, _Traits>::__match_at_start_ecma(
4839cb14a3feSDimitry Andric    const _CharT* __first,
4840cb14a3feSDimitry Andric    const _CharT* __last,
48410b57cec5SDimitry Andric    match_results<const _CharT*, _Allocator>& __m,
4842cb14a3feSDimitry Andric    regex_constants::match_flag_type __flags,
4843cb14a3feSDimitry Andric    bool __at_first) const {
48440b57cec5SDimitry Andric  vector<__state> __states;
48450b57cec5SDimitry Andric  __node* __st = __start_.get();
4846cb14a3feSDimitry Andric  if (__st) {
48470b57cec5SDimitry Andric    sub_match<const _CharT*> __unmatched;
48480b57cec5SDimitry Andric    __unmatched.first   = __last;
48490b57cec5SDimitry Andric    __unmatched.second  = __last;
48500b57cec5SDimitry Andric    __unmatched.matched = false;
48510b57cec5SDimitry Andric
48520b57cec5SDimitry Andric    __states.push_back(__state());
48530b57cec5SDimitry Andric    __states.back().__do_      = 0;
48540b57cec5SDimitry Andric    __states.back().__first_   = __first;
48550b57cec5SDimitry Andric    __states.back().__current_ = __first;
48560b57cec5SDimitry Andric    __states.back().__last_    = __last;
48570b57cec5SDimitry Andric    __states.back().__sub_matches_.resize(mark_count(), __unmatched);
48580b57cec5SDimitry Andric    __states.back().__loop_data_.resize(__loop_count());
48590b57cec5SDimitry Andric    __states.back().__node_     = __st;
48600b57cec5SDimitry Andric    __states.back().__flags_    = __flags;
48610b57cec5SDimitry Andric    __states.back().__at_first_ = __at_first;
48620b57cec5SDimitry Andric    int __counter               = 0;
48630b57cec5SDimitry Andric    int __length                = __last - __first;
4864cb14a3feSDimitry Andric    do {
48650b57cec5SDimitry Andric      ++__counter;
4866cb14a3feSDimitry Andric      if (__counter % _LIBCPP_REGEX_COMPLEXITY_FACTOR == 0 && __counter / _LIBCPP_REGEX_COMPLEXITY_FACTOR >= __length)
48670b57cec5SDimitry Andric        __throw_regex_error<regex_constants::error_complexity>();
48680b57cec5SDimitry Andric      __state& __s = __states.back();
48690b57cec5SDimitry Andric      if (__s.__node_)
48700b57cec5SDimitry Andric        __s.__node_->__exec(__s);
4871cb14a3feSDimitry Andric      switch (__s.__do_) {
48720b57cec5SDimitry Andric      case __state::__end_state:
4873cb14a3feSDimitry Andric        if ((__flags & regex_constants::match_not_null) && __s.__current_ == __first) {
48740b57cec5SDimitry Andric          __states.pop_back();
48750b57cec5SDimitry Andric          break;
48760b57cec5SDimitry Andric        }
4877cb14a3feSDimitry Andric        if ((__flags & regex_constants::__full_match) && __s.__current_ != __last) {
48780b57cec5SDimitry Andric          __states.pop_back();
48790b57cec5SDimitry Andric          break;
48800b57cec5SDimitry Andric        }
48810b57cec5SDimitry Andric        __m.__matches_[0].first   = __first;
48825f757f3fSDimitry Andric        __m.__matches_[0].second  = std::next(__first, __s.__current_ - __first);
48830b57cec5SDimitry Andric        __m.__matches_[0].matched = true;
48840b57cec5SDimitry Andric        for (unsigned __i = 0; __i < __s.__sub_matches_.size(); ++__i)
48850b57cec5SDimitry Andric          __m.__matches_[__i + 1] = __s.__sub_matches_[__i];
48860b57cec5SDimitry Andric        return true;
48870b57cec5SDimitry Andric      case __state::__accept_and_consume:
48880b57cec5SDimitry Andric      case __state::__repeat:
48890b57cec5SDimitry Andric      case __state::__accept_but_not_consume:
48900b57cec5SDimitry Andric        break;
4891cb14a3feSDimitry Andric      case __state::__split: {
48920b57cec5SDimitry Andric        __state __snext = __s;
48930b57cec5SDimitry Andric        __s.__node_->__exec_split(true, __s);
48940b57cec5SDimitry Andric        __snext.__node_->__exec_split(false, __snext);
48955f757f3fSDimitry Andric        __states.push_back(std::move(__snext));
4896cb14a3feSDimitry Andric      } break;
48970b57cec5SDimitry Andric      case __state::__reject:
48980b57cec5SDimitry Andric        __states.pop_back();
48990b57cec5SDimitry Andric        break;
49000b57cec5SDimitry Andric      default:
49010b57cec5SDimitry Andric        __throw_regex_error<regex_constants::__re_err_unknown>();
49020b57cec5SDimitry Andric        break;
49030b57cec5SDimitry Andric      }
49040b57cec5SDimitry Andric    } while (!__states.empty());
49050b57cec5SDimitry Andric  }
49060b57cec5SDimitry Andric  return false;
49070b57cec5SDimitry Andric}
49080b57cec5SDimitry Andric
49090b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
49100b57cec5SDimitry Andrictemplate <class _Allocator>
4911cb14a3feSDimitry Andricbool basic_regex<_CharT, _Traits>::__match_at_start_posix_nosubs(
4912cb14a3feSDimitry Andric    const _CharT* __first,
4913cb14a3feSDimitry Andric    const _CharT* __last,
49140b57cec5SDimitry Andric    match_results<const _CharT*, _Allocator>& __m,
4915cb14a3feSDimitry Andric    regex_constants::match_flag_type __flags,
4916cb14a3feSDimitry Andric    bool __at_first) const {
49170b57cec5SDimitry Andric  deque<__state> __states;
49180b57cec5SDimitry Andric  ptrdiff_t __highest_j = 0;
49195f757f3fSDimitry Andric  ptrdiff_t __np        = std::distance(__first, __last);
49200b57cec5SDimitry Andric  __node* __st          = __start_.get();
4921cb14a3feSDimitry Andric  if (__st) {
49220b57cec5SDimitry Andric    __states.push_back(__state());
49230b57cec5SDimitry Andric    __states.back().__do_      = 0;
49240b57cec5SDimitry Andric    __states.back().__first_   = __first;
49250b57cec5SDimitry Andric    __states.back().__current_ = __first;
49260b57cec5SDimitry Andric    __states.back().__last_    = __last;
49270b57cec5SDimitry Andric    __states.back().__loop_data_.resize(__loop_count());
49280b57cec5SDimitry Andric    __states.back().__node_     = __st;
49290b57cec5SDimitry Andric    __states.back().__flags_    = __flags;
49300b57cec5SDimitry Andric    __states.back().__at_first_ = __at_first;
49310b57cec5SDimitry Andric    bool __matched              = false;
49320b57cec5SDimitry Andric    int __counter               = 0;
49330b57cec5SDimitry Andric    int __length                = __last - __first;
4934cb14a3feSDimitry Andric    do {
49350b57cec5SDimitry Andric      ++__counter;
4936cb14a3feSDimitry Andric      if (__counter % _LIBCPP_REGEX_COMPLEXITY_FACTOR == 0 && __counter / _LIBCPP_REGEX_COMPLEXITY_FACTOR >= __length)
49370b57cec5SDimitry Andric        __throw_regex_error<regex_constants::error_complexity>();
49380b57cec5SDimitry Andric      __state& __s = __states.back();
49390b57cec5SDimitry Andric      if (__s.__node_)
49400b57cec5SDimitry Andric        __s.__node_->__exec(__s);
4941cb14a3feSDimitry Andric      switch (__s.__do_) {
49420b57cec5SDimitry Andric      case __state::__end_state:
4943cb14a3feSDimitry Andric        if ((__flags & regex_constants::match_not_null) && __s.__current_ == __first) {
49440b57cec5SDimitry Andric          __states.pop_back();
49450b57cec5SDimitry Andric          break;
49460b57cec5SDimitry Andric        }
4947cb14a3feSDimitry Andric        if ((__flags & regex_constants::__full_match) && __s.__current_ != __last) {
49480b57cec5SDimitry Andric          __states.pop_back();
49490b57cec5SDimitry Andric          break;
49500b57cec5SDimitry Andric        }
49510b57cec5SDimitry Andric        if (!__matched || __highest_j < __s.__current_ - __s.__first_)
49520b57cec5SDimitry Andric          __highest_j = __s.__current_ - __s.__first_;
49530b57cec5SDimitry Andric        __matched = true;
495406c3fb27SDimitry Andric        if (__highest_j == __np)
49550b57cec5SDimitry Andric          __states.clear();
49560b57cec5SDimitry Andric        else
49570b57cec5SDimitry Andric          __states.pop_back();
49580b57cec5SDimitry Andric        break;
49590b57cec5SDimitry Andric      case __state::__consume_input:
49600b57cec5SDimitry Andric        break;
49610b57cec5SDimitry Andric      case __state::__accept_and_consume:
49625f757f3fSDimitry Andric        __states.push_front(std::move(__s));
49630b57cec5SDimitry Andric        __states.pop_back();
49640b57cec5SDimitry Andric        break;
49650b57cec5SDimitry Andric      case __state::__repeat:
49660b57cec5SDimitry Andric      case __state::__accept_but_not_consume:
49670b57cec5SDimitry Andric        break;
4968cb14a3feSDimitry Andric      case __state::__split: {
49690b57cec5SDimitry Andric        __state __snext = __s;
49700b57cec5SDimitry Andric        __s.__node_->__exec_split(true, __s);
49710b57cec5SDimitry Andric        __snext.__node_->__exec_split(false, __snext);
49725f757f3fSDimitry Andric        __states.push_back(std::move(__snext));
4973cb14a3feSDimitry Andric      } break;
49740b57cec5SDimitry Andric      case __state::__reject:
49750b57cec5SDimitry Andric        __states.pop_back();
49760b57cec5SDimitry Andric        break;
49770b57cec5SDimitry Andric      default:
49780b57cec5SDimitry Andric        __throw_regex_error<regex_constants::__re_err_unknown>();
49790b57cec5SDimitry Andric        break;
49800b57cec5SDimitry Andric      }
49810b57cec5SDimitry Andric    } while (!__states.empty());
4982cb14a3feSDimitry Andric    if (__matched) {
49830b57cec5SDimitry Andric      __m.__matches_[0].first   = __first;
49845f757f3fSDimitry Andric      __m.__matches_[0].second  = std::next(__first, __highest_j);
49850b57cec5SDimitry Andric      __m.__matches_[0].matched = true;
49860b57cec5SDimitry Andric      return true;
49870b57cec5SDimitry Andric    }
49880b57cec5SDimitry Andric  }
49890b57cec5SDimitry Andric  return false;
49900b57cec5SDimitry Andric}
49910b57cec5SDimitry Andric
49920b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
49930b57cec5SDimitry Andrictemplate <class _Allocator>
4994cb14a3feSDimitry Andricbool basic_regex<_CharT, _Traits>::__match_at_start_posix_subs(
4995cb14a3feSDimitry Andric    const _CharT* __first,
4996cb14a3feSDimitry Andric    const _CharT* __last,
49970b57cec5SDimitry Andric    match_results<const _CharT*, _Allocator>& __m,
4998cb14a3feSDimitry Andric    regex_constants::match_flag_type __flags,
4999cb14a3feSDimitry Andric    bool __at_first) const {
50000b57cec5SDimitry Andric  vector<__state> __states;
50010b57cec5SDimitry Andric  __state __best_state;
50020b57cec5SDimitry Andric  ptrdiff_t __highest_j = 0;
50035f757f3fSDimitry Andric  ptrdiff_t __np        = std::distance(__first, __last);
50040b57cec5SDimitry Andric  __node* __st          = __start_.get();
5005cb14a3feSDimitry Andric  if (__st) {
50060b57cec5SDimitry Andric    sub_match<const _CharT*> __unmatched;
50070b57cec5SDimitry Andric    __unmatched.first   = __last;
50080b57cec5SDimitry Andric    __unmatched.second  = __last;
50090b57cec5SDimitry Andric    __unmatched.matched = false;
50100b57cec5SDimitry Andric
50110b57cec5SDimitry Andric    __states.push_back(__state());
50120b57cec5SDimitry Andric    __states.back().__do_      = 0;
50130b57cec5SDimitry Andric    __states.back().__first_   = __first;
50140b57cec5SDimitry Andric    __states.back().__current_ = __first;
50150b57cec5SDimitry Andric    __states.back().__last_    = __last;
50160b57cec5SDimitry Andric    __states.back().__sub_matches_.resize(mark_count(), __unmatched);
50170b57cec5SDimitry Andric    __states.back().__loop_data_.resize(__loop_count());
50180b57cec5SDimitry Andric    __states.back().__node_     = __st;
50190b57cec5SDimitry Andric    __states.back().__flags_    = __flags;
50200b57cec5SDimitry Andric    __states.back().__at_first_ = __at_first;
50210b57cec5SDimitry Andric    bool __matched              = false;
50220b57cec5SDimitry Andric    int __counter               = 0;
50230b57cec5SDimitry Andric    int __length                = __last - __first;
5024cb14a3feSDimitry Andric    do {
50250b57cec5SDimitry Andric      ++__counter;
5026cb14a3feSDimitry Andric      if (__counter % _LIBCPP_REGEX_COMPLEXITY_FACTOR == 0 && __counter / _LIBCPP_REGEX_COMPLEXITY_FACTOR >= __length)
50270b57cec5SDimitry Andric        __throw_regex_error<regex_constants::error_complexity>();
50280b57cec5SDimitry Andric      __state& __s = __states.back();
50290b57cec5SDimitry Andric      if (__s.__node_)
50300b57cec5SDimitry Andric        __s.__node_->__exec(__s);
5031cb14a3feSDimitry Andric      switch (__s.__do_) {
50320b57cec5SDimitry Andric      case __state::__end_state:
5033cb14a3feSDimitry Andric        if ((__flags & regex_constants::match_not_null) && __s.__current_ == __first) {
50340b57cec5SDimitry Andric          __states.pop_back();
50350b57cec5SDimitry Andric          break;
50360b57cec5SDimitry Andric        }
5037cb14a3feSDimitry Andric        if ((__flags & regex_constants::__full_match) && __s.__current_ != __last) {
50380b57cec5SDimitry Andric          __states.pop_back();
50390b57cec5SDimitry Andric          break;
50400b57cec5SDimitry Andric        }
5041cb14a3feSDimitry Andric        if (!__matched || __highest_j < __s.__current_ - __s.__first_) {
50420b57cec5SDimitry Andric          __highest_j  = __s.__current_ - __s.__first_;
50430b57cec5SDimitry Andric          __best_state = __s;
50440b57cec5SDimitry Andric        }
50450b57cec5SDimitry Andric        __matched = true;
504606c3fb27SDimitry Andric        if (__highest_j == __np)
50470b57cec5SDimitry Andric          __states.clear();
50480b57cec5SDimitry Andric        else
50490b57cec5SDimitry Andric          __states.pop_back();
50500b57cec5SDimitry Andric        break;
50510b57cec5SDimitry Andric      case __state::__accept_and_consume:
50520b57cec5SDimitry Andric      case __state::__repeat:
50530b57cec5SDimitry Andric      case __state::__accept_but_not_consume:
50540b57cec5SDimitry Andric        break;
5055cb14a3feSDimitry Andric      case __state::__split: {
50560b57cec5SDimitry Andric        __state __snext = __s;
50570b57cec5SDimitry Andric        __s.__node_->__exec_split(true, __s);
50580b57cec5SDimitry Andric        __snext.__node_->__exec_split(false, __snext);
50595f757f3fSDimitry Andric        __states.push_back(std::move(__snext));
5060cb14a3feSDimitry Andric      } break;
50610b57cec5SDimitry Andric      case __state::__reject:
50620b57cec5SDimitry Andric        __states.pop_back();
50630b57cec5SDimitry Andric        break;
50640b57cec5SDimitry Andric      default:
50650b57cec5SDimitry Andric        __throw_regex_error<regex_constants::__re_err_unknown>();
50660b57cec5SDimitry Andric        break;
50670b57cec5SDimitry Andric      }
50680b57cec5SDimitry Andric    } while (!__states.empty());
5069cb14a3feSDimitry Andric    if (__matched) {
50700b57cec5SDimitry Andric      __m.__matches_[0].first   = __first;
50715f757f3fSDimitry Andric      __m.__matches_[0].second  = std::next(__first, __highest_j);
50720b57cec5SDimitry Andric      __m.__matches_[0].matched = true;
50730b57cec5SDimitry Andric      for (unsigned __i = 0; __i < __best_state.__sub_matches_.size(); ++__i)
50740b57cec5SDimitry Andric        __m.__matches_[__i + 1] = __best_state.__sub_matches_[__i];
50750b57cec5SDimitry Andric      return true;
50760b57cec5SDimitry Andric    }
50770b57cec5SDimitry Andric  }
50780b57cec5SDimitry Andric  return false;
50790b57cec5SDimitry Andric}
50800b57cec5SDimitry Andric
50810b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
50820b57cec5SDimitry Andrictemplate <class _Allocator>
5083cb14a3feSDimitry Andricbool basic_regex<_CharT, _Traits>::__match_at_start(
5084cb14a3feSDimitry Andric    const _CharT* __first,
5085cb14a3feSDimitry Andric    const _CharT* __last,
50860b57cec5SDimitry Andric    match_results<const _CharT*, _Allocator>& __m,
5087cb14a3feSDimitry Andric    regex_constants::match_flag_type __flags,
5088cb14a3feSDimitry Andric    bool __at_first) const {
50890b57cec5SDimitry Andric  if (__get_grammar(__flags_) == ECMAScript)
50900b57cec5SDimitry Andric    return __match_at_start_ecma(__first, __last, __m, __flags, __at_first);
50910b57cec5SDimitry Andric  if (mark_count() == 0)
50920b57cec5SDimitry Andric    return __match_at_start_posix_nosubs(__first, __last, __m, __flags, __at_first);
50930b57cec5SDimitry Andric  return __match_at_start_posix_subs(__first, __last, __m, __flags, __at_first);
50940b57cec5SDimitry Andric}
50950b57cec5SDimitry Andric
50960b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
50970b57cec5SDimitry Andrictemplate <class _Allocator>
5098cb14a3feSDimitry Andricbool basic_regex<_CharT, _Traits>::__search(
5099cb14a3feSDimitry Andric    const _CharT* __first,
5100cb14a3feSDimitry Andric    const _CharT* __last,
51010b57cec5SDimitry Andric    match_results<const _CharT*, _Allocator>& __m,
5102cb14a3feSDimitry Andric    regex_constants::match_flag_type __flags) const {
51035ffd83dbSDimitry Andric  if (__flags & regex_constants::match_prev_avail)
51045ffd83dbSDimitry Andric    __flags &= ~(regex_constants::match_not_bol | regex_constants::match_not_bow);
51055ffd83dbSDimitry Andric
5106cb14a3feSDimitry Andric  __m.__init(1 + mark_count(), __first, __last, __flags & regex_constants::__no_update_pos);
5107cb14a3feSDimitry Andric  if (__match_at_start(__first, __last, __m, __flags, !(__flags & regex_constants::__no_update_pos))) {
51080b57cec5SDimitry Andric    __m.__prefix_.second  = __m[0].first;
51090b57cec5SDimitry Andric    __m.__prefix_.matched = __m.__prefix_.first != __m.__prefix_.second;
51100b57cec5SDimitry Andric    __m.__suffix_.first   = __m[0].second;
51110b57cec5SDimitry Andric    __m.__suffix_.matched = __m.__suffix_.first != __m.__suffix_.second;
51120b57cec5SDimitry Andric    return true;
51130b57cec5SDimitry Andric  }
5114cb14a3feSDimitry Andric  if (__first != __last && !(__flags & regex_constants::match_continuous)) {
51150b57cec5SDimitry Andric    __flags |= regex_constants::match_prev_avail;
5116cb14a3feSDimitry Andric    for (++__first; __first != __last; ++__first) {
51170b57cec5SDimitry Andric      __m.__matches_.assign(__m.size(), __m.__unmatched_);
5118cb14a3feSDimitry Andric      if (__match_at_start(__first, __last, __m, __flags, false)) {
51190b57cec5SDimitry Andric        __m.__prefix_.second  = __m[0].first;
51200b57cec5SDimitry Andric        __m.__prefix_.matched = __m.__prefix_.first != __m.__prefix_.second;
51210b57cec5SDimitry Andric        __m.__suffix_.first   = __m[0].second;
51220b57cec5SDimitry Andric        __m.__suffix_.matched = __m.__suffix_.first != __m.__suffix_.second;
51230b57cec5SDimitry Andric        return true;
51240b57cec5SDimitry Andric      }
51250b57cec5SDimitry Andric      __m.__matches_.assign(__m.size(), __m.__unmatched_);
51260b57cec5SDimitry Andric    }
5127*7a6dacacSDimitry Andric    __m.__matches_.assign(__m.size(), __m.__unmatched_);
5128*7a6dacacSDimitry Andric    if (__match_at_start(__first, __last, __m, __flags, false)) {
5129*7a6dacacSDimitry Andric      __m.__prefix_.second  = __m[0].first;
5130*7a6dacacSDimitry Andric      __m.__prefix_.matched = __m.__prefix_.first != __m.__prefix_.second;
5131*7a6dacacSDimitry Andric      __m.__suffix_.first   = __m[0].second;
5132*7a6dacacSDimitry Andric      __m.__suffix_.matched = __m.__suffix_.first != __m.__suffix_.second;
5133*7a6dacacSDimitry Andric      return true;
5134*7a6dacacSDimitry Andric    }
51350b57cec5SDimitry Andric  }
51360b57cec5SDimitry Andric  __m.__matches_.clear();
51370b57cec5SDimitry Andric  return false;
51380b57cec5SDimitry Andric}
51390b57cec5SDimitry Andric
51400b57cec5SDimitry Andrictemplate <class _BidirectionalIterator, class _Allocator, class _CharT, class _Traits>
5141cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool
5142cb14a3feSDimitry Andricregex_search(_BidirectionalIterator __first,
5143cb14a3feSDimitry Andric             _BidirectionalIterator __last,
51440b57cec5SDimitry Andric             match_results<_BidirectionalIterator, _Allocator>& __m,
51450b57cec5SDimitry Andric             const basic_regex<_CharT, _Traits>& __e,
5146cb14a3feSDimitry Andric             regex_constants::match_flag_type __flags = regex_constants::match_default) {
51470b57cec5SDimitry Andric  int __offset = (__flags & regex_constants::match_prev_avail) ? 1 : 0;
51485f757f3fSDimitry Andric  basic_string<_CharT> __s(std::prev(__first, __offset), __last);
51490b57cec5SDimitry Andric  match_results<const _CharT*> __mc;
51500b57cec5SDimitry Andric  bool __r = __e.__search(__s.data() + __offset, __s.data() + __s.size(), __mc, __flags);
51510b57cec5SDimitry Andric  __m.__assign(__first, __last, __mc, __flags & regex_constants::__no_update_pos);
51520b57cec5SDimitry Andric  return __r;
51530b57cec5SDimitry Andric}
51540b57cec5SDimitry Andric
51550b57cec5SDimitry Andrictemplate <class _Iter, class _Allocator, class _CharT, class _Traits>
5156cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool
51570b57cec5SDimitry Andricregex_search(__wrap_iter<_Iter> __first,
51580b57cec5SDimitry Andric             __wrap_iter<_Iter> __last,
51590b57cec5SDimitry Andric             match_results<__wrap_iter<_Iter>, _Allocator>& __m,
51600b57cec5SDimitry Andric             const basic_regex<_CharT, _Traits>& __e,
5161cb14a3feSDimitry Andric             regex_constants::match_flag_type __flags = regex_constants::match_default) {
51620b57cec5SDimitry Andric  match_results<const _CharT*> __mc;
51630b57cec5SDimitry Andric  bool __r = __e.__search(__first.base(), __last.base(), __mc, __flags);
51640b57cec5SDimitry Andric  __m.__assign(__first, __last, __mc, __flags & regex_constants::__no_update_pos);
51650b57cec5SDimitry Andric  return __r;
51660b57cec5SDimitry Andric}
51670b57cec5SDimitry Andric
51680b57cec5SDimitry Andrictemplate <class _Allocator, class _CharT, class _Traits>
5169cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool
5170cb14a3feSDimitry Andricregex_search(const _CharT* __first,
5171cb14a3feSDimitry Andric             const _CharT* __last,
51720b57cec5SDimitry Andric             match_results<const _CharT*, _Allocator>& __m,
51730b57cec5SDimitry Andric             const basic_regex<_CharT, _Traits>& __e,
5174cb14a3feSDimitry Andric             regex_constants::match_flag_type __flags = regex_constants::match_default) {
51750b57cec5SDimitry Andric  return __e.__search(__first, __last, __m, __flags);
51760b57cec5SDimitry Andric}
51770b57cec5SDimitry Andric
51780b57cec5SDimitry Andrictemplate <class _BidirectionalIterator, class _CharT, class _Traits>
5179cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool
5180cb14a3feSDimitry Andricregex_search(_BidirectionalIterator __first,
5181cb14a3feSDimitry Andric             _BidirectionalIterator __last,
51820b57cec5SDimitry Andric             const basic_regex<_CharT, _Traits>& __e,
5183cb14a3feSDimitry Andric             regex_constants::match_flag_type __flags = regex_constants::match_default) {
51840b57cec5SDimitry Andric  basic_string<_CharT> __s(__first, __last);
51850b57cec5SDimitry Andric  match_results<const _CharT*> __mc;
51860b57cec5SDimitry Andric  return __e.__search(__s.data(), __s.data() + __s.size(), __mc, __flags);
51870b57cec5SDimitry Andric}
51880b57cec5SDimitry Andric
51890b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
5190cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool
5191cb14a3feSDimitry Andricregex_search(const _CharT* __first,
5192cb14a3feSDimitry Andric             const _CharT* __last,
51930b57cec5SDimitry Andric             const basic_regex<_CharT, _Traits>& __e,
5194cb14a3feSDimitry Andric             regex_constants::match_flag_type __flags = regex_constants::match_default) {
51950b57cec5SDimitry Andric  match_results<const _CharT*> __mc;
51960b57cec5SDimitry Andric  return __e.__search(__first, __last, __mc, __flags);
51970b57cec5SDimitry Andric}
51980b57cec5SDimitry Andric
51990b57cec5SDimitry Andrictemplate <class _CharT, class _Allocator, class _Traits>
5200cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool
5201cb14a3feSDimitry Andricregex_search(const _CharT* __str,
5202cb14a3feSDimitry Andric             match_results<const _CharT*, _Allocator>& __m,
52030b57cec5SDimitry Andric             const basic_regex<_CharT, _Traits>& __e,
5204cb14a3feSDimitry Andric             regex_constants::match_flag_type __flags = regex_constants::match_default) {
52050b57cec5SDimitry Andric  return __e.__search(__str, __str + _Traits::length(__str), __m, __flags);
52060b57cec5SDimitry Andric}
52070b57cec5SDimitry Andric
52080b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
5209cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool
5210cb14a3feSDimitry Andricregex_search(const _CharT* __str,
5211cb14a3feSDimitry Andric             const basic_regex<_CharT, _Traits>& __e,
5212cb14a3feSDimitry Andric             regex_constants::match_flag_type __flags = regex_constants::match_default) {
52130b57cec5SDimitry Andric  match_results<const _CharT*> __m;
52145f757f3fSDimitry Andric  return std::regex_search(__str, __m, __e, __flags);
52150b57cec5SDimitry Andric}
52160b57cec5SDimitry Andric
52170b57cec5SDimitry Andrictemplate <class _ST, class _SA, class _CharT, class _Traits>
5218cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool
52190b57cec5SDimitry Andricregex_search(const basic_string<_CharT, _ST, _SA>& __s,
52200b57cec5SDimitry Andric             const basic_regex<_CharT, _Traits>& __e,
5221cb14a3feSDimitry Andric             regex_constants::match_flag_type __flags = regex_constants::match_default) {
52220b57cec5SDimitry Andric  match_results<const _CharT*> __mc;
52230b57cec5SDimitry Andric  return __e.__search(__s.data(), __s.data() + __s.size(), __mc, __flags);
52240b57cec5SDimitry Andric}
52250b57cec5SDimitry Andric
52260b57cec5SDimitry Andrictemplate <class _ST, class _SA, class _Allocator, class _CharT, class _Traits>
5227cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool
52280b57cec5SDimitry Andricregex_search(const basic_string<_CharT, _ST, _SA>& __s,
52290b57cec5SDimitry Andric             match_results<typename basic_string<_CharT, _ST, _SA>::const_iterator, _Allocator>& __m,
52300b57cec5SDimitry Andric             const basic_regex<_CharT, _Traits>& __e,
5231cb14a3feSDimitry Andric             regex_constants::match_flag_type __flags = regex_constants::match_default) {
52320b57cec5SDimitry Andric  match_results<const _CharT*> __mc;
52330b57cec5SDimitry Andric  bool __r = __e.__search(__s.data(), __s.data() + __s.size(), __mc, __flags);
52340b57cec5SDimitry Andric  __m.__assign(__s.begin(), __s.end(), __mc, __flags & regex_constants::__no_update_pos);
52350b57cec5SDimitry Andric  return __r;
52360b57cec5SDimitry Andric}
52370b57cec5SDimitry Andric
523806c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 14
52390b57cec5SDimitry Andrictemplate <class _ST, class _SA, class _Ap, class _Cp, class _Tp>
5240cb14a3feSDimitry Andricbool regex_search(const basic_string<_Cp, _ST, _SA>&& __s,
52410b57cec5SDimitry Andric                  match_results<typename basic_string<_Cp, _ST, _SA>::const_iterator, _Ap>&,
52420b57cec5SDimitry Andric                  const basic_regex<_Cp, _Tp>& __e,
52430b57cec5SDimitry Andric                  regex_constants::match_flag_type __flags = regex_constants::match_default) = delete;
52440b57cec5SDimitry Andric#endif
52450b57cec5SDimitry Andric
52460b57cec5SDimitry Andric// regex_match
52470b57cec5SDimitry Andric
52480b57cec5SDimitry Andrictemplate <class _BidirectionalIterator, class _Allocator, class _CharT, class _Traits>
5249bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI bool
5250cb14a3feSDimitry Andricregex_match(_BidirectionalIterator __first,
5251cb14a3feSDimitry Andric            _BidirectionalIterator __last,
52520b57cec5SDimitry Andric            match_results<_BidirectionalIterator, _Allocator>& __m,
52530b57cec5SDimitry Andric            const basic_regex<_CharT, _Traits>& __e,
5254cb14a3feSDimitry Andric            regex_constants::match_flag_type __flags = regex_constants::match_default) {
52555f757f3fSDimitry Andric  bool __r = std::regex_search(
5256cb14a3feSDimitry Andric      __first, __last, __m, __e, __flags | regex_constants::match_continuous | regex_constants::__full_match);
5257cb14a3feSDimitry Andric  if (__r) {
52580b57cec5SDimitry Andric    __r = !__m.suffix().matched;
52590b57cec5SDimitry Andric    if (!__r)
52600b57cec5SDimitry Andric      __m.__matches_.clear();
52610b57cec5SDimitry Andric  }
52620b57cec5SDimitry Andric  return __r;
52630b57cec5SDimitry Andric}
52640b57cec5SDimitry Andric
52650b57cec5SDimitry Andrictemplate <class _BidirectionalIterator, class _CharT, class _Traits>
5266cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool
5267cb14a3feSDimitry Andricregex_match(_BidirectionalIterator __first,
5268cb14a3feSDimitry Andric            _BidirectionalIterator __last,
52690b57cec5SDimitry Andric            const basic_regex<_CharT, _Traits>& __e,
5270cb14a3feSDimitry Andric            regex_constants::match_flag_type __flags = regex_constants::match_default) {
52710b57cec5SDimitry Andric  match_results<_BidirectionalIterator> __m;
52725f757f3fSDimitry Andric  return std::regex_match(__first, __last, __m, __e, __flags);
52730b57cec5SDimitry Andric}
52740b57cec5SDimitry Andric
52750b57cec5SDimitry Andrictemplate <class _CharT, class _Allocator, class _Traits>
5276cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool
5277cb14a3feSDimitry Andricregex_match(const _CharT* __str,
5278cb14a3feSDimitry Andric            match_results<const _CharT*, _Allocator>& __m,
52790b57cec5SDimitry Andric            const basic_regex<_CharT, _Traits>& __e,
5280cb14a3feSDimitry Andric            regex_constants::match_flag_type __flags = regex_constants::match_default) {
52815f757f3fSDimitry Andric  return std::regex_match(__str, __str + _Traits::length(__str), __m, __e, __flags);
52820b57cec5SDimitry Andric}
52830b57cec5SDimitry Andric
52840b57cec5SDimitry Andrictemplate <class _ST, class _SA, class _Allocator, class _CharT, class _Traits>
5285cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool
52860b57cec5SDimitry Andricregex_match(const basic_string<_CharT, _ST, _SA>& __s,
52870b57cec5SDimitry Andric            match_results<typename basic_string<_CharT, _ST, _SA>::const_iterator, _Allocator>& __m,
52880b57cec5SDimitry Andric            const basic_regex<_CharT, _Traits>& __e,
5289cb14a3feSDimitry Andric            regex_constants::match_flag_type __flags = regex_constants::match_default) {
52905f757f3fSDimitry Andric  return std::regex_match(__s.begin(), __s.end(), __m, __e, __flags);
52910b57cec5SDimitry Andric}
52920b57cec5SDimitry Andric
529306c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 14
52940b57cec5SDimitry Andrictemplate <class _ST, class _SA, class _Allocator, class _CharT, class _Traits>
5295cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool
52960b57cec5SDimitry Andricregex_match(const basic_string<_CharT, _ST, _SA>&& __s,
52970b57cec5SDimitry Andric            match_results<typename basic_string<_CharT, _ST, _SA>::const_iterator, _Allocator>& __m,
52980b57cec5SDimitry Andric            const basic_regex<_CharT, _Traits>& __e,
52990b57cec5SDimitry Andric            regex_constants::match_flag_type __flags = regex_constants::match_default) = delete;
53000b57cec5SDimitry Andric#endif
53010b57cec5SDimitry Andric
53020b57cec5SDimitry Andrictemplate <class _CharT, class _Traits>
5303cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool
5304cb14a3feSDimitry Andricregex_match(const _CharT* __str,
5305cb14a3feSDimitry Andric            const basic_regex<_CharT, _Traits>& __e,
5306cb14a3feSDimitry Andric            regex_constants::match_flag_type __flags = regex_constants::match_default) {
53075f757f3fSDimitry Andric  return std::regex_match(__str, __str + _Traits::length(__str), __e, __flags);
53080b57cec5SDimitry Andric}
53090b57cec5SDimitry Andric
53100b57cec5SDimitry Andrictemplate <class _ST, class _SA, class _CharT, class _Traits>
5311cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool
53120b57cec5SDimitry Andricregex_match(const basic_string<_CharT, _ST, _SA>& __s,
53130b57cec5SDimitry Andric            const basic_regex<_CharT, _Traits>& __e,
5314cb14a3feSDimitry Andric            regex_constants::match_flag_type __flags = regex_constants::match_default) {
53155f757f3fSDimitry Andric  return std::regex_match(__s.begin(), __s.end(), __e, __flags);
53160b57cec5SDimitry Andric}
53170b57cec5SDimitry Andric
53180b57cec5SDimitry Andric// regex_iterator
53190b57cec5SDimitry Andric
53200b57cec5SDimitry Andrictemplate <class _BidirectionalIterator,
53210b57cec5SDimitry Andric          class _CharT  = typename iterator_traits<_BidirectionalIterator>::value_type,
53220b57cec5SDimitry Andric          class _Traits = regex_traits<_CharT> >
5323e8d8bef9SDimitry Andricclass _LIBCPP_TEMPLATE_VIS regex_iterator;
5324e8d8bef9SDimitry Andric
5325e8d8bef9SDimitry Andrictypedef regex_iterator<const char*> cregex_iterator;
5326e8d8bef9SDimitry Andrictypedef regex_iterator<string::const_iterator> sregex_iterator;
5327349cc55cSDimitry Andric#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
5328349cc55cSDimitry Andrictypedef regex_iterator<const wchar_t*> wcregex_iterator;
5329e8d8bef9SDimitry Andrictypedef regex_iterator<wstring::const_iterator> wsregex_iterator;
5330349cc55cSDimitry Andric#endif
5331e8d8bef9SDimitry Andric
5332e8d8bef9SDimitry Andrictemplate <class _BidirectionalIterator, class _CharT, class _Traits>
5333cb14a3feSDimitry Andricclass _LIBCPP_TEMPLATE_VIS _LIBCPP_PREFERRED_NAME(cregex_iterator)
5334cb14a3feSDimitry Andric    _LIBCPP_IF_WIDE_CHARACTERS(_LIBCPP_PREFERRED_NAME(wcregex_iterator)) _LIBCPP_PREFERRED_NAME(sregex_iterator)
5335cb14a3feSDimitry Andric        _LIBCPP_IF_WIDE_CHARACTERS(_LIBCPP_PREFERRED_NAME(wsregex_iterator)) regex_iterator {
53360b57cec5SDimitry Andricpublic:
53370b57cec5SDimitry Andric  typedef basic_regex<_CharT, _Traits> regex_type;
53380b57cec5SDimitry Andric  typedef match_results<_BidirectionalIterator> value_type;
53390b57cec5SDimitry Andric  typedef ptrdiff_t difference_type;
53400b57cec5SDimitry Andric  typedef const value_type* pointer;
53410b57cec5SDimitry Andric  typedef const value_type& reference;
53420b57cec5SDimitry Andric  typedef forward_iterator_tag iterator_category;
53435f757f3fSDimitry Andric#if _LIBCPP_STD_VER >= 20
53445f757f3fSDimitry Andric  typedef input_iterator_tag iterator_concept;
53455f757f3fSDimitry Andric#endif
53460b57cec5SDimitry Andric
53470b57cec5SDimitry Andricprivate:
53480b57cec5SDimitry Andric  _BidirectionalIterator __begin_;
53490b57cec5SDimitry Andric  _BidirectionalIterator __end_;
53500b57cec5SDimitry Andric  const regex_type* __pregex_;
53510b57cec5SDimitry Andric  regex_constants::match_flag_type __flags_;
53520b57cec5SDimitry Andric  value_type __match_;
53530b57cec5SDimitry Andric
53540b57cec5SDimitry Andricpublic:
53550b57cec5SDimitry Andric  regex_iterator();
5356cb14a3feSDimitry Andric  regex_iterator(_BidirectionalIterator __a,
5357cb14a3feSDimitry Andric                 _BidirectionalIterator __b,
53580b57cec5SDimitry Andric                 const regex_type& __re,
5359cb14a3feSDimitry Andric                 regex_constants::match_flag_type __m = regex_constants::match_default);
536006c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 14
5361cb14a3feSDimitry Andric  regex_iterator(_BidirectionalIterator __a,
5362cb14a3feSDimitry Andric                 _BidirectionalIterator __b,
53630b57cec5SDimitry Andric                 const regex_type&& __re,
5364cb14a3feSDimitry Andric                 regex_constants::match_flag_type __m = regex_constants::match_default) = delete;
53650b57cec5SDimitry Andric#endif
53660b57cec5SDimitry Andric
536706c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI bool operator==(const regex_iterator& __x) const;
536806c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 20
536906c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI bool operator==(default_sentinel_t) const { return *this == regex_iterator(); }
537006c3fb27SDimitry Andric#endif
537106c3fb27SDimitry Andric#if _LIBCPP_STD_VER < 20
5372cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI bool operator!=(const regex_iterator& __x) const { return !(*this == __x); }
537306c3fb27SDimitry Andric#endif
53740b57cec5SDimitry Andric
5375cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI reference operator*() const { return __match_; }
5376cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI pointer operator->() const { return std::addressof(__match_); }
53770b57cec5SDimitry Andric
53780b57cec5SDimitry Andric  regex_iterator& operator++();
5379cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI regex_iterator operator++(int) {
53800b57cec5SDimitry Andric    regex_iterator __t(*this);
53810b57cec5SDimitry Andric    ++(*this);
53820b57cec5SDimitry Andric    return __t;
53830b57cec5SDimitry Andric  }
53840b57cec5SDimitry Andric};
53850b57cec5SDimitry Andric
53860b57cec5SDimitry Andrictemplate <class _BidirectionalIterator, class _CharT, class _Traits>
53870b57cec5SDimitry Andricregex_iterator<_BidirectionalIterator, _CharT, _Traits>::regex_iterator()
5388cb14a3feSDimitry Andric    : __begin_(), __end_(), __pregex_(nullptr), __flags_(), __match_() {}
53890b57cec5SDimitry Andric
53900b57cec5SDimitry Andrictemplate <class _BidirectionalIterator, class _CharT, class _Traits>
5391cb14a3feSDimitry Andricregex_iterator<_BidirectionalIterator, _CharT, _Traits>::regex_iterator(
5392cb14a3feSDimitry Andric    _BidirectionalIterator __a,
5393cb14a3feSDimitry Andric    _BidirectionalIterator __b,
5394cb14a3feSDimitry Andric    const regex_type& __re,
5395cb14a3feSDimitry Andric    regex_constants::match_flag_type __m)
5396cb14a3feSDimitry Andric    : __begin_(__a), __end_(__b), __pregex_(std::addressof(__re)), __flags_(__m) {
53975f757f3fSDimitry Andric  std::regex_search(__begin_, __end_, __match_, *__pregex_, __flags_);
53980b57cec5SDimitry Andric}
53990b57cec5SDimitry Andric
54000b57cec5SDimitry Andrictemplate <class _BidirectionalIterator, class _CharT, class _Traits>
5401cb14a3feSDimitry Andricbool regex_iterator<_BidirectionalIterator, _CharT, _Traits>::operator==(const regex_iterator& __x) const {
54020b57cec5SDimitry Andric  if (__match_.empty() && __x.__match_.empty())
54030b57cec5SDimitry Andric    return true;
54040b57cec5SDimitry Andric  if (__match_.empty() || __x.__match_.empty())
54050b57cec5SDimitry Andric    return false;
5406cb14a3feSDimitry Andric  return __begin_ == __x.__begin_ && __end_ == __x.__end_ && __pregex_ == __x.__pregex_ && __flags_ == __x.__flags_ &&
54070b57cec5SDimitry Andric         __match_[0] == __x.__match_[0];
54080b57cec5SDimitry Andric}
54090b57cec5SDimitry Andric
54100b57cec5SDimitry Andrictemplate <class _BidirectionalIterator, class _CharT, class _Traits>
54110b57cec5SDimitry Andricregex_iterator<_BidirectionalIterator, _CharT, _Traits>&
5412cb14a3feSDimitry Andricregex_iterator<_BidirectionalIterator, _CharT, _Traits>::operator++() {
54130b57cec5SDimitry Andric  __flags_ |= regex_constants::__no_update_pos;
54140b57cec5SDimitry Andric  _BidirectionalIterator __start = __match_[0].second;
5415cb14a3feSDimitry Andric  if (__match_[0].first == __match_[0].second) {
5416cb14a3feSDimitry Andric    if (__start == __end_) {
54170b57cec5SDimitry Andric      __match_ = value_type();
54180b57cec5SDimitry Andric      return *this;
5419cb14a3feSDimitry Andric    } else if (std::regex_search(__start,
5420cb14a3feSDimitry Andric                                 __end_,
5421cb14a3feSDimitry Andric                                 __match_,
5422cb14a3feSDimitry Andric                                 *__pregex_,
5423cb14a3feSDimitry Andric                                 __flags_ | regex_constants::match_not_null | regex_constants::match_continuous))
54240b57cec5SDimitry Andric      return *this;
54250b57cec5SDimitry Andric    else
54260b57cec5SDimitry Andric      ++__start;
54270b57cec5SDimitry Andric  }
54280b57cec5SDimitry Andric  __flags_ |= regex_constants::match_prev_avail;
54295f757f3fSDimitry Andric  if (!std::regex_search(__start, __end_, __match_, *__pregex_, __flags_))
54300b57cec5SDimitry Andric    __match_ = value_type();
54310b57cec5SDimitry Andric  return *this;
54320b57cec5SDimitry Andric}
54330b57cec5SDimitry Andric
54340b57cec5SDimitry Andric// regex_token_iterator
54350b57cec5SDimitry Andric
54360b57cec5SDimitry Andrictemplate <class _BidirectionalIterator,
54370b57cec5SDimitry Andric          class _CharT  = typename iterator_traits<_BidirectionalIterator>::value_type,
54380b57cec5SDimitry Andric          class _Traits = regex_traits<_CharT> >
5439e8d8bef9SDimitry Andricclass _LIBCPP_TEMPLATE_VIS regex_token_iterator;
5440e8d8bef9SDimitry Andric
5441e8d8bef9SDimitry Andrictypedef regex_token_iterator<const char*> cregex_token_iterator;
5442e8d8bef9SDimitry Andrictypedef regex_token_iterator<string::const_iterator> sregex_token_iterator;
5443349cc55cSDimitry Andric#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
5444349cc55cSDimitry Andrictypedef regex_token_iterator<const wchar_t*> wcregex_token_iterator;
5445e8d8bef9SDimitry Andrictypedef regex_token_iterator<wstring::const_iterator> wsregex_token_iterator;
5446349cc55cSDimitry Andric#endif
5447e8d8bef9SDimitry Andric
5448e8d8bef9SDimitry Andrictemplate <class _BidirectionalIterator, class _CharT, class _Traits>
5449cb14a3feSDimitry Andricclass _LIBCPP_TEMPLATE_VIS _LIBCPP_PREFERRED_NAME(cregex_token_iterator)
5450349cc55cSDimitry Andric    _LIBCPP_IF_WIDE_CHARACTERS(_LIBCPP_PREFERRED_NAME(wcregex_token_iterator))
5451e8d8bef9SDimitry Andric        _LIBCPP_PREFERRED_NAME(sregex_token_iterator)
5452cb14a3feSDimitry Andric            _LIBCPP_IF_WIDE_CHARACTERS(_LIBCPP_PREFERRED_NAME(wsregex_token_iterator)) regex_token_iterator {
54530b57cec5SDimitry Andricpublic:
54540b57cec5SDimitry Andric  typedef basic_regex<_CharT, _Traits> regex_type;
54550b57cec5SDimitry Andric  typedef sub_match<_BidirectionalIterator> value_type;
54560b57cec5SDimitry Andric  typedef ptrdiff_t difference_type;
54570b57cec5SDimitry Andric  typedef const value_type* pointer;
54580b57cec5SDimitry Andric  typedef const value_type& reference;
54590b57cec5SDimitry Andric  typedef forward_iterator_tag iterator_category;
54605f757f3fSDimitry Andric#if _LIBCPP_STD_VER >= 20
54615f757f3fSDimitry Andric  typedef input_iterator_tag iterator_concept;
54625f757f3fSDimitry Andric#endif
54630b57cec5SDimitry Andric
54640b57cec5SDimitry Andricprivate:
54650b57cec5SDimitry Andric  typedef regex_iterator<_BidirectionalIterator, _CharT, _Traits> _Position;
54660b57cec5SDimitry Andric
54670b57cec5SDimitry Andric  _Position __position_;
54680b57cec5SDimitry Andric  const value_type* __result_;
54690b57cec5SDimitry Andric  value_type __suffix_;
54700b57cec5SDimitry Andric  ptrdiff_t __n_;
54710b57cec5SDimitry Andric  vector<int> __subs_;
54720b57cec5SDimitry Andric
54730b57cec5SDimitry Andricpublic:
54740b57cec5SDimitry Andric  regex_token_iterator();
5475cb14a3feSDimitry Andric  regex_token_iterator(_BidirectionalIterator __a,
5476cb14a3feSDimitry Andric                       _BidirectionalIterator __b,
5477cb14a3feSDimitry Andric                       const regex_type& __re,
5478cb14a3feSDimitry Andric                       int __submatch                       = 0,
5479cb14a3feSDimitry Andric                       regex_constants::match_flag_type __m = regex_constants::match_default);
548006c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 14
5481cb14a3feSDimitry Andric  regex_token_iterator(_BidirectionalIterator __a,
5482cb14a3feSDimitry Andric                       _BidirectionalIterator __b,
5483cb14a3feSDimitry Andric                       const regex_type&& __re,
5484cb14a3feSDimitry Andric                       int __submatch                       = 0,
5485cb14a3feSDimitry Andric                       regex_constants::match_flag_type __m = regex_constants::match_default) = delete;
54860b57cec5SDimitry Andric#endif
54870b57cec5SDimitry Andric
5488cb14a3feSDimitry Andric  regex_token_iterator(_BidirectionalIterator __a,
5489cb14a3feSDimitry Andric                       _BidirectionalIterator __b,
5490cb14a3feSDimitry Andric                       const regex_type& __re,
5491cb14a3feSDimitry Andric                       const vector<int>& __submatches,
5492cb14a3feSDimitry Andric                       regex_constants::match_flag_type __m = regex_constants::match_default);
549306c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 14
5494cb14a3feSDimitry Andric  regex_token_iterator(_BidirectionalIterator __a,
5495cb14a3feSDimitry Andric                       _BidirectionalIterator __b,
5496cb14a3feSDimitry Andric                       const regex_type&& __re,
5497cb14a3feSDimitry Andric                       const vector<int>& __submatches,
5498cb14a3feSDimitry Andric                       regex_constants::match_flag_type __m = regex_constants::match_default) = delete;
54990b57cec5SDimitry Andric#endif
55000b57cec5SDimitry Andric
55010b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
5502cb14a3feSDimitry Andric  regex_token_iterator(_BidirectionalIterator __a,
5503cb14a3feSDimitry Andric                       _BidirectionalIterator __b,
55040b57cec5SDimitry Andric                       const regex_type& __re,
55050b57cec5SDimitry Andric                       initializer_list<int> __submatches,
5506cb14a3feSDimitry Andric                       regex_constants::match_flag_type __m = regex_constants::match_default);
55070b57cec5SDimitry Andric
550806c3fb27SDimitry Andric#  if _LIBCPP_STD_VER >= 14
5509cb14a3feSDimitry Andric  regex_token_iterator(_BidirectionalIterator __a,
5510cb14a3feSDimitry Andric                       _BidirectionalIterator __b,
55110b57cec5SDimitry Andric                       const regex_type&& __re,
55120b57cec5SDimitry Andric                       initializer_list<int> __submatches,
5513cb14a3feSDimitry Andric                       regex_constants::match_flag_type __m = regex_constants::match_default) = delete;
55140b57cec5SDimitry Andric#  endif
55150b57cec5SDimitry Andric#endif // _LIBCPP_CXX03_LANG
55160b57cec5SDimitry Andric  template <size_t _Np>
55170b57cec5SDimitry Andric  regex_token_iterator(_BidirectionalIterator __a,
55180b57cec5SDimitry Andric                       _BidirectionalIterator __b,
55190b57cec5SDimitry Andric                       const regex_type& __re,
55200b57cec5SDimitry Andric                       const int (&__submatches)[_Np],
5521cb14a3feSDimitry Andric                       regex_constants::match_flag_type __m = regex_constants::match_default);
552206c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 14
5523e8d8bef9SDimitry Andric  template <size_t _Np>
55240b57cec5SDimitry Andric  regex_token_iterator(_BidirectionalIterator __a,
55250b57cec5SDimitry Andric                       _BidirectionalIterator __b,
55260b57cec5SDimitry Andric                       const regex_type&& __re,
55270b57cec5SDimitry Andric                       const int (&__submatches)[_Np],
5528cb14a3feSDimitry Andric                       regex_constants::match_flag_type __m = regex_constants::match_default) = delete;
55290b57cec5SDimitry Andric#endif
55300b57cec5SDimitry Andric
55310b57cec5SDimitry Andric  regex_token_iterator(const regex_token_iterator&);
55320b57cec5SDimitry Andric  regex_token_iterator& operator=(const regex_token_iterator&);
55330b57cec5SDimitry Andric
553406c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI bool operator==(const regex_token_iterator& __x) const;
553506c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 20
553606c3fb27SDimitry Andric  _LIBCPP_HIDE_FROM_ABI _LIBCPP_HIDE_FROM_ABI bool operator==(default_sentinel_t) const {
553706c3fb27SDimitry Andric    return *this == regex_token_iterator();
553806c3fb27SDimitry Andric  }
553906c3fb27SDimitry Andric#endif
554006c3fb27SDimitry Andric#if _LIBCPP_STD_VER < 20
5541cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI bool operator!=(const regex_token_iterator& __x) const { return !(*this == __x); }
554206c3fb27SDimitry Andric#endif
55430b57cec5SDimitry Andric
5544cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI const value_type& operator*() const { return *__result_; }
5545cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI const value_type* operator->() const { return __result_; }
55460b57cec5SDimitry Andric
55470b57cec5SDimitry Andric  regex_token_iterator& operator++();
5548cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI regex_token_iterator operator++(int) {
55490b57cec5SDimitry Andric    regex_token_iterator __t(*this);
55500b57cec5SDimitry Andric    ++(*this);
55510b57cec5SDimitry Andric    return __t;
55520b57cec5SDimitry Andric  }
55530b57cec5SDimitry Andric
55540b57cec5SDimitry Andricprivate:
55550b57cec5SDimitry Andric  void __init(_BidirectionalIterator __a, _BidirectionalIterator __b);
55560b57cec5SDimitry Andric  void __establish_result() {
55570b57cec5SDimitry Andric    if (__subs_[__n_] == -1)
55580b57cec5SDimitry Andric      __result_ = &__position_->prefix();
55590b57cec5SDimitry Andric    else
55600b57cec5SDimitry Andric      __result_ = &(*__position_)[__subs_[__n_]];
55610b57cec5SDimitry Andric  }
55620b57cec5SDimitry Andric};
55630b57cec5SDimitry Andric
55640b57cec5SDimitry Andrictemplate <class _BidirectionalIterator, class _CharT, class _Traits>
5565cb14a3feSDimitry Andricregex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::regex_token_iterator()
5566cb14a3feSDimitry Andric    : __result_(nullptr), __suffix_(), __n_(0) {}
55670b57cec5SDimitry Andric
55680b57cec5SDimitry Andrictemplate <class _BidirectionalIterator, class _CharT, class _Traits>
5569cb14a3feSDimitry Andricvoid regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::__init(
5570cb14a3feSDimitry Andric    _BidirectionalIterator __a, _BidirectionalIterator __b) {
55710b57cec5SDimitry Andric  if (__position_ != _Position())
55720b57cec5SDimitry Andric    __establish_result();
5573cb14a3feSDimitry Andric  else if (__subs_[__n_] == -1) {
55740b57cec5SDimitry Andric    __suffix_.matched = true;
55750b57cec5SDimitry Andric    __suffix_.first   = __a;
55760b57cec5SDimitry Andric    __suffix_.second  = __b;
55770b57cec5SDimitry Andric    __result_         = &__suffix_;
5578cb14a3feSDimitry Andric  } else
55790b57cec5SDimitry Andric    __result_ = nullptr;
55800b57cec5SDimitry Andric}
55810b57cec5SDimitry Andric
55820b57cec5SDimitry Andrictemplate <class _BidirectionalIterator, class _CharT, class _Traits>
5583cb14a3feSDimitry Andricregex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::regex_token_iterator(
5584cb14a3feSDimitry Andric    _BidirectionalIterator __a,
5585cb14a3feSDimitry Andric    _BidirectionalIterator __b,
5586cb14a3feSDimitry Andric    const regex_type& __re,
5587cb14a3feSDimitry Andric    int __submatch,
55880b57cec5SDimitry Andric    regex_constants::match_flag_type __m)
5589cb14a3feSDimitry Andric    : __position_(__a, __b, __re, __m), __n_(0), __subs_(1, __submatch) {
55900b57cec5SDimitry Andric  __init(__a, __b);
55910b57cec5SDimitry Andric}
55920b57cec5SDimitry Andric
55930b57cec5SDimitry Andrictemplate <class _BidirectionalIterator, class _CharT, class _Traits>
5594cb14a3feSDimitry Andricregex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::regex_token_iterator(
5595cb14a3feSDimitry Andric    _BidirectionalIterator __a,
5596cb14a3feSDimitry Andric    _BidirectionalIterator __b,
5597cb14a3feSDimitry Andric    const regex_type& __re,
5598cb14a3feSDimitry Andric    const vector<int>& __submatches,
55990b57cec5SDimitry Andric    regex_constants::match_flag_type __m)
5600cb14a3feSDimitry Andric    : __position_(__a, __b, __re, __m), __n_(0), __subs_(__submatches) {
56010b57cec5SDimitry Andric  __init(__a, __b);
56020b57cec5SDimitry Andric}
56030b57cec5SDimitry Andric
56040b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
56050b57cec5SDimitry Andric
56060b57cec5SDimitry Andrictemplate <class _BidirectionalIterator, class _CharT, class _Traits>
5607cb14a3feSDimitry Andricregex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::regex_token_iterator(
5608cb14a3feSDimitry Andric    _BidirectionalIterator __a,
5609cb14a3feSDimitry Andric    _BidirectionalIterator __b,
56100b57cec5SDimitry Andric    const regex_type& __re,
56110b57cec5SDimitry Andric    initializer_list<int> __submatches,
56120b57cec5SDimitry Andric    regex_constants::match_flag_type __m)
5613cb14a3feSDimitry Andric    : __position_(__a, __b, __re, __m), __n_(0), __subs_(__submatches) {
56140b57cec5SDimitry Andric  __init(__a, __b);
56150b57cec5SDimitry Andric}
56160b57cec5SDimitry Andric
56170b57cec5SDimitry Andric#endif // _LIBCPP_CXX03_LANG
56180b57cec5SDimitry Andric
56190b57cec5SDimitry Andrictemplate <class _BidirectionalIterator, class _CharT, class _Traits>
56200b57cec5SDimitry Andrictemplate <size_t _Np>
5621cb14a3feSDimitry Andricregex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::regex_token_iterator(
5622cb14a3feSDimitry Andric    _BidirectionalIterator __a,
5623cb14a3feSDimitry Andric    _BidirectionalIterator __b,
56240b57cec5SDimitry Andric    const regex_type& __re,
56250b57cec5SDimitry Andric    const int (&__submatches)[_Np],
56260b57cec5SDimitry Andric    regex_constants::match_flag_type __m)
5627cb14a3feSDimitry Andric    : __position_(__a, __b, __re, __m), __n_(0), __subs_(begin(__submatches), end(__submatches)) {
56280b57cec5SDimitry Andric  __init(__a, __b);
56290b57cec5SDimitry Andric}
56300b57cec5SDimitry Andric
56310b57cec5SDimitry Andrictemplate <class _BidirectionalIterator, class _CharT, class _Traits>
5632cb14a3feSDimitry Andricregex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::regex_token_iterator(const regex_token_iterator& __x)
56330b57cec5SDimitry Andric    : __position_(__x.__position_),
56340b57cec5SDimitry Andric      __result_(__x.__result_),
56350b57cec5SDimitry Andric      __suffix_(__x.__suffix_),
56360b57cec5SDimitry Andric      __n_(__x.__n_),
5637cb14a3feSDimitry Andric      __subs_(__x.__subs_) {
56380b57cec5SDimitry Andric  if (__x.__result_ == &__x.__suffix_)
56390b57cec5SDimitry Andric    __result_ = &__suffix_;
56400b57cec5SDimitry Andric  else if (__result_ != nullptr)
56410b57cec5SDimitry Andric    __establish_result();
56420b57cec5SDimitry Andric}
56430b57cec5SDimitry Andric
56440b57cec5SDimitry Andrictemplate <class _BidirectionalIterator, class _CharT, class _Traits>
56450b57cec5SDimitry Andricregex_token_iterator<_BidirectionalIterator, _CharT, _Traits>&
5646cb14a3feSDimitry Andricregex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::operator=(const regex_token_iterator& __x) {
5647cb14a3feSDimitry Andric  if (this != &__x) {
56480b57cec5SDimitry Andric    __position_ = __x.__position_;
56490b57cec5SDimitry Andric    if (__x.__result_ == &__x.__suffix_)
56500b57cec5SDimitry Andric      __result_ = &__suffix_;
56510b57cec5SDimitry Andric    else
56520b57cec5SDimitry Andric      __result_ = __x.__result_;
56530b57cec5SDimitry Andric    __suffix_ = __x.__suffix_;
56540b57cec5SDimitry Andric    __n_      = __x.__n_;
56550b57cec5SDimitry Andric    __subs_   = __x.__subs_;
56560b57cec5SDimitry Andric
56570b57cec5SDimitry Andric    if (__result_ != nullptr && __result_ != &__suffix_)
56580b57cec5SDimitry Andric      __establish_result();
56590b57cec5SDimitry Andric  }
56600b57cec5SDimitry Andric  return *this;
56610b57cec5SDimitry Andric}
56620b57cec5SDimitry Andric
56630b57cec5SDimitry Andrictemplate <class _BidirectionalIterator, class _CharT, class _Traits>
5664cb14a3feSDimitry Andricbool regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::operator==(const regex_token_iterator& __x) const {
56650b57cec5SDimitry Andric  if (__result_ == nullptr && __x.__result_ == nullptr)
56660b57cec5SDimitry Andric    return true;
5667cb14a3feSDimitry Andric  if (__result_ == &__suffix_ && __x.__result_ == &__x.__suffix_ && __suffix_ == __x.__suffix_)
56680b57cec5SDimitry Andric    return true;
56690b57cec5SDimitry Andric  if (__result_ == nullptr || __x.__result_ == nullptr)
56700b57cec5SDimitry Andric    return false;
56710b57cec5SDimitry Andric  if (__result_ == &__suffix_ || __x.__result_ == &__x.__suffix_)
56720b57cec5SDimitry Andric    return false;
5673cb14a3feSDimitry Andric  return __position_ == __x.__position_ && __n_ == __x.__n_ && __subs_ == __x.__subs_;
56740b57cec5SDimitry Andric}
56750b57cec5SDimitry Andric
56760b57cec5SDimitry Andrictemplate <class _BidirectionalIterator, class _CharT, class _Traits>
56770b57cec5SDimitry Andricregex_token_iterator<_BidirectionalIterator, _CharT, _Traits>&
5678cb14a3feSDimitry Andricregex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::operator++() {
56790b57cec5SDimitry Andric  _Position __prev = __position_;
56800b57cec5SDimitry Andric  if (__result_ == &__suffix_)
56810b57cec5SDimitry Andric    __result_ = nullptr;
5682cb14a3feSDimitry Andric  else if (static_cast<size_t>(__n_ + 1) < __subs_.size()) {
56830b57cec5SDimitry Andric    ++__n_;
56840b57cec5SDimitry Andric    __establish_result();
5685cb14a3feSDimitry Andric  } else {
56860b57cec5SDimitry Andric    __n_ = 0;
56870b57cec5SDimitry Andric    ++__position_;
56880b57cec5SDimitry Andric    if (__position_ != _Position())
56890b57cec5SDimitry Andric      __establish_result();
5690cb14a3feSDimitry Andric    else {
5691cb14a3feSDimitry Andric      if (std::find(__subs_.begin(), __subs_.end(), -1) != __subs_.end() && __prev->suffix().length() != 0) {
56920b57cec5SDimitry Andric        __suffix_.matched = true;
56930b57cec5SDimitry Andric        __suffix_.first   = __prev->suffix().first;
56940b57cec5SDimitry Andric        __suffix_.second  = __prev->suffix().second;
56950b57cec5SDimitry Andric        __result_         = &__suffix_;
5696cb14a3feSDimitry Andric      } else
56970b57cec5SDimitry Andric        __result_ = nullptr;
56980b57cec5SDimitry Andric    }
56990b57cec5SDimitry Andric  }
57000b57cec5SDimitry Andric  return *this;
57010b57cec5SDimitry Andric}
57020b57cec5SDimitry Andric
57030b57cec5SDimitry Andric// regex_replace
57040b57cec5SDimitry Andric
5705cb14a3feSDimitry Andrictemplate <class _OutputIterator, class _BidirectionalIterator, class _Traits, class _CharT>
5706cb14a3feSDimitry Andric_LIBCPP_HIDE_FROM_ABI _OutputIterator regex_replace(
5707cb14a3feSDimitry Andric    _OutputIterator __output_iter,
5708cb14a3feSDimitry Andric    _BidirectionalIterator __first,
5709cb14a3feSDimitry Andric    _BidirectionalIterator __last,
5710cb14a3feSDimitry Andric    const basic_regex<_CharT, _Traits>& __e,
5711cb14a3feSDimitry Andric    const _CharT* __fmt,
5712cb14a3feSDimitry Andric    regex_constants::match_flag_type __flags = regex_constants::match_default) {
57130b57cec5SDimitry Andric  typedef regex_iterator<_BidirectionalIterator, _CharT, _Traits> _Iter;
57140b57cec5SDimitry Andric  _Iter __i(__first, __last, __e, __flags);
57150b57cec5SDimitry Andric  _Iter __eof;
5716cb14a3feSDimitry Andric  if (__i == __eof) {
57170b57cec5SDimitry Andric    if (!(__flags & regex_constants::format_no_copy))
57185f757f3fSDimitry Andric      __output_iter = std::copy(__first, __last, __output_iter);
5719cb14a3feSDimitry Andric  } else {
57200b57cec5SDimitry Andric    sub_match<_BidirectionalIterator> __lm;
5721cb14a3feSDimitry Andric    for (size_t __len = char_traits<_CharT>::length(__fmt); __i != __eof; ++__i) {
57220b57cec5SDimitry Andric      if (!(__flags & regex_constants::format_no_copy))
57235f757f3fSDimitry Andric        __output_iter = std::copy(__i->prefix().first, __i->prefix().second, __output_iter);
57240b57cec5SDimitry Andric      __output_iter = __i->format(__output_iter, __fmt, __fmt + __len, __flags);
57250b57cec5SDimitry Andric      __lm          = __i->suffix();
57260b57cec5SDimitry Andric      if (__flags & regex_constants::format_first_only)
57270b57cec5SDimitry Andric        break;
57280b57cec5SDimitry Andric    }
57290b57cec5SDimitry Andric    if (!(__flags & regex_constants::format_no_copy))
57305f757f3fSDimitry Andric      __output_iter = std::copy(__lm.first, __lm.second, __output_iter);
57310b57cec5SDimitry Andric  }
57320b57cec5SDimitry Andric  return __output_iter;
57330b57cec5SDimitry Andric}
57340b57cec5SDimitry Andric
5735cb14a3feSDimitry Andrictemplate <class _OutputIterator, class _BidirectionalIterator, class _Traits, class _CharT, class _ST, class _SA>
5736cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI _OutputIterator regex_replace(
5737cb14a3feSDimitry Andric    _OutputIterator __output_iter,
5738cb14a3feSDimitry Andric    _BidirectionalIterator __first,
5739cb14a3feSDimitry Andric    _BidirectionalIterator __last,
57400b57cec5SDimitry Andric    const basic_regex<_CharT, _Traits>& __e,
57410b57cec5SDimitry Andric    const basic_string<_CharT, _ST, _SA>& __fmt,
5742cb14a3feSDimitry Andric    regex_constants::match_flag_type __flags = regex_constants::match_default) {
57435f757f3fSDimitry Andric  return std::regex_replace(__output_iter, __first, __last, __e, __fmt.c_str(), __flags);
57440b57cec5SDimitry Andric}
57450b57cec5SDimitry Andric
5746cb14a3feSDimitry Andrictemplate <class _Traits, class _CharT, class _ST, class _SA, class _FST, class _FSA>
5747cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI basic_string<_CharT, _ST, _SA>
57480b57cec5SDimitry Andricregex_replace(const basic_string<_CharT, _ST, _SA>& __s,
57490b57cec5SDimitry Andric              const basic_regex<_CharT, _Traits>& __e,
57500b57cec5SDimitry Andric              const basic_string<_CharT, _FST, _FSA>& __fmt,
5751cb14a3feSDimitry Andric              regex_constants::match_flag_type __flags = regex_constants::match_default) {
57520b57cec5SDimitry Andric  basic_string<_CharT, _ST, _SA> __r;
5753cb14a3feSDimitry Andric  std::regex_replace(std::back_inserter(__r), __s.begin(), __s.end(), __e, __fmt.c_str(), __flags);
57540b57cec5SDimitry Andric  return __r;
57550b57cec5SDimitry Andric}
57560b57cec5SDimitry Andric
57570b57cec5SDimitry Andrictemplate <class _Traits, class _CharT, class _ST, class _SA>
5758cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI basic_string<_CharT, _ST, _SA>
57590b57cec5SDimitry Andricregex_replace(const basic_string<_CharT, _ST, _SA>& __s,
5760cb14a3feSDimitry Andric              const basic_regex<_CharT, _Traits>& __e,
5761cb14a3feSDimitry Andric              const _CharT* __fmt,
5762cb14a3feSDimitry Andric              regex_constants::match_flag_type __flags = regex_constants::match_default) {
57630b57cec5SDimitry Andric  basic_string<_CharT, _ST, _SA> __r;
5764cb14a3feSDimitry Andric  std::regex_replace(std::back_inserter(__r), __s.begin(), __s.end(), __e, __fmt, __flags);
57650b57cec5SDimitry Andric  return __r;
57660b57cec5SDimitry Andric}
57670b57cec5SDimitry Andric
57680b57cec5SDimitry Andrictemplate <class _Traits, class _CharT, class _ST, class _SA>
5769cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI basic_string<_CharT>
57700b57cec5SDimitry Andricregex_replace(const _CharT* __s,
57710b57cec5SDimitry Andric              const basic_regex<_CharT, _Traits>& __e,
57720b57cec5SDimitry Andric              const basic_string<_CharT, _ST, _SA>& __fmt,
5773cb14a3feSDimitry Andric              regex_constants::match_flag_type __flags = regex_constants::match_default) {
57740b57cec5SDimitry Andric  basic_string<_CharT> __r;
5775cb14a3feSDimitry Andric  std::regex_replace(std::back_inserter(__r), __s, __s + char_traits<_CharT>::length(__s), __e, __fmt.c_str(), __flags);
57760b57cec5SDimitry Andric  return __r;
57770b57cec5SDimitry Andric}
57780b57cec5SDimitry Andric
57790b57cec5SDimitry Andrictemplate <class _Traits, class _CharT>
5780cb14a3feSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI basic_string<_CharT>
57810b57cec5SDimitry Andricregex_replace(const _CharT* __s,
57820b57cec5SDimitry Andric              const basic_regex<_CharT, _Traits>& __e,
57830b57cec5SDimitry Andric              const _CharT* __fmt,
5784cb14a3feSDimitry Andric              regex_constants::match_flag_type __flags = regex_constants::match_default) {
57850b57cec5SDimitry Andric  basic_string<_CharT> __r;
5786cb14a3feSDimitry Andric  std::regex_replace(std::back_inserter(__r), __s, __s + char_traits<_CharT>::length(__s), __e, __fmt, __flags);
57870b57cec5SDimitry Andric  return __r;
57880b57cec5SDimitry Andric}
57890b57cec5SDimitry Andric
57900b57cec5SDimitry Andric_LIBCPP_END_NAMESPACE_STD
57910b57cec5SDimitry Andric
579206c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 17
5793bdd1243dSDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD
5794bdd1243dSDimitry Andricnamespace pmr {
5795bdd1243dSDimitry Andrictemplate <class _BidirT>
5796cb14a3feSDimitry Andricusing match_results _LIBCPP_AVAILABILITY_PMR =
5797cb14a3feSDimitry Andric    std::match_results<_BidirT, polymorphic_allocator<std::sub_match<_BidirT>>>;
5798bdd1243dSDimitry Andric
579906c3fb27SDimitry Andricusing cmatch _LIBCPP_AVAILABILITY_PMR = match_results<const char*>;
580006c3fb27SDimitry Andricusing smatch _LIBCPP_AVAILABILITY_PMR = match_results<std::pmr::string::const_iterator>;
5801bdd1243dSDimitry Andric
5802bdd1243dSDimitry Andric#  ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
580306c3fb27SDimitry Andricusing wcmatch _LIBCPP_AVAILABILITY_PMR = match_results<const wchar_t*>;
580406c3fb27SDimitry Andricusing wsmatch _LIBCPP_AVAILABILITY_PMR = match_results<std::pmr::wstring::const_iterator>;
5805bdd1243dSDimitry Andric#  endif
5806bdd1243dSDimitry Andric} // namespace pmr
5807bdd1243dSDimitry Andric_LIBCPP_END_NAMESPACE_STD
5808bdd1243dSDimitry Andric#endif
5809bdd1243dSDimitry Andric
58100b57cec5SDimitry Andric_LIBCPP_POP_MACROS
58110b57cec5SDimitry Andric
5812bdd1243dSDimitry Andric#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
5813bdd1243dSDimitry Andric#  include <atomic>
5814bdd1243dSDimitry Andric#  include <concepts>
581506c3fb27SDimitry Andric#  include <cstdlib>
5816bdd1243dSDimitry Andric#  include <iosfwd>
5817bdd1243dSDimitry Andric#  include <iterator>
58185f757f3fSDimitry Andric#  include <mutex>
5819bdd1243dSDimitry Andric#  include <new>
582006c3fb27SDimitry Andric#  include <type_traits>
5821bdd1243dSDimitry Andric#  include <typeinfo>
5822bdd1243dSDimitry Andric#  include <utility>
5823bdd1243dSDimitry Andric#endif
5824bdd1243dSDimitry Andric
58250b57cec5SDimitry Andric#endif // _LIBCPP_REGEX
5826