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