blob: 6104ce3bcc4e73092421d37fd42969855ff4bf78 [file] [log] [blame]
Howard Hinnanta3af5a32010-06-17 00:34:59 +00001// -*- C++ -*-
2//===--------------------------- regex ------------------------------------===//
3//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnanta3af5a32010-06-17 00:34:59 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_REGEX
11#define _LIBCPP_REGEX
12
13/*
14 regex synopsis
15
16#include <initializer_list>
17
18namespace std
19{
20
21namespace regex_constants
22{
23
Arthur O'Dwyer4f981132020-06-25 15:31:03 -040024enum syntax_option_type
Howard Hinnanta3af5a32010-06-17 00:34:59 +000025{
26 icase = unspecified,
27 nosubs = unspecified,
28 optimize = unspecified,
29 collate = unspecified,
30 ECMAScript = unspecified,
31 basic = unspecified,
32 extended = unspecified,
33 awk = unspecified,
34 grep = unspecified,
Mark de Wevera989cce2020-11-18 18:09:13 +010035 egrep = unspecified,
36 multiline = unspecified
Howard Hinnanta3af5a32010-06-17 00:34:59 +000037};
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;
Hubert Tong1f1ae9c2016-08-02 21:34:48 +0000130 typedef traits traits_type;
131 typedef typename traits::string_type string_type;
Howard Hinnanta3af5a32010-06-17 00:34:59 +0000132 typedef regex_constants::syntax_option_type flag_type;
133 typedef typename traits::locale_type locale_type;
134
135 // constants:
136 static constexpr regex_constants::syntax_option_type icase = regex_constants::icase;
137 static constexpr regex_constants::syntax_option_type nosubs = regex_constants::nosubs;
138 static constexpr regex_constants::syntax_option_type optimize = regex_constants::optimize;
139 static constexpr regex_constants::syntax_option_type collate = regex_constants::collate;
140 static constexpr regex_constants::syntax_option_type ECMAScript = regex_constants::ECMAScript;
141 static constexpr regex_constants::syntax_option_type basic = regex_constants::basic;
142 static constexpr regex_constants::syntax_option_type extended = regex_constants::extended;
143 static constexpr regex_constants::syntax_option_type awk = regex_constants::awk;
144 static constexpr regex_constants::syntax_option_type grep = regex_constants::grep;
145 static constexpr regex_constants::syntax_option_type egrep = regex_constants::egrep;
Mark de Wevera989cce2020-11-18 18:09:13 +0100146 static constexpr regex_constants::syntax_option_type multiline = regex_constants::multiline;
Howard Hinnanta3af5a32010-06-17 00:34:59 +0000147
148 // construct/copy/destroy:
149 basic_regex();
150 explicit basic_regex(const charT* p, flag_type f = regex_constants::ECMAScript);
Hubert Tong19662862016-08-07 22:26:04 +0000151 basic_regex(const charT* p, size_t len, flag_type f = regex_constants::ECMAScript);
Howard Hinnanta3af5a32010-06-17 00:34:59 +0000152 basic_regex(const basic_regex&);
Howard Hinnant5ddd33c2012-07-21 01:31:58 +0000153 basic_regex(basic_regex&&) noexcept;
Howard Hinnanta3af5a32010-06-17 00:34:59 +0000154 template <class ST, class SA>
155 explicit basic_regex(const basic_string<charT, ST, SA>& p,
156 flag_type f = regex_constants::ECMAScript);
157 template <class ForwardIterator>
158 basic_regex(ForwardIterator first, ForwardIterator last,
159 flag_type f = regex_constants::ECMAScript);
160 basic_regex(initializer_list<charT>, flag_type = regex_constants::ECMAScript);
161
162 ~basic_regex();
163
164 basic_regex& operator=(const basic_regex&);
Howard Hinnant5ddd33c2012-07-21 01:31:58 +0000165 basic_regex& operator=(basic_regex&&) noexcept;
Howard Hinnanta3af5a32010-06-17 00:34:59 +0000166 basic_regex& operator=(const charT* ptr);
167 basic_regex& operator=(initializer_list<charT> il);
168 template <class ST, class SA>
169 basic_regex& operator=(const basic_string<charT, ST, SA>& p);
170
171 // assign:
172 basic_regex& assign(const basic_regex& that);
Howard Hinnant5ddd33c2012-07-21 01:31:58 +0000173 basic_regex& assign(basic_regex&& that) noexcept;
Marshall Clowd4028932019-09-25 16:40:30 +0000174 basic_regex& assign(const charT* ptr, flag_type f = regex_constants::ECMAScript);
175 basic_regex& assign(const charT* p, size_t len, flag_type f = regex_constants::ECMAScript);
Howard Hinnanta3af5a32010-06-17 00:34:59 +0000176 template <class string_traits, class A>
177 basic_regex& assign(const basic_string<charT, string_traits, A>& s,
Marshall Clowd4028932019-09-25 16:40:30 +0000178 flag_type f = regex_constants::ECMAScript);
Howard Hinnanta3af5a32010-06-17 00:34:59 +0000179 template <class InputIterator>
180 basic_regex& assign(InputIterator first, InputIterator last,
Marshall Clowd4028932019-09-25 16:40:30 +0000181 flag_type f = regex_constants::ECMAScript);
182 basic_regex& assign(initializer_list<charT>, flag_type f = regex_constants::ECMAScript);
Howard Hinnanta3af5a32010-06-17 00:34:59 +0000183
184 // const operations:
185 unsigned mark_count() const;
186 flag_type flags() const;
187
188 // locale:
189 locale_type imbue(locale_type loc);
190 locale_type getloc() const;
191
192 // swap:
193 void swap(basic_regex&);
194};
195
Marshall Clow2dce1f42018-05-23 01:57:02 +0000196template<class ForwardIterator>
197basic_regex(ForwardIterator, ForwardIterator,
198 regex_constants::syntax_option_type = regex_constants::ECMAScript)
199 -> basic_regex<typename iterator_traits<ForwardIterator>::value_type>; // C++17
200
Howard Hinnanta3af5a32010-06-17 00:34:59 +0000201typedef basic_regex<char> regex;
202typedef basic_regex<wchar_t> wregex;
203
204template <class charT, class traits>
205 void swap(basic_regex<charT, traits>& e1, basic_regex<charT, traits>& e2);
206
207template <class BidirectionalIterator>
208class sub_match
209 : public pair<BidirectionalIterator, BidirectionalIterator>
210{
211public:
212 typedef typename iterator_traits<BidirectionalIterator>::value_type value_type;
213 typedef typename iterator_traits<BidirectionalIterator>::difference_type difference_type;
214 typedef BidirectionalIterator iterator;
215 typedef basic_string<value_type> string_type;
216
217 bool matched;
218
Howard Hinnantb5c53a82010-12-08 21:07:55 +0000219 constexpr sub_match();
220
Howard Hinnanta3af5a32010-06-17 00:34:59 +0000221 difference_type length() const;
222 operator string_type() const;
223 string_type str() const;
224
225 int compare(const sub_match& s) const;
226 int compare(const string_type& s) const;
227 int compare(const value_type* s) const;
228};
229
230typedef sub_match<const char*> csub_match;
231typedef sub_match<const wchar_t*> wcsub_match;
232typedef sub_match<string::const_iterator> ssub_match;
233typedef sub_match<wstring::const_iterator> wssub_match;
234
235template <class BiIter>
236 bool
237 operator==(const sub_match<BiIter>& lhs, const sub_match<BiIter>& rhs);
238
239template <class BiIter>
240 bool
241 operator!=(const sub_match<BiIter>& lhs, const sub_match<BiIter>& rhs);
242
243template <class BiIter>
244 bool
245 operator<(const sub_match<BiIter>& lhs, const sub_match<BiIter>& rhs);
246
247template <class BiIter>
248 bool
249 operator<=(const sub_match<BiIter>& lhs, const sub_match<BiIter>& rhs);
250
251template <class BiIter>
252 bool
253 operator>=(const sub_match<BiIter>& lhs, const sub_match<BiIter>& rhs);
254
255template <class BiIter>
256 bool
257 operator>(const sub_match<BiIter>& lhs, 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
271 operator<(const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& lhs,
272 const sub_match<BiIter>& rhs);
273
274template <class BiIter, class ST, class SA>
275 bool
276 operator>(const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& lhs,
277 const sub_match<BiIter>& rhs);
278
279template <class BiIter, class ST, class SA>
280 bool operator>=(const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& lhs,
281 const sub_match<BiIter>& rhs);
282
283template <class BiIter, class ST, class SA>
284 bool
285 operator<=(const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& lhs,
286 const sub_match<BiIter>& 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
295 operator!=(const sub_match<BiIter>& lhs,
296 const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& rhs);
297
298template <class BiIter, class ST, class SA>
299 bool
300 operator<(const sub_match<BiIter>& lhs,
301 const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& rhs);
302
303template <class BiIter, class ST, class SA>
304 bool operator>(const sub_match<BiIter>& lhs,
305 const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& rhs);
306
307template <class BiIter, class ST, class SA>
308 bool
309 operator>=(const sub_match<BiIter>& lhs,
310 const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& rhs);
311
312template <class BiIter, class ST, class SA>
313 bool
314 operator<=(const sub_match<BiIter>& lhs,
315 const basic_string<typename iterator_traits<BiIter>::value_type, ST, SA>& 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>=(typename iterator_traits<BiIter>::value_type const* lhs,
340 const sub_match<BiIter>& rhs);
341
342template <class BiIter>
343 bool
344 operator<=(typename iterator_traits<BiIter>::value_type const* lhs,
345 const sub_match<BiIter>& 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>=(const sub_match<BiIter>& lhs,
370 typename iterator_traits<BiIter>::value_type const* rhs);
371
372template <class BiIter>
373 bool
374 operator<=(const sub_match<BiIter>& lhs,
375 typename iterator_traits<BiIter>::value_type const* 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>=(typename iterator_traits<BiIter>::value_type const& lhs,
400 const sub_match<BiIter>& rhs);
401
402template <class BiIter>
403 bool
404 operator<=(typename iterator_traits<BiIter>::value_type const& lhs,
405 const sub_match<BiIter>& 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 BiIter>
428 bool
429 operator>=(const sub_match<BiIter>& lhs,
430 typename iterator_traits<BiIter>::value_type const& rhs);
431
432template <class BiIter>
433 bool
434 operator<=(const sub_match<BiIter>& lhs,
435 typename iterator_traits<BiIter>::value_type const& rhs);
436
437template <class charT, class ST, class BiIter>
438 basic_ostream<charT, ST>&
439 operator<<(basic_ostream<charT, ST>& os, const sub_match<BiIter>& m);
440
441template <class BidirectionalIterator,
442 class Allocator = allocator<sub_match<BidirectionalIterator>>>
443class match_results
444{
445public:
446 typedef sub_match<BidirectionalIterator> value_type;
447 typedef const value_type& const_reference;
Marshall Clow96e06142014-02-26 01:56:31 +0000448 typedef value_type& reference;
Howard Hinnanta3af5a32010-06-17 00:34:59 +0000449 typedef /implementation-defined/ const_iterator;
450 typedef const_iterator iterator;
451 typedef typename iterator_traits<BidirectionalIterator>::difference_type difference_type;
452 typedef typename allocator_traits<Allocator>::size_type size_type;
453 typedef Allocator allocator_type;
454 typedef typename iterator_traits<BidirectionalIterator>::value_type char_type;
455 typedef basic_string<char_type> string_type;
456
457 // construct/copy/destroy:
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +0100458 explicit match_results(const Allocator& a = Allocator()); // before C++20
459 match_results() : match_results(Allocator()) {} // C++20
460 explicit match_results(const Allocator& a); // C++20
Howard Hinnanta3af5a32010-06-17 00:34:59 +0000461 match_results(const match_results& m);
Howard Hinnant5ddd33c2012-07-21 01:31:58 +0000462 match_results(match_results&& m) noexcept;
Howard Hinnanta3af5a32010-06-17 00:34:59 +0000463 match_results& operator=(const match_results& m);
464 match_results& operator=(match_results&& m);
465 ~match_results();
466
Howard Hinnantb5c53a82010-12-08 21:07:55 +0000467 bool ready() const;
468
Howard Hinnanta3af5a32010-06-17 00:34:59 +0000469 // size:
470 size_type size() const;
471 size_type max_size() const;
472 bool empty() const;
473
474 // element access:
475 difference_type length(size_type sub = 0) const;
476 difference_type position(size_type sub = 0) const;
477 string_type str(size_type sub = 0) const;
478 const_reference operator[](size_type n) const;
479
480 const_reference prefix() const;
481 const_reference suffix() const;
482
483 const_iterator begin() const;
484 const_iterator end() const;
485 const_iterator cbegin() const;
486 const_iterator cend() const;
487
488 // format:
489 template <class OutputIter>
490 OutputIter
491 format(OutputIter out, const char_type* fmt_first,
492 const char_type* fmt_last,
493 regex_constants::match_flag_type flags = regex_constants::format_default) const;
494 template <class OutputIter, class ST, class SA>
495 OutputIter
496 format(OutputIter out, const basic_string<char_type, ST, SA>& fmt,
497 regex_constants::match_flag_type flags = regex_constants::format_default) const;
498 template <class ST, class SA>
499 basic_string<char_type, ST, SA>
500 format(const basic_string<char_type, ST, SA>& fmt,
501 regex_constants::match_flag_type flags = regex_constants::format_default) const;
502 string_type
503 format(const char_type* fmt,
504 regex_constants::match_flag_type flags = regex_constants::format_default) const;
505
506 // allocator:
507 allocator_type get_allocator() const;
508
509 // swap:
510 void swap(match_results& that);
511};
512
513typedef match_results<const char*> cmatch;
514typedef match_results<const wchar_t*> wcmatch;
515typedef match_results<string::const_iterator> smatch;
516typedef match_results<wstring::const_iterator> wsmatch;
517
518template <class BidirectionalIterator, class Allocator>
519 bool
520 operator==(const match_results<BidirectionalIterator, Allocator>& m1,
521 const match_results<BidirectionalIterator, Allocator>& m2);
522
523template <class BidirectionalIterator, class Allocator>
524 bool
525 operator!=(const match_results<BidirectionalIterator, Allocator>& m1,
526 const match_results<BidirectionalIterator, Allocator>& m2);
527
528template <class BidirectionalIterator, class Allocator>
529 void
530 swap(match_results<BidirectionalIterator, Allocator>& m1,
531 match_results<BidirectionalIterator, Allocator>& m2);
532
533template <class BidirectionalIterator, class Allocator, class charT, class traits>
534 bool
535 regex_match(BidirectionalIterator first, BidirectionalIterator last,
536 match_results<BidirectionalIterator, Allocator>& m,
537 const basic_regex<charT, traits>& e,
538 regex_constants::match_flag_type flags = regex_constants::match_default);
539
540template <class BidirectionalIterator, class charT, class traits>
541 bool
542 regex_match(BidirectionalIterator first, BidirectionalIterator last,
543 const basic_regex<charT, traits>& e,
544 regex_constants::match_flag_type flags = regex_constants::match_default);
545
546template <class charT, class Allocator, class traits>
547 bool
548 regex_match(const charT* str, match_results<const charT*, Allocator>& m,
549 const basic_regex<charT, traits>& e,
550 regex_constants::match_flag_type flags = regex_constants::match_default);
551
552template <class ST, class SA, class Allocator, class charT, class traits>
553 bool
554 regex_match(const basic_string<charT, ST, SA>& s,
555 match_results<typename basic_string<charT, ST, SA>::const_iterator, Allocator>& m,
556 const basic_regex<charT, traits>& e,
557 regex_constants::match_flag_type flags = regex_constants::match_default);
558
Marshall Clow8c950072014-02-19 21:21:11 +0000559template <class ST, class SA, class Allocator, class charT, class traits>
560 bool
561 regex_match(const basic_string<charT, ST, SA>&& s,
562 match_results<typename basic_string<charT, ST, SA>::const_iterator, Allocator>& m,
563 const basic_regex<charT, traits>& e,
564 regex_constants::match_flag_type flags = regex_constants::match_default) = delete; // C++14
565
Howard Hinnanta3af5a32010-06-17 00:34:59 +0000566template <class charT, class traits>
567 bool
568 regex_match(const charT* str, const basic_regex<charT, traits>& e,
569 regex_constants::match_flag_type flags = regex_constants::match_default);
570
571template <class ST, class SA, class charT, class traits>
572 bool
573 regex_match(const basic_string<charT, ST, SA>& s,
574 const basic_regex<charT, traits>& e,
575 regex_constants::match_flag_type flags = regex_constants::match_default);
576
577template <class BidirectionalIterator, class Allocator, class charT, class traits>
578 bool
579 regex_search(BidirectionalIterator first, BidirectionalIterator last,
580 match_results<BidirectionalIterator, Allocator>& m,
581 const basic_regex<charT, traits>& e,
582 regex_constants::match_flag_type flags = regex_constants::match_default);
583
584template <class BidirectionalIterator, class charT, class traits>
585 bool
586 regex_search(BidirectionalIterator first, BidirectionalIterator last,
587 const basic_regex<charT, traits>& e,
588 regex_constants::match_flag_type flags = regex_constants::match_default);
589
590template <class charT, class Allocator, class traits>
591 bool
592 regex_search(const charT* str, match_results<const charT*, Allocator>& m,
593 const basic_regex<charT, traits>& e,
594 regex_constants::match_flag_type flags = regex_constants::match_default);
595
596template <class charT, class traits>
597 bool
598 regex_search(const charT* str, const basic_regex<charT, traits>& e,
599 regex_constants::match_flag_type flags = regex_constants::match_default);
600
601template <class ST, class SA, class charT, class traits>
602 bool
603 regex_search(const basic_string<charT, ST, SA>& s,
604 const basic_regex<charT, traits>& e,
605 regex_constants::match_flag_type flags = regex_constants::match_default);
606
607template <class ST, class SA, class Allocator, class charT, class traits>
608 bool
609 regex_search(const basic_string<charT, ST, SA>& s,
610 match_results<typename basic_string<charT, ST, SA>::const_iterator, Allocator>& m,
611 const basic_regex<charT, traits>& e,
612 regex_constants::match_flag_type flags = regex_constants::match_default);
613
Marshall Clow8c950072014-02-19 21:21:11 +0000614template <class ST, class SA, class Allocator, class charT, class traits>
615 bool
616 regex_search(const basic_string<charT, ST, SA>&& s,
617 match_results<typename basic_string<charT, ST, SA>::const_iterator, Allocator>& m,
618 const basic_regex<charT, traits>& e,
619 regex_constants::match_flag_type flags = regex_constants::match_default) = delete; // C++14
620
Howard Hinnanta3af5a32010-06-17 00:34:59 +0000621template <class OutputIterator, class BidirectionalIterator,
622 class traits, class charT, class ST, class SA>
623 OutputIterator
624 regex_replace(OutputIterator out,
625 BidirectionalIterator first, BidirectionalIterator last,
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 OutputIterator, class BidirectionalIterator,
631 class traits, class charT>
632 OutputIterator
633 regex_replace(OutputIterator out,
634 BidirectionalIterator first, BidirectionalIterator last,
635 const basic_regex<charT, traits>& e, const charT* fmt,
636 regex_constants::match_flag_type flags = regex_constants::match_default);
637
Arthur O'Dwyer4f981132020-06-25 15:31:03 -0400638template <class traits, class charT, class ST, class SA, class FST, class FSA>
Howard Hinnanta3af5a32010-06-17 00:34:59 +0000639 basic_string<charT, ST, SA>
640 regex_replace(const basic_string<charT, ST, SA>& s,
641 const basic_regex<charT, traits>& e,
642 const basic_string<charT, FST, FSA>& fmt,
643 regex_constants::match_flag_type flags = regex_constants::match_default);
644
645template <class traits, class charT, class ST, class SA>
646 basic_string<charT, ST, SA>
647 regex_replace(const basic_string<charT, ST, SA>& s,
648 const basic_regex<charT, traits>& e, const charT* fmt,
649 regex_constants::match_flag_type flags = regex_constants::match_default);
650
651template <class traits, class charT, class ST, class SA>
652 basic_string<charT>
653 regex_replace(const charT* s,
654 const basic_regex<charT, traits>& e,
655 const basic_string<charT, ST, SA>& fmt,
656 regex_constants::match_flag_type flags = regex_constants::match_default);
657
658template <class traits, class charT>
659 basic_string<charT>
660 regex_replace(const charT* s,
661 const basic_regex<charT, traits>& e,
662 const charT* fmt,
663 regex_constants::match_flag_type flags = regex_constants::match_default);
664
665template <class BidirectionalIterator,
666 class charT = typename iterator_traits< BidirectionalIterator>::value_type,
667 class traits = regex_traits<charT>>
668class regex_iterator
669{
670public:
671 typedef basic_regex<charT, traits> regex_type;
672 typedef match_results<BidirectionalIterator> value_type;
673 typedef ptrdiff_t difference_type;
674 typedef const value_type* pointer;
675 typedef const value_type& reference;
676 typedef forward_iterator_tag iterator_category;
677
678 regex_iterator();
679 regex_iterator(BidirectionalIterator a, BidirectionalIterator b,
680 const regex_type& re,
681 regex_constants::match_flag_type m = regex_constants::match_default);
Arthur O'Dwyer4f981132020-06-25 15:31:03 -0400682 regex_iterator(BidirectionalIterator a, BidirectionalIterator b,
683 const regex_type&& re,
684 regex_constants::match_flag_type m
Marshall Clow8c950072014-02-19 21:21:11 +0000685 = regex_constants::match_default) = delete; // C++14
Howard Hinnanta3af5a32010-06-17 00:34:59 +0000686 regex_iterator(const regex_iterator&);
687 regex_iterator& operator=(const regex_iterator&);
688
689 bool operator==(const regex_iterator&) const;
690 bool operator!=(const regex_iterator&) const;
691
692 const value_type& operator*() const;
693 const value_type* operator->() const;
694
695 regex_iterator& operator++();
696 regex_iterator operator++(int);
697};
698
699typedef regex_iterator<const char*> cregex_iterator;
700typedef regex_iterator<const wchar_t*> wcregex_iterator;
701typedef regex_iterator<string::const_iterator> sregex_iterator;
702typedef regex_iterator<wstring::const_iterator> wsregex_iterator;
703
704template <class BidirectionalIterator,
Arthur O'Dwyer4f981132020-06-25 15:31:03 -0400705 class charT = typename iterator_traits<BidirectionalIterator>::value_type,
Howard Hinnanta3af5a32010-06-17 00:34:59 +0000706 class traits = regex_traits<charT>>
707class regex_token_iterator
708{
709public:
710 typedef basic_regex<charT, traits> regex_type;
711 typedef sub_match<BidirectionalIterator> value_type;
712 typedef ptrdiff_t difference_type;
713 typedef const value_type* pointer;
714 typedef const value_type& reference;
715 typedef forward_iterator_tag iterator_category;
716
717 regex_token_iterator();
718 regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b,
719 const regex_type& re, int submatch = 0,
720 regex_constants::match_flag_type m = regex_constants::match_default);
721 regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b,
Marshall Clow8c950072014-02-19 21:21:11 +0000722 const regex_type&& re, int submatch = 0,
723 regex_constants::match_flag_type m = regex_constants::match_default) = delete; // C++14
724 regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b,
Howard Hinnanta3af5a32010-06-17 00:34:59 +0000725 const regex_type& re, const vector<int>& submatches,
726 regex_constants::match_flag_type m = regex_constants::match_default);
727 regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b,
Marshall Clow8c950072014-02-19 21:21:11 +0000728 const regex_type&& re, const vector<int>& submatches,
729 regex_constants::match_flag_type m = regex_constants::match_default) = delete; // C++14
730 regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b,
Howard Hinnanta3af5a32010-06-17 00:34:59 +0000731 const regex_type& re, initializer_list<int> submatches,
732 regex_constants::match_flag_type m = regex_constants::match_default);
Marshall Clow8c950072014-02-19 21:21:11 +0000733 regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b,
734 const regex_type&& re, initializer_list<int> submatches,
735 regex_constants::match_flag_type m = regex_constants::match_default) = delete; // C++14
Howard Hinnanta3af5a32010-06-17 00:34:59 +0000736 template <size_t N>
737 regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b,
738 const regex_type& re, const int (&submatches)[N],
739 regex_constants::match_flag_type m = regex_constants::match_default);
Marshall Clow8c950072014-02-19 21:21:11 +0000740 template <size_t N>
741 regex_token_iterator(BidirectionalIterator a, BidirectionalIterator b,
Arthur O'Dwyer4f981132020-06-25 15:31:03 -0400742 const regex_type&& re, const int (&submatches)[N],
743 regex_constants::match_flag_type m = regex_constants::match_default) = delete; // C++14
Howard Hinnanta3af5a32010-06-17 00:34:59 +0000744 regex_token_iterator(const regex_token_iterator&);
745 regex_token_iterator& operator=(const regex_token_iterator&);
746
747 bool operator==(const regex_token_iterator&) const;
748 bool operator!=(const regex_token_iterator&) const;
749
750 const value_type& operator*() const;
751 const value_type* operator->() const;
752
753 regex_token_iterator& operator++();
754 regex_token_iterator operator++(int);
755};
756
757typedef regex_token_iterator<const char*> cregex_token_iterator;
758typedef regex_token_iterator<const wchar_t*> wcregex_token_iterator;
759typedef regex_token_iterator<string::const_iterator> sregex_token_iterator;
760typedef regex_token_iterator<wstring::const_iterator> wsregex_token_iterator;
761
762} // std
763*/
764
765#include <__config>
766#include <stdexcept>
767#include <__locale>
Arthur O'Dwyer7deec122021-03-24 18:19:12 -0400768#include <compare>
Howard Hinnant6c891682010-06-24 21:28:00 +0000769#include <initializer_list>
Howard Hinnant67ad2132010-06-29 18:37:43 +0000770#include <utility>
771#include <iterator>
772#include <string>
Howard Hinnant65a3f3d2010-06-30 00:21:42 +0000773#include <memory>
774#include <vector>
Howard Hinnantaa0874c2010-07-12 15:51:17 +0000775#include <deque>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000776#include <version>
Howard Hinnanta3af5a32010-06-17 00:34:59 +0000777
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000778#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnanta3af5a32010-06-17 00:34:59 +0000779#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000780#endif
Howard Hinnanta3af5a32010-06-17 00:34:59 +0000781
Eric Fiselierf4433a32017-05-31 22:07:49 +0000782_LIBCPP_PUSH_MACROS
783#include <__undef_macros>
784
785
Marshall Clowd39d21d2017-09-12 17:56:59 +0000786#define _LIBCPP_REGEX_COMPLEXITY_FACTOR 4096
787
Howard Hinnanta3af5a32010-06-17 00:34:59 +0000788_LIBCPP_BEGIN_NAMESPACE_STD
789
790namespace regex_constants
791{
792
793// syntax_option_type
794
795enum syntax_option_type
796{
797 icase = 1 << 0,
798 nosubs = 1 << 1,
799 optimize = 1 << 2,
800 collate = 1 << 3,
Marshall Clow88a30872019-03-28 17:30:23 +0000801#ifdef _LIBCPP_ABI_REGEX_CONSTANTS_NONZERO
802 ECMAScript = 1 << 9,
803#else
Howard Hinnantebbc2b62010-07-27 17:24:17 +0000804 ECMAScript = 0,
Marshall Clow88a30872019-03-28 17:30:23 +0000805#endif
Howard Hinnantebbc2b62010-07-27 17:24:17 +0000806 basic = 1 << 4,
807 extended = 1 << 5,
808 awk = 1 << 6,
809 grep = 1 << 7,
Mark de Wevera989cce2020-11-18 18:09:13 +0100810 egrep = 1 << 8,
811 // 1 << 9 may be used by ECMAScript
812 multiline = 1 << 10
Howard Hinnanta3af5a32010-06-17 00:34:59 +0000813};
814
Marshall Clow88a30872019-03-28 17:30:23 +0000815inline _LIBCPP_CONSTEXPR
816syntax_option_type __get_grammar(syntax_option_type __g)
817{
818#ifdef _LIBCPP_ABI_REGEX_CONSTANTS_NONZERO
819 return static_cast<syntax_option_type>(__g & 0x3F0);
820#else
821 return static_cast<syntax_option_type>(__g & 0x1F0);
822#endif
823}
824
Howard Hinnant7ca9d942010-09-23 15:13:20 +0000825inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5ddd33c2012-07-21 01:31:58 +0000826_LIBCPP_CONSTEXPR
Howard Hinnanta3af5a32010-06-17 00:34:59 +0000827syntax_option_type
828operator~(syntax_option_type __x)
829{
Marshall Clow924cffa2013-03-22 02:13:55 +0000830 return syntax_option_type(~int(__x) & 0x1FF);
Howard Hinnanta3af5a32010-06-17 00:34:59 +0000831}
832
Howard Hinnant7ca9d942010-09-23 15:13:20 +0000833inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5ddd33c2012-07-21 01:31:58 +0000834_LIBCPP_CONSTEXPR
Howard Hinnanta3af5a32010-06-17 00:34:59 +0000835syntax_option_type
836operator&(syntax_option_type __x, syntax_option_type __y)
837{
838 return syntax_option_type(int(__x) & int(__y));
839}
840
Howard Hinnant7ca9d942010-09-23 15:13:20 +0000841inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5ddd33c2012-07-21 01:31:58 +0000842_LIBCPP_CONSTEXPR
Howard Hinnanta3af5a32010-06-17 00:34:59 +0000843syntax_option_type
844operator|(syntax_option_type __x, syntax_option_type __y)
845{
846 return syntax_option_type(int(__x) | int(__y));
847}
848
Howard Hinnant7ca9d942010-09-23 15:13:20 +0000849inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5ddd33c2012-07-21 01:31:58 +0000850_LIBCPP_CONSTEXPR
Howard Hinnanta3af5a32010-06-17 00:34:59 +0000851syntax_option_type
852operator^(syntax_option_type __x, syntax_option_type __y)
853{
854 return syntax_option_type(int(__x) ^ int(__y));
855}
856
Howard Hinnant7ca9d942010-09-23 15:13:20 +0000857inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta3af5a32010-06-17 00:34:59 +0000858syntax_option_type&
859operator&=(syntax_option_type& __x, syntax_option_type __y)
860{
861 __x = __x & __y;
862 return __x;
863}
864
Howard Hinnant7ca9d942010-09-23 15:13:20 +0000865inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta3af5a32010-06-17 00:34:59 +0000866syntax_option_type&
867operator|=(syntax_option_type& __x, syntax_option_type __y)
868{
869 __x = __x | __y;
870 return __x;
871}
872
Howard Hinnant7ca9d942010-09-23 15:13:20 +0000873inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta3af5a32010-06-17 00:34:59 +0000874syntax_option_type&
875operator^=(syntax_option_type& __x, syntax_option_type __y)
876{
877 __x = __x ^ __y;
878 return __x;
879}
880
881// match_flag_type
882
883enum match_flag_type
884{
885 match_default = 0,
886 match_not_bol = 1 << 0,
887 match_not_eol = 1 << 1,
888 match_not_bow = 1 << 2,
889 match_not_eow = 1 << 3,
890 match_any = 1 << 4,
891 match_not_null = 1 << 5,
892 match_continuous = 1 << 6,
893 match_prev_avail = 1 << 7,
894 format_default = 0,
895 format_sed = 1 << 8,
896 format_no_copy = 1 << 9,
Howard Hinnantd3925342010-08-16 20:21:16 +0000897 format_first_only = 1 << 10,
Tim Shen11113f52016-10-27 21:40:34 +0000898 __no_update_pos = 1 << 11,
899 __full_match = 1 << 12
Howard Hinnanta3af5a32010-06-17 00:34:59 +0000900};
901
Howard Hinnant7ca9d942010-09-23 15:13:20 +0000902inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5ddd33c2012-07-21 01:31:58 +0000903_LIBCPP_CONSTEXPR
Howard Hinnanta3af5a32010-06-17 00:34:59 +0000904match_flag_type
905operator~(match_flag_type __x)
906{
Marshall Clow924cffa2013-03-22 02:13:55 +0000907 return match_flag_type(~int(__x) & 0x0FFF);
Howard Hinnanta3af5a32010-06-17 00:34:59 +0000908}
909
Howard Hinnant7ca9d942010-09-23 15:13:20 +0000910inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5ddd33c2012-07-21 01:31:58 +0000911_LIBCPP_CONSTEXPR
Howard Hinnanta3af5a32010-06-17 00:34:59 +0000912match_flag_type
913operator&(match_flag_type __x, match_flag_type __y)
914{
915 return match_flag_type(int(__x) & int(__y));
916}
917
Howard Hinnant7ca9d942010-09-23 15:13:20 +0000918inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5ddd33c2012-07-21 01:31:58 +0000919_LIBCPP_CONSTEXPR
Howard Hinnanta3af5a32010-06-17 00:34:59 +0000920match_flag_type
921operator|(match_flag_type __x, match_flag_type __y)
922{
923 return match_flag_type(int(__x) | int(__y));
924}
925
Howard Hinnant7ca9d942010-09-23 15:13:20 +0000926inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5ddd33c2012-07-21 01:31:58 +0000927_LIBCPP_CONSTEXPR
Howard Hinnanta3af5a32010-06-17 00:34:59 +0000928match_flag_type
929operator^(match_flag_type __x, match_flag_type __y)
930{
931 return match_flag_type(int(__x) ^ int(__y));
932}
933
Howard Hinnant7ca9d942010-09-23 15:13:20 +0000934inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta3af5a32010-06-17 00:34:59 +0000935match_flag_type&
936operator&=(match_flag_type& __x, match_flag_type __y)
937{
938 __x = __x & __y;
939 return __x;
940}
941
Howard Hinnant7ca9d942010-09-23 15:13:20 +0000942inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta3af5a32010-06-17 00:34:59 +0000943match_flag_type&
944operator|=(match_flag_type& __x, match_flag_type __y)
945{
946 __x = __x | __y;
947 return __x;
948}
949
Howard Hinnant7ca9d942010-09-23 15:13:20 +0000950inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta3af5a32010-06-17 00:34:59 +0000951match_flag_type&
952operator^=(match_flag_type& __x, match_flag_type __y)
953{
954 __x = __x ^ __y;
955 return __x;
956}
957
958enum error_type
959{
960 error_collate = 1,
961 error_ctype,
962 error_escape,
963 error_backref,
964 error_brack,
965 error_paren,
966 error_brace,
967 error_badbrace,
968 error_range,
969 error_space,
970 error_badrepeat,
971 error_complexity,
Howard Hinnant6c891682010-06-24 21:28:00 +0000972 error_stack,
Howard Hinnantebbc2b62010-07-27 17:24:17 +0000973 __re_err_grammar,
974 __re_err_empty,
Mark de Wevera0ad9762019-11-09 17:01:37 +0100975 __re_err_unknown,
976 __re_err_parse
Howard Hinnanta3af5a32010-06-17 00:34:59 +0000977};
978
979} // regex_constants
980
981class _LIBCPP_EXCEPTION_ABI regex_error
982 : public runtime_error
983{
984 regex_constants::error_type __code_;
985public:
986 explicit regex_error(regex_constants::error_type __ecode);
Dimitry Andric47269ce2020-03-13 19:36:26 +0100987 regex_error(const regex_error&) _NOEXCEPT = default;
988 virtual ~regex_error() _NOEXCEPT;
Howard Hinnant7ca9d942010-09-23 15:13:20 +0000989 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta3af5a32010-06-17 00:34:59 +0000990 regex_constants::error_type code() const {return __code_;}
991};
992
Marshall Clowc8ccc292015-07-28 13:30:47 +0000993template <regex_constants::error_type _Ev>
Louis Dionne16fe2952018-07-11 23:14:33 +0000994_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clowc8ccc292015-07-28 13:30:47 +0000995void __throw_regex_error()
996{
997#ifndef _LIBCPP_NO_EXCEPTIONS
Marshall Clow8bf61bb2015-08-17 21:14:16 +0000998 throw regex_error(_Ev);
999#else
Marshall Clow8fea1612016-08-25 15:09:01 +00001000 _VSTD::abort();
Marshall Clowc8ccc292015-07-28 13:30:47 +00001001#endif
1002}
1003
Howard Hinnanta3af5a32010-06-17 00:34:59 +00001004template <class _CharT>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001005struct _LIBCPP_TEMPLATE_VIS regex_traits
Howard Hinnanta3af5a32010-06-17 00:34:59 +00001006{
1007public:
1008 typedef _CharT char_type;
1009 typedef basic_string<char_type> string_type;
1010 typedef locale locale_type;
Dan Albert70ee07e2020-04-06 13:34:27 -07001011#ifdef __BIONIC__
1012 // Originally bionic's ctype_base used its own ctype masks because the
1013 // builtin ctype implementation wasn't in libc++ yet. Bionic's ctype mask
1014 // was only 8 bits wide and already saturated, so it used a wider type here
1015 // to make room for __regex_word (then a part of this class rather than
1016 // ctype_base). Bionic has since moved to the builtin ctype_base
1017 // implementation, but this was not updated to match. Since then Android has
1018 // needed to maintain a stable libc++ ABI, and this can't be changed without
1019 // an ABI break.
1020 typedef uint16_t char_class_type;
1021#else
Howard Hinnant40b45e12010-06-21 21:01:43 +00001022 typedef ctype_base::mask char_class_type;
Dan Albert70ee07e2020-04-06 13:34:27 -07001023#endif
Howard Hinnanta3af5a32010-06-17 00:34:59 +00001024
Mikhail Maltsev014ed062019-06-14 09:04:16 +00001025 static const char_class_type __regex_word = ctype_base::__regex_word;
Howard Hinnanta3af5a32010-06-17 00:34:59 +00001026private:
1027 locale __loc_;
1028 const ctype<char_type>* __ct_;
1029 const collate<char_type>* __col_;
1030
1031public:
1032 regex_traits();
1033
Howard Hinnant7ca9d942010-09-23 15:13:20 +00001034 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta3af5a32010-06-17 00:34:59 +00001035 static size_t length(const char_type* __p)
1036 {return char_traits<char_type>::length(__p);}
Howard Hinnant7ca9d942010-09-23 15:13:20 +00001037 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta3af5a32010-06-17 00:34:59 +00001038 char_type translate(char_type __c) const {return __c;}
1039 char_type translate_nocase(char_type __c) const;
1040 template <class _ForwardIterator>
1041 string_type
1042 transform(_ForwardIterator __f, _ForwardIterator __l) const;
1043 template <class _ForwardIterator>
Howard Hinnant7ca9d942010-09-23 15:13:20 +00001044 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta3af5a32010-06-17 00:34:59 +00001045 string_type
1046 transform_primary( _ForwardIterator __f, _ForwardIterator __l) const
1047 {return __transform_primary(__f, __l, char_type());}
1048 template <class _ForwardIterator>
Howard Hinnant7ca9d942010-09-23 15:13:20 +00001049 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta3af5a32010-06-17 00:34:59 +00001050 string_type
1051 lookup_collatename(_ForwardIterator __f, _ForwardIterator __l) const
1052 {return __lookup_collatename(__f, __l, char_type());}
1053 template <class _ForwardIterator>
Howard Hinnant7ca9d942010-09-23 15:13:20 +00001054 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta3af5a32010-06-17 00:34:59 +00001055 char_class_type
1056 lookup_classname(_ForwardIterator __f, _ForwardIterator __l,
Howard Hinnant40b45e12010-06-21 21:01:43 +00001057 bool __icase = false) const
1058 {return __lookup_classname(__f, __l, __icase, char_type());}
1059 bool isctype(char_type __c, char_class_type __m) const;
Howard Hinnant7ca9d942010-09-23 15:13:20 +00001060 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant40b45e12010-06-21 21:01:43 +00001061 int value(char_type __ch, int __radix) const
Marshall Clowef420192013-10-21 15:43:25 +00001062 {return __regex_traits_value(__ch, __radix);}
Howard Hinnanta3af5a32010-06-17 00:34:59 +00001063 locale_type imbue(locale_type __l);
Howard Hinnant7ca9d942010-09-23 15:13:20 +00001064 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta3af5a32010-06-17 00:34:59 +00001065 locale_type getloc()const {return __loc_;}
1066
1067private:
1068 void __init();
1069
1070 template <class _ForwardIterator>
1071 string_type
1072 __transform_primary(_ForwardIterator __f, _ForwardIterator __l, char) const;
1073 template <class _ForwardIterator>
1074 string_type
1075 __transform_primary(_ForwardIterator __f, _ForwardIterator __l, wchar_t) const;
1076
1077 template <class _ForwardIterator>
1078 string_type
1079 __lookup_collatename(_ForwardIterator __f, _ForwardIterator __l, char) const;
1080 template <class _ForwardIterator>
1081 string_type
1082 __lookup_collatename(_ForwardIterator __f, _ForwardIterator __l, wchar_t) const;
Howard Hinnant40b45e12010-06-21 21:01:43 +00001083
1084 template <class _ForwardIterator>
1085 char_class_type
1086 __lookup_classname(_ForwardIterator __f, _ForwardIterator __l,
1087 bool __icase, char) const;
1088 template <class _ForwardIterator>
1089 char_class_type
1090 __lookup_classname(_ForwardIterator __f, _ForwardIterator __l,
1091 bool __icase, wchar_t) const;
1092
Marshall Clowef420192013-10-21 15:43:25 +00001093 static int __regex_traits_value(unsigned char __ch, int __radix);
Howard Hinnant7ca9d942010-09-23 15:13:20 +00001094 _LIBCPP_INLINE_VISIBILITY
Marshall Clowef420192013-10-21 15:43:25 +00001095 int __regex_traits_value(char __ch, int __radix) const
1096 {return __regex_traits_value(static_cast<unsigned char>(__ch), __radix);}
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001097 _LIBCPP_INLINE_VISIBILITY
Marshall Clowef420192013-10-21 15:43:25 +00001098 int __regex_traits_value(wchar_t __ch, int __radix) const;
Howard Hinnanta3af5a32010-06-17 00:34:59 +00001099};
1100
1101template <class _CharT>
Howard Hinnant592cb612013-03-07 19:38:08 +00001102const typename regex_traits<_CharT>::char_class_type
1103regex_traits<_CharT>::__regex_word;
1104
1105template <class _CharT>
Howard Hinnanta3af5a32010-06-17 00:34:59 +00001106regex_traits<_CharT>::regex_traits()
1107{
1108 __init();
1109}
1110
1111template <class _CharT>
1112typename regex_traits<_CharT>::char_type
1113regex_traits<_CharT>::translate_nocase(char_type __c) const
1114{
1115 return __ct_->tolower(__c);
1116}
1117
1118template <class _CharT>
1119template <class _ForwardIterator>
1120typename regex_traits<_CharT>::string_type
1121regex_traits<_CharT>::transform(_ForwardIterator __f, _ForwardIterator __l) const
1122{
1123 string_type __s(__f, __l);
1124 return __col_->transform(__s.data(), __s.data() + __s.size());
1125}
1126
1127template <class _CharT>
1128void
1129regex_traits<_CharT>::__init()
1130{
1131 __ct_ = &use_facet<ctype<char_type> >(__loc_);
1132 __col_ = &use_facet<collate<char_type> >(__loc_);
1133}
1134
1135template <class _CharT>
1136typename regex_traits<_CharT>::locale_type
1137regex_traits<_CharT>::imbue(locale_type __l)
1138{
1139 locale __r = __loc_;
1140 __loc_ = __l;
1141 __init();
1142 return __r;
1143}
1144
1145// transform_primary is very FreeBSD-specific
1146
1147template <class _CharT>
1148template <class _ForwardIterator>
1149typename regex_traits<_CharT>::string_type
1150regex_traits<_CharT>::__transform_primary(_ForwardIterator __f,
1151 _ForwardIterator __l, char) const
1152{
1153 const string_type __s(__f, __l);
1154 string_type __d = __col_->transform(__s.data(), __s.data() + __s.size());
1155 switch (__d.size())
1156 {
1157 case 1:
1158 break;
1159 case 12:
1160 __d[11] = __d[3];
1161 break;
1162 default:
1163 __d.clear();
1164 break;
1165 }
1166 return __d;
1167}
1168
1169template <class _CharT>
1170template <class _ForwardIterator>
1171typename regex_traits<_CharT>::string_type
1172regex_traits<_CharT>::__transform_primary(_ForwardIterator __f,
1173 _ForwardIterator __l, wchar_t) const
1174{
1175 const string_type __s(__f, __l);
1176 string_type __d = __col_->transform(__s.data(), __s.data() + __s.size());
1177 switch (__d.size())
1178 {
1179 case 1:
1180 break;
1181 case 3:
1182 __d[2] = __d[0];
1183 break;
1184 default:
1185 __d.clear();
1186 break;
1187 }
1188 return __d;
1189}
1190
1191// lookup_collatename is very FreeBSD-specific
1192
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00001193_LIBCPP_FUNC_VIS string __get_collation_name(const char* __s);
Howard Hinnanta3af5a32010-06-17 00:34:59 +00001194
1195template <class _CharT>
1196template <class _ForwardIterator>
1197typename regex_traits<_CharT>::string_type
1198regex_traits<_CharT>::__lookup_collatename(_ForwardIterator __f,
1199 _ForwardIterator __l, char) const
1200{
1201 string_type __s(__f, __l);
1202 string_type __r;
1203 if (!__s.empty())
1204 {
1205 __r = __get_collation_name(__s.c_str());
1206 if (__r.empty() && __s.size() <= 2)
1207 {
1208 __r = __col_->transform(__s.data(), __s.data() + __s.size());
1209 if (__r.size() == 1 || __r.size() == 12)
1210 __r = __s;
1211 else
1212 __r.clear();
1213 }
1214 }
1215 return __r;
1216}
1217
1218template <class _CharT>
1219template <class _ForwardIterator>
1220typename regex_traits<_CharT>::string_type
1221regex_traits<_CharT>::__lookup_collatename(_ForwardIterator __f,
1222 _ForwardIterator __l, wchar_t) const
1223{
1224 string_type __s(__f, __l);
1225 string __n;
1226 __n.reserve(__s.size());
1227 for (typename string_type::const_iterator __i = __s.begin(), __e = __s.end();
1228 __i != __e; ++__i)
1229 {
1230 if (static_cast<unsigned>(*__i) >= 127)
1231 return string_type();
1232 __n.push_back(char(*__i));
1233 }
1234 string_type __r;
1235 if (!__s.empty())
1236 {
1237 __n = __get_collation_name(__n.c_str());
1238 if (!__n.empty())
1239 __r.assign(__n.begin(), __n.end());
1240 else if (__s.size() <= 2)
1241 {
1242 __r = __col_->transform(__s.data(), __s.data() + __s.size());
1243 if (__r.size() == 1 || __r.size() == 3)
1244 __r = __s;
1245 else
1246 __r.clear();
1247 }
1248 }
1249 return __r;
1250}
1251
Howard Hinnant40b45e12010-06-21 21:01:43 +00001252// lookup_classname
1253
Dan Albert49f384c2014-07-29 19:23:39 +00001254regex_traits<char>::char_class_type _LIBCPP_FUNC_VIS
1255__get_classname(const char* __s, bool __icase);
Howard Hinnant40b45e12010-06-21 21:01:43 +00001256
1257template <class _CharT>
1258template <class _ForwardIterator>
1259typename regex_traits<_CharT>::char_class_type
1260regex_traits<_CharT>::__lookup_classname(_ForwardIterator __f,
1261 _ForwardIterator __l,
1262 bool __icase, char) const
1263{
1264 string_type __s(__f, __l);
1265 __ct_->tolower(&__s[0], &__s[0] + __s.size());
1266 return __get_classname(__s.c_str(), __icase);
1267}
1268
1269template <class _CharT>
1270template <class _ForwardIterator>
1271typename regex_traits<_CharT>::char_class_type
1272regex_traits<_CharT>::__lookup_classname(_ForwardIterator __f,
1273 _ForwardIterator __l,
1274 bool __icase, wchar_t) const
1275{
1276 string_type __s(__f, __l);
1277 __ct_->tolower(&__s[0], &__s[0] + __s.size());
1278 string __n;
1279 __n.reserve(__s.size());
1280 for (typename string_type::const_iterator __i = __s.begin(), __e = __s.end();
1281 __i != __e; ++__i)
1282 {
1283 if (static_cast<unsigned>(*__i) >= 127)
1284 return char_class_type();
1285 __n.push_back(char(*__i));
1286 }
1287 return __get_classname(__n.c_str(), __icase);
1288}
1289
1290template <class _CharT>
1291bool
1292regex_traits<_CharT>::isctype(char_type __c, char_class_type __m) const
1293{
1294 if (__ct_->is(__m, __c))
1295 return true;
1296 return (__c == '_' && (__m & __regex_word));
1297}
1298
1299template <class _CharT>
1300int
Marshall Clowef420192013-10-21 15:43:25 +00001301regex_traits<_CharT>::__regex_traits_value(unsigned char __ch, int __radix)
Howard Hinnant40b45e12010-06-21 21:01:43 +00001302{
1303 if ((__ch & 0xF8u) == 0x30) // '0' <= __ch && __ch <= '7'
1304 return __ch - '0';
1305 if (__radix != 8)
1306 {
1307 if ((__ch & 0xFEu) == 0x38) // '8' <= __ch && __ch <= '9'
1308 return __ch - '0';
1309 if (__radix == 16)
1310 {
1311 __ch |= 0x20; // tolower
1312 if ('a' <= __ch && __ch <= 'f')
Howard Hinnant6c891682010-06-24 21:28:00 +00001313 return __ch - ('a' - 10);
Howard Hinnant40b45e12010-06-21 21:01:43 +00001314 }
1315 }
1316 return -1;
1317}
1318
1319template <class _CharT>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +00001320inline
Howard Hinnant40b45e12010-06-21 21:01:43 +00001321int
Marshall Clowef420192013-10-21 15:43:25 +00001322regex_traits<_CharT>::__regex_traits_value(wchar_t __ch, int __radix) const
Howard Hinnant40b45e12010-06-21 21:01:43 +00001323{
Marshall Clowef420192013-10-21 15:43:25 +00001324 return __regex_traits_value(static_cast<unsigned char>(__ct_->narrow(__ch, char_type())), __radix);
Howard Hinnant40b45e12010-06-21 21:01:43 +00001325}
1326
Howard Hinnantaa0874c2010-07-12 15:51:17 +00001327template <class _CharT> class __node;
1328
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001329template <class _BidirectionalIterator> class _LIBCPP_TEMPLATE_VIS sub_match;
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00001330
Howard Hinnant70d27852010-07-27 01:25:38 +00001331template <class _BidirectionalIterator,
1332 class _Allocator = allocator<sub_match<_BidirectionalIterator> > >
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001333class _LIBCPP_TEMPLATE_VIS match_results;
Howard Hinnant70d27852010-07-27 01:25:38 +00001334
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00001335template <class _CharT>
Howard Hinnantaa0874c2010-07-12 15:51:17 +00001336struct __state
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00001337{
1338 enum
1339 {
1340 __end_state = -1000,
1341 __consume_input, // -999
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00001342 __begin_marked_expr, // -998
1343 __end_marked_expr, // -997
Howard Hinnant5bf96132010-07-08 17:43:58 +00001344 __pop_state, // -996
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00001345 __accept_and_consume, // -995
1346 __accept_but_not_consume, // -994
1347 __reject, // -993
Howard Hinnantaa0874c2010-07-12 15:51:17 +00001348 __split,
1349 __repeat
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00001350 };
1351
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00001352 int __do_;
Howard Hinnantaa0874c2010-07-12 15:51:17 +00001353 const _CharT* __first_;
1354 const _CharT* __current_;
1355 const _CharT* __last_;
1356 vector<sub_match<const _CharT*> > __sub_matches_;
1357 vector<pair<size_t, const _CharT*> > __loop_data_;
1358 const __node<_CharT>* __node_;
1359 regex_constants::match_flag_type __flags_;
Howard Hinnant066ba512011-03-26 20:02:27 +00001360 bool __at_first_;
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00001361
Howard Hinnant7ca9d942010-09-23 15:13:20 +00001362 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaa0874c2010-07-12 15:51:17 +00001363 __state()
1364 : __do_(0), __first_(nullptr), __current_(nullptr), __last_(nullptr),
1365 __node_(nullptr), __flags_() {}
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00001366};
1367
Howard Hinnantaa0874c2010-07-12 15:51:17 +00001368// __node
Howard Hinnant67ad2132010-06-29 18:37:43 +00001369
1370template <class _CharT>
Howard Hinnantaa0874c2010-07-12 15:51:17 +00001371class __node
Howard Hinnant67ad2132010-06-29 18:37:43 +00001372{
Howard Hinnantaa0874c2010-07-12 15:51:17 +00001373 __node(const __node&);
1374 __node& operator=(const __node&);
Howard Hinnant67ad2132010-06-29 18:37:43 +00001375public:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001376 typedef _VSTD::__state<_CharT> __state;
Howard Hinnant67ad2132010-06-29 18:37:43 +00001377
Howard Hinnant7ca9d942010-09-23 15:13:20 +00001378 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaa0874c2010-07-12 15:51:17 +00001379 __node() {}
Howard Hinnant7ca9d942010-09-23 15:13:20 +00001380 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaa0874c2010-07-12 15:51:17 +00001381 virtual ~__node() {}
Howard Hinnant67ad2132010-06-29 18:37:43 +00001382
Howard Hinnant7ca9d942010-09-23 15:13:20 +00001383 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierb41db9a2018-10-01 01:59:37 +00001384 virtual void __exec(__state&) const {}
Howard Hinnant7ca9d942010-09-23 15:13:20 +00001385 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierb41db9a2018-10-01 01:59:37 +00001386 virtual void __exec_split(bool, __state&) const {}
Howard Hinnant67ad2132010-06-29 18:37:43 +00001387};
1388
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00001389// __end_state
Howard Hinnant67ad2132010-06-29 18:37:43 +00001390
1391template <class _CharT>
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00001392class __end_state
Howard Hinnantaa0874c2010-07-12 15:51:17 +00001393 : public __node<_CharT>
Howard Hinnant67ad2132010-06-29 18:37:43 +00001394{
Howard Hinnant67ad2132010-06-29 18:37:43 +00001395public:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001396 typedef _VSTD::__state<_CharT> __state;
Howard Hinnant67ad2132010-06-29 18:37:43 +00001397
Howard Hinnant7ca9d942010-09-23 15:13:20 +00001398 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00001399 __end_state() {}
Howard Hinnantf5cb6eb2010-06-30 17:22:19 +00001400
Howard Hinnantaa0874c2010-07-12 15:51:17 +00001401 virtual void __exec(__state&) const;
Howard Hinnant67ad2132010-06-29 18:37:43 +00001402};
1403
1404template <class _CharT>
Howard Hinnantaa0874c2010-07-12 15:51:17 +00001405void
1406__end_state<_CharT>::__exec(__state& __s) const
Howard Hinnant93ef6552010-06-30 20:30:19 +00001407{
Howard Hinnantaa0874c2010-07-12 15:51:17 +00001408 __s.__do_ = __state::__end_state;
Howard Hinnant93ef6552010-06-30 20:30:19 +00001409}
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00001410
1411// __has_one_state
1412
Howard Hinnant93ef6552010-06-30 20:30:19 +00001413template <class _CharT>
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00001414class __has_one_state
Howard Hinnantaa0874c2010-07-12 15:51:17 +00001415 : public __node<_CharT>
Howard Hinnantf5cb6eb2010-06-30 17:22:19 +00001416{
Howard Hinnantaa0874c2010-07-12 15:51:17 +00001417 __node<_CharT>* __first_;
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00001418
1419public:
Howard Hinnant7ca9d942010-09-23 15:13:20 +00001420 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaa0874c2010-07-12 15:51:17 +00001421 explicit __has_one_state(__node<_CharT>* __s)
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00001422 : __first_(__s) {}
1423
Howard Hinnant7ca9d942010-09-23 15:13:20 +00001424 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaa0874c2010-07-12 15:51:17 +00001425 __node<_CharT>* first() const {return __first_;}
Howard Hinnant7ca9d942010-09-23 15:13:20 +00001426 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaa0874c2010-07-12 15:51:17 +00001427 __node<_CharT>*& first() {return __first_;}
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00001428};
1429
1430// __owns_one_state
1431
1432template <class _CharT>
1433class __owns_one_state
1434 : public __has_one_state<_CharT>
1435{
1436 typedef __has_one_state<_CharT> base;
1437
1438public:
Howard Hinnant7ca9d942010-09-23 15:13:20 +00001439 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaa0874c2010-07-12 15:51:17 +00001440 explicit __owns_one_state(__node<_CharT>* __s)
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00001441 : base(__s) {}
1442
1443 virtual ~__owns_one_state();
1444};
1445
1446template <class _CharT>
1447__owns_one_state<_CharT>::~__owns_one_state()
1448{
1449 delete this->first();
Howard Hinnantf5cb6eb2010-06-30 17:22:19 +00001450}
1451
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00001452// __empty_state
1453
1454template <class _CharT>
1455class __empty_state
1456 : public __owns_one_state<_CharT>
1457{
1458 typedef __owns_one_state<_CharT> base;
1459
1460public:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001461 typedef _VSTD::__state<_CharT> __state;
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00001462
Howard Hinnant7ca9d942010-09-23 15:13:20 +00001463 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaa0874c2010-07-12 15:51:17 +00001464 explicit __empty_state(__node<_CharT>* __s)
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00001465 : base(__s) {}
1466
Howard Hinnantaa0874c2010-07-12 15:51:17 +00001467 virtual void __exec(__state&) const;
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00001468};
1469
1470template <class _CharT>
Howard Hinnantaa0874c2010-07-12 15:51:17 +00001471void
1472__empty_state<_CharT>::__exec(__state& __s) const
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00001473{
Howard Hinnantaa0874c2010-07-12 15:51:17 +00001474 __s.__do_ = __state::__accept_but_not_consume;
1475 __s.__node_ = this->first();
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00001476}
1477
1478// __empty_non_own_state
1479
1480template <class _CharT>
1481class __empty_non_own_state
1482 : public __has_one_state<_CharT>
1483{
1484 typedef __has_one_state<_CharT> base;
1485
1486public:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001487 typedef _VSTD::__state<_CharT> __state;
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00001488
Howard Hinnant7ca9d942010-09-23 15:13:20 +00001489 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaa0874c2010-07-12 15:51:17 +00001490 explicit __empty_non_own_state(__node<_CharT>* __s)
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00001491 : base(__s) {}
1492
Howard Hinnantaa0874c2010-07-12 15:51:17 +00001493 virtual void __exec(__state&) const;
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00001494};
1495
1496template <class _CharT>
Howard Hinnantaa0874c2010-07-12 15:51:17 +00001497void
1498__empty_non_own_state<_CharT>::__exec(__state& __s) const
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00001499{
Howard Hinnantaa0874c2010-07-12 15:51:17 +00001500 __s.__do_ = __state::__accept_but_not_consume;
1501 __s.__node_ = this->first();
1502}
1503
1504// __repeat_one_loop
1505
1506template <class _CharT>
1507class __repeat_one_loop
1508 : public __has_one_state<_CharT>
1509{
1510 typedef __has_one_state<_CharT> base;
1511
1512public:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001513 typedef _VSTD::__state<_CharT> __state;
Howard Hinnantaa0874c2010-07-12 15:51:17 +00001514
Howard Hinnant7ca9d942010-09-23 15:13:20 +00001515 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaa0874c2010-07-12 15:51:17 +00001516 explicit __repeat_one_loop(__node<_CharT>* __s)
1517 : base(__s) {}
1518
1519 virtual void __exec(__state&) const;
Howard Hinnantaa0874c2010-07-12 15:51:17 +00001520};
1521
1522template <class _CharT>
1523void
1524__repeat_one_loop<_CharT>::__exec(__state& __s) const
1525{
1526 __s.__do_ = __state::__repeat;
1527 __s.__node_ = this->first();
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00001528}
1529
1530// __owns_two_states
1531
1532template <class _CharT>
1533class __owns_two_states
1534 : public __owns_one_state<_CharT>
1535{
1536 typedef __owns_one_state<_CharT> base;
1537
1538 base* __second_;
1539
1540public:
Howard Hinnant7ca9d942010-09-23 15:13:20 +00001541 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaa0874c2010-07-12 15:51:17 +00001542 explicit __owns_two_states(__node<_CharT>* __s1, base* __s2)
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00001543 : base(__s1), __second_(__s2) {}
1544
1545 virtual ~__owns_two_states();
1546
Howard Hinnant7ca9d942010-09-23 15:13:20 +00001547 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00001548 base* second() const {return __second_;}
Howard Hinnant7ca9d942010-09-23 15:13:20 +00001549 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00001550 base*& second() {return __second_;}
1551};
1552
1553template <class _CharT>
1554__owns_two_states<_CharT>::~__owns_two_states()
1555{
1556 delete __second_;
1557}
1558
1559// __loop
1560
1561template <class _CharT>
1562class __loop
1563 : public __owns_two_states<_CharT>
1564{
1565 typedef __owns_two_states<_CharT> base;
1566
1567 size_t __min_;
1568 size_t __max_;
1569 unsigned __loop_id_;
Howard Hinnantaa0874c2010-07-12 15:51:17 +00001570 unsigned __mexp_begin_;
1571 unsigned __mexp_end_;
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00001572 bool __greedy_;
1573
1574public:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001575 typedef _VSTD::__state<_CharT> __state;
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00001576
Howard Hinnant7ca9d942010-09-23 15:13:20 +00001577 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00001578 explicit __loop(unsigned __loop_id,
Howard Hinnantaa0874c2010-07-12 15:51:17 +00001579 __node<_CharT>* __s1, __owns_one_state<_CharT>* __s2,
1580 unsigned __mexp_begin, unsigned __mexp_end,
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00001581 bool __greedy = true,
1582 size_t __min = 0,
1583 size_t __max = numeric_limits<size_t>::max())
1584 : base(__s1, __s2), __min_(__min), __max_(__max), __loop_id_(__loop_id),
Howard Hinnantaa0874c2010-07-12 15:51:17 +00001585 __mexp_begin_(__mexp_begin), __mexp_end_(__mexp_end),
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00001586 __greedy_(__greedy) {}
1587
Howard Hinnantaa0874c2010-07-12 15:51:17 +00001588 virtual void __exec(__state& __s) const;
1589 virtual void __exec_split(bool __second, __state& __s) const;
Howard Hinnant5bf96132010-07-08 17:43:58 +00001590
Howard Hinnantaa0874c2010-07-12 15:51:17 +00001591private:
Howard Hinnant7ca9d942010-09-23 15:13:20 +00001592 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaa0874c2010-07-12 15:51:17 +00001593 void __init_repeat(__state& __s) const
1594 {
1595 __s.__loop_data_[__loop_id_].second = __s.__current_;
1596 for (size_t __i = __mexp_begin_-1; __i != __mexp_end_-1; ++__i)
1597 {
1598 __s.__sub_matches_[__i].first = __s.__last_;
1599 __s.__sub_matches_[__i].second = __s.__last_;
1600 __s.__sub_matches_[__i].matched = false;
1601 }
1602 }
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00001603};
1604
1605template <class _CharT>
Howard Hinnantaa0874c2010-07-12 15:51:17 +00001606void
1607__loop<_CharT>::__exec(__state& __s) const
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00001608{
Howard Hinnantaa0874c2010-07-12 15:51:17 +00001609 if (__s.__do_ == __state::__repeat)
1610 {
1611 bool __do_repeat = ++__s.__loop_data_[__loop_id_].first < __max_;
1612 bool __do_alt = __s.__loop_data_[__loop_id_].first >= __min_;
1613 if (__do_repeat && __do_alt &&
1614 __s.__loop_data_[__loop_id_].second == __s.__current_)
1615 __do_repeat = false;
1616 if (__do_repeat && __do_alt)
1617 __s.__do_ = __state::__split;
1618 else if (__do_repeat)
1619 {
1620 __s.__do_ = __state::__accept_but_not_consume;
1621 __s.__node_ = this->first();
1622 __init_repeat(__s);
1623 }
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00001624 else
Howard Hinnantaa0874c2010-07-12 15:51:17 +00001625 {
1626 __s.__do_ = __state::__accept_but_not_consume;
1627 __s.__node_ = this->second();
1628 }
1629 }
1630 else
1631 {
Howard Hinnantebbc2b62010-07-27 17:24:17 +00001632 __s.__loop_data_[__loop_id_].first = 0;
1633 bool __do_repeat = 0 < __max_;
1634 bool __do_alt = 0 >= __min_;
1635 if (__do_repeat && __do_alt)
Howard Hinnantaa0874c2010-07-12 15:51:17 +00001636 __s.__do_ = __state::__split;
Howard Hinnantebbc2b62010-07-27 17:24:17 +00001637 else if (__do_repeat)
1638 {
1639 __s.__do_ = __state::__accept_but_not_consume;
1640 __s.__node_ = this->first();
1641 __init_repeat(__s);
1642 }
Howard Hinnantaa0874c2010-07-12 15:51:17 +00001643 else
1644 {
1645 __s.__do_ = __state::__accept_but_not_consume;
1646 __s.__node_ = this->second();
1647 }
1648 }
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00001649}
1650
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00001651template <class _CharT>
Howard Hinnantaa0874c2010-07-12 15:51:17 +00001652void
1653__loop<_CharT>::__exec_split(bool __second, __state& __s) const
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00001654{
Howard Hinnantaa0874c2010-07-12 15:51:17 +00001655 __s.__do_ = __state::__accept_but_not_consume;
1656 if (__greedy_ != __second)
Howard Hinnant5bf96132010-07-08 17:43:58 +00001657 {
Howard Hinnantaa0874c2010-07-12 15:51:17 +00001658 __s.__node_ = this->first();
1659 __init_repeat(__s);
Howard Hinnant5bf96132010-07-08 17:43:58 +00001660 }
Howard Hinnantaa0874c2010-07-12 15:51:17 +00001661 else
1662 __s.__node_ = this->second();
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00001663}
1664
Howard Hinnant16d65422010-07-16 19:08:36 +00001665// __alternate
1666
1667template <class _CharT>
1668class __alternate
1669 : public __owns_two_states<_CharT>
1670{
1671 typedef __owns_two_states<_CharT> base;
1672
1673public:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001674 typedef _VSTD::__state<_CharT> __state;
Howard Hinnant16d65422010-07-16 19:08:36 +00001675
Howard Hinnant7ca9d942010-09-23 15:13:20 +00001676 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant16d65422010-07-16 19:08:36 +00001677 explicit __alternate(__owns_one_state<_CharT>* __s1,
1678 __owns_one_state<_CharT>* __s2)
1679 : base(__s1, __s2) {}
1680
1681 virtual void __exec(__state& __s) const;
1682 virtual void __exec_split(bool __second, __state& __s) const;
Howard Hinnant16d65422010-07-16 19:08:36 +00001683};
1684
1685template <class _CharT>
1686void
1687__alternate<_CharT>::__exec(__state& __s) const
1688{
1689 __s.__do_ = __state::__split;
1690}
1691
1692template <class _CharT>
1693void
1694__alternate<_CharT>::__exec_split(bool __second, __state& __s) const
1695{
1696 __s.__do_ = __state::__accept_but_not_consume;
Howard Hinnantcbf2f3f2010-07-22 14:12:20 +00001697 if (__second)
Howard Hinnant16d65422010-07-16 19:08:36 +00001698 __s.__node_ = this->second();
Howard Hinnantcbf2f3f2010-07-22 14:12:20 +00001699 else
1700 __s.__node_ = this->first();
Howard Hinnant16d65422010-07-16 19:08:36 +00001701}
1702
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00001703// __begin_marked_subexpression
1704
1705template <class _CharT>
1706class __begin_marked_subexpression
1707 : public __owns_one_state<_CharT>
1708{
1709 typedef __owns_one_state<_CharT> base;
1710
Howard Hinnant5bf96132010-07-08 17:43:58 +00001711 unsigned __mexp_;
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00001712public:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001713 typedef _VSTD::__state<_CharT> __state;
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00001714
Howard Hinnant7ca9d942010-09-23 15:13:20 +00001715 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaa0874c2010-07-12 15:51:17 +00001716 explicit __begin_marked_subexpression(unsigned __mexp, __node<_CharT>* __s)
Howard Hinnant5bf96132010-07-08 17:43:58 +00001717 : base(__s), __mexp_(__mexp) {}
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00001718
Howard Hinnantaa0874c2010-07-12 15:51:17 +00001719 virtual void __exec(__state&) const;
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00001720};
1721
1722template <class _CharT>
Howard Hinnantaa0874c2010-07-12 15:51:17 +00001723void
1724__begin_marked_subexpression<_CharT>::__exec(__state& __s) const
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00001725{
Howard Hinnantaa0874c2010-07-12 15:51:17 +00001726 __s.__do_ = __state::__accept_but_not_consume;
1727 __s.__sub_matches_[__mexp_-1].first = __s.__current_;
1728 __s.__node_ = this->first();
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00001729}
1730
1731// __end_marked_subexpression
1732
1733template <class _CharT>
1734class __end_marked_subexpression
1735 : public __owns_one_state<_CharT>
1736{
1737 typedef __owns_one_state<_CharT> base;
1738
Howard Hinnant5bf96132010-07-08 17:43:58 +00001739 unsigned __mexp_;
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00001740public:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001741 typedef _VSTD::__state<_CharT> __state;
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00001742
Howard Hinnant7ca9d942010-09-23 15:13:20 +00001743 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaa0874c2010-07-12 15:51:17 +00001744 explicit __end_marked_subexpression(unsigned __mexp, __node<_CharT>* __s)
Howard Hinnant5bf96132010-07-08 17:43:58 +00001745 : base(__s), __mexp_(__mexp) {}
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00001746
Howard Hinnantaa0874c2010-07-12 15:51:17 +00001747 virtual void __exec(__state&) const;
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00001748};
1749
1750template <class _CharT>
Howard Hinnantaa0874c2010-07-12 15:51:17 +00001751void
1752__end_marked_subexpression<_CharT>::__exec(__state& __s) const
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00001753{
Howard Hinnantaa0874c2010-07-12 15:51:17 +00001754 __s.__do_ = __state::__accept_but_not_consume;
1755 __s.__sub_matches_[__mexp_-1].second = __s.__current_;
1756 __s.__sub_matches_[__mexp_-1].matched = true;
1757 __s.__node_ = this->first();
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00001758}
1759
Howard Hinnant2a315e32010-07-12 18:16:05 +00001760// __back_ref
1761
1762template <class _CharT>
1763class __back_ref
1764 : public __owns_one_state<_CharT>
1765{
1766 typedef __owns_one_state<_CharT> base;
1767
1768 unsigned __mexp_;
1769public:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001770 typedef _VSTD::__state<_CharT> __state;
Howard Hinnant2a315e32010-07-12 18:16:05 +00001771
Howard Hinnant7ca9d942010-09-23 15:13:20 +00001772 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2a315e32010-07-12 18:16:05 +00001773 explicit __back_ref(unsigned __mexp, __node<_CharT>* __s)
1774 : base(__s), __mexp_(__mexp) {}
1775
1776 virtual void __exec(__state&) const;
Howard Hinnant2a315e32010-07-12 18:16:05 +00001777};
1778
1779template <class _CharT>
1780void
1781__back_ref<_CharT>::__exec(__state& __s) const
1782{
Marshall Clow360e8392015-08-24 15:57:09 +00001783 if (__mexp_ > __s.__sub_matches_.size())
1784 __throw_regex_error<regex_constants::error_backref>();
Howard Hinnant2a315e32010-07-12 18:16:05 +00001785 sub_match<const _CharT*>& __sm = __s.__sub_matches_[__mexp_-1];
1786 if (__sm.matched)
1787 {
1788 ptrdiff_t __len = __sm.second - __sm.first;
1789 if (__s.__last_ - __s.__current_ >= __len &&
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001790 _VSTD::equal(__sm.first, __sm.second, __s.__current_))
Howard Hinnant2a315e32010-07-12 18:16:05 +00001791 {
1792 __s.__do_ = __state::__accept_but_not_consume;
1793 __s.__current_ += __len;
1794 __s.__node_ = this->first();
1795 }
1796 else
1797 {
1798 __s.__do_ = __state::__reject;
1799 __s.__node_ = nullptr;
1800 }
1801 }
1802 else
1803 {
1804 __s.__do_ = __state::__reject;
1805 __s.__node_ = nullptr;
1806 }
1807}
1808
Howard Hinnantcfe8d272010-07-12 19:11:27 +00001809// __back_ref_icase
1810
1811template <class _CharT, class _Traits>
1812class __back_ref_icase
1813 : public __owns_one_state<_CharT>
1814{
1815 typedef __owns_one_state<_CharT> base;
1816
1817 _Traits __traits_;
1818 unsigned __mexp_;
1819public:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001820 typedef _VSTD::__state<_CharT> __state;
Howard Hinnantcfe8d272010-07-12 19:11:27 +00001821
Howard Hinnant7ca9d942010-09-23 15:13:20 +00001822 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcfe8d272010-07-12 19:11:27 +00001823 explicit __back_ref_icase(const _Traits& __traits, unsigned __mexp,
1824 __node<_CharT>* __s)
1825 : base(__s), __traits_(__traits), __mexp_(__mexp) {}
1826
1827 virtual void __exec(__state&) const;
Howard Hinnantcfe8d272010-07-12 19:11:27 +00001828};
1829
1830template <class _CharT, class _Traits>
1831void
1832__back_ref_icase<_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_nocase(__sm.first[__i]) !=
1843 __traits_.translate_nocase(__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
1864// __back_ref_collate
1865
1866template <class _CharT, class _Traits>
1867class __back_ref_collate
1868 : public __owns_one_state<_CharT>
1869{
1870 typedef __owns_one_state<_CharT> base;
1871
1872 _Traits __traits_;
1873 unsigned __mexp_;
1874public:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001875 typedef _VSTD::__state<_CharT> __state;
Howard Hinnantcfe8d272010-07-12 19:11:27 +00001876
Howard Hinnant7ca9d942010-09-23 15:13:20 +00001877 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcfe8d272010-07-12 19:11:27 +00001878 explicit __back_ref_collate(const _Traits& __traits, unsigned __mexp,
1879 __node<_CharT>* __s)
1880 : base(__s), __traits_(__traits), __mexp_(__mexp) {}
1881
1882 virtual void __exec(__state&) const;
Howard Hinnantcfe8d272010-07-12 19:11:27 +00001883};
1884
1885template <class _CharT, class _Traits>
1886void
1887__back_ref_collate<_CharT, _Traits>::__exec(__state& __s) const
1888{
1889 sub_match<const _CharT*>& __sm = __s.__sub_matches_[__mexp_-1];
1890 if (__sm.matched)
1891 {
1892 ptrdiff_t __len = __sm.second - __sm.first;
1893 if (__s.__last_ - __s.__current_ >= __len)
1894 {
1895 for (ptrdiff_t __i = 0; __i < __len; ++__i)
1896 {
1897 if (__traits_.translate(__sm.first[__i]) !=
1898 __traits_.translate(__s.__current_[__i]))
1899 goto __not_equal;
1900 }
1901 __s.__do_ = __state::__accept_but_not_consume;
1902 __s.__current_ += __len;
1903 __s.__node_ = this->first();
1904 }
1905 else
1906 {
1907 __s.__do_ = __state::__reject;
1908 __s.__node_ = nullptr;
1909 }
1910 }
1911 else
1912 {
1913__not_equal:
1914 __s.__do_ = __state::__reject;
1915 __s.__node_ = nullptr;
1916 }
1917}
1918
Howard Hinnant70d27852010-07-27 01:25:38 +00001919// __word_boundary
1920
1921template <class _CharT, class _Traits>
1922class __word_boundary
1923 : public __owns_one_state<_CharT>
1924{
1925 typedef __owns_one_state<_CharT> base;
1926
1927 _Traits __traits_;
1928 bool __invert_;
1929public:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001930 typedef _VSTD::__state<_CharT> __state;
Howard Hinnant70d27852010-07-27 01:25:38 +00001931
Howard Hinnant7ca9d942010-09-23 15:13:20 +00001932 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant70d27852010-07-27 01:25:38 +00001933 explicit __word_boundary(const _Traits& __traits, bool __invert,
1934 __node<_CharT>* __s)
1935 : base(__s), __traits_(__traits), __invert_(__invert) {}
1936
1937 virtual void __exec(__state&) const;
Howard Hinnant70d27852010-07-27 01:25:38 +00001938};
1939
1940template <class _CharT, class _Traits>
1941void
1942__word_boundary<_CharT, _Traits>::__exec(__state& __s) const
1943{
1944 bool __is_word_b = false;
1945 if (__s.__first_ != __s.__last_)
1946 {
1947 if (__s.__current_ == __s.__last_)
1948 {
1949 if (!(__s.__flags_ & regex_constants::match_not_eow))
1950 {
1951 _CharT __c = __s.__current_[-1];
1952 __is_word_b = __c == '_' ||
1953 __traits_.isctype(__c, ctype_base::alnum);
1954 }
1955 }
Howard Hinnant6b2602a2010-07-29 15:17:28 +00001956 else if (__s.__current_ == __s.__first_ &&
1957 !(__s.__flags_ & regex_constants::match_prev_avail))
Howard Hinnant70d27852010-07-27 01:25:38 +00001958 {
1959 if (!(__s.__flags_ & regex_constants::match_not_bow))
1960 {
1961 _CharT __c = *__s.__current_;
1962 __is_word_b = __c == '_' ||
1963 __traits_.isctype(__c, ctype_base::alnum);
1964 }
1965 }
1966 else
1967 {
1968 _CharT __c1 = __s.__current_[-1];
1969 _CharT __c2 = *__s.__current_;
1970 bool __is_c1_b = __c1 == '_' ||
1971 __traits_.isctype(__c1, ctype_base::alnum);
1972 bool __is_c2_b = __c2 == '_' ||
1973 __traits_.isctype(__c2, ctype_base::alnum);
1974 __is_word_b = __is_c1_b != __is_c2_b;
1975 }
1976 }
1977 if (__is_word_b != __invert_)
1978 {
1979 __s.__do_ = __state::__accept_but_not_consume;
1980 __s.__node_ = this->first();
1981 }
1982 else
1983 {
1984 __s.__do_ = __state::__reject;
1985 __s.__node_ = nullptr;
1986 }
1987}
1988
Howard Hinnant066ba512011-03-26 20:02:27 +00001989// __l_anchor
1990
1991template <class _CharT>
Mark de Wevera989cce2020-11-18 18:09:13 +01001992_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1993bool __is_eol(_CharT c)
1994{
1995 return c == '\r' || c == '\n';
1996}
1997
1998template <class _CharT>
1999class __l_anchor_multiline
Howard Hinnant066ba512011-03-26 20:02:27 +00002000 : public __owns_one_state<_CharT>
2001{
2002 typedef __owns_one_state<_CharT> base;
2003
Mark de Wevera989cce2020-11-18 18:09:13 +01002004 bool __multiline;
2005
Howard Hinnant066ba512011-03-26 20:02:27 +00002006public:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002007 typedef _VSTD::__state<_CharT> __state;
Howard Hinnant066ba512011-03-26 20:02:27 +00002008
2009 _LIBCPP_INLINE_VISIBILITY
Mark de Wevera989cce2020-11-18 18:09:13 +01002010 __l_anchor_multiline(bool __multiline, __node<_CharT>* __s)
2011 : base(__s), __multiline(__multiline) {}
Howard Hinnant066ba512011-03-26 20:02:27 +00002012
2013 virtual void __exec(__state&) const;
2014};
2015
2016template <class _CharT>
2017void
Mark de Wevera989cce2020-11-18 18:09:13 +01002018__l_anchor_multiline<_CharT>::__exec(__state& __s) const
Howard Hinnant066ba512011-03-26 20:02:27 +00002019{
Marshall Clow54404392015-03-19 17:05:59 +00002020 if (__s.__at_first_ && __s.__current_ == __s.__first_ &&
2021 !(__s.__flags_ & regex_constants::match_not_bol))
Howard Hinnant066ba512011-03-26 20:02:27 +00002022 {
2023 __s.__do_ = __state::__accept_but_not_consume;
2024 __s.__node_ = this->first();
2025 }
Mark de Wevera989cce2020-11-18 18:09:13 +01002026 else if (__multiline &&
2027 !__s.__at_first_ &&
2028 __is_eol(*_VSTD::prev(__s.__current_)))
2029 {
2030 __s.__do_ = __state::__accept_but_not_consume;
2031 __s.__node_ = this->first();
2032 }
Howard Hinnant066ba512011-03-26 20:02:27 +00002033 else
2034 {
2035 __s.__do_ = __state::__reject;
2036 __s.__node_ = nullptr;
2037 }
2038}
2039
Howard Hinnantaad0aa62010-07-09 00:15:26 +00002040// __r_anchor
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00002041
2042template <class _CharT>
Mark de Wevera989cce2020-11-18 18:09:13 +01002043class __r_anchor_multiline
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00002044 : public __owns_one_state<_CharT>
2045{
2046 typedef __owns_one_state<_CharT> base;
2047
Mark de Wevera989cce2020-11-18 18:09:13 +01002048 bool __multiline;
2049
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00002050public:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002051 typedef _VSTD::__state<_CharT> __state;
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00002052
Howard Hinnant7ca9d942010-09-23 15:13:20 +00002053 _LIBCPP_INLINE_VISIBILITY
Mark de Wevera989cce2020-11-18 18:09:13 +01002054 __r_anchor_multiline(bool __multiline, __node<_CharT>* __s)
2055 : base(__s), __multiline(__multiline) {}
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00002056
Howard Hinnantaa0874c2010-07-12 15:51:17 +00002057 virtual void __exec(__state&) const;
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00002058};
2059
2060template <class _CharT>
Howard Hinnantaa0874c2010-07-12 15:51:17 +00002061void
Mark de Wevera989cce2020-11-18 18:09:13 +01002062__r_anchor_multiline<_CharT>::__exec(__state& __s) const
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00002063{
Marshall Clow54404392015-03-19 17:05:59 +00002064 if (__s.__current_ == __s.__last_ &&
2065 !(__s.__flags_ & regex_constants::match_not_eol))
Howard Hinnantaa0874c2010-07-12 15:51:17 +00002066 {
2067 __s.__do_ = __state::__accept_but_not_consume;
2068 __s.__node_ = this->first();
2069 }
Mark de Wevera989cce2020-11-18 18:09:13 +01002070 else if (__multiline && __is_eol(*__s.__current_))
2071 {
2072 __s.__do_ = __state::__accept_but_not_consume;
2073 __s.__node_ = this->first();
2074 }
Howard Hinnantaa0874c2010-07-12 15:51:17 +00002075 else
2076 {
2077 __s.__do_ = __state::__reject;
2078 __s.__node_ = nullptr;
2079 }
2080}
2081
2082// __match_any
2083
2084template <class _CharT>
2085class __match_any
2086 : public __owns_one_state<_CharT>
2087{
2088 typedef __owns_one_state<_CharT> base;
2089
2090public:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002091 typedef _VSTD::__state<_CharT> __state;
Howard Hinnantaa0874c2010-07-12 15:51:17 +00002092
Howard Hinnant7ca9d942010-09-23 15:13:20 +00002093 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaa0874c2010-07-12 15:51:17 +00002094 __match_any(__node<_CharT>* __s)
2095 : base(__s) {}
2096
2097 virtual void __exec(__state&) const;
Howard Hinnantaa0874c2010-07-12 15:51:17 +00002098};
2099
2100template <class _CharT>
2101void
2102__match_any<_CharT>::__exec(__state& __s) const
2103{
2104 if (__s.__current_ != __s.__last_ && *__s.__current_ != 0)
2105 {
2106 __s.__do_ = __state::__accept_and_consume;
2107 ++__s.__current_;
2108 __s.__node_ = this->first();
2109 }
2110 else
2111 {
2112 __s.__do_ = __state::__reject;
2113 __s.__node_ = nullptr;
2114 }
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00002115}
2116
Howard Hinnant70d27852010-07-27 01:25:38 +00002117// __match_any_but_newline
2118
2119template <class _CharT>
2120class __match_any_but_newline
2121 : public __owns_one_state<_CharT>
2122{
2123 typedef __owns_one_state<_CharT> base;
2124
2125public:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002126 typedef _VSTD::__state<_CharT> __state;
Howard Hinnant70d27852010-07-27 01:25:38 +00002127
Howard Hinnant7ca9d942010-09-23 15:13:20 +00002128 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant70d27852010-07-27 01:25:38 +00002129 __match_any_but_newline(__node<_CharT>* __s)
2130 : base(__s) {}
2131
2132 virtual void __exec(__state&) const;
Howard Hinnant70d27852010-07-27 01:25:38 +00002133};
2134
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00002135template <> _LIBCPP_FUNC_VIS void __match_any_but_newline<char>::__exec(__state&) const;
2136template <> _LIBCPP_FUNC_VIS void __match_any_but_newline<wchar_t>::__exec(__state&) const;
2137
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00002138// __match_char
2139
Howard Hinnantf5cb6eb2010-06-30 17:22:19 +00002140template <class _CharT>
Howard Hinnant67ad2132010-06-29 18:37:43 +00002141class __match_char
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00002142 : public __owns_one_state<_CharT>
Howard Hinnant67ad2132010-06-29 18:37:43 +00002143{
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00002144 typedef __owns_one_state<_CharT> base;
2145
Howard Hinnant67ad2132010-06-29 18:37:43 +00002146 _CharT __c_;
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00002147
2148 __match_char(const __match_char&);
2149 __match_char& operator=(const __match_char&);
Howard Hinnant67ad2132010-06-29 18:37:43 +00002150public:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002151 typedef _VSTD::__state<_CharT> __state;
Howard Hinnant93ef6552010-06-30 20:30:19 +00002152
Howard Hinnant7ca9d942010-09-23 15:13:20 +00002153 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaa0874c2010-07-12 15:51:17 +00002154 __match_char(_CharT __c, __node<_CharT>* __s)
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00002155 : base(__s), __c_(__c) {}
Howard Hinnant67ad2132010-06-29 18:37:43 +00002156
Howard Hinnantaa0874c2010-07-12 15:51:17 +00002157 virtual void __exec(__state&) const;
Howard Hinnant67ad2132010-06-29 18:37:43 +00002158};
2159
Howard Hinnant93ef6552010-06-30 20:30:19 +00002160template <class _CharT>
Howard Hinnantaa0874c2010-07-12 15:51:17 +00002161void
2162__match_char<_CharT>::__exec(__state& __s) const
Howard Hinnant93ef6552010-06-30 20:30:19 +00002163{
Howard Hinnantaa0874c2010-07-12 15:51:17 +00002164 if (__s.__current_ != __s.__last_ && *__s.__current_ == __c_)
2165 {
2166 __s.__do_ = __state::__accept_and_consume;
2167 ++__s.__current_;
2168 __s.__node_ = this->first();
2169 }
2170 else
2171 {
2172 __s.__do_ = __state::__reject;
2173 __s.__node_ = nullptr;
2174 }
Howard Hinnant93ef6552010-06-30 20:30:19 +00002175}
Howard Hinnant93ef6552010-06-30 20:30:19 +00002176
Howard Hinnantcfe8d272010-07-12 19:11:27 +00002177// __match_char_icase
2178
2179template <class _CharT, class _Traits>
2180class __match_char_icase
2181 : public __owns_one_state<_CharT>
2182{
2183 typedef __owns_one_state<_CharT> base;
2184
2185 _Traits __traits_;
2186 _CharT __c_;
2187
2188 __match_char_icase(const __match_char_icase&);
2189 __match_char_icase& operator=(const __match_char_icase&);
2190public:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002191 typedef _VSTD::__state<_CharT> __state;
Howard Hinnantcfe8d272010-07-12 19:11:27 +00002192
Howard Hinnant7ca9d942010-09-23 15:13:20 +00002193 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcfe8d272010-07-12 19:11:27 +00002194 __match_char_icase(const _Traits& __traits, _CharT __c, __node<_CharT>* __s)
2195 : base(__s), __traits_(__traits), __c_(__traits.translate_nocase(__c)) {}
2196
2197 virtual void __exec(__state&) const;
Howard Hinnantcfe8d272010-07-12 19:11:27 +00002198};
2199
2200template <class _CharT, class _Traits>
2201void
2202__match_char_icase<_CharT, _Traits>::__exec(__state& __s) const
2203{
2204 if (__s.__current_ != __s.__last_ &&
2205 __traits_.translate_nocase(*__s.__current_) == __c_)
2206 {
2207 __s.__do_ = __state::__accept_and_consume;
2208 ++__s.__current_;
2209 __s.__node_ = this->first();
2210 }
2211 else
2212 {
2213 __s.__do_ = __state::__reject;
2214 __s.__node_ = nullptr;
2215 }
2216}
2217
2218// __match_char_collate
2219
2220template <class _CharT, class _Traits>
2221class __match_char_collate
2222 : public __owns_one_state<_CharT>
2223{
2224 typedef __owns_one_state<_CharT> base;
2225
2226 _Traits __traits_;
2227 _CharT __c_;
2228
2229 __match_char_collate(const __match_char_collate&);
2230 __match_char_collate& operator=(const __match_char_collate&);
2231public:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002232 typedef _VSTD::__state<_CharT> __state;
Howard Hinnantcfe8d272010-07-12 19:11:27 +00002233
Howard Hinnant7ca9d942010-09-23 15:13:20 +00002234 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcfe8d272010-07-12 19:11:27 +00002235 __match_char_collate(const _Traits& __traits, _CharT __c, __node<_CharT>* __s)
2236 : base(__s), __traits_(__traits), __c_(__traits.translate(__c)) {}
2237
2238 virtual void __exec(__state&) const;
Howard Hinnantcfe8d272010-07-12 19:11:27 +00002239};
2240
2241template <class _CharT, class _Traits>
2242void
2243__match_char_collate<_CharT, _Traits>::__exec(__state& __s) const
2244{
2245 if (__s.__current_ != __s.__last_ &&
2246 __traits_.translate(*__s.__current_) == __c_)
2247 {
2248 __s.__do_ = __state::__accept_and_consume;
2249 ++__s.__current_;
2250 __s.__node_ = this->first();
2251 }
2252 else
2253 {
2254 __s.__do_ = __state::__reject;
2255 __s.__node_ = nullptr;
2256 }
2257}
2258
Howard Hinnant3034c902010-07-13 21:48:06 +00002259// __bracket_expression
2260
2261template <class _CharT, class _Traits>
2262class __bracket_expression
2263 : public __owns_one_state<_CharT>
2264{
2265 typedef __owns_one_state<_CharT> base;
2266 typedef typename _Traits::string_type string_type;
2267
2268 _Traits __traits_;
2269 vector<_CharT> __chars_;
Howard Hinnant70b3e192010-07-28 17:35:27 +00002270 vector<_CharT> __neg_chars_;
Howard Hinnant3034c902010-07-13 21:48:06 +00002271 vector<pair<string_type, string_type> > __ranges_;
2272 vector<pair<_CharT, _CharT> > __digraphs_;
2273 vector<string_type> __equivalences_;
Dan Albert49f384c2014-07-29 19:23:39 +00002274 typename regex_traits<_CharT>::char_class_type __mask_;
2275 typename regex_traits<_CharT>::char_class_type __neg_mask_;
Howard Hinnant3034c902010-07-13 21:48:06 +00002276 bool __negate_;
2277 bool __icase_;
2278 bool __collate_;
Howard Hinnant5d4aaa42010-07-14 15:45:11 +00002279 bool __might_have_digraph_;
Howard Hinnant3034c902010-07-13 21:48:06 +00002280
2281 __bracket_expression(const __bracket_expression&);
2282 __bracket_expression& operator=(const __bracket_expression&);
2283public:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002284 typedef _VSTD::__state<_CharT> __state;
Howard Hinnant3034c902010-07-13 21:48:06 +00002285
Howard Hinnant7ca9d942010-09-23 15:13:20 +00002286 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3034c902010-07-13 21:48:06 +00002287 __bracket_expression(const _Traits& __traits, __node<_CharT>* __s,
2288 bool __negate, bool __icase, bool __collate)
Howard Hinnant70b3e192010-07-28 17:35:27 +00002289 : base(__s), __traits_(__traits), __mask_(), __neg_mask_(),
2290 __negate_(__negate), __icase_(__icase), __collate_(__collate),
Howard Hinnant5d4aaa42010-07-14 15:45:11 +00002291 __might_have_digraph_(__traits_.getloc().name() != "C") {}
Howard Hinnant3034c902010-07-13 21:48:06 +00002292
2293 virtual void __exec(__state&) const;
2294
Howard Hinnant7ca9d942010-09-23 15:13:20 +00002295 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant70b3e192010-07-28 17:35:27 +00002296 bool __negated() const {return __negate_;}
2297
Howard Hinnant7ca9d942010-09-23 15:13:20 +00002298 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3034c902010-07-13 21:48:06 +00002299 void __add_char(_CharT __c)
2300 {
2301 if (__icase_)
2302 __chars_.push_back(__traits_.translate_nocase(__c));
2303 else if (__collate_)
2304 __chars_.push_back(__traits_.translate(__c));
2305 else
2306 __chars_.push_back(__c);
2307 }
Howard Hinnant7ca9d942010-09-23 15:13:20 +00002308 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant70b3e192010-07-28 17:35:27 +00002309 void __add_neg_char(_CharT __c)
2310 {
2311 if (__icase_)
2312 __neg_chars_.push_back(__traits_.translate_nocase(__c));
2313 else if (__collate_)
2314 __neg_chars_.push_back(__traits_.translate(__c));
2315 else
2316 __neg_chars_.push_back(__c);
2317 }
Howard Hinnant7ca9d942010-09-23 15:13:20 +00002318 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3034c902010-07-13 21:48:06 +00002319 void __add_range(string_type __b, string_type __e)
2320 {
2321 if (__collate_)
2322 {
2323 if (__icase_)
2324 {
2325 for (size_t __i = 0; __i < __b.size(); ++__i)
2326 __b[__i] = __traits_.translate_nocase(__b[__i]);
2327 for (size_t __i = 0; __i < __e.size(); ++__i)
2328 __e[__i] = __traits_.translate_nocase(__e[__i]);
2329 }
2330 else
2331 {
2332 for (size_t __i = 0; __i < __b.size(); ++__i)
2333 __b[__i] = __traits_.translate(__b[__i]);
2334 for (size_t __i = 0; __i < __e.size(); ++__i)
2335 __e[__i] = __traits_.translate(__e[__i]);
2336 }
2337 __ranges_.push_back(make_pair(
2338 __traits_.transform(__b.begin(), __b.end()),
2339 __traits_.transform(__e.begin(), __e.end())));
2340 }
2341 else
2342 {
2343 if (__b.size() != 1 || __e.size() != 1)
Marshall Clowa5212112019-05-28 22:42:32 +00002344 __throw_regex_error<regex_constants::error_range>();
Howard Hinnant3034c902010-07-13 21:48:06 +00002345 if (__icase_)
2346 {
2347 __b[0] = __traits_.translate_nocase(__b[0]);
2348 __e[0] = __traits_.translate_nocase(__e[0]);
2349 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002350 __ranges_.push_back(make_pair(_VSTD::move(__b), _VSTD::move(__e)));
Howard Hinnant3034c902010-07-13 21:48:06 +00002351 }
2352 }
Howard Hinnant7ca9d942010-09-23 15:13:20 +00002353 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3034c902010-07-13 21:48:06 +00002354 void __add_digraph(_CharT __c1, _CharT __c2)
2355 {
2356 if (__icase_)
2357 __digraphs_.push_back(make_pair(__traits_.translate_nocase(__c1),
2358 __traits_.translate_nocase(__c2)));
2359 else if (__collate_)
2360 __digraphs_.push_back(make_pair(__traits_.translate(__c1),
2361 __traits_.translate(__c2)));
2362 else
2363 __digraphs_.push_back(make_pair(__c1, __c2));
2364 }
Howard Hinnant7ca9d942010-09-23 15:13:20 +00002365 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3034c902010-07-13 21:48:06 +00002366 void __add_equivalence(const string_type& __s)
2367 {__equivalences_.push_back(__s);}
Howard Hinnant7ca9d942010-09-23 15:13:20 +00002368 _LIBCPP_INLINE_VISIBILITY
Dan Albert49f384c2014-07-29 19:23:39 +00002369 void __add_class(typename regex_traits<_CharT>::char_class_type __mask)
Howard Hinnant3034c902010-07-13 21:48:06 +00002370 {__mask_ |= __mask;}
Howard Hinnant7ca9d942010-09-23 15:13:20 +00002371 _LIBCPP_INLINE_VISIBILITY
Dan Albert49f384c2014-07-29 19:23:39 +00002372 void __add_neg_class(typename regex_traits<_CharT>::char_class_type __mask)
Howard Hinnant70b3e192010-07-28 17:35:27 +00002373 {__neg_mask_ |= __mask;}
Howard Hinnant3034c902010-07-13 21:48:06 +00002374};
2375
2376template <class _CharT, class _Traits>
2377void
2378__bracket_expression<_CharT, _Traits>::__exec(__state& __s) const
2379{
2380 bool __found = false;
2381 unsigned __consumed = 0;
2382 if (__s.__current_ != __s.__last_)
2383 {
2384 ++__consumed;
Howard Hinnant5d4aaa42010-07-14 15:45:11 +00002385 if (__might_have_digraph_)
Howard Hinnant3034c902010-07-13 21:48:06 +00002386 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002387 const _CharT* __next = _VSTD::next(__s.__current_);
Howard Hinnant5d4aaa42010-07-14 15:45:11 +00002388 if (__next != __s.__last_)
Howard Hinnant3034c902010-07-13 21:48:06 +00002389 {
Howard Hinnant5d4aaa42010-07-14 15:45:11 +00002390 pair<_CharT, _CharT> __ch2(*__s.__current_, *__next);
2391 if (__icase_)
Howard Hinnant3034c902010-07-13 21:48:06 +00002392 {
Howard Hinnant5d4aaa42010-07-14 15:45:11 +00002393 __ch2.first = __traits_.translate_nocase(__ch2.first);
2394 __ch2.second = __traits_.translate_nocase(__ch2.second);
2395 }
2396 else if (__collate_)
2397 {
2398 __ch2.first = __traits_.translate(__ch2.first);
2399 __ch2.second = __traits_.translate(__ch2.second);
2400 }
2401 if (!__traits_.lookup_collatename(&__ch2.first, &__ch2.first+2).empty())
2402 {
2403 // __ch2 is a digraph in this locale
2404 ++__consumed;
2405 for (size_t __i = 0; __i < __digraphs_.size(); ++__i)
2406 {
2407 if (__ch2 == __digraphs_[__i])
2408 {
2409 __found = true;
2410 goto __exit;
2411 }
2412 }
2413 if (__collate_ && !__ranges_.empty())
2414 {
2415 string_type __s2 = __traits_.transform(&__ch2.first,
2416 &__ch2.first + 2);
2417 for (size_t __i = 0; __i < __ranges_.size(); ++__i)
2418 {
2419 if (__ranges_[__i].first <= __s2 &&
2420 __s2 <= __ranges_[__i].second)
2421 {
2422 __found = true;
2423 goto __exit;
2424 }
2425 }
2426 }
2427 if (!__equivalences_.empty())
2428 {
2429 string_type __s2 = __traits_.transform_primary(&__ch2.first,
2430 &__ch2.first + 2);
2431 for (size_t __i = 0; __i < __equivalences_.size(); ++__i)
2432 {
2433 if (__s2 == __equivalences_[__i])
2434 {
2435 __found = true;
2436 goto __exit;
2437 }
2438 }
2439 }
2440 if (__traits_.isctype(__ch2.first, __mask_) &&
2441 __traits_.isctype(__ch2.second, __mask_))
Howard Hinnant3034c902010-07-13 21:48:06 +00002442 {
2443 __found = true;
2444 goto __exit;
2445 }
Howard Hinnant70b3e192010-07-28 17:35:27 +00002446 if (!__traits_.isctype(__ch2.first, __neg_mask_) &&
2447 !__traits_.isctype(__ch2.second, __neg_mask_))
2448 {
2449 __found = true;
2450 goto __exit;
2451 }
Howard Hinnant3034c902010-07-13 21:48:06 +00002452 goto __exit;
2453 }
Howard Hinnant3034c902010-07-13 21:48:06 +00002454 }
2455 }
2456 // test *__s.__current_ as not a digraph
2457 _CharT __ch = *__s.__current_;
2458 if (__icase_)
2459 __ch = __traits_.translate_nocase(__ch);
2460 else if (__collate_)
2461 __ch = __traits_.translate(__ch);
2462 for (size_t __i = 0; __i < __chars_.size(); ++__i)
2463 {
2464 if (__ch == __chars_[__i])
2465 {
2466 __found = true;
2467 goto __exit;
2468 }
2469 }
Louis Dionne9023f022018-08-24 14:10:28 +00002470 // When there's at least one of __neg_chars_ and __neg_mask_, the set
2471 // of "__found" chars is
Marshall Clow42af8d92017-10-18 16:49:22 +00002472 // union(complement(union(__neg_chars_, __neg_mask_)),
2473 // other cases...)
2474 //
Louis Dionne9023f022018-08-24 14:10:28 +00002475 // It doesn't make sense to check this when there are no __neg_chars_
2476 // and no __neg_mask_.
2477 if (!(__neg_mask_ == 0 && __neg_chars_.empty()))
Howard Hinnant70b3e192010-07-28 17:35:27 +00002478 {
Louis Dionne9023f022018-08-24 14:10:28 +00002479 const bool __in_neg_mask = __traits_.isctype(__ch, __neg_mask_);
Marshall Clow42af8d92017-10-18 16:49:22 +00002480 const bool __in_neg_chars =
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05002481 _VSTD::find(__neg_chars_.begin(), __neg_chars_.end(), __ch) !=
Marshall Clow42af8d92017-10-18 16:49:22 +00002482 __neg_chars_.end();
2483 if (!(__in_neg_mask || __in_neg_chars))
2484 {
Howard Hinnant70b3e192010-07-28 17:35:27 +00002485 __found = true;
2486 goto __exit;
Marshall Clow42af8d92017-10-18 16:49:22 +00002487 }
Howard Hinnant70b3e192010-07-28 17:35:27 +00002488 }
Howard Hinnant3034c902010-07-13 21:48:06 +00002489 if (!__ranges_.empty())
2490 {
2491 string_type __s2 = __collate_ ?
2492 __traits_.transform(&__ch, &__ch + 1) :
2493 string_type(1, __ch);
2494 for (size_t __i = 0; __i < __ranges_.size(); ++__i)
2495 {
2496 if (__ranges_[__i].first <= __s2 && __s2 <= __ranges_[__i].second)
2497 {
2498 __found = true;
2499 goto __exit;
2500 }
2501 }
2502 }
2503 if (!__equivalences_.empty())
2504 {
2505 string_type __s2 = __traits_.transform_primary(&__ch, &__ch + 1);
2506 for (size_t __i = 0; __i < __equivalences_.size(); ++__i)
2507 {
2508 if (__s2 == __equivalences_[__i])
2509 {
2510 __found = true;
2511 goto __exit;
2512 }
2513 }
2514 }
2515 if (__traits_.isctype(__ch, __mask_))
Howard Hinnant70b3e192010-07-28 17:35:27 +00002516 {
Howard Hinnant3034c902010-07-13 21:48:06 +00002517 __found = true;
Howard Hinnant70b3e192010-07-28 17:35:27 +00002518 goto __exit;
2519 }
Howard Hinnant3034c902010-07-13 21:48:06 +00002520 }
2521 else
2522 __found = __negate_; // force reject
2523__exit:
2524 if (__found != __negate_)
2525 {
Howard Hinnant3034c902010-07-13 21:48:06 +00002526 __s.__do_ = __state::__accept_and_consume;
2527 __s.__current_ += __consumed;
2528 __s.__node_ = this->first();
2529 }
2530 else
2531 {
2532 __s.__do_ = __state::__reject;
2533 __s.__node_ = nullptr;
2534 }
2535}
2536
Howard Hinnant944510a2011-06-14 19:58:17 +00002537template <class _CharT, class _Traits> class __lookahead;
Howard Hinnantf5cb6eb2010-06-30 17:22:19 +00002538
Howard Hinnant6c891682010-06-24 21:28:00 +00002539template <class _CharT, class _Traits = regex_traits<_CharT> >
Richard Smith256954d2020-11-11 17:12:18 -08002540 class _LIBCPP_TEMPLATE_VIS basic_regex;
2541
2542typedef basic_regex<char> regex;
2543typedef basic_regex<wchar_t> wregex;
2544
2545template <class _CharT, class _Traits>
2546class
2547 _LIBCPP_TEMPLATE_VIS
2548 _LIBCPP_PREFERRED_NAME(regex)
2549 _LIBCPP_PREFERRED_NAME(wregex)
2550 basic_regex
Howard Hinnant6c891682010-06-24 21:28:00 +00002551{
2552public:
2553 // types:
2554 typedef _CharT value_type;
Hubert Tong1f1ae9c2016-08-02 21:34:48 +00002555 typedef _Traits traits_type;
2556 typedef typename _Traits::string_type string_type;
Howard Hinnant6c891682010-06-24 21:28:00 +00002557 typedef regex_constants::syntax_option_type flag_type;
2558 typedef typename _Traits::locale_type locale_type;
2559
2560private:
2561 _Traits __traits_;
2562 flag_type __flags_;
2563 unsigned __marked_count_;
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00002564 unsigned __loop_count_;
Howard Hinnant67ad2132010-06-29 18:37:43 +00002565 int __open_count_;
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00002566 shared_ptr<__empty_state<_CharT> > __start_;
2567 __owns_one_state<_CharT>* __end_;
2568
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002569 typedef _VSTD::__state<_CharT> __state;
2570 typedef _VSTD::__node<_CharT> __node;
Howard Hinnant6c891682010-06-24 21:28:00 +00002571
2572public:
2573 // constants:
Howard Hinnant5ddd33c2012-07-21 01:31:58 +00002574 static const regex_constants::syntax_option_type icase = regex_constants::icase;
2575 static const regex_constants::syntax_option_type nosubs = regex_constants::nosubs;
2576 static const regex_constants::syntax_option_type optimize = regex_constants::optimize;
2577 static const regex_constants::syntax_option_type collate = regex_constants::collate;
2578 static const regex_constants::syntax_option_type ECMAScript = regex_constants::ECMAScript;
2579 static const regex_constants::syntax_option_type basic = regex_constants::basic;
2580 static const regex_constants::syntax_option_type extended = regex_constants::extended;
2581 static const regex_constants::syntax_option_type awk = regex_constants::awk;
2582 static const regex_constants::syntax_option_type grep = regex_constants::grep;
2583 static const regex_constants::syntax_option_type egrep = regex_constants::egrep;
Mark de Wevera989cce2020-11-18 18:09:13 +01002584 static const regex_constants::syntax_option_type multiline = regex_constants::multiline;
Howard Hinnant6c891682010-06-24 21:28:00 +00002585
2586 // construct/copy/destroy:
Howard Hinnant7ca9d942010-09-23 15:13:20 +00002587 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00002588 basic_regex()
Marshall Clow88a30872019-03-28 17:30:23 +00002589 : __flags_(regex_constants::ECMAScript), __marked_count_(0), __loop_count_(0), __open_count_(0),
Bruce Mitchener170d8972020-11-24 12:53:53 -05002590 __end_(nullptr)
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00002591 {}
Howard Hinnant7ca9d942010-09-23 15:13:20 +00002592 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6c891682010-06-24 21:28:00 +00002593 explicit basic_regex(const value_type* __p, flag_type __f = regex_constants::ECMAScript)
Howard Hinnantaad0aa62010-07-09 00:15:26 +00002594 : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
Bruce Mitchener170d8972020-11-24 12:53:53 -05002595 __end_(nullptr)
Marshall Clow88a30872019-03-28 17:30:23 +00002596 {
Mark de Wevera0ad9762019-11-09 17:01:37 +01002597 __init(__p, __p + __traits_.length(__p));
Marshall Clow88a30872019-03-28 17:30:23 +00002598 }
2599
Howard Hinnant7ca9d942010-09-23 15:13:20 +00002600 _LIBCPP_INLINE_VISIBILITY
Hubert Tong19662862016-08-07 22:26:04 +00002601 basic_regex(const value_type* __p, size_t __len, flag_type __f = regex_constants::ECMAScript)
Howard Hinnantaad0aa62010-07-09 00:15:26 +00002602 : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
Bruce Mitchener170d8972020-11-24 12:53:53 -05002603 __end_(nullptr)
Marshall Clow88a30872019-03-28 17:30:23 +00002604 {
Mark de Wevera0ad9762019-11-09 17:01:37 +01002605 __init(__p, __p + __len);
Marshall Clow88a30872019-03-28 17:30:23 +00002606 }
2607
Howard Hinnant126da6a2010-07-27 22:20:32 +00002608// basic_regex(const basic_regex&) = default;
2609// basic_regex(basic_regex&&) = default;
Howard Hinnant6c891682010-06-24 21:28:00 +00002610 template <class _ST, class _SA>
Howard Hinnant7ca9d942010-09-23 15:13:20 +00002611 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6c891682010-06-24 21:28:00 +00002612 explicit basic_regex(const basic_string<value_type, _ST, _SA>& __p,
2613 flag_type __f = regex_constants::ECMAScript)
Howard Hinnantaad0aa62010-07-09 00:15:26 +00002614 : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
Bruce Mitchener170d8972020-11-24 12:53:53 -05002615 __end_(nullptr)
Marshall Clow88a30872019-03-28 17:30:23 +00002616 {
Mark de Wevera0ad9762019-11-09 17:01:37 +01002617 __init(__p.begin(), __p.end());
Marshall Clow88a30872019-03-28 17:30:23 +00002618 }
2619
Howard Hinnant6c891682010-06-24 21:28:00 +00002620 template <class _ForwardIterator>
Howard Hinnant7ca9d942010-09-23 15:13:20 +00002621 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6c891682010-06-24 21:28:00 +00002622 basic_regex(_ForwardIterator __first, _ForwardIterator __last,
2623 flag_type __f = regex_constants::ECMAScript)
Howard Hinnantaad0aa62010-07-09 00:15:26 +00002624 : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
Bruce Mitchener170d8972020-11-24 12:53:53 -05002625 __end_(nullptr)
Marshall Clow88a30872019-03-28 17:30:23 +00002626 {
Mark de Wevera0ad9762019-11-09 17:01:37 +01002627 __init(__first, __last);
Marshall Clow88a30872019-03-28 17:30:23 +00002628 }
Eric Fiselier6f8516f2017-04-18 23:42:15 +00002629#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant7ca9d942010-09-23 15:13:20 +00002630 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6c891682010-06-24 21:28:00 +00002631 basic_regex(initializer_list<value_type> __il,
2632 flag_type __f = regex_constants::ECMAScript)
Howard Hinnantaad0aa62010-07-09 00:15:26 +00002633 : __flags_(__f), __marked_count_(0), __loop_count_(0), __open_count_(0),
Bruce Mitchener170d8972020-11-24 12:53:53 -05002634 __end_(nullptr)
Marshall Clow88a30872019-03-28 17:30:23 +00002635 {
Mark de Wevera0ad9762019-11-09 17:01:37 +01002636 __init(__il.begin(), __il.end());
Marshall Clow88a30872019-03-28 17:30:23 +00002637 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002638#endif // _LIBCPP_CXX03_LANG
Howard Hinnant6c891682010-06-24 21:28:00 +00002639
Howard Hinnant997621e2010-08-13 18:11:23 +00002640// ~basic_regex() = default;
Howard Hinnant6c891682010-06-24 21:28:00 +00002641
Howard Hinnant126da6a2010-07-27 22:20:32 +00002642// basic_regex& operator=(const basic_regex&) = default;
2643// basic_regex& operator=(basic_regex&&) = default;
Howard Hinnant7ca9d942010-09-23 15:13:20 +00002644 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant997621e2010-08-13 18:11:23 +00002645 basic_regex& operator=(const value_type* __p)
2646 {return assign(__p);}
Eric Fiselier6f8516f2017-04-18 23:42:15 +00002647#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant7ca9d942010-09-23 15:13:20 +00002648 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant997621e2010-08-13 18:11:23 +00002649 basic_regex& operator=(initializer_list<value_type> __il)
2650 {return assign(__il);}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002651#endif // _LIBCPP_CXX03_LANG
Howard Hinnant6c891682010-06-24 21:28:00 +00002652 template <class _ST, class _SA>
Howard Hinnant7ca9d942010-09-23 15:13:20 +00002653 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant997621e2010-08-13 18:11:23 +00002654 basic_regex& operator=(const basic_string<value_type, _ST, _SA>& __p)
2655 {return assign(__p);}
Howard Hinnant6c891682010-06-24 21:28:00 +00002656
2657 // assign:
Howard Hinnant7ca9d942010-09-23 15:13:20 +00002658 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant997621e2010-08-13 18:11:23 +00002659 basic_regex& assign(const basic_regex& __that)
2660 {return *this = __that;}
Eric Fiselier6f8516f2017-04-18 23:42:15 +00002661#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant5ddd33c2012-07-21 01:31:58 +00002662 _LIBCPP_INLINE_VISIBILITY
2663 basic_regex& assign(basic_regex&& __that) _NOEXCEPT
2664 {return *this = _VSTD::move(__that);}
2665#endif
Howard Hinnant7ca9d942010-09-23 15:13:20 +00002666 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant997621e2010-08-13 18:11:23 +00002667 basic_regex& assign(const value_type* __p, flag_type __f = regex_constants::ECMAScript)
2668 {return assign(__p, __p + __traits_.length(__p), __f);}
Howard Hinnant7ca9d942010-09-23 15:13:20 +00002669 _LIBCPP_INLINE_VISIBILITY
Marshall Clowd4028932019-09-25 16:40:30 +00002670 basic_regex& assign(const value_type* __p, size_t __len, flag_type __f = regex_constants::ECMAScript)
Howard Hinnant997621e2010-08-13 18:11:23 +00002671 {return assign(__p, __p + __len, __f);}
Howard Hinnant6c891682010-06-24 21:28:00 +00002672 template <class _ST, class _SA>
Howard Hinnant7ca9d942010-09-23 15:13:20 +00002673 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6c891682010-06-24 21:28:00 +00002674 basic_regex& assign(const basic_string<value_type, _ST, _SA>& __s,
Howard Hinnant997621e2010-08-13 18:11:23 +00002675 flag_type __f = regex_constants::ECMAScript)
2676 {return assign(__s.begin(), __s.end(), __f);}
2677
Howard Hinnant6c891682010-06-24 21:28:00 +00002678 template <class _InputIterator>
Howard Hinnant7ca9d942010-09-23 15:13:20 +00002679 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant997621e2010-08-13 18:11:23 +00002680 typename enable_if
2681 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002682 __is_cpp17_input_iterator <_InputIterator>::value &&
2683 !__is_cpp17_forward_iterator<_InputIterator>::value,
Howard Hinnant997621e2010-08-13 18:11:23 +00002684 basic_regex&
2685 >::type
2686 assign(_InputIterator __first, _InputIterator __last,
2687 flag_type __f = regex_constants::ECMAScript)
2688 {
2689 basic_string<_CharT> __t(__first, __last);
2690 return assign(__t.begin(), __t.end(), __f);
2691 }
2692
2693private:
Howard Hinnant7ca9d942010-09-23 15:13:20 +00002694 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant997621e2010-08-13 18:11:23 +00002695 void __member_init(flag_type __f)
2696 {
2697 __flags_ = __f;
2698 __marked_count_ = 0;
2699 __loop_count_ = 0;
2700 __open_count_ = 0;
2701 __end_ = nullptr;
Howard Hinnant997621e2010-08-13 18:11:23 +00002702 }
2703public:
2704
2705 template <class _ForwardIterator>
Howard Hinnant7ca9d942010-09-23 15:13:20 +00002706 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant997621e2010-08-13 18:11:23 +00002707 typename enable_if
2708 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002709 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnant997621e2010-08-13 18:11:23 +00002710 basic_regex&
2711 >::type
2712 assign(_ForwardIterator __first, _ForwardIterator __last,
2713 flag_type __f = regex_constants::ECMAScript)
2714 {
Marshall Clowce03dc12015-01-13 16:49:52 +00002715 return assign(basic_regex(__first, __last, __f));
Howard Hinnant997621e2010-08-13 18:11:23 +00002716 }
2717
Eric Fiselier6f8516f2017-04-18 23:42:15 +00002718#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002719
Howard Hinnant7ca9d942010-09-23 15:13:20 +00002720 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6c891682010-06-24 21:28:00 +00002721 basic_regex& assign(initializer_list<value_type> __il,
Howard Hinnant997621e2010-08-13 18:11:23 +00002722 flag_type __f = regex_constants::ECMAScript)
2723 {return assign(__il.begin(), __il.end(), __f);}
Howard Hinnant6c891682010-06-24 21:28:00 +00002724
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002725#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002726
Howard Hinnant6c891682010-06-24 21:28:00 +00002727 // const operations:
Howard Hinnant7ca9d942010-09-23 15:13:20 +00002728 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6c891682010-06-24 21:28:00 +00002729 unsigned mark_count() const {return __marked_count_;}
Howard Hinnant7ca9d942010-09-23 15:13:20 +00002730 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6c891682010-06-24 21:28:00 +00002731 flag_type flags() const {return __flags_;}
2732
2733 // locale:
Howard Hinnant7ca9d942010-09-23 15:13:20 +00002734 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant997621e2010-08-13 18:11:23 +00002735 locale_type imbue(locale_type __loc)
2736 {
2737 __member_init(ECMAScript);
2738 __start_.reset();
2739 return __traits_.imbue(__loc);
2740 }
Howard Hinnant7ca9d942010-09-23 15:13:20 +00002741 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant6c891682010-06-24 21:28:00 +00002742 locale_type getloc() const {return __traits_.getloc();}
2743
2744 // swap:
Howard Hinnant997621e2010-08-13 18:11:23 +00002745 void swap(basic_regex& __r);
Howard Hinnant6c891682010-06-24 21:28:00 +00002746
2747private:
Howard Hinnant7ca9d942010-09-23 15:13:20 +00002748 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00002749 unsigned __loop_count() const {return __loop_count_;}
2750
Mark de Wevera989cce2020-11-18 18:09:13 +01002751 _LIBCPP_INLINE_VISIBILITY
2752 bool __use_multiline() const
2753 {
2754 return __get_grammar(__flags_) == ECMAScript && (__flags_ & multiline);
2755 }
2756
Howard Hinnant6c891682010-06-24 21:28:00 +00002757 template <class _ForwardIterator>
Mark de Wevera0ad9762019-11-09 17:01:37 +01002758 void
2759 __init(_ForwardIterator __first, _ForwardIterator __last);
2760 template <class _ForwardIterator>
Howard Hinnant126da6a2010-07-27 22:20:32 +00002761 _ForwardIterator
2762 __parse(_ForwardIterator __first, _ForwardIterator __last);
Howard Hinnant6c891682010-06-24 21:28:00 +00002763 template <class _ForwardIterator>
2764 _ForwardIterator
2765 __parse_basic_reg_exp(_ForwardIterator __first, _ForwardIterator __last);
2766 template <class _ForwardIterator>
2767 _ForwardIterator
2768 __parse_RE_expression(_ForwardIterator __first, _ForwardIterator __last);
2769 template <class _ForwardIterator>
2770 _ForwardIterator
2771 __parse_simple_RE(_ForwardIterator __first, _ForwardIterator __last);
2772 template <class _ForwardIterator>
2773 _ForwardIterator
2774 __parse_nondupl_RE(_ForwardIterator __first, _ForwardIterator __last);
2775 template <class _ForwardIterator>
2776 _ForwardIterator
2777 __parse_one_char_or_coll_elem_RE(_ForwardIterator __first, _ForwardIterator __last);
2778 template <class _ForwardIterator>
2779 _ForwardIterator
2780 __parse_Back_open_paren(_ForwardIterator __first, _ForwardIterator __last);
2781 template <class _ForwardIterator>
2782 _ForwardIterator
2783 __parse_Back_close_paren(_ForwardIterator __first, _ForwardIterator __last);
2784 template <class _ForwardIterator>
2785 _ForwardIterator
2786 __parse_Back_open_brace(_ForwardIterator __first, _ForwardIterator __last);
2787 template <class _ForwardIterator>
2788 _ForwardIterator
2789 __parse_Back_close_brace(_ForwardIterator __first, _ForwardIterator __last);
2790 template <class _ForwardIterator>
2791 _ForwardIterator
2792 __parse_BACKREF(_ForwardIterator __first, _ForwardIterator __last);
2793 template <class _ForwardIterator>
2794 _ForwardIterator
2795 __parse_ORD_CHAR(_ForwardIterator __first, _ForwardIterator __last);
2796 template <class _ForwardIterator>
2797 _ForwardIterator
2798 __parse_QUOTED_CHAR(_ForwardIterator __first, _ForwardIterator __last);
2799 template <class _ForwardIterator>
2800 _ForwardIterator
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00002801 __parse_RE_dupl_symbol(_ForwardIterator __first, _ForwardIterator __last,
Howard Hinnant5bf96132010-07-08 17:43:58 +00002802 __owns_one_state<_CharT>* __s,
2803 unsigned __mexp_begin, unsigned __mexp_end);
Howard Hinnant89a40572010-06-25 20:56:08 +00002804 template <class _ForwardIterator>
2805 _ForwardIterator
Howard Hinnant16d65422010-07-16 19:08:36 +00002806 __parse_ERE_dupl_symbol(_ForwardIterator __first, _ForwardIterator __last,
2807 __owns_one_state<_CharT>* __s,
2808 unsigned __mexp_begin, unsigned __mexp_end);
Howard Hinnant67ad2132010-06-29 18:37:43 +00002809 template <class _ForwardIterator>
2810 _ForwardIterator
Howard Hinnant89a40572010-06-25 20:56:08 +00002811 __parse_bracket_expression(_ForwardIterator __first, _ForwardIterator __last);
2812 template <class _ForwardIterator>
2813 _ForwardIterator
Howard Hinnant3034c902010-07-13 21:48:06 +00002814 __parse_follow_list(_ForwardIterator __first, _ForwardIterator __last,
2815 __bracket_expression<_CharT, _Traits>* __ml);
Howard Hinnant89a40572010-06-25 20:56:08 +00002816 template <class _ForwardIterator>
2817 _ForwardIterator
Howard Hinnant3034c902010-07-13 21:48:06 +00002818 __parse_expression_term(_ForwardIterator __first, _ForwardIterator __last,
2819 __bracket_expression<_CharT, _Traits>* __ml);
Howard Hinnant89a40572010-06-25 20:56:08 +00002820 template <class _ForwardIterator>
2821 _ForwardIterator
Howard Hinnant3034c902010-07-13 21:48:06 +00002822 __parse_equivalence_class(_ForwardIterator __first, _ForwardIterator __last,
2823 __bracket_expression<_CharT, _Traits>* __ml);
Howard Hinnant89a40572010-06-25 20:56:08 +00002824 template <class _ForwardIterator>
2825 _ForwardIterator
Howard Hinnant3034c902010-07-13 21:48:06 +00002826 __parse_character_class(_ForwardIterator __first, _ForwardIterator __last,
2827 __bracket_expression<_CharT, _Traits>* __ml);
Howard Hinnant89a40572010-06-25 20:56:08 +00002828 template <class _ForwardIterator>
2829 _ForwardIterator
Howard Hinnant3034c902010-07-13 21:48:06 +00002830 __parse_collating_symbol(_ForwardIterator __first, _ForwardIterator __last,
2831 basic_string<_CharT>& __col_sym);
Howard Hinnant89a40572010-06-25 20:56:08 +00002832 template <class _ForwardIterator>
2833 _ForwardIterator
2834 __parse_DUP_COUNT(_ForwardIterator __first, _ForwardIterator __last, int& __c);
Howard Hinnant67ad2132010-06-29 18:37:43 +00002835 template <class _ForwardIterator>
2836 _ForwardIterator
2837 __parse_extended_reg_exp(_ForwardIterator __first, _ForwardIterator __last);
2838 template <class _ForwardIterator>
2839 _ForwardIterator
2840 __parse_ERE_branch(_ForwardIterator __first, _ForwardIterator __last);
2841 template <class _ForwardIterator>
2842 _ForwardIterator
2843 __parse_ERE_expression(_ForwardIterator __first, _ForwardIterator __last);
2844 template <class _ForwardIterator>
2845 _ForwardIterator
2846 __parse_one_char_or_coll_elem_ERE(_ForwardIterator __first, _ForwardIterator __last);
2847 template <class _ForwardIterator>
2848 _ForwardIterator
2849 __parse_ORD_CHAR_ERE(_ForwardIterator __first, _ForwardIterator __last);
2850 template <class _ForwardIterator>
2851 _ForwardIterator
2852 __parse_QUOTED_CHAR_ERE(_ForwardIterator __first, _ForwardIterator __last);
Howard Hinnante1053822010-07-22 17:53:24 +00002853 template <class _ForwardIterator>
2854 _ForwardIterator
2855 __parse_ecma_exp(_ForwardIterator __first, _ForwardIterator __last);
2856 template <class _ForwardIterator>
2857 _ForwardIterator
2858 __parse_alternative(_ForwardIterator __first, _ForwardIterator __last);
2859 template <class _ForwardIterator>
2860 _ForwardIterator
2861 __parse_term(_ForwardIterator __first, _ForwardIterator __last);
2862 template <class _ForwardIterator>
2863 _ForwardIterator
2864 __parse_assertion(_ForwardIterator __first, _ForwardIterator __last);
2865 template <class _ForwardIterator>
2866 _ForwardIterator
2867 __parse_atom(_ForwardIterator __first, _ForwardIterator __last);
2868 template <class _ForwardIterator>
2869 _ForwardIterator
Howard Hinnant70d27852010-07-27 01:25:38 +00002870 __parse_atom_escape(_ForwardIterator __first, _ForwardIterator __last);
2871 template <class _ForwardIterator>
2872 _ForwardIterator
2873 __parse_decimal_escape(_ForwardIterator __first, _ForwardIterator __last);
2874 template <class _ForwardIterator>
2875 _ForwardIterator
2876 __parse_character_class_escape(_ForwardIterator __first, _ForwardIterator __last);
2877 template <class _ForwardIterator>
2878 _ForwardIterator
Howard Hinnant70b3e192010-07-28 17:35:27 +00002879 __parse_character_escape(_ForwardIterator __first, _ForwardIterator __last,
2880 basic_string<_CharT>* __str = nullptr);
Howard Hinnant70d27852010-07-27 01:25:38 +00002881 template <class _ForwardIterator>
2882 _ForwardIterator
2883 __parse_pattern_character(_ForwardIterator __first, _ForwardIterator __last);
Howard Hinnanteaf649e2010-07-27 19:53:10 +00002884 template <class _ForwardIterator>
2885 _ForwardIterator
2886 __parse_grep(_ForwardIterator __first, _ForwardIterator __last);
2887 template <class _ForwardIterator>
2888 _ForwardIterator
2889 __parse_egrep(_ForwardIterator __first, _ForwardIterator __last);
Howard Hinnant70b3e192010-07-28 17:35:27 +00002890 template <class _ForwardIterator>
2891 _ForwardIterator
2892 __parse_class_escape(_ForwardIterator __first, _ForwardIterator __last,
2893 basic_string<_CharT>& __str,
2894 __bracket_expression<_CharT, _Traits>* __ml);
2895 template <class _ForwardIterator>
2896 _ForwardIterator
2897 __parse_awk_escape(_ForwardIterator __first, _ForwardIterator __last,
2898 basic_string<_CharT>* __str = nullptr);
Howard Hinnant6c891682010-06-24 21:28:00 +00002899
Louis Dionnef16eb592020-02-19 15:56:15 -05002900 bool __test_back_ref(_CharT c);
2901
Howard Hinnant7ca9d942010-09-23 15:13:20 +00002902 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant066ba512011-03-26 20:02:27 +00002903 void __push_l_anchor();
Howard Hinnantaad0aa62010-07-09 00:15:26 +00002904 void __push_r_anchor();
Howard Hinnantaa0874c2010-07-12 15:51:17 +00002905 void __push_match_any();
Howard Hinnant70d27852010-07-27 01:25:38 +00002906 void __push_match_any_but_newline();
Howard Hinnant7ca9d942010-09-23 15:13:20 +00002907 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5bf96132010-07-08 17:43:58 +00002908 void __push_greedy_inf_repeat(size_t __min, __owns_one_state<_CharT>* __s,
2909 unsigned __mexp_begin = 0, unsigned __mexp_end = 0)
2910 {__push_loop(__min, numeric_limits<size_t>::max(), __s,
2911 __mexp_begin, __mexp_end);}
Howard Hinnant7ca9d942010-09-23 15:13:20 +00002912 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant70d27852010-07-27 01:25:38 +00002913 void __push_nongreedy_inf_repeat(size_t __min, __owns_one_state<_CharT>* __s,
2914 unsigned __mexp_begin = 0, unsigned __mexp_end = 0)
2915 {__push_loop(__min, numeric_limits<size_t>::max(), __s,
2916 __mexp_begin, __mexp_end, false);}
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00002917 void __push_loop(size_t __min, size_t __max, __owns_one_state<_CharT>* __s,
2918 size_t __mexp_begin = 0, size_t __mexp_end = 0,
2919 bool __greedy = true);
Howard Hinnant3034c902010-07-13 21:48:06 +00002920 __bracket_expression<_CharT, _Traits>* __start_matching_list(bool __negate);
Howard Hinnant67ad2132010-06-29 18:37:43 +00002921 void __push_char(value_type __c);
Howard Hinnant2a315e32010-07-12 18:16:05 +00002922 void __push_back_ref(int __i);
Howard Hinnant16d65422010-07-16 19:08:36 +00002923 void __push_alternation(__owns_one_state<_CharT>* __sa,
2924 __owns_one_state<_CharT>* __sb);
Howard Hinnant93ef6552010-06-30 20:30:19 +00002925 void __push_begin_marked_subexpression();
2926 void __push_end_marked_subexpression(unsigned);
Howard Hinnante1053822010-07-22 17:53:24 +00002927 void __push_empty();
Howard Hinnant70d27852010-07-27 01:25:38 +00002928 void __push_word_boundary(bool);
Howard Hinnant3efac712013-07-23 16:18:04 +00002929 void __push_lookahead(const basic_regex&, bool, unsigned);
Howard Hinnantf5cb6eb2010-06-30 17:22:19 +00002930
Howard Hinnant66423212010-07-14 21:14:52 +00002931 template <class _Allocator>
Howard Hinnantf5cb6eb2010-06-30 17:22:19 +00002932 bool
Howard Hinnant66423212010-07-14 21:14:52 +00002933 __search(const _CharT* __first, const _CharT* __last,
2934 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnantf5cb6eb2010-06-30 17:22:19 +00002935 regex_constants::match_flag_type __flags) const;
2936
Howard Hinnant66423212010-07-14 21:14:52 +00002937 template <class _Allocator>
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00002938 bool
Howard Hinnant66423212010-07-14 21:14:52 +00002939 __match_at_start(const _CharT* __first, const _CharT* __last,
2940 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnant066ba512011-03-26 20:02:27 +00002941 regex_constants::match_flag_type __flags, bool) const;
Howard Hinnant70d27852010-07-27 01:25:38 +00002942 template <class _Allocator>
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00002943 bool
Howard Hinnant70d27852010-07-27 01:25:38 +00002944 __match_at_start_ecma(const _CharT* __first, const _CharT* __last,
2945 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnant066ba512011-03-26 20:02:27 +00002946 regex_constants::match_flag_type __flags, bool) const;
Howard Hinnant66423212010-07-14 21:14:52 +00002947 template <class _Allocator>
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00002948 bool
2949 __match_at_start_posix_nosubs(const _CharT* __first, const _CharT* __last,
Howard Hinnant66423212010-07-14 21:14:52 +00002950 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnant066ba512011-03-26 20:02:27 +00002951 regex_constants::match_flag_type __flags, bool) const;
Howard Hinnant66423212010-07-14 21:14:52 +00002952 template <class _Allocator>
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00002953 bool
Howard Hinnant66423212010-07-14 21:14:52 +00002954 __match_at_start_posix_subs(const _CharT* __first, const _CharT* __last,
2955 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnant066ba512011-03-26 20:02:27 +00002956 regex_constants::match_flag_type __flags, bool) const;
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00002957
Howard Hinnantc834c512011-11-29 18:15:50 +00002958 template <class _Bp, class _Ap, class _Cp, class _Tp>
Howard Hinnantf5cb6eb2010-06-30 17:22:19 +00002959 friend
2960 bool
Howard Hinnantc834c512011-11-29 18:15:50 +00002961 regex_search(_Bp, _Bp, match_results<_Bp, _Ap>&, const basic_regex<_Cp, _Tp>&,
Howard Hinnantf5cb6eb2010-06-30 17:22:19 +00002962 regex_constants::match_flag_type);
Howard Hinnant6c891682010-06-24 21:28:00 +00002963
Howard Hinnantc834c512011-11-29 18:15:50 +00002964 template <class _Ap, class _Cp, class _Tp>
Howard Hinnant66423212010-07-14 21:14:52 +00002965 friend
2966 bool
Howard Hinnantc834c512011-11-29 18:15:50 +00002967 regex_search(const _Cp*, const _Cp*, match_results<const _Cp*, _Ap>&,
2968 const basic_regex<_Cp, _Tp>&, regex_constants::match_flag_type);
Howard Hinnant66423212010-07-14 21:14:52 +00002969
Howard Hinnantc834c512011-11-29 18:15:50 +00002970 template <class _Bp, class _Cp, class _Tp>
Howard Hinnant66423212010-07-14 21:14:52 +00002971 friend
2972 bool
Howard Hinnantc834c512011-11-29 18:15:50 +00002973 regex_search(_Bp, _Bp, const basic_regex<_Cp, _Tp>&,
Howard Hinnant66423212010-07-14 21:14:52 +00002974 regex_constants::match_flag_type);
2975
Howard Hinnantc834c512011-11-29 18:15:50 +00002976 template <class _Cp, class _Tp>
Howard Hinnant66423212010-07-14 21:14:52 +00002977 friend
2978 bool
Howard Hinnantc834c512011-11-29 18:15:50 +00002979 regex_search(const _Cp*, const _Cp*,
2980 const basic_regex<_Cp, _Tp>&, regex_constants::match_flag_type);
Howard Hinnant66423212010-07-14 21:14:52 +00002981
Howard Hinnantc834c512011-11-29 18:15:50 +00002982 template <class _Cp, class _Ap, class _Tp>
Howard Hinnant66423212010-07-14 21:14:52 +00002983 friend
2984 bool
Howard Hinnantc834c512011-11-29 18:15:50 +00002985 regex_search(const _Cp*, match_results<const _Cp*, _Ap>&, const basic_regex<_Cp, _Tp>&,
Howard Hinnant66423212010-07-14 21:14:52 +00002986 regex_constants::match_flag_type);
2987
Howard Hinnantc834c512011-11-29 18:15:50 +00002988 template <class _ST, class _SA, class _Cp, class _Tp>
Howard Hinnant66423212010-07-14 21:14:52 +00002989 friend
2990 bool
Howard Hinnantc834c512011-11-29 18:15:50 +00002991 regex_search(const basic_string<_Cp, _ST, _SA>& __s,
2992 const basic_regex<_Cp, _Tp>& __e,
Howard Hinnant66423212010-07-14 21:14:52 +00002993 regex_constants::match_flag_type __flags);
2994
Howard Hinnantc834c512011-11-29 18:15:50 +00002995 template <class _ST, class _SA, class _Ap, class _Cp, class _Tp>
Howard Hinnant66423212010-07-14 21:14:52 +00002996 friend
2997 bool
Howard Hinnantc834c512011-11-29 18:15:50 +00002998 regex_search(const basic_string<_Cp, _ST, _SA>& __s,
2999 match_results<typename basic_string<_Cp, _ST, _SA>::const_iterator, _Ap>&,
3000 const basic_regex<_Cp, _Tp>& __e,
Howard Hinnant66423212010-07-14 21:14:52 +00003001 regex_constants::match_flag_type __flags);
Howard Hinnant126da6a2010-07-27 22:20:32 +00003002
Howard Hinnant4018c482013-06-29 23:45:43 +00003003 template <class _Iter, class _Ap, class _Cp, class _Tp>
3004 friend
3005 bool
3006 regex_search(__wrap_iter<_Iter> __first,
3007 __wrap_iter<_Iter> __last,
3008 match_results<__wrap_iter<_Iter>, _Ap>& __m,
3009 const basic_regex<_Cp, _Tp>& __e,
3010 regex_constants::match_flag_type __flags);
3011
Howard Hinnant126da6a2010-07-27 22:20:32 +00003012 template <class, class> friend class __lookahead;
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00003013};
Howard Hinnant6c891682010-06-24 21:28:00 +00003014
Marshall Clow2dce1f42018-05-23 01:57:02 +00003015#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
3016template <class _ForwardIterator,
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003017 class = typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value, nullptr_t>::type
Marshall Clow2dce1f42018-05-23 01:57:02 +00003018>
3019basic_regex(_ForwardIterator, _ForwardIterator,
3020 regex_constants::syntax_option_type = regex_constants::ECMAScript)
3021 -> basic_regex<typename iterator_traits<_ForwardIterator>::value_type>;
3022#endif
3023
Howard Hinnant6c891682010-06-24 21:28:00 +00003024template <class _CharT, class _Traits>
Howard Hinnant2c45cb42012-12-12 21:14:28 +00003025 const regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::icase;
3026template <class _CharT, class _Traits>
3027 const regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::nosubs;
3028template <class _CharT, class _Traits>
3029 const regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::optimize;
3030template <class _CharT, class _Traits>
3031 const regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::collate;
3032template <class _CharT, class _Traits>
3033 const regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::ECMAScript;
3034template <class _CharT, class _Traits>
3035 const regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::basic;
3036template <class _CharT, class _Traits>
3037 const regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::extended;
3038template <class _CharT, class _Traits>
3039 const regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::awk;
3040template <class _CharT, class _Traits>
3041 const regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::grep;
3042template <class _CharT, class _Traits>
3043 const regex_constants::syntax_option_type basic_regex<_CharT, _Traits>::egrep;
3044
3045template <class _CharT, class _Traits>
Howard Hinnant997621e2010-08-13 18:11:23 +00003046void
3047basic_regex<_CharT, _Traits>::swap(basic_regex& __r)
Howard Hinnant6c891682010-06-24 21:28:00 +00003048{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003049 using _VSTD::swap;
Howard Hinnant997621e2010-08-13 18:11:23 +00003050 swap(__traits_, __r.__traits_);
3051 swap(__flags_, __r.__flags_);
3052 swap(__marked_count_, __r.__marked_count_);
3053 swap(__loop_count_, __r.__loop_count_);
3054 swap(__open_count_, __r.__open_count_);
3055 swap(__start_, __r.__start_);
3056 swap(__end_, __r.__end_);
Howard Hinnant997621e2010-08-13 18:11:23 +00003057}
3058
3059template <class _CharT, class _Traits>
3060inline _LIBCPP_INLINE_VISIBILITY
3061void
3062swap(basic_regex<_CharT, _Traits>& __x, basic_regex<_CharT, _Traits>& __y)
3063{
3064 return __x.swap(__y);
Howard Hinnant6c891682010-06-24 21:28:00 +00003065}
3066
Howard Hinnant126da6a2010-07-27 22:20:32 +00003067// __lookahead
3068
3069template <class _CharT, class _Traits>
3070class __lookahead
3071 : public __owns_one_state<_CharT>
3072{
3073 typedef __owns_one_state<_CharT> base;
3074
3075 basic_regex<_CharT, _Traits> __exp_;
Howard Hinnant3efac712013-07-23 16:18:04 +00003076 unsigned __mexp_;
Howard Hinnant126da6a2010-07-27 22:20:32 +00003077 bool __invert_;
3078
3079 __lookahead(const __lookahead&);
3080 __lookahead& operator=(const __lookahead&);
3081public:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003082 typedef _VSTD::__state<_CharT> __state;
Howard Hinnant126da6a2010-07-27 22:20:32 +00003083
Howard Hinnant7ca9d942010-09-23 15:13:20 +00003084 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3efac712013-07-23 16:18:04 +00003085 __lookahead(const basic_regex<_CharT, _Traits>& __exp, bool __invert, __node<_CharT>* __s, unsigned __mexp)
Eric Fiseliera75ee262015-07-22 01:29:41 +00003086 : base(__s), __exp_(__exp), __mexp_(__mexp), __invert_(__invert) {}
Howard Hinnant126da6a2010-07-27 22:20:32 +00003087
3088 virtual void __exec(__state&) const;
Howard Hinnant126da6a2010-07-27 22:20:32 +00003089};
3090
3091template <class _CharT, class _Traits>
3092void
3093__lookahead<_CharT, _Traits>::__exec(__state& __s) const
3094{
3095 match_results<const _CharT*> __m;
3096 __m.__init(1 + __exp_.mark_count(), __s.__current_, __s.__last_);
Tim Shen11113f52016-10-27 21:40:34 +00003097 bool __matched = __exp_.__match_at_start_ecma(
3098 __s.__current_, __s.__last_,
3099 __m,
3100 (__s.__flags_ | regex_constants::match_continuous) &
3101 ~regex_constants::__full_match,
3102 __s.__at_first_ && __s.__current_ == __s.__first_);
Howard Hinnant126da6a2010-07-27 22:20:32 +00003103 if (__matched != __invert_)
3104 {
3105 __s.__do_ = __state::__accept_but_not_consume;
3106 __s.__node_ = this->first();
Howard Hinnant3efac712013-07-23 16:18:04 +00003107 for (unsigned __i = 1; __i < __m.size(); ++__i) {
3108 __s.__sub_matches_[__mexp_ + __i - 1] = __m.__matches_[__i];
3109 }
Howard Hinnant126da6a2010-07-27 22:20:32 +00003110 }
3111 else
3112 {
3113 __s.__do_ = __state::__reject;
3114 __s.__node_ = nullptr;
3115 }
3116}
3117
Howard Hinnant6c891682010-06-24 21:28:00 +00003118template <class _CharT, class _Traits>
3119template <class _ForwardIterator>
Mark de Wevera0ad9762019-11-09 17:01:37 +01003120void
3121basic_regex<_CharT, _Traits>::__init(_ForwardIterator __first, _ForwardIterator __last)
3122{
3123 if (__get_grammar(__flags_) == 0) __flags_ |= regex_constants::ECMAScript;
3124 _ForwardIterator __temp = __parse(__first, __last);
3125 if ( __temp != __last)
3126 __throw_regex_error<regex_constants::__re_err_parse>();
3127}
3128
3129template <class _CharT, class _Traits>
3130template <class _ForwardIterator>
Howard Hinnant126da6a2010-07-27 22:20:32 +00003131_ForwardIterator
Howard Hinnant6c891682010-06-24 21:28:00 +00003132basic_regex<_CharT, _Traits>::__parse(_ForwardIterator __first,
3133 _ForwardIterator __last)
3134{
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00003135 {
Howard Hinnantaa0874c2010-07-12 15:51:17 +00003136 unique_ptr<__node> __h(new __end_state<_CharT>);
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00003137 __start_.reset(new __empty_state<_CharT>(__h.get()));
3138 __h.release();
3139 __end_ = __start_.get();
3140 }
Marshall Clow88a30872019-03-28 17:30:23 +00003141 switch (__get_grammar(__flags_))
Howard Hinnant6c891682010-06-24 21:28:00 +00003142 {
3143 case ECMAScript:
Howard Hinnant126da6a2010-07-27 22:20:32 +00003144 __first = __parse_ecma_exp(__first, __last);
Howard Hinnant6c891682010-06-24 21:28:00 +00003145 break;
3146 case basic:
Howard Hinnant126da6a2010-07-27 22:20:32 +00003147 __first = __parse_basic_reg_exp(__first, __last);
Howard Hinnant6c891682010-06-24 21:28:00 +00003148 break;
3149 case extended:
Howard Hinnant6c891682010-06-24 21:28:00 +00003150 case awk:
Howard Hinnant70b3e192010-07-28 17:35:27 +00003151 __first = __parse_extended_reg_exp(__first, __last);
Howard Hinnant6c891682010-06-24 21:28:00 +00003152 break;
3153 case grep:
Howard Hinnant126da6a2010-07-27 22:20:32 +00003154 __first = __parse_grep(__first, __last);
Howard Hinnant6c891682010-06-24 21:28:00 +00003155 break;
3156 case egrep:
Howard Hinnant126da6a2010-07-27 22:20:32 +00003157 __first = __parse_egrep(__first, __last);
Howard Hinnant6c891682010-06-24 21:28:00 +00003158 break;
3159 default:
Marshall Clowc8ccc292015-07-28 13:30:47 +00003160 __throw_regex_error<regex_constants::__re_err_grammar>();
Howard Hinnant6c891682010-06-24 21:28:00 +00003161 }
Howard Hinnant126da6a2010-07-27 22:20:32 +00003162 return __first;
Howard Hinnant6c891682010-06-24 21:28:00 +00003163}
3164
3165template <class _CharT, class _Traits>
3166template <class _ForwardIterator>
3167_ForwardIterator
3168basic_regex<_CharT, _Traits>::__parse_basic_reg_exp(_ForwardIterator __first,
3169 _ForwardIterator __last)
3170{
3171 if (__first != __last)
3172 {
3173 if (*__first == '^')
3174 {
3175 __push_l_anchor();
3176 ++__first;
3177 }
3178 if (__first != __last)
3179 {
3180 __first = __parse_RE_expression(__first, __last);
3181 if (__first != __last)
3182 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003183 _ForwardIterator __temp = _VSTD::next(__first);
Howard Hinnant6c891682010-06-24 21:28:00 +00003184 if (__temp == __last && *__first == '$')
3185 {
3186 __push_r_anchor();
3187 ++__first;
3188 }
3189 }
3190 }
3191 if (__first != __last)
Marshall Clowc8ccc292015-07-28 13:30:47 +00003192 __throw_regex_error<regex_constants::__re_err_empty>();
Howard Hinnant6c891682010-06-24 21:28:00 +00003193 }
3194 return __first;
3195}
3196
3197template <class _CharT, class _Traits>
3198template <class _ForwardIterator>
3199_ForwardIterator
Howard Hinnant67ad2132010-06-29 18:37:43 +00003200basic_regex<_CharT, _Traits>::__parse_extended_reg_exp(_ForwardIterator __first,
3201 _ForwardIterator __last)
3202{
Howard Hinnant16d65422010-07-16 19:08:36 +00003203 __owns_one_state<_CharT>* __sa = __end_;
3204 _ForwardIterator __temp = __parse_ERE_branch(__first, __last);
3205 if (__temp == __first)
Marshall Clowc8ccc292015-07-28 13:30:47 +00003206 __throw_regex_error<regex_constants::__re_err_empty>();
Howard Hinnant16d65422010-07-16 19:08:36 +00003207 __first = __temp;
3208 while (__first != __last && *__first == '|')
Howard Hinnant67ad2132010-06-29 18:37:43 +00003209 {
Howard Hinnant16d65422010-07-16 19:08:36 +00003210 __owns_one_state<_CharT>* __sb = __end_;
3211 __temp = __parse_ERE_branch(++__first, __last);
Howard Hinnant67ad2132010-06-29 18:37:43 +00003212 if (__temp == __first)
Marshall Clowc8ccc292015-07-28 13:30:47 +00003213 __throw_regex_error<regex_constants::__re_err_empty>();
Howard Hinnant16d65422010-07-16 19:08:36 +00003214 __push_alternation(__sa, __sb);
Howard Hinnant67ad2132010-06-29 18:37:43 +00003215 __first = __temp;
Howard Hinnant67ad2132010-06-29 18:37:43 +00003216 }
3217 return __first;
3218}
3219
3220template <class _CharT, class _Traits>
3221template <class _ForwardIterator>
3222_ForwardIterator
3223basic_regex<_CharT, _Traits>::__parse_ERE_branch(_ForwardIterator __first,
3224 _ForwardIterator __last)
3225{
3226 _ForwardIterator __temp = __parse_ERE_expression(__first, __last);
3227 if (__temp == __first)
Marshall Clowc8ccc292015-07-28 13:30:47 +00003228 __throw_regex_error<regex_constants::__re_err_empty>();
Howard Hinnant67ad2132010-06-29 18:37:43 +00003229 do
3230 {
3231 __first = __temp;
3232 __temp = __parse_ERE_expression(__first, __last);
3233 } while (__temp != __first);
3234 return __first;
3235}
3236
3237template <class _CharT, class _Traits>
3238template <class _ForwardIterator>
3239_ForwardIterator
3240basic_regex<_CharT, _Traits>::__parse_ERE_expression(_ForwardIterator __first,
3241 _ForwardIterator __last)
3242{
Howard Hinnant16d65422010-07-16 19:08:36 +00003243 __owns_one_state<_CharT>* __e = __end_;
3244 unsigned __mexp_begin = __marked_count_;
Howard Hinnant67ad2132010-06-29 18:37:43 +00003245 _ForwardIterator __temp = __parse_one_char_or_coll_elem_ERE(__first, __last);
3246 if (__temp == __first && __temp != __last)
3247 {
3248 switch (*__temp)
3249 {
3250 case '^':
3251 __push_l_anchor();
3252 ++__temp;
3253 break;
3254 case '$':
3255 __push_r_anchor();
3256 ++__temp;
3257 break;
3258 case '(':
Howard Hinnant93ef6552010-06-30 20:30:19 +00003259 __push_begin_marked_subexpression();
3260 unsigned __temp_count = __marked_count_;
Howard Hinnant67ad2132010-06-29 18:37:43 +00003261 ++__open_count_;
3262 __temp = __parse_extended_reg_exp(++__temp, __last);
3263 if (__temp == __last || *__temp != ')')
Marshall Clowc8ccc292015-07-28 13:30:47 +00003264 __throw_regex_error<regex_constants::error_paren>();
Howard Hinnant93ef6552010-06-30 20:30:19 +00003265 __push_end_marked_subexpression(__temp_count);
Howard Hinnant67ad2132010-06-29 18:37:43 +00003266 --__open_count_;
3267 ++__temp;
3268 break;
3269 }
3270 }
3271 if (__temp != __first)
Howard Hinnant16d65422010-07-16 19:08:36 +00003272 __temp = __parse_ERE_dupl_symbol(__temp, __last, __e, __mexp_begin+1,
3273 __marked_count_+1);
Howard Hinnant67ad2132010-06-29 18:37:43 +00003274 __first = __temp;
3275 return __first;
3276}
3277
3278template <class _CharT, class _Traits>
3279template <class _ForwardIterator>
3280_ForwardIterator
Howard Hinnant6c891682010-06-24 21:28:00 +00003281basic_regex<_CharT, _Traits>::__parse_RE_expression(_ForwardIterator __first,
3282 _ForwardIterator __last)
3283{
3284 while (true)
3285 {
3286 _ForwardIterator __temp = __parse_simple_RE(__first, __last);
3287 if (__temp == __first)
3288 break;
3289 __first = __temp;
3290 }
3291 return __first;
3292}
3293
3294template <class _CharT, class _Traits>
3295template <class _ForwardIterator>
3296_ForwardIterator
3297basic_regex<_CharT, _Traits>::__parse_simple_RE(_ForwardIterator __first,
3298 _ForwardIterator __last)
3299{
3300 if (__first != __last)
3301 {
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00003302 __owns_one_state<_CharT>* __e = __end_;
Howard Hinnant5bf96132010-07-08 17:43:58 +00003303 unsigned __mexp_begin = __marked_count_;
Howard Hinnant6c891682010-06-24 21:28:00 +00003304 _ForwardIterator __temp = __parse_nondupl_RE(__first, __last);
3305 if (__temp != __first)
Howard Hinnant5bf96132010-07-08 17:43:58 +00003306 __first = __parse_RE_dupl_symbol(__temp, __last, __e,
3307 __mexp_begin+1, __marked_count_+1);
Howard Hinnant6c891682010-06-24 21:28:00 +00003308 }
3309 return __first;
3310}
3311
3312template <class _CharT, class _Traits>
3313template <class _ForwardIterator>
3314_ForwardIterator
3315basic_regex<_CharT, _Traits>::__parse_nondupl_RE(_ForwardIterator __first,
3316 _ForwardIterator __last)
3317{
3318 _ForwardIterator __temp = __first;
3319 __first = __parse_one_char_or_coll_elem_RE(__first, __last);
3320 if (__temp == __first)
3321 {
3322 __temp = __parse_Back_open_paren(__first, __last);
3323 if (__temp != __first)
3324 {
Howard Hinnant93ef6552010-06-30 20:30:19 +00003325 __push_begin_marked_subexpression();
3326 unsigned __temp_count = __marked_count_;
Howard Hinnant6c891682010-06-24 21:28:00 +00003327 __first = __parse_RE_expression(__temp, __last);
3328 __temp = __parse_Back_close_paren(__first, __last);
3329 if (__temp == __first)
Marshall Clowc8ccc292015-07-28 13:30:47 +00003330 __throw_regex_error<regex_constants::error_paren>();
Howard Hinnant93ef6552010-06-30 20:30:19 +00003331 __push_end_marked_subexpression(__temp_count);
Howard Hinnant6c891682010-06-24 21:28:00 +00003332 __first = __temp;
Howard Hinnant6c891682010-06-24 21:28:00 +00003333 }
3334 else
3335 __first = __parse_BACKREF(__first, __last);
3336 }
3337 return __first;
3338}
3339
3340template <class _CharT, class _Traits>
3341template <class _ForwardIterator>
3342_ForwardIterator
3343basic_regex<_CharT, _Traits>::__parse_one_char_or_coll_elem_RE(
3344 _ForwardIterator __first,
3345 _ForwardIterator __last)
3346{
Howard Hinnant67ad2132010-06-29 18:37:43 +00003347 _ForwardIterator __temp = __parse_ORD_CHAR(__first, __last);
Howard Hinnant6c891682010-06-24 21:28:00 +00003348 if (__temp == __first)
3349 {
Howard Hinnant67ad2132010-06-29 18:37:43 +00003350 __temp = __parse_QUOTED_CHAR(__first, __last);
Howard Hinnant6c891682010-06-24 21:28:00 +00003351 if (__temp == __first)
3352 {
Howard Hinnant67ad2132010-06-29 18:37:43 +00003353 if (__temp != __last && *__temp == '.')
Howard Hinnant6c891682010-06-24 21:28:00 +00003354 {
3355 __push_match_any();
Howard Hinnant67ad2132010-06-29 18:37:43 +00003356 ++__temp;
Howard Hinnant6c891682010-06-24 21:28:00 +00003357 }
3358 else
Howard Hinnant67ad2132010-06-29 18:37:43 +00003359 __temp = __parse_bracket_expression(__first, __last);
Howard Hinnant6c891682010-06-24 21:28:00 +00003360 }
3361 }
Howard Hinnant67ad2132010-06-29 18:37:43 +00003362 __first = __temp;
3363 return __first;
3364}
3365
3366template <class _CharT, class _Traits>
3367template <class _ForwardIterator>
3368_ForwardIterator
3369basic_regex<_CharT, _Traits>::__parse_one_char_or_coll_elem_ERE(
3370 _ForwardIterator __first,
3371 _ForwardIterator __last)
3372{
3373 _ForwardIterator __temp = __parse_ORD_CHAR_ERE(__first, __last);
3374 if (__temp == __first)
3375 {
3376 __temp = __parse_QUOTED_CHAR_ERE(__first, __last);
3377 if (__temp == __first)
3378 {
3379 if (__temp != __last && *__temp == '.')
3380 {
3381 __push_match_any();
3382 ++__temp;
3383 }
3384 else
3385 __temp = __parse_bracket_expression(__first, __last);
3386 }
3387 }
3388 __first = __temp;
Howard Hinnant6c891682010-06-24 21:28:00 +00003389 return __first;
3390}
3391
3392template <class _CharT, class _Traits>
3393template <class _ForwardIterator>
3394_ForwardIterator
3395basic_regex<_CharT, _Traits>::__parse_Back_open_paren(_ForwardIterator __first,
3396 _ForwardIterator __last)
3397{
3398 if (__first != __last)
3399 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003400 _ForwardIterator __temp = _VSTD::next(__first);
Howard Hinnant6c891682010-06-24 21:28:00 +00003401 if (__temp != __last)
3402 {
3403 if (*__first == '\\' && *__temp == '(')
3404 __first = ++__temp;
3405 }
3406 }
3407 return __first;
3408}
3409
3410template <class _CharT, class _Traits>
3411template <class _ForwardIterator>
3412_ForwardIterator
3413basic_regex<_CharT, _Traits>::__parse_Back_close_paren(_ForwardIterator __first,
3414 _ForwardIterator __last)
3415{
3416 if (__first != __last)
3417 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003418 _ForwardIterator __temp = _VSTD::next(__first);
Howard Hinnant6c891682010-06-24 21:28:00 +00003419 if (__temp != __last)
3420 {
3421 if (*__first == '\\' && *__temp == ')')
3422 __first = ++__temp;
3423 }
3424 }
3425 return __first;
3426}
3427
3428template <class _CharT, class _Traits>
3429template <class _ForwardIterator>
3430_ForwardIterator
3431basic_regex<_CharT, _Traits>::__parse_Back_open_brace(_ForwardIterator __first,
3432 _ForwardIterator __last)
3433{
3434 if (__first != __last)
3435 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003436 _ForwardIterator __temp = _VSTD::next(__first);
Howard Hinnant6c891682010-06-24 21:28:00 +00003437 if (__temp != __last)
3438 {
3439 if (*__first == '\\' && *__temp == '{')
3440 __first = ++__temp;
3441 }
3442 }
3443 return __first;
3444}
3445
3446template <class _CharT, class _Traits>
3447template <class _ForwardIterator>
3448_ForwardIterator
3449basic_regex<_CharT, _Traits>::__parse_Back_close_brace(_ForwardIterator __first,
3450 _ForwardIterator __last)
3451{
3452 if (__first != __last)
3453 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003454 _ForwardIterator __temp = _VSTD::next(__first);
Howard Hinnant6c891682010-06-24 21:28:00 +00003455 if (__temp != __last)
3456 {
3457 if (*__first == '\\' && *__temp == '}')
3458 __first = ++__temp;
3459 }
3460 }
3461 return __first;
3462}
3463
3464template <class _CharT, class _Traits>
3465template <class _ForwardIterator>
3466_ForwardIterator
3467basic_regex<_CharT, _Traits>::__parse_BACKREF(_ForwardIterator __first,
3468 _ForwardIterator __last)
3469{
3470 if (__first != __last)
3471 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003472 _ForwardIterator __temp = _VSTD::next(__first);
Louis Dionnef16eb592020-02-19 15:56:15 -05003473 if (__temp != __last && *__first == '\\' && __test_back_ref(*__temp))
3474 __first = ++__temp;
Howard Hinnant6c891682010-06-24 21:28:00 +00003475 }
3476 return __first;
3477}
3478
3479template <class _CharT, class _Traits>
3480template <class _ForwardIterator>
3481_ForwardIterator
3482basic_regex<_CharT, _Traits>::__parse_ORD_CHAR(_ForwardIterator __first,
3483 _ForwardIterator __last)
3484{
3485 if (__first != __last)
3486 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003487 _ForwardIterator __temp = _VSTD::next(__first);
Howard Hinnant6c891682010-06-24 21:28:00 +00003488 if (__temp == __last && *__first == '$')
3489 return __first;
3490 // Not called inside a bracket
3491 if (*__first == '.' || *__first == '\\' || *__first == '[')
3492 return __first;
Howard Hinnant89a40572010-06-25 20:56:08 +00003493 __push_char(*__first);
Howard Hinnant6c891682010-06-24 21:28:00 +00003494 ++__first;
3495 }
3496 return __first;
3497}
3498
3499template <class _CharT, class _Traits>
3500template <class _ForwardIterator>
3501_ForwardIterator
Howard Hinnant67ad2132010-06-29 18:37:43 +00003502basic_regex<_CharT, _Traits>::__parse_ORD_CHAR_ERE(_ForwardIterator __first,
3503 _ForwardIterator __last)
3504{
3505 if (__first != __last)
3506 {
3507 switch (*__first)
3508 {
3509 case '^':
3510 case '.':
3511 case '[':
3512 case '$':
3513 case '(':
3514 case '|':
3515 case '*':
3516 case '+':
3517 case '?':
3518 case '{':
3519 case '\\':
3520 break;
3521 case ')':
3522 if (__open_count_ == 0)
3523 {
3524 __push_char(*__first);
3525 ++__first;
3526 }
3527 break;
3528 default:
3529 __push_char(*__first);
3530 ++__first;
3531 break;
3532 }
3533 }
3534 return __first;
3535}
3536
3537template <class _CharT, class _Traits>
3538template <class _ForwardIterator>
3539_ForwardIterator
Howard Hinnant6c891682010-06-24 21:28:00 +00003540basic_regex<_CharT, _Traits>::__parse_QUOTED_CHAR(_ForwardIterator __first,
3541 _ForwardIterator __last)
3542{
3543 if (__first != __last)
3544 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003545 _ForwardIterator __temp = _VSTD::next(__first);
Howard Hinnant6c891682010-06-24 21:28:00 +00003546 if (__temp != __last)
3547 {
3548 if (*__first == '\\')
3549 {
3550 switch (*__temp)
3551 {
3552 case '^':
3553 case '.':
3554 case '*':
3555 case '[':
3556 case '$':
3557 case '\\':
Howard Hinnant89a40572010-06-25 20:56:08 +00003558 __push_char(*__temp);
Howard Hinnant6c891682010-06-24 21:28:00 +00003559 __first = ++__temp;
3560 break;
3561 }
3562 }
3563 }
3564 }
3565 return __first;
3566}
3567
3568template <class _CharT, class _Traits>
3569template <class _ForwardIterator>
3570_ForwardIterator
Howard Hinnant67ad2132010-06-29 18:37:43 +00003571basic_regex<_CharT, _Traits>::__parse_QUOTED_CHAR_ERE(_ForwardIterator __first,
3572 _ForwardIterator __last)
3573{
3574 if (__first != __last)
3575 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003576 _ForwardIterator __temp = _VSTD::next(__first);
Howard Hinnant67ad2132010-06-29 18:37:43 +00003577 if (__temp != __last)
3578 {
3579 if (*__first == '\\')
3580 {
3581 switch (*__temp)
3582 {
3583 case '^':
3584 case '.':
3585 case '*':
3586 case '[':
3587 case '$':
3588 case '\\':
3589 case '(':
3590 case ')':
3591 case '|':
3592 case '+':
3593 case '?':
3594 case '{':
Howard Hinnant73072eb2013-06-28 20:31:05 +00003595 case '}':
Howard Hinnant67ad2132010-06-29 18:37:43 +00003596 __push_char(*__temp);
3597 __first = ++__temp;
3598 break;
Howard Hinnant70b3e192010-07-28 17:35:27 +00003599 default:
Marshall Clow88a30872019-03-28 17:30:23 +00003600 if (__get_grammar(__flags_) == awk)
Howard Hinnant70b3e192010-07-28 17:35:27 +00003601 __first = __parse_awk_escape(++__first, __last);
Louis Dionnef16eb592020-02-19 15:56:15 -05003602 else if(__test_back_ref(*__temp))
3603 __first = ++__temp;
Howard Hinnant70b3e192010-07-28 17:35:27 +00003604 break;
Howard Hinnant67ad2132010-06-29 18:37:43 +00003605 }
3606 }
3607 }
3608 }
3609 return __first;
3610}
3611
3612template <class _CharT, class _Traits>
3613template <class _ForwardIterator>
3614_ForwardIterator
Howard Hinnant6c891682010-06-24 21:28:00 +00003615basic_regex<_CharT, _Traits>::__parse_RE_dupl_symbol(_ForwardIterator __first,
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00003616 _ForwardIterator __last,
Howard Hinnant5bf96132010-07-08 17:43:58 +00003617 __owns_one_state<_CharT>* __s,
3618 unsigned __mexp_begin,
3619 unsigned __mexp_end)
Howard Hinnant6c891682010-06-24 21:28:00 +00003620{
3621 if (__first != __last)
3622 {
Howard Hinnant89a40572010-06-25 20:56:08 +00003623 if (*__first == '*')
Howard Hinnant6c891682010-06-24 21:28:00 +00003624 {
Howard Hinnant5bf96132010-07-08 17:43:58 +00003625 __push_greedy_inf_repeat(0, __s, __mexp_begin, __mexp_end);
Howard Hinnant6c891682010-06-24 21:28:00 +00003626 ++__first;
3627 }
3628 else
3629 {
3630 _ForwardIterator __temp = __parse_Back_open_brace(__first, __last);
3631 if (__temp != __first)
3632 {
3633 int __min = 0;
3634 __first = __temp;
3635 __temp = __parse_DUP_COUNT(__first, __last, __min);
3636 if (__temp == __first)
Marshall Clowc8ccc292015-07-28 13:30:47 +00003637 __throw_regex_error<regex_constants::error_badbrace>();
Howard Hinnant6c891682010-06-24 21:28:00 +00003638 __first = __temp;
3639 if (__first == __last)
Marshall Clowc8ccc292015-07-28 13:30:47 +00003640 __throw_regex_error<regex_constants::error_brace>();
Howard Hinnant6c891682010-06-24 21:28:00 +00003641 if (*__first != ',')
3642 {
3643 __temp = __parse_Back_close_brace(__first, __last);
3644 if (__temp == __first)
Marshall Clowc8ccc292015-07-28 13:30:47 +00003645 __throw_regex_error<regex_constants::error_brace>();
Howard Hinnant2a315e32010-07-12 18:16:05 +00003646 __push_loop(__min, __min, __s, __mexp_begin, __mexp_end,
3647 true);
Howard Hinnant6c891682010-06-24 21:28:00 +00003648 __first = __temp;
3649 }
3650 else
3651 {
3652 ++__first; // consume ','
3653 int __max = -1;
3654 __first = __parse_DUP_COUNT(__first, __last, __max);
3655 __temp = __parse_Back_close_brace(__first, __last);
3656 if (__temp == __first)
Marshall Clowc8ccc292015-07-28 13:30:47 +00003657 __throw_regex_error<regex_constants::error_brace>();
Howard Hinnant6c891682010-06-24 21:28:00 +00003658 if (__max == -1)
Howard Hinnant16d65422010-07-16 19:08:36 +00003659 __push_greedy_inf_repeat(__min, __s, __mexp_begin, __mexp_end);
Howard Hinnant6c891682010-06-24 21:28:00 +00003660 else
3661 {
3662 if (__max < __min)
Marshall Clowc8ccc292015-07-28 13:30:47 +00003663 __throw_regex_error<regex_constants::error_badbrace>();
Howard Hinnant2a315e32010-07-12 18:16:05 +00003664 __push_loop(__min, __max, __s, __mexp_begin, __mexp_end,
3665 true);
Howard Hinnant6c891682010-06-24 21:28:00 +00003666 }
3667 __first = __temp;
3668 }
3669 }
3670 }
3671 }
3672 return __first;
3673}
3674
Howard Hinnant89a40572010-06-25 20:56:08 +00003675template <class _CharT, class _Traits>
3676template <class _ForwardIterator>
3677_ForwardIterator
Howard Hinnant67ad2132010-06-29 18:37:43 +00003678basic_regex<_CharT, _Traits>::__parse_ERE_dupl_symbol(_ForwardIterator __first,
Howard Hinnant16d65422010-07-16 19:08:36 +00003679 _ForwardIterator __last,
3680 __owns_one_state<_CharT>* __s,
3681 unsigned __mexp_begin,
3682 unsigned __mexp_end)
Howard Hinnant67ad2132010-06-29 18:37:43 +00003683{
3684 if (__first != __last)
3685 {
Marshall Clow88a30872019-03-28 17:30:23 +00003686 unsigned __grammar = __get_grammar(__flags_);
Howard Hinnant67ad2132010-06-29 18:37:43 +00003687 switch (*__first)
3688 {
3689 case '*':
Howard Hinnant67ad2132010-06-29 18:37:43 +00003690 ++__first;
Howard Hinnant446e9c62010-07-29 00:36:00 +00003691 if (__grammar == ECMAScript && __first != __last && *__first == '?')
Howard Hinnant70d27852010-07-27 01:25:38 +00003692 {
3693 ++__first;
3694 __push_nongreedy_inf_repeat(0, __s, __mexp_begin, __mexp_end);
3695 }
3696 else
3697 __push_greedy_inf_repeat(0, __s, __mexp_begin, __mexp_end);
Howard Hinnant67ad2132010-06-29 18:37:43 +00003698 break;
3699 case '+':
Howard Hinnant67ad2132010-06-29 18:37:43 +00003700 ++__first;
Howard Hinnant446e9c62010-07-29 00:36:00 +00003701 if (__grammar == ECMAScript && __first != __last && *__first == '?')
Howard Hinnant70d27852010-07-27 01:25:38 +00003702 {
3703 ++__first;
3704 __push_nongreedy_inf_repeat(1, __s, __mexp_begin, __mexp_end);
3705 }
3706 else
3707 __push_greedy_inf_repeat(1, __s, __mexp_begin, __mexp_end);
Howard Hinnant67ad2132010-06-29 18:37:43 +00003708 break;
3709 case '?':
Howard Hinnant67ad2132010-06-29 18:37:43 +00003710 ++__first;
Howard Hinnant446e9c62010-07-29 00:36:00 +00003711 if (__grammar == ECMAScript && __first != __last && *__first == '?')
Howard Hinnant70d27852010-07-27 01:25:38 +00003712 {
3713 ++__first;
3714 __push_loop(0, 1, __s, __mexp_begin, __mexp_end, false);
3715 }
3716 else
3717 __push_loop(0, 1, __s, __mexp_begin, __mexp_end);
Howard Hinnant67ad2132010-06-29 18:37:43 +00003718 break;
3719 case '{':
3720 {
3721 int __min;
Howard Hinnant16d65422010-07-16 19:08:36 +00003722 _ForwardIterator __temp = __parse_DUP_COUNT(++__first, __last, __min);
Howard Hinnant67ad2132010-06-29 18:37:43 +00003723 if (__temp == __first)
Marshall Clowc8ccc292015-07-28 13:30:47 +00003724 __throw_regex_error<regex_constants::error_badbrace>();
Howard Hinnant67ad2132010-06-29 18:37:43 +00003725 __first = __temp;
3726 if (__first == __last)
Marshall Clowc8ccc292015-07-28 13:30:47 +00003727 __throw_regex_error<regex_constants::error_brace>();
Howard Hinnant67ad2132010-06-29 18:37:43 +00003728 switch (*__first)
3729 {
3730 case '}':
Howard Hinnant67ad2132010-06-29 18:37:43 +00003731 ++__first;
Howard Hinnant446e9c62010-07-29 00:36:00 +00003732 if (__grammar == ECMAScript && __first != __last && *__first == '?')
Howard Hinnant70d27852010-07-27 01:25:38 +00003733 {
3734 ++__first;
3735 __push_loop(__min, __min, __s, __mexp_begin, __mexp_end, false);
3736 }
3737 else
3738 __push_loop(__min, __min, __s, __mexp_begin, __mexp_end);
Howard Hinnant67ad2132010-06-29 18:37:43 +00003739 break;
3740 case ',':
Howard Hinnant72f73582010-08-11 17:04:31 +00003741 ++__first;
Howard Hinnant72f73582010-08-11 17:04:31 +00003742 if (__first == __last)
Marshall Clowc8ccc292015-07-28 13:30:47 +00003743 __throw_regex_error<regex_constants::error_badbrace>();
Howard Hinnant67ad2132010-06-29 18:37:43 +00003744 if (*__first == '}')
3745 {
Howard Hinnant67ad2132010-06-29 18:37:43 +00003746 ++__first;
Howard Hinnant446e9c62010-07-29 00:36:00 +00003747 if (__grammar == ECMAScript && __first != __last && *__first == '?')
Howard Hinnant70d27852010-07-27 01:25:38 +00003748 {
3749 ++__first;
3750 __push_nongreedy_inf_repeat(__min, __s, __mexp_begin, __mexp_end);
3751 }
3752 else
3753 __push_greedy_inf_repeat(__min, __s, __mexp_begin, __mexp_end);
Howard Hinnant67ad2132010-06-29 18:37:43 +00003754 }
3755 else
3756 {
Howard Hinnant16d65422010-07-16 19:08:36 +00003757 int __max = -1;
Howard Hinnant67ad2132010-06-29 18:37:43 +00003758 __temp = __parse_DUP_COUNT(__first, __last, __max);
3759 if (__temp == __first)
Marshall Clowc8ccc292015-07-28 13:30:47 +00003760 __throw_regex_error<regex_constants::error_brace>();
Howard Hinnant67ad2132010-06-29 18:37:43 +00003761 __first = __temp;
3762 if (__first == __last || *__first != '}')
Marshall Clowc8ccc292015-07-28 13:30:47 +00003763 __throw_regex_error<regex_constants::error_brace>();
Howard Hinnant67ad2132010-06-29 18:37:43 +00003764 ++__first;
3765 if (__max < __min)
Marshall Clowc8ccc292015-07-28 13:30:47 +00003766 __throw_regex_error<regex_constants::error_badbrace>();
Howard Hinnant446e9c62010-07-29 00:36:00 +00003767 if (__grammar == ECMAScript && __first != __last && *__first == '?')
Howard Hinnant70d27852010-07-27 01:25:38 +00003768 {
3769 ++__first;
3770 __push_loop(__min, __max, __s, __mexp_begin, __mexp_end, false);
3771 }
3772 else
3773 __push_loop(__min, __max, __s, __mexp_begin, __mexp_end);
Howard Hinnant67ad2132010-06-29 18:37:43 +00003774 }
Howard Hinnant16d65422010-07-16 19:08:36 +00003775 break;
Howard Hinnant67ad2132010-06-29 18:37:43 +00003776 default:
Marshall Clowc8ccc292015-07-28 13:30:47 +00003777 __throw_regex_error<regex_constants::error_badbrace>();
Howard Hinnant67ad2132010-06-29 18:37:43 +00003778 }
3779 }
3780 break;
3781 }
3782 }
3783 return __first;
3784}
3785
3786template <class _CharT, class _Traits>
3787template <class _ForwardIterator>
3788_ForwardIterator
Howard Hinnant89a40572010-06-25 20:56:08 +00003789basic_regex<_CharT, _Traits>::__parse_bracket_expression(_ForwardIterator __first,
3790 _ForwardIterator __last)
3791{
3792 if (__first != __last && *__first == '[')
3793 {
Howard Hinnant72f73582010-08-11 17:04:31 +00003794 ++__first;
Howard Hinnant72f73582010-08-11 17:04:31 +00003795 if (__first == __last)
Marshall Clowc8ccc292015-07-28 13:30:47 +00003796 __throw_regex_error<regex_constants::error_brack>();
Howard Hinnant3034c902010-07-13 21:48:06 +00003797 bool __negate = false;
Howard Hinnant89a40572010-06-25 20:56:08 +00003798 if (*__first == '^')
3799 {
3800 ++__first;
Howard Hinnant3034c902010-07-13 21:48:06 +00003801 __negate = true;
Howard Hinnant89a40572010-06-25 20:56:08 +00003802 }
Howard Hinnant3034c902010-07-13 21:48:06 +00003803 __bracket_expression<_CharT, _Traits>* __ml = __start_matching_list(__negate);
3804 // __ml owned by *this
Howard Hinnant89a40572010-06-25 20:56:08 +00003805 if (__first == __last)
Marshall Clowc8ccc292015-07-28 13:30:47 +00003806 __throw_regex_error<regex_constants::error_brack>();
Marshall Clow88a30872019-03-28 17:30:23 +00003807 if (__get_grammar(__flags_) != ECMAScript && *__first == ']')
Howard Hinnant89a40572010-06-25 20:56:08 +00003808 {
Howard Hinnant3034c902010-07-13 21:48:06 +00003809 __ml->__add_char(']');
Howard Hinnant89a40572010-06-25 20:56:08 +00003810 ++__first;
3811 }
Howard Hinnant3034c902010-07-13 21:48:06 +00003812 __first = __parse_follow_list(__first, __last, __ml);
Howard Hinnant89a40572010-06-25 20:56:08 +00003813 if (__first == __last)
Marshall Clowc8ccc292015-07-28 13:30:47 +00003814 __throw_regex_error<regex_constants::error_brack>();
Howard Hinnant89a40572010-06-25 20:56:08 +00003815 if (*__first == '-')
3816 {
Howard Hinnant3034c902010-07-13 21:48:06 +00003817 __ml->__add_char('-');
Howard Hinnant89a40572010-06-25 20:56:08 +00003818 ++__first;
3819 }
3820 if (__first == __last || *__first != ']')
Marshall Clowc8ccc292015-07-28 13:30:47 +00003821 __throw_regex_error<regex_constants::error_brack>();
Howard Hinnant89a40572010-06-25 20:56:08 +00003822 ++__first;
3823 }
3824 return __first;
3825}
3826
3827template <class _CharT, class _Traits>
3828template <class _ForwardIterator>
3829_ForwardIterator
3830basic_regex<_CharT, _Traits>::__parse_follow_list(_ForwardIterator __first,
Howard Hinnant3034c902010-07-13 21:48:06 +00003831 _ForwardIterator __last,
3832 __bracket_expression<_CharT, _Traits>* __ml)
Howard Hinnant89a40572010-06-25 20:56:08 +00003833{
3834 if (__first != __last)
3835 {
3836 while (true)
3837 {
Howard Hinnant3034c902010-07-13 21:48:06 +00003838 _ForwardIterator __temp = __parse_expression_term(__first, __last,
3839 __ml);
Howard Hinnant89a40572010-06-25 20:56:08 +00003840 if (__temp == __first)
3841 break;
3842 __first = __temp;
3843 }
3844 }
3845 return __first;
3846}
3847
3848template <class _CharT, class _Traits>
3849template <class _ForwardIterator>
3850_ForwardIterator
3851basic_regex<_CharT, _Traits>::__parse_expression_term(_ForwardIterator __first,
Howard Hinnant3034c902010-07-13 21:48:06 +00003852 _ForwardIterator __last,
3853 __bracket_expression<_CharT, _Traits>* __ml)
Howard Hinnant89a40572010-06-25 20:56:08 +00003854{
3855 if (__first != __last && *__first != ']')
3856 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003857 _ForwardIterator __temp = _VSTD::next(__first);
Howard Hinnant3034c902010-07-13 21:48:06 +00003858 basic_string<_CharT> __start_range;
Howard Hinnant89a40572010-06-25 20:56:08 +00003859 if (__temp != __last && *__first == '[')
3860 {
3861 if (*__temp == '=')
Howard Hinnant3034c902010-07-13 21:48:06 +00003862 return __parse_equivalence_class(++__temp, __last, __ml);
Howard Hinnant89a40572010-06-25 20:56:08 +00003863 else if (*__temp == ':')
Howard Hinnant3034c902010-07-13 21:48:06 +00003864 return __parse_character_class(++__temp, __last, __ml);
Howard Hinnant89a40572010-06-25 20:56:08 +00003865 else if (*__temp == '.')
Howard Hinnant3034c902010-07-13 21:48:06 +00003866 __first = __parse_collating_symbol(++__temp, __last, __start_range);
Howard Hinnant89a40572010-06-25 20:56:08 +00003867 }
Marshall Clow88a30872019-03-28 17:30:23 +00003868 unsigned __grammar = __get_grammar(__flags_);
Howard Hinnant70b3e192010-07-28 17:35:27 +00003869 if (__start_range.empty())
Howard Hinnant89a40572010-06-25 20:56:08 +00003870 {
Howard Hinnant70b3e192010-07-28 17:35:27 +00003871 if ((__grammar == ECMAScript || __grammar == awk) && *__first == '\\')
3872 {
3873 if (__grammar == ECMAScript)
3874 __first = __parse_class_escape(++__first, __last, __start_range, __ml);
3875 else
3876 __first = __parse_awk_escape(++__first, __last, &__start_range);
3877 }
3878 else
3879 {
3880 __start_range = *__first;
3881 ++__first;
3882 }
Howard Hinnant89a40572010-06-25 20:56:08 +00003883 }
3884 if (__first != __last && *__first != ']')
3885 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003886 __temp = _VSTD::next(__first);
Howard Hinnant89a40572010-06-25 20:56:08 +00003887 if (__temp != __last && *__first == '-' && *__temp != ']')
3888 {
3889 // parse a range
Howard Hinnant3034c902010-07-13 21:48:06 +00003890 basic_string<_CharT> __end_range;
Howard Hinnant89a40572010-06-25 20:56:08 +00003891 __first = __temp;
3892 ++__temp;
3893 if (__temp != __last && *__first == '[' && *__temp == '.')
Howard Hinnant3034c902010-07-13 21:48:06 +00003894 __first = __parse_collating_symbol(++__temp, __last, __end_range);
Howard Hinnant89a40572010-06-25 20:56:08 +00003895 else
3896 {
Howard Hinnant70b3e192010-07-28 17:35:27 +00003897 if ((__grammar == ECMAScript || __grammar == awk) && *__first == '\\')
3898 {
3899 if (__grammar == ECMAScript)
3900 __first = __parse_class_escape(++__first, __last,
3901 __end_range, __ml);
3902 else
3903 __first = __parse_awk_escape(++__first, __last,
3904 &__end_range);
3905 }
3906 else
3907 {
3908 __end_range = *__first;
3909 ++__first;
3910 }
Howard Hinnant89a40572010-06-25 20:56:08 +00003911 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003912 __ml->__add_range(_VSTD::move(__start_range), _VSTD::move(__end_range));
Howard Hinnant89a40572010-06-25 20:56:08 +00003913 }
Howard Hinnant8ea98242013-08-23 17:37:05 +00003914 else if (!__start_range.empty())
Howard Hinnant3034c902010-07-13 21:48:06 +00003915 {
3916 if (__start_range.size() == 1)
3917 __ml->__add_char(__start_range[0]);
3918 else
3919 __ml->__add_digraph(__start_range[0], __start_range[1]);
3920 }
3921 }
Howard Hinnant8ea98242013-08-23 17:37:05 +00003922 else if (!__start_range.empty())
Howard Hinnant3034c902010-07-13 21:48:06 +00003923 {
3924 if (__start_range.size() == 1)
3925 __ml->__add_char(__start_range[0]);
3926 else
3927 __ml->__add_digraph(__start_range[0], __start_range[1]);
Howard Hinnant89a40572010-06-25 20:56:08 +00003928 }
3929 }
3930 return __first;
3931}
3932
3933template <class _CharT, class _Traits>
3934template <class _ForwardIterator>
3935_ForwardIterator
Howard Hinnant70b3e192010-07-28 17:35:27 +00003936basic_regex<_CharT, _Traits>::__parse_class_escape(_ForwardIterator __first,
3937 _ForwardIterator __last,
3938 basic_string<_CharT>& __str,
3939 __bracket_expression<_CharT, _Traits>* __ml)
3940{
3941 if (__first == __last)
Marshall Clowc8ccc292015-07-28 13:30:47 +00003942 __throw_regex_error<regex_constants::error_escape>();
Howard Hinnant70b3e192010-07-28 17:35:27 +00003943 switch (*__first)
3944 {
3945 case 0:
3946 __str = *__first;
3947 return ++__first;
3948 case 'b':
3949 __str = _CharT(8);
3950 return ++__first;
3951 case 'd':
3952 __ml->__add_class(ctype_base::digit);
3953 return ++__first;
3954 case 'D':
3955 __ml->__add_neg_class(ctype_base::digit);
3956 return ++__first;
3957 case 's':
3958 __ml->__add_class(ctype_base::space);
3959 return ++__first;
3960 case 'S':
3961 __ml->__add_neg_class(ctype_base::space);
3962 return ++__first;
3963 case 'w':
3964 __ml->__add_class(ctype_base::alnum);
3965 __ml->__add_char('_');
3966 return ++__first;
3967 case 'W':
3968 __ml->__add_neg_class(ctype_base::alnum);
3969 __ml->__add_neg_char('_');
3970 return ++__first;
3971 }
3972 __first = __parse_character_escape(__first, __last, &__str);
3973 return __first;
3974}
3975
3976template <class _CharT, class _Traits>
3977template <class _ForwardIterator>
3978_ForwardIterator
3979basic_regex<_CharT, _Traits>::__parse_awk_escape(_ForwardIterator __first,
3980 _ForwardIterator __last,
3981 basic_string<_CharT>* __str)
3982{
3983 if (__first == __last)
Marshall Clowc8ccc292015-07-28 13:30:47 +00003984 __throw_regex_error<regex_constants::error_escape>();
Howard Hinnant70b3e192010-07-28 17:35:27 +00003985 switch (*__first)
3986 {
3987 case '\\':
3988 case '"':
3989 case '/':
3990 if (__str)
3991 *__str = *__first;
3992 else
3993 __push_char(*__first);
3994 return ++__first;
3995 case 'a':
3996 if (__str)
3997 *__str = _CharT(7);
3998 else
3999 __push_char(_CharT(7));
4000 return ++__first;
4001 case 'b':
4002 if (__str)
4003 *__str = _CharT(8);
4004 else
4005 __push_char(_CharT(8));
4006 return ++__first;
4007 case 'f':
4008 if (__str)
4009 *__str = _CharT(0xC);
4010 else
4011 __push_char(_CharT(0xC));
4012 return ++__first;
4013 case 'n':
4014 if (__str)
4015 *__str = _CharT(0xA);
4016 else
4017 __push_char(_CharT(0xA));
4018 return ++__first;
4019 case 'r':
4020 if (__str)
4021 *__str = _CharT(0xD);
4022 else
4023 __push_char(_CharT(0xD));
4024 return ++__first;
4025 case 't':
4026 if (__str)
4027 *__str = _CharT(0x9);
4028 else
4029 __push_char(_CharT(0x9));
4030 return ++__first;
4031 case 'v':
4032 if (__str)
4033 *__str = _CharT(0xB);
4034 else
4035 __push_char(_CharT(0xB));
4036 return ++__first;
4037 }
4038 if ('0' <= *__first && *__first <= '7')
4039 {
4040 unsigned __val = *__first - '0';
4041 if (++__first != __last && ('0' <= *__first && *__first <= '7'))
4042 {
4043 __val = 8 * __val + *__first - '0';
4044 if (++__first != __last && ('0' <= *__first && *__first <= '7'))
Howard Hinnanta3096872013-07-02 17:43:31 +00004045 __val = 8 * __val + *__first++ - '0';
Howard Hinnant70b3e192010-07-28 17:35:27 +00004046 }
4047 if (__str)
4048 *__str = _CharT(__val);
4049 else
4050 __push_char(_CharT(__val));
4051 }
4052 else
Marshall Clowc8ccc292015-07-28 13:30:47 +00004053 __throw_regex_error<regex_constants::error_escape>();
Howard Hinnant70b3e192010-07-28 17:35:27 +00004054 return __first;
4055}
4056
4057template <class _CharT, class _Traits>
4058template <class _ForwardIterator>
4059_ForwardIterator
Howard Hinnant89a40572010-06-25 20:56:08 +00004060basic_regex<_CharT, _Traits>::__parse_equivalence_class(_ForwardIterator __first,
Howard Hinnant3034c902010-07-13 21:48:06 +00004061 _ForwardIterator __last,
4062 __bracket_expression<_CharT, _Traits>* __ml)
Howard Hinnant89a40572010-06-25 20:56:08 +00004063{
4064 // Found [=
4065 // This means =] must exist
4066 value_type _Equal_close[2] = {'=', ']'};
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004067 _ForwardIterator __temp = _VSTD::search(__first, __last, _Equal_close,
Howard Hinnant89a40572010-06-25 20:56:08 +00004068 _Equal_close+2);
4069 if (__temp == __last)
Marshall Clowc8ccc292015-07-28 13:30:47 +00004070 __throw_regex_error<regex_constants::error_brack>();
Howard Hinnant89a40572010-06-25 20:56:08 +00004071 // [__first, __temp) contains all text in [= ... =]
Howard Hinnant89a40572010-06-25 20:56:08 +00004072 string_type __collate_name =
4073 __traits_.lookup_collatename(__first, __temp);
4074 if (__collate_name.empty())
Marshall Clowc8ccc292015-07-28 13:30:47 +00004075 __throw_regex_error<regex_constants::error_collate>();
Howard Hinnant89a40572010-06-25 20:56:08 +00004076 string_type __equiv_name =
4077 __traits_.transform_primary(__collate_name.begin(),
4078 __collate_name.end());
4079 if (!__equiv_name.empty())
Howard Hinnant3034c902010-07-13 21:48:06 +00004080 __ml->__add_equivalence(__equiv_name);
Howard Hinnant89a40572010-06-25 20:56:08 +00004081 else
Howard Hinnant3034c902010-07-13 21:48:06 +00004082 {
4083 switch (__collate_name.size())
4084 {
4085 case 1:
4086 __ml->__add_char(__collate_name[0]);
4087 break;
4088 case 2:
4089 __ml->__add_digraph(__collate_name[0], __collate_name[1]);
4090 break;
4091 default:
Marshall Clowc8ccc292015-07-28 13:30:47 +00004092 __throw_regex_error<regex_constants::error_collate>();
Howard Hinnant3034c902010-07-13 21:48:06 +00004093 }
4094 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004095 __first = _VSTD::next(__temp, 2);
Howard Hinnant89a40572010-06-25 20:56:08 +00004096 return __first;
4097}
4098
4099template <class _CharT, class _Traits>
4100template <class _ForwardIterator>
4101_ForwardIterator
4102basic_regex<_CharT, _Traits>::__parse_character_class(_ForwardIterator __first,
Howard Hinnant3034c902010-07-13 21:48:06 +00004103 _ForwardIterator __last,
4104 __bracket_expression<_CharT, _Traits>* __ml)
Howard Hinnant89a40572010-06-25 20:56:08 +00004105{
4106 // Found [:
4107 // This means :] must exist
4108 value_type _Colon_close[2] = {':', ']'};
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004109 _ForwardIterator __temp = _VSTD::search(__first, __last, _Colon_close,
Howard Hinnant89a40572010-06-25 20:56:08 +00004110 _Colon_close+2);
4111 if (__temp == __last)
Marshall Clowc8ccc292015-07-28 13:30:47 +00004112 __throw_regex_error<regex_constants::error_brack>();
Howard Hinnant89a40572010-06-25 20:56:08 +00004113 // [__first, __temp) contains all text in [: ... :]
4114 typedef typename _Traits::char_class_type char_class_type;
4115 char_class_type __class_type =
4116 __traits_.lookup_classname(__first, __temp, __flags_ & icase);
4117 if (__class_type == 0)
Mikhail Maltsev6ef945b2018-01-24 12:45:18 +00004118 __throw_regex_error<regex_constants::error_ctype>();
Howard Hinnant3034c902010-07-13 21:48:06 +00004119 __ml->__add_class(__class_type);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004120 __first = _VSTD::next(__temp, 2);
Howard Hinnant89a40572010-06-25 20:56:08 +00004121 return __first;
4122}
4123
4124template <class _CharT, class _Traits>
4125template <class _ForwardIterator>
4126_ForwardIterator
4127basic_regex<_CharT, _Traits>::__parse_collating_symbol(_ForwardIterator __first,
Howard Hinnant3034c902010-07-13 21:48:06 +00004128 _ForwardIterator __last,
4129 basic_string<_CharT>& __col_sym)
Howard Hinnant89a40572010-06-25 20:56:08 +00004130{
4131 // Found [.
4132 // This means .] must exist
4133 value_type _Dot_close[2] = {'.', ']'};
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004134 _ForwardIterator __temp = _VSTD::search(__first, __last, _Dot_close,
Howard Hinnant89a40572010-06-25 20:56:08 +00004135 _Dot_close+2);
4136 if (__temp == __last)
Marshall Clowc8ccc292015-07-28 13:30:47 +00004137 __throw_regex_error<regex_constants::error_brack>();
Howard Hinnant89a40572010-06-25 20:56:08 +00004138 // [__first, __temp) contains all text in [. ... .]
Howard Hinnant3034c902010-07-13 21:48:06 +00004139 __col_sym = __traits_.lookup_collatename(__first, __temp);
4140 switch (__col_sym.size())
4141 {
4142 case 1:
4143 case 2:
4144 break;
4145 default:
Marshall Clowc8ccc292015-07-28 13:30:47 +00004146 __throw_regex_error<regex_constants::error_collate>();
Howard Hinnant3034c902010-07-13 21:48:06 +00004147 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004148 __first = _VSTD::next(__temp, 2);
Howard Hinnant89a40572010-06-25 20:56:08 +00004149 return __first;
4150}
4151
4152template <class _CharT, class _Traits>
4153template <class _ForwardIterator>
4154_ForwardIterator
4155basic_regex<_CharT, _Traits>::__parse_DUP_COUNT(_ForwardIterator __first,
4156 _ForwardIterator __last,
4157 int& __c)
4158{
Marshall Clowaa38d972014-01-18 03:40:03 +00004159 if (__first != __last )
Howard Hinnant89a40572010-06-25 20:56:08 +00004160 {
Marshall Clowaa38d972014-01-18 03:40:03 +00004161 int __val = __traits_.value(*__first, 10);
4162 if ( __val != -1 )
Howard Hinnant89a40572010-06-25 20:56:08 +00004163 {
Marshall Clowaa38d972014-01-18 03:40:03 +00004164 __c = __val;
Louis Dionne173f29e2019-05-29 16:01:36 +00004165 for (++__first;
Marshall Clowaa38d972014-01-18 03:40:03 +00004166 __first != __last && ( __val = __traits_.value(*__first, 10)) != -1;
4167 ++__first)
4168 {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05004169 if (__c >= numeric_limits<int>::max() / 10)
Marshall Clow863ae382017-10-19 17:39:16 +00004170 __throw_regex_error<regex_constants::error_badbrace>();
Marshall Clowaa38d972014-01-18 03:40:03 +00004171 __c *= 10;
4172 __c += __val;
4173 }
Howard Hinnant89a40572010-06-25 20:56:08 +00004174 }
4175 }
4176 return __first;
4177}
4178
Howard Hinnant67ad2132010-06-29 18:37:43 +00004179template <class _CharT, class _Traits>
Howard Hinnante1053822010-07-22 17:53:24 +00004180template <class _ForwardIterator>
4181_ForwardIterator
4182basic_regex<_CharT, _Traits>::__parse_ecma_exp(_ForwardIterator __first,
4183 _ForwardIterator __last)
4184{
4185 __owns_one_state<_CharT>* __sa = __end_;
4186 _ForwardIterator __temp = __parse_alternative(__first, __last);
4187 if (__temp == __first)
4188 __push_empty();
4189 __first = __temp;
4190 while (__first != __last && *__first == '|')
4191 {
4192 __owns_one_state<_CharT>* __sb = __end_;
4193 __temp = __parse_alternative(++__first, __last);
4194 if (__temp == __first)
4195 __push_empty();
4196 __push_alternation(__sa, __sb);
4197 __first = __temp;
4198 }
4199 return __first;
4200}
4201
4202template <class _CharT, class _Traits>
4203template <class _ForwardIterator>
4204_ForwardIterator
4205basic_regex<_CharT, _Traits>::__parse_alternative(_ForwardIterator __first,
4206 _ForwardIterator __last)
4207{
4208 while (true)
4209 {
4210 _ForwardIterator __temp = __parse_term(__first, __last);
4211 if (__temp == __first)
4212 break;
4213 __first = __temp;
4214 }
4215 return __first;
4216}
4217
4218template <class _CharT, class _Traits>
4219template <class _ForwardIterator>
4220_ForwardIterator
4221basic_regex<_CharT, _Traits>::__parse_term(_ForwardIterator __first,
4222 _ForwardIterator __last)
4223{
4224 _ForwardIterator __temp = __parse_assertion(__first, __last);
4225 if (__temp == __first)
4226 {
Howard Hinnant70d27852010-07-27 01:25:38 +00004227 __owns_one_state<_CharT>* __e = __end_;
4228 unsigned __mexp_begin = __marked_count_;
Howard Hinnante1053822010-07-22 17:53:24 +00004229 __temp = __parse_atom(__first, __last);
4230 if (__temp != __first)
Howard Hinnant70d27852010-07-27 01:25:38 +00004231 __first = __parse_ERE_dupl_symbol(__temp, __last, __e,
4232 __mexp_begin+1, __marked_count_+1);
Howard Hinnante1053822010-07-22 17:53:24 +00004233 }
Howard Hinnant70d27852010-07-27 01:25:38 +00004234 else
4235 __first = __temp;
Howard Hinnante1053822010-07-22 17:53:24 +00004236 return __first;
4237}
4238
4239template <class _CharT, class _Traits>
4240template <class _ForwardIterator>
4241_ForwardIterator
4242basic_regex<_CharT, _Traits>::__parse_assertion(_ForwardIterator __first,
4243 _ForwardIterator __last)
4244{
4245 if (__first != __last)
4246 {
4247 switch (*__first)
4248 {
4249 case '^':
4250 __push_l_anchor();
4251 ++__first;
4252 break;
4253 case '$':
4254 __push_r_anchor();
4255 ++__first;
4256 break;
4257 case '\\':
4258 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004259 _ForwardIterator __temp = _VSTD::next(__first);
Howard Hinnante1053822010-07-22 17:53:24 +00004260 if (__temp != __last)
4261 {
4262 if (*__temp == 'b')
4263 {
Howard Hinnant70d27852010-07-27 01:25:38 +00004264 __push_word_boundary(false);
Howard Hinnante1053822010-07-22 17:53:24 +00004265 __first = ++__temp;
4266 }
4267 else if (*__temp == 'B')
4268 {
Howard Hinnant70d27852010-07-27 01:25:38 +00004269 __push_word_boundary(true);
Howard Hinnante1053822010-07-22 17:53:24 +00004270 __first = ++__temp;
4271 }
4272 }
4273 }
4274 break;
4275 case '(':
4276 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004277 _ForwardIterator __temp = _VSTD::next(__first);
Howard Hinnante1053822010-07-22 17:53:24 +00004278 if (__temp != __last && *__temp == '?')
4279 {
4280 if (++__temp != __last)
4281 {
4282 switch (*__temp)
4283 {
4284 case '=':
Howard Hinnant126da6a2010-07-27 22:20:32 +00004285 {
4286 basic_regex __exp;
4287 __exp.__flags_ = __flags_;
4288 __temp = __exp.__parse(++__temp, __last);
Howard Hinnant3efac712013-07-23 16:18:04 +00004289 unsigned __mexp = __exp.__marked_count_;
4290 __push_lookahead(_VSTD::move(__exp), false, __marked_count_);
4291 __marked_count_ += __mexp;
Howard Hinnant126da6a2010-07-27 22:20:32 +00004292 if (__temp == __last || *__temp != ')')
Marshall Clowc8ccc292015-07-28 13:30:47 +00004293 __throw_regex_error<regex_constants::error_paren>();
Howard Hinnant126da6a2010-07-27 22:20:32 +00004294 __first = ++__temp;
4295 }
Howard Hinnante1053822010-07-22 17:53:24 +00004296 break;
4297 case '!':
Howard Hinnant126da6a2010-07-27 22:20:32 +00004298 {
4299 basic_regex __exp;
4300 __exp.__flags_ = __flags_;
4301 __temp = __exp.__parse(++__temp, __last);
Howard Hinnant3efac712013-07-23 16:18:04 +00004302 unsigned __mexp = __exp.__marked_count_;
4303 __push_lookahead(_VSTD::move(__exp), true, __marked_count_);
4304 __marked_count_ += __mexp;
Howard Hinnant126da6a2010-07-27 22:20:32 +00004305 if (__temp == __last || *__temp != ')')
Marshall Clowc8ccc292015-07-28 13:30:47 +00004306 __throw_regex_error<regex_constants::error_paren>();
Howard Hinnant126da6a2010-07-27 22:20:32 +00004307 __first = ++__temp;
4308 }
Howard Hinnante1053822010-07-22 17:53:24 +00004309 break;
4310 }
4311 }
4312 }
4313 }
4314 break;
4315 }
4316 }
4317 return __first;
4318}
4319
4320template <class _CharT, class _Traits>
4321template <class _ForwardIterator>
4322_ForwardIterator
4323basic_regex<_CharT, _Traits>::__parse_atom(_ForwardIterator __first,
4324 _ForwardIterator __last)
4325{
Howard Hinnant70d27852010-07-27 01:25:38 +00004326 if (__first != __last)
4327 {
4328 switch (*__first)
4329 {
4330 case '.':
4331 __push_match_any_but_newline();
4332 ++__first;
4333 break;
4334 case '\\':
4335 __first = __parse_atom_escape(__first, __last);
4336 break;
4337 case '[':
4338 __first = __parse_bracket_expression(__first, __last);
4339 break;
4340 case '(':
4341 {
Howard Hinnant72f73582010-08-11 17:04:31 +00004342 ++__first;
Howard Hinnant72f73582010-08-11 17:04:31 +00004343 if (__first == __last)
Marshall Clowc8ccc292015-07-28 13:30:47 +00004344 __throw_regex_error<regex_constants::error_paren>();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004345 _ForwardIterator __temp = _VSTD::next(__first);
Howard Hinnant70d27852010-07-27 01:25:38 +00004346 if (__temp != __last && *__first == '?' && *__temp == ':')
4347 {
4348 ++__open_count_;
4349 __first = __parse_ecma_exp(++__temp, __last);
4350 if (__first == __last || *__first != ')')
Marshall Clowc8ccc292015-07-28 13:30:47 +00004351 __throw_regex_error<regex_constants::error_paren>();
Howard Hinnant70d27852010-07-27 01:25:38 +00004352 --__open_count_;
4353 ++__first;
4354 }
4355 else
4356 {
4357 __push_begin_marked_subexpression();
4358 unsigned __temp_count = __marked_count_;
4359 ++__open_count_;
4360 __first = __parse_ecma_exp(__first, __last);
4361 if (__first == __last || *__first != ')')
Marshall Clowc8ccc292015-07-28 13:30:47 +00004362 __throw_regex_error<regex_constants::error_paren>();
Howard Hinnant70d27852010-07-27 01:25:38 +00004363 __push_end_marked_subexpression(__temp_count);
4364 --__open_count_;
4365 ++__first;
4366 }
4367 }
4368 break;
Marshall Clow82058212015-07-23 18:27:51 +00004369 case '*':
4370 case '+':
4371 case '?':
4372 case '{':
Marshall Clowc8ccc292015-07-28 13:30:47 +00004373 __throw_regex_error<regex_constants::error_badrepeat>();
Marshall Clow82058212015-07-23 18:27:51 +00004374 break;
Howard Hinnant70d27852010-07-27 01:25:38 +00004375 default:
4376 __first = __parse_pattern_character(__first, __last);
4377 break;
4378 }
4379 }
4380 return __first;
4381}
4382
4383template <class _CharT, class _Traits>
4384template <class _ForwardIterator>
4385_ForwardIterator
4386basic_regex<_CharT, _Traits>::__parse_atom_escape(_ForwardIterator __first,
4387 _ForwardIterator __last)
4388{
4389 if (__first != __last && *__first == '\\')
4390 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004391 _ForwardIterator __t1 = _VSTD::next(__first);
Marshall Clowdb9633c2016-01-19 00:50:37 +00004392 if (__t1 == __last)
4393 __throw_regex_error<regex_constants::error_escape>();
4394
Howard Hinnant70d27852010-07-27 01:25:38 +00004395 _ForwardIterator __t2 = __parse_decimal_escape(__t1, __last);
4396 if (__t2 != __t1)
4397 __first = __t2;
4398 else
4399 {
4400 __t2 = __parse_character_class_escape(__t1, __last);
4401 if (__t2 != __t1)
4402 __first = __t2;
4403 else
4404 {
4405 __t2 = __parse_character_escape(__t1, __last);
4406 if (__t2 != __t1)
4407 __first = __t2;
4408 }
4409 }
4410 }
4411 return __first;
4412}
4413
4414template <class _CharT, class _Traits>
4415template <class _ForwardIterator>
4416_ForwardIterator
4417basic_regex<_CharT, _Traits>::__parse_decimal_escape(_ForwardIterator __first,
4418 _ForwardIterator __last)
4419{
4420 if (__first != __last)
4421 {
4422 if (*__first == '0')
4423 {
4424 __push_char(_CharT());
4425 ++__first;
4426 }
4427 else if ('1' <= *__first && *__first <= '9')
4428 {
4429 unsigned __v = *__first - '0';
Marshall Clowdc03ec72016-12-24 17:21:03 +00004430 for (++__first;
4431 __first != __last && '0' <= *__first && *__first <= '9'; ++__first)
Marshall Clow266b5ec2017-10-19 22:10:41 +00004432 {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05004433 if (__v >= numeric_limits<unsigned>::max() / 10)
Marshall Clow266b5ec2017-10-19 22:10:41 +00004434 __throw_regex_error<regex_constants::error_backref>();
Howard Hinnant70d27852010-07-27 01:25:38 +00004435 __v = 10 * __v + *__first - '0';
Marshall Clow266b5ec2017-10-19 22:10:41 +00004436 }
4437 if (__v == 0 || __v > mark_count())
Marshall Clowc8ccc292015-07-28 13:30:47 +00004438 __throw_regex_error<regex_constants::error_backref>();
Howard Hinnant70d27852010-07-27 01:25:38 +00004439 __push_back_ref(__v);
4440 }
4441 }
4442 return __first;
4443}
4444
4445template <class _CharT, class _Traits>
4446template <class _ForwardIterator>
4447_ForwardIterator
4448basic_regex<_CharT, _Traits>::__parse_character_class_escape(_ForwardIterator __first,
4449 _ForwardIterator __last)
4450{
4451 if (__first != __last)
4452 {
4453 __bracket_expression<_CharT, _Traits>* __ml;
4454 switch (*__first)
4455 {
4456 case 'd':
4457 __ml = __start_matching_list(false);
4458 __ml->__add_class(ctype_base::digit);
4459 ++__first;
4460 break;
4461 case 'D':
4462 __ml = __start_matching_list(true);
4463 __ml->__add_class(ctype_base::digit);
4464 ++__first;
4465 break;
4466 case 's':
4467 __ml = __start_matching_list(false);
4468 __ml->__add_class(ctype_base::space);
4469 ++__first;
4470 break;
4471 case 'S':
4472 __ml = __start_matching_list(true);
4473 __ml->__add_class(ctype_base::space);
4474 ++__first;
4475 break;
4476 case 'w':
4477 __ml = __start_matching_list(false);
4478 __ml->__add_class(ctype_base::alnum);
4479 __ml->__add_char('_');
4480 ++__first;
4481 break;
4482 case 'W':
4483 __ml = __start_matching_list(true);
4484 __ml->__add_class(ctype_base::alnum);
4485 __ml->__add_char('_');
4486 ++__first;
4487 break;
4488 }
4489 }
4490 return __first;
4491}
4492
4493template <class _CharT, class _Traits>
4494template <class _ForwardIterator>
4495_ForwardIterator
4496basic_regex<_CharT, _Traits>::__parse_character_escape(_ForwardIterator __first,
Howard Hinnant70b3e192010-07-28 17:35:27 +00004497 _ForwardIterator __last,
4498 basic_string<_CharT>* __str)
Howard Hinnant70d27852010-07-27 01:25:38 +00004499{
4500 if (__first != __last)
4501 {
4502 _ForwardIterator __t;
4503 unsigned __sum = 0;
4504 int __hd;
4505 switch (*__first)
4506 {
4507 case 'f':
Howard Hinnant70b3e192010-07-28 17:35:27 +00004508 if (__str)
4509 *__str = _CharT(0xC);
4510 else
4511 __push_char(_CharT(0xC));
Howard Hinnant70d27852010-07-27 01:25:38 +00004512 ++__first;
4513 break;
4514 case 'n':
Howard Hinnant70b3e192010-07-28 17:35:27 +00004515 if (__str)
4516 *__str = _CharT(0xA);
4517 else
4518 __push_char(_CharT(0xA));
Howard Hinnant70d27852010-07-27 01:25:38 +00004519 ++__first;
4520 break;
4521 case 'r':
Howard Hinnant70b3e192010-07-28 17:35:27 +00004522 if (__str)
4523 *__str = _CharT(0xD);
4524 else
4525 __push_char(_CharT(0xD));
Howard Hinnant70d27852010-07-27 01:25:38 +00004526 ++__first;
4527 break;
4528 case 't':
Howard Hinnant70b3e192010-07-28 17:35:27 +00004529 if (__str)
4530 *__str = _CharT(0x9);
4531 else
4532 __push_char(_CharT(0x9));
Howard Hinnant70d27852010-07-27 01:25:38 +00004533 ++__first;
4534 break;
4535 case 'v':
Howard Hinnant70b3e192010-07-28 17:35:27 +00004536 if (__str)
4537 *__str = _CharT(0xB);
4538 else
4539 __push_char(_CharT(0xB));
Howard Hinnant70d27852010-07-27 01:25:38 +00004540 ++__first;
4541 break;
4542 case 'c':
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004543 if ((__t = _VSTD::next(__first)) != __last)
Howard Hinnant70d27852010-07-27 01:25:38 +00004544 {
Louis Dionne173f29e2019-05-29 16:01:36 +00004545 if (('A' <= *__t && *__t <= 'Z') ||
Howard Hinnantd04741b2013-07-15 18:21:11 +00004546 ('a' <= *__t && *__t <= 'z'))
Howard Hinnant70d27852010-07-27 01:25:38 +00004547 {
Howard Hinnant70b3e192010-07-28 17:35:27 +00004548 if (__str)
4549 *__str = _CharT(*__t % 32);
4550 else
4551 __push_char(_CharT(*__t % 32));
Howard Hinnant70d27852010-07-27 01:25:38 +00004552 __first = ++__t;
4553 }
Louis Dionne173f29e2019-05-29 16:01:36 +00004554 else
Marshall Clowc8ccc292015-07-28 13:30:47 +00004555 __throw_regex_error<regex_constants::error_escape>();
Howard Hinnant70d27852010-07-27 01:25:38 +00004556 }
Howard Hinnantd04741b2013-07-15 18:21:11 +00004557 else
Marshall Clowc8ccc292015-07-28 13:30:47 +00004558 __throw_regex_error<regex_constants::error_escape>();
Howard Hinnant70d27852010-07-27 01:25:38 +00004559 break;
4560 case 'u':
Howard Hinnant72f73582010-08-11 17:04:31 +00004561 ++__first;
Howard Hinnant72f73582010-08-11 17:04:31 +00004562 if (__first == __last)
Marshall Clowc8ccc292015-07-28 13:30:47 +00004563 __throw_regex_error<regex_constants::error_escape>();
Howard Hinnant70d27852010-07-27 01:25:38 +00004564 __hd = __traits_.value(*__first, 16);
4565 if (__hd == -1)
Marshall Clowc8ccc292015-07-28 13:30:47 +00004566 __throw_regex_error<regex_constants::error_escape>();
Howard Hinnant28b24882011-12-01 20:21:04 +00004567 __sum = 16 * __sum + static_cast<unsigned>(__hd);
Howard Hinnant72f73582010-08-11 17:04:31 +00004568 ++__first;
Howard Hinnant72f73582010-08-11 17:04:31 +00004569 if (__first == __last)
Marshall Clowc8ccc292015-07-28 13:30:47 +00004570 __throw_regex_error<regex_constants::error_escape>();
Howard Hinnant70d27852010-07-27 01:25:38 +00004571 __hd = __traits_.value(*__first, 16);
4572 if (__hd == -1)
Marshall Clowc8ccc292015-07-28 13:30:47 +00004573 __throw_regex_error<regex_constants::error_escape>();
Howard Hinnant28b24882011-12-01 20:21:04 +00004574 __sum = 16 * __sum + static_cast<unsigned>(__hd);
Howard Hinnant70d27852010-07-27 01:25:38 +00004575 // drop through
4576 case 'x':
Howard Hinnant72f73582010-08-11 17:04:31 +00004577 ++__first;
Howard Hinnant72f73582010-08-11 17:04:31 +00004578 if (__first == __last)
Marshall Clowc8ccc292015-07-28 13:30:47 +00004579 __throw_regex_error<regex_constants::error_escape>();
Howard Hinnant70d27852010-07-27 01:25:38 +00004580 __hd = __traits_.value(*__first, 16);
4581 if (__hd == -1)
Marshall Clowc8ccc292015-07-28 13:30:47 +00004582 __throw_regex_error<regex_constants::error_escape>();
Howard Hinnant28b24882011-12-01 20:21:04 +00004583 __sum = 16 * __sum + static_cast<unsigned>(__hd);
Howard Hinnant72f73582010-08-11 17:04:31 +00004584 ++__first;
Howard Hinnant72f73582010-08-11 17:04:31 +00004585 if (__first == __last)
Marshall Clowc8ccc292015-07-28 13:30:47 +00004586 __throw_regex_error<regex_constants::error_escape>();
Howard Hinnant70d27852010-07-27 01:25:38 +00004587 __hd = __traits_.value(*__first, 16);
4588 if (__hd == -1)
Marshall Clowc8ccc292015-07-28 13:30:47 +00004589 __throw_regex_error<regex_constants::error_escape>();
Howard Hinnant28b24882011-12-01 20:21:04 +00004590 __sum = 16 * __sum + static_cast<unsigned>(__hd);
Howard Hinnant70b3e192010-07-28 17:35:27 +00004591 if (__str)
4592 *__str = _CharT(__sum);
4593 else
4594 __push_char(_CharT(__sum));
Howard Hinnant70d27852010-07-27 01:25:38 +00004595 ++__first;
4596 break;
Marshall Clowf6cef0d2014-05-21 16:29:50 +00004597 case '0':
4598 if (__str)
4599 *__str = _CharT(0);
4600 else
4601 __push_char(_CharT(0));
4602 ++__first;
4603 break;
Howard Hinnant70d27852010-07-27 01:25:38 +00004604 default:
4605 if (*__first != '_' && !__traits_.isctype(*__first, ctype_base::alnum))
4606 {
Howard Hinnant70b3e192010-07-28 17:35:27 +00004607 if (__str)
4608 *__str = *__first;
4609 else
4610 __push_char(*__first);
Howard Hinnant70d27852010-07-27 01:25:38 +00004611 ++__first;
4612 }
Howard Hinnant4f7a1f52013-06-28 18:57:30 +00004613 else
Marshall Clowc8ccc292015-07-28 13:30:47 +00004614 __throw_regex_error<regex_constants::error_escape>();
Howard Hinnant70d27852010-07-27 01:25:38 +00004615 break;
4616 }
4617 }
4618 return __first;
4619}
4620
4621template <class _CharT, class _Traits>
4622template <class _ForwardIterator>
4623_ForwardIterator
4624basic_regex<_CharT, _Traits>::__parse_pattern_character(_ForwardIterator __first,
4625 _ForwardIterator __last)
4626{
4627 if (__first != __last)
4628 {
4629 switch (*__first)
4630 {
4631 case '^':
4632 case '$':
4633 case '\\':
4634 case '.':
4635 case '*':
4636 case '+':
4637 case '?':
4638 case '(':
4639 case ')':
4640 case '[':
4641 case ']':
4642 case '{':
4643 case '}':
4644 case '|':
4645 break;
4646 default:
4647 __push_char(*__first);
4648 ++__first;
4649 break;
4650 }
4651 }
4652 return __first;
Howard Hinnante1053822010-07-22 17:53:24 +00004653}
4654
4655template <class _CharT, class _Traits>
Howard Hinnanteaf649e2010-07-27 19:53:10 +00004656template <class _ForwardIterator>
4657_ForwardIterator
4658basic_regex<_CharT, _Traits>::__parse_grep(_ForwardIterator __first,
4659 _ForwardIterator __last)
4660{
4661 __owns_one_state<_CharT>* __sa = __end_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004662 _ForwardIterator __t1 = _VSTD::find(__first, __last, _CharT('\n'));
Howard Hinnanteaf649e2010-07-27 19:53:10 +00004663 if (__t1 != __first)
4664 __parse_basic_reg_exp(__first, __t1);
4665 else
4666 __push_empty();
4667 __first = __t1;
4668 if (__first != __last)
4669 ++__first;
4670 while (__first != __last)
4671 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004672 __t1 = _VSTD::find(__first, __last, _CharT('\n'));
Howard Hinnanteaf649e2010-07-27 19:53:10 +00004673 __owns_one_state<_CharT>* __sb = __end_;
4674 if (__t1 != __first)
4675 __parse_basic_reg_exp(__first, __t1);
4676 else
4677 __push_empty();
4678 __push_alternation(__sa, __sb);
4679 __first = __t1;
4680 if (__first != __last)
4681 ++__first;
4682 }
4683 return __first;
4684}
4685
4686template <class _CharT, class _Traits>
4687template <class _ForwardIterator>
4688_ForwardIterator
4689basic_regex<_CharT, _Traits>::__parse_egrep(_ForwardIterator __first,
4690 _ForwardIterator __last)
4691{
4692 __owns_one_state<_CharT>* __sa = __end_;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004693 _ForwardIterator __t1 = _VSTD::find(__first, __last, _CharT('\n'));
Howard Hinnanteaf649e2010-07-27 19:53:10 +00004694 if (__t1 != __first)
4695 __parse_extended_reg_exp(__first, __t1);
4696 else
4697 __push_empty();
4698 __first = __t1;
4699 if (__first != __last)
4700 ++__first;
4701 while (__first != __last)
4702 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004703 __t1 = _VSTD::find(__first, __last, _CharT('\n'));
Howard Hinnanteaf649e2010-07-27 19:53:10 +00004704 __owns_one_state<_CharT>* __sb = __end_;
4705 if (__t1 != __first)
4706 __parse_extended_reg_exp(__first, __t1);
4707 else
4708 __push_empty();
4709 __push_alternation(__sa, __sb);
4710 __first = __t1;
4711 if (__first != __last)
4712 ++__first;
4713 }
4714 return __first;
4715}
4716
4717template <class _CharT, class _Traits>
Louis Dionnef16eb592020-02-19 15:56:15 -05004718bool
4719basic_regex<_CharT, _Traits>::__test_back_ref(_CharT c)
4720{
4721 unsigned __val = __traits_.value(c, 10);
4722 if (__val >= 1 && __val <= 9)
4723 {
Mark de Weverd324e5f2020-02-20 18:13:38 -05004724 if (__val > mark_count())
4725 __throw_regex_error<regex_constants::error_backref>();
Louis Dionnef16eb592020-02-19 15:56:15 -05004726 __push_back_ref(__val);
4727 return true;
4728 }
4729
4730 return false;
4731}
4732
4733template <class _CharT, class _Traits>
Howard Hinnant67ad2132010-06-29 18:37:43 +00004734void
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00004735basic_regex<_CharT, _Traits>::__push_loop(size_t __min, size_t __max,
4736 __owns_one_state<_CharT>* __s, size_t __mexp_begin, size_t __mexp_end,
4737 bool __greedy)
4738{
4739 unique_ptr<__empty_state<_CharT> > __e1(new __empty_state<_CharT>(__end_->first()));
4740 __end_->first() = nullptr;
Howard Hinnantaa0874c2010-07-12 15:51:17 +00004741 unique_ptr<__loop<_CharT> > __e2(new __loop<_CharT>(__loop_count_,
4742 __s->first(), __e1.get(), __mexp_begin, __mexp_end, __greedy,
4743 __min, __max));
4744 __s->first() = nullptr;
4745 __e1.release();
4746 __end_->first() = new __repeat_one_loop<_CharT>(__e2.get());
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00004747 __end_ = __e2->second();
Howard Hinnantaa0874c2010-07-12 15:51:17 +00004748 __s->first() = __e2.release();
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00004749 ++__loop_count_;
4750}
4751
4752template <class _CharT, class _Traits>
4753void
Howard Hinnant67ad2132010-06-29 18:37:43 +00004754basic_regex<_CharT, _Traits>::__push_char(value_type __c)
4755{
Howard Hinnant3034c902010-07-13 21:48:06 +00004756 if (flags() & icase)
Howard Hinnantcfe8d272010-07-12 19:11:27 +00004757 __end_->first() = new __match_char_icase<_CharT, _Traits>
4758 (__traits_, __c, __end_->first());
Howard Hinnant3034c902010-07-13 21:48:06 +00004759 else if (flags() & collate)
Howard Hinnantcfe8d272010-07-12 19:11:27 +00004760 __end_->first() = new __match_char_collate<_CharT, _Traits>
4761 (__traits_, __c, __end_->first());
4762 else
4763 __end_->first() = new __match_char<_CharT>(__c, __end_->first());
Howard Hinnant5bf96132010-07-08 17:43:58 +00004764 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
Howard Hinnant67ad2132010-06-29 18:37:43 +00004765}
4766
Howard Hinnant93ef6552010-06-30 20:30:19 +00004767template <class _CharT, class _Traits>
4768void
4769basic_regex<_CharT, _Traits>::__push_begin_marked_subexpression()
4770{
Howard Hinnant5d4aaa42010-07-14 15:45:11 +00004771 if (!(__flags_ & nosubs))
4772 {
4773 __end_->first() =
4774 new __begin_marked_subexpression<_CharT>(++__marked_count_,
4775 __end_->first());
4776 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4777 }
Howard Hinnant93ef6552010-06-30 20:30:19 +00004778}
4779
4780template <class _CharT, class _Traits>
4781void
4782basic_regex<_CharT, _Traits>::__push_end_marked_subexpression(unsigned __sub)
4783{
Howard Hinnant5d4aaa42010-07-14 15:45:11 +00004784 if (!(__flags_ & nosubs))
4785 {
4786 __end_->first() =
4787 new __end_marked_subexpression<_CharT>(__sub, __end_->first());
4788 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4789 }
Howard Hinnant93ef6552010-06-30 20:30:19 +00004790}
4791
Howard Hinnantaad0aa62010-07-09 00:15:26 +00004792template <class _CharT, class _Traits>
4793void
Howard Hinnant066ba512011-03-26 20:02:27 +00004794basic_regex<_CharT, _Traits>::__push_l_anchor()
4795{
Mark de Wevera989cce2020-11-18 18:09:13 +01004796 __end_->first() = new __l_anchor_multiline<_CharT>(__use_multiline(), __end_->first());
Howard Hinnant066ba512011-03-26 20:02:27 +00004797 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4798}
4799
4800template <class _CharT, class _Traits>
4801void
Howard Hinnantaad0aa62010-07-09 00:15:26 +00004802basic_regex<_CharT, _Traits>::__push_r_anchor()
4803{
Mark de Wevera989cce2020-11-18 18:09:13 +01004804 __end_->first() = new __r_anchor_multiline<_CharT>(__use_multiline(), __end_->first());
Howard Hinnantaad0aa62010-07-09 00:15:26 +00004805 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4806}
4807
Howard Hinnantaa0874c2010-07-12 15:51:17 +00004808template <class _CharT, class _Traits>
4809void
4810basic_regex<_CharT, _Traits>::__push_match_any()
4811{
4812 __end_->first() = new __match_any<_CharT>(__end_->first());
4813 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4814}
Howard Hinnantaad0aa62010-07-09 00:15:26 +00004815
Howard Hinnant2a315e32010-07-12 18:16:05 +00004816template <class _CharT, class _Traits>
4817void
Howard Hinnant70d27852010-07-27 01:25:38 +00004818basic_regex<_CharT, _Traits>::__push_match_any_but_newline()
4819{
4820 __end_->first() = new __match_any_but_newline<_CharT>(__end_->first());
4821 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4822}
4823
4824template <class _CharT, class _Traits>
4825void
Howard Hinnante1053822010-07-22 17:53:24 +00004826basic_regex<_CharT, _Traits>::__push_empty()
4827{
4828 __end_->first() = new __empty_state<_CharT>(__end_->first());
4829 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4830}
4831
4832template <class _CharT, class _Traits>
4833void
Howard Hinnant70d27852010-07-27 01:25:38 +00004834basic_regex<_CharT, _Traits>::__push_word_boundary(bool __invert)
4835{
4836 __end_->first() = new __word_boundary<_CharT, _Traits>(__traits_, __invert,
4837 __end_->first());
4838 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4839}
4840
4841template <class _CharT, class _Traits>
4842void
Howard Hinnant2a315e32010-07-12 18:16:05 +00004843basic_regex<_CharT, _Traits>::__push_back_ref(int __i)
4844{
Howard Hinnant3034c902010-07-13 21:48:06 +00004845 if (flags() & icase)
Howard Hinnantcfe8d272010-07-12 19:11:27 +00004846 __end_->first() = new __back_ref_icase<_CharT, _Traits>
4847 (__traits_, __i, __end_->first());
Howard Hinnant3034c902010-07-13 21:48:06 +00004848 else if (flags() & collate)
Howard Hinnantcfe8d272010-07-12 19:11:27 +00004849 __end_->first() = new __back_ref_collate<_CharT, _Traits>
4850 (__traits_, __i, __end_->first());
4851 else
4852 __end_->first() = new __back_ref<_CharT>(__i, __end_->first());
Howard Hinnant2a315e32010-07-12 18:16:05 +00004853 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4854}
4855
Howard Hinnant3034c902010-07-13 21:48:06 +00004856template <class _CharT, class _Traits>
Howard Hinnant16d65422010-07-16 19:08:36 +00004857void
4858basic_regex<_CharT, _Traits>::__push_alternation(__owns_one_state<_CharT>* __sa,
4859 __owns_one_state<_CharT>* __ea)
4860{
4861 __sa->first() = new __alternate<_CharT>(
4862 static_cast<__owns_one_state<_CharT>*>(__sa->first()),
4863 static_cast<__owns_one_state<_CharT>*>(__ea->first()));
4864 __ea->first() = nullptr;
4865 __ea->first() = new __empty_state<_CharT>(__end_->first());
4866 __end_->first() = nullptr;
4867 __end_->first() = new __empty_non_own_state<_CharT>(__ea->first());
4868 __end_ = static_cast<__owns_one_state<_CharT>*>(__ea->first());
4869}
4870
4871template <class _CharT, class _Traits>
Howard Hinnant3034c902010-07-13 21:48:06 +00004872__bracket_expression<_CharT, _Traits>*
4873basic_regex<_CharT, _Traits>::__start_matching_list(bool __negate)
4874{
4875 __bracket_expression<_CharT, _Traits>* __r =
4876 new __bracket_expression<_CharT, _Traits>(__traits_, __end_->first(),
4877 __negate, __flags_ & icase,
4878 __flags_ & collate);
4879 __end_->first() = __r;
4880 __end_ = __r;
4881 return __r;
4882}
4883
Howard Hinnant126da6a2010-07-27 22:20:32 +00004884template <class _CharT, class _Traits>
4885void
4886basic_regex<_CharT, _Traits>::__push_lookahead(const basic_regex& __exp,
Howard Hinnant3efac712013-07-23 16:18:04 +00004887 bool __invert,
4888 unsigned __mexp)
Howard Hinnant126da6a2010-07-27 22:20:32 +00004889{
4890 __end_->first() = new __lookahead<_CharT, _Traits>(__exp, __invert,
Howard Hinnant3efac712013-07-23 16:18:04 +00004891 __end_->first(), __mexp);
Howard Hinnant126da6a2010-07-27 22:20:32 +00004892 __end_ = static_cast<__owns_one_state<_CharT>*>(__end_->first());
4893}
4894
Howard Hinnant67ad2132010-06-29 18:37:43 +00004895// sub_match
4896
Richard Smith256954d2020-11-11 17:12:18 -08004897typedef sub_match<const char*> csub_match;
4898typedef sub_match<const wchar_t*> wcsub_match;
4899typedef sub_match<string::const_iterator> ssub_match;
4900typedef sub_match<wstring::const_iterator> wssub_match;
4901
Howard Hinnant67ad2132010-06-29 18:37:43 +00004902template <class _BidirectionalIterator>
Richard Smith256954d2020-11-11 17:12:18 -08004903class
4904 _LIBCPP_TEMPLATE_VIS
4905 _LIBCPP_PREFERRED_NAME(csub_match)
4906 _LIBCPP_PREFERRED_NAME(wcsub_match)
4907 _LIBCPP_PREFERRED_NAME(ssub_match)
4908 _LIBCPP_PREFERRED_NAME(wssub_match)
4909 sub_match
Howard Hinnant67ad2132010-06-29 18:37:43 +00004910 : public pair<_BidirectionalIterator, _BidirectionalIterator>
4911{
4912public:
4913 typedef _BidirectionalIterator iterator;
4914 typedef typename iterator_traits<iterator>::value_type value_type;
4915 typedef typename iterator_traits<iterator>::difference_type difference_type;
4916 typedef basic_string<value_type> string_type;
4917
4918 bool matched;
4919
Howard Hinnant7ca9d942010-09-23 15:13:20 +00004920 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant5ddd33c2012-07-21 01:31:58 +00004921 _LIBCPP_CONSTEXPR sub_match() : matched() {}
Howard Hinnantb5c53a82010-12-08 21:07:55 +00004922
4923 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant67ad2132010-06-29 18:37:43 +00004924 difference_type length() const
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004925 {return matched ? _VSTD::distance(this->first, this->second) : 0;}
Howard Hinnant7ca9d942010-09-23 15:13:20 +00004926 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant67ad2132010-06-29 18:37:43 +00004927 string_type str() const
4928 {return matched ? string_type(this->first, this->second) : string_type();}
Howard Hinnant7ca9d942010-09-23 15:13:20 +00004929 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant67ad2132010-06-29 18:37:43 +00004930 operator string_type() const
4931 {return str();}
4932
Howard Hinnant7ca9d942010-09-23 15:13:20 +00004933 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant67ad2132010-06-29 18:37:43 +00004934 int compare(const sub_match& __s) const
4935 {return str().compare(__s.str());}
Howard Hinnant7ca9d942010-09-23 15:13:20 +00004936 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant67ad2132010-06-29 18:37:43 +00004937 int compare(const string_type& __s) const
4938 {return str().compare(__s);}
Howard Hinnant7ca9d942010-09-23 15:13:20 +00004939 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant67ad2132010-06-29 18:37:43 +00004940 int compare(const value_type* __s) const
4941 {return str().compare(__s);}
4942};
4943
Howard Hinnant67ad2132010-06-29 18:37:43 +00004944template <class _BiIter>
4945inline _LIBCPP_INLINE_VISIBILITY
4946bool
4947operator==(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
4948{
4949 return __x.compare(__y) == 0;
4950}
4951
4952template <class _BiIter>
4953inline _LIBCPP_INLINE_VISIBILITY
4954bool
4955operator!=(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
4956{
4957 return !(__x == __y);
4958}
4959
4960template <class _BiIter>
4961inline _LIBCPP_INLINE_VISIBILITY
4962bool
4963operator<(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
4964{
4965 return __x.compare(__y) < 0;
4966}
4967
4968template <class _BiIter>
4969inline _LIBCPP_INLINE_VISIBILITY
4970bool
4971operator<=(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
4972{
4973 return !(__y < __x);
4974}
4975
4976template <class _BiIter>
4977inline _LIBCPP_INLINE_VISIBILITY
4978bool
4979operator>=(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
4980{
4981 return !(__x < __y);
4982}
4983
4984template <class _BiIter>
4985inline _LIBCPP_INLINE_VISIBILITY
4986bool
4987operator>(const sub_match<_BiIter>& __x, const sub_match<_BiIter>& __y)
4988{
4989 return __y < __x;
4990}
4991
4992template <class _BiIter, class _ST, class _SA>
4993inline _LIBCPP_INLINE_VISIBILITY
4994bool
4995operator==(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
4996 const sub_match<_BiIter>& __y)
4997{
Marshall Clow54a46342014-12-15 23:57:56 +00004998 return __y.compare(typename sub_match<_BiIter>::string_type(__x.data(), __x.size())) == 0;
Howard Hinnant67ad2132010-06-29 18:37:43 +00004999}
5000
5001template <class _BiIter, class _ST, class _SA>
5002inline _LIBCPP_INLINE_VISIBILITY
5003bool
5004operator!=(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
5005 const sub_match<_BiIter>& __y)
5006{
5007 return !(__x == __y);
5008}
5009
5010template <class _BiIter, class _ST, class _SA>
5011inline _LIBCPP_INLINE_VISIBILITY
5012bool
5013operator<(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
5014 const sub_match<_BiIter>& __y)
5015{
Marshall Clow54a46342014-12-15 23:57:56 +00005016 return __y.compare(typename sub_match<_BiIter>::string_type(__x.data(), __x.size())) > 0;
Howard Hinnant67ad2132010-06-29 18:37:43 +00005017}
5018
5019template <class _BiIter, class _ST, class _SA>
5020inline _LIBCPP_INLINE_VISIBILITY
5021bool
5022operator>(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
5023 const sub_match<_BiIter>& __y)
5024{
5025 return __y < __x;
5026}
5027
5028template <class _BiIter, class _ST, class _SA>
5029inline _LIBCPP_INLINE_VISIBILITY
5030bool operator>=(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
5031 const sub_match<_BiIter>& __y)
5032{
5033 return !(__x < __y);
5034}
5035
5036template <class _BiIter, class _ST, class _SA>
5037inline _LIBCPP_INLINE_VISIBILITY
5038bool
5039operator<=(const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __x,
5040 const sub_match<_BiIter>& __y)
5041{
5042 return !(__y < __x);
5043}
5044
5045template <class _BiIter, class _ST, class _SA>
5046inline _LIBCPP_INLINE_VISIBILITY
5047bool
5048operator==(const sub_match<_BiIter>& __x,
5049 const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
5050{
Marshall Clow54a46342014-12-15 23:57:56 +00005051 return __x.compare(typename sub_match<_BiIter>::string_type(__y.data(), __y.size())) == 0;
Howard Hinnant67ad2132010-06-29 18:37:43 +00005052}
5053
5054template <class _BiIter, class _ST, class _SA>
5055inline _LIBCPP_INLINE_VISIBILITY
5056bool
5057operator!=(const sub_match<_BiIter>& __x,
5058 const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
5059{
5060 return !(__x == __y);
5061}
5062
5063template <class _BiIter, class _ST, class _SA>
5064inline _LIBCPP_INLINE_VISIBILITY
5065bool
5066operator<(const sub_match<_BiIter>& __x,
5067 const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
5068{
Marshall Clow54a46342014-12-15 23:57:56 +00005069 return __x.compare(typename sub_match<_BiIter>::string_type(__y.data(), __y.size())) < 0;
Howard Hinnant67ad2132010-06-29 18:37:43 +00005070}
5071
5072template <class _BiIter, class _ST, class _SA>
5073inline _LIBCPP_INLINE_VISIBILITY
5074bool operator>(const sub_match<_BiIter>& __x,
5075 const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
5076{
5077 return __y < __x;
5078}
5079
5080template <class _BiIter, class _ST, class _SA>
5081inline _LIBCPP_INLINE_VISIBILITY
5082bool
5083operator>=(const sub_match<_BiIter>& __x,
5084 const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
5085{
5086 return !(__x < __y);
5087}
5088
5089template <class _BiIter, class _ST, class _SA>
5090inline _LIBCPP_INLINE_VISIBILITY
5091bool
5092operator<=(const sub_match<_BiIter>& __x,
5093 const basic_string<typename iterator_traits<_BiIter>::value_type, _ST, _SA>& __y)
5094{
5095 return !(__y < __x);
5096}
5097
5098template <class _BiIter>
5099inline _LIBCPP_INLINE_VISIBILITY
5100bool
5101operator==(typename iterator_traits<_BiIter>::value_type const* __x,
5102 const sub_match<_BiIter>& __y)
5103{
5104 return __y.compare(__x) == 0;
5105}
5106
5107template <class _BiIter>
5108inline _LIBCPP_INLINE_VISIBILITY
5109bool
5110operator!=(typename iterator_traits<_BiIter>::value_type const* __x,
5111 const sub_match<_BiIter>& __y)
5112{
5113 return !(__x == __y);
5114}
5115
5116template <class _BiIter>
5117inline _LIBCPP_INLINE_VISIBILITY
5118bool
5119operator<(typename iterator_traits<_BiIter>::value_type const* __x,
5120 const sub_match<_BiIter>& __y)
5121{
5122 return __y.compare(__x) > 0;
5123}
5124
5125template <class _BiIter>
5126inline _LIBCPP_INLINE_VISIBILITY
5127bool
5128operator>(typename iterator_traits<_BiIter>::value_type const* __x,
5129 const sub_match<_BiIter>& __y)
5130{
5131 return __y < __x;
5132}
5133
5134template <class _BiIter>
5135inline _LIBCPP_INLINE_VISIBILITY
5136bool
5137operator>=(typename iterator_traits<_BiIter>::value_type const* __x,
5138 const sub_match<_BiIter>& __y)
5139{
5140 return !(__x < __y);
5141}
5142
5143template <class _BiIter>
5144inline _LIBCPP_INLINE_VISIBILITY
5145bool
5146operator<=(typename iterator_traits<_BiIter>::value_type const* __x,
5147 const sub_match<_BiIter>& __y)
5148{
5149 return !(__y < __x);
5150}
5151
5152template <class _BiIter>
5153inline _LIBCPP_INLINE_VISIBILITY
5154bool
5155operator==(const sub_match<_BiIter>& __x,
5156 typename iterator_traits<_BiIter>::value_type const* __y)
5157{
5158 return __x.compare(__y) == 0;
5159}
5160
5161template <class _BiIter>
5162inline _LIBCPP_INLINE_VISIBILITY
5163bool
5164operator!=(const sub_match<_BiIter>& __x,
5165 typename iterator_traits<_BiIter>::value_type const* __y)
5166{
5167 return !(__x == __y);
5168}
5169
5170template <class _BiIter>
5171inline _LIBCPP_INLINE_VISIBILITY
5172bool
5173operator<(const sub_match<_BiIter>& __x,
5174 typename iterator_traits<_BiIter>::value_type const* __y)
5175{
5176 return __x.compare(__y) < 0;
5177}
5178
5179template <class _BiIter>
5180inline _LIBCPP_INLINE_VISIBILITY
5181bool
5182operator>(const sub_match<_BiIter>& __x,
5183 typename iterator_traits<_BiIter>::value_type const* __y)
5184{
5185 return __y < __x;
5186}
5187
5188template <class _BiIter>
5189inline _LIBCPP_INLINE_VISIBILITY
5190bool
5191operator>=(const sub_match<_BiIter>& __x,
5192 typename iterator_traits<_BiIter>::value_type const* __y)
5193{
5194 return !(__x < __y);
5195}
5196
5197template <class _BiIter>
5198inline _LIBCPP_INLINE_VISIBILITY
5199bool
5200operator<=(const sub_match<_BiIter>& __x,
5201 typename iterator_traits<_BiIter>::value_type const* __y)
5202{
5203 return !(__y < __x);
5204}
5205
5206template <class _BiIter>
5207inline _LIBCPP_INLINE_VISIBILITY
5208bool
5209operator==(typename iterator_traits<_BiIter>::value_type const& __x,
5210 const sub_match<_BiIter>& __y)
5211{
5212 typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
5213 return __y.compare(string_type(1, __x)) == 0;
5214}
5215
5216template <class _BiIter>
5217inline _LIBCPP_INLINE_VISIBILITY
5218bool
5219operator!=(typename iterator_traits<_BiIter>::value_type const& __x,
5220 const sub_match<_BiIter>& __y)
5221{
5222 return !(__x == __y);
5223}
5224
5225template <class _BiIter>
5226inline _LIBCPP_INLINE_VISIBILITY
5227bool
5228operator<(typename iterator_traits<_BiIter>::value_type const& __x,
5229 const sub_match<_BiIter>& __y)
5230{
5231 typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
5232 return __y.compare(string_type(1, __x)) > 0;
5233}
5234
5235template <class _BiIter>
5236inline _LIBCPP_INLINE_VISIBILITY
5237bool
5238operator>(typename iterator_traits<_BiIter>::value_type const& __x,
5239 const sub_match<_BiIter>& __y)
5240{
5241 return __y < __x;
5242}
5243
5244template <class _BiIter>
5245inline _LIBCPP_INLINE_VISIBILITY
5246bool
5247operator>=(typename iterator_traits<_BiIter>::value_type const& __x,
5248 const sub_match<_BiIter>& __y)
5249{
5250 return !(__x < __y);
5251}
5252
5253template <class _BiIter>
5254inline _LIBCPP_INLINE_VISIBILITY
5255bool
5256operator<=(typename iterator_traits<_BiIter>::value_type const& __x,
5257 const sub_match<_BiIter>& __y)
5258{
5259 return !(__y < __x);
5260}
5261
5262template <class _BiIter>
5263inline _LIBCPP_INLINE_VISIBILITY
5264bool
5265operator==(const sub_match<_BiIter>& __x,
5266 typename iterator_traits<_BiIter>::value_type const& __y)
5267{
5268 typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
5269 return __x.compare(string_type(1, __y)) == 0;
5270}
5271
5272template <class _BiIter>
5273inline _LIBCPP_INLINE_VISIBILITY
5274bool
5275operator!=(const sub_match<_BiIter>& __x,
5276 typename iterator_traits<_BiIter>::value_type const& __y)
5277{
5278 return !(__x == __y);
5279}
5280
5281template <class _BiIter>
5282inline _LIBCPP_INLINE_VISIBILITY
5283bool
5284operator<(const sub_match<_BiIter>& __x,
5285 typename iterator_traits<_BiIter>::value_type const& __y)
5286{
5287 typedef basic_string<typename iterator_traits<_BiIter>::value_type> string_type;
5288 return __x.compare(string_type(1, __y)) < 0;
5289}
5290
5291template <class _BiIter>
5292inline _LIBCPP_INLINE_VISIBILITY
5293bool
5294operator>(const sub_match<_BiIter>& __x,
5295 typename iterator_traits<_BiIter>::value_type const& __y)
5296{
5297 return __y < __x;
5298}
5299
5300template <class _BiIter>
5301inline _LIBCPP_INLINE_VISIBILITY
5302bool
5303operator>=(const sub_match<_BiIter>& __x,
5304 typename iterator_traits<_BiIter>::value_type const& __y)
5305{
5306 return !(__x < __y);
5307}
5308
5309template <class _BiIter>
5310inline _LIBCPP_INLINE_VISIBILITY
5311bool
5312operator<=(const sub_match<_BiIter>& __x,
5313 typename iterator_traits<_BiIter>::value_type const& __y)
5314{
5315 return !(__y < __x);
5316}
5317
5318template <class _CharT, class _ST, class _BiIter>
5319inline _LIBCPP_INLINE_VISIBILITY
5320basic_ostream<_CharT, _ST>&
5321operator<<(basic_ostream<_CharT, _ST>& __os, const sub_match<_BiIter>& __m)
5322{
5323 return __os << __m.str();
5324}
5325
Richard Smith256954d2020-11-11 17:12:18 -08005326typedef match_results<const char*> cmatch;
5327typedef match_results<const wchar_t*> wcmatch;
5328typedef match_results<string::const_iterator> smatch;
5329typedef match_results<wstring::const_iterator> wsmatch;
5330
Howard Hinnant70d27852010-07-27 01:25:38 +00005331template <class _BidirectionalIterator, class _Allocator>
Richard Smith256954d2020-11-11 17:12:18 -08005332class
5333 _LIBCPP_TEMPLATE_VIS
5334 _LIBCPP_PREFERRED_NAME(cmatch)
5335 _LIBCPP_PREFERRED_NAME(wcmatch)
5336 _LIBCPP_PREFERRED_NAME(smatch)
5337 _LIBCPP_PREFERRED_NAME(wsmatch)
5338 match_results
Howard Hinnant65a3f3d2010-06-30 00:21:42 +00005339{
5340public:
5341 typedef _Allocator allocator_type;
5342 typedef sub_match<_BidirectionalIterator> value_type;
5343private:
5344 typedef vector<value_type, allocator_type> __container_type;
5345
5346 __container_type __matches_;
5347 value_type __unmatched_;
5348 value_type __prefix_;
5349 value_type __suffix_;
Howard Hinnantb5c53a82010-12-08 21:07:55 +00005350 bool __ready_;
Howard Hinnant65a3f3d2010-06-30 00:21:42 +00005351public:
Howard Hinnantd3925342010-08-16 20:21:16 +00005352 _BidirectionalIterator __position_start_;
Howard Hinnant65a3f3d2010-06-30 00:21:42 +00005353 typedef const value_type& const_reference;
Marshall Clow96e06142014-02-26 01:56:31 +00005354 typedef value_type& reference;
Howard Hinnant65a3f3d2010-06-30 00:21:42 +00005355 typedef typename __container_type::const_iterator const_iterator;
5356 typedef const_iterator iterator;
5357 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type;
5358 typedef typename allocator_traits<allocator_type>::size_type size_type;
5359 typedef typename iterator_traits<_BidirectionalIterator>::value_type char_type;
5360 typedef basic_string<char_type> string_type;
5361
5362 // construct/copy/destroy:
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01005363#ifndef _LIBCPP_CXX03_LANG
5364 match_results() : match_results(allocator_type()) {}
5365 explicit match_results(const allocator_type& __a);
5366#else
Howard Hinnant65a3f3d2010-06-30 00:21:42 +00005367 explicit match_results(const allocator_type& __a = allocator_type());
Marek Kurdejcd0bd6a2021-01-19 08:21:09 +01005368#endif
5369
Howard Hinnant65a3f3d2010-06-30 00:21:42 +00005370// match_results(const match_results&) = default;
5371// match_results& operator=(const match_results&) = default;
Howard Hinnant65a3f3d2010-06-30 00:21:42 +00005372// match_results(match_results&& __m) = default;
5373// match_results& operator=(match_results&& __m) = default;
Howard Hinnant65a3f3d2010-06-30 00:21:42 +00005374// ~match_results() = default;
5375
Howard Hinnantb5c53a82010-12-08 21:07:55 +00005376 _LIBCPP_INLINE_VISIBILITY
5377 bool ready() const {return __ready_;}
5378
Howard Hinnant65a3f3d2010-06-30 00:21:42 +00005379 // size:
Howard Hinnant7ca9d942010-09-23 15:13:20 +00005380 _LIBCPP_INLINE_VISIBILITY
Marshall Clow081bcd22017-11-16 04:48:34 +00005381 size_type size() const _NOEXCEPT {return __matches_.size();}
Howard Hinnant7ca9d942010-09-23 15:13:20 +00005382 _LIBCPP_INLINE_VISIBILITY
Marshall Clow081bcd22017-11-16 04:48:34 +00005383 size_type max_size() const _NOEXCEPT {return __matches_.max_size();}
5384 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
5385 bool empty() const _NOEXCEPT {return size() == 0;}
Howard Hinnant65a3f3d2010-06-30 00:21:42 +00005386
5387 // element access:
Howard Hinnant7ca9d942010-09-23 15:13:20 +00005388 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant65a3f3d2010-06-30 00:21:42 +00005389 difference_type length(size_type __sub = 0) const
Marshall Clow14d319a2019-04-26 17:10:03 +00005390 {
5391 _LIBCPP_ASSERT(ready(), "match_results::length() called when not ready");
5392 return (*this)[__sub].length();
5393 }
Howard Hinnant7ca9d942010-09-23 15:13:20 +00005394 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant65a3f3d2010-06-30 00:21:42 +00005395 difference_type position(size_type __sub = 0) const
Marshall Clow14d319a2019-04-26 17:10:03 +00005396 {
5397 _LIBCPP_ASSERT(ready(), "match_results::position() called when not ready");
5398 return _VSTD::distance(__position_start_, (*this)[__sub].first);
5399 }
Howard Hinnant7ca9d942010-09-23 15:13:20 +00005400 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant65a3f3d2010-06-30 00:21:42 +00005401 string_type str(size_type __sub = 0) const
Marshall Clow14d319a2019-04-26 17:10:03 +00005402 {
5403 _LIBCPP_ASSERT(ready(), "match_results::str() called when not ready");
5404 return (*this)[__sub].str();
5405 }
Howard Hinnant7ca9d942010-09-23 15:13:20 +00005406 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant65a3f3d2010-06-30 00:21:42 +00005407 const_reference operator[](size_type __n) const
Marshall Clow14d319a2019-04-26 17:10:03 +00005408 {
5409 _LIBCPP_ASSERT(ready(), "match_results::operator[]() called when not ready");
5410 return __n < __matches_.size() ? __matches_[__n] : __unmatched_;
5411 }
Howard Hinnant65a3f3d2010-06-30 00:21:42 +00005412
Howard Hinnant7ca9d942010-09-23 15:13:20 +00005413 _LIBCPP_INLINE_VISIBILITY
Marshall Clow14d319a2019-04-26 17:10:03 +00005414 const_reference prefix() const
5415 {
5416 _LIBCPP_ASSERT(ready(), "match_results::prefix() called when not ready");
5417 return __prefix_;
5418 }
Howard Hinnant7ca9d942010-09-23 15:13:20 +00005419 _LIBCPP_INLINE_VISIBILITY
Marshall Clow14d319a2019-04-26 17:10:03 +00005420 const_reference suffix() const
5421 {
5422 _LIBCPP_ASSERT(ready(), "match_results::suffix() called when not ready");
5423 return __suffix_;
5424 }
Howard Hinnant65a3f3d2010-06-30 00:21:42 +00005425
Howard Hinnant7ca9d942010-09-23 15:13:20 +00005426 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f4191f2011-10-08 14:36:16 +00005427 const_iterator begin() const {return empty() ? __matches_.end() : __matches_.begin();}
Howard Hinnant7ca9d942010-09-23 15:13:20 +00005428 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant65a3f3d2010-06-30 00:21:42 +00005429 const_iterator end() const {return __matches_.end();}
Howard Hinnant7ca9d942010-09-23 15:13:20 +00005430 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f4191f2011-10-08 14:36:16 +00005431 const_iterator cbegin() const {return empty() ? __matches_.end() : __matches_.begin();}
Howard Hinnant7ca9d942010-09-23 15:13:20 +00005432 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant65a3f3d2010-06-30 00:21:42 +00005433 const_iterator cend() const {return __matches_.end();}
5434
5435 // format:
5436 template <class _OutputIter>
5437 _OutputIter
Alexander Richardsonc9637642017-11-14 11:14:25 +00005438 format(_OutputIter __output_iter, const char_type* __fmt_first,
Howard Hinnant65a3f3d2010-06-30 00:21:42 +00005439 const char_type* __fmt_last,
5440 regex_constants::match_flag_type __flags = regex_constants::format_default) const;
5441 template <class _OutputIter, class _ST, class _SA>
Howard Hinnant7ca9d942010-09-23 15:13:20 +00005442 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant65a3f3d2010-06-30 00:21:42 +00005443 _OutputIter
Alexander Richardsonc9637642017-11-14 11:14:25 +00005444 format(_OutputIter __output_iter, const basic_string<char_type, _ST, _SA>& __fmt,
Howard Hinnantcbc45252010-08-14 18:14:02 +00005445 regex_constants::match_flag_type __flags = regex_constants::format_default) const
Alexander Richardsonc9637642017-11-14 11:14:25 +00005446 {return format(__output_iter, __fmt.data(), __fmt.data() + __fmt.size(), __flags);}
Howard Hinnant65a3f3d2010-06-30 00:21:42 +00005447 template <class _ST, class _SA>
Howard Hinnant7ca9d942010-09-23 15:13:20 +00005448 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant65a3f3d2010-06-30 00:21:42 +00005449 basic_string<char_type, _ST, _SA>
5450 format(const basic_string<char_type, _ST, _SA>& __fmt,
Howard Hinnantcbc45252010-08-14 18:14:02 +00005451 regex_constants::match_flag_type __flags = regex_constants::format_default) const
5452 {
5453 basic_string<char_type, _ST, _SA> __r;
5454 format(back_inserter(__r), __fmt.data(), __fmt.data() + __fmt.size(),
5455 __flags);
5456 return __r;
5457 }
Howard Hinnant7ca9d942010-09-23 15:13:20 +00005458 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant65a3f3d2010-06-30 00:21:42 +00005459 string_type
5460 format(const char_type* __fmt,
Howard Hinnantcbc45252010-08-14 18:14:02 +00005461 regex_constants::match_flag_type __flags = regex_constants::format_default) const
5462 {
5463 string_type __r;
5464 format(back_inserter(__r), __fmt,
5465 __fmt + char_traits<char_type>::length(__fmt), __flags);
5466 return __r;
5467 }
Howard Hinnant65a3f3d2010-06-30 00:21:42 +00005468
5469 // allocator:
Howard Hinnant7ca9d942010-09-23 15:13:20 +00005470 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant65a3f3d2010-06-30 00:21:42 +00005471 allocator_type get_allocator() const {return __matches_.get_allocator();}
5472
5473 // swap:
5474 void swap(match_results& __m);
5475
Howard Hinnantc834c512011-11-29 18:15:50 +00005476 template <class _Bp, class _Ap>
Howard Hinnant7ca9d942010-09-23 15:13:20 +00005477 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant66423212010-07-14 21:14:52 +00005478 void __assign(_BidirectionalIterator __f, _BidirectionalIterator __l,
Howard Hinnantc834c512011-11-29 18:15:50 +00005479 const match_results<_Bp, _Ap>& __m, bool __no_update_pos)
Howard Hinnant66423212010-07-14 21:14:52 +00005480 {
Howard Hinnantc834c512011-11-29 18:15:50 +00005481 _Bp __mf = __m.prefix().first;
Howard Hinnant66423212010-07-14 21:14:52 +00005482 __matches_.resize(__m.size());
5483 for (size_type __i = 0; __i < __matches_.size(); ++__i)
5484 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005485 __matches_[__i].first = _VSTD::next(__f, _VSTD::distance(__mf, __m[__i].first));
5486 __matches_[__i].second = _VSTD::next(__f, _VSTD::distance(__mf, __m[__i].second));
Howard Hinnant66423212010-07-14 21:14:52 +00005487 __matches_[__i].matched = __m[__i].matched;
5488 }
5489 __unmatched_.first = __l;
5490 __unmatched_.second = __l;
5491 __unmatched_.matched = false;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005492 __prefix_.first = _VSTD::next(__f, _VSTD::distance(__mf, __m.prefix().first));
5493 __prefix_.second = _VSTD::next(__f, _VSTD::distance(__mf, __m.prefix().second));
Howard Hinnant66423212010-07-14 21:14:52 +00005494 __prefix_.matched = __m.prefix().matched;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005495 __suffix_.first = _VSTD::next(__f, _VSTD::distance(__mf, __m.suffix().first));
5496 __suffix_.second = _VSTD::next(__f, _VSTD::distance(__mf, __m.suffix().second));
Howard Hinnant66423212010-07-14 21:14:52 +00005497 __suffix_.matched = __m.suffix().matched;
Howard Hinnantd3925342010-08-16 20:21:16 +00005498 if (!__no_update_pos)
5499 __position_start_ = __prefix_.first;
Howard Hinnantb5c53a82010-12-08 21:07:55 +00005500 __ready_ = __m.ready();
Howard Hinnant66423212010-07-14 21:14:52 +00005501 }
5502
Howard Hinnantf5cb6eb2010-06-30 17:22:19 +00005503private:
5504 void __init(unsigned __s,
Howard Hinnantd3925342010-08-16 20:21:16 +00005505 _BidirectionalIterator __f, _BidirectionalIterator __l,
5506 bool __no_update_pos = false);
Howard Hinnantf5cb6eb2010-06-30 17:22:19 +00005507
5508 template <class, class> friend class basic_regex;
5509
Howard Hinnantc834c512011-11-29 18:15:50 +00005510 template <class _Bp, class _Ap, class _Cp, class _Tp>
Howard Hinnant65a3f3d2010-06-30 00:21:42 +00005511 friend
5512 bool
Howard Hinnantc834c512011-11-29 18:15:50 +00005513 regex_match(_Bp, _Bp, match_results<_Bp, _Ap>&, const basic_regex<_Cp, _Tp>&,
Howard Hinnant65a3f3d2010-06-30 00:21:42 +00005514 regex_constants::match_flag_type);
Howard Hinnant126da6a2010-07-27 22:20:32 +00005515
Howard Hinnantc834c512011-11-29 18:15:50 +00005516 template <class _Bp, class _Ap>
Howard Hinnantcbc45252010-08-14 18:14:02 +00005517 friend
5518 bool
Howard Hinnantc834c512011-11-29 18:15:50 +00005519 operator==(const match_results<_Bp, _Ap>&, const match_results<_Bp, _Ap>&);
Howard Hinnantcbc45252010-08-14 18:14:02 +00005520
Howard Hinnant126da6a2010-07-27 22:20:32 +00005521 template <class, class> friend class __lookahead;
Howard Hinnant65a3f3d2010-06-30 00:21:42 +00005522};
5523
5524template <class _BidirectionalIterator, class _Allocator>
5525match_results<_BidirectionalIterator, _Allocator>::match_results(
5526 const allocator_type& __a)
5527 : __matches_(__a),
5528 __unmatched_(),
5529 __prefix_(),
Howard Hinnantd3925342010-08-16 20:21:16 +00005530 __suffix_(),
Eric Fiseliera75ee262015-07-22 01:29:41 +00005531 __ready_(false),
5532 __position_start_()
Howard Hinnant65a3f3d2010-06-30 00:21:42 +00005533{
5534}
5535
Howard Hinnantf5cb6eb2010-06-30 17:22:19 +00005536template <class _BidirectionalIterator, class _Allocator>
5537void
5538match_results<_BidirectionalIterator, _Allocator>::__init(unsigned __s,
Howard Hinnantd3925342010-08-16 20:21:16 +00005539 _BidirectionalIterator __f, _BidirectionalIterator __l,
5540 bool __no_update_pos)
Howard Hinnantf5cb6eb2010-06-30 17:22:19 +00005541{
Howard Hinnantf5cb6eb2010-06-30 17:22:19 +00005542 __unmatched_.first = __l;
5543 __unmatched_.second = __l;
5544 __unmatched_.matched = false;
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00005545 __matches_.assign(__s, __unmatched_);
Howard Hinnantf5cb6eb2010-06-30 17:22:19 +00005546 __prefix_.first = __f;
5547 __prefix_.second = __f;
5548 __prefix_.matched = false;
Howard Hinnant5bf96132010-07-08 17:43:58 +00005549 __suffix_ = __unmatched_;
Howard Hinnantd3925342010-08-16 20:21:16 +00005550 if (!__no_update_pos)
5551 __position_start_ = __prefix_.first;
Howard Hinnantb5c53a82010-12-08 21:07:55 +00005552 __ready_ = true;
Howard Hinnantf5cb6eb2010-06-30 17:22:19 +00005553}
5554
Howard Hinnantcbc45252010-08-14 18:14:02 +00005555template <class _BidirectionalIterator, class _Allocator>
5556template <class _OutputIter>
5557_OutputIter
Alexander Richardsonc9637642017-11-14 11:14:25 +00005558match_results<_BidirectionalIterator, _Allocator>::format(_OutputIter __output_iter,
Howard Hinnantcbc45252010-08-14 18:14:02 +00005559 const char_type* __fmt_first, const char_type* __fmt_last,
5560 regex_constants::match_flag_type __flags) const
5561{
Marshall Clow14d319a2019-04-26 17:10:03 +00005562 _LIBCPP_ASSERT(ready(), "match_results::format() called when not ready");
Howard Hinnantcbc45252010-08-14 18:14:02 +00005563 if (__flags & regex_constants::format_sed)
5564 {
5565 for (; __fmt_first != __fmt_last; ++__fmt_first)
5566 {
5567 if (*__fmt_first == '&')
Alexander Richardsonc9637642017-11-14 11:14:25 +00005568 __output_iter = _VSTD::copy(__matches_[0].first, __matches_[0].second,
5569 __output_iter);
Howard Hinnantcbc45252010-08-14 18:14:02 +00005570 else if (*__fmt_first == '\\' && __fmt_first + 1 != __fmt_last)
5571 {
5572 ++__fmt_first;
5573 if ('0' <= *__fmt_first && *__fmt_first <= '9')
5574 {
5575 size_t __i = *__fmt_first - '0';
Alexander Richardsonc9637642017-11-14 11:14:25 +00005576 __output_iter = _VSTD::copy((*this)[__i].first,
5577 (*this)[__i].second, __output_iter);
Howard Hinnantcbc45252010-08-14 18:14:02 +00005578 }
5579 else
5580 {
Alexander Richardsonc9637642017-11-14 11:14:25 +00005581 *__output_iter = *__fmt_first;
5582 ++__output_iter;
Howard Hinnantcbc45252010-08-14 18:14:02 +00005583 }
5584 }
5585 else
5586 {
Alexander Richardsonc9637642017-11-14 11:14:25 +00005587 *__output_iter = *__fmt_first;
5588 ++__output_iter;
Howard Hinnantcbc45252010-08-14 18:14:02 +00005589 }
5590 }
5591 }
5592 else
5593 {
5594 for (; __fmt_first != __fmt_last; ++__fmt_first)
5595 {
5596 if (*__fmt_first == '$' && __fmt_first + 1 != __fmt_last)
5597 {
5598 switch (__fmt_first[1])
5599 {
5600 case '$':
Alexander Richardsonc9637642017-11-14 11:14:25 +00005601 *__output_iter = *++__fmt_first;
5602 ++__output_iter;
Howard Hinnantcbc45252010-08-14 18:14:02 +00005603 break;
5604 case '&':
5605 ++__fmt_first;
Alexander Richardsonc9637642017-11-14 11:14:25 +00005606 __output_iter = _VSTD::copy(__matches_[0].first, __matches_[0].second,
5607 __output_iter);
Howard Hinnantcbc45252010-08-14 18:14:02 +00005608 break;
5609 case '`':
5610 ++__fmt_first;
Alexander Richardsonc9637642017-11-14 11:14:25 +00005611 __output_iter = _VSTD::copy(__prefix_.first, __prefix_.second, __output_iter);
Howard Hinnantcbc45252010-08-14 18:14:02 +00005612 break;
5613 case '\'':
5614 ++__fmt_first;
Alexander Richardsonc9637642017-11-14 11:14:25 +00005615 __output_iter = _VSTD::copy(__suffix_.first, __suffix_.second, __output_iter);
Howard Hinnantcbc45252010-08-14 18:14:02 +00005616 break;
5617 default:
5618 if ('0' <= __fmt_first[1] && __fmt_first[1] <= '9')
5619 {
5620 ++__fmt_first;
Marshall Clow266b5ec2017-10-19 22:10:41 +00005621 size_t __idx = *__fmt_first - '0';
Howard Hinnantcbc45252010-08-14 18:14:02 +00005622 if (__fmt_first + 1 != __fmt_last &&
5623 '0' <= __fmt_first[1] && __fmt_first[1] <= '9')
5624 {
5625 ++__fmt_first;
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05005626 if (__idx >= numeric_limits<size_t>::max() / 10)
Marshall Clow266b5ec2017-10-19 22:10:41 +00005627 __throw_regex_error<regex_constants::error_escape>();
5628 __idx = 10 * __idx + *__fmt_first - '0';
Howard Hinnantcbc45252010-08-14 18:14:02 +00005629 }
Alexander Richardsonc9637642017-11-14 11:14:25 +00005630 __output_iter = _VSTD::copy((*this)[__idx].first,
5631 (*this)[__idx].second, __output_iter);
Howard Hinnantcbc45252010-08-14 18:14:02 +00005632 }
5633 else
5634 {
Alexander Richardsonc9637642017-11-14 11:14:25 +00005635 *__output_iter = *__fmt_first;
5636 ++__output_iter;
Howard Hinnantcbc45252010-08-14 18:14:02 +00005637 }
5638 break;
5639 }
5640 }
5641 else
5642 {
Alexander Richardsonc9637642017-11-14 11:14:25 +00005643 *__output_iter = *__fmt_first;
5644 ++__output_iter;
Howard Hinnantcbc45252010-08-14 18:14:02 +00005645 }
5646 }
5647 }
Alexander Richardsonc9637642017-11-14 11:14:25 +00005648 return __output_iter;
Howard Hinnantcbc45252010-08-14 18:14:02 +00005649}
5650
5651template <class _BidirectionalIterator, class _Allocator>
5652void
5653match_results<_BidirectionalIterator, _Allocator>::swap(match_results& __m)
5654{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005655 using _VSTD::swap;
Howard Hinnantcbc45252010-08-14 18:14:02 +00005656 swap(__matches_, __m.__matches_);
5657 swap(__unmatched_, __m.__unmatched_);
5658 swap(__prefix_, __m.__prefix_);
5659 swap(__suffix_, __m.__suffix_);
Howard Hinnantd3925342010-08-16 20:21:16 +00005660 swap(__position_start_, __m.__position_start_);
Howard Hinnantb5c53a82010-12-08 21:07:55 +00005661 swap(__ready_, __m.__ready_);
Howard Hinnantcbc45252010-08-14 18:14:02 +00005662}
5663
Howard Hinnant65a3f3d2010-06-30 00:21:42 +00005664template <class _BidirectionalIterator, class _Allocator>
Howard Hinnantcbc45252010-08-14 18:14:02 +00005665bool
5666operator==(const match_results<_BidirectionalIterator, _Allocator>& __x,
5667 const match_results<_BidirectionalIterator, _Allocator>& __y)
5668{
Howard Hinnantb5c53a82010-12-08 21:07:55 +00005669 if (__x.__ready_ != __y.__ready_)
5670 return false;
5671 if (!__x.__ready_)
5672 return true;
Howard Hinnantcbc45252010-08-14 18:14:02 +00005673 return __x.__matches_ == __y.__matches_ &&
5674 __x.__prefix_ == __y.__prefix_ &&
Howard Hinnantb5c53a82010-12-08 21:07:55 +00005675 __x.__suffix_ == __y.__suffix_;
Howard Hinnantcbc45252010-08-14 18:14:02 +00005676}
Howard Hinnant65a3f3d2010-06-30 00:21:42 +00005677
5678template <class _BidirectionalIterator, class _Allocator>
Howard Hinnantcbc45252010-08-14 18:14:02 +00005679inline _LIBCPP_INLINE_VISIBILITY
5680bool
5681operator!=(const match_results<_BidirectionalIterator, _Allocator>& __x,
5682 const match_results<_BidirectionalIterator, _Allocator>& __y)
5683{
5684 return !(__x == __y);
5685}
Howard Hinnant65a3f3d2010-06-30 00:21:42 +00005686
5687template <class _BidirectionalIterator, class _Allocator>
Howard Hinnantcbc45252010-08-14 18:14:02 +00005688inline _LIBCPP_INLINE_VISIBILITY
5689void
5690swap(match_results<_BidirectionalIterator, _Allocator>& __x,
5691 match_results<_BidirectionalIterator, _Allocator>& __y)
5692{
5693 __x.swap(__y);
5694}
Howard Hinnant65a3f3d2010-06-30 00:21:42 +00005695
5696// regex_search
5697
Howard Hinnantf5cb6eb2010-06-30 17:22:19 +00005698template <class _CharT, class _Traits>
Howard Hinnant70d27852010-07-27 01:25:38 +00005699template <class _Allocator>
Howard Hinnantf5cb6eb2010-06-30 17:22:19 +00005700bool
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00005701basic_regex<_CharT, _Traits>::__match_at_start_ecma(
Howard Hinnant70d27852010-07-27 01:25:38 +00005702 const _CharT* __first, const _CharT* __last,
5703 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnant066ba512011-03-26 20:02:27 +00005704 regex_constants::match_flag_type __flags, bool __at_first) const
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00005705{
Howard Hinnant70d27852010-07-27 01:25:38 +00005706 vector<__state> __states;
Howard Hinnant70d27852010-07-27 01:25:38 +00005707 __node* __st = __start_.get();
5708 if (__st)
5709 {
Marshall Clow8db143c2015-01-28 22:22:35 +00005710 sub_match<const _CharT*> __unmatched;
5711 __unmatched.first = __last;
5712 __unmatched.second = __last;
5713 __unmatched.matched = false;
5714
Howard Hinnant70d27852010-07-27 01:25:38 +00005715 __states.push_back(__state());
5716 __states.back().__do_ = 0;
5717 __states.back().__first_ = __first;
5718 __states.back().__current_ = __first;
5719 __states.back().__last_ = __last;
Marshall Clow8db143c2015-01-28 22:22:35 +00005720 __states.back().__sub_matches_.resize(mark_count(), __unmatched);
Howard Hinnant70d27852010-07-27 01:25:38 +00005721 __states.back().__loop_data_.resize(__loop_count());
5722 __states.back().__node_ = __st;
5723 __states.back().__flags_ = __flags;
Howard Hinnant066ba512011-03-26 20:02:27 +00005724 __states.back().__at_first_ = __at_first;
Marshall Clowd39d21d2017-09-12 17:56:59 +00005725 int __counter = 0;
5726 int __length = __last - __first;
Howard Hinnant70d27852010-07-27 01:25:38 +00005727 do
5728 {
Marshall Clowd39d21d2017-09-12 17:56:59 +00005729 ++__counter;
5730 if (__counter % _LIBCPP_REGEX_COMPLEXITY_FACTOR == 0 &&
5731 __counter / _LIBCPP_REGEX_COMPLEXITY_FACTOR >= __length)
5732 __throw_regex_error<regex_constants::error_complexity>();
Howard Hinnant70d27852010-07-27 01:25:38 +00005733 __state& __s = __states.back();
5734 if (__s.__node_)
5735 __s.__node_->__exec(__s);
5736 switch (__s.__do_)
5737 {
5738 case __state::__end_state:
Tim Shen11113f52016-10-27 21:40:34 +00005739 if ((__flags & regex_constants::match_not_null) &&
Tim Shend5f175a2016-10-21 20:41:47 +00005740 __s.__current_ == __first)
5741 {
5742 __states.pop_back();
5743 break;
5744 }
Tim Shen11113f52016-10-27 21:40:34 +00005745 if ((__flags & regex_constants::__full_match) &&
5746 __s.__current_ != __last)
5747 {
5748 __states.pop_back();
5749 break;
5750 }
Howard Hinnant70d27852010-07-27 01:25:38 +00005751 __m.__matches_[0].first = __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005752 __m.__matches_[0].second = _VSTD::next(__first, __s.__current_ - __first);
Howard Hinnant70d27852010-07-27 01:25:38 +00005753 __m.__matches_[0].matched = true;
5754 for (unsigned __i = 0; __i < __s.__sub_matches_.size(); ++__i)
5755 __m.__matches_[__i+1] = __s.__sub_matches_[__i];
5756 return true;
5757 case __state::__accept_and_consume:
5758 case __state::__repeat:
5759 case __state::__accept_but_not_consume:
5760 break;
5761 case __state::__split:
5762 {
5763 __state __snext = __s;
5764 __s.__node_->__exec_split(true, __s);
5765 __snext.__node_->__exec_split(false, __snext);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005766 __states.push_back(_VSTD::move(__snext));
Howard Hinnant70d27852010-07-27 01:25:38 +00005767 }
5768 break;
5769 case __state::__reject:
5770 __states.pop_back();
5771 break;
5772 default:
Marshall Clowc8ccc292015-07-28 13:30:47 +00005773 __throw_regex_error<regex_constants::__re_err_unknown>();
Howard Hinnant70d27852010-07-27 01:25:38 +00005774 break;
Howard Hinnant72f73582010-08-11 17:04:31 +00005775
Howard Hinnant70d27852010-07-27 01:25:38 +00005776 }
5777 } while (!__states.empty());
5778 }
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00005779 return false;
5780}
5781
5782template <class _CharT, class _Traits>
Howard Hinnant66423212010-07-14 21:14:52 +00005783template <class _Allocator>
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00005784bool
5785basic_regex<_CharT, _Traits>::__match_at_start_posix_nosubs(
5786 const _CharT* __first, const _CharT* __last,
Howard Hinnant66423212010-07-14 21:14:52 +00005787 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnant066ba512011-03-26 20:02:27 +00005788 regex_constants::match_flag_type __flags, bool __at_first) const
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00005789{
Howard Hinnantaa0874c2010-07-12 15:51:17 +00005790 deque<__state> __states;
Howard Hinnant66423212010-07-14 21:14:52 +00005791 ptrdiff_t __highest_j = 0;
Howard Hinnantc834c512011-11-29 18:15:50 +00005792 ptrdiff_t _Np = _VSTD::distance(__first, __last);
Howard Hinnantaa0874c2010-07-12 15:51:17 +00005793 __node* __st = __start_.get();
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00005794 if (__st)
5795 {
Howard Hinnantaa0874c2010-07-12 15:51:17 +00005796 __states.push_back(__state());
Howard Hinnantaa0874c2010-07-12 15:51:17 +00005797 __states.back().__do_ = 0;
5798 __states.back().__first_ = __first;
5799 __states.back().__current_ = __first;
5800 __states.back().__last_ = __last;
5801 __states.back().__loop_data_.resize(__loop_count());
5802 __states.back().__node_ = __st;
5803 __states.back().__flags_ = __flags;
Howard Hinnant066ba512011-03-26 20:02:27 +00005804 __states.back().__at_first_ = __at_first;
Howard Hinnant5d4aaa42010-07-14 15:45:11 +00005805 bool __matched = false;
Marshall Clowd39d21d2017-09-12 17:56:59 +00005806 int __counter = 0;
5807 int __length = __last - __first;
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00005808 do
5809 {
Marshall Clowd39d21d2017-09-12 17:56:59 +00005810 ++__counter;
5811 if (__counter % _LIBCPP_REGEX_COMPLEXITY_FACTOR == 0 &&
5812 __counter / _LIBCPP_REGEX_COMPLEXITY_FACTOR >= __length)
5813 __throw_regex_error<regex_constants::error_complexity>();
Howard Hinnantaa0874c2010-07-12 15:51:17 +00005814 __state& __s = __states.back();
5815 if (__s.__node_)
5816 __s.__node_->__exec(__s);
5817 switch (__s.__do_)
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00005818 {
Howard Hinnantaa0874c2010-07-12 15:51:17 +00005819 case __state::__end_state:
Tim Shen11113f52016-10-27 21:40:34 +00005820 if ((__flags & regex_constants::match_not_null) &&
Tim Shend5f175a2016-10-21 20:41:47 +00005821 __s.__current_ == __first)
5822 {
5823 __states.pop_back();
5824 break;
5825 }
Tim Shen11113f52016-10-27 21:40:34 +00005826 if ((__flags & regex_constants::__full_match) &&
5827 __s.__current_ != __last)
5828 {
5829 __states.pop_back();
5830 break;
5831 }
Howard Hinnantebbc2b62010-07-27 17:24:17 +00005832 if (!__matched || __highest_j < __s.__current_ - __s.__first_)
Howard Hinnant5d4aaa42010-07-14 15:45:11 +00005833 __highest_j = __s.__current_ - __s.__first_;
Howard Hinnantebbc2b62010-07-27 17:24:17 +00005834 __matched = true;
Howard Hinnantc834c512011-11-29 18:15:50 +00005835 if (__highest_j == _Np)
Howard Hinnantaa0874c2010-07-12 15:51:17 +00005836 __states.clear();
5837 else
5838 __states.pop_back();
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00005839 break;
Howard Hinnantaa0874c2010-07-12 15:51:17 +00005840 case __state::__consume_input:
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00005841 break;
Howard Hinnantaa0874c2010-07-12 15:51:17 +00005842 case __state::__accept_and_consume:
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005843 __states.push_front(_VSTD::move(__s));
Howard Hinnantaa0874c2010-07-12 15:51:17 +00005844 __states.pop_back();
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00005845 break;
Howard Hinnantaa0874c2010-07-12 15:51:17 +00005846 case __state::__repeat:
5847 case __state::__accept_but_not_consume:
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00005848 break;
Howard Hinnantaa0874c2010-07-12 15:51:17 +00005849 case __state::__split:
5850 {
5851 __state __snext = __s;
5852 __s.__node_->__exec_split(true, __s);
5853 __snext.__node_->__exec_split(false, __snext);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005854 __states.push_back(_VSTD::move(__snext));
Howard Hinnantaa0874c2010-07-12 15:51:17 +00005855 }
5856 break;
5857 case __state::__reject:
5858 __states.pop_back();
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00005859 break;
5860 default:
Marshall Clowc8ccc292015-07-28 13:30:47 +00005861 __throw_regex_error<regex_constants::__re_err_unknown>();
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00005862 break;
5863 }
Howard Hinnantaa0874c2010-07-12 15:51:17 +00005864 } while (!__states.empty());
Howard Hinnant5d4aaa42010-07-14 15:45:11 +00005865 if (__matched)
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00005866 {
5867 __m.__matches_[0].first = __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005868 __m.__matches_[0].second = _VSTD::next(__first, __highest_j);
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00005869 __m.__matches_[0].matched = true;
5870 return true;
5871 }
5872 }
5873 return false;
5874}
5875
5876template <class _CharT, class _Traits>
Howard Hinnant66423212010-07-14 21:14:52 +00005877template <class _Allocator>
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00005878bool
5879basic_regex<_CharT, _Traits>::__match_at_start_posix_subs(
Howard Hinnant66423212010-07-14 21:14:52 +00005880 const _CharT* __first, const _CharT* __last,
5881 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnant066ba512011-03-26 20:02:27 +00005882 regex_constants::match_flag_type __flags, bool __at_first) const
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00005883{
Howard Hinnantaa0874c2010-07-12 15:51:17 +00005884 vector<__state> __states;
Howard Hinnantaa0874c2010-07-12 15:51:17 +00005885 __state __best_state;
Howard Hinnant66423212010-07-14 21:14:52 +00005886 ptrdiff_t __j = 0;
5887 ptrdiff_t __highest_j = 0;
Howard Hinnantc834c512011-11-29 18:15:50 +00005888 ptrdiff_t _Np = _VSTD::distance(__first, __last);
Howard Hinnantaa0874c2010-07-12 15:51:17 +00005889 __node* __st = __start_.get();
Howard Hinnant5bf96132010-07-08 17:43:58 +00005890 if (__st)
5891 {
Marshall Clow8db143c2015-01-28 22:22:35 +00005892 sub_match<const _CharT*> __unmatched;
5893 __unmatched.first = __last;
5894 __unmatched.second = __last;
5895 __unmatched.matched = false;
5896
Howard Hinnantaa0874c2010-07-12 15:51:17 +00005897 __states.push_back(__state());
5898 __states.back().__do_ = 0;
5899 __states.back().__first_ = __first;
5900 __states.back().__current_ = __first;
5901 __states.back().__last_ = __last;
Marshall Clow8db143c2015-01-28 22:22:35 +00005902 __states.back().__sub_matches_.resize(mark_count(), __unmatched);
Howard Hinnantaa0874c2010-07-12 15:51:17 +00005903 __states.back().__loop_data_.resize(__loop_count());
5904 __states.back().__node_ = __st;
5905 __states.back().__flags_ = __flags;
Howard Hinnant066ba512011-03-26 20:02:27 +00005906 __states.back().__at_first_ = __at_first;
Howard Hinnant66423212010-07-14 21:14:52 +00005907 const _CharT* __current = __first;
Howard Hinnantaa0874c2010-07-12 15:51:17 +00005908 bool __matched = false;
Marshall Clowd39d21d2017-09-12 17:56:59 +00005909 int __counter = 0;
5910 int __length = __last - __first;
Howard Hinnant5bf96132010-07-08 17:43:58 +00005911 do
5912 {
Marshall Clowd39d21d2017-09-12 17:56:59 +00005913 ++__counter;
5914 if (__counter % _LIBCPP_REGEX_COMPLEXITY_FACTOR == 0 &&
5915 __counter / _LIBCPP_REGEX_COMPLEXITY_FACTOR >= __length)
5916 __throw_regex_error<regex_constants::error_complexity>();
Howard Hinnantaa0874c2010-07-12 15:51:17 +00005917 __state& __s = __states.back();
5918 if (__s.__node_)
5919 __s.__node_->__exec(__s);
5920 switch (__s.__do_)
Howard Hinnant5bf96132010-07-08 17:43:58 +00005921 {
Howard Hinnantaa0874c2010-07-12 15:51:17 +00005922 case __state::__end_state:
Tim Shen11113f52016-10-27 21:40:34 +00005923 if ((__flags & regex_constants::match_not_null) &&
Tim Shend5f175a2016-10-21 20:41:47 +00005924 __s.__current_ == __first)
5925 {
5926 __states.pop_back();
5927 break;
5928 }
Tim Shen11113f52016-10-27 21:40:34 +00005929 if ((__flags & regex_constants::__full_match) &&
5930 __s.__current_ != __last)
5931 {
5932 __states.pop_back();
5933 break;
5934 }
Howard Hinnantebbc2b62010-07-27 17:24:17 +00005935 if (!__matched || __highest_j < __s.__current_ - __s.__first_)
Howard Hinnant5bf96132010-07-08 17:43:58 +00005936 {
Howard Hinnantebbc2b62010-07-27 17:24:17 +00005937 __highest_j = __s.__current_ - __s.__first_;
Howard Hinnantaa0874c2010-07-12 15:51:17 +00005938 __best_state = __s;
Howard Hinnant5bf96132010-07-08 17:43:58 +00005939 }
Howard Hinnantebbc2b62010-07-27 17:24:17 +00005940 __matched = true;
Howard Hinnantc834c512011-11-29 18:15:50 +00005941 if (__highest_j == _Np)
Howard Hinnantebbc2b62010-07-27 17:24:17 +00005942 __states.clear();
5943 else
5944 __states.pop_back();
Howard Hinnant5bf96132010-07-08 17:43:58 +00005945 break;
Howard Hinnantaa0874c2010-07-12 15:51:17 +00005946 case __state::__accept_and_consume:
Howard Hinnant2a315e32010-07-12 18:16:05 +00005947 __j += __s.__current_ - __current;
5948 __current = __s.__current_;
Howard Hinnant5bf96132010-07-08 17:43:58 +00005949 break;
Howard Hinnantaa0874c2010-07-12 15:51:17 +00005950 case __state::__repeat:
5951 case __state::__accept_but_not_consume:
Howard Hinnant5bf96132010-07-08 17:43:58 +00005952 break;
Howard Hinnantaa0874c2010-07-12 15:51:17 +00005953 case __state::__split:
5954 {
5955 __state __snext = __s;
5956 __s.__node_->__exec_split(true, __s);
5957 __snext.__node_->__exec_split(false, __snext);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005958 __states.push_back(_VSTD::move(__snext));
Howard Hinnantaa0874c2010-07-12 15:51:17 +00005959 }
5960 break;
5961 case __state::__reject:
5962 __states.pop_back();
Howard Hinnant5bf96132010-07-08 17:43:58 +00005963 break;
5964 default:
Marshall Clowc8ccc292015-07-28 13:30:47 +00005965 __throw_regex_error<regex_constants::__re_err_unknown>();
Howard Hinnant5bf96132010-07-08 17:43:58 +00005966 break;
5967 }
Howard Hinnantaa0874c2010-07-12 15:51:17 +00005968 } while (!__states.empty());
5969 if (__matched)
Howard Hinnant5bf96132010-07-08 17:43:58 +00005970 {
5971 __m.__matches_[0].first = __first;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00005972 __m.__matches_[0].second = _VSTD::next(__first, __highest_j);
Howard Hinnant5bf96132010-07-08 17:43:58 +00005973 __m.__matches_[0].matched = true;
Howard Hinnantaa0874c2010-07-12 15:51:17 +00005974 for (unsigned __i = 0; __i < __best_state.__sub_matches_.size(); ++__i)
5975 __m.__matches_[__i+1] = __best_state.__sub_matches_[__i];
Howard Hinnant5bf96132010-07-08 17:43:58 +00005976 return true;
5977 }
5978 }
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00005979 return false;
5980}
5981
5982template <class _CharT, class _Traits>
Howard Hinnant66423212010-07-14 21:14:52 +00005983template <class _Allocator>
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00005984bool
5985basic_regex<_CharT, _Traits>::__match_at_start(
Howard Hinnant66423212010-07-14 21:14:52 +00005986 const _CharT* __first, const _CharT* __last,
5987 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnant066ba512011-03-26 20:02:27 +00005988 regex_constants::match_flag_type __flags, bool __at_first) const
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00005989{
Marshall Clow88a30872019-03-28 17:30:23 +00005990 if (__get_grammar(__flags_) == ECMAScript)
Howard Hinnant066ba512011-03-26 20:02:27 +00005991 return __match_at_start_ecma(__first, __last, __m, __flags, __at_first);
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00005992 if (mark_count() == 0)
Howard Hinnant066ba512011-03-26 20:02:27 +00005993 return __match_at_start_posix_nosubs(__first, __last, __m, __flags, __at_first);
5994 return __match_at_start_posix_subs(__first, __last, __m, __flags, __at_first);
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00005995}
5996
5997template <class _CharT, class _Traits>
Howard Hinnant66423212010-07-14 21:14:52 +00005998template <class _Allocator>
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00005999bool
Howard Hinnantf5cb6eb2010-06-30 17:22:19 +00006000basic_regex<_CharT, _Traits>::__search(
Howard Hinnant66423212010-07-14 21:14:52 +00006001 const _CharT* __first, const _CharT* __last,
6002 match_results<const _CharT*, _Allocator>& __m,
Howard Hinnantf5cb6eb2010-06-30 17:22:19 +00006003 regex_constants::match_flag_type __flags) const
6004{
Diogo Sampaio300ade72020-04-30 23:34:01 +01006005 if (__flags & regex_constants::match_prev_avail)
6006 __flags &= ~(regex_constants::match_not_bol | regex_constants::match_not_bow);
6007
Howard Hinnantd3925342010-08-16 20:21:16 +00006008 __m.__init(1 + mark_count(), __first, __last,
6009 __flags & regex_constants::__no_update_pos);
Louis Dionne173f29e2019-05-29 16:01:36 +00006010 if (__match_at_start(__first, __last, __m, __flags,
Howard Hinnant38d14f72013-07-09 17:29:09 +00006011 !(__flags & regex_constants::__no_update_pos)))
Howard Hinnantf5cb6eb2010-06-30 17:22:19 +00006012 {
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00006013 __m.__prefix_.second = __m[0].first;
6014 __m.__prefix_.matched = __m.__prefix_.first != __m.__prefix_.second;
6015 __m.__suffix_.first = __m[0].second;
6016 __m.__suffix_.matched = __m.__suffix_.first != __m.__suffix_.second;
6017 return true;
6018 }
Howard Hinnant22cf4862010-07-29 01:15:27 +00006019 if (__first != __last && !(__flags & regex_constants::match_continuous))
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00006020 {
Howard Hinnant6b2602a2010-07-29 15:17:28 +00006021 __flags |= regex_constants::match_prev_avail;
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00006022 for (++__first; __first != __last; ++__first)
Howard Hinnantf5cb6eb2010-06-30 17:22:19 +00006023 {
Howard Hinnant6b2602a2010-07-29 15:17:28 +00006024 __m.__matches_.assign(__m.size(), __m.__unmatched_);
Howard Hinnant066ba512011-03-26 20:02:27 +00006025 if (__match_at_start(__first, __last, __m, __flags, false))
Howard Hinnantf5cb6eb2010-06-30 17:22:19 +00006026 {
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00006027 __m.__prefix_.second = __m[0].first;
6028 __m.__prefix_.matched = __m.__prefix_.first != __m.__prefix_.second;
6029 __m.__suffix_.first = __m[0].second;
6030 __m.__suffix_.matched = __m.__suffix_.first != __m.__suffix_.second;
6031 return true;
Howard Hinnantf5cb6eb2010-06-30 17:22:19 +00006032 }
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00006033 __m.__matches_.assign(__m.size(), __m.__unmatched_);
Howard Hinnantf5cb6eb2010-06-30 17:22:19 +00006034 }
6035 }
Howard Hinnant66bcf7e2010-07-07 19:14:52 +00006036 __m.__matches_.clear();
6037 return false;
Howard Hinnantf5cb6eb2010-06-30 17:22:19 +00006038}
6039
Howard Hinnant65a3f3d2010-06-30 00:21:42 +00006040template <class _BidirectionalIterator, class _Allocator, class _CharT, class _Traits>
Howard Hinnantf5cb6eb2010-06-30 17:22:19 +00006041inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant65a3f3d2010-06-30 00:21:42 +00006042bool
6043regex_search(_BidirectionalIterator __first, _BidirectionalIterator __last,
6044 match_results<_BidirectionalIterator, _Allocator>& __m,
6045 const basic_regex<_CharT, _Traits>& __e,
Howard Hinnantf5cb6eb2010-06-30 17:22:19 +00006046 regex_constants::match_flag_type __flags = regex_constants::match_default)
6047{
Howard Hinnant1e5de642013-07-11 15:32:55 +00006048 int __offset = (__flags & regex_constants::match_prev_avail) ? 1 : 0;
6049 basic_string<_CharT> __s(_VSTD::prev(__first, __offset), __last);
Howard Hinnant66423212010-07-14 21:14:52 +00006050 match_results<const _CharT*> __mc;
Howard Hinnant1e5de642013-07-11 15:32:55 +00006051 bool __r = __e.__search(__s.data() + __offset, __s.data() + __s.size(), __mc, __flags);
Howard Hinnantd3925342010-08-16 20:21:16 +00006052 __m.__assign(__first, __last, __mc, __flags & regex_constants::__no_update_pos);
Howard Hinnant66423212010-07-14 21:14:52 +00006053 return __r;
6054}
6055
Howard Hinnant4018c482013-06-29 23:45:43 +00006056template <class _Iter, class _Allocator, class _CharT, class _Traits>
6057inline _LIBCPP_INLINE_VISIBILITY
6058bool
6059regex_search(__wrap_iter<_Iter> __first,
6060 __wrap_iter<_Iter> __last,
6061 match_results<__wrap_iter<_Iter>, _Allocator>& __m,
6062 const basic_regex<_CharT, _Traits>& __e,
6063 regex_constants::match_flag_type __flags = regex_constants::match_default)
6064{
6065 match_results<const _CharT*> __mc;
6066 bool __r = __e.__search(__first.base(), __last.base(), __mc, __flags);
6067 __m.__assign(__first, __last, __mc, __flags & regex_constants::__no_update_pos);
6068 return __r;
6069}
6070
Howard Hinnant66423212010-07-14 21:14:52 +00006071template <class _Allocator, class _CharT, class _Traits>
6072inline _LIBCPP_INLINE_VISIBILITY
6073bool
6074regex_search(const _CharT* __first, const _CharT* __last,
6075 match_results<const _CharT*, _Allocator>& __m,
6076 const basic_regex<_CharT, _Traits>& __e,
6077 regex_constants::match_flag_type __flags = regex_constants::match_default)
6078{
Howard Hinnantf5cb6eb2010-06-30 17:22:19 +00006079 return __e.__search(__first, __last, __m, __flags);
6080}
Howard Hinnant65a3f3d2010-06-30 00:21:42 +00006081
6082template <class _BidirectionalIterator, class _CharT, class _Traits>
6083inline _LIBCPP_INLINE_VISIBILITY
6084bool
6085regex_search(_BidirectionalIterator __first, _BidirectionalIterator __last,
6086 const basic_regex<_CharT, _Traits>& __e,
6087 regex_constants::match_flag_type __flags = regex_constants::match_default)
6088{
Howard Hinnant66423212010-07-14 21:14:52 +00006089 basic_string<_CharT> __s(__first, __last);
6090 match_results<const _CharT*> __mc;
6091 return __e.__search(__s.data(), __s.data() + __s.size(), __mc, __flags);
6092}
6093
6094template <class _CharT, class _Traits>
6095inline _LIBCPP_INLINE_VISIBILITY
6096bool
6097regex_search(const _CharT* __first, const _CharT* __last,
6098 const basic_regex<_CharT, _Traits>& __e,
6099 regex_constants::match_flag_type __flags = regex_constants::match_default)
6100{
6101 match_results<const _CharT*> __mc;
6102 return __e.__search(__first, __last, __mc, __flags);
Howard Hinnant65a3f3d2010-06-30 00:21:42 +00006103}
6104
6105template <class _CharT, class _Allocator, class _Traits>
6106inline _LIBCPP_INLINE_VISIBILITY
6107bool
6108regex_search(const _CharT* __str, match_results<const _CharT*, _Allocator>& __m,
6109 const basic_regex<_CharT, _Traits>& __e,
6110 regex_constants::match_flag_type __flags = regex_constants::match_default)
6111{
Howard Hinnant66423212010-07-14 21:14:52 +00006112 return __e.__search(__str, __str + _Traits::length(__str), __m, __flags);
Howard Hinnant65a3f3d2010-06-30 00:21:42 +00006113}
6114
6115template <class _CharT, class _Traits>
6116inline _LIBCPP_INLINE_VISIBILITY
6117bool
6118regex_search(const _CharT* __str, const basic_regex<_CharT, _Traits>& __e,
6119 regex_constants::match_flag_type __flags = regex_constants::match_default)
6120{
Howard Hinnant66423212010-07-14 21:14:52 +00006121 match_results<const _CharT*> __m;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00006122 return _VSTD::regex_search(__str, __m, __e, __flags);
Howard Hinnant65a3f3d2010-06-30 00:21:42 +00006123}
6124
6125template <class _ST, class _SA, class _CharT, class _Traits>
6126inline _LIBCPP_INLINE_VISIBILITY
6127bool
6128regex_search(const basic_string<_CharT, _ST, _SA>& __s,
6129 const basic_regex<_CharT, _Traits>& __e,
6130 regex_constants::match_flag_type __flags = regex_constants::match_default)
6131{
Howard Hinnant66423212010-07-14 21:14:52 +00006132 match_results<const _CharT*> __mc;
6133 return __e.__search(__s.data(), __s.data() + __s.size(), __mc, __flags);
Howard Hinnant65a3f3d2010-06-30 00:21:42 +00006134}
6135
6136template <class _ST, class _SA, class _Allocator, class _CharT, class _Traits>
6137inline _LIBCPP_INLINE_VISIBILITY
6138bool
6139regex_search(const basic_string<_CharT, _ST, _SA>& __s,
6140 match_results<typename basic_string<_CharT, _ST, _SA>::const_iterator, _Allocator>& __m,
6141 const basic_regex<_CharT, _Traits>& __e,
6142 regex_constants::match_flag_type __flags = regex_constants::match_default)
6143{
Howard Hinnant66423212010-07-14 21:14:52 +00006144 match_results<const _CharT*> __mc;
6145 bool __r = __e.__search(__s.data(), __s.data() + __s.size(), __mc, __flags);
Howard Hinnantd3925342010-08-16 20:21:16 +00006146 __m.__assign(__s.begin(), __s.end(), __mc, __flags & regex_constants::__no_update_pos);
Howard Hinnant66423212010-07-14 21:14:52 +00006147 return __r;
Howard Hinnant65a3f3d2010-06-30 00:21:42 +00006148}
6149
Marshall Clow8c950072014-02-19 21:21:11 +00006150#if _LIBCPP_STD_VER > 11
6151template <class _ST, class _SA, class _Ap, class _Cp, class _Tp>
6152bool
6153regex_search(const basic_string<_Cp, _ST, _SA>&& __s,
6154 match_results<typename basic_string<_Cp, _ST, _SA>::const_iterator, _Ap>&,
6155 const basic_regex<_Cp, _Tp>& __e,
Louis Dionne173f29e2019-05-29 16:01:36 +00006156 regex_constants::match_flag_type __flags = regex_constants::match_default) = delete;
Marshall Clow8c950072014-02-19 21:21:11 +00006157#endif
6158
Howard Hinnant65a3f3d2010-06-30 00:21:42 +00006159// regex_match
6160
6161template <class _BidirectionalIterator, class _Allocator, class _CharT, class _Traits>
6162bool
6163regex_match(_BidirectionalIterator __first, _BidirectionalIterator __last,
6164 match_results<_BidirectionalIterator, _Allocator>& __m,
6165 const basic_regex<_CharT, _Traits>& __e,
6166 regex_constants::match_flag_type __flags = regex_constants::match_default)
6167{
Tim Shen11113f52016-10-27 21:40:34 +00006168 bool __r = _VSTD::regex_search(
6169 __first, __last, __m, __e,
6170 __flags | regex_constants::match_continuous |
6171 regex_constants::__full_match);
Howard Hinnant65a3f3d2010-06-30 00:21:42 +00006172 if (__r)
6173 {
6174 __r = !__m.suffix().matched;
6175 if (!__r)
6176 __m.__matches_.clear();
6177 }
6178 return __r;
6179}
6180
6181template <class _BidirectionalIterator, class _CharT, class _Traits>
6182inline _LIBCPP_INLINE_VISIBILITY
6183bool
6184regex_match(_BidirectionalIterator __first, _BidirectionalIterator __last,
6185 const basic_regex<_CharT, _Traits>& __e,
6186 regex_constants::match_flag_type __flags = regex_constants::match_default)
6187{
6188 match_results<_BidirectionalIterator> __m;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00006189 return _VSTD::regex_match(__first, __last, __m, __e, __flags);
Howard Hinnant65a3f3d2010-06-30 00:21:42 +00006190}
6191
6192template <class _CharT, class _Allocator, class _Traits>
6193inline _LIBCPP_INLINE_VISIBILITY
6194bool
6195regex_match(const _CharT* __str, match_results<const _CharT*, _Allocator>& __m,
6196 const basic_regex<_CharT, _Traits>& __e,
6197 regex_constants::match_flag_type __flags = regex_constants::match_default)
6198{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00006199 return _VSTD::regex_match(__str, __str + _Traits::length(__str), __m, __e, __flags);
Howard Hinnant65a3f3d2010-06-30 00:21:42 +00006200}
6201
6202template <class _ST, class _SA, class _Allocator, class _CharT, class _Traits>
6203inline _LIBCPP_INLINE_VISIBILITY
6204bool
6205regex_match(const basic_string<_CharT, _ST, _SA>& __s,
6206 match_results<typename basic_string<_CharT, _ST, _SA>::const_iterator, _Allocator>& __m,
6207 const basic_regex<_CharT, _Traits>& __e,
6208 regex_constants::match_flag_type __flags = regex_constants::match_default)
6209{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00006210 return _VSTD::regex_match(__s.begin(), __s.end(), __m, __e, __flags);
Howard Hinnant65a3f3d2010-06-30 00:21:42 +00006211}
6212
Marshall Clow8c950072014-02-19 21:21:11 +00006213#if _LIBCPP_STD_VER > 11
6214template <class _ST, class _SA, class _Allocator, class _CharT, class _Traits>
6215inline _LIBCPP_INLINE_VISIBILITY
6216bool
6217regex_match(const basic_string<_CharT, _ST, _SA>&& __s,
6218 match_results<typename basic_string<_CharT, _ST, _SA>::const_iterator, _Allocator>& __m,
6219 const basic_regex<_CharT, _Traits>& __e,
Louis Dionne173f29e2019-05-29 16:01:36 +00006220 regex_constants::match_flag_type __flags = regex_constants::match_default) = delete;
Marshall Clow8c950072014-02-19 21:21:11 +00006221#endif
6222
Howard Hinnant65a3f3d2010-06-30 00:21:42 +00006223template <class _CharT, class _Traits>
6224inline _LIBCPP_INLINE_VISIBILITY
6225bool
6226regex_match(const _CharT* __str, const basic_regex<_CharT, _Traits>& __e,
6227 regex_constants::match_flag_type __flags = regex_constants::match_default)
6228{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00006229 return _VSTD::regex_match(__str, __str + _Traits::length(__str), __e, __flags);
Howard Hinnant65a3f3d2010-06-30 00:21:42 +00006230}
6231
6232template <class _ST, class _SA, class _CharT, class _Traits>
6233inline _LIBCPP_INLINE_VISIBILITY
6234bool
6235regex_match(const basic_string<_CharT, _ST, _SA>& __s,
6236 const basic_regex<_CharT, _Traits>& __e,
6237 regex_constants::match_flag_type __flags = regex_constants::match_default)
6238{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00006239 return _VSTD::regex_match(__s.begin(), __s.end(), __e, __flags);
Howard Hinnant65a3f3d2010-06-30 00:21:42 +00006240}
6241
Howard Hinnantd3925342010-08-16 20:21:16 +00006242// regex_iterator
6243
6244template <class _BidirectionalIterator,
6245 class _CharT = typename iterator_traits<_BidirectionalIterator>::value_type,
6246 class _Traits = regex_traits<_CharT> >
Richard Smith256954d2020-11-11 17:12:18 -08006247 class _LIBCPP_TEMPLATE_VIS regex_iterator;
6248
6249typedef regex_iterator<const char*> cregex_iterator;
6250typedef regex_iterator<const wchar_t*> wcregex_iterator;
6251typedef regex_iterator<string::const_iterator> sregex_iterator;
6252typedef regex_iterator<wstring::const_iterator> wsregex_iterator;
6253
6254template <class _BidirectionalIterator, class _CharT, class _Traits>
6255class
6256 _LIBCPP_TEMPLATE_VIS
6257 _LIBCPP_PREFERRED_NAME(cregex_iterator)
6258 _LIBCPP_PREFERRED_NAME(wcregex_iterator)
6259 _LIBCPP_PREFERRED_NAME(sregex_iterator)
6260 _LIBCPP_PREFERRED_NAME(wsregex_iterator)
6261 regex_iterator
Howard Hinnantd3925342010-08-16 20:21:16 +00006262{
6263public:
6264 typedef basic_regex<_CharT, _Traits> regex_type;
6265 typedef match_results<_BidirectionalIterator> value_type;
6266 typedef ptrdiff_t difference_type;
6267 typedef const value_type* pointer;
6268 typedef const value_type& reference;
6269 typedef forward_iterator_tag iterator_category;
6270
6271private:
6272 _BidirectionalIterator __begin_;
6273 _BidirectionalIterator __end_;
6274 const regex_type* __pregex_;
6275 regex_constants::match_flag_type __flags_;
6276 value_type __match_;
6277
6278public:
6279 regex_iterator();
6280 regex_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6281 const regex_type& __re,
Marshall Clow8c950072014-02-19 21:21:11 +00006282 regex_constants::match_flag_type __m
6283 = regex_constants::match_default);
6284#if _LIBCPP_STD_VER > 11
6285 regex_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6286 const regex_type&& __re,
Louis Dionne173f29e2019-05-29 16:01:36 +00006287 regex_constants::match_flag_type __m
Marshall Clow8c950072014-02-19 21:21:11 +00006288 = regex_constants::match_default) = delete;
6289#endif
Howard Hinnantd3925342010-08-16 20:21:16 +00006290
6291 bool operator==(const regex_iterator& __x) const;
Howard Hinnant7ca9d942010-09-23 15:13:20 +00006292 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd3925342010-08-16 20:21:16 +00006293 bool operator!=(const regex_iterator& __x) const {return !(*this == __x);}
6294
Howard Hinnant7ca9d942010-09-23 15:13:20 +00006295 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd3925342010-08-16 20:21:16 +00006296 reference operator*() const {return __match_;}
Howard Hinnant7ca9d942010-09-23 15:13:20 +00006297 _LIBCPP_INLINE_VISIBILITY
Marshall Clowf01062c2019-01-24 02:02:50 +00006298 pointer operator->() const {return _VSTD::addressof(__match_);}
Howard Hinnantd3925342010-08-16 20:21:16 +00006299
6300 regex_iterator& operator++();
Howard Hinnant7ca9d942010-09-23 15:13:20 +00006301 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd3925342010-08-16 20:21:16 +00006302 regex_iterator operator++(int)
6303 {
6304 regex_iterator __t(*this);
6305 ++(*this);
6306 return __t;
6307 }
6308};
6309
6310template <class _BidirectionalIterator, class _CharT, class _Traits>
6311regex_iterator<_BidirectionalIterator, _CharT, _Traits>::regex_iterator()
6312 : __begin_(), __end_(), __pregex_(nullptr), __flags_(), __match_()
6313{
6314}
6315
6316template <class _BidirectionalIterator, class _CharT, class _Traits>
6317regex_iterator<_BidirectionalIterator, _CharT, _Traits>::
6318 regex_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6319 const regex_type& __re, regex_constants::match_flag_type __m)
6320 : __begin_(__a),
6321 __end_(__b),
Marshall Clowf01062c2019-01-24 02:02:50 +00006322 __pregex_(_VSTD::addressof(__re)),
Howard Hinnantd3925342010-08-16 20:21:16 +00006323 __flags_(__m)
6324{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00006325 _VSTD::regex_search(__begin_, __end_, __match_, *__pregex_, __flags_);
Howard Hinnantd3925342010-08-16 20:21:16 +00006326}
6327
6328template <class _BidirectionalIterator, class _CharT, class _Traits>
6329bool
6330regex_iterator<_BidirectionalIterator, _CharT, _Traits>::
6331 operator==(const regex_iterator& __x) const
6332{
6333 if (__match_.empty() && __x.__match_.empty())
6334 return true;
6335 if (__match_.empty() || __x.__match_.empty())
6336 return false;
6337 return __begin_ == __x.__begin_ &&
6338 __end_ == __x.__end_ &&
6339 __pregex_ == __x.__pregex_ &&
6340 __flags_ == __x.__flags_ &&
6341 __match_[0] == __x.__match_[0];
6342}
6343
6344template <class _BidirectionalIterator, class _CharT, class _Traits>
6345regex_iterator<_BidirectionalIterator, _CharT, _Traits>&
6346regex_iterator<_BidirectionalIterator, _CharT, _Traits>::operator++()
6347{
6348 __flags_ |= regex_constants::__no_update_pos;
6349 _BidirectionalIterator __start = __match_[0].second;
Marshall Clowfaa964d2017-07-05 16:37:19 +00006350 if (__match_[0].first == __match_[0].second)
Howard Hinnantd3925342010-08-16 20:21:16 +00006351 {
6352 if (__start == __end_)
6353 {
6354 __match_ = value_type();
6355 return *this;
6356 }
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00006357 else if (_VSTD::regex_search(__start, __end_, __match_, *__pregex_,
Howard Hinnantd3925342010-08-16 20:21:16 +00006358 __flags_ | regex_constants::match_not_null |
6359 regex_constants::match_continuous))
6360 return *this;
6361 else
6362 ++__start;
6363 }
6364 __flags_ |= regex_constants::match_prev_avail;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00006365 if (!_VSTD::regex_search(__start, __end_, __match_, *__pregex_, __flags_))
Howard Hinnantd3925342010-08-16 20:21:16 +00006366 __match_ = value_type();
6367 return *this;
6368}
6369
Howard Hinnantd3925342010-08-16 20:21:16 +00006370// regex_token_iterator
6371
6372template <class _BidirectionalIterator,
6373 class _CharT = typename iterator_traits<_BidirectionalIterator>::value_type,
6374 class _Traits = regex_traits<_CharT> >
Richard Smith256954d2020-11-11 17:12:18 -08006375 class _LIBCPP_TEMPLATE_VIS regex_token_iterator;
6376
6377typedef regex_token_iterator<const char*> cregex_token_iterator;
6378typedef regex_token_iterator<const wchar_t*> wcregex_token_iterator;
6379typedef regex_token_iterator<string::const_iterator> sregex_token_iterator;
6380typedef regex_token_iterator<wstring::const_iterator> wsregex_token_iterator;
6381
6382template <class _BidirectionalIterator, class _CharT, class _Traits>
6383class
6384 _LIBCPP_TEMPLATE_VIS
6385 _LIBCPP_PREFERRED_NAME(cregex_token_iterator)
6386 _LIBCPP_PREFERRED_NAME(wcregex_token_iterator)
6387 _LIBCPP_PREFERRED_NAME(sregex_token_iterator)
6388 _LIBCPP_PREFERRED_NAME(wsregex_token_iterator)
6389 regex_token_iterator
Howard Hinnantd3925342010-08-16 20:21:16 +00006390{
6391public:
6392 typedef basic_regex<_CharT, _Traits> regex_type;
6393 typedef sub_match<_BidirectionalIterator> value_type;
6394 typedef ptrdiff_t difference_type;
6395 typedef const value_type* pointer;
6396 typedef const value_type& reference;
6397 typedef forward_iterator_tag iterator_category;
6398
Howard Hinnant63c9ce62010-08-17 20:42:03 +00006399private:
6400 typedef regex_iterator<_BidirectionalIterator, _CharT, _Traits> _Position;
6401
6402 _Position __position_;
6403 const value_type* __result_;
6404 value_type __suffix_;
Eric Fiselier37c22152016-12-24 00:24:44 +00006405 ptrdiff_t __n_;
Howard Hinnant63c9ce62010-08-17 20:42:03 +00006406 vector<int> __subs_;
6407
6408public:
Howard Hinnantd3925342010-08-16 20:21:16 +00006409 regex_token_iterator();
6410 regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6411 const regex_type& __re, int __submatch = 0,
Howard Hinnant63c9ce62010-08-17 20:42:03 +00006412 regex_constants::match_flag_type __m =
6413 regex_constants::match_default);
Marshall Clow8c950072014-02-19 21:21:11 +00006414#if _LIBCPP_STD_VER > 11
6415 regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6416 const regex_type&& __re, int __submatch = 0,
6417 regex_constants::match_flag_type __m =
6418 regex_constants::match_default) = delete;
6419#endif
6420
Howard Hinnantd3925342010-08-16 20:21:16 +00006421 regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6422 const regex_type& __re, const vector<int>& __submatches,
Howard Hinnant63c9ce62010-08-17 20:42:03 +00006423 regex_constants::match_flag_type __m =
6424 regex_constants::match_default);
Marshall Clow8c950072014-02-19 21:21:11 +00006425#if _LIBCPP_STD_VER > 11
6426 regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6427 const regex_type&& __re, const vector<int>& __submatches,
6428 regex_constants::match_flag_type __m =
6429 regex_constants::match_default) = delete;
6430#endif
6431
Eric Fiselier6f8516f2017-04-18 23:42:15 +00006432#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantd3925342010-08-16 20:21:16 +00006433 regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
Howard Hinnant63c9ce62010-08-17 20:42:03 +00006434 const regex_type& __re,
6435 initializer_list<int> __submatches,
6436 regex_constants::match_flag_type __m =
6437 regex_constants::match_default);
Marshall Clow8c950072014-02-19 21:21:11 +00006438
6439#if _LIBCPP_STD_VER > 11
6440 regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6441 const regex_type&& __re,
6442 initializer_list<int> __submatches,
6443 regex_constants::match_flag_type __m =
6444 regex_constants::match_default) = delete;
6445#endif
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04006446#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc834c512011-11-29 18:15:50 +00006447 template <size_t _Np>
Howard Hinnant63c9ce62010-08-17 20:42:03 +00006448 regex_token_iterator(_BidirectionalIterator __a,
6449 _BidirectionalIterator __b,
6450 const regex_type& __re,
Howard Hinnantc834c512011-11-29 18:15:50 +00006451 const int (&__submatches)[_Np],
Howard Hinnant63c9ce62010-08-17 20:42:03 +00006452 regex_constants::match_flag_type __m =
6453 regex_constants::match_default);
Marshall Clow8c950072014-02-19 21:21:11 +00006454#if _LIBCPP_STD_VER > 11
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05006455 template <size_t _Np>
Marshall Clow8c950072014-02-19 21:21:11 +00006456 regex_token_iterator(_BidirectionalIterator __a,
6457 _BidirectionalIterator __b,
6458 const regex_type&& __re,
6459 const int (&__submatches)[_Np],
6460 regex_constants::match_flag_type __m =
6461 regex_constants::match_default) = delete;
6462#endif
6463
Howard Hinnantd3925342010-08-16 20:21:16 +00006464 regex_token_iterator(const regex_token_iterator&);
6465 regex_token_iterator& operator=(const regex_token_iterator&);
6466
Howard Hinnant63c9ce62010-08-17 20:42:03 +00006467 bool operator==(const regex_token_iterator& __x) const;
Howard Hinnant7ca9d942010-09-23 15:13:20 +00006468 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant63c9ce62010-08-17 20:42:03 +00006469 bool operator!=(const regex_token_iterator& __x) const {return !(*this == __x);}
Howard Hinnantd3925342010-08-16 20:21:16 +00006470
Howard Hinnant7ca9d942010-09-23 15:13:20 +00006471 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant63c9ce62010-08-17 20:42:03 +00006472 const value_type& operator*() const {return *__result_;}
Howard Hinnant7ca9d942010-09-23 15:13:20 +00006473 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant63c9ce62010-08-17 20:42:03 +00006474 const value_type* operator->() const {return __result_;}
Howard Hinnantd3925342010-08-16 20:21:16 +00006475
6476 regex_token_iterator& operator++();
Howard Hinnant7ca9d942010-09-23 15:13:20 +00006477 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant63c9ce62010-08-17 20:42:03 +00006478 regex_token_iterator operator++(int)
6479 {
6480 regex_token_iterator __t(*this);
6481 ++(*this);
6482 return __t;
6483 }
6484
6485private:
6486 void __init(_BidirectionalIterator __a, _BidirectionalIterator __b);
Marshall Clow68083022014-01-09 18:25:57 +00006487 void __establish_result () {
Eric Fiselier37c22152016-12-24 00:24:44 +00006488 if (__subs_[__n_] == -1)
Marshall Clow68083022014-01-09 18:25:57 +00006489 __result_ = &__position_->prefix();
6490 else
Eric Fiselier37c22152016-12-24 00:24:44 +00006491 __result_ = &(*__position_)[__subs_[__n_]];
Louis Dionne173f29e2019-05-29 16:01:36 +00006492 }
Howard Hinnantd3925342010-08-16 20:21:16 +00006493};
6494
Howard Hinnant63c9ce62010-08-17 20:42:03 +00006495template <class _BidirectionalIterator, class _CharT, class _Traits>
6496regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6497 regex_token_iterator()
6498 : __result_(nullptr),
6499 __suffix_(),
Eric Fiselier37c22152016-12-24 00:24:44 +00006500 __n_(0)
Howard Hinnant63c9ce62010-08-17 20:42:03 +00006501{
6502}
6503
6504template <class _BidirectionalIterator, class _CharT, class _Traits>
6505void
6506regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6507 __init(_BidirectionalIterator __a, _BidirectionalIterator __b)
6508{
6509 if (__position_ != _Position())
Marshall Clow68083022014-01-09 18:25:57 +00006510 __establish_result ();
Eric Fiselier37c22152016-12-24 00:24:44 +00006511 else if (__subs_[__n_] == -1)
Howard Hinnant63c9ce62010-08-17 20:42:03 +00006512 {
6513 __suffix_.matched = true;
6514 __suffix_.first = __a;
6515 __suffix_.second = __b;
6516 __result_ = &__suffix_;
6517 }
6518 else
6519 __result_ = nullptr;
6520}
6521
6522template <class _BidirectionalIterator, class _CharT, class _Traits>
6523regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6524 regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6525 const regex_type& __re, int __submatch,
6526 regex_constants::match_flag_type __m)
6527 : __position_(__a, __b, __re, __m),
Eric Fiselier37c22152016-12-24 00:24:44 +00006528 __n_(0),
Howard Hinnant63c9ce62010-08-17 20:42:03 +00006529 __subs_(1, __submatch)
6530{
6531 __init(__a, __b);
6532}
6533
6534template <class _BidirectionalIterator, class _CharT, class _Traits>
6535regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6536 regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6537 const regex_type& __re, const vector<int>& __submatches,
6538 regex_constants::match_flag_type __m)
6539 : __position_(__a, __b, __re, __m),
Eric Fiselier37c22152016-12-24 00:24:44 +00006540 __n_(0),
Howard Hinnant63c9ce62010-08-17 20:42:03 +00006541 __subs_(__submatches)
6542{
6543 __init(__a, __b);
6544}
6545
Eric Fiselier6f8516f2017-04-18 23:42:15 +00006546#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00006547
Howard Hinnant63c9ce62010-08-17 20:42:03 +00006548template <class _BidirectionalIterator, class _CharT, class _Traits>
6549regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6550 regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6551 const regex_type& __re,
6552 initializer_list<int> __submatches,
6553 regex_constants::match_flag_type __m)
6554 : __position_(__a, __b, __re, __m),
Eric Fiselier37c22152016-12-24 00:24:44 +00006555 __n_(0),
Howard Hinnant63c9ce62010-08-17 20:42:03 +00006556 __subs_(__submatches)
6557{
6558 __init(__a, __b);
6559}
6560
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04006561#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00006562
Howard Hinnant63c9ce62010-08-17 20:42:03 +00006563template <class _BidirectionalIterator, class _CharT, class _Traits>
Howard Hinnantc834c512011-11-29 18:15:50 +00006564template <size_t _Np>
Howard Hinnant63c9ce62010-08-17 20:42:03 +00006565regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6566 regex_token_iterator(_BidirectionalIterator __a, _BidirectionalIterator __b,
6567 const regex_type& __re,
Howard Hinnantc834c512011-11-29 18:15:50 +00006568 const int (&__submatches)[_Np],
Howard Hinnant63c9ce62010-08-17 20:42:03 +00006569 regex_constants::match_flag_type __m)
6570 : __position_(__a, __b, __re, __m),
Eric Fiselier37c22152016-12-24 00:24:44 +00006571 __n_(0),
Marshall Clowf01062c2019-01-24 02:02:50 +00006572 __subs_(begin(__submatches), end(__submatches))
Howard Hinnant63c9ce62010-08-17 20:42:03 +00006573{
6574 __init(__a, __b);
6575}
6576
6577template <class _BidirectionalIterator, class _CharT, class _Traits>
6578regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6579 regex_token_iterator(const regex_token_iterator& __x)
6580 : __position_(__x.__position_),
6581 __result_(__x.__result_),
6582 __suffix_(__x.__suffix_),
Eric Fiselier37c22152016-12-24 00:24:44 +00006583 __n_(__x.__n_),
Howard Hinnant63c9ce62010-08-17 20:42:03 +00006584 __subs_(__x.__subs_)
6585{
6586 if (__x.__result_ == &__x.__suffix_)
Marshall Clow20756ac2014-01-13 17:47:08 +00006587 __result_ = &__suffix_;
Marshall Clow68083022014-01-09 18:25:57 +00006588 else if ( __result_ != nullptr )
6589 __establish_result ();
Howard Hinnant63c9ce62010-08-17 20:42:03 +00006590}
6591
6592template <class _BidirectionalIterator, class _CharT, class _Traits>
6593regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>&
6594regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6595 operator=(const regex_token_iterator& __x)
6596{
6597 if (this != &__x)
6598 {
6599 __position_ = __x.__position_;
6600 if (__x.__result_ == &__x.__suffix_)
Marshall Clow68083022014-01-09 18:25:57 +00006601 __result_ = &__suffix_;
Howard Hinnant63c9ce62010-08-17 20:42:03 +00006602 else
6603 __result_ = __x.__result_;
6604 __suffix_ = __x.__suffix_;
Eric Fiselier37c22152016-12-24 00:24:44 +00006605 __n_ = __x.__n_;
Howard Hinnant63c9ce62010-08-17 20:42:03 +00006606 __subs_ = __x.__subs_;
Marshall Clow68083022014-01-09 18:25:57 +00006607
6608 if ( __result_ != nullptr && __result_ != &__suffix_ )
6609 __establish_result();
Howard Hinnant63c9ce62010-08-17 20:42:03 +00006610 }
6611 return *this;
6612}
6613
6614template <class _BidirectionalIterator, class _CharT, class _Traits>
6615bool
6616regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::
6617 operator==(const regex_token_iterator& __x) const
6618{
6619 if (__result_ == nullptr && __x.__result_ == nullptr)
6620 return true;
6621 if (__result_ == &__suffix_ && __x.__result_ == &__x.__suffix_ &&
6622 __suffix_ == __x.__suffix_)
6623 return true;
6624 if (__result_ == nullptr || __x.__result_ == nullptr)
6625 return false;
6626 if (__result_ == &__suffix_ || __x.__result_ == &__x.__suffix_)
6627 return false;
Eric Fiselier37c22152016-12-24 00:24:44 +00006628 return __position_ == __x.__position_ && __n_ == __x.__n_ &&
Howard Hinnant63c9ce62010-08-17 20:42:03 +00006629 __subs_ == __x.__subs_;
6630}
6631
6632template <class _BidirectionalIterator, class _CharT, class _Traits>
6633regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>&
6634regex_token_iterator<_BidirectionalIterator, _CharT, _Traits>::operator++()
6635{
6636 _Position __prev = __position_;
6637 if (__result_ == &__suffix_)
6638 __result_ = nullptr;
Eric Fiselier37c22152016-12-24 00:24:44 +00006639 else if (static_cast<size_t>(__n_ + 1) < __subs_.size())
Howard Hinnant63c9ce62010-08-17 20:42:03 +00006640 {
Eric Fiselier37c22152016-12-24 00:24:44 +00006641 ++__n_;
Marshall Clow68083022014-01-09 18:25:57 +00006642 __establish_result();
Howard Hinnant63c9ce62010-08-17 20:42:03 +00006643 }
6644 else
6645 {
Eric Fiselier37c22152016-12-24 00:24:44 +00006646 __n_ = 0;
Howard Hinnant63c9ce62010-08-17 20:42:03 +00006647 ++__position_;
6648 if (__position_ != _Position())
Marshall Clow68083022014-01-09 18:25:57 +00006649 __establish_result();
Howard Hinnant63c9ce62010-08-17 20:42:03 +00006650 else
6651 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00006652 if (_VSTD::find(__subs_.begin(), __subs_.end(), -1) != __subs_.end()
Howard Hinnant63c9ce62010-08-17 20:42:03 +00006653 && __prev->suffix().length() != 0)
6654 {
6655 __suffix_.matched = true;
6656 __suffix_.first = __prev->suffix().first;
6657 __suffix_.second = __prev->suffix().second;
6658 __result_ = &__suffix_;
6659 }
6660 else
6661 __result_ = nullptr;
6662 }
6663 }
6664 return *this;
6665}
6666
Howard Hinnante90434c2010-08-18 00:13:08 +00006667// regex_replace
6668
6669template <class _OutputIterator, class _BidirectionalIterator,
6670 class _Traits, class _CharT>
6671_OutputIterator
Alexander Richardsonc9637642017-11-14 11:14:25 +00006672regex_replace(_OutputIterator __output_iter,
Howard Hinnante90434c2010-08-18 00:13:08 +00006673 _BidirectionalIterator __first, _BidirectionalIterator __last,
6674 const basic_regex<_CharT, _Traits>& __e, const _CharT* __fmt,
6675 regex_constants::match_flag_type __flags = regex_constants::match_default)
6676{
6677 typedef regex_iterator<_BidirectionalIterator, _CharT, _Traits> _Iter;
6678 _Iter __i(__first, __last, __e, __flags);
6679 _Iter __eof;
6680 if (__i == __eof)
6681 {
6682 if (!(__flags & regex_constants::format_no_copy))
Alexander Richardsonc9637642017-11-14 11:14:25 +00006683 __output_iter = _VSTD::copy(__first, __last, __output_iter);
Howard Hinnante90434c2010-08-18 00:13:08 +00006684 }
6685 else
6686 {
6687 sub_match<_BidirectionalIterator> __lm;
6688 for (size_t __len = char_traits<_CharT>::length(__fmt); __i != __eof; ++__i)
6689 {
6690 if (!(__flags & regex_constants::format_no_copy))
Alexander Richardsonc9637642017-11-14 11:14:25 +00006691 __output_iter = _VSTD::copy(__i->prefix().first, __i->prefix().second, __output_iter);
6692 __output_iter = __i->format(__output_iter, __fmt, __fmt + __len, __flags);
Howard Hinnante90434c2010-08-18 00:13:08 +00006693 __lm = __i->suffix();
6694 if (__flags & regex_constants::format_first_only)
6695 break;
6696 }
6697 if (!(__flags & regex_constants::format_no_copy))
Alexander Richardsonc9637642017-11-14 11:14:25 +00006698 __output_iter = _VSTD::copy(__lm.first, __lm.second, __output_iter);
Howard Hinnante90434c2010-08-18 00:13:08 +00006699 }
Alexander Richardsonc9637642017-11-14 11:14:25 +00006700 return __output_iter;
Howard Hinnante90434c2010-08-18 00:13:08 +00006701}
6702
6703template <class _OutputIterator, class _BidirectionalIterator,
6704 class _Traits, class _CharT, class _ST, class _SA>
6705inline _LIBCPP_INLINE_VISIBILITY
6706_OutputIterator
Alexander Richardsonc9637642017-11-14 11:14:25 +00006707regex_replace(_OutputIterator __output_iter,
Howard Hinnante90434c2010-08-18 00:13:08 +00006708 _BidirectionalIterator __first, _BidirectionalIterator __last,
6709 const basic_regex<_CharT, _Traits>& __e,
6710 const basic_string<_CharT, _ST, _SA>& __fmt,
6711 regex_constants::match_flag_type __flags = regex_constants::match_default)
6712{
Alexander Richardsonc9637642017-11-14 11:14:25 +00006713 return _VSTD::regex_replace(__output_iter, __first, __last, __e, __fmt.c_str(), __flags);
Howard Hinnante90434c2010-08-18 00:13:08 +00006714}
6715
6716template <class _Traits, class _CharT, class _ST, class _SA, class _FST,
6717 class _FSA>
6718inline _LIBCPP_INLINE_VISIBILITY
6719basic_string<_CharT, _ST, _SA>
6720regex_replace(const basic_string<_CharT, _ST, _SA>& __s,
6721 const basic_regex<_CharT, _Traits>& __e,
6722 const basic_string<_CharT, _FST, _FSA>& __fmt,
6723 regex_constants::match_flag_type __flags = regex_constants::match_default)
6724{
6725 basic_string<_CharT, _ST, _SA> __r;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00006726 _VSTD::regex_replace(back_inserter(__r), __s.begin(), __s.end(), __e,
Howard Hinnante90434c2010-08-18 00:13:08 +00006727 __fmt.c_str(), __flags);
6728 return __r;
6729}
6730
6731template <class _Traits, class _CharT, class _ST, class _SA>
6732inline _LIBCPP_INLINE_VISIBILITY
6733basic_string<_CharT, _ST, _SA>
6734regex_replace(const basic_string<_CharT, _ST, _SA>& __s,
6735 const basic_regex<_CharT, _Traits>& __e, const _CharT* __fmt,
6736 regex_constants::match_flag_type __flags = regex_constants::match_default)
6737{
6738 basic_string<_CharT, _ST, _SA> __r;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00006739 _VSTD::regex_replace(back_inserter(__r), __s.begin(), __s.end(), __e,
Howard Hinnante90434c2010-08-18 00:13:08 +00006740 __fmt, __flags);
6741 return __r;
6742}
6743
6744template <class _Traits, class _CharT, class _ST, class _SA>
6745inline _LIBCPP_INLINE_VISIBILITY
6746basic_string<_CharT>
6747regex_replace(const _CharT* __s,
6748 const basic_regex<_CharT, _Traits>& __e,
6749 const basic_string<_CharT, _ST, _SA>& __fmt,
6750 regex_constants::match_flag_type __flags = regex_constants::match_default)
6751{
6752 basic_string<_CharT> __r;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00006753 _VSTD::regex_replace(back_inserter(__r), __s,
Howard Hinnante90434c2010-08-18 00:13:08 +00006754 __s + char_traits<_CharT>::length(__s), __e,
6755 __fmt.c_str(), __flags);
6756 return __r;
6757}
6758
6759template <class _Traits, class _CharT>
6760inline _LIBCPP_INLINE_VISIBILITY
6761basic_string<_CharT>
6762regex_replace(const _CharT* __s,
6763 const basic_regex<_CharT, _Traits>& __e,
6764 const _CharT* __fmt,
6765 regex_constants::match_flag_type __flags = regex_constants::match_default)
6766{
6767 basic_string<_CharT> __r;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00006768 _VSTD::regex_replace(back_inserter(__r), __s,
Howard Hinnante90434c2010-08-18 00:13:08 +00006769 __s + char_traits<_CharT>::length(__s), __e,
6770 __fmt, __flags);
6771 return __r;
6772}
6773
Howard Hinnanta3af5a32010-06-17 00:34:59 +00006774_LIBCPP_END_NAMESPACE_STD
6775
Eric Fiselierf4433a32017-05-31 22:07:49 +00006776_LIBCPP_POP_MACROS
6777
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04006778#endif // _LIBCPP_REGEX