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