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