blob: 14a0e4bcc0d4ee392678c208d3cfedec610805bd [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;
1886 if (__invert_)
1887 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 }
1910 else if (__s.__current_ == __s.__first_)
1911 {
1912 if (!(__s.__flags_ & regex_constants::match_not_bow))
1913 {
1914 _CharT __c = *__s.__current_;
1915 __is_word_b = __c == '_' ||
1916 __traits_.isctype(__c, ctype_base::alnum);
1917 }
1918 }
1919 else
1920 {
1921 _CharT __c1 = __s.__current_[-1];
1922 _CharT __c2 = *__s.__current_;
1923 bool __is_c1_b = __c1 == '_' ||
1924 __traits_.isctype(__c1, ctype_base::alnum);
1925 bool __is_c2_b = __c2 == '_' ||
1926 __traits_.isctype(__c2, ctype_base::alnum);
1927 __is_word_b = __is_c1_b != __is_c2_b;
1928 }
1929 }
1930 if (__is_word_b != __invert_)
1931 {
1932 __s.__do_ = __state::__accept_but_not_consume;
1933 __s.__node_ = this->first();
1934 }
1935 else
1936 {
1937 __s.__do_ = __state::__reject;
1938 __s.__node_ = nullptr;
1939 }
1940}
1941
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001942// __r_anchor
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001943
1944template <class _CharT>
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001945class __r_anchor
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001946 : public __owns_one_state<_CharT>
1947{
1948 typedef __owns_one_state<_CharT> base;
1949
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001950public:
Howard Hinnantac303862010-07-12 15:51:17 +00001951 typedef _STD::__state<_CharT> __state;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001952
Howard Hinnantac303862010-07-12 15:51:17 +00001953 __r_anchor(__node<_CharT>* __s)
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001954 : base(__s) {}
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001955
Howard Hinnantac303862010-07-12 15:51:17 +00001956 virtual void __exec(__state&) const;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001957
1958 virtual string speak() const
1959 {
1960 ostringstream os;
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00001961 os << "right anchor";
Howard Hinnante77aa5e2010-07-08 17:43:58 +00001962 return os.str();
1963 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001964};
1965
1966template <class _CharT>
Howard Hinnantac303862010-07-12 15:51:17 +00001967void
1968__r_anchor<_CharT>::__exec(__state& __s) const
Howard Hinnantf8ce4592010-07-07 19:14:52 +00001969{
Howard Hinnantac303862010-07-12 15:51:17 +00001970 if (__s.__current_ == __s.__last_)
1971 {
1972 __s.__do_ = __state::__accept_but_not_consume;
1973 __s.__node_ = this->first();
1974 }
1975 else
1976 {
1977 __s.__do_ = __state::__reject;
1978 __s.__node_ = nullptr;
1979 }
1980}
1981
1982// __match_any
1983
1984template <class _CharT>
1985class __match_any
1986 : public __owns_one_state<_CharT>
1987{
1988 typedef __owns_one_state<_CharT> base;
1989
1990public:
1991 typedef _STD::__state<_CharT> __state;
1992
1993 __match_any(__node<_CharT>* __s)
1994 : base(__s) {}
1995
1996 virtual void __exec(__state&) const;
1997
1998 virtual string speak() const
1999 {
2000 ostringstream os;
2001 os << "match any";
2002 return os.str();
2003 }
2004};
2005
2006template <class _CharT>
2007void
2008__match_any<_CharT>::__exec(__state& __s) const
2009{
2010 if (__s.__current_ != __s.__last_ && *__s.__current_ != 0)
2011 {
2012 __s.__do_ = __state::__accept_and_consume;
2013 ++__s.__current_;
2014 __s.__node_ = this->first();
2015 }
2016 else
2017 {
2018 __s.__do_ = __state::__reject;
2019 __s.__node_ = nullptr;
2020 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002021}
2022
Howard Hinnant17615b02010-07-27 01:25:38 +00002023// __match_any_but_newline
2024
2025template <class _CharT>
2026class __match_any_but_newline
2027 : public __owns_one_state<_CharT>
2028{
2029 typedef __owns_one_state<_CharT> base;
2030
2031public:
2032 typedef _STD::__state<_CharT> __state;
2033
2034 __match_any_but_newline(__node<_CharT>* __s)
2035 : base(__s) {}
2036
2037 virtual void __exec(__state&) const;
2038
2039 virtual string speak() const
2040 {
2041 ostringstream os;
2042 os << "match any but newline";
2043 return os.str();
2044 }
2045};
2046
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002047// __match_char
2048
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00002049template <class _CharT>
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002050class __match_char
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002051 : public __owns_one_state<_CharT>
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002052{
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002053 typedef __owns_one_state<_CharT> base;
2054
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002055 _CharT __c_;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002056
2057 __match_char(const __match_char&);
2058 __match_char& operator=(const __match_char&);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002059public:
Howard Hinnantac303862010-07-12 15:51:17 +00002060 typedef _STD::__state<_CharT> __state;
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00002061
Howard Hinnantac303862010-07-12 15:51:17 +00002062 __match_char(_CharT __c, __node<_CharT>* __s)
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002063 : base(__s), __c_(__c) {}
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002064
Howard Hinnantac303862010-07-12 15:51:17 +00002065 virtual void __exec(__state&) const;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00002066
2067 virtual string speak() const
2068 {
2069 ostringstream os;
2070 os << "match char " << __c_;
2071 return os.str();
2072 }
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002073};
2074
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00002075template <class _CharT>
Howard Hinnantac303862010-07-12 15:51:17 +00002076void
2077__match_char<_CharT>::__exec(__state& __s) const
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00002078{
Howard Hinnantac303862010-07-12 15:51:17 +00002079 if (__s.__current_ != __s.__last_ && *__s.__current_ == __c_)
2080 {
2081 __s.__do_ = __state::__accept_and_consume;
2082 ++__s.__current_;
2083 __s.__node_ = this->first();
2084 }
2085 else
2086 {
2087 __s.__do_ = __state::__reject;
2088 __s.__node_ = nullptr;
2089 }
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00002090}
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00002091
Howard Hinnante34f17d2010-07-12 19:11:27 +00002092// __match_char_icase
2093
2094template <class _CharT, class _Traits>
2095class __match_char_icase
2096 : public __owns_one_state<_CharT>
2097{
2098 typedef __owns_one_state<_CharT> base;
2099
2100 _Traits __traits_;
2101 _CharT __c_;
2102
2103 __match_char_icase(const __match_char_icase&);
2104 __match_char_icase& operator=(const __match_char_icase&);
2105public:
2106 typedef _STD::__state<_CharT> __state;
2107
2108 __match_char_icase(const _Traits& __traits, _CharT __c, __node<_CharT>* __s)
2109 : base(__s), __traits_(__traits), __c_(__traits.translate_nocase(__c)) {}
2110
2111 virtual void __exec(__state&) const;
2112
2113 virtual string speak() const
2114 {
2115 ostringstream os;
2116 os << "match char icase " << __c_;
2117 return os.str();
2118 }
2119};
2120
2121template <class _CharT, class _Traits>
2122void
2123__match_char_icase<_CharT, _Traits>::__exec(__state& __s) const
2124{
2125 if (__s.__current_ != __s.__last_ &&
2126 __traits_.translate_nocase(*__s.__current_) == __c_)
2127 {
2128 __s.__do_ = __state::__accept_and_consume;
2129 ++__s.__current_;
2130 __s.__node_ = this->first();
2131 }
2132 else
2133 {
2134 __s.__do_ = __state::__reject;
2135 __s.__node_ = nullptr;
2136 }
2137}
2138
2139// __match_char_collate
2140
2141template <class _CharT, class _Traits>
2142class __match_char_collate
2143 : public __owns_one_state<_CharT>
2144{
2145 typedef __owns_one_state<_CharT> base;
2146
2147 _Traits __traits_;
2148 _CharT __c_;
2149
2150 __match_char_collate(const __match_char_collate&);
2151 __match_char_collate& operator=(const __match_char_collate&);
2152public:
2153 typedef _STD::__state<_CharT> __state;
2154
2155 __match_char_collate(const _Traits& __traits, _CharT __c, __node<_CharT>* __s)
2156 : base(__s), __traits_(__traits), __c_(__traits.translate(__c)) {}
2157
2158 virtual void __exec(__state&) const;
2159
2160 virtual string speak() const
2161 {
2162 ostringstream os;
2163 os << "match char icase " << __c_;
2164 return os.str();
2165 }
2166};
2167
2168template <class _CharT, class _Traits>
2169void
2170__match_char_collate<_CharT, _Traits>::__exec(__state& __s) const
2171{
2172 if (__s.__current_ != __s.__last_ &&
2173 __traits_.translate(*__s.__current_) == __c_)
2174 {
2175 __s.__do_ = __state::__accept_and_consume;
2176 ++__s.__current_;
2177 __s.__node_ = this->first();
2178 }
2179 else
2180 {
2181 __s.__do_ = __state::__reject;
2182 __s.__node_ = nullptr;
2183 }
2184}
2185
Howard Hinnant173968a2010-07-13 21:48:06 +00002186// __bracket_expression
2187
2188template <class _CharT, class _Traits>
2189class __bracket_expression
2190 : public __owns_one_state<_CharT>
2191{
2192 typedef __owns_one_state<_CharT> base;
2193 typedef typename _Traits::string_type string_type;
2194
2195 _Traits __traits_;
2196 vector<_CharT> __chars_;
2197 vector<pair<string_type, string_type> > __ranges_;
2198 vector<pair<_CharT, _CharT> > __digraphs_;
2199 vector<string_type> __equivalences_;
2200 ctype_base::mask __mask_;
2201 bool __negate_;
2202 bool __icase_;
2203 bool __collate_;
Howard Hinnant68025ed2010-07-14 15:45:11 +00002204 bool __might_have_digraph_;
Howard Hinnant173968a2010-07-13 21:48:06 +00002205
2206 __bracket_expression(const __bracket_expression&);
2207 __bracket_expression& operator=(const __bracket_expression&);
2208public:
2209 typedef _STD::__state<_CharT> __state;
2210
2211 __bracket_expression(const _Traits& __traits, __node<_CharT>* __s,
2212 bool __negate, bool __icase, bool __collate)
2213 : base(__s), __traits_(__traits), __mask_(), __negate_(__negate),
Howard Hinnant68025ed2010-07-14 15:45:11 +00002214 __icase_(__icase), __collate_(__collate),
2215 __might_have_digraph_(__traits_.getloc().name() != "C") {}
Howard Hinnant173968a2010-07-13 21:48:06 +00002216
2217 virtual void __exec(__state&) const;
2218
2219 void __add_char(_CharT __c)
2220 {
2221 if (__icase_)
2222 __chars_.push_back(__traits_.translate_nocase(__c));
2223 else if (__collate_)
2224 __chars_.push_back(__traits_.translate(__c));
2225 else
2226 __chars_.push_back(__c);
2227 }
2228 void __add_range(string_type __b, string_type __e)
2229 {
2230 if (__collate_)
2231 {
2232 if (__icase_)
2233 {
2234 for (size_t __i = 0; __i < __b.size(); ++__i)
2235 __b[__i] = __traits_.translate_nocase(__b[__i]);
2236 for (size_t __i = 0; __i < __e.size(); ++__i)
2237 __e[__i] = __traits_.translate_nocase(__e[__i]);
2238 }
2239 else
2240 {
2241 for (size_t __i = 0; __i < __b.size(); ++__i)
2242 __b[__i] = __traits_.translate(__b[__i]);
2243 for (size_t __i = 0; __i < __e.size(); ++__i)
2244 __e[__i] = __traits_.translate(__e[__i]);
2245 }
2246 __ranges_.push_back(make_pair(
2247 __traits_.transform(__b.begin(), __b.end()),
2248 __traits_.transform(__e.begin(), __e.end())));
2249 }
2250 else
2251 {
2252 if (__b.size() != 1 || __e.size() != 1)
2253 throw regex_error(regex_constants::error_collate);
2254 if (__icase_)
2255 {
2256 __b[0] = __traits_.translate_nocase(__b[0]);
2257 __e[0] = __traits_.translate_nocase(__e[0]);
2258 }
2259 __ranges_.push_back(make_pair(_STD::move(__b), _STD::move(__e)));
2260 }
2261 }
2262 void __add_digraph(_CharT __c1, _CharT __c2)
2263 {
2264 if (__icase_)
2265 __digraphs_.push_back(make_pair(__traits_.translate_nocase(__c1),
2266 __traits_.translate_nocase(__c2)));
2267 else if (__collate_)
2268 __digraphs_.push_back(make_pair(__traits_.translate(__c1),
2269 __traits_.translate(__c2)));
2270 else
2271 __digraphs_.push_back(make_pair(__c1, __c2));
2272 }
2273 void __add_equivalence(const string_type& __s)
2274 {__equivalences_.push_back(__s);}
2275 void __add_class(ctype_base::mask __mask)
2276 {__mask_ |= __mask;}
2277
2278 virtual string speak() const
2279 {
2280 ostringstream os;
2281 os << "__bracket_expression ";
2282 return os.str();
2283 }
2284};
2285
2286template <class _CharT, class _Traits>
2287void
2288__bracket_expression<_CharT, _Traits>::__exec(__state& __s) const
2289{
2290 bool __found = false;
2291 unsigned __consumed = 0;
2292 if (__s.__current_ != __s.__last_)
2293 {
2294 ++__consumed;
Howard Hinnant68025ed2010-07-14 15:45:11 +00002295 if (__might_have_digraph_)
Howard Hinnant173968a2010-07-13 21:48:06 +00002296 {
Howard Hinnant68025ed2010-07-14 15:45:11 +00002297 const _CharT* __next = next(__s.__current_);
2298 if (__next != __s.__last_)
Howard Hinnant173968a2010-07-13 21:48:06 +00002299 {
Howard Hinnant68025ed2010-07-14 15:45:11 +00002300 pair<_CharT, _CharT> __ch2(*__s.__current_, *__next);
2301 if (__icase_)
Howard Hinnant173968a2010-07-13 21:48:06 +00002302 {
Howard Hinnant68025ed2010-07-14 15:45:11 +00002303 __ch2.first = __traits_.translate_nocase(__ch2.first);
2304 __ch2.second = __traits_.translate_nocase(__ch2.second);
2305 }
2306 else if (__collate_)
2307 {
2308 __ch2.first = __traits_.translate(__ch2.first);
2309 __ch2.second = __traits_.translate(__ch2.second);
2310 }
2311 if (!__traits_.lookup_collatename(&__ch2.first, &__ch2.first+2).empty())
2312 {
2313 // __ch2 is a digraph in this locale
2314 ++__consumed;
2315 for (size_t __i = 0; __i < __digraphs_.size(); ++__i)
2316 {
2317 if (__ch2 == __digraphs_[__i])
2318 {
2319 __found = true;
2320 goto __exit;
2321 }
2322 }
2323 if (__collate_ && !__ranges_.empty())
2324 {
2325 string_type __s2 = __traits_.transform(&__ch2.first,
2326 &__ch2.first + 2);
2327 for (size_t __i = 0; __i < __ranges_.size(); ++__i)
2328 {
2329 if (__ranges_[__i].first <= __s2 &&
2330 __s2 <= __ranges_[__i].second)
2331 {
2332 __found = true;
2333 goto __exit;
2334 }
2335 }
2336 }
2337 if (!__equivalences_.empty())
2338 {
2339 string_type __s2 = __traits_.transform_primary(&__ch2.first,
2340 &__ch2.first + 2);
2341 for (size_t __i = 0; __i < __equivalences_.size(); ++__i)
2342 {
2343 if (__s2 == __equivalences_[__i])
2344 {
2345 __found = true;
2346 goto __exit;
2347 }
2348 }
2349 }
2350 if (__traits_.isctype(__ch2.first, __mask_) &&
2351 __traits_.isctype(__ch2.second, __mask_))
Howard Hinnant173968a2010-07-13 21:48:06 +00002352 {
2353 __found = true;
2354 goto __exit;
2355 }
Howard Hinnant173968a2010-07-13 21:48:06 +00002356 goto __exit;
2357 }
Howard Hinnant173968a2010-07-13 21:48:06 +00002358 }
2359 }
2360 // test *__s.__current_ as not a digraph
2361 _CharT __ch = *__s.__current_;
2362 if (__icase_)
2363 __ch = __traits_.translate_nocase(__ch);
2364 else if (__collate_)
2365 __ch = __traits_.translate(__ch);
2366 for (size_t __i = 0; __i < __chars_.size(); ++__i)
2367 {
2368 if (__ch == __chars_[__i])
2369 {
2370 __found = true;
2371 goto __exit;
2372 }
2373 }
2374 if (!__ranges_.empty())
2375 {
2376 string_type __s2 = __collate_ ?
2377 __traits_.transform(&__ch, &__ch + 1) :
2378 string_type(1, __ch);
2379 for (size_t __i = 0; __i < __ranges_.size(); ++__i)
2380 {
2381 if (__ranges_[__i].first <= __s2 && __s2 <= __ranges_[__i].second)
2382 {
2383 __found = true;
2384 goto __exit;
2385 }
2386 }
2387 }
2388 if (!__equivalences_.empty())
2389 {
2390 string_type __s2 = __traits_.transform_primary(&__ch, &__ch + 1);
2391 for (size_t __i = 0; __i < __equivalences_.size(); ++__i)
2392 {
2393 if (__s2 == __equivalences_[__i])
2394 {
2395 __found = true;
2396 goto __exit;
2397 }
2398 }
2399 }
2400 if (__traits_.isctype(__ch, __mask_))
2401 __found = true;
2402 }
2403 else
2404 __found = __negate_; // force reject
2405__exit:
2406 if (__found != __negate_)
2407 {
Howard Hinnant173968a2010-07-13 21:48:06 +00002408 __s.__do_ = __state::__accept_and_consume;
2409 __s.__current_ += __consumed;
2410 __s.__node_ = this->first();
2411 }
2412 else
2413 {
2414 __s.__do_ = __state::__reject;
2415 __s.__node_ = nullptr;
2416 }
2417}
2418
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002419template <class, class> class __lookahead;
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00002420
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002421template <class _CharT, class _Traits = regex_traits<_CharT> >
2422class basic_regex
2423{
2424public:
2425 // types:
2426 typedef _CharT value_type;
2427 typedef regex_constants::syntax_option_type flag_type;
2428 typedef typename _Traits::locale_type locale_type;
2429
2430private:
2431 _Traits __traits_;
2432 flag_type __flags_;
2433 unsigned __marked_count_;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002434 unsigned __loop_count_;
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002435 int __open_count_;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002436 shared_ptr<__empty_state<_CharT> > __start_;
2437 __owns_one_state<_CharT>* __end_;
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00002438 bool __left_anchor_;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002439
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00002440 typedef _STD::__state<_CharT> __state;
Howard Hinnantac303862010-07-12 15:51:17 +00002441 typedef _STD::__node<_CharT> __node;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002442
2443public:
2444 // constants:
2445 static const/*expr*/ regex_constants::syntax_option_type icase = regex_constants::icase;
2446 static const/*expr*/ regex_constants::syntax_option_type nosubs = regex_constants::nosubs;
2447 static const/*expr*/ regex_constants::syntax_option_type optimize = regex_constants::optimize;
2448 static const/*expr*/ regex_constants::syntax_option_type collate = regex_constants::collate;
2449 static const/*expr*/ regex_constants::syntax_option_type ECMAScript = regex_constants::ECMAScript;
2450 static const/*expr*/ regex_constants::syntax_option_type basic = regex_constants::basic;
2451 static const/*expr*/ regex_constants::syntax_option_type extended = regex_constants::extended;
2452 static const/*expr*/ regex_constants::syntax_option_type awk = regex_constants::awk;
2453 static const/*expr*/ regex_constants::syntax_option_type grep = regex_constants::grep;
2454 static const/*expr*/ regex_constants::syntax_option_type egrep = regex_constants::egrep;
2455
2456 // construct/copy/destroy:
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002457 basic_regex()
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00002458 : __flags_(), __marked_count_(0), __loop_count_(0), __open_count_(0),
2459 __end_(0), __left_anchor_(false)
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002460 {}
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002461 explicit basic_regex(const value_type* __p, flag_type __f = regex_constants::ECMAScript)
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00002462 : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
2463 __end_(0), __left_anchor_(false)
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002464 {__parse(__p, __p + __traits_.length(__p));}
2465 basic_regex(const value_type* __p, size_t __len, flag_type __f)
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00002466 : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
2467 __end_(0), __left_anchor_(false)
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002468 {__parse(__p, __p + __len);}
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002469// basic_regex(const basic_regex&) = default;
2470// basic_regex(basic_regex&&) = default;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002471 template <class _ST, class _SA>
2472 explicit basic_regex(const basic_string<value_type, _ST, _SA>& __p,
2473 flag_type __f = regex_constants::ECMAScript)
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00002474 : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
2475 __end_(0), __left_anchor_(false)
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002476 {__parse(__p.begin(), __p.end());}
2477 template <class _ForwardIterator>
2478 basic_regex(_ForwardIterator __first, _ForwardIterator __last,
2479 flag_type __f = regex_constants::ECMAScript)
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00002480 : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
2481 __end_(0), __left_anchor_(false)
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002482 {__parse(__first, __last);}
2483 basic_regex(initializer_list<value_type> __il,
2484 flag_type __f = regex_constants::ECMAScript)
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00002485 : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
2486 __end_(0), __left_anchor_(false)
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002487 {__parse(__il.begin(), __il.end());}
2488
2489 ~basic_regex();
2490
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002491// basic_regex& operator=(const basic_regex&) = default;
2492// basic_regex& operator=(basic_regex&&) = default;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002493 basic_regex& operator=(const value_type* __p);
2494 basic_regex& operator=(initializer_list<value_type> __il);
2495 template <class _ST, class _SA>
2496 basic_regex& operator=(const basic_string<value_type, _ST, _SA>& __p);
2497
2498 // assign:
2499 basic_regex& assign(const basic_regex& __that);
2500#ifdef _LIBCPP_MOVE
2501 basic_regex& assign(basic_regex&& __that);
2502#endif
2503 basic_regex& assign(const value_type* __p, flag_type __f = regex_constants::ECMAScript);
2504 basic_regex& assign(const value_type* __p, size_t __len, flag_type __f);
2505 template <class _ST, class _SA>
2506 basic_regex& assign(const basic_string<value_type, _ST, _SA>& __s,
2507 flag_type __f = regex_constants::ECMAScript);
2508 template <class _InputIterator>
2509 basic_regex& assign(_InputIterator __first, _InputIterator __last,
2510 flag_type __f = regex_constants::ECMAScript);
2511 basic_regex& assign(initializer_list<value_type> __il,
2512 flag_type = regex_constants::ECMAScript);
2513
2514 // const operations:
2515 unsigned mark_count() const {return __marked_count_;}
2516 flag_type flags() const {return __flags_;}
2517
2518 // locale:
2519 locale_type imbue(locale_type __loc) {return __traits_.imbue(__loc);}
2520 locale_type getloc() const {return __traits_.getloc();}
2521
2522 // swap:
2523 void swap(basic_regex&);
2524
2525private:
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002526 unsigned __loop_count() const {return __loop_count_;}
2527
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002528 template <class _ForwardIterator>
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002529 _ForwardIterator
2530 __parse(_ForwardIterator __first, _ForwardIterator __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002531 template <class _ForwardIterator>
2532 _ForwardIterator
2533 __parse_basic_reg_exp(_ForwardIterator __first, _ForwardIterator __last);
2534 template <class _ForwardIterator>
2535 _ForwardIterator
2536 __parse_RE_expression(_ForwardIterator __first, _ForwardIterator __last);
2537 template <class _ForwardIterator>
2538 _ForwardIterator
2539 __parse_simple_RE(_ForwardIterator __first, _ForwardIterator __last);
2540 template <class _ForwardIterator>
2541 _ForwardIterator
2542 __parse_nondupl_RE(_ForwardIterator __first, _ForwardIterator __last);
2543 template <class _ForwardIterator>
2544 _ForwardIterator
2545 __parse_one_char_or_coll_elem_RE(_ForwardIterator __first, _ForwardIterator __last);
2546 template <class _ForwardIterator>
2547 _ForwardIterator
2548 __parse_Back_open_paren(_ForwardIterator __first, _ForwardIterator __last);
2549 template <class _ForwardIterator>
2550 _ForwardIterator
2551 __parse_Back_close_paren(_ForwardIterator __first, _ForwardIterator __last);
2552 template <class _ForwardIterator>
2553 _ForwardIterator
2554 __parse_Back_open_brace(_ForwardIterator __first, _ForwardIterator __last);
2555 template <class _ForwardIterator>
2556 _ForwardIterator
2557 __parse_Back_close_brace(_ForwardIterator __first, _ForwardIterator __last);
2558 template <class _ForwardIterator>
2559 _ForwardIterator
2560 __parse_BACKREF(_ForwardIterator __first, _ForwardIterator __last);
2561 template <class _ForwardIterator>
2562 _ForwardIterator
2563 __parse_ORD_CHAR(_ForwardIterator __first, _ForwardIterator __last);
2564 template <class _ForwardIterator>
2565 _ForwardIterator
2566 __parse_QUOTED_CHAR(_ForwardIterator __first, _ForwardIterator __last);
2567 template <class _ForwardIterator>
2568 _ForwardIterator
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002569 __parse_RE_dupl_symbol(_ForwardIterator __first, _ForwardIterator __last,
Howard Hinnante77aa5e2010-07-08 17:43:58 +00002570 __owns_one_state<_CharT>* __s,
2571 unsigned __mexp_begin, unsigned __mexp_end);
Howard Hinnant0de86b62010-06-25 20:56:08 +00002572 template <class _ForwardIterator>
2573 _ForwardIterator
Howard Hinnantaa698082010-07-16 19:08:36 +00002574 __parse_ERE_dupl_symbol(_ForwardIterator __first, _ForwardIterator __last,
2575 __owns_one_state<_CharT>* __s,
2576 unsigned __mexp_begin, unsigned __mexp_end);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002577 template <class _ForwardIterator>
2578 _ForwardIterator
Howard Hinnant0de86b62010-06-25 20:56:08 +00002579 __parse_bracket_expression(_ForwardIterator __first, _ForwardIterator __last);
2580 template <class _ForwardIterator>
2581 _ForwardIterator
Howard Hinnant173968a2010-07-13 21:48:06 +00002582 __parse_follow_list(_ForwardIterator __first, _ForwardIterator __last,
2583 __bracket_expression<_CharT, _Traits>* __ml);
Howard Hinnant0de86b62010-06-25 20:56:08 +00002584 template <class _ForwardIterator>
2585 _ForwardIterator
Howard Hinnant173968a2010-07-13 21:48:06 +00002586 __parse_expression_term(_ForwardIterator __first, _ForwardIterator __last,
2587 __bracket_expression<_CharT, _Traits>* __ml);
Howard Hinnant0de86b62010-06-25 20:56:08 +00002588 template <class _ForwardIterator>
2589 _ForwardIterator
Howard Hinnant173968a2010-07-13 21:48:06 +00002590 __parse_equivalence_class(_ForwardIterator __first, _ForwardIterator __last,
2591 __bracket_expression<_CharT, _Traits>* __ml);
Howard Hinnant0de86b62010-06-25 20:56:08 +00002592 template <class _ForwardIterator>
2593 _ForwardIterator
Howard Hinnant173968a2010-07-13 21:48:06 +00002594 __parse_character_class(_ForwardIterator __first, _ForwardIterator __last,
2595 __bracket_expression<_CharT, _Traits>* __ml);
Howard Hinnant0de86b62010-06-25 20:56:08 +00002596 template <class _ForwardIterator>
2597 _ForwardIterator
Howard Hinnant173968a2010-07-13 21:48:06 +00002598 __parse_collating_symbol(_ForwardIterator __first, _ForwardIterator __last,
2599 basic_string<_CharT>& __col_sym);
Howard Hinnant0de86b62010-06-25 20:56:08 +00002600 template <class _ForwardIterator>
2601 _ForwardIterator
2602 __parse_DUP_COUNT(_ForwardIterator __first, _ForwardIterator __last, int& __c);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002603 template <class _ForwardIterator>
2604 _ForwardIterator
2605 __parse_extended_reg_exp(_ForwardIterator __first, _ForwardIterator __last);
2606 template <class _ForwardIterator>
2607 _ForwardIterator
2608 __parse_ERE_branch(_ForwardIterator __first, _ForwardIterator __last);
2609 template <class _ForwardIterator>
2610 _ForwardIterator
2611 __parse_ERE_expression(_ForwardIterator __first, _ForwardIterator __last);
2612 template <class _ForwardIterator>
2613 _ForwardIterator
2614 __parse_one_char_or_coll_elem_ERE(_ForwardIterator __first, _ForwardIterator __last);
2615 template <class _ForwardIterator>
2616 _ForwardIterator
2617 __parse_ORD_CHAR_ERE(_ForwardIterator __first, _ForwardIterator __last);
2618 template <class _ForwardIterator>
2619 _ForwardIterator
2620 __parse_QUOTED_CHAR_ERE(_ForwardIterator __first, _ForwardIterator __last);
Howard Hinnant2ade7c22010-07-22 17:53:24 +00002621 template <class _ForwardIterator>
2622 _ForwardIterator
2623 __parse_ecma_exp(_ForwardIterator __first, _ForwardIterator __last);
2624 template <class _ForwardIterator>
2625 _ForwardIterator
2626 __parse_alternative(_ForwardIterator __first, _ForwardIterator __last);
2627 template <class _ForwardIterator>
2628 _ForwardIterator
2629 __parse_term(_ForwardIterator __first, _ForwardIterator __last);
2630 template <class _ForwardIterator>
2631 _ForwardIterator
2632 __parse_assertion(_ForwardIterator __first, _ForwardIterator __last);
2633 template <class _ForwardIterator>
2634 _ForwardIterator
2635 __parse_atom(_ForwardIterator __first, _ForwardIterator __last);
2636 template <class _ForwardIterator>
2637 _ForwardIterator
Howard Hinnant17615b02010-07-27 01:25:38 +00002638 __parse_atom_escape(_ForwardIterator __first, _ForwardIterator __last);
2639 template <class _ForwardIterator>
2640 _ForwardIterator
2641 __parse_decimal_escape(_ForwardIterator __first, _ForwardIterator __last);
2642 template <class _ForwardIterator>
2643 _ForwardIterator
2644 __parse_character_class_escape(_ForwardIterator __first, _ForwardIterator __last);
2645 template <class _ForwardIterator>
2646 _ForwardIterator
2647 __parse_character_escape(_ForwardIterator __first, _ForwardIterator __last);
2648 template <class _ForwardIterator>
2649 _ForwardIterator
2650 __parse_pattern_character(_ForwardIterator __first, _ForwardIterator __last);
Howard Hinnant856846b2010-07-27 19:53:10 +00002651 template <class _ForwardIterator>
2652 _ForwardIterator
2653 __parse_grep(_ForwardIterator __first, _ForwardIterator __last);
2654 template <class _ForwardIterator>
2655 _ForwardIterator
2656 __parse_egrep(_ForwardIterator __first, _ForwardIterator __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002657
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00002658 void __push_l_anchor() {__left_anchor_ = true;}
2659 void __push_r_anchor();
Howard Hinnantac303862010-07-12 15:51:17 +00002660 void __push_match_any();
Howard Hinnant17615b02010-07-27 01:25:38 +00002661 void __push_match_any_but_newline();
Howard Hinnante77aa5e2010-07-08 17:43:58 +00002662 void __push_greedy_inf_repeat(size_t __min, __owns_one_state<_CharT>* __s,
2663 unsigned __mexp_begin = 0, unsigned __mexp_end = 0)
2664 {__push_loop(__min, numeric_limits<size_t>::max(), __s,
2665 __mexp_begin, __mexp_end);}
Howard Hinnant17615b02010-07-27 01:25:38 +00002666 void __push_nongreedy_inf_repeat(size_t __min, __owns_one_state<_CharT>* __s,
2667 unsigned __mexp_begin = 0, unsigned __mexp_end = 0)
2668 {__push_loop(__min, numeric_limits<size_t>::max(), __s,
2669 __mexp_begin, __mexp_end, false);}
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002670 void __push_loop(size_t __min, size_t __max, __owns_one_state<_CharT>* __s,
2671 size_t __mexp_begin = 0, size_t __mexp_end = 0,
2672 bool __greedy = true);
Howard Hinnant173968a2010-07-13 21:48:06 +00002673 __bracket_expression<_CharT, _Traits>* __start_matching_list(bool __negate);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002674 void __push_char(value_type __c);
Howard Hinnantcba352d2010-07-12 18:16:05 +00002675 void __push_back_ref(int __i);
Howard Hinnantaa698082010-07-16 19:08:36 +00002676 void __push_alternation(__owns_one_state<_CharT>* __sa,
2677 __owns_one_state<_CharT>* __sb);
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00002678 void __push_begin_marked_subexpression();
2679 void __push_end_marked_subexpression(unsigned);
Howard Hinnant2ade7c22010-07-22 17:53:24 +00002680 void __push_empty();
Howard Hinnant17615b02010-07-27 01:25:38 +00002681 void __push_word_boundary(bool);
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002682 void __push_lookahead(const basic_regex&, bool);
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00002683
Howard Hinnant22ce0b42010-07-14 21:14:52 +00002684 template <class _Allocator>
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00002685 bool
Howard Hinnant22ce0b42010-07-14 21:14:52 +00002686 __search(const _CharT* __first, const _CharT* __last,
2687 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00002688 regex_constants::match_flag_type __flags) const;
2689
Howard Hinnant22ce0b42010-07-14 21:14:52 +00002690 template <class _Allocator>
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002691 bool
Howard Hinnant22ce0b42010-07-14 21:14:52 +00002692 __match_at_start(const _CharT* __first, const _CharT* __last,
2693 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002694 regex_constants::match_flag_type __flags) const;
Howard Hinnant17615b02010-07-27 01:25:38 +00002695 template <class _Allocator>
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002696 bool
Howard Hinnant17615b02010-07-27 01:25:38 +00002697 __match_at_start_ecma(const _CharT* __first, const _CharT* __last,
2698 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002699 regex_constants::match_flag_type __flags) const;
Howard Hinnant22ce0b42010-07-14 21:14:52 +00002700 template <class _Allocator>
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002701 bool
2702 __match_at_start_posix_nosubs(const _CharT* __first, const _CharT* __last,
Howard Hinnant22ce0b42010-07-14 21:14:52 +00002703 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002704 regex_constants::match_flag_type __flags) const;
Howard Hinnant22ce0b42010-07-14 21:14:52 +00002705 template <class _Allocator>
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002706 bool
Howard Hinnant22ce0b42010-07-14 21:14:52 +00002707 __match_at_start_posix_subs(const _CharT* __first, const _CharT* __last,
2708 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002709 regex_constants::match_flag_type __flags) const;
2710
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00002711 template <class _B, class _A, class _C, class _T>
2712 friend
2713 bool
2714 regex_search(_B, _B, match_results<_B, _A>&, const basic_regex<_C, _T>&,
2715 regex_constants::match_flag_type);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002716
Howard Hinnant22ce0b42010-07-14 21:14:52 +00002717 template <class _A, class _C, class _T>
2718 friend
2719 bool
2720 regex_search(const _C*, const _C*, match_results<const _C*, _A>&,
2721 const basic_regex<_C, _T>&, regex_constants::match_flag_type);
2722
2723 template <class _B, class _C, class _T>
2724 friend
2725 bool
2726 regex_search(_B, _B, const basic_regex<_C, _T>&,
2727 regex_constants::match_flag_type);
2728
2729 template <class _C, class _T>
2730 friend
2731 bool
2732 regex_search(const _C*, const _C*,
2733 const basic_regex<_C, _T>&, regex_constants::match_flag_type);
2734
2735 template <class _C, class _A, class _T>
2736 friend
2737 bool
2738 regex_search(const _C*, match_results<const _C*, _A>&, const basic_regex<_C, _T>&,
2739 regex_constants::match_flag_type);
2740
2741 template <class _ST, class _SA, class _C, class _T>
2742 friend
2743 bool
2744 regex_search(const basic_string<_C, _ST, _SA>& __s,
2745 const basic_regex<_C, _T>& __e,
2746 regex_constants::match_flag_type __flags);
2747
2748 template <class _ST, class _SA, class _A, class _C, class _T>
2749 friend
2750 bool
2751 regex_search(const basic_string<_C, _ST, _SA>& __s,
2752 match_results<typename basic_string<_C, _ST, _SA>::const_iterator, _A>&,
2753 const basic_regex<_C, _T>& __e,
2754 regex_constants::match_flag_type __flags);
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002755
2756 template <class, class> friend class __lookahead;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002757};
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002758
2759template <class _CharT, class _Traits>
2760basic_regex<_CharT, _Traits>::~basic_regex()
2761{
2762}
2763
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002764// __lookahead
2765
2766template <class _CharT, class _Traits>
2767class __lookahead
2768 : public __owns_one_state<_CharT>
2769{
2770 typedef __owns_one_state<_CharT> base;
2771
2772 basic_regex<_CharT, _Traits> __exp_;
2773 bool __invert_;
2774
2775 __lookahead(const __lookahead&);
2776 __lookahead& operator=(const __lookahead&);
2777public:
2778 typedef _STD::__state<_CharT> __state;
2779
2780 __lookahead(const basic_regex<_CharT, _Traits>& __exp, bool __invert, __node<_CharT>* __s)
2781 : base(__s), __exp_(__exp), __invert_(__invert) {}
2782
2783 virtual void __exec(__state&) const;
2784
2785 virtual string speak() const
2786 {
2787 ostringstream os;
2788 if (__invert_)
2789 os << "not lookahead";
2790 else
2791 os << "lookahead";
2792 return os.str();
2793 }
2794};
2795
2796template <class _CharT, class _Traits>
2797void
2798__lookahead<_CharT, _Traits>::__exec(__state& __s) const
2799{
2800 match_results<const _CharT*> __m;
2801 __m.__init(1 + __exp_.mark_count(), __s.__current_, __s.__last_);
2802 bool __matched = __exp_.__match_at_start_ecma(__s.__current_, __s.__last_,
2803 __m, __s.__flags_);
2804 if (__matched != __invert_)
2805 {
2806 __s.__do_ = __state::__accept_but_not_consume;
2807 __s.__node_ = this->first();
2808 }
2809 else
2810 {
2811 __s.__do_ = __state::__reject;
2812 __s.__node_ = nullptr;
2813 }
2814}
2815
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002816template <class _CharT, class _Traits>
2817template <class _ForwardIterator>
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002818_ForwardIterator
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002819basic_regex<_CharT, _Traits>::__parse(_ForwardIterator __first,
2820 _ForwardIterator __last)
2821{
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002822 {
Howard Hinnantac303862010-07-12 15:51:17 +00002823 unique_ptr<__node> __h(new __end_state<_CharT>);
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002824 __start_.reset(new __empty_state<_CharT>(__h.get()));
2825 __h.release();
2826 __end_ = __start_.get();
2827 }
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00002828 switch (__flags_ & 0x1F0)
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002829 {
2830 case ECMAScript:
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002831 __first = __parse_ecma_exp(__first, __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002832 break;
2833 case basic:
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002834 __first = __parse_basic_reg_exp(__first, __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002835 break;
2836 case extended:
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002837 __first = __parse_extended_reg_exp(__first, __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002838 break;
2839 case awk:
2840 break;
2841 case grep:
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002842 __first = __parse_grep(__first, __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002843 break;
2844 case egrep:
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002845 __first = __parse_egrep(__first, __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002846 break;
2847 default:
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00002848 throw regex_error(regex_constants::__re_err_grammar);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002849 }
Howard Hinnante9de5ff2010-07-27 22:20:32 +00002850 return __first;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002851}
2852
2853template <class _CharT, class _Traits>
2854template <class _ForwardIterator>
2855_ForwardIterator
2856basic_regex<_CharT, _Traits>::__parse_basic_reg_exp(_ForwardIterator __first,
2857 _ForwardIterator __last)
2858{
2859 if (__first != __last)
2860 {
2861 if (*__first == '^')
2862 {
2863 __push_l_anchor();
2864 ++__first;
2865 }
2866 if (__first != __last)
2867 {
2868 __first = __parse_RE_expression(__first, __last);
2869 if (__first != __last)
2870 {
2871 _ForwardIterator __temp = next(__first);
2872 if (__temp == __last && *__first == '$')
2873 {
2874 __push_r_anchor();
2875 ++__first;
2876 }
2877 }
2878 }
2879 if (__first != __last)
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00002880 throw regex_error(regex_constants::__re_err_empty);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002881 }
2882 return __first;
2883}
2884
2885template <class _CharT, class _Traits>
2886template <class _ForwardIterator>
2887_ForwardIterator
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002888basic_regex<_CharT, _Traits>::__parse_extended_reg_exp(_ForwardIterator __first,
2889 _ForwardIterator __last)
2890{
Howard Hinnantaa698082010-07-16 19:08:36 +00002891 __owns_one_state<_CharT>* __sa = __end_;
2892 _ForwardIterator __temp = __parse_ERE_branch(__first, __last);
2893 if (__temp == __first)
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00002894 throw regex_error(regex_constants::__re_err_empty);
Howard Hinnantaa698082010-07-16 19:08:36 +00002895 __first = __temp;
2896 while (__first != __last && *__first == '|')
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002897 {
Howard Hinnantaa698082010-07-16 19:08:36 +00002898 __owns_one_state<_CharT>* __sb = __end_;
2899 __temp = __parse_ERE_branch(++__first, __last);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002900 if (__temp == __first)
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00002901 throw regex_error(regex_constants::__re_err_empty);
Howard Hinnantaa698082010-07-16 19:08:36 +00002902 __push_alternation(__sa, __sb);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002903 __first = __temp;
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002904 }
2905 return __first;
2906}
2907
2908template <class _CharT, class _Traits>
2909template <class _ForwardIterator>
2910_ForwardIterator
2911basic_regex<_CharT, _Traits>::__parse_ERE_branch(_ForwardIterator __first,
2912 _ForwardIterator __last)
2913{
2914 _ForwardIterator __temp = __parse_ERE_expression(__first, __last);
2915 if (__temp == __first)
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00002916 throw regex_error(regex_constants::__re_err_empty);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002917 do
2918 {
2919 __first = __temp;
2920 __temp = __parse_ERE_expression(__first, __last);
2921 } while (__temp != __first);
2922 return __first;
2923}
2924
2925template <class _CharT, class _Traits>
2926template <class _ForwardIterator>
2927_ForwardIterator
2928basic_regex<_CharT, _Traits>::__parse_ERE_expression(_ForwardIterator __first,
2929 _ForwardIterator __last)
2930{
Howard Hinnantaa698082010-07-16 19:08:36 +00002931 __owns_one_state<_CharT>* __e = __end_;
2932 unsigned __mexp_begin = __marked_count_;
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002933 _ForwardIterator __temp = __parse_one_char_or_coll_elem_ERE(__first, __last);
2934 if (__temp == __first && __temp != __last)
2935 {
2936 switch (*__temp)
2937 {
2938 case '^':
2939 __push_l_anchor();
2940 ++__temp;
2941 break;
2942 case '$':
2943 __push_r_anchor();
2944 ++__temp;
2945 break;
2946 case '(':
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00002947 __push_begin_marked_subexpression();
2948 unsigned __temp_count = __marked_count_;
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002949 ++__open_count_;
2950 __temp = __parse_extended_reg_exp(++__temp, __last);
2951 if (__temp == __last || *__temp != ')')
2952 throw regex_error(regex_constants::error_paren);
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00002953 __push_end_marked_subexpression(__temp_count);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002954 --__open_count_;
2955 ++__temp;
2956 break;
2957 }
2958 }
2959 if (__temp != __first)
Howard Hinnantaa698082010-07-16 19:08:36 +00002960 __temp = __parse_ERE_dupl_symbol(__temp, __last, __e, __mexp_begin+1,
2961 __marked_count_+1);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00002962 __first = __temp;
2963 return __first;
2964}
2965
2966template <class _CharT, class _Traits>
2967template <class _ForwardIterator>
2968_ForwardIterator
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002969basic_regex<_CharT, _Traits>::__parse_RE_expression(_ForwardIterator __first,
2970 _ForwardIterator __last)
2971{
2972 while (true)
2973 {
2974 _ForwardIterator __temp = __parse_simple_RE(__first, __last);
2975 if (__temp == __first)
2976 break;
2977 __first = __temp;
2978 }
2979 return __first;
2980}
2981
2982template <class _CharT, class _Traits>
2983template <class _ForwardIterator>
2984_ForwardIterator
2985basic_regex<_CharT, _Traits>::__parse_simple_RE(_ForwardIterator __first,
2986 _ForwardIterator __last)
2987{
2988 if (__first != __last)
2989 {
Howard Hinnantf8ce4592010-07-07 19:14:52 +00002990 __owns_one_state<_CharT>* __e = __end_;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00002991 unsigned __mexp_begin = __marked_count_;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002992 _ForwardIterator __temp = __parse_nondupl_RE(__first, __last);
2993 if (__temp != __first)
Howard Hinnante77aa5e2010-07-08 17:43:58 +00002994 __first = __parse_RE_dupl_symbol(__temp, __last, __e,
2995 __mexp_begin+1, __marked_count_+1);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00002996 }
2997 return __first;
2998}
2999
3000template <class _CharT, class _Traits>
3001template <class _ForwardIterator>
3002_ForwardIterator
3003basic_regex<_CharT, _Traits>::__parse_nondupl_RE(_ForwardIterator __first,
3004 _ForwardIterator __last)
3005{
3006 _ForwardIterator __temp = __first;
3007 __first = __parse_one_char_or_coll_elem_RE(__first, __last);
3008 if (__temp == __first)
3009 {
3010 __temp = __parse_Back_open_paren(__first, __last);
3011 if (__temp != __first)
3012 {
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00003013 __push_begin_marked_subexpression();
3014 unsigned __temp_count = __marked_count_;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003015 __first = __parse_RE_expression(__temp, __last);
3016 __temp = __parse_Back_close_paren(__first, __last);
3017 if (__temp == __first)
3018 throw regex_error(regex_constants::error_paren);
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00003019 __push_end_marked_subexpression(__temp_count);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003020 __first = __temp;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003021 }
3022 else
3023 __first = __parse_BACKREF(__first, __last);
3024 }
3025 return __first;
3026}
3027
3028template <class _CharT, class _Traits>
3029template <class _ForwardIterator>
3030_ForwardIterator
3031basic_regex<_CharT, _Traits>::__parse_one_char_or_coll_elem_RE(
3032 _ForwardIterator __first,
3033 _ForwardIterator __last)
3034{
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003035 _ForwardIterator __temp = __parse_ORD_CHAR(__first, __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003036 if (__temp == __first)
3037 {
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003038 __temp = __parse_QUOTED_CHAR(__first, __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003039 if (__temp == __first)
3040 {
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003041 if (__temp != __last && *__temp == '.')
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003042 {
3043 __push_match_any();
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003044 ++__temp;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003045 }
3046 else
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003047 __temp = __parse_bracket_expression(__first, __last);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003048 }
3049 }
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003050 __first = __temp;
3051 return __first;
3052}
3053
3054template <class _CharT, class _Traits>
3055template <class _ForwardIterator>
3056_ForwardIterator
3057basic_regex<_CharT, _Traits>::__parse_one_char_or_coll_elem_ERE(
3058 _ForwardIterator __first,
3059 _ForwardIterator __last)
3060{
3061 _ForwardIterator __temp = __parse_ORD_CHAR_ERE(__first, __last);
3062 if (__temp == __first)
3063 {
3064 __temp = __parse_QUOTED_CHAR_ERE(__first, __last);
3065 if (__temp == __first)
3066 {
3067 if (__temp != __last && *__temp == '.')
3068 {
3069 __push_match_any();
3070 ++__temp;
3071 }
3072 else
3073 __temp = __parse_bracket_expression(__first, __last);
3074 }
3075 }
3076 __first = __temp;
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003077 return __first;
3078}
3079
3080template <class _CharT, class _Traits>
3081template <class _ForwardIterator>
3082_ForwardIterator
3083basic_regex<_CharT, _Traits>::__parse_Back_open_paren(_ForwardIterator __first,
3084 _ForwardIterator __last)
3085{
3086 if (__first != __last)
3087 {
3088 _ForwardIterator __temp = next(__first);
3089 if (__temp != __last)
3090 {
3091 if (*__first == '\\' && *__temp == '(')
3092 __first = ++__temp;
3093 }
3094 }
3095 return __first;
3096}
3097
3098template <class _CharT, class _Traits>
3099template <class _ForwardIterator>
3100_ForwardIterator
3101basic_regex<_CharT, _Traits>::__parse_Back_close_paren(_ForwardIterator __first,
3102 _ForwardIterator __last)
3103{
3104 if (__first != __last)
3105 {
3106 _ForwardIterator __temp = next(__first);
3107 if (__temp != __last)
3108 {
3109 if (*__first == '\\' && *__temp == ')')
3110 __first = ++__temp;
3111 }
3112 }
3113 return __first;
3114}
3115
3116template <class _CharT, class _Traits>
3117template <class _ForwardIterator>
3118_ForwardIterator
3119basic_regex<_CharT, _Traits>::__parse_Back_open_brace(_ForwardIterator __first,
3120 _ForwardIterator __last)
3121{
3122 if (__first != __last)
3123 {
3124 _ForwardIterator __temp = next(__first);
3125 if (__temp != __last)
3126 {
3127 if (*__first == '\\' && *__temp == '{')
3128 __first = ++__temp;
3129 }
3130 }
3131 return __first;
3132}
3133
3134template <class _CharT, class _Traits>
3135template <class _ForwardIterator>
3136_ForwardIterator
3137basic_regex<_CharT, _Traits>::__parse_Back_close_brace(_ForwardIterator __first,
3138 _ForwardIterator __last)
3139{
3140 if (__first != __last)
3141 {
3142 _ForwardIterator __temp = next(__first);
3143 if (__temp != __last)
3144 {
3145 if (*__first == '\\' && *__temp == '}')
3146 __first = ++__temp;
3147 }
3148 }
3149 return __first;
3150}
3151
3152template <class _CharT, class _Traits>
3153template <class _ForwardIterator>
3154_ForwardIterator
3155basic_regex<_CharT, _Traits>::__parse_BACKREF(_ForwardIterator __first,
3156 _ForwardIterator __last)
3157{
3158 if (__first != __last)
3159 {
3160 _ForwardIterator __temp = next(__first);
3161 if (__temp != __last)
3162 {
3163 if (*__first == '\\' && '1' <= *__temp && *__temp <= '9')
3164 {
3165 __push_back_ref(*__temp - '0');
3166 __first = ++__temp;
3167 }
3168 }
3169 }
3170 return __first;
3171}
3172
3173template <class _CharT, class _Traits>
3174template <class _ForwardIterator>
3175_ForwardIterator
3176basic_regex<_CharT, _Traits>::__parse_ORD_CHAR(_ForwardIterator __first,
3177 _ForwardIterator __last)
3178{
3179 if (__first != __last)
3180 {
3181 _ForwardIterator __temp = next(__first);
3182 if (__temp == __last && *__first == '$')
3183 return __first;
3184 // Not called inside a bracket
3185 if (*__first == '.' || *__first == '\\' || *__first == '[')
3186 return __first;
Howard Hinnant0de86b62010-06-25 20:56:08 +00003187 __push_char(*__first);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003188 ++__first;
3189 }
3190 return __first;
3191}
3192
3193template <class _CharT, class _Traits>
3194template <class _ForwardIterator>
3195_ForwardIterator
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003196basic_regex<_CharT, _Traits>::__parse_ORD_CHAR_ERE(_ForwardIterator __first,
3197 _ForwardIterator __last)
3198{
3199 if (__first != __last)
3200 {
3201 switch (*__first)
3202 {
3203 case '^':
3204 case '.':
3205 case '[':
3206 case '$':
3207 case '(':
3208 case '|':
3209 case '*':
3210 case '+':
3211 case '?':
3212 case '{':
3213 case '\\':
3214 break;
3215 case ')':
3216 if (__open_count_ == 0)
3217 {
3218 __push_char(*__first);
3219 ++__first;
3220 }
3221 break;
3222 default:
3223 __push_char(*__first);
3224 ++__first;
3225 break;
3226 }
3227 }
3228 return __first;
3229}
3230
3231template <class _CharT, class _Traits>
3232template <class _ForwardIterator>
3233_ForwardIterator
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003234basic_regex<_CharT, _Traits>::__parse_QUOTED_CHAR(_ForwardIterator __first,
3235 _ForwardIterator __last)
3236{
3237 if (__first != __last)
3238 {
3239 _ForwardIterator __temp = next(__first);
3240 if (__temp != __last)
3241 {
3242 if (*__first == '\\')
3243 {
3244 switch (*__temp)
3245 {
3246 case '^':
3247 case '.':
3248 case '*':
3249 case '[':
3250 case '$':
3251 case '\\':
Howard Hinnant0de86b62010-06-25 20:56:08 +00003252 __push_char(*__temp);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003253 __first = ++__temp;
3254 break;
3255 }
3256 }
3257 }
3258 }
3259 return __first;
3260}
3261
3262template <class _CharT, class _Traits>
3263template <class _ForwardIterator>
3264_ForwardIterator
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003265basic_regex<_CharT, _Traits>::__parse_QUOTED_CHAR_ERE(_ForwardIterator __first,
3266 _ForwardIterator __last)
3267{
3268 if (__first != __last)
3269 {
3270 _ForwardIterator __temp = next(__first);
3271 if (__temp != __last)
3272 {
3273 if (*__first == '\\')
3274 {
3275 switch (*__temp)
3276 {
3277 case '^':
3278 case '.':
3279 case '*':
3280 case '[':
3281 case '$':
3282 case '\\':
3283 case '(':
3284 case ')':
3285 case '|':
3286 case '+':
3287 case '?':
3288 case '{':
3289 __push_char(*__temp);
3290 __first = ++__temp;
3291 break;
3292 }
3293 }
3294 }
3295 }
3296 return __first;
3297}
3298
3299template <class _CharT, class _Traits>
3300template <class _ForwardIterator>
3301_ForwardIterator
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003302basic_regex<_CharT, _Traits>::__parse_RE_dupl_symbol(_ForwardIterator __first,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00003303 _ForwardIterator __last,
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003304 __owns_one_state<_CharT>* __s,
3305 unsigned __mexp_begin,
3306 unsigned __mexp_end)
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003307{
3308 if (__first != __last)
3309 {
Howard Hinnant0de86b62010-06-25 20:56:08 +00003310 if (*__first == '*')
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003311 {
Howard Hinnante77aa5e2010-07-08 17:43:58 +00003312 __push_greedy_inf_repeat(0, __s, __mexp_begin, __mexp_end);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003313 ++__first;
3314 }
3315 else
3316 {
3317 _ForwardIterator __temp = __parse_Back_open_brace(__first, __last);
3318 if (__temp != __first)
3319 {
3320 int __min = 0;
3321 __first = __temp;
3322 __temp = __parse_DUP_COUNT(__first, __last, __min);
3323 if (__temp == __first)
3324 throw regex_error(regex_constants::error_badbrace);
3325 __first = __temp;
3326 if (__first == __last)
3327 throw regex_error(regex_constants::error_brace);
3328 if (*__first != ',')
3329 {
3330 __temp = __parse_Back_close_brace(__first, __last);
3331 if (__temp == __first)
3332 throw regex_error(regex_constants::error_brace);
Howard Hinnantcba352d2010-07-12 18:16:05 +00003333 __push_loop(__min, __min, __s, __mexp_begin, __mexp_end,
3334 true);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003335 __first = __temp;
3336 }
3337 else
3338 {
3339 ++__first; // consume ','
3340 int __max = -1;
3341 __first = __parse_DUP_COUNT(__first, __last, __max);
3342 __temp = __parse_Back_close_brace(__first, __last);
3343 if (__temp == __first)
3344 throw regex_error(regex_constants::error_brace);
3345 if (__max == -1)
Howard Hinnantaa698082010-07-16 19:08:36 +00003346 __push_greedy_inf_repeat(__min, __s, __mexp_begin, __mexp_end);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003347 else
3348 {
3349 if (__max < __min)
3350 throw regex_error(regex_constants::error_badbrace);
Howard Hinnantcba352d2010-07-12 18:16:05 +00003351 __push_loop(__min, __max, __s, __mexp_begin, __mexp_end,
3352 true);
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00003353 }
3354 __first = __temp;
3355 }
3356 }
3357 }
3358 }
3359 return __first;
3360}
3361
Howard Hinnant0de86b62010-06-25 20:56:08 +00003362template <class _CharT, class _Traits>
3363template <class _ForwardIterator>
3364_ForwardIterator
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003365basic_regex<_CharT, _Traits>::__parse_ERE_dupl_symbol(_ForwardIterator __first,
Howard Hinnantaa698082010-07-16 19:08:36 +00003366 _ForwardIterator __last,
3367 __owns_one_state<_CharT>* __s,
3368 unsigned __mexp_begin,
3369 unsigned __mexp_end)
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003370{
3371 if (__first != __last)
3372 {
3373 switch (*__first)
3374 {
3375 case '*':
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003376 ++__first;
Howard Hinnant17615b02010-07-27 01:25:38 +00003377 if ((__flags_ & ECMAScript) && __first != __last && *__first == '?')
3378 {
3379 ++__first;
3380 __push_nongreedy_inf_repeat(0, __s, __mexp_begin, __mexp_end);
3381 }
3382 else
3383 __push_greedy_inf_repeat(0, __s, __mexp_begin, __mexp_end);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003384 break;
3385 case '+':
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003386 ++__first;
Howard Hinnant17615b02010-07-27 01:25:38 +00003387 if ((__flags_ & ECMAScript) && __first != __last && *__first == '?')
3388 {
3389 ++__first;
3390 __push_nongreedy_inf_repeat(1, __s, __mexp_begin, __mexp_end);
3391 }
3392 else
3393 __push_greedy_inf_repeat(1, __s, __mexp_begin, __mexp_end);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003394 break;
3395 case '?':
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003396 ++__first;
Howard Hinnant17615b02010-07-27 01:25:38 +00003397 if ((__flags_ & ECMAScript) && __first != __last && *__first == '?')
3398 {
3399 ++__first;
3400 __push_loop(0, 1, __s, __mexp_begin, __mexp_end, false);
3401 }
3402 else
3403 __push_loop(0, 1, __s, __mexp_begin, __mexp_end);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003404 break;
3405 case '{':
3406 {
3407 int __min;
Howard Hinnantaa698082010-07-16 19:08:36 +00003408 _ForwardIterator __temp = __parse_DUP_COUNT(++__first, __last, __min);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003409 if (__temp == __first)
3410 throw regex_error(regex_constants::error_badbrace);
3411 __first = __temp;
3412 if (__first == __last)
3413 throw regex_error(regex_constants::error_brace);
3414 switch (*__first)
3415 {
3416 case '}':
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003417 ++__first;
Howard Hinnant17615b02010-07-27 01:25:38 +00003418 if ((__flags_ & ECMAScript) && __first != __last && *__first == '?')
3419 {
3420 ++__first;
3421 __push_loop(__min, __min, __s, __mexp_begin, __mexp_end, false);
3422 }
3423 else
3424 __push_loop(__min, __min, __s, __mexp_begin, __mexp_end);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003425 break;
3426 case ',':
3427 if (++__first == __last)
3428 throw regex_error(regex_constants::error_badbrace);
3429 if (*__first == '}')
3430 {
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003431 ++__first;
Howard Hinnant17615b02010-07-27 01:25:38 +00003432 if ((__flags_ & ECMAScript) && __first != __last && *__first == '?')
3433 {
3434 ++__first;
3435 __push_nongreedy_inf_repeat(__min, __s, __mexp_begin, __mexp_end);
3436 }
3437 else
3438 __push_greedy_inf_repeat(__min, __s, __mexp_begin, __mexp_end);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003439 }
3440 else
3441 {
Howard Hinnantaa698082010-07-16 19:08:36 +00003442 int __max = -1;
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003443 __temp = __parse_DUP_COUNT(__first, __last, __max);
3444 if (__temp == __first)
3445 throw regex_error(regex_constants::error_brace);
3446 __first = __temp;
3447 if (__first == __last || *__first != '}')
3448 throw regex_error(regex_constants::error_brace);
3449 ++__first;
3450 if (__max < __min)
3451 throw regex_error(regex_constants::error_badbrace);
Howard Hinnant17615b02010-07-27 01:25:38 +00003452 if ((__flags_ & ECMAScript) && __first != __last && *__first == '?')
3453 {
3454 ++__first;
3455 __push_loop(__min, __max, __s, __mexp_begin, __mexp_end, false);
3456 }
3457 else
3458 __push_loop(__min, __max, __s, __mexp_begin, __mexp_end);
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003459 }
Howard Hinnantaa698082010-07-16 19:08:36 +00003460 break;
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003461 default:
3462 throw regex_error(regex_constants::error_badbrace);
3463 }
3464 }
3465 break;
3466 }
3467 }
3468 return __first;
3469}
3470
3471template <class _CharT, class _Traits>
3472template <class _ForwardIterator>
3473_ForwardIterator
Howard Hinnant0de86b62010-06-25 20:56:08 +00003474basic_regex<_CharT, _Traits>::__parse_bracket_expression(_ForwardIterator __first,
3475 _ForwardIterator __last)
3476{
3477 if (__first != __last && *__first == '[')
3478 {
3479 if (++__first == __last)
3480 throw regex_error(regex_constants::error_brack);
Howard Hinnant173968a2010-07-13 21:48:06 +00003481 bool __negate = false;
Howard Hinnant0de86b62010-06-25 20:56:08 +00003482 if (*__first == '^')
3483 {
3484 ++__first;
Howard Hinnant173968a2010-07-13 21:48:06 +00003485 __negate = true;
Howard Hinnant0de86b62010-06-25 20:56:08 +00003486 }
Howard Hinnant173968a2010-07-13 21:48:06 +00003487 __bracket_expression<_CharT, _Traits>* __ml = __start_matching_list(__negate);
3488 // __ml owned by *this
Howard Hinnant0de86b62010-06-25 20:56:08 +00003489 if (__first == __last)
3490 throw regex_error(regex_constants::error_brack);
3491 if (*__first == ']')
3492 {
Howard Hinnant173968a2010-07-13 21:48:06 +00003493 __ml->__add_char(']');
Howard Hinnant0de86b62010-06-25 20:56:08 +00003494 ++__first;
3495 }
Howard Hinnant173968a2010-07-13 21:48:06 +00003496 __first = __parse_follow_list(__first, __last, __ml);
Howard Hinnant0de86b62010-06-25 20:56:08 +00003497 if (__first == __last)
3498 throw regex_error(regex_constants::error_brack);
3499 if (*__first == '-')
3500 {
Howard Hinnant173968a2010-07-13 21:48:06 +00003501 __ml->__add_char('-');
Howard Hinnant0de86b62010-06-25 20:56:08 +00003502 ++__first;
3503 }
3504 if (__first == __last || *__first != ']')
3505 throw regex_error(regex_constants::error_brack);
Howard Hinnant0de86b62010-06-25 20:56:08 +00003506 ++__first;
3507 }
3508 return __first;
3509}
3510
3511template <class _CharT, class _Traits>
3512template <class _ForwardIterator>
3513_ForwardIterator
3514basic_regex<_CharT, _Traits>::__parse_follow_list(_ForwardIterator __first,
Howard Hinnant173968a2010-07-13 21:48:06 +00003515 _ForwardIterator __last,
3516 __bracket_expression<_CharT, _Traits>* __ml)
Howard Hinnant0de86b62010-06-25 20:56:08 +00003517{
3518 if (__first != __last)
3519 {
3520 while (true)
3521 {
Howard Hinnant173968a2010-07-13 21:48:06 +00003522 _ForwardIterator __temp = __parse_expression_term(__first, __last,
3523 __ml);
Howard Hinnant0de86b62010-06-25 20:56:08 +00003524 if (__temp == __first)
3525 break;
3526 __first = __temp;
3527 }
3528 }
3529 return __first;
3530}
3531
3532template <class _CharT, class _Traits>
3533template <class _ForwardIterator>
3534_ForwardIterator
3535basic_regex<_CharT, _Traits>::__parse_expression_term(_ForwardIterator __first,
Howard Hinnant173968a2010-07-13 21:48:06 +00003536 _ForwardIterator __last,
3537 __bracket_expression<_CharT, _Traits>* __ml)
Howard Hinnant0de86b62010-06-25 20:56:08 +00003538{
3539 if (__first != __last && *__first != ']')
3540 {
3541 bool __parsed_one = false;
3542 _ForwardIterator __temp = next(__first);
Howard Hinnant173968a2010-07-13 21:48:06 +00003543 basic_string<_CharT> __start_range;
Howard Hinnant0de86b62010-06-25 20:56:08 +00003544 if (__temp != __last && *__first == '[')
3545 {
3546 if (*__temp == '=')
Howard Hinnant173968a2010-07-13 21:48:06 +00003547 return __parse_equivalence_class(++__temp, __last, __ml);
Howard Hinnant0de86b62010-06-25 20:56:08 +00003548 else if (*__temp == ':')
Howard Hinnant173968a2010-07-13 21:48:06 +00003549 return __parse_character_class(++__temp, __last, __ml);
Howard Hinnant0de86b62010-06-25 20:56:08 +00003550 else if (*__temp == '.')
3551 {
Howard Hinnant173968a2010-07-13 21:48:06 +00003552 __first = __parse_collating_symbol(++__temp, __last, __start_range);
Howard Hinnant0de86b62010-06-25 20:56:08 +00003553 __parsed_one = true;
3554 }
3555 }
3556 if (!__parsed_one)
3557 {
Howard Hinnant173968a2010-07-13 21:48:06 +00003558 __start_range = *__first;
Howard Hinnant0de86b62010-06-25 20:56:08 +00003559 ++__first;
3560 }
3561 if (__first != __last && *__first != ']')
3562 {
3563 __temp = next(__first);
3564 if (__temp != __last && *__first == '-' && *__temp != ']')
3565 {
3566 // parse a range
Howard Hinnant173968a2010-07-13 21:48:06 +00003567 basic_string<_CharT> __end_range;
Howard Hinnant0de86b62010-06-25 20:56:08 +00003568 __first = __temp;
3569 ++__temp;
3570 if (__temp != __last && *__first == '[' && *__temp == '.')
Howard Hinnant173968a2010-07-13 21:48:06 +00003571 __first = __parse_collating_symbol(++__temp, __last, __end_range);
Howard Hinnant0de86b62010-06-25 20:56:08 +00003572 else
3573 {
Howard Hinnant173968a2010-07-13 21:48:06 +00003574 __end_range = *__first;
Howard Hinnant0de86b62010-06-25 20:56:08 +00003575 ++__first;
3576 }
Howard Hinnant173968a2010-07-13 21:48:06 +00003577 __ml->__add_range(_STD::move(__start_range), _STD::move(__end_range));
Howard Hinnant0de86b62010-06-25 20:56:08 +00003578 }
Howard Hinnant173968a2010-07-13 21:48:06 +00003579 else
3580 {
3581 if (__start_range.size() == 1)
3582 __ml->__add_char(__start_range[0]);
3583 else
3584 __ml->__add_digraph(__start_range[0], __start_range[1]);
3585 }
3586 }
3587 else
3588 {
3589 if (__start_range.size() == 1)
3590 __ml->__add_char(__start_range[0]);
3591 else
3592 __ml->__add_digraph(__start_range[0], __start_range[1]);
Howard Hinnant0de86b62010-06-25 20:56:08 +00003593 }
3594 }
3595 return __first;
3596}
3597
3598template <class _CharT, class _Traits>
3599template <class _ForwardIterator>
3600_ForwardIterator
3601basic_regex<_CharT, _Traits>::__parse_equivalence_class(_ForwardIterator __first,
Howard Hinnant173968a2010-07-13 21:48:06 +00003602 _ForwardIterator __last,
3603 __bracket_expression<_CharT, _Traits>* __ml)
Howard Hinnant0de86b62010-06-25 20:56:08 +00003604{
3605 // Found [=
3606 // This means =] must exist
3607 value_type _Equal_close[2] = {'=', ']'};
3608 _ForwardIterator __temp = _STD::search(__first, __last, _Equal_close,
3609 _Equal_close+2);
3610 if (__temp == __last)
3611 throw regex_error(regex_constants::error_brack);
3612 // [__first, __temp) contains all text in [= ... =]
3613 typedef typename _Traits::string_type string_type;
3614 string_type __collate_name =
3615 __traits_.lookup_collatename(__first, __temp);
3616 if (__collate_name.empty())
Howard Hinnant173968a2010-07-13 21:48:06 +00003617 throw regex_error(regex_constants::error_collate);
Howard Hinnant0de86b62010-06-25 20:56:08 +00003618 string_type __equiv_name =
3619 __traits_.transform_primary(__collate_name.begin(),
3620 __collate_name.end());
3621 if (!__equiv_name.empty())
Howard Hinnant173968a2010-07-13 21:48:06 +00003622 __ml->__add_equivalence(__equiv_name);
Howard Hinnant0de86b62010-06-25 20:56:08 +00003623 else
Howard Hinnant173968a2010-07-13 21:48:06 +00003624 {
3625 switch (__collate_name.size())
3626 {
3627 case 1:
3628 __ml->__add_char(__collate_name[0]);
3629 break;
3630 case 2:
3631 __ml->__add_digraph(__collate_name[0], __collate_name[1]);
3632 break;
3633 default:
3634 throw regex_error(regex_constants::error_collate);
3635 }
3636 }
Howard Hinnant0de86b62010-06-25 20:56:08 +00003637 __first = next(__temp, 2);
3638 return __first;
3639}
3640
3641template <class _CharT, class _Traits>
3642template <class _ForwardIterator>
3643_ForwardIterator
3644basic_regex<_CharT, _Traits>::__parse_character_class(_ForwardIterator __first,
Howard Hinnant173968a2010-07-13 21:48:06 +00003645 _ForwardIterator __last,
3646 __bracket_expression<_CharT, _Traits>* __ml)
Howard Hinnant0de86b62010-06-25 20:56:08 +00003647{
3648 // Found [:
3649 // This means :] must exist
3650 value_type _Colon_close[2] = {':', ']'};
3651 _ForwardIterator __temp = _STD::search(__first, __last, _Colon_close,
3652 _Colon_close+2);
3653 if (__temp == __last)
3654 throw regex_error(regex_constants::error_brack);
3655 // [__first, __temp) contains all text in [: ... :]
3656 typedef typename _Traits::char_class_type char_class_type;
3657 char_class_type __class_type =
3658 __traits_.lookup_classname(__first, __temp, __flags_ & icase);
3659 if (__class_type == 0)
3660 throw regex_error(regex_constants::error_brack);
Howard Hinnant173968a2010-07-13 21:48:06 +00003661 __ml->__add_class(__class_type);
Howard Hinnant0de86b62010-06-25 20:56:08 +00003662 __first = next(__temp, 2);
3663 return __first;
3664}
3665
3666template <class _CharT, class _Traits>
3667template <class _ForwardIterator>
3668_ForwardIterator
3669basic_regex<_CharT, _Traits>::__parse_collating_symbol(_ForwardIterator __first,
Howard Hinnant173968a2010-07-13 21:48:06 +00003670 _ForwardIterator __last,
3671 basic_string<_CharT>& __col_sym)
Howard Hinnant0de86b62010-06-25 20:56:08 +00003672{
3673 // Found [.
3674 // This means .] must exist
3675 value_type _Dot_close[2] = {'.', ']'};
3676 _ForwardIterator __temp = _STD::search(__first, __last, _Dot_close,
3677 _Dot_close+2);
3678 if (__temp == __last)
3679 throw regex_error(regex_constants::error_brack);
3680 // [__first, __temp) contains all text in [. ... .]
3681 typedef typename _Traits::string_type string_type;
Howard Hinnant173968a2010-07-13 21:48:06 +00003682 __col_sym = __traits_.lookup_collatename(__first, __temp);
3683 switch (__col_sym.size())
3684 {
3685 case 1:
3686 case 2:
3687 break;
3688 default:
3689 throw regex_error(regex_constants::error_collate);
3690 }
Howard Hinnant0de86b62010-06-25 20:56:08 +00003691 __first = next(__temp, 2);
3692 return __first;
3693}
3694
3695template <class _CharT, class _Traits>
3696template <class _ForwardIterator>
3697_ForwardIterator
3698basic_regex<_CharT, _Traits>::__parse_DUP_COUNT(_ForwardIterator __first,
3699 _ForwardIterator __last,
3700 int& __c)
3701{
3702 if (__first != __last && '0' <= *__first && *__first <= '9')
3703 {
3704 __c = *__first - '0';
3705 for (++__first; __first != __last && '0' <= *__first && *__first <= '9';
3706 ++__first)
3707 {
3708 __c *= 10;
3709 __c += *__first - '0';
3710 }
3711 }
3712 return __first;
3713}
3714
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00003715template <class _CharT, class _Traits>
Howard Hinnant2ade7c22010-07-22 17:53:24 +00003716template <class _ForwardIterator>
3717_ForwardIterator
3718basic_regex<_CharT, _Traits>::__parse_ecma_exp(_ForwardIterator __first,
3719 _ForwardIterator __last)
3720{
3721 __owns_one_state<_CharT>* __sa = __end_;
3722 _ForwardIterator __temp = __parse_alternative(__first, __last);
3723 if (__temp == __first)
3724 __push_empty();
3725 __first = __temp;
3726 while (__first != __last && *__first == '|')
3727 {
3728 __owns_one_state<_CharT>* __sb = __end_;
3729 __temp = __parse_alternative(++__first, __last);
3730 if (__temp == __first)
3731 __push_empty();
3732 __push_alternation(__sa, __sb);
3733 __first = __temp;
3734 }
3735 return __first;
3736}
3737
3738template <class _CharT, class _Traits>
3739template <class _ForwardIterator>
3740_ForwardIterator
3741basic_regex<_CharT, _Traits>::__parse_alternative(_ForwardIterator __first,
3742 _ForwardIterator __last)
3743{
3744 while (true)
3745 {
3746 _ForwardIterator __temp = __parse_term(__first, __last);
3747 if (__temp == __first)
3748 break;
3749 __first = __temp;
3750 }
3751 return __first;
3752}
3753
3754template <class _CharT, class _Traits>
3755template <class _ForwardIterator>
3756_ForwardIterator
3757basic_regex<_CharT, _Traits>::__parse_term(_ForwardIterator __first,
3758 _ForwardIterator __last)
3759{
3760 _ForwardIterator __temp = __parse_assertion(__first, __last);
3761 if (__temp == __first)
3762 {
Howard Hinnant17615b02010-07-27 01:25:38 +00003763 __owns_one_state<_CharT>* __e = __end_;
3764 unsigned __mexp_begin = __marked_count_;
Howard Hinnant2ade7c22010-07-22 17:53:24 +00003765 __temp = __parse_atom(__first, __last);
3766 if (__temp != __first)
Howard Hinnant17615b02010-07-27 01:25:38 +00003767 __first = __parse_ERE_dupl_symbol(__temp, __last, __e,
3768 __mexp_begin+1, __marked_count_+1);
Howard Hinnant2ade7c22010-07-22 17:53:24 +00003769 }
Howard Hinnant17615b02010-07-27 01:25:38 +00003770 else
3771 __first = __temp;
Howard Hinnant2ade7c22010-07-22 17:53:24 +00003772 return __first;
3773}
3774
3775template <class _CharT, class _Traits>
3776template <class _ForwardIterator>
3777_ForwardIterator
3778basic_regex<_CharT, _Traits>::__parse_assertion(_ForwardIterator __first,
3779 _ForwardIterator __last)
3780{
3781 if (__first != __last)
3782 {
3783 switch (*__first)
3784 {
3785 case '^':
3786 __push_l_anchor();
3787 ++__first;
3788 break;
3789 case '$':
3790 __push_r_anchor();
3791 ++__first;
3792 break;
3793 case '\\':
3794 {
3795 _ForwardIterator __temp = _STD::next(__first);
3796 if (__temp != __last)
3797 {
3798 if (*__temp == 'b')
3799 {
Howard Hinnant17615b02010-07-27 01:25:38 +00003800 __push_word_boundary(false);
Howard Hinnant2ade7c22010-07-22 17:53:24 +00003801 __first = ++__temp;
3802 }
3803 else if (*__temp == 'B')
3804 {
Howard Hinnant17615b02010-07-27 01:25:38 +00003805 __push_word_boundary(true);
Howard Hinnant2ade7c22010-07-22 17:53:24 +00003806 __first = ++__temp;
3807 }
3808 }
3809 }
3810 break;
3811 case '(':
3812 {
3813 _ForwardIterator __temp = _STD::next(__first);
3814 if (__temp != __last && *__temp == '?')
3815 {
3816 if (++__temp != __last)
3817 {
3818 switch (*__temp)
3819 {
3820 case '=':
Howard Hinnante9de5ff2010-07-27 22:20:32 +00003821 {
3822 basic_regex __exp;
3823 __exp.__flags_ = __flags_;
3824 __temp = __exp.__parse(++__temp, __last);
3825 __exp.__push_l_anchor();
3826 __push_lookahead(_STD::move(__exp), false);
3827 if (__temp == __last || *__temp != ')')
3828 throw regex_error(regex_constants::error_paren);
3829 __first = ++__temp;
3830 }
Howard Hinnant2ade7c22010-07-22 17:53:24 +00003831 break;
3832 case '!':
Howard Hinnante9de5ff2010-07-27 22:20:32 +00003833 {
3834 basic_regex __exp;
3835 __exp.__flags_ = __flags_;
3836 __temp = __exp.__parse(++__temp, __last);
3837 __exp.__push_l_anchor();
3838 __push_lookahead(_STD::move(__exp), true);
3839 if (__temp == __last || *__temp != ')')
3840 throw regex_error(regex_constants::error_paren);
3841 __first = ++__temp;
3842 }
Howard Hinnant2ade7c22010-07-22 17:53:24 +00003843 break;
3844 }
3845 }
3846 }
3847 }
3848 break;
3849 }
3850 }
3851 return __first;
3852}
3853
3854template <class _CharT, class _Traits>
3855template <class _ForwardIterator>
3856_ForwardIterator
3857basic_regex<_CharT, _Traits>::__parse_atom(_ForwardIterator __first,
3858 _ForwardIterator __last)
3859{
Howard Hinnant17615b02010-07-27 01:25:38 +00003860 if (__first != __last)
3861 {
3862 switch (*__first)
3863 {
3864 case '.':
3865 __push_match_any_but_newline();
3866 ++__first;
3867 break;
3868 case '\\':
3869 __first = __parse_atom_escape(__first, __last);
3870 break;
3871 case '[':
3872 __first = __parse_bracket_expression(__first, __last);
3873 break;
3874 case '(':
3875 {
3876 if (++__first == __last)
3877 throw regex_error(regex_constants::error_paren);
3878 _ForwardIterator __temp = _STD::next(__first);
3879 if (__temp != __last && *__first == '?' && *__temp == ':')
3880 {
3881 ++__open_count_;
3882 __first = __parse_ecma_exp(++__temp, __last);
3883 if (__first == __last || *__first != ')')
3884 throw regex_error(regex_constants::error_paren);
3885 --__open_count_;
3886 ++__first;
3887 }
3888 else
3889 {
3890 __push_begin_marked_subexpression();
3891 unsigned __temp_count = __marked_count_;
3892 ++__open_count_;
3893 __first = __parse_ecma_exp(__first, __last);
3894 if (__first == __last || *__first != ')')
3895 throw regex_error(regex_constants::error_paren);
3896 __push_end_marked_subexpression(__temp_count);
3897 --__open_count_;
3898 ++__first;
3899 }
3900 }
3901 break;
3902 default:
3903 __first = __parse_pattern_character(__first, __last);
3904 break;
3905 }
3906 }
3907 return __first;
3908}
3909
3910template <class _CharT, class _Traits>
3911template <class _ForwardIterator>
3912_ForwardIterator
3913basic_regex<_CharT, _Traits>::__parse_atom_escape(_ForwardIterator __first,
3914 _ForwardIterator __last)
3915{
3916 if (__first != __last && *__first == '\\')
3917 {
3918 _ForwardIterator __t1 = _STD::next(__first);
3919 _ForwardIterator __t2 = __parse_decimal_escape(__t1, __last);
3920 if (__t2 != __t1)
3921 __first = __t2;
3922 else
3923 {
3924 __t2 = __parse_character_class_escape(__t1, __last);
3925 if (__t2 != __t1)
3926 __first = __t2;
3927 else
3928 {
3929 __t2 = __parse_character_escape(__t1, __last);
3930 if (__t2 != __t1)
3931 __first = __t2;
3932 }
3933 }
3934 }
3935 return __first;
3936}
3937
3938template <class _CharT, class _Traits>
3939template <class _ForwardIterator>
3940_ForwardIterator
3941basic_regex<_CharT, _Traits>::__parse_decimal_escape(_ForwardIterator __first,
3942 _ForwardIterator __last)
3943{
3944 if (__first != __last)
3945 {
3946 if (*__first == '0')
3947 {
3948 __push_char(_CharT());
3949 ++__first;
3950 }
3951 else if ('1' <= *__first && *__first <= '9')
3952 {
3953 unsigned __v = *__first - '0';
3954 for (++__first; '0' <= *__first && *__first <= '9'; ++__first)
3955 __v = 10 * __v + *__first - '0';
3956 if (__v > mark_count())
3957 throw regex_error(regex_constants::error_backref);
3958 __push_back_ref(__v);
3959 }
3960 }
3961 return __first;
3962}
3963
3964template <class _CharT, class _Traits>
3965template <class _ForwardIterator>
3966_ForwardIterator
3967basic_regex<_CharT, _Traits>::__parse_character_class_escape(_ForwardIterator __first,
3968 _ForwardIterator __last)
3969{
3970 if (__first != __last)
3971 {
3972 __bracket_expression<_CharT, _Traits>* __ml;
3973 switch (*__first)
3974 {
3975 case 'd':
3976 __ml = __start_matching_list(false);
3977 __ml->__add_class(ctype_base::digit);
3978 ++__first;
3979 break;
3980 case 'D':
3981 __ml = __start_matching_list(true);
3982 __ml->__add_class(ctype_base::digit);
3983 ++__first;
3984 break;
3985 case 's':
3986 __ml = __start_matching_list(false);
3987 __ml->__add_class(ctype_base::space);
3988 ++__first;
3989 break;
3990 case 'S':
3991 __ml = __start_matching_list(true);
3992 __ml->__add_class(ctype_base::space);
3993 ++__first;
3994 break;
3995 case 'w':
3996 __ml = __start_matching_list(false);
3997 __ml->__add_class(ctype_base::alnum);
3998 __ml->__add_char('_');
3999 ++__first;
4000 break;
4001 case 'W':
4002 __ml = __start_matching_list(true);
4003 __ml->__add_class(ctype_base::alnum);
4004 __ml->__add_char('_');
4005 ++__first;
4006 break;
4007 }
4008 }
4009 return __first;
4010}
4011
4012template <class _CharT, class _Traits>
4013template <class _ForwardIterator>
4014_ForwardIterator
4015basic_regex<_CharT, _Traits>::__parse_character_escape(_ForwardIterator __first,
4016 _ForwardIterator __last)
4017{
4018 if (__first != __last)
4019 {
4020 _ForwardIterator __t;
4021 unsigned __sum = 0;
4022 int __hd;
4023 switch (*__first)
4024 {
4025 case 'f':
4026 __push_char(_CharT(0xC));
4027 ++__first;
4028 break;
4029 case 'n':
4030 __push_char(_CharT(0xA));
4031 ++__first;
4032 break;
4033 case 'r':
4034 __push_char(_CharT(0xD));
4035 ++__first;
4036 break;
4037 case 't':
4038 __push_char(_CharT(0x9));
4039 ++__first;
4040 break;
4041 case 'v':
4042 __push_char(_CharT(0xB));
4043 ++__first;
4044 break;
4045 case 'c':
4046 if ((__t = _STD::next(__first)) != __last)
4047 {
4048 if ('A' <= *__t <= 'Z' || 'a' <= *__t <= 'z')
4049 {
4050 __push_char(_CharT(*__t % 32));
4051 __first = ++__t;
4052 }
4053 }
4054 break;
4055 case 'u':
4056 if (++__first == __last)
4057 throw regex_error(regex_constants::error_escape);
4058 __hd = __traits_.value(*__first, 16);
4059 if (__hd == -1)
4060 throw regex_error(regex_constants::error_escape);
4061 __sum = 16 * __sum + __hd;
4062 if (++__first == __last)
4063 throw regex_error(regex_constants::error_escape);
4064 __hd = __traits_.value(*__first, 16);
4065 if (__hd == -1)
4066 throw regex_error(regex_constants::error_escape);
4067 __sum = 16 * __sum + __hd;
4068 // drop through
4069 case 'x':
4070 if (++__first == __last)
4071 throw regex_error(regex_constants::error_escape);
4072 __hd = __traits_.value(*__first, 16);
4073 if (__hd == -1)
4074 throw regex_error(regex_constants::error_escape);
4075 __sum = 16 * __sum + __hd;
4076 if (++__first == __last)
4077 throw regex_error(regex_constants::error_escape);
4078 __hd = __traits_.value(*__first, 16);
4079 if (__hd == -1)
4080 throw regex_error(regex_constants::error_escape);
4081 __sum = 16 * __sum + __hd;
4082 __push_char(_CharT(__sum));
4083 ++__first;
4084 break;
4085 default:
4086 if (*__first != '_' && !__traits_.isctype(*__first, ctype_base::alnum))
4087 {
4088 __push_char(*__first);
4089 ++__first;
4090 }
4091 break;
4092 }
4093 }
4094 return __first;
4095}
4096
4097template <class _CharT, class _Traits>
4098template <class _ForwardIterator>
4099_ForwardIterator
4100basic_regex<_CharT, _Traits>::__parse_pattern_character(_ForwardIterator __first,
4101 _ForwardIterator __last)
4102{
4103 if (__first != __last)
4104 {
4105 switch (*__first)
4106 {
4107 case '^':
4108 case '$':
4109 case '\\':
4110 case '.':
4111 case '*':
4112 case '+':
4113 case '?':
4114 case '(':
4115 case ')':
4116 case '[':
4117 case ']':
4118 case '{':
4119 case '}':
4120 case '|':
4121 break;
4122 default:
4123 __push_char(*__first);
4124 ++__first;
4125 break;
4126 }
4127 }
4128 return __first;
Howard Hinnant2ade7c22010-07-22 17:53:24 +00004129}
4130
4131template <class _CharT, class _Traits>
Howard Hinnant856846b2010-07-27 19:53:10 +00004132template <class _ForwardIterator>
4133_ForwardIterator
4134basic_regex<_CharT, _Traits>::__parse_grep(_ForwardIterator __first,
4135 _ForwardIterator __last)
4136{
4137 __owns_one_state<_CharT>* __sa = __end_;
4138 _ForwardIterator __t1 = _STD::find(__first, __last, _CharT('\n'));
4139 if (__t1 != __first)
4140 __parse_basic_reg_exp(__first, __t1);
4141 else
4142 __push_empty();
4143 __first = __t1;
4144 if (__first != __last)
4145 ++__first;
4146 while (__first != __last)
4147 {
4148 __t1 = _STD::find(__first, __last, _CharT('\n'));
4149 __owns_one_state<_CharT>* __sb = __end_;
4150 if (__t1 != __first)
4151 __parse_basic_reg_exp(__first, __t1);
4152 else
4153 __push_empty();
4154 __push_alternation(__sa, __sb);
4155 __first = __t1;
4156 if (__first != __last)
4157 ++__first;
4158 }
4159 return __first;
4160}
4161
4162template <class _CharT, class _Traits>
4163template <class _ForwardIterator>
4164_ForwardIterator
4165basic_regex<_CharT, _Traits>::__parse_egrep(_ForwardIterator __first,
4166 _ForwardIterator __last)
4167{
4168 __owns_one_state<_CharT>* __sa = __end_;
4169 _ForwardIterator __t1 = _STD::find(__first, __last, _CharT('\n'));
4170 if (__t1 != __first)
4171 __parse_extended_reg_exp(__first, __t1);
4172 else
4173 __push_empty();
4174 __first = __t1;
4175 if (__first != __last)
4176 ++__first;
4177 while (__first != __last)
4178 {
4179 __t1 = _STD::find(__first, __last, _CharT('\n'));
4180 __owns_one_state<_CharT>* __sb = __end_;
4181 if (__t1 != __first)
4182 __parse_extended_reg_exp(__first, __t1);
4183 else
4184 __push_empty();
4185 __push_alternation(__sa, __sb);
4186 __first = __t1;
4187 if (__first != __last)
4188 ++__first;
4189 }
4190 return __first;
4191}
4192
4193template <class _CharT, class _Traits>
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00004194void
Howard Hinnantf8ce4592010-07-07 19:14:52 +00004195basic_regex<_CharT, _Traits>::__push_loop(size_t __min, size_t __max,
4196 __owns_one_state<_CharT>* __s, size_t __mexp_begin, size_t __mexp_end,
4197 bool __greedy)
4198{
4199 unique_ptr<__empty_state<_CharT> > __e1(new __empty_state<_CharT>(__end_->first()));
4200 __end_->first() = nullptr;
Howard Hinnantac303862010-07-12 15:51:17 +00004201 unique_ptr<__loop<_CharT> > __e2(new __loop<_CharT>(__loop_count_,
4202 __s->first(), __e1.get(), __mexp_begin, __mexp_end, __greedy,
4203 __min, __max));
4204 __s->first() = nullptr;
4205 __e1.release();
4206 __end_->first() = new __repeat_one_loop<_CharT>(__e2.get());
Howard Hinnantf8ce4592010-07-07 19:14:52 +00004207 __end_ = __e2->second();
Howard Hinnantac303862010-07-12 15:51:17 +00004208 __s->first() = __e2.release();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00004209 ++__loop_count_;
4210}
4211
4212template <class _CharT, class _Traits>
4213void
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00004214basic_regex<_CharT, _Traits>::__push_char(value_type __c)
4215{
Howard Hinnant173968a2010-07-13 21:48:06 +00004216 if (flags() & icase)
Howard Hinnante34f17d2010-07-12 19:11:27 +00004217 __end_->first() = new __match_char_icase<_CharT, _Traits>
4218 (__traits_, __c, __end_->first());
Howard Hinnant173968a2010-07-13 21:48:06 +00004219 else if (flags() & collate)
Howard Hinnante34f17d2010-07-12 19:11:27 +00004220 __end_->first() = new __match_char_collate<_CharT, _Traits>
4221 (__traits_, __c, __end_->first());
4222 else
4223 __end_->first() = new __match_char<_CharT>(__c, __end_->first());
Howard Hinnante77aa5e2010-07-08 17:43:58 +00004224 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00004225}
4226
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00004227template <class _CharT, class _Traits>
4228void
4229basic_regex<_CharT, _Traits>::__push_begin_marked_subexpression()
4230{
Howard Hinnant68025ed2010-07-14 15:45:11 +00004231 if (!(__flags_ & nosubs))
4232 {
4233 __end_->first() =
4234 new __begin_marked_subexpression<_CharT>(++__marked_count_,
4235 __end_->first());
4236 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4237 }
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00004238}
4239
4240template <class _CharT, class _Traits>
4241void
4242basic_regex<_CharT, _Traits>::__push_end_marked_subexpression(unsigned __sub)
4243{
Howard Hinnant68025ed2010-07-14 15:45:11 +00004244 if (!(__flags_ & nosubs))
4245 {
4246 __end_->first() =
4247 new __end_marked_subexpression<_CharT>(__sub, __end_->first());
4248 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4249 }
Howard Hinnant0dca5fc2010-06-30 20:30:19 +00004250}
4251
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00004252template <class _CharT, class _Traits>
4253void
4254basic_regex<_CharT, _Traits>::__push_r_anchor()
4255{
4256 __end_->first() = new __r_anchor<_CharT>(__end_->first());
4257 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4258}
4259
Howard Hinnantac303862010-07-12 15:51:17 +00004260template <class _CharT, class _Traits>
4261void
4262basic_regex<_CharT, _Traits>::__push_match_any()
4263{
4264 __end_->first() = new __match_any<_CharT>(__end_->first());
4265 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4266}
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00004267
Howard Hinnantcba352d2010-07-12 18:16:05 +00004268template <class _CharT, class _Traits>
4269void
Howard Hinnant17615b02010-07-27 01:25:38 +00004270basic_regex<_CharT, _Traits>::__push_match_any_but_newline()
4271{
4272 __end_->first() = new __match_any_but_newline<_CharT>(__end_->first());
4273 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4274}
4275
4276template <class _CharT, class _Traits>
4277void
Howard Hinnant2ade7c22010-07-22 17:53:24 +00004278basic_regex<_CharT, _Traits>::__push_empty()
4279{
4280 __end_->first() = new __empty_state<_CharT>(__end_->first());
4281 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4282}
4283
4284template <class _CharT, class _Traits>
4285void
Howard Hinnant17615b02010-07-27 01:25:38 +00004286basic_regex<_CharT, _Traits>::__push_word_boundary(bool __invert)
4287{
4288 __end_->first() = new __word_boundary<_CharT, _Traits>(__traits_, __invert,
4289 __end_->first());
4290 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4291}
4292
4293template <class _CharT, class _Traits>
4294void
Howard Hinnantcba352d2010-07-12 18:16:05 +00004295basic_regex<_CharT, _Traits>::__push_back_ref(int __i)
4296{
Howard Hinnant173968a2010-07-13 21:48:06 +00004297 if (flags() & icase)
Howard Hinnante34f17d2010-07-12 19:11:27 +00004298 __end_->first() = new __back_ref_icase<_CharT, _Traits>
4299 (__traits_, __i, __end_->first());
Howard Hinnant173968a2010-07-13 21:48:06 +00004300 else if (flags() & collate)
Howard Hinnante34f17d2010-07-12 19:11:27 +00004301 __end_->first() = new __back_ref_collate<_CharT, _Traits>
4302 (__traits_, __i, __end_->first());
4303 else
4304 __end_->first() = new __back_ref<_CharT>(__i, __end_->first());
Howard Hinnantcba352d2010-07-12 18:16:05 +00004305 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4306}
4307
Howard Hinnant173968a2010-07-13 21:48:06 +00004308template <class _CharT, class _Traits>
Howard Hinnantaa698082010-07-16 19:08:36 +00004309void
4310basic_regex<_CharT, _Traits>::__push_alternation(__owns_one_state<_CharT>* __sa,
4311 __owns_one_state<_CharT>* __ea)
4312{
4313 __sa->first() = new __alternate<_CharT>(
4314 static_cast<__owns_one_state<_CharT>*>(__sa->first()),
4315 static_cast<__owns_one_state<_CharT>*>(__ea->first()));
4316 __ea->first() = nullptr;
4317 __ea->first() = new __empty_state<_CharT>(__end_->first());
4318 __end_->first() = nullptr;
4319 __end_->first() = new __empty_non_own_state<_CharT>(__ea->first());
4320 __end_ = static_cast<__owns_one_state<_CharT>*>(__ea->first());
4321}
4322
4323template <class _CharT, class _Traits>
Howard Hinnant173968a2010-07-13 21:48:06 +00004324__bracket_expression<_CharT, _Traits>*
4325basic_regex<_CharT, _Traits>::__start_matching_list(bool __negate)
4326{
4327 __bracket_expression<_CharT, _Traits>* __r =
4328 new __bracket_expression<_CharT, _Traits>(__traits_, __end_->first(),
4329 __negate, __flags_ & icase,
4330 __flags_ & collate);
4331 __end_->first() = __r;
4332 __end_ = __r;
4333 return __r;
4334}
4335
Howard Hinnante9de5ff2010-07-27 22:20:32 +00004336template <class _CharT, class _Traits>
4337void
4338basic_regex<_CharT, _Traits>::__push_lookahead(const basic_regex& __exp,
4339 bool __invert)
4340{
4341 __end_->first() = new __lookahead<_CharT, _Traits>(__exp, __invert,
4342 __end_->first());
4343 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4344}
4345
Howard Hinnant8c2c18d2010-06-24 21:28:00 +00004346typedef basic_regex<char> regex;
4347typedef basic_regex<wchar_t> wregex;
4348
Howard Hinnantcd85b9e2010-06-29 18:37:43 +00004349// sub_match
4350
4351template <class _BidirectionalIterator>
4352class sub_match
4353 : public pair<_BidirectionalIterator, _BidirectionalIterator>
4354{
4355public:
4356 typedef _BidirectionalIterator iterator;
4357 typedef typename iterator_traits<iterator>::value_type value_type;
4358 typedef typename iterator_traits<iterator>::difference_type difference_type;
4359 typedef basic_string<value_type> string_type;
4360
4361 bool matched;
4362
4363 difference_type length() const
4364 {return matched ? _STD::distance(this->first, this->second) : 0;}
4365 string_type str() const
4366 {return matched ? string_type(this->first, this->second) : string_type();}
4367 operator string_type() const
4368 {return str();}
4369
4370 int compare(const sub_match& __s) const
4371 {return str().compare(__s.str());}
4372 int compare(const string_type& __s) const
4373 {return str().compare(__s);}
4374 int compare(const value_type* __s) const
4375 {return str().compare(__s);}
4376};
4377
4378typedef sub_match<const char*> csub_match;
4379typedef sub_match<const wchar_t*> wcsub_match;
4380typedef sub_match<string::const_iterator> ssub_match;
4381typedef sub_match<wstring::const_iterator> wssub_match;
4382
4383template <class _BiIter>
4384inline _LIBCPP_INLINE_VISIBILITY
4385bool
4386operator==(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
4387{
4388 return __x.compare(__y) == 0;
4389}
4390
4391template <class _BiIter>
4392inline _LIBCPP_INLINE_VISIBILITY
4393bool
4394operator!=(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
4395{
4396 return !(__x == __y);
4397}
4398
4399template <class _BiIter>
4400inline _LIBCPP_INLINE_VISIBILITY
4401bool
4402operator<(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
4403{
4404 return __x.compare(__y) < 0;
4405}
4406
4407template <class _BiIter>
4408inline _LIBCPP_INLINE_VISIBILITY
4409bool
4410operator<=(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
4411{
4412 return !(__y < __x);
4413}
4414
4415template <class _BiIter>
4416inline _LIBCPP_INLINE_VISIBILITY
4417bool
4418operator>=(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
4419{
4420 return !(__x < __y);
4421}
4422
4423template <class _BiIter>
4424inline _LIBCPP_INLINE_VISIBILITY
4425bool
4426operator>(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
4427{
4428 return __y < __x;
4429}
4430
4431template <class _BiIter, class _ST, class _SA>
4432inline _LIBCPP_INLINE_VISIBILITY
4433bool
4434operator==(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4435 const sub_match<_BiIter>& __y)
4436{
4437 return __y.compare(__x.c_str()) == 0;
4438}
4439
4440template <class _BiIter, class _ST, class _SA>
4441inline _LIBCPP_INLINE_VISIBILITY
4442bool
4443operator!=(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4444 const sub_match<_BiIter>& __y)
4445{
4446 return !(__x == __y);
4447}
4448
4449template <class _BiIter, class _ST, class _SA>
4450inline _LIBCPP_INLINE_VISIBILITY
4451bool
4452operator<(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4453 const sub_match<_BiIter>& __y)
4454{
4455 return __y.compare(__x.c_str()) > 0;
4456}
4457
4458template <class _BiIter, class _ST, class _SA>
4459inline _LIBCPP_INLINE_VISIBILITY
4460bool
4461operator>(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4462 const sub_match<_BiIter>& __y)
4463{
4464 return __y < __x;
4465}
4466
4467template <class _BiIter, class _ST, class _SA>
4468inline _LIBCPP_INLINE_VISIBILITY
4469bool operator>=(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4470 const sub_match<_BiIter>& __y)
4471{
4472 return !(__x < __y);
4473}
4474
4475template <class _BiIter, class _ST, class _SA>
4476inline _LIBCPP_INLINE_VISIBILITY
4477bool
4478operator<=(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4479 const sub_match<_BiIter>& __y)
4480{
4481 return !(__y < __x);
4482}
4483
4484template <class _BiIter, class _ST, class _SA>
4485inline _LIBCPP_INLINE_VISIBILITY
4486bool
4487operator==(const sub_match<_BiIter>& __x,
4488 const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
4489{
4490 return __x.compare(__y.c_str()) == 0;
4491}
4492
4493template <class _BiIter, class _ST, class _SA>
4494inline _LIBCPP_INLINE_VISIBILITY
4495bool
4496operator!=(const sub_match<_BiIter>& __x,
4497 const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
4498{
4499 return !(__x == __y);
4500}
4501
4502template <class _BiIter, class _ST, class _SA>
4503inline _LIBCPP_INLINE_VISIBILITY
4504bool
4505operator<(const sub_match<_BiIter>& __x,
4506 const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
4507{
4508 return __x.compare(__y.c_str()) < 0;
4509}
4510
4511template <class _BiIter, class _ST, class _SA>
4512inline _LIBCPP_INLINE_VISIBILITY
4513bool operator>(const sub_match<_BiIter>& __x,
4514 const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
4515{
4516 return __y < __x;
4517}
4518
4519template <class _BiIter, class _ST, class _SA>
4520inline _LIBCPP_INLINE_VISIBILITY
4521bool
4522operator>=(const sub_match<_BiIter>& __x,
4523 const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
4524{
4525 return !(__x < __y);
4526}
4527
4528template <class _BiIter, class _ST, class _SA>
4529inline _LIBCPP_INLINE_VISIBILITY
4530bool
4531operator<=(const sub_match<_BiIter>& __x,
4532 const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
4533{
4534 return !(__y < __x);
4535}
4536
4537template <class _BiIter>
4538inline _LIBCPP_INLINE_VISIBILITY
4539bool
4540operator==(typename iterator_traits<_BiIter>::value_type const* __x,
4541 const sub_match<_BiIter>& __y)
4542{
4543 return __y.compare(__x) == 0;
4544}
4545
4546template <class _BiIter>
4547inline _LIBCPP_INLINE_VISIBILITY
4548bool
4549operator!=(typename iterator_traits<_BiIter>::value_type const* __x,
4550 const sub_match<_BiIter>& __y)
4551{
4552 return !(__x == __y);
4553}
4554
4555template <class _BiIter>
4556inline _LIBCPP_INLINE_VISIBILITY
4557bool
4558operator<(typename iterator_traits<_BiIter>::value_type const* __x,
4559 const sub_match<_BiIter>& __y)
4560{
4561 return __y.compare(__x) > 0;
4562}
4563
4564template <class _BiIter>
4565inline _LIBCPP_INLINE_VISIBILITY
4566bool
4567operator>(typename iterator_traits<_BiIter>::value_type const* __x,
4568 const sub_match<_BiIter>& __y)
4569{
4570 return __y < __x;
4571}
4572
4573template <class _BiIter>
4574inline _LIBCPP_INLINE_VISIBILITY
4575bool
4576operator>=(typename iterator_traits<_BiIter>::value_type const* __x,
4577 const sub_match<_BiIter>& __y)
4578{
4579 return !(__x < __y);
4580}
4581
4582template <class _BiIter>
4583inline _LIBCPP_INLINE_VISIBILITY
4584bool
4585operator<=(typename iterator_traits<_BiIter>::value_type const* __x,
4586 const sub_match<_BiIter>& __y)
4587{
4588 return !(__y < __x);
4589}
4590
4591template <class _BiIter>
4592inline _LIBCPP_INLINE_VISIBILITY
4593bool
4594operator==(const sub_match<_BiIter>& __x,
4595 typename iterator_traits<_BiIter>::value_type const* __y)
4596{
4597 return __x.compare(__y) == 0;
4598}
4599
4600template <class _BiIter>
4601inline _LIBCPP_INLINE_VISIBILITY
4602bool
4603operator!=(const sub_match<_BiIter>& __x,
4604 typename iterator_traits<_BiIter>::value_type const* __y)
4605{
4606 return !(__x == __y);
4607}
4608
4609template <class _BiIter>
4610inline _LIBCPP_INLINE_VISIBILITY
4611bool
4612operator<(const sub_match<_BiIter>& __x,
4613 typename iterator_traits<_BiIter>::value_type const* __y)
4614{
4615 return __x.compare(__y) < 0;
4616}
4617
4618template <class _BiIter>
4619inline _LIBCPP_INLINE_VISIBILITY
4620bool
4621operator>(const sub_match<_BiIter>& __x,
4622 typename iterator_traits<_BiIter>::value_type const* __y)
4623{
4624 return __y < __x;
4625}
4626
4627template <class _BiIter>
4628inline _LIBCPP_INLINE_VISIBILITY
4629bool
4630operator>=(const sub_match<_BiIter>& __x,
4631 typename iterator_traits<_BiIter>::value_type const* __y)
4632{
4633 return !(__x < __y);
4634}
4635
4636template <class _BiIter>
4637inline _LIBCPP_INLINE_VISIBILITY
4638bool
4639operator<=(const sub_match<_BiIter>& __x,
4640 typename iterator_traits<_BiIter>::value_type const* __y)
4641{
4642 return !(__y < __x);
4643}
4644
4645template <class _BiIter>
4646inline _LIBCPP_INLINE_VISIBILITY
4647bool
4648operator==(typename iterator_traits<_BiIter>::value_type const& __x,
4649 const sub_match<_BiIter>& __y)
4650{
4651 typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
4652 return __y.compare(string_type(1, __x)) == 0;
4653}
4654
4655template <class _BiIter>
4656inline _LIBCPP_INLINE_VISIBILITY
4657bool
4658operator!=(typename iterator_traits<_BiIter>::value_type const& __x,
4659 const sub_match<_BiIter>& __y)
4660{
4661 return !(__x == __y);
4662}
4663
4664template <class _BiIter>
4665inline _LIBCPP_INLINE_VISIBILITY
4666bool
4667operator<(typename iterator_traits<_BiIter>::value_type const& __x,
4668 const sub_match<_BiIter>& __y)
4669{
4670 typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
4671 return __y.compare(string_type(1, __x)) > 0;
4672}
4673
4674template <class _BiIter>
4675inline _LIBCPP_INLINE_VISIBILITY
4676bool
4677operator>(typename iterator_traits<_BiIter>::value_type const& __x,
4678 const sub_match<_BiIter>& __y)
4679{
4680 return __y < __x;
4681}
4682
4683template <class _BiIter>
4684inline _LIBCPP_INLINE_VISIBILITY
4685bool
4686operator>=(typename iterator_traits<_BiIter>::value_type const& __x,
4687 const sub_match<_BiIter>& __y)
4688{
4689 return !(__x < __y);
4690}
4691
4692template <class _BiIter>
4693inline _LIBCPP_INLINE_VISIBILITY
4694bool
4695operator<=(typename iterator_traits<_BiIter>::value_type const& __x,
4696 const sub_match<_BiIter>& __y)
4697{
4698 return !(__y < __x);
4699}
4700
4701template <class _BiIter>
4702inline _LIBCPP_INLINE_VISIBILITY
4703bool
4704operator==(const sub_match<_BiIter>& __x,
4705 typename iterator_traits<_BiIter>::value_type const& __y)
4706{
4707 typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
4708 return __x.compare(string_type(1, __y)) == 0;
4709}
4710
4711template <class _BiIter>
4712inline _LIBCPP_INLINE_VISIBILITY
4713bool
4714operator!=(const sub_match<_BiIter>& __x,
4715 typename iterator_traits<_BiIter>::value_type const& __y)
4716{
4717 return !(__x == __y);
4718}
4719
4720template <class _BiIter>
4721inline _LIBCPP_INLINE_VISIBILITY
4722bool
4723operator<(const sub_match<_BiIter>& __x,
4724 typename iterator_traits<_BiIter>::value_type const& __y)
4725{
4726 typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
4727 return __x.compare(string_type(1, __y)) < 0;
4728}
4729
4730template <class _BiIter>
4731inline _LIBCPP_INLINE_VISIBILITY
4732bool
4733operator>(const sub_match<_BiIter>& __x,
4734 typename iterator_traits<_BiIter>::value_type const& __y)
4735{
4736 return __y < __x;
4737}
4738
4739template <class _BiIter>
4740inline _LIBCPP_INLINE_VISIBILITY
4741bool
4742operator>=(const sub_match<_BiIter>& __x,
4743 typename iterator_traits<_BiIter>::value_type const& __y)
4744{
4745 return !(__x < __y);
4746}
4747
4748template <class _BiIter>
4749inline _LIBCPP_INLINE_VISIBILITY
4750bool
4751operator<=(const sub_match<_BiIter>& __x,
4752 typename iterator_traits<_BiIter>::value_type const& __y)
4753{
4754 return !(__y < __x);
4755}
4756
4757template <class _CharT, class _ST, class _BiIter>
4758inline _LIBCPP_INLINE_VISIBILITY
4759basic_ostream<_CharT, _ST>&
4760operator<<(basic_ostream<_CharT, _ST>& __os, const sub_match<_BiIter>& __m)
4761{
4762 return __os << __m.str();
4763}
4764
Howard Hinnant17615b02010-07-27 01:25:38 +00004765template <class _BidirectionalIterator, class _Allocator>
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00004766class match_results
4767{
4768public:
4769 typedef _Allocator allocator_type;
4770 typedef sub_match<_BidirectionalIterator> value_type;
4771private:
4772 typedef vector<value_type, allocator_type> __container_type;
4773
4774 __container_type __matches_;
4775 value_type __unmatched_;
4776 value_type __prefix_;
4777 value_type __suffix_;
4778public:
4779 typedef const value_type& const_reference;
4780 typedef const_reference reference;
4781 typedef typename __container_type::const_iterator const_iterator;
4782 typedef const_iterator iterator;
4783 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
4784 typedef typename allocator_traits<allocator_type>::size_type size_type;
4785 typedef typename iterator_traits<_BidirectionalIterator>::value_type char_type;
4786 typedef basic_string<char_type> string_type;
4787
4788 // construct/copy/destroy:
4789 explicit match_results(const allocator_type& __a = allocator_type());
4790// match_results(const match_results&) = default;
4791// match_results& operator=(const match_results&) = default;
4792#ifdef _LIBCPP_MOVE
4793// match_results(match_results&& __m) = default;
4794// match_results& operator=(match_results&& __m) = default;
4795#endif
4796// ~match_results() = default;
4797
4798 // size:
4799 size_type size() const {return __matches_.size();}
4800 size_type max_size() const {return __matches_.max_size();}
4801 bool empty() const {return size() == 0;}
4802
4803 // element access:
4804 difference_type length(size_type __sub = 0) const
4805 {return (*this)[__sub].length();}
4806 difference_type position(size_type __sub = 0) const
4807 {return _STD::distance(__prefix_.first, (*this)[__sub].first);}
4808 string_type str(size_type __sub = 0) const
4809 {return (*this)[__sub].str();}
4810 const_reference operator[](size_type __n) const
4811 {return __n < __matches_.size() ? __matches_[__n] : __unmatched_;}
4812
4813 const_reference prefix() const {return __prefix_;}
4814 const_reference suffix() const {return __suffix_;}
4815
4816 const_iterator begin() const {return empty() ? __matches_.end() : __matches_.begin() + 1;}
4817 const_iterator end() const {return __matches_.end();}
4818 const_iterator cbegin() const {return empty() ? __matches_.end() : __matches_.begin() + 1;}
4819 const_iterator cend() const {return __matches_.end();}
4820
4821 // format:
4822 template <class _OutputIter>
4823 _OutputIter
4824 format(_OutputIter __out, const char_type* __fmt_first,
4825 const char_type* __fmt_last,
4826 regex_constants::match_flag_type __flags = regex_constants::format_default) const;
4827 template <class _OutputIter, class _ST, class _SA>
4828 _OutputIter
4829 format(_OutputIter __out, const basic_string<char_type, _ST, _SA>& __fmt,
4830 regex_constants::match_flag_type __flags = regex_constants::format_default) const;
4831 template <class _ST, class _SA>
4832 basic_string<char_type, _ST, _SA>
4833 format(const basic_string<char_type, _ST, _SA>& __fmt,
4834 regex_constants::match_flag_type __flags = regex_constants::format_default) const;
4835 string_type
4836 format(const char_type* __fmt,
4837 regex_constants::match_flag_type __flags = regex_constants::format_default) const;
4838
4839 // allocator:
4840 allocator_type get_allocator() const {return __matches_.get_allocator();}
4841
4842 // swap:
4843 void swap(match_results& __m);
4844
Howard Hinnant22ce0b42010-07-14 21:14:52 +00004845 template <class _B, class _A>
4846 void __assign(_BidirectionalIterator __f, _BidirectionalIterator __l,
4847 const match_results<_B, _A>& __m)
4848 {
4849 _B __mf = __m.prefix().first;
4850 __matches_.resize(__m.size());
4851 for (size_type __i = 0; __i < __matches_.size(); ++__i)
4852 {
4853 __matches_[__i].first = next(__f, _STD::distance(__mf, __m[__i].first));
4854 __matches_[__i].second = next(__f, _STD::distance(__mf, __m[__i].second));
4855 __matches_[__i].matched = __m[__i].matched;
4856 }
4857 __unmatched_.first = __l;
4858 __unmatched_.second = __l;
4859 __unmatched_.matched = false;
4860 __prefix_.first = next(__f, _STD::distance(__mf, __m.prefix().first));
4861 __prefix_.second = next(__f, _STD::distance(__mf, __m.prefix().second));
4862 __prefix_.matched = __m.prefix().matched;
4863 __suffix_.first = next(__f, _STD::distance(__mf, __m.suffix().first));
4864 __suffix_.second = next(__f, _STD::distance(__mf, __m.suffix().second));
4865 __suffix_.matched = __m.suffix().matched;
4866 }
4867
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00004868private:
4869 void __init(unsigned __s,
4870 _BidirectionalIterator __f, _BidirectionalIterator __l);
4871
4872 template <class, class> friend class basic_regex;
4873
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00004874 template <class _B, class _A, class _C, class _T>
4875 friend
4876 bool
4877 regex_match(_B, _B, match_results<_B, _A>&, const basic_regex<_C, _T>&,
4878 regex_constants::match_flag_type);
Howard Hinnante9de5ff2010-07-27 22:20:32 +00004879
4880 template <class, class> friend class __lookahead;
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00004881};
4882
4883template <class _BidirectionalIterator, class _Allocator>
4884match_results<_BidirectionalIterator, _Allocator>::match_results(
4885 const allocator_type& __a)
4886 : __matches_(__a),
4887 __unmatched_(),
4888 __prefix_(),
4889 __suffix_()
4890{
4891}
4892
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00004893template <class _BidirectionalIterator, class _Allocator>
4894void
4895match_results<_BidirectionalIterator, _Allocator>::__init(unsigned __s,
4896 _BidirectionalIterator __f, _BidirectionalIterator __l)
4897{
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00004898 __unmatched_.first = __l;
4899 __unmatched_.second = __l;
4900 __unmatched_.matched = false;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00004901 __matches_.assign(__s, __unmatched_);
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00004902 __prefix_.first = __f;
4903 __prefix_.second = __f;
4904 __prefix_.matched = false;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00004905 __suffix_ = __unmatched_;
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00004906}
4907
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00004908typedef match_results<const char*> cmatch;
4909typedef match_results<const wchar_t*> wcmatch;
4910typedef match_results<string::const_iterator> smatch;
4911typedef match_results<wstring::const_iterator> wsmatch;
4912
4913template <class _BidirectionalIterator, class _Allocator>
4914 bool
4915 operator==(const match_results<_BidirectionalIterator, _Allocator>& __x,
4916 const match_results<_BidirectionalIterator, _Allocator>& __y);
4917
4918template <class _BidirectionalIterator, class _Allocator>
4919 bool
4920 operator!=(const match_results<_BidirectionalIterator, _Allocator>& __x,
4921 const match_results<_BidirectionalIterator, _Allocator>& __y);
4922
4923template <class _BidirectionalIterator, class _Allocator>
4924 void
4925 swap(match_results<_BidirectionalIterator, _Allocator>& __x,
4926 match_results<_BidirectionalIterator, _Allocator>& __y);
4927
4928// regex_search
4929
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00004930template <class _CharT, class _Traits>
Howard Hinnant17615b02010-07-27 01:25:38 +00004931template <class _Allocator>
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00004932bool
Howard Hinnantf8ce4592010-07-07 19:14:52 +00004933basic_regex<_CharT, _Traits>::__match_at_start_ecma(
Howard Hinnant17615b02010-07-27 01:25:38 +00004934 const _CharT* __first, const _CharT* __last,
4935 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00004936 regex_constants::match_flag_type __flags) const
4937{
Howard Hinnant17615b02010-07-27 01:25:38 +00004938 vector<__state> __states;
4939 ptrdiff_t __j = 0;
4940 ptrdiff_t _N = _STD::distance(__first, __last);
4941 __node* __st = __start_.get();
4942 if (__st)
4943 {
4944 __states.push_back(__state());
4945 __states.back().__do_ = 0;
4946 __states.back().__first_ = __first;
4947 __states.back().__current_ = __first;
4948 __states.back().__last_ = __last;
4949 __states.back().__sub_matches_.resize(mark_count());
4950 __states.back().__loop_data_.resize(__loop_count());
4951 __states.back().__node_ = __st;
4952 __states.back().__flags_ = __flags;
4953 bool __matched = false;
4954 do
4955 {
4956 __state& __s = __states.back();
4957 if (__s.__node_)
4958 __s.__node_->__exec(__s);
4959 switch (__s.__do_)
4960 {
4961 case __state::__end_state:
4962 __m.__matches_[0].first = __first;
4963 __m.__matches_[0].second = _STD::next(__first, __s.__current_ - __first);
4964 __m.__matches_[0].matched = true;
4965 for (unsigned __i = 0; __i < __s.__sub_matches_.size(); ++__i)
4966 __m.__matches_[__i+1] = __s.__sub_matches_[__i];
4967 return true;
4968 case __state::__accept_and_consume:
4969 case __state::__repeat:
4970 case __state::__accept_but_not_consume:
4971 break;
4972 case __state::__split:
4973 {
4974 __state __snext = __s;
4975 __s.__node_->__exec_split(true, __s);
4976 __snext.__node_->__exec_split(false, __snext);
4977 __states.push_back(_STD::move(__snext));
4978 }
4979 break;
4980 case __state::__reject:
4981 __states.pop_back();
4982 break;
4983 default:
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00004984 throw regex_error(regex_constants::__re_err_unknown);
Howard Hinnant17615b02010-07-27 01:25:38 +00004985 break;
4986 }
4987 } while (!__states.empty());
4988 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00004989 return false;
4990}
4991
4992template <class _CharT, class _Traits>
Howard Hinnant22ce0b42010-07-14 21:14:52 +00004993template <class _Allocator>
Howard Hinnantf8ce4592010-07-07 19:14:52 +00004994bool
4995basic_regex<_CharT, _Traits>::__match_at_start_posix_nosubs(
4996 const _CharT* __first, const _CharT* __last,
Howard Hinnant22ce0b42010-07-14 21:14:52 +00004997 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00004998 regex_constants::match_flag_type __flags) const
4999{
Howard Hinnantac303862010-07-12 15:51:17 +00005000 deque<__state> __states;
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005001 ptrdiff_t __highest_j = 0;
5002 ptrdiff_t _N = _STD::distance(__first, __last);
Howard Hinnantac303862010-07-12 15:51:17 +00005003 __node* __st = __start_.get();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005004 if (__st)
5005 {
Howard Hinnantac303862010-07-12 15:51:17 +00005006 __states.push_back(__state());
Howard Hinnantac303862010-07-12 15:51:17 +00005007 __states.back().__do_ = 0;
5008 __states.back().__first_ = __first;
5009 __states.back().__current_ = __first;
5010 __states.back().__last_ = __last;
5011 __states.back().__loop_data_.resize(__loop_count());
5012 __states.back().__node_ = __st;
5013 __states.back().__flags_ = __flags;
Howard Hinnant68025ed2010-07-14 15:45:11 +00005014 bool __matched = false;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005015 do
5016 {
Howard Hinnantac303862010-07-12 15:51:17 +00005017 __state& __s = __states.back();
5018 if (__s.__node_)
5019 __s.__node_->__exec(__s);
5020 switch (__s.__do_)
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005021 {
Howard Hinnantac303862010-07-12 15:51:17 +00005022 case __state::__end_state:
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005023 if (!__matched || __highest_j < __s.__current_ - __s.__first_)
Howard Hinnant68025ed2010-07-14 15:45:11 +00005024 __highest_j = __s.__current_ - __s.__first_;
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005025 __matched = true;
Howard Hinnantac303862010-07-12 15:51:17 +00005026 if (__highest_j == _N)
5027 __states.clear();
5028 else
5029 __states.pop_back();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005030 break;
Howard Hinnantac303862010-07-12 15:51:17 +00005031 case __state::__consume_input:
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005032 break;
Howard Hinnantac303862010-07-12 15:51:17 +00005033 case __state::__accept_and_consume:
Howard Hinnantac303862010-07-12 15:51:17 +00005034 __states.push_front(_STD::move(__s));
5035 __states.pop_back();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005036 break;
Howard Hinnantac303862010-07-12 15:51:17 +00005037 case __state::__repeat:
5038 case __state::__accept_but_not_consume:
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005039 break;
Howard Hinnantac303862010-07-12 15:51:17 +00005040 case __state::__split:
5041 {
5042 __state __snext = __s;
5043 __s.__node_->__exec_split(true, __s);
5044 __snext.__node_->__exec_split(false, __snext);
5045 __states.push_back(_STD::move(__snext));
5046 }
5047 break;
5048 case __state::__reject:
5049 __states.pop_back();
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005050 break;
5051 default:
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005052 throw regex_error(regex_constants::__re_err_unknown);
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005053 break;
5054 }
Howard Hinnantac303862010-07-12 15:51:17 +00005055 } while (!__states.empty());
Howard Hinnant68025ed2010-07-14 15:45:11 +00005056 if (__matched)
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005057 {
5058 __m.__matches_[0].first = __first;
5059 __m.__matches_[0].second = _STD::next(__first, __highest_j);
5060 __m.__matches_[0].matched = true;
5061 return true;
5062 }
5063 }
5064 return false;
5065}
5066
5067template <class _CharT, class _Traits>
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005068template <class _Allocator>
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005069bool
5070basic_regex<_CharT, _Traits>::__match_at_start_posix_subs(
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005071 const _CharT* __first, const _CharT* __last,
5072 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005073 regex_constants::match_flag_type __flags) const
5074{
Howard Hinnantac303862010-07-12 15:51:17 +00005075 vector<__state> __states;
Howard Hinnantac303862010-07-12 15:51:17 +00005076 __state __best_state;
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005077 ptrdiff_t __j = 0;
5078 ptrdiff_t __highest_j = 0;
5079 ptrdiff_t _N = _STD::distance(__first, __last);
Howard Hinnantac303862010-07-12 15:51:17 +00005080 __node* __st = __start_.get();
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005081 if (__st)
5082 {
Howard Hinnantac303862010-07-12 15:51:17 +00005083 __states.push_back(__state());
5084 __states.back().__do_ = 0;
5085 __states.back().__first_ = __first;
5086 __states.back().__current_ = __first;
5087 __states.back().__last_ = __last;
5088 __states.back().__sub_matches_.resize(mark_count());
5089 __states.back().__loop_data_.resize(__loop_count());
5090 __states.back().__node_ = __st;
5091 __states.back().__flags_ = __flags;
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005092 const _CharT* __current = __first;
Howard Hinnantac303862010-07-12 15:51:17 +00005093 bool __matched = false;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005094 do
5095 {
Howard Hinnantac303862010-07-12 15:51:17 +00005096 __state& __s = __states.back();
5097 if (__s.__node_)
5098 __s.__node_->__exec(__s);
5099 switch (__s.__do_)
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005100 {
Howard Hinnantac303862010-07-12 15:51:17 +00005101 case __state::__end_state:
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005102 if (!__matched || __highest_j < __s.__current_ - __s.__first_)
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005103 {
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005104 __highest_j = __s.__current_ - __s.__first_;
Howard Hinnantac303862010-07-12 15:51:17 +00005105 __best_state = __s;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005106 }
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005107 __matched = true;
5108 if (__highest_j == _N)
5109 __states.clear();
5110 else
5111 __states.pop_back();
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005112 break;
Howard Hinnantac303862010-07-12 15:51:17 +00005113 case __state::__accept_and_consume:
Howard Hinnantcba352d2010-07-12 18:16:05 +00005114 __j += __s.__current_ - __current;
5115 __current = __s.__current_;
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005116 break;
Howard Hinnantac303862010-07-12 15:51:17 +00005117 case __state::__repeat:
5118 case __state::__accept_but_not_consume:
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005119 break;
Howard Hinnantac303862010-07-12 15:51:17 +00005120 case __state::__split:
5121 {
5122 __state __snext = __s;
5123 __s.__node_->__exec_split(true, __s);
5124 __snext.__node_->__exec_split(false, __snext);
5125 __states.push_back(_STD::move(__snext));
5126 }
5127 break;
5128 case __state::__reject:
5129 __states.pop_back();
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005130 break;
5131 default:
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005132 throw regex_error(regex_constants::__re_err_unknown);
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005133 break;
5134 }
Howard Hinnantac303862010-07-12 15:51:17 +00005135 } while (!__states.empty());
5136 if (__matched)
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005137 {
5138 __m.__matches_[0].first = __first;
5139 __m.__matches_[0].second = _STD::next(__first, __highest_j);
5140 __m.__matches_[0].matched = true;
Howard Hinnantac303862010-07-12 15:51:17 +00005141 for (unsigned __i = 0; __i < __best_state.__sub_matches_.size(); ++__i)
5142 __m.__matches_[__i+1] = __best_state.__sub_matches_[__i];
Howard Hinnante77aa5e2010-07-08 17:43:58 +00005143 return true;
5144 }
5145 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005146 return false;
5147}
5148
5149template <class _CharT, class _Traits>
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005150template <class _Allocator>
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005151bool
5152basic_regex<_CharT, _Traits>::__match_at_start(
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005153 const _CharT* __first, const _CharT* __last,
5154 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005155 regex_constants::match_flag_type __flags) const
5156{
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005157 if ((__flags_ & 0x1F0) == ECMAScript)
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005158 return __match_at_start_ecma(__first, __last, __m, __flags);
5159 if (mark_count() == 0)
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005160 return __match_at_start_posix_nosubs(__first, __last, __m, __flags);
5161 return __match_at_start_posix_subs(__first, __last, __m, __flags);
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005162}
5163
5164template <class _CharT, class _Traits>
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005165template <class _Allocator>
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005166bool
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005167basic_regex<_CharT, _Traits>::__search(
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005168 const _CharT* __first, const _CharT* __last,
5169 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005170 regex_constants::match_flag_type __flags) const
5171{
Howard Hinnant37f9f9c2010-07-09 00:15:26 +00005172 if (__left_anchor_)
5173 __flags |= regex_constants::match_continuous;
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005174 __m.__init(1 + mark_count(), __first, __last);
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005175 if (__match_at_start(__first, __last, __m, __flags))
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005176 {
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005177 __m.__prefix_.second = __m[0].first;
5178 __m.__prefix_.matched = __m.__prefix_.first != __m.__prefix_.second;
5179 __m.__suffix_.first = __m[0].second;
5180 __m.__suffix_.matched = __m.__suffix_.first != __m.__suffix_.second;
5181 return true;
5182 }
5183 if (!(__flags & regex_constants::match_continuous))
5184 {
5185 __m.__matches_.assign(__m.size(), __m.__unmatched_);
5186 for (++__first; __first != __last; ++__first)
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005187 {
Howard Hinnantad2a7ab2010-07-27 17:24:17 +00005188 if (__match_at_start(__first, __last, __m, __flags))
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005189 {
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005190 __m.__prefix_.second = __m[0].first;
5191 __m.__prefix_.matched = __m.__prefix_.first != __m.__prefix_.second;
5192 __m.__suffix_.first = __m[0].second;
5193 __m.__suffix_.matched = __m.__suffix_.first != __m.__suffix_.second;
5194 return true;
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005195 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005196 __m.__matches_.assign(__m.size(), __m.__unmatched_);
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005197 }
5198 }
Howard Hinnantf8ce4592010-07-07 19:14:52 +00005199 __m.__matches_.clear();
5200 return false;
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005201}
5202
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005203template <class _BidirectionalIterator, class _Allocator, class _CharT, class _Traits>
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005204inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005205bool
5206regex_search(_BidirectionalIterator __first, _BidirectionalIterator __last,
5207 match_results<_BidirectionalIterator, _Allocator>& __m,
5208 const basic_regex<_CharT, _Traits>& __e,
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005209 regex_constants::match_flag_type __flags = regex_constants::match_default)
5210{
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005211 basic_string<_CharT> __s(__first, __last);
5212 match_results<const _CharT*> __mc;
5213 bool __r = __e.__search(__s.data(), __s.data() + __s.size(), __mc, __flags);
5214 __m.__assign(__first, __last, __mc);
5215 return __r;
5216}
5217
5218template <class _Allocator, class _CharT, class _Traits>
5219inline _LIBCPP_INLINE_VISIBILITY
5220bool
5221regex_search(const _CharT* __first, const _CharT* __last,
5222 match_results<const _CharT*, _Allocator>& __m,
5223 const basic_regex<_CharT, _Traits>& __e,
5224 regex_constants::match_flag_type __flags = regex_constants::match_default)
5225{
Howard Hinnant9b80f2b2010-06-30 17:22:19 +00005226 return __e.__search(__first, __last, __m, __flags);
5227}
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005228
5229template <class _BidirectionalIterator, class _CharT, class _Traits>
5230inline _LIBCPP_INLINE_VISIBILITY
5231bool
5232regex_search(_BidirectionalIterator __first, _BidirectionalIterator __last,
5233 const basic_regex<_CharT, _Traits>& __e,
5234 regex_constants::match_flag_type __flags = regex_constants::match_default)
5235{
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005236 basic_string<_CharT> __s(__first, __last);
5237 match_results<const _CharT*> __mc;
5238 return __e.__search(__s.data(), __s.data() + __s.size(), __mc, __flags);
5239}
5240
5241template <class _CharT, class _Traits>
5242inline _LIBCPP_INLINE_VISIBILITY
5243bool
5244regex_search(const _CharT* __first, const _CharT* __last,
5245 const basic_regex<_CharT, _Traits>& __e,
5246 regex_constants::match_flag_type __flags = regex_constants::match_default)
5247{
5248 match_results<const _CharT*> __mc;
5249 return __e.__search(__first, __last, __mc, __flags);
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005250}
5251
5252template <class _CharT, class _Allocator, class _Traits>
5253inline _LIBCPP_INLINE_VISIBILITY
5254bool
5255regex_search(const _CharT* __str, match_results<const _CharT*, _Allocator>& __m,
5256 const basic_regex<_CharT, _Traits>& __e,
5257 regex_constants::match_flag_type __flags = regex_constants::match_default)
5258{
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005259 return __e.__search(__str, __str + _Traits::length(__str), __m, __flags);
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005260}
5261
5262template <class _CharT, class _Traits>
5263inline _LIBCPP_INLINE_VISIBILITY
5264bool
5265regex_search(const _CharT* __str, const basic_regex<_CharT, _Traits>& __e,
5266 regex_constants::match_flag_type __flags = regex_constants::match_default)
5267{
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005268 match_results<const _CharT*> __m;
5269 return _STD::regex_search(__str, __m, __e, __flags);
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005270}
5271
5272template <class _ST, class _SA, class _CharT, class _Traits>
5273inline _LIBCPP_INLINE_VISIBILITY
5274bool
5275regex_search(const basic_string<_CharT, _ST, _SA>& __s,
5276 const basic_regex<_CharT, _Traits>& __e,
5277 regex_constants::match_flag_type __flags = regex_constants::match_default)
5278{
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005279 match_results<const _CharT*> __mc;
5280 return __e.__search(__s.data(), __s.data() + __s.size(), __mc, __flags);
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005281}
5282
5283template <class _ST, class _SA, class _Allocator, class _CharT, class _Traits>
5284inline _LIBCPP_INLINE_VISIBILITY
5285bool
5286regex_search(const basic_string<_CharT, _ST, _SA>& __s,
5287 match_results<typename basic_string<_CharT, _ST, _SA>::const_iterator, _Allocator>& __m,
5288 const basic_regex<_CharT, _Traits>& __e,
5289 regex_constants::match_flag_type __flags = regex_constants::match_default)
5290{
Howard Hinnant22ce0b42010-07-14 21:14:52 +00005291 match_results<const _CharT*> __mc;
5292 bool __r = __e.__search(__s.data(), __s.data() + __s.size(), __mc, __flags);
5293 __m.__assign(__s.begin(), __s.end(), __mc);
5294 return __r;
Howard Hinnant7e9d84b2010-06-30 00:21:42 +00005295}
5296
5297// regex_match
5298
5299template <class _BidirectionalIterator, class _Allocator, class _CharT, class _Traits>
5300bool
5301regex_match(_BidirectionalIterator __first, _BidirectionalIterator __last,
5302 match_results<_BidirectionalIterator, _Allocator>& __m,
5303 const basic_regex<_CharT, _Traits>& __e,
5304 regex_constants::match_flag_type __flags = regex_constants::match_default)
5305{
5306 bool __r = _STD::regex_search(__first, __last, __m, __e,
5307 __flags | regex_constants::match_continuous);
5308 if (__r)
5309 {
5310 __r = !__m.suffix().matched;
5311 if (!__r)
5312 __m.__matches_.clear();
5313 }
5314 return __r;
5315}
5316
5317template <class _BidirectionalIterator, class _CharT, class _Traits>
5318inline _LIBCPP_INLINE_VISIBILITY
5319bool
5320regex_match(_BidirectionalIterator __first, _BidirectionalIterator __last,
5321 const basic_regex<_CharT, _Traits>& __e,
5322 regex_constants::match_flag_type __flags = regex_constants::match_default)
5323{
5324 match_results<_BidirectionalIterator> __m;
5325 return _STD::regex_match(__first, __last, __m, __e, __flags);
5326}
5327
5328template <class _CharT, class _Allocator, class _Traits>
5329inline _LIBCPP_INLINE_VISIBILITY
5330bool
5331regex_match(const _CharT* __str, match_results<const _CharT*, _Allocator>& __m,
5332 const basic_regex<_CharT, _Traits>& __e,
5333 regex_constants::match_flag_type __flags = regex_constants::match_default)
5334{
5335 return _STD::regex_match(__str, __str + _Traits::length(__str), __m, __e, __flags);
5336}
5337
5338template <class _ST, class _SA, class _Allocator, class _CharT, class _Traits>
5339inline _LIBCPP_INLINE_VISIBILITY
5340bool
5341regex_match(const basic_string<_CharT, _ST, _SA>& __s,
5342 match_results<typename basic_string<_CharT, _ST, _SA>::const_iterator, _Allocator>& __m,
5343 const basic_regex<_CharT, _Traits>& __e,
5344 regex_constants::match_flag_type __flags = regex_constants::match_default)
5345{
5346 return _STD::regex_match(__s.begin(), __s.end(), __m, __e, __flags);
5347}
5348
5349template <class _CharT, class _Traits>
5350inline _LIBCPP_INLINE_VISIBILITY
5351bool
5352regex_match(const _CharT* __str, const basic_regex<_CharT, _Traits>& __e,
5353 regex_constants::match_flag_type __flags = regex_constants::match_default)
5354{
5355 return _STD::regex_match(__str, __str + _Traits::length(__str), __e, __flags);
5356}
5357
5358template <class _ST, class _SA, class _CharT, class _Traits>
5359inline _LIBCPP_INLINE_VISIBILITY
5360bool
5361regex_match(const basic_string<_CharT, _ST, _SA>& __s,
5362 const basic_regex<_CharT, _Traits>& __e,
5363 regex_constants::match_flag_type __flags = regex_constants::match_default)
5364{
5365 return _STD::regex_match(__s.begin(), __s.end(), __e, __flags);
5366}
5367
Howard Hinnant3257c982010-06-17 00:34:59 +00005368_LIBCPP_END_NAMESPACE_STD
5369
5370#endif // _LIBCPP_REGEX