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