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