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