xref: /freebsd/contrib/llvm-project/libcxx/include/regex (revision 297eecfb02bb25902531dbb5c3b9a88caf8adf29)
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 (__s.__current_ == __s.__first_ && !(__s.__flags_ & regex_constants::match_not_eol)) {
1893    __s.__do_   = __state::__accept_but_not_consume;
1894    __s.__node_ = this->first();
1895  } else if (__multiline_ && std::__is_eol(*__s.__current_)) {
1896    __s.__do_   = __state::__accept_but_not_consume;
1897    __s.__node_ = this->first();
1898  } else {
1899    __s.__do_   = __state::__reject;
1900    __s.__node_ = nullptr;
1901  }
1902}
1903
1904// __match_any
1905
1906template <class _CharT>
1907class __match_any : public __owns_one_state<_CharT> {
1908  typedef __owns_one_state<_CharT> base;
1909
1910public:
1911  typedef std::__state<_CharT> __state;
1912
1913  _LIBCPP_HIDE_FROM_ABI __match_any(__node<_CharT>* __s) : base(__s) {}
1914
1915  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
1916};
1917
1918template <class _CharT>
1919void __match_any<_CharT>::__exec(__state& __s) const {
1920  if (__s.__current_ != __s.__last_ && *__s.__current_ != 0) {
1921    __s.__do_ = __state::__accept_and_consume;
1922    ++__s.__current_;
1923    __s.__node_ = this->first();
1924  } else {
1925    __s.__do_   = __state::__reject;
1926    __s.__node_ = nullptr;
1927  }
1928}
1929
1930// __match_any_but_newline
1931
1932template <class _CharT>
1933class __match_any_but_newline : public __owns_one_state<_CharT> {
1934  typedef __owns_one_state<_CharT> base;
1935
1936public:
1937  typedef std::__state<_CharT> __state;
1938
1939  _LIBCPP_HIDE_FROM_ABI __match_any_but_newline(__node<_CharT>* __s) : base(__s) {}
1940
1941  void __exec(__state&) const override;
1942};
1943
1944template <>
1945_LIBCPP_EXPORTED_FROM_ABI void __match_any_but_newline<char>::__exec(__state&) const;
1946#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1947template <>
1948_LIBCPP_EXPORTED_FROM_ABI void __match_any_but_newline<wchar_t>::__exec(__state&) const;
1949#endif
1950
1951// __match_char
1952
1953template <class _CharT>
1954class __match_char : public __owns_one_state<_CharT> {
1955  typedef __owns_one_state<_CharT> base;
1956
1957  _CharT __c_;
1958
1959  __match_char(const __match_char&);
1960  __match_char& operator=(const __match_char&);
1961
1962public:
1963  typedef std::__state<_CharT> __state;
1964
1965  _LIBCPP_HIDE_FROM_ABI __match_char(_CharT __c, __node<_CharT>* __s) : base(__s), __c_(__c) {}
1966
1967  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
1968};
1969
1970template <class _CharT>
1971void __match_char<_CharT>::__exec(__state& __s) const {
1972  if (__s.__current_ != __s.__last_ && *__s.__current_ == __c_) {
1973    __s.__do_ = __state::__accept_and_consume;
1974    ++__s.__current_;
1975    __s.__node_ = this->first();
1976  } else {
1977    __s.__do_   = __state::__reject;
1978    __s.__node_ = nullptr;
1979  }
1980}
1981
1982// __match_char_icase
1983
1984template <class _CharT, class _Traits>
1985class __match_char_icase : public __owns_one_state<_CharT> {
1986  typedef __owns_one_state<_CharT> base;
1987
1988  _Traits __traits_;
1989  _CharT __c_;
1990
1991  __match_char_icase(const __match_char_icase&);
1992  __match_char_icase& operator=(const __match_char_icase&);
1993
1994public:
1995  typedef std::__state<_CharT> __state;
1996
1997  _LIBCPP_HIDE_FROM_ABI __match_char_icase(const _Traits& __traits, _CharT __c, __node<_CharT>* __s)
1998      : base(__s), __traits_(__traits), __c_(__traits.translate_nocase(__c)) {}
1999
2000  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
2001};
2002
2003template <class _CharT, class _Traits>
2004void __match_char_icase<_CharT, _Traits>::__exec(__state& __s) const {
2005  if (__s.__current_ != __s.__last_ && __traits_.translate_nocase(*__s.__current_) == __c_) {
2006    __s.__do_ = __state::__accept_and_consume;
2007    ++__s.__current_;
2008    __s.__node_ = this->first();
2009  } else {
2010    __s.__do_   = __state::__reject;
2011    __s.__node_ = nullptr;
2012  }
2013}
2014
2015// __match_char_collate
2016
2017template <class _CharT, class _Traits>
2018class __match_char_collate : public __owns_one_state<_CharT> {
2019  typedef __owns_one_state<_CharT> base;
2020
2021  _Traits __traits_;
2022  _CharT __c_;
2023
2024  __match_char_collate(const __match_char_collate&);
2025  __match_char_collate& operator=(const __match_char_collate&);
2026
2027public:
2028  typedef std::__state<_CharT> __state;
2029
2030  _LIBCPP_HIDE_FROM_ABI __match_char_collate(const _Traits& __traits, _CharT __c, __node<_CharT>* __s)
2031      : base(__s), __traits_(__traits), __c_(__traits.translate(__c)) {}
2032
2033  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
2034};
2035
2036template <class _CharT, class _Traits>
2037void __match_char_collate<_CharT, _Traits>::__exec(__state& __s) const {
2038  if (__s.__current_ != __s.__last_ && __traits_.translate(*__s.__current_) == __c_) {
2039    __s.__do_ = __state::__accept_and_consume;
2040    ++__s.__current_;
2041    __s.__node_ = this->first();
2042  } else {
2043    __s.__do_   = __state::__reject;
2044    __s.__node_ = nullptr;
2045  }
2046}
2047
2048// __bracket_expression
2049
2050template <class _CharT, class _Traits>
2051class __bracket_expression : public __owns_one_state<_CharT> {
2052  typedef __owns_one_state<_CharT> base;
2053  typedef typename _Traits::string_type string_type;
2054
2055  _Traits __traits_;
2056  vector<_CharT> __chars_;
2057  vector<_CharT> __neg_chars_;
2058  vector<pair<string_type, string_type> > __ranges_;
2059  vector<pair<_CharT, _CharT> > __digraphs_;
2060  vector<string_type> __equivalences_;
2061  typename regex_traits<_CharT>::char_class_type __mask_;
2062  typename regex_traits<_CharT>::char_class_type __neg_mask_;
2063  bool __negate_;
2064  bool __icase_;
2065  bool __collate_;
2066  bool __might_have_digraph_;
2067
2068  __bracket_expression(const __bracket_expression&);
2069  __bracket_expression& operator=(const __bracket_expression&);
2070
2071public:
2072  typedef std::__state<_CharT> __state;
2073
2074  _LIBCPP_HIDE_FROM_ABI
2075  __bracket_expression(const _Traits& __traits, __node<_CharT>* __s, bool __negate, bool __icase, bool __collate)
2076      : base(__s),
2077        __traits_(__traits),
2078        __mask_(),
2079        __neg_mask_(),
2080        __negate_(__negate),
2081        __icase_(__icase),
2082        __collate_(__collate),
2083        __might_have_digraph_(__traits_.getloc().name() != "C") {}
2084
2085  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
2086
2087  _LIBCPP_HIDE_FROM_ABI bool __negated() const { return __negate_; }
2088
2089  _LIBCPP_HIDE_FROM_ABI void __add_char(_CharT __c) {
2090    if (__icase_)
2091      __chars_.push_back(__traits_.translate_nocase(__c));
2092    else if (__collate_)
2093      __chars_.push_back(__traits_.translate(__c));
2094    else
2095      __chars_.push_back(__c);
2096  }
2097  _LIBCPP_HIDE_FROM_ABI void __add_neg_char(_CharT __c) {
2098    if (__icase_)
2099      __neg_chars_.push_back(__traits_.translate_nocase(__c));
2100    else if (__collate_)
2101      __neg_chars_.push_back(__traits_.translate(__c));
2102    else
2103      __neg_chars_.push_back(__c);
2104  }
2105  _LIBCPP_HIDE_FROM_ABI void __add_range(string_type __b, string_type __e) {
2106    if (__collate_) {
2107      if (__icase_) {
2108        for (size_t __i = 0; __i < __b.size(); ++__i)
2109          __b[__i] = __traits_.translate_nocase(__b[__i]);
2110        for (size_t __i = 0; __i < __e.size(); ++__i)
2111          __e[__i] = __traits_.translate_nocase(__e[__i]);
2112      } else {
2113        for (size_t __i = 0; __i < __b.size(); ++__i)
2114          __b[__i] = __traits_.translate(__b[__i]);
2115        for (size_t __i = 0; __i < __e.size(); ++__i)
2116          __e[__i] = __traits_.translate(__e[__i]);
2117      }
2118      __ranges_.push_back(
2119          std::make_pair(__traits_.transform(__b.begin(), __b.end()), __traits_.transform(__e.begin(), __e.end())));
2120    } else {
2121      if (__b.size() != 1 || __e.size() != 1)
2122        __throw_regex_error<regex_constants::error_range>();
2123      if (__icase_) {
2124        __b[0] = __traits_.translate_nocase(__b[0]);
2125        __e[0] = __traits_.translate_nocase(__e[0]);
2126      }
2127      __ranges_.push_back(std::make_pair(std::move(__b), std::move(__e)));
2128    }
2129  }
2130  _LIBCPP_HIDE_FROM_ABI void __add_digraph(_CharT __c1, _CharT __c2) {
2131    if (__icase_)
2132      __digraphs_.push_back(std::make_pair(__traits_.translate_nocase(__c1), __traits_.translate_nocase(__c2)));
2133    else if (__collate_)
2134      __digraphs_.push_back(std::make_pair(__traits_.translate(__c1), __traits_.translate(__c2)));
2135    else
2136      __digraphs_.push_back(std::make_pair(__c1, __c2));
2137  }
2138  _LIBCPP_HIDE_FROM_ABI void __add_equivalence(const string_type& __s) { __equivalences_.push_back(__s); }
2139  _LIBCPP_HIDE_FROM_ABI void __add_class(typename regex_traits<_CharT>::char_class_type __mask) { __mask_ |= __mask; }
2140  _LIBCPP_HIDE_FROM_ABI void __add_neg_class(typename regex_traits<_CharT>::char_class_type __mask) {
2141    __neg_mask_ |= __mask;
2142  }
2143};
2144
2145template <class _CharT, class _Traits>
2146void __bracket_expression<_CharT, _Traits>::__exec(__state& __s) const {
2147  bool __found        = false;
2148  unsigned __consumed = 0;
2149  if (__s.__current_ != __s.__last_) {
2150    ++__consumed;
2151    if (__might_have_digraph_) {
2152      const _CharT* __next = std::next(__s.__current_);
2153      if (__next != __s.__last_) {
2154        pair<_CharT, _CharT> __ch2(*__s.__current_, *__next);
2155        if (__icase_) {
2156          __ch2.first  = __traits_.translate_nocase(__ch2.first);
2157          __ch2.second = __traits_.translate_nocase(__ch2.second);
2158        } else if (__collate_) {
2159          __ch2.first  = __traits_.translate(__ch2.first);
2160          __ch2.second = __traits_.translate(__ch2.second);
2161        }
2162        if (!__traits_.lookup_collatename(&__ch2.first, &__ch2.first + 2).empty()) {
2163          // __ch2 is a digraph in this locale
2164          ++__consumed;
2165          for (size_t __i = 0; __i < __digraphs_.size(); ++__i) {
2166            if (__ch2 == __digraphs_[__i]) {
2167              __found = true;
2168              goto __exit;
2169            }
2170          }
2171          if (__collate_ && !__ranges_.empty()) {
2172            string_type __s2 = __traits_.transform(&__ch2.first, &__ch2.first + 2);
2173            for (size_t __i = 0; __i < __ranges_.size(); ++__i) {
2174              if (__ranges_[__i].first <= __s2 && __s2 <= __ranges_[__i].second) {
2175                __found = true;
2176                goto __exit;
2177              }
2178            }
2179          }
2180          if (!__equivalences_.empty()) {
2181            string_type __s2 = __traits_.transform_primary(&__ch2.first, &__ch2.first + 2);
2182            for (size_t __i = 0; __i < __equivalences_.size(); ++__i) {
2183              if (__s2 == __equivalences_[__i]) {
2184                __found = true;
2185                goto __exit;
2186              }
2187            }
2188          }
2189          if (__traits_.isctype(__ch2.first, __mask_) && __traits_.isctype(__ch2.second, __mask_)) {
2190            __found = true;
2191            goto __exit;
2192          }
2193          if (!__traits_.isctype(__ch2.first, __neg_mask_) && !__traits_.isctype(__ch2.second, __neg_mask_)) {
2194            __found = true;
2195            goto __exit;
2196          }
2197          goto __exit;
2198        }
2199      }
2200    }
2201    // test *__s.__current_ as not a digraph
2202    _CharT __ch = *__s.__current_;
2203    if (__icase_)
2204      __ch = __traits_.translate_nocase(__ch);
2205    else if (__collate_)
2206      __ch = __traits_.translate(__ch);
2207    for (size_t __i = 0; __i < __chars_.size(); ++__i) {
2208      if (__ch == __chars_[__i]) {
2209        __found = true;
2210        goto __exit;
2211      }
2212    }
2213    // When there's at least one of __neg_chars_ and __neg_mask_, the set
2214    // of "__found" chars is
2215    //   union(complement(union(__neg_chars_, __neg_mask_)),
2216    //         other cases...)
2217    //
2218    // It doesn't make sense to check this when there are no __neg_chars_
2219    // and no __neg_mask_.
2220    if (!(__neg_mask_ == 0 && __neg_chars_.empty())) {
2221      const bool __in_neg_mask  = __traits_.isctype(__ch, __neg_mask_);
2222      const bool __in_neg_chars = std::find(__neg_chars_.begin(), __neg_chars_.end(), __ch) != __neg_chars_.end();
2223      if (!(__in_neg_mask || __in_neg_chars)) {
2224        __found = true;
2225        goto __exit;
2226      }
2227    }
2228    if (!__ranges_.empty()) {
2229      string_type __s2 = __collate_ ? __traits_.transform(&__ch, &__ch + 1) : string_type(1, __ch);
2230      for (size_t __i = 0; __i < __ranges_.size(); ++__i) {
2231        if (__ranges_[__i].first <= __s2 && __s2 <= __ranges_[__i].second) {
2232          __found = true;
2233          goto __exit;
2234        }
2235      }
2236    }
2237    if (!__equivalences_.empty()) {
2238      string_type __s2 = __traits_.transform_primary(&__ch, &__ch + 1);
2239      for (size_t __i = 0; __i < __equivalences_.size(); ++__i) {
2240        if (__s2 == __equivalences_[__i]) {
2241          __found = true;
2242          goto __exit;
2243        }
2244      }
2245    }
2246    if (__traits_.isctype(__ch, __mask_)) {
2247      __found = true;
2248      goto __exit;
2249    }
2250  } else
2251    __found = __negate_; // force reject
2252__exit:
2253  if (__found != __negate_) {
2254    __s.__do_ = __state::__accept_and_consume;
2255    __s.__current_ += __consumed;
2256    __s.__node_ = this->first();
2257  } else {
2258    __s.__do_   = __state::__reject;
2259    __s.__node_ = nullptr;
2260  }
2261}
2262
2263template <class _CharT, class _Traits>
2264class __lookahead;
2265
2266template <class _CharT, class _Traits = regex_traits<_CharT> >
2267class _LIBCPP_TEMPLATE_VIS basic_regex;
2268
2269typedef basic_regex<char> regex;
2270#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
2271typedef basic_regex<wchar_t> wregex;
2272#endif
2273
2274template <class _CharT, class _Traits>
2275class _LIBCPP_TEMPLATE_VIS _LIBCPP_PREFERRED_NAME(regex)
2276    _LIBCPP_IF_WIDE_CHARACTERS(_LIBCPP_PREFERRED_NAME(wregex)) basic_regex {
2277public:
2278  // types:
2279  typedef _CharT value_type;
2280  typedef _Traits traits_type;
2281  typedef typename _Traits::string_type string_type;
2282  typedef regex_constants::syntax_option_type flag_type;
2283  typedef typename _Traits::locale_type locale_type;
2284
2285private:
2286  _Traits __traits_;
2287  flag_type __flags_;
2288  unsigned __marked_count_;
2289  unsigned __loop_count_;
2290  int __open_count_;
2291  shared_ptr<__empty_state<_CharT> > __start_;
2292  __owns_one_state<_CharT>* __end_;
2293
2294  typedef std::__state<_CharT> __state;
2295  typedef std::__node<_CharT> __node;
2296
2297public:
2298  // constants:
2299  static const regex_constants::syntax_option_type icase      = regex_constants::icase;
2300  static const regex_constants::syntax_option_type nosubs     = regex_constants::nosubs;
2301  static const regex_constants::syntax_option_type optimize   = regex_constants::optimize;
2302  static const regex_constants::syntax_option_type collate    = regex_constants::collate;
2303  static const regex_constants::syntax_option_type ECMAScript = regex_constants::ECMAScript;
2304  static const regex_constants::syntax_option_type basic      = regex_constants::basic;
2305  static const regex_constants::syntax_option_type extended   = regex_constants::extended;
2306  static const regex_constants::syntax_option_type awk        = regex_constants::awk;
2307  static const regex_constants::syntax_option_type grep       = regex_constants::grep;
2308  static const regex_constants::syntax_option_type egrep      = regex_constants::egrep;
2309  static const regex_constants::syntax_option_type multiline  = regex_constants::multiline;
2310
2311  // construct/copy/destroy:
2312  _LIBCPP_HIDE_FROM_ABI basic_regex()
2313      : __flags_(regex_constants::ECMAScript),
2314        __marked_count_(0),
2315        __loop_count_(0),
2316        __open_count_(0),
2317        __end_(nullptr) {}
2318  _LIBCPP_HIDE_FROM_ABI explicit basic_regex(const value_type* __p, flag_type __f = regex_constants::ECMAScript)
2319      : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0), __end_(nullptr) {
2320    __init(__p, __p + __traits_.length(__p));
2321  }
2322
2323  _LIBCPP_HIDE_FROM_ABI basic_regex(const value_type* __p, size_t __len, flag_type __f = regex_constants::ECMAScript)
2324      : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0), __end_(nullptr) {
2325    __init(__p, __p + __len);
2326  }
2327
2328  //     basic_regex(const basic_regex&) = default;
2329  //     basic_regex(basic_regex&&) = default;
2330  template <class _ST, class _SA>
2331  _LIBCPP_HIDE_FROM_ABI explicit basic_regex(const basic_string<value_type, _ST, _SA>& __p,
2332                                             flag_type __f = regex_constants::ECMAScript)
2333      : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0), __end_(nullptr) {
2334    __init(__p.begin(), __p.end());
2335  }
2336
2337  template <class _ForwardIterator>
2338  _LIBCPP_HIDE_FROM_ABI
2339  basic_regex(_ForwardIterator __first, _ForwardIterator __last, flag_type __f = regex_constants::ECMAScript)
2340      : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0), __end_(nullptr) {
2341    __init(__first, __last);
2342  }
2343#ifndef _LIBCPP_CXX03_LANG
2344  _LIBCPP_HIDE_FROM_ABI basic_regex(initializer_list<value_type> __il, flag_type __f = regex_constants::ECMAScript)
2345      : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0), __end_(nullptr) {
2346    __init(__il.begin(), __il.end());
2347  }
2348#endif // _LIBCPP_CXX03_LANG
2349
2350  //    ~basic_regex() = default;
2351
2352  //     basic_regex& operator=(const basic_regex&) = default;
2353  //     basic_regex& operator=(basic_regex&&) = default;
2354  _LIBCPP_HIDE_FROM_ABI basic_regex& operator=(const value_type* __p) { return assign(__p); }
2355#ifndef _LIBCPP_CXX03_LANG
2356  _LIBCPP_HIDE_FROM_ABI basic_regex& operator=(initializer_list<value_type> __il) { return assign(__il); }
2357#endif // _LIBCPP_CXX03_LANG
2358  template <class _ST, class _SA>
2359  _LIBCPP_HIDE_FROM_ABI basic_regex& operator=(const basic_string<value_type, _ST, _SA>& __p) {
2360    return assign(__p);
2361  }
2362
2363  // assign:
2364  _LIBCPP_HIDE_FROM_ABI basic_regex& assign(const basic_regex& __that) { return *this = __that; }
2365#ifndef _LIBCPP_CXX03_LANG
2366  _LIBCPP_HIDE_FROM_ABI basic_regex& assign(basic_regex&& __that) _NOEXCEPT { return *this = std::move(__that); }
2367#endif
2368  _LIBCPP_HIDE_FROM_ABI basic_regex& assign(const value_type* __p, flag_type __f = regex_constants::ECMAScript) {
2369    return assign(__p, __p + __traits_.length(__p), __f);
2370  }
2371  _LIBCPP_HIDE_FROM_ABI basic_regex&
2372  assign(const value_type* __p, size_t __len, flag_type __f = regex_constants::ECMAScript) {
2373    return assign(__p, __p + __len, __f);
2374  }
2375  template <class _ST, class _SA>
2376  _LIBCPP_HIDE_FROM_ABI basic_regex&
2377  assign(const basic_string<value_type, _ST, _SA>& __s, flag_type __f = regex_constants::ECMAScript) {
2378    return assign(__s.begin(), __s.end(), __f);
2379  }
2380
2381  template <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value, int> = 0>
2382  _LIBCPP_HIDE_FROM_ABI basic_regex&
2383  assign(_InputIterator __first, _InputIterator __last, flag_type __f = regex_constants::ECMAScript) {
2384    basic_string<_CharT> __t(__first, __last);
2385    return assign(__t.begin(), __t.end(), __f);
2386  }
2387
2388private:
2389  _LIBCPP_HIDE_FROM_ABI void __member_init(flag_type __f) {
2390    __flags_        = __f;
2391    __marked_count_ = 0;
2392    __loop_count_   = 0;
2393    __open_count_   = 0;
2394    __end_          = nullptr;
2395  }
2396
2397public:
2398  template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> = 0>
2399  _LIBCPP_HIDE_FROM_ABI basic_regex&
2400  assign(_ForwardIterator __first, _ForwardIterator __last, flag_type __f = regex_constants::ECMAScript) {
2401    return assign(basic_regex(__first, __last, __f));
2402  }
2403
2404#ifndef _LIBCPP_CXX03_LANG
2405
2406  _LIBCPP_HIDE_FROM_ABI basic_regex&
2407  assign(initializer_list<value_type> __il, flag_type __f = regex_constants::ECMAScript) {
2408    return assign(__il.begin(), __il.end(), __f);
2409  }
2410
2411#endif // _LIBCPP_CXX03_LANG
2412
2413  // const operations:
2414  _LIBCPP_HIDE_FROM_ABI unsigned mark_count() const { return __marked_count_; }
2415  _LIBCPP_HIDE_FROM_ABI flag_type flags() const { return __flags_; }
2416
2417  // locale:
2418  _LIBCPP_HIDE_FROM_ABI locale_type imbue(locale_type __loc) {
2419    __member_init(ECMAScript);
2420    __start_.reset();
2421    return __traits_.imbue(__loc);
2422  }
2423  _LIBCPP_HIDE_FROM_ABI locale_type getloc() const { return __traits_.getloc(); }
2424
2425  // swap:
2426  void swap(basic_regex& __r);
2427
2428private:
2429  _LIBCPP_HIDE_FROM_ABI unsigned __loop_count() const { return __loop_count_; }
2430
2431  _LIBCPP_HIDE_FROM_ABI bool __use_multiline() const {
2432    return __get_grammar(__flags_) == ECMAScript && (__flags_ & multiline);
2433  }
2434
2435  template <class _ForwardIterator>
2436  void __init(_ForwardIterator __first, _ForwardIterator __last);
2437  template <class _ForwardIterator>
2438  _ForwardIterator __parse(_ForwardIterator __first, _ForwardIterator __last);
2439  template <class _ForwardIterator>
2440  _ForwardIterator __parse_basic_reg_exp(_ForwardIterator __first, _ForwardIterator __last);
2441  template <class _ForwardIterator>
2442  _ForwardIterator __parse_RE_expression(_ForwardIterator __first, _ForwardIterator __last);
2443  template <class _ForwardIterator>
2444  _ForwardIterator __parse_simple_RE(_ForwardIterator __first, _ForwardIterator __last);
2445  template <class _ForwardIterator>
2446  _ForwardIterator __parse_nondupl_RE(_ForwardIterator __first, _ForwardIterator __last);
2447  template <class _ForwardIterator>
2448  _ForwardIterator __parse_one_char_or_coll_elem_RE(_ForwardIterator __first, _ForwardIterator __last);
2449  template <class _ForwardIterator>
2450  _ForwardIterator __parse_Back_open_paren(_ForwardIterator __first, _ForwardIterator __last);
2451  template <class _ForwardIterator>
2452  _ForwardIterator __parse_Back_close_paren(_ForwardIterator __first, _ForwardIterator __last);
2453  template <class _ForwardIterator>
2454  _ForwardIterator __parse_Back_open_brace(_ForwardIterator __first, _ForwardIterator __last);
2455  template <class _ForwardIterator>
2456  _ForwardIterator __parse_Back_close_brace(_ForwardIterator __first, _ForwardIterator __last);
2457  template <class _ForwardIterator>
2458  _ForwardIterator __parse_BACKREF(_ForwardIterator __first, _ForwardIterator __last);
2459  template <class _ForwardIterator>
2460  _ForwardIterator __parse_ORD_CHAR(_ForwardIterator __first, _ForwardIterator __last);
2461  template <class _ForwardIterator>
2462  _ForwardIterator __parse_QUOTED_CHAR(_ForwardIterator __first, _ForwardIterator __last);
2463  template <class _ForwardIterator>
2464  _ForwardIterator __parse_RE_dupl_symbol(
2465      _ForwardIterator __first,
2466      _ForwardIterator __last,
2467      __owns_one_state<_CharT>* __s,
2468      unsigned __mexp_begin,
2469      unsigned __mexp_end);
2470  template <class _ForwardIterator>
2471  _ForwardIterator __parse_ERE_dupl_symbol(
2472      _ForwardIterator __first,
2473      _ForwardIterator __last,
2474      __owns_one_state<_CharT>* __s,
2475      unsigned __mexp_begin,
2476      unsigned __mexp_end);
2477  template <class _ForwardIterator>
2478  _ForwardIterator __parse_bracket_expression(_ForwardIterator __first, _ForwardIterator __last);
2479  template <class _ForwardIterator>
2480  _ForwardIterator
2481  __parse_follow_list(_ForwardIterator __first, _ForwardIterator __last, __bracket_expression<_CharT, _Traits>* __ml);
2482  template <class _ForwardIterator>
2483  _ForwardIterator __parse_expression_term(
2484      _ForwardIterator __first, _ForwardIterator __last, __bracket_expression<_CharT, _Traits>* __ml);
2485  template <class _ForwardIterator>
2486  _ForwardIterator __parse_equivalence_class(
2487      _ForwardIterator __first, _ForwardIterator __last, __bracket_expression<_CharT, _Traits>* __ml);
2488  template <class _ForwardIterator>
2489  _ForwardIterator __parse_character_class(
2490      _ForwardIterator __first, _ForwardIterator __last, __bracket_expression<_CharT, _Traits>* __ml);
2491  template <class _ForwardIterator>
2492  _ForwardIterator
2493  __parse_collating_symbol(_ForwardIterator __first, _ForwardIterator __last, basic_string<_CharT>& __col_sym);
2494  template <class _ForwardIterator>
2495  _ForwardIterator __parse_DUP_COUNT(_ForwardIterator __first, _ForwardIterator __last, int& __c);
2496  template <class _ForwardIterator>
2497  _ForwardIterator __parse_extended_reg_exp(_ForwardIterator __first, _ForwardIterator __last);
2498  template <class _ForwardIterator>
2499  _ForwardIterator __parse_ERE_branch(_ForwardIterator __first, _ForwardIterator __last);
2500  template <class _ForwardIterator>
2501  _ForwardIterator __parse_ERE_expression(_ForwardIterator __first, _ForwardIterator __last);
2502  template <class _ForwardIterator>
2503  _ForwardIterator __parse_one_char_or_coll_elem_ERE(_ForwardIterator __first, _ForwardIterator __last);
2504  template <class _ForwardIterator>
2505  _ForwardIterator __parse_ORD_CHAR_ERE(_ForwardIterator __first, _ForwardIterator __last);
2506  template <class _ForwardIterator>
2507  _ForwardIterator __parse_QUOTED_CHAR_ERE(_ForwardIterator __first, _ForwardIterator __last);
2508  template <class _ForwardIterator>
2509  _ForwardIterator __parse_ecma_exp(_ForwardIterator __first, _ForwardIterator __last);
2510  template <class _ForwardIterator>
2511  _ForwardIterator __parse_alternative(_ForwardIterator __first, _ForwardIterator __last);
2512  template <class _ForwardIterator>
2513  _ForwardIterator __parse_term(_ForwardIterator __first, _ForwardIterator __last);
2514  template <class _ForwardIterator>
2515  _ForwardIterator __parse_assertion(_ForwardIterator __first, _ForwardIterator __last);
2516  template <class _ForwardIterator>
2517  _ForwardIterator __parse_atom(_ForwardIterator __first, _ForwardIterator __last);
2518  template <class _ForwardIterator>
2519  _ForwardIterator __parse_atom_escape(_ForwardIterator __first, _ForwardIterator __last);
2520  template <class _ForwardIterator>
2521  _ForwardIterator __parse_decimal_escape(_ForwardIterator __first, _ForwardIterator __last);
2522  template <class _ForwardIterator>
2523  _ForwardIterator __parse_character_class_escape(_ForwardIterator __first, _ForwardIterator __last);
2524  template <class _ForwardIterator>
2525  _ForwardIterator
2526  __parse_character_escape(_ForwardIterator __first, _ForwardIterator __last, basic_string<_CharT>* __str = nullptr);
2527  template <class _ForwardIterator>
2528  _ForwardIterator __parse_pattern_character(_ForwardIterator __first, _ForwardIterator __last);
2529  template <class _ForwardIterator>
2530  _ForwardIterator __parse_grep(_ForwardIterator __first, _ForwardIterator __last);
2531  template <class _ForwardIterator>
2532  _ForwardIterator __parse_egrep(_ForwardIterator __first, _ForwardIterator __last);
2533  template <class _ForwardIterator>
2534  _ForwardIterator __parse_class_escape(
2535      _ForwardIterator __first,
2536      _ForwardIterator __last,
2537      basic_string<_CharT>& __str,
2538      __bracket_expression<_CharT, _Traits>* __ml);
2539  template <class _ForwardIterator>
2540  _ForwardIterator
2541  __parse_awk_escape(_ForwardIterator __first, _ForwardIterator __last, basic_string<_CharT>* __str = nullptr);
2542
2543  bool __test_back_ref(_CharT);
2544
2545  _LIBCPP_HIDE_FROM_ABI void __push_l_anchor();
2546  void __push_r_anchor();
2547  void __push_match_any();
2548  void __push_match_any_but_newline();
2549  _LIBCPP_HIDE_FROM_ABI void __push_greedy_inf_repeat(
2550      size_t __min, __owns_one_state<_CharT>* __s, unsigned __mexp_begin = 0, unsigned __mexp_end = 0) {
2551    __push_loop(__min, numeric_limits<size_t>::max(), __s, __mexp_begin, __mexp_end);
2552  }
2553  _LIBCPP_HIDE_FROM_ABI void __push_nongreedy_inf_repeat(
2554      size_t __min, __owns_one_state<_CharT>* __s, unsigned __mexp_begin = 0, unsigned __mexp_end = 0) {
2555    __push_loop(__min, numeric_limits<size_t>::max(), __s, __mexp_begin, __mexp_end, false);
2556  }
2557  void __push_loop(size_t __min,
2558                   size_t __max,
2559                   __owns_one_state<_CharT>* __s,
2560                   size_t __mexp_begin = 0,
2561                   size_t __mexp_end   = 0,
2562                   bool __greedy       = true);
2563  __bracket_expression<_CharT, _Traits>* __start_matching_list(bool __negate);
2564  void __push_char(value_type __c);
2565  void __push_back_ref(int __i);
2566  void __push_alternation(__owns_one_state<_CharT>* __sa, __owns_one_state<_CharT>* __sb);
2567  void __push_begin_marked_subexpression();
2568  void __push_end_marked_subexpression(unsigned);
2569  void __push_empty();
2570  void __push_word_boundary(bool);
2571  void __push_lookahead(const basic_regex&, bool, unsigned);
2572
2573  template <class _Allocator>
2574  bool __search(const _CharT* __first,
2575                const _CharT* __last,
2576                match_results<const _CharT*, _Allocator>& __m,
2577                regex_constants::match_flag_type __flags) const;
2578
2579  template <class _Allocator>
2580  bool __match_at_start(const _CharT* __first,
2581                        const _CharT* __last,
2582                        match_results<const _CharT*, _Allocator>& __m,
2583                        regex_constants::match_flag_type __flags,
2584                        bool) const;
2585  template <class _Allocator>
2586  bool __match_at_start_ecma(
2587      const _CharT* __first,
2588      const _CharT* __last,
2589      match_results<const _CharT*, _Allocator>& __m,
2590      regex_constants::match_flag_type __flags,
2591      bool) const;
2592  template <class _Allocator>
2593  bool __match_at_start_posix_nosubs(
2594      const _CharT* __first,
2595      const _CharT* __last,
2596      match_results<const _CharT*, _Allocator>& __m,
2597      regex_constants::match_flag_type __flags,
2598      bool) const;
2599  template <class _Allocator>
2600  bool __match_at_start_posix_subs(
2601      const _CharT* __first,
2602      const _CharT* __last,
2603      match_results<const _CharT*, _Allocator>& __m,
2604      regex_constants::match_flag_type __flags,
2605      bool) const;
2606
2607  template <class _Bp, class _Ap, class _Cp, class _Tp>
2608  friend bool
2609  regex_search(_Bp, _Bp, match_results<_Bp, _Ap>&, const basic_regex<_Cp, _Tp>&, regex_constants::match_flag_type);
2610
2611  template <class _Ap, class _Cp, class _Tp>
2612  friend bool
2613  regex_search(const _Cp*,
2614               const _Cp*,
2615               match_results<const _Cp*, _Ap>&,
2616               const basic_regex<_Cp, _Tp>&,
2617               regex_constants::match_flag_type);
2618
2619  template <class _Bp, class _Cp, class _Tp>
2620  friend bool regex_search(_Bp, _Bp, const basic_regex<_Cp, _Tp>&, regex_constants::match_flag_type);
2621
2622  template <class _Cp, class _Tp>
2623  friend bool regex_search(const _Cp*, const _Cp*, const basic_regex<_Cp, _Tp>&, regex_constants::match_flag_type);
2624
2625  template <class _Cp, class _Ap, class _Tp>
2626  friend bool regex_search(
2627      const _Cp*, match_results<const _Cp*, _Ap>&, const basic_regex<_Cp, _Tp>&, regex_constants::match_flag_type);
2628
2629  template <class _ST, class _SA, class _Cp, class _Tp>
2630  friend bool regex_search(const basic_string<_Cp, _ST, _SA>& __s,
2631                           const basic_regex<_Cp, _Tp>& __e,
2632                           regex_constants::match_flag_type __flags);
2633
2634  template <class _ST, class _SA, class _Ap, class _Cp, class _Tp>
2635  friend bool regex_search(const basic_string<_Cp, _ST, _SA>& __s,
2636                           match_results<typename basic_string<_Cp, _ST, _SA>::const_iterator, _Ap>&,
2637                           const basic_regex<_Cp, _Tp>& __e,
2638                           regex_constants::match_flag_type __flags);
2639
2640  template <class _Iter, class _Ap, class _Cp, class _Tp>
2641  friend bool
2642  regex_search(__wrap_iter<_Iter> __first,
2643               __wrap_iter<_Iter> __last,
2644               match_results<__wrap_iter<_Iter>, _Ap>& __m,
2645               const basic_regex<_Cp, _Tp>& __e,
2646               regex_constants::match_flag_type __flags);
2647
2648  template <class, class>
2649  friend class __lookahead;
2650};
2651
2652#if _LIBCPP_STD_VER >= 17
2653template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> = 0>
2654basic_regex(_ForwardIterator, _ForwardIterator, regex_constants::syntax_option_type = regex_constants::ECMAScript)
2655    -> basic_regex<typename iterator_traits<_ForwardIterator>::value_type>;
2656#endif
2657
2658template <class _CharT, class _Traits>
2659const regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::icase;
2660template <class _CharT, class _Traits>
2661const regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::nosubs;
2662template <class _CharT, class _Traits>
2663const regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::optimize;
2664template <class _CharT, class _Traits>
2665const regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::collate;
2666template <class _CharT, class _Traits>
2667const regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::ECMAScript;
2668template <class _CharT, class _Traits>
2669const regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::basic;
2670template <class _CharT, class _Traits>
2671const regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::extended;
2672template <class _CharT, class _Traits>
2673const regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::awk;
2674template <class _CharT, class _Traits>
2675const regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::grep;
2676template <class _CharT, class _Traits>
2677const regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::egrep;
2678
2679template <class _CharT, class _Traits>
2680void basic_regex<_CharT, _Traits>::swap(basic_regex& __r) {
2681  using std::swap;
2682  swap(__traits_, __r.__traits_);
2683  swap(__flags_, __r.__flags_);
2684  swap(__marked_count_, __r.__marked_count_);
2685  swap(__loop_count_, __r.__loop_count_);
2686  swap(__open_count_, __r.__open_count_);
2687  swap(__start_, __r.__start_);
2688  swap(__end_, __r.__end_);
2689}
2690
2691template <class _CharT, class _Traits>
2692inline _LIBCPP_HIDE_FROM_ABI void swap(basic_regex<_CharT, _Traits>& __x, basic_regex<_CharT, _Traits>& __y) {
2693  return __x.swap(__y);
2694}
2695
2696// __lookahead
2697
2698template <class _CharT, class _Traits>
2699class __lookahead : public __owns_one_state<_CharT> {
2700  typedef __owns_one_state<_CharT> base;
2701
2702  basic_regex<_CharT, _Traits> __exp_;
2703  unsigned __mexp_;
2704  bool __invert_;
2705
2706  __lookahead(const __lookahead&);
2707  __lookahead& operator=(const __lookahead&);
2708
2709public:
2710  typedef std::__state<_CharT> __state;
2711
2712  _LIBCPP_HIDE_FROM_ABI
2713  __lookahead(const basic_regex<_CharT, _Traits>& __exp, bool __invert, __node<_CharT>* __s, unsigned __mexp)
2714      : base(__s), __exp_(__exp), __mexp_(__mexp), __invert_(__invert) {}
2715
2716  _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
2717};
2718
2719template <class _CharT, class _Traits>
2720void __lookahead<_CharT, _Traits>::__exec(__state& __s) const {
2721  match_results<const _CharT*> __m;
2722  __m.__init(1 + __exp_.mark_count(), __s.__current_, __s.__last_);
2723  bool __matched = __exp_.__match_at_start_ecma(
2724      __s.__current_,
2725      __s.__last_,
2726      __m,
2727      (__s.__flags_ | regex_constants::match_continuous) & ~regex_constants::__full_match,
2728      __s.__at_first_ && __s.__current_ == __s.__first_);
2729  if (__matched != __invert_) {
2730    __s.__do_   = __state::__accept_but_not_consume;
2731    __s.__node_ = this->first();
2732    for (unsigned __i = 1; __i < __m.size(); ++__i) {
2733      __s.__sub_matches_[__mexp_ + __i - 1] = __m.__matches_[__i];
2734    }
2735  } else {
2736    __s.__do_   = __state::__reject;
2737    __s.__node_ = nullptr;
2738  }
2739}
2740
2741template <class _CharT, class _Traits>
2742template <class _ForwardIterator>
2743void basic_regex<_CharT, _Traits>::__init(_ForwardIterator __first, _ForwardIterator __last) {
2744  if (__get_grammar(__flags_) == 0)
2745    __flags_ |= regex_constants::ECMAScript;
2746  _ForwardIterator __temp = __parse(__first, __last);
2747  if (__temp != __last)
2748    __throw_regex_error<regex_constants::__re_err_parse>();
2749}
2750
2751template <class _CharT, class _Traits>
2752template <class _ForwardIterator>
2753_ForwardIterator basic_regex<_CharT, _Traits>::__parse(_ForwardIterator __first, _ForwardIterator __last) {
2754  {
2755    unique_ptr<__node> __h(new __end_state<_CharT>);
2756    __start_.reset(new __empty_state<_CharT>(__h.get()));
2757    __h.release();
2758    __end_ = __start_.get();
2759  }
2760  switch (__get_grammar(__flags_)) {
2761  case ECMAScript:
2762    __first = __parse_ecma_exp(__first, __last);
2763    break;
2764  case basic:
2765    __first = __parse_basic_reg_exp(__first, __last);
2766    break;
2767  case extended:
2768  case awk:
2769    __first = __parse_extended_reg_exp(__first, __last);
2770    break;
2771  case grep:
2772    __first = __parse_grep(__first, __last);
2773    break;
2774  case egrep:
2775    __first = __parse_egrep(__first, __last);
2776    break;
2777  default:
2778    __throw_regex_error<regex_constants::__re_err_grammar>();
2779  }
2780  return __first;
2781}
2782
2783template <class _CharT, class _Traits>
2784template <class _ForwardIterator>
2785_ForwardIterator
2786basic_regex<_CharT, _Traits>::__parse_basic_reg_exp(_ForwardIterator __first, _ForwardIterator __last) {
2787  if (__first != __last) {
2788    if (*__first == '^') {
2789      __push_l_anchor();
2790      ++__first;
2791    }
2792    if (__first != __last) {
2793      __first = __parse_RE_expression(__first, __last);
2794      if (__first != __last) {
2795        _ForwardIterator __temp = std::next(__first);
2796        if (__temp == __last && *__first == '$') {
2797          __push_r_anchor();
2798          ++__first;
2799        }
2800      }
2801    }
2802    if (__first != __last)
2803      __throw_regex_error<regex_constants::__re_err_empty>();
2804  }
2805  return __first;
2806}
2807
2808template <class _CharT, class _Traits>
2809template <class _ForwardIterator>
2810_ForwardIterator
2811basic_regex<_CharT, _Traits>::__parse_extended_reg_exp(_ForwardIterator __first, _ForwardIterator __last) {
2812  __owns_one_state<_CharT>* __sa = __end_;
2813  _ForwardIterator __temp        = __parse_ERE_branch(__first, __last);
2814  if (__temp == __first)
2815    __throw_regex_error<regex_constants::__re_err_empty>();
2816  __first = __temp;
2817  while (__first != __last && *__first == '|') {
2818    __owns_one_state<_CharT>* __sb = __end_;
2819    __temp                         = __parse_ERE_branch(++__first, __last);
2820    if (__temp == __first)
2821      __throw_regex_error<regex_constants::__re_err_empty>();
2822    __push_alternation(__sa, __sb);
2823    __first = __temp;
2824  }
2825  return __first;
2826}
2827
2828template <class _CharT, class _Traits>
2829template <class _ForwardIterator>
2830_ForwardIterator basic_regex<_CharT, _Traits>::__parse_ERE_branch(_ForwardIterator __first, _ForwardIterator __last) {
2831  _ForwardIterator __temp = __parse_ERE_expression(__first, __last);
2832  if (__temp == __first)
2833    __throw_regex_error<regex_constants::__re_err_empty>();
2834  do {
2835    __first = __temp;
2836    __temp  = __parse_ERE_expression(__first, __last);
2837  } while (__temp != __first);
2838  return __first;
2839}
2840
2841template <class _CharT, class _Traits>
2842template <class _ForwardIterator>
2843_ForwardIterator
2844basic_regex<_CharT, _Traits>::__parse_ERE_expression(_ForwardIterator __first, _ForwardIterator __last) {
2845  __owns_one_state<_CharT>* __e = __end_;
2846  unsigned __mexp_begin         = __marked_count_;
2847  _ForwardIterator __temp       = __parse_one_char_or_coll_elem_ERE(__first, __last);
2848  if (__temp == __first && __temp != __last) {
2849    switch (*__temp) {
2850    case '^':
2851      __push_l_anchor();
2852      ++__temp;
2853      break;
2854    case '$':
2855      __push_r_anchor();
2856      ++__temp;
2857      break;
2858    case '(':
2859      __push_begin_marked_subexpression();
2860      unsigned __temp_count = __marked_count_;
2861      ++__open_count_;
2862      __temp = __parse_extended_reg_exp(++__temp, __last);
2863      if (__temp == __last || *__temp != ')')
2864        __throw_regex_error<regex_constants::error_paren>();
2865      __push_end_marked_subexpression(__temp_count);
2866      --__open_count_;
2867      ++__temp;
2868      break;
2869    }
2870  }
2871  if (__temp != __first)
2872    __temp = __parse_ERE_dupl_symbol(__temp, __last, __e, __mexp_begin + 1, __marked_count_ + 1);
2873  __first = __temp;
2874  return __first;
2875}
2876
2877template <class _CharT, class _Traits>
2878template <class _ForwardIterator>
2879_ForwardIterator
2880basic_regex<_CharT, _Traits>::__parse_RE_expression(_ForwardIterator __first, _ForwardIterator __last) {
2881  while (true) {
2882    _ForwardIterator __temp = __parse_simple_RE(__first, __last);
2883    if (__temp == __first)
2884      break;
2885    __first = __temp;
2886  }
2887  return __first;
2888}
2889
2890template <class _CharT, class _Traits>
2891template <class _ForwardIterator>
2892_ForwardIterator basic_regex<_CharT, _Traits>::__parse_simple_RE(_ForwardIterator __first, _ForwardIterator __last) {
2893  if (__first != __last) {
2894    __owns_one_state<_CharT>* __e = __end_;
2895    unsigned __mexp_begin         = __marked_count_;
2896    _ForwardIterator __temp       = __parse_nondupl_RE(__first, __last);
2897    if (__temp != __first)
2898      __first = __parse_RE_dupl_symbol(__temp, __last, __e, __mexp_begin + 1, __marked_count_ + 1);
2899  }
2900  return __first;
2901}
2902
2903template <class _CharT, class _Traits>
2904template <class _ForwardIterator>
2905_ForwardIterator basic_regex<_CharT, _Traits>::__parse_nondupl_RE(_ForwardIterator __first, _ForwardIterator __last) {
2906  _ForwardIterator __temp = __first;
2907  __first                 = __parse_one_char_or_coll_elem_RE(__first, __last);
2908  if (__temp == __first) {
2909    __temp = __parse_Back_open_paren(__first, __last);
2910    if (__temp != __first) {
2911      __push_begin_marked_subexpression();
2912      unsigned __temp_count = __marked_count_;
2913      __first               = __parse_RE_expression(__temp, __last);
2914      __temp                = __parse_Back_close_paren(__first, __last);
2915      if (__temp == __first)
2916        __throw_regex_error<regex_constants::error_paren>();
2917      __push_end_marked_subexpression(__temp_count);
2918      __first = __temp;
2919    } else
2920      __first = __parse_BACKREF(__first, __last);
2921  }
2922  return __first;
2923}
2924
2925template <class _CharT, class _Traits>
2926template <class _ForwardIterator>
2927_ForwardIterator
2928basic_regex<_CharT, _Traits>::__parse_one_char_or_coll_elem_RE(_ForwardIterator __first, _ForwardIterator __last) {
2929  _ForwardIterator __temp = __parse_ORD_CHAR(__first, __last);
2930  if (__temp == __first) {
2931    __temp = __parse_QUOTED_CHAR(__first, __last);
2932    if (__temp == __first) {
2933      if (__temp != __last && *__temp == '.') {
2934        __push_match_any();
2935        ++__temp;
2936      } else
2937        __temp = __parse_bracket_expression(__first, __last);
2938    }
2939  }
2940  __first = __temp;
2941  return __first;
2942}
2943
2944template <class _CharT, class _Traits>
2945template <class _ForwardIterator>
2946_ForwardIterator
2947basic_regex<_CharT, _Traits>::__parse_one_char_or_coll_elem_ERE(_ForwardIterator __first, _ForwardIterator __last) {
2948  _ForwardIterator __temp = __parse_ORD_CHAR_ERE(__first, __last);
2949  if (__temp == __first) {
2950    __temp = __parse_QUOTED_CHAR_ERE(__first, __last);
2951    if (__temp == __first) {
2952      if (__temp != __last && *__temp == '.') {
2953        __push_match_any();
2954        ++__temp;
2955      } else
2956        __temp = __parse_bracket_expression(__first, __last);
2957    }
2958  }
2959  __first = __temp;
2960  return __first;
2961}
2962
2963template <class _CharT, class _Traits>
2964template <class _ForwardIterator>
2965_ForwardIterator
2966basic_regex<_CharT, _Traits>::__parse_Back_open_paren(_ForwardIterator __first, _ForwardIterator __last) {
2967  if (__first != __last) {
2968    _ForwardIterator __temp = std::next(__first);
2969    if (__temp != __last) {
2970      if (*__first == '\\' && *__temp == '(')
2971        __first = ++__temp;
2972    }
2973  }
2974  return __first;
2975}
2976
2977template <class _CharT, class _Traits>
2978template <class _ForwardIterator>
2979_ForwardIterator
2980basic_regex<_CharT, _Traits>::__parse_Back_close_paren(_ForwardIterator __first, _ForwardIterator __last) {
2981  if (__first != __last) {
2982    _ForwardIterator __temp = std::next(__first);
2983    if (__temp != __last) {
2984      if (*__first == '\\' && *__temp == ')')
2985        __first = ++__temp;
2986    }
2987  }
2988  return __first;
2989}
2990
2991template <class _CharT, class _Traits>
2992template <class _ForwardIterator>
2993_ForwardIterator
2994basic_regex<_CharT, _Traits>::__parse_Back_open_brace(_ForwardIterator __first, _ForwardIterator __last) {
2995  if (__first != __last) {
2996    _ForwardIterator __temp = std::next(__first);
2997    if (__temp != __last) {
2998      if (*__first == '\\' && *__temp == '{')
2999        __first = ++__temp;
3000    }
3001  }
3002  return __first;
3003}
3004
3005template <class _CharT, class _Traits>
3006template <class _ForwardIterator>
3007_ForwardIterator
3008basic_regex<_CharT, _Traits>::__parse_Back_close_brace(_ForwardIterator __first, _ForwardIterator __last) {
3009  if (__first != __last) {
3010    _ForwardIterator __temp = std::next(__first);
3011    if (__temp != __last) {
3012      if (*__first == '\\' && *__temp == '}')
3013        __first = ++__temp;
3014    }
3015  }
3016  return __first;
3017}
3018
3019template <class _CharT, class _Traits>
3020template <class _ForwardIterator>
3021_ForwardIterator basic_regex<_CharT, _Traits>::__parse_BACKREF(_ForwardIterator __first, _ForwardIterator __last) {
3022  if (__first != __last) {
3023    _ForwardIterator __temp = std::next(__first);
3024    if (__temp != __last && *__first == '\\' && __test_back_ref(*__temp))
3025      __first = ++__temp;
3026  }
3027  return __first;
3028}
3029
3030template <class _CharT, class _Traits>
3031template <class _ForwardIterator>
3032_ForwardIterator basic_regex<_CharT, _Traits>::__parse_ORD_CHAR(_ForwardIterator __first, _ForwardIterator __last) {
3033  if (__first != __last) {
3034    _ForwardIterator __temp = std::next(__first);
3035    if (__temp == __last && *__first == '$')
3036      return __first;
3037    // Not called inside a bracket
3038    if (*__first == '.' || *__first == '\\' || *__first == '[')
3039      return __first;
3040    __push_char(*__first);
3041    ++__first;
3042  }
3043  return __first;
3044}
3045
3046template <class _CharT, class _Traits>
3047template <class _ForwardIterator>
3048_ForwardIterator basic_regex<_CharT, _Traits>::__parse_ORD_CHAR_ERE(_ForwardIterator __first, _ForwardIterator __last) {
3049  if (__first != __last) {
3050    switch (*__first) {
3051    case '^':
3052    case '.':
3053    case '[':
3054    case '$':
3055    case '(':
3056    case '|':
3057    case '*':
3058    case '+':
3059    case '?':
3060    case '{':
3061    case '\\':
3062      break;
3063    case ')':
3064      if (__open_count_ == 0) {
3065        __push_char(*__first);
3066        ++__first;
3067      }
3068      break;
3069    default:
3070      __push_char(*__first);
3071      ++__first;
3072      break;
3073    }
3074  }
3075  return __first;
3076}
3077
3078template <class _CharT, class _Traits>
3079template <class _ForwardIterator>
3080_ForwardIterator basic_regex<_CharT, _Traits>::__parse_QUOTED_CHAR(_ForwardIterator __first, _ForwardIterator __last) {
3081  if (__first != __last) {
3082    _ForwardIterator __temp = std::next(__first);
3083    if (__temp != __last) {
3084      if (*__first == '\\') {
3085        switch (*__temp) {
3086        case '^':
3087        case '.':
3088        case '*':
3089        case '[':
3090        case '$':
3091        case '\\':
3092          __push_char(*__temp);
3093          __first = ++__temp;
3094          break;
3095        }
3096      }
3097    }
3098  }
3099  return __first;
3100}
3101
3102template <class _CharT, class _Traits>
3103template <class _ForwardIterator>
3104_ForwardIterator
3105basic_regex<_CharT, _Traits>::__parse_QUOTED_CHAR_ERE(_ForwardIterator __first, _ForwardIterator __last) {
3106  if (__first != __last) {
3107    _ForwardIterator __temp = std::next(__first);
3108    if (__temp != __last) {
3109      if (*__first == '\\') {
3110        switch (*__temp) {
3111        case '^':
3112        case '.':
3113        case '*':
3114        case '[':
3115        case '$':
3116        case '\\':
3117        case '(':
3118        case ')':
3119        case '|':
3120        case '+':
3121        case '?':
3122        case '{':
3123        case '}':
3124          __push_char(*__temp);
3125          __first = ++__temp;
3126          break;
3127        default:
3128          if (__get_grammar(__flags_) == awk)
3129            __first = __parse_awk_escape(++__first, __last);
3130          else if (__test_back_ref(*__temp))
3131            __first = ++__temp;
3132          break;
3133        }
3134      }
3135    }
3136  }
3137  return __first;
3138}
3139
3140template <class _CharT, class _Traits>
3141template <class _ForwardIterator>
3142_ForwardIterator basic_regex<_CharT, _Traits>::__parse_RE_dupl_symbol(
3143    _ForwardIterator __first,
3144    _ForwardIterator __last,
3145    __owns_one_state<_CharT>* __s,
3146    unsigned __mexp_begin,
3147    unsigned __mexp_end) {
3148  if (__first != __last) {
3149    if (*__first == '*') {
3150      __push_greedy_inf_repeat(0, __s, __mexp_begin, __mexp_end);
3151      ++__first;
3152    } else {
3153      _ForwardIterator __temp = __parse_Back_open_brace(__first, __last);
3154      if (__temp != __first) {
3155        int __min = 0;
3156        __first   = __temp;
3157        __temp    = __parse_DUP_COUNT(__first, __last, __min);
3158        if (__temp == __first)
3159          __throw_regex_error<regex_constants::error_badbrace>();
3160        __first = __temp;
3161        if (__first == __last)
3162          __throw_regex_error<regex_constants::error_brace>();
3163        if (*__first != ',') {
3164          __temp = __parse_Back_close_brace(__first, __last);
3165          if (__temp == __first)
3166            __throw_regex_error<regex_constants::error_brace>();
3167          __push_loop(__min, __min, __s, __mexp_begin, __mexp_end, true);
3168          __first = __temp;
3169        } else {
3170          ++__first; // consume ','
3171          int __max = -1;
3172          __first   = __parse_DUP_COUNT(__first, __last, __max);
3173          __temp    = __parse_Back_close_brace(__first, __last);
3174          if (__temp == __first)
3175            __throw_regex_error<regex_constants::error_brace>();
3176          if (__max == -1)
3177            __push_greedy_inf_repeat(__min, __s, __mexp_begin, __mexp_end);
3178          else {
3179            if (__max < __min)
3180              __throw_regex_error<regex_constants::error_badbrace>();
3181            __push_loop(__min, __max, __s, __mexp_begin, __mexp_end, true);
3182          }
3183          __first = __temp;
3184        }
3185      }
3186    }
3187  }
3188  return __first;
3189}
3190
3191template <class _CharT, class _Traits>
3192template <class _ForwardIterator>
3193_ForwardIterator basic_regex<_CharT, _Traits>::__parse_ERE_dupl_symbol(
3194    _ForwardIterator __first,
3195    _ForwardIterator __last,
3196    __owns_one_state<_CharT>* __s,
3197    unsigned __mexp_begin,
3198    unsigned __mexp_end) {
3199  if (__first != __last) {
3200    unsigned __grammar = __get_grammar(__flags_);
3201    switch (*__first) {
3202    case '*':
3203      ++__first;
3204      if (__grammar == ECMAScript && __first != __last && *__first == '?') {
3205        ++__first;
3206        __push_nongreedy_inf_repeat(0, __s, __mexp_begin, __mexp_end);
3207      } else
3208        __push_greedy_inf_repeat(0, __s, __mexp_begin, __mexp_end);
3209      break;
3210    case '+':
3211      ++__first;
3212      if (__grammar == ECMAScript && __first != __last && *__first == '?') {
3213        ++__first;
3214        __push_nongreedy_inf_repeat(1, __s, __mexp_begin, __mexp_end);
3215      } else
3216        __push_greedy_inf_repeat(1, __s, __mexp_begin, __mexp_end);
3217      break;
3218    case '?':
3219      ++__first;
3220      if (__grammar == ECMAScript && __first != __last && *__first == '?') {
3221        ++__first;
3222        __push_loop(0, 1, __s, __mexp_begin, __mexp_end, false);
3223      } else
3224        __push_loop(0, 1, __s, __mexp_begin, __mexp_end);
3225      break;
3226    case '{': {
3227      int __min;
3228      _ForwardIterator __temp = __parse_DUP_COUNT(++__first, __last, __min);
3229      if (__temp == __first)
3230        __throw_regex_error<regex_constants::error_badbrace>();
3231      __first = __temp;
3232      if (__first == __last)
3233        __throw_regex_error<regex_constants::error_brace>();
3234      switch (*__first) {
3235      case '}':
3236        ++__first;
3237        if (__grammar == ECMAScript && __first != __last && *__first == '?') {
3238          ++__first;
3239          __push_loop(__min, __min, __s, __mexp_begin, __mexp_end, false);
3240        } else
3241          __push_loop(__min, __min, __s, __mexp_begin, __mexp_end);
3242        break;
3243      case ',':
3244        ++__first;
3245        if (__first == __last)
3246          __throw_regex_error<regex_constants::error_badbrace>();
3247        if (*__first == '}') {
3248          ++__first;
3249          if (__grammar == ECMAScript && __first != __last && *__first == '?') {
3250            ++__first;
3251            __push_nongreedy_inf_repeat(__min, __s, __mexp_begin, __mexp_end);
3252          } else
3253            __push_greedy_inf_repeat(__min, __s, __mexp_begin, __mexp_end);
3254        } else {
3255          int __max = -1;
3256          __temp    = __parse_DUP_COUNT(__first, __last, __max);
3257          if (__temp == __first)
3258            __throw_regex_error<regex_constants::error_brace>();
3259          __first = __temp;
3260          if (__first == __last || *__first != '}')
3261            __throw_regex_error<regex_constants::error_brace>();
3262          ++__first;
3263          if (__max < __min)
3264            __throw_regex_error<regex_constants::error_badbrace>();
3265          if (__grammar == ECMAScript && __first != __last && *__first == '?') {
3266            ++__first;
3267            __push_loop(__min, __max, __s, __mexp_begin, __mexp_end, false);
3268          } else
3269            __push_loop(__min, __max, __s, __mexp_begin, __mexp_end);
3270        }
3271        break;
3272      default:
3273        __throw_regex_error<regex_constants::error_badbrace>();
3274      }
3275    } break;
3276    }
3277  }
3278  return __first;
3279}
3280
3281template <class _CharT, class _Traits>
3282template <class _ForwardIterator>
3283_ForwardIterator
3284basic_regex<_CharT, _Traits>::__parse_bracket_expression(_ForwardIterator __first, _ForwardIterator __last) {
3285  if (__first != __last && *__first == '[') {
3286    ++__first;
3287    if (__first == __last)
3288      __throw_regex_error<regex_constants::error_brack>();
3289    bool __negate = false;
3290    if (*__first == '^') {
3291      ++__first;
3292      __negate = true;
3293    }
3294    __bracket_expression<_CharT, _Traits>* __ml = __start_matching_list(__negate);
3295    // __ml owned by *this
3296    if (__first == __last)
3297      __throw_regex_error<regex_constants::error_brack>();
3298    if (__get_grammar(__flags_) != ECMAScript && *__first == ']') {
3299      __ml->__add_char(']');
3300      ++__first;
3301    }
3302    __first = __parse_follow_list(__first, __last, __ml);
3303    if (__first == __last)
3304      __throw_regex_error<regex_constants::error_brack>();
3305    if (*__first == '-') {
3306      __ml->__add_char('-');
3307      ++__first;
3308    }
3309    if (__first == __last || *__first != ']')
3310      __throw_regex_error<regex_constants::error_brack>();
3311    ++__first;
3312  }
3313  return __first;
3314}
3315
3316template <class _CharT, class _Traits>
3317template <class _ForwardIterator>
3318_ForwardIterator basic_regex<_CharT, _Traits>::__parse_follow_list(
3319    _ForwardIterator __first, _ForwardIterator __last, __bracket_expression<_CharT, _Traits>* __ml) {
3320  if (__first != __last) {
3321    while (true) {
3322      _ForwardIterator __temp = __parse_expression_term(__first, __last, __ml);
3323      if (__temp == __first)
3324        break;
3325      __first = __temp;
3326    }
3327  }
3328  return __first;
3329}
3330
3331template <class _CharT, class _Traits>
3332template <class _ForwardIterator>
3333_ForwardIterator basic_regex<_CharT, _Traits>::__parse_expression_term(
3334    _ForwardIterator __first, _ForwardIterator __last, __bracket_expression<_CharT, _Traits>* __ml) {
3335  if (__first != __last && *__first != ']') {
3336    _ForwardIterator __temp = std::next(__first);
3337    basic_string<_CharT> __start_range;
3338    if (__temp != __last && *__first == '[') {
3339      if (*__temp == '=')
3340        return __parse_equivalence_class(++__temp, __last, __ml);
3341      else if (*__temp == ':')
3342        return __parse_character_class(++__temp, __last, __ml);
3343      else if (*__temp == '.')
3344        __first = __parse_collating_symbol(++__temp, __last, __start_range);
3345    }
3346    unsigned __grammar = __get_grammar(__flags_);
3347    if (__start_range.empty()) {
3348      if ((__grammar == ECMAScript || __grammar == awk) && *__first == '\\') {
3349        if (__grammar == ECMAScript)
3350          __first = __parse_class_escape(++__first, __last, __start_range, __ml);
3351        else
3352          __first = __parse_awk_escape(++__first, __last, &__start_range);
3353      } else {
3354        __start_range = *__first;
3355        ++__first;
3356      }
3357    }
3358    if (__first != __last && *__first != ']') {
3359      __temp = std::next(__first);
3360      if (__temp != __last && *__first == '-' && *__temp != ']') {
3361        // parse a range
3362        basic_string<_CharT> __end_range;
3363        __first = __temp;
3364        ++__temp;
3365        if (__temp != __last && *__first == '[' && *__temp == '.')
3366          __first = __parse_collating_symbol(++__temp, __last, __end_range);
3367        else {
3368          if ((__grammar == ECMAScript || __grammar == awk) && *__first == '\\') {
3369            if (__grammar == ECMAScript)
3370              __first = __parse_class_escape(++__first, __last, __end_range, __ml);
3371            else
3372              __first = __parse_awk_escape(++__first, __last, &__end_range);
3373          } else {
3374            __end_range = *__first;
3375            ++__first;
3376          }
3377        }
3378        __ml->__add_range(std::move(__start_range), std::move(__end_range));
3379      } else if (!__start_range.empty()) {
3380        if (__start_range.size() == 1)
3381          __ml->__add_char(__start_range[0]);
3382        else
3383          __ml->__add_digraph(__start_range[0], __start_range[1]);
3384      }
3385    } else if (!__start_range.empty()) {
3386      if (__start_range.size() == 1)
3387        __ml->__add_char(__start_range[0]);
3388      else
3389        __ml->__add_digraph(__start_range[0], __start_range[1]);
3390    }
3391  }
3392  return __first;
3393}
3394
3395template <class _CharT, class _Traits>
3396template <class _ForwardIterator>
3397_ForwardIterator basic_regex<_CharT, _Traits>::__parse_class_escape(
3398    _ForwardIterator __first,
3399    _ForwardIterator __last,
3400    basic_string<_CharT>& __str,
3401    __bracket_expression<_CharT, _Traits>* __ml) {
3402  if (__first == __last)
3403    __throw_regex_error<regex_constants::error_escape>();
3404  switch (*__first) {
3405  case 0:
3406    __str = *__first;
3407    return ++__first;
3408  case 'b':
3409    __str = _CharT(8);
3410    return ++__first;
3411  case 'd':
3412    __ml->__add_class(ctype_base::digit);
3413    return ++__first;
3414  case 'D':
3415    __ml->__add_neg_class(ctype_base::digit);
3416    return ++__first;
3417  case 's':
3418    __ml->__add_class(ctype_base::space);
3419    return ++__first;
3420  case 'S':
3421    __ml->__add_neg_class(ctype_base::space);
3422    return ++__first;
3423  case 'w':
3424    __ml->__add_class(ctype_base::alnum);
3425    __ml->__add_char('_');
3426    return ++__first;
3427  case 'W':
3428    __ml->__add_neg_class(ctype_base::alnum);
3429    __ml->__add_neg_char('_');
3430    return ++__first;
3431  }
3432  __first = __parse_character_escape(__first, __last, &__str);
3433  return __first;
3434}
3435
3436template <class _CharT, class _Traits>
3437template <class _ForwardIterator>
3438_ForwardIterator basic_regex<_CharT, _Traits>::__parse_awk_escape(
3439    _ForwardIterator __first, _ForwardIterator __last, basic_string<_CharT>* __str) {
3440  if (__first == __last)
3441    __throw_regex_error<regex_constants::error_escape>();
3442  switch (*__first) {
3443  case '\\':
3444  case '"':
3445  case '/':
3446    if (__str)
3447      *__str = *__first;
3448    else
3449      __push_char(*__first);
3450    return ++__first;
3451  case 'a':
3452    if (__str)
3453      *__str = _CharT(7);
3454    else
3455      __push_char(_CharT(7));
3456    return ++__first;
3457  case 'b':
3458    if (__str)
3459      *__str = _CharT(8);
3460    else
3461      __push_char(_CharT(8));
3462    return ++__first;
3463  case 'f':
3464    if (__str)
3465      *__str = _CharT(0xC);
3466    else
3467      __push_char(_CharT(0xC));
3468    return ++__first;
3469  case 'n':
3470    if (__str)
3471      *__str = _CharT(0xA);
3472    else
3473      __push_char(_CharT(0xA));
3474    return ++__first;
3475  case 'r':
3476    if (__str)
3477      *__str = _CharT(0xD);
3478    else
3479      __push_char(_CharT(0xD));
3480    return ++__first;
3481  case 't':
3482    if (__str)
3483      *__str = _CharT(0x9);
3484    else
3485      __push_char(_CharT(0x9));
3486    return ++__first;
3487  case 'v':
3488    if (__str)
3489      *__str = _CharT(0xB);
3490    else
3491      __push_char(_CharT(0xB));
3492    return ++__first;
3493  }
3494  if ('0' <= *__first && *__first <= '7') {
3495    unsigned __val = *__first - '0';
3496    if (++__first != __last && ('0' <= *__first && *__first <= '7')) {
3497      __val = 8 * __val + *__first - '0';
3498      if (++__first != __last && ('0' <= *__first && *__first <= '7'))
3499        __val = 8 * __val + *__first++ - '0';
3500    }
3501    if (__str)
3502      *__str = _CharT(__val);
3503    else
3504      __push_char(_CharT(__val));
3505  } else
3506    __throw_regex_error<regex_constants::error_escape>();
3507  return __first;
3508}
3509
3510template <class _CharT, class _Traits>
3511template <class _ForwardIterator>
3512_ForwardIterator basic_regex<_CharT, _Traits>::__parse_equivalence_class(
3513    _ForwardIterator __first, _ForwardIterator __last, __bracket_expression<_CharT, _Traits>* __ml) {
3514  // Found [=
3515  //   This means =] must exist
3516  value_type __equal_close[2] = {'=', ']'};
3517  _ForwardIterator __temp     = std::search(__first, __last, __equal_close, __equal_close + 2);
3518  if (__temp == __last)
3519    __throw_regex_error<regex_constants::error_brack>();
3520  // [__first, __temp) contains all text in [= ... =]
3521  string_type __collate_name = __traits_.lookup_collatename(__first, __temp);
3522  if (__collate_name.empty())
3523    __throw_regex_error<regex_constants::error_collate>();
3524  string_type __equiv_name = __traits_.transform_primary(__collate_name.begin(), __collate_name.end());
3525  if (!__equiv_name.empty())
3526    __ml->__add_equivalence(__equiv_name);
3527  else {
3528    switch (__collate_name.size()) {
3529    case 1:
3530      __ml->__add_char(__collate_name[0]);
3531      break;
3532    case 2:
3533      __ml->__add_digraph(__collate_name[0], __collate_name[1]);
3534      break;
3535    default:
3536      __throw_regex_error<regex_constants::error_collate>();
3537    }
3538  }
3539  __first = std::next(__temp, 2);
3540  return __first;
3541}
3542
3543template <class _CharT, class _Traits>
3544template <class _ForwardIterator>
3545_ForwardIterator basic_regex<_CharT, _Traits>::__parse_character_class(
3546    _ForwardIterator __first, _ForwardIterator __last, __bracket_expression<_CharT, _Traits>* __ml) {
3547  // Found [:
3548  //   This means :] must exist
3549  value_type __colon_close[2] = {':', ']'};
3550  _ForwardIterator __temp     = std::search(__first, __last, __colon_close, __colon_close + 2);
3551  if (__temp == __last)
3552    __throw_regex_error<regex_constants::error_brack>();
3553  // [__first, __temp) contains all text in [: ... :]
3554  typedef typename _Traits::char_class_type char_class_type;
3555  char_class_type __class_type = __traits_.lookup_classname(__first, __temp, __flags_ & icase);
3556  if (__class_type == 0)
3557    __throw_regex_error<regex_constants::error_ctype>();
3558  __ml->__add_class(__class_type);
3559  __first = std::next(__temp, 2);
3560  return __first;
3561}
3562
3563template <class _CharT, class _Traits>
3564template <class _ForwardIterator>
3565_ForwardIterator basic_regex<_CharT, _Traits>::__parse_collating_symbol(
3566    _ForwardIterator __first, _ForwardIterator __last, basic_string<_CharT>& __col_sym) {
3567  // Found [.
3568  //   This means .] must exist
3569  value_type __dot_close[2] = {'.', ']'};
3570  _ForwardIterator __temp   = std::search(__first, __last, __dot_close, __dot_close + 2);
3571  if (__temp == __last)
3572    __throw_regex_error<regex_constants::error_brack>();
3573  // [__first, __temp) contains all text in [. ... .]
3574  __col_sym = __traits_.lookup_collatename(__first, __temp);
3575  switch (__col_sym.size()) {
3576  case 1:
3577  case 2:
3578    break;
3579  default:
3580    __throw_regex_error<regex_constants::error_collate>();
3581  }
3582  __first = std::next(__temp, 2);
3583  return __first;
3584}
3585
3586template <class _CharT, class _Traits>
3587template <class _ForwardIterator>
3588_ForwardIterator
3589basic_regex<_CharT, _Traits>::__parse_DUP_COUNT(_ForwardIterator __first, _ForwardIterator __last, int& __c) {
3590  if (__first != __last) {
3591    int __val = __traits_.value(*__first, 10);
3592    if (__val != -1) {
3593      __c = __val;
3594      for (++__first; __first != __last && (__val = __traits_.value(*__first, 10)) != -1; ++__first) {
3595        if (__c >= numeric_limits<int>::max() / 10)
3596          __throw_regex_error<regex_constants::error_badbrace>();
3597        __c *= 10;
3598        __c += __val;
3599      }
3600    }
3601  }
3602  return __first;
3603}
3604
3605template <class _CharT, class _Traits>
3606template <class _ForwardIterator>
3607_ForwardIterator basic_regex<_CharT, _Traits>::__parse_ecma_exp(_ForwardIterator __first, _ForwardIterator __last) {
3608  __owns_one_state<_CharT>* __sa = __end_;
3609  _ForwardIterator __temp        = __parse_alternative(__first, __last);
3610  if (__temp == __first)
3611    __push_empty();
3612  __first = __temp;
3613  while (__first != __last && *__first == '|') {
3614    __owns_one_state<_CharT>* __sb = __end_;
3615    __temp                         = __parse_alternative(++__first, __last);
3616    if (__temp == __first)
3617      __push_empty();
3618    __push_alternation(__sa, __sb);
3619    __first = __temp;
3620  }
3621  return __first;
3622}
3623
3624template <class _CharT, class _Traits>
3625template <class _ForwardIterator>
3626_ForwardIterator basic_regex<_CharT, _Traits>::__parse_alternative(_ForwardIterator __first, _ForwardIterator __last) {
3627  while (true) {
3628    _ForwardIterator __temp = __parse_term(__first, __last);
3629    if (__temp == __first)
3630      break;
3631    __first = __temp;
3632  }
3633  return __first;
3634}
3635
3636template <class _CharT, class _Traits>
3637template <class _ForwardIterator>
3638_ForwardIterator basic_regex<_CharT, _Traits>::__parse_term(_ForwardIterator __first, _ForwardIterator __last) {
3639  _ForwardIterator __temp = __parse_assertion(__first, __last);
3640  if (__temp == __first) {
3641    __owns_one_state<_CharT>* __e = __end_;
3642    unsigned __mexp_begin         = __marked_count_;
3643    __temp                        = __parse_atom(__first, __last);
3644    if (__temp != __first)
3645      __first = __parse_ERE_dupl_symbol(__temp, __last, __e, __mexp_begin + 1, __marked_count_ + 1);
3646  } else
3647    __first = __temp;
3648  return __first;
3649}
3650
3651template <class _CharT, class _Traits>
3652template <class _ForwardIterator>
3653_ForwardIterator basic_regex<_CharT, _Traits>::__parse_assertion(_ForwardIterator __first, _ForwardIterator __last) {
3654  if (__first != __last) {
3655    switch (*__first) {
3656    case '^':
3657      __push_l_anchor();
3658      ++__first;
3659      break;
3660    case '$':
3661      __push_r_anchor();
3662      ++__first;
3663      break;
3664    case '\\': {
3665      _ForwardIterator __temp = std::next(__first);
3666      if (__temp != __last) {
3667        if (*__temp == 'b') {
3668          __push_word_boundary(false);
3669          __first = ++__temp;
3670        } else if (*__temp == 'B') {
3671          __push_word_boundary(true);
3672          __first = ++__temp;
3673        }
3674      }
3675    } break;
3676    case '(': {
3677      _ForwardIterator __temp = std::next(__first);
3678      if (__temp != __last && *__temp == '?') {
3679        if (++__temp != __last) {
3680          switch (*__temp) {
3681          case '=': {
3682            basic_regex __exp;
3683            __exp.__flags_  = __flags_;
3684            __temp          = __exp.__parse(++__temp, __last);
3685            unsigned __mexp = __exp.__marked_count_;
3686            __push_lookahead(std::move(__exp), false, __marked_count_);
3687            __marked_count_ += __mexp;
3688            if (__temp == __last || *__temp != ')')
3689              __throw_regex_error<regex_constants::error_paren>();
3690            __first = ++__temp;
3691          } break;
3692          case '!': {
3693            basic_regex __exp;
3694            __exp.__flags_  = __flags_;
3695            __temp          = __exp.__parse(++__temp, __last);
3696            unsigned __mexp = __exp.__marked_count_;
3697            __push_lookahead(std::move(__exp), true, __marked_count_);
3698            __marked_count_ += __mexp;
3699            if (__temp == __last || *__temp != ')')
3700              __throw_regex_error<regex_constants::error_paren>();
3701            __first = ++__temp;
3702          } break;
3703          }
3704        }
3705      }
3706    } break;
3707    }
3708  }
3709  return __first;
3710}
3711
3712template <class _CharT, class _Traits>
3713template <class _ForwardIterator>
3714_ForwardIterator basic_regex<_CharT, _Traits>::__parse_atom(_ForwardIterator __first, _ForwardIterator __last) {
3715  if (__first != __last) {
3716    switch (*__first) {
3717    case '.':
3718      __push_match_any_but_newline();
3719      ++__first;
3720      break;
3721    case '\\':
3722      __first = __parse_atom_escape(__first, __last);
3723      break;
3724    case '[':
3725      __first = __parse_bracket_expression(__first, __last);
3726      break;
3727    case '(': {
3728      ++__first;
3729      if (__first == __last)
3730        __throw_regex_error<regex_constants::error_paren>();
3731      _ForwardIterator __temp = std::next(__first);
3732      if (__temp != __last && *__first == '?' && *__temp == ':') {
3733        ++__open_count_;
3734        __first = __parse_ecma_exp(++__temp, __last);
3735        if (__first == __last || *__first != ')')
3736          __throw_regex_error<regex_constants::error_paren>();
3737        --__open_count_;
3738        ++__first;
3739      } else {
3740        __push_begin_marked_subexpression();
3741        unsigned __temp_count = __marked_count_;
3742        ++__open_count_;
3743        __first = __parse_ecma_exp(__first, __last);
3744        if (__first == __last || *__first != ')')
3745          __throw_regex_error<regex_constants::error_paren>();
3746        __push_end_marked_subexpression(__temp_count);
3747        --__open_count_;
3748        ++__first;
3749      }
3750    } break;
3751    case '*':
3752    case '+':
3753    case '?':
3754    case '{':
3755      __throw_regex_error<regex_constants::error_badrepeat>();
3756      break;
3757    default:
3758      __first = __parse_pattern_character(__first, __last);
3759      break;
3760    }
3761  }
3762  return __first;
3763}
3764
3765template <class _CharT, class _Traits>
3766template <class _ForwardIterator>
3767_ForwardIterator basic_regex<_CharT, _Traits>::__parse_atom_escape(_ForwardIterator __first, _ForwardIterator __last) {
3768  if (__first != __last && *__first == '\\') {
3769    _ForwardIterator __t1 = std::next(__first);
3770    if (__t1 == __last)
3771      __throw_regex_error<regex_constants::error_escape>();
3772
3773    _ForwardIterator __t2 = __parse_decimal_escape(__t1, __last);
3774    if (__t2 != __t1)
3775      __first = __t2;
3776    else {
3777      __t2 = __parse_character_class_escape(__t1, __last);
3778      if (__t2 != __t1)
3779        __first = __t2;
3780      else {
3781        __t2 = __parse_character_escape(__t1, __last);
3782        if (__t2 != __t1)
3783          __first = __t2;
3784      }
3785    }
3786  }
3787  return __first;
3788}
3789
3790template <class _CharT, class _Traits>
3791template <class _ForwardIterator>
3792_ForwardIterator
3793basic_regex<_CharT, _Traits>::__parse_decimal_escape(_ForwardIterator __first, _ForwardIterator __last) {
3794  if (__first != __last) {
3795    if (*__first == '0') {
3796      __push_char(_CharT());
3797      ++__first;
3798    } else if ('1' <= *__first && *__first <= '9') {
3799      unsigned __v = *__first - '0';
3800      for (++__first; __first != __last && '0' <= *__first && *__first <= '9'; ++__first) {
3801        if (__v >= numeric_limits<unsigned>::max() / 10)
3802          __throw_regex_error<regex_constants::error_backref>();
3803        __v = 10 * __v + *__first - '0';
3804      }
3805      if (__v == 0 || __v > mark_count())
3806        __throw_regex_error<regex_constants::error_backref>();
3807      __push_back_ref(__v);
3808    }
3809  }
3810  return __first;
3811}
3812
3813template <class _CharT, class _Traits>
3814template <class _ForwardIterator>
3815_ForwardIterator
3816basic_regex<_CharT, _Traits>::__parse_character_class_escape(_ForwardIterator __first, _ForwardIterator __last) {
3817  if (__first != __last) {
3818    __bracket_expression<_CharT, _Traits>* __ml;
3819    switch (*__first) {
3820    case 'd':
3821      __ml = __start_matching_list(false);
3822      __ml->__add_class(ctype_base::digit);
3823      ++__first;
3824      break;
3825    case 'D':
3826      __ml = __start_matching_list(true);
3827      __ml->__add_class(ctype_base::digit);
3828      ++__first;
3829      break;
3830    case 's':
3831      __ml = __start_matching_list(false);
3832      __ml->__add_class(ctype_base::space);
3833      ++__first;
3834      break;
3835    case 'S':
3836      __ml = __start_matching_list(true);
3837      __ml->__add_class(ctype_base::space);
3838      ++__first;
3839      break;
3840    case 'w':
3841      __ml = __start_matching_list(false);
3842      __ml->__add_class(ctype_base::alnum);
3843      __ml->__add_char('_');
3844      ++__first;
3845      break;
3846    case 'W':
3847      __ml = __start_matching_list(true);
3848      __ml->__add_class(ctype_base::alnum);
3849      __ml->__add_char('_');
3850      ++__first;
3851      break;
3852    }
3853  }
3854  return __first;
3855}
3856
3857template <class _CharT, class _Traits>
3858template <class _ForwardIterator>
3859_ForwardIterator basic_regex<_CharT, _Traits>::__parse_character_escape(
3860    _ForwardIterator __first, _ForwardIterator __last, basic_string<_CharT>* __str) {
3861  if (__first != __last) {
3862    _ForwardIterator __t;
3863    unsigned __sum = 0;
3864    int __hd;
3865    switch (*__first) {
3866    case 'f':
3867      if (__str)
3868        *__str = _CharT(0xC);
3869      else
3870        __push_char(_CharT(0xC));
3871      ++__first;
3872      break;
3873    case 'n':
3874      if (__str)
3875        *__str = _CharT(0xA);
3876      else
3877        __push_char(_CharT(0xA));
3878      ++__first;
3879      break;
3880    case 'r':
3881      if (__str)
3882        *__str = _CharT(0xD);
3883      else
3884        __push_char(_CharT(0xD));
3885      ++__first;
3886      break;
3887    case 't':
3888      if (__str)
3889        *__str = _CharT(0x9);
3890      else
3891        __push_char(_CharT(0x9));
3892      ++__first;
3893      break;
3894    case 'v':
3895      if (__str)
3896        *__str = _CharT(0xB);
3897      else
3898        __push_char(_CharT(0xB));
3899      ++__first;
3900      break;
3901    case 'c':
3902      if ((__t = std::next(__first)) != __last) {
3903        if (('A' <= *__t && *__t <= 'Z') || ('a' <= *__t && *__t <= 'z')) {
3904          if (__str)
3905            *__str = _CharT(*__t % 32);
3906          else
3907            __push_char(_CharT(*__t % 32));
3908          __first = ++__t;
3909        } else
3910          __throw_regex_error<regex_constants::error_escape>();
3911      } else
3912        __throw_regex_error<regex_constants::error_escape>();
3913      break;
3914    case 'u':
3915      ++__first;
3916      if (__first == __last)
3917        __throw_regex_error<regex_constants::error_escape>();
3918      __hd = __traits_.value(*__first, 16);
3919      if (__hd == -1)
3920        __throw_regex_error<regex_constants::error_escape>();
3921      __sum = 16 * __sum + static_cast<unsigned>(__hd);
3922      ++__first;
3923      if (__first == __last)
3924        __throw_regex_error<regex_constants::error_escape>();
3925      __hd = __traits_.value(*__first, 16);
3926      if (__hd == -1)
3927        __throw_regex_error<regex_constants::error_escape>();
3928      __sum = 16 * __sum + static_cast<unsigned>(__hd);
3929      // fallthrough
3930    case 'x':
3931      ++__first;
3932      if (__first == __last)
3933        __throw_regex_error<regex_constants::error_escape>();
3934      __hd = __traits_.value(*__first, 16);
3935      if (__hd == -1)
3936        __throw_regex_error<regex_constants::error_escape>();
3937      __sum = 16 * __sum + static_cast<unsigned>(__hd);
3938      ++__first;
3939      if (__first == __last)
3940        __throw_regex_error<regex_constants::error_escape>();
3941      __hd = __traits_.value(*__first, 16);
3942      if (__hd == -1)
3943        __throw_regex_error<regex_constants::error_escape>();
3944      __sum = 16 * __sum + static_cast<unsigned>(__hd);
3945      if (__str)
3946        *__str = _CharT(__sum);
3947      else
3948        __push_char(_CharT(__sum));
3949      ++__first;
3950      break;
3951    case '0':
3952      if (__str)
3953        *__str = _CharT(0);
3954      else
3955        __push_char(_CharT(0));
3956      ++__first;
3957      break;
3958    default:
3959      if (*__first != '_' && !__traits_.isctype(*__first, ctype_base::alnum)) {
3960        if (__str)
3961          *__str = *__first;
3962        else
3963          __push_char(*__first);
3964        ++__first;
3965      } else
3966        __throw_regex_error<regex_constants::error_escape>();
3967      break;
3968    }
3969  }
3970  return __first;
3971}
3972
3973template <class _CharT, class _Traits>
3974template <class _ForwardIterator>
3975_ForwardIterator
3976basic_regex<_CharT, _Traits>::__parse_pattern_character(_ForwardIterator __first, _ForwardIterator __last) {
3977  if (__first != __last) {
3978    switch (*__first) {
3979    case '^':
3980    case '$':
3981    case '\\':
3982    case '.':
3983    case '*':
3984    case '+':
3985    case '?':
3986    case '(':
3987    case ')':
3988    case '[':
3989    case ']':
3990    case '{':
3991    case '}':
3992    case '|':
3993      break;
3994    default:
3995      __push_char(*__first);
3996      ++__first;
3997      break;
3998    }
3999  }
4000  return __first;
4001}
4002
4003template <class _CharT, class _Traits>
4004template <class _ForwardIterator>
4005_ForwardIterator basic_regex<_CharT, _Traits>::__parse_grep(_ForwardIterator __first, _ForwardIterator __last) {
4006  __owns_one_state<_CharT>* __sa = __end_;
4007  _ForwardIterator __t1          = std::find(__first, __last, _CharT('\n'));
4008  if (__t1 != __first)
4009    __parse_basic_reg_exp(__first, __t1);
4010  else
4011    __push_empty();
4012  __first = __t1;
4013  if (__first != __last)
4014    ++__first;
4015  while (__first != __last) {
4016    __t1                           = std::find(__first, __last, _CharT('\n'));
4017    __owns_one_state<_CharT>* __sb = __end_;
4018    if (__t1 != __first)
4019      __parse_basic_reg_exp(__first, __t1);
4020    else
4021      __push_empty();
4022    __push_alternation(__sa, __sb);
4023    __first = __t1;
4024    if (__first != __last)
4025      ++__first;
4026  }
4027  return __first;
4028}
4029
4030template <class _CharT, class _Traits>
4031template <class _ForwardIterator>
4032_ForwardIterator basic_regex<_CharT, _Traits>::__parse_egrep(_ForwardIterator __first, _ForwardIterator __last) {
4033  __owns_one_state<_CharT>* __sa = __end_;
4034  _ForwardIterator __t1          = std::find(__first, __last, _CharT('\n'));
4035  if (__t1 != __first)
4036    __parse_extended_reg_exp(__first, __t1);
4037  else
4038    __push_empty();
4039  __first = __t1;
4040  if (__first != __last)
4041    ++__first;
4042  while (__first != __last) {
4043    __t1                           = std::find(__first, __last, _CharT('\n'));
4044    __owns_one_state<_CharT>* __sb = __end_;
4045    if (__t1 != __first)
4046      __parse_extended_reg_exp(__first, __t1);
4047    else
4048      __push_empty();
4049    __push_alternation(__sa, __sb);
4050    __first = __t1;
4051    if (__first != __last)
4052      ++__first;
4053  }
4054  return __first;
4055}
4056
4057template <class _CharT, class _Traits>
4058bool basic_regex<_CharT, _Traits>::__test_back_ref(_CharT __c) {
4059  unsigned __val = __traits_.value(__c, 10);
4060  if (__val >= 1 && __val <= 9) {
4061    if (__val > mark_count())
4062      __throw_regex_error<regex_constants::error_backref>();
4063    __push_back_ref(__val);
4064    return true;
4065  }
4066
4067  return false;
4068}
4069
4070template <class _CharT, class _Traits>
4071void basic_regex<_CharT, _Traits>::__push_loop(
4072    size_t __min, size_t __max, __owns_one_state<_CharT>* __s, size_t __mexp_begin, size_t __mexp_end, bool __greedy) {
4073  unique_ptr<__empty_state<_CharT> > __e1(new __empty_state<_CharT>(__end_->first()));
4074  __end_->first() = nullptr;
4075  unique_ptr<__loop<_CharT> > __e2(
4076      new __loop<_CharT>(__loop_count_, __s->first(), __e1.get(), __mexp_begin, __mexp_end, __greedy, __min, __max));
4077  __s->first() = nullptr;
4078  __e1.release();
4079  __end_->first() = new __repeat_one_loop<_CharT>(__e2.get());
4080  __end_          = __e2->second();
4081  __s->first()    = __e2.release();
4082  ++__loop_count_;
4083}
4084
4085template <class _CharT, class _Traits>
4086void basic_regex<_CharT, _Traits>::__push_char(value_type __c) {
4087  if (flags() & icase)
4088    __end_->first() = new __match_char_icase<_CharT, _Traits>(__traits_, __c, __end_->first());
4089  else if (flags() & collate)
4090    __end_->first() = new __match_char_collate<_CharT, _Traits>(__traits_, __c, __end_->first());
4091  else
4092    __end_->first() = new __match_char<_CharT>(__c, __end_->first());
4093  __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4094}
4095
4096template <class _CharT, class _Traits>
4097void basic_regex<_CharT, _Traits>::__push_begin_marked_subexpression() {
4098  if (!(__flags_ & nosubs)) {
4099    __end_->first() = new __begin_marked_subexpression<_CharT>(++__marked_count_, __end_->first());
4100    __end_          = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4101  }
4102}
4103
4104template <class _CharT, class _Traits>
4105void basic_regex<_CharT, _Traits>::__push_end_marked_subexpression(unsigned __sub) {
4106  if (!(__flags_ & nosubs)) {
4107    __end_->first() = new __end_marked_subexpression<_CharT>(__sub, __end_->first());
4108    __end_          = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4109  }
4110}
4111
4112template <class _CharT, class _Traits>
4113void basic_regex<_CharT, _Traits>::__push_l_anchor() {
4114  __end_->first() = new __l_anchor_multiline<_CharT>(__use_multiline(), __end_->first());
4115  __end_          = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4116}
4117
4118template <class _CharT, class _Traits>
4119void basic_regex<_CharT, _Traits>::__push_r_anchor() {
4120  __end_->first() = new __r_anchor_multiline<_CharT>(__use_multiline(), __end_->first());
4121  __end_          = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4122}
4123
4124template <class _CharT, class _Traits>
4125void basic_regex<_CharT, _Traits>::__push_match_any() {
4126  __end_->first() = new __match_any<_CharT>(__end_->first());
4127  __end_          = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4128}
4129
4130template <class _CharT, class _Traits>
4131void basic_regex<_CharT, _Traits>::__push_match_any_but_newline() {
4132  __end_->first() = new __match_any_but_newline<_CharT>(__end_->first());
4133  __end_          = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4134}
4135
4136template <class _CharT, class _Traits>
4137void basic_regex<_CharT, _Traits>::__push_empty() {
4138  __end_->first() = new __empty_state<_CharT>(__end_->first());
4139  __end_          = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4140}
4141
4142template <class _CharT, class _Traits>
4143void basic_regex<_CharT, _Traits>::__push_word_boundary(bool __invert) {
4144  __end_->first() = new __word_boundary<_CharT, _Traits>(__traits_, __invert, __end_->first());
4145  __end_          = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4146}
4147
4148template <class _CharT, class _Traits>
4149void basic_regex<_CharT, _Traits>::__push_back_ref(int __i) {
4150  if (flags() & icase)
4151    __end_->first() = new __back_ref_icase<_CharT, _Traits>(__traits_, __i, __end_->first());
4152  else if (flags() & collate)
4153    __end_->first() = new __back_ref_collate<_CharT, _Traits>(__traits_, __i, __end_->first());
4154  else
4155    __end_->first() = new __back_ref<_CharT>(__i, __end_->first());
4156  __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4157}
4158
4159template <class _CharT, class _Traits>
4160void basic_regex<_CharT, _Traits>::__push_alternation(__owns_one_state<_CharT>* __sa, __owns_one_state<_CharT>* __ea) {
4161  __sa->first() = new __alternate<_CharT>(
4162      static_cast<__owns_one_state<_CharT>*>(__sa->first()), static_cast<__owns_one_state<_CharT>*>(__ea->first()));
4163  __ea->first()   = nullptr;
4164  __ea->first()   = new __empty_state<_CharT>(__end_->first());
4165  __end_->first() = nullptr;
4166  __end_->first() = new __empty_non_own_state<_CharT>(__ea->first());
4167  __end_          = static_cast<__owns_one_state<_CharT>*>(__ea->first());
4168}
4169
4170template <class _CharT, class _Traits>
4171__bracket_expression<_CharT, _Traits>* basic_regex<_CharT, _Traits>::__start_matching_list(bool __negate) {
4172  __bracket_expression<_CharT, _Traits>* __r = new __bracket_expression<_CharT, _Traits>(
4173      __traits_, __end_->first(), __negate, __flags_ & icase, __flags_ & collate);
4174  __end_->first() = __r;
4175  __end_          = __r;
4176  return __r;
4177}
4178
4179template <class _CharT, class _Traits>
4180void basic_regex<_CharT, _Traits>::__push_lookahead(const basic_regex& __exp, bool __invert, unsigned __mexp) {
4181  __end_->first() = new __lookahead<_CharT, _Traits>(__exp, __invert, __end_->first(), __mexp);
4182  __end_          = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4183}
4184
4185// sub_match
4186
4187typedef sub_match<const char*> csub_match;
4188typedef sub_match<string::const_iterator> ssub_match;
4189#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
4190typedef sub_match<const wchar_t*> wcsub_match;
4191typedef sub_match<wstring::const_iterator> wssub_match;
4192#endif
4193
4194template <class _BidirectionalIterator>
4195class _LIBCPP_TEMPLATE_VIS _LIBCPP_PREFERRED_NAME(csub_match)
4196    _LIBCPP_IF_WIDE_CHARACTERS(_LIBCPP_PREFERRED_NAME(wcsub_match)) _LIBCPP_PREFERRED_NAME(ssub_match)
4197        _LIBCPP_IF_WIDE_CHARACTERS(_LIBCPP_PREFERRED_NAME(wssub_match)) sub_match
4198    : public pair<_BidirectionalIterator, _BidirectionalIterator> {
4199public:
4200  typedef _BidirectionalIterator iterator;
4201  typedef typename iterator_traits<iterator>::value_type value_type;
4202  typedef typename iterator_traits<iterator>::difference_type difference_type;
4203  typedef basic_string<value_type> string_type;
4204
4205  bool matched;
4206
4207  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR sub_match() : matched() {}
4208
4209  _LIBCPP_HIDE_FROM_ABI difference_type length() const {
4210    return matched ? std::distance(this->first, this->second) : 0;
4211  }
4212  _LIBCPP_HIDE_FROM_ABI string_type str() const {
4213    return matched ? string_type(this->first, this->second) : string_type();
4214  }
4215  _LIBCPP_HIDE_FROM_ABI operator string_type() const { return str(); }
4216
4217  _LIBCPP_HIDE_FROM_ABI int compare(const sub_match& __s) const { return str().compare(__s.str()); }
4218  _LIBCPP_HIDE_FROM_ABI int compare(const string_type& __s) const { return str().compare(__s); }
4219  _LIBCPP_HIDE_FROM_ABI int compare(const value_type* __s) const { return str().compare(__s); }
4220
4221  _LIBCPP_HIDE_FROM_ABI void swap(sub_match& __s)
4222#ifndef _LIBCPP_CXX03_LANG
4223      _NOEXCEPT(__is_nothrow_swappable<_BidirectionalIterator>::value)
4224#endif // _LIBCPP_CXX03_LANG
4225  {
4226    this->pair<_BidirectionalIterator, _BidirectionalIterator>::swap(__s);
4227    std::swap(matched, __s.matched);
4228  }
4229};
4230
4231template <class _BiIter>
4232inline _LIBCPP_HIDE_FROM_ABI bool operator==(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y) {
4233  return __x.compare(__y) == 0;
4234}
4235
4236#if _LIBCPP_STD_VER >= 20
4237template <class _BiIter>
4238using __sub_match_cat = compare_three_way_result_t<basic_string<typename iterator_traits<_BiIter>::value_type>>;
4239
4240template <class _BiIter>
4241_LIBCPP_HIDE_FROM_ABI auto operator<=>(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y) {
4242  return static_cast<__sub_match_cat<_BiIter>>(__x.compare(__y) <=> 0);
4243}
4244#else  // _LIBCPP_STD_VER >= 20
4245template <class _BiIter>
4246inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y) {
4247  return !(__x == __y);
4248}
4249
4250template <class _BiIter>
4251inline _LIBCPP_HIDE_FROM_ABI bool operator<(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y) {
4252  return __x.compare(__y) < 0;
4253}
4254
4255template <class _BiIter>
4256inline _LIBCPP_HIDE_FROM_ABI bool operator<=(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y) {
4257  return !(__y < __x);
4258}
4259
4260template <class _BiIter>
4261inline _LIBCPP_HIDE_FROM_ABI bool operator>=(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y) {
4262  return !(__x < __y);
4263}
4264
4265template <class _BiIter>
4266inline _LIBCPP_HIDE_FROM_ABI bool operator>(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y) {
4267  return __y < __x;
4268}
4269
4270template <class _BiIter, class _ST, class _SA>
4271inline _LIBCPP_HIDE_FROM_ABI bool
4272operator==(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4273           const sub_match<_BiIter>& __y) {
4274  return __y.compare(typename sub_match<_BiIter>::string_type(__x.data(), __x.size())) == 0;
4275}
4276
4277template <class _BiIter, class _ST, class _SA>
4278inline _LIBCPP_HIDE_FROM_ABI bool
4279operator!=(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4280           const sub_match<_BiIter>& __y) {
4281  return !(__x == __y);
4282}
4283
4284template <class _BiIter, class _ST, class _SA>
4285inline _LIBCPP_HIDE_FROM_ABI bool
4286operator<(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4287          const sub_match<_BiIter>& __y) {
4288  return __y.compare(typename sub_match<_BiIter>::string_type(__x.data(), __x.size())) > 0;
4289}
4290
4291template <class _BiIter, class _ST, class _SA>
4292inline _LIBCPP_HIDE_FROM_ABI bool
4293operator>(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4294          const sub_match<_BiIter>& __y) {
4295  return __y < __x;
4296}
4297
4298template <class _BiIter, class _ST, class _SA>
4299inline _LIBCPP_HIDE_FROM_ABI bool
4300operator>=(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4301           const sub_match<_BiIter>& __y) {
4302  return !(__x < __y);
4303}
4304
4305template <class _BiIter, class _ST, class _SA>
4306inline _LIBCPP_HIDE_FROM_ABI bool
4307operator<=(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4308           const sub_match<_BiIter>& __y) {
4309  return !(__y < __x);
4310}
4311#endif // _LIBCPP_STD_VER >= 20
4312
4313template <class _BiIter, class _ST, class _SA>
4314inline _LIBCPP_HIDE_FROM_ABI bool
4315operator==(const sub_match<_BiIter>& __x,
4316           const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y) {
4317  return __x.compare(typename sub_match<_BiIter>::string_type(__y.data(), __y.size())) == 0;
4318}
4319
4320#if _LIBCPP_STD_VER >= 20
4321template <class _BiIter, class _ST, class _SA>
4322_LIBCPP_HIDE_FROM_ABI auto
4323operator<=>(const sub_match<_BiIter>& __x,
4324            const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y) {
4325  return static_cast<__sub_match_cat<_BiIter>>(
4326      __x.compare(typename sub_match<_BiIter>::string_type(__y.data(), __y.size())) <=> 0);
4327}
4328#else  // _LIBCPP_STD_VER >= 20
4329template <class _BiIter, class _ST, class _SA>
4330inline _LIBCPP_HIDE_FROM_ABI bool
4331operator!=(const sub_match<_BiIter>& __x,
4332           const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y) {
4333  return !(__x == __y);
4334}
4335
4336template <class _BiIter, class _ST, class _SA>
4337inline _LIBCPP_HIDE_FROM_ABI bool
4338operator<(const sub_match<_BiIter>& __x,
4339          const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y) {
4340  return __x.compare(typename sub_match<_BiIter>::string_type(__y.data(), __y.size())) < 0;
4341}
4342
4343template <class _BiIter, class _ST, class _SA>
4344inline _LIBCPP_HIDE_FROM_ABI bool
4345operator>(const sub_match<_BiIter>& __x,
4346          const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y) {
4347  return __y < __x;
4348}
4349
4350template <class _BiIter, class _ST, class _SA>
4351inline _LIBCPP_HIDE_FROM_ABI bool
4352operator>=(const sub_match<_BiIter>& __x,
4353           const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y) {
4354  return !(__x < __y);
4355}
4356
4357template <class _BiIter, class _ST, class _SA>
4358inline _LIBCPP_HIDE_FROM_ABI bool
4359operator<=(const sub_match<_BiIter>& __x,
4360           const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y) {
4361  return !(__y < __x);
4362}
4363
4364template <class _BiIter>
4365inline _LIBCPP_HIDE_FROM_ABI bool
4366operator==(typename iterator_traits<_BiIter>::value_type const* __x, const sub_match<_BiIter>& __y) {
4367  return __y.compare(__x) == 0;
4368}
4369
4370template <class _BiIter>
4371inline _LIBCPP_HIDE_FROM_ABI bool
4372operator!=(typename iterator_traits<_BiIter>::value_type const* __x, const sub_match<_BiIter>& __y) {
4373  return !(__x == __y);
4374}
4375
4376template <class _BiIter>
4377inline _LIBCPP_HIDE_FROM_ABI bool
4378operator<(typename iterator_traits<_BiIter>::value_type const* __x, const sub_match<_BiIter>& __y) {
4379  return __y.compare(__x) > 0;
4380}
4381
4382template <class _BiIter>
4383inline _LIBCPP_HIDE_FROM_ABI bool
4384operator>(typename iterator_traits<_BiIter>::value_type const* __x, const sub_match<_BiIter>& __y) {
4385  return __y < __x;
4386}
4387
4388template <class _BiIter>
4389inline _LIBCPP_HIDE_FROM_ABI bool
4390operator>=(typename iterator_traits<_BiIter>::value_type const* __x, const sub_match<_BiIter>& __y) {
4391  return !(__x < __y);
4392}
4393
4394template <class _BiIter>
4395inline _LIBCPP_HIDE_FROM_ABI bool
4396operator<=(typename iterator_traits<_BiIter>::value_type const* __x, const sub_match<_BiIter>& __y) {
4397  return !(__y < __x);
4398}
4399#endif // _LIBCPP_STD_VER >= 20
4400
4401template <class _BiIter>
4402inline _LIBCPP_HIDE_FROM_ABI bool
4403operator==(const sub_match<_BiIter>& __x, typename iterator_traits<_BiIter>::value_type const* __y) {
4404  return __x.compare(__y) == 0;
4405}
4406
4407#if _LIBCPP_STD_VER >= 20
4408template <class _BiIter>
4409_LIBCPP_HIDE_FROM_ABI auto
4410operator<=>(const sub_match<_BiIter>& __x, typename iterator_traits<_BiIter>::value_type const* __y) {
4411  return static_cast<__sub_match_cat<_BiIter>>(__x.compare(__y) <=> 0);
4412}
4413#else  // _LIBCPP_STD_VER >= 20
4414template <class _BiIter>
4415inline _LIBCPP_HIDE_FROM_ABI bool
4416operator!=(const sub_match<_BiIter>& __x, typename iterator_traits<_BiIter>::value_type const* __y) {
4417  return !(__x == __y);
4418}
4419
4420template <class _BiIter>
4421inline _LIBCPP_HIDE_FROM_ABI bool
4422operator<(const sub_match<_BiIter>& __x, typename iterator_traits<_BiIter>::value_type const* __y) {
4423  return __x.compare(__y) < 0;
4424}
4425
4426template <class _BiIter>
4427inline _LIBCPP_HIDE_FROM_ABI bool
4428operator>(const sub_match<_BiIter>& __x, typename iterator_traits<_BiIter>::value_type const* __y) {
4429  return __y < __x;
4430}
4431
4432template <class _BiIter>
4433inline _LIBCPP_HIDE_FROM_ABI bool
4434operator>=(const sub_match<_BiIter>& __x, typename iterator_traits<_BiIter>::value_type const* __y) {
4435  return !(__x < __y);
4436}
4437
4438template <class _BiIter>
4439inline _LIBCPP_HIDE_FROM_ABI bool
4440operator<=(const sub_match<_BiIter>& __x, typename iterator_traits<_BiIter>::value_type const* __y) {
4441  return !(__y < __x);
4442}
4443
4444template <class _BiIter>
4445inline _LIBCPP_HIDE_FROM_ABI bool
4446operator==(typename iterator_traits<_BiIter>::value_type const& __x, const sub_match<_BiIter>& __y) {
4447  typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
4448  return __y.compare(string_type(1, __x)) == 0;
4449}
4450
4451template <class _BiIter>
4452inline _LIBCPP_HIDE_FROM_ABI bool
4453operator!=(typename iterator_traits<_BiIter>::value_type const& __x, const sub_match<_BiIter>& __y) {
4454  return !(__x == __y);
4455}
4456
4457template <class _BiIter>
4458inline _LIBCPP_HIDE_FROM_ABI bool
4459operator<(typename iterator_traits<_BiIter>::value_type const& __x, const sub_match<_BiIter>& __y) {
4460  typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
4461  return __y.compare(string_type(1, __x)) > 0;
4462}
4463
4464template <class _BiIter>
4465inline _LIBCPP_HIDE_FROM_ABI bool
4466operator>(typename iterator_traits<_BiIter>::value_type const& __x, const sub_match<_BiIter>& __y) {
4467  return __y < __x;
4468}
4469
4470template <class _BiIter>
4471inline _LIBCPP_HIDE_FROM_ABI bool
4472operator>=(typename iterator_traits<_BiIter>::value_type const& __x, const sub_match<_BiIter>& __y) {
4473  return !(__x < __y);
4474}
4475
4476template <class _BiIter>
4477inline _LIBCPP_HIDE_FROM_ABI bool
4478operator<=(typename iterator_traits<_BiIter>::value_type const& __x, const sub_match<_BiIter>& __y) {
4479  return !(__y < __x);
4480}
4481#endif // _LIBCPP_STD_VER >= 20
4482
4483template <class _BiIter>
4484inline _LIBCPP_HIDE_FROM_ABI bool
4485operator==(const sub_match<_BiIter>& __x, typename iterator_traits<_BiIter>::value_type const& __y) {
4486  typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
4487  return __x.compare(string_type(1, __y)) == 0;
4488}
4489
4490#if _LIBCPP_STD_VER >= 20
4491template <class _BiIter>
4492_LIBCPP_HIDE_FROM_ABI auto
4493operator<=>(const sub_match<_BiIter>& __x, typename iterator_traits<_BiIter>::value_type const& __y) {
4494  using string_type = basic_string<typename iterator_traits<_BiIter>::value_type>;
4495  return static_cast<__sub_match_cat<_BiIter>>(__x.compare(string_type(1, __y)) <=> 0);
4496}
4497#else  // _LIBCPP_STD_VER >= 20
4498template <class _BiIter>
4499inline _LIBCPP_HIDE_FROM_ABI bool
4500operator!=(const sub_match<_BiIter>& __x, typename iterator_traits<_BiIter>::value_type const& __y) {
4501  return !(__x == __y);
4502}
4503
4504template <class _BiIter>
4505inline _LIBCPP_HIDE_FROM_ABI bool
4506operator<(const sub_match<_BiIter>& __x, typename iterator_traits<_BiIter>::value_type const& __y) {
4507  typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
4508  return __x.compare(string_type(1, __y)) < 0;
4509}
4510
4511template <class _BiIter>
4512inline _LIBCPP_HIDE_FROM_ABI bool
4513operator>(const sub_match<_BiIter>& __x, typename iterator_traits<_BiIter>::value_type const& __y) {
4514  return __y < __x;
4515}
4516
4517template <class _BiIter>
4518inline _LIBCPP_HIDE_FROM_ABI bool
4519operator>=(const sub_match<_BiIter>& __x, typename iterator_traits<_BiIter>::value_type const& __y) {
4520  return !(__x < __y);
4521}
4522
4523template <class _BiIter>
4524inline _LIBCPP_HIDE_FROM_ABI bool
4525operator<=(const sub_match<_BiIter>& __x, typename iterator_traits<_BiIter>::value_type const& __y) {
4526  return !(__y < __x);
4527}
4528#endif // _LIBCPP_STD_VER >= 20
4529
4530template <class _CharT, class _ST, class _BiIter>
4531inline _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _ST>&
4532operator<<(basic_ostream<_CharT, _ST>& __os, const sub_match<_BiIter>& __m) {
4533  return __os << __m.str();
4534}
4535
4536typedef match_results<const char*> cmatch;
4537typedef match_results<string::const_iterator> smatch;
4538#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
4539typedef match_results<const wchar_t*> wcmatch;
4540typedef match_results<wstring::const_iterator> wsmatch;
4541#endif
4542
4543template <class _BidirectionalIterator, class _Allocator>
4544class _LIBCPP_TEMPLATE_VIS _LIBCPP_PREFERRED_NAME(cmatch) _LIBCPP_IF_WIDE_CHARACTERS(_LIBCPP_PREFERRED_NAME(wcmatch))
4545    _LIBCPP_PREFERRED_NAME(smatch) _LIBCPP_IF_WIDE_CHARACTERS(_LIBCPP_PREFERRED_NAME(wsmatch)) match_results {
4546public:
4547  typedef _Allocator allocator_type;
4548  typedef sub_match<_BidirectionalIterator> value_type;
4549
4550private:
4551  typedef vector<value_type, allocator_type> __container_type;
4552
4553  __container_type __matches_;
4554  value_type __unmatched_;
4555  value_type __prefix_;
4556  value_type __suffix_;
4557  bool __ready_;
4558
4559public:
4560  _BidirectionalIterator __position_start_;
4561  typedef const value_type& const_reference;
4562  typedef value_type& reference;
4563  typedef typename __container_type::const_iterator const_iterator;
4564  typedef const_iterator iterator;
4565  typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
4566  typedef typename allocator_traits<allocator_type>::size_type size_type;
4567  typedef typename iterator_traits<_BidirectionalIterator>::value_type char_type;
4568  typedef basic_string<char_type> string_type;
4569
4570  // construct/copy/destroy:
4571#ifndef _LIBCPP_CXX03_LANG
4572  match_results() : match_results(allocator_type()) {}
4573  explicit match_results(const allocator_type& __a);
4574#else
4575  explicit match_results(const allocator_type& __a = allocator_type());
4576#endif
4577
4578  //    match_results(const match_results&) = default;
4579  //    match_results& operator=(const match_results&) = default;
4580  //    match_results(match_results&& __m) = default;
4581  //    match_results& operator=(match_results&& __m) = default;
4582  //    ~match_results() = default;
4583
4584  _LIBCPP_HIDE_FROM_ABI bool ready() const { return __ready_; }
4585
4586  // size:
4587  _LIBCPP_HIDE_FROM_ABI size_type size() const _NOEXCEPT { return __matches_.size(); }
4588  _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT { return __matches_.max_size(); }
4589  _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI bool empty() const _NOEXCEPT { return size() == 0; }
4590
4591  // element access:
4592  _LIBCPP_HIDE_FROM_ABI difference_type length(size_type __sub = 0) const {
4593    // If the match results are not ready, this will return `0`.
4594    _LIBCPP_ASSERT_PEDANTIC(ready(), "match_results::length() called when not ready");
4595    return (*this)[__sub].length();
4596  }
4597  _LIBCPP_HIDE_FROM_ABI difference_type position(size_type __sub = 0) const {
4598    // If the match results are not ready, this will return the result of subtracting two default-constructed iterators
4599    // (which is typically a well-defined operation).
4600    _LIBCPP_ASSERT_PEDANTIC(ready(), "match_results::position() called when not ready");
4601    return std::distance(__position_start_, (*this)[__sub].first);
4602  }
4603  _LIBCPP_HIDE_FROM_ABI string_type str(size_type __sub = 0) const {
4604    // If the match results are not ready, this will return an empty string.
4605    _LIBCPP_ASSERT_PEDANTIC(ready(), "match_results::str() called when not ready");
4606    return (*this)[__sub].str();
4607  }
4608  _LIBCPP_HIDE_FROM_ABI const_reference operator[](size_type __n) const {
4609    // If the match results are not ready, this call will be equivalent to calling this function with `__n >= size()`,
4610    // returning an empty subrange.
4611    _LIBCPP_ASSERT_PEDANTIC(ready(), "match_results::operator[]() called when not ready");
4612    return __n < __matches_.size() ? __matches_[__n] : __unmatched_;
4613  }
4614
4615  _LIBCPP_HIDE_FROM_ABI const_reference prefix() const {
4616    // If the match results are not ready, this will return a default-constructed empty `__suffix_`.
4617    _LIBCPP_ASSERT_PEDANTIC(ready(), "match_results::prefix() called when not ready");
4618    return __prefix_;
4619  }
4620  _LIBCPP_HIDE_FROM_ABI const_reference suffix() const {
4621    // If the match results are not ready, this will return a default-constructed empty `__suffix_`.
4622    _LIBCPP_ASSERT_PEDANTIC(ready(), "match_results::suffix() called when not ready");
4623    return __suffix_;
4624  }
4625
4626  _LIBCPP_HIDE_FROM_ABI const_iterator begin() const { return empty() ? __matches_.end() : __matches_.begin(); }
4627  _LIBCPP_HIDE_FROM_ABI const_iterator end() const { return __matches_.end(); }
4628  _LIBCPP_HIDE_FROM_ABI const_iterator cbegin() const { return empty() ? __matches_.end() : __matches_.begin(); }
4629  _LIBCPP_HIDE_FROM_ABI const_iterator cend() const { return __matches_.end(); }
4630
4631  // format:
4632  template <class _OutputIter>
4633  _OutputIter format(_OutputIter __output_iter,
4634                     const char_type* __fmt_first,
4635                     const char_type* __fmt_last,
4636                     regex_constants::match_flag_type __flags = regex_constants::format_default) const;
4637  template <class _OutputIter, class _ST, class _SA>
4638  _LIBCPP_HIDE_FROM_ABI _OutputIter
4639  format(_OutputIter __output_iter,
4640         const basic_string<char_type, _ST, _SA>& __fmt,
4641         regex_constants::match_flag_type __flags = regex_constants::format_default) const {
4642    return format(__output_iter, __fmt.data(), __fmt.data() + __fmt.size(), __flags);
4643  }
4644  template <class _ST, class _SA>
4645  _LIBCPP_HIDE_FROM_ABI basic_string<char_type, _ST, _SA>
4646  format(const basic_string<char_type, _ST, _SA>& __fmt,
4647         regex_constants::match_flag_type __flags = regex_constants::format_default) const {
4648    basic_string<char_type, _ST, _SA> __r;
4649    format(std::back_inserter(__r), __fmt.data(), __fmt.data() + __fmt.size(), __flags);
4650    return __r;
4651  }
4652  _LIBCPP_HIDE_FROM_ABI string_type
4653  format(const char_type* __fmt, regex_constants::match_flag_type __flags = regex_constants::format_default) const {
4654    string_type __r;
4655    format(std::back_inserter(__r), __fmt, __fmt + char_traits<char_type>::length(__fmt), __flags);
4656    return __r;
4657  }
4658
4659  // allocator:
4660  _LIBCPP_HIDE_FROM_ABI allocator_type get_allocator() const { return __matches_.get_allocator(); }
4661
4662  // swap:
4663  void swap(match_results& __m);
4664
4665  template <class _Bp, class _Ap>
4666  _LIBCPP_HIDE_FROM_ABI void
4667  __assign(_BidirectionalIterator __f,
4668           _BidirectionalIterator __l,
4669           const match_results<_Bp, _Ap>& __m,
4670           bool __no_update_pos) {
4671    _Bp __mf = __m.prefix().first;
4672    __matches_.resize(__m.size());
4673    for (size_type __i = 0; __i < __matches_.size(); ++__i) {
4674      __matches_[__i].first   = std::next(__f, std::distance(__mf, __m[__i].first));
4675      __matches_[__i].second  = std::next(__f, std::distance(__mf, __m[__i].second));
4676      __matches_[__i].matched = __m[__i].matched;
4677    }
4678    __unmatched_.first   = __l;
4679    __unmatched_.second  = __l;
4680    __unmatched_.matched = false;
4681    __prefix_.first      = std::next(__f, std::distance(__mf, __m.prefix().first));
4682    __prefix_.second     = std::next(__f, std::distance(__mf, __m.prefix().second));
4683    __prefix_.matched    = __m.prefix().matched;
4684    __suffix_.first      = std::next(__f, std::distance(__mf, __m.suffix().first));
4685    __suffix_.second     = std::next(__f, std::distance(__mf, __m.suffix().second));
4686    __suffix_.matched    = __m.suffix().matched;
4687    if (!__no_update_pos)
4688      __position_start_ = __prefix_.first;
4689    __ready_ = __m.ready();
4690  }
4691
4692private:
4693  void __init(unsigned __s, _BidirectionalIterator __f, _BidirectionalIterator __l, bool __no_update_pos = false);
4694
4695  template <class, class>
4696  friend class basic_regex;
4697
4698  template <class _Bp, class _Ap, class _Cp, class _Tp>
4699  friend bool
4700  regex_match(_Bp, _Bp, match_results<_Bp, _Ap>&, const basic_regex<_Cp, _Tp>&, regex_constants::match_flag_type);
4701
4702  template <class _Bp, class _Ap>
4703  friend bool operator==(const match_results<_Bp, _Ap>&, const match_results<_Bp, _Ap>&);
4704
4705  template <class, class>
4706  friend class __lookahead;
4707};
4708
4709template <class _BidirectionalIterator, class _Allocator>
4710match_results<_BidirectionalIterator, _Allocator>::match_results(const allocator_type& __a)
4711    : __matches_(__a), __unmatched_(), __prefix_(), __suffix_(), __ready_(false), __position_start_() {}
4712
4713template <class _BidirectionalIterator, class _Allocator>
4714void match_results<_BidirectionalIterator, _Allocator>::__init(
4715    unsigned __s, _BidirectionalIterator __f, _BidirectionalIterator __l, bool __no_update_pos) {
4716  __unmatched_.first   = __l;
4717  __unmatched_.second  = __l;
4718  __unmatched_.matched = false;
4719  __matches_.assign(__s, __unmatched_);
4720  __prefix_.first   = __f;
4721  __prefix_.second  = __f;
4722  __prefix_.matched = false;
4723  __suffix_         = __unmatched_;
4724  if (!__no_update_pos)
4725    __position_start_ = __prefix_.first;
4726  __ready_ = true;
4727}
4728
4729template <class _BidirectionalIterator, class _Allocator>
4730template <class _OutputIter>
4731_OutputIter match_results<_BidirectionalIterator, _Allocator>::format(
4732    _OutputIter __output_iter,
4733    const char_type* __fmt_first,
4734    const char_type* __fmt_last,
4735    regex_constants::match_flag_type __flags) const {
4736  // Note: this duplicates a check in `vector::operator[]` but provides a better error message.
4737  _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(ready(), "match_results::format() called when not ready");
4738  if (__flags & regex_constants::format_sed) {
4739    for (; __fmt_first != __fmt_last; ++__fmt_first) {
4740      if (*__fmt_first == '&')
4741        __output_iter = std::copy(__matches_[0].first, __matches_[0].second, __output_iter);
4742      else if (*__fmt_first == '\\' && __fmt_first + 1 != __fmt_last) {
4743        ++__fmt_first;
4744        if ('0' <= *__fmt_first && *__fmt_first <= '9') {
4745          size_t __i    = *__fmt_first - '0';
4746          __output_iter = std::copy((*this)[__i].first, (*this)[__i].second, __output_iter);
4747        } else {
4748          *__output_iter = *__fmt_first;
4749          ++__output_iter;
4750        }
4751      } else {
4752        *__output_iter = *__fmt_first;
4753        ++__output_iter;
4754      }
4755    }
4756  } else {
4757    for (; __fmt_first != __fmt_last; ++__fmt_first) {
4758      if (*__fmt_first == '$' && __fmt_first + 1 != __fmt_last) {
4759        switch (__fmt_first[1]) {
4760        case '$':
4761          *__output_iter = *++__fmt_first;
4762          ++__output_iter;
4763          break;
4764        case '&':
4765          ++__fmt_first;
4766          __output_iter = std::copy(__matches_[0].first, __matches_[0].second, __output_iter);
4767          break;
4768        case '`':
4769          ++__fmt_first;
4770          __output_iter = std::copy(__prefix_.first, __prefix_.second, __output_iter);
4771          break;
4772        case '\'':
4773          ++__fmt_first;
4774          __output_iter = std::copy(__suffix_.first, __suffix_.second, __output_iter);
4775          break;
4776        default:
4777          if ('0' <= __fmt_first[1] && __fmt_first[1] <= '9') {
4778            ++__fmt_first;
4779            size_t __idx = *__fmt_first - '0';
4780            if (__fmt_first + 1 != __fmt_last && '0' <= __fmt_first[1] && __fmt_first[1] <= '9') {
4781              ++__fmt_first;
4782              if (__idx >= numeric_limits<size_t>::max() / 10)
4783                __throw_regex_error<regex_constants::error_escape>();
4784              __idx = 10 * __idx + *__fmt_first - '0';
4785            }
4786            __output_iter = std::copy((*this)[__idx].first, (*this)[__idx].second, __output_iter);
4787          } else {
4788            *__output_iter = *__fmt_first;
4789            ++__output_iter;
4790          }
4791          break;
4792        }
4793      } else {
4794        *__output_iter = *__fmt_first;
4795        ++__output_iter;
4796      }
4797    }
4798  }
4799  return __output_iter;
4800}
4801
4802template <class _BidirectionalIterator, class _Allocator>
4803void match_results<_BidirectionalIterator, _Allocator>::swap(match_results& __m) {
4804  using std::swap;
4805  swap(__matches_, __m.__matches_);
4806  swap(__unmatched_, __m.__unmatched_);
4807  swap(__prefix_, __m.__prefix_);
4808  swap(__suffix_, __m.__suffix_);
4809  swap(__position_start_, __m.__position_start_);
4810  swap(__ready_, __m.__ready_);
4811}
4812
4813template <class _BidirectionalIterator, class _Allocator>
4814_LIBCPP_HIDE_FROM_ABI bool operator==(const match_results<_BidirectionalIterator, _Allocator>& __x,
4815                                      const match_results<_BidirectionalIterator, _Allocator>& __y) {
4816  if (__x.__ready_ != __y.__ready_)
4817    return false;
4818  if (!__x.__ready_)
4819    return true;
4820  return __x.__matches_ == __y.__matches_ && __x.__prefix_ == __y.__prefix_ && __x.__suffix_ == __y.__suffix_;
4821}
4822
4823#if _LIBCPP_STD_VER < 20
4824template <class _BidirectionalIterator, class _Allocator>
4825inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const match_results<_BidirectionalIterator, _Allocator>& __x,
4826                                             const match_results<_BidirectionalIterator, _Allocator>& __y) {
4827  return !(__x == __y);
4828}
4829#endif
4830
4831template <class _BidirectionalIterator, class _Allocator>
4832inline _LIBCPP_HIDE_FROM_ABI void
4833swap(match_results<_BidirectionalIterator, _Allocator>& __x, match_results<_BidirectionalIterator, _Allocator>& __y) {
4834  __x.swap(__y);
4835}
4836
4837// regex_search
4838
4839template <class _CharT, class _Traits>
4840template <class _Allocator>
4841bool basic_regex<_CharT, _Traits>::__match_at_start_ecma(
4842    const _CharT* __first,
4843    const _CharT* __last,
4844    match_results<const _CharT*, _Allocator>& __m,
4845    regex_constants::match_flag_type __flags,
4846    bool __at_first) const {
4847  vector<__state> __states;
4848  __node* __st = __start_.get();
4849  if (__st) {
4850    sub_match<const _CharT*> __unmatched;
4851    __unmatched.first   = __last;
4852    __unmatched.second  = __last;
4853    __unmatched.matched = false;
4854
4855    __states.push_back(__state());
4856    __states.back().__do_      = 0;
4857    __states.back().__first_   = __first;
4858    __states.back().__current_ = __first;
4859    __states.back().__last_    = __last;
4860    __states.back().__sub_matches_.resize(mark_count(), __unmatched);
4861    __states.back().__loop_data_.resize(__loop_count());
4862    __states.back().__node_     = __st;
4863    __states.back().__flags_    = __flags;
4864    __states.back().__at_first_ = __at_first;
4865    int __counter               = 0;
4866    int __length                = __last - __first;
4867    do {
4868      ++__counter;
4869      if (__counter % _LIBCPP_REGEX_COMPLEXITY_FACTOR == 0 && __counter / _LIBCPP_REGEX_COMPLEXITY_FACTOR >= __length)
4870        __throw_regex_error<regex_constants::error_complexity>();
4871      __state& __s = __states.back();
4872      if (__s.__node_)
4873        __s.__node_->__exec(__s);
4874      switch (__s.__do_) {
4875      case __state::__end_state:
4876        if ((__flags & regex_constants::match_not_null) && __s.__current_ == __first) {
4877          __states.pop_back();
4878          break;
4879        }
4880        if ((__flags & regex_constants::__full_match) && __s.__current_ != __last) {
4881          __states.pop_back();
4882          break;
4883        }
4884        __m.__matches_[0].first   = __first;
4885        __m.__matches_[0].second  = std::next(__first, __s.__current_ - __first);
4886        __m.__matches_[0].matched = true;
4887        for (unsigned __i = 0; __i < __s.__sub_matches_.size(); ++__i)
4888          __m.__matches_[__i + 1] = __s.__sub_matches_[__i];
4889        return true;
4890      case __state::__accept_and_consume:
4891      case __state::__repeat:
4892      case __state::__accept_but_not_consume:
4893        break;
4894      case __state::__split: {
4895        __state __snext = __s;
4896        __s.__node_->__exec_split(true, __s);
4897        __snext.__node_->__exec_split(false, __snext);
4898        __states.push_back(std::move(__snext));
4899      } break;
4900      case __state::__reject:
4901        __states.pop_back();
4902        break;
4903      default:
4904        __throw_regex_error<regex_constants::__re_err_unknown>();
4905        break;
4906      }
4907    } while (!__states.empty());
4908  }
4909  return false;
4910}
4911
4912template <class _CharT, class _Traits>
4913template <class _Allocator>
4914bool basic_regex<_CharT, _Traits>::__match_at_start_posix_nosubs(
4915    const _CharT* __first,
4916    const _CharT* __last,
4917    match_results<const _CharT*, _Allocator>& __m,
4918    regex_constants::match_flag_type __flags,
4919    bool __at_first) const {
4920  deque<__state> __states;
4921  ptrdiff_t __highest_j = 0;
4922  ptrdiff_t __np        = std::distance(__first, __last);
4923  __node* __st          = __start_.get();
4924  if (__st) {
4925    __states.push_back(__state());
4926    __states.back().__do_      = 0;
4927    __states.back().__first_   = __first;
4928    __states.back().__current_ = __first;
4929    __states.back().__last_    = __last;
4930    __states.back().__loop_data_.resize(__loop_count());
4931    __states.back().__node_     = __st;
4932    __states.back().__flags_    = __flags;
4933    __states.back().__at_first_ = __at_first;
4934    bool __matched              = false;
4935    int __counter               = 0;
4936    int __length                = __last - __first;
4937    do {
4938      ++__counter;
4939      if (__counter % _LIBCPP_REGEX_COMPLEXITY_FACTOR == 0 && __counter / _LIBCPP_REGEX_COMPLEXITY_FACTOR >= __length)
4940        __throw_regex_error<regex_constants::error_complexity>();
4941      __state& __s = __states.back();
4942      if (__s.__node_)
4943        __s.__node_->__exec(__s);
4944      switch (__s.__do_) {
4945      case __state::__end_state:
4946        if ((__flags & regex_constants::match_not_null) && __s.__current_ == __first) {
4947          __states.pop_back();
4948          break;
4949        }
4950        if ((__flags & regex_constants::__full_match) && __s.__current_ != __last) {
4951          __states.pop_back();
4952          break;
4953        }
4954        if (!__matched || __highest_j < __s.__current_ - __s.__first_)
4955          __highest_j = __s.__current_ - __s.__first_;
4956        __matched = true;
4957        if (__highest_j == __np)
4958          __states.clear();
4959        else
4960          __states.pop_back();
4961        break;
4962      case __state::__consume_input:
4963        break;
4964      case __state::__accept_and_consume:
4965        __states.push_front(std::move(__s));
4966        __states.pop_back();
4967        break;
4968      case __state::__repeat:
4969      case __state::__accept_but_not_consume:
4970        break;
4971      case __state::__split: {
4972        __state __snext = __s;
4973        __s.__node_->__exec_split(true, __s);
4974        __snext.__node_->__exec_split(false, __snext);
4975        __states.push_back(std::move(__snext));
4976      } break;
4977      case __state::__reject:
4978        __states.pop_back();
4979        break;
4980      default:
4981        __throw_regex_error<regex_constants::__re_err_unknown>();
4982        break;
4983      }
4984    } while (!__states.empty());
4985    if (__matched) {
4986      __m.__matches_[0].first   = __first;
4987      __m.__matches_[0].second  = std::next(__first, __highest_j);
4988      __m.__matches_[0].matched = true;
4989      return true;
4990    }
4991  }
4992  return false;
4993}
4994
4995template <class _CharT, class _Traits>
4996template <class _Allocator>
4997bool basic_regex<_CharT, _Traits>::__match_at_start_posix_subs(
4998    const _CharT* __first,
4999    const _CharT* __last,
5000    match_results<const _CharT*, _Allocator>& __m,
5001    regex_constants::match_flag_type __flags,
5002    bool __at_first) const {
5003  vector<__state> __states;
5004  __state __best_state;
5005  ptrdiff_t __highest_j = 0;
5006  ptrdiff_t __np        = std::distance(__first, __last);
5007  __node* __st          = __start_.get();
5008  if (__st) {
5009    sub_match<const _CharT*> __unmatched;
5010    __unmatched.first   = __last;
5011    __unmatched.second  = __last;
5012    __unmatched.matched = false;
5013
5014    __states.push_back(__state());
5015    __states.back().__do_      = 0;
5016    __states.back().__first_   = __first;
5017    __states.back().__current_ = __first;
5018    __states.back().__last_    = __last;
5019    __states.back().__sub_matches_.resize(mark_count(), __unmatched);
5020    __states.back().__loop_data_.resize(__loop_count());
5021    __states.back().__node_     = __st;
5022    __states.back().__flags_    = __flags;
5023    __states.back().__at_first_ = __at_first;
5024    bool __matched              = false;
5025    int __counter               = 0;
5026    int __length                = __last - __first;
5027    do {
5028      ++__counter;
5029      if (__counter % _LIBCPP_REGEX_COMPLEXITY_FACTOR == 0 && __counter / _LIBCPP_REGEX_COMPLEXITY_FACTOR >= __length)
5030        __throw_regex_error<regex_constants::error_complexity>();
5031      __state& __s = __states.back();
5032      if (__s.__node_)
5033        __s.__node_->__exec(__s);
5034      switch (__s.__do_) {
5035      case __state::__end_state:
5036        if ((__flags & regex_constants::match_not_null) && __s.__current_ == __first) {
5037          __states.pop_back();
5038          break;
5039        }
5040        if ((__flags & regex_constants::__full_match) && __s.__current_ != __last) {
5041          __states.pop_back();
5042          break;
5043        }
5044        if (!__matched || __highest_j < __s.__current_ - __s.__first_) {
5045          __highest_j  = __s.__current_ - __s.__first_;
5046          __best_state = __s;
5047        }
5048        __matched = true;
5049        if (__highest_j == __np)
5050          __states.clear();
5051        else
5052          __states.pop_back();
5053        break;
5054      case __state::__accept_and_consume:
5055      case __state::__repeat:
5056      case __state::__accept_but_not_consume:
5057        break;
5058      case __state::__split: {
5059        __state __snext = __s;
5060        __s.__node_->__exec_split(true, __s);
5061        __snext.__node_->__exec_split(false, __snext);
5062        __states.push_back(std::move(__snext));
5063      } break;
5064      case __state::__reject:
5065        __states.pop_back();
5066        break;
5067      default:
5068        __throw_regex_error<regex_constants::__re_err_unknown>();
5069        break;
5070      }
5071    } while (!__states.empty());
5072    if (__matched) {
5073      __m.__matches_[0].first   = __first;
5074      __m.__matches_[0].second  = std::next(__first, __highest_j);
5075      __m.__matches_[0].matched = true;
5076      for (unsigned __i = 0; __i < __best_state.__sub_matches_.size(); ++__i)
5077        __m.__matches_[__i + 1] = __best_state.__sub_matches_[__i];
5078      return true;
5079    }
5080  }
5081  return false;
5082}
5083
5084template <class _CharT, class _Traits>
5085template <class _Allocator>
5086bool basic_regex<_CharT, _Traits>::__match_at_start(
5087    const _CharT* __first,
5088    const _CharT* __last,
5089    match_results<const _CharT*, _Allocator>& __m,
5090    regex_constants::match_flag_type __flags,
5091    bool __at_first) const {
5092  if (__get_grammar(__flags_) == ECMAScript)
5093    return __match_at_start_ecma(__first, __last, __m, __flags, __at_first);
5094  if (mark_count() == 0)
5095    return __match_at_start_posix_nosubs(__first, __last, __m, __flags, __at_first);
5096  return __match_at_start_posix_subs(__first, __last, __m, __flags, __at_first);
5097}
5098
5099template <class _CharT, class _Traits>
5100template <class _Allocator>
5101bool basic_regex<_CharT, _Traits>::__search(
5102    const _CharT* __first,
5103    const _CharT* __last,
5104    match_results<const _CharT*, _Allocator>& __m,
5105    regex_constants::match_flag_type __flags) const {
5106  if (__flags & regex_constants::match_prev_avail)
5107    __flags &= ~(regex_constants::match_not_bol | regex_constants::match_not_bow);
5108
5109  __m.__init(1 + mark_count(), __first, __last, __flags & regex_constants::__no_update_pos);
5110  if (__match_at_start(__first, __last, __m, __flags, !(__flags & regex_constants::__no_update_pos))) {
5111    __m.__prefix_.second  = __m[0].first;
5112    __m.__prefix_.matched = __m.__prefix_.first != __m.__prefix_.second;
5113    __m.__suffix_.first   = __m[0].second;
5114    __m.__suffix_.matched = __m.__suffix_.first != __m.__suffix_.second;
5115    return true;
5116  }
5117  if (__first != __last && !(__flags & regex_constants::match_continuous)) {
5118    __flags |= regex_constants::match_prev_avail;
5119    for (++__first; __first != __last; ++__first) {
5120      __m.__matches_.assign(__m.size(), __m.__unmatched_);
5121      if (__match_at_start(__first, __last, __m, __flags, false)) {
5122        __m.__prefix_.second  = __m[0].first;
5123        __m.__prefix_.matched = __m.__prefix_.first != __m.__prefix_.second;
5124        __m.__suffix_.first   = __m[0].second;
5125        __m.__suffix_.matched = __m.__suffix_.first != __m.__suffix_.second;
5126        return true;
5127      }
5128      __m.__matches_.assign(__m.size(), __m.__unmatched_);
5129    }
5130  }
5131  __m.__matches_.clear();
5132  return false;
5133}
5134
5135template <class _BidirectionalIterator, class _Allocator, class _CharT, class _Traits>
5136inline _LIBCPP_HIDE_FROM_ABI bool
5137regex_search(_BidirectionalIterator __first,
5138             _BidirectionalIterator __last,
5139             match_results<_BidirectionalIterator, _Allocator>& __m,
5140             const basic_regex<_CharT, _Traits>& __e,
5141             regex_constants::match_flag_type __flags = regex_constants::match_default) {
5142  int __offset = (__flags & regex_constants::match_prev_avail) ? 1 : 0;
5143  basic_string<_CharT> __s(std::prev(__first, __offset), __last);
5144  match_results<const _CharT*> __mc;
5145  bool __r = __e.__search(__s.data() + __offset, __s.data() + __s.size(), __mc, __flags);
5146  __m.__assign(__first, __last, __mc, __flags & regex_constants::__no_update_pos);
5147  return __r;
5148}
5149
5150template <class _Iter, class _Allocator, class _CharT, class _Traits>
5151inline _LIBCPP_HIDE_FROM_ABI bool
5152regex_search(__wrap_iter<_Iter> __first,
5153             __wrap_iter<_Iter> __last,
5154             match_results<__wrap_iter<_Iter>, _Allocator>& __m,
5155             const basic_regex<_CharT, _Traits>& __e,
5156             regex_constants::match_flag_type __flags = regex_constants::match_default) {
5157  match_results<const _CharT*> __mc;
5158  bool __r = __e.__search(__first.base(), __last.base(), __mc, __flags);
5159  __m.__assign(__first, __last, __mc, __flags & regex_constants::__no_update_pos);
5160  return __r;
5161}
5162
5163template <class _Allocator, class _CharT, class _Traits>
5164inline _LIBCPP_HIDE_FROM_ABI bool
5165regex_search(const _CharT* __first,
5166             const _CharT* __last,
5167             match_results<const _CharT*, _Allocator>& __m,
5168             const basic_regex<_CharT, _Traits>& __e,
5169             regex_constants::match_flag_type __flags = regex_constants::match_default) {
5170  return __e.__search(__first, __last, __m, __flags);
5171}
5172
5173template <class _BidirectionalIterator, class _CharT, class _Traits>
5174inline _LIBCPP_HIDE_FROM_ABI bool
5175regex_search(_BidirectionalIterator __first,
5176             _BidirectionalIterator __last,
5177             const basic_regex<_CharT, _Traits>& __e,
5178             regex_constants::match_flag_type __flags = regex_constants::match_default) {
5179  basic_string<_CharT> __s(__first, __last);
5180  match_results<const _CharT*> __mc;
5181  return __e.__search(__s.data(), __s.data() + __s.size(), __mc, __flags);
5182}
5183
5184template <class _CharT, class _Traits>
5185inline _LIBCPP_HIDE_FROM_ABI bool
5186regex_search(const _CharT* __first,
5187             const _CharT* __last,
5188             const basic_regex<_CharT, _Traits>& __e,
5189             regex_constants::match_flag_type __flags = regex_constants::match_default) {
5190  match_results<const _CharT*> __mc;
5191  return __e.__search(__first, __last, __mc, __flags);
5192}
5193
5194template <class _CharT, class _Allocator, class _Traits>
5195inline _LIBCPP_HIDE_FROM_ABI bool
5196regex_search(const _CharT* __str,
5197             match_results<const _CharT*, _Allocator>& __m,
5198             const basic_regex<_CharT, _Traits>& __e,
5199             regex_constants::match_flag_type __flags = regex_constants::match_default) {
5200  return __e.__search(__str, __str + _Traits::length(__str), __m, __flags);
5201}
5202
5203template <class _CharT, class _Traits>
5204inline _LIBCPP_HIDE_FROM_ABI bool
5205regex_search(const _CharT* __str,
5206             const basic_regex<_CharT, _Traits>& __e,
5207             regex_constants::match_flag_type __flags = regex_constants::match_default) {
5208  match_results<const _CharT*> __m;
5209  return std::regex_search(__str, __m, __e, __flags);
5210}
5211
5212template <class _ST, class _SA, class _CharT, class _Traits>
5213inline _LIBCPP_HIDE_FROM_ABI bool
5214regex_search(const basic_string<_CharT, _ST, _SA>& __s,
5215             const basic_regex<_CharT, _Traits>& __e,
5216             regex_constants::match_flag_type __flags = regex_constants::match_default) {
5217  match_results<const _CharT*> __mc;
5218  return __e.__search(__s.data(), __s.data() + __s.size(), __mc, __flags);
5219}
5220
5221template <class _ST, class _SA, class _Allocator, class _CharT, class _Traits>
5222inline _LIBCPP_HIDE_FROM_ABI bool
5223regex_search(const basic_string<_CharT, _ST, _SA>& __s,
5224             match_results<typename basic_string<_CharT, _ST, _SA>::const_iterator, _Allocator>& __m,
5225             const basic_regex<_CharT, _Traits>& __e,
5226             regex_constants::match_flag_type __flags = regex_constants::match_default) {
5227  match_results<const _CharT*> __mc;
5228  bool __r = __e.__search(__s.data(), __s.data() + __s.size(), __mc, __flags);
5229  __m.__assign(__s.begin(), __s.end(), __mc, __flags & regex_constants::__no_update_pos);
5230  return __r;
5231}
5232
5233#if _LIBCPP_STD_VER >= 14
5234template <class _ST, class _SA, class _Ap, class _Cp, class _Tp>
5235bool regex_search(const basic_string<_Cp, _ST, _SA>&& __s,
5236                  match_results<typename basic_string<_Cp, _ST, _SA>::const_iterator, _Ap>&,
5237                  const basic_regex<_Cp, _Tp>& __e,
5238                  regex_constants::match_flag_type __flags = regex_constants::match_default) = delete;
5239#endif
5240
5241// regex_match
5242
5243template <class _BidirectionalIterator, class _Allocator, class _CharT, class _Traits>
5244_LIBCPP_HIDE_FROM_ABI bool
5245regex_match(_BidirectionalIterator __first,
5246            _BidirectionalIterator __last,
5247            match_results<_BidirectionalIterator, _Allocator>& __m,
5248            const basic_regex<_CharT, _Traits>& __e,
5249            regex_constants::match_flag_type __flags = regex_constants::match_default) {
5250  bool __r = std::regex_search(
5251      __first, __last, __m, __e, __flags | regex_constants::match_continuous | regex_constants::__full_match);
5252  if (__r) {
5253    __r = !__m.suffix().matched;
5254    if (!__r)
5255      __m.__matches_.clear();
5256  }
5257  return __r;
5258}
5259
5260template <class _BidirectionalIterator, class _CharT, class _Traits>
5261inline _LIBCPP_HIDE_FROM_ABI bool
5262regex_match(_BidirectionalIterator __first,
5263            _BidirectionalIterator __last,
5264            const basic_regex<_CharT, _Traits>& __e,
5265            regex_constants::match_flag_type __flags = regex_constants::match_default) {
5266  match_results<_BidirectionalIterator> __m;
5267  return std::regex_match(__first, __last, __m, __e, __flags);
5268}
5269
5270template <class _CharT, class _Allocator, class _Traits>
5271inline _LIBCPP_HIDE_FROM_ABI bool
5272regex_match(const _CharT* __str,
5273            match_results<const _CharT*, _Allocator>& __m,
5274            const basic_regex<_CharT, _Traits>& __e,
5275            regex_constants::match_flag_type __flags = regex_constants::match_default) {
5276  return std::regex_match(__str, __str + _Traits::length(__str), __m, __e, __flags);
5277}
5278
5279template <class _ST, class _SA, class _Allocator, class _CharT, class _Traits>
5280inline _LIBCPP_HIDE_FROM_ABI bool
5281regex_match(const basic_string<_CharT, _ST, _SA>& __s,
5282            match_results<typename basic_string<_CharT, _ST, _SA>::const_iterator, _Allocator>& __m,
5283            const basic_regex<_CharT, _Traits>& __e,
5284            regex_constants::match_flag_type __flags = regex_constants::match_default) {
5285  return std::regex_match(__s.begin(), __s.end(), __m, __e, __flags);
5286}
5287
5288#if _LIBCPP_STD_VER >= 14
5289template <class _ST, class _SA, class _Allocator, class _CharT, class _Traits>
5290inline _LIBCPP_HIDE_FROM_ABI bool
5291regex_match(const basic_string<_CharT, _ST, _SA>&& __s,
5292            match_results<typename basic_string<_CharT, _ST, _SA>::const_iterator, _Allocator>& __m,
5293            const basic_regex<_CharT, _Traits>& __e,
5294            regex_constants::match_flag_type __flags = regex_constants::match_default) = delete;
5295#endif
5296
5297template <class _CharT, class _Traits>
5298inline _LIBCPP_HIDE_FROM_ABI bool
5299regex_match(const _CharT* __str,
5300            const basic_regex<_CharT, _Traits>& __e,
5301            regex_constants::match_flag_type __flags = regex_constants::match_default) {
5302  return std::regex_match(__str, __str + _Traits::length(__str), __e, __flags);
5303}
5304
5305template <class _ST, class _SA, class _CharT, class _Traits>
5306inline _LIBCPP_HIDE_FROM_ABI bool
5307regex_match(const basic_string<_CharT, _ST, _SA>& __s,
5308            const basic_regex<_CharT, _Traits>& __e,
5309            regex_constants::match_flag_type __flags = regex_constants::match_default) {
5310  return std::regex_match(__s.begin(), __s.end(), __e, __flags);
5311}
5312
5313// regex_iterator
5314
5315template <class _BidirectionalIterator,
5316          class _CharT  = typename iterator_traits<_BidirectionalIterator>::value_type,
5317          class _Traits = regex_traits<_CharT> >
5318class _LIBCPP_TEMPLATE_VIS regex_iterator;
5319
5320typedef regex_iterator<const char*> cregex_iterator;
5321typedef regex_iterator<string::const_iterator> sregex_iterator;
5322#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
5323typedef regex_iterator<const wchar_t*> wcregex_iterator;
5324typedef regex_iterator<wstring::const_iterator> wsregex_iterator;
5325#endif
5326
5327template <class _BidirectionalIterator, class _CharT, class _Traits>
5328class _LIBCPP_TEMPLATE_VIS _LIBCPP_PREFERRED_NAME(cregex_iterator)
5329    _LIBCPP_IF_WIDE_CHARACTERS(_LIBCPP_PREFERRED_NAME(wcregex_iterator)) _LIBCPP_PREFERRED_NAME(sregex_iterator)
5330        _LIBCPP_IF_WIDE_CHARACTERS(_LIBCPP_PREFERRED_NAME(wsregex_iterator)) regex_iterator {
5331public:
5332  typedef basic_regex<_CharT, _Traits> regex_type;
5333  typedef match_results<_BidirectionalIterator> value_type;
5334  typedef ptrdiff_t difference_type;
5335  typedef const value_type* pointer;
5336  typedef const value_type& reference;
5337  typedef forward_iterator_tag iterator_category;
5338#if _LIBCPP_STD_VER >= 20
5339  typedef input_iterator_tag iterator_concept;
5340#endif
5341
5342private:
5343  _BidirectionalIterator __begin_;
5344  _BidirectionalIterator __end_;
5345  const regex_type* __pregex_;
5346  regex_constants::match_flag_type __flags_;
5347  value_type __match_;
5348
5349public:
5350  regex_iterator();
5351  regex_iterator(_BidirectionalIterator __a,
5352                 _BidirectionalIterator __b,
5353                 const regex_type& __re,
5354                 regex_constants::match_flag_type __m = regex_constants::match_default);
5355#if _LIBCPP_STD_VER >= 14
5356  regex_iterator(_BidirectionalIterator __a,
5357                 _BidirectionalIterator __b,
5358                 const regex_type&& __re,
5359                 regex_constants::match_flag_type __m = regex_constants::match_default) = delete;
5360#endif
5361
5362  _LIBCPP_HIDE_FROM_ABI bool operator==(const regex_iterator& __x) const;
5363#if _LIBCPP_STD_VER >= 20
5364  _LIBCPP_HIDE_FROM_ABI bool operator==(default_sentinel_t) const { return *this == regex_iterator(); }
5365#endif
5366#if _LIBCPP_STD_VER < 20
5367  _LIBCPP_HIDE_FROM_ABI bool operator!=(const regex_iterator& __x) const { return !(*this == __x); }
5368#endif
5369
5370  _LIBCPP_HIDE_FROM_ABI reference operator*() const { return __match_; }
5371  _LIBCPP_HIDE_FROM_ABI pointer operator->() const { return std::addressof(__match_); }
5372
5373  regex_iterator& operator++();
5374  _LIBCPP_HIDE_FROM_ABI regex_iterator operator++(int) {
5375    regex_iterator __t(*this);
5376    ++(*this);
5377    return __t;
5378  }
5379};
5380
5381template <class _BidirectionalIterator, class _CharT, class _Traits>
5382regex_iterator<_BidirectionalIterator, _CharT, _Traits>::regex_iterator()
5383    : __begin_(), __end_(), __pregex_(nullptr), __flags_(), __match_() {}
5384
5385template <class _BidirectionalIterator, class _CharT, class _Traits>
5386regex_iterator<_BidirectionalIterator, _CharT, _Traits>::regex_iterator(
5387    _BidirectionalIterator __a,
5388    _BidirectionalIterator __b,
5389    const regex_type& __re,
5390    regex_constants::match_flag_type __m)
5391    : __begin_(__a), __end_(__b), __pregex_(std::addressof(__re)), __flags_(__m) {
5392  std::regex_search(__begin_, __end_, __match_, *__pregex_, __flags_);
5393}
5394
5395template <class _BidirectionalIterator, class _CharT, class _Traits>
5396bool regex_iterator<_BidirectionalIterator, _CharT, _Traits>::operator==(const regex_iterator& __x) const {
5397  if (__match_.empty() && __x.__match_.empty())
5398    return true;
5399  if (__match_.empty() || __x.__match_.empty())
5400    return false;
5401  return __begin_ == __x.__begin_ && __end_ == __x.__end_ && __pregex_ == __x.__pregex_ && __flags_ == __x.__flags_ &&
5402         __match_[0] == __x.__match_[0];
5403}
5404
5405template <class _BidirectionalIterator, class _CharT, class _Traits>
5406regex_iterator<_BidirectionalIterator, _CharT, _Traits>&
5407regex_iterator<_BidirectionalIterator, _CharT, _Traits>::operator++() {
5408  __flags_ |= regex_constants::__no_update_pos;
5409  _BidirectionalIterator __start = __match_[0].second;
5410  if (__match_[0].first == __match_[0].second) {
5411    if (__start == __end_) {
5412      __match_ = value_type();
5413      return *this;
5414    } else if (std::regex_search(__start,
5415                                 __end_,
5416                                 __match_,
5417                                 *__pregex_,
5418                                 __flags_ | regex_constants::match_not_null | regex_constants::match_continuous))
5419      return *this;
5420    else
5421      ++__start;
5422  }
5423  __flags_ |= regex_constants::match_prev_avail;
5424  if (!std::regex_search(__start, __end_, __match_, *__pregex_, __flags_))
5425    __match_ = value_type();
5426  return *this;
5427}
5428
5429// regex_token_iterator
5430
5431template <class _BidirectionalIterator,
5432          class _CharT  = typename iterator_traits<_BidirectionalIterator>::value_type,
5433          class _Traits = regex_traits<_CharT> >
5434class _LIBCPP_TEMPLATE_VIS regex_token_iterator;
5435
5436typedef regex_token_iterator<const char*> cregex_token_iterator;
5437typedef regex_token_iterator<string::const_iterator> sregex_token_iterator;
5438#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
5439typedef regex_token_iterator<const wchar_t*> wcregex_token_iterator;
5440typedef regex_token_iterator<wstring::const_iterator> wsregex_token_iterator;
5441#endif
5442
5443template <class _BidirectionalIterator, class _CharT, class _Traits>
5444class _LIBCPP_TEMPLATE_VIS _LIBCPP_PREFERRED_NAME(cregex_token_iterator)
5445    _LIBCPP_IF_WIDE_CHARACTERS(_LIBCPP_PREFERRED_NAME(wcregex_token_iterator))
5446        _LIBCPP_PREFERRED_NAME(sregex_token_iterator)
5447            _LIBCPP_IF_WIDE_CHARACTERS(_LIBCPP_PREFERRED_NAME(wsregex_token_iterator)) regex_token_iterator {
5448public:
5449  typedef basic_regex<_CharT, _Traits> regex_type;
5450  typedef sub_match<_BidirectionalIterator> value_type;
5451  typedef ptrdiff_t difference_type;
5452  typedef const value_type* pointer;
5453  typedef const value_type& reference;
5454  typedef forward_iterator_tag iterator_category;
5455#if _LIBCPP_STD_VER >= 20
5456  typedef input_iterator_tag iterator_concept;
5457#endif
5458
5459private:
5460  typedef regex_iterator<_BidirectionalIterator, _CharT, _Traits> _Position;
5461
5462  _Position __position_;
5463  const value_type* __result_;
5464  value_type __suffix_;
5465  ptrdiff_t __n_;
5466  vector<int> __subs_;
5467
5468public:
5469  regex_token_iterator();
5470  regex_token_iterator(_BidirectionalIterator __a,
5471                       _BidirectionalIterator __b,
5472                       const regex_type& __re,
5473                       int __submatch                       = 0,
5474                       regex_constants::match_flag_type __m = regex_constants::match_default);
5475#if _LIBCPP_STD_VER >= 14
5476  regex_token_iterator(_BidirectionalIterator __a,
5477                       _BidirectionalIterator __b,
5478                       const regex_type&& __re,
5479                       int __submatch                       = 0,
5480                       regex_constants::match_flag_type __m = regex_constants::match_default) = delete;
5481#endif
5482
5483  regex_token_iterator(_BidirectionalIterator __a,
5484                       _BidirectionalIterator __b,
5485                       const regex_type& __re,
5486                       const vector<int>& __submatches,
5487                       regex_constants::match_flag_type __m = regex_constants::match_default);
5488#if _LIBCPP_STD_VER >= 14
5489  regex_token_iterator(_BidirectionalIterator __a,
5490                       _BidirectionalIterator __b,
5491                       const regex_type&& __re,
5492                       const vector<int>& __submatches,
5493                       regex_constants::match_flag_type __m = regex_constants::match_default) = delete;
5494#endif
5495
5496#ifndef _LIBCPP_CXX03_LANG
5497  regex_token_iterator(_BidirectionalIterator __a,
5498                       _BidirectionalIterator __b,
5499                       const regex_type& __re,
5500                       initializer_list<int> __submatches,
5501                       regex_constants::match_flag_type __m = regex_constants::match_default);
5502
5503#  if _LIBCPP_STD_VER >= 14
5504  regex_token_iterator(_BidirectionalIterator __a,
5505                       _BidirectionalIterator __b,
5506                       const regex_type&& __re,
5507                       initializer_list<int> __submatches,
5508                       regex_constants::match_flag_type __m = regex_constants::match_default) = delete;
5509#  endif
5510#endif // _LIBCPP_CXX03_LANG
5511  template <size_t _Np>
5512  regex_token_iterator(_BidirectionalIterator __a,
5513                       _BidirectionalIterator __b,
5514                       const regex_type& __re,
5515                       const int (&__submatches)[_Np],
5516                       regex_constants::match_flag_type __m = regex_constants::match_default);
5517#if _LIBCPP_STD_VER >= 14
5518  template <size_t _Np>
5519  regex_token_iterator(_BidirectionalIterator __a,
5520                       _BidirectionalIterator __b,
5521                       const regex_type&& __re,
5522                       const int (&__submatches)[_Np],
5523                       regex_constants::match_flag_type __m = regex_constants::match_default) = delete;
5524#endif
5525
5526  regex_token_iterator(const regex_token_iterator&);
5527  regex_token_iterator& operator=(const regex_token_iterator&);
5528
5529  _LIBCPP_HIDE_FROM_ABI bool operator==(const regex_token_iterator& __x) const;
5530#if _LIBCPP_STD_VER >= 20
5531  _LIBCPP_HIDE_FROM_ABI _LIBCPP_HIDE_FROM_ABI bool operator==(default_sentinel_t) const {
5532    return *this == regex_token_iterator();
5533  }
5534#endif
5535#if _LIBCPP_STD_VER < 20
5536  _LIBCPP_HIDE_FROM_ABI bool operator!=(const regex_token_iterator& __x) const { return !(*this == __x); }
5537#endif
5538
5539  _LIBCPP_HIDE_FROM_ABI const value_type& operator*() const { return *__result_; }
5540  _LIBCPP_HIDE_FROM_ABI const value_type* operator->() const { return __result_; }
5541
5542  regex_token_iterator& operator++();
5543  _LIBCPP_HIDE_FROM_ABI regex_token_iterator operator++(int) {
5544    regex_token_iterator __t(*this);
5545    ++(*this);
5546    return __t;
5547  }
5548
5549private:
5550  void __init(_BidirectionalIterator __a, _BidirectionalIterator __b);
5551  void __establish_result() {
5552    if (__subs_[__n_] == -1)
5553      __result_ = &__position_->prefix();
5554    else
5555      __result_ = &(*__position_)[__subs_[__n_]];
5556  }
5557};
5558
5559template <class _BidirectionalIterator, class _CharT, class _Traits>
5560regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::regex_token_iterator()
5561    : __result_(nullptr), __suffix_(), __n_(0) {}
5562
5563template <class _BidirectionalIterator, class _CharT, class _Traits>
5564void regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::__init(
5565    _BidirectionalIterator __a, _BidirectionalIterator __b) {
5566  if (__position_ != _Position())
5567    __establish_result();
5568  else if (__subs_[__n_] == -1) {
5569    __suffix_.matched = true;
5570    __suffix_.first   = __a;
5571    __suffix_.second  = __b;
5572    __result_         = &__suffix_;
5573  } else
5574    __result_ = nullptr;
5575}
5576
5577template <class _BidirectionalIterator, class _CharT, class _Traits>
5578regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::regex_token_iterator(
5579    _BidirectionalIterator __a,
5580    _BidirectionalIterator __b,
5581    const regex_type& __re,
5582    int __submatch,
5583    regex_constants::match_flag_type __m)
5584    : __position_(__a, __b, __re, __m), __n_(0), __subs_(1, __submatch) {
5585  __init(__a, __b);
5586}
5587
5588template <class _BidirectionalIterator, class _CharT, class _Traits>
5589regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::regex_token_iterator(
5590    _BidirectionalIterator __a,
5591    _BidirectionalIterator __b,
5592    const regex_type& __re,
5593    const vector<int>& __submatches,
5594    regex_constants::match_flag_type __m)
5595    : __position_(__a, __b, __re, __m), __n_(0), __subs_(__submatches) {
5596  __init(__a, __b);
5597}
5598
5599#ifndef _LIBCPP_CXX03_LANG
5600
5601template <class _BidirectionalIterator, class _CharT, class _Traits>
5602regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::regex_token_iterator(
5603    _BidirectionalIterator __a,
5604    _BidirectionalIterator __b,
5605    const regex_type& __re,
5606    initializer_list<int> __submatches,
5607    regex_constants::match_flag_type __m)
5608    : __position_(__a, __b, __re, __m), __n_(0), __subs_(__submatches) {
5609  __init(__a, __b);
5610}
5611
5612#endif // _LIBCPP_CXX03_LANG
5613
5614template <class _BidirectionalIterator, class _CharT, class _Traits>
5615template <size_t _Np>
5616regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::regex_token_iterator(
5617    _BidirectionalIterator __a,
5618    _BidirectionalIterator __b,
5619    const regex_type& __re,
5620    const int (&__submatches)[_Np],
5621    regex_constants::match_flag_type __m)
5622    : __position_(__a, __b, __re, __m), __n_(0), __subs_(begin(__submatches), end(__submatches)) {
5623  __init(__a, __b);
5624}
5625
5626template <class _BidirectionalIterator, class _CharT, class _Traits>
5627regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::regex_token_iterator(const regex_token_iterator& __x)
5628    : __position_(__x.__position_),
5629      __result_(__x.__result_),
5630      __suffix_(__x.__suffix_),
5631      __n_(__x.__n_),
5632      __subs_(__x.__subs_) {
5633  if (__x.__result_ == &__x.__suffix_)
5634    __result_ = &__suffix_;
5635  else if (__result_ != nullptr)
5636    __establish_result();
5637}
5638
5639template <class _BidirectionalIterator, class _CharT, class _Traits>
5640regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>&
5641regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::operator=(const regex_token_iterator& __x) {
5642  if (this != &__x) {
5643    __position_ = __x.__position_;
5644    if (__x.__result_ == &__x.__suffix_)
5645      __result_ = &__suffix_;
5646    else
5647      __result_ = __x.__result_;
5648    __suffix_ = __x.__suffix_;
5649    __n_      = __x.__n_;
5650    __subs_   = __x.__subs_;
5651
5652    if (__result_ != nullptr && __result_ != &__suffix_)
5653      __establish_result();
5654  }
5655  return *this;
5656}
5657
5658template <class _BidirectionalIterator, class _CharT, class _Traits>
5659bool regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::operator==(const regex_token_iterator& __x) const {
5660  if (__result_ == nullptr && __x.__result_ == nullptr)
5661    return true;
5662  if (__result_ == &__suffix_ && __x.__result_ == &__x.__suffix_ && __suffix_ == __x.__suffix_)
5663    return true;
5664  if (__result_ == nullptr || __x.__result_ == nullptr)
5665    return false;
5666  if (__result_ == &__suffix_ || __x.__result_ == &__x.__suffix_)
5667    return false;
5668  return __position_ == __x.__position_ && __n_ == __x.__n_ && __subs_ == __x.__subs_;
5669}
5670
5671template <class _BidirectionalIterator, class _CharT, class _Traits>
5672regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>&
5673regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::operator++() {
5674  _Position __prev = __position_;
5675  if (__result_ == &__suffix_)
5676    __result_ = nullptr;
5677  else if (static_cast<size_t>(__n_ + 1) < __subs_.size()) {
5678    ++__n_;
5679    __establish_result();
5680  } else {
5681    __n_ = 0;
5682    ++__position_;
5683    if (__position_ != _Position())
5684      __establish_result();
5685    else {
5686      if (std::find(__subs_.begin(), __subs_.end(), -1) != __subs_.end() && __prev->suffix().length() != 0) {
5687        __suffix_.matched = true;
5688        __suffix_.first   = __prev->suffix().first;
5689        __suffix_.second  = __prev->suffix().second;
5690        __result_         = &__suffix_;
5691      } else
5692        __result_ = nullptr;
5693    }
5694  }
5695  return *this;
5696}
5697
5698// regex_replace
5699
5700template <class _OutputIterator, class _BidirectionalIterator, class _Traits, class _CharT>
5701_LIBCPP_HIDE_FROM_ABI _OutputIterator regex_replace(
5702    _OutputIterator __output_iter,
5703    _BidirectionalIterator __first,
5704    _BidirectionalIterator __last,
5705    const basic_regex<_CharT, _Traits>& __e,
5706    const _CharT* __fmt,
5707    regex_constants::match_flag_type __flags = regex_constants::match_default) {
5708  typedef regex_iterator<_BidirectionalIterator, _CharT, _Traits> _Iter;
5709  _Iter __i(__first, __last, __e, __flags);
5710  _Iter __eof;
5711  if (__i == __eof) {
5712    if (!(__flags & regex_constants::format_no_copy))
5713      __output_iter = std::copy(__first, __last, __output_iter);
5714  } else {
5715    sub_match<_BidirectionalIterator> __lm;
5716    for (size_t __len = char_traits<_CharT>::length(__fmt); __i != __eof; ++__i) {
5717      if (!(__flags & regex_constants::format_no_copy))
5718        __output_iter = std::copy(__i->prefix().first, __i->prefix().second, __output_iter);
5719      __output_iter = __i->format(__output_iter, __fmt, __fmt + __len, __flags);
5720      __lm          = __i->suffix();
5721      if (__flags & regex_constants::format_first_only)
5722        break;
5723    }
5724    if (!(__flags & regex_constants::format_no_copy))
5725      __output_iter = std::copy(__lm.first, __lm.second, __output_iter);
5726  }
5727  return __output_iter;
5728}
5729
5730template <class _OutputIterator, class _BidirectionalIterator, class _Traits, class _CharT, class _ST, class _SA>
5731inline _LIBCPP_HIDE_FROM_ABI _OutputIterator regex_replace(
5732    _OutputIterator __output_iter,
5733    _BidirectionalIterator __first,
5734    _BidirectionalIterator __last,
5735    const basic_regex<_CharT, _Traits>& __e,
5736    const basic_string<_CharT, _ST, _SA>& __fmt,
5737    regex_constants::match_flag_type __flags = regex_constants::match_default) {
5738  return std::regex_replace(__output_iter, __first, __last, __e, __fmt.c_str(), __flags);
5739}
5740
5741template <class _Traits, class _CharT, class _ST, class _SA, class _FST, class _FSA>
5742inline _LIBCPP_HIDE_FROM_ABI basic_string<_CharT, _ST, _SA>
5743regex_replace(const basic_string<_CharT, _ST, _SA>& __s,
5744              const basic_regex<_CharT, _Traits>& __e,
5745              const basic_string<_CharT, _FST, _FSA>& __fmt,
5746              regex_constants::match_flag_type __flags = regex_constants::match_default) {
5747  basic_string<_CharT, _ST, _SA> __r;
5748  std::regex_replace(std::back_inserter(__r), __s.begin(), __s.end(), __e, __fmt.c_str(), __flags);
5749  return __r;
5750}
5751
5752template <class _Traits, class _CharT, class _ST, class _SA>
5753inline _LIBCPP_HIDE_FROM_ABI basic_string<_CharT, _ST, _SA>
5754regex_replace(const basic_string<_CharT, _ST, _SA>& __s,
5755              const basic_regex<_CharT, _Traits>& __e,
5756              const _CharT* __fmt,
5757              regex_constants::match_flag_type __flags = regex_constants::match_default) {
5758  basic_string<_CharT, _ST, _SA> __r;
5759  std::regex_replace(std::back_inserter(__r), __s.begin(), __s.end(), __e, __fmt, __flags);
5760  return __r;
5761}
5762
5763template <class _Traits, class _CharT, class _ST, class _SA>
5764inline _LIBCPP_HIDE_FROM_ABI basic_string<_CharT>
5765regex_replace(const _CharT* __s,
5766              const basic_regex<_CharT, _Traits>& __e,
5767              const basic_string<_CharT, _ST, _SA>& __fmt,
5768              regex_constants::match_flag_type __flags = regex_constants::match_default) {
5769  basic_string<_CharT> __r;
5770  std::regex_replace(std::back_inserter(__r), __s, __s + char_traits<_CharT>::length(__s), __e, __fmt.c_str(), __flags);
5771  return __r;
5772}
5773
5774template <class _Traits, class _CharT>
5775inline _LIBCPP_HIDE_FROM_ABI basic_string<_CharT>
5776regex_replace(const _CharT* __s,
5777              const basic_regex<_CharT, _Traits>& __e,
5778              const _CharT* __fmt,
5779              regex_constants::match_flag_type __flags = regex_constants::match_default) {
5780  basic_string<_CharT> __r;
5781  std::regex_replace(std::back_inserter(__r), __s, __s + char_traits<_CharT>::length(__s), __e, __fmt, __flags);
5782  return __r;
5783}
5784
5785_LIBCPP_END_NAMESPACE_STD
5786
5787#if _LIBCPP_STD_VER >= 17
5788_LIBCPP_BEGIN_NAMESPACE_STD
5789namespace pmr {
5790template <class _BidirT>
5791using match_results _LIBCPP_AVAILABILITY_PMR =
5792    std::match_results<_BidirT, polymorphic_allocator<std::sub_match<_BidirT>>>;
5793
5794using cmatch _LIBCPP_AVAILABILITY_PMR = match_results<const char*>;
5795using smatch _LIBCPP_AVAILABILITY_PMR = match_results<std::pmr::string::const_iterator>;
5796
5797#  ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
5798using wcmatch _LIBCPP_AVAILABILITY_PMR = match_results<const wchar_t*>;
5799using wsmatch _LIBCPP_AVAILABILITY_PMR = match_results<std::pmr::wstring::const_iterator>;
5800#  endif
5801} // namespace pmr
5802_LIBCPP_END_NAMESPACE_STD
5803#endif
5804
5805_LIBCPP_POP_MACROS
5806
5807#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
5808#  include <atomic>
5809#  include <concepts>
5810#  include <cstdlib>
5811#  include <iosfwd>
5812#  include <iterator>
5813#  include <mutex>
5814#  include <new>
5815#  include <type_traits>
5816#  include <typeinfo>
5817#  include <utility>
5818#endif
5819
5820#endif // _LIBCPP_REGEX
5821