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