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