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