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