blob: be49cd2948dff561b05ed96c3f2ce726cd214253 [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
2532 ~basic_regex();
2533
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002534// basic_regex& operator=(const basic_regex&) = default;
2535// basic_regex& operator=(basic_regex&&) = default;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002536 basic_regex& operator=(const value_type* __p);
2537 basic_regex& operator=(initializer_list<value_type> __il);
2538 template <class _ST, class _SA>
2539 basic_regex& operator=(const basic_string<value_type, _ST, _SA>& __p);
2540
2541 // assign:
2542 basic_regex& assign(const basic_regex& __that);
2543#ifdef _LIBCPP_MOVE
2544 basic_regex& assign(basic_regex&& __that);
2545#endif
2546 basic_regex& assign(const value_type* __p, flag_type __f = regex_constants::ECMAScript);
2547 basic_regex& assign(const value_type* __p, size_t __len, flag_type __f);
2548 template <class _ST, class _SA>
2549 basic_regex& assign(const basic_string<value_type, _ST, _SA>& __s,
2550 flag_type __f = regex_constants::ECMAScript);
2551 template <class _InputIterator>
2552 basic_regex& assign(_InputIterator __first, _InputIterator __last,
2553 flag_type __f = regex_constants::ECMAScript);
2554 basic_regex& assign(initializer_list<value_type> __il,
2555 flag_type = regex_constants::ECMAScript);
2556
2557 // const operations:
2558 unsigned mark_count() const {return __marked_count_;}
2559 flag_type flags() const {return __flags_;}
2560
2561 // locale:
2562 locale_type imbue(locale_type __loc) {return __traits_.imbue(__loc);}
2563 locale_type getloc() const {return __traits_.getloc();}
2564
2565 // swap:
2566 void swap(basic_regex&);
2567
2568private:
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002569 unsigned __loop_count() const {return __loop_count_;}
2570
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002571 template <class _ForwardIterator>
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002572 _ForwardIterator
2573 __parse(_ForwardIterator __first, _ForwardIterator __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002574 template <class _ForwardIterator>
2575 _ForwardIterator
2576 __parse_basic_reg_exp(_ForwardIterator __first, _ForwardIterator __last);
2577 template <class _ForwardIterator>
2578 _ForwardIterator
2579 __parse_RE_expression(_ForwardIterator __first, _ForwardIterator __last);
2580 template <class _ForwardIterator>
2581 _ForwardIterator
2582 __parse_simple_RE(_ForwardIterator __first, _ForwardIterator __last);
2583 template <class _ForwardIterator>
2584 _ForwardIterator
2585 __parse_nondupl_RE(_ForwardIterator __first, _ForwardIterator __last);
2586 template <class _ForwardIterator>
2587 _ForwardIterator
2588 __parse_one_char_or_coll_elem_RE(_ForwardIterator __first, _ForwardIterator __last);
2589 template <class _ForwardIterator>
2590 _ForwardIterator
2591 __parse_Back_open_paren(_ForwardIterator __first, _ForwardIterator __last);
2592 template <class _ForwardIterator>
2593 _ForwardIterator
2594 __parse_Back_close_paren(_ForwardIterator __first, _ForwardIterator __last);
2595 template <class _ForwardIterator>
2596 _ForwardIterator
2597 __parse_Back_open_brace(_ForwardIterator __first, _ForwardIterator __last);
2598 template <class _ForwardIterator>
2599 _ForwardIterator
2600 __parse_Back_close_brace(_ForwardIterator __first, _ForwardIterator __last);
2601 template <class _ForwardIterator>
2602 _ForwardIterator
2603 __parse_BACKREF(_ForwardIterator __first, _ForwardIterator __last);
2604 template <class _ForwardIterator>
2605 _ForwardIterator
2606 __parse_ORD_CHAR(_ForwardIterator __first, _ForwardIterator __last);
2607 template <class _ForwardIterator>
2608 _ForwardIterator
2609 __parse_QUOTED_CHAR(_ForwardIterator __first, _ForwardIterator __last);
2610 template <class _ForwardIterator>
2611 _ForwardIterator
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002612 __parse_RE_dupl_symbol(_ForwardIterator __first, _ForwardIterator __last,
Howard Hinnante77aa5e2010-07-08 17:43:58 +00002613 __owns_one_state<_CharT>* __s,
2614 unsigned __mexp_begin, unsigned __mexp_end);
Howard Hinnant0de86b62010-06-25 20:56:08 +00002615 template <class _ForwardIterator>
2616 _ForwardIterator
Howard Hinnantaa698082010-07-16 19:08:36 +00002617 __parse_ERE_dupl_symbol(_ForwardIterator __first, _ForwardIterator __last,
2618 __owns_one_state<_CharT>* __s,
2619 unsigned __mexp_begin, unsigned __mexp_end);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002620 template <class _ForwardIterator>
2621 _ForwardIterator
Howard Hinnant0de86b62010-06-25 20:56:08 +00002622 __parse_bracket_expression(_ForwardIterator __first, _ForwardIterator __last);
2623 template <class _ForwardIterator>
2624 _ForwardIterator
Howard Hinnant173968a2010-07-13 21:48:06 +00002625 __parse_follow_list(_ForwardIterator __first, _ForwardIterator __last,
2626 __bracket_expression<_CharT, _Traits>* __ml);
Howard Hinnant0de86b62010-06-25 20:56:08 +00002627 template <class _ForwardIterator>
2628 _ForwardIterator
Howard Hinnant173968a2010-07-13 21:48:06 +00002629 __parse_expression_term(_ForwardIterator __first, _ForwardIterator __last,
2630 __bracket_expression<_CharT, _Traits>* __ml);
Howard Hinnant0de86b62010-06-25 20:56:08 +00002631 template <class _ForwardIterator>
2632 _ForwardIterator
Howard Hinnant173968a2010-07-13 21:48:06 +00002633 __parse_equivalence_class(_ForwardIterator __first, _ForwardIterator __last,
2634 __bracket_expression<_CharT, _Traits>* __ml);
Howard Hinnant0de86b62010-06-25 20:56:08 +00002635 template <class _ForwardIterator>
2636 _ForwardIterator
Howard Hinnant173968a2010-07-13 21:48:06 +00002637 __parse_character_class(_ForwardIterator __first, _ForwardIterator __last,
2638 __bracket_expression<_CharT, _Traits>* __ml);
Howard Hinnant0de86b62010-06-25 20:56:08 +00002639 template <class _ForwardIterator>
2640 _ForwardIterator
Howard Hinnant173968a2010-07-13 21:48:06 +00002641 __parse_collating_symbol(_ForwardIterator __first, _ForwardIterator __last,
2642 basic_string<_CharT>& __col_sym);
Howard Hinnant0de86b62010-06-25 20:56:08 +00002643 template <class _ForwardIterator>
2644 _ForwardIterator
2645 __parse_DUP_COUNT(_ForwardIterator __first, _ForwardIterator __last, int& __c);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002646 template <class _ForwardIterator>
2647 _ForwardIterator
2648 __parse_extended_reg_exp(_ForwardIterator __first, _ForwardIterator __last);
2649 template <class _ForwardIterator>
2650 _ForwardIterator
2651 __parse_ERE_branch(_ForwardIterator __first, _ForwardIterator __last);
2652 template <class _ForwardIterator>
2653 _ForwardIterator
2654 __parse_ERE_expression(_ForwardIterator __first, _ForwardIterator __last);
2655 template <class _ForwardIterator>
2656 _ForwardIterator
2657 __parse_one_char_or_coll_elem_ERE(_ForwardIterator __first, _ForwardIterator __last);
2658 template <class _ForwardIterator>
2659 _ForwardIterator
2660 __parse_ORD_CHAR_ERE(_ForwardIterator __first, _ForwardIterator __last);
2661 template <class _ForwardIterator>
2662 _ForwardIterator
2663 __parse_QUOTED_CHAR_ERE(_ForwardIterator __first, _ForwardIterator __last);
Howard Hinnant2ade7c22010-07-22 17:53:24 +00002664 template <class _ForwardIterator>
2665 _ForwardIterator
2666 __parse_ecma_exp(_ForwardIterator __first, _ForwardIterator __last);
2667 template <class _ForwardIterator>
2668 _ForwardIterator
2669 __parse_alternative(_ForwardIterator __first, _ForwardIterator __last);
2670 template <class _ForwardIterator>
2671 _ForwardIterator
2672 __parse_term(_ForwardIterator __first, _ForwardIterator __last);
2673 template <class _ForwardIterator>
2674 _ForwardIterator
2675 __parse_assertion(_ForwardIterator __first, _ForwardIterator __last);
2676 template <class _ForwardIterator>
2677 _ForwardIterator
2678 __parse_atom(_ForwardIterator __first, _ForwardIterator __last);
2679 template <class _ForwardIterator>
2680 _ForwardIterator
Howard Hinnant17615b02010-07-27 01:25:38 +00002681 __parse_atom_escape(_ForwardIterator __first, _ForwardIterator __last);
2682 template <class _ForwardIterator>
2683 _ForwardIterator
2684 __parse_decimal_escape(_ForwardIterator __first, _ForwardIterator __last);
2685 template <class _ForwardIterator>
2686 _ForwardIterator
2687 __parse_character_class_escape(_ForwardIterator __first, _ForwardIterator __last);
2688 template <class _ForwardIterator>
2689 _ForwardIterator
Howard Hinnant15476f32010-07-28 17:35:27 +00002690 __parse_character_escape(_ForwardIterator __first, _ForwardIterator __last,
2691 basic_string<_CharT>* __str = nullptr);
Howard Hinnant17615b02010-07-27 01:25:38 +00002692 template <class _ForwardIterator>
2693 _ForwardIterator
2694 __parse_pattern_character(_ForwardIterator __first, _ForwardIterator __last);
Howard Hinnant856846b2010-07-27 19:53:10 +00002695 template <class _ForwardIterator>
2696 _ForwardIterator
2697 __parse_grep(_ForwardIterator __first, _ForwardIterator __last);
2698 template <class _ForwardIterator>
2699 _ForwardIterator
2700 __parse_egrep(_ForwardIterator __first, _ForwardIterator __last);
Howard Hinnant15476f32010-07-28 17:35:27 +00002701 template <class _ForwardIterator>
2702 _ForwardIterator
2703 __parse_class_escape(_ForwardIterator __first, _ForwardIterator __last,
2704 basic_string<_CharT>& __str,
2705 __bracket_expression<_CharT, _Traits>* __ml);
2706 template <class _ForwardIterator>
2707 _ForwardIterator
2708 __parse_awk_escape(_ForwardIterator __first, _ForwardIterator __last,
2709 basic_string<_CharT>* __str = nullptr);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002710
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00002711 void __push_l_anchor() {__left_anchor_ = true;}
2712 void __push_r_anchor();
Howard Hinnantac303862010-07-12 15:51:17 +00002713 void __push_match_any();
Howard Hinnant17615b02010-07-27 01:25:38 +00002714 void __push_match_any_but_newline();
Howard Hinnante77aa5e2010-07-08 17:43:58 +00002715 void __push_greedy_inf_repeat(size_t __min, __owns_one_state<_CharT>* __s,
2716 unsigned __mexp_begin = 0, unsigned __mexp_end = 0)
2717 {__push_loop(__min, numeric_limits<size_t>::max(), __s,
2718 __mexp_begin, __mexp_end);}
Howard Hinnant17615b02010-07-27 01:25:38 +00002719 void __push_nongreedy_inf_repeat(size_t __min, __owns_one_state<_CharT>* __s,
2720 unsigned __mexp_begin = 0, unsigned __mexp_end = 0)
2721 {__push_loop(__min, numeric_limits<size_t>::max(), __s,
2722 __mexp_begin, __mexp_end, false);}
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002723 void __push_loop(size_t __min, size_t __max, __owns_one_state<_CharT>* __s,
2724 size_t __mexp_begin = 0, size_t __mexp_end = 0,
2725 bool __greedy = true);
Howard Hinnant173968a2010-07-13 21:48:06 +00002726 __bracket_expression<_CharT, _Traits>* __start_matching_list(bool __negate);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002727 void __push_char(value_type __c);
Howard Hinnantcba352d2010-07-12 18:16:05 +00002728 void __push_back_ref(int __i);
Howard Hinnantaa698082010-07-16 19:08:36 +00002729 void __push_alternation(__owns_one_state<_CharT>* __sa,
2730 __owns_one_state<_CharT>* __sb);
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00002731 void __push_begin_marked_subexpression();
2732 void __push_end_marked_subexpression(unsigned);
Howard Hinnant2ade7c22010-07-22 17:53:24 +00002733 void __push_empty();
Howard Hinnant17615b02010-07-27 01:25:38 +00002734 void __push_word_boundary(bool);
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002735 void __push_lookahead(const basic_regex&, bool);
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00002736
Howard Hinnant22ce0b42010-07-14 21:14:52 +00002737 template <class _Allocator>
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00002738 bool
Howard Hinnant22ce0b42010-07-14 21:14:52 +00002739 __search(const _CharT* __first, const _CharT* __last,
2740 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00002741 regex_constants::match_flag_type __flags) const;
2742
Howard Hinnant22ce0b42010-07-14 21:14:52 +00002743 template <class _Allocator>
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002744 bool
Howard Hinnant22ce0b42010-07-14 21:14:52 +00002745 __match_at_start(const _CharT* __first, const _CharT* __last,
2746 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002747 regex_constants::match_flag_type __flags) const;
Howard Hinnant17615b02010-07-27 01:25:38 +00002748 template <class _Allocator>
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002749 bool
Howard Hinnant17615b02010-07-27 01:25:38 +00002750 __match_at_start_ecma(const _CharT* __first, const _CharT* __last,
2751 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002752 regex_constants::match_flag_type __flags) const;
Howard Hinnant22ce0b42010-07-14 21:14:52 +00002753 template <class _Allocator>
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002754 bool
2755 __match_at_start_posix_nosubs(const _CharT* __first, const _CharT* __last,
Howard Hinnant22ce0b42010-07-14 21:14:52 +00002756 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002757 regex_constants::match_flag_type __flags) const;
Howard Hinnant22ce0b42010-07-14 21:14:52 +00002758 template <class _Allocator>
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002759 bool
Howard Hinnant22ce0b42010-07-14 21:14:52 +00002760 __match_at_start_posix_subs(const _CharT* __first, const _CharT* __last,
2761 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002762 regex_constants::match_flag_type __flags) const;
2763
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00002764 template <class _B, class _A, class _C, class _T>
2765 friend
2766 bool
2767 regex_search(_B, _B, match_results<_B, _A>&, const basic_regex<_C, _T>&,
2768 regex_constants::match_flag_type);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002769
Howard Hinnant22ce0b42010-07-14 21:14:52 +00002770 template <class _A, class _C, class _T>
2771 friend
2772 bool
2773 regex_search(const _C*, const _C*, match_results<const _C*, _A>&,
2774 const basic_regex<_C, _T>&, regex_constants::match_flag_type);
2775
2776 template <class _B, class _C, class _T>
2777 friend
2778 bool
2779 regex_search(_B, _B, const basic_regex<_C, _T>&,
2780 regex_constants::match_flag_type);
2781
2782 template <class _C, class _T>
2783 friend
2784 bool
2785 regex_search(const _C*, const _C*,
2786 const basic_regex<_C, _T>&, regex_constants::match_flag_type);
2787
2788 template <class _C, class _A, class _T>
2789 friend
2790 bool
2791 regex_search(const _C*, match_results<const _C*, _A>&, const basic_regex<_C, _T>&,
2792 regex_constants::match_flag_type);
2793
2794 template <class _ST, class _SA, class _C, class _T>
2795 friend
2796 bool
2797 regex_search(const basic_string<_C, _ST, _SA>& __s,
2798 const basic_regex<_C, _T>& __e,
2799 regex_constants::match_flag_type __flags);
2800
2801 template <class _ST, class _SA, class _A, class _C, class _T>
2802 friend
2803 bool
2804 regex_search(const basic_string<_C, _ST, _SA>& __s,
2805 match_results<typename basic_string<_C, _ST, _SA>::const_iterator, _A>&,
2806 const basic_regex<_C, _T>& __e,
2807 regex_constants::match_flag_type __flags);
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002808
2809 template <class, class> friend class __lookahead;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002810};
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002811
2812template <class _CharT, class _Traits>
2813basic_regex<_CharT, _Traits>::~basic_regex()
2814{
2815}
2816
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002817// __lookahead
2818
2819template <class _CharT, class _Traits>
2820class __lookahead
2821 : public __owns_one_state<_CharT>
2822{
2823 typedef __owns_one_state<_CharT> base;
2824
2825 basic_regex<_CharT, _Traits> __exp_;
2826 bool __invert_;
2827
2828 __lookahead(const __lookahead&);
2829 __lookahead& operator=(const __lookahead&);
2830public:
2831 typedef _STD::__state<_CharT> __state;
2832
2833 __lookahead(const basic_regex<_CharT, _Traits>& __exp, bool __invert, __node<_CharT>* __s)
2834 : base(__s), __exp_(__exp), __invert_(__invert) {}
2835
2836 virtual void __exec(__state&) const;
2837
2838 virtual string speak() const
2839 {
2840 ostringstream os;
2841 if (__invert_)
2842 os << "not lookahead";
2843 else
2844 os << "lookahead";
2845 return os.str();
2846 }
2847};
2848
2849template <class _CharT, class _Traits>
2850void
2851__lookahead<_CharT, _Traits>::__exec(__state& __s) const
2852{
2853 match_results<const _CharT*> __m;
2854 __m.__init(1 + __exp_.mark_count(), __s.__current_, __s.__last_);
2855 bool __matched = __exp_.__match_at_start_ecma(__s.__current_, __s.__last_,
2856 __m, __s.__flags_);
2857 if (__matched != __invert_)
2858 {
2859 __s.__do_ = __state::__accept_but_not_consume;
2860 __s.__node_ = this->first();
2861 }
2862 else
2863 {
2864 __s.__do_ = __state::__reject;
2865 __s.__node_ = nullptr;
2866 }
2867}
2868
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002869template <class _CharT, class _Traits>
2870template <class _ForwardIterator>
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002871_ForwardIterator
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002872basic_regex<_CharT, _Traits>::__parse(_ForwardIterator __first,
2873 _ForwardIterator __last)
2874{
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002875 {
Howard Hinnantac303862010-07-12 15:51:17 +00002876 unique_ptr<__node> __h(new __end_state<_CharT>);
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002877 __start_.reset(new __empty_state<_CharT>(__h.get()));
2878 __h.release();
2879 __end_ = __start_.get();
2880 }
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00002881 switch (__flags_ & 0x1F0)
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002882 {
2883 case ECMAScript:
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002884 __first = __parse_ecma_exp(__first, __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002885 break;
2886 case basic:
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002887 __first = __parse_basic_reg_exp(__first, __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002888 break;
2889 case extended:
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002890 case awk:
Howard Hinnant15476f32010-07-28 17:35:27 +00002891 __first = __parse_extended_reg_exp(__first, __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002892 break;
2893 case grep:
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002894 __first = __parse_grep(__first, __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002895 break;
2896 case egrep:
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002897 __first = __parse_egrep(__first, __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002898 break;
Howard Hinnantd4444702010-08-11 17:04:31 +00002899#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002900 default:
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00002901 throw regex_error(regex_constants::__re_err_grammar);
Howard Hinnantd4444702010-08-11 17:04:31 +00002902#endif
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002903 }
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002904 return __first;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002905}
2906
2907template <class _CharT, class _Traits>
2908template <class _ForwardIterator>
2909_ForwardIterator
2910basic_regex<_CharT, _Traits>::__parse_basic_reg_exp(_ForwardIterator __first,
2911 _ForwardIterator __last)
2912{
2913 if (__first != __last)
2914 {
2915 if (*__first == '^')
2916 {
2917 __push_l_anchor();
2918 ++__first;
2919 }
2920 if (__first != __last)
2921 {
2922 __first = __parse_RE_expression(__first, __last);
2923 if (__first != __last)
2924 {
2925 _ForwardIterator __temp = next(__first);
2926 if (__temp == __last && *__first == '$')
2927 {
2928 __push_r_anchor();
2929 ++__first;
2930 }
2931 }
2932 }
Howard Hinnantd4444702010-08-11 17:04:31 +00002933#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002934 if (__first != __last)
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00002935 throw regex_error(regex_constants::__re_err_empty);
Howard Hinnantd4444702010-08-11 17:04:31 +00002936#endif
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002937 }
2938 return __first;
2939}
2940
2941template <class _CharT, class _Traits>
2942template <class _ForwardIterator>
2943_ForwardIterator
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002944basic_regex<_CharT, _Traits>::__parse_extended_reg_exp(_ForwardIterator __first,
2945 _ForwardIterator __last)
2946{
Howard Hinnantaa698082010-07-16 19:08:36 +00002947 __owns_one_state<_CharT>* __sa = __end_;
2948 _ForwardIterator __temp = __parse_ERE_branch(__first, __last);
Howard Hinnantd4444702010-08-11 17:04:31 +00002949#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantaa698082010-07-16 19:08:36 +00002950 if (__temp == __first)
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00002951 throw regex_error(regex_constants::__re_err_empty);
Howard Hinnantd4444702010-08-11 17:04:31 +00002952#endif
Howard Hinnantaa698082010-07-16 19:08:36 +00002953 __first = __temp;
2954 while (__first != __last && *__first == '|')
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002955 {
Howard Hinnantaa698082010-07-16 19:08:36 +00002956 __owns_one_state<_CharT>* __sb = __end_;
2957 __temp = __parse_ERE_branch(++__first, __last);
Howard Hinnantd4444702010-08-11 17:04:31 +00002958#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002959 if (__temp == __first)
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00002960 throw regex_error(regex_constants::__re_err_empty);
Howard Hinnantd4444702010-08-11 17:04:31 +00002961#endif
Howard Hinnantaa698082010-07-16 19:08:36 +00002962 __push_alternation(__sa, __sb);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002963 __first = __temp;
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002964 }
2965 return __first;
2966}
2967
2968template <class _CharT, class _Traits>
2969template <class _ForwardIterator>
2970_ForwardIterator
2971basic_regex<_CharT, _Traits>::__parse_ERE_branch(_ForwardIterator __first,
2972 _ForwardIterator __last)
2973{
2974 _ForwardIterator __temp = __parse_ERE_expression(__first, __last);
Howard Hinnantd4444702010-08-11 17:04:31 +00002975#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002976 if (__temp == __first)
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00002977 throw regex_error(regex_constants::__re_err_empty);
Howard Hinnantd4444702010-08-11 17:04:31 +00002978#endif
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002979 do
2980 {
2981 __first = __temp;
2982 __temp = __parse_ERE_expression(__first, __last);
2983 } while (__temp != __first);
2984 return __first;
2985}
2986
2987template <class _CharT, class _Traits>
2988template <class _ForwardIterator>
2989_ForwardIterator
2990basic_regex<_CharT, _Traits>::__parse_ERE_expression(_ForwardIterator __first,
2991 _ForwardIterator __last)
2992{
Howard Hinnantaa698082010-07-16 19:08:36 +00002993 __owns_one_state<_CharT>* __e = __end_;
2994 unsigned __mexp_begin = __marked_count_;
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002995 _ForwardIterator __temp = __parse_one_char_or_coll_elem_ERE(__first, __last);
2996 if (__temp == __first && __temp != __last)
2997 {
2998 switch (*__temp)
2999 {
3000 case '^':
3001 __push_l_anchor();
3002 ++__temp;
3003 break;
3004 case '$':
3005 __push_r_anchor();
3006 ++__temp;
3007 break;
3008 case '(':
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00003009 __push_begin_marked_subexpression();
3010 unsigned __temp_count = __marked_count_;
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003011 ++__open_count_;
3012 __temp = __parse_extended_reg_exp(++__temp, __last);
Howard Hinnantd4444702010-08-11 17:04:31 +00003013#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003014 if (__temp == __last || *__temp != ')')
3015 throw regex_error(regex_constants::error_paren);
Howard Hinnantd4444702010-08-11 17:04:31 +00003016#endif
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00003017 __push_end_marked_subexpression(__temp_count);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003018 --__open_count_;
3019 ++__temp;
3020 break;
3021 }
3022 }
3023 if (__temp != __first)
Howard Hinnantaa698082010-07-16 19:08:36 +00003024 __temp = __parse_ERE_dupl_symbol(__temp, __last, __e, __mexp_begin+1,
3025 __marked_count_+1);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003026 __first = __temp;
3027 return __first;
3028}
3029
3030template <class _CharT, class _Traits>
3031template <class _ForwardIterator>
3032_ForwardIterator
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003033basic_regex<_CharT, _Traits>::__parse_RE_expression(_ForwardIterator __first,
3034 _ForwardIterator __last)
3035{
3036 while (true)
3037 {
3038 _ForwardIterator __temp = __parse_simple_RE(__first, __last);
3039 if (__temp == __first)
3040 break;
3041 __first = __temp;
3042 }
3043 return __first;
3044}
3045
3046template <class _CharT, class _Traits>
3047template <class _ForwardIterator>
3048_ForwardIterator
3049basic_regex<_CharT, _Traits>::__parse_simple_RE(_ForwardIterator __first,
3050 _ForwardIterator __last)
3051{
3052 if (__first != __last)
3053 {
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003054 __owns_one_state<_CharT>* __e = __end_;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003055 unsigned __mexp_begin = __marked_count_;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003056 _ForwardIterator __temp = __parse_nondupl_RE(__first, __last);
3057 if (__temp != __first)
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003058 __first = __parse_RE_dupl_symbol(__temp, __last, __e,
3059 __mexp_begin+1, __marked_count_+1);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003060 }
3061 return __first;
3062}
3063
3064template <class _CharT, class _Traits>
3065template <class _ForwardIterator>
3066_ForwardIterator
3067basic_regex<_CharT, _Traits>::__parse_nondupl_RE(_ForwardIterator __first,
3068 _ForwardIterator __last)
3069{
3070 _ForwardIterator __temp = __first;
3071 __first = __parse_one_char_or_coll_elem_RE(__first, __last);
3072 if (__temp == __first)
3073 {
3074 __temp = __parse_Back_open_paren(__first, __last);
3075 if (__temp != __first)
3076 {
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00003077 __push_begin_marked_subexpression();
3078 unsigned __temp_count = __marked_count_;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003079 __first = __parse_RE_expression(__temp, __last);
3080 __temp = __parse_Back_close_paren(__first, __last);
Howard Hinnantd4444702010-08-11 17:04:31 +00003081#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003082 if (__temp == __first)
3083 throw regex_error(regex_constants::error_paren);
Howard Hinnantd4444702010-08-11 17:04:31 +00003084#endif
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00003085 __push_end_marked_subexpression(__temp_count);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003086 __first = __temp;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003087 }
3088 else
3089 __first = __parse_BACKREF(__first, __last);
3090 }
3091 return __first;
3092}
3093
3094template <class _CharT, class _Traits>
3095template <class _ForwardIterator>
3096_ForwardIterator
3097basic_regex<_CharT, _Traits>::__parse_one_char_or_coll_elem_RE(
3098 _ForwardIterator __first,
3099 _ForwardIterator __last)
3100{
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003101 _ForwardIterator __temp = __parse_ORD_CHAR(__first, __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003102 if (__temp == __first)
3103 {
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003104 __temp = __parse_QUOTED_CHAR(__first, __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003105 if (__temp == __first)
3106 {
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003107 if (__temp != __last && *__temp == '.')
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003108 {
3109 __push_match_any();
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003110 ++__temp;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003111 }
3112 else
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003113 __temp = __parse_bracket_expression(__first, __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003114 }
3115 }
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003116 __first = __temp;
3117 return __first;
3118}
3119
3120template <class _CharT, class _Traits>
3121template <class _ForwardIterator>
3122_ForwardIterator
3123basic_regex<_CharT, _Traits>::__parse_one_char_or_coll_elem_ERE(
3124 _ForwardIterator __first,
3125 _ForwardIterator __last)
3126{
3127 _ForwardIterator __temp = __parse_ORD_CHAR_ERE(__first, __last);
3128 if (__temp == __first)
3129 {
3130 __temp = __parse_QUOTED_CHAR_ERE(__first, __last);
3131 if (__temp == __first)
3132 {
3133 if (__temp != __last && *__temp == '.')
3134 {
3135 __push_match_any();
3136 ++__temp;
3137 }
3138 else
3139 __temp = __parse_bracket_expression(__first, __last);
3140 }
3141 }
3142 __first = __temp;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003143 return __first;
3144}
3145
3146template <class _CharT, class _Traits>
3147template <class _ForwardIterator>
3148_ForwardIterator
3149basic_regex<_CharT, _Traits>::__parse_Back_open_paren(_ForwardIterator __first,
3150 _ForwardIterator __last)
3151{
3152 if (__first != __last)
3153 {
3154 _ForwardIterator __temp = next(__first);
3155 if (__temp != __last)
3156 {
3157 if (*__first == '\\' && *__temp == '(')
3158 __first = ++__temp;
3159 }
3160 }
3161 return __first;
3162}
3163
3164template <class _CharT, class _Traits>
3165template <class _ForwardIterator>
3166_ForwardIterator
3167basic_regex<_CharT, _Traits>::__parse_Back_close_paren(_ForwardIterator __first,
3168 _ForwardIterator __last)
3169{
3170 if (__first != __last)
3171 {
3172 _ForwardIterator __temp = next(__first);
3173 if (__temp != __last)
3174 {
3175 if (*__first == '\\' && *__temp == ')')
3176 __first = ++__temp;
3177 }
3178 }
3179 return __first;
3180}
3181
3182template <class _CharT, class _Traits>
3183template <class _ForwardIterator>
3184_ForwardIterator
3185basic_regex<_CharT, _Traits>::__parse_Back_open_brace(_ForwardIterator __first,
3186 _ForwardIterator __last)
3187{
3188 if (__first != __last)
3189 {
3190 _ForwardIterator __temp = next(__first);
3191 if (__temp != __last)
3192 {
3193 if (*__first == '\\' && *__temp == '{')
3194 __first = ++__temp;
3195 }
3196 }
3197 return __first;
3198}
3199
3200template <class _CharT, class _Traits>
3201template <class _ForwardIterator>
3202_ForwardIterator
3203basic_regex<_CharT, _Traits>::__parse_Back_close_brace(_ForwardIterator __first,
3204 _ForwardIterator __last)
3205{
3206 if (__first != __last)
3207 {
3208 _ForwardIterator __temp = next(__first);
3209 if (__temp != __last)
3210 {
3211 if (*__first == '\\' && *__temp == '}')
3212 __first = ++__temp;
3213 }
3214 }
3215 return __first;
3216}
3217
3218template <class _CharT, class _Traits>
3219template <class _ForwardIterator>
3220_ForwardIterator
3221basic_regex<_CharT, _Traits>::__parse_BACKREF(_ForwardIterator __first,
3222 _ForwardIterator __last)
3223{
3224 if (__first != __last)
3225 {
3226 _ForwardIterator __temp = next(__first);
3227 if (__temp != __last)
3228 {
3229 if (*__first == '\\' && '1' <= *__temp && *__temp <= '9')
3230 {
3231 __push_back_ref(*__temp - '0');
3232 __first = ++__temp;
3233 }
3234 }
3235 }
3236 return __first;
3237}
3238
3239template <class _CharT, class _Traits>
3240template <class _ForwardIterator>
3241_ForwardIterator
3242basic_regex<_CharT, _Traits>::__parse_ORD_CHAR(_ForwardIterator __first,
3243 _ForwardIterator __last)
3244{
3245 if (__first != __last)
3246 {
3247 _ForwardIterator __temp = next(__first);
3248 if (__temp == __last && *__first == '$')
3249 return __first;
3250 // Not called inside a bracket
3251 if (*__first == '.' || *__first == '\\' || *__first == '[')
3252 return __first;
Howard Hinnant0de86b62010-06-25 20:56:08 +00003253 __push_char(*__first);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003254 ++__first;
3255 }
3256 return __first;
3257}
3258
3259template <class _CharT, class _Traits>
3260template <class _ForwardIterator>
3261_ForwardIterator
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003262basic_regex<_CharT, _Traits>::__parse_ORD_CHAR_ERE(_ForwardIterator __first,
3263 _ForwardIterator __last)
3264{
3265 if (__first != __last)
3266 {
3267 switch (*__first)
3268 {
3269 case '^':
3270 case '.':
3271 case '[':
3272 case '$':
3273 case '(':
3274 case '|':
3275 case '*':
3276 case '+':
3277 case '?':
3278 case '{':
3279 case '\\':
3280 break;
3281 case ')':
3282 if (__open_count_ == 0)
3283 {
3284 __push_char(*__first);
3285 ++__first;
3286 }
3287 break;
3288 default:
3289 __push_char(*__first);
3290 ++__first;
3291 break;
3292 }
3293 }
3294 return __first;
3295}
3296
3297template <class _CharT, class _Traits>
3298template <class _ForwardIterator>
3299_ForwardIterator
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003300basic_regex<_CharT, _Traits>::__parse_QUOTED_CHAR(_ForwardIterator __first,
3301 _ForwardIterator __last)
3302{
3303 if (__first != __last)
3304 {
3305 _ForwardIterator __temp = next(__first);
3306 if (__temp != __last)
3307 {
3308 if (*__first == '\\')
3309 {
3310 switch (*__temp)
3311 {
3312 case '^':
3313 case '.':
3314 case '*':
3315 case '[':
3316 case '$':
3317 case '\\':
Howard Hinnant0de86b62010-06-25 20:56:08 +00003318 __push_char(*__temp);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003319 __first = ++__temp;
3320 break;
3321 }
3322 }
3323 }
3324 }
3325 return __first;
3326}
3327
3328template <class _CharT, class _Traits>
3329template <class _ForwardIterator>
3330_ForwardIterator
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003331basic_regex<_CharT, _Traits>::__parse_QUOTED_CHAR_ERE(_ForwardIterator __first,
3332 _ForwardIterator __last)
3333{
3334 if (__first != __last)
3335 {
3336 _ForwardIterator __temp = next(__first);
3337 if (__temp != __last)
3338 {
3339 if (*__first == '\\')
3340 {
3341 switch (*__temp)
3342 {
3343 case '^':
3344 case '.':
3345 case '*':
3346 case '[':
3347 case '$':
3348 case '\\':
3349 case '(':
3350 case ')':
3351 case '|':
3352 case '+':
3353 case '?':
3354 case '{':
3355 __push_char(*__temp);
3356 __first = ++__temp;
3357 break;
Howard Hinnant15476f32010-07-28 17:35:27 +00003358 default:
3359 if ((__flags_ & 0x1F0) == awk)
3360 __first = __parse_awk_escape(++__first, __last);
3361 break;
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003362 }
3363 }
3364 }
3365 }
3366 return __first;
3367}
3368
3369template <class _CharT, class _Traits>
3370template <class _ForwardIterator>
3371_ForwardIterator
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003372basic_regex<_CharT, _Traits>::__parse_RE_dupl_symbol(_ForwardIterator __first,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003373 _ForwardIterator __last,
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003374 __owns_one_state<_CharT>* __s,
3375 unsigned __mexp_begin,
3376 unsigned __mexp_end)
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003377{
3378 if (__first != __last)
3379 {
Howard Hinnant0de86b62010-06-25 20:56:08 +00003380 if (*__first == '*')
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003381 {
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003382 __push_greedy_inf_repeat(0, __s, __mexp_begin, __mexp_end);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003383 ++__first;
3384 }
3385 else
3386 {
3387 _ForwardIterator __temp = __parse_Back_open_brace(__first, __last);
3388 if (__temp != __first)
3389 {
3390 int __min = 0;
3391 __first = __temp;
3392 __temp = __parse_DUP_COUNT(__first, __last, __min);
Howard Hinnantd4444702010-08-11 17:04:31 +00003393#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003394 if (__temp == __first)
3395 throw regex_error(regex_constants::error_badbrace);
Howard Hinnantd4444702010-08-11 17:04:31 +00003396#endif
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003397 __first = __temp;
Howard Hinnantd4444702010-08-11 17:04:31 +00003398#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003399 if (__first == __last)
3400 throw regex_error(regex_constants::error_brace);
Howard Hinnantd4444702010-08-11 17:04:31 +00003401#endif
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003402 if (*__first != ',')
3403 {
3404 __temp = __parse_Back_close_brace(__first, __last);
Howard Hinnantd4444702010-08-11 17:04:31 +00003405#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003406 if (__temp == __first)
3407 throw regex_error(regex_constants::error_brace);
Howard Hinnantd4444702010-08-11 17:04:31 +00003408#endif
Howard Hinnantcba352d2010-07-12 18:16:05 +00003409 __push_loop(__min, __min, __s, __mexp_begin, __mexp_end,
3410 true);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003411 __first = __temp;
3412 }
3413 else
3414 {
3415 ++__first; // consume ','
3416 int __max = -1;
3417 __first = __parse_DUP_COUNT(__first, __last, __max);
3418 __temp = __parse_Back_close_brace(__first, __last);
Howard Hinnantd4444702010-08-11 17:04:31 +00003419#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003420 if (__temp == __first)
3421 throw regex_error(regex_constants::error_brace);
Howard Hinnantd4444702010-08-11 17:04:31 +00003422#endif
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003423 if (__max == -1)
Howard Hinnantaa698082010-07-16 19:08:36 +00003424 __push_greedy_inf_repeat(__min, __s, __mexp_begin, __mexp_end);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003425 else
3426 {
Howard Hinnantd4444702010-08-11 17:04:31 +00003427#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003428 if (__max < __min)
3429 throw regex_error(regex_constants::error_badbrace);
Howard Hinnantd4444702010-08-11 17:04:31 +00003430#endif
Howard Hinnantcba352d2010-07-12 18:16:05 +00003431 __push_loop(__min, __max, __s, __mexp_begin, __mexp_end,
3432 true);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003433 }
3434 __first = __temp;
3435 }
3436 }
3437 }
3438 }
3439 return __first;
3440}
3441
Howard Hinnant0de86b62010-06-25 20:56:08 +00003442template <class _CharT, class _Traits>
3443template <class _ForwardIterator>
3444_ForwardIterator
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003445basic_regex<_CharT, _Traits>::__parse_ERE_dupl_symbol(_ForwardIterator __first,
Howard Hinnantaa698082010-07-16 19:08:36 +00003446 _ForwardIterator __last,
3447 __owns_one_state<_CharT>* __s,
3448 unsigned __mexp_begin,
3449 unsigned __mexp_end)
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003450{
3451 if (__first != __last)
3452 {
Howard Hinnanta0d045b2010-07-29 00:36:00 +00003453 unsigned __grammar = __flags_ & 0x1F0;
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003454 switch (*__first)
3455 {
3456 case '*':
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003457 ++__first;
Howard Hinnanta0d045b2010-07-29 00:36:00 +00003458 if (__grammar == ECMAScript && __first != __last && *__first == '?')
Howard Hinnant17615b02010-07-27 01:25:38 +00003459 {
3460 ++__first;
3461 __push_nongreedy_inf_repeat(0, __s, __mexp_begin, __mexp_end);
3462 }
3463 else
3464 __push_greedy_inf_repeat(0, __s, __mexp_begin, __mexp_end);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003465 break;
3466 case '+':
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003467 ++__first;
Howard Hinnanta0d045b2010-07-29 00:36:00 +00003468 if (__grammar == ECMAScript && __first != __last && *__first == '?')
Howard Hinnant17615b02010-07-27 01:25:38 +00003469 {
3470 ++__first;
3471 __push_nongreedy_inf_repeat(1, __s, __mexp_begin, __mexp_end);
3472 }
3473 else
3474 __push_greedy_inf_repeat(1, __s, __mexp_begin, __mexp_end);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003475 break;
3476 case '?':
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003477 ++__first;
Howard Hinnanta0d045b2010-07-29 00:36:00 +00003478 if (__grammar == ECMAScript && __first != __last && *__first == '?')
Howard Hinnant17615b02010-07-27 01:25:38 +00003479 {
3480 ++__first;
3481 __push_loop(0, 1, __s, __mexp_begin, __mexp_end, false);
3482 }
3483 else
3484 __push_loop(0, 1, __s, __mexp_begin, __mexp_end);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003485 break;
3486 case '{':
3487 {
3488 int __min;
Howard Hinnantaa698082010-07-16 19:08:36 +00003489 _ForwardIterator __temp = __parse_DUP_COUNT(++__first, __last, __min);
Howard Hinnantd4444702010-08-11 17:04:31 +00003490#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003491 if (__temp == __first)
3492 throw regex_error(regex_constants::error_badbrace);
Howard Hinnantd4444702010-08-11 17:04:31 +00003493#endif
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003494 __first = __temp;
Howard Hinnantd4444702010-08-11 17:04:31 +00003495#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003496 if (__first == __last)
3497 throw regex_error(regex_constants::error_brace);
Howard Hinnantd4444702010-08-11 17:04:31 +00003498#endif
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003499 switch (*__first)
3500 {
3501 case '}':
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003502 ++__first;
Howard Hinnanta0d045b2010-07-29 00:36:00 +00003503 if (__grammar == ECMAScript && __first != __last && *__first == '?')
Howard Hinnant17615b02010-07-27 01:25:38 +00003504 {
3505 ++__first;
3506 __push_loop(__min, __min, __s, __mexp_begin, __mexp_end, false);
3507 }
3508 else
3509 __push_loop(__min, __min, __s, __mexp_begin, __mexp_end);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003510 break;
3511 case ',':
Howard Hinnantd4444702010-08-11 17:04:31 +00003512 ++__first;
3513#ifndef _LIBCPP_NO_EXCEPTIONS
3514 if (__first == __last)
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003515 throw regex_error(regex_constants::error_badbrace);
Howard Hinnantd4444702010-08-11 17:04:31 +00003516#endif
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003517 if (*__first == '}')
3518 {
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003519 ++__first;
Howard Hinnanta0d045b2010-07-29 00:36:00 +00003520 if (__grammar == ECMAScript && __first != __last && *__first == '?')
Howard Hinnant17615b02010-07-27 01:25:38 +00003521 {
3522 ++__first;
3523 __push_nongreedy_inf_repeat(__min, __s, __mexp_begin, __mexp_end);
3524 }
3525 else
3526 __push_greedy_inf_repeat(__min, __s, __mexp_begin, __mexp_end);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003527 }
3528 else
3529 {
Howard Hinnantaa698082010-07-16 19:08:36 +00003530 int __max = -1;
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003531 __temp = __parse_DUP_COUNT(__first, __last, __max);
Howard Hinnantd4444702010-08-11 17:04:31 +00003532#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003533 if (__temp == __first)
3534 throw regex_error(regex_constants::error_brace);
Howard Hinnantd4444702010-08-11 17:04:31 +00003535#endif
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003536 __first = __temp;
Howard Hinnantd4444702010-08-11 17:04:31 +00003537#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003538 if (__first == __last || *__first != '}')
3539 throw regex_error(regex_constants::error_brace);
Howard Hinnantd4444702010-08-11 17:04:31 +00003540#endif
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003541 ++__first;
Howard Hinnantd4444702010-08-11 17:04:31 +00003542#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003543 if (__max < __min)
3544 throw regex_error(regex_constants::error_badbrace);
Howard Hinnantd4444702010-08-11 17:04:31 +00003545#endif
Howard Hinnanta0d045b2010-07-29 00:36:00 +00003546 if (__grammar == ECMAScript && __first != __last && *__first == '?')
Howard Hinnant17615b02010-07-27 01:25:38 +00003547 {
3548 ++__first;
3549 __push_loop(__min, __max, __s, __mexp_begin, __mexp_end, false);
3550 }
3551 else
3552 __push_loop(__min, __max, __s, __mexp_begin, __mexp_end);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003553 }
Howard Hinnantaa698082010-07-16 19:08:36 +00003554 break;
Howard Hinnantd4444702010-08-11 17:04:31 +00003555#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003556 default:
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 }
3560 }
3561 break;
3562 }
3563 }
3564 return __first;
3565}
3566
3567template <class _CharT, class _Traits>
3568template <class _ForwardIterator>
3569_ForwardIterator
Howard Hinnant0de86b62010-06-25 20:56:08 +00003570basic_regex<_CharT, _Traits>::__parse_bracket_expression(_ForwardIterator __first,
3571 _ForwardIterator __last)
3572{
3573 if (__first != __last && *__first == '[')
3574 {
Howard Hinnantd4444702010-08-11 17:04:31 +00003575 ++__first;
3576#ifndef _LIBCPP_NO_EXCEPTIONS
3577 if (__first == __last)
Howard Hinnant0de86b62010-06-25 20:56:08 +00003578 throw regex_error(regex_constants::error_brack);
Howard Hinnantd4444702010-08-11 17:04:31 +00003579#endif
Howard Hinnant173968a2010-07-13 21:48:06 +00003580 bool __negate = false;
Howard Hinnant0de86b62010-06-25 20:56:08 +00003581 if (*__first == '^')
3582 {
3583 ++__first;
Howard Hinnant173968a2010-07-13 21:48:06 +00003584 __negate = true;
Howard Hinnant0de86b62010-06-25 20:56:08 +00003585 }
Howard Hinnant173968a2010-07-13 21:48:06 +00003586 __bracket_expression<_CharT, _Traits>* __ml = __start_matching_list(__negate);
3587 // __ml owned by *this
Howard Hinnantd4444702010-08-11 17:04:31 +00003588#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0de86b62010-06-25 20:56:08 +00003589 if (__first == __last)
3590 throw regex_error(regex_constants::error_brack);
Howard Hinnantd4444702010-08-11 17:04:31 +00003591#endif
Howard Hinnant15476f32010-07-28 17:35:27 +00003592 if ((__flags_ & 0x1F0) != ECMAScript && *__first == ']')
Howard Hinnant0de86b62010-06-25 20:56:08 +00003593 {
Howard Hinnant173968a2010-07-13 21:48:06 +00003594 __ml->__add_char(']');
Howard Hinnant0de86b62010-06-25 20:56:08 +00003595 ++__first;
3596 }
Howard Hinnant173968a2010-07-13 21:48:06 +00003597 __first = __parse_follow_list(__first, __last, __ml);
Howard Hinnantd4444702010-08-11 17:04:31 +00003598#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0de86b62010-06-25 20:56:08 +00003599 if (__first == __last)
3600 throw regex_error(regex_constants::error_brack);
Howard Hinnantd4444702010-08-11 17:04:31 +00003601#endif
Howard Hinnant0de86b62010-06-25 20:56:08 +00003602 if (*__first == '-')
3603 {
Howard Hinnant173968a2010-07-13 21:48:06 +00003604 __ml->__add_char('-');
Howard Hinnant0de86b62010-06-25 20:56:08 +00003605 ++__first;
3606 }
Howard Hinnantd4444702010-08-11 17:04:31 +00003607#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0de86b62010-06-25 20:56:08 +00003608 if (__first == __last || *__first != ']')
3609 throw regex_error(regex_constants::error_brack);
Howard Hinnantd4444702010-08-11 17:04:31 +00003610#endif
Howard Hinnant0de86b62010-06-25 20:56:08 +00003611 ++__first;
3612 }
3613 return __first;
3614}
3615
3616template <class _CharT, class _Traits>
3617template <class _ForwardIterator>
3618_ForwardIterator
3619basic_regex<_CharT, _Traits>::__parse_follow_list(_ForwardIterator __first,
Howard Hinnant173968a2010-07-13 21:48:06 +00003620 _ForwardIterator __last,
3621 __bracket_expression<_CharT, _Traits>* __ml)
Howard Hinnant0de86b62010-06-25 20:56:08 +00003622{
3623 if (__first != __last)
3624 {
3625 while (true)
3626 {
Howard Hinnant173968a2010-07-13 21:48:06 +00003627 _ForwardIterator __temp = __parse_expression_term(__first, __last,
3628 __ml);
Howard Hinnant0de86b62010-06-25 20:56:08 +00003629 if (__temp == __first)
3630 break;
3631 __first = __temp;
3632 }
3633 }
3634 return __first;
3635}
3636
3637template <class _CharT, class _Traits>
3638template <class _ForwardIterator>
3639_ForwardIterator
3640basic_regex<_CharT, _Traits>::__parse_expression_term(_ForwardIterator __first,
Howard Hinnant173968a2010-07-13 21:48:06 +00003641 _ForwardIterator __last,
3642 __bracket_expression<_CharT, _Traits>* __ml)
Howard Hinnant0de86b62010-06-25 20:56:08 +00003643{
3644 if (__first != __last && *__first != ']')
3645 {
Howard Hinnant0de86b62010-06-25 20:56:08 +00003646 _ForwardIterator __temp = next(__first);
Howard Hinnant173968a2010-07-13 21:48:06 +00003647 basic_string<_CharT> __start_range;
Howard Hinnant0de86b62010-06-25 20:56:08 +00003648 if (__temp != __last && *__first == '[')
3649 {
3650 if (*__temp == '=')
Howard Hinnant173968a2010-07-13 21:48:06 +00003651 return __parse_equivalence_class(++__temp, __last, __ml);
Howard Hinnant0de86b62010-06-25 20:56:08 +00003652 else if (*__temp == ':')
Howard Hinnant173968a2010-07-13 21:48:06 +00003653 return __parse_character_class(++__temp, __last, __ml);
Howard Hinnant0de86b62010-06-25 20:56:08 +00003654 else if (*__temp == '.')
Howard Hinnant173968a2010-07-13 21:48:06 +00003655 __first = __parse_collating_symbol(++__temp, __last, __start_range);
Howard Hinnant0de86b62010-06-25 20:56:08 +00003656 }
Howard Hinnant15476f32010-07-28 17:35:27 +00003657 unsigned __grammar = __flags_ & 0x1F0;
3658 if (__start_range.empty())
Howard Hinnant0de86b62010-06-25 20:56:08 +00003659 {
Howard Hinnant15476f32010-07-28 17:35:27 +00003660 if ((__grammar == ECMAScript || __grammar == awk) && *__first == '\\')
3661 {
3662 if (__grammar == ECMAScript)
3663 __first = __parse_class_escape(++__first, __last, __start_range, __ml);
3664 else
3665 __first = __parse_awk_escape(++__first, __last, &__start_range);
3666 }
3667 else
3668 {
3669 __start_range = *__first;
3670 ++__first;
3671 }
Howard Hinnant0de86b62010-06-25 20:56:08 +00003672 }
3673 if (__first != __last && *__first != ']')
3674 {
3675 __temp = next(__first);
3676 if (__temp != __last && *__first == '-' && *__temp != ']')
3677 {
3678 // parse a range
Howard Hinnant173968a2010-07-13 21:48:06 +00003679 basic_string<_CharT> __end_range;
Howard Hinnant0de86b62010-06-25 20:56:08 +00003680 __first = __temp;
3681 ++__temp;
3682 if (__temp != __last && *__first == '[' && *__temp == '.')
Howard Hinnant173968a2010-07-13 21:48:06 +00003683 __first = __parse_collating_symbol(++__temp, __last, __end_range);
Howard Hinnant0de86b62010-06-25 20:56:08 +00003684 else
3685 {
Howard Hinnant15476f32010-07-28 17:35:27 +00003686 if ((__grammar == ECMAScript || __grammar == awk) && *__first == '\\')
3687 {
3688 if (__grammar == ECMAScript)
3689 __first = __parse_class_escape(++__first, __last,
3690 __end_range, __ml);
3691 else
3692 __first = __parse_awk_escape(++__first, __last,
3693 &__end_range);
3694 }
3695 else
3696 {
3697 __end_range = *__first;
3698 ++__first;
3699 }
Howard Hinnant0de86b62010-06-25 20:56:08 +00003700 }
Howard Hinnant173968a2010-07-13 21:48:06 +00003701 __ml->__add_range(_STD::move(__start_range), _STD::move(__end_range));
Howard Hinnant0de86b62010-06-25 20:56:08 +00003702 }
Howard Hinnant173968a2010-07-13 21:48:06 +00003703 else
3704 {
3705 if (__start_range.size() == 1)
3706 __ml->__add_char(__start_range[0]);
3707 else
3708 __ml->__add_digraph(__start_range[0], __start_range[1]);
3709 }
3710 }
3711 else
3712 {
3713 if (__start_range.size() == 1)
3714 __ml->__add_char(__start_range[0]);
3715 else
3716 __ml->__add_digraph(__start_range[0], __start_range[1]);
Howard Hinnant0de86b62010-06-25 20:56:08 +00003717 }
3718 }
3719 return __first;
3720}
3721
3722template <class _CharT, class _Traits>
3723template <class _ForwardIterator>
3724_ForwardIterator
Howard Hinnant15476f32010-07-28 17:35:27 +00003725basic_regex<_CharT, _Traits>::__parse_class_escape(_ForwardIterator __first,
3726 _ForwardIterator __last,
3727 basic_string<_CharT>& __str,
3728 __bracket_expression<_CharT, _Traits>* __ml)
3729{
Howard Hinnantd4444702010-08-11 17:04:31 +00003730#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant15476f32010-07-28 17:35:27 +00003731 if (__first == __last)
3732 throw regex_error(regex_constants::error_escape);
Howard Hinnantd4444702010-08-11 17:04:31 +00003733#endif
Howard Hinnant15476f32010-07-28 17:35:27 +00003734 switch (*__first)
3735 {
3736 case 0:
3737 __str = *__first;
3738 return ++__first;
3739 case 'b':
3740 __str = _CharT(8);
3741 return ++__first;
3742 case 'd':
3743 __ml->__add_class(ctype_base::digit);
3744 return ++__first;
3745 case 'D':
3746 __ml->__add_neg_class(ctype_base::digit);
3747 return ++__first;
3748 case 's':
3749 __ml->__add_class(ctype_base::space);
3750 return ++__first;
3751 case 'S':
3752 __ml->__add_neg_class(ctype_base::space);
3753 return ++__first;
3754 case 'w':
3755 __ml->__add_class(ctype_base::alnum);
3756 __ml->__add_char('_');
3757 return ++__first;
3758 case 'W':
3759 __ml->__add_neg_class(ctype_base::alnum);
3760 __ml->__add_neg_char('_');
3761 return ++__first;
3762 }
3763 __first = __parse_character_escape(__first, __last, &__str);
3764 return __first;
3765}
3766
3767template <class _CharT, class _Traits>
3768template <class _ForwardIterator>
3769_ForwardIterator
3770basic_regex<_CharT, _Traits>::__parse_awk_escape(_ForwardIterator __first,
3771 _ForwardIterator __last,
3772 basic_string<_CharT>* __str)
3773{
Howard Hinnantd4444702010-08-11 17:04:31 +00003774#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant15476f32010-07-28 17:35:27 +00003775 if (__first == __last)
3776 throw regex_error(regex_constants::error_escape);
Howard Hinnantd4444702010-08-11 17:04:31 +00003777#endif
Howard Hinnant15476f32010-07-28 17:35:27 +00003778 switch (*__first)
3779 {
3780 case '\\':
3781 case '"':
3782 case '/':
3783 if (__str)
3784 *__str = *__first;
3785 else
3786 __push_char(*__first);
3787 return ++__first;
3788 case 'a':
3789 if (__str)
3790 *__str = _CharT(7);
3791 else
3792 __push_char(_CharT(7));
3793 return ++__first;
3794 case 'b':
3795 if (__str)
3796 *__str = _CharT(8);
3797 else
3798 __push_char(_CharT(8));
3799 return ++__first;
3800 case 'f':
3801 if (__str)
3802 *__str = _CharT(0xC);
3803 else
3804 __push_char(_CharT(0xC));
3805 return ++__first;
3806 case 'n':
3807 if (__str)
3808 *__str = _CharT(0xA);
3809 else
3810 __push_char(_CharT(0xA));
3811 return ++__first;
3812 case 'r':
3813 if (__str)
3814 *__str = _CharT(0xD);
3815 else
3816 __push_char(_CharT(0xD));
3817 return ++__first;
3818 case 't':
3819 if (__str)
3820 *__str = _CharT(0x9);
3821 else
3822 __push_char(_CharT(0x9));
3823 return ++__first;
3824 case 'v':
3825 if (__str)
3826 *__str = _CharT(0xB);
3827 else
3828 __push_char(_CharT(0xB));
3829 return ++__first;
3830 }
3831 if ('0' <= *__first && *__first <= '7')
3832 {
3833 unsigned __val = *__first - '0';
3834 if (++__first != __last && ('0' <= *__first && *__first <= '7'))
3835 {
3836 __val = 8 * __val + *__first - '0';
3837 if (++__first != __last && ('0' <= *__first && *__first <= '7'))
3838 __val = 8 * __val + *__first - '0';
3839 }
3840 if (__str)
3841 *__str = _CharT(__val);
3842 else
3843 __push_char(_CharT(__val));
3844 }
Howard Hinnantd4444702010-08-11 17:04:31 +00003845#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant15476f32010-07-28 17:35:27 +00003846 else
3847 throw regex_error(regex_constants::error_escape);
Howard Hinnantd4444702010-08-11 17:04:31 +00003848#endif
Howard Hinnant15476f32010-07-28 17:35:27 +00003849 return __first;
3850}
3851
3852template <class _CharT, class _Traits>
3853template <class _ForwardIterator>
3854_ForwardIterator
Howard Hinnant0de86b62010-06-25 20:56:08 +00003855basic_regex<_CharT, _Traits>::__parse_equivalence_class(_ForwardIterator __first,
Howard Hinnant173968a2010-07-13 21:48:06 +00003856 _ForwardIterator __last,
3857 __bracket_expression<_CharT, _Traits>* __ml)
Howard Hinnant0de86b62010-06-25 20:56:08 +00003858{
3859 // Found [=
3860 // This means =] must exist
3861 value_type _Equal_close[2] = {'=', ']'};
3862 _ForwardIterator __temp = _STD::search(__first, __last, _Equal_close,
3863 _Equal_close+2);
Howard Hinnantd4444702010-08-11 17:04:31 +00003864#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0de86b62010-06-25 20:56:08 +00003865 if (__temp == __last)
3866 throw regex_error(regex_constants::error_brack);
Howard Hinnantd4444702010-08-11 17:04:31 +00003867#endif
Howard Hinnant0de86b62010-06-25 20:56:08 +00003868 // [__first, __temp) contains all text in [= ... =]
3869 typedef typename _Traits::string_type string_type;
3870 string_type __collate_name =
3871 __traits_.lookup_collatename(__first, __temp);
Howard Hinnantd4444702010-08-11 17:04:31 +00003872#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0de86b62010-06-25 20:56:08 +00003873 if (__collate_name.empty())
Howard Hinnant173968a2010-07-13 21:48:06 +00003874 throw regex_error(regex_constants::error_collate);
Howard Hinnantd4444702010-08-11 17:04:31 +00003875#endif
Howard Hinnant0de86b62010-06-25 20:56:08 +00003876 string_type __equiv_name =
3877 __traits_.transform_primary(__collate_name.begin(),
3878 __collate_name.end());
3879 if (!__equiv_name.empty())
Howard Hinnant173968a2010-07-13 21:48:06 +00003880 __ml->__add_equivalence(__equiv_name);
Howard Hinnant0de86b62010-06-25 20:56:08 +00003881 else
Howard Hinnant173968a2010-07-13 21:48:06 +00003882 {
3883 switch (__collate_name.size())
3884 {
3885 case 1:
3886 __ml->__add_char(__collate_name[0]);
3887 break;
3888 case 2:
3889 __ml->__add_digraph(__collate_name[0], __collate_name[1]);
3890 break;
Howard Hinnantd4444702010-08-11 17:04:31 +00003891#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant173968a2010-07-13 21:48:06 +00003892 default:
3893 throw regex_error(regex_constants::error_collate);
Howard Hinnantd4444702010-08-11 17:04:31 +00003894#endif
Howard Hinnant173968a2010-07-13 21:48:06 +00003895 }
3896 }
Howard Hinnant0de86b62010-06-25 20:56:08 +00003897 __first = next(__temp, 2);
3898 return __first;
3899}
3900
3901template <class _CharT, class _Traits>
3902template <class _ForwardIterator>
3903_ForwardIterator
3904basic_regex<_CharT, _Traits>::__parse_character_class(_ForwardIterator __first,
Howard Hinnant173968a2010-07-13 21:48:06 +00003905 _ForwardIterator __last,
3906 __bracket_expression<_CharT, _Traits>* __ml)
Howard Hinnant0de86b62010-06-25 20:56:08 +00003907{
3908 // Found [:
3909 // This means :] must exist
3910 value_type _Colon_close[2] = {':', ']'};
3911 _ForwardIterator __temp = _STD::search(__first, __last, _Colon_close,
3912 _Colon_close+2);
Howard Hinnantd4444702010-08-11 17:04:31 +00003913#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0de86b62010-06-25 20:56:08 +00003914 if (__temp == __last)
3915 throw regex_error(regex_constants::error_brack);
Howard Hinnantd4444702010-08-11 17:04:31 +00003916#endif
Howard Hinnant0de86b62010-06-25 20:56:08 +00003917 // [__first, __temp) contains all text in [: ... :]
3918 typedef typename _Traits::char_class_type char_class_type;
3919 char_class_type __class_type =
3920 __traits_.lookup_classname(__first, __temp, __flags_ & icase);
Howard Hinnantd4444702010-08-11 17:04:31 +00003921#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0de86b62010-06-25 20:56:08 +00003922 if (__class_type == 0)
3923 throw regex_error(regex_constants::error_brack);
Howard Hinnantd4444702010-08-11 17:04:31 +00003924#endif
Howard Hinnant173968a2010-07-13 21:48:06 +00003925 __ml->__add_class(__class_type);
Howard Hinnant0de86b62010-06-25 20:56:08 +00003926 __first = next(__temp, 2);
3927 return __first;
3928}
3929
3930template <class _CharT, class _Traits>
3931template <class _ForwardIterator>
3932_ForwardIterator
3933basic_regex<_CharT, _Traits>::__parse_collating_symbol(_ForwardIterator __first,
Howard Hinnant173968a2010-07-13 21:48:06 +00003934 _ForwardIterator __last,
3935 basic_string<_CharT>& __col_sym)
Howard Hinnant0de86b62010-06-25 20:56:08 +00003936{
3937 // Found [.
3938 // This means .] must exist
3939 value_type _Dot_close[2] = {'.', ']'};
3940 _ForwardIterator __temp = _STD::search(__first, __last, _Dot_close,
3941 _Dot_close+2);
Howard Hinnantd4444702010-08-11 17:04:31 +00003942#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0de86b62010-06-25 20:56:08 +00003943 if (__temp == __last)
3944 throw regex_error(regex_constants::error_brack);
Howard Hinnantd4444702010-08-11 17:04:31 +00003945#endif
Howard Hinnant0de86b62010-06-25 20:56:08 +00003946 // [__first, __temp) contains all text in [. ... .]
3947 typedef typename _Traits::string_type string_type;
Howard Hinnant173968a2010-07-13 21:48:06 +00003948 __col_sym = __traits_.lookup_collatename(__first, __temp);
3949 switch (__col_sym.size())
3950 {
3951 case 1:
3952 case 2:
3953 break;
Howard Hinnantd4444702010-08-11 17:04:31 +00003954#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant173968a2010-07-13 21:48:06 +00003955 default:
3956 throw regex_error(regex_constants::error_collate);
Howard Hinnantd4444702010-08-11 17:04:31 +00003957#endif
Howard Hinnant173968a2010-07-13 21:48:06 +00003958 }
Howard Hinnant0de86b62010-06-25 20:56:08 +00003959 __first = next(__temp, 2);
3960 return __first;
3961}
3962
3963template <class _CharT, class _Traits>
3964template <class _ForwardIterator>
3965_ForwardIterator
3966basic_regex<_CharT, _Traits>::__parse_DUP_COUNT(_ForwardIterator __first,
3967 _ForwardIterator __last,
3968 int& __c)
3969{
3970 if (__first != __last && '0' <= *__first && *__first <= '9')
3971 {
3972 __c = *__first - '0';
3973 for (++__first; __first != __last && '0' <= *__first && *__first <= '9';
3974 ++__first)
3975 {
3976 __c *= 10;
3977 __c += *__first - '0';
3978 }
3979 }
3980 return __first;
3981}
3982
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003983template <class _CharT, class _Traits>
Howard Hinnant2ade7c22010-07-22 17:53:24 +00003984template <class _ForwardIterator>
3985_ForwardIterator
3986basic_regex<_CharT, _Traits>::__parse_ecma_exp(_ForwardIterator __first,
3987 _ForwardIterator __last)
3988{
3989 __owns_one_state<_CharT>* __sa = __end_;
3990 _ForwardIterator __temp = __parse_alternative(__first, __last);
3991 if (__temp == __first)
3992 __push_empty();
3993 __first = __temp;
3994 while (__first != __last && *__first == '|')
3995 {
3996 __owns_one_state<_CharT>* __sb = __end_;
3997 __temp = __parse_alternative(++__first, __last);
3998 if (__temp == __first)
3999 __push_empty();
4000 __push_alternation(__sa, __sb);
4001 __first = __temp;
4002 }
4003 return __first;
4004}
4005
4006template <class _CharT, class _Traits>
4007template <class _ForwardIterator>
4008_ForwardIterator
4009basic_regex<_CharT, _Traits>::__parse_alternative(_ForwardIterator __first,
4010 _ForwardIterator __last)
4011{
4012 while (true)
4013 {
4014 _ForwardIterator __temp = __parse_term(__first, __last);
4015 if (__temp == __first)
4016 break;
4017 __first = __temp;
4018 }
4019 return __first;
4020}
4021
4022template <class _CharT, class _Traits>
4023template <class _ForwardIterator>
4024_ForwardIterator
4025basic_regex<_CharT, _Traits>::__parse_term(_ForwardIterator __first,
4026 _ForwardIterator __last)
4027{
4028 _ForwardIterator __temp = __parse_assertion(__first, __last);
4029 if (__temp == __first)
4030 {
Howard Hinnant17615b02010-07-27 01:25:38 +00004031 __owns_one_state<_CharT>* __e = __end_;
4032 unsigned __mexp_begin = __marked_count_;
Howard Hinnant2ade7c22010-07-22 17:53:24 +00004033 __temp = __parse_atom(__first, __last);
4034 if (__temp != __first)
Howard Hinnant17615b02010-07-27 01:25:38 +00004035 __first = __parse_ERE_dupl_symbol(__temp, __last, __e,
4036 __mexp_begin+1, __marked_count_+1);
Howard Hinnant2ade7c22010-07-22 17:53:24 +00004037 }
Howard Hinnant17615b02010-07-27 01:25:38 +00004038 else
4039 __first = __temp;
Howard Hinnant2ade7c22010-07-22 17:53:24 +00004040 return __first;
4041}
4042
4043template <class _CharT, class _Traits>
4044template <class _ForwardIterator>
4045_ForwardIterator
4046basic_regex<_CharT, _Traits>::__parse_assertion(_ForwardIterator __first,
4047 _ForwardIterator __last)
4048{
4049 if (__first != __last)
4050 {
4051 switch (*__first)
4052 {
4053 case '^':
4054 __push_l_anchor();
4055 ++__first;
4056 break;
4057 case '$':
4058 __push_r_anchor();
4059 ++__first;
4060 break;
4061 case '\\':
4062 {
4063 _ForwardIterator __temp = _STD::next(__first);
4064 if (__temp != __last)
4065 {
4066 if (*__temp == 'b')
4067 {
Howard Hinnant17615b02010-07-27 01:25:38 +00004068 __push_word_boundary(false);
Howard Hinnant2ade7c22010-07-22 17:53:24 +00004069 __first = ++__temp;
4070 }
4071 else if (*__temp == 'B')
4072 {
Howard Hinnant17615b02010-07-27 01:25:38 +00004073 __push_word_boundary(true);
Howard Hinnant2ade7c22010-07-22 17:53:24 +00004074 __first = ++__temp;
4075 }
4076 }
4077 }
4078 break;
4079 case '(':
4080 {
4081 _ForwardIterator __temp = _STD::next(__first);
4082 if (__temp != __last && *__temp == '?')
4083 {
4084 if (++__temp != __last)
4085 {
4086 switch (*__temp)
4087 {
4088 case '=':
Howard Hinnante9de5ff2010-07-27 22:20:32 +00004089 {
4090 basic_regex __exp;
4091 __exp.__flags_ = __flags_;
4092 __temp = __exp.__parse(++__temp, __last);
4093 __exp.__push_l_anchor();
4094 __push_lookahead(_STD::move(__exp), false);
Howard Hinnantd4444702010-08-11 17:04:31 +00004095#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnante9de5ff2010-07-27 22:20:32 +00004096 if (__temp == __last || *__temp != ')')
4097 throw regex_error(regex_constants::error_paren);
Howard Hinnantd4444702010-08-11 17:04:31 +00004098#endif
Howard Hinnante9de5ff2010-07-27 22:20:32 +00004099 __first = ++__temp;
4100 }
Howard Hinnant2ade7c22010-07-22 17:53:24 +00004101 break;
4102 case '!':
Howard Hinnante9de5ff2010-07-27 22:20:32 +00004103 {
4104 basic_regex __exp;
4105 __exp.__flags_ = __flags_;
4106 __temp = __exp.__parse(++__temp, __last);
4107 __exp.__push_l_anchor();
4108 __push_lookahead(_STD::move(__exp), true);
Howard Hinnantd4444702010-08-11 17:04:31 +00004109#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnante9de5ff2010-07-27 22:20:32 +00004110 if (__temp == __last || *__temp != ')')
4111 throw regex_error(regex_constants::error_paren);
Howard Hinnantd4444702010-08-11 17:04:31 +00004112#endif
Howard Hinnante9de5ff2010-07-27 22:20:32 +00004113 __first = ++__temp;
4114 }
Howard Hinnant2ade7c22010-07-22 17:53:24 +00004115 break;
4116 }
4117 }
4118 }
4119 }
4120 break;
4121 }
4122 }
4123 return __first;
4124}
4125
4126template <class _CharT, class _Traits>
4127template <class _ForwardIterator>
4128_ForwardIterator
4129basic_regex<_CharT, _Traits>::__parse_atom(_ForwardIterator __first,
4130 _ForwardIterator __last)
4131{
Howard Hinnant17615b02010-07-27 01:25:38 +00004132 if (__first != __last)
4133 {
4134 switch (*__first)
4135 {
4136 case '.':
4137 __push_match_any_but_newline();
4138 ++__first;
4139 break;
4140 case '\\':
4141 __first = __parse_atom_escape(__first, __last);
4142 break;
4143 case '[':
4144 __first = __parse_bracket_expression(__first, __last);
4145 break;
4146 case '(':
4147 {
Howard Hinnantd4444702010-08-11 17:04:31 +00004148 ++__first;
4149#ifndef _LIBCPP_NO_EXCEPTIONS
4150 if (__first == __last)
Howard Hinnant17615b02010-07-27 01:25:38 +00004151 throw regex_error(regex_constants::error_paren);
Howard Hinnantd4444702010-08-11 17:04:31 +00004152#endif
Howard Hinnant17615b02010-07-27 01:25:38 +00004153 _ForwardIterator __temp = _STD::next(__first);
4154 if (__temp != __last && *__first == '?' && *__temp == ':')
4155 {
4156 ++__open_count_;
4157 __first = __parse_ecma_exp(++__temp, __last);
Howard Hinnantd4444702010-08-11 17:04:31 +00004158#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant17615b02010-07-27 01:25:38 +00004159 if (__first == __last || *__first != ')')
4160 throw regex_error(regex_constants::error_paren);
Howard Hinnantd4444702010-08-11 17:04:31 +00004161#endif
Howard Hinnant17615b02010-07-27 01:25:38 +00004162 --__open_count_;
4163 ++__first;
4164 }
4165 else
4166 {
4167 __push_begin_marked_subexpression();
4168 unsigned __temp_count = __marked_count_;
4169 ++__open_count_;
4170 __first = __parse_ecma_exp(__first, __last);
Howard Hinnantd4444702010-08-11 17:04:31 +00004171#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant17615b02010-07-27 01:25:38 +00004172 if (__first == __last || *__first != ')')
4173 throw regex_error(regex_constants::error_paren);
Howard Hinnantd4444702010-08-11 17:04:31 +00004174#endif
Howard Hinnant17615b02010-07-27 01:25:38 +00004175 __push_end_marked_subexpression(__temp_count);
4176 --__open_count_;
4177 ++__first;
4178 }
4179 }
4180 break;
4181 default:
4182 __first = __parse_pattern_character(__first, __last);
4183 break;
4184 }
4185 }
4186 return __first;
4187}
4188
4189template <class _CharT, class _Traits>
4190template <class _ForwardIterator>
4191_ForwardIterator
4192basic_regex<_CharT, _Traits>::__parse_atom_escape(_ForwardIterator __first,
4193 _ForwardIterator __last)
4194{
4195 if (__first != __last && *__first == '\\')
4196 {
4197 _ForwardIterator __t1 = _STD::next(__first);
4198 _ForwardIterator __t2 = __parse_decimal_escape(__t1, __last);
4199 if (__t2 != __t1)
4200 __first = __t2;
4201 else
4202 {
4203 __t2 = __parse_character_class_escape(__t1, __last);
4204 if (__t2 != __t1)
4205 __first = __t2;
4206 else
4207 {
4208 __t2 = __parse_character_escape(__t1, __last);
4209 if (__t2 != __t1)
4210 __first = __t2;
4211 }
4212 }
4213 }
4214 return __first;
4215}
4216
4217template <class _CharT, class _Traits>
4218template <class _ForwardIterator>
4219_ForwardIterator
4220basic_regex<_CharT, _Traits>::__parse_decimal_escape(_ForwardIterator __first,
4221 _ForwardIterator __last)
4222{
4223 if (__first != __last)
4224 {
4225 if (*__first == '0')
4226 {
4227 __push_char(_CharT());
4228 ++__first;
4229 }
4230 else if ('1' <= *__first && *__first <= '9')
4231 {
4232 unsigned __v = *__first - '0';
4233 for (++__first; '0' <= *__first && *__first <= '9'; ++__first)
4234 __v = 10 * __v + *__first - '0';
Howard Hinnantd4444702010-08-11 17:04:31 +00004235#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant17615b02010-07-27 01:25:38 +00004236 if (__v > mark_count())
4237 throw regex_error(regex_constants::error_backref);
Howard Hinnantd4444702010-08-11 17:04:31 +00004238#endif
Howard Hinnant17615b02010-07-27 01:25:38 +00004239 __push_back_ref(__v);
4240 }
4241 }
4242 return __first;
4243}
4244
4245template <class _CharT, class _Traits>
4246template <class _ForwardIterator>
4247_ForwardIterator
4248basic_regex<_CharT, _Traits>::__parse_character_class_escape(_ForwardIterator __first,
4249 _ForwardIterator __last)
4250{
4251 if (__first != __last)
4252 {
4253 __bracket_expression<_CharT, _Traits>* __ml;
4254 switch (*__first)
4255 {
4256 case 'd':
4257 __ml = __start_matching_list(false);
4258 __ml->__add_class(ctype_base::digit);
4259 ++__first;
4260 break;
4261 case 'D':
4262 __ml = __start_matching_list(true);
4263 __ml->__add_class(ctype_base::digit);
4264 ++__first;
4265 break;
4266 case 's':
4267 __ml = __start_matching_list(false);
4268 __ml->__add_class(ctype_base::space);
4269 ++__first;
4270 break;
4271 case 'S':
4272 __ml = __start_matching_list(true);
4273 __ml->__add_class(ctype_base::space);
4274 ++__first;
4275 break;
4276 case 'w':
4277 __ml = __start_matching_list(false);
4278 __ml->__add_class(ctype_base::alnum);
4279 __ml->__add_char('_');
4280 ++__first;
4281 break;
4282 case 'W':
4283 __ml = __start_matching_list(true);
4284 __ml->__add_class(ctype_base::alnum);
4285 __ml->__add_char('_');
4286 ++__first;
4287 break;
4288 }
4289 }
4290 return __first;
4291}
4292
4293template <class _CharT, class _Traits>
4294template <class _ForwardIterator>
4295_ForwardIterator
4296basic_regex<_CharT, _Traits>::__parse_character_escape(_ForwardIterator __first,
Howard Hinnant15476f32010-07-28 17:35:27 +00004297 _ForwardIterator __last,
4298 basic_string<_CharT>* __str)
Howard Hinnant17615b02010-07-27 01:25:38 +00004299{
4300 if (__first != __last)
4301 {
4302 _ForwardIterator __t;
4303 unsigned __sum = 0;
4304 int __hd;
4305 switch (*__first)
4306 {
4307 case 'f':
Howard Hinnant15476f32010-07-28 17:35:27 +00004308 if (__str)
4309 *__str = _CharT(0xC);
4310 else
4311 __push_char(_CharT(0xC));
Howard Hinnant17615b02010-07-27 01:25:38 +00004312 ++__first;
4313 break;
4314 case 'n':
Howard Hinnant15476f32010-07-28 17:35:27 +00004315 if (__str)
4316 *__str = _CharT(0xA);
4317 else
4318 __push_char(_CharT(0xA));
Howard Hinnant17615b02010-07-27 01:25:38 +00004319 ++__first;
4320 break;
4321 case 'r':
Howard Hinnant15476f32010-07-28 17:35:27 +00004322 if (__str)
4323 *__str = _CharT(0xD);
4324 else
4325 __push_char(_CharT(0xD));
Howard Hinnant17615b02010-07-27 01:25:38 +00004326 ++__first;
4327 break;
4328 case 't':
Howard Hinnant15476f32010-07-28 17:35:27 +00004329 if (__str)
4330 *__str = _CharT(0x9);
4331 else
4332 __push_char(_CharT(0x9));
Howard Hinnant17615b02010-07-27 01:25:38 +00004333 ++__first;
4334 break;
4335 case 'v':
Howard Hinnant15476f32010-07-28 17:35:27 +00004336 if (__str)
4337 *__str = _CharT(0xB);
4338 else
4339 __push_char(_CharT(0xB));
Howard Hinnant17615b02010-07-27 01:25:38 +00004340 ++__first;
4341 break;
4342 case 'c':
4343 if ((__t = _STD::next(__first)) != __last)
4344 {
4345 if ('A' <= *__t <= 'Z' || 'a' <= *__t <= 'z')
4346 {
Howard Hinnant15476f32010-07-28 17:35:27 +00004347 if (__str)
4348 *__str = _CharT(*__t % 32);
4349 else
4350 __push_char(_CharT(*__t % 32));
Howard Hinnant17615b02010-07-27 01:25:38 +00004351 __first = ++__t;
4352 }
4353 }
4354 break;
4355 case 'u':
Howard Hinnantd4444702010-08-11 17:04:31 +00004356 ++__first;
4357#ifndef _LIBCPP_NO_EXCEPTIONS
4358 if (__first == __last)
Howard Hinnant17615b02010-07-27 01:25:38 +00004359 throw regex_error(regex_constants::error_escape);
Howard Hinnantd4444702010-08-11 17:04:31 +00004360#endif
Howard Hinnant17615b02010-07-27 01:25:38 +00004361 __hd = __traits_.value(*__first, 16);
Howard Hinnantd4444702010-08-11 17:04:31 +00004362#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant17615b02010-07-27 01:25:38 +00004363 if (__hd == -1)
4364 throw regex_error(regex_constants::error_escape);
Howard Hinnantd4444702010-08-11 17:04:31 +00004365#endif
Howard Hinnant17615b02010-07-27 01:25:38 +00004366 __sum = 16 * __sum + __hd;
Howard Hinnantd4444702010-08-11 17:04:31 +00004367 ++__first;
4368#ifndef _LIBCPP_NO_EXCEPTIONS
4369 if (__first == __last)
Howard Hinnant17615b02010-07-27 01:25:38 +00004370 throw regex_error(regex_constants::error_escape);
Howard Hinnantd4444702010-08-11 17:04:31 +00004371#endif
Howard Hinnant17615b02010-07-27 01:25:38 +00004372 __hd = __traits_.value(*__first, 16);
Howard Hinnantd4444702010-08-11 17:04:31 +00004373#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant17615b02010-07-27 01:25:38 +00004374 if (__hd == -1)
4375 throw regex_error(regex_constants::error_escape);
Howard Hinnantd4444702010-08-11 17:04:31 +00004376#endif
Howard Hinnant17615b02010-07-27 01:25:38 +00004377 __sum = 16 * __sum + __hd;
4378 // drop through
4379 case 'x':
Howard Hinnantd4444702010-08-11 17:04:31 +00004380 ++__first;
4381#ifndef _LIBCPP_NO_EXCEPTIONS
4382 if (__first == __last)
Howard Hinnant17615b02010-07-27 01:25:38 +00004383 throw regex_error(regex_constants::error_escape);
Howard Hinnantd4444702010-08-11 17:04:31 +00004384#endif
Howard Hinnant17615b02010-07-27 01:25:38 +00004385 __hd = __traits_.value(*__first, 16);
Howard Hinnantd4444702010-08-11 17:04:31 +00004386#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant17615b02010-07-27 01:25:38 +00004387 if (__hd == -1)
4388 throw regex_error(regex_constants::error_escape);
Howard Hinnantd4444702010-08-11 17:04:31 +00004389#endif
Howard Hinnant17615b02010-07-27 01:25:38 +00004390 __sum = 16 * __sum + __hd;
Howard Hinnantd4444702010-08-11 17:04:31 +00004391 ++__first;
4392#ifndef _LIBCPP_NO_EXCEPTIONS
4393 if (__first == __last)
Howard Hinnant17615b02010-07-27 01:25:38 +00004394 throw regex_error(regex_constants::error_escape);
Howard Hinnantd4444702010-08-11 17:04:31 +00004395#endif
Howard Hinnant17615b02010-07-27 01:25:38 +00004396 __hd = __traits_.value(*__first, 16);
Howard Hinnantd4444702010-08-11 17:04:31 +00004397#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant17615b02010-07-27 01:25:38 +00004398 if (__hd == -1)
4399 throw regex_error(regex_constants::error_escape);
Howard Hinnantd4444702010-08-11 17:04:31 +00004400#endif
Howard Hinnant17615b02010-07-27 01:25:38 +00004401 __sum = 16 * __sum + __hd;
Howard Hinnant15476f32010-07-28 17:35:27 +00004402 if (__str)
4403 *__str = _CharT(__sum);
4404 else
4405 __push_char(_CharT(__sum));
Howard Hinnant17615b02010-07-27 01:25:38 +00004406 ++__first;
4407 break;
4408 default:
4409 if (*__first != '_' && !__traits_.isctype(*__first, ctype_base::alnum))
4410 {
Howard Hinnant15476f32010-07-28 17:35:27 +00004411 if (__str)
4412 *__str = *__first;
4413 else
4414 __push_char(*__first);
Howard Hinnant17615b02010-07-27 01:25:38 +00004415 ++__first;
4416 }
Howard Hinnantd4444702010-08-11 17:04:31 +00004417#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant15476f32010-07-28 17:35:27 +00004418 else if (__str)
4419 throw regex_error(regex_constants::error_escape);
Howard Hinnantd4444702010-08-11 17:04:31 +00004420#endif
Howard Hinnant17615b02010-07-27 01:25:38 +00004421 break;
4422 }
4423 }
4424 return __first;
4425}
4426
4427template <class _CharT, class _Traits>
4428template <class _ForwardIterator>
4429_ForwardIterator
4430basic_regex<_CharT, _Traits>::__parse_pattern_character(_ForwardIterator __first,
4431 _ForwardIterator __last)
4432{
4433 if (__first != __last)
4434 {
4435 switch (*__first)
4436 {
4437 case '^':
4438 case '$':
4439 case '\\':
4440 case '.':
4441 case '*':
4442 case '+':
4443 case '?':
4444 case '(':
4445 case ')':
4446 case '[':
4447 case ']':
4448 case '{':
4449 case '}':
4450 case '|':
4451 break;
4452 default:
4453 __push_char(*__first);
4454 ++__first;
4455 break;
4456 }
4457 }
4458 return __first;
Howard Hinnant2ade7c22010-07-22 17:53:24 +00004459}
4460
4461template <class _CharT, class _Traits>
Howard Hinnant856846b2010-07-27 19:53:10 +00004462template <class _ForwardIterator>
4463_ForwardIterator
4464basic_regex<_CharT, _Traits>::__parse_grep(_ForwardIterator __first,
4465 _ForwardIterator __last)
4466{
4467 __owns_one_state<_CharT>* __sa = __end_;
4468 _ForwardIterator __t1 = _STD::find(__first, __last, _CharT('\n'));
4469 if (__t1 != __first)
4470 __parse_basic_reg_exp(__first, __t1);
4471 else
4472 __push_empty();
4473 __first = __t1;
4474 if (__first != __last)
4475 ++__first;
4476 while (__first != __last)
4477 {
4478 __t1 = _STD::find(__first, __last, _CharT('\n'));
4479 __owns_one_state<_CharT>* __sb = __end_;
4480 if (__t1 != __first)
4481 __parse_basic_reg_exp(__first, __t1);
4482 else
4483 __push_empty();
4484 __push_alternation(__sa, __sb);
4485 __first = __t1;
4486 if (__first != __last)
4487 ++__first;
4488 }
4489 return __first;
4490}
4491
4492template <class _CharT, class _Traits>
4493template <class _ForwardIterator>
4494_ForwardIterator
4495basic_regex<_CharT, _Traits>::__parse_egrep(_ForwardIterator __first,
4496 _ForwardIterator __last)
4497{
4498 __owns_one_state<_CharT>* __sa = __end_;
4499 _ForwardIterator __t1 = _STD::find(__first, __last, _CharT('\n'));
4500 if (__t1 != __first)
4501 __parse_extended_reg_exp(__first, __t1);
4502 else
4503 __push_empty();
4504 __first = __t1;
4505 if (__first != __last)
4506 ++__first;
4507 while (__first != __last)
4508 {
4509 __t1 = _STD::find(__first, __last, _CharT('\n'));
4510 __owns_one_state<_CharT>* __sb = __end_;
4511 if (__t1 != __first)
4512 __parse_extended_reg_exp(__first, __t1);
4513 else
4514 __push_empty();
4515 __push_alternation(__sa, __sb);
4516 __first = __t1;
4517 if (__first != __last)
4518 ++__first;
4519 }
4520 return __first;
4521}
4522
4523template <class _CharT, class _Traits>
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00004524void
Howard Hinnantf8ce4592010-07-07 19:14:52 +00004525basic_regex<_CharT, _Traits>::__push_loop(size_t __min, size_t __max,
4526 __owns_one_state<_CharT>* __s, size_t __mexp_begin, size_t __mexp_end,
4527 bool __greedy)
4528{
4529 unique_ptr<__empty_state<_CharT> > __e1(new __empty_state<_CharT>(__end_->first()));
4530 __end_->first() = nullptr;
Howard Hinnantac303862010-07-12 15:51:17 +00004531 unique_ptr<__loop<_CharT> > __e2(new __loop<_CharT>(__loop_count_,
4532 __s->first(), __e1.get(), __mexp_begin, __mexp_end, __greedy,
4533 __min, __max));
4534 __s->first() = nullptr;
4535 __e1.release();
4536 __end_->first() = new __repeat_one_loop<_CharT>(__e2.get());
Howard Hinnantf8ce4592010-07-07 19:14:52 +00004537 __end_ = __e2->second();
Howard Hinnantac303862010-07-12 15:51:17 +00004538 __s->first() = __e2.release();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00004539 ++__loop_count_;
4540}
4541
4542template <class _CharT, class _Traits>
4543void
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00004544basic_regex<_CharT, _Traits>::__push_char(value_type __c)
4545{
Howard Hinnant173968a2010-07-13 21:48:06 +00004546 if (flags() & icase)
Howard Hinnante34f17d2010-07-12 19:11:27 +00004547 __end_->first() = new __match_char_icase<_CharT, _Traits>
4548 (__traits_, __c, __end_->first());
Howard Hinnant173968a2010-07-13 21:48:06 +00004549 else if (flags() & collate)
Howard Hinnante34f17d2010-07-12 19:11:27 +00004550 __end_->first() = new __match_char_collate<_CharT, _Traits>
4551 (__traits_, __c, __end_->first());
4552 else
4553 __end_->first() = new __match_char<_CharT>(__c, __end_->first());
Howard Hinnante77aa5e2010-07-08 17:43:58 +00004554 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00004555}
4556
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00004557template <class _CharT, class _Traits>
4558void
4559basic_regex<_CharT, _Traits>::__push_begin_marked_subexpression()
4560{
Howard Hinnant68025ed2010-07-14 15:45:11 +00004561 if (!(__flags_ & nosubs))
4562 {
4563 __end_->first() =
4564 new __begin_marked_subexpression<_CharT>(++__marked_count_,
4565 __end_->first());
4566 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4567 }
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00004568}
4569
4570template <class _CharT, class _Traits>
4571void
4572basic_regex<_CharT, _Traits>::__push_end_marked_subexpression(unsigned __sub)
4573{
Howard Hinnant68025ed2010-07-14 15:45:11 +00004574 if (!(__flags_ & nosubs))
4575 {
4576 __end_->first() =
4577 new __end_marked_subexpression<_CharT>(__sub, __end_->first());
4578 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4579 }
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00004580}
4581
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00004582template <class _CharT, class _Traits>
4583void
4584basic_regex<_CharT, _Traits>::__push_r_anchor()
4585{
4586 __end_->first() = new __r_anchor<_CharT>(__end_->first());
4587 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4588}
4589
Howard Hinnantac303862010-07-12 15:51:17 +00004590template <class _CharT, class _Traits>
4591void
4592basic_regex<_CharT, _Traits>::__push_match_any()
4593{
4594 __end_->first() = new __match_any<_CharT>(__end_->first());
4595 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4596}
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00004597
Howard Hinnantcba352d2010-07-12 18:16:05 +00004598template <class _CharT, class _Traits>
4599void
Howard Hinnant17615b02010-07-27 01:25:38 +00004600basic_regex<_CharT, _Traits>::__push_match_any_but_newline()
4601{
4602 __end_->first() = new __match_any_but_newline<_CharT>(__end_->first());
4603 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4604}
4605
4606template <class _CharT, class _Traits>
4607void
Howard Hinnant2ade7c22010-07-22 17:53:24 +00004608basic_regex<_CharT, _Traits>::__push_empty()
4609{
4610 __end_->first() = new __empty_state<_CharT>(__end_->first());
4611 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4612}
4613
4614template <class _CharT, class _Traits>
4615void
Howard Hinnant17615b02010-07-27 01:25:38 +00004616basic_regex<_CharT, _Traits>::__push_word_boundary(bool __invert)
4617{
4618 __end_->first() = new __word_boundary<_CharT, _Traits>(__traits_, __invert,
4619 __end_->first());
4620 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4621}
4622
4623template <class _CharT, class _Traits>
4624void
Howard Hinnantcba352d2010-07-12 18:16:05 +00004625basic_regex<_CharT, _Traits>::__push_back_ref(int __i)
4626{
Howard Hinnant173968a2010-07-13 21:48:06 +00004627 if (flags() & icase)
Howard Hinnante34f17d2010-07-12 19:11:27 +00004628 __end_->first() = new __back_ref_icase<_CharT, _Traits>
4629 (__traits_, __i, __end_->first());
Howard Hinnant173968a2010-07-13 21:48:06 +00004630 else if (flags() & collate)
Howard Hinnante34f17d2010-07-12 19:11:27 +00004631 __end_->first() = new __back_ref_collate<_CharT, _Traits>
4632 (__traits_, __i, __end_->first());
4633 else
4634 __end_->first() = new __back_ref<_CharT>(__i, __end_->first());
Howard Hinnantcba352d2010-07-12 18:16:05 +00004635 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4636}
4637
Howard Hinnant173968a2010-07-13 21:48:06 +00004638template <class _CharT, class _Traits>
Howard Hinnantaa698082010-07-16 19:08:36 +00004639void
4640basic_regex<_CharT, _Traits>::__push_alternation(__owns_one_state<_CharT>* __sa,
4641 __owns_one_state<_CharT>* __ea)
4642{
4643 __sa->first() = new __alternate<_CharT>(
4644 static_cast<__owns_one_state<_CharT>*>(__sa->first()),
4645 static_cast<__owns_one_state<_CharT>*>(__ea->first()));
4646 __ea->first() = nullptr;
4647 __ea->first() = new __empty_state<_CharT>(__end_->first());
4648 __end_->first() = nullptr;
4649 __end_->first() = new __empty_non_own_state<_CharT>(__ea->first());
4650 __end_ = static_cast<__owns_one_state<_CharT>*>(__ea->first());
4651}
4652
4653template <class _CharT, class _Traits>
Howard Hinnant173968a2010-07-13 21:48:06 +00004654__bracket_expression<_CharT, _Traits>*
4655basic_regex<_CharT, _Traits>::__start_matching_list(bool __negate)
4656{
4657 __bracket_expression<_CharT, _Traits>* __r =
4658 new __bracket_expression<_CharT, _Traits>(__traits_, __end_->first(),
4659 __negate, __flags_ & icase,
4660 __flags_ & collate);
4661 __end_->first() = __r;
4662 __end_ = __r;
4663 return __r;
4664}
4665
Howard Hinnante9de5ff2010-07-27 22:20:32 +00004666template <class _CharT, class _Traits>
4667void
4668basic_regex<_CharT, _Traits>::__push_lookahead(const basic_regex& __exp,
4669 bool __invert)
4670{
4671 __end_->first() = new __lookahead<_CharT, _Traits>(__exp, __invert,
4672 __end_->first());
4673 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4674}
4675
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00004676typedef basic_regex<char> regex;
4677typedef basic_regex<wchar_t> wregex;
4678
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00004679// sub_match
4680
4681template <class _BidirectionalIterator>
4682class sub_match
4683 : public pair<_BidirectionalIterator, _BidirectionalIterator>
4684{
4685public:
4686 typedef _BidirectionalIterator iterator;
4687 typedef typename iterator_traits<iterator>::value_type value_type;
4688 typedef typename iterator_traits<iterator>::difference_type difference_type;
4689 typedef basic_string<value_type> string_type;
4690
4691 bool matched;
4692
4693 difference_type length() const
4694 {return matched ? _STD::distance(this->first, this->second) : 0;}
4695 string_type str() const
4696 {return matched ? string_type(this->first, this->second) : string_type();}
4697 operator string_type() const
4698 {return str();}
4699
4700 int compare(const sub_match& __s) const
4701 {return str().compare(__s.str());}
4702 int compare(const string_type& __s) const
4703 {return str().compare(__s);}
4704 int compare(const value_type* __s) const
4705 {return str().compare(__s);}
4706};
4707
4708typedef sub_match<const char*> csub_match;
4709typedef sub_match<const wchar_t*> wcsub_match;
4710typedef sub_match<string::const_iterator> ssub_match;
4711typedef sub_match<wstring::const_iterator> wssub_match;
4712
4713template <class _BiIter>
4714inline _LIBCPP_INLINE_VISIBILITY
4715bool
4716operator==(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
4717{
4718 return __x.compare(__y) == 0;
4719}
4720
4721template <class _BiIter>
4722inline _LIBCPP_INLINE_VISIBILITY
4723bool
4724operator!=(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
4725{
4726 return !(__x == __y);
4727}
4728
4729template <class _BiIter>
4730inline _LIBCPP_INLINE_VISIBILITY
4731bool
4732operator<(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
4733{
4734 return __x.compare(__y) < 0;
4735}
4736
4737template <class _BiIter>
4738inline _LIBCPP_INLINE_VISIBILITY
4739bool
4740operator<=(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
4741{
4742 return !(__y < __x);
4743}
4744
4745template <class _BiIter>
4746inline _LIBCPP_INLINE_VISIBILITY
4747bool
4748operator>=(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
4749{
4750 return !(__x < __y);
4751}
4752
4753template <class _BiIter>
4754inline _LIBCPP_INLINE_VISIBILITY
4755bool
4756operator>(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
4757{
4758 return __y < __x;
4759}
4760
4761template <class _BiIter, class _ST, class _SA>
4762inline _LIBCPP_INLINE_VISIBILITY
4763bool
4764operator==(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4765 const sub_match<_BiIter>& __y)
4766{
4767 return __y.compare(__x.c_str()) == 0;
4768}
4769
4770template <class _BiIter, class _ST, class _SA>
4771inline _LIBCPP_INLINE_VISIBILITY
4772bool
4773operator!=(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4774 const sub_match<_BiIter>& __y)
4775{
4776 return !(__x == __y);
4777}
4778
4779template <class _BiIter, class _ST, class _SA>
4780inline _LIBCPP_INLINE_VISIBILITY
4781bool
4782operator<(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4783 const sub_match<_BiIter>& __y)
4784{
4785 return __y.compare(__x.c_str()) > 0;
4786}
4787
4788template <class _BiIter, class _ST, class _SA>
4789inline _LIBCPP_INLINE_VISIBILITY
4790bool
4791operator>(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4792 const sub_match<_BiIter>& __y)
4793{
4794 return __y < __x;
4795}
4796
4797template <class _BiIter, class _ST, class _SA>
4798inline _LIBCPP_INLINE_VISIBILITY
4799bool operator>=(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4800 const sub_match<_BiIter>& __y)
4801{
4802 return !(__x < __y);
4803}
4804
4805template <class _BiIter, class _ST, class _SA>
4806inline _LIBCPP_INLINE_VISIBILITY
4807bool
4808operator<=(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4809 const sub_match<_BiIter>& __y)
4810{
4811 return !(__y < __x);
4812}
4813
4814template <class _BiIter, class _ST, class _SA>
4815inline _LIBCPP_INLINE_VISIBILITY
4816bool
4817operator==(const sub_match<_BiIter>& __x,
4818 const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
4819{
4820 return __x.compare(__y.c_str()) == 0;
4821}
4822
4823template <class _BiIter, class _ST, class _SA>
4824inline _LIBCPP_INLINE_VISIBILITY
4825bool
4826operator!=(const sub_match<_BiIter>& __x,
4827 const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
4828{
4829 return !(__x == __y);
4830}
4831
4832template <class _BiIter, class _ST, class _SA>
4833inline _LIBCPP_INLINE_VISIBILITY
4834bool
4835operator<(const sub_match<_BiIter>& __x,
4836 const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
4837{
4838 return __x.compare(__y.c_str()) < 0;
4839}
4840
4841template <class _BiIter, class _ST, class _SA>
4842inline _LIBCPP_INLINE_VISIBILITY
4843bool operator>(const sub_match<_BiIter>& __x,
4844 const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
4845{
4846 return __y < __x;
4847}
4848
4849template <class _BiIter, class _ST, class _SA>
4850inline _LIBCPP_INLINE_VISIBILITY
4851bool
4852operator>=(const sub_match<_BiIter>& __x,
4853 const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
4854{
4855 return !(__x < __y);
4856}
4857
4858template <class _BiIter, class _ST, class _SA>
4859inline _LIBCPP_INLINE_VISIBILITY
4860bool
4861operator<=(const sub_match<_BiIter>& __x,
4862 const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
4863{
4864 return !(__y < __x);
4865}
4866
4867template <class _BiIter>
4868inline _LIBCPP_INLINE_VISIBILITY
4869bool
4870operator==(typename iterator_traits<_BiIter>::value_type const* __x,
4871 const sub_match<_BiIter>& __y)
4872{
4873 return __y.compare(__x) == 0;
4874}
4875
4876template <class _BiIter>
4877inline _LIBCPP_INLINE_VISIBILITY
4878bool
4879operator!=(typename iterator_traits<_BiIter>::value_type const* __x,
4880 const sub_match<_BiIter>& __y)
4881{
4882 return !(__x == __y);
4883}
4884
4885template <class _BiIter>
4886inline _LIBCPP_INLINE_VISIBILITY
4887bool
4888operator<(typename iterator_traits<_BiIter>::value_type const* __x,
4889 const sub_match<_BiIter>& __y)
4890{
4891 return __y.compare(__x) > 0;
4892}
4893
4894template <class _BiIter>
4895inline _LIBCPP_INLINE_VISIBILITY
4896bool
4897operator>(typename iterator_traits<_BiIter>::value_type const* __x,
4898 const sub_match<_BiIter>& __y)
4899{
4900 return __y < __x;
4901}
4902
4903template <class _BiIter>
4904inline _LIBCPP_INLINE_VISIBILITY
4905bool
4906operator>=(typename iterator_traits<_BiIter>::value_type const* __x,
4907 const sub_match<_BiIter>& __y)
4908{
4909 return !(__x < __y);
4910}
4911
4912template <class _BiIter>
4913inline _LIBCPP_INLINE_VISIBILITY
4914bool
4915operator<=(typename iterator_traits<_BiIter>::value_type const* __x,
4916 const sub_match<_BiIter>& __y)
4917{
4918 return !(__y < __x);
4919}
4920
4921template <class _BiIter>
4922inline _LIBCPP_INLINE_VISIBILITY
4923bool
4924operator==(const sub_match<_BiIter>& __x,
4925 typename iterator_traits<_BiIter>::value_type const* __y)
4926{
4927 return __x.compare(__y) == 0;
4928}
4929
4930template <class _BiIter>
4931inline _LIBCPP_INLINE_VISIBILITY
4932bool
4933operator!=(const sub_match<_BiIter>& __x,
4934 typename iterator_traits<_BiIter>::value_type const* __y)
4935{
4936 return !(__x == __y);
4937}
4938
4939template <class _BiIter>
4940inline _LIBCPP_INLINE_VISIBILITY
4941bool
4942operator<(const sub_match<_BiIter>& __x,
4943 typename iterator_traits<_BiIter>::value_type const* __y)
4944{
4945 return __x.compare(__y) < 0;
4946}
4947
4948template <class _BiIter>
4949inline _LIBCPP_INLINE_VISIBILITY
4950bool
4951operator>(const sub_match<_BiIter>& __x,
4952 typename iterator_traits<_BiIter>::value_type const* __y)
4953{
4954 return __y < __x;
4955}
4956
4957template <class _BiIter>
4958inline _LIBCPP_INLINE_VISIBILITY
4959bool
4960operator>=(const sub_match<_BiIter>& __x,
4961 typename iterator_traits<_BiIter>::value_type const* __y)
4962{
4963 return !(__x < __y);
4964}
4965
4966template <class _BiIter>
4967inline _LIBCPP_INLINE_VISIBILITY
4968bool
4969operator<=(const sub_match<_BiIter>& __x,
4970 typename iterator_traits<_BiIter>::value_type const* __y)
4971{
4972 return !(__y < __x);
4973}
4974
4975template <class _BiIter>
4976inline _LIBCPP_INLINE_VISIBILITY
4977bool
4978operator==(typename iterator_traits<_BiIter>::value_type const& __x,
4979 const sub_match<_BiIter>& __y)
4980{
4981 typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
4982 return __y.compare(string_type(1, __x)) == 0;
4983}
4984
4985template <class _BiIter>
4986inline _LIBCPP_INLINE_VISIBILITY
4987bool
4988operator!=(typename iterator_traits<_BiIter>::value_type const& __x,
4989 const sub_match<_BiIter>& __y)
4990{
4991 return !(__x == __y);
4992}
4993
4994template <class _BiIter>
4995inline _LIBCPP_INLINE_VISIBILITY
4996bool
4997operator<(typename iterator_traits<_BiIter>::value_type const& __x,
4998 const sub_match<_BiIter>& __y)
4999{
5000 typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
5001 return __y.compare(string_type(1, __x)) > 0;
5002}
5003
5004template <class _BiIter>
5005inline _LIBCPP_INLINE_VISIBILITY
5006bool
5007operator>(typename iterator_traits<_BiIter>::value_type const& __x,
5008 const sub_match<_BiIter>& __y)
5009{
5010 return __y < __x;
5011}
5012
5013template <class _BiIter>
5014inline _LIBCPP_INLINE_VISIBILITY
5015bool
5016operator>=(typename iterator_traits<_BiIter>::value_type const& __x,
5017 const sub_match<_BiIter>& __y)
5018{
5019 return !(__x < __y);
5020}
5021
5022template <class _BiIter>
5023inline _LIBCPP_INLINE_VISIBILITY
5024bool
5025operator<=(typename iterator_traits<_BiIter>::value_type const& __x,
5026 const sub_match<_BiIter>& __y)
5027{
5028 return !(__y < __x);
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 typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
5038 return __x.compare(string_type(1, __y)) == 0;
5039}
5040
5041template <class _BiIter>
5042inline _LIBCPP_INLINE_VISIBILITY
5043bool
5044operator!=(const sub_match<_BiIter>& __x,
5045 typename iterator_traits<_BiIter>::value_type const& __y)
5046{
5047 return !(__x == __y);
5048}
5049
5050template <class _BiIter>
5051inline _LIBCPP_INLINE_VISIBILITY
5052bool
5053operator<(const sub_match<_BiIter>& __x,
5054 typename iterator_traits<_BiIter>::value_type const& __y)
5055{
5056 typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
5057 return __x.compare(string_type(1, __y)) < 0;
5058}
5059
5060template <class _BiIter>
5061inline _LIBCPP_INLINE_VISIBILITY
5062bool
5063operator>(const sub_match<_BiIter>& __x,
5064 typename iterator_traits<_BiIter>::value_type const& __y)
5065{
5066 return __y < __x;
5067}
5068
5069template <class _BiIter>
5070inline _LIBCPP_INLINE_VISIBILITY
5071bool
5072operator>=(const sub_match<_BiIter>& __x,
5073 typename iterator_traits<_BiIter>::value_type const& __y)
5074{
5075 return !(__x < __y);
5076}
5077
5078template <class _BiIter>
5079inline _LIBCPP_INLINE_VISIBILITY
5080bool
5081operator<=(const sub_match<_BiIter>& __x,
5082 typename iterator_traits<_BiIter>::value_type const& __y)
5083{
5084 return !(__y < __x);
5085}
5086
5087template <class _CharT, class _ST, class _BiIter>
5088inline _LIBCPP_INLINE_VISIBILITY
5089basic_ostream<_CharT, _ST>&
5090operator<<(basic_ostream<_CharT, _ST>& __os, const sub_match<_BiIter>& __m)
5091{
5092 return __os << __m.str();
5093}
5094
Howard Hinnant17615b02010-07-27 01:25:38 +00005095template <class _BidirectionalIterator, class _Allocator>
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005096class match_results
5097{
5098public:
5099 typedef _Allocator allocator_type;
5100 typedef sub_match<_BidirectionalIterator> value_type;
5101private:
5102 typedef vector<value_type, allocator_type> __container_type;
5103
5104 __container_type __matches_;
5105 value_type __unmatched_;
5106 value_type __prefix_;
5107 value_type __suffix_;
5108public:
5109 typedef const value_type& const_reference;
5110 typedef const_reference reference;
5111 typedef typename __container_type::const_iterator const_iterator;
5112 typedef const_iterator iterator;
5113 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
5114 typedef typename allocator_traits<allocator_type>::size_type size_type;
5115 typedef typename iterator_traits<_BidirectionalIterator>::value_type char_type;
5116 typedef basic_string<char_type> string_type;
5117
5118 // construct/copy/destroy:
5119 explicit match_results(const allocator_type& __a = allocator_type());
5120// match_results(const match_results&) = default;
5121// match_results& operator=(const match_results&) = default;
5122#ifdef _LIBCPP_MOVE
5123// match_results(match_results&& __m) = default;
5124// match_results& operator=(match_results&& __m) = default;
5125#endif
5126// ~match_results() = default;
5127
5128 // size:
5129 size_type size() const {return __matches_.size();}
5130 size_type max_size() const {return __matches_.max_size();}
5131 bool empty() const {return size() == 0;}
5132
5133 // element access:
5134 difference_type length(size_type __sub = 0) const
5135 {return (*this)[__sub].length();}
5136 difference_type position(size_type __sub = 0) const
5137 {return _STD::distance(__prefix_.first, (*this)[__sub].first);}
5138 string_type str(size_type __sub = 0) const
5139 {return (*this)[__sub].str();}
5140 const_reference operator[](size_type __n) const
5141 {return __n < __matches_.size() ? __matches_[__n] : __unmatched_;}
5142
5143 const_reference prefix() const {return __prefix_;}
5144 const_reference suffix() const {return __suffix_;}
5145
5146 const_iterator begin() const {return empty() ? __matches_.end() : __matches_.begin() + 1;}
5147 const_iterator end() const {return __matches_.end();}
5148 const_iterator cbegin() const {return empty() ? __matches_.end() : __matches_.begin() + 1;}
5149 const_iterator cend() const {return __matches_.end();}
5150
5151 // format:
5152 template <class _OutputIter>
5153 _OutputIter
5154 format(_OutputIter __out, const char_type* __fmt_first,
5155 const char_type* __fmt_last,
5156 regex_constants::match_flag_type __flags = regex_constants::format_default) const;
5157 template <class _OutputIter, class _ST, class _SA>
5158 _OutputIter
5159 format(_OutputIter __out, const basic_string<char_type, _ST, _SA>& __fmt,
5160 regex_constants::match_flag_type __flags = regex_constants::format_default) const;
5161 template <class _ST, class _SA>
5162 basic_string<char_type, _ST, _SA>
5163 format(const basic_string<char_type, _ST, _SA>& __fmt,
5164 regex_constants::match_flag_type __flags = regex_constants::format_default) const;
5165 string_type
5166 format(const char_type* __fmt,
5167 regex_constants::match_flag_type __flags = regex_constants::format_default) const;
5168
5169 // allocator:
5170 allocator_type get_allocator() const {return __matches_.get_allocator();}
5171
5172 // swap:
5173 void swap(match_results& __m);
5174
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005175 template <class _B, class _A>
5176 void __assign(_BidirectionalIterator __f, _BidirectionalIterator __l,
5177 const match_results<_B, _A>& __m)
5178 {
5179 _B __mf = __m.prefix().first;
5180 __matches_.resize(__m.size());
5181 for (size_type __i = 0; __i < __matches_.size(); ++__i)
5182 {
5183 __matches_[__i].first = next(__f, _STD::distance(__mf, __m[__i].first));
5184 __matches_[__i].second = next(__f, _STD::distance(__mf, __m[__i].second));
5185 __matches_[__i].matched = __m[__i].matched;
5186 }
5187 __unmatched_.first = __l;
5188 __unmatched_.second = __l;
5189 __unmatched_.matched = false;
5190 __prefix_.first = next(__f, _STD::distance(__mf, __m.prefix().first));
5191 __prefix_.second = next(__f, _STD::distance(__mf, __m.prefix().second));
5192 __prefix_.matched = __m.prefix().matched;
5193 __suffix_.first = next(__f, _STD::distance(__mf, __m.suffix().first));
5194 __suffix_.second = next(__f, _STD::distance(__mf, __m.suffix().second));
5195 __suffix_.matched = __m.suffix().matched;
5196 }
5197
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005198private:
5199 void __init(unsigned __s,
5200 _BidirectionalIterator __f, _BidirectionalIterator __l);
5201
5202 template <class, class> friend class basic_regex;
5203
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005204 template <class _B, class _A, class _C, class _T>
5205 friend
5206 bool
5207 regex_match(_B, _B, match_results<_B, _A>&, const basic_regex<_C, _T>&,
5208 regex_constants::match_flag_type);
Howard Hinnante9de5ff2010-07-27 22:20:32 +00005209
5210 template <class, class> friend class __lookahead;
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005211};
5212
5213template <class _BidirectionalIterator, class _Allocator>
5214match_results<_BidirectionalIterator, _Allocator>::match_results(
5215 const allocator_type& __a)
5216 : __matches_(__a),
5217 __unmatched_(),
5218 __prefix_(),
5219 __suffix_()
5220{
5221}
5222
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005223template <class _BidirectionalIterator, class _Allocator>
5224void
5225match_results<_BidirectionalIterator, _Allocator>::__init(unsigned __s,
5226 _BidirectionalIterator __f, _BidirectionalIterator __l)
5227{
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005228 __unmatched_.first = __l;
5229 __unmatched_.second = __l;
5230 __unmatched_.matched = false;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005231 __matches_.assign(__s, __unmatched_);
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005232 __prefix_.first = __f;
5233 __prefix_.second = __f;
5234 __prefix_.matched = false;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005235 __suffix_ = __unmatched_;
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005236}
5237
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005238typedef match_results<const char*> cmatch;
5239typedef match_results<const wchar_t*> wcmatch;
5240typedef match_results<string::const_iterator> smatch;
5241typedef match_results<wstring::const_iterator> wsmatch;
5242
5243template <class _BidirectionalIterator, class _Allocator>
5244 bool
5245 operator==(const match_results<_BidirectionalIterator, _Allocator>& __x,
5246 const match_results<_BidirectionalIterator, _Allocator>& __y);
5247
5248template <class _BidirectionalIterator, class _Allocator>
5249 bool
5250 operator!=(const match_results<_BidirectionalIterator, _Allocator>& __x,
5251 const match_results<_BidirectionalIterator, _Allocator>& __y);
5252
5253template <class _BidirectionalIterator, class _Allocator>
5254 void
5255 swap(match_results<_BidirectionalIterator, _Allocator>& __x,
5256 match_results<_BidirectionalIterator, _Allocator>& __y);
5257
5258// regex_search
5259
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005260template <class _CharT, class _Traits>
Howard Hinnant17615b02010-07-27 01:25:38 +00005261template <class _Allocator>
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005262bool
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005263basic_regex<_CharT, _Traits>::__match_at_start_ecma(
Howard Hinnant17615b02010-07-27 01:25:38 +00005264 const _CharT* __first, const _CharT* __last,
5265 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005266 regex_constants::match_flag_type __flags) const
5267{
Howard Hinnant17615b02010-07-27 01:25:38 +00005268 vector<__state> __states;
5269 ptrdiff_t __j = 0;
5270 ptrdiff_t _N = _STD::distance(__first, __last);
5271 __node* __st = __start_.get();
5272 if (__st)
5273 {
5274 __states.push_back(__state());
5275 __states.back().__do_ = 0;
5276 __states.back().__first_ = __first;
5277 __states.back().__current_ = __first;
5278 __states.back().__last_ = __last;
5279 __states.back().__sub_matches_.resize(mark_count());
5280 __states.back().__loop_data_.resize(__loop_count());
5281 __states.back().__node_ = __st;
5282 __states.back().__flags_ = __flags;
5283 bool __matched = false;
5284 do
5285 {
5286 __state& __s = __states.back();
5287 if (__s.__node_)
5288 __s.__node_->__exec(__s);
5289 switch (__s.__do_)
5290 {
5291 case __state::__end_state:
5292 __m.__matches_[0].first = __first;
5293 __m.__matches_[0].second = _STD::next(__first, __s.__current_ - __first);
5294 __m.__matches_[0].matched = true;
5295 for (unsigned __i = 0; __i < __s.__sub_matches_.size(); ++__i)
5296 __m.__matches_[__i+1] = __s.__sub_matches_[__i];
5297 return true;
5298 case __state::__accept_and_consume:
5299 case __state::__repeat:
5300 case __state::__accept_but_not_consume:
5301 break;
5302 case __state::__split:
5303 {
5304 __state __snext = __s;
5305 __s.__node_->__exec_split(true, __s);
5306 __snext.__node_->__exec_split(false, __snext);
5307 __states.push_back(_STD::move(__snext));
5308 }
5309 break;
5310 case __state::__reject:
5311 __states.pop_back();
5312 break;
5313 default:
Howard Hinnantd4444702010-08-11 17:04:31 +00005314#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005315 throw regex_error(regex_constants::__re_err_unknown);
Howard Hinnantd4444702010-08-11 17:04:31 +00005316#endif
Howard Hinnant17615b02010-07-27 01:25:38 +00005317 break;
Howard Hinnantd4444702010-08-11 17:04:31 +00005318
Howard Hinnant17615b02010-07-27 01:25:38 +00005319 }
5320 } while (!__states.empty());
5321 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005322 return false;
5323}
5324
5325template <class _CharT, class _Traits>
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005326template <class _Allocator>
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005327bool
5328basic_regex<_CharT, _Traits>::__match_at_start_posix_nosubs(
5329 const _CharT* __first, const _CharT* __last,
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005330 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005331 regex_constants::match_flag_type __flags) const
5332{
Howard Hinnantac303862010-07-12 15:51:17 +00005333 deque<__state> __states;
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005334 ptrdiff_t __highest_j = 0;
5335 ptrdiff_t _N = _STD::distance(__first, __last);
Howard Hinnantac303862010-07-12 15:51:17 +00005336 __node* __st = __start_.get();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005337 if (__st)
5338 {
Howard Hinnantac303862010-07-12 15:51:17 +00005339 __states.push_back(__state());
Howard Hinnantac303862010-07-12 15:51:17 +00005340 __states.back().__do_ = 0;
5341 __states.back().__first_ = __first;
5342 __states.back().__current_ = __first;
5343 __states.back().__last_ = __last;
5344 __states.back().__loop_data_.resize(__loop_count());
5345 __states.back().__node_ = __st;
5346 __states.back().__flags_ = __flags;
Howard Hinnant68025ed2010-07-14 15:45:11 +00005347 bool __matched = false;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005348 do
5349 {
Howard Hinnantac303862010-07-12 15:51:17 +00005350 __state& __s = __states.back();
5351 if (__s.__node_)
5352 __s.__node_->__exec(__s);
5353 switch (__s.__do_)
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005354 {
Howard Hinnantac303862010-07-12 15:51:17 +00005355 case __state::__end_state:
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005356 if (!__matched || __highest_j < __s.__current_ - __s.__first_)
Howard Hinnant68025ed2010-07-14 15:45:11 +00005357 __highest_j = __s.__current_ - __s.__first_;
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005358 __matched = true;
Howard Hinnantac303862010-07-12 15:51:17 +00005359 if (__highest_j == _N)
5360 __states.clear();
5361 else
5362 __states.pop_back();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005363 break;
Howard Hinnantac303862010-07-12 15:51:17 +00005364 case __state::__consume_input:
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005365 break;
Howard Hinnantac303862010-07-12 15:51:17 +00005366 case __state::__accept_and_consume:
Howard Hinnantac303862010-07-12 15:51:17 +00005367 __states.push_front(_STD::move(__s));
5368 __states.pop_back();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005369 break;
Howard Hinnantac303862010-07-12 15:51:17 +00005370 case __state::__repeat:
5371 case __state::__accept_but_not_consume:
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005372 break;
Howard Hinnantac303862010-07-12 15:51:17 +00005373 case __state::__split:
5374 {
5375 __state __snext = __s;
5376 __s.__node_->__exec_split(true, __s);
5377 __snext.__node_->__exec_split(false, __snext);
5378 __states.push_back(_STD::move(__snext));
5379 }
5380 break;
5381 case __state::__reject:
5382 __states.pop_back();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005383 break;
5384 default:
Howard Hinnantd4444702010-08-11 17:04:31 +00005385#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005386 throw regex_error(regex_constants::__re_err_unknown);
Howard Hinnantd4444702010-08-11 17:04:31 +00005387#endif
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005388 break;
5389 }
Howard Hinnantac303862010-07-12 15:51:17 +00005390 } while (!__states.empty());
Howard Hinnant68025ed2010-07-14 15:45:11 +00005391 if (__matched)
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005392 {
5393 __m.__matches_[0].first = __first;
5394 __m.__matches_[0].second = _STD::next(__first, __highest_j);
5395 __m.__matches_[0].matched = true;
5396 return true;
5397 }
5398 }
5399 return false;
5400}
5401
5402template <class _CharT, class _Traits>
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005403template <class _Allocator>
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005404bool
5405basic_regex<_CharT, _Traits>::__match_at_start_posix_subs(
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005406 const _CharT* __first, const _CharT* __last,
5407 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005408 regex_constants::match_flag_type __flags) const
5409{
Howard Hinnantac303862010-07-12 15:51:17 +00005410 vector<__state> __states;
Howard Hinnantac303862010-07-12 15:51:17 +00005411 __state __best_state;
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005412 ptrdiff_t __j = 0;
5413 ptrdiff_t __highest_j = 0;
5414 ptrdiff_t _N = _STD::distance(__first, __last);
Howard Hinnantac303862010-07-12 15:51:17 +00005415 __node* __st = __start_.get();
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005416 if (__st)
5417 {
Howard Hinnantac303862010-07-12 15:51:17 +00005418 __states.push_back(__state());
5419 __states.back().__do_ = 0;
5420 __states.back().__first_ = __first;
5421 __states.back().__current_ = __first;
5422 __states.back().__last_ = __last;
5423 __states.back().__sub_matches_.resize(mark_count());
5424 __states.back().__loop_data_.resize(__loop_count());
5425 __states.back().__node_ = __st;
5426 __states.back().__flags_ = __flags;
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005427 const _CharT* __current = __first;
Howard Hinnantac303862010-07-12 15:51:17 +00005428 bool __matched = false;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005429 do
5430 {
Howard Hinnantac303862010-07-12 15:51:17 +00005431 __state& __s = __states.back();
5432 if (__s.__node_)
5433 __s.__node_->__exec(__s);
5434 switch (__s.__do_)
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005435 {
Howard Hinnantac303862010-07-12 15:51:17 +00005436 case __state::__end_state:
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005437 if (!__matched || __highest_j < __s.__current_ - __s.__first_)
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005438 {
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005439 __highest_j = __s.__current_ - __s.__first_;
Howard Hinnantac303862010-07-12 15:51:17 +00005440 __best_state = __s;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005441 }
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005442 __matched = true;
5443 if (__highest_j == _N)
5444 __states.clear();
5445 else
5446 __states.pop_back();
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005447 break;
Howard Hinnantac303862010-07-12 15:51:17 +00005448 case __state::__accept_and_consume:
Howard Hinnantcba352d2010-07-12 18:16:05 +00005449 __j += __s.__current_ - __current;
5450 __current = __s.__current_;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005451 break;
Howard Hinnantac303862010-07-12 15:51:17 +00005452 case __state::__repeat:
5453 case __state::__accept_but_not_consume:
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005454 break;
Howard Hinnantac303862010-07-12 15:51:17 +00005455 case __state::__split:
5456 {
5457 __state __snext = __s;
5458 __s.__node_->__exec_split(true, __s);
5459 __snext.__node_->__exec_split(false, __snext);
5460 __states.push_back(_STD::move(__snext));
5461 }
5462 break;
5463 case __state::__reject:
5464 __states.pop_back();
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005465 break;
5466 default:
Howard Hinnantd4444702010-08-11 17:04:31 +00005467#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005468 throw regex_error(regex_constants::__re_err_unknown);
Howard Hinnantd4444702010-08-11 17:04:31 +00005469#endif
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005470 break;
5471 }
Howard Hinnantac303862010-07-12 15:51:17 +00005472 } while (!__states.empty());
5473 if (__matched)
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005474 {
5475 __m.__matches_[0].first = __first;
5476 __m.__matches_[0].second = _STD::next(__first, __highest_j);
5477 __m.__matches_[0].matched = true;
Howard Hinnantac303862010-07-12 15:51:17 +00005478 for (unsigned __i = 0; __i < __best_state.__sub_matches_.size(); ++__i)
5479 __m.__matches_[__i+1] = __best_state.__sub_matches_[__i];
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005480 return true;
5481 }
5482 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005483 return false;
5484}
5485
5486template <class _CharT, class _Traits>
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005487template <class _Allocator>
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005488bool
5489basic_regex<_CharT, _Traits>::__match_at_start(
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005490 const _CharT* __first, const _CharT* __last,
5491 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005492 regex_constants::match_flag_type __flags) const
5493{
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005494 if ((__flags_ & 0x1F0) == ECMAScript)
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005495 return __match_at_start_ecma(__first, __last, __m, __flags);
5496 if (mark_count() == 0)
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005497 return __match_at_start_posix_nosubs(__first, __last, __m, __flags);
5498 return __match_at_start_posix_subs(__first, __last, __m, __flags);
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005499}
5500
5501template <class _CharT, class _Traits>
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005502template <class _Allocator>
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005503bool
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005504basic_regex<_CharT, _Traits>::__search(
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005505 const _CharT* __first, const _CharT* __last,
5506 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005507 regex_constants::match_flag_type __flags) const
5508{
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00005509 if (__left_anchor_)
5510 __flags |= regex_constants::match_continuous;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005511 __m.__init(1 + mark_count(), __first, __last);
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005512 if (__match_at_start(__first, __last, __m, __flags))
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005513 {
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005514 __m.__prefix_.second = __m[0].first;
5515 __m.__prefix_.matched = __m.__prefix_.first != __m.__prefix_.second;
5516 __m.__suffix_.first = __m[0].second;
5517 __m.__suffix_.matched = __m.__suffix_.first != __m.__suffix_.second;
5518 return true;
5519 }
Howard Hinnant8daa7332010-07-29 01:15:27 +00005520 if (__first != __last && !(__flags & regex_constants::match_continuous))
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005521 {
Howard Hinnantf3dcca02010-07-29 15:17:28 +00005522 __flags |= regex_constants::match_prev_avail;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005523 for (++__first; __first != __last; ++__first)
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005524 {
Howard Hinnantf3dcca02010-07-29 15:17:28 +00005525 __m.__matches_.assign(__m.size(), __m.__unmatched_);
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005526 if (__match_at_start(__first, __last, __m, __flags))
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005527 {
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005528 __m.__prefix_.second = __m[0].first;
5529 __m.__prefix_.matched = __m.__prefix_.first != __m.__prefix_.second;
5530 __m.__suffix_.first = __m[0].second;
5531 __m.__suffix_.matched = __m.__suffix_.first != __m.__suffix_.second;
5532 return true;
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005533 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005534 __m.__matches_.assign(__m.size(), __m.__unmatched_);
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005535 }
5536 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005537 __m.__matches_.clear();
5538 return false;
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005539}
5540
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005541template <class _BidirectionalIterator, class _Allocator, class _CharT, class _Traits>
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005542inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005543bool
5544regex_search(_BidirectionalIterator __first, _BidirectionalIterator __last,
5545 match_results<_BidirectionalIterator, _Allocator>& __m,
5546 const basic_regex<_CharT, _Traits>& __e,
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005547 regex_constants::match_flag_type __flags = regex_constants::match_default)
5548{
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005549 basic_string<_CharT> __s(__first, __last);
5550 match_results<const _CharT*> __mc;
5551 bool __r = __e.__search(__s.data(), __s.data() + __s.size(), __mc, __flags);
5552 __m.__assign(__first, __last, __mc);
5553 return __r;
5554}
5555
5556template <class _Allocator, class _CharT, class _Traits>
5557inline _LIBCPP_INLINE_VISIBILITY
5558bool
5559regex_search(const _CharT* __first, const _CharT* __last,
5560 match_results<const _CharT*, _Allocator>& __m,
5561 const basic_regex<_CharT, _Traits>& __e,
5562 regex_constants::match_flag_type __flags = regex_constants::match_default)
5563{
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005564 return __e.__search(__first, __last, __m, __flags);
5565}
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005566
5567template <class _BidirectionalIterator, class _CharT, class _Traits>
5568inline _LIBCPP_INLINE_VISIBILITY
5569bool
5570regex_search(_BidirectionalIterator __first, _BidirectionalIterator __last,
5571 const basic_regex<_CharT, _Traits>& __e,
5572 regex_constants::match_flag_type __flags = regex_constants::match_default)
5573{
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005574 basic_string<_CharT> __s(__first, __last);
5575 match_results<const _CharT*> __mc;
5576 return __e.__search(__s.data(), __s.data() + __s.size(), __mc, __flags);
5577}
5578
5579template <class _CharT, class _Traits>
5580inline _LIBCPP_INLINE_VISIBILITY
5581bool
5582regex_search(const _CharT* __first, const _CharT* __last,
5583 const basic_regex<_CharT, _Traits>& __e,
5584 regex_constants::match_flag_type __flags = regex_constants::match_default)
5585{
5586 match_results<const _CharT*> __mc;
5587 return __e.__search(__first, __last, __mc, __flags);
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005588}
5589
5590template <class _CharT, class _Allocator, class _Traits>
5591inline _LIBCPP_INLINE_VISIBILITY
5592bool
5593regex_search(const _CharT* __str, match_results<const _CharT*, _Allocator>& __m,
5594 const basic_regex<_CharT, _Traits>& __e,
5595 regex_constants::match_flag_type __flags = regex_constants::match_default)
5596{
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005597 return __e.__search(__str, __str + _Traits::length(__str), __m, __flags);
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005598}
5599
5600template <class _CharT, class _Traits>
5601inline _LIBCPP_INLINE_VISIBILITY
5602bool
5603regex_search(const _CharT* __str, const basic_regex<_CharT, _Traits>& __e,
5604 regex_constants::match_flag_type __flags = regex_constants::match_default)
5605{
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005606 match_results<const _CharT*> __m;
5607 return _STD::regex_search(__str, __m, __e, __flags);
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005608}
5609
5610template <class _ST, class _SA, class _CharT, class _Traits>
5611inline _LIBCPP_INLINE_VISIBILITY
5612bool
5613regex_search(const basic_string<_CharT, _ST, _SA>& __s,
5614 const basic_regex<_CharT, _Traits>& __e,
5615 regex_constants::match_flag_type __flags = regex_constants::match_default)
5616{
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005617 match_results<const _CharT*> __mc;
5618 return __e.__search(__s.data(), __s.data() + __s.size(), __mc, __flags);
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005619}
5620
5621template <class _ST, class _SA, class _Allocator, class _CharT, class _Traits>
5622inline _LIBCPP_INLINE_VISIBILITY
5623bool
5624regex_search(const basic_string<_CharT, _ST, _SA>& __s,
5625 match_results<typename basic_string<_CharT, _ST, _SA>::const_iterator, _Allocator>& __m,
5626 const basic_regex<_CharT, _Traits>& __e,
5627 regex_constants::match_flag_type __flags = regex_constants::match_default)
5628{
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005629 match_results<const _CharT*> __mc;
5630 bool __r = __e.__search(__s.data(), __s.data() + __s.size(), __mc, __flags);
5631 __m.__assign(__s.begin(), __s.end(), __mc);
5632 return __r;
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005633}
5634
5635// regex_match
5636
5637template <class _BidirectionalIterator, class _Allocator, class _CharT, class _Traits>
5638bool
5639regex_match(_BidirectionalIterator __first, _BidirectionalIterator __last,
5640 match_results<_BidirectionalIterator, _Allocator>& __m,
5641 const basic_regex<_CharT, _Traits>& __e,
5642 regex_constants::match_flag_type __flags = regex_constants::match_default)
5643{
5644 bool __r = _STD::regex_search(__first, __last, __m, __e,
5645 __flags | regex_constants::match_continuous);
5646 if (__r)
5647 {
5648 __r = !__m.suffix().matched;
5649 if (!__r)
5650 __m.__matches_.clear();
5651 }
5652 return __r;
5653}
5654
5655template <class _BidirectionalIterator, class _CharT, class _Traits>
5656inline _LIBCPP_INLINE_VISIBILITY
5657bool
5658regex_match(_BidirectionalIterator __first, _BidirectionalIterator __last,
5659 const basic_regex<_CharT, _Traits>& __e,
5660 regex_constants::match_flag_type __flags = regex_constants::match_default)
5661{
5662 match_results<_BidirectionalIterator> __m;
5663 return _STD::regex_match(__first, __last, __m, __e, __flags);
5664}
5665
5666template <class _CharT, class _Allocator, class _Traits>
5667inline _LIBCPP_INLINE_VISIBILITY
5668bool
5669regex_match(const _CharT* __str, match_results<const _CharT*, _Allocator>& __m,
5670 const basic_regex<_CharT, _Traits>& __e,
5671 regex_constants::match_flag_type __flags = regex_constants::match_default)
5672{
5673 return _STD::regex_match(__str, __str + _Traits::length(__str), __m, __e, __flags);
5674}
5675
5676template <class _ST, class _SA, class _Allocator, class _CharT, class _Traits>
5677inline _LIBCPP_INLINE_VISIBILITY
5678bool
5679regex_match(const basic_string<_CharT, _ST, _SA>& __s,
5680 match_results<typename basic_string<_CharT, _ST, _SA>::const_iterator, _Allocator>& __m,
5681 const basic_regex<_CharT, _Traits>& __e,
5682 regex_constants::match_flag_type __flags = regex_constants::match_default)
5683{
5684 return _STD::regex_match(__s.begin(), __s.end(), __m, __e, __flags);
5685}
5686
5687template <class _CharT, class _Traits>
5688inline _LIBCPP_INLINE_VISIBILITY
5689bool
5690regex_match(const _CharT* __str, const basic_regex<_CharT, _Traits>& __e,
5691 regex_constants::match_flag_type __flags = regex_constants::match_default)
5692{
5693 return _STD::regex_match(__str, __str + _Traits::length(__str), __e, __flags);
5694}
5695
5696template <class _ST, class _SA, class _CharT, class _Traits>
5697inline _LIBCPP_INLINE_VISIBILITY
5698bool
5699regex_match(const basic_string<_CharT, _ST, _SA>& __s,
5700 const basic_regex<_CharT, _Traits>& __e,
5701 regex_constants::match_flag_type __flags = regex_constants::match_default)
5702{
5703 return _STD::regex_match(__s.begin(), __s.end(), __e, __flags);
5704}
5705
Howard Hinnant3257c982010-06-17 00:34:59 +00005706_LIBCPP_END_NAMESPACE_STD
5707
5708#endif // _LIBCPP_REGEX