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