blob: 2e0e8e39486304ab1be3fd26f58b8b6b1f36ba64 [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,
Howard Hinnantad2a7ab2010-07-27 17:24:17 +0000750 ECMAScript = 0,
751 basic = 1 << 4,
752 extended = 1 << 5,
753 awk = 1 << 6,
754 grep = 1 << 7,
755 egrep = 1 << 8
Howard Hinnant3257c982010-06-17 00:34:59 +0000756};
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,
Howard Hinnantad2a7ab2010-07-27 17:24:17 +0000910 __re_err_grammar,
911 __re_err_empty,
912 __re_err_unknown
Howard Hinnant3257c982010-06-17 00:34:59 +0000913};
914
915} // regex_constants
916
917class _LIBCPP_EXCEPTION_ABI regex_error
918 : public runtime_error
919{
920 regex_constants::error_type __code_;
921public:
922 explicit regex_error(regex_constants::error_type __ecode);
923 virtual ~regex_error() throw();
924 regex_constants::error_type code() const {return __code_;}
925};
926
927template <class _CharT>
928struct regex_traits
929{
930public:
931 typedef _CharT char_type;
932 typedef basic_string<char_type> string_type;
933 typedef locale locale_type;
Howard Hinnantf409d2f2010-06-21 21:01:43 +0000934 typedef ctype_base::mask char_class_type;
Howard Hinnant3257c982010-06-17 00:34:59 +0000935
Howard Hinnantf409d2f2010-06-21 21:01:43 +0000936 static const char_class_type __regex_word = 0x80;
Howard Hinnant3257c982010-06-17 00:34:59 +0000937private:
938 locale __loc_;
939 const ctype<char_type>* __ct_;
940 const collate<char_type>* __col_;
941
942public:
943 regex_traits();
944
945 static size_t length(const char_type* __p)
946 {return char_traits<char_type>::length(__p);}
947 char_type translate(char_type __c) const {return __c;}
948 char_type translate_nocase(char_type __c) const;
949 template <class _ForwardIterator>
950 string_type
951 transform(_ForwardIterator __f, _ForwardIterator __l) const;
952 template <class _ForwardIterator>
953 string_type
954 transform_primary( _ForwardIterator __f, _ForwardIterator __l) const
955 {return __transform_primary(__f, __l, char_type());}
956 template <class _ForwardIterator>
957 string_type
958 lookup_collatename(_ForwardIterator __f, _ForwardIterator __l) const
959 {return __lookup_collatename(__f, __l, char_type());}
960 template <class _ForwardIterator>
961 char_class_type
962 lookup_classname(_ForwardIterator __f, _ForwardIterator __l,
Howard Hinnantf409d2f2010-06-21 21:01:43 +0000963 bool __icase = false) const
964 {return __lookup_classname(__f, __l, __icase, char_type());}
965 bool isctype(char_type __c, char_class_type __m) const;
966 int value(char_type __ch, int __radix) const
967 {return __value(__ch, __radix);}
Howard Hinnant3257c982010-06-17 00:34:59 +0000968 locale_type imbue(locale_type __l);
969 locale_type getloc()const {return __loc_;}
970
971private:
972 void __init();
973
974 template <class _ForwardIterator>
975 string_type
976 __transform_primary(_ForwardIterator __f, _ForwardIterator __l, char) const;
977 template <class _ForwardIterator>
978 string_type
979 __transform_primary(_ForwardIterator __f, _ForwardIterator __l, wchar_t) const;
980
981 template <class _ForwardIterator>
982 string_type
983 __lookup_collatename(_ForwardIterator __f, _ForwardIterator __l, char) const;
984 template <class _ForwardIterator>
985 string_type
986 __lookup_collatename(_ForwardIterator __f, _ForwardIterator __l, wchar_t) const;
Howard Hinnantf409d2f2010-06-21 21:01:43 +0000987
988 template <class _ForwardIterator>
989 char_class_type
990 __lookup_classname(_ForwardIterator __f, _ForwardIterator __l,
991 bool __icase, char) const;
992 template <class _ForwardIterator>
993 char_class_type
994 __lookup_classname(_ForwardIterator __f, _ForwardIterator __l,
995 bool __icase, wchar_t) const;
996
997 static int __value(unsigned char __ch, int __radix);
998 int __value(char __ch, int __radix) const
999 {return __value(static_cast<unsigned char>(__ch), __radix);}
1000 int __value(wchar_t __ch, int __radix) const;
Howard Hinnant3257c982010-06-17 00:34:59 +00001001};
1002
1003template <class _CharT>
1004regex_traits<_CharT>::regex_traits()
1005{
1006 __init();
1007}
1008
1009template <class _CharT>
1010typename regex_traits<_CharT>::char_type
1011regex_traits<_CharT>::translate_nocase(char_type __c) const
1012{
1013 return __ct_->tolower(__c);
1014}
1015
1016template <class _CharT>
1017template <class _ForwardIterator>
1018typename regex_traits<_CharT>::string_type
1019regex_traits<_CharT>::transform(_ForwardIterator __f, _ForwardIterator __l) const
1020{
1021 string_type __s(__f, __l);
1022 return __col_->transform(__s.data(), __s.data() + __s.size());
1023}
1024
1025template <class _CharT>
1026void
1027regex_traits<_CharT>::__init()
1028{
1029 __ct_ = &use_facet<ctype<char_type> >(__loc_);
1030 __col_ = &use_facet<collate<char_type> >(__loc_);
1031}
1032
1033template <class _CharT>
1034typename regex_traits<_CharT>::locale_type
1035regex_traits<_CharT>::imbue(locale_type __l)
1036{
1037 locale __r = __loc_;
1038 __loc_ = __l;
1039 __init();
1040 return __r;
1041}
1042
1043// transform_primary is very FreeBSD-specific
1044
1045template <class _CharT>
1046template <class _ForwardIterator>
1047typename regex_traits<_CharT>::string_type
1048regex_traits<_CharT>::__transform_primary(_ForwardIterator __f,
1049 _ForwardIterator __l, char) const
1050{
1051 const string_type __s(__f, __l);
1052 string_type __d = __col_->transform(__s.data(), __s.data() + __s.size());
1053 switch (__d.size())
1054 {
1055 case 1:
1056 break;
1057 case 12:
1058 __d[11] = __d[3];
1059 break;
1060 default:
1061 __d.clear();
1062 break;
1063 }
1064 return __d;
1065}
1066
1067template <class _CharT>
1068template <class _ForwardIterator>
1069typename regex_traits<_CharT>::string_type
1070regex_traits<_CharT>::__transform_primary(_ForwardIterator __f,
1071 _ForwardIterator __l, wchar_t) const
1072{
1073 const string_type __s(__f, __l);
1074 string_type __d = __col_->transform(__s.data(), __s.data() + __s.size());
1075 switch (__d.size())
1076 {
1077 case 1:
1078 break;
1079 case 3:
1080 __d[2] = __d[0];
1081 break;
1082 default:
1083 __d.clear();
1084 break;
1085 }
1086 return __d;
1087}
1088
1089// lookup_collatename is very FreeBSD-specific
1090
Howard Hinnantf409d2f2010-06-21 21:01:43 +00001091string __get_collation_name(const char* __s);
Howard Hinnant3257c982010-06-17 00:34:59 +00001092
1093template <class _CharT>
1094template <class _ForwardIterator>
1095typename regex_traits<_CharT>::string_type
1096regex_traits<_CharT>::__lookup_collatename(_ForwardIterator __f,
1097 _ForwardIterator __l, char) const
1098{
1099 string_type __s(__f, __l);
1100 string_type __r;
1101 if (!__s.empty())
1102 {
1103 __r = __get_collation_name(__s.c_str());
1104 if (__r.empty() && __s.size() <= 2)
1105 {
1106 __r = __col_->transform(__s.data(), __s.data() + __s.size());
1107 if (__r.size() == 1 || __r.size() == 12)
1108 __r = __s;
1109 else
1110 __r.clear();
1111 }
1112 }
1113 return __r;
1114}
1115
1116template <class _CharT>
1117template <class _ForwardIterator>
1118typename regex_traits<_CharT>::string_type
1119regex_traits<_CharT>::__lookup_collatename(_ForwardIterator __f,
1120 _ForwardIterator __l, wchar_t) const
1121{
1122 string_type __s(__f, __l);
1123 string __n;
1124 __n.reserve(__s.size());
1125 for (typename string_type::const_iterator __i = __s.begin(), __e = __s.end();
1126 __i != __e; ++__i)
1127 {
1128 if (static_cast<unsigned>(*__i) >= 127)
1129 return string_type();
1130 __n.push_back(char(*__i));
1131 }
1132 string_type __r;
1133 if (!__s.empty())
1134 {
1135 __n = __get_collation_name(__n.c_str());
1136 if (!__n.empty())
1137 __r.assign(__n.begin(), __n.end());
1138 else if (__s.size() <= 2)
1139 {
1140 __r = __col_->transform(__s.data(), __s.data() + __s.size());
1141 if (__r.size() == 1 || __r.size() == 3)
1142 __r = __s;
1143 else
1144 __r.clear();
1145 }
1146 }
1147 return __r;
1148}
1149
Howard Hinnantf409d2f2010-06-21 21:01:43 +00001150// lookup_classname
1151
1152ctype_base::mask __get_classname(const char* __s, bool __icase);
1153
1154template <class _CharT>
1155template <class _ForwardIterator>
1156typename regex_traits<_CharT>::char_class_type
1157regex_traits<_CharT>::__lookup_classname(_ForwardIterator __f,
1158 _ForwardIterator __l,
1159 bool __icase, char) const
1160{
1161 string_type __s(__f, __l);
1162 __ct_->tolower(&__s[0], &__s[0] + __s.size());
1163 return __get_classname(__s.c_str(), __icase);
1164}
1165
1166template <class _CharT>
1167template <class _ForwardIterator>
1168typename regex_traits<_CharT>::char_class_type
1169regex_traits<_CharT>::__lookup_classname(_ForwardIterator __f,
1170 _ForwardIterator __l,
1171 bool __icase, wchar_t) const
1172{
1173 string_type __s(__f, __l);
1174 __ct_->tolower(&__s[0], &__s[0] + __s.size());
1175 string __n;
1176 __n.reserve(__s.size());
1177 for (typename string_type::const_iterator __i = __s.begin(), __e = __s.end();
1178 __i != __e; ++__i)
1179 {
1180 if (static_cast<unsigned>(*__i) >= 127)
1181 return char_class_type();
1182 __n.push_back(char(*__i));
1183 }
1184 return __get_classname(__n.c_str(), __icase);
1185}
1186
1187template <class _CharT>
1188bool
1189regex_traits<_CharT>::isctype(char_type __c, char_class_type __m) const
1190{
1191 if (__ct_->is(__m, __c))
1192 return true;
1193 return (__c == '_' && (__m & __regex_word));
1194}
1195
1196template <class _CharT>
1197int
1198regex_traits<_CharT>::__value(unsigned char __ch, int __radix)
1199{
1200 if ((__ch & 0xF8u) == 0x30) // '0' <= __ch && __ch <= '7'
1201 return __ch - '0';
1202 if (__radix != 8)
1203 {
1204 if ((__ch & 0xFEu) == 0x38) // '8' <= __ch && __ch <= '9'
1205 return __ch - '0';
1206 if (__radix == 16)
1207 {
1208 __ch |= 0x20; // tolower
1209 if ('a' <= __ch && __ch <= 'f')
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00001210 return __ch - ('a' - 10);
Howard Hinnantf409d2f2010-06-21 21:01:43 +00001211 }
1212 }
1213 return -1;
1214}
1215
1216template <class _CharT>
1217inline
1218int
1219regex_traits<_CharT>::__value(wchar_t __ch, int __radix) const
1220{
1221 return __value(static_cast<unsigned char>(__ct_->narrow(__ch, char_type())), __radix);
1222}
1223
Howard Hinnantac303862010-07-12 15:51:17 +00001224template <class _CharT> class __node;
1225
1226template <class _BidirectionalIterator> class sub_match;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001227
Howard Hinnant17615b02010-07-27 01:25:38 +00001228template <class _BidirectionalIterator,
1229 class _Allocator = allocator<sub_match<_BidirectionalIterator> > >
1230class match_results;
1231
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001232template <class _CharT>
Howard Hinnantac303862010-07-12 15:51:17 +00001233struct __state
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001234{
1235 enum
1236 {
1237 __end_state = -1000,
1238 __consume_input, // -999
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001239 __begin_marked_expr, // -998
1240 __end_marked_expr, // -997
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001241 __pop_state, // -996
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001242 __accept_and_consume, // -995
1243 __accept_but_not_consume, // -994
1244 __reject, // -993
Howard Hinnantac303862010-07-12 15:51:17 +00001245 __split,
1246 __repeat
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001247 };
1248
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001249 int __do_;
Howard Hinnantac303862010-07-12 15:51:17 +00001250 const _CharT* __first_;
1251 const _CharT* __current_;
1252 const _CharT* __last_;
1253 vector<sub_match<const _CharT*> > __sub_matches_;
1254 vector<pair<size_t, const _CharT*> > __loop_data_;
1255 const __node<_CharT>* __node_;
1256 regex_constants::match_flag_type __flags_;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001257
Howard Hinnantac303862010-07-12 15:51:17 +00001258 __state()
1259 : __do_(0), __first_(nullptr), __current_(nullptr), __last_(nullptr),
1260 __node_(nullptr), __flags_() {}
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001261};
1262
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001263template <class _CharT>
1264ostream&
Howard Hinnantac303862010-07-12 15:51:17 +00001265operator<<(ostream& os, const __state<_CharT>& c)
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001266{
1267 os << c.__do_;
Howard Hinnantac303862010-07-12 15:51:17 +00001268 if (c.__node_)
1269 os << ", " << c.__node_->speak();
1270else
1271 os << ", null";
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001272 return os;
1273}
1274
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001275
Howard Hinnantac303862010-07-12 15:51:17 +00001276// __node
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001277
1278template <class _CharT>
Howard Hinnantac303862010-07-12 15:51:17 +00001279class __node
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001280{
Howard Hinnantac303862010-07-12 15:51:17 +00001281 __node(const __node&);
1282 __node& operator=(const __node&);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001283public:
Howard Hinnantac303862010-07-12 15:51:17 +00001284 typedef _STD::__state<_CharT> __state;
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001285
Howard Hinnantac303862010-07-12 15:51:17 +00001286 __node() {}
1287 virtual ~__node() {}
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001288
Howard Hinnantac303862010-07-12 15:51:17 +00001289 virtual void __exec(__state&) const {};
1290 virtual void __exec_split(bool, __state&) const {};
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001291
Howard Hinnantac303862010-07-12 15:51:17 +00001292 virtual string speak() const {return "__node";}
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001293};
1294
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001295// __end_state
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001296
1297template <class _CharT>
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001298class __end_state
Howard Hinnantac303862010-07-12 15:51:17 +00001299 : public __node<_CharT>
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001300{
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001301public:
Howard Hinnantac303862010-07-12 15:51:17 +00001302 typedef _STD::__state<_CharT> __state;
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001303
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001304 __end_state() {}
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00001305
Howard Hinnantac303862010-07-12 15:51:17 +00001306 virtual void __exec(__state&) const;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001307
1308 virtual string speak() const {return "end state";}
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00001309};
1310
1311template <class _CharT>
Howard Hinnantac303862010-07-12 15:51:17 +00001312void
1313__end_state<_CharT>::__exec(__state& __s) const
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00001314{
Howard Hinnantac303862010-07-12 15:51:17 +00001315 __s.__do_ = __state::__end_state;
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00001316}
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001317
1318// __has_one_state
1319
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00001320template <class _CharT>
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001321class __has_one_state
Howard Hinnantac303862010-07-12 15:51:17 +00001322 : public __node<_CharT>
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00001323{
Howard Hinnantac303862010-07-12 15:51:17 +00001324 __node<_CharT>* __first_;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001325
1326public:
Howard Hinnantac303862010-07-12 15:51:17 +00001327 explicit __has_one_state(__node<_CharT>* __s)
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001328 : __first_(__s) {}
1329
Howard Hinnantac303862010-07-12 15:51:17 +00001330 __node<_CharT>* first() const {return __first_;}
1331 __node<_CharT>*& first() {return __first_;}
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001332};
1333
1334// __owns_one_state
1335
1336template <class _CharT>
1337class __owns_one_state
1338 : public __has_one_state<_CharT>
1339{
1340 typedef __has_one_state<_CharT> base;
1341
1342public:
Howard Hinnantac303862010-07-12 15:51:17 +00001343 explicit __owns_one_state(__node<_CharT>* __s)
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001344 : base(__s) {}
1345
1346 virtual ~__owns_one_state();
1347};
1348
1349template <class _CharT>
1350__owns_one_state<_CharT>::~__owns_one_state()
1351{
1352 delete this->first();
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00001353}
1354
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001355// __empty_state
1356
1357template <class _CharT>
1358class __empty_state
1359 : public __owns_one_state<_CharT>
1360{
1361 typedef __owns_one_state<_CharT> base;
1362
1363public:
Howard Hinnantac303862010-07-12 15:51:17 +00001364 typedef _STD::__state<_CharT> __state;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001365
Howard Hinnantac303862010-07-12 15:51:17 +00001366 explicit __empty_state(__node<_CharT>* __s)
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001367 : base(__s) {}
1368
Howard Hinnantac303862010-07-12 15:51:17 +00001369 virtual void __exec(__state&) const;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001370
1371 virtual string speak() const {return "empty state";}
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001372};
1373
1374template <class _CharT>
Howard Hinnantac303862010-07-12 15:51:17 +00001375void
1376__empty_state<_CharT>::__exec(__state& __s) const
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001377{
Howard Hinnantac303862010-07-12 15:51:17 +00001378 __s.__do_ = __state::__accept_but_not_consume;
1379 __s.__node_ = this->first();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001380}
1381
1382// __empty_non_own_state
1383
1384template <class _CharT>
1385class __empty_non_own_state
1386 : public __has_one_state<_CharT>
1387{
1388 typedef __has_one_state<_CharT> base;
1389
1390public:
Howard Hinnantac303862010-07-12 15:51:17 +00001391 typedef _STD::__state<_CharT> __state;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001392
Howard Hinnantac303862010-07-12 15:51:17 +00001393 explicit __empty_non_own_state(__node<_CharT>* __s)
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001394 : base(__s) {}
1395
Howard Hinnantac303862010-07-12 15:51:17 +00001396 virtual void __exec(__state&) const;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001397
1398 virtual string speak() const {return "empty non-owning state";}
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001399};
1400
1401template <class _CharT>
Howard Hinnantac303862010-07-12 15:51:17 +00001402void
1403__empty_non_own_state<_CharT>::__exec(__state& __s) const
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001404{
Howard Hinnantac303862010-07-12 15:51:17 +00001405 __s.__do_ = __state::__accept_but_not_consume;
1406 __s.__node_ = this->first();
1407}
1408
1409// __repeat_one_loop
1410
1411template <class _CharT>
1412class __repeat_one_loop
1413 : public __has_one_state<_CharT>
1414{
1415 typedef __has_one_state<_CharT> base;
1416
1417public:
1418 typedef _STD::__state<_CharT> __state;
1419
1420 explicit __repeat_one_loop(__node<_CharT>* __s)
1421 : base(__s) {}
1422
1423 virtual void __exec(__state&) const;
1424
1425 virtual string speak() const {return "repeat loop";}
1426};
1427
1428template <class _CharT>
1429void
1430__repeat_one_loop<_CharT>::__exec(__state& __s) const
1431{
1432 __s.__do_ = __state::__repeat;
1433 __s.__node_ = this->first();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001434}
1435
1436// __owns_two_states
1437
1438template <class _CharT>
1439class __owns_two_states
1440 : public __owns_one_state<_CharT>
1441{
1442 typedef __owns_one_state<_CharT> base;
1443
1444 base* __second_;
1445
1446public:
Howard Hinnantac303862010-07-12 15:51:17 +00001447 explicit __owns_two_states(__node<_CharT>* __s1, base* __s2)
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001448 : base(__s1), __second_(__s2) {}
1449
1450 virtual ~__owns_two_states();
1451
1452 base* second() const {return __second_;}
1453 base*& second() {return __second_;}
1454};
1455
1456template <class _CharT>
1457__owns_two_states<_CharT>::~__owns_two_states()
1458{
1459 delete __second_;
1460}
1461
1462// __loop
1463
1464template <class _CharT>
1465class __loop
1466 : public __owns_two_states<_CharT>
1467{
1468 typedef __owns_two_states<_CharT> base;
1469
1470 size_t __min_;
1471 size_t __max_;
1472 unsigned __loop_id_;
Howard Hinnantac303862010-07-12 15:51:17 +00001473 unsigned __mexp_begin_;
1474 unsigned __mexp_end_;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001475 bool __greedy_;
1476
1477public:
Howard Hinnantac303862010-07-12 15:51:17 +00001478 typedef _STD::__state<_CharT> __state;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001479
1480 explicit __loop(unsigned __loop_id,
Howard Hinnantac303862010-07-12 15:51:17 +00001481 __node<_CharT>* __s1, __owns_one_state<_CharT>* __s2,
1482 unsigned __mexp_begin, unsigned __mexp_end,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001483 bool __greedy = true,
1484 size_t __min = 0,
1485 size_t __max = numeric_limits<size_t>::max())
1486 : base(__s1, __s2), __min_(__min), __max_(__max), __loop_id_(__loop_id),
Howard Hinnantac303862010-07-12 15:51:17 +00001487 __mexp_begin_(__mexp_begin), __mexp_end_(__mexp_end),
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001488 __greedy_(__greedy) {}
1489
Howard Hinnantac303862010-07-12 15:51:17 +00001490 virtual void __exec(__state& __s) const;
1491 virtual void __exec_split(bool __second, __state& __s) const;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001492
1493 virtual string speak() const
1494 {
1495 ostringstream os;
Howard Hinnantac303862010-07-12 15:51:17 +00001496 os << "loop "<< __loop_id_ << " {" << __min_ << ',' << __max_ << "}";
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001497 if (!__greedy_)
1498 os << " not";
1499 os << " greedy";
1500 return os.str();
1501 }
Howard Hinnantac303862010-07-12 15:51:17 +00001502
1503private:
1504 void __init_repeat(__state& __s) const
1505 {
1506 __s.__loop_data_[__loop_id_].second = __s.__current_;
1507 for (size_t __i = __mexp_begin_-1; __i != __mexp_end_-1; ++__i)
1508 {
1509 __s.__sub_matches_[__i].first = __s.__last_;
1510 __s.__sub_matches_[__i].second = __s.__last_;
1511 __s.__sub_matches_[__i].matched = false;
1512 }
1513 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001514};
1515
1516template <class _CharT>
Howard Hinnantac303862010-07-12 15:51:17 +00001517void
1518__loop<_CharT>::__exec(__state& __s) const
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001519{
Howard Hinnantac303862010-07-12 15:51:17 +00001520 if (__s.__do_ == __state::__repeat)
1521 {
1522 bool __do_repeat = ++__s.__loop_data_[__loop_id_].first < __max_;
1523 bool __do_alt = __s.__loop_data_[__loop_id_].first >= __min_;
1524 if (__do_repeat && __do_alt &&
1525 __s.__loop_data_[__loop_id_].second == __s.__current_)
1526 __do_repeat = false;
1527 if (__do_repeat && __do_alt)
1528 __s.__do_ = __state::__split;
1529 else if (__do_repeat)
1530 {
1531 __s.__do_ = __state::__accept_but_not_consume;
1532 __s.__node_ = this->first();
1533 __init_repeat(__s);
1534 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001535 else
Howard Hinnantac303862010-07-12 15:51:17 +00001536 {
1537 __s.__do_ = __state::__accept_but_not_consume;
1538 __s.__node_ = this->second();
1539 }
1540 }
1541 else
1542 {
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00001543 __s.__loop_data_[__loop_id_].first = 0;
1544 bool __do_repeat = 0 < __max_;
1545 bool __do_alt = 0 >= __min_;
1546 if (__do_repeat && __do_alt)
Howard Hinnantac303862010-07-12 15:51:17 +00001547 __s.__do_ = __state::__split;
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00001548 else if (__do_repeat)
1549 {
1550 __s.__do_ = __state::__accept_but_not_consume;
1551 __s.__node_ = this->first();
1552 __init_repeat(__s);
1553 }
Howard Hinnantac303862010-07-12 15:51:17 +00001554 else
1555 {
1556 __s.__do_ = __state::__accept_but_not_consume;
1557 __s.__node_ = this->second();
1558 }
1559 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001560}
1561
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001562template <class _CharT>
Howard Hinnantac303862010-07-12 15:51:17 +00001563void
1564__loop<_CharT>::__exec_split(bool __second, __state& __s) const
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001565{
Howard Hinnantac303862010-07-12 15:51:17 +00001566 __s.__do_ = __state::__accept_but_not_consume;
1567 if (__greedy_ != __second)
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001568 {
Howard Hinnantac303862010-07-12 15:51:17 +00001569 __s.__node_ = this->first();
1570 __init_repeat(__s);
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001571 }
Howard Hinnantac303862010-07-12 15:51:17 +00001572 else
1573 __s.__node_ = this->second();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001574}
1575
Howard Hinnantaa698082010-07-16 19:08:36 +00001576// __alternate
1577
1578template <class _CharT>
1579class __alternate
1580 : public __owns_two_states<_CharT>
1581{
1582 typedef __owns_two_states<_CharT> base;
1583
1584public:
1585 typedef _STD::__state<_CharT> __state;
1586
1587 explicit __alternate(__owns_one_state<_CharT>* __s1,
1588 __owns_one_state<_CharT>* __s2)
1589 : base(__s1, __s2) {}
1590
1591 virtual void __exec(__state& __s) const;
1592 virtual void __exec_split(bool __second, __state& __s) const;
1593
1594 virtual string speak() const
1595 {
1596 ostringstream os;
1597 os << "__alternate";
1598 return os.str();
1599 }
1600};
1601
1602template <class _CharT>
1603void
1604__alternate<_CharT>::__exec(__state& __s) const
1605{
1606 __s.__do_ = __state::__split;
1607}
1608
1609template <class _CharT>
1610void
1611__alternate<_CharT>::__exec_split(bool __second, __state& __s) const
1612{
1613 __s.__do_ = __state::__accept_but_not_consume;
Howard Hinnant1371b2e2010-07-22 14:12:20 +00001614 if (__second)
Howard Hinnantaa698082010-07-16 19:08:36 +00001615 __s.__node_ = this->second();
Howard Hinnant1371b2e2010-07-22 14:12:20 +00001616 else
1617 __s.__node_ = this->first();
Howard Hinnantaa698082010-07-16 19:08:36 +00001618}
1619
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001620// __begin_marked_subexpression
1621
1622template <class _CharT>
1623class __begin_marked_subexpression
1624 : public __owns_one_state<_CharT>
1625{
1626 typedef __owns_one_state<_CharT> base;
1627
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001628 unsigned __mexp_;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001629public:
Howard Hinnantac303862010-07-12 15:51:17 +00001630 typedef _STD::__state<_CharT> __state;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001631
Howard Hinnantac303862010-07-12 15:51:17 +00001632 explicit __begin_marked_subexpression(unsigned __mexp, __node<_CharT>* __s)
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001633 : base(__s), __mexp_(__mexp) {}
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001634
Howard Hinnantac303862010-07-12 15:51:17 +00001635 virtual void __exec(__state&) const;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001636
1637 virtual string speak() const
1638 {
1639 ostringstream os;
1640 os << "begin marked expr " << __mexp_;
1641 return os.str();
1642 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001643};
1644
1645template <class _CharT>
Howard Hinnantac303862010-07-12 15:51:17 +00001646void
1647__begin_marked_subexpression<_CharT>::__exec(__state& __s) const
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001648{
Howard Hinnantac303862010-07-12 15:51:17 +00001649 __s.__do_ = __state::__accept_but_not_consume;
1650 __s.__sub_matches_[__mexp_-1].first = __s.__current_;
1651 __s.__node_ = this->first();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001652}
1653
1654// __end_marked_subexpression
1655
1656template <class _CharT>
1657class __end_marked_subexpression
1658 : public __owns_one_state<_CharT>
1659{
1660 typedef __owns_one_state<_CharT> base;
1661
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001662 unsigned __mexp_;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001663public:
Howard Hinnantac303862010-07-12 15:51:17 +00001664 typedef _STD::__state<_CharT> __state;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001665
Howard Hinnantac303862010-07-12 15:51:17 +00001666 explicit __end_marked_subexpression(unsigned __mexp, __node<_CharT>* __s)
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001667 : base(__s), __mexp_(__mexp) {}
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001668
Howard Hinnantac303862010-07-12 15:51:17 +00001669 virtual void __exec(__state&) const;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001670
1671 virtual string speak() const
1672 {
1673 ostringstream os;
1674 os << "end marked expr " << __mexp_;
1675 return os.str();
1676 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001677};
1678
1679template <class _CharT>
Howard Hinnantac303862010-07-12 15:51:17 +00001680void
1681__end_marked_subexpression<_CharT>::__exec(__state& __s) const
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001682{
Howard Hinnantac303862010-07-12 15:51:17 +00001683 __s.__do_ = __state::__accept_but_not_consume;
1684 __s.__sub_matches_[__mexp_-1].second = __s.__current_;
1685 __s.__sub_matches_[__mexp_-1].matched = true;
1686 __s.__node_ = this->first();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001687}
1688
Howard Hinnantcba352d2010-07-12 18:16:05 +00001689// __back_ref
1690
1691template <class _CharT>
1692class __back_ref
1693 : public __owns_one_state<_CharT>
1694{
1695 typedef __owns_one_state<_CharT> base;
1696
1697 unsigned __mexp_;
1698public:
1699 typedef _STD::__state<_CharT> __state;
1700
1701 explicit __back_ref(unsigned __mexp, __node<_CharT>* __s)
1702 : base(__s), __mexp_(__mexp) {}
1703
1704 virtual void __exec(__state&) const;
1705
1706 virtual string speak() const
1707 {
1708 ostringstream os;
1709 os << "__back_ref " << __mexp_;
1710 return os.str();
1711 }
1712};
1713
1714template <class _CharT>
1715void
1716__back_ref<_CharT>::__exec(__state& __s) const
1717{
1718 sub_match<const _CharT*>& __sm = __s.__sub_matches_[__mexp_-1];
1719 if (__sm.matched)
1720 {
1721 ptrdiff_t __len = __sm.second - __sm.first;
1722 if (__s.__last_ - __s.__current_ >= __len &&
1723 _STD::equal(__sm.first, __sm.second, __s.__current_))
1724 {
1725 __s.__do_ = __state::__accept_but_not_consume;
1726 __s.__current_ += __len;
1727 __s.__node_ = this->first();
1728 }
1729 else
1730 {
1731 __s.__do_ = __state::__reject;
1732 __s.__node_ = nullptr;
1733 }
1734 }
1735 else
1736 {
1737 __s.__do_ = __state::__reject;
1738 __s.__node_ = nullptr;
1739 }
1740}
1741
Howard Hinnante34f17d2010-07-12 19:11:27 +00001742// __back_ref_icase
1743
1744template <class _CharT, class _Traits>
1745class __back_ref_icase
1746 : public __owns_one_state<_CharT>
1747{
1748 typedef __owns_one_state<_CharT> base;
1749
1750 _Traits __traits_;
1751 unsigned __mexp_;
1752public:
1753 typedef _STD::__state<_CharT> __state;
1754
1755 explicit __back_ref_icase(const _Traits& __traits, unsigned __mexp,
1756 __node<_CharT>* __s)
1757 : base(__s), __traits_(__traits), __mexp_(__mexp) {}
1758
1759 virtual void __exec(__state&) const;
1760
1761 virtual string speak() const
1762 {
1763 ostringstream os;
1764 os << "__back_ref_icase " << __mexp_;
1765 return os.str();
1766 }
1767};
1768
1769template <class _CharT, class _Traits>
1770void
1771__back_ref_icase<_CharT, _Traits>::__exec(__state& __s) const
1772{
1773 sub_match<const _CharT*>& __sm = __s.__sub_matches_[__mexp_-1];
1774 if (__sm.matched)
1775 {
1776 ptrdiff_t __len = __sm.second - __sm.first;
1777 if (__s.__last_ - __s.__current_ >= __len)
1778 {
1779 for (ptrdiff_t __i = 0; __i < __len; ++__i)
1780 {
1781 if (__traits_.translate_nocase(__sm.first[__i]) !=
1782 __traits_.translate_nocase(__s.__current_[__i]))
1783 goto __not_equal;
1784 }
1785 __s.__do_ = __state::__accept_but_not_consume;
1786 __s.__current_ += __len;
1787 __s.__node_ = this->first();
1788 }
1789 else
1790 {
1791 __s.__do_ = __state::__reject;
1792 __s.__node_ = nullptr;
1793 }
1794 }
1795 else
1796 {
1797__not_equal:
1798 __s.__do_ = __state::__reject;
1799 __s.__node_ = nullptr;
1800 }
1801}
1802
1803// __back_ref_collate
1804
1805template <class _CharT, class _Traits>
1806class __back_ref_collate
1807 : public __owns_one_state<_CharT>
1808{
1809 typedef __owns_one_state<_CharT> base;
1810
1811 _Traits __traits_;
1812 unsigned __mexp_;
1813public:
1814 typedef _STD::__state<_CharT> __state;
1815
1816 explicit __back_ref_collate(const _Traits& __traits, unsigned __mexp,
1817 __node<_CharT>* __s)
1818 : base(__s), __traits_(__traits), __mexp_(__mexp) {}
1819
1820 virtual void __exec(__state&) const;
1821
1822 virtual string speak() const
1823 {
1824 ostringstream os;
1825 os << "__back_ref_collate " << __mexp_;
1826 return os.str();
1827 }
1828};
1829
1830template <class _CharT, class _Traits>
1831void
1832__back_ref_collate<_CharT, _Traits>::__exec(__state& __s) const
1833{
1834 sub_match<const _CharT*>& __sm = __s.__sub_matches_[__mexp_-1];
1835 if (__sm.matched)
1836 {
1837 ptrdiff_t __len = __sm.second - __sm.first;
1838 if (__s.__last_ - __s.__current_ >= __len)
1839 {
1840 for (ptrdiff_t __i = 0; __i < __len; ++__i)
1841 {
1842 if (__traits_.translate(__sm.first[__i]) !=
1843 __traits_.translate(__s.__current_[__i]))
1844 goto __not_equal;
1845 }
1846 __s.__do_ = __state::__accept_but_not_consume;
1847 __s.__current_ += __len;
1848 __s.__node_ = this->first();
1849 }
1850 else
1851 {
1852 __s.__do_ = __state::__reject;
1853 __s.__node_ = nullptr;
1854 }
1855 }
1856 else
1857 {
1858__not_equal:
1859 __s.__do_ = __state::__reject;
1860 __s.__node_ = nullptr;
1861 }
1862}
1863
Howard Hinnant17615b02010-07-27 01:25:38 +00001864// __word_boundary
1865
1866template <class _CharT, class _Traits>
1867class __word_boundary
1868 : public __owns_one_state<_CharT>
1869{
1870 typedef __owns_one_state<_CharT> base;
1871
1872 _Traits __traits_;
1873 bool __invert_;
1874public:
1875 typedef _STD::__state<_CharT> __state;
1876
1877 explicit __word_boundary(const _Traits& __traits, bool __invert,
1878 __node<_CharT>* __s)
1879 : base(__s), __traits_(__traits), __invert_(__invert) {}
1880
1881 virtual void __exec(__state&) const;
1882
1883 virtual string speak() const
1884 {
1885 ostringstream os;
Howard Hinnant8daa7332010-07-29 01:15:27 +00001886 if (!__invert_)
Howard Hinnant17615b02010-07-27 01:25:38 +00001887 os << "__word_boundary";
1888 else
1889 os << "not __word_boundary";
1890 return os.str();
1891 }
1892};
1893
1894template <class _CharT, class _Traits>
1895void
1896__word_boundary<_CharT, _Traits>::__exec(__state& __s) const
1897{
1898 bool __is_word_b = false;
1899 if (__s.__first_ != __s.__last_)
1900 {
1901 if (__s.__current_ == __s.__last_)
1902 {
1903 if (!(__s.__flags_ & regex_constants::match_not_eow))
1904 {
1905 _CharT __c = __s.__current_[-1];
1906 __is_word_b = __c == '_' ||
1907 __traits_.isctype(__c, ctype_base::alnum);
1908 }
1909 }
Howard Hinnantf3dcca02010-07-29 15:17:28 +00001910 else if (__s.__current_ == __s.__first_ &&
1911 !(__s.__flags_ & regex_constants::match_prev_avail))
Howard Hinnant17615b02010-07-27 01:25:38 +00001912 {
1913 if (!(__s.__flags_ & regex_constants::match_not_bow))
1914 {
1915 _CharT __c = *__s.__current_;
1916 __is_word_b = __c == '_' ||
1917 __traits_.isctype(__c, ctype_base::alnum);
1918 }
1919 }
1920 else
1921 {
1922 _CharT __c1 = __s.__current_[-1];
1923 _CharT __c2 = *__s.__current_;
1924 bool __is_c1_b = __c1 == '_' ||
1925 __traits_.isctype(__c1, ctype_base::alnum);
1926 bool __is_c2_b = __c2 == '_' ||
1927 __traits_.isctype(__c2, ctype_base::alnum);
1928 __is_word_b = __is_c1_b != __is_c2_b;
1929 }
1930 }
1931 if (__is_word_b != __invert_)
1932 {
1933 __s.__do_ = __state::__accept_but_not_consume;
1934 __s.__node_ = this->first();
1935 }
1936 else
1937 {
1938 __s.__do_ = __state::__reject;
1939 __s.__node_ = nullptr;
1940 }
1941}
1942
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001943// __r_anchor
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001944
1945template <class _CharT>
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001946class __r_anchor
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001947 : public __owns_one_state<_CharT>
1948{
1949 typedef __owns_one_state<_CharT> base;
1950
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001951public:
Howard Hinnantac303862010-07-12 15:51:17 +00001952 typedef _STD::__state<_CharT> __state;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001953
Howard Hinnantac303862010-07-12 15:51:17 +00001954 __r_anchor(__node<_CharT>* __s)
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001955 : base(__s) {}
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001956
Howard Hinnantac303862010-07-12 15:51:17 +00001957 virtual void __exec(__state&) const;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001958
1959 virtual string speak() const
1960 {
1961 ostringstream os;
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001962 os << "right anchor";
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001963 return os.str();
1964 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001965};
1966
1967template <class _CharT>
Howard Hinnantac303862010-07-12 15:51:17 +00001968void
1969__r_anchor<_CharT>::__exec(__state& __s) const
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001970{
Howard Hinnantac303862010-07-12 15:51:17 +00001971 if (__s.__current_ == __s.__last_)
1972 {
1973 __s.__do_ = __state::__accept_but_not_consume;
1974 __s.__node_ = this->first();
1975 }
1976 else
1977 {
1978 __s.__do_ = __state::__reject;
1979 __s.__node_ = nullptr;
1980 }
1981}
1982
1983// __match_any
1984
1985template <class _CharT>
1986class __match_any
1987 : public __owns_one_state<_CharT>
1988{
1989 typedef __owns_one_state<_CharT> base;
1990
1991public:
1992 typedef _STD::__state<_CharT> __state;
1993
1994 __match_any(__node<_CharT>* __s)
1995 : base(__s) {}
1996
1997 virtual void __exec(__state&) const;
1998
1999 virtual string speak() const
2000 {
2001 ostringstream os;
2002 os << "match any";
2003 return os.str();
2004 }
2005};
2006
2007template <class _CharT>
2008void
2009__match_any<_CharT>::__exec(__state& __s) const
2010{
2011 if (__s.__current_ != __s.__last_ && *__s.__current_ != 0)
2012 {
2013 __s.__do_ = __state::__accept_and_consume;
2014 ++__s.__current_;
2015 __s.__node_ = this->first();
2016 }
2017 else
2018 {
2019 __s.__do_ = __state::__reject;
2020 __s.__node_ = nullptr;
2021 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002022}
2023
Howard Hinnant17615b02010-07-27 01:25:38 +00002024// __match_any_but_newline
2025
2026template <class _CharT>
2027class __match_any_but_newline
2028 : public __owns_one_state<_CharT>
2029{
2030 typedef __owns_one_state<_CharT> base;
2031
2032public:
2033 typedef _STD::__state<_CharT> __state;
2034
2035 __match_any_but_newline(__node<_CharT>* __s)
2036 : base(__s) {}
2037
2038 virtual void __exec(__state&) const;
2039
2040 virtual string speak() const
2041 {
2042 ostringstream os;
2043 os << "match any but newline";
2044 return os.str();
2045 }
2046};
2047
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002048// __match_char
2049
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00002050template <class _CharT>
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002051class __match_char
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002052 : public __owns_one_state<_CharT>
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002053{
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002054 typedef __owns_one_state<_CharT> base;
2055
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002056 _CharT __c_;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002057
2058 __match_char(const __match_char&);
2059 __match_char& operator=(const __match_char&);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002060public:
Howard Hinnantac303862010-07-12 15:51:17 +00002061 typedef _STD::__state<_CharT> __state;
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00002062
Howard Hinnantac303862010-07-12 15:51:17 +00002063 __match_char(_CharT __c, __node<_CharT>* __s)
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002064 : base(__s), __c_(__c) {}
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002065
Howard Hinnantac303862010-07-12 15:51:17 +00002066 virtual void __exec(__state&) const;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00002067
2068 virtual string speak() const
2069 {
2070 ostringstream os;
2071 os << "match char " << __c_;
2072 return os.str();
2073 }
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002074};
2075
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00002076template <class _CharT>
Howard Hinnantac303862010-07-12 15:51:17 +00002077void
2078__match_char<_CharT>::__exec(__state& __s) const
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00002079{
Howard Hinnantac303862010-07-12 15:51:17 +00002080 if (__s.__current_ != __s.__last_ && *__s.__current_ == __c_)
2081 {
2082 __s.__do_ = __state::__accept_and_consume;
2083 ++__s.__current_;
2084 __s.__node_ = this->first();
2085 }
2086 else
2087 {
2088 __s.__do_ = __state::__reject;
2089 __s.__node_ = nullptr;
2090 }
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00002091}
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00002092
Howard Hinnante34f17d2010-07-12 19:11:27 +00002093// __match_char_icase
2094
2095template <class _CharT, class _Traits>
2096class __match_char_icase
2097 : public __owns_one_state<_CharT>
2098{
2099 typedef __owns_one_state<_CharT> base;
2100
2101 _Traits __traits_;
2102 _CharT __c_;
2103
2104 __match_char_icase(const __match_char_icase&);
2105 __match_char_icase& operator=(const __match_char_icase&);
2106public:
2107 typedef _STD::__state<_CharT> __state;
2108
2109 __match_char_icase(const _Traits& __traits, _CharT __c, __node<_CharT>* __s)
2110 : base(__s), __traits_(__traits), __c_(__traits.translate_nocase(__c)) {}
2111
2112 virtual void __exec(__state&) const;
2113
2114 virtual string speak() const
2115 {
2116 ostringstream os;
2117 os << "match char icase " << __c_;
2118 return os.str();
2119 }
2120};
2121
2122template <class _CharT, class _Traits>
2123void
2124__match_char_icase<_CharT, _Traits>::__exec(__state& __s) const
2125{
2126 if (__s.__current_ != __s.__last_ &&
2127 __traits_.translate_nocase(*__s.__current_) == __c_)
2128 {
2129 __s.__do_ = __state::__accept_and_consume;
2130 ++__s.__current_;
2131 __s.__node_ = this->first();
2132 }
2133 else
2134 {
2135 __s.__do_ = __state::__reject;
2136 __s.__node_ = nullptr;
2137 }
2138}
2139
2140// __match_char_collate
2141
2142template <class _CharT, class _Traits>
2143class __match_char_collate
2144 : public __owns_one_state<_CharT>
2145{
2146 typedef __owns_one_state<_CharT> base;
2147
2148 _Traits __traits_;
2149 _CharT __c_;
2150
2151 __match_char_collate(const __match_char_collate&);
2152 __match_char_collate& operator=(const __match_char_collate&);
2153public:
2154 typedef _STD::__state<_CharT> __state;
2155
2156 __match_char_collate(const _Traits& __traits, _CharT __c, __node<_CharT>* __s)
2157 : base(__s), __traits_(__traits), __c_(__traits.translate(__c)) {}
2158
2159 virtual void __exec(__state&) const;
2160
2161 virtual string speak() const
2162 {
2163 ostringstream os;
2164 os << "match char icase " << __c_;
2165 return os.str();
2166 }
2167};
2168
2169template <class _CharT, class _Traits>
2170void
2171__match_char_collate<_CharT, _Traits>::__exec(__state& __s) const
2172{
2173 if (__s.__current_ != __s.__last_ &&
2174 __traits_.translate(*__s.__current_) == __c_)
2175 {
2176 __s.__do_ = __state::__accept_and_consume;
2177 ++__s.__current_;
2178 __s.__node_ = this->first();
2179 }
2180 else
2181 {
2182 __s.__do_ = __state::__reject;
2183 __s.__node_ = nullptr;
2184 }
2185}
2186
Howard Hinnant173968a2010-07-13 21:48:06 +00002187// __bracket_expression
2188
2189template <class _CharT, class _Traits>
2190class __bracket_expression
2191 : public __owns_one_state<_CharT>
2192{
2193 typedef __owns_one_state<_CharT> base;
2194 typedef typename _Traits::string_type string_type;
2195
2196 _Traits __traits_;
2197 vector<_CharT> __chars_;
Howard Hinnant15476f32010-07-28 17:35:27 +00002198 vector<_CharT> __neg_chars_;
Howard Hinnant173968a2010-07-13 21:48:06 +00002199 vector<pair<string_type, string_type> > __ranges_;
2200 vector<pair<_CharT, _CharT> > __digraphs_;
2201 vector<string_type> __equivalences_;
2202 ctype_base::mask __mask_;
Howard Hinnant15476f32010-07-28 17:35:27 +00002203 ctype_base::mask __neg_mask_;
Howard Hinnant173968a2010-07-13 21:48:06 +00002204 bool __negate_;
2205 bool __icase_;
2206 bool __collate_;
Howard Hinnant68025ed2010-07-14 15:45:11 +00002207 bool __might_have_digraph_;
Howard Hinnant173968a2010-07-13 21:48:06 +00002208
2209 __bracket_expression(const __bracket_expression&);
2210 __bracket_expression& operator=(const __bracket_expression&);
2211public:
2212 typedef _STD::__state<_CharT> __state;
2213
2214 __bracket_expression(const _Traits& __traits, __node<_CharT>* __s,
2215 bool __negate, bool __icase, bool __collate)
Howard Hinnant15476f32010-07-28 17:35:27 +00002216 : base(__s), __traits_(__traits), __mask_(), __neg_mask_(),
2217 __negate_(__negate), __icase_(__icase), __collate_(__collate),
Howard Hinnant68025ed2010-07-14 15:45:11 +00002218 __might_have_digraph_(__traits_.getloc().name() != "C") {}
Howard Hinnant173968a2010-07-13 21:48:06 +00002219
2220 virtual void __exec(__state&) const;
2221
Howard Hinnant15476f32010-07-28 17:35:27 +00002222 bool __negated() const {return __negate_;}
2223
Howard Hinnant173968a2010-07-13 21:48:06 +00002224 void __add_char(_CharT __c)
2225 {
2226 if (__icase_)
2227 __chars_.push_back(__traits_.translate_nocase(__c));
2228 else if (__collate_)
2229 __chars_.push_back(__traits_.translate(__c));
2230 else
2231 __chars_.push_back(__c);
2232 }
Howard Hinnant15476f32010-07-28 17:35:27 +00002233 void __add_neg_char(_CharT __c)
2234 {
2235 if (__icase_)
2236 __neg_chars_.push_back(__traits_.translate_nocase(__c));
2237 else if (__collate_)
2238 __neg_chars_.push_back(__traits_.translate(__c));
2239 else
2240 __neg_chars_.push_back(__c);
2241 }
Howard Hinnant173968a2010-07-13 21:48:06 +00002242 void __add_range(string_type __b, string_type __e)
2243 {
2244 if (__collate_)
2245 {
2246 if (__icase_)
2247 {
2248 for (size_t __i = 0; __i < __b.size(); ++__i)
2249 __b[__i] = __traits_.translate_nocase(__b[__i]);
2250 for (size_t __i = 0; __i < __e.size(); ++__i)
2251 __e[__i] = __traits_.translate_nocase(__e[__i]);
2252 }
2253 else
2254 {
2255 for (size_t __i = 0; __i < __b.size(); ++__i)
2256 __b[__i] = __traits_.translate(__b[__i]);
2257 for (size_t __i = 0; __i < __e.size(); ++__i)
2258 __e[__i] = __traits_.translate(__e[__i]);
2259 }
2260 __ranges_.push_back(make_pair(
2261 __traits_.transform(__b.begin(), __b.end()),
2262 __traits_.transform(__e.begin(), __e.end())));
2263 }
2264 else
2265 {
Howard Hinnantd4444702010-08-11 17:04:31 +00002266#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant173968a2010-07-13 21:48:06 +00002267 if (__b.size() != 1 || __e.size() != 1)
2268 throw regex_error(regex_constants::error_collate);
Howard Hinnantd4444702010-08-11 17:04:31 +00002269#endif
Howard Hinnant173968a2010-07-13 21:48:06 +00002270 if (__icase_)
2271 {
2272 __b[0] = __traits_.translate_nocase(__b[0]);
2273 __e[0] = __traits_.translate_nocase(__e[0]);
2274 }
2275 __ranges_.push_back(make_pair(_STD::move(__b), _STD::move(__e)));
2276 }
2277 }
2278 void __add_digraph(_CharT __c1, _CharT __c2)
2279 {
2280 if (__icase_)
2281 __digraphs_.push_back(make_pair(__traits_.translate_nocase(__c1),
2282 __traits_.translate_nocase(__c2)));
2283 else if (__collate_)
2284 __digraphs_.push_back(make_pair(__traits_.translate(__c1),
2285 __traits_.translate(__c2)));
2286 else
2287 __digraphs_.push_back(make_pair(__c1, __c2));
2288 }
2289 void __add_equivalence(const string_type& __s)
2290 {__equivalences_.push_back(__s);}
2291 void __add_class(ctype_base::mask __mask)
2292 {__mask_ |= __mask;}
Howard Hinnant15476f32010-07-28 17:35:27 +00002293 void __add_neg_class(ctype_base::mask __mask)
2294 {__neg_mask_ |= __mask;}
Howard Hinnant173968a2010-07-13 21:48:06 +00002295
2296 virtual string speak() const
2297 {
2298 ostringstream os;
2299 os << "__bracket_expression ";
2300 return os.str();
2301 }
2302};
2303
2304template <class _CharT, class _Traits>
2305void
2306__bracket_expression<_CharT, _Traits>::__exec(__state& __s) const
2307{
2308 bool __found = false;
2309 unsigned __consumed = 0;
2310 if (__s.__current_ != __s.__last_)
2311 {
2312 ++__consumed;
Howard Hinnant68025ed2010-07-14 15:45:11 +00002313 if (__might_have_digraph_)
Howard Hinnant173968a2010-07-13 21:48:06 +00002314 {
Howard Hinnant68025ed2010-07-14 15:45:11 +00002315 const _CharT* __next = next(__s.__current_);
2316 if (__next != __s.__last_)
Howard Hinnant173968a2010-07-13 21:48:06 +00002317 {
Howard Hinnant68025ed2010-07-14 15:45:11 +00002318 pair<_CharT, _CharT> __ch2(*__s.__current_, *__next);
2319 if (__icase_)
Howard Hinnant173968a2010-07-13 21:48:06 +00002320 {
Howard Hinnant68025ed2010-07-14 15:45:11 +00002321 __ch2.first = __traits_.translate_nocase(__ch2.first);
2322 __ch2.second = __traits_.translate_nocase(__ch2.second);
2323 }
2324 else if (__collate_)
2325 {
2326 __ch2.first = __traits_.translate(__ch2.first);
2327 __ch2.second = __traits_.translate(__ch2.second);
2328 }
2329 if (!__traits_.lookup_collatename(&__ch2.first, &__ch2.first+2).empty())
2330 {
2331 // __ch2 is a digraph in this locale
2332 ++__consumed;
2333 for (size_t __i = 0; __i < __digraphs_.size(); ++__i)
2334 {
2335 if (__ch2 == __digraphs_[__i])
2336 {
2337 __found = true;
2338 goto __exit;
2339 }
2340 }
2341 if (__collate_ && !__ranges_.empty())
2342 {
2343 string_type __s2 = __traits_.transform(&__ch2.first,
2344 &__ch2.first + 2);
2345 for (size_t __i = 0; __i < __ranges_.size(); ++__i)
2346 {
2347 if (__ranges_[__i].first <= __s2 &&
2348 __s2 <= __ranges_[__i].second)
2349 {
2350 __found = true;
2351 goto __exit;
2352 }
2353 }
2354 }
2355 if (!__equivalences_.empty())
2356 {
2357 string_type __s2 = __traits_.transform_primary(&__ch2.first,
2358 &__ch2.first + 2);
2359 for (size_t __i = 0; __i < __equivalences_.size(); ++__i)
2360 {
2361 if (__s2 == __equivalences_[__i])
2362 {
2363 __found = true;
2364 goto __exit;
2365 }
2366 }
2367 }
2368 if (__traits_.isctype(__ch2.first, __mask_) &&
2369 __traits_.isctype(__ch2.second, __mask_))
Howard Hinnant173968a2010-07-13 21:48:06 +00002370 {
2371 __found = true;
2372 goto __exit;
2373 }
Howard Hinnant15476f32010-07-28 17:35:27 +00002374 if (!__traits_.isctype(__ch2.first, __neg_mask_) &&
2375 !__traits_.isctype(__ch2.second, __neg_mask_))
2376 {
2377 __found = true;
2378 goto __exit;
2379 }
Howard Hinnant173968a2010-07-13 21:48:06 +00002380 goto __exit;
2381 }
Howard Hinnant173968a2010-07-13 21:48:06 +00002382 }
2383 }
2384 // test *__s.__current_ as not a digraph
2385 _CharT __ch = *__s.__current_;
2386 if (__icase_)
2387 __ch = __traits_.translate_nocase(__ch);
2388 else if (__collate_)
2389 __ch = __traits_.translate(__ch);
2390 for (size_t __i = 0; __i < __chars_.size(); ++__i)
2391 {
2392 if (__ch == __chars_[__i])
2393 {
2394 __found = true;
2395 goto __exit;
2396 }
2397 }
Howard Hinnant15476f32010-07-28 17:35:27 +00002398 if (!__neg_chars_.empty())
2399 {
2400 for (size_t __i = 0; __i < __neg_chars_.size(); ++__i)
2401 {
2402 if (__ch == __neg_chars_[__i])
2403 goto __is_neg_char;
2404 }
2405 __found = true;
2406 goto __exit;
2407 }
2408__is_neg_char:
Howard Hinnant173968a2010-07-13 21:48:06 +00002409 if (!__ranges_.empty())
2410 {
2411 string_type __s2 = __collate_ ?
2412 __traits_.transform(&__ch, &__ch + 1) :
2413 string_type(1, __ch);
2414 for (size_t __i = 0; __i < __ranges_.size(); ++__i)
2415 {
2416 if (__ranges_[__i].first <= __s2 && __s2 <= __ranges_[__i].second)
2417 {
2418 __found = true;
2419 goto __exit;
2420 }
2421 }
2422 }
2423 if (!__equivalences_.empty())
2424 {
2425 string_type __s2 = __traits_.transform_primary(&__ch, &__ch + 1);
2426 for (size_t __i = 0; __i < __equivalences_.size(); ++__i)
2427 {
2428 if (__s2 == __equivalences_[__i])
2429 {
2430 __found = true;
2431 goto __exit;
2432 }
2433 }
2434 }
2435 if (__traits_.isctype(__ch, __mask_))
Howard Hinnant15476f32010-07-28 17:35:27 +00002436 {
Howard Hinnant173968a2010-07-13 21:48:06 +00002437 __found = true;
Howard Hinnant15476f32010-07-28 17:35:27 +00002438 goto __exit;
2439 }
2440 if (__neg_mask_ && !__traits_.isctype(__ch, __neg_mask_))
2441 {
2442 __found = true;
2443 goto __exit;
2444 }
Howard Hinnant173968a2010-07-13 21:48:06 +00002445 }
2446 else
2447 __found = __negate_; // force reject
2448__exit:
2449 if (__found != __negate_)
2450 {
Howard Hinnant173968a2010-07-13 21:48:06 +00002451 __s.__do_ = __state::__accept_and_consume;
2452 __s.__current_ += __consumed;
2453 __s.__node_ = this->first();
2454 }
2455 else
2456 {
2457 __s.__do_ = __state::__reject;
2458 __s.__node_ = nullptr;
2459 }
2460}
2461
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002462template <class, class> class __lookahead;
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00002463
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002464template <class _CharT, class _Traits = regex_traits<_CharT> >
2465class basic_regex
2466{
2467public:
2468 // types:
2469 typedef _CharT value_type;
2470 typedef regex_constants::syntax_option_type flag_type;
2471 typedef typename _Traits::locale_type locale_type;
2472
2473private:
2474 _Traits __traits_;
2475 flag_type __flags_;
2476 unsigned __marked_count_;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002477 unsigned __loop_count_;
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002478 int __open_count_;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002479 shared_ptr<__empty_state<_CharT> > __start_;
2480 __owns_one_state<_CharT>* __end_;
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00002481 bool __left_anchor_;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002482
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00002483 typedef _STD::__state<_CharT> __state;
Howard Hinnantac303862010-07-12 15:51:17 +00002484 typedef _STD::__node<_CharT> __node;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002485
2486public:
2487 // constants:
2488 static const/*expr*/ regex_constants::syntax_option_type icase = regex_constants::icase;
2489 static const/*expr*/ regex_constants::syntax_option_type nosubs = regex_constants::nosubs;
2490 static const/*expr*/ regex_constants::syntax_option_type optimize = regex_constants::optimize;
2491 static const/*expr*/ regex_constants::syntax_option_type collate = regex_constants::collate;
2492 static const/*expr*/ regex_constants::syntax_option_type ECMAScript = regex_constants::ECMAScript;
2493 static const/*expr*/ regex_constants::syntax_option_type basic = regex_constants::basic;
2494 static const/*expr*/ regex_constants::syntax_option_type extended = regex_constants::extended;
2495 static const/*expr*/ regex_constants::syntax_option_type awk = regex_constants::awk;
2496 static const/*expr*/ regex_constants::syntax_option_type grep = regex_constants::grep;
2497 static const/*expr*/ regex_constants::syntax_option_type egrep = regex_constants::egrep;
2498
2499 // construct/copy/destroy:
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002500 basic_regex()
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00002501 : __flags_(), __marked_count_(0), __loop_count_(0), __open_count_(0),
2502 __end_(0), __left_anchor_(false)
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002503 {}
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002504 explicit basic_regex(const value_type* __p, flag_type __f = regex_constants::ECMAScript)
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00002505 : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
2506 __end_(0), __left_anchor_(false)
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002507 {__parse(__p, __p + __traits_.length(__p));}
2508 basic_regex(const value_type* __p, size_t __len, flag_type __f)
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00002509 : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
2510 __end_(0), __left_anchor_(false)
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002511 {__parse(__p, __p + __len);}
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002512// basic_regex(const basic_regex&) = default;
2513// basic_regex(basic_regex&&) = default;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002514 template <class _ST, class _SA>
2515 explicit basic_regex(const basic_string<value_type, _ST, _SA>& __p,
2516 flag_type __f = regex_constants::ECMAScript)
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00002517 : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
2518 __end_(0), __left_anchor_(false)
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002519 {__parse(__p.begin(), __p.end());}
2520 template <class _ForwardIterator>
2521 basic_regex(_ForwardIterator __first, _ForwardIterator __last,
2522 flag_type __f = regex_constants::ECMAScript)
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00002523 : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
2524 __end_(0), __left_anchor_(false)
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002525 {__parse(__first, __last);}
2526 basic_regex(initializer_list<value_type> __il,
2527 flag_type __f = regex_constants::ECMAScript)
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00002528 : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
2529 __end_(0), __left_anchor_(false)
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002530 {__parse(__il.begin(), __il.end());}
2531
Howard Hinnant7026a172010-08-13 18:11:23 +00002532// ~basic_regex() = default;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002533
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002534// basic_regex& operator=(const basic_regex&) = default;
2535// basic_regex& operator=(basic_regex&&) = default;
Howard Hinnant7026a172010-08-13 18:11:23 +00002536 basic_regex& operator=(const value_type* __p)
2537 {return assign(__p);}
2538 basic_regex& operator=(initializer_list<value_type> __il)
2539 {return assign(__il);}
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002540 template <class _ST, class _SA>
Howard Hinnant7026a172010-08-13 18:11:23 +00002541 basic_regex& operator=(const basic_string<value_type, _ST, _SA>& __p)
2542 {return assign(__p);}
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002543
2544 // assign:
Howard Hinnant7026a172010-08-13 18:11:23 +00002545 basic_regex& assign(const basic_regex& __that)
2546 {return *this = __that;}
2547 basic_regex& assign(const value_type* __p, flag_type __f = regex_constants::ECMAScript)
2548 {return assign(__p, __p + __traits_.length(__p), __f);}
2549 basic_regex& assign(const value_type* __p, size_t __len, flag_type __f)
2550 {return assign(__p, __p + __len, __f);}
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002551 template <class _ST, class _SA>
2552 basic_regex& assign(const basic_string<value_type, _ST, _SA>& __s,
Howard Hinnant7026a172010-08-13 18:11:23 +00002553 flag_type __f = regex_constants::ECMAScript)
2554 {return assign(__s.begin(), __s.end(), __f);}
2555
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002556 template <class _InputIterator>
Howard Hinnant7026a172010-08-13 18:11:23 +00002557 typename enable_if
2558 <
2559 __is_input_iterator <_InputIterator>::value &&
2560 !__is_forward_iterator<_InputIterator>::value,
2561 basic_regex&
2562 >::type
2563 assign(_InputIterator __first, _InputIterator __last,
2564 flag_type __f = regex_constants::ECMAScript)
2565 {
2566 basic_string<_CharT> __t(__first, __last);
2567 return assign(__t.begin(), __t.end(), __f);
2568 }
2569
2570private:
2571 void __member_init(flag_type __f)
2572 {
2573 __flags_ = __f;
2574 __marked_count_ = 0;
2575 __loop_count_ = 0;
2576 __open_count_ = 0;
2577 __end_ = nullptr;
2578 __left_anchor_ = false;
2579 }
2580public:
2581
2582 template <class _ForwardIterator>
2583 typename enable_if
2584 <
2585 __is_forward_iterator<_ForwardIterator>::value,
2586 basic_regex&
2587 >::type
2588 assign(_ForwardIterator __first, _ForwardIterator __last,
2589 flag_type __f = regex_constants::ECMAScript)
2590 {
2591 __member_init(__f);
2592 __parse(__first, __last);
2593 }
2594
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002595 basic_regex& assign(initializer_list<value_type> __il,
Howard Hinnant7026a172010-08-13 18:11:23 +00002596 flag_type __f = regex_constants::ECMAScript)
2597 {return assign(__il.begin(), __il.end(), __f);}
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002598
2599 // const operations:
2600 unsigned mark_count() const {return __marked_count_;}
2601 flag_type flags() const {return __flags_;}
2602
2603 // locale:
Howard Hinnant7026a172010-08-13 18:11:23 +00002604 locale_type imbue(locale_type __loc)
2605 {
2606 __member_init(ECMAScript);
2607 __start_.reset();
2608 return __traits_.imbue(__loc);
2609 }
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002610 locale_type getloc() const {return __traits_.getloc();}
2611
2612 // swap:
Howard Hinnant7026a172010-08-13 18:11:23 +00002613 void swap(basic_regex& __r);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002614
2615private:
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002616 unsigned __loop_count() const {return __loop_count_;}
2617
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002618 template <class _ForwardIterator>
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002619 _ForwardIterator
2620 __parse(_ForwardIterator __first, _ForwardIterator __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002621 template <class _ForwardIterator>
2622 _ForwardIterator
2623 __parse_basic_reg_exp(_ForwardIterator __first, _ForwardIterator __last);
2624 template <class _ForwardIterator>
2625 _ForwardIterator
2626 __parse_RE_expression(_ForwardIterator __first, _ForwardIterator __last);
2627 template <class _ForwardIterator>
2628 _ForwardIterator
2629 __parse_simple_RE(_ForwardIterator __first, _ForwardIterator __last);
2630 template <class _ForwardIterator>
2631 _ForwardIterator
2632 __parse_nondupl_RE(_ForwardIterator __first, _ForwardIterator __last);
2633 template <class _ForwardIterator>
2634 _ForwardIterator
2635 __parse_one_char_or_coll_elem_RE(_ForwardIterator __first, _ForwardIterator __last);
2636 template <class _ForwardIterator>
2637 _ForwardIterator
2638 __parse_Back_open_paren(_ForwardIterator __first, _ForwardIterator __last);
2639 template <class _ForwardIterator>
2640 _ForwardIterator
2641 __parse_Back_close_paren(_ForwardIterator __first, _ForwardIterator __last);
2642 template <class _ForwardIterator>
2643 _ForwardIterator
2644 __parse_Back_open_brace(_ForwardIterator __first, _ForwardIterator __last);
2645 template <class _ForwardIterator>
2646 _ForwardIterator
2647 __parse_Back_close_brace(_ForwardIterator __first, _ForwardIterator __last);
2648 template <class _ForwardIterator>
2649 _ForwardIterator
2650 __parse_BACKREF(_ForwardIterator __first, _ForwardIterator __last);
2651 template <class _ForwardIterator>
2652 _ForwardIterator
2653 __parse_ORD_CHAR(_ForwardIterator __first, _ForwardIterator __last);
2654 template <class _ForwardIterator>
2655 _ForwardIterator
2656 __parse_QUOTED_CHAR(_ForwardIterator __first, _ForwardIterator __last);
2657 template <class _ForwardIterator>
2658 _ForwardIterator
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002659 __parse_RE_dupl_symbol(_ForwardIterator __first, _ForwardIterator __last,
Howard Hinnante77aa5e2010-07-08 17:43:58 +00002660 __owns_one_state<_CharT>* __s,
2661 unsigned __mexp_begin, unsigned __mexp_end);
Howard Hinnant0de86b62010-06-25 20:56:08 +00002662 template <class _ForwardIterator>
2663 _ForwardIterator
Howard Hinnantaa698082010-07-16 19:08:36 +00002664 __parse_ERE_dupl_symbol(_ForwardIterator __first, _ForwardIterator __last,
2665 __owns_one_state<_CharT>* __s,
2666 unsigned __mexp_begin, unsigned __mexp_end);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002667 template <class _ForwardIterator>
2668 _ForwardIterator
Howard Hinnant0de86b62010-06-25 20:56:08 +00002669 __parse_bracket_expression(_ForwardIterator __first, _ForwardIterator __last);
2670 template <class _ForwardIterator>
2671 _ForwardIterator
Howard Hinnant173968a2010-07-13 21:48:06 +00002672 __parse_follow_list(_ForwardIterator __first, _ForwardIterator __last,
2673 __bracket_expression<_CharT, _Traits>* __ml);
Howard Hinnant0de86b62010-06-25 20:56:08 +00002674 template <class _ForwardIterator>
2675 _ForwardIterator
Howard Hinnant173968a2010-07-13 21:48:06 +00002676 __parse_expression_term(_ForwardIterator __first, _ForwardIterator __last,
2677 __bracket_expression<_CharT, _Traits>* __ml);
Howard Hinnant0de86b62010-06-25 20:56:08 +00002678 template <class _ForwardIterator>
2679 _ForwardIterator
Howard Hinnant173968a2010-07-13 21:48:06 +00002680 __parse_equivalence_class(_ForwardIterator __first, _ForwardIterator __last,
2681 __bracket_expression<_CharT, _Traits>* __ml);
Howard Hinnant0de86b62010-06-25 20:56:08 +00002682 template <class _ForwardIterator>
2683 _ForwardIterator
Howard Hinnant173968a2010-07-13 21:48:06 +00002684 __parse_character_class(_ForwardIterator __first, _ForwardIterator __last,
2685 __bracket_expression<_CharT, _Traits>* __ml);
Howard Hinnant0de86b62010-06-25 20:56:08 +00002686 template <class _ForwardIterator>
2687 _ForwardIterator
Howard Hinnant173968a2010-07-13 21:48:06 +00002688 __parse_collating_symbol(_ForwardIterator __first, _ForwardIterator __last,
2689 basic_string<_CharT>& __col_sym);
Howard Hinnant0de86b62010-06-25 20:56:08 +00002690 template <class _ForwardIterator>
2691 _ForwardIterator
2692 __parse_DUP_COUNT(_ForwardIterator __first, _ForwardIterator __last, int& __c);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002693 template <class _ForwardIterator>
2694 _ForwardIterator
2695 __parse_extended_reg_exp(_ForwardIterator __first, _ForwardIterator __last);
2696 template <class _ForwardIterator>
2697 _ForwardIterator
2698 __parse_ERE_branch(_ForwardIterator __first, _ForwardIterator __last);
2699 template <class _ForwardIterator>
2700 _ForwardIterator
2701 __parse_ERE_expression(_ForwardIterator __first, _ForwardIterator __last);
2702 template <class _ForwardIterator>
2703 _ForwardIterator
2704 __parse_one_char_or_coll_elem_ERE(_ForwardIterator __first, _ForwardIterator __last);
2705 template <class _ForwardIterator>
2706 _ForwardIterator
2707 __parse_ORD_CHAR_ERE(_ForwardIterator __first, _ForwardIterator __last);
2708 template <class _ForwardIterator>
2709 _ForwardIterator
2710 __parse_QUOTED_CHAR_ERE(_ForwardIterator __first, _ForwardIterator __last);
Howard Hinnant2ade7c22010-07-22 17:53:24 +00002711 template <class _ForwardIterator>
2712 _ForwardIterator
2713 __parse_ecma_exp(_ForwardIterator __first, _ForwardIterator __last);
2714 template <class _ForwardIterator>
2715 _ForwardIterator
2716 __parse_alternative(_ForwardIterator __first, _ForwardIterator __last);
2717 template <class _ForwardIterator>
2718 _ForwardIterator
2719 __parse_term(_ForwardIterator __first, _ForwardIterator __last);
2720 template <class _ForwardIterator>
2721 _ForwardIterator
2722 __parse_assertion(_ForwardIterator __first, _ForwardIterator __last);
2723 template <class _ForwardIterator>
2724 _ForwardIterator
2725 __parse_atom(_ForwardIterator __first, _ForwardIterator __last);
2726 template <class _ForwardIterator>
2727 _ForwardIterator
Howard Hinnant17615b02010-07-27 01:25:38 +00002728 __parse_atom_escape(_ForwardIterator __first, _ForwardIterator __last);
2729 template <class _ForwardIterator>
2730 _ForwardIterator
2731 __parse_decimal_escape(_ForwardIterator __first, _ForwardIterator __last);
2732 template <class _ForwardIterator>
2733 _ForwardIterator
2734 __parse_character_class_escape(_ForwardIterator __first, _ForwardIterator __last);
2735 template <class _ForwardIterator>
2736 _ForwardIterator
Howard Hinnant15476f32010-07-28 17:35:27 +00002737 __parse_character_escape(_ForwardIterator __first, _ForwardIterator __last,
2738 basic_string<_CharT>* __str = nullptr);
Howard Hinnant17615b02010-07-27 01:25:38 +00002739 template <class _ForwardIterator>
2740 _ForwardIterator
2741 __parse_pattern_character(_ForwardIterator __first, _ForwardIterator __last);
Howard Hinnant856846b2010-07-27 19:53:10 +00002742 template <class _ForwardIterator>
2743 _ForwardIterator
2744 __parse_grep(_ForwardIterator __first, _ForwardIterator __last);
2745 template <class _ForwardIterator>
2746 _ForwardIterator
2747 __parse_egrep(_ForwardIterator __first, _ForwardIterator __last);
Howard Hinnant15476f32010-07-28 17:35:27 +00002748 template <class _ForwardIterator>
2749 _ForwardIterator
2750 __parse_class_escape(_ForwardIterator __first, _ForwardIterator __last,
2751 basic_string<_CharT>& __str,
2752 __bracket_expression<_CharT, _Traits>* __ml);
2753 template <class _ForwardIterator>
2754 _ForwardIterator
2755 __parse_awk_escape(_ForwardIterator __first, _ForwardIterator __last,
2756 basic_string<_CharT>* __str = nullptr);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002757
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00002758 void __push_l_anchor() {__left_anchor_ = true;}
2759 void __push_r_anchor();
Howard Hinnantac303862010-07-12 15:51:17 +00002760 void __push_match_any();
Howard Hinnant17615b02010-07-27 01:25:38 +00002761 void __push_match_any_but_newline();
Howard Hinnante77aa5e2010-07-08 17:43:58 +00002762 void __push_greedy_inf_repeat(size_t __min, __owns_one_state<_CharT>* __s,
2763 unsigned __mexp_begin = 0, unsigned __mexp_end = 0)
2764 {__push_loop(__min, numeric_limits<size_t>::max(), __s,
2765 __mexp_begin, __mexp_end);}
Howard Hinnant17615b02010-07-27 01:25:38 +00002766 void __push_nongreedy_inf_repeat(size_t __min, __owns_one_state<_CharT>* __s,
2767 unsigned __mexp_begin = 0, unsigned __mexp_end = 0)
2768 {__push_loop(__min, numeric_limits<size_t>::max(), __s,
2769 __mexp_begin, __mexp_end, false);}
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002770 void __push_loop(size_t __min, size_t __max, __owns_one_state<_CharT>* __s,
2771 size_t __mexp_begin = 0, size_t __mexp_end = 0,
2772 bool __greedy = true);
Howard Hinnant173968a2010-07-13 21:48:06 +00002773 __bracket_expression<_CharT, _Traits>* __start_matching_list(bool __negate);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002774 void __push_char(value_type __c);
Howard Hinnantcba352d2010-07-12 18:16:05 +00002775 void __push_back_ref(int __i);
Howard Hinnantaa698082010-07-16 19:08:36 +00002776 void __push_alternation(__owns_one_state<_CharT>* __sa,
2777 __owns_one_state<_CharT>* __sb);
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00002778 void __push_begin_marked_subexpression();
2779 void __push_end_marked_subexpression(unsigned);
Howard Hinnant2ade7c22010-07-22 17:53:24 +00002780 void __push_empty();
Howard Hinnant17615b02010-07-27 01:25:38 +00002781 void __push_word_boundary(bool);
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002782 void __push_lookahead(const basic_regex&, bool);
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00002783
Howard Hinnant22ce0b42010-07-14 21:14:52 +00002784 template <class _Allocator>
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00002785 bool
Howard Hinnant22ce0b42010-07-14 21:14:52 +00002786 __search(const _CharT* __first, const _CharT* __last,
2787 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00002788 regex_constants::match_flag_type __flags) const;
2789
Howard Hinnant22ce0b42010-07-14 21:14:52 +00002790 template <class _Allocator>
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002791 bool
Howard Hinnant22ce0b42010-07-14 21:14:52 +00002792 __match_at_start(const _CharT* __first, const _CharT* __last,
2793 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002794 regex_constants::match_flag_type __flags) const;
Howard Hinnant17615b02010-07-27 01:25:38 +00002795 template <class _Allocator>
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002796 bool
Howard Hinnant17615b02010-07-27 01:25:38 +00002797 __match_at_start_ecma(const _CharT* __first, const _CharT* __last,
2798 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002799 regex_constants::match_flag_type __flags) const;
Howard Hinnant22ce0b42010-07-14 21:14:52 +00002800 template <class _Allocator>
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002801 bool
2802 __match_at_start_posix_nosubs(const _CharT* __first, const _CharT* __last,
Howard Hinnant22ce0b42010-07-14 21:14:52 +00002803 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002804 regex_constants::match_flag_type __flags) const;
Howard Hinnant22ce0b42010-07-14 21:14:52 +00002805 template <class _Allocator>
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002806 bool
Howard Hinnant22ce0b42010-07-14 21:14:52 +00002807 __match_at_start_posix_subs(const _CharT* __first, const _CharT* __last,
2808 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002809 regex_constants::match_flag_type __flags) const;
2810
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00002811 template <class _B, class _A, class _C, class _T>
2812 friend
2813 bool
2814 regex_search(_B, _B, match_results<_B, _A>&, const basic_regex<_C, _T>&,
2815 regex_constants::match_flag_type);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002816
Howard Hinnant22ce0b42010-07-14 21:14:52 +00002817 template <class _A, class _C, class _T>
2818 friend
2819 bool
2820 regex_search(const _C*, const _C*, match_results<const _C*, _A>&,
2821 const basic_regex<_C, _T>&, regex_constants::match_flag_type);
2822
2823 template <class _B, class _C, class _T>
2824 friend
2825 bool
2826 regex_search(_B, _B, const basic_regex<_C, _T>&,
2827 regex_constants::match_flag_type);
2828
2829 template <class _C, class _T>
2830 friend
2831 bool
2832 regex_search(const _C*, const _C*,
2833 const basic_regex<_C, _T>&, regex_constants::match_flag_type);
2834
2835 template <class _C, class _A, class _T>
2836 friend
2837 bool
2838 regex_search(const _C*, match_results<const _C*, _A>&, const basic_regex<_C, _T>&,
2839 regex_constants::match_flag_type);
2840
2841 template <class _ST, class _SA, class _C, class _T>
2842 friend
2843 bool
2844 regex_search(const basic_string<_C, _ST, _SA>& __s,
2845 const basic_regex<_C, _T>& __e,
2846 regex_constants::match_flag_type __flags);
2847
2848 template <class _ST, class _SA, class _A, class _C, class _T>
2849 friend
2850 bool
2851 regex_search(const basic_string<_C, _ST, _SA>& __s,
2852 match_results<typename basic_string<_C, _ST, _SA>::const_iterator, _A>&,
2853 const basic_regex<_C, _T>& __e,
2854 regex_constants::match_flag_type __flags);
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002855
2856 template <class, class> friend class __lookahead;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002857};
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002858
2859template <class _CharT, class _Traits>
Howard Hinnant7026a172010-08-13 18:11:23 +00002860void
2861basic_regex<_CharT, _Traits>::swap(basic_regex& __r)
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002862{
Howard Hinnant7026a172010-08-13 18:11:23 +00002863 using _STD::swap;
2864 swap(__traits_, __r.__traits_);
2865 swap(__flags_, __r.__flags_);
2866 swap(__marked_count_, __r.__marked_count_);
2867 swap(__loop_count_, __r.__loop_count_);
2868 swap(__open_count_, __r.__open_count_);
2869 swap(__start_, __r.__start_);
2870 swap(__end_, __r.__end_);
2871 swap(__left_anchor_, __r.__left_anchor_);
2872}
2873
2874template <class _CharT, class _Traits>
2875inline _LIBCPP_INLINE_VISIBILITY
2876void
2877swap(basic_regex<_CharT, _Traits>& __x, basic_regex<_CharT, _Traits>& __y)
2878{
2879 return __x.swap(__y);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002880}
2881
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002882// __lookahead
2883
2884template <class _CharT, class _Traits>
2885class __lookahead
2886 : public __owns_one_state<_CharT>
2887{
2888 typedef __owns_one_state<_CharT> base;
2889
2890 basic_regex<_CharT, _Traits> __exp_;
2891 bool __invert_;
2892
2893 __lookahead(const __lookahead&);
2894 __lookahead& operator=(const __lookahead&);
2895public:
2896 typedef _STD::__state<_CharT> __state;
2897
2898 __lookahead(const basic_regex<_CharT, _Traits>& __exp, bool __invert, __node<_CharT>* __s)
2899 : base(__s), __exp_(__exp), __invert_(__invert) {}
2900
2901 virtual void __exec(__state&) const;
2902
2903 virtual string speak() const
2904 {
2905 ostringstream os;
2906 if (__invert_)
2907 os << "not lookahead";
2908 else
2909 os << "lookahead";
2910 return os.str();
2911 }
2912};
2913
2914template <class _CharT, class _Traits>
2915void
2916__lookahead<_CharT, _Traits>::__exec(__state& __s) const
2917{
2918 match_results<const _CharT*> __m;
2919 __m.__init(1 + __exp_.mark_count(), __s.__current_, __s.__last_);
2920 bool __matched = __exp_.__match_at_start_ecma(__s.__current_, __s.__last_,
2921 __m, __s.__flags_);
2922 if (__matched != __invert_)
2923 {
2924 __s.__do_ = __state::__accept_but_not_consume;
2925 __s.__node_ = this->first();
2926 }
2927 else
2928 {
2929 __s.__do_ = __state::__reject;
2930 __s.__node_ = nullptr;
2931 }
2932}
2933
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002934template <class _CharT, class _Traits>
2935template <class _ForwardIterator>
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002936_ForwardIterator
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002937basic_regex<_CharT, _Traits>::__parse(_ForwardIterator __first,
2938 _ForwardIterator __last)
2939{
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002940 {
Howard Hinnantac303862010-07-12 15:51:17 +00002941 unique_ptr<__node> __h(new __end_state<_CharT>);
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002942 __start_.reset(new __empty_state<_CharT>(__h.get()));
2943 __h.release();
2944 __end_ = __start_.get();
2945 }
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00002946 switch (__flags_ & 0x1F0)
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002947 {
2948 case ECMAScript:
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002949 __first = __parse_ecma_exp(__first, __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002950 break;
2951 case basic:
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002952 __first = __parse_basic_reg_exp(__first, __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002953 break;
2954 case extended:
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002955 case awk:
Howard Hinnant15476f32010-07-28 17:35:27 +00002956 __first = __parse_extended_reg_exp(__first, __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002957 break;
2958 case grep:
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002959 __first = __parse_grep(__first, __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002960 break;
2961 case egrep:
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002962 __first = __parse_egrep(__first, __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002963 break;
Howard Hinnantd4444702010-08-11 17:04:31 +00002964#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002965 default:
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00002966 throw regex_error(regex_constants::__re_err_grammar);
Howard Hinnantd4444702010-08-11 17:04:31 +00002967#endif
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002968 }
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002969 return __first;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002970}
2971
2972template <class _CharT, class _Traits>
2973template <class _ForwardIterator>
2974_ForwardIterator
2975basic_regex<_CharT, _Traits>::__parse_basic_reg_exp(_ForwardIterator __first,
2976 _ForwardIterator __last)
2977{
2978 if (__first != __last)
2979 {
2980 if (*__first == '^')
2981 {
2982 __push_l_anchor();
2983 ++__first;
2984 }
2985 if (__first != __last)
2986 {
2987 __first = __parse_RE_expression(__first, __last);
2988 if (__first != __last)
2989 {
2990 _ForwardIterator __temp = next(__first);
2991 if (__temp == __last && *__first == '$')
2992 {
2993 __push_r_anchor();
2994 ++__first;
2995 }
2996 }
2997 }
Howard Hinnantd4444702010-08-11 17:04:31 +00002998#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002999 if (__first != __last)
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00003000 throw regex_error(regex_constants::__re_err_empty);
Howard Hinnantd4444702010-08-11 17:04:31 +00003001#endif
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003002 }
3003 return __first;
3004}
3005
3006template <class _CharT, class _Traits>
3007template <class _ForwardIterator>
3008_ForwardIterator
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003009basic_regex<_CharT, _Traits>::__parse_extended_reg_exp(_ForwardIterator __first,
3010 _ForwardIterator __last)
3011{
Howard Hinnantaa698082010-07-16 19:08:36 +00003012 __owns_one_state<_CharT>* __sa = __end_;
3013 _ForwardIterator __temp = __parse_ERE_branch(__first, __last);
Howard Hinnantd4444702010-08-11 17:04:31 +00003014#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantaa698082010-07-16 19:08:36 +00003015 if (__temp == __first)
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00003016 throw regex_error(regex_constants::__re_err_empty);
Howard Hinnantd4444702010-08-11 17:04:31 +00003017#endif
Howard Hinnantaa698082010-07-16 19:08:36 +00003018 __first = __temp;
3019 while (__first != __last && *__first == '|')
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003020 {
Howard Hinnantaa698082010-07-16 19:08:36 +00003021 __owns_one_state<_CharT>* __sb = __end_;
3022 __temp = __parse_ERE_branch(++__first, __last);
Howard Hinnantd4444702010-08-11 17:04:31 +00003023#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003024 if (__temp == __first)
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00003025 throw regex_error(regex_constants::__re_err_empty);
Howard Hinnantd4444702010-08-11 17:04:31 +00003026#endif
Howard Hinnantaa698082010-07-16 19:08:36 +00003027 __push_alternation(__sa, __sb);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003028 __first = __temp;
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003029 }
3030 return __first;
3031}
3032
3033template <class _CharT, class _Traits>
3034template <class _ForwardIterator>
3035_ForwardIterator
3036basic_regex<_CharT, _Traits>::__parse_ERE_branch(_ForwardIterator __first,
3037 _ForwardIterator __last)
3038{
3039 _ForwardIterator __temp = __parse_ERE_expression(__first, __last);
Howard Hinnantd4444702010-08-11 17:04:31 +00003040#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003041 if (__temp == __first)
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00003042 throw regex_error(regex_constants::__re_err_empty);
Howard Hinnantd4444702010-08-11 17:04:31 +00003043#endif
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003044 do
3045 {
3046 __first = __temp;
3047 __temp = __parse_ERE_expression(__first, __last);
3048 } while (__temp != __first);
3049 return __first;
3050}
3051
3052template <class _CharT, class _Traits>
3053template <class _ForwardIterator>
3054_ForwardIterator
3055basic_regex<_CharT, _Traits>::__parse_ERE_expression(_ForwardIterator __first,
3056 _ForwardIterator __last)
3057{
Howard Hinnantaa698082010-07-16 19:08:36 +00003058 __owns_one_state<_CharT>* __e = __end_;
3059 unsigned __mexp_begin = __marked_count_;
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003060 _ForwardIterator __temp = __parse_one_char_or_coll_elem_ERE(__first, __last);
3061 if (__temp == __first && __temp != __last)
3062 {
3063 switch (*__temp)
3064 {
3065 case '^':
3066 __push_l_anchor();
3067 ++__temp;
3068 break;
3069 case '$':
3070 __push_r_anchor();
3071 ++__temp;
3072 break;
3073 case '(':
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00003074 __push_begin_marked_subexpression();
3075 unsigned __temp_count = __marked_count_;
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003076 ++__open_count_;
3077 __temp = __parse_extended_reg_exp(++__temp, __last);
Howard Hinnantd4444702010-08-11 17:04:31 +00003078#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003079 if (__temp == __last || *__temp != ')')
3080 throw regex_error(regex_constants::error_paren);
Howard Hinnantd4444702010-08-11 17:04:31 +00003081#endif
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00003082 __push_end_marked_subexpression(__temp_count);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003083 --__open_count_;
3084 ++__temp;
3085 break;
3086 }
3087 }
3088 if (__temp != __first)
Howard Hinnantaa698082010-07-16 19:08:36 +00003089 __temp = __parse_ERE_dupl_symbol(__temp, __last, __e, __mexp_begin+1,
3090 __marked_count_+1);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003091 __first = __temp;
3092 return __first;
3093}
3094
3095template <class _CharT, class _Traits>
3096template <class _ForwardIterator>
3097_ForwardIterator
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003098basic_regex<_CharT, _Traits>::__parse_RE_expression(_ForwardIterator __first,
3099 _ForwardIterator __last)
3100{
3101 while (true)
3102 {
3103 _ForwardIterator __temp = __parse_simple_RE(__first, __last);
3104 if (__temp == __first)
3105 break;
3106 __first = __temp;
3107 }
3108 return __first;
3109}
3110
3111template <class _CharT, class _Traits>
3112template <class _ForwardIterator>
3113_ForwardIterator
3114basic_regex<_CharT, _Traits>::__parse_simple_RE(_ForwardIterator __first,
3115 _ForwardIterator __last)
3116{
3117 if (__first != __last)
3118 {
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003119 __owns_one_state<_CharT>* __e = __end_;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003120 unsigned __mexp_begin = __marked_count_;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003121 _ForwardIterator __temp = __parse_nondupl_RE(__first, __last);
3122 if (__temp != __first)
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003123 __first = __parse_RE_dupl_symbol(__temp, __last, __e,
3124 __mexp_begin+1, __marked_count_+1);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003125 }
3126 return __first;
3127}
3128
3129template <class _CharT, class _Traits>
3130template <class _ForwardIterator>
3131_ForwardIterator
3132basic_regex<_CharT, _Traits>::__parse_nondupl_RE(_ForwardIterator __first,
3133 _ForwardIterator __last)
3134{
3135 _ForwardIterator __temp = __first;
3136 __first = __parse_one_char_or_coll_elem_RE(__first, __last);
3137 if (__temp == __first)
3138 {
3139 __temp = __parse_Back_open_paren(__first, __last);
3140 if (__temp != __first)
3141 {
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00003142 __push_begin_marked_subexpression();
3143 unsigned __temp_count = __marked_count_;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003144 __first = __parse_RE_expression(__temp, __last);
3145 __temp = __parse_Back_close_paren(__first, __last);
Howard Hinnantd4444702010-08-11 17:04:31 +00003146#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003147 if (__temp == __first)
3148 throw regex_error(regex_constants::error_paren);
Howard Hinnantd4444702010-08-11 17:04:31 +00003149#endif
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00003150 __push_end_marked_subexpression(__temp_count);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003151 __first = __temp;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003152 }
3153 else
3154 __first = __parse_BACKREF(__first, __last);
3155 }
3156 return __first;
3157}
3158
3159template <class _CharT, class _Traits>
3160template <class _ForwardIterator>
3161_ForwardIterator
3162basic_regex<_CharT, _Traits>::__parse_one_char_or_coll_elem_RE(
3163 _ForwardIterator __first,
3164 _ForwardIterator __last)
3165{
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003166 _ForwardIterator __temp = __parse_ORD_CHAR(__first, __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003167 if (__temp == __first)
3168 {
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003169 __temp = __parse_QUOTED_CHAR(__first, __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003170 if (__temp == __first)
3171 {
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003172 if (__temp != __last && *__temp == '.')
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003173 {
3174 __push_match_any();
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003175 ++__temp;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003176 }
3177 else
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003178 __temp = __parse_bracket_expression(__first, __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003179 }
3180 }
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003181 __first = __temp;
3182 return __first;
3183}
3184
3185template <class _CharT, class _Traits>
3186template <class _ForwardIterator>
3187_ForwardIterator
3188basic_regex<_CharT, _Traits>::__parse_one_char_or_coll_elem_ERE(
3189 _ForwardIterator __first,
3190 _ForwardIterator __last)
3191{
3192 _ForwardIterator __temp = __parse_ORD_CHAR_ERE(__first, __last);
3193 if (__temp == __first)
3194 {
3195 __temp = __parse_QUOTED_CHAR_ERE(__first, __last);
3196 if (__temp == __first)
3197 {
3198 if (__temp != __last && *__temp == '.')
3199 {
3200 __push_match_any();
3201 ++__temp;
3202 }
3203 else
3204 __temp = __parse_bracket_expression(__first, __last);
3205 }
3206 }
3207 __first = __temp;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003208 return __first;
3209}
3210
3211template <class _CharT, class _Traits>
3212template <class _ForwardIterator>
3213_ForwardIterator
3214basic_regex<_CharT, _Traits>::__parse_Back_open_paren(_ForwardIterator __first,
3215 _ForwardIterator __last)
3216{
3217 if (__first != __last)
3218 {
3219 _ForwardIterator __temp = next(__first);
3220 if (__temp != __last)
3221 {
3222 if (*__first == '\\' && *__temp == '(')
3223 __first = ++__temp;
3224 }
3225 }
3226 return __first;
3227}
3228
3229template <class _CharT, class _Traits>
3230template <class _ForwardIterator>
3231_ForwardIterator
3232basic_regex<_CharT, _Traits>::__parse_Back_close_paren(_ForwardIterator __first,
3233 _ForwardIterator __last)
3234{
3235 if (__first != __last)
3236 {
3237 _ForwardIterator __temp = next(__first);
3238 if (__temp != __last)
3239 {
3240 if (*__first == '\\' && *__temp == ')')
3241 __first = ++__temp;
3242 }
3243 }
3244 return __first;
3245}
3246
3247template <class _CharT, class _Traits>
3248template <class _ForwardIterator>
3249_ForwardIterator
3250basic_regex<_CharT, _Traits>::__parse_Back_open_brace(_ForwardIterator __first,
3251 _ForwardIterator __last)
3252{
3253 if (__first != __last)
3254 {
3255 _ForwardIterator __temp = next(__first);
3256 if (__temp != __last)
3257 {
3258 if (*__first == '\\' && *__temp == '{')
3259 __first = ++__temp;
3260 }
3261 }
3262 return __first;
3263}
3264
3265template <class _CharT, class _Traits>
3266template <class _ForwardIterator>
3267_ForwardIterator
3268basic_regex<_CharT, _Traits>::__parse_Back_close_brace(_ForwardIterator __first,
3269 _ForwardIterator __last)
3270{
3271 if (__first != __last)
3272 {
3273 _ForwardIterator __temp = next(__first);
3274 if (__temp != __last)
3275 {
3276 if (*__first == '\\' && *__temp == '}')
3277 __first = ++__temp;
3278 }
3279 }
3280 return __first;
3281}
3282
3283template <class _CharT, class _Traits>
3284template <class _ForwardIterator>
3285_ForwardIterator
3286basic_regex<_CharT, _Traits>::__parse_BACKREF(_ForwardIterator __first,
3287 _ForwardIterator __last)
3288{
3289 if (__first != __last)
3290 {
3291 _ForwardIterator __temp = next(__first);
3292 if (__temp != __last)
3293 {
3294 if (*__first == '\\' && '1' <= *__temp && *__temp <= '9')
3295 {
3296 __push_back_ref(*__temp - '0');
3297 __first = ++__temp;
3298 }
3299 }
3300 }
3301 return __first;
3302}
3303
3304template <class _CharT, class _Traits>
3305template <class _ForwardIterator>
3306_ForwardIterator
3307basic_regex<_CharT, _Traits>::__parse_ORD_CHAR(_ForwardIterator __first,
3308 _ForwardIterator __last)
3309{
3310 if (__first != __last)
3311 {
3312 _ForwardIterator __temp = next(__first);
3313 if (__temp == __last && *__first == '$')
3314 return __first;
3315 // Not called inside a bracket
3316 if (*__first == '.' || *__first == '\\' || *__first == '[')
3317 return __first;
Howard Hinnant0de86b62010-06-25 20:56:08 +00003318 __push_char(*__first);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003319 ++__first;
3320 }
3321 return __first;
3322}
3323
3324template <class _CharT, class _Traits>
3325template <class _ForwardIterator>
3326_ForwardIterator
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003327basic_regex<_CharT, _Traits>::__parse_ORD_CHAR_ERE(_ForwardIterator __first,
3328 _ForwardIterator __last)
3329{
3330 if (__first != __last)
3331 {
3332 switch (*__first)
3333 {
3334 case '^':
3335 case '.':
3336 case '[':
3337 case '$':
3338 case '(':
3339 case '|':
3340 case '*':
3341 case '+':
3342 case '?':
3343 case '{':
3344 case '\\':
3345 break;
3346 case ')':
3347 if (__open_count_ == 0)
3348 {
3349 __push_char(*__first);
3350 ++__first;
3351 }
3352 break;
3353 default:
3354 __push_char(*__first);
3355 ++__first;
3356 break;
3357 }
3358 }
3359 return __first;
3360}
3361
3362template <class _CharT, class _Traits>
3363template <class _ForwardIterator>
3364_ForwardIterator
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003365basic_regex<_CharT, _Traits>::__parse_QUOTED_CHAR(_ForwardIterator __first,
3366 _ForwardIterator __last)
3367{
3368 if (__first != __last)
3369 {
3370 _ForwardIterator __temp = next(__first);
3371 if (__temp != __last)
3372 {
3373 if (*__first == '\\')
3374 {
3375 switch (*__temp)
3376 {
3377 case '^':
3378 case '.':
3379 case '*':
3380 case '[':
3381 case '$':
3382 case '\\':
Howard Hinnant0de86b62010-06-25 20:56:08 +00003383 __push_char(*__temp);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003384 __first = ++__temp;
3385 break;
3386 }
3387 }
3388 }
3389 }
3390 return __first;
3391}
3392
3393template <class _CharT, class _Traits>
3394template <class _ForwardIterator>
3395_ForwardIterator
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003396basic_regex<_CharT, _Traits>::__parse_QUOTED_CHAR_ERE(_ForwardIterator __first,
3397 _ForwardIterator __last)
3398{
3399 if (__first != __last)
3400 {
3401 _ForwardIterator __temp = next(__first);
3402 if (__temp != __last)
3403 {
3404 if (*__first == '\\')
3405 {
3406 switch (*__temp)
3407 {
3408 case '^':
3409 case '.':
3410 case '*':
3411 case '[':
3412 case '$':
3413 case '\\':
3414 case '(':
3415 case ')':
3416 case '|':
3417 case '+':
3418 case '?':
3419 case '{':
3420 __push_char(*__temp);
3421 __first = ++__temp;
3422 break;
Howard Hinnant15476f32010-07-28 17:35:27 +00003423 default:
3424 if ((__flags_ & 0x1F0) == awk)
3425 __first = __parse_awk_escape(++__first, __last);
3426 break;
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003427 }
3428 }
3429 }
3430 }
3431 return __first;
3432}
3433
3434template <class _CharT, class _Traits>
3435template <class _ForwardIterator>
3436_ForwardIterator
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003437basic_regex<_CharT, _Traits>::__parse_RE_dupl_symbol(_ForwardIterator __first,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003438 _ForwardIterator __last,
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003439 __owns_one_state<_CharT>* __s,
3440 unsigned __mexp_begin,
3441 unsigned __mexp_end)
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003442{
3443 if (__first != __last)
3444 {
Howard Hinnant0de86b62010-06-25 20:56:08 +00003445 if (*__first == '*')
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003446 {
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003447 __push_greedy_inf_repeat(0, __s, __mexp_begin, __mexp_end);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003448 ++__first;
3449 }
3450 else
3451 {
3452 _ForwardIterator __temp = __parse_Back_open_brace(__first, __last);
3453 if (__temp != __first)
3454 {
3455 int __min = 0;
3456 __first = __temp;
3457 __temp = __parse_DUP_COUNT(__first, __last, __min);
Howard Hinnantd4444702010-08-11 17:04:31 +00003458#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003459 if (__temp == __first)
3460 throw regex_error(regex_constants::error_badbrace);
Howard Hinnantd4444702010-08-11 17:04:31 +00003461#endif
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003462 __first = __temp;
Howard Hinnantd4444702010-08-11 17:04:31 +00003463#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003464 if (__first == __last)
3465 throw regex_error(regex_constants::error_brace);
Howard Hinnantd4444702010-08-11 17:04:31 +00003466#endif
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003467 if (*__first != ',')
3468 {
3469 __temp = __parse_Back_close_brace(__first, __last);
Howard Hinnantd4444702010-08-11 17:04:31 +00003470#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003471 if (__temp == __first)
3472 throw regex_error(regex_constants::error_brace);
Howard Hinnantd4444702010-08-11 17:04:31 +00003473#endif
Howard Hinnantcba352d2010-07-12 18:16:05 +00003474 __push_loop(__min, __min, __s, __mexp_begin, __mexp_end,
3475 true);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003476 __first = __temp;
3477 }
3478 else
3479 {
3480 ++__first; // consume ','
3481 int __max = -1;
3482 __first = __parse_DUP_COUNT(__first, __last, __max);
3483 __temp = __parse_Back_close_brace(__first, __last);
Howard Hinnantd4444702010-08-11 17:04:31 +00003484#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003485 if (__temp == __first)
3486 throw regex_error(regex_constants::error_brace);
Howard Hinnantd4444702010-08-11 17:04:31 +00003487#endif
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003488 if (__max == -1)
Howard Hinnantaa698082010-07-16 19:08:36 +00003489 __push_greedy_inf_repeat(__min, __s, __mexp_begin, __mexp_end);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003490 else
3491 {
Howard Hinnantd4444702010-08-11 17:04:31 +00003492#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003493 if (__max < __min)
3494 throw regex_error(regex_constants::error_badbrace);
Howard Hinnantd4444702010-08-11 17:04:31 +00003495#endif
Howard Hinnantcba352d2010-07-12 18:16:05 +00003496 __push_loop(__min, __max, __s, __mexp_begin, __mexp_end,
3497 true);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003498 }
3499 __first = __temp;
3500 }
3501 }
3502 }
3503 }
3504 return __first;
3505}
3506
Howard Hinnant0de86b62010-06-25 20:56:08 +00003507template <class _CharT, class _Traits>
3508template <class _ForwardIterator>
3509_ForwardIterator
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003510basic_regex<_CharT, _Traits>::__parse_ERE_dupl_symbol(_ForwardIterator __first,
Howard Hinnantaa698082010-07-16 19:08:36 +00003511 _ForwardIterator __last,
3512 __owns_one_state<_CharT>* __s,
3513 unsigned __mexp_begin,
3514 unsigned __mexp_end)
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003515{
3516 if (__first != __last)
3517 {
Howard Hinnanta0d045b2010-07-29 00:36:00 +00003518 unsigned __grammar = __flags_ & 0x1F0;
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003519 switch (*__first)
3520 {
3521 case '*':
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003522 ++__first;
Howard Hinnanta0d045b2010-07-29 00:36:00 +00003523 if (__grammar == ECMAScript && __first != __last && *__first == '?')
Howard Hinnant17615b02010-07-27 01:25:38 +00003524 {
3525 ++__first;
3526 __push_nongreedy_inf_repeat(0, __s, __mexp_begin, __mexp_end);
3527 }
3528 else
3529 __push_greedy_inf_repeat(0, __s, __mexp_begin, __mexp_end);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003530 break;
3531 case '+':
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003532 ++__first;
Howard Hinnanta0d045b2010-07-29 00:36:00 +00003533 if (__grammar == ECMAScript && __first != __last && *__first == '?')
Howard Hinnant17615b02010-07-27 01:25:38 +00003534 {
3535 ++__first;
3536 __push_nongreedy_inf_repeat(1, __s, __mexp_begin, __mexp_end);
3537 }
3538 else
3539 __push_greedy_inf_repeat(1, __s, __mexp_begin, __mexp_end);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003540 break;
3541 case '?':
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003542 ++__first;
Howard Hinnanta0d045b2010-07-29 00:36:00 +00003543 if (__grammar == ECMAScript && __first != __last && *__first == '?')
Howard Hinnant17615b02010-07-27 01:25:38 +00003544 {
3545 ++__first;
3546 __push_loop(0, 1, __s, __mexp_begin, __mexp_end, false);
3547 }
3548 else
3549 __push_loop(0, 1, __s, __mexp_begin, __mexp_end);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003550 break;
3551 case '{':
3552 {
3553 int __min;
Howard Hinnantaa698082010-07-16 19:08:36 +00003554 _ForwardIterator __temp = __parse_DUP_COUNT(++__first, __last, __min);
Howard Hinnantd4444702010-08-11 17:04:31 +00003555#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003556 if (__temp == __first)
3557 throw regex_error(regex_constants::error_badbrace);
Howard Hinnantd4444702010-08-11 17:04:31 +00003558#endif
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003559 __first = __temp;
Howard Hinnantd4444702010-08-11 17:04:31 +00003560#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003561 if (__first == __last)
3562 throw regex_error(regex_constants::error_brace);
Howard Hinnantd4444702010-08-11 17:04:31 +00003563#endif
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003564 switch (*__first)
3565 {
3566 case '}':
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003567 ++__first;
Howard Hinnanta0d045b2010-07-29 00:36:00 +00003568 if (__grammar == ECMAScript && __first != __last && *__first == '?')
Howard Hinnant17615b02010-07-27 01:25:38 +00003569 {
3570 ++__first;
3571 __push_loop(__min, __min, __s, __mexp_begin, __mexp_end, false);
3572 }
3573 else
3574 __push_loop(__min, __min, __s, __mexp_begin, __mexp_end);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003575 break;
3576 case ',':
Howard Hinnantd4444702010-08-11 17:04:31 +00003577 ++__first;
3578#ifndef _LIBCPP_NO_EXCEPTIONS
3579 if (__first == __last)
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003580 throw regex_error(regex_constants::error_badbrace);
Howard Hinnantd4444702010-08-11 17:04:31 +00003581#endif
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003582 if (*__first == '}')
3583 {
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003584 ++__first;
Howard Hinnanta0d045b2010-07-29 00:36:00 +00003585 if (__grammar == ECMAScript && __first != __last && *__first == '?')
Howard Hinnant17615b02010-07-27 01:25:38 +00003586 {
3587 ++__first;
3588 __push_nongreedy_inf_repeat(__min, __s, __mexp_begin, __mexp_end);
3589 }
3590 else
3591 __push_greedy_inf_repeat(__min, __s, __mexp_begin, __mexp_end);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003592 }
3593 else
3594 {
Howard Hinnantaa698082010-07-16 19:08:36 +00003595 int __max = -1;
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003596 __temp = __parse_DUP_COUNT(__first, __last, __max);
Howard Hinnantd4444702010-08-11 17:04:31 +00003597#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003598 if (__temp == __first)
3599 throw regex_error(regex_constants::error_brace);
Howard Hinnantd4444702010-08-11 17:04:31 +00003600#endif
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003601 __first = __temp;
Howard Hinnantd4444702010-08-11 17:04:31 +00003602#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003603 if (__first == __last || *__first != '}')
3604 throw regex_error(regex_constants::error_brace);
Howard Hinnantd4444702010-08-11 17:04:31 +00003605#endif
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003606 ++__first;
Howard Hinnantd4444702010-08-11 17:04:31 +00003607#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003608 if (__max < __min)
3609 throw regex_error(regex_constants::error_badbrace);
Howard Hinnantd4444702010-08-11 17:04:31 +00003610#endif
Howard Hinnanta0d045b2010-07-29 00:36:00 +00003611 if (__grammar == ECMAScript && __first != __last && *__first == '?')
Howard Hinnant17615b02010-07-27 01:25:38 +00003612 {
3613 ++__first;
3614 __push_loop(__min, __max, __s, __mexp_begin, __mexp_end, false);
3615 }
3616 else
3617 __push_loop(__min, __max, __s, __mexp_begin, __mexp_end);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003618 }
Howard Hinnantaa698082010-07-16 19:08:36 +00003619 break;
Howard Hinnantd4444702010-08-11 17:04:31 +00003620#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003621 default:
3622 throw regex_error(regex_constants::error_badbrace);
Howard Hinnantd4444702010-08-11 17:04:31 +00003623#endif
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003624 }
3625 }
3626 break;
3627 }
3628 }
3629 return __first;
3630}
3631
3632template <class _CharT, class _Traits>
3633template <class _ForwardIterator>
3634_ForwardIterator
Howard Hinnant0de86b62010-06-25 20:56:08 +00003635basic_regex<_CharT, _Traits>::__parse_bracket_expression(_ForwardIterator __first,
3636 _ForwardIterator __last)
3637{
3638 if (__first != __last && *__first == '[')
3639 {
Howard Hinnantd4444702010-08-11 17:04:31 +00003640 ++__first;
3641#ifndef _LIBCPP_NO_EXCEPTIONS
3642 if (__first == __last)
Howard Hinnant0de86b62010-06-25 20:56:08 +00003643 throw regex_error(regex_constants::error_brack);
Howard Hinnantd4444702010-08-11 17:04:31 +00003644#endif
Howard Hinnant173968a2010-07-13 21:48:06 +00003645 bool __negate = false;
Howard Hinnant0de86b62010-06-25 20:56:08 +00003646 if (*__first == '^')
3647 {
3648 ++__first;
Howard Hinnant173968a2010-07-13 21:48:06 +00003649 __negate = true;
Howard Hinnant0de86b62010-06-25 20:56:08 +00003650 }
Howard Hinnant173968a2010-07-13 21:48:06 +00003651 __bracket_expression<_CharT, _Traits>* __ml = __start_matching_list(__negate);
3652 // __ml owned by *this
Howard Hinnantd4444702010-08-11 17:04:31 +00003653#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0de86b62010-06-25 20:56:08 +00003654 if (__first == __last)
3655 throw regex_error(regex_constants::error_brack);
Howard Hinnantd4444702010-08-11 17:04:31 +00003656#endif
Howard Hinnant15476f32010-07-28 17:35:27 +00003657 if ((__flags_ & 0x1F0) != ECMAScript && *__first == ']')
Howard Hinnant0de86b62010-06-25 20:56:08 +00003658 {
Howard Hinnant173968a2010-07-13 21:48:06 +00003659 __ml->__add_char(']');
Howard Hinnant0de86b62010-06-25 20:56:08 +00003660 ++__first;
3661 }
Howard Hinnant173968a2010-07-13 21:48:06 +00003662 __first = __parse_follow_list(__first, __last, __ml);
Howard Hinnantd4444702010-08-11 17:04:31 +00003663#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0de86b62010-06-25 20:56:08 +00003664 if (__first == __last)
3665 throw regex_error(regex_constants::error_brack);
Howard Hinnantd4444702010-08-11 17:04:31 +00003666#endif
Howard Hinnant0de86b62010-06-25 20:56:08 +00003667 if (*__first == '-')
3668 {
Howard Hinnant173968a2010-07-13 21:48:06 +00003669 __ml->__add_char('-');
Howard Hinnant0de86b62010-06-25 20:56:08 +00003670 ++__first;
3671 }
Howard Hinnantd4444702010-08-11 17:04:31 +00003672#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0de86b62010-06-25 20:56:08 +00003673 if (__first == __last || *__first != ']')
3674 throw regex_error(regex_constants::error_brack);
Howard Hinnantd4444702010-08-11 17:04:31 +00003675#endif
Howard Hinnant0de86b62010-06-25 20:56:08 +00003676 ++__first;
3677 }
3678 return __first;
3679}
3680
3681template <class _CharT, class _Traits>
3682template <class _ForwardIterator>
3683_ForwardIterator
3684basic_regex<_CharT, _Traits>::__parse_follow_list(_ForwardIterator __first,
Howard Hinnant173968a2010-07-13 21:48:06 +00003685 _ForwardIterator __last,
3686 __bracket_expression<_CharT, _Traits>* __ml)
Howard Hinnant0de86b62010-06-25 20:56:08 +00003687{
3688 if (__first != __last)
3689 {
3690 while (true)
3691 {
Howard Hinnant173968a2010-07-13 21:48:06 +00003692 _ForwardIterator __temp = __parse_expression_term(__first, __last,
3693 __ml);
Howard Hinnant0de86b62010-06-25 20:56:08 +00003694 if (__temp == __first)
3695 break;
3696 __first = __temp;
3697 }
3698 }
3699 return __first;
3700}
3701
3702template <class _CharT, class _Traits>
3703template <class _ForwardIterator>
3704_ForwardIterator
3705basic_regex<_CharT, _Traits>::__parse_expression_term(_ForwardIterator __first,
Howard Hinnant173968a2010-07-13 21:48:06 +00003706 _ForwardIterator __last,
3707 __bracket_expression<_CharT, _Traits>* __ml)
Howard Hinnant0de86b62010-06-25 20:56:08 +00003708{
3709 if (__first != __last && *__first != ']')
3710 {
Howard Hinnant0de86b62010-06-25 20:56:08 +00003711 _ForwardIterator __temp = next(__first);
Howard Hinnant173968a2010-07-13 21:48:06 +00003712 basic_string<_CharT> __start_range;
Howard Hinnant0de86b62010-06-25 20:56:08 +00003713 if (__temp != __last && *__first == '[')
3714 {
3715 if (*__temp == '=')
Howard Hinnant173968a2010-07-13 21:48:06 +00003716 return __parse_equivalence_class(++__temp, __last, __ml);
Howard Hinnant0de86b62010-06-25 20:56:08 +00003717 else if (*__temp == ':')
Howard Hinnant173968a2010-07-13 21:48:06 +00003718 return __parse_character_class(++__temp, __last, __ml);
Howard Hinnant0de86b62010-06-25 20:56:08 +00003719 else if (*__temp == '.')
Howard Hinnant173968a2010-07-13 21:48:06 +00003720 __first = __parse_collating_symbol(++__temp, __last, __start_range);
Howard Hinnant0de86b62010-06-25 20:56:08 +00003721 }
Howard Hinnant15476f32010-07-28 17:35:27 +00003722 unsigned __grammar = __flags_ & 0x1F0;
3723 if (__start_range.empty())
Howard Hinnant0de86b62010-06-25 20:56:08 +00003724 {
Howard Hinnant15476f32010-07-28 17:35:27 +00003725 if ((__grammar == ECMAScript || __grammar == awk) && *__first == '\\')
3726 {
3727 if (__grammar == ECMAScript)
3728 __first = __parse_class_escape(++__first, __last, __start_range, __ml);
3729 else
3730 __first = __parse_awk_escape(++__first, __last, &__start_range);
3731 }
3732 else
3733 {
3734 __start_range = *__first;
3735 ++__first;
3736 }
Howard Hinnant0de86b62010-06-25 20:56:08 +00003737 }
3738 if (__first != __last && *__first != ']')
3739 {
3740 __temp = next(__first);
3741 if (__temp != __last && *__first == '-' && *__temp != ']')
3742 {
3743 // parse a range
Howard Hinnant173968a2010-07-13 21:48:06 +00003744 basic_string<_CharT> __end_range;
Howard Hinnant0de86b62010-06-25 20:56:08 +00003745 __first = __temp;
3746 ++__temp;
3747 if (__temp != __last && *__first == '[' && *__temp == '.')
Howard Hinnant173968a2010-07-13 21:48:06 +00003748 __first = __parse_collating_symbol(++__temp, __last, __end_range);
Howard Hinnant0de86b62010-06-25 20:56:08 +00003749 else
3750 {
Howard Hinnant15476f32010-07-28 17:35:27 +00003751 if ((__grammar == ECMAScript || __grammar == awk) && *__first == '\\')
3752 {
3753 if (__grammar == ECMAScript)
3754 __first = __parse_class_escape(++__first, __last,
3755 __end_range, __ml);
3756 else
3757 __first = __parse_awk_escape(++__first, __last,
3758 &__end_range);
3759 }
3760 else
3761 {
3762 __end_range = *__first;
3763 ++__first;
3764 }
Howard Hinnant0de86b62010-06-25 20:56:08 +00003765 }
Howard Hinnant173968a2010-07-13 21:48:06 +00003766 __ml->__add_range(_STD::move(__start_range), _STD::move(__end_range));
Howard Hinnant0de86b62010-06-25 20:56:08 +00003767 }
Howard Hinnant173968a2010-07-13 21:48:06 +00003768 else
3769 {
3770 if (__start_range.size() == 1)
3771 __ml->__add_char(__start_range[0]);
3772 else
3773 __ml->__add_digraph(__start_range[0], __start_range[1]);
3774 }
3775 }
3776 else
3777 {
3778 if (__start_range.size() == 1)
3779 __ml->__add_char(__start_range[0]);
3780 else
3781 __ml->__add_digraph(__start_range[0], __start_range[1]);
Howard Hinnant0de86b62010-06-25 20:56:08 +00003782 }
3783 }
3784 return __first;
3785}
3786
3787template <class _CharT, class _Traits>
3788template <class _ForwardIterator>
3789_ForwardIterator
Howard Hinnant15476f32010-07-28 17:35:27 +00003790basic_regex<_CharT, _Traits>::__parse_class_escape(_ForwardIterator __first,
3791 _ForwardIterator __last,
3792 basic_string<_CharT>& __str,
3793 __bracket_expression<_CharT, _Traits>* __ml)
3794{
Howard Hinnantd4444702010-08-11 17:04:31 +00003795#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant15476f32010-07-28 17:35:27 +00003796 if (__first == __last)
3797 throw regex_error(regex_constants::error_escape);
Howard Hinnantd4444702010-08-11 17:04:31 +00003798#endif
Howard Hinnant15476f32010-07-28 17:35:27 +00003799 switch (*__first)
3800 {
3801 case 0:
3802 __str = *__first;
3803 return ++__first;
3804 case 'b':
3805 __str = _CharT(8);
3806 return ++__first;
3807 case 'd':
3808 __ml->__add_class(ctype_base::digit);
3809 return ++__first;
3810 case 'D':
3811 __ml->__add_neg_class(ctype_base::digit);
3812 return ++__first;
3813 case 's':
3814 __ml->__add_class(ctype_base::space);
3815 return ++__first;
3816 case 'S':
3817 __ml->__add_neg_class(ctype_base::space);
3818 return ++__first;
3819 case 'w':
3820 __ml->__add_class(ctype_base::alnum);
3821 __ml->__add_char('_');
3822 return ++__first;
3823 case 'W':
3824 __ml->__add_neg_class(ctype_base::alnum);
3825 __ml->__add_neg_char('_');
3826 return ++__first;
3827 }
3828 __first = __parse_character_escape(__first, __last, &__str);
3829 return __first;
3830}
3831
3832template <class _CharT, class _Traits>
3833template <class _ForwardIterator>
3834_ForwardIterator
3835basic_regex<_CharT, _Traits>::__parse_awk_escape(_ForwardIterator __first,
3836 _ForwardIterator __last,
3837 basic_string<_CharT>* __str)
3838{
Howard Hinnantd4444702010-08-11 17:04:31 +00003839#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant15476f32010-07-28 17:35:27 +00003840 if (__first == __last)
3841 throw regex_error(regex_constants::error_escape);
Howard Hinnantd4444702010-08-11 17:04:31 +00003842#endif
Howard Hinnant15476f32010-07-28 17:35:27 +00003843 switch (*__first)
3844 {
3845 case '\\':
3846 case '"':
3847 case '/':
3848 if (__str)
3849 *__str = *__first;
3850 else
3851 __push_char(*__first);
3852 return ++__first;
3853 case 'a':
3854 if (__str)
3855 *__str = _CharT(7);
3856 else
3857 __push_char(_CharT(7));
3858 return ++__first;
3859 case 'b':
3860 if (__str)
3861 *__str = _CharT(8);
3862 else
3863 __push_char(_CharT(8));
3864 return ++__first;
3865 case 'f':
3866 if (__str)
3867 *__str = _CharT(0xC);
3868 else
3869 __push_char(_CharT(0xC));
3870 return ++__first;
3871 case 'n':
3872 if (__str)
3873 *__str = _CharT(0xA);
3874 else
3875 __push_char(_CharT(0xA));
3876 return ++__first;
3877 case 'r':
3878 if (__str)
3879 *__str = _CharT(0xD);
3880 else
3881 __push_char(_CharT(0xD));
3882 return ++__first;
3883 case 't':
3884 if (__str)
3885 *__str = _CharT(0x9);
3886 else
3887 __push_char(_CharT(0x9));
3888 return ++__first;
3889 case 'v':
3890 if (__str)
3891 *__str = _CharT(0xB);
3892 else
3893 __push_char(_CharT(0xB));
3894 return ++__first;
3895 }
3896 if ('0' <= *__first && *__first <= '7')
3897 {
3898 unsigned __val = *__first - '0';
3899 if (++__first != __last && ('0' <= *__first && *__first <= '7'))
3900 {
3901 __val = 8 * __val + *__first - '0';
3902 if (++__first != __last && ('0' <= *__first && *__first <= '7'))
3903 __val = 8 * __val + *__first - '0';
3904 }
3905 if (__str)
3906 *__str = _CharT(__val);
3907 else
3908 __push_char(_CharT(__val));
3909 }
Howard Hinnantd4444702010-08-11 17:04:31 +00003910#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant15476f32010-07-28 17:35:27 +00003911 else
3912 throw regex_error(regex_constants::error_escape);
Howard Hinnantd4444702010-08-11 17:04:31 +00003913#endif
Howard Hinnant15476f32010-07-28 17:35:27 +00003914 return __first;
3915}
3916
3917template <class _CharT, class _Traits>
3918template <class _ForwardIterator>
3919_ForwardIterator
Howard Hinnant0de86b62010-06-25 20:56:08 +00003920basic_regex<_CharT, _Traits>::__parse_equivalence_class(_ForwardIterator __first,
Howard Hinnant173968a2010-07-13 21:48:06 +00003921 _ForwardIterator __last,
3922 __bracket_expression<_CharT, _Traits>* __ml)
Howard Hinnant0de86b62010-06-25 20:56:08 +00003923{
3924 // Found [=
3925 // This means =] must exist
3926 value_type _Equal_close[2] = {'=', ']'};
3927 _ForwardIterator __temp = _STD::search(__first, __last, _Equal_close,
3928 _Equal_close+2);
Howard Hinnantd4444702010-08-11 17:04:31 +00003929#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0de86b62010-06-25 20:56:08 +00003930 if (__temp == __last)
3931 throw regex_error(regex_constants::error_brack);
Howard Hinnantd4444702010-08-11 17:04:31 +00003932#endif
Howard Hinnant0de86b62010-06-25 20:56:08 +00003933 // [__first, __temp) contains all text in [= ... =]
3934 typedef typename _Traits::string_type string_type;
3935 string_type __collate_name =
3936 __traits_.lookup_collatename(__first, __temp);
Howard Hinnantd4444702010-08-11 17:04:31 +00003937#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0de86b62010-06-25 20:56:08 +00003938 if (__collate_name.empty())
Howard Hinnant173968a2010-07-13 21:48:06 +00003939 throw regex_error(regex_constants::error_collate);
Howard Hinnantd4444702010-08-11 17:04:31 +00003940#endif
Howard Hinnant0de86b62010-06-25 20:56:08 +00003941 string_type __equiv_name =
3942 __traits_.transform_primary(__collate_name.begin(),
3943 __collate_name.end());
3944 if (!__equiv_name.empty())
Howard Hinnant173968a2010-07-13 21:48:06 +00003945 __ml->__add_equivalence(__equiv_name);
Howard Hinnant0de86b62010-06-25 20:56:08 +00003946 else
Howard Hinnant173968a2010-07-13 21:48:06 +00003947 {
3948 switch (__collate_name.size())
3949 {
3950 case 1:
3951 __ml->__add_char(__collate_name[0]);
3952 break;
3953 case 2:
3954 __ml->__add_digraph(__collate_name[0], __collate_name[1]);
3955 break;
Howard Hinnantd4444702010-08-11 17:04:31 +00003956#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant173968a2010-07-13 21:48:06 +00003957 default:
3958 throw regex_error(regex_constants::error_collate);
Howard Hinnantd4444702010-08-11 17:04:31 +00003959#endif
Howard Hinnant173968a2010-07-13 21:48:06 +00003960 }
3961 }
Howard Hinnant0de86b62010-06-25 20:56:08 +00003962 __first = next(__temp, 2);
3963 return __first;
3964}
3965
3966template <class _CharT, class _Traits>
3967template <class _ForwardIterator>
3968_ForwardIterator
3969basic_regex<_CharT, _Traits>::__parse_character_class(_ForwardIterator __first,
Howard Hinnant173968a2010-07-13 21:48:06 +00003970 _ForwardIterator __last,
3971 __bracket_expression<_CharT, _Traits>* __ml)
Howard Hinnant0de86b62010-06-25 20:56:08 +00003972{
3973 // Found [:
3974 // This means :] must exist
3975 value_type _Colon_close[2] = {':', ']'};
3976 _ForwardIterator __temp = _STD::search(__first, __last, _Colon_close,
3977 _Colon_close+2);
Howard Hinnantd4444702010-08-11 17:04:31 +00003978#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0de86b62010-06-25 20:56:08 +00003979 if (__temp == __last)
3980 throw regex_error(regex_constants::error_brack);
Howard Hinnantd4444702010-08-11 17:04:31 +00003981#endif
Howard Hinnant0de86b62010-06-25 20:56:08 +00003982 // [__first, __temp) contains all text in [: ... :]
3983 typedef typename _Traits::char_class_type char_class_type;
3984 char_class_type __class_type =
3985 __traits_.lookup_classname(__first, __temp, __flags_ & icase);
Howard Hinnantd4444702010-08-11 17:04:31 +00003986#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0de86b62010-06-25 20:56:08 +00003987 if (__class_type == 0)
3988 throw regex_error(regex_constants::error_brack);
Howard Hinnantd4444702010-08-11 17:04:31 +00003989#endif
Howard Hinnant173968a2010-07-13 21:48:06 +00003990 __ml->__add_class(__class_type);
Howard Hinnant0de86b62010-06-25 20:56:08 +00003991 __first = next(__temp, 2);
3992 return __first;
3993}
3994
3995template <class _CharT, class _Traits>
3996template <class _ForwardIterator>
3997_ForwardIterator
3998basic_regex<_CharT, _Traits>::__parse_collating_symbol(_ForwardIterator __first,
Howard Hinnant173968a2010-07-13 21:48:06 +00003999 _ForwardIterator __last,
4000 basic_string<_CharT>& __col_sym)
Howard Hinnant0de86b62010-06-25 20:56:08 +00004001{
4002 // Found [.
4003 // This means .] must exist
4004 value_type _Dot_close[2] = {'.', ']'};
4005 _ForwardIterator __temp = _STD::search(__first, __last, _Dot_close,
4006 _Dot_close+2);
Howard Hinnantd4444702010-08-11 17:04:31 +00004007#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0de86b62010-06-25 20:56:08 +00004008 if (__temp == __last)
4009 throw regex_error(regex_constants::error_brack);
Howard Hinnantd4444702010-08-11 17:04:31 +00004010#endif
Howard Hinnant0de86b62010-06-25 20:56:08 +00004011 // [__first, __temp) contains all text in [. ... .]
4012 typedef typename _Traits::string_type string_type;
Howard Hinnant173968a2010-07-13 21:48:06 +00004013 __col_sym = __traits_.lookup_collatename(__first, __temp);
4014 switch (__col_sym.size())
4015 {
4016 case 1:
4017 case 2:
4018 break;
Howard Hinnantd4444702010-08-11 17:04:31 +00004019#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant173968a2010-07-13 21:48:06 +00004020 default:
4021 throw regex_error(regex_constants::error_collate);
Howard Hinnantd4444702010-08-11 17:04:31 +00004022#endif
Howard Hinnant173968a2010-07-13 21:48:06 +00004023 }
Howard Hinnant0de86b62010-06-25 20:56:08 +00004024 __first = next(__temp, 2);
4025 return __first;
4026}
4027
4028template <class _CharT, class _Traits>
4029template <class _ForwardIterator>
4030_ForwardIterator
4031basic_regex<_CharT, _Traits>::__parse_DUP_COUNT(_ForwardIterator __first,
4032 _ForwardIterator __last,
4033 int& __c)
4034{
4035 if (__first != __last && '0' <= *__first && *__first <= '9')
4036 {
4037 __c = *__first - '0';
4038 for (++__first; __first != __last && '0' <= *__first && *__first <= '9';
4039 ++__first)
4040 {
4041 __c *= 10;
4042 __c += *__first - '0';
4043 }
4044 }
4045 return __first;
4046}
4047
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00004048template <class _CharT, class _Traits>
Howard Hinnant2ade7c22010-07-22 17:53:24 +00004049template <class _ForwardIterator>
4050_ForwardIterator
4051basic_regex<_CharT, _Traits>::__parse_ecma_exp(_ForwardIterator __first,
4052 _ForwardIterator __last)
4053{
4054 __owns_one_state<_CharT>* __sa = __end_;
4055 _ForwardIterator __temp = __parse_alternative(__first, __last);
4056 if (__temp == __first)
4057 __push_empty();
4058 __first = __temp;
4059 while (__first != __last && *__first == '|')
4060 {
4061 __owns_one_state<_CharT>* __sb = __end_;
4062 __temp = __parse_alternative(++__first, __last);
4063 if (__temp == __first)
4064 __push_empty();
4065 __push_alternation(__sa, __sb);
4066 __first = __temp;
4067 }
4068 return __first;
4069}
4070
4071template <class _CharT, class _Traits>
4072template <class _ForwardIterator>
4073_ForwardIterator
4074basic_regex<_CharT, _Traits>::__parse_alternative(_ForwardIterator __first,
4075 _ForwardIterator __last)
4076{
4077 while (true)
4078 {
4079 _ForwardIterator __temp = __parse_term(__first, __last);
4080 if (__temp == __first)
4081 break;
4082 __first = __temp;
4083 }
4084 return __first;
4085}
4086
4087template <class _CharT, class _Traits>
4088template <class _ForwardIterator>
4089_ForwardIterator
4090basic_regex<_CharT, _Traits>::__parse_term(_ForwardIterator __first,
4091 _ForwardIterator __last)
4092{
4093 _ForwardIterator __temp = __parse_assertion(__first, __last);
4094 if (__temp == __first)
4095 {
Howard Hinnant17615b02010-07-27 01:25:38 +00004096 __owns_one_state<_CharT>* __e = __end_;
4097 unsigned __mexp_begin = __marked_count_;
Howard Hinnant2ade7c22010-07-22 17:53:24 +00004098 __temp = __parse_atom(__first, __last);
4099 if (__temp != __first)
Howard Hinnant17615b02010-07-27 01:25:38 +00004100 __first = __parse_ERE_dupl_symbol(__temp, __last, __e,
4101 __mexp_begin+1, __marked_count_+1);
Howard Hinnant2ade7c22010-07-22 17:53:24 +00004102 }
Howard Hinnant17615b02010-07-27 01:25:38 +00004103 else
4104 __first = __temp;
Howard Hinnant2ade7c22010-07-22 17:53:24 +00004105 return __first;
4106}
4107
4108template <class _CharT, class _Traits>
4109template <class _ForwardIterator>
4110_ForwardIterator
4111basic_regex<_CharT, _Traits>::__parse_assertion(_ForwardIterator __first,
4112 _ForwardIterator __last)
4113{
4114 if (__first != __last)
4115 {
4116 switch (*__first)
4117 {
4118 case '^':
4119 __push_l_anchor();
4120 ++__first;
4121 break;
4122 case '$':
4123 __push_r_anchor();
4124 ++__first;
4125 break;
4126 case '\\':
4127 {
4128 _ForwardIterator __temp = _STD::next(__first);
4129 if (__temp != __last)
4130 {
4131 if (*__temp == 'b')
4132 {
Howard Hinnant17615b02010-07-27 01:25:38 +00004133 __push_word_boundary(false);
Howard Hinnant2ade7c22010-07-22 17:53:24 +00004134 __first = ++__temp;
4135 }
4136 else if (*__temp == 'B')
4137 {
Howard Hinnant17615b02010-07-27 01:25:38 +00004138 __push_word_boundary(true);
Howard Hinnant2ade7c22010-07-22 17:53:24 +00004139 __first = ++__temp;
4140 }
4141 }
4142 }
4143 break;
4144 case '(':
4145 {
4146 _ForwardIterator __temp = _STD::next(__first);
4147 if (__temp != __last && *__temp == '?')
4148 {
4149 if (++__temp != __last)
4150 {
4151 switch (*__temp)
4152 {
4153 case '=':
Howard Hinnante9de5ff2010-07-27 22:20:32 +00004154 {
4155 basic_regex __exp;
4156 __exp.__flags_ = __flags_;
4157 __temp = __exp.__parse(++__temp, __last);
4158 __exp.__push_l_anchor();
4159 __push_lookahead(_STD::move(__exp), false);
Howard Hinnantd4444702010-08-11 17:04:31 +00004160#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnante9de5ff2010-07-27 22:20:32 +00004161 if (__temp == __last || *__temp != ')')
4162 throw regex_error(regex_constants::error_paren);
Howard Hinnantd4444702010-08-11 17:04:31 +00004163#endif
Howard Hinnante9de5ff2010-07-27 22:20:32 +00004164 __first = ++__temp;
4165 }
Howard Hinnant2ade7c22010-07-22 17:53:24 +00004166 break;
4167 case '!':
Howard Hinnante9de5ff2010-07-27 22:20:32 +00004168 {
4169 basic_regex __exp;
4170 __exp.__flags_ = __flags_;
4171 __temp = __exp.__parse(++__temp, __last);
4172 __exp.__push_l_anchor();
4173 __push_lookahead(_STD::move(__exp), true);
Howard Hinnantd4444702010-08-11 17:04:31 +00004174#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnante9de5ff2010-07-27 22:20:32 +00004175 if (__temp == __last || *__temp != ')')
4176 throw regex_error(regex_constants::error_paren);
Howard Hinnantd4444702010-08-11 17:04:31 +00004177#endif
Howard Hinnante9de5ff2010-07-27 22:20:32 +00004178 __first = ++__temp;
4179 }
Howard Hinnant2ade7c22010-07-22 17:53:24 +00004180 break;
4181 }
4182 }
4183 }
4184 }
4185 break;
4186 }
4187 }
4188 return __first;
4189}
4190
4191template <class _CharT, class _Traits>
4192template <class _ForwardIterator>
4193_ForwardIterator
4194basic_regex<_CharT, _Traits>::__parse_atom(_ForwardIterator __first,
4195 _ForwardIterator __last)
4196{
Howard Hinnant17615b02010-07-27 01:25:38 +00004197 if (__first != __last)
4198 {
4199 switch (*__first)
4200 {
4201 case '.':
4202 __push_match_any_but_newline();
4203 ++__first;
4204 break;
4205 case '\\':
4206 __first = __parse_atom_escape(__first, __last);
4207 break;
4208 case '[':
4209 __first = __parse_bracket_expression(__first, __last);
4210 break;
4211 case '(':
4212 {
Howard Hinnantd4444702010-08-11 17:04:31 +00004213 ++__first;
4214#ifndef _LIBCPP_NO_EXCEPTIONS
4215 if (__first == __last)
Howard Hinnant17615b02010-07-27 01:25:38 +00004216 throw regex_error(regex_constants::error_paren);
Howard Hinnantd4444702010-08-11 17:04:31 +00004217#endif
Howard Hinnant17615b02010-07-27 01:25:38 +00004218 _ForwardIterator __temp = _STD::next(__first);
4219 if (__temp != __last && *__first == '?' && *__temp == ':')
4220 {
4221 ++__open_count_;
4222 __first = __parse_ecma_exp(++__temp, __last);
Howard Hinnantd4444702010-08-11 17:04:31 +00004223#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant17615b02010-07-27 01:25:38 +00004224 if (__first == __last || *__first != ')')
4225 throw regex_error(regex_constants::error_paren);
Howard Hinnantd4444702010-08-11 17:04:31 +00004226#endif
Howard Hinnant17615b02010-07-27 01:25:38 +00004227 --__open_count_;
4228 ++__first;
4229 }
4230 else
4231 {
4232 __push_begin_marked_subexpression();
4233 unsigned __temp_count = __marked_count_;
4234 ++__open_count_;
4235 __first = __parse_ecma_exp(__first, __last);
Howard Hinnantd4444702010-08-11 17:04:31 +00004236#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant17615b02010-07-27 01:25:38 +00004237 if (__first == __last || *__first != ')')
4238 throw regex_error(regex_constants::error_paren);
Howard Hinnantd4444702010-08-11 17:04:31 +00004239#endif
Howard Hinnant17615b02010-07-27 01:25:38 +00004240 __push_end_marked_subexpression(__temp_count);
4241 --__open_count_;
4242 ++__first;
4243 }
4244 }
4245 break;
4246 default:
4247 __first = __parse_pattern_character(__first, __last);
4248 break;
4249 }
4250 }
4251 return __first;
4252}
4253
4254template <class _CharT, class _Traits>
4255template <class _ForwardIterator>
4256_ForwardIterator
4257basic_regex<_CharT, _Traits>::__parse_atom_escape(_ForwardIterator __first,
4258 _ForwardIterator __last)
4259{
4260 if (__first != __last && *__first == '\\')
4261 {
4262 _ForwardIterator __t1 = _STD::next(__first);
4263 _ForwardIterator __t2 = __parse_decimal_escape(__t1, __last);
4264 if (__t2 != __t1)
4265 __first = __t2;
4266 else
4267 {
4268 __t2 = __parse_character_class_escape(__t1, __last);
4269 if (__t2 != __t1)
4270 __first = __t2;
4271 else
4272 {
4273 __t2 = __parse_character_escape(__t1, __last);
4274 if (__t2 != __t1)
4275 __first = __t2;
4276 }
4277 }
4278 }
4279 return __first;
4280}
4281
4282template <class _CharT, class _Traits>
4283template <class _ForwardIterator>
4284_ForwardIterator
4285basic_regex<_CharT, _Traits>::__parse_decimal_escape(_ForwardIterator __first,
4286 _ForwardIterator __last)
4287{
4288 if (__first != __last)
4289 {
4290 if (*__first == '0')
4291 {
4292 __push_char(_CharT());
4293 ++__first;
4294 }
4295 else if ('1' <= *__first && *__first <= '9')
4296 {
4297 unsigned __v = *__first - '0';
4298 for (++__first; '0' <= *__first && *__first <= '9'; ++__first)
4299 __v = 10 * __v + *__first - '0';
Howard Hinnantd4444702010-08-11 17:04:31 +00004300#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant17615b02010-07-27 01:25:38 +00004301 if (__v > mark_count())
4302 throw regex_error(regex_constants::error_backref);
Howard Hinnantd4444702010-08-11 17:04:31 +00004303#endif
Howard Hinnant17615b02010-07-27 01:25:38 +00004304 __push_back_ref(__v);
4305 }
4306 }
4307 return __first;
4308}
4309
4310template <class _CharT, class _Traits>
4311template <class _ForwardIterator>
4312_ForwardIterator
4313basic_regex<_CharT, _Traits>::__parse_character_class_escape(_ForwardIterator __first,
4314 _ForwardIterator __last)
4315{
4316 if (__first != __last)
4317 {
4318 __bracket_expression<_CharT, _Traits>* __ml;
4319 switch (*__first)
4320 {
4321 case 'd':
4322 __ml = __start_matching_list(false);
4323 __ml->__add_class(ctype_base::digit);
4324 ++__first;
4325 break;
4326 case 'D':
4327 __ml = __start_matching_list(true);
4328 __ml->__add_class(ctype_base::digit);
4329 ++__first;
4330 break;
4331 case 's':
4332 __ml = __start_matching_list(false);
4333 __ml->__add_class(ctype_base::space);
4334 ++__first;
4335 break;
4336 case 'S':
4337 __ml = __start_matching_list(true);
4338 __ml->__add_class(ctype_base::space);
4339 ++__first;
4340 break;
4341 case 'w':
4342 __ml = __start_matching_list(false);
4343 __ml->__add_class(ctype_base::alnum);
4344 __ml->__add_char('_');
4345 ++__first;
4346 break;
4347 case 'W':
4348 __ml = __start_matching_list(true);
4349 __ml->__add_class(ctype_base::alnum);
4350 __ml->__add_char('_');
4351 ++__first;
4352 break;
4353 }
4354 }
4355 return __first;
4356}
4357
4358template <class _CharT, class _Traits>
4359template <class _ForwardIterator>
4360_ForwardIterator
4361basic_regex<_CharT, _Traits>::__parse_character_escape(_ForwardIterator __first,
Howard Hinnant15476f32010-07-28 17:35:27 +00004362 _ForwardIterator __last,
4363 basic_string<_CharT>* __str)
Howard Hinnant17615b02010-07-27 01:25:38 +00004364{
4365 if (__first != __last)
4366 {
4367 _ForwardIterator __t;
4368 unsigned __sum = 0;
4369 int __hd;
4370 switch (*__first)
4371 {
4372 case 'f':
Howard Hinnant15476f32010-07-28 17:35:27 +00004373 if (__str)
4374 *__str = _CharT(0xC);
4375 else
4376 __push_char(_CharT(0xC));
Howard Hinnant17615b02010-07-27 01:25:38 +00004377 ++__first;
4378 break;
4379 case 'n':
Howard Hinnant15476f32010-07-28 17:35:27 +00004380 if (__str)
4381 *__str = _CharT(0xA);
4382 else
4383 __push_char(_CharT(0xA));
Howard Hinnant17615b02010-07-27 01:25:38 +00004384 ++__first;
4385 break;
4386 case 'r':
Howard Hinnant15476f32010-07-28 17:35:27 +00004387 if (__str)
4388 *__str = _CharT(0xD);
4389 else
4390 __push_char(_CharT(0xD));
Howard Hinnant17615b02010-07-27 01:25:38 +00004391 ++__first;
4392 break;
4393 case 't':
Howard Hinnant15476f32010-07-28 17:35:27 +00004394 if (__str)
4395 *__str = _CharT(0x9);
4396 else
4397 __push_char(_CharT(0x9));
Howard Hinnant17615b02010-07-27 01:25:38 +00004398 ++__first;
4399 break;
4400 case 'v':
Howard Hinnant15476f32010-07-28 17:35:27 +00004401 if (__str)
4402 *__str = _CharT(0xB);
4403 else
4404 __push_char(_CharT(0xB));
Howard Hinnant17615b02010-07-27 01:25:38 +00004405 ++__first;
4406 break;
4407 case 'c':
4408 if ((__t = _STD::next(__first)) != __last)
4409 {
4410 if ('A' <= *__t <= 'Z' || 'a' <= *__t <= 'z')
4411 {
Howard Hinnant15476f32010-07-28 17:35:27 +00004412 if (__str)
4413 *__str = _CharT(*__t % 32);
4414 else
4415 __push_char(_CharT(*__t % 32));
Howard Hinnant17615b02010-07-27 01:25:38 +00004416 __first = ++__t;
4417 }
4418 }
4419 break;
4420 case 'u':
Howard Hinnantd4444702010-08-11 17:04:31 +00004421 ++__first;
4422#ifndef _LIBCPP_NO_EXCEPTIONS
4423 if (__first == __last)
Howard Hinnant17615b02010-07-27 01:25:38 +00004424 throw regex_error(regex_constants::error_escape);
Howard Hinnantd4444702010-08-11 17:04:31 +00004425#endif
Howard Hinnant17615b02010-07-27 01:25:38 +00004426 __hd = __traits_.value(*__first, 16);
Howard Hinnantd4444702010-08-11 17:04:31 +00004427#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant17615b02010-07-27 01:25:38 +00004428 if (__hd == -1)
4429 throw regex_error(regex_constants::error_escape);
Howard Hinnantd4444702010-08-11 17:04:31 +00004430#endif
Howard Hinnant17615b02010-07-27 01:25:38 +00004431 __sum = 16 * __sum + __hd;
Howard Hinnantd4444702010-08-11 17:04:31 +00004432 ++__first;
4433#ifndef _LIBCPP_NO_EXCEPTIONS
4434 if (__first == __last)
Howard Hinnant17615b02010-07-27 01:25:38 +00004435 throw regex_error(regex_constants::error_escape);
Howard Hinnantd4444702010-08-11 17:04:31 +00004436#endif
Howard Hinnant17615b02010-07-27 01:25:38 +00004437 __hd = __traits_.value(*__first, 16);
Howard Hinnantd4444702010-08-11 17:04:31 +00004438#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant17615b02010-07-27 01:25:38 +00004439 if (__hd == -1)
4440 throw regex_error(regex_constants::error_escape);
Howard Hinnantd4444702010-08-11 17:04:31 +00004441#endif
Howard Hinnant17615b02010-07-27 01:25:38 +00004442 __sum = 16 * __sum + __hd;
4443 // drop through
4444 case 'x':
Howard Hinnantd4444702010-08-11 17:04:31 +00004445 ++__first;
4446#ifndef _LIBCPP_NO_EXCEPTIONS
4447 if (__first == __last)
Howard Hinnant17615b02010-07-27 01:25:38 +00004448 throw regex_error(regex_constants::error_escape);
Howard Hinnantd4444702010-08-11 17:04:31 +00004449#endif
Howard Hinnant17615b02010-07-27 01:25:38 +00004450 __hd = __traits_.value(*__first, 16);
Howard Hinnantd4444702010-08-11 17:04:31 +00004451#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant17615b02010-07-27 01:25:38 +00004452 if (__hd == -1)
4453 throw regex_error(regex_constants::error_escape);
Howard Hinnantd4444702010-08-11 17:04:31 +00004454#endif
Howard Hinnant17615b02010-07-27 01:25:38 +00004455 __sum = 16 * __sum + __hd;
Howard Hinnantd4444702010-08-11 17:04:31 +00004456 ++__first;
4457#ifndef _LIBCPP_NO_EXCEPTIONS
4458 if (__first == __last)
Howard Hinnant17615b02010-07-27 01:25:38 +00004459 throw regex_error(regex_constants::error_escape);
Howard Hinnantd4444702010-08-11 17:04:31 +00004460#endif
Howard Hinnant17615b02010-07-27 01:25:38 +00004461 __hd = __traits_.value(*__first, 16);
Howard Hinnantd4444702010-08-11 17:04:31 +00004462#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant17615b02010-07-27 01:25:38 +00004463 if (__hd == -1)
4464 throw regex_error(regex_constants::error_escape);
Howard Hinnantd4444702010-08-11 17:04:31 +00004465#endif
Howard Hinnant17615b02010-07-27 01:25:38 +00004466 __sum = 16 * __sum + __hd;
Howard Hinnant15476f32010-07-28 17:35:27 +00004467 if (__str)
4468 *__str = _CharT(__sum);
4469 else
4470 __push_char(_CharT(__sum));
Howard Hinnant17615b02010-07-27 01:25:38 +00004471 ++__first;
4472 break;
4473 default:
4474 if (*__first != '_' && !__traits_.isctype(*__first, ctype_base::alnum))
4475 {
Howard Hinnant15476f32010-07-28 17:35:27 +00004476 if (__str)
4477 *__str = *__first;
4478 else
4479 __push_char(*__first);
Howard Hinnant17615b02010-07-27 01:25:38 +00004480 ++__first;
4481 }
Howard Hinnantd4444702010-08-11 17:04:31 +00004482#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant15476f32010-07-28 17:35:27 +00004483 else if (__str)
4484 throw regex_error(regex_constants::error_escape);
Howard Hinnantd4444702010-08-11 17:04:31 +00004485#endif
Howard Hinnant17615b02010-07-27 01:25:38 +00004486 break;
4487 }
4488 }
4489 return __first;
4490}
4491
4492template <class _CharT, class _Traits>
4493template <class _ForwardIterator>
4494_ForwardIterator
4495basic_regex<_CharT, _Traits>::__parse_pattern_character(_ForwardIterator __first,
4496 _ForwardIterator __last)
4497{
4498 if (__first != __last)
4499 {
4500 switch (*__first)
4501 {
4502 case '^':
4503 case '$':
4504 case '\\':
4505 case '.':
4506 case '*':
4507 case '+':
4508 case '?':
4509 case '(':
4510 case ')':
4511 case '[':
4512 case ']':
4513 case '{':
4514 case '}':
4515 case '|':
4516 break;
4517 default:
4518 __push_char(*__first);
4519 ++__first;
4520 break;
4521 }
4522 }
4523 return __first;
Howard Hinnant2ade7c22010-07-22 17:53:24 +00004524}
4525
4526template <class _CharT, class _Traits>
Howard Hinnant856846b2010-07-27 19:53:10 +00004527template <class _ForwardIterator>
4528_ForwardIterator
4529basic_regex<_CharT, _Traits>::__parse_grep(_ForwardIterator __first,
4530 _ForwardIterator __last)
4531{
4532 __owns_one_state<_CharT>* __sa = __end_;
4533 _ForwardIterator __t1 = _STD::find(__first, __last, _CharT('\n'));
4534 if (__t1 != __first)
4535 __parse_basic_reg_exp(__first, __t1);
4536 else
4537 __push_empty();
4538 __first = __t1;
4539 if (__first != __last)
4540 ++__first;
4541 while (__first != __last)
4542 {
4543 __t1 = _STD::find(__first, __last, _CharT('\n'));
4544 __owns_one_state<_CharT>* __sb = __end_;
4545 if (__t1 != __first)
4546 __parse_basic_reg_exp(__first, __t1);
4547 else
4548 __push_empty();
4549 __push_alternation(__sa, __sb);
4550 __first = __t1;
4551 if (__first != __last)
4552 ++__first;
4553 }
4554 return __first;
4555}
4556
4557template <class _CharT, class _Traits>
4558template <class _ForwardIterator>
4559_ForwardIterator
4560basic_regex<_CharT, _Traits>::__parse_egrep(_ForwardIterator __first,
4561 _ForwardIterator __last)
4562{
4563 __owns_one_state<_CharT>* __sa = __end_;
4564 _ForwardIterator __t1 = _STD::find(__first, __last, _CharT('\n'));
4565 if (__t1 != __first)
4566 __parse_extended_reg_exp(__first, __t1);
4567 else
4568 __push_empty();
4569 __first = __t1;
4570 if (__first != __last)
4571 ++__first;
4572 while (__first != __last)
4573 {
4574 __t1 = _STD::find(__first, __last, _CharT('\n'));
4575 __owns_one_state<_CharT>* __sb = __end_;
4576 if (__t1 != __first)
4577 __parse_extended_reg_exp(__first, __t1);
4578 else
4579 __push_empty();
4580 __push_alternation(__sa, __sb);
4581 __first = __t1;
4582 if (__first != __last)
4583 ++__first;
4584 }
4585 return __first;
4586}
4587
4588template <class _CharT, class _Traits>
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00004589void
Howard Hinnantf8ce4592010-07-07 19:14:52 +00004590basic_regex<_CharT, _Traits>::__push_loop(size_t __min, size_t __max,
4591 __owns_one_state<_CharT>* __s, size_t __mexp_begin, size_t __mexp_end,
4592 bool __greedy)
4593{
4594 unique_ptr<__empty_state<_CharT> > __e1(new __empty_state<_CharT>(__end_->first()));
4595 __end_->first() = nullptr;
Howard Hinnantac303862010-07-12 15:51:17 +00004596 unique_ptr<__loop<_CharT> > __e2(new __loop<_CharT>(__loop_count_,
4597 __s->first(), __e1.get(), __mexp_begin, __mexp_end, __greedy,
4598 __min, __max));
4599 __s->first() = nullptr;
4600 __e1.release();
4601 __end_->first() = new __repeat_one_loop<_CharT>(__e2.get());
Howard Hinnantf8ce4592010-07-07 19:14:52 +00004602 __end_ = __e2->second();
Howard Hinnantac303862010-07-12 15:51:17 +00004603 __s->first() = __e2.release();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00004604 ++__loop_count_;
4605}
4606
4607template <class _CharT, class _Traits>
4608void
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00004609basic_regex<_CharT, _Traits>::__push_char(value_type __c)
4610{
Howard Hinnant173968a2010-07-13 21:48:06 +00004611 if (flags() & icase)
Howard Hinnante34f17d2010-07-12 19:11:27 +00004612 __end_->first() = new __match_char_icase<_CharT, _Traits>
4613 (__traits_, __c, __end_->first());
Howard Hinnant173968a2010-07-13 21:48:06 +00004614 else if (flags() & collate)
Howard Hinnante34f17d2010-07-12 19:11:27 +00004615 __end_->first() = new __match_char_collate<_CharT, _Traits>
4616 (__traits_, __c, __end_->first());
4617 else
4618 __end_->first() = new __match_char<_CharT>(__c, __end_->first());
Howard Hinnante77aa5e2010-07-08 17:43:58 +00004619 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00004620}
4621
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00004622template <class _CharT, class _Traits>
4623void
4624basic_regex<_CharT, _Traits>::__push_begin_marked_subexpression()
4625{
Howard Hinnant68025ed2010-07-14 15:45:11 +00004626 if (!(__flags_ & nosubs))
4627 {
4628 __end_->first() =
4629 new __begin_marked_subexpression<_CharT>(++__marked_count_,
4630 __end_->first());
4631 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4632 }
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00004633}
4634
4635template <class _CharT, class _Traits>
4636void
4637basic_regex<_CharT, _Traits>::__push_end_marked_subexpression(unsigned __sub)
4638{
Howard Hinnant68025ed2010-07-14 15:45:11 +00004639 if (!(__flags_ & nosubs))
4640 {
4641 __end_->first() =
4642 new __end_marked_subexpression<_CharT>(__sub, __end_->first());
4643 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4644 }
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00004645}
4646
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00004647template <class _CharT, class _Traits>
4648void
4649basic_regex<_CharT, _Traits>::__push_r_anchor()
4650{
4651 __end_->first() = new __r_anchor<_CharT>(__end_->first());
4652 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4653}
4654
Howard Hinnantac303862010-07-12 15:51:17 +00004655template <class _CharT, class _Traits>
4656void
4657basic_regex<_CharT, _Traits>::__push_match_any()
4658{
4659 __end_->first() = new __match_any<_CharT>(__end_->first());
4660 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4661}
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00004662
Howard Hinnantcba352d2010-07-12 18:16:05 +00004663template <class _CharT, class _Traits>
4664void
Howard Hinnant17615b02010-07-27 01:25:38 +00004665basic_regex<_CharT, _Traits>::__push_match_any_but_newline()
4666{
4667 __end_->first() = new __match_any_but_newline<_CharT>(__end_->first());
4668 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4669}
4670
4671template <class _CharT, class _Traits>
4672void
Howard Hinnant2ade7c22010-07-22 17:53:24 +00004673basic_regex<_CharT, _Traits>::__push_empty()
4674{
4675 __end_->first() = new __empty_state<_CharT>(__end_->first());
4676 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4677}
4678
4679template <class _CharT, class _Traits>
4680void
Howard Hinnant17615b02010-07-27 01:25:38 +00004681basic_regex<_CharT, _Traits>::__push_word_boundary(bool __invert)
4682{
4683 __end_->first() = new __word_boundary<_CharT, _Traits>(__traits_, __invert,
4684 __end_->first());
4685 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4686}
4687
4688template <class _CharT, class _Traits>
4689void
Howard Hinnantcba352d2010-07-12 18:16:05 +00004690basic_regex<_CharT, _Traits>::__push_back_ref(int __i)
4691{
Howard Hinnant173968a2010-07-13 21:48:06 +00004692 if (flags() & icase)
Howard Hinnante34f17d2010-07-12 19:11:27 +00004693 __end_->first() = new __back_ref_icase<_CharT, _Traits>
4694 (__traits_, __i, __end_->first());
Howard Hinnant173968a2010-07-13 21:48:06 +00004695 else if (flags() & collate)
Howard Hinnante34f17d2010-07-12 19:11:27 +00004696 __end_->first() = new __back_ref_collate<_CharT, _Traits>
4697 (__traits_, __i, __end_->first());
4698 else
4699 __end_->first() = new __back_ref<_CharT>(__i, __end_->first());
Howard Hinnantcba352d2010-07-12 18:16:05 +00004700 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4701}
4702
Howard Hinnant173968a2010-07-13 21:48:06 +00004703template <class _CharT, class _Traits>
Howard Hinnantaa698082010-07-16 19:08:36 +00004704void
4705basic_regex<_CharT, _Traits>::__push_alternation(__owns_one_state<_CharT>* __sa,
4706 __owns_one_state<_CharT>* __ea)
4707{
4708 __sa->first() = new __alternate<_CharT>(
4709 static_cast<__owns_one_state<_CharT>*>(__sa->first()),
4710 static_cast<__owns_one_state<_CharT>*>(__ea->first()));
4711 __ea->first() = nullptr;
4712 __ea->first() = new __empty_state<_CharT>(__end_->first());
4713 __end_->first() = nullptr;
4714 __end_->first() = new __empty_non_own_state<_CharT>(__ea->first());
4715 __end_ = static_cast<__owns_one_state<_CharT>*>(__ea->first());
4716}
4717
4718template <class _CharT, class _Traits>
Howard Hinnant173968a2010-07-13 21:48:06 +00004719__bracket_expression<_CharT, _Traits>*
4720basic_regex<_CharT, _Traits>::__start_matching_list(bool __negate)
4721{
4722 __bracket_expression<_CharT, _Traits>* __r =
4723 new __bracket_expression<_CharT, _Traits>(__traits_, __end_->first(),
4724 __negate, __flags_ & icase,
4725 __flags_ & collate);
4726 __end_->first() = __r;
4727 __end_ = __r;
4728 return __r;
4729}
4730
Howard Hinnante9de5ff2010-07-27 22:20:32 +00004731template <class _CharT, class _Traits>
4732void
4733basic_regex<_CharT, _Traits>::__push_lookahead(const basic_regex& __exp,
4734 bool __invert)
4735{
4736 __end_->first() = new __lookahead<_CharT, _Traits>(__exp, __invert,
4737 __end_->first());
4738 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4739}
4740
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00004741typedef basic_regex<char> regex;
4742typedef basic_regex<wchar_t> wregex;
4743
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00004744// sub_match
4745
4746template <class _BidirectionalIterator>
4747class sub_match
4748 : public pair<_BidirectionalIterator, _BidirectionalIterator>
4749{
4750public:
4751 typedef _BidirectionalIterator iterator;
4752 typedef typename iterator_traits<iterator>::value_type value_type;
4753 typedef typename iterator_traits<iterator>::difference_type difference_type;
4754 typedef basic_string<value_type> string_type;
4755
4756 bool matched;
4757
4758 difference_type length() const
4759 {return matched ? _STD::distance(this->first, this->second) : 0;}
4760 string_type str() const
4761 {return matched ? string_type(this->first, this->second) : string_type();}
4762 operator string_type() const
4763 {return str();}
4764
4765 int compare(const sub_match& __s) const
4766 {return str().compare(__s.str());}
4767 int compare(const string_type& __s) const
4768 {return str().compare(__s);}
4769 int compare(const value_type* __s) const
4770 {return str().compare(__s);}
4771};
4772
4773typedef sub_match<const char*> csub_match;
4774typedef sub_match<const wchar_t*> wcsub_match;
4775typedef sub_match<string::const_iterator> ssub_match;
4776typedef sub_match<wstring::const_iterator> wssub_match;
4777
4778template <class _BiIter>
4779inline _LIBCPP_INLINE_VISIBILITY
4780bool
4781operator==(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
4782{
4783 return __x.compare(__y) == 0;
4784}
4785
4786template <class _BiIter>
4787inline _LIBCPP_INLINE_VISIBILITY
4788bool
4789operator!=(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
4790{
4791 return !(__x == __y);
4792}
4793
4794template <class _BiIter>
4795inline _LIBCPP_INLINE_VISIBILITY
4796bool
4797operator<(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
4798{
4799 return __x.compare(__y) < 0;
4800}
4801
4802template <class _BiIter>
4803inline _LIBCPP_INLINE_VISIBILITY
4804bool
4805operator<=(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
4806{
4807 return !(__y < __x);
4808}
4809
4810template <class _BiIter>
4811inline _LIBCPP_INLINE_VISIBILITY
4812bool
4813operator>=(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
4814{
4815 return !(__x < __y);
4816}
4817
4818template <class _BiIter>
4819inline _LIBCPP_INLINE_VISIBILITY
4820bool
4821operator>(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
4822{
4823 return __y < __x;
4824}
4825
4826template <class _BiIter, class _ST, class _SA>
4827inline _LIBCPP_INLINE_VISIBILITY
4828bool
4829operator==(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4830 const sub_match<_BiIter>& __y)
4831{
4832 return __y.compare(__x.c_str()) == 0;
4833}
4834
4835template <class _BiIter, class _ST, class _SA>
4836inline _LIBCPP_INLINE_VISIBILITY
4837bool
4838operator!=(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4839 const sub_match<_BiIter>& __y)
4840{
4841 return !(__x == __y);
4842}
4843
4844template <class _BiIter, class _ST, class _SA>
4845inline _LIBCPP_INLINE_VISIBILITY
4846bool
4847operator<(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4848 const sub_match<_BiIter>& __y)
4849{
4850 return __y.compare(__x.c_str()) > 0;
4851}
4852
4853template <class _BiIter, class _ST, class _SA>
4854inline _LIBCPP_INLINE_VISIBILITY
4855bool
4856operator>(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4857 const sub_match<_BiIter>& __y)
4858{
4859 return __y < __x;
4860}
4861
4862template <class _BiIter, class _ST, class _SA>
4863inline _LIBCPP_INLINE_VISIBILITY
4864bool operator>=(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4865 const sub_match<_BiIter>& __y)
4866{
4867 return !(__x < __y);
4868}
4869
4870template <class _BiIter, class _ST, class _SA>
4871inline _LIBCPP_INLINE_VISIBILITY
4872bool
4873operator<=(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4874 const sub_match<_BiIter>& __y)
4875{
4876 return !(__y < __x);
4877}
4878
4879template <class _BiIter, class _ST, class _SA>
4880inline _LIBCPP_INLINE_VISIBILITY
4881bool
4882operator==(const sub_match<_BiIter>& __x,
4883 const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
4884{
4885 return __x.compare(__y.c_str()) == 0;
4886}
4887
4888template <class _BiIter, class _ST, class _SA>
4889inline _LIBCPP_INLINE_VISIBILITY
4890bool
4891operator!=(const sub_match<_BiIter>& __x,
4892 const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
4893{
4894 return !(__x == __y);
4895}
4896
4897template <class _BiIter, class _ST, class _SA>
4898inline _LIBCPP_INLINE_VISIBILITY
4899bool
4900operator<(const sub_match<_BiIter>& __x,
4901 const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
4902{
4903 return __x.compare(__y.c_str()) < 0;
4904}
4905
4906template <class _BiIter, class _ST, class _SA>
4907inline _LIBCPP_INLINE_VISIBILITY
4908bool operator>(const sub_match<_BiIter>& __x,
4909 const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
4910{
4911 return __y < __x;
4912}
4913
4914template <class _BiIter, class _ST, class _SA>
4915inline _LIBCPP_INLINE_VISIBILITY
4916bool
4917operator>=(const sub_match<_BiIter>& __x,
4918 const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
4919{
4920 return !(__x < __y);
4921}
4922
4923template <class _BiIter, class _ST, class _SA>
4924inline _LIBCPP_INLINE_VISIBILITY
4925bool
4926operator<=(const sub_match<_BiIter>& __x,
4927 const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
4928{
4929 return !(__y < __x);
4930}
4931
4932template <class _BiIter>
4933inline _LIBCPP_INLINE_VISIBILITY
4934bool
4935operator==(typename iterator_traits<_BiIter>::value_type const* __x,
4936 const sub_match<_BiIter>& __y)
4937{
4938 return __y.compare(__x) == 0;
4939}
4940
4941template <class _BiIter>
4942inline _LIBCPP_INLINE_VISIBILITY
4943bool
4944operator!=(typename iterator_traits<_BiIter>::value_type const* __x,
4945 const sub_match<_BiIter>& __y)
4946{
4947 return !(__x == __y);
4948}
4949
4950template <class _BiIter>
4951inline _LIBCPP_INLINE_VISIBILITY
4952bool
4953operator<(typename iterator_traits<_BiIter>::value_type const* __x,
4954 const sub_match<_BiIter>& __y)
4955{
4956 return __y.compare(__x) > 0;
4957}
4958
4959template <class _BiIter>
4960inline _LIBCPP_INLINE_VISIBILITY
4961bool
4962operator>(typename iterator_traits<_BiIter>::value_type const* __x,
4963 const sub_match<_BiIter>& __y)
4964{
4965 return __y < __x;
4966}
4967
4968template <class _BiIter>
4969inline _LIBCPP_INLINE_VISIBILITY
4970bool
4971operator>=(typename iterator_traits<_BiIter>::value_type const* __x,
4972 const sub_match<_BiIter>& __y)
4973{
4974 return !(__x < __y);
4975}
4976
4977template <class _BiIter>
4978inline _LIBCPP_INLINE_VISIBILITY
4979bool
4980operator<=(typename iterator_traits<_BiIter>::value_type const* __x,
4981 const sub_match<_BiIter>& __y)
4982{
4983 return !(__y < __x);
4984}
4985
4986template <class _BiIter>
4987inline _LIBCPP_INLINE_VISIBILITY
4988bool
4989operator==(const sub_match<_BiIter>& __x,
4990 typename iterator_traits<_BiIter>::value_type const* __y)
4991{
4992 return __x.compare(__y) == 0;
4993}
4994
4995template <class _BiIter>
4996inline _LIBCPP_INLINE_VISIBILITY
4997bool
4998operator!=(const sub_match<_BiIter>& __x,
4999 typename iterator_traits<_BiIter>::value_type const* __y)
5000{
5001 return !(__x == __y);
5002}
5003
5004template <class _BiIter>
5005inline _LIBCPP_INLINE_VISIBILITY
5006bool
5007operator<(const sub_match<_BiIter>& __x,
5008 typename iterator_traits<_BiIter>::value_type const* __y)
5009{
5010 return __x.compare(__y) < 0;
5011}
5012
5013template <class _BiIter>
5014inline _LIBCPP_INLINE_VISIBILITY
5015bool
5016operator>(const sub_match<_BiIter>& __x,
5017 typename iterator_traits<_BiIter>::value_type const* __y)
5018{
5019 return __y < __x;
5020}
5021
5022template <class _BiIter>
5023inline _LIBCPP_INLINE_VISIBILITY
5024bool
5025operator>=(const sub_match<_BiIter>& __x,
5026 typename iterator_traits<_BiIter>::value_type const* __y)
5027{
5028 return !(__x < __y);
5029}
5030
5031template <class _BiIter>
5032inline _LIBCPP_INLINE_VISIBILITY
5033bool
5034operator<=(const sub_match<_BiIter>& __x,
5035 typename iterator_traits<_BiIter>::value_type const* __y)
5036{
5037 return !(__y < __x);
5038}
5039
5040template <class _BiIter>
5041inline _LIBCPP_INLINE_VISIBILITY
5042bool
5043operator==(typename iterator_traits<_BiIter>::value_type const& __x,
5044 const sub_match<_BiIter>& __y)
5045{
5046 typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
5047 return __y.compare(string_type(1, __x)) == 0;
5048}
5049
5050template <class _BiIter>
5051inline _LIBCPP_INLINE_VISIBILITY
5052bool
5053operator!=(typename iterator_traits<_BiIter>::value_type const& __x,
5054 const sub_match<_BiIter>& __y)
5055{
5056 return !(__x == __y);
5057}
5058
5059template <class _BiIter>
5060inline _LIBCPP_INLINE_VISIBILITY
5061bool
5062operator<(typename iterator_traits<_BiIter>::value_type const& __x,
5063 const sub_match<_BiIter>& __y)
5064{
5065 typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
5066 return __y.compare(string_type(1, __x)) > 0;
5067}
5068
5069template <class _BiIter>
5070inline _LIBCPP_INLINE_VISIBILITY
5071bool
5072operator>(typename iterator_traits<_BiIter>::value_type const& __x,
5073 const sub_match<_BiIter>& __y)
5074{
5075 return __y < __x;
5076}
5077
5078template <class _BiIter>
5079inline _LIBCPP_INLINE_VISIBILITY
5080bool
5081operator>=(typename iterator_traits<_BiIter>::value_type const& __x,
5082 const sub_match<_BiIter>& __y)
5083{
5084 return !(__x < __y);
5085}
5086
5087template <class _BiIter>
5088inline _LIBCPP_INLINE_VISIBILITY
5089bool
5090operator<=(typename iterator_traits<_BiIter>::value_type const& __x,
5091 const sub_match<_BiIter>& __y)
5092{
5093 return !(__y < __x);
5094}
5095
5096template <class _BiIter>
5097inline _LIBCPP_INLINE_VISIBILITY
5098bool
5099operator==(const sub_match<_BiIter>& __x,
5100 typename iterator_traits<_BiIter>::value_type const& __y)
5101{
5102 typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
5103 return __x.compare(string_type(1, __y)) == 0;
5104}
5105
5106template <class _BiIter>
5107inline _LIBCPP_INLINE_VISIBILITY
5108bool
5109operator!=(const sub_match<_BiIter>& __x,
5110 typename iterator_traits<_BiIter>::value_type const& __y)
5111{
5112 return !(__x == __y);
5113}
5114
5115template <class _BiIter>
5116inline _LIBCPP_INLINE_VISIBILITY
5117bool
5118operator<(const sub_match<_BiIter>& __x,
5119 typename iterator_traits<_BiIter>::value_type const& __y)
5120{
5121 typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
5122 return __x.compare(string_type(1, __y)) < 0;
5123}
5124
5125template <class _BiIter>
5126inline _LIBCPP_INLINE_VISIBILITY
5127bool
5128operator>(const sub_match<_BiIter>& __x,
5129 typename iterator_traits<_BiIter>::value_type const& __y)
5130{
5131 return __y < __x;
5132}
5133
5134template <class _BiIter>
5135inline _LIBCPP_INLINE_VISIBILITY
5136bool
5137operator>=(const sub_match<_BiIter>& __x,
5138 typename iterator_traits<_BiIter>::value_type const& __y)
5139{
5140 return !(__x < __y);
5141}
5142
5143template <class _BiIter>
5144inline _LIBCPP_INLINE_VISIBILITY
5145bool
5146operator<=(const sub_match<_BiIter>& __x,
5147 typename iterator_traits<_BiIter>::value_type const& __y)
5148{
5149 return !(__y < __x);
5150}
5151
5152template <class _CharT, class _ST, class _BiIter>
5153inline _LIBCPP_INLINE_VISIBILITY
5154basic_ostream<_CharT, _ST>&
5155operator<<(basic_ostream<_CharT, _ST>& __os, const sub_match<_BiIter>& __m)
5156{
5157 return __os << __m.str();
5158}
5159
Howard Hinnant17615b02010-07-27 01:25:38 +00005160template <class _BidirectionalIterator, class _Allocator>
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005161class match_results
5162{
5163public:
5164 typedef _Allocator allocator_type;
5165 typedef sub_match<_BidirectionalIterator> value_type;
5166private:
5167 typedef vector<value_type, allocator_type> __container_type;
5168
5169 __container_type __matches_;
5170 value_type __unmatched_;
5171 value_type __prefix_;
5172 value_type __suffix_;
5173public:
5174 typedef const value_type& const_reference;
5175 typedef const_reference reference;
5176 typedef typename __container_type::const_iterator const_iterator;
5177 typedef const_iterator iterator;
5178 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
5179 typedef typename allocator_traits<allocator_type>::size_type size_type;
5180 typedef typename iterator_traits<_BidirectionalIterator>::value_type char_type;
5181 typedef basic_string<char_type> string_type;
5182
5183 // construct/copy/destroy:
5184 explicit match_results(const allocator_type& __a = allocator_type());
5185// match_results(const match_results&) = default;
5186// match_results& operator=(const match_results&) = default;
5187#ifdef _LIBCPP_MOVE
5188// match_results(match_results&& __m) = default;
5189// match_results& operator=(match_results&& __m) = default;
5190#endif
5191// ~match_results() = default;
5192
5193 // size:
5194 size_type size() const {return __matches_.size();}
5195 size_type max_size() const {return __matches_.max_size();}
5196 bool empty() const {return size() == 0;}
5197
5198 // element access:
5199 difference_type length(size_type __sub = 0) const
5200 {return (*this)[__sub].length();}
5201 difference_type position(size_type __sub = 0) const
5202 {return _STD::distance(__prefix_.first, (*this)[__sub].first);}
5203 string_type str(size_type __sub = 0) const
5204 {return (*this)[__sub].str();}
5205 const_reference operator[](size_type __n) const
5206 {return __n < __matches_.size() ? __matches_[__n] : __unmatched_;}
5207
5208 const_reference prefix() const {return __prefix_;}
5209 const_reference suffix() const {return __suffix_;}
5210
5211 const_iterator begin() const {return empty() ? __matches_.end() : __matches_.begin() + 1;}
5212 const_iterator end() const {return __matches_.end();}
5213 const_iterator cbegin() const {return empty() ? __matches_.end() : __matches_.begin() + 1;}
5214 const_iterator cend() const {return __matches_.end();}
5215
5216 // format:
5217 template <class _OutputIter>
5218 _OutputIter
5219 format(_OutputIter __out, const char_type* __fmt_first,
5220 const char_type* __fmt_last,
5221 regex_constants::match_flag_type __flags = regex_constants::format_default) const;
5222 template <class _OutputIter, class _ST, class _SA>
5223 _OutputIter
5224 format(_OutputIter __out, const basic_string<char_type, _ST, _SA>& __fmt,
5225 regex_constants::match_flag_type __flags = regex_constants::format_default) const;
5226 template <class _ST, class _SA>
5227 basic_string<char_type, _ST, _SA>
5228 format(const basic_string<char_type, _ST, _SA>& __fmt,
5229 regex_constants::match_flag_type __flags = regex_constants::format_default) const;
5230 string_type
5231 format(const char_type* __fmt,
5232 regex_constants::match_flag_type __flags = regex_constants::format_default) const;
5233
5234 // allocator:
5235 allocator_type get_allocator() const {return __matches_.get_allocator();}
5236
5237 // swap:
5238 void swap(match_results& __m);
5239
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005240 template <class _B, class _A>
5241 void __assign(_BidirectionalIterator __f, _BidirectionalIterator __l,
5242 const match_results<_B, _A>& __m)
5243 {
5244 _B __mf = __m.prefix().first;
5245 __matches_.resize(__m.size());
5246 for (size_type __i = 0; __i < __matches_.size(); ++__i)
5247 {
5248 __matches_[__i].first = next(__f, _STD::distance(__mf, __m[__i].first));
5249 __matches_[__i].second = next(__f, _STD::distance(__mf, __m[__i].second));
5250 __matches_[__i].matched = __m[__i].matched;
5251 }
5252 __unmatched_.first = __l;
5253 __unmatched_.second = __l;
5254 __unmatched_.matched = false;
5255 __prefix_.first = next(__f, _STD::distance(__mf, __m.prefix().first));
5256 __prefix_.second = next(__f, _STD::distance(__mf, __m.prefix().second));
5257 __prefix_.matched = __m.prefix().matched;
5258 __suffix_.first = next(__f, _STD::distance(__mf, __m.suffix().first));
5259 __suffix_.second = next(__f, _STD::distance(__mf, __m.suffix().second));
5260 __suffix_.matched = __m.suffix().matched;
5261 }
5262
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005263private:
5264 void __init(unsigned __s,
5265 _BidirectionalIterator __f, _BidirectionalIterator __l);
5266
5267 template <class, class> friend class basic_regex;
5268
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005269 template <class _B, class _A, class _C, class _T>
5270 friend
5271 bool
5272 regex_match(_B, _B, match_results<_B, _A>&, const basic_regex<_C, _T>&,
5273 regex_constants::match_flag_type);
Howard Hinnante9de5ff2010-07-27 22:20:32 +00005274
5275 template <class, class> friend class __lookahead;
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005276};
5277
5278template <class _BidirectionalIterator, class _Allocator>
5279match_results<_BidirectionalIterator, _Allocator>::match_results(
5280 const allocator_type& __a)
5281 : __matches_(__a),
5282 __unmatched_(),
5283 __prefix_(),
5284 __suffix_()
5285{
5286}
5287
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005288template <class _BidirectionalIterator, class _Allocator>
5289void
5290match_results<_BidirectionalIterator, _Allocator>::__init(unsigned __s,
5291 _BidirectionalIterator __f, _BidirectionalIterator __l)
5292{
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005293 __unmatched_.first = __l;
5294 __unmatched_.second = __l;
5295 __unmatched_.matched = false;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005296 __matches_.assign(__s, __unmatched_);
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005297 __prefix_.first = __f;
5298 __prefix_.second = __f;
5299 __prefix_.matched = false;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005300 __suffix_ = __unmatched_;
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005301}
5302
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005303typedef match_results<const char*> cmatch;
5304typedef match_results<const wchar_t*> wcmatch;
5305typedef match_results<string::const_iterator> smatch;
5306typedef match_results<wstring::const_iterator> wsmatch;
5307
5308template <class _BidirectionalIterator, class _Allocator>
5309 bool
5310 operator==(const match_results<_BidirectionalIterator, _Allocator>& __x,
5311 const match_results<_BidirectionalIterator, _Allocator>& __y);
5312
5313template <class _BidirectionalIterator, class _Allocator>
5314 bool
5315 operator!=(const match_results<_BidirectionalIterator, _Allocator>& __x,
5316 const match_results<_BidirectionalIterator, _Allocator>& __y);
5317
5318template <class _BidirectionalIterator, class _Allocator>
5319 void
5320 swap(match_results<_BidirectionalIterator, _Allocator>& __x,
5321 match_results<_BidirectionalIterator, _Allocator>& __y);
5322
5323// regex_search
5324
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005325template <class _CharT, class _Traits>
Howard Hinnant17615b02010-07-27 01:25:38 +00005326template <class _Allocator>
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005327bool
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005328basic_regex<_CharT, _Traits>::__match_at_start_ecma(
Howard Hinnant17615b02010-07-27 01:25:38 +00005329 const _CharT* __first, const _CharT* __last,
5330 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005331 regex_constants::match_flag_type __flags) const
5332{
Howard Hinnant17615b02010-07-27 01:25:38 +00005333 vector<__state> __states;
5334 ptrdiff_t __j = 0;
5335 ptrdiff_t _N = _STD::distance(__first, __last);
5336 __node* __st = __start_.get();
5337 if (__st)
5338 {
5339 __states.push_back(__state());
5340 __states.back().__do_ = 0;
5341 __states.back().__first_ = __first;
5342 __states.back().__current_ = __first;
5343 __states.back().__last_ = __last;
5344 __states.back().__sub_matches_.resize(mark_count());
5345 __states.back().__loop_data_.resize(__loop_count());
5346 __states.back().__node_ = __st;
5347 __states.back().__flags_ = __flags;
5348 bool __matched = false;
5349 do
5350 {
5351 __state& __s = __states.back();
5352 if (__s.__node_)
5353 __s.__node_->__exec(__s);
5354 switch (__s.__do_)
5355 {
5356 case __state::__end_state:
5357 __m.__matches_[0].first = __first;
5358 __m.__matches_[0].second = _STD::next(__first, __s.__current_ - __first);
5359 __m.__matches_[0].matched = true;
5360 for (unsigned __i = 0; __i < __s.__sub_matches_.size(); ++__i)
5361 __m.__matches_[__i+1] = __s.__sub_matches_[__i];
5362 return true;
5363 case __state::__accept_and_consume:
5364 case __state::__repeat:
5365 case __state::__accept_but_not_consume:
5366 break;
5367 case __state::__split:
5368 {
5369 __state __snext = __s;
5370 __s.__node_->__exec_split(true, __s);
5371 __snext.__node_->__exec_split(false, __snext);
5372 __states.push_back(_STD::move(__snext));
5373 }
5374 break;
5375 case __state::__reject:
5376 __states.pop_back();
5377 break;
5378 default:
Howard Hinnantd4444702010-08-11 17:04:31 +00005379#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005380 throw regex_error(regex_constants::__re_err_unknown);
Howard Hinnantd4444702010-08-11 17:04:31 +00005381#endif
Howard Hinnant17615b02010-07-27 01:25:38 +00005382 break;
Howard Hinnantd4444702010-08-11 17:04:31 +00005383
Howard Hinnant17615b02010-07-27 01:25:38 +00005384 }
5385 } while (!__states.empty());
5386 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005387 return false;
5388}
5389
5390template <class _CharT, class _Traits>
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005391template <class _Allocator>
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005392bool
5393basic_regex<_CharT, _Traits>::__match_at_start_posix_nosubs(
5394 const _CharT* __first, const _CharT* __last,
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005395 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005396 regex_constants::match_flag_type __flags) const
5397{
Howard Hinnantac303862010-07-12 15:51:17 +00005398 deque<__state> __states;
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005399 ptrdiff_t __highest_j = 0;
5400 ptrdiff_t _N = _STD::distance(__first, __last);
Howard Hinnantac303862010-07-12 15:51:17 +00005401 __node* __st = __start_.get();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005402 if (__st)
5403 {
Howard Hinnantac303862010-07-12 15:51:17 +00005404 __states.push_back(__state());
Howard Hinnantac303862010-07-12 15:51:17 +00005405 __states.back().__do_ = 0;
5406 __states.back().__first_ = __first;
5407 __states.back().__current_ = __first;
5408 __states.back().__last_ = __last;
5409 __states.back().__loop_data_.resize(__loop_count());
5410 __states.back().__node_ = __st;
5411 __states.back().__flags_ = __flags;
Howard Hinnant68025ed2010-07-14 15:45:11 +00005412 bool __matched = false;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005413 do
5414 {
Howard Hinnantac303862010-07-12 15:51:17 +00005415 __state& __s = __states.back();
5416 if (__s.__node_)
5417 __s.__node_->__exec(__s);
5418 switch (__s.__do_)
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005419 {
Howard Hinnantac303862010-07-12 15:51:17 +00005420 case __state::__end_state:
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005421 if (!__matched || __highest_j < __s.__current_ - __s.__first_)
Howard Hinnant68025ed2010-07-14 15:45:11 +00005422 __highest_j = __s.__current_ - __s.__first_;
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005423 __matched = true;
Howard Hinnantac303862010-07-12 15:51:17 +00005424 if (__highest_j == _N)
5425 __states.clear();
5426 else
5427 __states.pop_back();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005428 break;
Howard Hinnantac303862010-07-12 15:51:17 +00005429 case __state::__consume_input:
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005430 break;
Howard Hinnantac303862010-07-12 15:51:17 +00005431 case __state::__accept_and_consume:
Howard Hinnantac303862010-07-12 15:51:17 +00005432 __states.push_front(_STD::move(__s));
5433 __states.pop_back();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005434 break;
Howard Hinnantac303862010-07-12 15:51:17 +00005435 case __state::__repeat:
5436 case __state::__accept_but_not_consume:
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005437 break;
Howard Hinnantac303862010-07-12 15:51:17 +00005438 case __state::__split:
5439 {
5440 __state __snext = __s;
5441 __s.__node_->__exec_split(true, __s);
5442 __snext.__node_->__exec_split(false, __snext);
5443 __states.push_back(_STD::move(__snext));
5444 }
5445 break;
5446 case __state::__reject:
5447 __states.pop_back();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005448 break;
5449 default:
Howard Hinnantd4444702010-08-11 17:04:31 +00005450#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005451 throw regex_error(regex_constants::__re_err_unknown);
Howard Hinnantd4444702010-08-11 17:04:31 +00005452#endif
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005453 break;
5454 }
Howard Hinnantac303862010-07-12 15:51:17 +00005455 } while (!__states.empty());
Howard Hinnant68025ed2010-07-14 15:45:11 +00005456 if (__matched)
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005457 {
5458 __m.__matches_[0].first = __first;
5459 __m.__matches_[0].second = _STD::next(__first, __highest_j);
5460 __m.__matches_[0].matched = true;
5461 return true;
5462 }
5463 }
5464 return false;
5465}
5466
5467template <class _CharT, class _Traits>
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005468template <class _Allocator>
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005469bool
5470basic_regex<_CharT, _Traits>::__match_at_start_posix_subs(
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005471 const _CharT* __first, const _CharT* __last,
5472 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005473 regex_constants::match_flag_type __flags) const
5474{
Howard Hinnantac303862010-07-12 15:51:17 +00005475 vector<__state> __states;
Howard Hinnantac303862010-07-12 15:51:17 +00005476 __state __best_state;
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005477 ptrdiff_t __j = 0;
5478 ptrdiff_t __highest_j = 0;
5479 ptrdiff_t _N = _STD::distance(__first, __last);
Howard Hinnantac303862010-07-12 15:51:17 +00005480 __node* __st = __start_.get();
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005481 if (__st)
5482 {
Howard Hinnantac303862010-07-12 15:51:17 +00005483 __states.push_back(__state());
5484 __states.back().__do_ = 0;
5485 __states.back().__first_ = __first;
5486 __states.back().__current_ = __first;
5487 __states.back().__last_ = __last;
5488 __states.back().__sub_matches_.resize(mark_count());
5489 __states.back().__loop_data_.resize(__loop_count());
5490 __states.back().__node_ = __st;
5491 __states.back().__flags_ = __flags;
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005492 const _CharT* __current = __first;
Howard Hinnantac303862010-07-12 15:51:17 +00005493 bool __matched = false;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005494 do
5495 {
Howard Hinnantac303862010-07-12 15:51:17 +00005496 __state& __s = __states.back();
5497 if (__s.__node_)
5498 __s.__node_->__exec(__s);
5499 switch (__s.__do_)
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005500 {
Howard Hinnantac303862010-07-12 15:51:17 +00005501 case __state::__end_state:
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005502 if (!__matched || __highest_j < __s.__current_ - __s.__first_)
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005503 {
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005504 __highest_j = __s.__current_ - __s.__first_;
Howard Hinnantac303862010-07-12 15:51:17 +00005505 __best_state = __s;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005506 }
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005507 __matched = true;
5508 if (__highest_j == _N)
5509 __states.clear();
5510 else
5511 __states.pop_back();
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005512 break;
Howard Hinnantac303862010-07-12 15:51:17 +00005513 case __state::__accept_and_consume:
Howard Hinnantcba352d2010-07-12 18:16:05 +00005514 __j += __s.__current_ - __current;
5515 __current = __s.__current_;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005516 break;
Howard Hinnantac303862010-07-12 15:51:17 +00005517 case __state::__repeat:
5518 case __state::__accept_but_not_consume:
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005519 break;
Howard Hinnantac303862010-07-12 15:51:17 +00005520 case __state::__split:
5521 {
5522 __state __snext = __s;
5523 __s.__node_->__exec_split(true, __s);
5524 __snext.__node_->__exec_split(false, __snext);
5525 __states.push_back(_STD::move(__snext));
5526 }
5527 break;
5528 case __state::__reject:
5529 __states.pop_back();
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005530 break;
5531 default:
Howard Hinnantd4444702010-08-11 17:04:31 +00005532#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005533 throw regex_error(regex_constants::__re_err_unknown);
Howard Hinnantd4444702010-08-11 17:04:31 +00005534#endif
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005535 break;
5536 }
Howard Hinnantac303862010-07-12 15:51:17 +00005537 } while (!__states.empty());
5538 if (__matched)
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005539 {
5540 __m.__matches_[0].first = __first;
5541 __m.__matches_[0].second = _STD::next(__first, __highest_j);
5542 __m.__matches_[0].matched = true;
Howard Hinnantac303862010-07-12 15:51:17 +00005543 for (unsigned __i = 0; __i < __best_state.__sub_matches_.size(); ++__i)
5544 __m.__matches_[__i+1] = __best_state.__sub_matches_[__i];
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005545 return true;
5546 }
5547 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005548 return false;
5549}
5550
5551template <class _CharT, class _Traits>
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005552template <class _Allocator>
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005553bool
5554basic_regex<_CharT, _Traits>::__match_at_start(
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005555 const _CharT* __first, const _CharT* __last,
5556 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005557 regex_constants::match_flag_type __flags) const
5558{
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005559 if ((__flags_ & 0x1F0) == ECMAScript)
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005560 return __match_at_start_ecma(__first, __last, __m, __flags);
5561 if (mark_count() == 0)
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005562 return __match_at_start_posix_nosubs(__first, __last, __m, __flags);
5563 return __match_at_start_posix_subs(__first, __last, __m, __flags);
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005564}
5565
5566template <class _CharT, class _Traits>
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005567template <class _Allocator>
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005568bool
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005569basic_regex<_CharT, _Traits>::__search(
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005570 const _CharT* __first, const _CharT* __last,
5571 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005572 regex_constants::match_flag_type __flags) const
5573{
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00005574 if (__left_anchor_)
5575 __flags |= regex_constants::match_continuous;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005576 __m.__init(1 + mark_count(), __first, __last);
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005577 if (__match_at_start(__first, __last, __m, __flags))
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005578 {
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005579 __m.__prefix_.second = __m[0].first;
5580 __m.__prefix_.matched = __m.__prefix_.first != __m.__prefix_.second;
5581 __m.__suffix_.first = __m[0].second;
5582 __m.__suffix_.matched = __m.__suffix_.first != __m.__suffix_.second;
5583 return true;
5584 }
Howard Hinnant8daa7332010-07-29 01:15:27 +00005585 if (__first != __last && !(__flags & regex_constants::match_continuous))
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005586 {
Howard Hinnantf3dcca02010-07-29 15:17:28 +00005587 __flags |= regex_constants::match_prev_avail;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005588 for (++__first; __first != __last; ++__first)
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005589 {
Howard Hinnantf3dcca02010-07-29 15:17:28 +00005590 __m.__matches_.assign(__m.size(), __m.__unmatched_);
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005591 if (__match_at_start(__first, __last, __m, __flags))
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005592 {
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005593 __m.__prefix_.second = __m[0].first;
5594 __m.__prefix_.matched = __m.__prefix_.first != __m.__prefix_.second;
5595 __m.__suffix_.first = __m[0].second;
5596 __m.__suffix_.matched = __m.__suffix_.first != __m.__suffix_.second;
5597 return true;
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005598 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005599 __m.__matches_.assign(__m.size(), __m.__unmatched_);
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005600 }
5601 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005602 __m.__matches_.clear();
5603 return false;
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005604}
5605
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005606template <class _BidirectionalIterator, class _Allocator, class _CharT, class _Traits>
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005607inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005608bool
5609regex_search(_BidirectionalIterator __first, _BidirectionalIterator __last,
5610 match_results<_BidirectionalIterator, _Allocator>& __m,
5611 const basic_regex<_CharT, _Traits>& __e,
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005612 regex_constants::match_flag_type __flags = regex_constants::match_default)
5613{
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005614 basic_string<_CharT> __s(__first, __last);
5615 match_results<const _CharT*> __mc;
5616 bool __r = __e.__search(__s.data(), __s.data() + __s.size(), __mc, __flags);
5617 __m.__assign(__first, __last, __mc);
5618 return __r;
5619}
5620
5621template <class _Allocator, class _CharT, class _Traits>
5622inline _LIBCPP_INLINE_VISIBILITY
5623bool
5624regex_search(const _CharT* __first, const _CharT* __last,
5625 match_results<const _CharT*, _Allocator>& __m,
5626 const basic_regex<_CharT, _Traits>& __e,
5627 regex_constants::match_flag_type __flags = regex_constants::match_default)
5628{
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005629 return __e.__search(__first, __last, __m, __flags);
5630}
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005631
5632template <class _BidirectionalIterator, class _CharT, class _Traits>
5633inline _LIBCPP_INLINE_VISIBILITY
5634bool
5635regex_search(_BidirectionalIterator __first, _BidirectionalIterator __last,
5636 const basic_regex<_CharT, _Traits>& __e,
5637 regex_constants::match_flag_type __flags = regex_constants::match_default)
5638{
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005639 basic_string<_CharT> __s(__first, __last);
5640 match_results<const _CharT*> __mc;
5641 return __e.__search(__s.data(), __s.data() + __s.size(), __mc, __flags);
5642}
5643
5644template <class _CharT, class _Traits>
5645inline _LIBCPP_INLINE_VISIBILITY
5646bool
5647regex_search(const _CharT* __first, const _CharT* __last,
5648 const basic_regex<_CharT, _Traits>& __e,
5649 regex_constants::match_flag_type __flags = regex_constants::match_default)
5650{
5651 match_results<const _CharT*> __mc;
5652 return __e.__search(__first, __last, __mc, __flags);
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005653}
5654
5655template <class _CharT, class _Allocator, class _Traits>
5656inline _LIBCPP_INLINE_VISIBILITY
5657bool
5658regex_search(const _CharT* __str, match_results<const _CharT*, _Allocator>& __m,
5659 const basic_regex<_CharT, _Traits>& __e,
5660 regex_constants::match_flag_type __flags = regex_constants::match_default)
5661{
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005662 return __e.__search(__str, __str + _Traits::length(__str), __m, __flags);
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005663}
5664
5665template <class _CharT, class _Traits>
5666inline _LIBCPP_INLINE_VISIBILITY
5667bool
5668regex_search(const _CharT* __str, const basic_regex<_CharT, _Traits>& __e,
5669 regex_constants::match_flag_type __flags = regex_constants::match_default)
5670{
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005671 match_results<const _CharT*> __m;
5672 return _STD::regex_search(__str, __m, __e, __flags);
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005673}
5674
5675template <class _ST, class _SA, class _CharT, class _Traits>
5676inline _LIBCPP_INLINE_VISIBILITY
5677bool
5678regex_search(const basic_string<_CharT, _ST, _SA>& __s,
5679 const basic_regex<_CharT, _Traits>& __e,
5680 regex_constants::match_flag_type __flags = regex_constants::match_default)
5681{
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005682 match_results<const _CharT*> __mc;
5683 return __e.__search(__s.data(), __s.data() + __s.size(), __mc, __flags);
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005684}
5685
5686template <class _ST, class _SA, class _Allocator, class _CharT, class _Traits>
5687inline _LIBCPP_INLINE_VISIBILITY
5688bool
5689regex_search(const basic_string<_CharT, _ST, _SA>& __s,
5690 match_results<typename basic_string<_CharT, _ST, _SA>::const_iterator, _Allocator>& __m,
5691 const basic_regex<_CharT, _Traits>& __e,
5692 regex_constants::match_flag_type __flags = regex_constants::match_default)
5693{
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005694 match_results<const _CharT*> __mc;
5695 bool __r = __e.__search(__s.data(), __s.data() + __s.size(), __mc, __flags);
5696 __m.__assign(__s.begin(), __s.end(), __mc);
5697 return __r;
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005698}
5699
5700// regex_match
5701
5702template <class _BidirectionalIterator, class _Allocator, class _CharT, class _Traits>
5703bool
5704regex_match(_BidirectionalIterator __first, _BidirectionalIterator __last,
5705 match_results<_BidirectionalIterator, _Allocator>& __m,
5706 const basic_regex<_CharT, _Traits>& __e,
5707 regex_constants::match_flag_type __flags = regex_constants::match_default)
5708{
5709 bool __r = _STD::regex_search(__first, __last, __m, __e,
5710 __flags | regex_constants::match_continuous);
5711 if (__r)
5712 {
5713 __r = !__m.suffix().matched;
5714 if (!__r)
5715 __m.__matches_.clear();
5716 }
5717 return __r;
5718}
5719
5720template <class _BidirectionalIterator, class _CharT, class _Traits>
5721inline _LIBCPP_INLINE_VISIBILITY
5722bool
5723regex_match(_BidirectionalIterator __first, _BidirectionalIterator __last,
5724 const basic_regex<_CharT, _Traits>& __e,
5725 regex_constants::match_flag_type __flags = regex_constants::match_default)
5726{
5727 match_results<_BidirectionalIterator> __m;
5728 return _STD::regex_match(__first, __last, __m, __e, __flags);
5729}
5730
5731template <class _CharT, class _Allocator, class _Traits>
5732inline _LIBCPP_INLINE_VISIBILITY
5733bool
5734regex_match(const _CharT* __str, match_results<const _CharT*, _Allocator>& __m,
5735 const basic_regex<_CharT, _Traits>& __e,
5736 regex_constants::match_flag_type __flags = regex_constants::match_default)
5737{
5738 return _STD::regex_match(__str, __str + _Traits::length(__str), __m, __e, __flags);
5739}
5740
5741template <class _ST, class _SA, class _Allocator, class _CharT, class _Traits>
5742inline _LIBCPP_INLINE_VISIBILITY
5743bool
5744regex_match(const basic_string<_CharT, _ST, _SA>& __s,
5745 match_results<typename basic_string<_CharT, _ST, _SA>::const_iterator, _Allocator>& __m,
5746 const basic_regex<_CharT, _Traits>& __e,
5747 regex_constants::match_flag_type __flags = regex_constants::match_default)
5748{
5749 return _STD::regex_match(__s.begin(), __s.end(), __m, __e, __flags);
5750}
5751
5752template <class _CharT, class _Traits>
5753inline _LIBCPP_INLINE_VISIBILITY
5754bool
5755regex_match(const _CharT* __str, const basic_regex<_CharT, _Traits>& __e,
5756 regex_constants::match_flag_type __flags = regex_constants::match_default)
5757{
5758 return _STD::regex_match(__str, __str + _Traits::length(__str), __e, __flags);
5759}
5760
5761template <class _ST, class _SA, class _CharT, class _Traits>
5762inline _LIBCPP_INLINE_VISIBILITY
5763bool
5764regex_match(const basic_string<_CharT, _ST, _SA>& __s,
5765 const basic_regex<_CharT, _Traits>& __e,
5766 regex_constants::match_flag_type __flags = regex_constants::match_default)
5767{
5768 return _STD::regex_match(__s.begin(), __s.end(), __e, __flags);
5769}
5770
Howard Hinnant3257c982010-06-17 00:34:59 +00005771_LIBCPP_END_NAMESPACE_STD
5772
5773#endif // _LIBCPP_REGEX