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