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