xref: /llvm-project-15.0.7/libcxx/include/regex (revision 57a2eaf3)
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        if (__val > mark_count())
4665            __throw_regex_error<regex_constants::error_backref>();
4666        __push_back_ref(__val);
4667        return true;
4668    }
4669
4670    return false;
4671}
4672
4673template <class _CharT, class _Traits>
4674void
4675basic_regex<_CharT, _Traits>::__push_loop(size_t __min, size_t __max,
4676        __owns_one_state<_CharT>* __s, size_t __mexp_begin, size_t __mexp_end,
4677        bool __greedy)
4678{
4679    unique_ptr<__empty_state<_CharT> > __e1(new __empty_state<_CharT>(__end_->first()));
4680    __end_->first() = nullptr;
4681    unique_ptr<__loop<_CharT> > __e2(new __loop<_CharT>(__loop_count_,
4682                __s->first(), __e1.get(), __mexp_begin, __mexp_end, __greedy,
4683                __min, __max));
4684    __s->first() = nullptr;
4685    __e1.release();
4686    __end_->first() = new __repeat_one_loop<_CharT>(__e2.get());
4687    __end_ = __e2->second();
4688    __s->first() = __e2.release();
4689    ++__loop_count_;
4690}
4691
4692template <class _CharT, class _Traits>
4693void
4694basic_regex<_CharT, _Traits>::__push_char(value_type __c)
4695{
4696    if (flags() & icase)
4697        __end_->first() = new __match_char_icase<_CharT, _Traits>
4698                                              (__traits_, __c, __end_->first());
4699    else if (flags() & collate)
4700        __end_->first() = new __match_char_collate<_CharT, _Traits>
4701                                              (__traits_, __c, __end_->first());
4702    else
4703        __end_->first() = new __match_char<_CharT>(__c, __end_->first());
4704    __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4705}
4706
4707template <class _CharT, class _Traits>
4708void
4709basic_regex<_CharT, _Traits>::__push_begin_marked_subexpression()
4710{
4711    if (!(__flags_ & nosubs))
4712    {
4713        __end_->first() =
4714                new __begin_marked_subexpression<_CharT>(++__marked_count_,
4715                                                         __end_->first());
4716        __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4717    }
4718}
4719
4720template <class _CharT, class _Traits>
4721void
4722basic_regex<_CharT, _Traits>::__push_end_marked_subexpression(unsigned __sub)
4723{
4724    if (!(__flags_ & nosubs))
4725    {
4726        __end_->first() =
4727                new __end_marked_subexpression<_CharT>(__sub, __end_->first());
4728        __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4729    }
4730}
4731
4732template <class _CharT, class _Traits>
4733void
4734basic_regex<_CharT, _Traits>::__push_l_anchor()
4735{
4736    __end_->first() = new __l_anchor<_CharT>(__end_->first());
4737    __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4738}
4739
4740template <class _CharT, class _Traits>
4741void
4742basic_regex<_CharT, _Traits>::__push_r_anchor()
4743{
4744    __end_->first() = new __r_anchor<_CharT>(__end_->first());
4745    __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4746}
4747
4748template <class _CharT, class _Traits>
4749void
4750basic_regex<_CharT, _Traits>::__push_match_any()
4751{
4752    __end_->first() = new __match_any<_CharT>(__end_->first());
4753    __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4754}
4755
4756template <class _CharT, class _Traits>
4757void
4758basic_regex<_CharT, _Traits>::__push_match_any_but_newline()
4759{
4760    __end_->first() = new __match_any_but_newline<_CharT>(__end_->first());
4761    __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4762}
4763
4764template <class _CharT, class _Traits>
4765void
4766basic_regex<_CharT, _Traits>::__push_empty()
4767{
4768    __end_->first() = new __empty_state<_CharT>(__end_->first());
4769    __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4770}
4771
4772template <class _CharT, class _Traits>
4773void
4774basic_regex<_CharT, _Traits>::__push_word_boundary(bool __invert)
4775{
4776    __end_->first() = new __word_boundary<_CharT, _Traits>(__traits_, __invert,
4777                                                           __end_->first());
4778    __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4779}
4780
4781template <class _CharT, class _Traits>
4782void
4783basic_regex<_CharT, _Traits>::__push_back_ref(int __i)
4784{
4785    if (flags() & icase)
4786        __end_->first() = new __back_ref_icase<_CharT, _Traits>
4787                                              (__traits_, __i, __end_->first());
4788    else if (flags() & collate)
4789        __end_->first() = new __back_ref_collate<_CharT, _Traits>
4790                                              (__traits_, __i, __end_->first());
4791    else
4792        __end_->first() = new __back_ref<_CharT>(__i, __end_->first());
4793    __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4794}
4795
4796template <class _CharT, class _Traits>
4797void
4798basic_regex<_CharT, _Traits>::__push_alternation(__owns_one_state<_CharT>* __sa,
4799                                                 __owns_one_state<_CharT>* __ea)
4800{
4801    __sa->first() = new __alternate<_CharT>(
4802                         static_cast<__owns_one_state<_CharT>*>(__sa->first()),
4803                         static_cast<__owns_one_state<_CharT>*>(__ea->first()));
4804    __ea->first() = nullptr;
4805    __ea->first() = new __empty_state<_CharT>(__end_->first());
4806    __end_->first() = nullptr;
4807    __end_->first() = new __empty_non_own_state<_CharT>(__ea->first());
4808    __end_ = static_cast<__owns_one_state<_CharT>*>(__ea->first());
4809}
4810
4811template <class _CharT, class _Traits>
4812__bracket_expression<_CharT, _Traits>*
4813basic_regex<_CharT, _Traits>::__start_matching_list(bool __negate)
4814{
4815    __bracket_expression<_CharT, _Traits>* __r =
4816        new __bracket_expression<_CharT, _Traits>(__traits_, __end_->first(),
4817                                                  __negate, __flags_ & icase,
4818                                                  __flags_ & collate);
4819    __end_->first() = __r;
4820    __end_ = __r;
4821    return __r;
4822}
4823
4824template <class _CharT, class _Traits>
4825void
4826basic_regex<_CharT, _Traits>::__push_lookahead(const basic_regex& __exp,
4827                                               bool __invert,
4828                                               unsigned __mexp)
4829{
4830    __end_->first() = new __lookahead<_CharT, _Traits>(__exp, __invert,
4831                                                           __end_->first(), __mexp);
4832    __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4833}
4834
4835typedef basic_regex<char>    regex;
4836typedef basic_regex<wchar_t> wregex;
4837
4838// sub_match
4839
4840template <class _BidirectionalIterator>
4841class _LIBCPP_TEMPLATE_VIS sub_match
4842    : public pair<_BidirectionalIterator, _BidirectionalIterator>
4843{
4844public:
4845    typedef _BidirectionalIterator                              iterator;
4846    typedef typename iterator_traits<iterator>::value_type      value_type;
4847    typedef typename iterator_traits<iterator>::difference_type difference_type;
4848    typedef basic_string<value_type>                            string_type;
4849
4850    bool matched;
4851
4852    _LIBCPP_INLINE_VISIBILITY
4853    _LIBCPP_CONSTEXPR sub_match() : matched() {}
4854
4855    _LIBCPP_INLINE_VISIBILITY
4856    difference_type length() const
4857        {return matched ? _VSTD::distance(this->first, this->second) : 0;}
4858    _LIBCPP_INLINE_VISIBILITY
4859    string_type str() const
4860        {return matched ? string_type(this->first, this->second) : string_type();}
4861    _LIBCPP_INLINE_VISIBILITY
4862    operator string_type() const
4863        {return str();}
4864
4865    _LIBCPP_INLINE_VISIBILITY
4866    int compare(const sub_match& __s) const
4867        {return str().compare(__s.str());}
4868    _LIBCPP_INLINE_VISIBILITY
4869    int compare(const string_type& __s) const
4870        {return str().compare(__s);}
4871    _LIBCPP_INLINE_VISIBILITY
4872    int compare(const value_type* __s) const
4873        {return str().compare(__s);}
4874};
4875
4876typedef sub_match<const char*>             csub_match;
4877typedef sub_match<const wchar_t*>          wcsub_match;
4878typedef sub_match<string::const_iterator>  ssub_match;
4879typedef sub_match<wstring::const_iterator> wssub_match;
4880
4881template <class _BiIter>
4882inline _LIBCPP_INLINE_VISIBILITY
4883bool
4884operator==(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
4885{
4886    return __x.compare(__y) == 0;
4887}
4888
4889template <class _BiIter>
4890inline _LIBCPP_INLINE_VISIBILITY
4891bool
4892operator!=(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
4893{
4894    return !(__x == __y);
4895}
4896
4897template <class _BiIter>
4898inline _LIBCPP_INLINE_VISIBILITY
4899bool
4900operator<(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
4901{
4902    return __x.compare(__y) < 0;
4903}
4904
4905template <class _BiIter>
4906inline _LIBCPP_INLINE_VISIBILITY
4907bool
4908operator<=(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
4909{
4910    return !(__y < __x);
4911}
4912
4913template <class _BiIter>
4914inline _LIBCPP_INLINE_VISIBILITY
4915bool
4916operator>=(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
4917{
4918    return !(__x < __y);
4919}
4920
4921template <class _BiIter>
4922inline _LIBCPP_INLINE_VISIBILITY
4923bool
4924operator>(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
4925{
4926    return __y < __x;
4927}
4928
4929template <class _BiIter, class _ST, class _SA>
4930inline _LIBCPP_INLINE_VISIBILITY
4931bool
4932operator==(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4933           const sub_match<_BiIter>& __y)
4934{
4935    return __y.compare(typename sub_match<_BiIter>::string_type(__x.data(), __x.size())) == 0;
4936}
4937
4938template <class _BiIter, class _ST, class _SA>
4939inline _LIBCPP_INLINE_VISIBILITY
4940bool
4941operator!=(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4942           const sub_match<_BiIter>& __y)
4943{
4944    return !(__x == __y);
4945}
4946
4947template <class _BiIter, class _ST, class _SA>
4948inline _LIBCPP_INLINE_VISIBILITY
4949bool
4950operator<(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4951          const sub_match<_BiIter>& __y)
4952{
4953    return __y.compare(typename sub_match<_BiIter>::string_type(__x.data(), __x.size())) > 0;
4954}
4955
4956template <class _BiIter, class _ST, class _SA>
4957inline _LIBCPP_INLINE_VISIBILITY
4958bool
4959operator>(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4960          const sub_match<_BiIter>& __y)
4961{
4962    return __y < __x;
4963}
4964
4965template <class _BiIter, class _ST, class _SA>
4966inline _LIBCPP_INLINE_VISIBILITY
4967bool operator>=(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4968                const sub_match<_BiIter>& __y)
4969{
4970    return !(__x < __y);
4971}
4972
4973template <class _BiIter, class _ST, class _SA>
4974inline _LIBCPP_INLINE_VISIBILITY
4975bool
4976operator<=(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4977           const sub_match<_BiIter>& __y)
4978{
4979    return !(__y < __x);
4980}
4981
4982template <class _BiIter, class _ST, class _SA>
4983inline _LIBCPP_INLINE_VISIBILITY
4984bool
4985operator==(const sub_match<_BiIter>& __x,
4986           const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
4987{
4988    return __x.compare(typename sub_match<_BiIter>::string_type(__y.data(), __y.size())) == 0;
4989}
4990
4991template <class _BiIter, class _ST, class _SA>
4992inline _LIBCPP_INLINE_VISIBILITY
4993bool
4994operator!=(const sub_match<_BiIter>& __x,
4995           const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
4996{
4997    return !(__x == __y);
4998}
4999
5000template <class _BiIter, class _ST, class _SA>
5001inline _LIBCPP_INLINE_VISIBILITY
5002bool
5003operator<(const sub_match<_BiIter>& __x,
5004          const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
5005{
5006    return __x.compare(typename sub_match<_BiIter>::string_type(__y.data(), __y.size())) < 0;
5007}
5008
5009template <class _BiIter, class _ST, class _SA>
5010inline _LIBCPP_INLINE_VISIBILITY
5011bool operator>(const sub_match<_BiIter>& __x,
5012               const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
5013{
5014    return __y < __x;
5015}
5016
5017template <class _BiIter, class _ST, class _SA>
5018inline _LIBCPP_INLINE_VISIBILITY
5019bool
5020operator>=(const sub_match<_BiIter>& __x,
5021           const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
5022{
5023    return !(__x < __y);
5024}
5025
5026template <class _BiIter, class _ST, class _SA>
5027inline _LIBCPP_INLINE_VISIBILITY
5028bool
5029operator<=(const sub_match<_BiIter>& __x,
5030           const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
5031{
5032    return !(__y < __x);
5033}
5034
5035template <class _BiIter>
5036inline _LIBCPP_INLINE_VISIBILITY
5037bool
5038operator==(typename iterator_traits<_BiIter>::value_type const* __x,
5039           const sub_match<_BiIter>& __y)
5040{
5041    return __y.compare(__x) == 0;
5042}
5043
5044template <class _BiIter>
5045inline _LIBCPP_INLINE_VISIBILITY
5046bool
5047operator!=(typename iterator_traits<_BiIter>::value_type const* __x,
5048           const sub_match<_BiIter>& __y)
5049{
5050    return !(__x == __y);
5051}
5052
5053template <class _BiIter>
5054inline _LIBCPP_INLINE_VISIBILITY
5055bool
5056operator<(typename iterator_traits<_BiIter>::value_type const* __x,
5057          const sub_match<_BiIter>& __y)
5058{
5059    return __y.compare(__x) > 0;
5060}
5061
5062template <class _BiIter>
5063inline _LIBCPP_INLINE_VISIBILITY
5064bool
5065operator>(typename iterator_traits<_BiIter>::value_type const* __x,
5066          const sub_match<_BiIter>& __y)
5067{
5068    return __y < __x;
5069}
5070
5071template <class _BiIter>
5072inline _LIBCPP_INLINE_VISIBILITY
5073bool
5074operator>=(typename iterator_traits<_BiIter>::value_type const* __x,
5075           const sub_match<_BiIter>& __y)
5076{
5077    return !(__x < __y);
5078}
5079
5080template <class _BiIter>
5081inline _LIBCPP_INLINE_VISIBILITY
5082bool
5083operator<=(typename iterator_traits<_BiIter>::value_type const* __x,
5084           const sub_match<_BiIter>& __y)
5085{
5086    return !(__y < __x);
5087}
5088
5089template <class _BiIter>
5090inline _LIBCPP_INLINE_VISIBILITY
5091bool
5092operator==(const sub_match<_BiIter>& __x,
5093           typename iterator_traits<_BiIter>::value_type const* __y)
5094{
5095    return __x.compare(__y) == 0;
5096}
5097
5098template <class _BiIter>
5099inline _LIBCPP_INLINE_VISIBILITY
5100bool
5101operator!=(const sub_match<_BiIter>& __x,
5102           typename iterator_traits<_BiIter>::value_type const* __y)
5103{
5104    return !(__x == __y);
5105}
5106
5107template <class _BiIter>
5108inline _LIBCPP_INLINE_VISIBILITY
5109bool
5110operator<(const sub_match<_BiIter>& __x,
5111          typename iterator_traits<_BiIter>::value_type const* __y)
5112{
5113    return __x.compare(__y) < 0;
5114}
5115
5116template <class _BiIter>
5117inline _LIBCPP_INLINE_VISIBILITY
5118bool
5119operator>(const sub_match<_BiIter>& __x,
5120          typename iterator_traits<_BiIter>::value_type const* __y)
5121{
5122    return __y < __x;
5123}
5124
5125template <class _BiIter>
5126inline _LIBCPP_INLINE_VISIBILITY
5127bool
5128operator>=(const sub_match<_BiIter>& __x,
5129           typename iterator_traits<_BiIter>::value_type const* __y)
5130{
5131    return !(__x < __y);
5132}
5133
5134template <class _BiIter>
5135inline _LIBCPP_INLINE_VISIBILITY
5136bool
5137operator<=(const sub_match<_BiIter>& __x,
5138           typename iterator_traits<_BiIter>::value_type const* __y)
5139{
5140    return !(__y < __x);
5141}
5142
5143template <class _BiIter>
5144inline _LIBCPP_INLINE_VISIBILITY
5145bool
5146operator==(typename iterator_traits<_BiIter>::value_type const& __x,
5147           const sub_match<_BiIter>& __y)
5148{
5149    typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
5150    return __y.compare(string_type(1, __x)) == 0;
5151}
5152
5153template <class _BiIter>
5154inline _LIBCPP_INLINE_VISIBILITY
5155bool
5156operator!=(typename iterator_traits<_BiIter>::value_type const& __x,
5157           const sub_match<_BiIter>& __y)
5158{
5159    return !(__x == __y);
5160}
5161
5162template <class _BiIter>
5163inline _LIBCPP_INLINE_VISIBILITY
5164bool
5165operator<(typename iterator_traits<_BiIter>::value_type const& __x,
5166          const sub_match<_BiIter>& __y)
5167{
5168    typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
5169    return __y.compare(string_type(1, __x)) > 0;
5170}
5171
5172template <class _BiIter>
5173inline _LIBCPP_INLINE_VISIBILITY
5174bool
5175operator>(typename iterator_traits<_BiIter>::value_type const& __x,
5176          const sub_match<_BiIter>& __y)
5177{
5178    return __y < __x;
5179}
5180
5181template <class _BiIter>
5182inline _LIBCPP_INLINE_VISIBILITY
5183bool
5184operator>=(typename iterator_traits<_BiIter>::value_type const& __x,
5185           const sub_match<_BiIter>& __y)
5186{
5187    return !(__x < __y);
5188}
5189
5190template <class _BiIter>
5191inline _LIBCPP_INLINE_VISIBILITY
5192bool
5193operator<=(typename iterator_traits<_BiIter>::value_type const& __x,
5194           const sub_match<_BiIter>& __y)
5195{
5196    return !(__y < __x);
5197}
5198
5199template <class _BiIter>
5200inline _LIBCPP_INLINE_VISIBILITY
5201bool
5202operator==(const sub_match<_BiIter>& __x,
5203           typename iterator_traits<_BiIter>::value_type const& __y)
5204{
5205    typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
5206    return __x.compare(string_type(1, __y)) == 0;
5207}
5208
5209template <class _BiIter>
5210inline _LIBCPP_INLINE_VISIBILITY
5211bool
5212operator!=(const sub_match<_BiIter>& __x,
5213           typename iterator_traits<_BiIter>::value_type const& __y)
5214{
5215    return !(__x == __y);
5216}
5217
5218template <class _BiIter>
5219inline _LIBCPP_INLINE_VISIBILITY
5220bool
5221operator<(const sub_match<_BiIter>& __x,
5222          typename iterator_traits<_BiIter>::value_type const& __y)
5223{
5224    typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
5225    return __x.compare(string_type(1, __y)) < 0;
5226}
5227
5228template <class _BiIter>
5229inline _LIBCPP_INLINE_VISIBILITY
5230bool
5231operator>(const sub_match<_BiIter>& __x,
5232          typename iterator_traits<_BiIter>::value_type const& __y)
5233{
5234    return __y < __x;
5235}
5236
5237template <class _BiIter>
5238inline _LIBCPP_INLINE_VISIBILITY
5239bool
5240operator>=(const sub_match<_BiIter>& __x,
5241           typename iterator_traits<_BiIter>::value_type const& __y)
5242{
5243    return !(__x < __y);
5244}
5245
5246template <class _BiIter>
5247inline _LIBCPP_INLINE_VISIBILITY
5248bool
5249operator<=(const sub_match<_BiIter>& __x,
5250           typename iterator_traits<_BiIter>::value_type const& __y)
5251{
5252    return !(__y < __x);
5253}
5254
5255template <class _CharT, class _ST, class _BiIter>
5256inline _LIBCPP_INLINE_VISIBILITY
5257basic_ostream<_CharT, _ST>&
5258operator<<(basic_ostream<_CharT, _ST>& __os, const sub_match<_BiIter>& __m)
5259{
5260    return __os << __m.str();
5261}
5262
5263template <class _BidirectionalIterator, class _Allocator>
5264class _LIBCPP_TEMPLATE_VIS match_results
5265{
5266public:
5267    typedef _Allocator                                        allocator_type;
5268    typedef sub_match<_BidirectionalIterator>                 value_type;
5269private:
5270    typedef vector<value_type, allocator_type>                __container_type;
5271
5272    __container_type  __matches_;
5273    value_type __unmatched_;
5274    value_type __prefix_;
5275    value_type __suffix_;
5276    bool       __ready_;
5277public:
5278    _BidirectionalIterator __position_start_;
5279    typedef const value_type&                                 const_reference;
5280    typedef value_type&                                       reference;
5281    typedef typename __container_type::const_iterator         const_iterator;
5282    typedef const_iterator                                    iterator;
5283    typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
5284    typedef typename allocator_traits<allocator_type>::size_type size_type;
5285    typedef typename iterator_traits<_BidirectionalIterator>::value_type char_type;
5286    typedef basic_string<char_type>                           string_type;
5287
5288    // construct/copy/destroy:
5289    explicit match_results(const allocator_type& __a = allocator_type());
5290//    match_results(const match_results&) = default;
5291//    match_results& operator=(const match_results&) = default;
5292//    match_results(match_results&& __m) = default;
5293//    match_results& operator=(match_results&& __m) = default;
5294//    ~match_results() = default;
5295
5296    _LIBCPP_INLINE_VISIBILITY
5297    bool ready() const {return __ready_;}
5298
5299    // size:
5300    _LIBCPP_INLINE_VISIBILITY
5301    size_type size() const _NOEXCEPT {return __matches_.size();}
5302    _LIBCPP_INLINE_VISIBILITY
5303    size_type max_size() const _NOEXCEPT {return __matches_.max_size();}
5304    _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
5305    bool empty() const _NOEXCEPT {return size() == 0;}
5306
5307    // element access:
5308    _LIBCPP_INLINE_VISIBILITY
5309    difference_type length(size_type __sub = 0) const
5310        {
5311        _LIBCPP_ASSERT(ready(), "match_results::length() called when not ready");
5312        return (*this)[__sub].length();
5313        }
5314    _LIBCPP_INLINE_VISIBILITY
5315    difference_type position(size_type __sub = 0) const
5316        {
5317        _LIBCPP_ASSERT(ready(), "match_results::position() called when not ready");
5318        return _VSTD::distance(__position_start_, (*this)[__sub].first);
5319        }
5320    _LIBCPP_INLINE_VISIBILITY
5321    string_type str(size_type __sub = 0) const
5322        {
5323        _LIBCPP_ASSERT(ready(), "match_results::str() called when not ready");
5324        return (*this)[__sub].str();
5325        }
5326    _LIBCPP_INLINE_VISIBILITY
5327    const_reference operator[](size_type __n) const
5328        {
5329        _LIBCPP_ASSERT(ready(), "match_results::operator[]() called when not ready");
5330        return __n < __matches_.size() ? __matches_[__n] : __unmatched_;
5331        }
5332
5333    _LIBCPP_INLINE_VISIBILITY
5334    const_reference prefix() const
5335        {
5336        _LIBCPP_ASSERT(ready(), "match_results::prefix() called when not ready");
5337        return __prefix_;
5338        }
5339    _LIBCPP_INLINE_VISIBILITY
5340    const_reference suffix() const
5341        {
5342        _LIBCPP_ASSERT(ready(), "match_results::suffix() called when not ready");
5343        return __suffix_;
5344        }
5345
5346    _LIBCPP_INLINE_VISIBILITY
5347    const_iterator begin() const {return empty() ? __matches_.end() : __matches_.begin();}
5348    _LIBCPP_INLINE_VISIBILITY
5349    const_iterator end() const {return __matches_.end();}
5350    _LIBCPP_INLINE_VISIBILITY
5351    const_iterator cbegin() const {return empty() ? __matches_.end() : __matches_.begin();}
5352    _LIBCPP_INLINE_VISIBILITY
5353    const_iterator cend() const {return __matches_.end();}
5354
5355    // format:
5356    template <class _OutputIter>
5357        _OutputIter
5358        format(_OutputIter __output_iter, const char_type* __fmt_first,
5359               const char_type* __fmt_last,
5360               regex_constants::match_flag_type __flags = regex_constants::format_default) const;
5361    template <class _OutputIter, class _ST, class _SA>
5362        _LIBCPP_INLINE_VISIBILITY
5363        _OutputIter
5364        format(_OutputIter __output_iter, const basic_string<char_type, _ST, _SA>& __fmt,
5365               regex_constants::match_flag_type __flags = regex_constants::format_default) const
5366            {return format(__output_iter, __fmt.data(), __fmt.data() + __fmt.size(), __flags);}
5367    template <class _ST, class _SA>
5368        _LIBCPP_INLINE_VISIBILITY
5369        basic_string<char_type, _ST, _SA>
5370        format(const basic_string<char_type, _ST, _SA>& __fmt,
5371               regex_constants::match_flag_type __flags = regex_constants::format_default) const
5372        {
5373            basic_string<char_type, _ST, _SA> __r;
5374            format(back_inserter(__r), __fmt.data(), __fmt.data() + __fmt.size(),
5375                   __flags);
5376            return __r;
5377        }
5378    _LIBCPP_INLINE_VISIBILITY
5379    string_type
5380        format(const char_type* __fmt,
5381               regex_constants::match_flag_type __flags = regex_constants::format_default) const
5382        {
5383            string_type __r;
5384            format(back_inserter(__r), __fmt,
5385                   __fmt + char_traits<char_type>::length(__fmt), __flags);
5386            return __r;
5387        }
5388
5389    // allocator:
5390    _LIBCPP_INLINE_VISIBILITY
5391    allocator_type get_allocator() const {return __matches_.get_allocator();}
5392
5393    // swap:
5394    void swap(match_results& __m);
5395
5396    template <class _Bp, class _Ap>
5397        _LIBCPP_INLINE_VISIBILITY
5398        void __assign(_BidirectionalIterator __f, _BidirectionalIterator __l,
5399                      const match_results<_Bp, _Ap>& __m, bool __no_update_pos)
5400    {
5401        _Bp __mf = __m.prefix().first;
5402        __matches_.resize(__m.size());
5403        for (size_type __i = 0; __i < __matches_.size(); ++__i)
5404        {
5405            __matches_[__i].first = _VSTD::next(__f, _VSTD::distance(__mf, __m[__i].first));
5406            __matches_[__i].second = _VSTD::next(__f, _VSTD::distance(__mf, __m[__i].second));
5407            __matches_[__i].matched = __m[__i].matched;
5408        }
5409        __unmatched_.first   = __l;
5410        __unmatched_.second  = __l;
5411        __unmatched_.matched = false;
5412        __prefix_.first = _VSTD::next(__f, _VSTD::distance(__mf, __m.prefix().first));
5413        __prefix_.second = _VSTD::next(__f, _VSTD::distance(__mf, __m.prefix().second));
5414        __prefix_.matched = __m.prefix().matched;
5415        __suffix_.first = _VSTD::next(__f, _VSTD::distance(__mf, __m.suffix().first));
5416        __suffix_.second = _VSTD::next(__f, _VSTD::distance(__mf, __m.suffix().second));
5417        __suffix_.matched = __m.suffix().matched;
5418        if (!__no_update_pos)
5419            __position_start_ = __prefix_.first;
5420        __ready_ = __m.ready();
5421    }
5422
5423private:
5424    void __init(unsigned __s,
5425                _BidirectionalIterator __f, _BidirectionalIterator __l,
5426                bool __no_update_pos = false);
5427
5428    template <class, class> friend class basic_regex;
5429
5430    template <class _Bp, class _Ap, class _Cp, class _Tp>
5431    friend
5432    bool
5433    regex_match(_Bp, _Bp, match_results<_Bp, _Ap>&, const basic_regex<_Cp, _Tp>&,
5434                regex_constants::match_flag_type);
5435
5436    template <class _Bp, class _Ap>
5437    friend
5438    bool
5439    operator==(const match_results<_Bp, _Ap>&, const match_results<_Bp, _Ap>&);
5440
5441    template <class, class> friend class __lookahead;
5442};
5443
5444template <class _BidirectionalIterator, class _Allocator>
5445match_results<_BidirectionalIterator, _Allocator>::match_results(
5446        const allocator_type& __a)
5447    : __matches_(__a),
5448      __unmatched_(),
5449      __prefix_(),
5450      __suffix_(),
5451      __ready_(false),
5452      __position_start_()
5453{
5454}
5455
5456template <class _BidirectionalIterator, class _Allocator>
5457void
5458match_results<_BidirectionalIterator, _Allocator>::__init(unsigned __s,
5459                         _BidirectionalIterator __f, _BidirectionalIterator __l,
5460                         bool __no_update_pos)
5461{
5462    __unmatched_.first   = __l;
5463    __unmatched_.second  = __l;
5464    __unmatched_.matched = false;
5465    __matches_.assign(__s, __unmatched_);
5466    __prefix_.first      = __f;
5467    __prefix_.second     = __f;
5468    __prefix_.matched    = false;
5469    __suffix_ = __unmatched_;
5470    if (!__no_update_pos)
5471        __position_start_ = __prefix_.first;
5472    __ready_ = true;
5473}
5474
5475template <class _BidirectionalIterator, class _Allocator>
5476template <class _OutputIter>
5477_OutputIter
5478match_results<_BidirectionalIterator, _Allocator>::format(_OutputIter __output_iter,
5479        const char_type* __fmt_first, const char_type* __fmt_last,
5480        regex_constants::match_flag_type __flags) const
5481{
5482    _LIBCPP_ASSERT(ready(), "match_results::format() called when not ready");
5483    if (__flags & regex_constants::format_sed)
5484    {
5485        for (; __fmt_first != __fmt_last; ++__fmt_first)
5486        {
5487            if (*__fmt_first == '&')
5488                __output_iter = _VSTD::copy(__matches_[0].first, __matches_[0].second,
5489                                   __output_iter);
5490            else if (*__fmt_first == '\\' && __fmt_first + 1 != __fmt_last)
5491            {
5492                ++__fmt_first;
5493                if ('0' <= *__fmt_first && *__fmt_first <= '9')
5494                {
5495                    size_t __i = *__fmt_first - '0';
5496                    __output_iter = _VSTD::copy((*this)[__i].first,
5497                                        (*this)[__i].second, __output_iter);
5498                }
5499                else
5500                {
5501                    *__output_iter = *__fmt_first;
5502                    ++__output_iter;
5503                }
5504            }
5505            else
5506            {
5507                *__output_iter = *__fmt_first;
5508                ++__output_iter;
5509            }
5510        }
5511    }
5512    else
5513    {
5514        for (; __fmt_first != __fmt_last; ++__fmt_first)
5515        {
5516            if (*__fmt_first == '$' && __fmt_first + 1 != __fmt_last)
5517            {
5518                switch (__fmt_first[1])
5519                {
5520                case '$':
5521                    *__output_iter = *++__fmt_first;
5522                    ++__output_iter;
5523                    break;
5524                case '&':
5525                    ++__fmt_first;
5526                    __output_iter = _VSTD::copy(__matches_[0].first, __matches_[0].second,
5527                                       __output_iter);
5528                    break;
5529                case '`':
5530                    ++__fmt_first;
5531                    __output_iter = _VSTD::copy(__prefix_.first, __prefix_.second, __output_iter);
5532                    break;
5533                case '\'':
5534                    ++__fmt_first;
5535                    __output_iter = _VSTD::copy(__suffix_.first, __suffix_.second, __output_iter);
5536                    break;
5537                default:
5538                    if ('0' <= __fmt_first[1] && __fmt_first[1] <= '9')
5539                    {
5540                        ++__fmt_first;
5541                        size_t __idx = *__fmt_first - '0';
5542                        if (__fmt_first + 1 != __fmt_last &&
5543                            '0' <= __fmt_first[1] && __fmt_first[1] <= '9')
5544                        {
5545                            ++__fmt_first;
5546                            if (__idx >= std::numeric_limits<size_t>::max() / 10)
5547                                __throw_regex_error<regex_constants::error_escape>();
5548                            __idx = 10 * __idx + *__fmt_first - '0';
5549                        }
5550                        __output_iter = _VSTD::copy((*this)[__idx].first,
5551                                            (*this)[__idx].second, __output_iter);
5552                    }
5553                    else
5554                    {
5555                        *__output_iter = *__fmt_first;
5556                        ++__output_iter;
5557                    }
5558                    break;
5559                }
5560            }
5561            else
5562            {
5563                *__output_iter = *__fmt_first;
5564                ++__output_iter;
5565            }
5566        }
5567    }
5568    return __output_iter;
5569}
5570
5571template <class _BidirectionalIterator, class _Allocator>
5572void
5573match_results<_BidirectionalIterator, _Allocator>::swap(match_results& __m)
5574{
5575    using _VSTD::swap;
5576    swap(__matches_, __m.__matches_);
5577    swap(__unmatched_, __m.__unmatched_);
5578    swap(__prefix_, __m.__prefix_);
5579    swap(__suffix_, __m.__suffix_);
5580    swap(__position_start_, __m.__position_start_);
5581    swap(__ready_, __m.__ready_);
5582}
5583
5584typedef match_results<const char*>             cmatch;
5585typedef match_results<const wchar_t*>          wcmatch;
5586typedef match_results<string::const_iterator>  smatch;
5587typedef match_results<wstring::const_iterator> wsmatch;
5588
5589template <class _BidirectionalIterator, class _Allocator>
5590bool
5591operator==(const match_results<_BidirectionalIterator, _Allocator>& __x,
5592           const match_results<_BidirectionalIterator, _Allocator>& __y)
5593{
5594    if (__x.__ready_ != __y.__ready_)
5595        return false;
5596    if (!__x.__ready_)
5597        return true;
5598    return __x.__matches_ == __y.__matches_ &&
5599           __x.__prefix_ == __y.__prefix_ &&
5600           __x.__suffix_ == __y.__suffix_;
5601}
5602
5603template <class _BidirectionalIterator, class _Allocator>
5604inline _LIBCPP_INLINE_VISIBILITY
5605bool
5606operator!=(const match_results<_BidirectionalIterator, _Allocator>& __x,
5607           const match_results<_BidirectionalIterator, _Allocator>& __y)
5608{
5609    return !(__x == __y);
5610}
5611
5612template <class _BidirectionalIterator, class _Allocator>
5613inline _LIBCPP_INLINE_VISIBILITY
5614void
5615swap(match_results<_BidirectionalIterator, _Allocator>& __x,
5616     match_results<_BidirectionalIterator, _Allocator>& __y)
5617{
5618    __x.swap(__y);
5619}
5620
5621// regex_search
5622
5623template <class _CharT, class _Traits>
5624template <class _Allocator>
5625bool
5626basic_regex<_CharT, _Traits>::__match_at_start_ecma(
5627        const _CharT* __first, const _CharT* __last,
5628        match_results<const _CharT*, _Allocator>& __m,
5629        regex_constants::match_flag_type __flags, bool __at_first) const
5630{
5631    vector<__state> __states;
5632    __node* __st = __start_.get();
5633    if (__st)
5634    {
5635        sub_match<const _CharT*> __unmatched;
5636        __unmatched.first   = __last;
5637        __unmatched.second  = __last;
5638        __unmatched.matched = false;
5639
5640        __states.push_back(__state());
5641        __states.back().__do_ = 0;
5642        __states.back().__first_ = __first;
5643        __states.back().__current_ = __first;
5644        __states.back().__last_ = __last;
5645        __states.back().__sub_matches_.resize(mark_count(), __unmatched);
5646        __states.back().__loop_data_.resize(__loop_count());
5647        __states.back().__node_ = __st;
5648        __states.back().__flags_ = __flags;
5649        __states.back().__at_first_ = __at_first;
5650        int __counter = 0;
5651        int __length = __last - __first;
5652        do
5653        {
5654            ++__counter;
5655            if (__counter % _LIBCPP_REGEX_COMPLEXITY_FACTOR == 0 &&
5656                __counter / _LIBCPP_REGEX_COMPLEXITY_FACTOR >= __length)
5657              __throw_regex_error<regex_constants::error_complexity>();
5658            __state& __s = __states.back();
5659            if (__s.__node_)
5660                __s.__node_->__exec(__s);
5661            switch (__s.__do_)
5662            {
5663            case __state::__end_state:
5664                if ((__flags & regex_constants::match_not_null) &&
5665                    __s.__current_ == __first)
5666                {
5667                  __states.pop_back();
5668                  break;
5669                }
5670                if ((__flags & regex_constants::__full_match) &&
5671                    __s.__current_ != __last)
5672                {
5673                  __states.pop_back();
5674                  break;
5675                }
5676                __m.__matches_[0].first = __first;
5677                __m.__matches_[0].second = _VSTD::next(__first, __s.__current_ - __first);
5678                __m.__matches_[0].matched = true;
5679                for (unsigned __i = 0; __i < __s.__sub_matches_.size(); ++__i)
5680                    __m.__matches_[__i+1] = __s.__sub_matches_[__i];
5681                return true;
5682            case __state::__accept_and_consume:
5683            case __state::__repeat:
5684            case __state::__accept_but_not_consume:
5685                break;
5686            case __state::__split:
5687                {
5688                __state __snext = __s;
5689                __s.__node_->__exec_split(true, __s);
5690                __snext.__node_->__exec_split(false, __snext);
5691                __states.push_back(_VSTD::move(__snext));
5692                }
5693                break;
5694            case __state::__reject:
5695                __states.pop_back();
5696                break;
5697            default:
5698                __throw_regex_error<regex_constants::__re_err_unknown>();
5699                break;
5700
5701            }
5702        } while (!__states.empty());
5703    }
5704    return false;
5705}
5706
5707template <class _CharT, class _Traits>
5708template <class _Allocator>
5709bool
5710basic_regex<_CharT, _Traits>::__match_at_start_posix_nosubs(
5711        const _CharT* __first, const _CharT* __last,
5712        match_results<const _CharT*, _Allocator>& __m,
5713        regex_constants::match_flag_type __flags, bool __at_first) const
5714{
5715    deque<__state> __states;
5716    ptrdiff_t __highest_j = 0;
5717    ptrdiff_t _Np = _VSTD::distance(__first, __last);
5718    __node* __st = __start_.get();
5719    if (__st)
5720    {
5721        __states.push_back(__state());
5722        __states.back().__do_ = 0;
5723        __states.back().__first_ = __first;
5724        __states.back().__current_ = __first;
5725        __states.back().__last_ = __last;
5726        __states.back().__loop_data_.resize(__loop_count());
5727        __states.back().__node_ = __st;
5728        __states.back().__flags_ = __flags;
5729        __states.back().__at_first_ = __at_first;
5730        bool __matched = false;
5731        int __counter = 0;
5732        int __length = __last - __first;
5733        do
5734        {
5735            ++__counter;
5736            if (__counter % _LIBCPP_REGEX_COMPLEXITY_FACTOR == 0 &&
5737                __counter / _LIBCPP_REGEX_COMPLEXITY_FACTOR >= __length)
5738              __throw_regex_error<regex_constants::error_complexity>();
5739            __state& __s = __states.back();
5740            if (__s.__node_)
5741                __s.__node_->__exec(__s);
5742            switch (__s.__do_)
5743            {
5744            case __state::__end_state:
5745                if ((__flags & regex_constants::match_not_null) &&
5746                    __s.__current_ == __first)
5747                {
5748                  __states.pop_back();
5749                  break;
5750                }
5751                if ((__flags & regex_constants::__full_match) &&
5752                    __s.__current_ != __last)
5753                {
5754                  __states.pop_back();
5755                  break;
5756                }
5757                if (!__matched || __highest_j < __s.__current_ - __s.__first_)
5758                    __highest_j = __s.__current_ - __s.__first_;
5759                __matched = true;
5760                if (__highest_j == _Np)
5761                    __states.clear();
5762                else
5763                    __states.pop_back();
5764                break;
5765            case __state::__consume_input:
5766                break;
5767            case __state::__accept_and_consume:
5768                __states.push_front(_VSTD::move(__s));
5769                __states.pop_back();
5770                break;
5771            case __state::__repeat:
5772            case __state::__accept_but_not_consume:
5773                break;
5774            case __state::__split:
5775                {
5776                __state __snext = __s;
5777                __s.__node_->__exec_split(true, __s);
5778                __snext.__node_->__exec_split(false, __snext);
5779                __states.push_back(_VSTD::move(__snext));
5780                }
5781                break;
5782            case __state::__reject:
5783                __states.pop_back();
5784                break;
5785            default:
5786                __throw_regex_error<regex_constants::__re_err_unknown>();
5787                break;
5788            }
5789        } while (!__states.empty());
5790        if (__matched)
5791        {
5792            __m.__matches_[0].first = __first;
5793            __m.__matches_[0].second = _VSTD::next(__first, __highest_j);
5794            __m.__matches_[0].matched = true;
5795            return true;
5796        }
5797    }
5798    return false;
5799}
5800
5801template <class _CharT, class _Traits>
5802template <class _Allocator>
5803bool
5804basic_regex<_CharT, _Traits>::__match_at_start_posix_subs(
5805        const _CharT* __first, const _CharT* __last,
5806        match_results<const _CharT*, _Allocator>& __m,
5807        regex_constants::match_flag_type __flags, bool __at_first) const
5808{
5809    vector<__state> __states;
5810    __state __best_state;
5811    ptrdiff_t __j = 0;
5812    ptrdiff_t __highest_j = 0;
5813    ptrdiff_t _Np = _VSTD::distance(__first, __last);
5814    __node* __st = __start_.get();
5815    if (__st)
5816    {
5817        sub_match<const _CharT*> __unmatched;
5818        __unmatched.first   = __last;
5819        __unmatched.second  = __last;
5820        __unmatched.matched = false;
5821
5822        __states.push_back(__state());
5823        __states.back().__do_ = 0;
5824        __states.back().__first_ = __first;
5825        __states.back().__current_ = __first;
5826        __states.back().__last_ = __last;
5827        __states.back().__sub_matches_.resize(mark_count(), __unmatched);
5828        __states.back().__loop_data_.resize(__loop_count());
5829        __states.back().__node_ = __st;
5830        __states.back().__flags_ = __flags;
5831        __states.back().__at_first_ = __at_first;
5832        const _CharT* __current = __first;
5833        bool __matched = false;
5834        int __counter = 0;
5835        int __length = __last - __first;
5836        do
5837        {
5838            ++__counter;
5839            if (__counter % _LIBCPP_REGEX_COMPLEXITY_FACTOR == 0 &&
5840                __counter / _LIBCPP_REGEX_COMPLEXITY_FACTOR >= __length)
5841              __throw_regex_error<regex_constants::error_complexity>();
5842            __state& __s = __states.back();
5843            if (__s.__node_)
5844                __s.__node_->__exec(__s);
5845            switch (__s.__do_)
5846            {
5847            case __state::__end_state:
5848                if ((__flags & regex_constants::match_not_null) &&
5849                    __s.__current_ == __first)
5850                {
5851                  __states.pop_back();
5852                  break;
5853                }
5854                if ((__flags & regex_constants::__full_match) &&
5855                    __s.__current_ != __last)
5856                {
5857                  __states.pop_back();
5858                  break;
5859                }
5860                if (!__matched || __highest_j < __s.__current_ - __s.__first_)
5861                {
5862                    __highest_j = __s.__current_ - __s.__first_;
5863                    __best_state = __s;
5864                }
5865                __matched = true;
5866                if (__highest_j == _Np)
5867                    __states.clear();
5868                else
5869                    __states.pop_back();
5870                break;
5871            case __state::__accept_and_consume:
5872                __j += __s.__current_ - __current;
5873                __current = __s.__current_;
5874                break;
5875            case __state::__repeat:
5876            case __state::__accept_but_not_consume:
5877                break;
5878            case __state::__split:
5879                {
5880                __state __snext = __s;
5881                __s.__node_->__exec_split(true, __s);
5882                __snext.__node_->__exec_split(false, __snext);
5883                __states.push_back(_VSTD::move(__snext));
5884                }
5885                break;
5886            case __state::__reject:
5887                __states.pop_back();
5888                break;
5889            default:
5890                __throw_regex_error<regex_constants::__re_err_unknown>();
5891                break;
5892            }
5893        } while (!__states.empty());
5894        if (__matched)
5895        {
5896            __m.__matches_[0].first = __first;
5897            __m.__matches_[0].second = _VSTD::next(__first, __highest_j);
5898            __m.__matches_[0].matched = true;
5899            for (unsigned __i = 0; __i < __best_state.__sub_matches_.size(); ++__i)
5900                __m.__matches_[__i+1] = __best_state.__sub_matches_[__i];
5901            return true;
5902        }
5903    }
5904    return false;
5905}
5906
5907template <class _CharT, class _Traits>
5908template <class _Allocator>
5909bool
5910basic_regex<_CharT, _Traits>::__match_at_start(
5911        const _CharT* __first, const _CharT* __last,
5912        match_results<const _CharT*, _Allocator>& __m,
5913        regex_constants::match_flag_type __flags, bool __at_first) const
5914{
5915    if (__get_grammar(__flags_) == ECMAScript)
5916        return __match_at_start_ecma(__first, __last, __m, __flags, __at_first);
5917    if (mark_count() == 0)
5918        return __match_at_start_posix_nosubs(__first, __last, __m, __flags, __at_first);
5919    return __match_at_start_posix_subs(__first, __last, __m, __flags, __at_first);
5920}
5921
5922template <class _CharT, class _Traits>
5923template <class _Allocator>
5924bool
5925basic_regex<_CharT, _Traits>::__search(
5926        const _CharT* __first, const _CharT* __last,
5927        match_results<const _CharT*, _Allocator>& __m,
5928        regex_constants::match_flag_type __flags) const
5929{
5930    __m.__init(1 + mark_count(), __first, __last,
5931                                    __flags & regex_constants::__no_update_pos);
5932    if (__match_at_start(__first, __last, __m, __flags,
5933                                    !(__flags & regex_constants::__no_update_pos)))
5934    {
5935        __m.__prefix_.second = __m[0].first;
5936        __m.__prefix_.matched = __m.__prefix_.first != __m.__prefix_.second;
5937        __m.__suffix_.first = __m[0].second;
5938        __m.__suffix_.matched = __m.__suffix_.first != __m.__suffix_.second;
5939        return true;
5940    }
5941    if (__first != __last && !(__flags & regex_constants::match_continuous))
5942    {
5943        __flags |= regex_constants::match_prev_avail;
5944        for (++__first; __first != __last; ++__first)
5945        {
5946            __m.__matches_.assign(__m.size(), __m.__unmatched_);
5947            if (__match_at_start(__first, __last, __m, __flags, false))
5948            {
5949                __m.__prefix_.second = __m[0].first;
5950                __m.__prefix_.matched = __m.__prefix_.first != __m.__prefix_.second;
5951                __m.__suffix_.first = __m[0].second;
5952                __m.__suffix_.matched = __m.__suffix_.first != __m.__suffix_.second;
5953                return true;
5954            }
5955            __m.__matches_.assign(__m.size(), __m.__unmatched_);
5956        }
5957    }
5958    __m.__matches_.clear();
5959    return false;
5960}
5961
5962template <class _BidirectionalIterator, class _Allocator, class _CharT, class _Traits>
5963inline _LIBCPP_INLINE_VISIBILITY
5964bool
5965regex_search(_BidirectionalIterator __first, _BidirectionalIterator __last,
5966             match_results<_BidirectionalIterator, _Allocator>& __m,
5967             const basic_regex<_CharT, _Traits>& __e,
5968             regex_constants::match_flag_type __flags = regex_constants::match_default)
5969{
5970    int __offset = (__flags & regex_constants::match_prev_avail) ? 1 : 0;
5971    basic_string<_CharT> __s(_VSTD::prev(__first, __offset), __last);
5972    match_results<const _CharT*> __mc;
5973    bool __r = __e.__search(__s.data() + __offset, __s.data() + __s.size(), __mc, __flags);
5974    __m.__assign(__first, __last, __mc, __flags & regex_constants::__no_update_pos);
5975    return __r;
5976}
5977
5978template <class _Iter, class _Allocator, class _CharT, class _Traits>
5979inline _LIBCPP_INLINE_VISIBILITY
5980bool
5981regex_search(__wrap_iter<_Iter> __first,
5982             __wrap_iter<_Iter> __last,
5983             match_results<__wrap_iter<_Iter>, _Allocator>& __m,
5984             const basic_regex<_CharT, _Traits>& __e,
5985             regex_constants::match_flag_type __flags = regex_constants::match_default)
5986{
5987    match_results<const _CharT*> __mc;
5988    bool __r = __e.__search(__first.base(), __last.base(), __mc, __flags);
5989    __m.__assign(__first, __last, __mc, __flags & regex_constants::__no_update_pos);
5990    return __r;
5991}
5992
5993template <class _Allocator, class _CharT, class _Traits>
5994inline _LIBCPP_INLINE_VISIBILITY
5995bool
5996regex_search(const _CharT* __first, const _CharT* __last,
5997             match_results<const _CharT*, _Allocator>& __m,
5998             const basic_regex<_CharT, _Traits>& __e,
5999             regex_constants::match_flag_type __flags = regex_constants::match_default)
6000{
6001    return __e.__search(__first, __last, __m, __flags);
6002}
6003
6004template <class _BidirectionalIterator, class _CharT, class _Traits>
6005inline _LIBCPP_INLINE_VISIBILITY
6006bool
6007regex_search(_BidirectionalIterator __first, _BidirectionalIterator __last,
6008             const basic_regex<_CharT, _Traits>& __e,
6009             regex_constants::match_flag_type __flags = regex_constants::match_default)
6010{
6011    basic_string<_CharT> __s(__first, __last);
6012    match_results<const _CharT*> __mc;
6013    return __e.__search(__s.data(), __s.data() + __s.size(), __mc, __flags);
6014}
6015
6016template <class _CharT, class _Traits>
6017inline _LIBCPP_INLINE_VISIBILITY
6018bool
6019regex_search(const _CharT* __first, const _CharT* __last,
6020             const basic_regex<_CharT, _Traits>& __e,
6021             regex_constants::match_flag_type __flags = regex_constants::match_default)
6022{
6023    match_results<const _CharT*> __mc;
6024    return __e.__search(__first, __last, __mc, __flags);
6025}
6026
6027template <class _CharT, class _Allocator, class _Traits>
6028inline _LIBCPP_INLINE_VISIBILITY
6029bool
6030regex_search(const _CharT* __str, match_results<const _CharT*, _Allocator>& __m,
6031             const basic_regex<_CharT, _Traits>& __e,
6032             regex_constants::match_flag_type __flags = regex_constants::match_default)
6033{
6034    return __e.__search(__str, __str + _Traits::length(__str), __m, __flags);
6035}
6036
6037template <class _CharT, class _Traits>
6038inline _LIBCPP_INLINE_VISIBILITY
6039bool
6040regex_search(const _CharT* __str, const basic_regex<_CharT, _Traits>& __e,
6041             regex_constants::match_flag_type __flags = regex_constants::match_default)
6042{
6043    match_results<const _CharT*> __m;
6044    return _VSTD::regex_search(__str, __m, __e, __flags);
6045}
6046
6047template <class _ST, class _SA, class _CharT, class _Traits>
6048inline _LIBCPP_INLINE_VISIBILITY
6049bool
6050regex_search(const basic_string<_CharT, _ST, _SA>& __s,
6051             const basic_regex<_CharT, _Traits>& __e,
6052             regex_constants::match_flag_type __flags = regex_constants::match_default)
6053{
6054    match_results<const _CharT*> __mc;
6055    return __e.__search(__s.data(), __s.data() + __s.size(), __mc, __flags);
6056}
6057
6058template <class _ST, class _SA, class _Allocator, class _CharT, class _Traits>
6059inline _LIBCPP_INLINE_VISIBILITY
6060bool
6061regex_search(const basic_string<_CharT, _ST, _SA>& __s,
6062             match_results<typename basic_string<_CharT, _ST, _SA>::const_iterator, _Allocator>& __m,
6063             const basic_regex<_CharT, _Traits>& __e,
6064             regex_constants::match_flag_type __flags = regex_constants::match_default)
6065{
6066    match_results<const _CharT*> __mc;
6067    bool __r = __e.__search(__s.data(), __s.data() + __s.size(), __mc, __flags);
6068    __m.__assign(__s.begin(), __s.end(), __mc, __flags & regex_constants::__no_update_pos);
6069    return __r;
6070}
6071
6072#if _LIBCPP_STD_VER > 11
6073template <class _ST, class _SA, class _Ap, class _Cp, class _Tp>
6074bool
6075regex_search(const basic_string<_Cp, _ST, _SA>&& __s,
6076             match_results<typename basic_string<_Cp, _ST, _SA>::const_iterator, _Ap>&,
6077             const basic_regex<_Cp, _Tp>& __e,
6078             regex_constants::match_flag_type __flags = regex_constants::match_default) = delete;
6079#endif
6080
6081// regex_match
6082
6083template <class _BidirectionalIterator, class _Allocator, class _CharT, class _Traits>
6084bool
6085regex_match(_BidirectionalIterator __first, _BidirectionalIterator __last,
6086            match_results<_BidirectionalIterator, _Allocator>& __m,
6087            const basic_regex<_CharT, _Traits>& __e,
6088            regex_constants::match_flag_type __flags = regex_constants::match_default)
6089{
6090    bool __r = _VSTD::regex_search(
6091        __first, __last, __m, __e,
6092        __flags | regex_constants::match_continuous |
6093        regex_constants::__full_match);
6094    if (__r)
6095    {
6096        __r = !__m.suffix().matched;
6097        if (!__r)
6098            __m.__matches_.clear();
6099    }
6100    return __r;
6101}
6102
6103template <class _BidirectionalIterator, class _CharT, class _Traits>
6104inline _LIBCPP_INLINE_VISIBILITY
6105bool
6106regex_match(_BidirectionalIterator __first, _BidirectionalIterator __last,
6107            const basic_regex<_CharT, _Traits>& __e,
6108            regex_constants::match_flag_type __flags = regex_constants::match_default)
6109{
6110    match_results<_BidirectionalIterator> __m;
6111    return _VSTD::regex_match(__first, __last, __m, __e, __flags);
6112}
6113
6114template <class _CharT, class _Allocator, class _Traits>
6115inline _LIBCPP_INLINE_VISIBILITY
6116bool
6117regex_match(const _CharT* __str, match_results<const _CharT*, _Allocator>& __m,
6118            const basic_regex<_CharT, _Traits>& __e,
6119            regex_constants::match_flag_type __flags = regex_constants::match_default)
6120{
6121    return _VSTD::regex_match(__str, __str + _Traits::length(__str), __m, __e, __flags);
6122}
6123
6124template <class _ST, class _SA, class _Allocator, class _CharT, class _Traits>
6125inline _LIBCPP_INLINE_VISIBILITY
6126bool
6127regex_match(const basic_string<_CharT, _ST, _SA>& __s,
6128            match_results<typename basic_string<_CharT, _ST, _SA>::const_iterator, _Allocator>& __m,
6129            const basic_regex<_CharT, _Traits>& __e,
6130            regex_constants::match_flag_type __flags = regex_constants::match_default)
6131{
6132    return _VSTD::regex_match(__s.begin(), __s.end(), __m, __e, __flags);
6133}
6134
6135#if _LIBCPP_STD_VER > 11
6136template <class _ST, class _SA, class _Allocator, class _CharT, class _Traits>
6137inline _LIBCPP_INLINE_VISIBILITY
6138bool
6139regex_match(const basic_string<_CharT, _ST, _SA>&& __s,
6140            match_results<typename basic_string<_CharT, _ST, _SA>::const_iterator, _Allocator>& __m,
6141            const basic_regex<_CharT, _Traits>& __e,
6142            regex_constants::match_flag_type __flags = regex_constants::match_default) = delete;
6143#endif
6144
6145template <class _CharT, class _Traits>
6146inline _LIBCPP_INLINE_VISIBILITY
6147bool
6148regex_match(const _CharT* __str, const basic_regex<_CharT, _Traits>& __e,
6149            regex_constants::match_flag_type __flags = regex_constants::match_default)
6150{
6151    return _VSTD::regex_match(__str, __str + _Traits::length(__str), __e, __flags);
6152}
6153
6154template <class _ST, class _SA, class _CharT, class _Traits>
6155inline _LIBCPP_INLINE_VISIBILITY
6156bool
6157regex_match(const basic_string<_CharT, _ST, _SA>& __s,
6158            const basic_regex<_CharT, _Traits>& __e,
6159            regex_constants::match_flag_type __flags = regex_constants::match_default)
6160{
6161    return _VSTD::regex_match(__s.begin(), __s.end(), __e, __flags);
6162}
6163
6164// regex_iterator
6165
6166template <class _BidirectionalIterator,
6167          class _CharT = typename iterator_traits<_BidirectionalIterator>::value_type,
6168          class _Traits = regex_traits<_CharT> >
6169class _LIBCPP_TEMPLATE_VIS regex_iterator
6170{
6171public:
6172    typedef basic_regex<_CharT, _Traits>          regex_type;
6173    typedef match_results<_BidirectionalIterator> value_type;
6174    typedef ptrdiff_t                             difference_type;
6175    typedef const value_type*                     pointer;
6176    typedef const value_type&                     reference;
6177    typedef forward_iterator_tag                  iterator_category;
6178
6179private:
6180    _BidirectionalIterator           __begin_;
6181    _BidirectionalIterator           __end_;
6182    const regex_type*                __pregex_;
6183    regex_constants::match_flag_type __flags_;
6184    value_type                       __match_;
6185
6186public:
6187    regex_iterator();
6188    regex_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6189                   const regex_type& __re,
6190                   regex_constants::match_flag_type __m
6191                                              = regex_constants::match_default);
6192#if _LIBCPP_STD_VER > 11
6193    regex_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6194                   const regex_type&& __re,
6195                   regex_constants::match_flag_type __m
6196                                     = regex_constants::match_default) = delete;
6197#endif
6198
6199    bool operator==(const regex_iterator& __x) const;
6200    _LIBCPP_INLINE_VISIBILITY
6201    bool operator!=(const regex_iterator& __x) const {return !(*this == __x);}
6202
6203    _LIBCPP_INLINE_VISIBILITY
6204    reference operator*() const {return  __match_;}
6205    _LIBCPP_INLINE_VISIBILITY
6206    pointer operator->() const  {return _VSTD::addressof(__match_);}
6207
6208    regex_iterator& operator++();
6209    _LIBCPP_INLINE_VISIBILITY
6210    regex_iterator operator++(int)
6211    {
6212        regex_iterator __t(*this);
6213        ++(*this);
6214        return __t;
6215    }
6216};
6217
6218template <class _BidirectionalIterator, class _CharT, class _Traits>
6219regex_iterator<_BidirectionalIterator, _CharT, _Traits>::regex_iterator()
6220    : __begin_(), __end_(), __pregex_(nullptr), __flags_(), __match_()
6221{
6222}
6223
6224template <class _BidirectionalIterator, class _CharT, class _Traits>
6225regex_iterator<_BidirectionalIterator, _CharT, _Traits>::
6226    regex_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6227                   const regex_type& __re, regex_constants::match_flag_type __m)
6228    : __begin_(__a),
6229      __end_(__b),
6230      __pregex_(_VSTD::addressof(__re)),
6231      __flags_(__m)
6232{
6233    _VSTD::regex_search(__begin_, __end_, __match_, *__pregex_, __flags_);
6234}
6235
6236template <class _BidirectionalIterator, class _CharT, class _Traits>
6237bool
6238regex_iterator<_BidirectionalIterator, _CharT, _Traits>::
6239    operator==(const regex_iterator& __x) const
6240{
6241    if (__match_.empty() && __x.__match_.empty())
6242        return true;
6243    if (__match_.empty() || __x.__match_.empty())
6244        return false;
6245    return __begin_ == __x.__begin_       &&
6246           __end_ == __x.__end_           &&
6247           __pregex_ == __x.__pregex_     &&
6248           __flags_ == __x.__flags_       &&
6249           __match_[0] == __x.__match_[0];
6250}
6251
6252template <class _BidirectionalIterator, class _CharT, class _Traits>
6253regex_iterator<_BidirectionalIterator, _CharT, _Traits>&
6254regex_iterator<_BidirectionalIterator, _CharT, _Traits>::operator++()
6255{
6256    __flags_ |= regex_constants::__no_update_pos;
6257    _BidirectionalIterator __start = __match_[0].second;
6258    if (__match_[0].first == __match_[0].second)
6259    {
6260        if (__start == __end_)
6261        {
6262            __match_ = value_type();
6263            return *this;
6264        }
6265        else if (_VSTD::regex_search(__start, __end_, __match_, *__pregex_,
6266                                    __flags_ | regex_constants::match_not_null |
6267                                    regex_constants::match_continuous))
6268            return *this;
6269        else
6270            ++__start;
6271    }
6272    __flags_ |= regex_constants::match_prev_avail;
6273    if (!_VSTD::regex_search(__start, __end_, __match_, *__pregex_, __flags_))
6274        __match_ = value_type();
6275    return *this;
6276}
6277
6278typedef regex_iterator<const char*>             cregex_iterator;
6279typedef regex_iterator<const wchar_t*>          wcregex_iterator;
6280typedef regex_iterator<string::const_iterator>  sregex_iterator;
6281typedef regex_iterator<wstring::const_iterator> wsregex_iterator;
6282
6283// regex_token_iterator
6284
6285template <class _BidirectionalIterator,
6286          class _CharT = typename iterator_traits<_BidirectionalIterator>::value_type,
6287          class _Traits = regex_traits<_CharT> >
6288class _LIBCPP_TEMPLATE_VIS regex_token_iterator
6289{
6290public:
6291    typedef basic_regex<_CharT, _Traits>      regex_type;
6292    typedef sub_match<_BidirectionalIterator> value_type;
6293    typedef ptrdiff_t                         difference_type;
6294    typedef const value_type*                 pointer;
6295    typedef const value_type&                 reference;
6296    typedef forward_iterator_tag              iterator_category;
6297
6298private:
6299    typedef regex_iterator<_BidirectionalIterator, _CharT, _Traits> _Position;
6300
6301    _Position         __position_;
6302    const value_type* __result_;
6303    value_type        __suffix_;
6304    ptrdiff_t         __n_;
6305    vector<int>       __subs_;
6306
6307public:
6308    regex_token_iterator();
6309    regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6310                         const regex_type& __re, int __submatch = 0,
6311                         regex_constants::match_flag_type __m =
6312                                                regex_constants::match_default);
6313#if _LIBCPP_STD_VER > 11
6314    regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6315                         const regex_type&& __re, int __submatch = 0,
6316                         regex_constants::match_flag_type __m =
6317                                       regex_constants::match_default) = delete;
6318#endif
6319
6320    regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6321                         const regex_type& __re, const vector<int>& __submatches,
6322                         regex_constants::match_flag_type __m =
6323                                                regex_constants::match_default);
6324#if _LIBCPP_STD_VER > 11
6325    regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6326                         const regex_type&& __re, const vector<int>& __submatches,
6327                         regex_constants::match_flag_type __m =
6328                                     regex_constants::match_default) = delete;
6329#endif
6330
6331#ifndef _LIBCPP_CXX03_LANG
6332    regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6333                         const regex_type& __re,
6334                         initializer_list<int> __submatches,
6335                         regex_constants::match_flag_type __m =
6336                                                regex_constants::match_default);
6337
6338#if _LIBCPP_STD_VER > 11
6339    regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6340                         const regex_type&& __re,
6341                         initializer_list<int> __submatches,
6342                         regex_constants::match_flag_type __m =
6343                                       regex_constants::match_default) = delete;
6344#endif
6345#endif  // _LIBCPP_CXX03_LANG
6346    template <size_t _Np>
6347        regex_token_iterator(_BidirectionalIterator __a,
6348                             _BidirectionalIterator __b,
6349                             const regex_type& __re,
6350                             const int (&__submatches)[_Np],
6351                             regex_constants::match_flag_type __m =
6352                                                regex_constants::match_default);
6353#if _LIBCPP_STD_VER > 11
6354    template <std::size_t _Np>
6355        regex_token_iterator(_BidirectionalIterator __a,
6356                             _BidirectionalIterator __b,
6357                             const regex_type&& __re,
6358                             const int (&__submatches)[_Np],
6359                             regex_constants::match_flag_type __m =
6360                                      regex_constants::match_default) = delete;
6361#endif
6362
6363    regex_token_iterator(const regex_token_iterator&);
6364    regex_token_iterator& operator=(const regex_token_iterator&);
6365
6366    bool operator==(const regex_token_iterator& __x) const;
6367    _LIBCPP_INLINE_VISIBILITY
6368    bool operator!=(const regex_token_iterator& __x) const {return !(*this == __x);}
6369
6370    _LIBCPP_INLINE_VISIBILITY
6371    const value_type& operator*() const {return *__result_;}
6372    _LIBCPP_INLINE_VISIBILITY
6373    const value_type* operator->() const {return __result_;}
6374
6375    regex_token_iterator& operator++();
6376    _LIBCPP_INLINE_VISIBILITY
6377    regex_token_iterator operator++(int)
6378    {
6379        regex_token_iterator __t(*this);
6380        ++(*this);
6381        return __t;
6382    }
6383
6384private:
6385    void __init(_BidirectionalIterator __a, _BidirectionalIterator __b);
6386    void __establish_result () {
6387        if (__subs_[__n_] == -1)
6388            __result_ = &__position_->prefix();
6389        else
6390            __result_ = &(*__position_)[__subs_[__n_]];
6391        }
6392};
6393
6394template <class _BidirectionalIterator, class _CharT, class _Traits>
6395regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6396    regex_token_iterator()
6397    : __result_(nullptr),
6398      __suffix_(),
6399      __n_(0)
6400{
6401}
6402
6403template <class _BidirectionalIterator, class _CharT, class _Traits>
6404void
6405regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6406    __init(_BidirectionalIterator __a, _BidirectionalIterator __b)
6407{
6408    if (__position_ != _Position())
6409        __establish_result ();
6410    else if (__subs_[__n_] == -1)
6411    {
6412        __suffix_.matched = true;
6413        __suffix_.first = __a;
6414        __suffix_.second = __b;
6415        __result_ = &__suffix_;
6416    }
6417    else
6418        __result_ = nullptr;
6419}
6420
6421template <class _BidirectionalIterator, class _CharT, class _Traits>
6422regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6423    regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6424                         const regex_type& __re, int __submatch,
6425                         regex_constants::match_flag_type __m)
6426    : __position_(__a, __b, __re, __m),
6427      __n_(0),
6428      __subs_(1, __submatch)
6429{
6430    __init(__a, __b);
6431}
6432
6433template <class _BidirectionalIterator, class _CharT, class _Traits>
6434regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6435    regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6436                         const regex_type& __re, const vector<int>& __submatches,
6437                         regex_constants::match_flag_type __m)
6438    : __position_(__a, __b, __re, __m),
6439      __n_(0),
6440      __subs_(__submatches)
6441{
6442    __init(__a, __b);
6443}
6444
6445#ifndef _LIBCPP_CXX03_LANG
6446
6447template <class _BidirectionalIterator, class _CharT, class _Traits>
6448regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6449    regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6450                         const regex_type& __re,
6451                         initializer_list<int> __submatches,
6452                         regex_constants::match_flag_type __m)
6453    : __position_(__a, __b, __re, __m),
6454      __n_(0),
6455      __subs_(__submatches)
6456{
6457    __init(__a, __b);
6458}
6459
6460#endif  // _LIBCPP_CXX03_LANG
6461
6462template <class _BidirectionalIterator, class _CharT, class _Traits>
6463template <size_t _Np>
6464regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6465    regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6466                             const regex_type& __re,
6467                             const int (&__submatches)[_Np],
6468                             regex_constants::match_flag_type __m)
6469    : __position_(__a, __b, __re, __m),
6470      __n_(0),
6471      __subs_(begin(__submatches), end(__submatches))
6472{
6473    __init(__a, __b);
6474}
6475
6476template <class _BidirectionalIterator, class _CharT, class _Traits>
6477regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6478    regex_token_iterator(const regex_token_iterator& __x)
6479    : __position_(__x.__position_),
6480      __result_(__x.__result_),
6481      __suffix_(__x.__suffix_),
6482      __n_(__x.__n_),
6483      __subs_(__x.__subs_)
6484{
6485    if (__x.__result_ == &__x.__suffix_)
6486        __result_ = &__suffix_;
6487    else if ( __result_ != nullptr )
6488        __establish_result ();
6489}
6490
6491template <class _BidirectionalIterator, class _CharT, class _Traits>
6492regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>&
6493regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6494    operator=(const regex_token_iterator& __x)
6495{
6496    if (this != &__x)
6497    {
6498        __position_ = __x.__position_;
6499        if (__x.__result_ == &__x.__suffix_)
6500            __result_ = &__suffix_;
6501        else
6502            __result_ = __x.__result_;
6503        __suffix_ = __x.__suffix_;
6504        __n_ = __x.__n_;
6505        __subs_ = __x.__subs_;
6506
6507        if ( __result_ != nullptr && __result_ != &__suffix_ )
6508            __establish_result();
6509    }
6510    return *this;
6511}
6512
6513template <class _BidirectionalIterator, class _CharT, class _Traits>
6514bool
6515regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6516    operator==(const regex_token_iterator& __x) const
6517{
6518    if (__result_ == nullptr && __x.__result_ == nullptr)
6519        return true;
6520    if (__result_ == &__suffix_ && __x.__result_ == &__x.__suffix_ &&
6521            __suffix_ == __x.__suffix_)
6522        return true;
6523    if (__result_ == nullptr || __x.__result_ == nullptr)
6524        return false;
6525    if (__result_ == &__suffix_ || __x.__result_ == &__x.__suffix_)
6526        return false;
6527    return __position_ == __x.__position_ && __n_ == __x.__n_ &&
6528           __subs_ == __x.__subs_;
6529}
6530
6531template <class _BidirectionalIterator, class _CharT, class _Traits>
6532regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>&
6533regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::operator++()
6534{
6535    _Position __prev = __position_;
6536    if (__result_ == &__suffix_)
6537        __result_ = nullptr;
6538    else if (static_cast<size_t>(__n_ + 1) < __subs_.size())
6539    {
6540        ++__n_;
6541        __establish_result();
6542    }
6543    else
6544    {
6545        __n_ = 0;
6546        ++__position_;
6547        if (__position_ != _Position())
6548            __establish_result();
6549        else
6550        {
6551            if (_VSTD::find(__subs_.begin(), __subs_.end(), -1) != __subs_.end()
6552                && __prev->suffix().length() != 0)
6553            {
6554                __suffix_.matched = true;
6555                __suffix_.first = __prev->suffix().first;
6556                __suffix_.second = __prev->suffix().second;
6557                __result_ = &__suffix_;
6558            }
6559            else
6560                __result_ = nullptr;
6561        }
6562    }
6563    return *this;
6564}
6565
6566typedef regex_token_iterator<const char*>             cregex_token_iterator;
6567typedef regex_token_iterator<const wchar_t*>          wcregex_token_iterator;
6568typedef regex_token_iterator<string::const_iterator>  sregex_token_iterator;
6569typedef regex_token_iterator<wstring::const_iterator> wsregex_token_iterator;
6570
6571// regex_replace
6572
6573template <class _OutputIterator, class _BidirectionalIterator,
6574          class _Traits, class _CharT>
6575_OutputIterator
6576regex_replace(_OutputIterator __output_iter,
6577              _BidirectionalIterator __first, _BidirectionalIterator __last,
6578              const basic_regex<_CharT, _Traits>& __e, const _CharT* __fmt,
6579              regex_constants::match_flag_type __flags = regex_constants::match_default)
6580{
6581    typedef regex_iterator<_BidirectionalIterator, _CharT, _Traits> _Iter;
6582    _Iter __i(__first, __last, __e, __flags);
6583    _Iter __eof;
6584    if (__i == __eof)
6585    {
6586        if (!(__flags & regex_constants::format_no_copy))
6587            __output_iter = _VSTD::copy(__first, __last, __output_iter);
6588    }
6589    else
6590    {
6591        sub_match<_BidirectionalIterator> __lm;
6592        for (size_t __len = char_traits<_CharT>::length(__fmt); __i != __eof; ++__i)
6593        {
6594            if (!(__flags & regex_constants::format_no_copy))
6595                __output_iter = _VSTD::copy(__i->prefix().first, __i->prefix().second, __output_iter);
6596            __output_iter = __i->format(__output_iter, __fmt, __fmt + __len, __flags);
6597            __lm = __i->suffix();
6598            if (__flags & regex_constants::format_first_only)
6599                break;
6600        }
6601        if (!(__flags & regex_constants::format_no_copy))
6602            __output_iter = _VSTD::copy(__lm.first, __lm.second, __output_iter);
6603    }
6604    return __output_iter;
6605}
6606
6607template <class _OutputIterator, class _BidirectionalIterator,
6608          class _Traits, class _CharT, class _ST, class _SA>
6609inline _LIBCPP_INLINE_VISIBILITY
6610_OutputIterator
6611regex_replace(_OutputIterator __output_iter,
6612              _BidirectionalIterator __first, _BidirectionalIterator __last,
6613              const basic_regex<_CharT, _Traits>& __e,
6614              const basic_string<_CharT, _ST, _SA>& __fmt,
6615              regex_constants::match_flag_type __flags = regex_constants::match_default)
6616{
6617    return _VSTD::regex_replace(__output_iter, __first, __last, __e, __fmt.c_str(), __flags);
6618}
6619
6620template <class _Traits, class _CharT, class _ST, class _SA, class _FST,
6621          class _FSA>
6622inline _LIBCPP_INLINE_VISIBILITY
6623basic_string<_CharT, _ST, _SA>
6624regex_replace(const basic_string<_CharT, _ST, _SA>& __s,
6625              const basic_regex<_CharT, _Traits>& __e,
6626              const basic_string<_CharT, _FST, _FSA>& __fmt,
6627              regex_constants::match_flag_type __flags = regex_constants::match_default)
6628{
6629    basic_string<_CharT, _ST, _SA> __r;
6630    _VSTD::regex_replace(back_inserter(__r), __s.begin(), __s.end(), __e,
6631                        __fmt.c_str(), __flags);
6632    return __r;
6633}
6634
6635template <class _Traits, class _CharT, class _ST, class _SA>
6636inline _LIBCPP_INLINE_VISIBILITY
6637basic_string<_CharT, _ST, _SA>
6638regex_replace(const basic_string<_CharT, _ST, _SA>& __s,
6639              const basic_regex<_CharT, _Traits>& __e, const _CharT* __fmt,
6640              regex_constants::match_flag_type __flags = regex_constants::match_default)
6641{
6642    basic_string<_CharT, _ST, _SA> __r;
6643    _VSTD::regex_replace(back_inserter(__r), __s.begin(), __s.end(), __e,
6644                        __fmt, __flags);
6645    return __r;
6646}
6647
6648template <class _Traits, class _CharT, class _ST, class _SA>
6649inline _LIBCPP_INLINE_VISIBILITY
6650basic_string<_CharT>
6651regex_replace(const _CharT* __s,
6652              const basic_regex<_CharT, _Traits>& __e,
6653              const basic_string<_CharT, _ST, _SA>& __fmt,
6654              regex_constants::match_flag_type __flags = regex_constants::match_default)
6655{
6656    basic_string<_CharT> __r;
6657    _VSTD::regex_replace(back_inserter(__r), __s,
6658                        __s + char_traits<_CharT>::length(__s), __e,
6659                        __fmt.c_str(), __flags);
6660    return __r;
6661}
6662
6663template <class _Traits, class _CharT>
6664inline _LIBCPP_INLINE_VISIBILITY
6665basic_string<_CharT>
6666regex_replace(const _CharT* __s,
6667              const basic_regex<_CharT, _Traits>& __e,
6668              const _CharT* __fmt,
6669              regex_constants::match_flag_type __flags = regex_constants::match_default)
6670{
6671    basic_string<_CharT> __r;
6672    _VSTD::regex_replace(back_inserter(__r), __s,
6673                        __s + char_traits<_CharT>::length(__s), __e,
6674                        __fmt, __flags);
6675    return __r;
6676}
6677
6678_LIBCPP_END_NAMESPACE_STD
6679
6680_LIBCPP_POP_MACROS
6681
6682#endif  // _LIBCPP_REGEX
6683