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