blob: 75f70f0e0eb9a681c19d23aa0c68376a6f3cda17 [file] [log] [blame]
Howard Hinnant3257c982010-06-17 00:34:59 +00001// -*- C++ -*-
2//===--------------------------- regex ------------------------------------===//
3//
4// The LLVM Compiler Infrastructure
5//
6// This file is distributed under the University of Illinois Open Source
7// License. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_REGEX
12#define _LIBCPP_REGEX
13
14/*
15 regex synopsis
16
17#include <initializer_list>
18
19namespace std
20{
21
22namespace regex_constants
23{
24
25emum syntax_option_type
26{
27 icase = unspecified,
28 nosubs = unspecified,
29 optimize = unspecified,
30 collate = unspecified,
31 ECMAScript = unspecified,
32 basic = unspecified,
33 extended = unspecified,
34 awk = unspecified,
35 grep = unspecified,
36 egrep = unspecified
37};
38
39constexpr syntax_option_type operator~(syntax_option_type f);
40constexpr syntax_option_type operator&(syntax_option_type lhs, syntax_option_type rhs);
41constexpr syntax_option_type operator|(syntax_option_type lhs, syntax_option_type rhs);
42
43enum match_flag_type
44{
45 match_default = 0,
46 match_not_bol = unspecified,
47 match_not_eol = unspecified,
48 match_not_bow = unspecified,
49 match_not_eow = unspecified,
50 match_any = unspecified,
51 match_not_null = unspecified,
52 match_continuous = unspecified,
53 match_prev_avail = unspecified,
54 format_default = 0,
55 format_sed = unspecified,
56 format_no_copy = unspecified,
57 format_first_only = unspecified
58};
59
60constexpr match_flag_type operator~(match_flag_type f);
61constexpr match_flag_type operator&(match_flag_type lhs, match_flag_type rhs);
62constexpr match_flag_type operator|(match_flag_type lhs, match_flag_type rhs);
63
64enum error_type
65{
66 error_collate = unspecified,
67 error_ctype = unspecified,
68 error_escape = unspecified,
69 error_backref = unspecified,
70 error_brack = unspecified,
71 error_paren = unspecified,
72 error_brace = unspecified,
73 error_badbrace = unspecified,
74 error_range = unspecified,
75 error_space = unspecified,
76 error_badrepeat = unspecified,
77 error_complexity = unspecified,
78 error_stack = unspecified
79};
80
81} // regex_constants
82
83class regex_error
84 : public runtime_error
85{
86public:
87 explicit regex_error(regex_constants::error_type ecode);
88 regex_constants::error_type code() const;
89};
90
91template <class charT>
92struct regex_traits
93{
94public:
95 typedef charT char_type;
96 typedef basic_string<char_type> string_type;
97 typedef locale locale_type;
98 typedef /bitmask_type/ char_class_type;
99
100 regex_traits();
101
102 static size_t length(const char_type* p);
103 charT translate(charT c) const;
104 charT translate_nocase(charT c) const;
105 template <class ForwardIterator>
106 string_type
107 transform(ForwardIterator first, ForwardIterator last) const;
108 template <class ForwardIterator>
109 string_type
110 transform_primary( ForwardIterator first, ForwardIterator last) const;
111 template <class ForwardIterator>
112 string_type
113 lookup_collatename(ForwardIterator first, ForwardIterator last) const;
114 template <class ForwardIterator>
115 char_class_type
116 lookup_classname(ForwardIterator first, ForwardIterator last,
117 bool icase = false) const;
118 bool isctype(charT c, char_class_type f) const;
119 int value(charT ch, int radix) const;
120 locale_type imbue(locale_type l);
121 locale_type getloc()const;
122};
123
124template <class charT, class traits = regex_traits<charT>>
125class basic_regex
126{
127public:
128 // types:
129 typedef charT value_type;
130 typedef regex_constants::syntax_option_type flag_type;
131 typedef typename traits::locale_type locale_type;
132
133 // constants:
134 static constexpr regex_constants::syntax_option_type icase = regex_constants::icase;
135 static constexpr regex_constants::syntax_option_type nosubs = regex_constants::nosubs;
136 static constexpr regex_constants::syntax_option_type optimize = regex_constants::optimize;
137 static constexpr regex_constants::syntax_option_type collate = regex_constants::collate;
138 static constexpr regex_constants::syntax_option_type ECMAScript = regex_constants::ECMAScript;
139 static constexpr regex_constants::syntax_option_type basic = regex_constants::basic;
140 static constexpr regex_constants::syntax_option_type extended = regex_constants::extended;
141 static constexpr regex_constants::syntax_option_type awk = regex_constants::awk;
142 static constexpr regex_constants::syntax_option_type grep = regex_constants::grep;
143 static constexpr regex_constants::syntax_option_type egrep = regex_constants::egrep;
144
145 // construct/copy/destroy:
146 basic_regex();
147 explicit basic_regex(const charT* p, flag_type f = regex_constants::ECMAScript);
148 basic_regex(const charT* p, size_t len, flag_type f);
149 basic_regex(const basic_regex&);
150 basic_regex(basic_regex&&);
151 template <class ST, class SA>
152 explicit basic_regex(const basic_string<charT, ST, SA>& p,
153 flag_type f = regex_constants::ECMAScript);
154 template <class ForwardIterator>
155 basic_regex(ForwardIterator first, ForwardIterator last,
156 flag_type f = regex_constants::ECMAScript);
157 basic_regex(initializer_list<charT>, flag_type = regex_constants::ECMAScript);
158
159 ~basic_regex();
160
161 basic_regex& operator=(const basic_regex&);
162 basic_regex& operator=(basic_regex&&);
163 basic_regex& operator=(const charT* ptr);
164 basic_regex& operator=(initializer_list<charT> il);
165 template <class ST, class SA>
166 basic_regex& operator=(const basic_string<charT, ST, SA>& p);
167
168 // assign:
169 basic_regex& assign(const basic_regex& that);
170 basic_regex& assign(basic_regex&& that);
171 basic_regex& assign(const charT* ptr, flag_type f = regex_constants::ECMAScript);
172 basic_regex& assign(const charT* p, size_t len, flag_type f);
173 template <class string_traits, class A>
174 basic_regex& assign(const basic_string<charT, string_traits, A>& s,
175 flag_type f = regex_constants::ECMAScript);
176 template <class InputIterator>
177 basic_regex& assign(InputIterator first, InputIterator last,
178 flag_type f = regex_constants::ECMAScript);
179 basic_regex& assign(initializer_list<charT>, flag_type = regex_constants::ECMAScript);
180
181 // const operations:
182 unsigned mark_count() const;
183 flag_type flags() const;
184
185 // locale:
186 locale_type imbue(locale_type loc);
187 locale_type getloc() const;
188
189 // swap:
190 void swap(basic_regex&);
191};
192
193typedef basic_regex<char> regex;
194typedef basic_regex<wchar_t> wregex;
195
196template <class charT, class traits>
197 void swap(basic_regex<charT, traits>& e1, basic_regex<charT, traits>& e2);
198
199template <class BidirectionalIterator>
200class sub_match
201 : public pair<BidirectionalIterator, BidirectionalIterator>
202{
203public:
204 typedef typename iterator_traits<BidirectionalIterator>::value_type value_type;
205 typedef typename iterator_traits<BidirectionalIterator>::difference_type difference_type;
206 typedef BidirectionalIterator iterator;
207 typedef basic_string<value_type> string_type;
208
209 bool matched;
210
211 difference_type length() const;
212 operator string_type() const;
213 string_type str() const;
214
215 int compare(const sub_match& s) const;
216 int compare(const string_type& s) const;
217 int compare(const value_type* s) const;
218};
219
220typedef sub_match<const char*> csub_match;
221typedef sub_match<const wchar_t*> wcsub_match;
222typedef sub_match<string::const_iterator> ssub_match;
223typedef sub_match<wstring::const_iterator> wssub_match;
224
225template <class BiIter>
226 bool
227 operator==(const sub_match<BiIter>& lhs, const sub_match<BiIter>& rhs);
228
229template <class BiIter>
230 bool
231 operator!=(const sub_match<BiIter>& lhs, const sub_match<BiIter>& rhs);
232
233template <class BiIter>
234 bool
235 operator<(const sub_match<BiIter>& lhs, const sub_match<BiIter>& rhs);
236
237template <class BiIter>
238 bool
239 operator<=(const sub_match<BiIter>& lhs, const sub_match<BiIter>& rhs);
240
241template <class BiIter>
242 bool
243 operator>=(const sub_match<BiIter>& lhs, const sub_match<BiIter>& rhs);
244
245template <class BiIter>
246 bool
247 operator>(const sub_match<BiIter>& lhs, const sub_match<BiIter>& rhs);
248
249template <class BiIter, class ST, class SA>
250 bool
251 operator==(const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& lhs,
252 const sub_match<BiIter>& rhs);
253
254template <class BiIter, class ST, class SA>
255 bool
256 operator!=(const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& lhs,
257 const sub_match<BiIter>& rhs);
258
259template <class BiIter, class ST, class SA>
260 bool
261 operator<(const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& lhs,
262 const sub_match<BiIter>& rhs);
263
264template <class BiIter, class ST, class SA>
265 bool
266 operator>(const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& lhs,
267 const sub_match<BiIter>& rhs);
268
269template <class BiIter, class ST, class SA>
270 bool operator>=(const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& lhs,
271 const sub_match<BiIter>& rhs);
272
273template <class BiIter, class ST, class SA>
274 bool
275 operator<=(const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& lhs,
276 const sub_match<BiIter>& rhs);
277
278template <class BiIter, class ST, class SA>
279 bool
280 operator==(const sub_match<BiIter>& lhs,
281 const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& rhs);
282
283template <class BiIter, class ST, class SA>
284 bool
285 operator!=(const sub_match<BiIter>& lhs,
286 const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& rhs);
287
288template <class BiIter, class ST, class SA>
289 bool
290 operator<(const sub_match<BiIter>& lhs,
291 const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& rhs);
292
293template <class BiIter, class ST, class SA>
294 bool operator>(const sub_match<BiIter>& lhs,
295 const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& rhs);
296
297template <class BiIter, class ST, class SA>
298 bool
299 operator>=(const sub_match<BiIter>& lhs,
300 const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& rhs);
301
302template <class BiIter, class ST, class SA>
303 bool
304 operator<=(const sub_match<BiIter>& lhs,
305 const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& rhs);
306
307template <class BiIter>
308 bool
309 operator==(typename iterator_traits<BiIter>::value_type const* lhs,
310 const sub_match<BiIter>& rhs);
311
312template <class BiIter>
313 bool
314 operator!=(typename iterator_traits<BiIter>::value_type const* lhs,
315 const sub_match<BiIter>& rhs);
316
317template <class BiIter>
318 bool
319 operator<(typename iterator_traits<BiIter>::value_type const* lhs,
320 const sub_match<BiIter>& rhs);
321
322template <class BiIter>
323 bool
324 operator>(typename iterator_traits<BiIter>::value_type const* lhs,
325 const sub_match<BiIter>& rhs);
326
327template <class BiIter>
328 bool
329 operator>=(typename iterator_traits<BiIter>::value_type const* lhs,
330 const sub_match<BiIter>& rhs);
331
332template <class BiIter>
333 bool
334 operator<=(typename iterator_traits<BiIter>::value_type const* lhs,
335 const sub_match<BiIter>& rhs);
336
337template <class BiIter>
338 bool
339 operator==(const sub_match<BiIter>& lhs,
340 typename iterator_traits<BiIter>::value_type const* rhs);
341
342template <class BiIter>
343 bool
344 operator!=(const sub_match<BiIter>& lhs,
345 typename iterator_traits<BiIter>::value_type const* rhs);
346
347template <class BiIter>
348 bool
349 operator<(const sub_match<BiIter>& lhs,
350 typename iterator_traits<BiIter>::value_type const* rhs);
351
352template <class BiIter>
353 bool
354 operator>(const sub_match<BiIter>& lhs,
355 typename iterator_traits<BiIter>::value_type const* rhs);
356
357template <class BiIter>
358 bool
359 operator>=(const sub_match<BiIter>& lhs,
360 typename iterator_traits<BiIter>::value_type const* rhs);
361
362template <class BiIter>
363 bool
364 operator<=(const sub_match<BiIter>& lhs,
365 typename iterator_traits<BiIter>::value_type const* rhs);
366
367template <class BiIter>
368 bool
369 operator==(typename iterator_traits<BiIter>::value_type const& lhs,
370 const sub_match<BiIter>& rhs);
371
372template <class BiIter>
373 bool
374 operator!=(typename iterator_traits<BiIter>::value_type const& lhs,
375 const sub_match<BiIter>& rhs);
376
377template <class BiIter>
378 bool
379 operator<(typename iterator_traits<BiIter>::value_type const& lhs,
380 const sub_match<BiIter>& rhs);
381
382template <class BiIter>
383 bool
384 operator>(typename iterator_traits<BiIter>::value_type const& lhs,
385 const sub_match<BiIter>& rhs);
386
387template <class BiIter>
388 bool
389 operator>=(typename iterator_traits<BiIter>::value_type const& lhs,
390 const sub_match<BiIter>& rhs);
391
392template <class BiIter>
393 bool
394 operator<=(typename iterator_traits<BiIter>::value_type const& lhs,
395 const sub_match<BiIter>& rhs);
396
397template <class BiIter>
398 bool
399 operator==(const sub_match<BiIter>& lhs,
400 typename iterator_traits<BiIter>::value_type const& rhs);
401
402template <class BiIter>
403 bool
404 operator!=(const sub_match<BiIter>& lhs,
405 typename iterator_traits<BiIter>::value_type const& rhs);
406
407template <class BiIter>
408 bool
409 operator<(const sub_match<BiIter>& lhs,
410 typename iterator_traits<BiIter>::value_type const& rhs);
411
412template <class BiIter>
413 bool
414 operator>(const sub_match<BiIter>& lhs,
415 typename iterator_traits<BiIter>::value_type const& rhs);
416
417template <class BiIter>
418 bool
419 operator>=(const sub_match<BiIter>& lhs,
420 typename iterator_traits<BiIter>::value_type const& rhs);
421
422template <class BiIter>
423 bool
424 operator<=(const sub_match<BiIter>& lhs,
425 typename iterator_traits<BiIter>::value_type const& rhs);
426
427template <class charT, class ST, class BiIter>
428 basic_ostream<charT, ST>&
429 operator<<(basic_ostream<charT, ST>& os, const sub_match<BiIter>& m);
430
431template <class BidirectionalIterator,
432 class Allocator = allocator<sub_match<BidirectionalIterator>>>
433class match_results
434{
435public:
436 typedef sub_match<BidirectionalIterator> value_type;
437 typedef const value_type& const_reference;
438 typedef const_reference reference;
439 typedef /implementation-defined/ const_iterator;
440 typedef const_iterator iterator;
441 typedef typename iterator_traits<BidirectionalIterator>::difference_type difference_type;
442 typedef typename allocator_traits<Allocator>::size_type size_type;
443 typedef Allocator allocator_type;
444 typedef typename iterator_traits<BidirectionalIterator>::value_type char_type;
445 typedef basic_string<char_type> string_type;
446
447 // construct/copy/destroy:
448 explicit match_results(const Allocator& a = Allocator());
449 match_results(const match_results& m);
450 match_results(match_results&& m);
451 match_results& operator=(const match_results& m);
452 match_results& operator=(match_results&& m);
453 ~match_results();
454
455 // size:
456 size_type size() const;
457 size_type max_size() const;
458 bool empty() const;
459
460 // element access:
461 difference_type length(size_type sub = 0) const;
462 difference_type position(size_type sub = 0) const;
463 string_type str(size_type sub = 0) const;
464 const_reference operator[](size_type n) const;
465
466 const_reference prefix() const;
467 const_reference suffix() const;
468
469 const_iterator begin() const;
470 const_iterator end() const;
471 const_iterator cbegin() const;
472 const_iterator cend() const;
473
474 // format:
475 template <class OutputIter>
476 OutputIter
477 format(OutputIter out, const char_type* fmt_first,
478 const char_type* fmt_last,
479 regex_constants::match_flag_type flags = regex_constants::format_default) const;
480 template <class OutputIter, class ST, class SA>
481 OutputIter
482 format(OutputIter out, const basic_string<char_type, ST, SA>& fmt,
483 regex_constants::match_flag_type flags = regex_constants::format_default) const;
484 template <class ST, class SA>
485 basic_string<char_type, ST, SA>
486 format(const basic_string<char_type, ST, SA>& fmt,
487 regex_constants::match_flag_type flags = regex_constants::format_default) const;
488 string_type
489 format(const char_type* fmt,
490 regex_constants::match_flag_type flags = regex_constants::format_default) const;
491
492 // allocator:
493 allocator_type get_allocator() const;
494
495 // swap:
496 void swap(match_results& that);
497};
498
499typedef match_results<const char*> cmatch;
500typedef match_results<const wchar_t*> wcmatch;
501typedef match_results<string::const_iterator> smatch;
502typedef match_results<wstring::const_iterator> wsmatch;
503
504template <class BidirectionalIterator, class Allocator>
505 bool
506 operator==(const match_results<BidirectionalIterator, Allocator>& m1,
507 const match_results<BidirectionalIterator, Allocator>& m2);
508
509template <class BidirectionalIterator, class Allocator>
510 bool
511 operator!=(const match_results<BidirectionalIterator, Allocator>& m1,
512 const match_results<BidirectionalIterator, Allocator>& m2);
513
514template <class BidirectionalIterator, class Allocator>
515 void
516 swap(match_results<BidirectionalIterator, Allocator>& m1,
517 match_results<BidirectionalIterator, Allocator>& m2);
518
519template <class BidirectionalIterator, class Allocator, class charT, class traits>
520 bool
521 regex_match(BidirectionalIterator first, BidirectionalIterator last,
522 match_results<BidirectionalIterator, Allocator>& m,
523 const basic_regex<charT, traits>& e,
524 regex_constants::match_flag_type flags = regex_constants::match_default);
525
526template <class BidirectionalIterator, class charT, class traits>
527 bool
528 regex_match(BidirectionalIterator first, BidirectionalIterator last,
529 const basic_regex<charT, traits>& e,
530 regex_constants::match_flag_type flags = regex_constants::match_default);
531
532template <class charT, class Allocator, class traits>
533 bool
534 regex_match(const charT* str, match_results<const charT*, Allocator>& m,
535 const basic_regex<charT, traits>& e,
536 regex_constants::match_flag_type flags = regex_constants::match_default);
537
538template <class ST, class SA, class Allocator, class charT, class traits>
539 bool
540 regex_match(const basic_string<charT, ST, SA>& s,
541 match_results<typename basic_string<charT, ST, SA>::const_iterator, Allocator>& m,
542 const basic_regex<charT, traits>& e,
543 regex_constants::match_flag_type flags = regex_constants::match_default);
544
545template <class charT, class traits>
546 bool
547 regex_match(const charT* str, const basic_regex<charT, traits>& e,
548 regex_constants::match_flag_type flags = regex_constants::match_default);
549
550template <class ST, class SA, class charT, class traits>
551 bool
552 regex_match(const basic_string<charT, ST, SA>& s,
553 const basic_regex<charT, traits>& e,
554 regex_constants::match_flag_type flags = regex_constants::match_default);
555
556template <class BidirectionalIterator, class Allocator, class charT, class traits>
557 bool
558 regex_search(BidirectionalIterator first, BidirectionalIterator last,
559 match_results<BidirectionalIterator, Allocator>& m,
560 const basic_regex<charT, traits>& e,
561 regex_constants::match_flag_type flags = regex_constants::match_default);
562
563template <class BidirectionalIterator, class charT, class traits>
564 bool
565 regex_search(BidirectionalIterator first, BidirectionalIterator last,
566 const basic_regex<charT, traits>& e,
567 regex_constants::match_flag_type flags = regex_constants::match_default);
568
569template <class charT, class Allocator, class traits>
570 bool
571 regex_search(const charT* str, match_results<const charT*, Allocator>& m,
572 const basic_regex<charT, traits>& e,
573 regex_constants::match_flag_type flags = regex_constants::match_default);
574
575template <class charT, class traits>
576 bool
577 regex_search(const charT* str, const basic_regex<charT, traits>& e,
578 regex_constants::match_flag_type flags = regex_constants::match_default);
579
580template <class ST, class SA, class charT, class traits>
581 bool
582 regex_search(const basic_string<charT, ST, SA>& s,
583 const basic_regex<charT, traits>& e,
584 regex_constants::match_flag_type flags = regex_constants::match_default);
585
586template <class ST, class SA, class Allocator, class charT, class traits>
587 bool
588 regex_search(const basic_string<charT, ST, SA>& s,
589 match_results<typename basic_string<charT, ST, SA>::const_iterator, Allocator>& m,
590 const basic_regex<charT, traits>& e,
591 regex_constants::match_flag_type flags = regex_constants::match_default);
592
593template <class OutputIterator, class BidirectionalIterator,
594 class traits, class charT, class ST, class SA>
595 OutputIterator
596 regex_replace(OutputIterator out,
597 BidirectionalIterator first, BidirectionalIterator last,
598 const basic_regex<charT, traits>& e,
599 const basic_string<charT, ST, SA>& fmt,
600 regex_constants::match_flag_type flags = regex_constants::match_default);
601
602template <class OutputIterator, class BidirectionalIterator,
603 class traits, class charT>
604 OutputIterator
605 regex_replace(OutputIterator out,
606 BidirectionalIterator first, BidirectionalIterator last,
607 const basic_regex<charT, traits>& e, const charT* fmt,
608 regex_constants::match_flag_type flags = regex_constants::match_default);
609
610template <class traits, class charT, class ST, class SA, class FST, class FSA>>
611 basic_string<charT, ST, SA>
612 regex_replace(const basic_string<charT, ST, SA>& s,
613 const basic_regex<charT, traits>& e,
614 const basic_string<charT, FST, FSA>& fmt,
615 regex_constants::match_flag_type flags = regex_constants::match_default);
616
617template <class traits, class charT, class ST, class SA>
618 basic_string<charT, ST, SA>
619 regex_replace(const basic_string<charT, ST, SA>& s,
620 const basic_regex<charT, traits>& e, const charT* fmt,
621 regex_constants::match_flag_type flags = regex_constants::match_default);
622
623template <class traits, class charT, class ST, class SA>
624 basic_string<charT>
625 regex_replace(const charT* s,
626 const basic_regex<charT, traits>& e,
627 const basic_string<charT, ST, SA>& fmt,
628 regex_constants::match_flag_type flags = regex_constants::match_default);
629
630template <class traits, class charT>
631 basic_string<charT>
632 regex_replace(const charT* s,
633 const basic_regex<charT, traits>& e,
634 const charT* fmt,
635 regex_constants::match_flag_type flags = regex_constants::match_default);
636
637template <class BidirectionalIterator,
638 class charT = typename iterator_traits< BidirectionalIterator>::value_type,
639 class traits = regex_traits<charT>>
640class regex_iterator
641{
642public:
643 typedef basic_regex<charT, traits> regex_type;
644 typedef match_results<BidirectionalIterator> value_type;
645 typedef ptrdiff_t difference_type;
646 typedef const value_type* pointer;
647 typedef const value_type& reference;
648 typedef forward_iterator_tag iterator_category;
649
650 regex_iterator();
651 regex_iterator(BidirectionalIterator a, BidirectionalIterator b,
652 const regex_type& re,
653 regex_constants::match_flag_type m = regex_constants::match_default);
654 regex_iterator(const regex_iterator&);
655 regex_iterator& operator=(const regex_iterator&);
656
657 bool operator==(const regex_iterator&) const;
658 bool operator!=(const regex_iterator&) const;
659
660 const value_type& operator*() const;
661 const value_type* operator->() const;
662
663 regex_iterator& operator++();
664 regex_iterator operator++(int);
665};
666
667typedef regex_iterator<const char*> cregex_iterator;
668typedef regex_iterator<const wchar_t*> wcregex_iterator;
669typedef regex_iterator<string::const_iterator> sregex_iterator;
670typedef regex_iterator<wstring::const_iterator> wsregex_iterator;
671
672template <class BidirectionalIterator,
673 class charT = typename iterator_traits< BidirectionalIterator>::value_type,
674 class traits = regex_traits<charT>>
675class regex_token_iterator
676{
677public:
678 typedef basic_regex<charT, traits> regex_type;
679 typedef sub_match<BidirectionalIterator> value_type;
680 typedef ptrdiff_t difference_type;
681 typedef const value_type* pointer;
682 typedef const value_type& reference;
683 typedef forward_iterator_tag iterator_category;
684
685 regex_token_iterator();
686 regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b,
687 const regex_type& re, int submatch = 0,
688 regex_constants::match_flag_type m = regex_constants::match_default);
689 regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b,
690 const regex_type& re, const vector<int>& submatches,
691 regex_constants::match_flag_type m = regex_constants::match_default);
692 regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b,
693 const regex_type& re, initializer_list<int> submatches,
694 regex_constants::match_flag_type m = regex_constants::match_default);
695 template <size_t N>
696 regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b,
697 const regex_type& re, const int (&submatches)[N],
698 regex_constants::match_flag_type m = regex_constants::match_default);
699 regex_token_iterator(const regex_token_iterator&);
700 regex_token_iterator& operator=(const regex_token_iterator&);
701
702 bool operator==(const regex_token_iterator&) const;
703 bool operator!=(const regex_token_iterator&) const;
704
705 const value_type& operator*() const;
706 const value_type* operator->() const;
707
708 regex_token_iterator& operator++();
709 regex_token_iterator operator++(int);
710};
711
712typedef regex_token_iterator<const char*> cregex_token_iterator;
713typedef regex_token_iterator<const wchar_t*> wcregex_token_iterator;
714typedef regex_token_iterator<string::const_iterator> sregex_token_iterator;
715typedef regex_token_iterator<wstring::const_iterator> wsregex_token_iterator;
716
717} // std
718*/
719
Howard Hinnantac303862010-07-12 15:51:17 +0000720// temporary!
Howard Hinnante77aa5e2010-07-08 17:43:58 +0000721#include <sstream>
722#include <cassert>
723
Howard Hinnant3257c982010-06-17 00:34:59 +0000724#include <__config>
725#include <stdexcept>
726#include <__locale>
Howard Hinnant8c2c18d2010-06-24 21:28:00 +0000727#include <initializer_list>
Howard Hinnantcd85b9e2010-06-29 18:37:43 +0000728#include <utility>
729#include <iterator>
730#include <string>
Howard Hinnant7e9d84b2010-06-30 00:21:42 +0000731#include <memory>
732#include <vector>
Howard Hinnantac303862010-07-12 15:51:17 +0000733#include <deque>
Howard Hinnant3257c982010-06-17 00:34:59 +0000734
735#pragma GCC system_header
736
737_LIBCPP_BEGIN_NAMESPACE_STD
738
739namespace regex_constants
740{
741
742// syntax_option_type
743
744enum syntax_option_type
745{
746 icase = 1 << 0,
747 nosubs = 1 << 1,
748 optimize = 1 << 2,
749 collate = 1 << 3,
750 ECMAScript = 1 << 4,
751 basic = 1 << 5,
752 extended = 1 << 6,
753 awk = 1 << 7,
754 grep = 1 << 8,
755 egrep = 1 << 9
756};
757
758inline
759/*constexpr*/
760syntax_option_type
761operator~(syntax_option_type __x)
762{
763 return syntax_option_type(~int(__x));
764}
765
766inline
767/*constexpr*/
768syntax_option_type
769operator&(syntax_option_type __x, syntax_option_type __y)
770{
771 return syntax_option_type(int(__x) & int(__y));
772}
773
774inline
775/*constexpr*/
776syntax_option_type
777operator|(syntax_option_type __x, syntax_option_type __y)
778{
779 return syntax_option_type(int(__x) | int(__y));
780}
781
782inline
783/*constexpr*/
784syntax_option_type
785operator^(syntax_option_type __x, syntax_option_type __y)
786{
787 return syntax_option_type(int(__x) ^ int(__y));
788}
789
790inline
791/*constexpr*/
792syntax_option_type&
793operator&=(syntax_option_type& __x, syntax_option_type __y)
794{
795 __x = __x & __y;
796 return __x;
797}
798
799inline
800/*constexpr*/
801syntax_option_type&
802operator|=(syntax_option_type& __x, syntax_option_type __y)
803{
804 __x = __x | __y;
805 return __x;
806}
807
808inline
809/*constexpr*/
810syntax_option_type&
811operator^=(syntax_option_type& __x, syntax_option_type __y)
812{
813 __x = __x ^ __y;
814 return __x;
815}
816
817// match_flag_type
818
819enum match_flag_type
820{
821 match_default = 0,
822 match_not_bol = 1 << 0,
823 match_not_eol = 1 << 1,
824 match_not_bow = 1 << 2,
825 match_not_eow = 1 << 3,
826 match_any = 1 << 4,
827 match_not_null = 1 << 5,
828 match_continuous = 1 << 6,
829 match_prev_avail = 1 << 7,
830 format_default = 0,
831 format_sed = 1 << 8,
832 format_no_copy = 1 << 9,
833 format_first_only = 1 << 10
834};
835
836inline
837/*constexpr*/
838match_flag_type
839operator~(match_flag_type __x)
840{
841 return match_flag_type(~int(__x));
842}
843
844inline
845/*constexpr*/
846match_flag_type
847operator&(match_flag_type __x, match_flag_type __y)
848{
849 return match_flag_type(int(__x) & int(__y));
850}
851
852inline
853/*constexpr*/
854match_flag_type
855operator|(match_flag_type __x, match_flag_type __y)
856{
857 return match_flag_type(int(__x) | int(__y));
858}
859
860inline
861/*constexpr*/
862match_flag_type
863operator^(match_flag_type __x, match_flag_type __y)
864{
865 return match_flag_type(int(__x) ^ int(__y));
866}
867
868inline
869/*constexpr*/
870match_flag_type&
871operator&=(match_flag_type& __x, match_flag_type __y)
872{
873 __x = __x & __y;
874 return __x;
875}
876
877inline
878/*constexpr*/
879match_flag_type&
880operator|=(match_flag_type& __x, match_flag_type __y)
881{
882 __x = __x | __y;
883 return __x;
884}
885
886inline
887/*constexpr*/
888match_flag_type&
889operator^=(match_flag_type& __x, match_flag_type __y)
890{
891 __x = __x ^ __y;
892 return __x;
893}
894
895enum error_type
896{
897 error_collate = 1,
898 error_ctype,
899 error_escape,
900 error_backref,
901 error_brack,
902 error_paren,
903 error_brace,
904 error_badbrace,
905 error_range,
906 error_space,
907 error_badrepeat,
908 error_complexity,
Howard Hinnant8c2c18d2010-06-24 21:28:00 +0000909 error_stack,
910 error_temp
Howard Hinnant3257c982010-06-17 00:34:59 +0000911};
912
913} // regex_constants
914
915class _LIBCPP_EXCEPTION_ABI regex_error
916 : public runtime_error
917{
918 regex_constants::error_type __code_;
919public:
920 explicit regex_error(regex_constants::error_type __ecode);
921 virtual ~regex_error() throw();
922 regex_constants::error_type code() const {return __code_;}
923};
924
925template <class _CharT>
926struct regex_traits
927{
928public:
929 typedef _CharT char_type;
930 typedef basic_string<char_type> string_type;
931 typedef locale locale_type;
Howard Hinnantf409d2f2010-06-21 21:01:43 +0000932 typedef ctype_base::mask char_class_type;
Howard Hinnant3257c982010-06-17 00:34:59 +0000933
Howard Hinnantf409d2f2010-06-21 21:01:43 +0000934 static const char_class_type __regex_word = 0x80;
Howard Hinnant3257c982010-06-17 00:34:59 +0000935private:
936 locale __loc_;
937 const ctype<char_type>* __ct_;
938 const collate<char_type>* __col_;
939
940public:
941 regex_traits();
942
943 static size_t length(const char_type* __p)
944 {return char_traits<char_type>::length(__p);}
945 char_type translate(char_type __c) const {return __c;}
946 char_type translate_nocase(char_type __c) const;
947 template <class _ForwardIterator>
948 string_type
949 transform(_ForwardIterator __f, _ForwardIterator __l) const;
950 template <class _ForwardIterator>
951 string_type
952 transform_primary( _ForwardIterator __f, _ForwardIterator __l) const
953 {return __transform_primary(__f, __l, char_type());}
954 template <class _ForwardIterator>
955 string_type
956 lookup_collatename(_ForwardIterator __f, _ForwardIterator __l) const
957 {return __lookup_collatename(__f, __l, char_type());}
958 template <class _ForwardIterator>
959 char_class_type
960 lookup_classname(_ForwardIterator __f, _ForwardIterator __l,
Howard Hinnantf409d2f2010-06-21 21:01:43 +0000961 bool __icase = false) const
962 {return __lookup_classname(__f, __l, __icase, char_type());}
963 bool isctype(char_type __c, char_class_type __m) const;
964 int value(char_type __ch, int __radix) const
965 {return __value(__ch, __radix);}
Howard Hinnant3257c982010-06-17 00:34:59 +0000966 locale_type imbue(locale_type __l);
967 locale_type getloc()const {return __loc_;}
968
969private:
970 void __init();
971
972 template <class _ForwardIterator>
973 string_type
974 __transform_primary(_ForwardIterator __f, _ForwardIterator __l, char) const;
975 template <class _ForwardIterator>
976 string_type
977 __transform_primary(_ForwardIterator __f, _ForwardIterator __l, wchar_t) const;
978
979 template <class _ForwardIterator>
980 string_type
981 __lookup_collatename(_ForwardIterator __f, _ForwardIterator __l, char) const;
982 template <class _ForwardIterator>
983 string_type
984 __lookup_collatename(_ForwardIterator __f, _ForwardIterator __l, wchar_t) const;
Howard Hinnantf409d2f2010-06-21 21:01:43 +0000985
986 template <class _ForwardIterator>
987 char_class_type
988 __lookup_classname(_ForwardIterator __f, _ForwardIterator __l,
989 bool __icase, char) const;
990 template <class _ForwardIterator>
991 char_class_type
992 __lookup_classname(_ForwardIterator __f, _ForwardIterator __l,
993 bool __icase, wchar_t) const;
994
995 static int __value(unsigned char __ch, int __radix);
996 int __value(char __ch, int __radix) const
997 {return __value(static_cast<unsigned char>(__ch), __radix);}
998 int __value(wchar_t __ch, int __radix) const;
Howard Hinnant3257c982010-06-17 00:34:59 +0000999};
1000
1001template <class _CharT>
1002regex_traits<_CharT>::regex_traits()
1003{
1004 __init();
1005}
1006
1007template <class _CharT>
1008typename regex_traits<_CharT>::char_type
1009regex_traits<_CharT>::translate_nocase(char_type __c) const
1010{
1011 return __ct_->tolower(__c);
1012}
1013
1014template <class _CharT>
1015template <class _ForwardIterator>
1016typename regex_traits<_CharT>::string_type
1017regex_traits<_CharT>::transform(_ForwardIterator __f, _ForwardIterator __l) const
1018{
1019 string_type __s(__f, __l);
1020 return __col_->transform(__s.data(), __s.data() + __s.size());
1021}
1022
1023template <class _CharT>
1024void
1025regex_traits<_CharT>::__init()
1026{
1027 __ct_ = &use_facet<ctype<char_type> >(__loc_);
1028 __col_ = &use_facet<collate<char_type> >(__loc_);
1029}
1030
1031template <class _CharT>
1032typename regex_traits<_CharT>::locale_type
1033regex_traits<_CharT>::imbue(locale_type __l)
1034{
1035 locale __r = __loc_;
1036 __loc_ = __l;
1037 __init();
1038 return __r;
1039}
1040
1041// transform_primary is very FreeBSD-specific
1042
1043template <class _CharT>
1044template <class _ForwardIterator>
1045typename regex_traits<_CharT>::string_type
1046regex_traits<_CharT>::__transform_primary(_ForwardIterator __f,
1047 _ForwardIterator __l, char) const
1048{
1049 const string_type __s(__f, __l);
1050 string_type __d = __col_->transform(__s.data(), __s.data() + __s.size());
1051 switch (__d.size())
1052 {
1053 case 1:
1054 break;
1055 case 12:
1056 __d[11] = __d[3];
1057 break;
1058 default:
1059 __d.clear();
1060 break;
1061 }
1062 return __d;
1063}
1064
1065template <class _CharT>
1066template <class _ForwardIterator>
1067typename regex_traits<_CharT>::string_type
1068regex_traits<_CharT>::__transform_primary(_ForwardIterator __f,
1069 _ForwardIterator __l, wchar_t) const
1070{
1071 const string_type __s(__f, __l);
1072 string_type __d = __col_->transform(__s.data(), __s.data() + __s.size());
1073 switch (__d.size())
1074 {
1075 case 1:
1076 break;
1077 case 3:
1078 __d[2] = __d[0];
1079 break;
1080 default:
1081 __d.clear();
1082 break;
1083 }
1084 return __d;
1085}
1086
1087// lookup_collatename is very FreeBSD-specific
1088
Howard Hinnantf409d2f2010-06-21 21:01:43 +00001089string __get_collation_name(const char* __s);
Howard Hinnant3257c982010-06-17 00:34:59 +00001090
1091template <class _CharT>
1092template <class _ForwardIterator>
1093typename regex_traits<_CharT>::string_type
1094regex_traits<_CharT>::__lookup_collatename(_ForwardIterator __f,
1095 _ForwardIterator __l, char) const
1096{
1097 string_type __s(__f, __l);
1098 string_type __r;
1099 if (!__s.empty())
1100 {
1101 __r = __get_collation_name(__s.c_str());
1102 if (__r.empty() && __s.size() <= 2)
1103 {
1104 __r = __col_->transform(__s.data(), __s.data() + __s.size());
1105 if (__r.size() == 1 || __r.size() == 12)
1106 __r = __s;
1107 else
1108 __r.clear();
1109 }
1110 }
1111 return __r;
1112}
1113
1114template <class _CharT>
1115template <class _ForwardIterator>
1116typename regex_traits<_CharT>::string_type
1117regex_traits<_CharT>::__lookup_collatename(_ForwardIterator __f,
1118 _ForwardIterator __l, wchar_t) const
1119{
1120 string_type __s(__f, __l);
1121 string __n;
1122 __n.reserve(__s.size());
1123 for (typename string_type::const_iterator __i = __s.begin(), __e = __s.end();
1124 __i != __e; ++__i)
1125 {
1126 if (static_cast<unsigned>(*__i) >= 127)
1127 return string_type();
1128 __n.push_back(char(*__i));
1129 }
1130 string_type __r;
1131 if (!__s.empty())
1132 {
1133 __n = __get_collation_name(__n.c_str());
1134 if (!__n.empty())
1135 __r.assign(__n.begin(), __n.end());
1136 else if (__s.size() <= 2)
1137 {
1138 __r = __col_->transform(__s.data(), __s.data() + __s.size());
1139 if (__r.size() == 1 || __r.size() == 3)
1140 __r = __s;
1141 else
1142 __r.clear();
1143 }
1144 }
1145 return __r;
1146}
1147
Howard Hinnantf409d2f2010-06-21 21:01:43 +00001148// lookup_classname
1149
1150ctype_base::mask __get_classname(const char* __s, bool __icase);
1151
1152template <class _CharT>
1153template <class _ForwardIterator>
1154typename regex_traits<_CharT>::char_class_type
1155regex_traits<_CharT>::__lookup_classname(_ForwardIterator __f,
1156 _ForwardIterator __l,
1157 bool __icase, char) const
1158{
1159 string_type __s(__f, __l);
1160 __ct_->tolower(&__s[0], &__s[0] + __s.size());
1161 return __get_classname(__s.c_str(), __icase);
1162}
1163
1164template <class _CharT>
1165template <class _ForwardIterator>
1166typename regex_traits<_CharT>::char_class_type
1167regex_traits<_CharT>::__lookup_classname(_ForwardIterator __f,
1168 _ForwardIterator __l,
1169 bool __icase, wchar_t) const
1170{
1171 string_type __s(__f, __l);
1172 __ct_->tolower(&__s[0], &__s[0] + __s.size());
1173 string __n;
1174 __n.reserve(__s.size());
1175 for (typename string_type::const_iterator __i = __s.begin(), __e = __s.end();
1176 __i != __e; ++__i)
1177 {
1178 if (static_cast<unsigned>(*__i) >= 127)
1179 return char_class_type();
1180 __n.push_back(char(*__i));
1181 }
1182 return __get_classname(__n.c_str(), __icase);
1183}
1184
1185template <class _CharT>
1186bool
1187regex_traits<_CharT>::isctype(char_type __c, char_class_type __m) const
1188{
1189 if (__ct_->is(__m, __c))
1190 return true;
1191 return (__c == '_' && (__m & __regex_word));
1192}
1193
1194template <class _CharT>
1195int
1196regex_traits<_CharT>::__value(unsigned char __ch, int __radix)
1197{
1198 if ((__ch & 0xF8u) == 0x30) // '0' <= __ch && __ch <= '7'
1199 return __ch - '0';
1200 if (__radix != 8)
1201 {
1202 if ((__ch & 0xFEu) == 0x38) // '8' <= __ch && __ch <= '9'
1203 return __ch - '0';
1204 if (__radix == 16)
1205 {
1206 __ch |= 0x20; // tolower
1207 if ('a' <= __ch && __ch <= 'f')
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00001208 return __ch - ('a' - 10);
Howard Hinnantf409d2f2010-06-21 21:01:43 +00001209 }
1210 }
1211 return -1;
1212}
1213
1214template <class _CharT>
1215inline
1216int
1217regex_traits<_CharT>::__value(wchar_t __ch, int __radix) const
1218{
1219 return __value(static_cast<unsigned char>(__ct_->narrow(__ch, char_type())), __radix);
1220}
1221
Howard Hinnantac303862010-07-12 15:51:17 +00001222template <class _CharT> class __node;
1223
1224template <class _BidirectionalIterator> class sub_match;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001225
1226template <class _CharT>
Howard Hinnantac303862010-07-12 15:51:17 +00001227struct __state
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001228{
1229 enum
1230 {
1231 __end_state = -1000,
1232 __consume_input, // -999
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001233 __begin_marked_expr, // -998
1234 __end_marked_expr, // -997
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001235 __pop_state, // -996
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001236 __accept_and_consume, // -995
1237 __accept_but_not_consume, // -994
1238 __reject, // -993
Howard Hinnantac303862010-07-12 15:51:17 +00001239 __split,
1240 __repeat
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001241 };
1242
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001243 int __do_;
Howard Hinnantac303862010-07-12 15:51:17 +00001244 const _CharT* __first_;
1245 const _CharT* __current_;
1246 const _CharT* __last_;
1247 vector<sub_match<const _CharT*> > __sub_matches_;
1248 vector<pair<size_t, const _CharT*> > __loop_data_;
1249 const __node<_CharT>* __node_;
1250 regex_constants::match_flag_type __flags_;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001251
Howard Hinnantac303862010-07-12 15:51:17 +00001252 __state()
1253 : __do_(0), __first_(nullptr), __current_(nullptr), __last_(nullptr),
1254 __node_(nullptr), __flags_() {}
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001255};
1256
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001257template <class _CharT>
1258ostream&
Howard Hinnantac303862010-07-12 15:51:17 +00001259operator<<(ostream& os, const __state<_CharT>& c)
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001260{
1261 os << c.__do_;
Howard Hinnantac303862010-07-12 15:51:17 +00001262 if (c.__node_)
1263 os << ", " << c.__node_->speak();
1264else
1265 os << ", null";
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001266 return os;
1267}
1268
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001269
Howard Hinnantac303862010-07-12 15:51:17 +00001270// __node
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001271
1272template <class _CharT>
Howard Hinnantac303862010-07-12 15:51:17 +00001273class __node
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001274{
Howard Hinnantac303862010-07-12 15:51:17 +00001275 __node(const __node&);
1276 __node& operator=(const __node&);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001277public:
Howard Hinnantac303862010-07-12 15:51:17 +00001278 typedef _STD::__state<_CharT> __state;
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001279
Howard Hinnantac303862010-07-12 15:51:17 +00001280 __node() {}
1281 virtual ~__node() {}
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001282
Howard Hinnantac303862010-07-12 15:51:17 +00001283 virtual void __exec(__state&) const {};
1284 virtual void __exec_split(bool, __state&) const {};
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001285
Howard Hinnantac303862010-07-12 15:51:17 +00001286 virtual string speak() const {return "__node";}
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001287};
1288
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001289// __end_state
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001290
1291template <class _CharT>
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001292class __end_state
Howard Hinnantac303862010-07-12 15:51:17 +00001293 : public __node<_CharT>
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001294{
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001295public:
Howard Hinnantac303862010-07-12 15:51:17 +00001296 typedef _STD::__state<_CharT> __state;
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001297
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001298 __end_state() {}
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00001299
Howard Hinnantac303862010-07-12 15:51:17 +00001300 virtual void __exec(__state&) const;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001301
1302 virtual string speak() const {return "end state";}
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001303};
1304
1305template <class _CharT>
Howard Hinnantac303862010-07-12 15:51:17 +00001306void
1307__end_state<_CharT>::__exec(__state& __s) const
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00001308{
Howard Hinnantac303862010-07-12 15:51:17 +00001309 __s.__do_ = __state::__end_state;
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00001310}
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001311
1312// __has_one_state
1313
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00001314template <class _CharT>
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001315class __has_one_state
Howard Hinnantac303862010-07-12 15:51:17 +00001316 : public __node<_CharT>
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00001317{
Howard Hinnantac303862010-07-12 15:51:17 +00001318 __node<_CharT>* __first_;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001319
1320public:
Howard Hinnantac303862010-07-12 15:51:17 +00001321 explicit __has_one_state(__node<_CharT>* __s)
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001322 : __first_(__s) {}
1323
Howard Hinnantac303862010-07-12 15:51:17 +00001324 __node<_CharT>* first() const {return __first_;}
1325 __node<_CharT>*& first() {return __first_;}
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001326};
1327
1328// __owns_one_state
1329
1330template <class _CharT>
1331class __owns_one_state
1332 : public __has_one_state<_CharT>
1333{
1334 typedef __has_one_state<_CharT> base;
1335
1336public:
Howard Hinnantac303862010-07-12 15:51:17 +00001337 explicit __owns_one_state(__node<_CharT>* __s)
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001338 : base(__s) {}
1339
1340 virtual ~__owns_one_state();
1341};
1342
1343template <class _CharT>
1344__owns_one_state<_CharT>::~__owns_one_state()
1345{
1346 delete this->first();
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00001347}
1348
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001349// __empty_state
1350
1351template <class _CharT>
1352class __empty_state
1353 : public __owns_one_state<_CharT>
1354{
1355 typedef __owns_one_state<_CharT> base;
1356
1357public:
Howard Hinnantac303862010-07-12 15:51:17 +00001358 typedef _STD::__state<_CharT> __state;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001359
Howard Hinnantac303862010-07-12 15:51:17 +00001360 explicit __empty_state(__node<_CharT>* __s)
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001361 : base(__s) {}
1362
Howard Hinnantac303862010-07-12 15:51:17 +00001363 virtual void __exec(__state&) const;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001364
1365 virtual string speak() const {return "empty state";}
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001366};
1367
1368template <class _CharT>
Howard Hinnantac303862010-07-12 15:51:17 +00001369void
1370__empty_state<_CharT>::__exec(__state& __s) const
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001371{
Howard Hinnantac303862010-07-12 15:51:17 +00001372 __s.__do_ = __state::__accept_but_not_consume;
1373 __s.__node_ = this->first();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001374}
1375
1376// __empty_non_own_state
1377
1378template <class _CharT>
1379class __empty_non_own_state
1380 : public __has_one_state<_CharT>
1381{
1382 typedef __has_one_state<_CharT> base;
1383
1384public:
Howard Hinnantac303862010-07-12 15:51:17 +00001385 typedef _STD::__state<_CharT> __state;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001386
Howard Hinnantac303862010-07-12 15:51:17 +00001387 explicit __empty_non_own_state(__node<_CharT>* __s)
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001388 : base(__s) {}
1389
Howard Hinnantac303862010-07-12 15:51:17 +00001390 virtual void __exec(__state&) const;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001391
1392 virtual string speak() const {return "empty non-owning state";}
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001393};
1394
1395template <class _CharT>
Howard Hinnantac303862010-07-12 15:51:17 +00001396void
1397__empty_non_own_state<_CharT>::__exec(__state& __s) const
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001398{
Howard Hinnantac303862010-07-12 15:51:17 +00001399 __s.__do_ = __state::__accept_but_not_consume;
1400 __s.__node_ = this->first();
1401}
1402
1403// __repeat_one_loop
1404
1405template <class _CharT>
1406class __repeat_one_loop
1407 : public __has_one_state<_CharT>
1408{
1409 typedef __has_one_state<_CharT> base;
1410
1411public:
1412 typedef _STD::__state<_CharT> __state;
1413
1414 explicit __repeat_one_loop(__node<_CharT>* __s)
1415 : base(__s) {}
1416
1417 virtual void __exec(__state&) const;
1418
1419 virtual string speak() const {return "repeat loop";}
1420};
1421
1422template <class _CharT>
1423void
1424__repeat_one_loop<_CharT>::__exec(__state& __s) const
1425{
1426 __s.__do_ = __state::__repeat;
1427 __s.__node_ = this->first();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001428}
1429
1430// __owns_two_states
1431
1432template <class _CharT>
1433class __owns_two_states
1434 : public __owns_one_state<_CharT>
1435{
1436 typedef __owns_one_state<_CharT> base;
1437
1438 base* __second_;
1439
1440public:
Howard Hinnantac303862010-07-12 15:51:17 +00001441 explicit __owns_two_states(__node<_CharT>* __s1, base* __s2)
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001442 : base(__s1), __second_(__s2) {}
1443
1444 virtual ~__owns_two_states();
1445
1446 base* second() const {return __second_;}
1447 base*& second() {return __second_;}
1448};
1449
1450template <class _CharT>
1451__owns_two_states<_CharT>::~__owns_two_states()
1452{
1453 delete __second_;
1454}
1455
1456// __loop
1457
1458template <class _CharT>
1459class __loop
1460 : public __owns_two_states<_CharT>
1461{
1462 typedef __owns_two_states<_CharT> base;
1463
1464 size_t __min_;
1465 size_t __max_;
1466 unsigned __loop_id_;
Howard Hinnantac303862010-07-12 15:51:17 +00001467 unsigned __mexp_begin_;
1468 unsigned __mexp_end_;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001469 bool __greedy_;
1470
1471public:
Howard Hinnantac303862010-07-12 15:51:17 +00001472 typedef _STD::__state<_CharT> __state;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001473
1474 explicit __loop(unsigned __loop_id,
Howard Hinnantac303862010-07-12 15:51:17 +00001475 __node<_CharT>* __s1, __owns_one_state<_CharT>* __s2,
1476 unsigned __mexp_begin, unsigned __mexp_end,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001477 bool __greedy = true,
1478 size_t __min = 0,
1479 size_t __max = numeric_limits<size_t>::max())
1480 : base(__s1, __s2), __min_(__min), __max_(__max), __loop_id_(__loop_id),
Howard Hinnantac303862010-07-12 15:51:17 +00001481 __mexp_begin_(__mexp_begin), __mexp_end_(__mexp_end),
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001482 __greedy_(__greedy) {}
1483
Howard Hinnantac303862010-07-12 15:51:17 +00001484 virtual void __exec(__state& __s) const;
1485 virtual void __exec_split(bool __second, __state& __s) const;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001486
1487 virtual string speak() const
1488 {
1489 ostringstream os;
Howard Hinnantac303862010-07-12 15:51:17 +00001490 os << "loop "<< __loop_id_ << " {" << __min_ << ',' << __max_ << "}";
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001491 if (!__greedy_)
1492 os << " not";
1493 os << " greedy";
1494 return os.str();
1495 }
Howard Hinnantac303862010-07-12 15:51:17 +00001496
1497private:
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 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001508};
1509
1510template <class _CharT>
Howard Hinnantac303862010-07-12 15:51:17 +00001511void
1512__loop<_CharT>::__exec(__state& __s) const
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001513{
Howard Hinnantac303862010-07-12 15:51:17 +00001514 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 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001529 else
Howard Hinnantac303862010-07-12 15:51:17 +00001530 {
1531 __s.__do_ = __state::__accept_but_not_consume;
1532 __s.__node_ = this->second();
1533 }
1534 }
1535 else
1536 {
1537 if (__max_ > 0)
1538 __s.__do_ = __state::__split;
1539 else
1540 {
1541 __s.__do_ = __state::__accept_but_not_consume;
1542 __s.__node_ = this->second();
1543 }
1544 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001545}
1546
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001547template <class _CharT>
Howard Hinnantac303862010-07-12 15:51:17 +00001548void
1549__loop<_CharT>::__exec_split(bool __second, __state& __s) const
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001550{
Howard Hinnantac303862010-07-12 15:51:17 +00001551 __s.__do_ = __state::__accept_but_not_consume;
1552 if (__greedy_ != __second)
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001553 {
Howard Hinnantac303862010-07-12 15:51:17 +00001554 __s.__node_ = this->first();
1555 __init_repeat(__s);
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001556 }
Howard Hinnantac303862010-07-12 15:51:17 +00001557 else
1558 __s.__node_ = this->second();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001559}
1560
1561// __begin_marked_subexpression
1562
1563template <class _CharT>
1564class __begin_marked_subexpression
1565 : public __owns_one_state<_CharT>
1566{
1567 typedef __owns_one_state<_CharT> base;
1568
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001569 unsigned __mexp_;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001570public:
Howard Hinnantac303862010-07-12 15:51:17 +00001571 typedef _STD::__state<_CharT> __state;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001572
Howard Hinnantac303862010-07-12 15:51:17 +00001573 explicit __begin_marked_subexpression(unsigned __mexp, __node<_CharT>* __s)
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001574 : base(__s), __mexp_(__mexp) {}
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001575
Howard Hinnantac303862010-07-12 15:51:17 +00001576 virtual void __exec(__state&) const;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001577
1578 virtual string speak() const
1579 {
1580 ostringstream os;
1581 os << "begin marked expr " << __mexp_;
1582 return os.str();
1583 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001584};
1585
1586template <class _CharT>
Howard Hinnantac303862010-07-12 15:51:17 +00001587void
1588__begin_marked_subexpression<_CharT>::__exec(__state& __s) const
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001589{
Howard Hinnantac303862010-07-12 15:51:17 +00001590 __s.__do_ = __state::__accept_but_not_consume;
1591 __s.__sub_matches_[__mexp_-1].first = __s.__current_;
1592 __s.__node_ = this->first();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001593}
1594
1595// __end_marked_subexpression
1596
1597template <class _CharT>
1598class __end_marked_subexpression
1599 : public __owns_one_state<_CharT>
1600{
1601 typedef __owns_one_state<_CharT> base;
1602
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001603 unsigned __mexp_;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001604public:
Howard Hinnantac303862010-07-12 15:51:17 +00001605 typedef _STD::__state<_CharT> __state;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001606
Howard Hinnantac303862010-07-12 15:51:17 +00001607 explicit __end_marked_subexpression(unsigned __mexp, __node<_CharT>* __s)
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001608 : base(__s), __mexp_(__mexp) {}
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001609
Howard Hinnantac303862010-07-12 15:51:17 +00001610 virtual void __exec(__state&) const;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001611
1612 virtual string speak() const
1613 {
1614 ostringstream os;
1615 os << "end marked expr " << __mexp_;
1616 return os.str();
1617 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001618};
1619
1620template <class _CharT>
Howard Hinnantac303862010-07-12 15:51:17 +00001621void
1622__end_marked_subexpression<_CharT>::__exec(__state& __s) const
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001623{
Howard Hinnantac303862010-07-12 15:51:17 +00001624 __s.__do_ = __state::__accept_but_not_consume;
1625 __s.__sub_matches_[__mexp_-1].second = __s.__current_;
1626 __s.__sub_matches_[__mexp_-1].matched = true;
1627 __s.__node_ = this->first();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001628}
1629
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001630// __r_anchor
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001631
1632template <class _CharT>
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001633class __r_anchor
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001634 : public __owns_one_state<_CharT>
1635{
1636 typedef __owns_one_state<_CharT> base;
1637
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001638public:
Howard Hinnantac303862010-07-12 15:51:17 +00001639 typedef _STD::__state<_CharT> __state;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001640
Howard Hinnantac303862010-07-12 15:51:17 +00001641 __r_anchor(__node<_CharT>* __s)
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001642 : base(__s) {}
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001643
Howard Hinnantac303862010-07-12 15:51:17 +00001644 virtual void __exec(__state&) const;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001645
1646 virtual string speak() const
1647 {
1648 ostringstream os;
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001649 os << "right anchor";
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001650 return os.str();
1651 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001652};
1653
1654template <class _CharT>
Howard Hinnantac303862010-07-12 15:51:17 +00001655void
1656__r_anchor<_CharT>::__exec(__state& __s) const
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001657{
Howard Hinnantac303862010-07-12 15:51:17 +00001658 if (__s.__current_ == __s.__last_)
1659 {
1660 __s.__do_ = __state::__accept_but_not_consume;
1661 __s.__node_ = this->first();
1662 }
1663 else
1664 {
1665 __s.__do_ = __state::__reject;
1666 __s.__node_ = nullptr;
1667 }
1668}
1669
1670// __match_any
1671
1672template <class _CharT>
1673class __match_any
1674 : public __owns_one_state<_CharT>
1675{
1676 typedef __owns_one_state<_CharT> base;
1677
1678public:
1679 typedef _STD::__state<_CharT> __state;
1680
1681 __match_any(__node<_CharT>* __s)
1682 : base(__s) {}
1683
1684 virtual void __exec(__state&) const;
1685
1686 virtual string speak() const
1687 {
1688 ostringstream os;
1689 os << "match any";
1690 return os.str();
1691 }
1692};
1693
1694template <class _CharT>
1695void
1696__match_any<_CharT>::__exec(__state& __s) const
1697{
1698 if (__s.__current_ != __s.__last_ && *__s.__current_ != 0)
1699 {
1700 __s.__do_ = __state::__accept_and_consume;
1701 ++__s.__current_;
1702 __s.__node_ = this->first();
1703 }
1704 else
1705 {
1706 __s.__do_ = __state::__reject;
1707 __s.__node_ = nullptr;
1708 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001709}
1710
1711// __match_char
1712
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00001713template <class _CharT>
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001714class __match_char
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001715 : public __owns_one_state<_CharT>
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001716{
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001717 typedef __owns_one_state<_CharT> base;
1718
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001719 _CharT __c_;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001720
1721 __match_char(const __match_char&);
1722 __match_char& operator=(const __match_char&);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001723public:
Howard Hinnantac303862010-07-12 15:51:17 +00001724 typedef _STD::__state<_CharT> __state;
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00001725
Howard Hinnantac303862010-07-12 15:51:17 +00001726 __match_char(_CharT __c, __node<_CharT>* __s)
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001727 : base(__s), __c_(__c) {}
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001728
Howard Hinnantac303862010-07-12 15:51:17 +00001729 virtual void __exec(__state&) const;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001730
1731 virtual string speak() const
1732 {
1733 ostringstream os;
1734 os << "match char " << __c_;
1735 return os.str();
1736 }
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001737};
1738
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00001739template <class _CharT>
Howard Hinnantac303862010-07-12 15:51:17 +00001740void
1741__match_char<_CharT>::__exec(__state& __s) const
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00001742{
Howard Hinnantac303862010-07-12 15:51:17 +00001743 if (__s.__current_ != __s.__last_ && *__s.__current_ == __c_)
1744 {
1745 __s.__do_ = __state::__accept_and_consume;
1746 ++__s.__current_;
1747 __s.__node_ = this->first();
1748 }
1749 else
1750 {
1751 __s.__do_ = __state::__reject;
1752 __s.__node_ = nullptr;
1753 }
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00001754}
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00001755
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00001756template <class, class> class match_results;
1757
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00001758template <class _CharT, class _Traits = regex_traits<_CharT> >
1759class basic_regex
1760{
1761public:
1762 // types:
1763 typedef _CharT value_type;
1764 typedef regex_constants::syntax_option_type flag_type;
1765 typedef typename _Traits::locale_type locale_type;
1766
1767private:
1768 _Traits __traits_;
1769 flag_type __flags_;
1770 unsigned __marked_count_;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001771 unsigned __loop_count_;
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001772 int __open_count_;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001773 shared_ptr<__empty_state<_CharT> > __start_;
1774 __owns_one_state<_CharT>* __end_;
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001775 bool __left_anchor_;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001776
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001777 typedef _STD::__state<_CharT> __state;
Howard Hinnantac303862010-07-12 15:51:17 +00001778 typedef _STD::__node<_CharT> __node;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00001779
1780public:
1781 // constants:
1782 static const/*expr*/ regex_constants::syntax_option_type icase = regex_constants::icase;
1783 static const/*expr*/ regex_constants::syntax_option_type nosubs = regex_constants::nosubs;
1784 static const/*expr*/ regex_constants::syntax_option_type optimize = regex_constants::optimize;
1785 static const/*expr*/ regex_constants::syntax_option_type collate = regex_constants::collate;
1786 static const/*expr*/ regex_constants::syntax_option_type ECMAScript = regex_constants::ECMAScript;
1787 static const/*expr*/ regex_constants::syntax_option_type basic = regex_constants::basic;
1788 static const/*expr*/ regex_constants::syntax_option_type extended = regex_constants::extended;
1789 static const/*expr*/ regex_constants::syntax_option_type awk = regex_constants::awk;
1790 static const/*expr*/ regex_constants::syntax_option_type grep = regex_constants::grep;
1791 static const/*expr*/ regex_constants::syntax_option_type egrep = regex_constants::egrep;
1792
1793 // construct/copy/destroy:
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001794 basic_regex()
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001795 : __flags_(), __marked_count_(0), __loop_count_(0), __open_count_(0),
1796 __end_(0), __left_anchor_(false)
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001797 {}
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00001798 explicit basic_regex(const value_type* __p, flag_type __f = regex_constants::ECMAScript)
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001799 : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
1800 __end_(0), __left_anchor_(false)
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00001801 {__parse(__p, __p + __traits_.length(__p));}
1802 basic_regex(const value_type* __p, size_t __len, flag_type __f)
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001803 : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
1804 __end_(0), __left_anchor_(false)
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00001805 {__parse(__p, __p + __len);}
1806 basic_regex(const basic_regex&);
1807#ifdef _LIBCPP_MOVE
1808 basic_regex(basic_regex&&);
1809#endif
1810 template <class _ST, class _SA>
1811 explicit basic_regex(const basic_string<value_type, _ST, _SA>& __p,
1812 flag_type __f = regex_constants::ECMAScript)
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001813 : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
1814 __end_(0), __left_anchor_(false)
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00001815 {__parse(__p.begin(), __p.end());}
1816 template <class _ForwardIterator>
1817 basic_regex(_ForwardIterator __first, _ForwardIterator __last,
1818 flag_type __f = regex_constants::ECMAScript)
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001819 : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
1820 __end_(0), __left_anchor_(false)
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00001821 {__parse(__first, __last);}
1822 basic_regex(initializer_list<value_type> __il,
1823 flag_type __f = regex_constants::ECMAScript)
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001824 : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
1825 __end_(0), __left_anchor_(false)
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00001826 {__parse(__il.begin(), __il.end());}
1827
1828 ~basic_regex();
1829
1830 basic_regex& operator=(const basic_regex&);
1831#ifdef _LIBCPP_MOVE
1832 basic_regex& operator=(basic_regex&&);
1833#endif
1834 basic_regex& operator=(const value_type* __p);
1835 basic_regex& operator=(initializer_list<value_type> __il);
1836 template <class _ST, class _SA>
1837 basic_regex& operator=(const basic_string<value_type, _ST, _SA>& __p);
1838
1839 // assign:
1840 basic_regex& assign(const basic_regex& __that);
1841#ifdef _LIBCPP_MOVE
1842 basic_regex& assign(basic_regex&& __that);
1843#endif
1844 basic_regex& assign(const value_type* __p, flag_type __f = regex_constants::ECMAScript);
1845 basic_regex& assign(const value_type* __p, size_t __len, flag_type __f);
1846 template <class _ST, class _SA>
1847 basic_regex& assign(const basic_string<value_type, _ST, _SA>& __s,
1848 flag_type __f = regex_constants::ECMAScript);
1849 template <class _InputIterator>
1850 basic_regex& assign(_InputIterator __first, _InputIterator __last,
1851 flag_type __f = regex_constants::ECMAScript);
1852 basic_regex& assign(initializer_list<value_type> __il,
1853 flag_type = regex_constants::ECMAScript);
1854
1855 // const operations:
1856 unsigned mark_count() const {return __marked_count_;}
1857 flag_type flags() const {return __flags_;}
1858
1859 // locale:
1860 locale_type imbue(locale_type __loc) {return __traits_.imbue(__loc);}
1861 locale_type getloc() const {return __traits_.getloc();}
1862
1863 // swap:
1864 void swap(basic_regex&);
1865
1866private:
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001867 unsigned __loop_count() const {return __loop_count_;}
1868
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00001869 template <class _ForwardIterator>
1870 void __parse(_ForwardIterator __first, _ForwardIterator __last);
1871 template <class _ForwardIterator>
1872 _ForwardIterator
1873 __parse_basic_reg_exp(_ForwardIterator __first, _ForwardIterator __last);
1874 template <class _ForwardIterator>
1875 _ForwardIterator
1876 __parse_RE_expression(_ForwardIterator __first, _ForwardIterator __last);
1877 template <class _ForwardIterator>
1878 _ForwardIterator
1879 __parse_simple_RE(_ForwardIterator __first, _ForwardIterator __last);
1880 template <class _ForwardIterator>
1881 _ForwardIterator
1882 __parse_nondupl_RE(_ForwardIterator __first, _ForwardIterator __last);
1883 template <class _ForwardIterator>
1884 _ForwardIterator
1885 __parse_one_char_or_coll_elem_RE(_ForwardIterator __first, _ForwardIterator __last);
1886 template <class _ForwardIterator>
1887 _ForwardIterator
1888 __parse_Back_open_paren(_ForwardIterator __first, _ForwardIterator __last);
1889 template <class _ForwardIterator>
1890 _ForwardIterator
1891 __parse_Back_close_paren(_ForwardIterator __first, _ForwardIterator __last);
1892 template <class _ForwardIterator>
1893 _ForwardIterator
1894 __parse_Back_open_brace(_ForwardIterator __first, _ForwardIterator __last);
1895 template <class _ForwardIterator>
1896 _ForwardIterator
1897 __parse_Back_close_brace(_ForwardIterator __first, _ForwardIterator __last);
1898 template <class _ForwardIterator>
1899 _ForwardIterator
1900 __parse_BACKREF(_ForwardIterator __first, _ForwardIterator __last);
1901 template <class _ForwardIterator>
1902 _ForwardIterator
1903 __parse_ORD_CHAR(_ForwardIterator __first, _ForwardIterator __last);
1904 template <class _ForwardIterator>
1905 _ForwardIterator
1906 __parse_QUOTED_CHAR(_ForwardIterator __first, _ForwardIterator __last);
1907 template <class _ForwardIterator>
1908 _ForwardIterator
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001909 __parse_RE_dupl_symbol(_ForwardIterator __first, _ForwardIterator __last,
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001910 __owns_one_state<_CharT>* __s,
1911 unsigned __mexp_begin, unsigned __mexp_end);
Howard Hinnant0de86b62010-06-25 20:56:08 +00001912 template <class _ForwardIterator>
1913 _ForwardIterator
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001914 __parse_ERE_dupl_symbol(_ForwardIterator __first, _ForwardIterator __last);
1915 template <class _ForwardIterator>
1916 _ForwardIterator
Howard Hinnant0de86b62010-06-25 20:56:08 +00001917 __parse_bracket_expression(_ForwardIterator __first, _ForwardIterator __last);
1918 template <class _ForwardIterator>
1919 _ForwardIterator
1920 __parse_follow_list(_ForwardIterator __first, _ForwardIterator __last);
1921 template <class _ForwardIterator>
1922 _ForwardIterator
1923 __parse_expression_term(_ForwardIterator __first, _ForwardIterator __last);
1924 template <class _ForwardIterator>
1925 _ForwardIterator
1926 __parse_equivalence_class(_ForwardIterator __first, _ForwardIterator __last);
1927 template <class _ForwardIterator>
1928 _ForwardIterator
1929 __parse_character_class(_ForwardIterator __first, _ForwardIterator __last);
1930 template <class _ForwardIterator>
1931 _ForwardIterator
1932 __parse_collating_symbol(_ForwardIterator __first, _ForwardIterator __last);
1933 template <class _ForwardIterator>
1934 _ForwardIterator
1935 __parse_DUP_COUNT(_ForwardIterator __first, _ForwardIterator __last, int& __c);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001936 template <class _ForwardIterator>
1937 _ForwardIterator
1938 __parse_extended_reg_exp(_ForwardIterator __first, _ForwardIterator __last);
1939 template <class _ForwardIterator>
1940 _ForwardIterator
1941 __parse_ERE_branch(_ForwardIterator __first, _ForwardIterator __last);
1942 template <class _ForwardIterator>
1943 _ForwardIterator
1944 __parse_ERE_expression(_ForwardIterator __first, _ForwardIterator __last);
1945 template <class _ForwardIterator>
1946 _ForwardIterator
1947 __parse_one_char_or_coll_elem_ERE(_ForwardIterator __first, _ForwardIterator __last);
1948 template <class _ForwardIterator>
1949 _ForwardIterator
1950 __parse_ORD_CHAR_ERE(_ForwardIterator __first, _ForwardIterator __last);
1951 template <class _ForwardIterator>
1952 _ForwardIterator
1953 __parse_QUOTED_CHAR_ERE(_ForwardIterator __first, _ForwardIterator __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00001954
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001955 void __push_l_anchor() {__left_anchor_ = true;}
1956 void __push_r_anchor();
Howard Hinnantac303862010-07-12 15:51:17 +00001957 void __push_match_any();
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001958 void __push_greedy_inf_repeat(size_t __min, __owns_one_state<_CharT>* __s,
1959 unsigned __mexp_begin = 0, unsigned __mexp_end = 0)
1960 {__push_loop(__min, numeric_limits<size_t>::max(), __s,
1961 __mexp_begin, __mexp_end);}
Howard Hinnant0de86b62010-06-25 20:56:08 +00001962 void __push_exact_repeat(int __count) {}
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001963 void __push_loop(size_t __min, size_t __max, __owns_one_state<_CharT>* __s,
1964 size_t __mexp_begin = 0, size_t __mexp_end = 0,
1965 bool __greedy = true);
Howard Hinnant0de86b62010-06-25 20:56:08 +00001966 void __start_nonmatching_list() {}
1967 void __start_matching_list() {}
1968 void __end_nonmatching_list() {}
1969 void __end_matching_list() {}
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001970 void __push_char(value_type __c);
Howard Hinnant0de86b62010-06-25 20:56:08 +00001971 void __push_char(const typename _Traits::string_type& __c) {}
1972 void __push_range() {}
1973 void __push_class_type(typename _Traits::char_class_type) {}
1974 void __push_back_ref(int __i) {}
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001975 void __push_alternation() {}
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00001976 void __push_begin_marked_subexpression();
1977 void __push_end_marked_subexpression(unsigned);
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00001978
1979 template <class _BidirectionalIterator, class _Allocator>
1980 bool
1981 __search(_BidirectionalIterator __first, _BidirectionalIterator __last,
1982 match_results<_BidirectionalIterator, _Allocator>& __m,
1983 regex_constants::match_flag_type __flags) const;
1984
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001985 template <class _BidirectionalIterator, class _Allocator>
1986 bool
1987 __match_at_start(_BidirectionalIterator __first, _BidirectionalIterator __last,
1988 match_results<_BidirectionalIterator, _Allocator>& __m,
1989 vector<size_t>& __lc,
1990 regex_constants::match_flag_type __flags) const;
1991 template <class _BidirectionalIterator, class _Allocator>
1992 bool
1993 __match_at_start_ecma(_BidirectionalIterator __first, _BidirectionalIterator __last,
1994 match_results<_BidirectionalIterator, _Allocator>& __m,
1995 regex_constants::match_flag_type __flags) const;
1996 template <class _BidirectionalIterator, class _Allocator>
1997 bool
1998 __match_at_start_posix_nosubs(const _CharT* __first, const _CharT* __last,
1999 match_results<_BidirectionalIterator, _Allocator>& __m,
2000 vector<size_t>& __lc,
2001 regex_constants::match_flag_type __flags) const;
2002 template <class _BidirectionalIterator, class _Allocator>
2003 bool
2004 __match_at_start_posix_subs(_BidirectionalIterator __first, _BidirectionalIterator __last,
2005 match_results<_BidirectionalIterator, _Allocator>& __m,
Howard Hinnante77aa5e2010-07-08 17:43:58 +00002006 vector<size_t>& __lc,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002007 regex_constants::match_flag_type __flags) const;
2008
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00002009 template <class _B, class _A, class _C, class _T>
2010 friend
2011 bool
2012 regex_search(_B, _B, match_results<_B, _A>&, const basic_regex<_C, _T>&,
2013 regex_constants::match_flag_type);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002014
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002015};
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002016
2017template <class _CharT, class _Traits>
2018basic_regex<_CharT, _Traits>::~basic_regex()
2019{
2020}
2021
2022template <class _CharT, class _Traits>
2023template <class _ForwardIterator>
2024void
2025basic_regex<_CharT, _Traits>::__parse(_ForwardIterator __first,
2026 _ForwardIterator __last)
2027{
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002028 {
Howard Hinnantac303862010-07-12 15:51:17 +00002029 unique_ptr<__node> __h(new __end_state<_CharT>);
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002030 __start_.reset(new __empty_state<_CharT>(__h.get()));
2031 __h.release();
2032 __end_ = __start_.get();
2033 }
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002034 switch (__flags_ & 0x3F0)
2035 {
2036 case ECMAScript:
2037 break;
2038 case basic:
2039 __parse_basic_reg_exp(__first, __last);
2040 break;
2041 case extended:
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002042 __parse_extended_reg_exp(__first, __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002043 break;
2044 case awk:
2045 break;
2046 case grep:
2047 break;
2048 case egrep:
2049 break;
2050 default:
2051 throw regex_error(regex_constants::error_temp);
2052 }
2053}
2054
2055template <class _CharT, class _Traits>
2056template <class _ForwardIterator>
2057_ForwardIterator
2058basic_regex<_CharT, _Traits>::__parse_basic_reg_exp(_ForwardIterator __first,
2059 _ForwardIterator __last)
2060{
2061 if (__first != __last)
2062 {
2063 if (*__first == '^')
2064 {
2065 __push_l_anchor();
2066 ++__first;
2067 }
2068 if (__first != __last)
2069 {
2070 __first = __parse_RE_expression(__first, __last);
2071 if (__first != __last)
2072 {
2073 _ForwardIterator __temp = next(__first);
2074 if (__temp == __last && *__first == '$')
2075 {
2076 __push_r_anchor();
2077 ++__first;
2078 }
2079 }
2080 }
2081 if (__first != __last)
2082 throw regex_error(regex_constants::error_temp);
2083 }
2084 return __first;
2085}
2086
2087template <class _CharT, class _Traits>
2088template <class _ForwardIterator>
2089_ForwardIterator
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002090basic_regex<_CharT, _Traits>::__parse_extended_reg_exp(_ForwardIterator __first,
2091 _ForwardIterator __last)
2092{
2093 while (true)
2094 {
2095 _ForwardIterator __temp = __parse_ERE_branch(__first, __last);
2096 if (__temp == __first)
2097 throw regex_error(regex_constants::error_temp);
2098 __first = __temp;
2099 if (__first == __last)
2100 break;
2101 if (*__first != '|')
2102 throw regex_error(regex_constants::error_temp);
2103 __push_alternation();
2104 ++__first;
2105 }
2106 return __first;
2107}
2108
2109template <class _CharT, class _Traits>
2110template <class _ForwardIterator>
2111_ForwardIterator
2112basic_regex<_CharT, _Traits>::__parse_ERE_branch(_ForwardIterator __first,
2113 _ForwardIterator __last)
2114{
2115 _ForwardIterator __temp = __parse_ERE_expression(__first, __last);
2116 if (__temp == __first)
2117 throw regex_error(regex_constants::error_temp);
2118 do
2119 {
2120 __first = __temp;
2121 __temp = __parse_ERE_expression(__first, __last);
2122 } while (__temp != __first);
2123 return __first;
2124}
2125
2126template <class _CharT, class _Traits>
2127template <class _ForwardIterator>
2128_ForwardIterator
2129basic_regex<_CharT, _Traits>::__parse_ERE_expression(_ForwardIterator __first,
2130 _ForwardIterator __last)
2131{
2132 _ForwardIterator __temp = __parse_one_char_or_coll_elem_ERE(__first, __last);
2133 if (__temp == __first && __temp != __last)
2134 {
2135 switch (*__temp)
2136 {
2137 case '^':
2138 __push_l_anchor();
2139 ++__temp;
2140 break;
2141 case '$':
2142 __push_r_anchor();
2143 ++__temp;
2144 break;
2145 case '(':
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00002146 __push_begin_marked_subexpression();
2147 unsigned __temp_count = __marked_count_;
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002148 ++__open_count_;
2149 __temp = __parse_extended_reg_exp(++__temp, __last);
2150 if (__temp == __last || *__temp != ')')
2151 throw regex_error(regex_constants::error_paren);
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00002152 __push_end_marked_subexpression(__temp_count);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002153 --__open_count_;
2154 ++__temp;
2155 break;
2156 }
2157 }
2158 if (__temp != __first)
2159 __temp = __parse_ERE_dupl_symbol(__temp, __last);
2160 __first = __temp;
2161 return __first;
2162}
2163
2164template <class _CharT, class _Traits>
2165template <class _ForwardIterator>
2166_ForwardIterator
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002167basic_regex<_CharT, _Traits>::__parse_RE_expression(_ForwardIterator __first,
2168 _ForwardIterator __last)
2169{
2170 while (true)
2171 {
2172 _ForwardIterator __temp = __parse_simple_RE(__first, __last);
2173 if (__temp == __first)
2174 break;
2175 __first = __temp;
2176 }
2177 return __first;
2178}
2179
2180template <class _CharT, class _Traits>
2181template <class _ForwardIterator>
2182_ForwardIterator
2183basic_regex<_CharT, _Traits>::__parse_simple_RE(_ForwardIterator __first,
2184 _ForwardIterator __last)
2185{
2186 if (__first != __last)
2187 {
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002188 __owns_one_state<_CharT>* __e = __end_;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00002189 unsigned __mexp_begin = __marked_count_;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002190 _ForwardIterator __temp = __parse_nondupl_RE(__first, __last);
2191 if (__temp != __first)
Howard Hinnante77aa5e2010-07-08 17:43:58 +00002192 __first = __parse_RE_dupl_symbol(__temp, __last, __e,
2193 __mexp_begin+1, __marked_count_+1);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002194 }
2195 return __first;
2196}
2197
2198template <class _CharT, class _Traits>
2199template <class _ForwardIterator>
2200_ForwardIterator
2201basic_regex<_CharT, _Traits>::__parse_nondupl_RE(_ForwardIterator __first,
2202 _ForwardIterator __last)
2203{
2204 _ForwardIterator __temp = __first;
2205 __first = __parse_one_char_or_coll_elem_RE(__first, __last);
2206 if (__temp == __first)
2207 {
2208 __temp = __parse_Back_open_paren(__first, __last);
2209 if (__temp != __first)
2210 {
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00002211 __push_begin_marked_subexpression();
2212 unsigned __temp_count = __marked_count_;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002213 __first = __parse_RE_expression(__temp, __last);
2214 __temp = __parse_Back_close_paren(__first, __last);
2215 if (__temp == __first)
2216 throw regex_error(regex_constants::error_paren);
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00002217 __push_end_marked_subexpression(__temp_count);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002218 __first = __temp;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002219 }
2220 else
2221 __first = __parse_BACKREF(__first, __last);
2222 }
2223 return __first;
2224}
2225
2226template <class _CharT, class _Traits>
2227template <class _ForwardIterator>
2228_ForwardIterator
2229basic_regex<_CharT, _Traits>::__parse_one_char_or_coll_elem_RE(
2230 _ForwardIterator __first,
2231 _ForwardIterator __last)
2232{
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002233 _ForwardIterator __temp = __parse_ORD_CHAR(__first, __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002234 if (__temp == __first)
2235 {
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002236 __temp = __parse_QUOTED_CHAR(__first, __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002237 if (__temp == __first)
2238 {
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002239 if (__temp != __last && *__temp == '.')
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002240 {
2241 __push_match_any();
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002242 ++__temp;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002243 }
2244 else
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002245 __temp = __parse_bracket_expression(__first, __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002246 }
2247 }
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002248 __first = __temp;
2249 return __first;
2250}
2251
2252template <class _CharT, class _Traits>
2253template <class _ForwardIterator>
2254_ForwardIterator
2255basic_regex<_CharT, _Traits>::__parse_one_char_or_coll_elem_ERE(
2256 _ForwardIterator __first,
2257 _ForwardIterator __last)
2258{
2259 _ForwardIterator __temp = __parse_ORD_CHAR_ERE(__first, __last);
2260 if (__temp == __first)
2261 {
2262 __temp = __parse_QUOTED_CHAR_ERE(__first, __last);
2263 if (__temp == __first)
2264 {
2265 if (__temp != __last && *__temp == '.')
2266 {
2267 __push_match_any();
2268 ++__temp;
2269 }
2270 else
2271 __temp = __parse_bracket_expression(__first, __last);
2272 }
2273 }
2274 __first = __temp;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002275 return __first;
2276}
2277
2278template <class _CharT, class _Traits>
2279template <class _ForwardIterator>
2280_ForwardIterator
2281basic_regex<_CharT, _Traits>::__parse_Back_open_paren(_ForwardIterator __first,
2282 _ForwardIterator __last)
2283{
2284 if (__first != __last)
2285 {
2286 _ForwardIterator __temp = next(__first);
2287 if (__temp != __last)
2288 {
2289 if (*__first == '\\' && *__temp == '(')
2290 __first = ++__temp;
2291 }
2292 }
2293 return __first;
2294}
2295
2296template <class _CharT, class _Traits>
2297template <class _ForwardIterator>
2298_ForwardIterator
2299basic_regex<_CharT, _Traits>::__parse_Back_close_paren(_ForwardIterator __first,
2300 _ForwardIterator __last)
2301{
2302 if (__first != __last)
2303 {
2304 _ForwardIterator __temp = next(__first);
2305 if (__temp != __last)
2306 {
2307 if (*__first == '\\' && *__temp == ')')
2308 __first = ++__temp;
2309 }
2310 }
2311 return __first;
2312}
2313
2314template <class _CharT, class _Traits>
2315template <class _ForwardIterator>
2316_ForwardIterator
2317basic_regex<_CharT, _Traits>::__parse_Back_open_brace(_ForwardIterator __first,
2318 _ForwardIterator __last)
2319{
2320 if (__first != __last)
2321 {
2322 _ForwardIterator __temp = next(__first);
2323 if (__temp != __last)
2324 {
2325 if (*__first == '\\' && *__temp == '{')
2326 __first = ++__temp;
2327 }
2328 }
2329 return __first;
2330}
2331
2332template <class _CharT, class _Traits>
2333template <class _ForwardIterator>
2334_ForwardIterator
2335basic_regex<_CharT, _Traits>::__parse_Back_close_brace(_ForwardIterator __first,
2336 _ForwardIterator __last)
2337{
2338 if (__first != __last)
2339 {
2340 _ForwardIterator __temp = next(__first);
2341 if (__temp != __last)
2342 {
2343 if (*__first == '\\' && *__temp == '}')
2344 __first = ++__temp;
2345 }
2346 }
2347 return __first;
2348}
2349
2350template <class _CharT, class _Traits>
2351template <class _ForwardIterator>
2352_ForwardIterator
2353basic_regex<_CharT, _Traits>::__parse_BACKREF(_ForwardIterator __first,
2354 _ForwardIterator __last)
2355{
2356 if (__first != __last)
2357 {
2358 _ForwardIterator __temp = next(__first);
2359 if (__temp != __last)
2360 {
2361 if (*__first == '\\' && '1' <= *__temp && *__temp <= '9')
2362 {
2363 __push_back_ref(*__temp - '0');
2364 __first = ++__temp;
2365 }
2366 }
2367 }
2368 return __first;
2369}
2370
2371template <class _CharT, class _Traits>
2372template <class _ForwardIterator>
2373_ForwardIterator
2374basic_regex<_CharT, _Traits>::__parse_ORD_CHAR(_ForwardIterator __first,
2375 _ForwardIterator __last)
2376{
2377 if (__first != __last)
2378 {
2379 _ForwardIterator __temp = next(__first);
2380 if (__temp == __last && *__first == '$')
2381 return __first;
2382 // Not called inside a bracket
2383 if (*__first == '.' || *__first == '\\' || *__first == '[')
2384 return __first;
Howard Hinnant0de86b62010-06-25 20:56:08 +00002385 __push_char(*__first);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002386 ++__first;
2387 }
2388 return __first;
2389}
2390
2391template <class _CharT, class _Traits>
2392template <class _ForwardIterator>
2393_ForwardIterator
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002394basic_regex<_CharT, _Traits>::__parse_ORD_CHAR_ERE(_ForwardIterator __first,
2395 _ForwardIterator __last)
2396{
2397 if (__first != __last)
2398 {
2399 switch (*__first)
2400 {
2401 case '^':
2402 case '.':
2403 case '[':
2404 case '$':
2405 case '(':
2406 case '|':
2407 case '*':
2408 case '+':
2409 case '?':
2410 case '{':
2411 case '\\':
2412 break;
2413 case ')':
2414 if (__open_count_ == 0)
2415 {
2416 __push_char(*__first);
2417 ++__first;
2418 }
2419 break;
2420 default:
2421 __push_char(*__first);
2422 ++__first;
2423 break;
2424 }
2425 }
2426 return __first;
2427}
2428
2429template <class _CharT, class _Traits>
2430template <class _ForwardIterator>
2431_ForwardIterator
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002432basic_regex<_CharT, _Traits>::__parse_QUOTED_CHAR(_ForwardIterator __first,
2433 _ForwardIterator __last)
2434{
2435 if (__first != __last)
2436 {
2437 _ForwardIterator __temp = next(__first);
2438 if (__temp != __last)
2439 {
2440 if (*__first == '\\')
2441 {
2442 switch (*__temp)
2443 {
2444 case '^':
2445 case '.':
2446 case '*':
2447 case '[':
2448 case '$':
2449 case '\\':
Howard Hinnant0de86b62010-06-25 20:56:08 +00002450 __push_char(*__temp);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002451 __first = ++__temp;
2452 break;
2453 }
2454 }
2455 }
2456 }
2457 return __first;
2458}
2459
2460template <class _CharT, class _Traits>
2461template <class _ForwardIterator>
2462_ForwardIterator
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002463basic_regex<_CharT, _Traits>::__parse_QUOTED_CHAR_ERE(_ForwardIterator __first,
2464 _ForwardIterator __last)
2465{
2466 if (__first != __last)
2467 {
2468 _ForwardIterator __temp = next(__first);
2469 if (__temp != __last)
2470 {
2471 if (*__first == '\\')
2472 {
2473 switch (*__temp)
2474 {
2475 case '^':
2476 case '.':
2477 case '*':
2478 case '[':
2479 case '$':
2480 case '\\':
2481 case '(':
2482 case ')':
2483 case '|':
2484 case '+':
2485 case '?':
2486 case '{':
2487 __push_char(*__temp);
2488 __first = ++__temp;
2489 break;
2490 }
2491 }
2492 }
2493 }
2494 return __first;
2495}
2496
2497template <class _CharT, class _Traits>
2498template <class _ForwardIterator>
2499_ForwardIterator
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002500basic_regex<_CharT, _Traits>::__parse_RE_dupl_symbol(_ForwardIterator __first,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002501 _ForwardIterator __last,
Howard Hinnante77aa5e2010-07-08 17:43:58 +00002502 __owns_one_state<_CharT>* __s,
2503 unsigned __mexp_begin,
2504 unsigned __mexp_end)
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002505{
2506 if (__first != __last)
2507 {
Howard Hinnant0de86b62010-06-25 20:56:08 +00002508 if (*__first == '*')
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002509 {
Howard Hinnante77aa5e2010-07-08 17:43:58 +00002510 __push_greedy_inf_repeat(0, __s, __mexp_begin, __mexp_end);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002511 ++__first;
2512 }
2513 else
2514 {
2515 _ForwardIterator __temp = __parse_Back_open_brace(__first, __last);
2516 if (__temp != __first)
2517 {
2518 int __min = 0;
2519 __first = __temp;
2520 __temp = __parse_DUP_COUNT(__first, __last, __min);
2521 if (__temp == __first)
2522 throw regex_error(regex_constants::error_badbrace);
2523 __first = __temp;
2524 if (__first == __last)
2525 throw regex_error(regex_constants::error_brace);
2526 if (*__first != ',')
2527 {
2528 __temp = __parse_Back_close_brace(__first, __last);
2529 if (__temp == __first)
2530 throw regex_error(regex_constants::error_brace);
2531 __push_exact_repeat(__min);
2532 __first = __temp;
2533 }
2534 else
2535 {
2536 ++__first; // consume ','
2537 int __max = -1;
2538 __first = __parse_DUP_COUNT(__first, __last, __max);
2539 __temp = __parse_Back_close_brace(__first, __last);
2540 if (__temp == __first)
2541 throw regex_error(regex_constants::error_brace);
2542 if (__max == -1)
Howard Hinnante77aa5e2010-07-08 17:43:58 +00002543 __push_greedy_inf_repeat(__min, __s, __mexp_end, __mexp_end);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002544 else
2545 {
2546 if (__max < __min)
2547 throw regex_error(regex_constants::error_badbrace);
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002548 __push_loop(__min, __max, __s);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002549 }
2550 __first = __temp;
2551 }
2552 }
2553 }
2554 }
2555 return __first;
2556}
2557
Howard Hinnant0de86b62010-06-25 20:56:08 +00002558template <class _CharT, class _Traits>
2559template <class _ForwardIterator>
2560_ForwardIterator
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002561basic_regex<_CharT, _Traits>::__parse_ERE_dupl_symbol(_ForwardIterator __first,
2562 _ForwardIterator __last)
2563{
2564 if (__first != __last)
2565 {
2566 switch (*__first)
2567 {
2568 case '*':
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002569 __push_greedy_inf_repeat(0, nullptr);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002570 ++__first;
2571 break;
2572 case '+':
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002573 __push_greedy_inf_repeat(1, nullptr);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002574 ++__first;
2575 break;
2576 case '?':
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002577 __push_loop(0, 1, nullptr);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002578 ++__first;
2579 break;
2580 case '{':
2581 {
2582 int __min;
2583 _ForwardIterator __temp = __parse_DUP_COUNT(__first, __last, __min);
2584 if (__temp == __first)
2585 throw regex_error(regex_constants::error_badbrace);
2586 __first = __temp;
2587 if (__first == __last)
2588 throw regex_error(regex_constants::error_brace);
2589 switch (*__first)
2590 {
2591 case '}':
2592 __push_exact_repeat(__min);
2593 ++__first;
2594 break;
2595 case ',':
2596 if (++__first == __last)
2597 throw regex_error(regex_constants::error_badbrace);
2598 if (*__first == '}')
2599 {
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002600 __push_greedy_inf_repeat(__min, nullptr);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002601 ++__first;
2602 }
2603 else
2604 {
2605 int __max;
2606 __temp = __parse_DUP_COUNT(__first, __last, __max);
2607 if (__temp == __first)
2608 throw regex_error(regex_constants::error_brace);
2609 __first = __temp;
2610 if (__first == __last || *__first != '}')
2611 throw regex_error(regex_constants::error_brace);
2612 ++__first;
2613 if (__max < __min)
2614 throw regex_error(regex_constants::error_badbrace);
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002615 __push_loop(__min, __max, nullptr);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002616 }
2617 default:
2618 throw regex_error(regex_constants::error_badbrace);
2619 }
2620 }
2621 break;
2622 }
2623 }
2624 return __first;
2625}
2626
2627template <class _CharT, class _Traits>
2628template <class _ForwardIterator>
2629_ForwardIterator
Howard Hinnant0de86b62010-06-25 20:56:08 +00002630basic_regex<_CharT, _Traits>::__parse_bracket_expression(_ForwardIterator __first,
2631 _ForwardIterator __last)
2632{
2633 if (__first != __last && *__first == '[')
2634 {
2635 if (++__first == __last)
2636 throw regex_error(regex_constants::error_brack);
2637 bool __non_matching = false;
2638 if (*__first == '^')
2639 {
2640 ++__first;
2641 __non_matching = true;
2642 __start_nonmatching_list();
2643 }
2644 else
2645 __start_matching_list();
2646 if (__first == __last)
2647 throw regex_error(regex_constants::error_brack);
2648 if (*__first == ']')
2649 {
2650 __push_char(']');
2651 ++__first;
2652 }
2653 __first = __parse_follow_list(__first, __last);
2654 if (__first == __last)
2655 throw regex_error(regex_constants::error_brack);
2656 if (*__first == '-')
2657 {
2658 __push_char('-');
2659 ++__first;
2660 }
2661 if (__first == __last || *__first != ']')
2662 throw regex_error(regex_constants::error_brack);
2663 if (__non_matching)
2664 __end_nonmatching_list();
2665 else
2666 __end_matching_list();
2667 ++__first;
2668 }
2669 return __first;
2670}
2671
2672template <class _CharT, class _Traits>
2673template <class _ForwardIterator>
2674_ForwardIterator
2675basic_regex<_CharT, _Traits>::__parse_follow_list(_ForwardIterator __first,
2676 _ForwardIterator __last)
2677{
2678 if (__first != __last)
2679 {
2680 while (true)
2681 {
2682 _ForwardIterator __temp = __parse_expression_term(__first, __last);
2683 if (__temp == __first)
2684 break;
2685 __first = __temp;
2686 }
2687 }
2688 return __first;
2689}
2690
2691template <class _CharT, class _Traits>
2692template <class _ForwardIterator>
2693_ForwardIterator
2694basic_regex<_CharT, _Traits>::__parse_expression_term(_ForwardIterator __first,
2695 _ForwardIterator __last)
2696{
2697 if (__first != __last && *__first != ']')
2698 {
2699 bool __parsed_one = false;
2700 _ForwardIterator __temp = next(__first);
2701 if (__temp != __last && *__first == '[')
2702 {
2703 if (*__temp == '=')
2704 return __parse_equivalence_class(++__temp, __last);
2705 else if (*__temp == ':')
2706 return __parse_character_class(++__temp, __last);
2707 else if (*__temp == '.')
2708 {
2709 __first = __parse_collating_symbol(++__temp, __last);
2710 __parsed_one = true;
2711 }
2712 }
2713 if (!__parsed_one)
2714 {
2715 __push_char(*__first);
2716 ++__first;
2717 }
2718 if (__first != __last && *__first != ']')
2719 {
2720 __temp = next(__first);
2721 if (__temp != __last && *__first == '-' && *__temp != ']')
2722 {
2723 // parse a range
2724 __first = __temp;
2725 ++__temp;
2726 if (__temp != __last && *__first == '[' && *__temp == '.')
2727 __first = __parse_collating_symbol(++__temp, __last);
2728 else
2729 {
2730 __push_char(*__first);
2731 ++__first;
2732 }
2733 __push_range();
2734 }
2735 }
2736 }
2737 return __first;
2738}
2739
2740template <class _CharT, class _Traits>
2741template <class _ForwardIterator>
2742_ForwardIterator
2743basic_regex<_CharT, _Traits>::__parse_equivalence_class(_ForwardIterator __first,
2744 _ForwardIterator __last)
2745{
2746 // Found [=
2747 // This means =] must exist
2748 value_type _Equal_close[2] = {'=', ']'};
2749 _ForwardIterator __temp = _STD::search(__first, __last, _Equal_close,
2750 _Equal_close+2);
2751 if (__temp == __last)
2752 throw regex_error(regex_constants::error_brack);
2753 // [__first, __temp) contains all text in [= ... =]
2754 typedef typename _Traits::string_type string_type;
2755 string_type __collate_name =
2756 __traits_.lookup_collatename(__first, __temp);
2757 if (__collate_name.empty())
2758 throw regex_error(regex_constants::error_brack);
2759 string_type __equiv_name =
2760 __traits_.transform_primary(__collate_name.begin(),
2761 __collate_name.end());
2762 if (!__equiv_name.empty())
2763 __push_char(__equiv_name);
2764 else
2765 __push_char(__collate_name);
2766 __first = next(__temp, 2);
2767 return __first;
2768}
2769
2770template <class _CharT, class _Traits>
2771template <class _ForwardIterator>
2772_ForwardIterator
2773basic_regex<_CharT, _Traits>::__parse_character_class(_ForwardIterator __first,
2774 _ForwardIterator __last)
2775{
2776 // Found [:
2777 // This means :] must exist
2778 value_type _Colon_close[2] = {':', ']'};
2779 _ForwardIterator __temp = _STD::search(__first, __last, _Colon_close,
2780 _Colon_close+2);
2781 if (__temp == __last)
2782 throw regex_error(regex_constants::error_brack);
2783 // [__first, __temp) contains all text in [: ... :]
2784 typedef typename _Traits::char_class_type char_class_type;
2785 char_class_type __class_type =
2786 __traits_.lookup_classname(__first, __temp, __flags_ & icase);
2787 if (__class_type == 0)
2788 throw regex_error(regex_constants::error_brack);
2789 __push_class_type(__class_type);
2790 __first = next(__temp, 2);
2791 return __first;
2792}
2793
2794template <class _CharT, class _Traits>
2795template <class _ForwardIterator>
2796_ForwardIterator
2797basic_regex<_CharT, _Traits>::__parse_collating_symbol(_ForwardIterator __first,
2798 _ForwardIterator __last)
2799{
2800 // Found [.
2801 // This means .] must exist
2802 value_type _Dot_close[2] = {'.', ']'};
2803 _ForwardIterator __temp = _STD::search(__first, __last, _Dot_close,
2804 _Dot_close+2);
2805 if (__temp == __last)
2806 throw regex_error(regex_constants::error_brack);
2807 // [__first, __temp) contains all text in [. ... .]
2808 typedef typename _Traits::string_type string_type;
2809 string_type __collate_name =
2810 __traits_.lookup_collatename(__first, __temp);
2811 if (__collate_name.empty())
2812 throw regex_error(regex_constants::error_brack);
2813 __push_char(__collate_name);
2814 __first = next(__temp, 2);
2815 return __first;
2816}
2817
2818template <class _CharT, class _Traits>
2819template <class _ForwardIterator>
2820_ForwardIterator
2821basic_regex<_CharT, _Traits>::__parse_DUP_COUNT(_ForwardIterator __first,
2822 _ForwardIterator __last,
2823 int& __c)
2824{
2825 if (__first != __last && '0' <= *__first && *__first <= '9')
2826 {
2827 __c = *__first - '0';
2828 for (++__first; __first != __last && '0' <= *__first && *__first <= '9';
2829 ++__first)
2830 {
2831 __c *= 10;
2832 __c += *__first - '0';
2833 }
2834 }
2835 return __first;
2836}
2837
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002838template <class _CharT, class _Traits>
2839void
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002840basic_regex<_CharT, _Traits>::__push_loop(size_t __min, size_t __max,
2841 __owns_one_state<_CharT>* __s, size_t __mexp_begin, size_t __mexp_end,
2842 bool __greedy)
2843{
2844 unique_ptr<__empty_state<_CharT> > __e1(new __empty_state<_CharT>(__end_->first()));
2845 __end_->first() = nullptr;
Howard Hinnantac303862010-07-12 15:51:17 +00002846 unique_ptr<__loop<_CharT> > __e2(new __loop<_CharT>(__loop_count_,
2847 __s->first(), __e1.get(), __mexp_begin, __mexp_end, __greedy,
2848 __min, __max));
2849 __s->first() = nullptr;
2850 __e1.release();
2851 __end_->first() = new __repeat_one_loop<_CharT>(__e2.get());
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002852 __end_ = __e2->second();
Howard Hinnantac303862010-07-12 15:51:17 +00002853 __s->first() = __e2.release();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002854 ++__loop_count_;
2855}
2856
2857template <class _CharT, class _Traits>
2858void
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002859basic_regex<_CharT, _Traits>::__push_char(value_type __c)
2860{
Howard Hinnante77aa5e2010-07-08 17:43:58 +00002861 __end_->first() = new __match_char<_CharT>(__c, __end_->first());
2862 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002863}
2864
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00002865template <class _CharT, class _Traits>
2866void
2867basic_regex<_CharT, _Traits>::__push_begin_marked_subexpression()
2868{
Howard Hinnante77aa5e2010-07-08 17:43:58 +00002869 __end_->first() = new __begin_marked_subexpression<_CharT>(++__marked_count_,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002870 __end_->first());
Howard Hinnante77aa5e2010-07-08 17:43:58 +00002871 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00002872}
2873
2874template <class _CharT, class _Traits>
2875void
2876basic_regex<_CharT, _Traits>::__push_end_marked_subexpression(unsigned __sub)
2877{
Howard Hinnante77aa5e2010-07-08 17:43:58 +00002878 __end_->first() = new __end_marked_subexpression<_CharT>(__sub,
2879 __end_->first());
2880 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00002881}
2882
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00002883template <class _CharT, class _Traits>
2884void
2885basic_regex<_CharT, _Traits>::__push_r_anchor()
2886{
2887 __end_->first() = new __r_anchor<_CharT>(__end_->first());
2888 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
2889}
2890
Howard Hinnantac303862010-07-12 15:51:17 +00002891template <class _CharT, class _Traits>
2892void
2893basic_regex<_CharT, _Traits>::__push_match_any()
2894{
2895 __end_->first() = new __match_any<_CharT>(__end_->first());
2896 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
2897}
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00002898
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002899typedef basic_regex<char> regex;
2900typedef basic_regex<wchar_t> wregex;
2901
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002902// sub_match
2903
2904template <class _BidirectionalIterator>
2905class sub_match
2906 : public pair<_BidirectionalIterator, _BidirectionalIterator>
2907{
2908public:
2909 typedef _BidirectionalIterator iterator;
2910 typedef typename iterator_traits<iterator>::value_type value_type;
2911 typedef typename iterator_traits<iterator>::difference_type difference_type;
2912 typedef basic_string<value_type> string_type;
2913
2914 bool matched;
2915
2916 difference_type length() const
2917 {return matched ? _STD::distance(this->first, this->second) : 0;}
2918 string_type str() const
2919 {return matched ? string_type(this->first, this->second) : string_type();}
2920 operator string_type() const
2921 {return str();}
2922
2923 int compare(const sub_match& __s) const
2924 {return str().compare(__s.str());}
2925 int compare(const string_type& __s) const
2926 {return str().compare(__s);}
2927 int compare(const value_type* __s) const
2928 {return str().compare(__s);}
2929};
2930
2931typedef sub_match<const char*> csub_match;
2932typedef sub_match<const wchar_t*> wcsub_match;
2933typedef sub_match<string::const_iterator> ssub_match;
2934typedef sub_match<wstring::const_iterator> wssub_match;
2935
2936template <class _BiIter>
2937inline _LIBCPP_INLINE_VISIBILITY
2938bool
2939operator==(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
2940{
2941 return __x.compare(__y) == 0;
2942}
2943
2944template <class _BiIter>
2945inline _LIBCPP_INLINE_VISIBILITY
2946bool
2947operator!=(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
2948{
2949 return !(__x == __y);
2950}
2951
2952template <class _BiIter>
2953inline _LIBCPP_INLINE_VISIBILITY
2954bool
2955operator<(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
2956{
2957 return __x.compare(__y) < 0;
2958}
2959
2960template <class _BiIter>
2961inline _LIBCPP_INLINE_VISIBILITY
2962bool
2963operator<=(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
2964{
2965 return !(__y < __x);
2966}
2967
2968template <class _BiIter>
2969inline _LIBCPP_INLINE_VISIBILITY
2970bool
2971operator>=(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
2972{
2973 return !(__x < __y);
2974}
2975
2976template <class _BiIter>
2977inline _LIBCPP_INLINE_VISIBILITY
2978bool
2979operator>(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
2980{
2981 return __y < __x;
2982}
2983
2984template <class _BiIter, class _ST, class _SA>
2985inline _LIBCPP_INLINE_VISIBILITY
2986bool
2987operator==(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
2988 const sub_match<_BiIter>& __y)
2989{
2990 return __y.compare(__x.c_str()) == 0;
2991}
2992
2993template <class _BiIter, class _ST, class _SA>
2994inline _LIBCPP_INLINE_VISIBILITY
2995bool
2996operator!=(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
2997 const sub_match<_BiIter>& __y)
2998{
2999 return !(__x == __y);
3000}
3001
3002template <class _BiIter, class _ST, class _SA>
3003inline _LIBCPP_INLINE_VISIBILITY
3004bool
3005operator<(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
3006 const sub_match<_BiIter>& __y)
3007{
3008 return __y.compare(__x.c_str()) > 0;
3009}
3010
3011template <class _BiIter, class _ST, class _SA>
3012inline _LIBCPP_INLINE_VISIBILITY
3013bool
3014operator>(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
3015 const sub_match<_BiIter>& __y)
3016{
3017 return __y < __x;
3018}
3019
3020template <class _BiIter, class _ST, class _SA>
3021inline _LIBCPP_INLINE_VISIBILITY
3022bool operator>=(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
3023 const sub_match<_BiIter>& __y)
3024{
3025 return !(__x < __y);
3026}
3027
3028template <class _BiIter, class _ST, class _SA>
3029inline _LIBCPP_INLINE_VISIBILITY
3030bool
3031operator<=(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
3032 const sub_match<_BiIter>& __y)
3033{
3034 return !(__y < __x);
3035}
3036
3037template <class _BiIter, class _ST, class _SA>
3038inline _LIBCPP_INLINE_VISIBILITY
3039bool
3040operator==(const sub_match<_BiIter>& __x,
3041 const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
3042{
3043 return __x.compare(__y.c_str()) == 0;
3044}
3045
3046template <class _BiIter, class _ST, class _SA>
3047inline _LIBCPP_INLINE_VISIBILITY
3048bool
3049operator!=(const sub_match<_BiIter>& __x,
3050 const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
3051{
3052 return !(__x == __y);
3053}
3054
3055template <class _BiIter, class _ST, class _SA>
3056inline _LIBCPP_INLINE_VISIBILITY
3057bool
3058operator<(const sub_match<_BiIter>& __x,
3059 const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
3060{
3061 return __x.compare(__y.c_str()) < 0;
3062}
3063
3064template <class _BiIter, class _ST, class _SA>
3065inline _LIBCPP_INLINE_VISIBILITY
3066bool operator>(const sub_match<_BiIter>& __x,
3067 const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
3068{
3069 return __y < __x;
3070}
3071
3072template <class _BiIter, class _ST, class _SA>
3073inline _LIBCPP_INLINE_VISIBILITY
3074bool
3075operator>=(const sub_match<_BiIter>& __x,
3076 const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
3077{
3078 return !(__x < __y);
3079}
3080
3081template <class _BiIter, class _ST, class _SA>
3082inline _LIBCPP_INLINE_VISIBILITY
3083bool
3084operator<=(const sub_match<_BiIter>& __x,
3085 const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
3086{
3087 return !(__y < __x);
3088}
3089
3090template <class _BiIter>
3091inline _LIBCPP_INLINE_VISIBILITY
3092bool
3093operator==(typename iterator_traits<_BiIter>::value_type const* __x,
3094 const sub_match<_BiIter>& __y)
3095{
3096 return __y.compare(__x) == 0;
3097}
3098
3099template <class _BiIter>
3100inline _LIBCPP_INLINE_VISIBILITY
3101bool
3102operator!=(typename iterator_traits<_BiIter>::value_type const* __x,
3103 const sub_match<_BiIter>& __y)
3104{
3105 return !(__x == __y);
3106}
3107
3108template <class _BiIter>
3109inline _LIBCPP_INLINE_VISIBILITY
3110bool
3111operator<(typename iterator_traits<_BiIter>::value_type const* __x,
3112 const sub_match<_BiIter>& __y)
3113{
3114 return __y.compare(__x) > 0;
3115}
3116
3117template <class _BiIter>
3118inline _LIBCPP_INLINE_VISIBILITY
3119bool
3120operator>(typename iterator_traits<_BiIter>::value_type const* __x,
3121 const sub_match<_BiIter>& __y)
3122{
3123 return __y < __x;
3124}
3125
3126template <class _BiIter>
3127inline _LIBCPP_INLINE_VISIBILITY
3128bool
3129operator>=(typename iterator_traits<_BiIter>::value_type const* __x,
3130 const sub_match<_BiIter>& __y)
3131{
3132 return !(__x < __y);
3133}
3134
3135template <class _BiIter>
3136inline _LIBCPP_INLINE_VISIBILITY
3137bool
3138operator<=(typename iterator_traits<_BiIter>::value_type const* __x,
3139 const sub_match<_BiIter>& __y)
3140{
3141 return !(__y < __x);
3142}
3143
3144template <class _BiIter>
3145inline _LIBCPP_INLINE_VISIBILITY
3146bool
3147operator==(const sub_match<_BiIter>& __x,
3148 typename iterator_traits<_BiIter>::value_type const* __y)
3149{
3150 return __x.compare(__y) == 0;
3151}
3152
3153template <class _BiIter>
3154inline _LIBCPP_INLINE_VISIBILITY
3155bool
3156operator!=(const sub_match<_BiIter>& __x,
3157 typename iterator_traits<_BiIter>::value_type const* __y)
3158{
3159 return !(__x == __y);
3160}
3161
3162template <class _BiIter>
3163inline _LIBCPP_INLINE_VISIBILITY
3164bool
3165operator<(const sub_match<_BiIter>& __x,
3166 typename iterator_traits<_BiIter>::value_type const* __y)
3167{
3168 return __x.compare(__y) < 0;
3169}
3170
3171template <class _BiIter>
3172inline _LIBCPP_INLINE_VISIBILITY
3173bool
3174operator>(const sub_match<_BiIter>& __x,
3175 typename iterator_traits<_BiIter>::value_type const* __y)
3176{
3177 return __y < __x;
3178}
3179
3180template <class _BiIter>
3181inline _LIBCPP_INLINE_VISIBILITY
3182bool
3183operator>=(const sub_match<_BiIter>& __x,
3184 typename iterator_traits<_BiIter>::value_type const* __y)
3185{
3186 return !(__x < __y);
3187}
3188
3189template <class _BiIter>
3190inline _LIBCPP_INLINE_VISIBILITY
3191bool
3192operator<=(const sub_match<_BiIter>& __x,
3193 typename iterator_traits<_BiIter>::value_type const* __y)
3194{
3195 return !(__y < __x);
3196}
3197
3198template <class _BiIter>
3199inline _LIBCPP_INLINE_VISIBILITY
3200bool
3201operator==(typename iterator_traits<_BiIter>::value_type const& __x,
3202 const sub_match<_BiIter>& __y)
3203{
3204 typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
3205 return __y.compare(string_type(1, __x)) == 0;
3206}
3207
3208template <class _BiIter>
3209inline _LIBCPP_INLINE_VISIBILITY
3210bool
3211operator!=(typename iterator_traits<_BiIter>::value_type const& __x,
3212 const sub_match<_BiIter>& __y)
3213{
3214 return !(__x == __y);
3215}
3216
3217template <class _BiIter>
3218inline _LIBCPP_INLINE_VISIBILITY
3219bool
3220operator<(typename iterator_traits<_BiIter>::value_type const& __x,
3221 const sub_match<_BiIter>& __y)
3222{
3223 typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
3224 return __y.compare(string_type(1, __x)) > 0;
3225}
3226
3227template <class _BiIter>
3228inline _LIBCPP_INLINE_VISIBILITY
3229bool
3230operator>(typename iterator_traits<_BiIter>::value_type const& __x,
3231 const sub_match<_BiIter>& __y)
3232{
3233 return __y < __x;
3234}
3235
3236template <class _BiIter>
3237inline _LIBCPP_INLINE_VISIBILITY
3238bool
3239operator>=(typename iterator_traits<_BiIter>::value_type const& __x,
3240 const sub_match<_BiIter>& __y)
3241{
3242 return !(__x < __y);
3243}
3244
3245template <class _BiIter>
3246inline _LIBCPP_INLINE_VISIBILITY
3247bool
3248operator<=(typename iterator_traits<_BiIter>::value_type const& __x,
3249 const sub_match<_BiIter>& __y)
3250{
3251 return !(__y < __x);
3252}
3253
3254template <class _BiIter>
3255inline _LIBCPP_INLINE_VISIBILITY
3256bool
3257operator==(const sub_match<_BiIter>& __x,
3258 typename iterator_traits<_BiIter>::value_type const& __y)
3259{
3260 typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
3261 return __x.compare(string_type(1, __y)) == 0;
3262}
3263
3264template <class _BiIter>
3265inline _LIBCPP_INLINE_VISIBILITY
3266bool
3267operator!=(const sub_match<_BiIter>& __x,
3268 typename iterator_traits<_BiIter>::value_type const& __y)
3269{
3270 return !(__x == __y);
3271}
3272
3273template <class _BiIter>
3274inline _LIBCPP_INLINE_VISIBILITY
3275bool
3276operator<(const sub_match<_BiIter>& __x,
3277 typename iterator_traits<_BiIter>::value_type const& __y)
3278{
3279 typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
3280 return __x.compare(string_type(1, __y)) < 0;
3281}
3282
3283template <class _BiIter>
3284inline _LIBCPP_INLINE_VISIBILITY
3285bool
3286operator>(const sub_match<_BiIter>& __x,
3287 typename iterator_traits<_BiIter>::value_type const& __y)
3288{
3289 return __y < __x;
3290}
3291
3292template <class _BiIter>
3293inline _LIBCPP_INLINE_VISIBILITY
3294bool
3295operator>=(const sub_match<_BiIter>& __x,
3296 typename iterator_traits<_BiIter>::value_type const& __y)
3297{
3298 return !(__x < __y);
3299}
3300
3301template <class _BiIter>
3302inline _LIBCPP_INLINE_VISIBILITY
3303bool
3304operator<=(const sub_match<_BiIter>& __x,
3305 typename iterator_traits<_BiIter>::value_type const& __y)
3306{
3307 return !(__y < __x);
3308}
3309
3310template <class _CharT, class _ST, class _BiIter>
3311inline _LIBCPP_INLINE_VISIBILITY
3312basic_ostream<_CharT, _ST>&
3313operator<<(basic_ostream<_CharT, _ST>& __os, const sub_match<_BiIter>& __m)
3314{
3315 return __os << __m.str();
3316}
3317
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00003318template <class _BidirectionalIterator,
3319 class _Allocator = allocator<sub_match<_BidirectionalIterator> > >
3320class match_results
3321{
3322public:
3323 typedef _Allocator allocator_type;
3324 typedef sub_match<_BidirectionalIterator> value_type;
3325private:
3326 typedef vector<value_type, allocator_type> __container_type;
3327
3328 __container_type __matches_;
3329 value_type __unmatched_;
3330 value_type __prefix_;
3331 value_type __suffix_;
3332public:
3333 typedef const value_type& const_reference;
3334 typedef const_reference reference;
3335 typedef typename __container_type::const_iterator const_iterator;
3336 typedef const_iterator iterator;
3337 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
3338 typedef typename allocator_traits<allocator_type>::size_type size_type;
3339 typedef typename iterator_traits<_BidirectionalIterator>::value_type char_type;
3340 typedef basic_string<char_type> string_type;
3341
3342 // construct/copy/destroy:
3343 explicit match_results(const allocator_type& __a = allocator_type());
3344// match_results(const match_results&) = default;
3345// match_results& operator=(const match_results&) = default;
3346#ifdef _LIBCPP_MOVE
3347// match_results(match_results&& __m) = default;
3348// match_results& operator=(match_results&& __m) = default;
3349#endif
3350// ~match_results() = default;
3351
3352 // size:
3353 size_type size() const {return __matches_.size();}
3354 size_type max_size() const {return __matches_.max_size();}
3355 bool empty() const {return size() == 0;}
3356
3357 // element access:
3358 difference_type length(size_type __sub = 0) const
3359 {return (*this)[__sub].length();}
3360 difference_type position(size_type __sub = 0) const
3361 {return _STD::distance(__prefix_.first, (*this)[__sub].first);}
3362 string_type str(size_type __sub = 0) const
3363 {return (*this)[__sub].str();}
3364 const_reference operator[](size_type __n) const
3365 {return __n < __matches_.size() ? __matches_[__n] : __unmatched_;}
3366
3367 const_reference prefix() const {return __prefix_;}
3368 const_reference suffix() const {return __suffix_;}
3369
3370 const_iterator begin() const {return empty() ? __matches_.end() : __matches_.begin() + 1;}
3371 const_iterator end() const {return __matches_.end();}
3372 const_iterator cbegin() const {return empty() ? __matches_.end() : __matches_.begin() + 1;}
3373 const_iterator cend() const {return __matches_.end();}
3374
3375 // format:
3376 template <class _OutputIter>
3377 _OutputIter
3378 format(_OutputIter __out, const char_type* __fmt_first,
3379 const char_type* __fmt_last,
3380 regex_constants::match_flag_type __flags = regex_constants::format_default) const;
3381 template <class _OutputIter, class _ST, class _SA>
3382 _OutputIter
3383 format(_OutputIter __out, const basic_string<char_type, _ST, _SA>& __fmt,
3384 regex_constants::match_flag_type __flags = regex_constants::format_default) const;
3385 template <class _ST, class _SA>
3386 basic_string<char_type, _ST, _SA>
3387 format(const basic_string<char_type, _ST, _SA>& __fmt,
3388 regex_constants::match_flag_type __flags = regex_constants::format_default) const;
3389 string_type
3390 format(const char_type* __fmt,
3391 regex_constants::match_flag_type __flags = regex_constants::format_default) const;
3392
3393 // allocator:
3394 allocator_type get_allocator() const {return __matches_.get_allocator();}
3395
3396 // swap:
3397 void swap(match_results& __m);
3398
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00003399private:
3400 void __init(unsigned __s,
3401 _BidirectionalIterator __f, _BidirectionalIterator __l);
3402
3403 template <class, class> friend class basic_regex;
3404
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00003405 template <class _B, class _A, class _C, class _T>
3406 friend
3407 bool
3408 regex_match(_B, _B, match_results<_B, _A>&, const basic_regex<_C, _T>&,
3409 regex_constants::match_flag_type);
3410};
3411
3412template <class _BidirectionalIterator, class _Allocator>
3413match_results<_BidirectionalIterator, _Allocator>::match_results(
3414 const allocator_type& __a)
3415 : __matches_(__a),
3416 __unmatched_(),
3417 __prefix_(),
3418 __suffix_()
3419{
3420}
3421
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00003422template <class _BidirectionalIterator, class _Allocator>
3423void
3424match_results<_BidirectionalIterator, _Allocator>::__init(unsigned __s,
3425 _BidirectionalIterator __f, _BidirectionalIterator __l)
3426{
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00003427 __unmatched_.first = __l;
3428 __unmatched_.second = __l;
3429 __unmatched_.matched = false;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003430 __matches_.assign(__s, __unmatched_);
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00003431 __prefix_.first = __f;
3432 __prefix_.second = __f;
3433 __prefix_.matched = false;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003434 __suffix_ = __unmatched_;
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00003435}
3436
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00003437typedef match_results<const char*> cmatch;
3438typedef match_results<const wchar_t*> wcmatch;
3439typedef match_results<string::const_iterator> smatch;
3440typedef match_results<wstring::const_iterator> wsmatch;
3441
3442template <class _BidirectionalIterator, class _Allocator>
3443 bool
3444 operator==(const match_results<_BidirectionalIterator, _Allocator>& __x,
3445 const match_results<_BidirectionalIterator, _Allocator>& __y);
3446
3447template <class _BidirectionalIterator, class _Allocator>
3448 bool
3449 operator!=(const match_results<_BidirectionalIterator, _Allocator>& __x,
3450 const match_results<_BidirectionalIterator, _Allocator>& __y);
3451
3452template <class _BidirectionalIterator, class _Allocator>
3453 void
3454 swap(match_results<_BidirectionalIterator, _Allocator>& __x,
3455 match_results<_BidirectionalIterator, _Allocator>& __y);
3456
3457// regex_search
3458
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00003459template <class _CharT, class _Traits>
3460template <class _BidirectionalIterator, class _Allocator>
3461bool
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003462basic_regex<_CharT, _Traits>::__match_at_start_ecma(
3463 _BidirectionalIterator __first, _BidirectionalIterator __last,
3464 match_results<_BidirectionalIterator, _Allocator>& __m,
3465 regex_constants::match_flag_type __flags) const
3466{
3467 return false;
3468}
3469
3470template <class _CharT, class _Traits>
3471template <class _BidirectionalIterator, class _Allocator>
3472bool
3473basic_regex<_CharT, _Traits>::__match_at_start_posix_nosubs(
3474 const _CharT* __first, const _CharT* __last,
3475 match_results<_BidirectionalIterator, _Allocator>& __m,
3476 vector<size_t>& __lc,
3477 regex_constants::match_flag_type __flags) const
3478{
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003479 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
Howard Hinnantac303862010-07-12 15:51:17 +00003480 deque<__state> __states;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003481 difference_type __j = 0;
3482 difference_type __highest_j = 0;
3483 difference_type _N = _STD::distance(__first, __last);
Howard Hinnantac303862010-07-12 15:51:17 +00003484 __node* __st = __start_.get();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003485 if (__st)
3486 {
Howard Hinnantac303862010-07-12 15:51:17 +00003487 __states.push_back(__state());
3488 __states.back().__do_ = __state::__consume_input;
3489 __states.push_back(__state());
3490 __states.back().__do_ = 0;
3491 __states.back().__first_ = __first;
3492 __states.back().__current_ = __first;
3493 __states.back().__last_ = __last;
3494 __states.back().__loop_data_.resize(__loop_count());
3495 __states.back().__node_ = __st;
3496 __states.back().__flags_ = __flags;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003497 _BidirectionalIterator __current = __first;
3498 do
3499 {
Howard Hinnantac303862010-07-12 15:51:17 +00003500 __state& __s = __states.back();
3501 if (__s.__node_)
3502 __s.__node_->__exec(__s);
3503 switch (__s.__do_)
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003504 {
Howard Hinnantac303862010-07-12 15:51:17 +00003505 case __state::__end_state:
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003506 __highest_j = _STD::max(__highest_j, __j);
Howard Hinnantac303862010-07-12 15:51:17 +00003507 if (__highest_j == _N)
3508 __states.clear();
3509 else
3510 __states.pop_back();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003511 break;
Howard Hinnantac303862010-07-12 15:51:17 +00003512 case __state::__consume_input:
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003513 if (__j == _N)
3514 return false;
3515 ++__current;
Howard Hinnantac303862010-07-12 15:51:17 +00003516 if (++__j != _N && __states.size() > 1)
3517 __states.push_front(_STD::move(__s));
3518 __states.pop_back();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003519 break;
Howard Hinnantac303862010-07-12 15:51:17 +00003520 case __state::__accept_and_consume:
3521 // needs to be changed for the case that this state
3522 // consumed more than one character. This will scan
3523 // down the deque and insert extra __consume_input
3524 // states as necessary
3525 __states.push_front(_STD::move(__s));
3526 __states.pop_back();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003527 break;
Howard Hinnantac303862010-07-12 15:51:17 +00003528 case __state::__repeat:
3529 case __state::__accept_but_not_consume:
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003530 break;
Howard Hinnantac303862010-07-12 15:51:17 +00003531 case __state::__split:
3532 {
3533 __state __snext = __s;
3534 __s.__node_->__exec_split(true, __s);
3535 __snext.__node_->__exec_split(false, __snext);
3536 __states.push_back(_STD::move(__snext));
3537 }
3538 break;
3539 case __state::__reject:
3540 __states.pop_back();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003541 break;
3542 default:
3543 throw regex_error(regex_constants::error_temp);
3544 break;
3545 }
Howard Hinnantac303862010-07-12 15:51:17 +00003546 } while (!__states.empty());
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003547 if (__highest_j != 0)
3548 {
3549 __m.__matches_[0].first = __first;
3550 __m.__matches_[0].second = _STD::next(__first, __highest_j);
3551 __m.__matches_[0].matched = true;
3552 return true;
3553 }
3554 }
3555 return false;
3556}
3557
3558template <class _CharT, class _Traits>
3559template <class _BidirectionalIterator, class _Allocator>
3560bool
3561basic_regex<_CharT, _Traits>::__match_at_start_posix_subs(
3562 _BidirectionalIterator __first, _BidirectionalIterator __last,
3563 match_results<_BidirectionalIterator, _Allocator>& __m,
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003564 vector<size_t>& __lc,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003565 regex_constants::match_flag_type __flags) const
3566{
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003567 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
Howard Hinnantac303862010-07-12 15:51:17 +00003568 vector<__state> __states;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003569 vector<_BidirectionalIterator> __current_stack;
3570 vector<sub_match<_BidirectionalIterator> > __saved_matches;
Howard Hinnantac303862010-07-12 15:51:17 +00003571 __state __best_state;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003572 difference_type __j = 0;
3573 difference_type __highest_j = 0;
3574 difference_type _N = _STD::distance(__first, __last);
Howard Hinnantac303862010-07-12 15:51:17 +00003575 __node* __st = __start_.get();
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003576 if (__st)
3577 {
Howard Hinnantac303862010-07-12 15:51:17 +00003578 __states.push_back(__state());
3579 __states.back().__do_ = 0;
3580 __states.back().__first_ = __first;
3581 __states.back().__current_ = __first;
3582 __states.back().__last_ = __last;
3583 __states.back().__sub_matches_.resize(mark_count());
3584 __states.back().__loop_data_.resize(__loop_count());
3585 __states.back().__node_ = __st;
3586 __states.back().__flags_ = __flags;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003587 _BidirectionalIterator __current = __first;
Howard Hinnantac303862010-07-12 15:51:17 +00003588 bool __matched = false;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003589 do
3590 {
Howard Hinnantac303862010-07-12 15:51:17 +00003591 __state& __s = __states.back();
3592 if (__s.__node_)
3593 __s.__node_->__exec(__s);
3594 switch (__s.__do_)
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003595 {
Howard Hinnantac303862010-07-12 15:51:17 +00003596 case __state::__end_state:
3597 if (__j == 0 || __highest_j < __j)
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003598 {
Howard Hinnantac303862010-07-12 15:51:17 +00003599 __matched = true;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003600 __highest_j = __j;
Howard Hinnantac303862010-07-12 15:51:17 +00003601 __best_state = __s;
3602 if (__highest_j == _N || __highest_j == 0)
3603 __states.clear();
3604 else
3605 __states.pop_back();
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003606 }
3607 break;
Howard Hinnantac303862010-07-12 15:51:17 +00003608 case __state::__accept_and_consume:
3609 // needs to be changed for the case that this state
3610 // consumed more than one character. This will adjust
3611 // __current based on __s.__current_
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003612 if (__current != __last)
3613 {
3614 ++__current;
3615 ++__j;
3616 }
3617 break;
Howard Hinnantac303862010-07-12 15:51:17 +00003618 case __state::__repeat:
3619 case __state::__accept_but_not_consume:
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003620 break;
Howard Hinnantac303862010-07-12 15:51:17 +00003621 case __state::__split:
3622 {
3623 __state __snext = __s;
3624 __s.__node_->__exec_split(true, __s);
3625 __snext.__node_->__exec_split(false, __snext);
3626 __states.push_back(_STD::move(__snext));
3627 }
3628 break;
3629 case __state::__reject:
3630 __states.pop_back();
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003631 break;
3632 default:
3633 throw regex_error(regex_constants::error_temp);
3634 break;
3635 }
Howard Hinnantac303862010-07-12 15:51:17 +00003636 } while (!__states.empty());
3637 if (__matched)
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003638 {
3639 __m.__matches_[0].first = __first;
3640 __m.__matches_[0].second = _STD::next(__first, __highest_j);
3641 __m.__matches_[0].matched = true;
Howard Hinnantac303862010-07-12 15:51:17 +00003642 for (unsigned __i = 0; __i < __best_state.__sub_matches_.size(); ++__i)
3643 __m.__matches_[__i+1] = __best_state.__sub_matches_[__i];
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003644 return true;
3645 }
3646 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003647 return false;
3648}
3649
3650template <class _CharT, class _Traits>
3651template <class _BidirectionalIterator, class _Allocator>
3652bool
3653basic_regex<_CharT, _Traits>::__match_at_start(
3654 _BidirectionalIterator __first, _BidirectionalIterator __last,
3655 match_results<_BidirectionalIterator, _Allocator>& __m,
3656 vector<size_t>& __lc,
3657 regex_constants::match_flag_type __flags) const
3658{
3659 if (__flags_ & ECMAScript)
3660 return __match_at_start_ecma(__first, __last, __m, __flags);
3661 if (mark_count() == 0)
3662 return __match_at_start_posix_nosubs(__first, __last, __m, __lc, __flags);
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003663 return __match_at_start_posix_subs(__first, __last, __m, __lc, __flags);
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003664}
3665
3666template <class _CharT, class _Traits>
3667template <class _BidirectionalIterator, class _Allocator>
3668bool
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00003669basic_regex<_CharT, _Traits>::__search(
3670 _BidirectionalIterator __first, _BidirectionalIterator __last,
3671 match_results<_BidirectionalIterator, _Allocator>& __m,
3672 regex_constants::match_flag_type __flags) const
3673{
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00003674 if (__left_anchor_)
3675 __flags |= regex_constants::match_continuous;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003676 __m.__init(1 + mark_count(), __first, __last);
3677 vector<size_t> __lc(__loop_count());
3678 if (__match_at_start(__first, __last, __m, __lc, __flags))
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00003679 {
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003680 __m.__prefix_.second = __m[0].first;
3681 __m.__prefix_.matched = __m.__prefix_.first != __m.__prefix_.second;
3682 __m.__suffix_.first = __m[0].second;
3683 __m.__suffix_.matched = __m.__suffix_.first != __m.__suffix_.second;
3684 return true;
3685 }
3686 if (!(__flags & regex_constants::match_continuous))
3687 {
3688 __m.__matches_.assign(__m.size(), __m.__unmatched_);
3689 for (++__first; __first != __last; ++__first)
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00003690 {
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003691 if (__match_at_start(__first, __last, __m, __lc, __flags))
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00003692 {
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003693 __m.__prefix_.second = __m[0].first;
3694 __m.__prefix_.matched = __m.__prefix_.first != __m.__prefix_.second;
3695 __m.__suffix_.first = __m[0].second;
3696 __m.__suffix_.matched = __m.__suffix_.first != __m.__suffix_.second;
3697 return true;
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00003698 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003699 __m.__matches_.assign(__m.size(), __m.__unmatched_);
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00003700 }
3701 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003702 __m.__matches_.clear();
3703 return false;
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00003704}
3705
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00003706template <class _BidirectionalIterator, class _Allocator, class _CharT, class _Traits>
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00003707inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00003708bool
3709regex_search(_BidirectionalIterator __first, _BidirectionalIterator __last,
3710 match_results<_BidirectionalIterator, _Allocator>& __m,
3711 const basic_regex<_CharT, _Traits>& __e,
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00003712 regex_constants::match_flag_type __flags = regex_constants::match_default)
3713{
3714 return __e.__search(__first, __last, __m, __flags);
3715}
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00003716
3717template <class _BidirectionalIterator, class _CharT, class _Traits>
3718inline _LIBCPP_INLINE_VISIBILITY
3719bool
3720regex_search(_BidirectionalIterator __first, _BidirectionalIterator __last,
3721 const basic_regex<_CharT, _Traits>& __e,
3722 regex_constants::match_flag_type __flags = regex_constants::match_default)
3723{
3724 match_results<_BidirectionalIterator> __m;
3725 return _STD::regex_search(__first, __last, __m, __e, __flags);
3726}
3727
3728template <class _CharT, class _Allocator, class _Traits>
3729inline _LIBCPP_INLINE_VISIBILITY
3730bool
3731regex_search(const _CharT* __str, match_results<const _CharT*, _Allocator>& __m,
3732 const basic_regex<_CharT, _Traits>& __e,
3733 regex_constants::match_flag_type __flags = regex_constants::match_default)
3734{
3735 return _STD::regex_search(__str, __str + _Traits::length(__str), __m, __e, __flags);
3736}
3737
3738template <class _CharT, class _Traits>
3739inline _LIBCPP_INLINE_VISIBILITY
3740bool
3741regex_search(const _CharT* __str, const basic_regex<_CharT, _Traits>& __e,
3742 regex_constants::match_flag_type __flags = regex_constants::match_default)
3743{
3744 return _STD::regex_search(__str, __str + _Traits::length(__str), __e, __flags);
3745}
3746
3747template <class _ST, class _SA, class _CharT, class _Traits>
3748inline _LIBCPP_INLINE_VISIBILITY
3749bool
3750regex_search(const basic_string<_CharT, _ST, _SA>& __s,
3751 const basic_regex<_CharT, _Traits>& __e,
3752 regex_constants::match_flag_type __flags = regex_constants::match_default)
3753{
3754 return _STD::regex_search(__s.begin(), __s.end(), __e, __flags);
3755}
3756
3757template <class _ST, class _SA, class _Allocator, class _CharT, class _Traits>
3758inline _LIBCPP_INLINE_VISIBILITY
3759bool
3760regex_search(const basic_string<_CharT, _ST, _SA>& __s,
3761 match_results<typename basic_string<_CharT, _ST, _SA>::const_iterator, _Allocator>& __m,
3762 const basic_regex<_CharT, _Traits>& __e,
3763 regex_constants::match_flag_type __flags = regex_constants::match_default)
3764{
3765 return _STD::regex_search(__s.begin(), __s.end(), __m, __e, __flags);
3766}
3767
3768// regex_match
3769
3770template <class _BidirectionalIterator, class _Allocator, class _CharT, class _Traits>
3771bool
3772regex_match(_BidirectionalIterator __first, _BidirectionalIterator __last,
3773 match_results<_BidirectionalIterator, _Allocator>& __m,
3774 const basic_regex<_CharT, _Traits>& __e,
3775 regex_constants::match_flag_type __flags = regex_constants::match_default)
3776{
3777 bool __r = _STD::regex_search(__first, __last, __m, __e,
3778 __flags | regex_constants::match_continuous);
3779 if (__r)
3780 {
3781 __r = !__m.suffix().matched;
3782 if (!__r)
3783 __m.__matches_.clear();
3784 }
3785 return __r;
3786}
3787
3788template <class _BidirectionalIterator, class _CharT, class _Traits>
3789inline _LIBCPP_INLINE_VISIBILITY
3790bool
3791regex_match(_BidirectionalIterator __first, _BidirectionalIterator __last,
3792 const basic_regex<_CharT, _Traits>& __e,
3793 regex_constants::match_flag_type __flags = regex_constants::match_default)
3794{
3795 match_results<_BidirectionalIterator> __m;
3796 return _STD::regex_match(__first, __last, __m, __e, __flags);
3797}
3798
3799template <class _CharT, class _Allocator, class _Traits>
3800inline _LIBCPP_INLINE_VISIBILITY
3801bool
3802regex_match(const _CharT* __str, match_results<const _CharT*, _Allocator>& __m,
3803 const basic_regex<_CharT, _Traits>& __e,
3804 regex_constants::match_flag_type __flags = regex_constants::match_default)
3805{
3806 return _STD::regex_match(__str, __str + _Traits::length(__str), __m, __e, __flags);
3807}
3808
3809template <class _ST, class _SA, class _Allocator, class _CharT, class _Traits>
3810inline _LIBCPP_INLINE_VISIBILITY
3811bool
3812regex_match(const basic_string<_CharT, _ST, _SA>& __s,
3813 match_results<typename basic_string<_CharT, _ST, _SA>::const_iterator, _Allocator>& __m,
3814 const basic_regex<_CharT, _Traits>& __e,
3815 regex_constants::match_flag_type __flags = regex_constants::match_default)
3816{
3817 return _STD::regex_match(__s.begin(), __s.end(), __m, __e, __flags);
3818}
3819
3820template <class _CharT, class _Traits>
3821inline _LIBCPP_INLINE_VISIBILITY
3822bool
3823regex_match(const _CharT* __str, const basic_regex<_CharT, _Traits>& __e,
3824 regex_constants::match_flag_type __flags = regex_constants::match_default)
3825{
3826 return _STD::regex_match(__str, __str + _Traits::length(__str), __e, __flags);
3827}
3828
3829template <class _ST, class _SA, class _CharT, class _Traits>
3830inline _LIBCPP_INLINE_VISIBILITY
3831bool
3832regex_match(const basic_string<_CharT, _ST, _SA>& __s,
3833 const basic_regex<_CharT, _Traits>& __e,
3834 regex_constants::match_flag_type __flags = regex_constants::match_default)
3835{
3836 return _STD::regex_match(__s.begin(), __s.end(), __e, __flags);
3837}
3838
Howard Hinnant3257c982010-06-17 00:34:59 +00003839_LIBCPP_END_NAMESPACE_STD
3840
3841#endif // _LIBCPP_REGEX