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