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