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