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