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