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