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