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