• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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    void swap(sub_match& s) noexcept(see below);
230};
231
232typedef sub_match<const char*>             csub_match;
233typedef sub_match<const wchar_t*>          wcsub_match;
234typedef sub_match<string::const_iterator>  ssub_match;
235typedef sub_match<wstring::const_iterator> wssub_match;
236
237template <class BiIter>
238    bool
239    operator==(const sub_match<BiIter>& lhs, const sub_match<BiIter>& rhs);
240
241template <class BiIter>
242    bool
243    operator!=(const sub_match<BiIter>& lhs, const sub_match<BiIter>& rhs);
244
245template <class BiIter>
246    bool
247    operator<(const sub_match<BiIter>& lhs, const sub_match<BiIter>& rhs);
248
249template <class BiIter>
250    bool
251    operator<=(const sub_match<BiIter>& lhs, const sub_match<BiIter>& rhs);
252
253template <class BiIter>
254    bool
255    operator>=(const sub_match<BiIter>& lhs, const sub_match<BiIter>& rhs);
256
257template <class BiIter>
258    bool
259    operator>(const sub_match<BiIter>& lhs, const sub_match<BiIter>& rhs);
260
261template <class BiIter, class ST, class SA>
262    bool
263    operator==(const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& lhs,
264               const sub_match<BiIter>& rhs);
265
266template <class BiIter, class ST, class SA>
267    bool
268    operator!=(const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& lhs,
269               const sub_match<BiIter>& rhs);
270
271template <class BiIter, class ST, class SA>
272    bool
273    operator<(const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& lhs,
274              const sub_match<BiIter>& rhs);
275
276template <class BiIter, class ST, class SA>
277    bool
278    operator>(const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& lhs,
279              const sub_match<BiIter>& rhs);
280
281template <class BiIter, class ST, class SA>
282    bool operator>=(const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& lhs,
283                    const sub_match<BiIter>& rhs);
284
285template <class BiIter, class ST, class SA>
286    bool
287    operator<=(const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& lhs,
288               const sub_match<BiIter>& rhs);
289
290template <class BiIter, class ST, class SA>
291    bool
292    operator==(const sub_match<BiIter>& lhs,
293               const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& rhs);
294
295template <class BiIter, class ST, class SA>
296    bool
297    operator!=(const sub_match<BiIter>& lhs,
298               const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& rhs);
299
300template <class BiIter, class ST, class SA>
301    bool
302    operator<(const sub_match<BiIter>& lhs,
303              const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& rhs);
304
305template <class BiIter, class ST, class SA>
306    bool operator>(const sub_match<BiIter>& lhs,
307                   const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& rhs);
308
309template <class BiIter, class ST, class SA>
310    bool
311    operator>=(const sub_match<BiIter>& lhs,
312               const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& rhs);
313
314template <class BiIter, class ST, class SA>
315    bool
316    operator<=(const sub_match<BiIter>& lhs,
317               const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& rhs);
318
319template <class BiIter>
320    bool
321    operator==(typename iterator_traits<BiIter>::value_type const* lhs,
322               const sub_match<BiIter>& rhs);
323
324template <class BiIter>
325    bool
326    operator!=(typename iterator_traits<BiIter>::value_type const* lhs,
327               const sub_match<BiIter>& rhs);
328
329template <class BiIter>
330    bool
331    operator<(typename iterator_traits<BiIter>::value_type const* lhs,
332              const sub_match<BiIter>& rhs);
333
334template <class BiIter>
335    bool
336    operator>(typename iterator_traits<BiIter>::value_type const* lhs,
337              const sub_match<BiIter>& rhs);
338
339template <class BiIter>
340    bool
341    operator>=(typename iterator_traits<BiIter>::value_type const* lhs,
342               const sub_match<BiIter>& rhs);
343
344template <class BiIter>
345    bool
346    operator<=(typename iterator_traits<BiIter>::value_type const* lhs,
347               const sub_match<BiIter>& rhs);
348
349template <class BiIter>
350    bool
351    operator==(const sub_match<BiIter>& lhs,
352               typename iterator_traits<BiIter>::value_type const* rhs);
353
354template <class BiIter>
355    bool
356    operator!=(const sub_match<BiIter>& lhs,
357               typename iterator_traits<BiIter>::value_type const* rhs);
358
359template <class BiIter>
360    bool
361    operator<(const sub_match<BiIter>& lhs,
362              typename iterator_traits<BiIter>::value_type const* rhs);
363
364template <class BiIter>
365    bool
366    operator>(const sub_match<BiIter>& lhs,
367              typename iterator_traits<BiIter>::value_type const* rhs);
368
369template <class BiIter>
370    bool
371    operator>=(const sub_match<BiIter>& lhs,
372               typename iterator_traits<BiIter>::value_type const* rhs);
373
374template <class BiIter>
375    bool
376    operator<=(const sub_match<BiIter>& lhs,
377               typename iterator_traits<BiIter>::value_type const* rhs);
378
379template <class BiIter>
380    bool
381    operator==(typename iterator_traits<BiIter>::value_type const& lhs,
382               const sub_match<BiIter>& rhs);
383
384template <class BiIter>
385    bool
386    operator!=(typename iterator_traits<BiIter>::value_type const& lhs,
387               const sub_match<BiIter>& rhs);
388
389template <class BiIter>
390    bool
391    operator<(typename iterator_traits<BiIter>::value_type const& lhs,
392              const sub_match<BiIter>& rhs);
393
394template <class BiIter>
395    bool
396    operator>(typename iterator_traits<BiIter>::value_type const& lhs,
397              const sub_match<BiIter>& rhs);
398
399template <class BiIter>
400    bool
401    operator>=(typename iterator_traits<BiIter>::value_type const& lhs,
402               const sub_match<BiIter>& rhs);
403
404template <class BiIter>
405    bool
406    operator<=(typename iterator_traits<BiIter>::value_type const& lhs,
407               const sub_match<BiIter>& rhs);
408
409template <class BiIter>
410    bool
411    operator==(const sub_match<BiIter>& lhs,
412               typename iterator_traits<BiIter>::value_type const& rhs);
413
414template <class BiIter>
415    bool
416    operator!=(const sub_match<BiIter>& lhs,
417               typename iterator_traits<BiIter>::value_type const& rhs);
418
419template <class BiIter>
420    bool
421    operator<(const sub_match<BiIter>& lhs,
422              typename iterator_traits<BiIter>::value_type const& rhs);
423
424template <class BiIter>
425    bool
426    operator>(const sub_match<BiIter>& lhs,
427              typename iterator_traits<BiIter>::value_type const& rhs);
428
429template <class BiIter>
430    bool
431    operator>=(const sub_match<BiIter>& lhs,
432               typename iterator_traits<BiIter>::value_type const& rhs);
433
434template <class BiIter>
435    bool
436    operator<=(const sub_match<BiIter>& lhs,
437               typename iterator_traits<BiIter>::value_type const& rhs);
438
439template <class charT, class ST, class BiIter>
440    basic_ostream<charT, ST>&
441    operator<<(basic_ostream<charT, ST>& os, const sub_match<BiIter>& m);
442
443template <class BidirectionalIterator,
444          class Allocator = allocator<sub_match<BidirectionalIterator>>>
445class match_results
446{
447public:
448    typedef sub_match<BidirectionalIterator>                  value_type;
449    typedef const value_type&                                 const_reference;
450    typedef value_type&                                       reference;
451    typedef /implementation-defined/                          const_iterator;
452    typedef const_iterator                                    iterator;
453    typedef typename iterator_traits<BidirectionalIterator>::difference_type difference_type;
454    typedef typename allocator_traits<Allocator>::size_type   size_type;
455    typedef Allocator                                         allocator_type;
456    typedef typename iterator_traits<BidirectionalIterator>::value_type char_type;
457    typedef basic_string<char_type>                           string_type;
458
459    // construct/copy/destroy:
460    explicit match_results(const Allocator& a = Allocator()); // before C++20
461    match_results() : match_results(Allocator()) {}           // C++20
462    explicit match_results(const Allocator& a);               // C++20
463    match_results(const match_results& m);
464    match_results(match_results&& m) noexcept;
465    match_results& operator=(const match_results& m);
466    match_results& operator=(match_results&& m);
467    ~match_results();
468
469    bool ready() const;
470
471    // size:
472    size_type size() const;
473    size_type max_size() const;
474    bool empty() const;
475
476    // element access:
477    difference_type length(size_type sub = 0) const;
478    difference_type position(size_type sub = 0) const;
479    string_type str(size_type sub = 0) const;
480    const_reference operator[](size_type n) const;
481
482    const_reference prefix() const;
483    const_reference suffix() const;
484
485    const_iterator begin() const;
486    const_iterator end() const;
487    const_iterator cbegin() const;
488    const_iterator cend() const;
489
490    // format:
491    template <class OutputIter>
492        OutputIter
493        format(OutputIter out, const char_type* fmt_first,
494               const char_type* fmt_last,
495               regex_constants::match_flag_type flags = regex_constants::format_default) const;
496    template <class OutputIter, class ST, class SA>
497        OutputIter
498        format(OutputIter out, const basic_string<char_type, ST, SA>& fmt,
499               regex_constants::match_flag_type flags = regex_constants::format_default) const;
500    template <class ST, class SA>
501        basic_string<char_type, ST, SA>
502        format(const basic_string<char_type, ST, SA>& fmt,
503               regex_constants::match_flag_type flags = regex_constants::format_default) const;
504    string_type
505        format(const char_type* fmt,
506               regex_constants::match_flag_type flags = regex_constants::format_default) const;
507
508    // allocator:
509    allocator_type get_allocator() const;
510
511    // swap:
512    void swap(match_results& that);
513};
514
515typedef match_results<const char*>             cmatch;
516typedef match_results<const wchar_t*>          wcmatch;
517typedef match_results<string::const_iterator>  smatch;
518typedef match_results<wstring::const_iterator> wsmatch;
519
520template <class BidirectionalIterator, class Allocator>
521    bool
522    operator==(const match_results<BidirectionalIterator, Allocator>& m1,
523               const match_results<BidirectionalIterator, Allocator>& m2);
524
525template <class BidirectionalIterator, class Allocator>
526    bool
527    operator!=(const match_results<BidirectionalIterator, Allocator>& m1,
528               const match_results<BidirectionalIterator, Allocator>& m2);
529
530template <class BidirectionalIterator, class Allocator>
531    void
532    swap(match_results<BidirectionalIterator, Allocator>& m1,
533         match_results<BidirectionalIterator, Allocator>& m2);
534
535template <class BidirectionalIterator, class Allocator, class charT, class traits>
536    bool
537    regex_match(BidirectionalIterator first, BidirectionalIterator last,
538                match_results<BidirectionalIterator, Allocator>& m,
539                const basic_regex<charT, traits>& e,
540                regex_constants::match_flag_type flags = regex_constants::match_default);
541
542template <class BidirectionalIterator, class charT, class traits>
543    bool
544    regex_match(BidirectionalIterator first, BidirectionalIterator last,
545                const basic_regex<charT, traits>& e,
546                regex_constants::match_flag_type flags = regex_constants::match_default);
547
548template <class charT, class Allocator, class traits>
549    bool
550    regex_match(const charT* str, match_results<const charT*, Allocator>& m,
551                const basic_regex<charT, traits>& e,
552                regex_constants::match_flag_type flags = regex_constants::match_default);
553
554template <class ST, class SA, class Allocator, class charT, class traits>
555    bool
556    regex_match(const basic_string<charT, ST, SA>& s,
557                match_results<typename basic_string<charT, ST, SA>::const_iterator, Allocator>& m,
558                const basic_regex<charT, traits>& e,
559                regex_constants::match_flag_type flags = regex_constants::match_default);
560
561template <class ST, class SA, class Allocator, class charT, class traits>
562    bool
563    regex_match(const basic_string<charT, ST, SA>&& s,
564                match_results<typename basic_string<charT, ST, SA>::const_iterator, Allocator>& m,
565                const basic_regex<charT, traits>& e,
566                regex_constants::match_flag_type flags = regex_constants::match_default) = delete; // C++14
567
568template <class charT, class traits>
569    bool
570    regex_match(const charT* str, const basic_regex<charT, traits>& e,
571                regex_constants::match_flag_type flags = regex_constants::match_default);
572
573template <class ST, class SA, class charT, class traits>
574    bool
575    regex_match(const basic_string<charT, ST, SA>& s,
576                const basic_regex<charT, traits>& e,
577                regex_constants::match_flag_type flags = regex_constants::match_default);
578
579template <class BidirectionalIterator, class Allocator, class charT, class traits>
580    bool
581    regex_search(BidirectionalIterator first, BidirectionalIterator last,
582                 match_results<BidirectionalIterator, Allocator>& m,
583                 const basic_regex<charT, traits>& e,
584                 regex_constants::match_flag_type flags = regex_constants::match_default);
585
586template <class BidirectionalIterator, class charT, class traits>
587    bool
588    regex_search(BidirectionalIterator first, BidirectionalIterator last,
589                 const basic_regex<charT, traits>& e,
590                 regex_constants::match_flag_type flags = regex_constants::match_default);
591
592template <class charT, class Allocator, class traits>
593    bool
594    regex_search(const charT* str, match_results<const charT*, Allocator>& m,
595                 const basic_regex<charT, traits>& e,
596                 regex_constants::match_flag_type flags = regex_constants::match_default);
597
598template <class charT, class traits>
599    bool
600    regex_search(const charT* str, const basic_regex<charT, traits>& e,
601                 regex_constants::match_flag_type flags = regex_constants::match_default);
602
603template <class ST, class SA, class charT, class traits>
604    bool
605    regex_search(const basic_string<charT, ST, SA>& s,
606                 const basic_regex<charT, traits>& e,
607                 regex_constants::match_flag_type flags = regex_constants::match_default);
608
609template <class ST, class SA, class Allocator, class charT, class traits>
610    bool
611    regex_search(const basic_string<charT, ST, SA>& s,
612                 match_results<typename basic_string<charT, ST, SA>::const_iterator, Allocator>& m,
613                 const basic_regex<charT, traits>& e,
614                 regex_constants::match_flag_type flags = regex_constants::match_default);
615
616template <class ST, class SA, class Allocator, class charT, class traits>
617    bool
618    regex_search(const basic_string<charT, ST, SA>&& s,
619                 match_results<typename basic_string<charT, ST, SA>::const_iterator, Allocator>& m,
620                 const basic_regex<charT, traits>& e,
621                 regex_constants::match_flag_type flags = regex_constants::match_default) = delete; // C++14
622
623template <class OutputIterator, class BidirectionalIterator,
624          class traits, class charT, class ST, class SA>
625    OutputIterator
626    regex_replace(OutputIterator out,
627                  BidirectionalIterator first, BidirectionalIterator last,
628                  const basic_regex<charT, traits>& e,
629                  const basic_string<charT, ST, SA>& fmt,
630                  regex_constants::match_flag_type flags = regex_constants::match_default);
631
632template <class OutputIterator, class BidirectionalIterator,
633          class traits, class charT>
634    OutputIterator
635    regex_replace(OutputIterator out,
636                  BidirectionalIterator first, BidirectionalIterator last,
637                  const basic_regex<charT, traits>& e, const charT* fmt,
638                  regex_constants::match_flag_type flags = regex_constants::match_default);
639
640template <class traits, class charT, class ST, class SA, class FST, class FSA>
641    basic_string<charT, ST, SA>
642    regex_replace(const basic_string<charT, ST, SA>& s,
643                  const basic_regex<charT, traits>& e,
644                  const basic_string<charT, FST, FSA>& fmt,
645                  regex_constants::match_flag_type flags = regex_constants::match_default);
646
647template <class traits, class charT, class ST, class SA>
648    basic_string<charT, ST, SA>
649    regex_replace(const basic_string<charT, ST, SA>& s,
650                  const basic_regex<charT, traits>& e, const charT* fmt,
651                  regex_constants::match_flag_type flags = regex_constants::match_default);
652
653template <class traits, class charT, class ST, class SA>
654    basic_string<charT>
655    regex_replace(const charT* s,
656                  const basic_regex<charT, traits>& e,
657                  const basic_string<charT, ST, SA>& fmt,
658                  regex_constants::match_flag_type flags = regex_constants::match_default);
659
660template <class traits, class charT>
661    basic_string<charT>
662    regex_replace(const charT* s,
663                  const basic_regex<charT, traits>& e,
664                  const charT* fmt,
665                  regex_constants::match_flag_type flags = regex_constants::match_default);
666
667template <class BidirectionalIterator,
668          class charT = typename iterator_traits< BidirectionalIterator>::value_type,
669          class traits = regex_traits<charT>>
670class regex_iterator
671{
672public:
673    typedef basic_regex<charT, traits>           regex_type;
674    typedef match_results<BidirectionalIterator> value_type;
675    typedef ptrdiff_t                            difference_type;
676    typedef const value_type*                    pointer;
677    typedef const value_type&                    reference;
678    typedef forward_iterator_tag                 iterator_category;
679
680    regex_iterator();
681    regex_iterator(BidirectionalIterator a, BidirectionalIterator b,
682                   const regex_type& re,
683                   regex_constants::match_flag_type m = regex_constants::match_default);
684    regex_iterator(BidirectionalIterator a, BidirectionalIterator b,
685                   const regex_type&& re,
686                   regex_constants::match_flag_type m
687                                     = regex_constants::match_default) = delete; // C++14
688    regex_iterator(const regex_iterator&);
689    regex_iterator& operator=(const regex_iterator&);
690
691    bool operator==(const regex_iterator&) const;
692    bool operator!=(const regex_iterator&) const;
693
694    const value_type& operator*() const;
695    const value_type* operator->() const;
696
697    regex_iterator& operator++();
698    regex_iterator operator++(int);
699};
700
701typedef regex_iterator<const char*>             cregex_iterator;
702typedef regex_iterator<const wchar_t*>          wcregex_iterator;
703typedef regex_iterator<string::const_iterator>  sregex_iterator;
704typedef regex_iterator<wstring::const_iterator> wsregex_iterator;
705
706template <class BidirectionalIterator,
707          class charT = typename iterator_traits<BidirectionalIterator>::value_type,
708          class traits = regex_traits<charT>>
709class regex_token_iterator
710{
711public:
712    typedef basic_regex<charT, traits>       regex_type;
713    typedef sub_match<BidirectionalIterator> value_type;
714    typedef ptrdiff_t                        difference_type;
715    typedef const value_type*                pointer;
716    typedef const value_type&                reference;
717    typedef forward_iterator_tag             iterator_category;
718
719    regex_token_iterator();
720    regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b,
721                         const regex_type& re, int submatch = 0,
722                         regex_constants::match_flag_type m = regex_constants::match_default);
723    regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b,
724                         const regex_type&& re, int submatch = 0,
725                         regex_constants::match_flag_type m = regex_constants::match_default) = delete; // C++14
726    regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b,
727                         const regex_type& re, const vector<int>& submatches,
728                         regex_constants::match_flag_type m = regex_constants::match_default);
729    regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b,
730                         const regex_type&& re, const vector<int>& submatches,
731                         regex_constants::match_flag_type m = regex_constants::match_default) = delete; // C++14
732    regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b,
733                         const regex_type& re, initializer_list<int> submatches,
734                         regex_constants::match_flag_type m = regex_constants::match_default);
735    regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b,
736                         const regex_type&& re, initializer_list<int> submatches,
737                         regex_constants::match_flag_type m = regex_constants::match_default) = delete; // C++14
738    template <size_t N>
739        regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b,
740                             const regex_type& re, const int (&submatches)[N],
741                             regex_constants::match_flag_type m = regex_constants::match_default);
742    template <size_t N>
743        regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b,
744                             const regex_type&& re, const int (&submatches)[N],
745                             regex_constants::match_flag_type m = regex_constants::match_default) = delete; // C++14
746    regex_token_iterator(const regex_token_iterator&);
747    regex_token_iterator& operator=(const regex_token_iterator&);
748
749    bool operator==(const regex_token_iterator&) const;
750    bool operator!=(const regex_token_iterator&) const;
751
752    const value_type& operator*() const;
753    const value_type* operator->() const;
754
755    regex_token_iterator& operator++();
756    regex_token_iterator operator++(int);
757};
758
759typedef regex_token_iterator<const char*>             cregex_token_iterator;
760typedef regex_token_iterator<const wchar_t*>          wcregex_token_iterator;
761typedef regex_token_iterator<string::const_iterator>  sregex_token_iterator;
762typedef regex_token_iterator<wstring::const_iterator> wsregex_token_iterator;
763
764} // std
765*/
766
767#include <__algorithm/find.h>
768#include <__algorithm/search.h>
769#include <__assert> // all public C++ headers provide the assertion handler
770#include <__config>
771#include <__iterator/back_insert_iterator.h>
772#include <__iterator/wrap_iter.h>
773#include <__locale>
774#include <__memory_resource/polymorphic_allocator.h>
775#include <__type_traits/is_swappable.h>
776#include <__utility/move.h>
777#include <__utility/pair.h>
778#include <__utility/swap.h>
779#include <__verbose_abort>
780#include <deque>
781#include <stdexcept>
782#include <string>
783#include <vector>
784#include <version>
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
836_LIBCPP_HIDE_FROM_ABI inline _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    _LIBCPP_HIDE_FROM_ABI regex_error(const regex_error&) _NOEXCEPT = default;
1009    ~regex_error() _NOEXCEPT override;
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_HAS_NO_EXCEPTIONS
1019    throw regex_error(_Ev);
1020#else
1021    _LIBCPP_VERBOSE_ABORT("regex_error was thrown in -fno-exceptions mode");
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#if defined(__BIONIC__) || defined(_NEWLIB_VERSION)
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    // We also need this workaround for newlib since _NEWLIB_VERSION is not
1042    // defined yet inside __config, so we can't set the
1043    // _LIBCPP_PROVIDES_DEFAULT_RUNE_TABLE macro. Additionally, newlib is
1044    // often used for space constrained environments, so it makes sense not to
1045    // duplicate the ctype table.
1046    typedef uint16_t char_class_type;
1047#else
1048    typedef ctype_base::mask        char_class_type;
1049#endif
1050
1051    static const char_class_type __regex_word = ctype_base::__regex_word;
1052private:
1053    locale __loc_;
1054    const ctype<char_type>* __ct_;
1055    const collate<char_type>* __col_;
1056
1057public:
1058    regex_traits();
1059
1060    _LIBCPP_INLINE_VISIBILITY
1061    static size_t length(const char_type* __p)
1062        {return char_traits<char_type>::length(__p);}
1063    _LIBCPP_INLINE_VISIBILITY
1064    char_type translate(char_type __c) const {return __c;}
1065    char_type translate_nocase(char_type __c) const;
1066    template <class _ForwardIterator>
1067        string_type
1068        transform(_ForwardIterator __f, _ForwardIterator __l) const;
1069    template <class _ForwardIterator>
1070        _LIBCPP_INLINE_VISIBILITY
1071        string_type
1072        transform_primary( _ForwardIterator __f, _ForwardIterator __l) const
1073            {return __transform_primary(__f, __l, char_type());}
1074    template <class _ForwardIterator>
1075        _LIBCPP_INLINE_VISIBILITY
1076        string_type
1077        lookup_collatename(_ForwardIterator __f, _ForwardIterator __l) const
1078            {return __lookup_collatename(__f, __l, char_type());}
1079    template <class _ForwardIterator>
1080        _LIBCPP_INLINE_VISIBILITY
1081        char_class_type
1082        lookup_classname(_ForwardIterator __f, _ForwardIterator __l,
1083                         bool __icase = false) const
1084            {return __lookup_classname(__f, __l, __icase, char_type());}
1085    bool isctype(char_type __c, char_class_type __m) const;
1086    _LIBCPP_INLINE_VISIBILITY
1087    int value(char_type __ch, int __radix) const
1088        {return __regex_traits_value(__ch, __radix);}
1089    locale_type imbue(locale_type __l);
1090    _LIBCPP_INLINE_VISIBILITY
1091    locale_type getloc()const {return __loc_;}
1092
1093private:
1094    void __init();
1095
1096    template <class _ForwardIterator>
1097        string_type
1098        __transform_primary(_ForwardIterator __f, _ForwardIterator __l, char) const;
1099#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1100    template <class _ForwardIterator>
1101        string_type
1102        __transform_primary(_ForwardIterator __f, _ForwardIterator __l, wchar_t) const;
1103#endif
1104    template <class _ForwardIterator>
1105        string_type
1106        __lookup_collatename(_ForwardIterator __f, _ForwardIterator __l, char) const;
1107#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1108    template <class _ForwardIterator>
1109        string_type
1110        __lookup_collatename(_ForwardIterator __f, _ForwardIterator __l, wchar_t) const;
1111#endif
1112    template <class _ForwardIterator>
1113        char_class_type
1114        __lookup_classname(_ForwardIterator __f, _ForwardIterator __l,
1115                           bool __icase, char) const;
1116#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1117    template <class _ForwardIterator>
1118        char_class_type
1119        __lookup_classname(_ForwardIterator __f, _ForwardIterator __l,
1120                           bool __icase, wchar_t) const;
1121#endif
1122
1123    static int __regex_traits_value(unsigned char __ch, int __radix);
1124    _LIBCPP_INLINE_VISIBILITY
1125    int __regex_traits_value(char __ch, int __radix) const
1126        {return __regex_traits_value(static_cast<unsigned char>(__ch), __radix);}
1127#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1128    _LIBCPP_INLINE_VISIBILITY
1129    int __regex_traits_value(wchar_t __ch, int __radix) const;
1130#endif
1131};
1132
1133template <class _CharT>
1134const typename regex_traits<_CharT>::char_class_type
1135regex_traits<_CharT>::__regex_word;
1136
1137template <class _CharT>
1138regex_traits<_CharT>::regex_traits()
1139{
1140    __init();
1141}
1142
1143template <class _CharT>
1144typename regex_traits<_CharT>::char_type
1145regex_traits<_CharT>::translate_nocase(char_type __c) const
1146{
1147    return __ct_->tolower(__c);
1148}
1149
1150template <class _CharT>
1151template <class _ForwardIterator>
1152typename regex_traits<_CharT>::string_type
1153regex_traits<_CharT>::transform(_ForwardIterator __f, _ForwardIterator __l) const
1154{
1155    string_type __s(__f, __l);
1156    return __col_->transform(__s.data(), __s.data() + __s.size());
1157}
1158
1159template <class _CharT>
1160void
1161regex_traits<_CharT>::__init()
1162{
1163    __ct_ = &std::use_facet<ctype<char_type> >(__loc_);
1164    __col_ = &std::use_facet<collate<char_type> >(__loc_);
1165}
1166
1167template <class _CharT>
1168typename regex_traits<_CharT>::locale_type
1169regex_traits<_CharT>::imbue(locale_type __l)
1170{
1171    locale __r = __loc_;
1172    __loc_ = __l;
1173    __init();
1174    return __r;
1175}
1176
1177// transform_primary is very FreeBSD-specific
1178
1179template <class _CharT>
1180template <class _ForwardIterator>
1181typename regex_traits<_CharT>::string_type
1182regex_traits<_CharT>::__transform_primary(_ForwardIterator __f,
1183                                          _ForwardIterator __l, char) const
1184{
1185    const string_type __s(__f, __l);
1186    string_type __d = __col_->transform(__s.data(), __s.data() + __s.size());
1187    switch (__d.size())
1188    {
1189    case 1:
1190        break;
1191    case 12:
1192        __d[11] = __d[3];
1193        break;
1194    default:
1195        __d.clear();
1196        break;
1197    }
1198    return __d;
1199}
1200
1201#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1202template <class _CharT>
1203template <class _ForwardIterator>
1204typename regex_traits<_CharT>::string_type
1205regex_traits<_CharT>::__transform_primary(_ForwardIterator __f,
1206                                          _ForwardIterator __l, wchar_t) const
1207{
1208    const string_type __s(__f, __l);
1209    string_type __d = __col_->transform(__s.data(), __s.data() + __s.size());
1210    switch (__d.size())
1211    {
1212    case 1:
1213        break;
1214    case 3:
1215        __d[2] = __d[0];
1216        break;
1217    default:
1218        __d.clear();
1219        break;
1220    }
1221    return __d;
1222}
1223#endif
1224
1225// lookup_collatename is very FreeBSD-specific
1226
1227_LIBCPP_FUNC_VIS string __get_collation_name(const char* __s);
1228
1229template <class _CharT>
1230template <class _ForwardIterator>
1231typename regex_traits<_CharT>::string_type
1232regex_traits<_CharT>::__lookup_collatename(_ForwardIterator __f,
1233                                           _ForwardIterator __l, char) const
1234{
1235    string_type __s(__f, __l);
1236    string_type __r;
1237    if (!__s.empty())
1238    {
1239        __r = std::__get_collation_name(__s.c_str());
1240        if (__r.empty() && __s.size() <= 2)
1241        {
1242            __r = __col_->transform(__s.data(), __s.data() + __s.size());
1243            if (__r.size() == 1 || __r.size() == 12)
1244                __r = __s;
1245            else
1246                __r.clear();
1247        }
1248    }
1249    return __r;
1250}
1251
1252#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1253template <class _CharT>
1254template <class _ForwardIterator>
1255typename regex_traits<_CharT>::string_type
1256regex_traits<_CharT>::__lookup_collatename(_ForwardIterator __f,
1257                                           _ForwardIterator __l, wchar_t) const
1258{
1259    string_type __s(__f, __l);
1260    string __n;
1261    __n.reserve(__s.size());
1262    for (typename string_type::const_iterator __i = __s.begin(), __e = __s.end();
1263                                                              __i != __e; ++__i)
1264    {
1265        if (static_cast<unsigned>(*__i) >= 127)
1266            return string_type();
1267        __n.push_back(char(*__i));
1268    }
1269    string_type __r;
1270    if (!__s.empty())
1271    {
1272        __n = __get_collation_name(__n.c_str());
1273        if (!__n.empty())
1274            __r.assign(__n.begin(), __n.end());
1275        else if (__s.size() <= 2)
1276        {
1277            __r = __col_->transform(__s.data(), __s.data() + __s.size());
1278            if (__r.size() == 1 || __r.size() == 3)
1279                __r = __s;
1280            else
1281                __r.clear();
1282        }
1283    }
1284    return __r;
1285}
1286#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
1287
1288// lookup_classname
1289
1290regex_traits<char>::char_class_type _LIBCPP_FUNC_VIS
1291__get_classname(const char* __s, bool __icase);
1292
1293template <class _CharT>
1294template <class _ForwardIterator>
1295typename regex_traits<_CharT>::char_class_type
1296regex_traits<_CharT>::__lookup_classname(_ForwardIterator __f,
1297                                         _ForwardIterator __l,
1298                                         bool __icase, char) const
1299{
1300    string_type __s(__f, __l);
1301    __ct_->tolower(&__s[0], &__s[0] + __s.size());
1302    return std::__get_classname(__s.c_str(), __icase);
1303}
1304
1305#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1306template <class _CharT>
1307template <class _ForwardIterator>
1308typename regex_traits<_CharT>::char_class_type
1309regex_traits<_CharT>::__lookup_classname(_ForwardIterator __f,
1310                                         _ForwardIterator __l,
1311                                         bool __icase, wchar_t) const
1312{
1313    string_type __s(__f, __l);
1314    __ct_->tolower(&__s[0], &__s[0] + __s.size());
1315    string __n;
1316    __n.reserve(__s.size());
1317    for (typename string_type::const_iterator __i = __s.begin(), __e = __s.end();
1318                                                              __i != __e; ++__i)
1319    {
1320        if (static_cast<unsigned>(*__i) >= 127)
1321            return char_class_type();
1322        __n.push_back(char(*__i));
1323    }
1324    return __get_classname(__n.c_str(), __icase);
1325}
1326#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
1327
1328template <class _CharT>
1329bool
1330regex_traits<_CharT>::isctype(char_type __c, char_class_type __m) const
1331{
1332    if (__ct_->is(__m, __c))
1333        return true;
1334    return (__c == '_' && (__m & __regex_word));
1335}
1336
1337inline _LIBCPP_INLINE_VISIBILITY
1338bool __is_07(unsigned char __c)
1339{
1340    return (__c & 0xF8u) ==
1341#if defined(__MVS__) && !defined(__NATIVE_ASCII_F)
1342        0xF0;
1343#else
1344        0x30;
1345#endif
1346}
1347
1348inline _LIBCPP_INLINE_VISIBILITY
1349bool __is_89(unsigned char __c)
1350{
1351    return (__c & 0xFEu) ==
1352#if defined(__MVS__) && !defined(__NATIVE_ASCII_F)
1353        0xF8;
1354#else
1355        0x38;
1356#endif
1357}
1358
1359inline _LIBCPP_INLINE_VISIBILITY
1360unsigned char __to_lower(unsigned char __c)
1361{
1362#if defined(__MVS__) && !defined(__NATIVE_ASCII_F)
1363    return __c & 0xBF;
1364#else
1365    return __c | 0x20;
1366#endif
1367}
1368
1369template <class _CharT>
1370int
1371regex_traits<_CharT>::__regex_traits_value(unsigned char __ch, int __radix)
1372{
1373    if (__is_07(__ch))  // '0' <= __ch && __ch <= '7'
1374        return __ch - '0';
1375    if (__radix != 8)
1376    {
1377        if (__is_89(__ch))  // '8' <= __ch && __ch <= '9'
1378            return __ch - '0';
1379        if (__radix == 16)
1380        {
1381            __ch = __to_lower(__ch);  // tolower
1382            if ('a' <= __ch && __ch <= 'f')
1383                return __ch - ('a' - 10);
1384        }
1385    }
1386    return -1;
1387}
1388
1389#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1390template <class _CharT>
1391inline
1392int
1393regex_traits<_CharT>::__regex_traits_value(wchar_t __ch, int __radix) const
1394{
1395    return __regex_traits_value(static_cast<unsigned char>(__ct_->narrow(__ch, char_type())), __radix);
1396}
1397#endif
1398
1399template <class _CharT> class __node;
1400
1401template <class _BidirectionalIterator> class _LIBCPP_TEMPLATE_VIS sub_match;
1402
1403template <class _BidirectionalIterator,
1404          class _Allocator = allocator<sub_match<_BidirectionalIterator> > >
1405class _LIBCPP_TEMPLATE_VIS match_results;
1406
1407template <class _CharT>
1408struct __state
1409{
1410    enum
1411    {
1412        __end_state = -1000,
1413        __consume_input,  // -999
1414        __begin_marked_expr, // -998
1415        __end_marked_expr,   // -997
1416        __pop_state,           // -996
1417        __accept_and_consume,  // -995
1418        __accept_but_not_consume,  // -994
1419        __reject,                  // -993
1420        __split,
1421        __repeat
1422    };
1423
1424    int __do_;
1425    const _CharT* __first_;
1426    const _CharT* __current_;
1427    const _CharT* __last_;
1428    vector<sub_match<const _CharT*> > __sub_matches_;
1429    vector<pair<size_t, const _CharT*> > __loop_data_;
1430    const __node<_CharT>* __node_;
1431    regex_constants::match_flag_type __flags_;
1432    bool __at_first_;
1433
1434    _LIBCPP_INLINE_VISIBILITY
1435    __state()
1436        : __do_(0), __first_(nullptr), __current_(nullptr), __last_(nullptr),
1437          __node_(nullptr), __flags_() {}
1438};
1439
1440// __node
1441
1442template <class _CharT>
1443class __node
1444{
1445    __node(const __node&);
1446    __node& operator=(const __node&);
1447public:
1448    typedef _VSTD::__state<_CharT> __state;
1449
1450    _LIBCPP_INLINE_VISIBILITY
1451    __node() {}
1452    _LIBCPP_HIDE_FROM_ABI_VIRTUAL
1453    virtual ~__node() {}
1454
1455    _LIBCPP_HIDE_FROM_ABI_VIRTUAL
1456    virtual void __exec(__state&) const {}
1457    _LIBCPP_HIDE_FROM_ABI_VIRTUAL
1458    virtual void __exec_split(bool, __state&) const {}
1459};
1460
1461// __end_state
1462
1463template <class _CharT>
1464class __end_state
1465    : public __node<_CharT>
1466{
1467public:
1468    typedef _VSTD::__state<_CharT> __state;
1469
1470    _LIBCPP_INLINE_VISIBILITY
1471    __end_state() {}
1472
1473    _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
1474};
1475
1476template <class _CharT>
1477void
1478__end_state<_CharT>::__exec(__state& __s) const
1479{
1480    __s.__do_ = __state::__end_state;
1481}
1482
1483// __has_one_state
1484
1485template <class _CharT>
1486class __has_one_state
1487    : public __node<_CharT>
1488{
1489    __node<_CharT>* __first_;
1490
1491public:
1492    _LIBCPP_INLINE_VISIBILITY
1493    explicit __has_one_state(__node<_CharT>* __s)
1494        : __first_(__s) {}
1495
1496    _LIBCPP_INLINE_VISIBILITY
1497    __node<_CharT>*  first() const {return __first_;}
1498    _LIBCPP_INLINE_VISIBILITY
1499    __node<_CharT>*& first()       {return __first_;}
1500};
1501
1502// __owns_one_state
1503
1504template <class _CharT>
1505class __owns_one_state
1506    : public __has_one_state<_CharT>
1507{
1508    typedef __has_one_state<_CharT> base;
1509
1510public:
1511    _LIBCPP_INLINE_VISIBILITY
1512    explicit __owns_one_state(__node<_CharT>* __s)
1513        : base(__s) {}
1514
1515    ~__owns_one_state() override;
1516};
1517
1518template <class _CharT>
1519__owns_one_state<_CharT>::~__owns_one_state()
1520{
1521    delete this->first();
1522}
1523
1524// __empty_state
1525
1526template <class _CharT>
1527class __empty_state
1528    : public __owns_one_state<_CharT>
1529{
1530    typedef __owns_one_state<_CharT> base;
1531
1532public:
1533    typedef _VSTD::__state<_CharT> __state;
1534
1535    _LIBCPP_INLINE_VISIBILITY
1536    explicit __empty_state(__node<_CharT>* __s)
1537        : base(__s) {}
1538
1539    _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
1540};
1541
1542template <class _CharT>
1543void
1544__empty_state<_CharT>::__exec(__state& __s) const
1545{
1546    __s.__do_ = __state::__accept_but_not_consume;
1547    __s.__node_ = this->first();
1548}
1549
1550// __empty_non_own_state
1551
1552template <class _CharT>
1553class __empty_non_own_state
1554    : public __has_one_state<_CharT>
1555{
1556    typedef __has_one_state<_CharT> base;
1557
1558public:
1559    typedef _VSTD::__state<_CharT> __state;
1560
1561    _LIBCPP_INLINE_VISIBILITY
1562    explicit __empty_non_own_state(__node<_CharT>* __s)
1563        : base(__s) {}
1564
1565    _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
1566};
1567
1568template <class _CharT>
1569void
1570__empty_non_own_state<_CharT>::__exec(__state& __s) const
1571{
1572    __s.__do_ = __state::__accept_but_not_consume;
1573    __s.__node_ = this->first();
1574}
1575
1576// __repeat_one_loop
1577
1578template <class _CharT>
1579class __repeat_one_loop
1580    : public __has_one_state<_CharT>
1581{
1582    typedef __has_one_state<_CharT> base;
1583
1584public:
1585    typedef _VSTD::__state<_CharT> __state;
1586
1587    _LIBCPP_INLINE_VISIBILITY
1588    explicit __repeat_one_loop(__node<_CharT>* __s)
1589        : base(__s) {}
1590
1591    _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
1592};
1593
1594template <class _CharT>
1595void
1596__repeat_one_loop<_CharT>::__exec(__state& __s) const
1597{
1598    __s.__do_ = __state::__repeat;
1599    __s.__node_ = this->first();
1600}
1601
1602// __owns_two_states
1603
1604template <class _CharT>
1605class __owns_two_states
1606    : public __owns_one_state<_CharT>
1607{
1608    typedef __owns_one_state<_CharT> base;
1609
1610    base* __second_;
1611
1612public:
1613    _LIBCPP_INLINE_VISIBILITY
1614    explicit __owns_two_states(__node<_CharT>* __s1, base* __s2)
1615        : base(__s1), __second_(__s2) {}
1616
1617    _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual ~__owns_two_states();
1618
1619    _LIBCPP_INLINE_VISIBILITY
1620    base*  second() const {return __second_;}
1621    _LIBCPP_INLINE_VISIBILITY
1622    base*& second()       {return __second_;}
1623};
1624
1625template <class _CharT>
1626__owns_two_states<_CharT>::~__owns_two_states()
1627{
1628    delete __second_;
1629}
1630
1631// __loop
1632
1633template <class _CharT>
1634class __loop
1635    : public __owns_two_states<_CharT>
1636{
1637    typedef __owns_two_states<_CharT> base;
1638
1639    size_t __min_;
1640    size_t __max_;
1641    unsigned __loop_id_;
1642    unsigned __mexp_begin_;
1643    unsigned __mexp_end_;
1644    bool __greedy_;
1645
1646public:
1647    typedef _VSTD::__state<_CharT> __state;
1648
1649    _LIBCPP_INLINE_VISIBILITY
1650    explicit __loop(unsigned __loop_id,
1651                          __node<_CharT>* __s1, __owns_one_state<_CharT>* __s2,
1652                          unsigned __mexp_begin, unsigned __mexp_end,
1653                          bool __greedy = true,
1654                          size_t __min = 0,
1655                          size_t __max = numeric_limits<size_t>::max())
1656        : base(__s1, __s2), __min_(__min), __max_(__max), __loop_id_(__loop_id),
1657          __mexp_begin_(__mexp_begin), __mexp_end_(__mexp_end),
1658          __greedy_(__greedy) {}
1659
1660    _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state& __s) const;
1661    _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec_split(bool __second, __state& __s) const;
1662
1663private:
1664    _LIBCPP_INLINE_VISIBILITY
1665    void __init_repeat(__state& __s) const
1666    {
1667        __s.__loop_data_[__loop_id_].second = __s.__current_;
1668        for (size_t __i = __mexp_begin_-1; __i != __mexp_end_-1; ++__i)
1669        {
1670            __s.__sub_matches_[__i].first = __s.__last_;
1671            __s.__sub_matches_[__i].second = __s.__last_;
1672            __s.__sub_matches_[__i].matched = false;
1673        }
1674    }
1675};
1676
1677template <class _CharT>
1678void
1679__loop<_CharT>::__exec(__state& __s) const
1680{
1681    if (__s.__do_ == __state::__repeat)
1682    {
1683        bool __do_repeat = ++__s.__loop_data_[__loop_id_].first < __max_;
1684        bool __do_alt = __s.__loop_data_[__loop_id_].first >= __min_;
1685        if (__do_repeat && __do_alt &&
1686                               __s.__loop_data_[__loop_id_].second == __s.__current_)
1687            __do_repeat = false;
1688        if (__do_repeat && __do_alt)
1689            __s.__do_ = __state::__split;
1690        else if (__do_repeat)
1691        {
1692            __s.__do_ = __state::__accept_but_not_consume;
1693            __s.__node_ = this->first();
1694            __init_repeat(__s);
1695        }
1696        else
1697        {
1698            __s.__do_ = __state::__accept_but_not_consume;
1699            __s.__node_ = this->second();
1700        }
1701    }
1702    else
1703    {
1704        __s.__loop_data_[__loop_id_].first = 0;
1705        bool __do_repeat = 0 < __max_;
1706        bool __do_alt = 0 >= __min_;
1707        if (__do_repeat && __do_alt)
1708            __s.__do_ = __state::__split;
1709        else if (__do_repeat)
1710        {
1711            __s.__do_ = __state::__accept_but_not_consume;
1712            __s.__node_ = this->first();
1713            __init_repeat(__s);
1714        }
1715        else
1716        {
1717            __s.__do_ = __state::__accept_but_not_consume;
1718            __s.__node_ = this->second();
1719        }
1720    }
1721}
1722
1723template <class _CharT>
1724void
1725__loop<_CharT>::__exec_split(bool __second, __state& __s) const
1726{
1727    __s.__do_ = __state::__accept_but_not_consume;
1728    if (__greedy_ != __second)
1729    {
1730        __s.__node_ = this->first();
1731        __init_repeat(__s);
1732    }
1733    else
1734        __s.__node_ = this->second();
1735}
1736
1737// __alternate
1738
1739template <class _CharT>
1740class __alternate
1741    : public __owns_two_states<_CharT>
1742{
1743    typedef __owns_two_states<_CharT> base;
1744
1745public:
1746    typedef _VSTD::__state<_CharT> __state;
1747
1748    _LIBCPP_INLINE_VISIBILITY
1749    explicit __alternate(__owns_one_state<_CharT>* __s1,
1750                         __owns_one_state<_CharT>* __s2)
1751        : base(__s1, __s2) {}
1752
1753    _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state& __s) const;
1754    _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec_split(bool __second, __state& __s) const;
1755};
1756
1757template <class _CharT>
1758void
1759__alternate<_CharT>::__exec(__state& __s) const
1760{
1761    __s.__do_ = __state::__split;
1762}
1763
1764template <class _CharT>
1765void
1766__alternate<_CharT>::__exec_split(bool __second, __state& __s) const
1767{
1768    __s.__do_ = __state::__accept_but_not_consume;
1769    if (__second)
1770        __s.__node_ = this->second();
1771    else
1772        __s.__node_ = this->first();
1773}
1774
1775// __begin_marked_subexpression
1776
1777template <class _CharT>
1778class __begin_marked_subexpression
1779    : public __owns_one_state<_CharT>
1780{
1781    typedef __owns_one_state<_CharT> base;
1782
1783    unsigned __mexp_;
1784public:
1785    typedef _VSTD::__state<_CharT> __state;
1786
1787    _LIBCPP_INLINE_VISIBILITY
1788    explicit __begin_marked_subexpression(unsigned __mexp, __node<_CharT>* __s)
1789        : base(__s), __mexp_(__mexp) {}
1790
1791    _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
1792};
1793
1794template <class _CharT>
1795void
1796__begin_marked_subexpression<_CharT>::__exec(__state& __s) const
1797{
1798    __s.__do_ = __state::__accept_but_not_consume;
1799    __s.__sub_matches_[__mexp_-1].first = __s.__current_;
1800    __s.__node_ = this->first();
1801}
1802
1803// __end_marked_subexpression
1804
1805template <class _CharT>
1806class __end_marked_subexpression
1807    : public __owns_one_state<_CharT>
1808{
1809    typedef __owns_one_state<_CharT> base;
1810
1811    unsigned __mexp_;
1812public:
1813    typedef _VSTD::__state<_CharT> __state;
1814
1815    _LIBCPP_INLINE_VISIBILITY
1816    explicit __end_marked_subexpression(unsigned __mexp, __node<_CharT>* __s)
1817        : base(__s), __mexp_(__mexp) {}
1818
1819    _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
1820};
1821
1822template <class _CharT>
1823void
1824__end_marked_subexpression<_CharT>::__exec(__state& __s) const
1825{
1826    __s.__do_ = __state::__accept_but_not_consume;
1827    __s.__sub_matches_[__mexp_-1].second = __s.__current_;
1828    __s.__sub_matches_[__mexp_-1].matched = true;
1829    __s.__node_ = this->first();
1830}
1831
1832// __back_ref
1833
1834template <class _CharT>
1835class __back_ref
1836    : public __owns_one_state<_CharT>
1837{
1838    typedef __owns_one_state<_CharT> base;
1839
1840    unsigned __mexp_;
1841public:
1842    typedef _VSTD::__state<_CharT> __state;
1843
1844    _LIBCPP_INLINE_VISIBILITY
1845    explicit __back_ref(unsigned __mexp, __node<_CharT>* __s)
1846        : base(__s), __mexp_(__mexp) {}
1847
1848    _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
1849};
1850
1851template <class _CharT>
1852void
1853__back_ref<_CharT>::__exec(__state& __s) const
1854{
1855    if (__mexp_ > __s.__sub_matches_.size())
1856        __throw_regex_error<regex_constants::error_backref>();
1857    sub_match<const _CharT*>& __sm = __s.__sub_matches_[__mexp_-1];
1858    if (__sm.matched)
1859    {
1860        ptrdiff_t __len = __sm.second - __sm.first;
1861        if (__s.__last_ - __s.__current_ >= __len &&
1862            _VSTD::equal(__sm.first, __sm.second, __s.__current_))
1863        {
1864            __s.__do_ = __state::__accept_but_not_consume;
1865            __s.__current_ += __len;
1866            __s.__node_ = this->first();
1867        }
1868        else
1869        {
1870            __s.__do_ = __state::__reject;
1871            __s.__node_ = nullptr;
1872        }
1873    }
1874    else
1875    {
1876        __s.__do_ = __state::__reject;
1877        __s.__node_ = nullptr;
1878    }
1879}
1880
1881// __back_ref_icase
1882
1883template <class _CharT, class _Traits>
1884class __back_ref_icase
1885    : public __owns_one_state<_CharT>
1886{
1887    typedef __owns_one_state<_CharT> base;
1888
1889    _Traits __traits_;
1890    unsigned __mexp_;
1891public:
1892    typedef _VSTD::__state<_CharT> __state;
1893
1894    _LIBCPP_INLINE_VISIBILITY
1895    explicit __back_ref_icase(const _Traits& __traits, unsigned __mexp,
1896                              __node<_CharT>* __s)
1897        : base(__s), __traits_(__traits), __mexp_(__mexp) {}
1898
1899    _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
1900};
1901
1902template <class _CharT, class _Traits>
1903void
1904__back_ref_icase<_CharT, _Traits>::__exec(__state& __s) const
1905{
1906    sub_match<const _CharT*>& __sm = __s.__sub_matches_[__mexp_-1];
1907    if (__sm.matched)
1908    {
1909        ptrdiff_t __len = __sm.second - __sm.first;
1910        if (__s.__last_ - __s.__current_ >= __len)
1911        {
1912            for (ptrdiff_t __i = 0; __i < __len; ++__i)
1913            {
1914                if (__traits_.translate_nocase(__sm.first[__i]) !=
1915                                __traits_.translate_nocase(__s.__current_[__i]))
1916                    goto __not_equal;
1917            }
1918            __s.__do_ = __state::__accept_but_not_consume;
1919            __s.__current_ += __len;
1920            __s.__node_ = this->first();
1921        }
1922        else
1923        {
1924            __s.__do_ = __state::__reject;
1925            __s.__node_ = nullptr;
1926        }
1927    }
1928    else
1929    {
1930__not_equal:
1931        __s.__do_ = __state::__reject;
1932        __s.__node_ = nullptr;
1933    }
1934}
1935
1936// __back_ref_collate
1937
1938template <class _CharT, class _Traits>
1939class __back_ref_collate
1940    : public __owns_one_state<_CharT>
1941{
1942    typedef __owns_one_state<_CharT> base;
1943
1944    _Traits __traits_;
1945    unsigned __mexp_;
1946public:
1947    typedef _VSTD::__state<_CharT> __state;
1948
1949    _LIBCPP_INLINE_VISIBILITY
1950    explicit __back_ref_collate(const _Traits& __traits, unsigned __mexp,
1951                              __node<_CharT>* __s)
1952        : base(__s), __traits_(__traits), __mexp_(__mexp) {}
1953
1954    _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
1955};
1956
1957template <class _CharT, class _Traits>
1958void
1959__back_ref_collate<_CharT, _Traits>::__exec(__state& __s) const
1960{
1961    sub_match<const _CharT*>& __sm = __s.__sub_matches_[__mexp_-1];
1962    if (__sm.matched)
1963    {
1964        ptrdiff_t __len = __sm.second - __sm.first;
1965        if (__s.__last_ - __s.__current_ >= __len)
1966        {
1967            for (ptrdiff_t __i = 0; __i < __len; ++__i)
1968            {
1969                if (__traits_.translate(__sm.first[__i]) !=
1970                                       __traits_.translate(__s.__current_[__i]))
1971                    goto __not_equal;
1972            }
1973            __s.__do_ = __state::__accept_but_not_consume;
1974            __s.__current_ += __len;
1975            __s.__node_ = this->first();
1976        }
1977        else
1978        {
1979            __s.__do_ = __state::__reject;
1980            __s.__node_ = nullptr;
1981        }
1982    }
1983    else
1984    {
1985__not_equal:
1986        __s.__do_ = __state::__reject;
1987        __s.__node_ = nullptr;
1988    }
1989}
1990
1991// __word_boundary
1992
1993template <class _CharT, class _Traits>
1994class __word_boundary
1995    : public __owns_one_state<_CharT>
1996{
1997    typedef __owns_one_state<_CharT> base;
1998
1999    _Traits __traits_;
2000    bool __invert_;
2001public:
2002    typedef _VSTD::__state<_CharT> __state;
2003
2004    _LIBCPP_INLINE_VISIBILITY
2005    explicit __word_boundary(const _Traits& __traits, bool __invert,
2006                             __node<_CharT>* __s)
2007        : base(__s), __traits_(__traits), __invert_(__invert) {}
2008
2009    _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
2010};
2011
2012template <class _CharT, class _Traits>
2013void
2014__word_boundary<_CharT, _Traits>::__exec(__state& __s) const
2015{
2016    bool __is_word_b = false;
2017    if (__s.__first_ != __s.__last_)
2018    {
2019        if (__s.__current_ == __s.__last_)
2020        {
2021            if (!(__s.__flags_ & regex_constants::match_not_eow))
2022            {
2023                _CharT __c = __s.__current_[-1];
2024                __is_word_b = __c == '_' ||
2025                              __traits_.isctype(__c, ctype_base::alnum);
2026            }
2027        }
2028        else if (__s.__current_ == __s.__first_ &&
2029                !(__s.__flags_ & regex_constants::match_prev_avail))
2030        {
2031            if (!(__s.__flags_ & regex_constants::match_not_bow))
2032            {
2033                _CharT __c = *__s.__current_;
2034                __is_word_b = __c == '_' ||
2035                              __traits_.isctype(__c, ctype_base::alnum);
2036            }
2037        }
2038        else
2039        {
2040            _CharT __c1 = __s.__current_[-1];
2041            _CharT __c2 = *__s.__current_;
2042            bool __is_c1_b = __c1 == '_' ||
2043                             __traits_.isctype(__c1, ctype_base::alnum);
2044            bool __is_c2_b = __c2 == '_' ||
2045                             __traits_.isctype(__c2, ctype_base::alnum);
2046            __is_word_b = __is_c1_b != __is_c2_b;
2047        }
2048    }
2049    if (__is_word_b != __invert_)
2050    {
2051        __s.__do_ = __state::__accept_but_not_consume;
2052        __s.__node_ = this->first();
2053    }
2054    else
2055    {
2056        __s.__do_ = __state::__reject;
2057        __s.__node_ = nullptr;
2058    }
2059}
2060
2061// __l_anchor
2062
2063template <class _CharT>
2064_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
2065bool __is_eol(_CharT __c)
2066{
2067    return __c == '\r' || __c == '\n';
2068}
2069
2070template <class _CharT>
2071class __l_anchor_multiline
2072    : public __owns_one_state<_CharT>
2073{
2074    typedef __owns_one_state<_CharT> base;
2075
2076    bool __multiline_;
2077
2078public:
2079    typedef _VSTD::__state<_CharT> __state;
2080
2081    _LIBCPP_INLINE_VISIBILITY
2082    __l_anchor_multiline(bool __multiline, __node<_CharT>* __s)
2083        : base(__s), __multiline_(__multiline) {}
2084
2085    _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
2086};
2087
2088template <class _CharT>
2089void
2090__l_anchor_multiline<_CharT>::__exec(__state& __s) const
2091{
2092    if (__s.__at_first_ && __s.__current_ == __s.__first_ &&
2093        !(__s.__flags_ & regex_constants::match_not_bol))
2094    {
2095        __s.__do_ = __state::__accept_but_not_consume;
2096        __s.__node_ = this->first();
2097    }
2098    else if (__multiline_ &&
2099             !__s.__at_first_ &&
2100             std::__is_eol(*_VSTD::prev(__s.__current_)))
2101    {
2102        __s.__do_ = __state::__accept_but_not_consume;
2103        __s.__node_ = this->first();
2104    }
2105    else
2106    {
2107        __s.__do_ = __state::__reject;
2108        __s.__node_ = nullptr;
2109    }
2110}
2111
2112// __r_anchor
2113
2114template <class _CharT>
2115class __r_anchor_multiline
2116    : public __owns_one_state<_CharT>
2117{
2118    typedef __owns_one_state<_CharT> base;
2119
2120    bool __multiline_;
2121
2122public:
2123    typedef _VSTD::__state<_CharT> __state;
2124
2125    _LIBCPP_INLINE_VISIBILITY
2126    __r_anchor_multiline(bool __multiline, __node<_CharT>* __s)
2127        : base(__s), __multiline_(__multiline) {}
2128
2129    _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
2130};
2131
2132template <class _CharT>
2133void
2134__r_anchor_multiline<_CharT>::__exec(__state& __s) const
2135{
2136    if (__s.__current_ == __s.__last_ &&
2137        !(__s.__flags_ & regex_constants::match_not_eol))
2138    {
2139        __s.__do_ = __state::__accept_but_not_consume;
2140        __s.__node_ = this->first();
2141    }
2142    else if (__multiline_ && std::__is_eol(*__s.__current_))
2143    {
2144        __s.__do_ = __state::__accept_but_not_consume;
2145        __s.__node_ = this->first();
2146    }
2147    else
2148    {
2149        __s.__do_ = __state::__reject;
2150        __s.__node_ = nullptr;
2151    }
2152}
2153
2154// __match_any
2155
2156template <class _CharT>
2157class __match_any
2158    : public __owns_one_state<_CharT>
2159{
2160    typedef __owns_one_state<_CharT> base;
2161
2162public:
2163    typedef _VSTD::__state<_CharT> __state;
2164
2165    _LIBCPP_INLINE_VISIBILITY
2166    __match_any(__node<_CharT>* __s)
2167        : base(__s) {}
2168
2169    _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
2170};
2171
2172template <class _CharT>
2173void
2174__match_any<_CharT>::__exec(__state& __s) const
2175{
2176    if (__s.__current_ != __s.__last_ && *__s.__current_ != 0)
2177    {
2178        __s.__do_ = __state::__accept_and_consume;
2179        ++__s.__current_;
2180        __s.__node_ = this->first();
2181    }
2182    else
2183    {
2184        __s.__do_ = __state::__reject;
2185        __s.__node_ = nullptr;
2186    }
2187}
2188
2189// __match_any_but_newline
2190
2191template <class _CharT>
2192class __match_any_but_newline
2193    : public __owns_one_state<_CharT>
2194{
2195    typedef __owns_one_state<_CharT> base;
2196
2197public:
2198    typedef _VSTD::__state<_CharT> __state;
2199
2200    _LIBCPP_INLINE_VISIBILITY
2201    __match_any_but_newline(__node<_CharT>* __s)
2202        : base(__s) {}
2203
2204    void __exec(__state&) const override;
2205};
2206
2207template <> _LIBCPP_FUNC_VIS void __match_any_but_newline<char>::__exec(__state&) const;
2208#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
2209template <> _LIBCPP_FUNC_VIS void __match_any_but_newline<wchar_t>::__exec(__state&) const;
2210#endif
2211
2212// __match_char
2213
2214template <class _CharT>
2215class __match_char
2216    : public __owns_one_state<_CharT>
2217{
2218    typedef __owns_one_state<_CharT> base;
2219
2220    _CharT __c_;
2221
2222    __match_char(const __match_char&);
2223    __match_char& operator=(const __match_char&);
2224public:
2225    typedef _VSTD::__state<_CharT> __state;
2226
2227    _LIBCPP_INLINE_VISIBILITY
2228    __match_char(_CharT __c, __node<_CharT>* __s)
2229        : base(__s), __c_(__c) {}
2230
2231    _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
2232};
2233
2234template <class _CharT>
2235void
2236__match_char<_CharT>::__exec(__state& __s) const
2237{
2238    if (__s.__current_ != __s.__last_ && *__s.__current_ == __c_)
2239    {
2240        __s.__do_ = __state::__accept_and_consume;
2241        ++__s.__current_;
2242        __s.__node_ = this->first();
2243    }
2244    else
2245    {
2246        __s.__do_ = __state::__reject;
2247        __s.__node_ = nullptr;
2248    }
2249}
2250
2251// __match_char_icase
2252
2253template <class _CharT, class _Traits>
2254class __match_char_icase
2255    : public __owns_one_state<_CharT>
2256{
2257    typedef __owns_one_state<_CharT> base;
2258
2259    _Traits __traits_;
2260    _CharT __c_;
2261
2262    __match_char_icase(const __match_char_icase&);
2263    __match_char_icase& operator=(const __match_char_icase&);
2264public:
2265    typedef _VSTD::__state<_CharT> __state;
2266
2267    _LIBCPP_INLINE_VISIBILITY
2268    __match_char_icase(const _Traits& __traits, _CharT __c, __node<_CharT>* __s)
2269        : base(__s), __traits_(__traits), __c_(__traits.translate_nocase(__c)) {}
2270
2271    _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
2272};
2273
2274template <class _CharT, class _Traits>
2275void
2276__match_char_icase<_CharT, _Traits>::__exec(__state& __s) const
2277{
2278    if (__s.__current_ != __s.__last_ &&
2279        __traits_.translate_nocase(*__s.__current_) == __c_)
2280    {
2281        __s.__do_ = __state::__accept_and_consume;
2282        ++__s.__current_;
2283        __s.__node_ = this->first();
2284    }
2285    else
2286    {
2287        __s.__do_ = __state::__reject;
2288        __s.__node_ = nullptr;
2289    }
2290}
2291
2292// __match_char_collate
2293
2294template <class _CharT, class _Traits>
2295class __match_char_collate
2296    : public __owns_one_state<_CharT>
2297{
2298    typedef __owns_one_state<_CharT> base;
2299
2300    _Traits __traits_;
2301    _CharT __c_;
2302
2303    __match_char_collate(const __match_char_collate&);
2304    __match_char_collate& operator=(const __match_char_collate&);
2305public:
2306    typedef _VSTD::__state<_CharT> __state;
2307
2308    _LIBCPP_INLINE_VISIBILITY
2309    __match_char_collate(const _Traits& __traits, _CharT __c, __node<_CharT>* __s)
2310        : base(__s), __traits_(__traits), __c_(__traits.translate(__c)) {}
2311
2312    _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
2313};
2314
2315template <class _CharT, class _Traits>
2316void
2317__match_char_collate<_CharT, _Traits>::__exec(__state& __s) const
2318{
2319    if (__s.__current_ != __s.__last_ &&
2320        __traits_.translate(*__s.__current_) == __c_)
2321    {
2322        __s.__do_ = __state::__accept_and_consume;
2323        ++__s.__current_;
2324        __s.__node_ = this->first();
2325    }
2326    else
2327    {
2328        __s.__do_ = __state::__reject;
2329        __s.__node_ = nullptr;
2330    }
2331}
2332
2333// __bracket_expression
2334
2335template <class _CharT, class _Traits>
2336class __bracket_expression
2337    : public __owns_one_state<_CharT>
2338{
2339    typedef __owns_one_state<_CharT> base;
2340    typedef typename _Traits::string_type string_type;
2341
2342    _Traits __traits_;
2343    vector<_CharT> __chars_;
2344    vector<_CharT> __neg_chars_;
2345    vector<pair<string_type, string_type> > __ranges_;
2346    vector<pair<_CharT, _CharT> > __digraphs_;
2347    vector<string_type> __equivalences_;
2348    typename regex_traits<_CharT>::char_class_type __mask_;
2349    typename regex_traits<_CharT>::char_class_type __neg_mask_;
2350    bool __negate_;
2351    bool __icase_;
2352    bool __collate_;
2353    bool __might_have_digraph_;
2354
2355    __bracket_expression(const __bracket_expression&);
2356    __bracket_expression& operator=(const __bracket_expression&);
2357public:
2358    typedef _VSTD::__state<_CharT> __state;
2359
2360    _LIBCPP_INLINE_VISIBILITY
2361    __bracket_expression(const _Traits& __traits, __node<_CharT>* __s,
2362                                 bool __negate, bool __icase, bool __collate)
2363        : base(__s), __traits_(__traits), __mask_(), __neg_mask_(),
2364          __negate_(__negate), __icase_(__icase), __collate_(__collate),
2365          __might_have_digraph_(__traits_.getloc().name() != "C") {}
2366
2367    _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __exec(__state&) const;
2368
2369    _LIBCPP_INLINE_VISIBILITY
2370    bool __negated() const {return __negate_;}
2371
2372    _LIBCPP_INLINE_VISIBILITY
2373    void __add_char(_CharT __c)
2374        {
2375            if (__icase_)
2376                __chars_.push_back(__traits_.translate_nocase(__c));
2377            else if (__collate_)
2378                __chars_.push_back(__traits_.translate(__c));
2379            else
2380                __chars_.push_back(__c);
2381        }
2382    _LIBCPP_INLINE_VISIBILITY
2383    void __add_neg_char(_CharT __c)
2384        {
2385            if (__icase_)
2386                __neg_chars_.push_back(__traits_.translate_nocase(__c));
2387            else if (__collate_)
2388                __neg_chars_.push_back(__traits_.translate(__c));
2389            else
2390                __neg_chars_.push_back(__c);
2391        }
2392    _LIBCPP_INLINE_VISIBILITY
2393    void __add_range(string_type __b, string_type __e)
2394        {
2395            if (__collate_)
2396            {
2397                if (__icase_)
2398                {
2399                    for (size_t __i = 0; __i < __b.size(); ++__i)
2400                        __b[__i] = __traits_.translate_nocase(__b[__i]);
2401                    for (size_t __i = 0; __i < __e.size(); ++__i)
2402                        __e[__i] = __traits_.translate_nocase(__e[__i]);
2403                }
2404                else
2405                {
2406                    for (size_t __i = 0; __i < __b.size(); ++__i)
2407                        __b[__i] = __traits_.translate(__b[__i]);
2408                    for (size_t __i = 0; __i < __e.size(); ++__i)
2409                        __e[__i] = __traits_.translate(__e[__i]);
2410                }
2411                __ranges_.push_back(std::make_pair(
2412                                  __traits_.transform(__b.begin(), __b.end()),
2413                                  __traits_.transform(__e.begin(), __e.end())));
2414            }
2415            else
2416            {
2417                if (__b.size() != 1 || __e.size() != 1)
2418                    __throw_regex_error<regex_constants::error_range>();
2419                if (__icase_)
2420                {
2421                    __b[0] = __traits_.translate_nocase(__b[0]);
2422                    __e[0] = __traits_.translate_nocase(__e[0]);
2423                }
2424                __ranges_.push_back(std::make_pair(_VSTD::move(__b), _VSTD::move(__e)));
2425            }
2426        }
2427    _LIBCPP_INLINE_VISIBILITY
2428    void __add_digraph(_CharT __c1, _CharT __c2)
2429        {
2430            if (__icase_)
2431                __digraphs_.push_back(std::make_pair(__traits_.translate_nocase(__c1),
2432                                                     __traits_.translate_nocase(__c2)));
2433            else if (__collate_)
2434                __digraphs_.push_back(std::make_pair(__traits_.translate(__c1),
2435                                                     __traits_.translate(__c2)));
2436            else
2437                __digraphs_.push_back(std::make_pair(__c1, __c2));
2438        }
2439    _LIBCPP_INLINE_VISIBILITY
2440    void __add_equivalence(const string_type& __s)
2441        {__equivalences_.push_back(__s);}
2442    _LIBCPP_INLINE_VISIBILITY
2443    void __add_class(typename regex_traits<_CharT>::char_class_type __mask)
2444        {__mask_ |= __mask;}
2445    _LIBCPP_INLINE_VISIBILITY
2446    void __add_neg_class(typename regex_traits<_CharT>::char_class_type __mask)
2447        {__neg_mask_ |= __mask;}
2448};
2449
2450template <class _CharT, class _Traits>
2451void
2452__bracket_expression<_CharT, _Traits>::__exec(__state& __s) const
2453{
2454    bool __found = false;
2455    unsigned __consumed = 0;
2456    if (__s.__current_ != __s.__last_)
2457    {
2458        ++__consumed;
2459        if (__might_have_digraph_)
2460        {
2461            const _CharT* __next = _VSTD::next(__s.__current_);
2462            if (__next != __s.__last_)
2463            {
2464                pair<_CharT, _CharT> __ch2(*__s.__current_, *__next);
2465                if (__icase_)
2466                {
2467                    __ch2.first = __traits_.translate_nocase(__ch2.first);
2468                    __ch2.second = __traits_.translate_nocase(__ch2.second);
2469                }
2470                else if (__collate_)
2471                {
2472                    __ch2.first = __traits_.translate(__ch2.first);
2473                    __ch2.second = __traits_.translate(__ch2.second);
2474                }
2475                if (!__traits_.lookup_collatename(&__ch2.first, &__ch2.first+2).empty())
2476                {
2477                    // __ch2 is a digraph in this locale
2478                    ++__consumed;
2479                    for (size_t __i = 0; __i < __digraphs_.size(); ++__i)
2480                    {
2481                        if (__ch2 == __digraphs_[__i])
2482                        {
2483                            __found = true;
2484                            goto __exit;
2485                        }
2486                    }
2487                    if (__collate_ && !__ranges_.empty())
2488                    {
2489                        string_type __s2 = __traits_.transform(&__ch2.first,
2490                                                               &__ch2.first + 2);
2491                        for (size_t __i = 0; __i < __ranges_.size(); ++__i)
2492                        {
2493                            if (__ranges_[__i].first <= __s2 &&
2494                                __s2 <= __ranges_[__i].second)
2495                            {
2496                                __found = true;
2497                                goto __exit;
2498                            }
2499                        }
2500                    }
2501                    if (!__equivalences_.empty())
2502                    {
2503                        string_type __s2 = __traits_.transform_primary(&__ch2.first,
2504                                                                       &__ch2.first + 2);
2505                        for (size_t __i = 0; __i < __equivalences_.size(); ++__i)
2506                        {
2507                            if (__s2 == __equivalences_[__i])
2508                            {
2509                                __found = true;
2510                                goto __exit;
2511                            }
2512                        }
2513                    }
2514                    if (__traits_.isctype(__ch2.first, __mask_) &&
2515                        __traits_.isctype(__ch2.second, __mask_))
2516                    {
2517                        __found = true;
2518                        goto __exit;
2519                    }
2520                    if (!__traits_.isctype(__ch2.first, __neg_mask_) &&
2521                        !__traits_.isctype(__ch2.second, __neg_mask_))
2522                    {
2523                        __found = true;
2524                        goto __exit;
2525                    }
2526                    goto __exit;
2527                }
2528            }
2529        }
2530        // test *__s.__current_ as not a digraph
2531        _CharT __ch = *__s.__current_;
2532        if (__icase_)
2533            __ch = __traits_.translate_nocase(__ch);
2534        else if (__collate_)
2535            __ch = __traits_.translate(__ch);
2536        for (size_t __i = 0; __i < __chars_.size(); ++__i)
2537        {
2538            if (__ch == __chars_[__i])
2539            {
2540                __found = true;
2541                goto __exit;
2542            }
2543        }
2544        // When there's at least one of __neg_chars_ and __neg_mask_, the set
2545        // of "__found" chars is
2546        //   union(complement(union(__neg_chars_, __neg_mask_)),
2547        //         other cases...)
2548        //
2549        // It doesn't make sense to check this when there are no __neg_chars_
2550        // and no __neg_mask_.
2551        if (!(__neg_mask_ == 0 && __neg_chars_.empty()))
2552        {
2553            const bool __in_neg_mask = __traits_.isctype(__ch, __neg_mask_);
2554          const bool __in_neg_chars =
2555              _VSTD::find(__neg_chars_.begin(), __neg_chars_.end(), __ch) !=
2556              __neg_chars_.end();
2557          if (!(__in_neg_mask || __in_neg_chars))
2558          {
2559            __found = true;
2560            goto __exit;
2561          }
2562        }
2563        if (!__ranges_.empty())
2564        {
2565            string_type __s2 = __collate_ ?
2566                                   __traits_.transform(&__ch, &__ch + 1) :
2567                                   string_type(1, __ch);
2568            for (size_t __i = 0; __i < __ranges_.size(); ++__i)
2569            {
2570                if (__ranges_[__i].first <= __s2 && __s2 <= __ranges_[__i].second)
2571                {
2572                    __found = true;
2573                    goto __exit;
2574                }
2575            }
2576        }
2577        if (!__equivalences_.empty())
2578        {
2579            string_type __s2 = __traits_.transform_primary(&__ch, &__ch + 1);
2580            for (size_t __i = 0; __i < __equivalences_.size(); ++__i)
2581            {
2582                if (__s2 == __equivalences_[__i])
2583                {
2584                    __found = true;
2585                    goto __exit;
2586                }
2587            }
2588        }
2589        if (__traits_.isctype(__ch, __mask_))
2590        {
2591            __found = true;
2592            goto __exit;
2593        }
2594    }
2595    else
2596        __found = __negate_;  // force reject
2597__exit:
2598    if (__found != __negate_)
2599    {
2600        __s.__do_ = __state::__accept_and_consume;
2601        __s.__current_ += __consumed;
2602        __s.__node_ = this->first();
2603    }
2604    else
2605    {
2606        __s.__do_ = __state::__reject;
2607        __s.__node_ = nullptr;
2608    }
2609}
2610
2611template <class _CharT, class _Traits> class __lookahead;
2612
2613template <class _CharT, class _Traits = regex_traits<_CharT> >
2614    class _LIBCPP_TEMPLATE_VIS basic_regex;
2615
2616typedef basic_regex<char>    regex;
2617#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
2618typedef basic_regex<wchar_t> wregex;
2619#endif
2620
2621template <class _CharT, class _Traits>
2622class
2623    _LIBCPP_TEMPLATE_VIS
2624    _LIBCPP_PREFERRED_NAME(regex)
2625    _LIBCPP_IF_WIDE_CHARACTERS(_LIBCPP_PREFERRED_NAME(wregex))
2626    basic_regex
2627{
2628public:
2629    // types:
2630    typedef _CharT                              value_type;
2631    typedef _Traits                             traits_type;
2632    typedef typename _Traits::string_type       string_type;
2633    typedef regex_constants::syntax_option_type flag_type;
2634    typedef typename _Traits::locale_type       locale_type;
2635
2636private:
2637    _Traits   __traits_;
2638    flag_type __flags_;
2639    unsigned __marked_count_;
2640    unsigned __loop_count_;
2641    int __open_count_;
2642    shared_ptr<__empty_state<_CharT> > __start_;
2643    __owns_one_state<_CharT>* __end_;
2644
2645    typedef _VSTD::__state<_CharT> __state;
2646    typedef _VSTD::__node<_CharT> __node;
2647
2648public:
2649    // constants:
2650    static const regex_constants::syntax_option_type icase = regex_constants::icase;
2651    static const regex_constants::syntax_option_type nosubs = regex_constants::nosubs;
2652    static const regex_constants::syntax_option_type optimize = regex_constants::optimize;
2653    static const regex_constants::syntax_option_type collate = regex_constants::collate;
2654    static const regex_constants::syntax_option_type ECMAScript = regex_constants::ECMAScript;
2655    static const regex_constants::syntax_option_type basic = regex_constants::basic;
2656    static const regex_constants::syntax_option_type extended = regex_constants::extended;
2657    static const regex_constants::syntax_option_type awk = regex_constants::awk;
2658    static const regex_constants::syntax_option_type grep = regex_constants::grep;
2659    static const regex_constants::syntax_option_type egrep = regex_constants::egrep;
2660    static const regex_constants::syntax_option_type multiline = regex_constants::multiline;
2661
2662    // construct/copy/destroy:
2663    _LIBCPP_INLINE_VISIBILITY
2664    basic_regex()
2665        : __flags_(regex_constants::ECMAScript), __marked_count_(0), __loop_count_(0), __open_count_(0),
2666          __end_(nullptr)
2667        {}
2668    _LIBCPP_INLINE_VISIBILITY
2669    explicit basic_regex(const value_type* __p, flag_type __f = regex_constants::ECMAScript)
2670        : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
2671          __end_(nullptr)
2672        {
2673        __init(__p, __p + __traits_.length(__p));
2674        }
2675
2676    _LIBCPP_INLINE_VISIBILITY
2677    basic_regex(const value_type* __p, size_t __len, flag_type __f = regex_constants::ECMAScript)
2678        : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
2679          __end_(nullptr)
2680        {
2681        __init(__p, __p + __len);
2682        }
2683
2684//     basic_regex(const basic_regex&) = default;
2685//     basic_regex(basic_regex&&) = default;
2686    template <class _ST, class _SA>
2687        _LIBCPP_INLINE_VISIBILITY
2688        explicit basic_regex(const basic_string<value_type, _ST, _SA>& __p,
2689                             flag_type __f = regex_constants::ECMAScript)
2690        : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
2691          __end_(nullptr)
2692        {
2693        __init(__p.begin(), __p.end());
2694        }
2695
2696    template <class _ForwardIterator>
2697        _LIBCPP_INLINE_VISIBILITY
2698        basic_regex(_ForwardIterator __first, _ForwardIterator __last,
2699                    flag_type __f = regex_constants::ECMAScript)
2700        : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
2701          __end_(nullptr)
2702        {
2703        __init(__first, __last);
2704        }
2705#ifndef _LIBCPP_CXX03_LANG
2706    _LIBCPP_INLINE_VISIBILITY
2707    basic_regex(initializer_list<value_type> __il,
2708                flag_type __f = regex_constants::ECMAScript)
2709        : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
2710          __end_(nullptr)
2711        {
2712        __init(__il.begin(), __il.end());
2713        }
2714#endif // _LIBCPP_CXX03_LANG
2715
2716//    ~basic_regex() = default;
2717
2718//     basic_regex& operator=(const basic_regex&) = default;
2719//     basic_regex& operator=(basic_regex&&) = default;
2720    _LIBCPP_INLINE_VISIBILITY
2721    basic_regex& operator=(const value_type* __p)
2722        {return assign(__p);}
2723#ifndef _LIBCPP_CXX03_LANG
2724    _LIBCPP_INLINE_VISIBILITY
2725    basic_regex& operator=(initializer_list<value_type> __il)
2726        {return assign(__il);}
2727#endif // _LIBCPP_CXX03_LANG
2728    template <class _ST, class _SA>
2729        _LIBCPP_INLINE_VISIBILITY
2730        basic_regex& operator=(const basic_string<value_type, _ST, _SA>& __p)
2731        {return assign(__p);}
2732
2733    // assign:
2734    _LIBCPP_INLINE_VISIBILITY
2735    basic_regex& assign(const basic_regex& __that)
2736        {return *this = __that;}
2737#ifndef _LIBCPP_CXX03_LANG
2738    _LIBCPP_INLINE_VISIBILITY
2739    basic_regex& assign(basic_regex&& __that) _NOEXCEPT
2740        {return *this = _VSTD::move(__that);}
2741#endif
2742    _LIBCPP_INLINE_VISIBILITY
2743    basic_regex& assign(const value_type* __p, flag_type __f = regex_constants::ECMAScript)
2744        {return assign(__p, __p + __traits_.length(__p), __f);}
2745    _LIBCPP_INLINE_VISIBILITY
2746    basic_regex& assign(const value_type* __p, size_t __len, flag_type __f = regex_constants::ECMAScript)
2747        {return assign(__p, __p + __len, __f);}
2748    template <class _ST, class _SA>
2749        _LIBCPP_INLINE_VISIBILITY
2750        basic_regex& assign(const basic_string<value_type, _ST, _SA>& __s,
2751                            flag_type __f = regex_constants::ECMAScript)
2752            {return assign(__s.begin(), __s.end(), __f);}
2753
2754    template <class _InputIterator>
2755        _LIBCPP_INLINE_VISIBILITY
2756        typename enable_if<__is_exactly_cpp17_input_iterator<_InputIterator>::value, basic_regex&>::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);
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    _LIBCPP_HIDE_FROM_ABI_VIRTUAL 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    _LIBCPP_HIDE_FROM_ABI
5017    void swap(sub_match& __s)
5018#ifndef _LIBCPP_CXX03_LANG
5019    _NOEXCEPT(__is_nothrow_swappable<_BidirectionalIterator>::value)
5020#endif // _LIBCPP_CXX03_LANG
5021    {
5022        this->pair<_BidirectionalIterator, _BidirectionalIterator>::swap(__s);
5023        std::swap(matched, __s.matched);
5024    }
5025};
5026
5027template <class _BiIter>
5028inline _LIBCPP_INLINE_VISIBILITY
5029bool
5030operator==(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
5031{
5032    return __x.compare(__y) == 0;
5033}
5034
5035template <class _BiIter>
5036inline _LIBCPP_INLINE_VISIBILITY
5037bool
5038operator!=(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
5039{
5040    return !(__x == __y);
5041}
5042
5043template <class _BiIter>
5044inline _LIBCPP_INLINE_VISIBILITY
5045bool
5046operator<(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
5047{
5048    return __x.compare(__y) < 0;
5049}
5050
5051template <class _BiIter>
5052inline _LIBCPP_INLINE_VISIBILITY
5053bool
5054operator<=(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
5055{
5056    return !(__y < __x);
5057}
5058
5059template <class _BiIter>
5060inline _LIBCPP_INLINE_VISIBILITY
5061bool
5062operator>=(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
5063{
5064    return !(__x < __y);
5065}
5066
5067template <class _BiIter>
5068inline _LIBCPP_INLINE_VISIBILITY
5069bool
5070operator>(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
5071{
5072    return __y < __x;
5073}
5074
5075template <class _BiIter, class _ST, class _SA>
5076inline _LIBCPP_INLINE_VISIBILITY
5077bool
5078operator==(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
5079           const sub_match<_BiIter>& __y)
5080{
5081    return __y.compare(typename sub_match<_BiIter>::string_type(__x.data(), __x.size())) == 0;
5082}
5083
5084template <class _BiIter, class _ST, class _SA>
5085inline _LIBCPP_INLINE_VISIBILITY
5086bool
5087operator!=(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
5088           const sub_match<_BiIter>& __y)
5089{
5090    return !(__x == __y);
5091}
5092
5093template <class _BiIter, class _ST, class _SA>
5094inline _LIBCPP_INLINE_VISIBILITY
5095bool
5096operator<(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
5097          const sub_match<_BiIter>& __y)
5098{
5099    return __y.compare(typename sub_match<_BiIter>::string_type(__x.data(), __x.size())) > 0;
5100}
5101
5102template <class _BiIter, class _ST, class _SA>
5103inline _LIBCPP_INLINE_VISIBILITY
5104bool
5105operator>(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
5106          const sub_match<_BiIter>& __y)
5107{
5108    return __y < __x;
5109}
5110
5111template <class _BiIter, class _ST, class _SA>
5112inline _LIBCPP_INLINE_VISIBILITY
5113bool operator>=(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
5114                const sub_match<_BiIter>& __y)
5115{
5116    return !(__x < __y);
5117}
5118
5119template <class _BiIter, class _ST, class _SA>
5120inline _LIBCPP_INLINE_VISIBILITY
5121bool
5122operator<=(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
5123           const sub_match<_BiIter>& __y)
5124{
5125    return !(__y < __x);
5126}
5127
5128template <class _BiIter, class _ST, class _SA>
5129inline _LIBCPP_INLINE_VISIBILITY
5130bool
5131operator==(const sub_match<_BiIter>& __x,
5132           const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
5133{
5134    return __x.compare(typename sub_match<_BiIter>::string_type(__y.data(), __y.size())) == 0;
5135}
5136
5137template <class _BiIter, class _ST, class _SA>
5138inline _LIBCPP_INLINE_VISIBILITY
5139bool
5140operator!=(const sub_match<_BiIter>& __x,
5141           const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
5142{
5143    return !(__x == __y);
5144}
5145
5146template <class _BiIter, class _ST, class _SA>
5147inline _LIBCPP_INLINE_VISIBILITY
5148bool
5149operator<(const sub_match<_BiIter>& __x,
5150          const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
5151{
5152    return __x.compare(typename sub_match<_BiIter>::string_type(__y.data(), __y.size())) < 0;
5153}
5154
5155template <class _BiIter, class _ST, class _SA>
5156inline _LIBCPP_INLINE_VISIBILITY
5157bool operator>(const sub_match<_BiIter>& __x,
5158               const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
5159{
5160    return __y < __x;
5161}
5162
5163template <class _BiIter, class _ST, class _SA>
5164inline _LIBCPP_INLINE_VISIBILITY
5165bool
5166operator>=(const sub_match<_BiIter>& __x,
5167           const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
5168{
5169    return !(__x < __y);
5170}
5171
5172template <class _BiIter, class _ST, class _SA>
5173inline _LIBCPP_INLINE_VISIBILITY
5174bool
5175operator<=(const sub_match<_BiIter>& __x,
5176           const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
5177{
5178    return !(__y < __x);
5179}
5180
5181template <class _BiIter>
5182inline _LIBCPP_INLINE_VISIBILITY
5183bool
5184operator==(typename iterator_traits<_BiIter>::value_type const* __x,
5185           const sub_match<_BiIter>& __y)
5186{
5187    return __y.compare(__x) == 0;
5188}
5189
5190template <class _BiIter>
5191inline _LIBCPP_INLINE_VISIBILITY
5192bool
5193operator!=(typename iterator_traits<_BiIter>::value_type const* __x,
5194           const sub_match<_BiIter>& __y)
5195{
5196    return !(__x == __y);
5197}
5198
5199template <class _BiIter>
5200inline _LIBCPP_INLINE_VISIBILITY
5201bool
5202operator<(typename iterator_traits<_BiIter>::value_type const* __x,
5203          const sub_match<_BiIter>& __y)
5204{
5205    return __y.compare(__x) > 0;
5206}
5207
5208template <class _BiIter>
5209inline _LIBCPP_INLINE_VISIBILITY
5210bool
5211operator>(typename iterator_traits<_BiIter>::value_type const* __x,
5212          const sub_match<_BiIter>& __y)
5213{
5214    return __y < __x;
5215}
5216
5217template <class _BiIter>
5218inline _LIBCPP_INLINE_VISIBILITY
5219bool
5220operator>=(typename iterator_traits<_BiIter>::value_type const* __x,
5221           const sub_match<_BiIter>& __y)
5222{
5223    return !(__x < __y);
5224}
5225
5226template <class _BiIter>
5227inline _LIBCPP_INLINE_VISIBILITY
5228bool
5229operator<=(typename iterator_traits<_BiIter>::value_type const* __x,
5230           const sub_match<_BiIter>& __y)
5231{
5232    return !(__y < __x);
5233}
5234
5235template <class _BiIter>
5236inline _LIBCPP_INLINE_VISIBILITY
5237bool
5238operator==(const sub_match<_BiIter>& __x,
5239           typename iterator_traits<_BiIter>::value_type const* __y)
5240{
5241    return __x.compare(__y) == 0;
5242}
5243
5244template <class _BiIter>
5245inline _LIBCPP_INLINE_VISIBILITY
5246bool
5247operator!=(const sub_match<_BiIter>& __x,
5248           typename iterator_traits<_BiIter>::value_type const* __y)
5249{
5250    return !(__x == __y);
5251}
5252
5253template <class _BiIter>
5254inline _LIBCPP_INLINE_VISIBILITY
5255bool
5256operator<(const sub_match<_BiIter>& __x,
5257          typename iterator_traits<_BiIter>::value_type const* __y)
5258{
5259    return __x.compare(__y) < 0;
5260}
5261
5262template <class _BiIter>
5263inline _LIBCPP_INLINE_VISIBILITY
5264bool
5265operator>(const sub_match<_BiIter>& __x,
5266          typename iterator_traits<_BiIter>::value_type const* __y)
5267{
5268    return __y < __x;
5269}
5270
5271template <class _BiIter>
5272inline _LIBCPP_INLINE_VISIBILITY
5273bool
5274operator>=(const sub_match<_BiIter>& __x,
5275           typename iterator_traits<_BiIter>::value_type const* __y)
5276{
5277    return !(__x < __y);
5278}
5279
5280template <class _BiIter>
5281inline _LIBCPP_INLINE_VISIBILITY
5282bool
5283operator<=(const sub_match<_BiIter>& __x,
5284           typename iterator_traits<_BiIter>::value_type const* __y)
5285{
5286    return !(__y < __x);
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    typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
5296    return __y.compare(string_type(1, __x)) == 0;
5297}
5298
5299template <class _BiIter>
5300inline _LIBCPP_INLINE_VISIBILITY
5301bool
5302operator!=(typename iterator_traits<_BiIter>::value_type const& __x,
5303           const sub_match<_BiIter>& __y)
5304{
5305    return !(__x == __y);
5306}
5307
5308template <class _BiIter>
5309inline _LIBCPP_INLINE_VISIBILITY
5310bool
5311operator<(typename iterator_traits<_BiIter>::value_type const& __x,
5312          const sub_match<_BiIter>& __y)
5313{
5314    typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
5315    return __y.compare(string_type(1, __x)) > 0;
5316}
5317
5318template <class _BiIter>
5319inline _LIBCPP_INLINE_VISIBILITY
5320bool
5321operator>(typename iterator_traits<_BiIter>::value_type const& __x,
5322          const sub_match<_BiIter>& __y)
5323{
5324    return __y < __x;
5325}
5326
5327template <class _BiIter>
5328inline _LIBCPP_INLINE_VISIBILITY
5329bool
5330operator>=(typename iterator_traits<_BiIter>::value_type const& __x,
5331           const sub_match<_BiIter>& __y)
5332{
5333    return !(__x < __y);
5334}
5335
5336template <class _BiIter>
5337inline _LIBCPP_INLINE_VISIBILITY
5338bool
5339operator<=(typename iterator_traits<_BiIter>::value_type const& __x,
5340           const sub_match<_BiIter>& __y)
5341{
5342    return !(__y < __x);
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    typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
5352    return __x.compare(string_type(1, __y)) == 0;
5353}
5354
5355template <class _BiIter>
5356inline _LIBCPP_INLINE_VISIBILITY
5357bool
5358operator!=(const sub_match<_BiIter>& __x,
5359           typename iterator_traits<_BiIter>::value_type const& __y)
5360{
5361    return !(__x == __y);
5362}
5363
5364template <class _BiIter>
5365inline _LIBCPP_INLINE_VISIBILITY
5366bool
5367operator<(const sub_match<_BiIter>& __x,
5368          typename iterator_traits<_BiIter>::value_type const& __y)
5369{
5370    typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
5371    return __x.compare(string_type(1, __y)) < 0;
5372}
5373
5374template <class _BiIter>
5375inline _LIBCPP_INLINE_VISIBILITY
5376bool
5377operator>(const sub_match<_BiIter>& __x,
5378          typename iterator_traits<_BiIter>::value_type const& __y)
5379{
5380    return __y < __x;
5381}
5382
5383template <class _BiIter>
5384inline _LIBCPP_INLINE_VISIBILITY
5385bool
5386operator>=(const sub_match<_BiIter>& __x,
5387           typename iterator_traits<_BiIter>::value_type const& __y)
5388{
5389    return !(__x < __y);
5390}
5391
5392template <class _BiIter>
5393inline _LIBCPP_INLINE_VISIBILITY
5394bool
5395operator<=(const sub_match<_BiIter>& __x,
5396           typename iterator_traits<_BiIter>::value_type const& __y)
5397{
5398    return !(__y < __x);
5399}
5400
5401template <class _CharT, class _ST, class _BiIter>
5402inline _LIBCPP_INLINE_VISIBILITY
5403basic_ostream<_CharT, _ST>&
5404operator<<(basic_ostream<_CharT, _ST>& __os, const sub_match<_BiIter>& __m)
5405{
5406    return __os << __m.str();
5407}
5408
5409typedef match_results<const char*>             cmatch;
5410typedef match_results<string::const_iterator>  smatch;
5411#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
5412typedef match_results<const wchar_t*>          wcmatch;
5413typedef match_results<wstring::const_iterator> wsmatch;
5414#endif
5415
5416template <class _BidirectionalIterator, class _Allocator>
5417class
5418    _LIBCPP_TEMPLATE_VIS
5419    _LIBCPP_PREFERRED_NAME(cmatch)
5420    _LIBCPP_IF_WIDE_CHARACTERS(_LIBCPP_PREFERRED_NAME(wcmatch))
5421    _LIBCPP_PREFERRED_NAME(smatch)
5422    _LIBCPP_IF_WIDE_CHARACTERS(_LIBCPP_PREFERRED_NAME(wsmatch))
5423    match_results
5424{
5425public:
5426    typedef _Allocator                                        allocator_type;
5427    typedef sub_match<_BidirectionalIterator>                 value_type;
5428private:
5429    typedef vector<value_type, allocator_type>                __container_type;
5430
5431    __container_type  __matches_;
5432    value_type __unmatched_;
5433    value_type __prefix_;
5434    value_type __suffix_;
5435    bool       __ready_;
5436public:
5437    _BidirectionalIterator __position_start_;
5438    typedef const value_type&                                 const_reference;
5439    typedef value_type&                                       reference;
5440    typedef typename __container_type::const_iterator         const_iterator;
5441    typedef const_iterator                                    iterator;
5442    typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
5443    typedef typename allocator_traits<allocator_type>::size_type size_type;
5444    typedef typename iterator_traits<_BidirectionalIterator>::value_type char_type;
5445    typedef basic_string<char_type>                           string_type;
5446
5447    // construct/copy/destroy:
5448#ifndef _LIBCPP_CXX03_LANG
5449    match_results() : match_results(allocator_type()) {}
5450    explicit match_results(const allocator_type& __a);
5451#else
5452    explicit match_results(const allocator_type& __a = allocator_type());
5453#endif
5454
5455//    match_results(const match_results&) = default;
5456//    match_results& operator=(const match_results&) = default;
5457//    match_results(match_results&& __m) = default;
5458//    match_results& operator=(match_results&& __m) = default;
5459//    ~match_results() = default;
5460
5461    _LIBCPP_INLINE_VISIBILITY
5462    bool ready() const {return __ready_;}
5463
5464    // size:
5465    _LIBCPP_INLINE_VISIBILITY
5466    size_type size() const _NOEXCEPT {return __matches_.size();}
5467    _LIBCPP_INLINE_VISIBILITY
5468    size_type max_size() const _NOEXCEPT {return __matches_.max_size();}
5469    _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
5470    bool empty() const _NOEXCEPT {return size() == 0;}
5471
5472    // element access:
5473    _LIBCPP_INLINE_VISIBILITY
5474    difference_type length(size_type __sub = 0) const
5475        {
5476        _LIBCPP_ASSERT(ready(), "match_results::length() called when not ready");
5477        return (*this)[__sub].length();
5478        }
5479    _LIBCPP_INLINE_VISIBILITY
5480    difference_type position(size_type __sub = 0) const
5481        {
5482        _LIBCPP_ASSERT(ready(), "match_results::position() called when not ready");
5483        return _VSTD::distance(__position_start_, (*this)[__sub].first);
5484        }
5485    _LIBCPP_INLINE_VISIBILITY
5486    string_type str(size_type __sub = 0) const
5487        {
5488        _LIBCPP_ASSERT(ready(), "match_results::str() called when not ready");
5489        return (*this)[__sub].str();
5490        }
5491    _LIBCPP_INLINE_VISIBILITY
5492    const_reference operator[](size_type __n) const
5493        {
5494        _LIBCPP_ASSERT(ready(), "match_results::operator[]() called when not ready");
5495        return __n < __matches_.size() ? __matches_[__n] : __unmatched_;
5496        }
5497
5498    _LIBCPP_INLINE_VISIBILITY
5499    const_reference prefix() const
5500        {
5501        _LIBCPP_ASSERT(ready(), "match_results::prefix() called when not ready");
5502        return __prefix_;
5503        }
5504    _LIBCPP_INLINE_VISIBILITY
5505    const_reference suffix() const
5506        {
5507        _LIBCPP_ASSERT(ready(), "match_results::suffix() called when not ready");
5508        return __suffix_;
5509        }
5510
5511    _LIBCPP_INLINE_VISIBILITY
5512    const_iterator begin() const {return empty() ? __matches_.end() : __matches_.begin();}
5513    _LIBCPP_INLINE_VISIBILITY
5514    const_iterator end() const {return __matches_.end();}
5515    _LIBCPP_INLINE_VISIBILITY
5516    const_iterator cbegin() const {return empty() ? __matches_.end() : __matches_.begin();}
5517    _LIBCPP_INLINE_VISIBILITY
5518    const_iterator cend() const {return __matches_.end();}
5519
5520    // format:
5521    template <class _OutputIter>
5522        _OutputIter
5523        format(_OutputIter __output_iter, const char_type* __fmt_first,
5524               const char_type* __fmt_last,
5525               regex_constants::match_flag_type __flags = regex_constants::format_default) const;
5526    template <class _OutputIter, class _ST, class _SA>
5527        _LIBCPP_INLINE_VISIBILITY
5528        _OutputIter
5529        format(_OutputIter __output_iter, const basic_string<char_type, _ST, _SA>& __fmt,
5530               regex_constants::match_flag_type __flags = regex_constants::format_default) const
5531            {return format(__output_iter, __fmt.data(), __fmt.data() + __fmt.size(), __flags);}
5532    template <class _ST, class _SA>
5533        _LIBCPP_INLINE_VISIBILITY
5534        basic_string<char_type, _ST, _SA>
5535        format(const basic_string<char_type, _ST, _SA>& __fmt,
5536               regex_constants::match_flag_type __flags = regex_constants::format_default) const
5537        {
5538            basic_string<char_type, _ST, _SA> __r;
5539            format(std::back_inserter(__r), __fmt.data(), __fmt.data() + __fmt.size(),
5540                   __flags);
5541            return __r;
5542        }
5543    _LIBCPP_INLINE_VISIBILITY
5544    string_type
5545        format(const char_type* __fmt,
5546               regex_constants::match_flag_type __flags = regex_constants::format_default) const
5547        {
5548            string_type __r;
5549            format(std::back_inserter(__r), __fmt,
5550                   __fmt + char_traits<char_type>::length(__fmt), __flags);
5551            return __r;
5552        }
5553
5554    // allocator:
5555    _LIBCPP_INLINE_VISIBILITY
5556    allocator_type get_allocator() const {return __matches_.get_allocator();}
5557
5558    // swap:
5559    void swap(match_results& __m);
5560
5561    template <class _Bp, class _Ap>
5562        _LIBCPP_INLINE_VISIBILITY
5563        void __assign(_BidirectionalIterator __f, _BidirectionalIterator __l,
5564                      const match_results<_Bp, _Ap>& __m, bool __no_update_pos)
5565    {
5566        _Bp __mf = __m.prefix().first;
5567        __matches_.resize(__m.size());
5568        for (size_type __i = 0; __i < __matches_.size(); ++__i)
5569        {
5570            __matches_[__i].first = _VSTD::next(__f, _VSTD::distance(__mf, __m[__i].first));
5571            __matches_[__i].second = _VSTD::next(__f, _VSTD::distance(__mf, __m[__i].second));
5572            __matches_[__i].matched = __m[__i].matched;
5573        }
5574        __unmatched_.first   = __l;
5575        __unmatched_.second  = __l;
5576        __unmatched_.matched = false;
5577        __prefix_.first = _VSTD::next(__f, _VSTD::distance(__mf, __m.prefix().first));
5578        __prefix_.second = _VSTD::next(__f, _VSTD::distance(__mf, __m.prefix().second));
5579        __prefix_.matched = __m.prefix().matched;
5580        __suffix_.first = _VSTD::next(__f, _VSTD::distance(__mf, __m.suffix().first));
5581        __suffix_.second = _VSTD::next(__f, _VSTD::distance(__mf, __m.suffix().second));
5582        __suffix_.matched = __m.suffix().matched;
5583        if (!__no_update_pos)
5584            __position_start_ = __prefix_.first;
5585        __ready_ = __m.ready();
5586    }
5587
5588private:
5589    void __init(unsigned __s,
5590                _BidirectionalIterator __f, _BidirectionalIterator __l,
5591                bool __no_update_pos = false);
5592
5593    template <class, class> friend class basic_regex;
5594
5595    template <class _Bp, class _Ap, class _Cp, class _Tp>
5596    friend
5597    bool
5598    regex_match(_Bp, _Bp, match_results<_Bp, _Ap>&, const basic_regex<_Cp, _Tp>&,
5599                regex_constants::match_flag_type);
5600
5601    template <class _Bp, class _Ap>
5602    friend
5603    bool
5604    operator==(const match_results<_Bp, _Ap>&, const match_results<_Bp, _Ap>&);
5605
5606    template <class, class> friend class __lookahead;
5607};
5608
5609template <class _BidirectionalIterator, class _Allocator>
5610match_results<_BidirectionalIterator, _Allocator>::match_results(
5611        const allocator_type& __a)
5612    : __matches_(__a),
5613      __unmatched_(),
5614      __prefix_(),
5615      __suffix_(),
5616      __ready_(false),
5617      __position_start_()
5618{
5619}
5620
5621template <class _BidirectionalIterator, class _Allocator>
5622void
5623match_results<_BidirectionalIterator, _Allocator>::__init(unsigned __s,
5624                         _BidirectionalIterator __f, _BidirectionalIterator __l,
5625                         bool __no_update_pos)
5626{
5627    __unmatched_.first   = __l;
5628    __unmatched_.second  = __l;
5629    __unmatched_.matched = false;
5630    __matches_.assign(__s, __unmatched_);
5631    __prefix_.first      = __f;
5632    __prefix_.second     = __f;
5633    __prefix_.matched    = false;
5634    __suffix_ = __unmatched_;
5635    if (!__no_update_pos)
5636        __position_start_ = __prefix_.first;
5637    __ready_ = true;
5638}
5639
5640template <class _BidirectionalIterator, class _Allocator>
5641template <class _OutputIter>
5642_OutputIter
5643match_results<_BidirectionalIterator, _Allocator>::format(_OutputIter __output_iter,
5644        const char_type* __fmt_first, const char_type* __fmt_last,
5645        regex_constants::match_flag_type __flags) const
5646{
5647    _LIBCPP_ASSERT(ready(), "match_results::format() called when not ready");
5648    if (__flags & regex_constants::format_sed)
5649    {
5650        for (; __fmt_first != __fmt_last; ++__fmt_first)
5651        {
5652            if (*__fmt_first == '&')
5653                __output_iter = _VSTD::copy(__matches_[0].first, __matches_[0].second,
5654                                   __output_iter);
5655            else if (*__fmt_first == '\\' && __fmt_first + 1 != __fmt_last)
5656            {
5657                ++__fmt_first;
5658                if ('0' <= *__fmt_first && *__fmt_first <= '9')
5659                {
5660                    size_t __i = *__fmt_first - '0';
5661                    __output_iter = _VSTD::copy((*this)[__i].first,
5662                                        (*this)[__i].second, __output_iter);
5663                }
5664                else
5665                {
5666                    *__output_iter = *__fmt_first;
5667                    ++__output_iter;
5668                }
5669            }
5670            else
5671            {
5672                *__output_iter = *__fmt_first;
5673                ++__output_iter;
5674            }
5675        }
5676    }
5677    else
5678    {
5679        for (; __fmt_first != __fmt_last; ++__fmt_first)
5680        {
5681            if (*__fmt_first == '$' && __fmt_first + 1 != __fmt_last)
5682            {
5683                switch (__fmt_first[1])
5684                {
5685                case '$':
5686                    *__output_iter = *++__fmt_first;
5687                    ++__output_iter;
5688                    break;
5689                case '&':
5690                    ++__fmt_first;
5691                    __output_iter = _VSTD::copy(__matches_[0].first, __matches_[0].second,
5692                                       __output_iter);
5693                    break;
5694                case '`':
5695                    ++__fmt_first;
5696                    __output_iter = _VSTD::copy(__prefix_.first, __prefix_.second, __output_iter);
5697                    break;
5698                case '\'':
5699                    ++__fmt_first;
5700                    __output_iter = _VSTD::copy(__suffix_.first, __suffix_.second, __output_iter);
5701                    break;
5702                default:
5703                    if ('0' <= __fmt_first[1] && __fmt_first[1] <= '9')
5704                    {
5705                        ++__fmt_first;
5706                        size_t __idx = *__fmt_first - '0';
5707                        if (__fmt_first + 1 != __fmt_last &&
5708                            '0' <= __fmt_first[1] && __fmt_first[1] <= '9')
5709                        {
5710                            ++__fmt_first;
5711                            if (__idx >= numeric_limits<size_t>::max() / 10)
5712                                __throw_regex_error<regex_constants::error_escape>();
5713                            __idx = 10 * __idx + *__fmt_first - '0';
5714                        }
5715                        __output_iter = _VSTD::copy((*this)[__idx].first,
5716                                            (*this)[__idx].second, __output_iter);
5717                    }
5718                    else
5719                    {
5720                        *__output_iter = *__fmt_first;
5721                        ++__output_iter;
5722                    }
5723                    break;
5724                }
5725            }
5726            else
5727            {
5728                *__output_iter = *__fmt_first;
5729                ++__output_iter;
5730            }
5731        }
5732    }
5733    return __output_iter;
5734}
5735
5736template <class _BidirectionalIterator, class _Allocator>
5737void
5738match_results<_BidirectionalIterator, _Allocator>::swap(match_results& __m)
5739{
5740    using _VSTD::swap;
5741    swap(__matches_, __m.__matches_);
5742    swap(__unmatched_, __m.__unmatched_);
5743    swap(__prefix_, __m.__prefix_);
5744    swap(__suffix_, __m.__suffix_);
5745    swap(__position_start_, __m.__position_start_);
5746    swap(__ready_, __m.__ready_);
5747}
5748
5749template <class _BidirectionalIterator, class _Allocator>
5750_LIBCPP_HIDE_FROM_ABI bool
5751operator==(const match_results<_BidirectionalIterator, _Allocator>& __x,
5752           const match_results<_BidirectionalIterator, _Allocator>& __y)
5753{
5754    if (__x.__ready_ != __y.__ready_)
5755        return false;
5756    if (!__x.__ready_)
5757        return true;
5758    return __x.__matches_ == __y.__matches_ &&
5759           __x.__prefix_ == __y.__prefix_ &&
5760           __x.__suffix_ == __y.__suffix_;
5761}
5762
5763template <class _BidirectionalIterator, class _Allocator>
5764inline _LIBCPP_INLINE_VISIBILITY
5765bool
5766operator!=(const match_results<_BidirectionalIterator, _Allocator>& __x,
5767           const match_results<_BidirectionalIterator, _Allocator>& __y)
5768{
5769    return !(__x == __y);
5770}
5771
5772template <class _BidirectionalIterator, class _Allocator>
5773inline _LIBCPP_INLINE_VISIBILITY
5774void
5775swap(match_results<_BidirectionalIterator, _Allocator>& __x,
5776     match_results<_BidirectionalIterator, _Allocator>& __y)
5777{
5778    __x.swap(__y);
5779}
5780
5781// regex_search
5782
5783template <class _CharT, class _Traits>
5784template <class _Allocator>
5785bool
5786basic_regex<_CharT, _Traits>::__match_at_start_ecma(
5787        const _CharT* __first, const _CharT* __last,
5788        match_results<const _CharT*, _Allocator>& __m,
5789        regex_constants::match_flag_type __flags, bool __at_first) const
5790{
5791    vector<__state> __states;
5792    __node* __st = __start_.get();
5793    if (__st)
5794    {
5795        sub_match<const _CharT*> __unmatched;
5796        __unmatched.first   = __last;
5797        __unmatched.second  = __last;
5798        __unmatched.matched = false;
5799
5800        __states.push_back(__state());
5801        __states.back().__do_ = 0;
5802        __states.back().__first_ = __first;
5803        __states.back().__current_ = __first;
5804        __states.back().__last_ = __last;
5805        __states.back().__sub_matches_.resize(mark_count(), __unmatched);
5806        __states.back().__loop_data_.resize(__loop_count());
5807        __states.back().__node_ = __st;
5808        __states.back().__flags_ = __flags;
5809        __states.back().__at_first_ = __at_first;
5810        int __counter = 0;
5811        int __length = __last - __first;
5812        do
5813        {
5814            ++__counter;
5815            if (__counter % _LIBCPP_REGEX_COMPLEXITY_FACTOR == 0 &&
5816                __counter / _LIBCPP_REGEX_COMPLEXITY_FACTOR >= __length)
5817              __throw_regex_error<regex_constants::error_complexity>();
5818            __state& __s = __states.back();
5819            if (__s.__node_)
5820                __s.__node_->__exec(__s);
5821            switch (__s.__do_)
5822            {
5823            case __state::__end_state:
5824                if ((__flags & regex_constants::match_not_null) &&
5825                    __s.__current_ == __first)
5826                {
5827                  __states.pop_back();
5828                  break;
5829                }
5830                if ((__flags & regex_constants::__full_match) &&
5831                    __s.__current_ != __last)
5832                {
5833                  __states.pop_back();
5834                  break;
5835                }
5836                __m.__matches_[0].first = __first;
5837                __m.__matches_[0].second = _VSTD::next(__first, __s.__current_ - __first);
5838                __m.__matches_[0].matched = true;
5839                for (unsigned __i = 0; __i < __s.__sub_matches_.size(); ++__i)
5840                    __m.__matches_[__i+1] = __s.__sub_matches_[__i];
5841                return true;
5842            case __state::__accept_and_consume:
5843            case __state::__repeat:
5844            case __state::__accept_but_not_consume:
5845                break;
5846            case __state::__split:
5847                {
5848                __state __snext = __s;
5849                __s.__node_->__exec_split(true, __s);
5850                __snext.__node_->__exec_split(false, __snext);
5851                __states.push_back(_VSTD::move(__snext));
5852                }
5853                break;
5854            case __state::__reject:
5855                __states.pop_back();
5856                break;
5857            default:
5858                __throw_regex_error<regex_constants::__re_err_unknown>();
5859                break;
5860
5861            }
5862        } while (!__states.empty());
5863    }
5864    return false;
5865}
5866
5867template <class _CharT, class _Traits>
5868template <class _Allocator>
5869bool
5870basic_regex<_CharT, _Traits>::__match_at_start_posix_nosubs(
5871        const _CharT* __first, const _CharT* __last,
5872        match_results<const _CharT*, _Allocator>& __m,
5873        regex_constants::match_flag_type __flags, bool __at_first) const
5874{
5875    deque<__state> __states;
5876    ptrdiff_t __highest_j = 0;
5877    ptrdiff_t __np = _VSTD::distance(__first, __last);
5878    __node* __st = __start_.get();
5879    if (__st)
5880    {
5881        __states.push_back(__state());
5882        __states.back().__do_ = 0;
5883        __states.back().__first_ = __first;
5884        __states.back().__current_ = __first;
5885        __states.back().__last_ = __last;
5886        __states.back().__loop_data_.resize(__loop_count());
5887        __states.back().__node_ = __st;
5888        __states.back().__flags_ = __flags;
5889        __states.back().__at_first_ = __at_first;
5890        bool __matched = false;
5891        int __counter = 0;
5892        int __length = __last - __first;
5893        do
5894        {
5895            ++__counter;
5896            if (__counter % _LIBCPP_REGEX_COMPLEXITY_FACTOR == 0 &&
5897                __counter / _LIBCPP_REGEX_COMPLEXITY_FACTOR >= __length)
5898              __throw_regex_error<regex_constants::error_complexity>();
5899            __state& __s = __states.back();
5900            if (__s.__node_)
5901                __s.__node_->__exec(__s);
5902            switch (__s.__do_)
5903            {
5904            case __state::__end_state:
5905                if ((__flags & regex_constants::match_not_null) &&
5906                    __s.__current_ == __first)
5907                {
5908                  __states.pop_back();
5909                  break;
5910                }
5911                if ((__flags & regex_constants::__full_match) &&
5912                    __s.__current_ != __last)
5913                {
5914                  __states.pop_back();
5915                  break;
5916                }
5917                if (!__matched || __highest_j < __s.__current_ - __s.__first_)
5918                    __highest_j = __s.__current_ - __s.__first_;
5919                __matched = true;
5920                if (__highest_j == __np)
5921                    __states.clear();
5922                else
5923                    __states.pop_back();
5924                break;
5925            case __state::__consume_input:
5926                break;
5927            case __state::__accept_and_consume:
5928                __states.push_front(_VSTD::move(__s));
5929                __states.pop_back();
5930                break;
5931            case __state::__repeat:
5932            case __state::__accept_but_not_consume:
5933                break;
5934            case __state::__split:
5935                {
5936                __state __snext = __s;
5937                __s.__node_->__exec_split(true, __s);
5938                __snext.__node_->__exec_split(false, __snext);
5939                __states.push_back(_VSTD::move(__snext));
5940                }
5941                break;
5942            case __state::__reject:
5943                __states.pop_back();
5944                break;
5945            default:
5946                __throw_regex_error<regex_constants::__re_err_unknown>();
5947                break;
5948            }
5949        } while (!__states.empty());
5950        if (__matched)
5951        {
5952            __m.__matches_[0].first = __first;
5953            __m.__matches_[0].second = _VSTD::next(__first, __highest_j);
5954            __m.__matches_[0].matched = true;
5955            return true;
5956        }
5957    }
5958    return false;
5959}
5960
5961template <class _CharT, class _Traits>
5962template <class _Allocator>
5963bool
5964basic_regex<_CharT, _Traits>::__match_at_start_posix_subs(
5965        const _CharT* __first, const _CharT* __last,
5966        match_results<const _CharT*, _Allocator>& __m,
5967        regex_constants::match_flag_type __flags, bool __at_first) const
5968{
5969    vector<__state> __states;
5970    __state __best_state;
5971    ptrdiff_t __highest_j = 0;
5972    ptrdiff_t __np = _VSTD::distance(__first, __last);
5973    __node* __st = __start_.get();
5974    if (__st)
5975    {
5976        sub_match<const _CharT*> __unmatched;
5977        __unmatched.first   = __last;
5978        __unmatched.second  = __last;
5979        __unmatched.matched = false;
5980
5981        __states.push_back(__state());
5982        __states.back().__do_ = 0;
5983        __states.back().__first_ = __first;
5984        __states.back().__current_ = __first;
5985        __states.back().__last_ = __last;
5986        __states.back().__sub_matches_.resize(mark_count(), __unmatched);
5987        __states.back().__loop_data_.resize(__loop_count());
5988        __states.back().__node_ = __st;
5989        __states.back().__flags_ = __flags;
5990        __states.back().__at_first_ = __at_first;
5991        bool __matched = false;
5992        int __counter = 0;
5993        int __length = __last - __first;
5994        do
5995        {
5996            ++__counter;
5997            if (__counter % _LIBCPP_REGEX_COMPLEXITY_FACTOR == 0 &&
5998                __counter / _LIBCPP_REGEX_COMPLEXITY_FACTOR >= __length)
5999              __throw_regex_error<regex_constants::error_complexity>();
6000            __state& __s = __states.back();
6001            if (__s.__node_)
6002                __s.__node_->__exec(__s);
6003            switch (__s.__do_)
6004            {
6005            case __state::__end_state:
6006                if ((__flags & regex_constants::match_not_null) &&
6007                    __s.__current_ == __first)
6008                {
6009                  __states.pop_back();
6010                  break;
6011                }
6012                if ((__flags & regex_constants::__full_match) &&
6013                    __s.__current_ != __last)
6014                {
6015                  __states.pop_back();
6016                  break;
6017                }
6018                if (!__matched || __highest_j < __s.__current_ - __s.__first_)
6019                {
6020                    __highest_j = __s.__current_ - __s.__first_;
6021                    __best_state = __s;
6022                }
6023                __matched = true;
6024                if (__highest_j == __np)
6025                    __states.clear();
6026                else
6027                    __states.pop_back();
6028                break;
6029            case __state::__accept_and_consume:
6030            case __state::__repeat:
6031            case __state::__accept_but_not_consume:
6032                break;
6033            case __state::__split:
6034                {
6035                __state __snext = __s;
6036                __s.__node_->__exec_split(true, __s);
6037                __snext.__node_->__exec_split(false, __snext);
6038                __states.push_back(_VSTD::move(__snext));
6039                }
6040                break;
6041            case __state::__reject:
6042                __states.pop_back();
6043                break;
6044            default:
6045                __throw_regex_error<regex_constants::__re_err_unknown>();
6046                break;
6047            }
6048        } while (!__states.empty());
6049        if (__matched)
6050        {
6051            __m.__matches_[0].first = __first;
6052            __m.__matches_[0].second = _VSTD::next(__first, __highest_j);
6053            __m.__matches_[0].matched = true;
6054            for (unsigned __i = 0; __i < __best_state.__sub_matches_.size(); ++__i)
6055                __m.__matches_[__i+1] = __best_state.__sub_matches_[__i];
6056            return true;
6057        }
6058    }
6059    return false;
6060}
6061
6062template <class _CharT, class _Traits>
6063template <class _Allocator>
6064bool
6065basic_regex<_CharT, _Traits>::__match_at_start(
6066        const _CharT* __first, const _CharT* __last,
6067        match_results<const _CharT*, _Allocator>& __m,
6068        regex_constants::match_flag_type __flags, bool __at_first) const
6069{
6070    if (__get_grammar(__flags_) == ECMAScript)
6071        return __match_at_start_ecma(__first, __last, __m, __flags, __at_first);
6072    if (mark_count() == 0)
6073        return __match_at_start_posix_nosubs(__first, __last, __m, __flags, __at_first);
6074    return __match_at_start_posix_subs(__first, __last, __m, __flags, __at_first);
6075}
6076
6077template <class _CharT, class _Traits>
6078template <class _Allocator>
6079bool
6080basic_regex<_CharT, _Traits>::__search(
6081        const _CharT* __first, const _CharT* __last,
6082        match_results<const _CharT*, _Allocator>& __m,
6083        regex_constants::match_flag_type __flags) const
6084{
6085    if (__flags & regex_constants::match_prev_avail)
6086        __flags &= ~(regex_constants::match_not_bol | regex_constants::match_not_bow);
6087
6088    __m.__init(1 + mark_count(), __first, __last,
6089                                    __flags & regex_constants::__no_update_pos);
6090    if (__match_at_start(__first, __last, __m, __flags,
6091                                    !(__flags & regex_constants::__no_update_pos)))
6092    {
6093        __m.__prefix_.second = __m[0].first;
6094        __m.__prefix_.matched = __m.__prefix_.first != __m.__prefix_.second;
6095        __m.__suffix_.first = __m[0].second;
6096        __m.__suffix_.matched = __m.__suffix_.first != __m.__suffix_.second;
6097        return true;
6098    }
6099    if (__first != __last && !(__flags & regex_constants::match_continuous))
6100    {
6101        __flags |= regex_constants::match_prev_avail;
6102        for (++__first; __first != __last; ++__first)
6103        {
6104            __m.__matches_.assign(__m.size(), __m.__unmatched_);
6105            if (__match_at_start(__first, __last, __m, __flags, false))
6106            {
6107                __m.__prefix_.second = __m[0].first;
6108                __m.__prefix_.matched = __m.__prefix_.first != __m.__prefix_.second;
6109                __m.__suffix_.first = __m[0].second;
6110                __m.__suffix_.matched = __m.__suffix_.first != __m.__suffix_.second;
6111                return true;
6112            }
6113            __m.__matches_.assign(__m.size(), __m.__unmatched_);
6114        }
6115    }
6116    __m.__matches_.clear();
6117    return false;
6118}
6119
6120template <class _BidirectionalIterator, class _Allocator, class _CharT, class _Traits>
6121inline _LIBCPP_INLINE_VISIBILITY
6122bool
6123regex_search(_BidirectionalIterator __first, _BidirectionalIterator __last,
6124             match_results<_BidirectionalIterator, _Allocator>& __m,
6125             const basic_regex<_CharT, _Traits>& __e,
6126             regex_constants::match_flag_type __flags = regex_constants::match_default)
6127{
6128    int __offset = (__flags & regex_constants::match_prev_avail) ? 1 : 0;
6129    basic_string<_CharT> __s(_VSTD::prev(__first, __offset), __last);
6130    match_results<const _CharT*> __mc;
6131    bool __r = __e.__search(__s.data() + __offset, __s.data() + __s.size(), __mc, __flags);
6132    __m.__assign(__first, __last, __mc, __flags & regex_constants::__no_update_pos);
6133    return __r;
6134}
6135
6136template <class _Iter, class _Allocator, class _CharT, class _Traits>
6137inline _LIBCPP_INLINE_VISIBILITY
6138bool
6139regex_search(__wrap_iter<_Iter> __first,
6140             __wrap_iter<_Iter> __last,
6141             match_results<__wrap_iter<_Iter>, _Allocator>& __m,
6142             const basic_regex<_CharT, _Traits>& __e,
6143             regex_constants::match_flag_type __flags = regex_constants::match_default)
6144{
6145    match_results<const _CharT*> __mc;
6146    bool __r = __e.__search(__first.base(), __last.base(), __mc, __flags);
6147    __m.__assign(__first, __last, __mc, __flags & regex_constants::__no_update_pos);
6148    return __r;
6149}
6150
6151template <class _Allocator, class _CharT, class _Traits>
6152inline _LIBCPP_INLINE_VISIBILITY
6153bool
6154regex_search(const _CharT* __first, const _CharT* __last,
6155             match_results<const _CharT*, _Allocator>& __m,
6156             const basic_regex<_CharT, _Traits>& __e,
6157             regex_constants::match_flag_type __flags = regex_constants::match_default)
6158{
6159    return __e.__search(__first, __last, __m, __flags);
6160}
6161
6162template <class _BidirectionalIterator, class _CharT, class _Traits>
6163inline _LIBCPP_INLINE_VISIBILITY
6164bool
6165regex_search(_BidirectionalIterator __first, _BidirectionalIterator __last,
6166             const basic_regex<_CharT, _Traits>& __e,
6167             regex_constants::match_flag_type __flags = regex_constants::match_default)
6168{
6169    basic_string<_CharT> __s(__first, __last);
6170    match_results<const _CharT*> __mc;
6171    return __e.__search(__s.data(), __s.data() + __s.size(), __mc, __flags);
6172}
6173
6174template <class _CharT, class _Traits>
6175inline _LIBCPP_INLINE_VISIBILITY
6176bool
6177regex_search(const _CharT* __first, const _CharT* __last,
6178             const basic_regex<_CharT, _Traits>& __e,
6179             regex_constants::match_flag_type __flags = regex_constants::match_default)
6180{
6181    match_results<const _CharT*> __mc;
6182    return __e.__search(__first, __last, __mc, __flags);
6183}
6184
6185template <class _CharT, class _Allocator, class _Traits>
6186inline _LIBCPP_INLINE_VISIBILITY
6187bool
6188regex_search(const _CharT* __str, match_results<const _CharT*, _Allocator>& __m,
6189             const basic_regex<_CharT, _Traits>& __e,
6190             regex_constants::match_flag_type __flags = regex_constants::match_default)
6191{
6192    return __e.__search(__str, __str + _Traits::length(__str), __m, __flags);
6193}
6194
6195template <class _CharT, class _Traits>
6196inline _LIBCPP_INLINE_VISIBILITY
6197bool
6198regex_search(const _CharT* __str, const basic_regex<_CharT, _Traits>& __e,
6199             regex_constants::match_flag_type __flags = regex_constants::match_default)
6200{
6201    match_results<const _CharT*> __m;
6202    return _VSTD::regex_search(__str, __m, __e, __flags);
6203}
6204
6205template <class _ST, class _SA, class _CharT, class _Traits>
6206inline _LIBCPP_INLINE_VISIBILITY
6207bool
6208regex_search(const basic_string<_CharT, _ST, _SA>& __s,
6209             const basic_regex<_CharT, _Traits>& __e,
6210             regex_constants::match_flag_type __flags = regex_constants::match_default)
6211{
6212    match_results<const _CharT*> __mc;
6213    return __e.__search(__s.data(), __s.data() + __s.size(), __mc, __flags);
6214}
6215
6216template <class _ST, class _SA, class _Allocator, class _CharT, class _Traits>
6217inline _LIBCPP_INLINE_VISIBILITY
6218bool
6219regex_search(const basic_string<_CharT, _ST, _SA>& __s,
6220             match_results<typename basic_string<_CharT, _ST, _SA>::const_iterator, _Allocator>& __m,
6221             const basic_regex<_CharT, _Traits>& __e,
6222             regex_constants::match_flag_type __flags = regex_constants::match_default)
6223{
6224    match_results<const _CharT*> __mc;
6225    bool __r = __e.__search(__s.data(), __s.data() + __s.size(), __mc, __flags);
6226    __m.__assign(__s.begin(), __s.end(), __mc, __flags & regex_constants::__no_update_pos);
6227    return __r;
6228}
6229
6230#if _LIBCPP_STD_VER >= 14
6231template <class _ST, class _SA, class _Ap, class _Cp, class _Tp>
6232bool
6233regex_search(const basic_string<_Cp, _ST, _SA>&& __s,
6234             match_results<typename basic_string<_Cp, _ST, _SA>::const_iterator, _Ap>&,
6235             const basic_regex<_Cp, _Tp>& __e,
6236             regex_constants::match_flag_type __flags = regex_constants::match_default) = delete;
6237#endif
6238
6239// regex_match
6240
6241template <class _BidirectionalIterator, class _Allocator, class _CharT, class _Traits>
6242_LIBCPP_HIDE_FROM_ABI bool
6243regex_match(_BidirectionalIterator __first, _BidirectionalIterator __last,
6244            match_results<_BidirectionalIterator, _Allocator>& __m,
6245            const basic_regex<_CharT, _Traits>& __e,
6246            regex_constants::match_flag_type __flags = regex_constants::match_default)
6247{
6248    bool __r = _VSTD::regex_search(
6249        __first, __last, __m, __e,
6250        __flags | regex_constants::match_continuous |
6251        regex_constants::__full_match);
6252    if (__r)
6253    {
6254        __r = !__m.suffix().matched;
6255        if (!__r)
6256            __m.__matches_.clear();
6257    }
6258    return __r;
6259}
6260
6261template <class _BidirectionalIterator, class _CharT, class _Traits>
6262inline _LIBCPP_INLINE_VISIBILITY
6263bool
6264regex_match(_BidirectionalIterator __first, _BidirectionalIterator __last,
6265            const basic_regex<_CharT, _Traits>& __e,
6266            regex_constants::match_flag_type __flags = regex_constants::match_default)
6267{
6268    match_results<_BidirectionalIterator> __m;
6269    return _VSTD::regex_match(__first, __last, __m, __e, __flags);
6270}
6271
6272template <class _CharT, class _Allocator, class _Traits>
6273inline _LIBCPP_INLINE_VISIBILITY
6274bool
6275regex_match(const _CharT* __str, match_results<const _CharT*, _Allocator>& __m,
6276            const basic_regex<_CharT, _Traits>& __e,
6277            regex_constants::match_flag_type __flags = regex_constants::match_default)
6278{
6279    return _VSTD::regex_match(__str, __str + _Traits::length(__str), __m, __e, __flags);
6280}
6281
6282template <class _ST, class _SA, class _Allocator, class _CharT, class _Traits>
6283inline _LIBCPP_INLINE_VISIBILITY
6284bool
6285regex_match(const basic_string<_CharT, _ST, _SA>& __s,
6286            match_results<typename basic_string<_CharT, _ST, _SA>::const_iterator, _Allocator>& __m,
6287            const basic_regex<_CharT, _Traits>& __e,
6288            regex_constants::match_flag_type __flags = regex_constants::match_default)
6289{
6290    return _VSTD::regex_match(__s.begin(), __s.end(), __m, __e, __flags);
6291}
6292
6293#if _LIBCPP_STD_VER >= 14
6294template <class _ST, class _SA, class _Allocator, class _CharT, class _Traits>
6295inline _LIBCPP_INLINE_VISIBILITY
6296bool
6297regex_match(const basic_string<_CharT, _ST, _SA>&& __s,
6298            match_results<typename basic_string<_CharT, _ST, _SA>::const_iterator, _Allocator>& __m,
6299            const basic_regex<_CharT, _Traits>& __e,
6300            regex_constants::match_flag_type __flags = regex_constants::match_default) = delete;
6301#endif
6302
6303template <class _CharT, class _Traits>
6304inline _LIBCPP_INLINE_VISIBILITY
6305bool
6306regex_match(const _CharT* __str, const basic_regex<_CharT, _Traits>& __e,
6307            regex_constants::match_flag_type __flags = regex_constants::match_default)
6308{
6309    return _VSTD::regex_match(__str, __str + _Traits::length(__str), __e, __flags);
6310}
6311
6312template <class _ST, class _SA, class _CharT, class _Traits>
6313inline _LIBCPP_INLINE_VISIBILITY
6314bool
6315regex_match(const basic_string<_CharT, _ST, _SA>& __s,
6316            const basic_regex<_CharT, _Traits>& __e,
6317            regex_constants::match_flag_type __flags = regex_constants::match_default)
6318{
6319    return _VSTD::regex_match(__s.begin(), __s.end(), __e, __flags);
6320}
6321
6322// regex_iterator
6323
6324template <class _BidirectionalIterator,
6325          class _CharT = typename iterator_traits<_BidirectionalIterator>::value_type,
6326          class _Traits = regex_traits<_CharT> >
6327    class _LIBCPP_TEMPLATE_VIS regex_iterator;
6328
6329typedef regex_iterator<const char*>             cregex_iterator;
6330typedef regex_iterator<string::const_iterator>  sregex_iterator;
6331#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
6332typedef regex_iterator<const wchar_t*>          wcregex_iterator;
6333typedef regex_iterator<wstring::const_iterator> wsregex_iterator;
6334#endif
6335
6336template <class _BidirectionalIterator, class _CharT, class _Traits>
6337class
6338    _LIBCPP_TEMPLATE_VIS
6339    _LIBCPP_PREFERRED_NAME(cregex_iterator)
6340    _LIBCPP_IF_WIDE_CHARACTERS(_LIBCPP_PREFERRED_NAME(wcregex_iterator))
6341    _LIBCPP_PREFERRED_NAME(sregex_iterator)
6342    _LIBCPP_IF_WIDE_CHARACTERS(_LIBCPP_PREFERRED_NAME(wsregex_iterator))
6343    regex_iterator
6344{
6345public:
6346    typedef basic_regex<_CharT, _Traits>          regex_type;
6347    typedef match_results<_BidirectionalIterator> value_type;
6348    typedef ptrdiff_t                             difference_type;
6349    typedef const value_type*                     pointer;
6350    typedef const value_type&                     reference;
6351    typedef forward_iterator_tag                  iterator_category;
6352
6353private:
6354    _BidirectionalIterator           __begin_;
6355    _BidirectionalIterator           __end_;
6356    const regex_type*                __pregex_;
6357    regex_constants::match_flag_type __flags_;
6358    value_type                       __match_;
6359
6360public:
6361    regex_iterator();
6362    regex_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6363                   const regex_type& __re,
6364                   regex_constants::match_flag_type __m
6365                                              = regex_constants::match_default);
6366#if _LIBCPP_STD_VER >= 14
6367    regex_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6368                   const regex_type&& __re,
6369                   regex_constants::match_flag_type __m
6370                                     = regex_constants::match_default) = delete;
6371#endif
6372
6373    bool operator==(const regex_iterator& __x) const;
6374    _LIBCPP_INLINE_VISIBILITY
6375    bool operator!=(const regex_iterator& __x) const {return !(*this == __x);}
6376
6377    _LIBCPP_INLINE_VISIBILITY
6378    reference operator*() const {return  __match_;}
6379    _LIBCPP_INLINE_VISIBILITY
6380    pointer operator->() const  {return _VSTD::addressof(__match_);}
6381
6382    regex_iterator& operator++();
6383    _LIBCPP_INLINE_VISIBILITY
6384    regex_iterator operator++(int)
6385    {
6386        regex_iterator __t(*this);
6387        ++(*this);
6388        return __t;
6389    }
6390};
6391
6392template <class _BidirectionalIterator, class _CharT, class _Traits>
6393regex_iterator<_BidirectionalIterator, _CharT, _Traits>::regex_iterator()
6394    : __begin_(), __end_(), __pregex_(nullptr), __flags_(), __match_()
6395{
6396}
6397
6398template <class _BidirectionalIterator, class _CharT, class _Traits>
6399regex_iterator<_BidirectionalIterator, _CharT, _Traits>::
6400    regex_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6401                   const regex_type& __re, regex_constants::match_flag_type __m)
6402    : __begin_(__a),
6403      __end_(__b),
6404      __pregex_(_VSTD::addressof(__re)),
6405      __flags_(__m)
6406{
6407    _VSTD::regex_search(__begin_, __end_, __match_, *__pregex_, __flags_);
6408}
6409
6410template <class _BidirectionalIterator, class _CharT, class _Traits>
6411bool
6412regex_iterator<_BidirectionalIterator, _CharT, _Traits>::
6413    operator==(const regex_iterator& __x) const
6414{
6415    if (__match_.empty() && __x.__match_.empty())
6416        return true;
6417    if (__match_.empty() || __x.__match_.empty())
6418        return false;
6419    return __begin_ == __x.__begin_       &&
6420           __end_ == __x.__end_           &&
6421           __pregex_ == __x.__pregex_     &&
6422           __flags_ == __x.__flags_       &&
6423           __match_[0] == __x.__match_[0];
6424}
6425
6426template <class _BidirectionalIterator, class _CharT, class _Traits>
6427regex_iterator<_BidirectionalIterator, _CharT, _Traits>&
6428regex_iterator<_BidirectionalIterator, _CharT, _Traits>::operator++()
6429{
6430    __flags_ |= regex_constants::__no_update_pos;
6431    _BidirectionalIterator __start = __match_[0].second;
6432    if (__match_[0].first == __match_[0].second)
6433    {
6434        if (__start == __end_)
6435        {
6436            __match_ = value_type();
6437            return *this;
6438        }
6439        else if (_VSTD::regex_search(__start, __end_, __match_, *__pregex_,
6440                                    __flags_ | regex_constants::match_not_null |
6441                                    regex_constants::match_continuous))
6442            return *this;
6443        else
6444            ++__start;
6445    }
6446    __flags_ |= regex_constants::match_prev_avail;
6447    if (!_VSTD::regex_search(__start, __end_, __match_, *__pregex_, __flags_))
6448        __match_ = value_type();
6449    return *this;
6450}
6451
6452// regex_token_iterator
6453
6454template <class _BidirectionalIterator,
6455          class _CharT = typename iterator_traits<_BidirectionalIterator>::value_type,
6456          class _Traits = regex_traits<_CharT> >
6457    class _LIBCPP_TEMPLATE_VIS regex_token_iterator;
6458
6459typedef regex_token_iterator<const char*>             cregex_token_iterator;
6460typedef regex_token_iterator<string::const_iterator>  sregex_token_iterator;
6461#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
6462typedef regex_token_iterator<const wchar_t*>          wcregex_token_iterator;
6463typedef regex_token_iterator<wstring::const_iterator> wsregex_token_iterator;
6464#endif
6465
6466template <class _BidirectionalIterator, class _CharT, class _Traits>
6467class
6468    _LIBCPP_TEMPLATE_VIS
6469    _LIBCPP_PREFERRED_NAME(cregex_token_iterator)
6470    _LIBCPP_IF_WIDE_CHARACTERS(_LIBCPP_PREFERRED_NAME(wcregex_token_iterator))
6471    _LIBCPP_PREFERRED_NAME(sregex_token_iterator)
6472    _LIBCPP_IF_WIDE_CHARACTERS(_LIBCPP_PREFERRED_NAME(wsregex_token_iterator))
6473    regex_token_iterator
6474{
6475public:
6476    typedef basic_regex<_CharT, _Traits>      regex_type;
6477    typedef sub_match<_BidirectionalIterator> value_type;
6478    typedef ptrdiff_t                         difference_type;
6479    typedef const value_type*                 pointer;
6480    typedef const value_type&                 reference;
6481    typedef forward_iterator_tag              iterator_category;
6482
6483private:
6484    typedef regex_iterator<_BidirectionalIterator, _CharT, _Traits> _Position;
6485
6486    _Position         __position_;
6487    const value_type* __result_;
6488    value_type        __suffix_;
6489    ptrdiff_t         __n_;
6490    vector<int>       __subs_;
6491
6492public:
6493    regex_token_iterator();
6494    regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6495                         const regex_type& __re, int __submatch = 0,
6496                         regex_constants::match_flag_type __m =
6497                                                regex_constants::match_default);
6498#if _LIBCPP_STD_VER >= 14
6499    regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6500                         const regex_type&& __re, int __submatch = 0,
6501                         regex_constants::match_flag_type __m =
6502                                       regex_constants::match_default) = delete;
6503#endif
6504
6505    regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6506                         const regex_type& __re, const vector<int>& __submatches,
6507                         regex_constants::match_flag_type __m =
6508                                                regex_constants::match_default);
6509#if _LIBCPP_STD_VER >= 14
6510    regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6511                         const regex_type&& __re, const vector<int>& __submatches,
6512                         regex_constants::match_flag_type __m =
6513                                     regex_constants::match_default) = delete;
6514#endif
6515
6516#ifndef _LIBCPP_CXX03_LANG
6517    regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6518                         const regex_type& __re,
6519                         initializer_list<int> __submatches,
6520                         regex_constants::match_flag_type __m =
6521                                                regex_constants::match_default);
6522
6523#if _LIBCPP_STD_VER >= 14
6524    regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6525                         const regex_type&& __re,
6526                         initializer_list<int> __submatches,
6527                         regex_constants::match_flag_type __m =
6528                                       regex_constants::match_default) = delete;
6529#endif
6530#endif // _LIBCPP_CXX03_LANG
6531    template <size_t _Np>
6532        regex_token_iterator(_BidirectionalIterator __a,
6533                             _BidirectionalIterator __b,
6534                             const regex_type& __re,
6535                             const int (&__submatches)[_Np],
6536                             regex_constants::match_flag_type __m =
6537                                                regex_constants::match_default);
6538#if _LIBCPP_STD_VER >= 14
6539    template <size_t _Np>
6540        regex_token_iterator(_BidirectionalIterator __a,
6541                             _BidirectionalIterator __b,
6542                             const regex_type&& __re,
6543                             const int (&__submatches)[_Np],
6544                             regex_constants::match_flag_type __m =
6545                                      regex_constants::match_default) = delete;
6546#endif
6547
6548    regex_token_iterator(const regex_token_iterator&);
6549    regex_token_iterator& operator=(const regex_token_iterator&);
6550
6551    bool operator==(const regex_token_iterator& __x) const;
6552    _LIBCPP_INLINE_VISIBILITY
6553    bool operator!=(const regex_token_iterator& __x) const {return !(*this == __x);}
6554
6555    _LIBCPP_INLINE_VISIBILITY
6556    const value_type& operator*() const {return *__result_;}
6557    _LIBCPP_INLINE_VISIBILITY
6558    const value_type* operator->() const {return __result_;}
6559
6560    regex_token_iterator& operator++();
6561    _LIBCPP_INLINE_VISIBILITY
6562    regex_token_iterator operator++(int)
6563    {
6564        regex_token_iterator __t(*this);
6565        ++(*this);
6566        return __t;
6567    }
6568
6569private:
6570    void __init(_BidirectionalIterator __a, _BidirectionalIterator __b);
6571    void __establish_result () {
6572        if (__subs_[__n_] == -1)
6573            __result_ = &__position_->prefix();
6574        else
6575            __result_ = &(*__position_)[__subs_[__n_]];
6576        }
6577};
6578
6579template <class _BidirectionalIterator, class _CharT, class _Traits>
6580regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6581    regex_token_iterator()
6582    : __result_(nullptr),
6583      __suffix_(),
6584      __n_(0)
6585{
6586}
6587
6588template <class _BidirectionalIterator, class _CharT, class _Traits>
6589void
6590regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6591    __init(_BidirectionalIterator __a, _BidirectionalIterator __b)
6592{
6593    if (__position_ != _Position())
6594        __establish_result ();
6595    else if (__subs_[__n_] == -1)
6596    {
6597        __suffix_.matched = true;
6598        __suffix_.first = __a;
6599        __suffix_.second = __b;
6600        __result_ = &__suffix_;
6601    }
6602    else
6603        __result_ = nullptr;
6604}
6605
6606template <class _BidirectionalIterator, class _CharT, class _Traits>
6607regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6608    regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6609                         const regex_type& __re, int __submatch,
6610                         regex_constants::match_flag_type __m)
6611    : __position_(__a, __b, __re, __m),
6612      __n_(0),
6613      __subs_(1, __submatch)
6614{
6615    __init(__a, __b);
6616}
6617
6618template <class _BidirectionalIterator, class _CharT, class _Traits>
6619regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6620    regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6621                         const regex_type& __re, const vector<int>& __submatches,
6622                         regex_constants::match_flag_type __m)
6623    : __position_(__a, __b, __re, __m),
6624      __n_(0),
6625      __subs_(__submatches)
6626{
6627    __init(__a, __b);
6628}
6629
6630#ifndef _LIBCPP_CXX03_LANG
6631
6632template <class _BidirectionalIterator, class _CharT, class _Traits>
6633regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6634    regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6635                         const regex_type& __re,
6636                         initializer_list<int> __submatches,
6637                         regex_constants::match_flag_type __m)
6638    : __position_(__a, __b, __re, __m),
6639      __n_(0),
6640      __subs_(__submatches)
6641{
6642    __init(__a, __b);
6643}
6644
6645#endif // _LIBCPP_CXX03_LANG
6646
6647template <class _BidirectionalIterator, class _CharT, class _Traits>
6648template <size_t _Np>
6649regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6650    regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6651                             const regex_type& __re,
6652                             const int (&__submatches)[_Np],
6653                             regex_constants::match_flag_type __m)
6654    : __position_(__a, __b, __re, __m),
6655      __n_(0),
6656      __subs_(begin(__submatches), end(__submatches))
6657{
6658    __init(__a, __b);
6659}
6660
6661template <class _BidirectionalIterator, class _CharT, class _Traits>
6662regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6663    regex_token_iterator(const regex_token_iterator& __x)
6664    : __position_(__x.__position_),
6665      __result_(__x.__result_),
6666      __suffix_(__x.__suffix_),
6667      __n_(__x.__n_),
6668      __subs_(__x.__subs_)
6669{
6670    if (__x.__result_ == &__x.__suffix_)
6671        __result_ = &__suffix_;
6672    else if ( __result_ != nullptr )
6673        __establish_result ();
6674}
6675
6676template <class _BidirectionalIterator, class _CharT, class _Traits>
6677regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>&
6678regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6679    operator=(const regex_token_iterator& __x)
6680{
6681    if (this != &__x)
6682    {
6683        __position_ = __x.__position_;
6684        if (__x.__result_ == &__x.__suffix_)
6685            __result_ = &__suffix_;
6686        else
6687            __result_ = __x.__result_;
6688        __suffix_ = __x.__suffix_;
6689        __n_ = __x.__n_;
6690        __subs_ = __x.__subs_;
6691
6692        if ( __result_ != nullptr && __result_ != &__suffix_ )
6693            __establish_result();
6694    }
6695    return *this;
6696}
6697
6698template <class _BidirectionalIterator, class _CharT, class _Traits>
6699bool
6700regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6701    operator==(const regex_token_iterator& __x) const
6702{
6703    if (__result_ == nullptr && __x.__result_ == nullptr)
6704        return true;
6705    if (__result_ == &__suffix_ && __x.__result_ == &__x.__suffix_ &&
6706            __suffix_ == __x.__suffix_)
6707        return true;
6708    if (__result_ == nullptr || __x.__result_ == nullptr)
6709        return false;
6710    if (__result_ == &__suffix_ || __x.__result_ == &__x.__suffix_)
6711        return false;
6712    return __position_ == __x.__position_ && __n_ == __x.__n_ &&
6713           __subs_ == __x.__subs_;
6714}
6715
6716template <class _BidirectionalIterator, class _CharT, class _Traits>
6717regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>&
6718regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::operator++()
6719{
6720    _Position __prev = __position_;
6721    if (__result_ == &__suffix_)
6722        __result_ = nullptr;
6723    else if (static_cast<size_t>(__n_ + 1) < __subs_.size())
6724    {
6725        ++__n_;
6726        __establish_result();
6727    }
6728    else
6729    {
6730        __n_ = 0;
6731        ++__position_;
6732        if (__position_ != _Position())
6733            __establish_result();
6734        else
6735        {
6736            if (_VSTD::find(__subs_.begin(), __subs_.end(), -1) != __subs_.end()
6737                && __prev->suffix().length() != 0)
6738            {
6739                __suffix_.matched = true;
6740                __suffix_.first = __prev->suffix().first;
6741                __suffix_.second = __prev->suffix().second;
6742                __result_ = &__suffix_;
6743            }
6744            else
6745                __result_ = nullptr;
6746        }
6747    }
6748    return *this;
6749}
6750
6751// regex_replace
6752
6753template <class _OutputIterator, class _BidirectionalIterator,
6754          class _Traits, class _CharT>
6755_LIBCPP_HIDE_FROM_ABI _OutputIterator
6756regex_replace(_OutputIterator __output_iter,
6757              _BidirectionalIterator __first, _BidirectionalIterator __last,
6758              const basic_regex<_CharT, _Traits>& __e, const _CharT* __fmt,
6759              regex_constants::match_flag_type __flags = regex_constants::match_default)
6760{
6761    typedef regex_iterator<_BidirectionalIterator, _CharT, _Traits> _Iter;
6762    _Iter __i(__first, __last, __e, __flags);
6763    _Iter __eof;
6764    if (__i == __eof)
6765    {
6766        if (!(__flags & regex_constants::format_no_copy))
6767            __output_iter = _VSTD::copy(__first, __last, __output_iter);
6768    }
6769    else
6770    {
6771        sub_match<_BidirectionalIterator> __lm;
6772        for (size_t __len = char_traits<_CharT>::length(__fmt); __i != __eof; ++__i)
6773        {
6774            if (!(__flags & regex_constants::format_no_copy))
6775                __output_iter = _VSTD::copy(__i->prefix().first, __i->prefix().second, __output_iter);
6776            __output_iter = __i->format(__output_iter, __fmt, __fmt + __len, __flags);
6777            __lm = __i->suffix();
6778            if (__flags & regex_constants::format_first_only)
6779                break;
6780        }
6781        if (!(__flags & regex_constants::format_no_copy))
6782            __output_iter = _VSTD::copy(__lm.first, __lm.second, __output_iter);
6783    }
6784    return __output_iter;
6785}
6786
6787template <class _OutputIterator, class _BidirectionalIterator,
6788          class _Traits, class _CharT, class _ST, class _SA>
6789inline _LIBCPP_INLINE_VISIBILITY
6790_OutputIterator
6791regex_replace(_OutputIterator __output_iter,
6792              _BidirectionalIterator __first, _BidirectionalIterator __last,
6793              const basic_regex<_CharT, _Traits>& __e,
6794              const basic_string<_CharT, _ST, _SA>& __fmt,
6795              regex_constants::match_flag_type __flags = regex_constants::match_default)
6796{
6797    return _VSTD::regex_replace(__output_iter, __first, __last, __e, __fmt.c_str(), __flags);
6798}
6799
6800template <class _Traits, class _CharT, class _ST, class _SA, class _FST,
6801          class _FSA>
6802inline _LIBCPP_INLINE_VISIBILITY
6803basic_string<_CharT, _ST, _SA>
6804regex_replace(const basic_string<_CharT, _ST, _SA>& __s,
6805              const basic_regex<_CharT, _Traits>& __e,
6806              const basic_string<_CharT, _FST, _FSA>& __fmt,
6807              regex_constants::match_flag_type __flags = regex_constants::match_default)
6808{
6809    basic_string<_CharT, _ST, _SA> __r;
6810    _VSTD::regex_replace(std::back_inserter(__r), __s.begin(), __s.end(), __e,
6811                        __fmt.c_str(), __flags);
6812    return __r;
6813}
6814
6815template <class _Traits, class _CharT, class _ST, class _SA>
6816inline _LIBCPP_INLINE_VISIBILITY
6817basic_string<_CharT, _ST, _SA>
6818regex_replace(const basic_string<_CharT, _ST, _SA>& __s,
6819              const basic_regex<_CharT, _Traits>& __e, const _CharT* __fmt,
6820              regex_constants::match_flag_type __flags = regex_constants::match_default)
6821{
6822    basic_string<_CharT, _ST, _SA> __r;
6823    _VSTD::regex_replace(std::back_inserter(__r), __s.begin(), __s.end(), __e,
6824                        __fmt, __flags);
6825    return __r;
6826}
6827
6828template <class _Traits, class _CharT, class _ST, class _SA>
6829inline _LIBCPP_INLINE_VISIBILITY
6830basic_string<_CharT>
6831regex_replace(const _CharT* __s,
6832              const basic_regex<_CharT, _Traits>& __e,
6833              const basic_string<_CharT, _ST, _SA>& __fmt,
6834              regex_constants::match_flag_type __flags = regex_constants::match_default)
6835{
6836    basic_string<_CharT> __r;
6837    _VSTD::regex_replace(std::back_inserter(__r), __s,
6838                        __s + char_traits<_CharT>::length(__s), __e,
6839                        __fmt.c_str(), __flags);
6840    return __r;
6841}
6842
6843template <class _Traits, class _CharT>
6844inline _LIBCPP_INLINE_VISIBILITY
6845basic_string<_CharT>
6846regex_replace(const _CharT* __s,
6847              const basic_regex<_CharT, _Traits>& __e,
6848              const _CharT* __fmt,
6849              regex_constants::match_flag_type __flags = regex_constants::match_default)
6850{
6851    basic_string<_CharT> __r;
6852    _VSTD::regex_replace(std::back_inserter(__r), __s,
6853                        __s + char_traits<_CharT>::length(__s), __e,
6854                        __fmt, __flags);
6855    return __r;
6856}
6857
6858_LIBCPP_END_NAMESPACE_STD
6859
6860#if _LIBCPP_STD_VER >= 17
6861_LIBCPP_BEGIN_NAMESPACE_STD
6862namespace pmr {
6863template <class _BidirT>
6864using match_results = std::match_results<_BidirT, polymorphic_allocator<std::sub_match<_BidirT>>>;
6865
6866using cmatch  = match_results<const char*>;
6867using smatch  = match_results<std::pmr::string::const_iterator>;
6868
6869#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
6870using wcmatch = match_results<const wchar_t*>;
6871using wsmatch = match_results<std::pmr::wstring::const_iterator>;
6872#endif
6873} // namespace pmr
6874_LIBCPP_END_NAMESPACE_STD
6875#endif
6876
6877_LIBCPP_POP_MACROS
6878
6879#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
6880#  include <atomic>
6881#  include <concepts>
6882#  include <cstdlib>
6883#  include <iosfwd>
6884#  include <iterator>
6885#  include <new>
6886#  include <type_traits>
6887#  include <typeinfo>
6888#  include <utility>
6889#endif
6890
6891#endif // _LIBCPP_REGEX
6892