1// class template regex -*- C++ -*-
2
3// Copyright (C) 2010-2021 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
24
25/**
26 * @file bits/regex.h
27 * This is an internal header file, included by other library headers.
28 * Do not attempt to use it directly. @headername{regex}
29 */
30
31namespace std _GLIBCXX_VISIBILITY(default)
32{
33_GLIBCXX_BEGIN_NAMESPACE_VERSION
34_GLIBCXX_BEGIN_NAMESPACE_CXX11
35 template<typename, typename>
36 class basic_regex;
37
38 template<typename _Bi_iter, typename _Alloc>
39 class match_results;
40
41_GLIBCXX_END_NAMESPACE_CXX11
42
43namespace __detail
44{
45 enum class _RegexExecutorPolicy : int { _S_auto, _S_alternate };
46
47 template<typename _BiIter, typename _Alloc,
48 typename _CharT, typename _TraitsT,
49 _RegexExecutorPolicy __policy,
50 bool __match_mode>
51 bool
52 __regex_algo_impl(_BiIter __s,
53 _BiIter __e,
54 match_results<_BiIter, _Alloc>& __m,
55 const basic_regex<_CharT, _TraitsT>& __re,
56 regex_constants::match_flag_type __flags);
57
58 template<typename, typename, typename, bool>
59 class _Executor;
60
61 template<typename _Tp>
62 struct __is_contiguous_iter : false_type { };
63
64 template<typename _Tp>
65 struct __is_contiguous_iter<_Tp*> : true_type { };
66
67 template<typename _Tp, typename _Cont>
68 struct __is_contiguous_iter<__gnu_cxx::__normal_iterator<_Tp*, _Cont>>
69 : true_type { };
70}
71
72_GLIBCXX_BEGIN_NAMESPACE_CXX11
73
74 /**
75 * @addtogroup regex
76 * @{
77 */
78
79 /**
80 * @brief Describes aspects of a regular expression.
81 *
82 * A regular expression traits class that satisfies the requirements of
83 * section [28.7].
84 *
85 * The class %regex is parameterized around a set of related types and
86 * functions used to complete the definition of its semantics. This class
87 * satisfies the requirements of such a traits class.
88 */
89 template<typename _Ch_type>
90 class regex_traits
91 {
92 public:
93 typedef _Ch_type char_type;
94 typedef std::basic_string<char_type> string_type;
95 typedef std::locale locale_type;
96
97 private:
98 struct _RegexMask
99 {
100 typedef std::ctype_base::mask _BaseType;
101 _BaseType _M_base;
102 unsigned char _M_extended;
103 static constexpr unsigned char _S_under = 1 << 0;
104 static constexpr unsigned char _S_valid_mask = 0x1;
105
106 constexpr _RegexMask(_BaseType __base = 0,
107 unsigned char __extended = 0)
108 : _M_base(__base), _M_extended(__extended)
109 { }
110
111 constexpr _RegexMask
112 operator&(_RegexMask __other) const
113 {
114 return _RegexMask(_M_base & __other._M_base,
115 _M_extended & __other._M_extended);
116 }
117
118 constexpr _RegexMask
119 operator|(_RegexMask __other) const
120 {
121 return _RegexMask(_M_base | __other._M_base,
122 _M_extended | __other._M_extended);
123 }
124
125 constexpr _RegexMask
126 operator^(_RegexMask __other) const
127 {
128 return _RegexMask(_M_base ^ __other._M_base,
129 _M_extended ^ __other._M_extended);
130 }
131
132 constexpr _RegexMask
133 operator~() const
134 { return _RegexMask(~_M_base, ~_M_extended); }
135
136 _RegexMask&
137 operator&=(_RegexMask __other)
138 { return *this = (*this) & __other; }
139
140 _RegexMask&
141 operator|=(_RegexMask __other)
142 { return *this = (*this) | __other; }
143
144 _RegexMask&
145 operator^=(_RegexMask __other)
146 { return *this = (*this) ^ __other; }
147
148 constexpr bool
149 operator==(_RegexMask __other) const
150 {
151 return (_M_extended & _S_valid_mask)
152 == (__other._M_extended & _S_valid_mask)
153 && _M_base == __other._M_base;
154 }
155
156#if __cpp_impl_three_way_comparison < 201907L
157 constexpr bool
158 operator!=(_RegexMask __other) const
159 { return !((*this) == __other); }
160#endif
161 };
162
163 public:
164 typedef _RegexMask char_class_type;
165
166 public:
167 /**
168 * @brief Constructs a default traits object.
169 */
170 regex_traits() { }
171
172 /**
173 * @brief Gives the length of a C-style string starting at @p __p.
174 *
175 * @param __p a pointer to the start of a character sequence.
176 *
177 * @returns the number of characters between @p *__p and the first
178 * default-initialized value of type @p char_type. In other words, uses
179 * the C-string algorithm for determining the length of a sequence of
180 * characters.
181 */
182 static std::size_t
183 length(const char_type* __p)
184 { return string_type::traits_type::length(__p); }
185
186 /**
187 * @brief Performs the identity translation.
188 *
189 * @param __c A character to the locale-specific character set.
190 *
191 * @returns __c.
192 */
193 char_type
194 translate(char_type __c) const
195 { return __c; }
196
197 /**
198 * @brief Translates a character into a case-insensitive equivalent.
199 *
200 * @param __c A character to the locale-specific character set.
201 *
202 * @returns the locale-specific lower-case equivalent of __c.
203 * @throws std::bad_cast if the imbued locale does not support the ctype
204 * facet.
205 */
206 char_type
207 translate_nocase(char_type __c) const
208 {
209 typedef std::ctype<char_type> __ctype_type;
210 const __ctype_type& __fctyp(use_facet<__ctype_type>(_M_locale));
211 return __fctyp.tolower(__c);
212 }
213
214 /**
215 * @brief Gets a sort key for a character sequence.
216 *
217 * @param __first beginning of the character sequence.
218 * @param __last one-past-the-end of the character sequence.
219 *
220 * Returns a sort key for the character sequence designated by the
221 * iterator range [F1, F2) such that if the character sequence [G1, G2)
222 * sorts before the character sequence [H1, H2) then
223 * v.transform(G1, G2) < v.transform(H1, H2).
224 *
225 * What this really does is provide a more efficient way to compare a
226 * string to multiple other strings in locales with fancy collation
227 * rules and equivalence classes.
228 *
229 * @returns a locale-specific sort key equivalent to the input range.
230 *
231 * @throws std::bad_cast if the current locale does not have a collate
232 * facet.
233 */
234 template<typename _Fwd_iter>
235 string_type
236 transform(_Fwd_iter __first, _Fwd_iter __last) const
237 {
238 typedef std::collate<char_type> __collate_type;
239 const __collate_type& __fclt(use_facet<__collate_type>(_M_locale));
240 string_type __s(__first, __last);
241 return __fclt.transform(__s.data(), __s.data() + __s.size());
242 }
243
244 /**
245 * @brief Gets a sort key for a character sequence, independent of case.
246 *
247 * @param __first beginning of the character sequence.
248 * @param __last one-past-the-end of the character sequence.
249 *
250 * Effects: if typeid(use_facet<collate<_Ch_type> >) ==
251 * typeid(collate_byname<_Ch_type>) and the form of the sort key
252 * returned by collate_byname<_Ch_type>::transform(__first, __last)
253 * is known and can be converted into a primary sort key
254 * then returns that key, otherwise returns an empty string.
255 *
256 * @todo Implement this function correctly.
257 */
258 template<typename _Fwd_iter>
259 string_type
260 transform_primary(_Fwd_iter __first, _Fwd_iter __last) const
261 {
262 // TODO : this is not entirely correct.
263 // This function requires extra support from the platform.
264 //
265 // Read http://gcc.gnu.org/ml/libstdc++/2013-09/msg00117.html and
266 // http://www.open-std.org/Jtc1/sc22/wg21/docs/papers/2003/n1429.htm
267 // for details.
268 typedef std::ctype<char_type> __ctype_type;
269 const __ctype_type& __fctyp(use_facet<__ctype_type>(_M_locale));
270 std::vector<char_type> __s(__first, __last);
271 __fctyp.tolower(__s.data(), __s.data() + __s.size());
272 return this->transform(__s.data(), __s.data() + __s.size());
273 }
274
275 /**
276 * @brief Gets a collation element by name.
277 *
278 * @param __first beginning of the collation element name.
279 * @param __last one-past-the-end of the collation element name.
280 *
281 * @returns a sequence of one or more characters that represents the
282 * collating element consisting of the character sequence designated by
283 * the iterator range [__first, __last). Returns an empty string if the
284 * character sequence is not a valid collating element.
285 */
286 template<typename _Fwd_iter>
287 string_type
288 lookup_collatename(_Fwd_iter __first, _Fwd_iter __last) const;
289
290 /**
291 * @brief Maps one or more characters to a named character
292 * classification.
293 *
294 * @param __first beginning of the character sequence.
295 * @param __last one-past-the-end of the character sequence.
296 * @param __icase ignores the case of the classification name.
297 *
298 * @returns an unspecified value that represents the character
299 * classification named by the character sequence designated by
300 * the iterator range [__first, __last). If @p icase is true,
301 * the returned mask identifies the classification regardless of
302 * the case of the characters to be matched (for example,
303 * [[:lower:]] is the same as [[:alpha:]]), otherwise a
304 * case-dependent classification is returned. The value
305 * returned shall be independent of the case of the characters
306 * in the character sequence. If the name is not recognized then
307 * returns a value that compares equal to 0.
308 *
309 * At least the following names (or their wide-character equivalent) are
310 * supported.
311 * - d
312 * - w
313 * - s
314 * - alnum
315 * - alpha
316 * - blank
317 * - cntrl
318 * - digit
319 * - graph
320 * - lower
321 * - print
322 * - punct
323 * - space
324 * - upper
325 * - xdigit
326 */
327 template<typename _Fwd_iter>
328 char_class_type
329 lookup_classname(_Fwd_iter __first, _Fwd_iter __last,
330 bool __icase = false) const;
331
332 /**
333 * @brief Determines if @p c is a member of an identified class.
334 *
335 * @param __c a character.
336 * @param __f a class type (as returned from lookup_classname).
337 *
338 * @returns true if the character @p __c is a member of the classification
339 * represented by @p __f, false otherwise.
340 *
341 * @throws std::bad_cast if the current locale does not have a ctype
342 * facet.
343 */
344 bool
345 isctype(_Ch_type __c, char_class_type __f) const;
346
347 /**
348 * @brief Converts a digit to an int.
349 *
350 * @param __ch a character representing a digit.
351 * @param __radix the radix if the numeric conversion (limited to 8, 10,
352 * or 16).
353 *
354 * @returns the value represented by the digit __ch in base radix if the
355 * character __ch is a valid digit in base radix; otherwise returns -1.
356 */
357 int
358 value(_Ch_type __ch, int __radix) const;
359
360 /**
361 * @brief Imbues the regex_traits object with a copy of a new locale.
362 *
363 * @param __loc A locale.
364 *
365 * @returns a copy of the previous locale in use by the regex_traits
366 * object.
367 *
368 * @note Calling imbue with a different locale than the one currently in
369 * use invalidates all cached data held by *this.
370 */
371 locale_type
372 imbue(locale_type __loc)
373 {
374 std::swap(a&: _M_locale, b&: __loc);
375 return __loc;
376 }
377
378 /**
379 * @brief Gets a copy of the current locale in use by the regex_traits
380 * object.
381 */
382 locale_type
383 getloc() const
384 { return _M_locale; }
385
386 protected:
387 locale_type _M_locale;
388 };
389
390 // [7.8] Class basic_regex
391 /**
392 * Objects of specializations of this class represent regular expressions
393 * constructed from sequences of character type @p _Ch_type.
394 *
395 * Storage for the regular expression is allocated and deallocated as
396 * necessary by the member functions of this class.
397 */
398 template<typename _Ch_type, typename _Rx_traits = regex_traits<_Ch_type>>
399 class basic_regex
400 {
401 public:
402 static_assert(is_same<_Ch_type, typename _Rx_traits::char_type>::value,
403 "regex traits class must have the same char_type");
404
405 // types:
406 typedef _Ch_type value_type;
407 typedef _Rx_traits traits_type;
408 typedef typename traits_type::string_type string_type;
409 typedef regex_constants::syntax_option_type flag_type;
410 typedef typename traits_type::locale_type locale_type;
411
412 /**
413 * @name Constants
414 * std [28.8.1](1)
415 */
416 ///@{
417 static constexpr flag_type icase = regex_constants::icase;
418 static constexpr flag_type nosubs = regex_constants::nosubs;
419 static constexpr flag_type optimize = regex_constants::optimize;
420 static constexpr flag_type collate = regex_constants::collate;
421 static constexpr flag_type ECMAScript = regex_constants::ECMAScript;
422 static constexpr flag_type basic = regex_constants::basic;
423 static constexpr flag_type extended = regex_constants::extended;
424 static constexpr flag_type awk = regex_constants::awk;
425 static constexpr flag_type grep = regex_constants::grep;
426 static constexpr flag_type egrep = regex_constants::egrep;
427#if __cplusplus >= 201703L || !defined __STRICT_ANSI__
428 static constexpr flag_type multiline = regex_constants::multiline;
429#endif
430 ///@}
431
432 // [7.8.2] construct/copy/destroy
433 /**
434 * Constructs a basic regular expression that does not match any
435 * character sequence.
436 */
437 basic_regex() noexcept
438 : _M_flags(ECMAScript), _M_loc(), _M_automaton(nullptr)
439 { }
440
441 /**
442 * @brief Constructs a basic regular expression from the
443 * sequence [__p, __p + char_traits<_Ch_type>::length(__p))
444 * interpreted according to the flags in @p __f.
445 *
446 * @param __p A pointer to the start of a C-style null-terminated string
447 * containing a regular expression.
448 * @param __f Flags indicating the syntax rules and options.
449 *
450 * @throws regex_error if @p __p is not a valid regular expression.
451 */
452 explicit
453 basic_regex(const _Ch_type* __p, flag_type __f = ECMAScript)
454 { _M_compile(first: __p, last: __p + _Rx_traits::length(__p), __f); }
455
456 /**
457 * @brief Constructs a basic regular expression from the sequence
458 * [p, p + len) interpreted according to the flags in @p f.
459 *
460 * @param __p A pointer to the start of a string containing a regular
461 * expression.
462 * @param __len The length of the string containing the regular
463 * expression.
464 * @param __f Flags indicating the syntax rules and options.
465 *
466 * @throws regex_error if @p __p is not a valid regular expression.
467 */
468 basic_regex(const _Ch_type* __p, std::size_t __len,
469 flag_type __f = ECMAScript)
470 { _M_compile(first: __p, last: __p + __len, __f); }
471
472 /**
473 * @brief Copy-constructs a basic regular expression.
474 *
475 * @param __rhs A @p regex object.
476 */
477 basic_regex(const basic_regex& __rhs) = default;
478
479 /**
480 * @brief Move-constructs a basic regular expression.
481 *
482 * @param __rhs A @p regex object.
483 */
484 basic_regex(basic_regex&& __rhs) noexcept = default;
485
486 /**
487 * @brief Constructs a basic regular expression from the string
488 * @p s interpreted according to the flags in @p f.
489 *
490 * @param __s A string containing a regular expression.
491 * @param __f Flags indicating the syntax rules and options.
492 *
493 * @throws regex_error if @p __s is not a valid regular expression.
494 */
495 template<typename _Ch_traits, typename _Ch_alloc>
496 explicit
497 basic_regex(const std::basic_string<_Ch_type, _Ch_traits,
498 _Ch_alloc>& __s,
499 flag_type __f = ECMAScript)
500 { _M_compile(first: __s.data(), last: __s.data() + __s.size(), __f); }
501
502 /**
503 * @brief Constructs a basic regular expression from the range
504 * [first, last) interpreted according to the flags in @p f.
505 *
506 * @param __first The start of a range containing a valid regular
507 * expression.
508 * @param __last The end of a range containing a valid regular
509 * expression.
510 * @param __f The format flags of the regular expression.
511 *
512 * @throws regex_error if @p [__first, __last) is not a valid regular
513 * expression.
514 */
515 template<typename _FwdIter>
516 basic_regex(_FwdIter __first, _FwdIter __last,
517 flag_type __f = ECMAScript)
518 { this->assign(__first, __last, __f); }
519
520 /**
521 * @brief Constructs a basic regular expression from an initializer list.
522 *
523 * @param __l The initializer list.
524 * @param __f The format flags of the regular expression.
525 *
526 * @throws regex_error if @p __l is not a valid regular expression.
527 */
528 basic_regex(initializer_list<_Ch_type> __l, flag_type __f = ECMAScript)
529 { _M_compile(first: __l.begin(), last: __l.end(), __f); }
530
531 /**
532 * @brief Destroys a basic regular expression.
533 */
534 ~basic_regex()
535 { }
536
537 /**
538 * @brief Assigns one regular expression to another.
539 */
540 basic_regex&
541 operator=(const basic_regex&) = default;
542
543 /**
544 * @brief Move-assigns one regular expression to another.
545 */
546 basic_regex&
547 operator=(basic_regex&&) = default;
548
549 /**
550 * @brief Replaces a regular expression with a new one constructed from
551 * a C-style null-terminated string.
552 *
553 * @param __p A pointer to the start of a null-terminated C-style string
554 * containing a regular expression.
555 */
556 basic_regex&
557 operator=(const _Ch_type* __p)
558 { return this->assign(__p); }
559
560 /**
561 * @brief Replaces a regular expression with a new one constructed from
562 * an initializer list.
563 *
564 * @param __l The initializer list.
565 *
566 * @throws regex_error if @p __l is not a valid regular expression.
567 */
568 basic_regex&
569 operator=(initializer_list<_Ch_type> __l)
570 { return this->assign(__l); }
571
572 /**
573 * @brief Replaces a regular expression with a new one constructed from
574 * a string.
575 *
576 * @param __s A pointer to a string containing a regular expression.
577 */
578 template<typename _Ch_traits, typename _Alloc>
579 basic_regex&
580 operator=(const basic_string<_Ch_type, _Ch_traits, _Alloc>& __s)
581 { return this->assign(__s); }
582
583 // [7.8.3] assign
584 /**
585 * @brief Assigns one regular expression to another.
586 *
587 * @param __rhs Another regular expression object.
588 */
589 basic_regex&
590 assign(const basic_regex& __rhs) noexcept
591 { return *this = __rhs; }
592
593 /**
594 * @brief Move-assigns one regular expression to another.
595 *
596 * @param __rhs Another regular expression object.
597 */
598 basic_regex&
599 assign(basic_regex&& __rhs) noexcept
600 { return *this = std::move(__rhs); }
601
602 /**
603 * @brief Assigns a new regular expression to a regex object from a
604 * C-style null-terminated string containing a regular expression
605 * pattern.
606 *
607 * @param __p A pointer to a C-style null-terminated string containing
608 * a regular expression pattern.
609 * @param __flags Syntax option flags.
610 *
611 * @throws regex_error if __p does not contain a valid regular
612 * expression pattern interpreted according to @p __flags. If
613 * regex_error is thrown, *this remains unchanged.
614 */
615 basic_regex&
616 assign(const _Ch_type* __p, flag_type __flags = ECMAScript)
617 {
618 _M_compile(first: __p, last: __p + _Rx_traits::length(__p), f: __flags);
619 return *this;
620 }
621
622 /**
623 * @brief Assigns a new regular expression to a regex object from a
624 * C-style string containing a regular expression pattern.
625 *
626 * @param __p A pointer to a C-style string containing a
627 * regular expression pattern.
628 * @param __len The length of the regular expression pattern string.
629 * @param __flags Syntax option flags.
630 *
631 * @throws regex_error if p does not contain a valid regular
632 * expression pattern interpreted according to @p __flags. If
633 * regex_error is thrown, *this remains unchanged.
634 */
635 // _GLIBCXX_RESOLVE_LIB_DEFECTS
636 // 3296. Inconsistent default argument for basic_regex<>::assign
637 basic_regex&
638 assign(const _Ch_type* __p, size_t __len, flag_type __flags = ECMAScript)
639 {
640 _M_compile(first: __p, last: __p + __len, f: __flags);
641 return *this;
642 }
643
644 /**
645 * @brief Assigns a new regular expression to a regex object from a
646 * string containing a regular expression pattern.
647 *
648 * @param __s A string containing a regular expression pattern.
649 * @param __flags Syntax option flags.
650 *
651 * @throws regex_error if __s does not contain a valid regular
652 * expression pattern interpreted according to @p __flags. If
653 * regex_error is thrown, *this remains unchanged.
654 */
655 template<typename _Ch_traits, typename _Alloc>
656 basic_regex&
657 assign(const basic_string<_Ch_type, _Ch_traits, _Alloc>& __s,
658 flag_type __flags = ECMAScript)
659 {
660 _M_compile(first: __s.data(), last: __s.data() + __s.size(), f: __flags);
661 return *this;
662 }
663
664 /**
665 * @brief Assigns a new regular expression to a regex object.
666 *
667 * @param __first The start of a range containing a valid regular
668 * expression.
669 * @param __last The end of a range containing a valid regular
670 * expression.
671 * @param __flags Syntax option flags.
672 *
673 * @throws regex_error if p does not contain a valid regular
674 * expression pattern interpreted according to @p __flags. If
675 * regex_error is thrown, the object remains unchanged.
676 */
677 template<typename _InputIterator>
678 basic_regex&
679 assign(_InputIterator __first, _InputIterator __last,
680 flag_type __flags = ECMAScript)
681 {
682#if __cplusplus >= 201703L
683 using _ValT = typename iterator_traits<_InputIterator>::value_type;
684 if constexpr (__detail::__is_contiguous_iter<_InputIterator>::value
685 && is_same_v<_ValT, value_type>)
686 {
687 const auto __len = __last - __first;
688 const _Ch_type* __p = std::__to_address(__first);
689 _M_compile(first: __p, last: __p + __len, f: __flags);
690 }
691 else
692#endif
693 this->assign(string_type(__first, __last), __flags);
694 return *this;
695 }
696
697 /**
698 * @brief Assigns a new regular expression to a regex object.
699 *
700 * @param __l An initializer list representing a regular expression.
701 * @param __flags Syntax option flags.
702 *
703 * @throws regex_error if @p __l does not contain a valid
704 * regular expression pattern interpreted according to @p
705 * __flags. If regex_error is thrown, the object remains
706 * unchanged.
707 */
708 basic_regex&
709 assign(initializer_list<_Ch_type> __l, flag_type __flags = ECMAScript)
710 {
711 _M_compile(first: __l.begin(), last: __l.end(), f: __flags);
712 return *this;
713 }
714
715 // [7.8.4] const operations
716 /**
717 * @brief Gets the number of marked subexpressions within the regular
718 * expression.
719 */
720 unsigned int
721 mark_count() const noexcept
722 {
723 if (_M_automaton)
724 return _M_automaton->_M_sub_count() - 1;
725 return 0;
726 }
727
728 /**
729 * @brief Gets the flags used to construct the regular expression
730 * or in the last call to assign().
731 */
732 flag_type
733 flags() const noexcept
734 { return _M_flags; }
735
736 // [7.8.5] locale
737 /**
738 * @brief Imbues the regular expression object with the given locale.
739 *
740 * @param __loc A locale.
741 */
742 locale_type
743 imbue(locale_type __loc)
744 {
745 std::swap(__loc, _M_loc);
746 _M_automaton.reset();
747 return __loc;
748 }
749
750 /**
751 * @brief Gets the locale currently imbued in the regular expression
752 * object.
753 */
754 locale_type
755 getloc() const noexcept
756 { return _M_loc; }
757
758 // [7.8.6] swap
759 /**
760 * @brief Swaps the contents of two regular expression objects.
761 *
762 * @param __rhs Another regular expression object.
763 */
764 void
765 swap(basic_regex& __rhs) noexcept
766 {
767 std::swap(_M_flags, __rhs._M_flags);
768 std::swap(_M_loc, __rhs._M_loc);
769 std::swap(_M_automaton, __rhs._M_automaton);
770 }
771
772#ifdef _GLIBCXX_DEBUG
773 void
774 _M_dot(std::ostream& __ostr)
775 { _M_automaton->_M_dot(__ostr); }
776#endif
777
778 private:
779 typedef std::shared_ptr<const __detail::_NFA<_Rx_traits>> _AutomatonPtr;
780
781 void
782 _M_compile(const _Ch_type* __first, const _Ch_type* __last,
783 flag_type __f)
784 {
785 __detail::_Compiler<_Rx_traits> __c(__first, __last, _M_loc, __f);
786 _M_automaton = __c._M_get_nfa();
787 _M_flags = __f;
788 }
789
790 template<typename _Bp, typename _Ap, typename _Cp, typename _Rp,
791 __detail::_RegexExecutorPolicy, bool>
792 friend bool
793 __detail::__regex_algo_impl(_Bp, _Bp, match_results<_Bp, _Ap>&,
794 const basic_regex<_Cp, _Rp>&,
795 regex_constants::match_flag_type);
796
797 template<typename, typename, typename, bool>
798 friend class __detail::_Executor;
799
800 flag_type _M_flags;
801 locale_type _M_loc;
802 _AutomatonPtr _M_automaton;
803 };
804
805#if __cplusplus < 201703L
806 template<typename _Ch, typename _Tr>
807 constexpr regex_constants::syntax_option_type
808 basic_regex<_Ch, _Tr>::icase;
809
810 template<typename _Ch, typename _Tr>
811 constexpr regex_constants::syntax_option_type
812 basic_regex<_Ch, _Tr>::nosubs;
813
814 template<typename _Ch, typename _Tr>
815 constexpr regex_constants::syntax_option_type
816 basic_regex<_Ch, _Tr>::optimize;
817
818 template<typename _Ch, typename _Tr>
819 constexpr regex_constants::syntax_option_type
820 basic_regex<_Ch, _Tr>::collate;
821
822 template<typename _Ch, typename _Tr>
823 constexpr regex_constants::syntax_option_type
824 basic_regex<_Ch, _Tr>::ECMAScript;
825
826 template<typename _Ch, typename _Tr>
827 constexpr regex_constants::syntax_option_type
828 basic_regex<_Ch, _Tr>::basic;
829
830 template<typename _Ch, typename _Tr>
831 constexpr regex_constants::syntax_option_type
832 basic_regex<_Ch, _Tr>::extended;
833
834 template<typename _Ch, typename _Tr>
835 constexpr regex_constants::syntax_option_type
836 basic_regex<_Ch, _Tr>::awk;
837
838 template<typename _Ch, typename _Tr>
839 constexpr regex_constants::syntax_option_type
840 basic_regex<_Ch, _Tr>::grep;
841
842 template<typename _Ch, typename _Tr>
843 constexpr regex_constants::syntax_option_type
844 basic_regex<_Ch, _Tr>::egrep;
845#endif // ! C++17
846
847#if __cpp_deduction_guides >= 201606
848 template<typename _ForwardIterator>
849 basic_regex(_ForwardIterator, _ForwardIterator,
850 regex_constants::syntax_option_type = {})
851 -> basic_regex<typename iterator_traits<_ForwardIterator>::value_type>;
852#endif
853
854 /** @brief Standard regular expressions. */
855 typedef basic_regex<char> regex;
856
857#ifdef _GLIBCXX_USE_WCHAR_T
858 /** @brief Standard wide-character regular expressions. */
859 typedef basic_regex<wchar_t> wregex;
860#endif
861
862
863 // [7.8.6] basic_regex swap
864 /**
865 * @brief Swaps the contents of two regular expression objects.
866 * @param __lhs First regular expression.
867 * @param __rhs Second regular expression.
868 * @relates basic_regex
869 */
870 template<typename _Ch_type, typename _Rx_traits>
871 inline void
872 swap(basic_regex<_Ch_type, _Rx_traits>& __lhs,
873 basic_regex<_Ch_type, _Rx_traits>& __rhs) noexcept
874 { __lhs.swap(__rhs); }
875
876
877 // C++11 28.9 [re.submatch] Class template sub_match
878 /**
879 * A sequence of characters matched by a particular marked sub-expression.
880 *
881 * An object of this class is essentially a pair of iterators marking a
882 * matched subexpression within a regular expression pattern match. Such
883 * objects can be converted to and compared with std::basic_string objects
884 * of a similar base character type as the pattern matched by the regular
885 * expression.
886 *
887 * The iterators that make up the pair are the usual half-open interval
888 * referencing the actual original pattern matched.
889 */
890 template<typename _BiIter>
891 class sub_match : public std::pair<_BiIter, _BiIter>
892 {
893 typedef iterator_traits<_BiIter> __iter_traits;
894
895 public:
896 typedef typename __iter_traits::value_type value_type;
897 typedef typename __iter_traits::difference_type difference_type;
898 typedef _BiIter iterator;
899 typedef basic_string<value_type> string_type;
900
901 bool matched;
902
903 constexpr sub_match() noexcept : matched() { }
904
905 /// Gets the length of the matching sequence.
906 difference_type
907 length() const noexcept
908 { return this->matched ? std::distance(this->first, this->second) : 0; }
909
910 /**
911 * @brief Gets the matching sequence as a string.
912 *
913 * @returns the matching sequence as a string.
914 *
915 * This is the implicit conversion operator. It is identical to the
916 * str() member function except that it will want to pop up in
917 * unexpected places and cause a great deal of confusion and cursing
918 * from the unwary.
919 */
920 operator string_type() const
921 { return str(); }
922
923 /**
924 * @brief Gets the matching sequence as a string.
925 *
926 * @returns the matching sequence as a string.
927 */
928 string_type
929 str() const
930 {
931 return this->matched
932 ? string_type(this->first, this->second)
933 : string_type();
934 }
935
936 /**
937 * @brief Compares this and another matched sequence.
938 *
939 * @param __s Another matched sequence to compare to this one.
940 *
941 * @retval negative This matched sequence will collate before `__s`.
942 * @retval zero This matched sequence is equivalent to `__s`.
943 * @retval positive This matched sequence will collate after `__s`.
944 */
945 int
946 compare(const sub_match& __s) const
947 { return this->_M_str().compare(__s._M_str()); }
948
949 /**
950 * @{
951 * @brief Compares this `sub_match` to a string.
952 *
953 * @param __s A string to compare to this `sub_match`.
954 *
955 * @retval negative This matched sequence will collate before `__s`.
956 * @retval zero This matched sequence is equivalent to `__s`.
957 * @retval positive This matched sequence will collate after `__s`.
958 */
959 int
960 compare(const string_type& __s) const
961 { return this->_M_str().compare(__s); }
962
963 int
964 compare(const value_type* __s) const
965 { return this->_M_str().compare(__s); }
966 /// @}
967
968 /// @cond undocumented
969 // Non-standard, used by comparison operators
970 int
971 _M_compare(const value_type* __s, size_t __n) const
972 { return this->_M_str().compare({__s, __n}); }
973 /// @endcond
974
975 private:
976 // Simplified basic_string_view for C++11
977 struct __string_view
978 {
979 using traits_type = typename string_type::traits_type;
980
981 __string_view() = default;
982
983 __string_view(const value_type* __s, size_t __n) noexcept
984 : _M_data(__s), _M_len(__n) { }
985
986 __string_view(const value_type* __s) noexcept
987 : _M_data(__s), _M_len(traits_type::length(__s)) { }
988
989 __string_view(const string_type& __s) noexcept
990 : _M_data(__s.data()), _M_len(__s.length()) { }
991
992 int
993 compare(__string_view __s) const noexcept
994 {
995 if (const size_t __n = std::min(_M_len, __s._M_len))
996 if (int __ret = traits_type::compare(_M_data, __s._M_data, __n))
997 return __ret;
998 using __limits = __gnu_cxx::__int_traits<int>;
999 const difference_type __diff = _M_len - __s._M_len;
1000 if (__diff > __limits::__max)
1001 return __limits::__max;
1002 if (__diff < __limits::__min)
1003 return __limits::__min;
1004 return static_cast<int>(__diff);
1005 }
1006
1007 private:
1008 const value_type* _M_data = nullptr;
1009 size_t _M_len = 0;
1010 };
1011
1012 // Create a __string_view over the iterator range.
1013 template<typename _Iter = _BiIter>
1014 __enable_if_t<__detail::__is_contiguous_iter<_Iter>::value,
1015 __string_view>
1016 _M_str() const noexcept
1017 {
1018 if (this->matched)
1019 if (size_t __len = this->second - this->first)
1020 return { std::__addressof(*this->first), __len };
1021 return {};
1022 }
1023
1024 // Create a temporary string that can be converted to __string_view.
1025 template<typename _Iter = _BiIter>
1026 __enable_if_t<!__detail::__is_contiguous_iter<_Iter>::value,
1027 string_type>
1028 _M_str() const
1029 { return str(); }
1030 };
1031
1032
1033 /** @brief Standard regex submatch over a C-style null-terminated string. */
1034 typedef sub_match<const char*> csub_match;
1035
1036 /** @brief Standard regex submatch over a standard string. */
1037 typedef sub_match<string::const_iterator> ssub_match;
1038
1039#ifdef _GLIBCXX_USE_WCHAR_T
1040 /** @brief Regex submatch over a C-style null-terminated wide string. */
1041 typedef sub_match<const wchar_t*> wcsub_match;
1042
1043 /** @brief Regex submatch over a standard wide string. */
1044 typedef sub_match<wstring::const_iterator> wssub_match;
1045#endif
1046
1047 // [7.9.2] sub_match non-member operators
1048
1049 /// @relates sub_match @{
1050
1051 /**
1052 * @brief Tests the equivalence of two regular expression submatches.
1053 * @param __lhs First regular expression submatch.
1054 * @param __rhs Second regular expression submatch.
1055 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1056 */
1057 template<typename _BiIter>
1058 inline bool
1059 operator==(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
1060 { return __lhs.compare(__rhs) == 0; }
1061
1062#if __cpp_lib_three_way_comparison
1063 /**
1064 * @brief Three-way comparison of two regular expression submatches.
1065 * @param __lhs First regular expression submatch.
1066 * @param __rhs Second regular expression submatch.
1067 * @returns A value indicating whether `__lhs` is less than, equal to,
1068 * greater than, or incomparable with `__rhs`.
1069 */
1070 template<typename _BiIter>
1071 inline auto
1072 operator<=>(const sub_match<_BiIter>& __lhs,
1073 const sub_match<_BiIter>& __rhs)
1074 noexcept(__detail::__is_contiguous_iter<_BiIter>::value)
1075 {
1076 using _Tr = char_traits<typename iterator_traits<_BiIter>::value_type>;
1077 return __detail::__char_traits_cmp_cat<_Tr>(__lhs.compare(__rhs));
1078 }
1079#else
1080 /**
1081 * @brief Tests the inequivalence of two regular expression submatches.
1082 * @param __lhs First regular expression submatch.
1083 * @param __rhs Second regular expression submatch.
1084 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1085 */
1086 template<typename _BiIter>
1087 inline bool
1088 operator!=(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
1089 { return __lhs.compare(__rhs) != 0; }
1090
1091 /**
1092 * @brief Tests the ordering of two regular expression submatches.
1093 * @param __lhs First regular expression submatch.
1094 * @param __rhs Second regular expression submatch.
1095 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1096 */
1097 template<typename _BiIter>
1098 inline bool
1099 operator<(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
1100 { return __lhs.compare(__rhs) < 0; }
1101
1102 /**
1103 * @brief Tests the ordering of two regular expression submatches.
1104 * @param __lhs First regular expression submatch.
1105 * @param __rhs Second regular expression submatch.
1106 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1107 */
1108 template<typename _BiIter>
1109 inline bool
1110 operator<=(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
1111 { return __lhs.compare(__rhs) <= 0; }
1112
1113 /**
1114 * @brief Tests the ordering of two regular expression submatches.
1115 * @param __lhs First regular expression submatch.
1116 * @param __rhs Second regular expression submatch.
1117 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1118 */
1119 template<typename _BiIter>
1120 inline bool
1121 operator>=(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
1122 { return __lhs.compare(__rhs) >= 0; }
1123
1124 /**
1125 * @brief Tests the ordering of two regular expression submatches.
1126 * @param __lhs First regular expression submatch.
1127 * @param __rhs Second regular expression submatch.
1128 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1129 */
1130 template<typename _BiIter>
1131 inline bool
1132 operator>(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
1133 { return __lhs.compare(__rhs) > 0; }
1134#endif // three-way comparison
1135
1136 /// @cond undocumented
1137
1138 // Alias for a basic_string that can be compared to a sub_match.
1139 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1140 using __sub_match_string = basic_string<
1141 typename iterator_traits<_Bi_iter>::value_type,
1142 _Ch_traits, _Ch_alloc>;
1143 /// @endcond
1144
1145#if ! __cpp_lib_three_way_comparison
1146 /**
1147 * @brief Tests the equivalence of a string and a regular expression
1148 * submatch.
1149 * @param __lhs A string.
1150 * @param __rhs A regular expression submatch.
1151 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1152 */
1153 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1154 inline bool
1155 operator==(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1156 const sub_match<_Bi_iter>& __rhs)
1157 { return __rhs._M_compare(__lhs.data(), __lhs.size()) == 0; }
1158
1159 /**
1160 * @brief Tests the inequivalence of a string and a regular expression
1161 * submatch.
1162 * @param __lhs A string.
1163 * @param __rhs A regular expression submatch.
1164 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1165 */
1166 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1167 inline bool
1168 operator!=(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1169 const sub_match<_Bi_iter>& __rhs)
1170 { return !(__lhs == __rhs); }
1171
1172 /**
1173 * @brief Tests the ordering of a string and a regular expression submatch.
1174 * @param __lhs A string.
1175 * @param __rhs A regular expression submatch.
1176 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1177 */
1178 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1179 inline bool
1180 operator<(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1181 const sub_match<_Bi_iter>& __rhs)
1182 { return __rhs._M_compare(__lhs.data(), __lhs.size()) > 0; }
1183
1184 /**
1185 * @brief Tests the ordering of a string and a regular expression submatch.
1186 * @param __lhs A string.
1187 * @param __rhs A regular expression submatch.
1188 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1189 */
1190 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1191 inline bool
1192 operator>(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1193 const sub_match<_Bi_iter>& __rhs)
1194 { return __rhs < __lhs; }
1195
1196 /**
1197 * @brief Tests the ordering of a string and a regular expression submatch.
1198 * @param __lhs A string.
1199 * @param __rhs A regular expression submatch.
1200 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1201 */
1202 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1203 inline bool
1204 operator>=(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1205 const sub_match<_Bi_iter>& __rhs)
1206 { return !(__lhs < __rhs); }
1207
1208 /**
1209 * @brief Tests the ordering of a string and a regular expression submatch.
1210 * @param __lhs A string.
1211 * @param __rhs A regular expression submatch.
1212 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1213 */
1214 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1215 inline bool
1216 operator<=(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1217 const sub_match<_Bi_iter>& __rhs)
1218 { return !(__rhs < __lhs); }
1219#endif // three-way comparison
1220
1221 /**
1222 * @brief Tests the equivalence of a regular expression submatch and a
1223 * string.
1224 * @param __lhs A regular expression submatch.
1225 * @param __rhs A string.
1226 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1227 */
1228 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1229 inline bool
1230 operator==(const sub_match<_Bi_iter>& __lhs,
1231 const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1232 { return __lhs._M_compare(__rhs.data(), __rhs.size()) == 0; }
1233
1234#if __cpp_lib_three_way_comparison
1235 /**
1236 * @brief Three-way comparison of a regular expression submatch and a string.
1237 * @param __lhs A regular expression submatch.
1238 * @param __rhs A string.
1239 * @returns A value indicating whether `__lhs` is less than, equal to,
1240 * greater than, or incomparable with `__rhs`.
1241 */
1242 template<typename _Bi_iter, typename _Ch_traits, typename _Alloc>
1243 inline auto
1244 operator<=>(const sub_match<_Bi_iter>& __lhs,
1245 const __sub_match_string<_Bi_iter, _Ch_traits, _Alloc>& __rhs)
1246 noexcept(__detail::__is_contiguous_iter<_Bi_iter>::value)
1247 {
1248 return __detail::__char_traits_cmp_cat<_Ch_traits>(
1249 __lhs._M_compare(__rhs.data(), __rhs.size()));
1250 }
1251#else
1252 /**
1253 * @brief Tests the inequivalence of a regular expression submatch and a
1254 * string.
1255 * @param __lhs A regular expression submatch.
1256 * @param __rhs A string.
1257 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1258 */
1259 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1260 inline bool
1261 operator!=(const sub_match<_Bi_iter>& __lhs,
1262 const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1263 { return !(__lhs == __rhs); }
1264
1265 /**
1266 * @brief Tests the ordering of a regular expression submatch and a string.
1267 * @param __lhs A regular expression submatch.
1268 * @param __rhs A string.
1269 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1270 */
1271 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1272 inline bool
1273 operator<(const sub_match<_Bi_iter>& __lhs,
1274 const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1275 { return __lhs._M_compare(__rhs.data(), __rhs.size()) < 0; }
1276
1277 /**
1278 * @brief Tests the ordering of a regular expression submatch and a string.
1279 * @param __lhs A regular expression submatch.
1280 * @param __rhs A string.
1281 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1282 */
1283 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1284 inline bool
1285 operator>(const sub_match<_Bi_iter>& __lhs,
1286 const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1287 { return __rhs < __lhs; }
1288
1289 /**
1290 * @brief Tests the ordering of a regular expression submatch and a string.
1291 * @param __lhs A regular expression submatch.
1292 * @param __rhs A string.
1293 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1294 */
1295 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1296 inline bool
1297 operator>=(const sub_match<_Bi_iter>& __lhs,
1298 const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1299 { return !(__lhs < __rhs); }
1300
1301 /**
1302 * @brief Tests the ordering of a regular expression submatch and a string.
1303 * @param __lhs A regular expression submatch.
1304 * @param __rhs A string.
1305 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1306 */
1307 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1308 inline bool
1309 operator<=(const sub_match<_Bi_iter>& __lhs,
1310 const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1311 { return !(__rhs < __lhs); }
1312
1313 /**
1314 * @brief Tests the equivalence of a C string and a regular expression
1315 * submatch.
1316 * @param __lhs A null-terminated string.
1317 * @param __rhs A regular expression submatch.
1318 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1319 */
1320 template<typename _Bi_iter>
1321 inline bool
1322 operator==(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1323 const sub_match<_Bi_iter>& __rhs)
1324 { return __rhs.compare(__lhs) == 0; }
1325
1326 /**
1327 * @brief Tests the inequivalence of a C string and a regular
1328 * expression submatch.
1329 * @param __lhs A null-terminated string.
1330 * @param __rhs A regular expression submatch.
1331 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1332 */
1333 template<typename _Bi_iter>
1334 inline bool
1335 operator!=(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1336 const sub_match<_Bi_iter>& __rhs)
1337 { return !(__lhs == __rhs); }
1338
1339 /**
1340 * @brief Tests the ordering of a C string and a regular expression submatch.
1341 * @param __lhs A null-terminated string.
1342 * @param __rhs A regular expression submatch.
1343 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1344 */
1345 template<typename _Bi_iter>
1346 inline bool
1347 operator<(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1348 const sub_match<_Bi_iter>& __rhs)
1349 { return __rhs.compare(__lhs) > 0; }
1350
1351 /**
1352 * @brief Tests the ordering of a C string and a regular expression submatch.
1353 * @param __lhs A null-terminated string.
1354 * @param __rhs A regular expression submatch.
1355 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1356 */
1357 template<typename _Bi_iter>
1358 inline bool
1359 operator>(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1360 const sub_match<_Bi_iter>& __rhs)
1361 { return __rhs < __lhs; }
1362
1363 /**
1364 * @brief Tests the ordering of a C string and a regular expression submatch.
1365 * @param __lhs A null-terminated string.
1366 * @param __rhs A regular expression submatch.
1367 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1368 */
1369 template<typename _Bi_iter>
1370 inline bool
1371 operator>=(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1372 const sub_match<_Bi_iter>& __rhs)
1373 { return !(__lhs < __rhs); }
1374
1375 /**
1376 * @brief Tests the ordering of a C string and a regular expression submatch.
1377 * @param __lhs A null-terminated string.
1378 * @param __rhs A regular expression submatch.
1379 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1380 */
1381 template<typename _Bi_iter>
1382 inline bool
1383 operator<=(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1384 const sub_match<_Bi_iter>& __rhs)
1385 { return !(__rhs < __lhs); }
1386#endif // three-way comparison
1387
1388 /**
1389 * @brief Tests the equivalence of a regular expression submatch and a C
1390 * string.
1391 * @param __lhs A regular expression submatch.
1392 * @param __rhs A null-terminated string.
1393 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1394 */
1395 template<typename _Bi_iter>
1396 inline bool
1397 operator==(const sub_match<_Bi_iter>& __lhs,
1398 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1399 { return __lhs.compare(__rhs) == 0; }
1400
1401#if __cpp_lib_three_way_comparison
1402 /**
1403 * @brief Three-way comparison of a regular expression submatch and a C
1404 * string.
1405 * @param __lhs A regular expression submatch.
1406 * @param __rhs A null-terminated string.
1407 * @returns A value indicating whether `__lhs` is less than, equal to,
1408 * greater than, or incomparable with `__rhs`.
1409 */
1410 template<typename _Bi_iter>
1411 inline auto
1412 operator<=>(const sub_match<_Bi_iter>& __lhs,
1413 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1414 noexcept(__detail::__is_contiguous_iter<_Bi_iter>::value)
1415 {
1416 using _Tr = char_traits<typename iterator_traits<_Bi_iter>::value_type>;
1417 return __detail::__char_traits_cmp_cat<_Tr>(__lhs.compare(__rhs));
1418 }
1419#else
1420 /**
1421 * @brief Tests the inequivalence of a regular expression submatch and a
1422 * string.
1423 * @param __lhs A regular expression submatch.
1424 * @param __rhs A null-terminated string.
1425 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1426 */
1427 template<typename _Bi_iter>
1428 inline bool
1429 operator!=(const sub_match<_Bi_iter>& __lhs,
1430 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1431 { return !(__lhs == __rhs); }
1432
1433 /**
1434 * @brief Tests the ordering of a regular expression submatch and a C string.
1435 * @param __lhs A regular expression submatch.
1436 * @param __rhs A null-terminated string.
1437 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1438 */
1439 template<typename _Bi_iter>
1440 inline bool
1441 operator<(const sub_match<_Bi_iter>& __lhs,
1442 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1443 { return __lhs.compare(__rhs) < 0; }
1444
1445 /**
1446 * @brief Tests the ordering of a regular expression submatch and a C string.
1447 * @param __lhs A regular expression submatch.
1448 * @param __rhs A null-terminated string.
1449 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1450 */
1451 template<typename _Bi_iter>
1452 inline bool
1453 operator>(const sub_match<_Bi_iter>& __lhs,
1454 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1455 { return __rhs < __lhs; }
1456
1457 /**
1458 * @brief Tests the ordering of a regular expression submatch and a C string.
1459 * @param __lhs A regular expression submatch.
1460 * @param __rhs A null-terminated string.
1461 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1462 */
1463 template<typename _Bi_iter>
1464 inline bool
1465 operator>=(const sub_match<_Bi_iter>& __lhs,
1466 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1467 { return !(__lhs < __rhs); }
1468
1469 /**
1470 * @brief Tests the ordering of a regular expression submatch and a C string.
1471 * @param __lhs A regular expression submatch.
1472 * @param __rhs A null-terminated string.
1473 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1474 */
1475 template<typename _Bi_iter>
1476 inline bool
1477 operator<=(const sub_match<_Bi_iter>& __lhs,
1478 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1479 { return !(__rhs < __lhs); }
1480
1481 /**
1482 * @brief Tests the equivalence of a character and a regular expression
1483 * submatch.
1484 * @param __lhs A character.
1485 * @param __rhs A regular expression submatch.
1486 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1487 */
1488 template<typename _Bi_iter>
1489 inline bool
1490 operator==(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1491 const sub_match<_Bi_iter>& __rhs)
1492 { return __rhs._M_compare(std::__addressof(__lhs), 1) == 0; }
1493
1494 /**
1495 * @brief Tests the inequivalence of a character and a regular expression
1496 * submatch.
1497 * @param __lhs A character.
1498 * @param __rhs A regular expression submatch.
1499 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1500 */
1501 template<typename _Bi_iter>
1502 inline bool
1503 operator!=(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1504 const sub_match<_Bi_iter>& __rhs)
1505 { return !(__lhs == __rhs); }
1506
1507 /**
1508 * @brief Tests the ordering of a character and a regular expression
1509 * submatch.
1510 * @param __lhs A character.
1511 * @param __rhs A regular expression submatch.
1512 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1513 */
1514 template<typename _Bi_iter>
1515 inline bool
1516 operator<(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1517 const sub_match<_Bi_iter>& __rhs)
1518 { return __rhs._M_compare(std::__addressof(__lhs), 1) > 0; }
1519
1520 /**
1521 * @brief Tests the ordering of a character and a regular expression
1522 * submatch.
1523 * @param __lhs A character.
1524 * @param __rhs A regular expression submatch.
1525 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1526 */
1527 template<typename _Bi_iter>
1528 inline bool
1529 operator>(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1530 const sub_match<_Bi_iter>& __rhs)
1531 { return __rhs < __lhs; }
1532
1533 /**
1534 * @brief Tests the ordering of a character and a regular expression
1535 * submatch.
1536 * @param __lhs A character.
1537 * @param __rhs A regular expression submatch.
1538 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1539 */
1540 template<typename _Bi_iter>
1541 inline bool
1542 operator>=(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1543 const sub_match<_Bi_iter>& __rhs)
1544 { return !(__lhs < __rhs); }
1545
1546 /**
1547 * @brief Tests the ordering of a character and a regular expression
1548 * submatch.
1549 * @param __lhs A character.
1550 * @param __rhs A regular expression submatch.
1551 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1552 */
1553 template<typename _Bi_iter>
1554 inline bool
1555 operator<=(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1556 const sub_match<_Bi_iter>& __rhs)
1557 { return !(__rhs < __lhs); }
1558#endif // three-way comparison
1559
1560 /**
1561 * @brief Tests the equivalence of a regular expression submatch and a
1562 * character.
1563 * @param __lhs A regular expression submatch.
1564 * @param __rhs A character.
1565 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1566 */
1567 template<typename _Bi_iter>
1568 inline bool
1569 operator==(const sub_match<_Bi_iter>& __lhs,
1570 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1571 { return __lhs._M_compare(std::__addressof(__rhs), 1) == 0; }
1572
1573#if __cpp_lib_three_way_comparison
1574 /**
1575 * @brief Three-way comparison of a regular expression submatch and a
1576 * character.
1577 * @param __lhs A regular expression submatch.
1578 * @param __rhs A character.
1579 * @returns A value indicating whether `__lhs` is less than, equal to,
1580 * greater than, or incomparable with `__rhs`.
1581 */
1582
1583 template<typename _Bi_iter>
1584 inline auto
1585 operator<=>(const sub_match<_Bi_iter>& __lhs,
1586 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1587 noexcept(__detail::__is_contiguous_iter<_Bi_iter>::value)
1588 {
1589 using _Tr = char_traits<typename iterator_traits<_Bi_iter>::value_type>;
1590 return __detail::__char_traits_cmp_cat<_Tr>(
1591 __lhs._M_compare(std::__addressof(__rhs), 1));
1592 }
1593#else
1594 /**
1595 * @brief Tests the inequivalence of a regular expression submatch and a
1596 * character.
1597 * @param __lhs A regular expression submatch.
1598 * @param __rhs A character.
1599 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1600 */
1601 template<typename _Bi_iter>
1602 inline bool
1603 operator!=(const sub_match<_Bi_iter>& __lhs,
1604 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1605 { return !(__lhs == __rhs); }
1606
1607 /**
1608 * @brief Tests the ordering of a regular expression submatch and a
1609 * character.
1610 * @param __lhs A regular expression submatch.
1611 * @param __rhs A character.
1612 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1613 */
1614 template<typename _Bi_iter>
1615 inline bool
1616 operator<(const sub_match<_Bi_iter>& __lhs,
1617 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1618 { return __lhs._M_compare(std::__addressof(__rhs), 1) < 0; }
1619
1620 /**
1621 * @brief Tests the ordering of a regular expression submatch and a
1622 * character.
1623 * @param __lhs A regular expression submatch.
1624 * @param __rhs A character.
1625 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1626 */
1627 template<typename _Bi_iter>
1628 inline bool
1629 operator>(const sub_match<_Bi_iter>& __lhs,
1630 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1631 { return __rhs < __lhs; }
1632
1633 /**
1634 * @brief Tests the ordering of a regular expression submatch and a
1635 * character.
1636 * @param __lhs A regular expression submatch.
1637 * @param __rhs A character.
1638 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1639 */
1640 template<typename _Bi_iter>
1641 inline bool
1642 operator>=(const sub_match<_Bi_iter>& __lhs,
1643 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1644 { return !(__lhs < __rhs); }
1645
1646 /**
1647 * @brief Tests the ordering of a regular expression submatch and a
1648 * character.
1649 * @param __lhs A regular expression submatch.
1650 * @param __rhs A character.
1651 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1652 */
1653 template<typename _Bi_iter>
1654 inline bool
1655 operator<=(const sub_match<_Bi_iter>& __lhs,
1656 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1657 { return !(__rhs < __lhs); }
1658#endif // three-way comparison
1659
1660 /**
1661 * @brief Inserts a matched string into an output stream.
1662 *
1663 * @param __os The output stream.
1664 * @param __m A submatch string.
1665 *
1666 * @returns the output stream with the submatch string inserted.
1667 */
1668 template<typename _Ch_type, typename _Ch_traits, typename _Bi_iter>
1669 inline
1670 basic_ostream<_Ch_type, _Ch_traits>&
1671 operator<<(basic_ostream<_Ch_type, _Ch_traits>& __os,
1672 const sub_match<_Bi_iter>& __m)
1673 { return __os << __m.str(); }
1674
1675 /// @} relates sub_match
1676
1677 // [7.10] Class template match_results
1678
1679 /**
1680 * @brief The results of a match or search operation.
1681 *
1682 * A collection of character sequences representing the result of a regular
1683 * expression match. Storage for the collection is allocated and freed as
1684 * necessary by the member functions of class template match_results.
1685 *
1686 * This class satisfies the Sequence requirements, with the exception that
1687 * only the operations defined for a const-qualified Sequence are supported.
1688 *
1689 * The sub_match object stored at index 0 represents sub-expression 0, i.e.
1690 * the whole match. In this case the %sub_match member matched is always true.
1691 * The sub_match object stored at index n denotes what matched the marked
1692 * sub-expression n within the matched expression. If the sub-expression n
1693 * participated in a regular expression match then the %sub_match member
1694 * matched evaluates to true, and members first and second denote the range
1695 * of characters [first, second) which formed that match. Otherwise matched
1696 * is false, and members first and second point to the end of the sequence
1697 * that was searched.
1698 */
1699 template<typename _Bi_iter,
1700 typename _Alloc = allocator<sub_match<_Bi_iter> > >
1701 class match_results
1702 : private std::vector<sub_match<_Bi_iter>, _Alloc>
1703 {
1704 private:
1705 /*
1706 * The vector base is empty if this does not represent a match (!ready());
1707 * Otherwise if it's a match failure, it contains 3 elements:
1708 * [0] unmatched
1709 * [1] prefix
1710 * [2] suffix
1711 * Otherwise it contains n+4 elements where n is the number of marked
1712 * sub-expressions:
1713 * [0] entire match
1714 * [1] 1st marked subexpression
1715 * ...
1716 * [n] nth marked subexpression
1717 * [n+1] unmatched
1718 * [n+2] prefix
1719 * [n+3] suffix
1720 */
1721 typedef std::vector<sub_match<_Bi_iter>, _Alloc> _Base_type;
1722 typedef std::iterator_traits<_Bi_iter> __iter_traits;
1723 typedef regex_constants::match_flag_type match_flag_type;
1724
1725 public:
1726 /**
1727 * @name 28.10 Public Types
1728 */
1729 ///@{
1730 typedef sub_match<_Bi_iter> value_type;
1731 typedef const value_type& const_reference;
1732 typedef value_type& reference;
1733 typedef typename _Base_type::const_iterator const_iterator;
1734 typedef const_iterator iterator;
1735 typedef typename __iter_traits::difference_type difference_type;
1736 typedef typename allocator_traits<_Alloc>::size_type size_type;
1737 typedef _Alloc allocator_type;
1738 typedef typename __iter_traits::value_type char_type;
1739 typedef std::basic_string<char_type> string_type;
1740 ///@}
1741
1742 public:
1743 /**
1744 * @name 28.10.1 Construction, Copying, and Destruction
1745 */
1746 ///@{
1747
1748 /**
1749 * @brief Constructs a default %match_results container.
1750 * @post size() returns 0 and str() returns an empty string.
1751 */
1752 match_results() : match_results(_Alloc()) { }
1753
1754 /**
1755 * @brief Constructs a default %match_results container.
1756 * @post size() returns 0 and str() returns an empty string.
1757 */
1758 explicit
1759 match_results(const _Alloc& __a) noexcept
1760 : _Base_type(__a)
1761 { }
1762
1763 /**
1764 * @brief Copy constructs a %match_results.
1765 */
1766 match_results(const match_results&) = default;
1767
1768 /**
1769 * @brief Move constructs a %match_results.
1770 */
1771 match_results(match_results&&) noexcept = default;
1772
1773 /**
1774 * @brief Assigns rhs to *this.
1775 */
1776 match_results&
1777 operator=(const match_results&) = default;
1778
1779 /**
1780 * @brief Move-assigns rhs to *this.
1781 */
1782 match_results&
1783 operator=(match_results&&) = default;
1784
1785 /**
1786 * @brief Destroys a %match_results object.
1787 */
1788 ~match_results() = default;
1789
1790 ///@}
1791
1792 // 28.10.2, state:
1793 /**
1794 * @brief Indicates if the %match_results is ready.
1795 * @retval true The object has a fully-established result state.
1796 * @retval false The object is not ready.
1797 */
1798 bool ready() const noexcept { return !_Base_type::empty(); }
1799
1800 /**
1801 * @name 28.10.2 Size
1802 */
1803 ///@{
1804
1805 /**
1806 * @brief Gets the number of matches and submatches.
1807 *
1808 * The number of matches for a given regular expression will be either 0
1809 * if there was no match or mark_count() + 1 if a match was successful.
1810 * Some matches may be empty.
1811 *
1812 * @returns the number of matches found.
1813 */
1814 size_type
1815 size() const noexcept
1816 { return _Base_type::empty() ? 0 : _Base_type::size() - 3; }
1817
1818 size_type
1819 max_size() const noexcept
1820 { return _Base_type::max_size() - 3; }
1821
1822 /**
1823 * @brief Indicates if the %match_results contains no results.
1824 * @retval true The %match_results object is empty.
1825 * @retval false The %match_results object is not empty.
1826 */
1827 _GLIBCXX_NODISCARD bool
1828 empty() const noexcept
1829 { return _Base_type::size() <= 3; }
1830
1831 ///@}
1832
1833 /**
1834 * @name 28.10.4 Element Access
1835 */
1836 ///@{
1837
1838 /**
1839 * @brief Gets the length of the indicated submatch.
1840 * @param __sub indicates the submatch.
1841 * @pre ready() == true
1842 *
1843 * This function returns the length of the indicated submatch, or the
1844 * length of the entire match if @p __sub is zero (the default).
1845 */
1846 difference_type
1847 length(size_type __sub = 0) const
1848 { return (*this)[__sub].length(); }
1849
1850 /**
1851 * @brief Gets the offset of the beginning of the indicated submatch.
1852 * @param __sub indicates the submatch.
1853 * @pre ready() == true
1854 *
1855 * This function returns the offset from the beginning of the target
1856 * sequence to the beginning of the submatch, unless the value of @p __sub
1857 * is zero (the default), in which case this function returns the offset
1858 * from the beginning of the target sequence to the beginning of the
1859 * match.
1860 */
1861 difference_type
1862 position(size_type __sub = 0) const
1863 { return std::distance(_M_begin, (*this)[__sub].first); }
1864
1865 /**
1866 * @brief Gets the match or submatch converted to a string type.
1867 * @param __sub indicates the submatch.
1868 * @pre ready() == true
1869 *
1870 * This function gets the submatch (or match, if @p __sub is
1871 * zero) extracted from the target range and converted to the
1872 * associated string type.
1873 */
1874 string_type
1875 str(size_type __sub = 0) const
1876 { return string_type((*this)[__sub]); }
1877
1878 /**
1879 * @brief Gets a %sub_match reference for the match or submatch.
1880 * @param __sub indicates the submatch.
1881 * @pre ready() == true
1882 *
1883 * This function gets a reference to the indicated submatch, or
1884 * the entire match if @p __sub is zero.
1885 *
1886 * If @p __sub >= size() then this function returns a %sub_match with a
1887 * special value indicating no submatch.
1888 */
1889 const_reference
1890 operator[](size_type __sub) const
1891 {
1892 __glibcxx_assert( ready() );
1893 return __sub < size()
1894 ? _Base_type::operator[](__sub)
1895 : _M_unmatched_sub();
1896 }
1897
1898 /**
1899 * @brief Gets a %sub_match representing the match prefix.
1900 * @pre ready() == true
1901 *
1902 * This function gets a reference to a %sub_match object representing the
1903 * part of the target range between the start of the target range and the
1904 * start of the match.
1905 */
1906 const_reference
1907 prefix() const
1908 {
1909 __glibcxx_assert( ready() );
1910 return !empty() ? _M_prefix() : _M_unmatched_sub();
1911 }
1912
1913 /**
1914 * @brief Gets a %sub_match representing the match suffix.
1915 * @pre ready() == true
1916 *
1917 * This function gets a reference to a %sub_match object representing the
1918 * part of the target range between the end of the match and the end of
1919 * the target range.
1920 */
1921 const_reference
1922 suffix() const
1923 {
1924 __glibcxx_assert( ready() );
1925 return !empty() ? _M_suffix() : _M_unmatched_sub();
1926 }
1927
1928 /**
1929 * @brief Gets an iterator to the start of the %sub_match collection.
1930 */
1931 const_iterator
1932 begin() const noexcept
1933 { return _Base_type::begin(); }
1934
1935 /**
1936 * @brief Gets an iterator to the start of the %sub_match collection.
1937 */
1938 const_iterator
1939 cbegin() const noexcept
1940 { return this->begin(); }
1941
1942 /**
1943 * @brief Gets an iterator to one-past-the-end of the collection.
1944 */
1945 const_iterator
1946 end() const noexcept
1947 { return _Base_type::end() - (_Base_type::empty() ? 0 : 3); }
1948
1949 /**
1950 * @brief Gets an iterator to one-past-the-end of the collection.
1951 */
1952 const_iterator
1953 cend() const noexcept
1954 { return this->end(); }
1955
1956 ///@}
1957
1958 /**
1959 * @name 28.10.5 Formatting
1960 *
1961 * These functions perform formatted substitution of the matched
1962 * character sequences into their target. The format specifiers and
1963 * escape sequences accepted by these functions are determined by
1964 * their @p flags parameter as documented above.
1965 */
1966 ///@{
1967
1968 /**
1969 * @pre ready() == true
1970 */
1971 template<typename _Out_iter>
1972 _Out_iter
1973 format(_Out_iter __out, const char_type* __fmt_first,
1974 const char_type* __fmt_last,
1975 match_flag_type __flags = regex_constants::format_default) const;
1976
1977 /**
1978 * @pre ready() == true
1979 */
1980 template<typename _Out_iter, typename _St, typename _Sa>
1981 _Out_iter
1982 format(_Out_iter __out, const basic_string<char_type, _St, _Sa>& __fmt,
1983 match_flag_type __flags = regex_constants::format_default) const
1984 {
1985 return format(__out, __fmt.data(), __fmt.data() + __fmt.size(),
1986 __flags);
1987 }
1988
1989 /**
1990 * @pre ready() == true
1991 */
1992 template<typename _St, typename _Sa>
1993 basic_string<char_type, _St, _Sa>
1994 format(const basic_string<char_type, _St, _Sa>& __fmt,
1995 match_flag_type __flags = regex_constants::format_default) const
1996 {
1997 basic_string<char_type, _St, _Sa> __result;
1998 format(std::back_inserter(__result), __fmt, __flags);
1999 return __result;
2000 }
2001
2002 /**
2003 * @pre ready() == true
2004 */
2005 string_type
2006 format(const char_type* __fmt,
2007 match_flag_type __flags = regex_constants::format_default) const
2008 {
2009 string_type __result;
2010 format(std::back_inserter(__result),
2011 __fmt,
2012 __fmt + char_traits<char_type>::length(__fmt),
2013 __flags);
2014 return __result;
2015 }
2016
2017 ///@}
2018
2019 /**
2020 * @name 28.10.6 Allocator
2021 */
2022 ///@{
2023
2024 /**
2025 * @brief Gets a copy of the allocator.
2026 */
2027 allocator_type
2028 get_allocator() const noexcept
2029 { return _Base_type::get_allocator(); }
2030
2031 ///@}
2032
2033 /**
2034 * @name 28.10.7 Swap
2035 */
2036 ///@{
2037
2038 /**
2039 * @brief Swaps the contents of two match_results.
2040 */
2041 void
2042 swap(match_results& __that) noexcept
2043 {
2044 using std::swap;
2045 _Base_type::swap(__that);
2046 swap(_M_begin, __that._M_begin);
2047 }
2048 ///@}
2049
2050 private:
2051 template<typename, typename, typename>
2052 friend class regex_iterator;
2053
2054 /// @cond undocumented
2055
2056 template<typename, typename, typename, bool>
2057 friend class __detail::_Executor;
2058
2059 template<typename _Bp, typename _Ap, typename _Cp, typename _Rp,
2060 __detail::_RegexExecutorPolicy, bool>
2061 friend bool
2062 __detail::__regex_algo_impl(_Bp, _Bp, match_results<_Bp, _Ap>&,
2063 const basic_regex<_Cp, _Rp>&,
2064 regex_constants::match_flag_type);
2065
2066 // Reset contents to __size unmatched sub_match objects
2067 // (plus additional objects for prefix, suffix and unmatched sub).
2068 void
2069 _M_resize(unsigned int __size)
2070 { _Base_type::assign(__size + 3, sub_match<_Bi_iter>{}); }
2071
2072 // Set state to a failed match for the given past-the-end iterator.
2073 void
2074 _M_establish_failed_match(_Bi_iter __end)
2075 {
2076 sub_match<_Bi_iter> __sm;
2077 __sm.first = __sm.second = __end;
2078 _Base_type::assign(3, __sm);
2079 }
2080
2081 const_reference
2082 _M_unmatched_sub() const
2083 { return _Base_type::operator[](_Base_type::size() - 3); }
2084
2085 sub_match<_Bi_iter>&
2086 _M_unmatched_sub()
2087 { return _Base_type::operator[](_Base_type::size() - 3); }
2088
2089 const_reference
2090 _M_prefix() const
2091 { return _Base_type::operator[](_Base_type::size() - 2); }
2092
2093 sub_match<_Bi_iter>&
2094 _M_prefix()
2095 { return _Base_type::operator[](_Base_type::size() - 2); }
2096
2097 const_reference
2098 _M_suffix() const
2099 { return _Base_type::operator[](_Base_type::size() - 1); }
2100
2101 sub_match<_Bi_iter>&
2102 _M_suffix()
2103 { return _Base_type::operator[](_Base_type::size() - 1); }
2104
2105 _Bi_iter _M_begin {};
2106 /// @endcond
2107 };
2108
2109 typedef match_results<const char*> cmatch;
2110 typedef match_results<string::const_iterator> smatch;
2111#ifdef _GLIBCXX_USE_WCHAR_T
2112 typedef match_results<const wchar_t*> wcmatch;
2113 typedef match_results<wstring::const_iterator> wsmatch;
2114#endif
2115
2116 // match_results comparisons
2117
2118 /**
2119 * @brief Compares two match_results for equality.
2120 * @returns true if the two objects refer to the same match,
2121 * false otherwise.
2122 */
2123 template<typename _Bi_iter, typename _Alloc>
2124 inline bool
2125 operator==(const match_results<_Bi_iter, _Alloc>& __m1,
2126 const match_results<_Bi_iter, _Alloc>& __m2)
2127 {
2128 if (__m1.ready() != __m2.ready())
2129 return false;
2130 if (!__m1.ready()) // both are not ready
2131 return true;
2132 if (__m1.empty() != __m2.empty())
2133 return false;
2134 if (__m1.empty()) // both are empty
2135 return true;
2136 return __m1.prefix() == __m2.prefix()
2137 && __m1.size() == __m2.size()
2138 && std::equal(__m1.begin(), __m1.end(), __m2.begin())
2139 && __m1.suffix() == __m2.suffix();
2140 }
2141
2142#if ! __cpp_lib_three_way_comparison
2143 /**
2144 * @brief Compares two match_results for inequality.
2145 * @returns true if the two objects do not refer to the same match,
2146 * false otherwise.
2147 */
2148 template<typename _Bi_iter, class _Alloc>
2149 inline bool
2150 operator!=(const match_results<_Bi_iter, _Alloc>& __m1,
2151 const match_results<_Bi_iter, _Alloc>& __m2)
2152 { return !(__m1 == __m2); }
2153#endif
2154
2155 // [7.10.6] match_results swap
2156 /**
2157 * @brief Swaps two match results.
2158 * @param __lhs A match result.
2159 * @param __rhs A match result.
2160 *
2161 * The contents of the two match_results objects are swapped.
2162 */
2163 template<typename _Bi_iter, typename _Alloc>
2164 inline void
2165 swap(match_results<_Bi_iter, _Alloc>& __lhs,
2166 match_results<_Bi_iter, _Alloc>& __rhs) noexcept
2167 { __lhs.swap(__rhs); }
2168
2169_GLIBCXX_END_NAMESPACE_CXX11
2170
2171 // [28.11.2] Function template regex_match
2172 /**
2173 * @name Matching, Searching, and Replacing
2174 */
2175 ///@{
2176
2177 /**
2178 * @brief Determines if there is a match between the regular expression @p e
2179 * and all of the character sequence [first, last).
2180 *
2181 * @param __s Start of the character sequence to match.
2182 * @param __e One-past-the-end of the character sequence to match.
2183 * @param __m The match results.
2184 * @param __re The regular expression.
2185 * @param __flags Controls how the regular expression is matched.
2186 *
2187 * @retval true A match exists.
2188 * @retval false Otherwise.
2189 *
2190 * @throws an exception of type regex_error.
2191 */
2192 template<typename _Bi_iter, typename _Alloc,
2193 typename _Ch_type, typename _Rx_traits>
2194 inline bool
2195 regex_match(_Bi_iter __s,
2196 _Bi_iter __e,
2197 match_results<_Bi_iter, _Alloc>& __m,
2198 const basic_regex<_Ch_type, _Rx_traits>& __re,
2199 regex_constants::match_flag_type __flags
2200 = regex_constants::match_default)
2201 {
2202 return __detail::__regex_algo_impl<_Bi_iter, _Alloc, _Ch_type, _Rx_traits,
2203 __detail::_RegexExecutorPolicy::_S_auto, true>
2204 (__s, __e, __m, __re, __flags);
2205 }
2206
2207 /**
2208 * @brief Indicates if there is a match between the regular expression @p e
2209 * and all of the character sequence [first, last).
2210 *
2211 * @param __first Beginning of the character sequence to match.
2212 * @param __last One-past-the-end of the character sequence to match.
2213 * @param __re The regular expression.
2214 * @param __flags Controls how the regular expression is matched.
2215 *
2216 * @retval true A match exists.
2217 * @retval false Otherwise.
2218 *
2219 * @throws an exception of type regex_error.
2220 */
2221 template<typename _Bi_iter, typename _Ch_type, typename _Rx_traits>
2222 inline bool
2223 regex_match(_Bi_iter __first, _Bi_iter __last,
2224 const basic_regex<_Ch_type, _Rx_traits>& __re,
2225 regex_constants::match_flag_type __flags
2226 = regex_constants::match_default)
2227 {
2228 match_results<_Bi_iter> __what;
2229 return regex_match(__first, __last, __what, __re, __flags);
2230 }
2231
2232 /**
2233 * @brief Determines if there is a match between the regular expression @p e
2234 * and a C-style null-terminated string.
2235 *
2236 * @param __s The C-style null-terminated string to match.
2237 * @param __m The match results.
2238 * @param __re The regular expression.
2239 * @param __f Controls how the regular expression is matched.
2240 *
2241 * @retval true A match exists.
2242 * @retval false Otherwise.
2243 *
2244 * @throws an exception of type regex_error.
2245 */
2246 template<typename _Ch_type, typename _Alloc, typename _Rx_traits>
2247 inline bool
2248 regex_match(const _Ch_type* __s,
2249 match_results<const _Ch_type*, _Alloc>& __m,
2250 const basic_regex<_Ch_type, _Rx_traits>& __re,
2251 regex_constants::match_flag_type __f
2252 = regex_constants::match_default)
2253 { return regex_match(__s, __s + _Rx_traits::length(__s), __m, __re, __f); }
2254
2255 /**
2256 * @brief Determines if there is a match between the regular expression @p e
2257 * and a string.
2258 *
2259 * @param __s The string to match.
2260 * @param __m The match results.
2261 * @param __re The regular expression.
2262 * @param __flags Controls how the regular expression is matched.
2263 *
2264 * @retval true A match exists.
2265 * @retval false Otherwise.
2266 *
2267 * @throws an exception of type regex_error.
2268 */
2269 template<typename _Ch_traits, typename _Ch_alloc,
2270 typename _Alloc, typename _Ch_type, typename _Rx_traits>
2271 inline bool
2272 regex_match(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>& __s,
2273 match_results<typename basic_string<_Ch_type,
2274 _Ch_traits, _Ch_alloc>::const_iterator, _Alloc>& __m,
2275 const basic_regex<_Ch_type, _Rx_traits>& __re,
2276 regex_constants::match_flag_type __flags
2277 = regex_constants::match_default)
2278 { return regex_match(__s.begin(), __s.end(), __m, __re, __flags); }
2279
2280 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2281 // 2329. regex_match() with match_results should forbid temporary strings
2282 /// Prevent unsafe attempts to get match_results from a temporary string.
2283 template<typename _Ch_traits, typename _Ch_alloc,
2284 typename _Alloc, typename _Ch_type, typename _Rx_traits>
2285 bool
2286 regex_match(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>&&,
2287 match_results<typename basic_string<_Ch_type,
2288 _Ch_traits, _Ch_alloc>::const_iterator, _Alloc>&,
2289 const basic_regex<_Ch_type, _Rx_traits>&,
2290 regex_constants::match_flag_type
2291 = regex_constants::match_default) = delete;
2292
2293 /**
2294 * @brief Indicates if there is a match between the regular expression @p e
2295 * and a C-style null-terminated string.
2296 *
2297 * @param __s The C-style null-terminated string to match.
2298 * @param __re The regular expression.
2299 * @param __f Controls how the regular expression is matched.
2300 *
2301 * @retval true A match exists.
2302 * @retval false Otherwise.
2303 *
2304 * @throws an exception of type regex_error.
2305 */
2306 template<typename _Ch_type, class _Rx_traits>
2307 inline bool
2308 regex_match(const _Ch_type* __s,
2309 const basic_regex<_Ch_type, _Rx_traits>& __re,
2310 regex_constants::match_flag_type __f
2311 = regex_constants::match_default)
2312 { return regex_match(__s, __s + _Rx_traits::length(__s), __re, __f); }
2313
2314 /**
2315 * @brief Indicates if there is a match between the regular expression @p e
2316 * and a string.
2317 *
2318 * @param __s [IN] The string to match.
2319 * @param __re [IN] The regular expression.
2320 * @param __flags [IN] Controls how the regular expression is matched.
2321 *
2322 * @retval true A match exists.
2323 * @retval false Otherwise.
2324 *
2325 * @throws an exception of type regex_error.
2326 */
2327 template<typename _Ch_traits, typename _Str_allocator,
2328 typename _Ch_type, typename _Rx_traits>
2329 inline bool
2330 regex_match(const basic_string<_Ch_type, _Ch_traits, _Str_allocator>& __s,
2331 const basic_regex<_Ch_type, _Rx_traits>& __re,
2332 regex_constants::match_flag_type __flags
2333 = regex_constants::match_default)
2334 { return regex_match(__s.begin(), __s.end(), __re, __flags); }
2335
2336 // [7.11.3] Function template regex_search
2337 /**
2338 * Searches for a regular expression within a range.
2339 * @param __s [IN] The start of the string to search.
2340 * @param __e [IN] One-past-the-end of the string to search.
2341 * @param __m [OUT] The match results.
2342 * @param __re [IN] The regular expression to search for.
2343 * @param __flags [IN] Search policy flags.
2344 * @retval true A match was found within the string.
2345 * @retval false No match was found within the string, the content of %m is
2346 * undefined.
2347 *
2348 * @throws an exception of type regex_error.
2349 */
2350 template<typename _Bi_iter, typename _Alloc,
2351 typename _Ch_type, typename _Rx_traits>
2352 inline bool
2353 regex_search(_Bi_iter __s, _Bi_iter __e,
2354 match_results<_Bi_iter, _Alloc>& __m,
2355 const basic_regex<_Ch_type, _Rx_traits>& __re,
2356 regex_constants::match_flag_type __flags
2357 = regex_constants::match_default)
2358 {
2359 return __detail::__regex_algo_impl<_Bi_iter, _Alloc, _Ch_type, _Rx_traits,
2360 __detail::_RegexExecutorPolicy::_S_auto, false>
2361 (__s, __e, __m, __re, __flags);
2362 }
2363
2364 /**
2365 * Searches for a regular expression within a range.
2366 * @param __first [IN] The start of the string to search.
2367 * @param __last [IN] One-past-the-end of the string to search.
2368 * @param __re [IN] The regular expression to search for.
2369 * @param __flags [IN] Search policy flags.
2370 * @retval true A match was found within the string.
2371 * @retval false No match was found within the string.
2372 *
2373 * @throws an exception of type regex_error.
2374 */
2375 template<typename _Bi_iter, typename _Ch_type, typename _Rx_traits>
2376 inline bool
2377 regex_search(_Bi_iter __first, _Bi_iter __last,
2378 const basic_regex<_Ch_type, _Rx_traits>& __re,
2379 regex_constants::match_flag_type __flags
2380 = regex_constants::match_default)
2381 {
2382 match_results<_Bi_iter> __what;
2383 return regex_search(__first, __last, __what, __re, __flags);
2384 }
2385
2386 /**
2387 * @brief Searches for a regular expression within a C-string.
2388 * @param __s [IN] A C-string to search for the regex.
2389 * @param __m [OUT] The set of regex matches.
2390 * @param __e [IN] The regex to search for in @p s.
2391 * @param __f [IN] The search flags.
2392 * @retval true A match was found within the string.
2393 * @retval false No match was found within the string, the content of %m is
2394 * undefined.
2395 *
2396 * @throws an exception of type regex_error.
2397 */
2398 template<typename _Ch_type, class _Alloc, class _Rx_traits>
2399 inline bool
2400 regex_search(const _Ch_type* __s,
2401 match_results<const _Ch_type*, _Alloc>& __m,
2402 const basic_regex<_Ch_type, _Rx_traits>& __e,
2403 regex_constants::match_flag_type __f
2404 = regex_constants::match_default)
2405 { return regex_search(__s, __s + _Rx_traits::length(__s), __m, __e, __f); }
2406
2407 /**
2408 * @brief Searches for a regular expression within a C-string.
2409 * @param __s [IN] The C-string to search.
2410 * @param __e [IN] The regular expression to search for.
2411 * @param __f [IN] Search policy flags.
2412 * @retval true A match was found within the string.
2413 * @retval false No match was found within the string.
2414 *
2415 * @throws an exception of type regex_error.
2416 */
2417 template<typename _Ch_type, typename _Rx_traits>
2418 inline bool
2419 regex_search(const _Ch_type* __s,
2420 const basic_regex<_Ch_type, _Rx_traits>& __e,
2421 regex_constants::match_flag_type __f
2422 = regex_constants::match_default)
2423 { return regex_search(__s, __s + _Rx_traits::length(__s), __e, __f); }
2424
2425 /**
2426 * @brief Searches for a regular expression within a string.
2427 * @param __s [IN] The string to search.
2428 * @param __e [IN] The regular expression to search for.
2429 * @param __flags [IN] Search policy flags.
2430 * @retval true A match was found within the string.
2431 * @retval false No match was found within the string.
2432 *
2433 * @throws an exception of type regex_error.
2434 */
2435 template<typename _Ch_traits, typename _String_allocator,
2436 typename _Ch_type, typename _Rx_traits>
2437 inline bool
2438 regex_search(const basic_string<_Ch_type, _Ch_traits,
2439 _String_allocator>& __s,
2440 const basic_regex<_Ch_type, _Rx_traits>& __e,
2441 regex_constants::match_flag_type __flags
2442 = regex_constants::match_default)
2443 { return regex_search(__s.begin(), __s.end(), __e, __flags); }
2444
2445 /**
2446 * @brief Searches for a regular expression within a string.
2447 * @param __s [IN] A C++ string to search for the regex.
2448 * @param __m [OUT] The set of regex matches.
2449 * @param __e [IN] The regex to search for in @p s.
2450 * @param __f [IN] The search flags.
2451 * @retval true A match was found within the string.
2452 * @retval false No match was found within the string, the content of %m is
2453 * undefined.
2454 *
2455 * @throws an exception of type regex_error.
2456 */
2457 template<typename _Ch_traits, typename _Ch_alloc,
2458 typename _Alloc, typename _Ch_type,
2459 typename _Rx_traits>
2460 inline bool
2461 regex_search(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>& __s,
2462 match_results<typename basic_string<_Ch_type,
2463 _Ch_traits, _Ch_alloc>::const_iterator, _Alloc>& __m,
2464 const basic_regex<_Ch_type, _Rx_traits>& __e,
2465 regex_constants::match_flag_type __f
2466 = regex_constants::match_default)
2467 { return regex_search(__s.begin(), __s.end(), __m, __e, __f); }
2468
2469 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2470 // 2329. regex_search() with match_results should forbid temporary strings
2471 /// Prevent unsafe attempts to get match_results from a temporary string.
2472 template<typename _Ch_traits, typename _Ch_alloc,
2473 typename _Alloc, typename _Ch_type,
2474 typename _Rx_traits>
2475 bool
2476 regex_search(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>&&,
2477 match_results<typename basic_string<_Ch_type,
2478 _Ch_traits, _Ch_alloc>::const_iterator, _Alloc>&,
2479 const basic_regex<_Ch_type, _Rx_traits>&,
2480 regex_constants::match_flag_type
2481 = regex_constants::match_default) = delete;
2482
2483 // std [28.11.4] Function template regex_replace
2484
2485 template<typename _Out_iter, typename _Bi_iter,
2486 typename _Rx_traits, typename _Ch_type>
2487 _Out_iter
2488 __regex_replace(_Out_iter __out, _Bi_iter __first, _Bi_iter __last,
2489 const basic_regex<_Ch_type, _Rx_traits>& __e,
2490 const _Ch_type* __fmt, size_t __len,
2491 regex_constants::match_flag_type __flags);
2492
2493 /**
2494 * @brief Search for a regular expression within a range for multiple times,
2495 and replace the matched parts through filling a format string.
2496 * @param __out [OUT] The output iterator.
2497 * @param __first [IN] The start of the string to search.
2498 * @param __last [IN] One-past-the-end of the string to search.
2499 * @param __e [IN] The regular expression to search for.
2500 * @param __fmt [IN] The format string.
2501 * @param __flags [IN] Search and replace policy flags.
2502 *
2503 * @returns __out
2504 * @throws an exception of type regex_error.
2505 */
2506 template<typename _Out_iter, typename _Bi_iter,
2507 typename _Rx_traits, typename _Ch_type,
2508 typename _St, typename _Sa>
2509 inline _Out_iter
2510 regex_replace(_Out_iter __out, _Bi_iter __first, _Bi_iter __last,
2511 const basic_regex<_Ch_type, _Rx_traits>& __e,
2512 const basic_string<_Ch_type, _St, _Sa>& __fmt,
2513 regex_constants::match_flag_type __flags
2514 = regex_constants::match_default)
2515 {
2516 return std::__regex_replace(__out, __first, __last, __e, __fmt.c_str(),
2517 __fmt.length(), __flags);
2518 }
2519
2520 /**
2521 * @brief Search for a regular expression within a range for multiple times,
2522 and replace the matched parts through filling a format C-string.
2523 * @param __out [OUT] The output iterator.
2524 * @param __first [IN] The start of the string to search.
2525 * @param __last [IN] One-past-the-end of the string to search.
2526 * @param __e [IN] The regular expression to search for.
2527 * @param __fmt [IN] The format C-string.
2528 * @param __flags [IN] Search and replace policy flags.
2529 *
2530 * @returns __out
2531 * @throws an exception of type regex_error.
2532 */
2533 template<typename _Out_iter, typename _Bi_iter,
2534 typename _Rx_traits, typename _Ch_type>
2535 _Out_iter
2536 regex_replace(_Out_iter __out, _Bi_iter __first, _Bi_iter __last,
2537 const basic_regex<_Ch_type, _Rx_traits>& __e,
2538 const _Ch_type* __fmt,
2539 regex_constants::match_flag_type __flags
2540 = regex_constants::match_default)
2541 {
2542 return std::__regex_replace(__out, __first, __last, __e, __fmt,
2543 char_traits<_Ch_type>::length(__fmt),
2544 __flags);
2545 }
2546
2547
2548 /**
2549 * @brief Search for a regular expression within a string for multiple times,
2550 and replace the matched parts through filling a format string.
2551 * @param __s [IN] The string to search and replace.
2552 * @param __e [IN] The regular expression to search for.
2553 * @param __fmt [IN] The format string.
2554 * @param __flags [IN] Search and replace policy flags.
2555 *
2556 * @returns The string after replacing.
2557 * @throws an exception of type regex_error.
2558 */
2559 template<typename _Rx_traits, typename _Ch_type,
2560 typename _St, typename _Sa, typename _Fst, typename _Fsa>
2561 inline basic_string<_Ch_type, _St, _Sa>
2562 regex_replace(const basic_string<_Ch_type, _St, _Sa>& __s,
2563 const basic_regex<_Ch_type, _Rx_traits>& __e,
2564 const basic_string<_Ch_type, _Fst, _Fsa>& __fmt,
2565 regex_constants::match_flag_type __flags
2566 = regex_constants::match_default)
2567 {
2568 basic_string<_Ch_type, _St, _Sa> __result;
2569 regex_replace(std::back_inserter(__result),
2570 __s.begin(), __s.end(), __e, __fmt, __flags);
2571 return __result;
2572 }
2573
2574 /**
2575 * @brief Search for a regular expression within a string for multiple times,
2576 and replace the matched parts through filling a format C-string.
2577 * @param __s [IN] The string to search and replace.
2578 * @param __e [IN] The regular expression to search for.
2579 * @param __fmt [IN] The format C-string.
2580 * @param __flags [IN] Search and replace policy flags.
2581 *
2582 * @returns The string after replacing.
2583 * @throws an exception of type regex_error.
2584 */
2585 template<typename _Rx_traits, typename _Ch_type,
2586 typename _St, typename _Sa>
2587 inline basic_string<_Ch_type, _St, _Sa>
2588 regex_replace(const basic_string<_Ch_type, _St, _Sa>& __s,
2589 const basic_regex<_Ch_type, _Rx_traits>& __e,
2590 const _Ch_type* __fmt,
2591 regex_constants::match_flag_type __flags
2592 = regex_constants::match_default)
2593 {
2594 basic_string<_Ch_type, _St, _Sa> __result;
2595 regex_replace(std::back_inserter(__result),
2596 __s.begin(), __s.end(), __e, __fmt, __flags);
2597 return __result;
2598 }
2599
2600 /**
2601 * @brief Search for a regular expression within a C-string for multiple
2602 times, and replace the matched parts through filling a format string.
2603 * @param __s [IN] The C-string to search and replace.
2604 * @param __e [IN] The regular expression to search for.
2605 * @param __fmt [IN] The format string.
2606 * @param __flags [IN] Search and replace policy flags.
2607 *
2608 * @returns The string after replacing.
2609 * @throws an exception of type regex_error.
2610 */
2611 template<typename _Rx_traits, typename _Ch_type,
2612 typename _St, typename _Sa>
2613 inline basic_string<_Ch_type>
2614 regex_replace(const _Ch_type* __s,
2615 const basic_regex<_Ch_type, _Rx_traits>& __e,
2616 const basic_string<_Ch_type, _St, _Sa>& __fmt,
2617 regex_constants::match_flag_type __flags
2618 = regex_constants::match_default)
2619 {
2620 basic_string<_Ch_type> __result;
2621 regex_replace(std::back_inserter(__result), __s,
2622 __s + char_traits<_Ch_type>::length(__s),
2623 __e, __fmt, __flags);
2624 return __result;
2625 }
2626
2627 /**
2628 * @brief Search for a regular expression within a C-string for multiple
2629 times, and replace the matched parts through filling a format C-string.
2630 * @param __s [IN] The C-string to search and replace.
2631 * @param __e [IN] The regular expression to search for.
2632 * @param __fmt [IN] The format C-string.
2633 * @param __flags [IN] Search and replace policy flags.
2634 *
2635 * @returns The string after replacing.
2636 * @throws an exception of type regex_error.
2637 */
2638 template<typename _Rx_traits, typename _Ch_type>
2639 inline basic_string<_Ch_type>
2640 regex_replace(const _Ch_type* __s,
2641 const basic_regex<_Ch_type, _Rx_traits>& __e,
2642 const _Ch_type* __fmt,
2643 regex_constants::match_flag_type __flags
2644 = regex_constants::match_default)
2645 {
2646 basic_string<_Ch_type> __result;
2647 regex_replace(std::back_inserter(__result), __s,
2648 __s + char_traits<_Ch_type>::length(__s),
2649 __e, __fmt, __flags);
2650 return __result;
2651 }
2652
2653 ///@}
2654
2655_GLIBCXX_BEGIN_NAMESPACE_CXX11
2656
2657 // std [28.12] Class template regex_iterator
2658 /**
2659 * An iterator adaptor that will provide repeated calls of regex_search over
2660 * a range until no more matches remain.
2661 */
2662 template<typename _Bi_iter,
2663 typename _Ch_type = typename iterator_traits<_Bi_iter>::value_type,
2664 typename _Rx_traits = regex_traits<_Ch_type> >
2665 class regex_iterator
2666 {
2667 public:
2668 typedef basic_regex<_Ch_type, _Rx_traits> regex_type;
2669 typedef match_results<_Bi_iter> value_type;
2670 typedef std::ptrdiff_t difference_type;
2671 typedef const value_type* pointer;
2672 typedef const value_type& reference;
2673 typedef std::forward_iterator_tag iterator_category;
2674
2675 /**
2676 * @brief Provides a singular iterator, useful for indicating
2677 * one-past-the-end of a range.
2678 */
2679 regex_iterator() = default;
2680
2681 /**
2682 * Constructs a %regex_iterator...
2683 * @param __a [IN] The start of a text range to search.
2684 * @param __b [IN] One-past-the-end of the text range to search.
2685 * @param __re [IN] The regular expression to match.
2686 * @param __m [IN] Policy flags for match rules.
2687 */
2688 regex_iterator(_Bi_iter __a, _Bi_iter __b, const regex_type& __re,
2689 regex_constants::match_flag_type __m
2690 = regex_constants::match_default)
2691 : _M_begin(__a), _M_end(__b), _M_pregex(&__re), _M_flags(__m), _M_match()
2692 {
2693 if (!regex_search(_M_begin, _M_end, _M_match, *_M_pregex, _M_flags))
2694 *this = regex_iterator();
2695 }
2696
2697 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2698 // 2332. regex_iterator should forbid temporary regexes
2699 regex_iterator(_Bi_iter, _Bi_iter, const regex_type&&,
2700 regex_constants::match_flag_type
2701 = regex_constants::match_default) = delete;
2702
2703 /// Copy constructs a %regex_iterator.
2704 regex_iterator(const regex_iterator&) = default;
2705
2706 /// Copy assigns one %regex_iterator to another.
2707 regex_iterator&
2708 operator=(const regex_iterator&) = default;
2709
2710 ~regex_iterator() = default;
2711
2712 /**
2713 * @brief Tests the equivalence of two regex iterators.
2714 */
2715 bool
2716 operator==(const regex_iterator&) const noexcept;
2717
2718 /**
2719 * @brief Tests the inequivalence of two regex iterators.
2720 */
2721 bool
2722 operator!=(const regex_iterator& __rhs) const noexcept
2723 { return !(*this == __rhs); }
2724
2725 /**
2726 * @brief Dereferences a %regex_iterator.
2727 */
2728 const value_type&
2729 operator*() const noexcept
2730 { return _M_match; }
2731
2732 /**
2733 * @brief Selects a %regex_iterator member.
2734 */
2735 const value_type*
2736 operator->() const noexcept
2737 { return &_M_match; }
2738
2739 /**
2740 * @brief Increments a %regex_iterator.
2741 */
2742 regex_iterator&
2743 operator++();
2744
2745 /**
2746 * @brief Postincrements a %regex_iterator.
2747 */
2748 regex_iterator
2749 operator++(int)
2750 {
2751 auto __tmp = *this;
2752 ++(*this);
2753 return __tmp;
2754 }
2755
2756 private:
2757 _Bi_iter _M_begin {};
2758 _Bi_iter _M_end {};
2759 const regex_type* _M_pregex = nullptr;
2760 regex_constants::match_flag_type _M_flags {};
2761 match_results<_Bi_iter> _M_match;
2762 };
2763
2764 typedef regex_iterator<const char*> cregex_iterator;
2765 typedef regex_iterator<string::const_iterator> sregex_iterator;
2766#ifdef _GLIBCXX_USE_WCHAR_T
2767 typedef regex_iterator<const wchar_t*> wcregex_iterator;
2768 typedef regex_iterator<wstring::const_iterator> wsregex_iterator;
2769#endif
2770
2771 // [7.12.2] Class template regex_token_iterator
2772 /**
2773 * Iterates over submatches in a range (or @a splits a text string).
2774 *
2775 * The purpose of this iterator is to enumerate all, or all specified,
2776 * matches of a regular expression within a text range. The dereferenced
2777 * value of an iterator of this class is a std::sub_match object.
2778 */
2779 template<typename _Bi_iter,
2780 typename _Ch_type = typename iterator_traits<_Bi_iter>::value_type,
2781 typename _Rx_traits = regex_traits<_Ch_type> >
2782 class regex_token_iterator
2783 {
2784 public:
2785 typedef basic_regex<_Ch_type, _Rx_traits> regex_type;
2786 typedef sub_match<_Bi_iter> value_type;
2787 typedef std::ptrdiff_t difference_type;
2788 typedef const value_type* pointer;
2789 typedef const value_type& reference;
2790 typedef std::forward_iterator_tag iterator_category;
2791
2792 public:
2793 /**
2794 * @brief Default constructs a %regex_token_iterator.
2795 *
2796 * A default-constructed %regex_token_iterator is a singular iterator
2797 * that will compare equal to the one-past-the-end value for any
2798 * iterator of the same type.
2799 */
2800 regex_token_iterator()
2801 : _M_position(), _M_subs(), _M_suffix(), _M_n(0), _M_result(nullptr),
2802 _M_has_m1(false)
2803 { }
2804
2805 /**
2806 * Constructs a %regex_token_iterator...
2807 * @param __a [IN] The start of the text to search.
2808 * @param __b [IN] One-past-the-end of the text to search.
2809 * @param __re [IN] The regular expression to search for.
2810 * @param __submatch [IN] Which submatch to return. There are some
2811 * special values for this parameter:
2812 * - -1 each enumerated subexpression does NOT
2813 * match the regular expression (aka field
2814 * splitting)
2815 * - 0 the entire string matching the
2816 * subexpression is returned for each match
2817 * within the text.
2818 * - >0 enumerates only the indicated
2819 * subexpression from a match within the text.
2820 * @param __m [IN] Policy flags for match rules.
2821 */
2822 regex_token_iterator(_Bi_iter __a, _Bi_iter __b, const regex_type& __re,
2823 int __submatch = 0,
2824 regex_constants::match_flag_type __m
2825 = regex_constants::match_default)
2826 : _M_position(__a, __b, __re, __m), _M_subs(1, __submatch), _M_n(0)
2827 { _M_init(__a, __b); }
2828
2829 /**
2830 * Constructs a %regex_token_iterator...
2831 * @param __a [IN] The start of the text to search.
2832 * @param __b [IN] One-past-the-end of the text to search.
2833 * @param __re [IN] The regular expression to search for.
2834 * @param __submatches [IN] A list of subexpressions to return for each
2835 * regular expression match within the text.
2836 * @param __m [IN] Policy flags for match rules.
2837 */
2838 regex_token_iterator(_Bi_iter __a, _Bi_iter __b,
2839 const regex_type& __re,
2840 const std::vector<int>& __submatches,
2841 regex_constants::match_flag_type __m
2842 = regex_constants::match_default)
2843 : _M_position(__a, __b, __re, __m), _M_subs(__submatches), _M_n(0)
2844 { _M_init(__a, __b); }
2845
2846 /**
2847 * Constructs a %regex_token_iterator...
2848 * @param __a [IN] The start of the text to search.
2849 * @param __b [IN] One-past-the-end of the text to search.
2850 * @param __re [IN] The regular expression to search for.
2851 * @param __submatches [IN] A list of subexpressions to return for each
2852 * regular expression match within the text.
2853 * @param __m [IN] Policy flags for match rules.
2854 */
2855 regex_token_iterator(_Bi_iter __a, _Bi_iter __b,
2856 const regex_type& __re,
2857 initializer_list<int> __submatches,
2858 regex_constants::match_flag_type __m
2859 = regex_constants::match_default)
2860 : _M_position(__a, __b, __re, __m), _M_subs(__submatches), _M_n(0)
2861 { _M_init(__a, __b); }
2862
2863 /**
2864 * Constructs a %regex_token_iterator...
2865 * @param __a [IN] The start of the text to search.
2866 * @param __b [IN] One-past-the-end of the text to search.
2867 * @param __re [IN] The regular expression to search for.
2868 * @param __submatches [IN] A list of subexpressions to return for each
2869 * regular expression match within the text.
2870 * @param __m [IN] Policy flags for match rules.
2871 */
2872 template<std::size_t _Nm>
2873 regex_token_iterator(_Bi_iter __a, _Bi_iter __b,
2874 const regex_type& __re,
2875 const int (&__submatches)[_Nm],
2876 regex_constants::match_flag_type __m
2877 = regex_constants::match_default)
2878 : _M_position(__a, __b, __re, __m),
2879 _M_subs(__submatches, __submatches + _Nm), _M_n(0)
2880 { _M_init(__a, __b); }
2881
2882 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2883 // 2332. regex_token_iterator should forbid temporary regexes
2884 regex_token_iterator(_Bi_iter, _Bi_iter, const regex_type&&, int = 0,
2885 regex_constants::match_flag_type =
2886 regex_constants::match_default) = delete;
2887 regex_token_iterator(_Bi_iter, _Bi_iter, const regex_type&&,
2888 const std::vector<int>&,
2889 regex_constants::match_flag_type =
2890 regex_constants::match_default) = delete;
2891 regex_token_iterator(_Bi_iter, _Bi_iter, const regex_type&&,
2892 initializer_list<int>,
2893 regex_constants::match_flag_type =
2894 regex_constants::match_default) = delete;
2895 template <std::size_t _Nm>
2896 regex_token_iterator(_Bi_iter, _Bi_iter, const regex_type&&,
2897 const int (&)[_Nm],
2898 regex_constants::match_flag_type =
2899 regex_constants::match_default) = delete;
2900
2901 /**
2902 * @brief Copy constructs a %regex_token_iterator.
2903 * @param __rhs [IN] A %regex_token_iterator to copy.
2904 */
2905 regex_token_iterator(const regex_token_iterator& __rhs)
2906 : _M_position(__rhs._M_position), _M_subs(__rhs._M_subs),
2907 _M_suffix(__rhs._M_suffix), _M_n(__rhs._M_n), _M_has_m1(__rhs._M_has_m1)
2908 { _M_normalize_result(); }
2909
2910 /**
2911 * @brief Assigns a %regex_token_iterator to another.
2912 * @param __rhs [IN] A %regex_token_iterator to copy.
2913 */
2914 regex_token_iterator&
2915 operator=(const regex_token_iterator& __rhs);
2916
2917 /**
2918 * @brief Compares a %regex_token_iterator to another for equality.
2919 */
2920 bool
2921 operator==(const regex_token_iterator& __rhs) const;
2922
2923 /**
2924 * @brief Compares a %regex_token_iterator to another for inequality.
2925 */
2926 bool
2927 operator!=(const regex_token_iterator& __rhs) const
2928 { return !(*this == __rhs); }
2929
2930 /**
2931 * @brief Dereferences a %regex_token_iterator.
2932 */
2933 const value_type&
2934 operator*() const
2935 { return *_M_result; }
2936
2937 /**
2938 * @brief Selects a %regex_token_iterator member.
2939 */
2940 const value_type*
2941 operator->() const
2942 { return _M_result; }
2943
2944 /**
2945 * @brief Increments a %regex_token_iterator.
2946 */
2947 regex_token_iterator&
2948 operator++();
2949
2950 /**
2951 * @brief Postincrements a %regex_token_iterator.
2952 */
2953 regex_token_iterator
2954 operator++(int)
2955 {
2956 auto __tmp = *this;
2957 ++(*this);
2958 return __tmp;
2959 }
2960
2961 private:
2962 typedef regex_iterator<_Bi_iter, _Ch_type, _Rx_traits> _Position;
2963
2964 void
2965 _M_init(_Bi_iter __a, _Bi_iter __b);
2966
2967 const value_type&
2968 _M_current_match() const
2969 {
2970 if (_M_subs[_M_n] == -1)
2971 return (*_M_position).prefix();
2972 else
2973 return (*_M_position)[_M_subs[_M_n]];
2974 }
2975
2976 constexpr bool
2977 _M_end_of_seq() const
2978 { return _M_result == nullptr; }
2979
2980 // [28.12.2.2.4]
2981 void
2982 _M_normalize_result()
2983 {
2984 if (_M_position != _Position())
2985 _M_result = &_M_current_match();
2986 else if (_M_has_m1)
2987 _M_result = &_M_suffix;
2988 else
2989 _M_result = nullptr;
2990 }
2991
2992 _Position _M_position;
2993 std::vector<int> _M_subs;
2994 value_type _M_suffix;
2995 std::size_t _M_n;
2996 const value_type* _M_result;
2997
2998 // Show whether _M_subs contains -1
2999 bool _M_has_m1;
3000 };
3001
3002 /** @brief Token iterator for C-style NULL-terminated strings. */
3003 typedef regex_token_iterator<const char*> cregex_token_iterator;
3004
3005 /** @brief Token iterator for standard strings. */
3006 typedef regex_token_iterator<string::const_iterator> sregex_token_iterator;
3007
3008#ifdef _GLIBCXX_USE_WCHAR_T
3009 /** @brief Token iterator for C-style NULL-terminated wide strings. */
3010 typedef regex_token_iterator<const wchar_t*> wcregex_token_iterator;
3011
3012 /** @brief Token iterator for standard wide-character strings. */
3013 typedef regex_token_iterator<wstring::const_iterator> wsregex_token_iterator;
3014#endif
3015
3016 ///@} // group regex
3017
3018_GLIBCXX_END_NAMESPACE_CXX11
3019_GLIBCXX_END_NAMESPACE_VERSION
3020} // namespace
3021
3022#include <bits/regex.tcc>
3023

source code of include/c++/11/bits/regex.h