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