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