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