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