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