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