PR libstdc++/87278 restore support for std::make_shared<volatile T>()
[official-gcc.git] / libstdc++-v3 / include / bits / regex.h
blobaf6fe3f0d79b0375d754d9e66c95e98aa7da788e
1 // class template regex -*- C++ -*-
3 // Copyright (C) 2010-2018 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.
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.
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.
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/>.
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}
31 namespace std _GLIBCXX_VISIBILITY(default)
33 _GLIBCXX_BEGIN_NAMESPACE_VERSION
34 _GLIBCXX_BEGIN_NAMESPACE_CXX11
35 template<typename, typename>
36 class basic_regex;
38 template<typename, typename>
39 class match_results;
41 _GLIBCXX_END_NAMESPACE_CXX11
43 namespace __detail
45 enum class _RegexExecutorPolicy : int { _S_auto, _S_alternate };
47 template<typename _BiIter, typename _Alloc,
48 typename _CharT, typename _TraitsT,
49 _RegexExecutorPolicy __policy,
50 bool __match_mode>
51 bool
52 __regex_algo_impl(_BiIter __s,
53 _BiIter __e,
54 match_results<_BiIter, _Alloc>& __m,
55 const basic_regex<_CharT, _TraitsT>& __re,
56 regex_constants::match_flag_type __flags);
58 template<typename, typename, typename, bool>
59 class _Executor;
62 _GLIBCXX_BEGIN_NAMESPACE_CXX11
64 /**
65 * @addtogroup regex
66 * @{
69 /**
70 * @brief Describes aspects of a regular expression.
72 * A regular expression traits class that satisfies the requirements of
73 * section [28.7].
75 * The class %regex is parameterized around a set of related types and
76 * functions used to complete the definition of its semantics. This class
77 * satisfies the requirements of such a traits class.
79 template<typename _Ch_type>
80 struct regex_traits
82 public:
83 typedef _Ch_type char_type;
84 typedef std::basic_string<char_type> string_type;
85 typedef std::locale locale_type;
86 private:
87 struct _RegexMask
89 typedef std::ctype_base::mask _BaseType;
90 _BaseType _M_base;
91 unsigned char _M_extended;
92 static constexpr unsigned char _S_under = 1 << 0;
93 static constexpr unsigned char _S_valid_mask = 0x1;
95 constexpr _RegexMask(_BaseType __base = 0,
96 unsigned char __extended = 0)
97 : _M_base(__base), _M_extended(__extended)
98 { }
100 constexpr _RegexMask
101 operator&(_RegexMask __other) const
103 return _RegexMask(_M_base & __other._M_base,
104 _M_extended & __other._M_extended);
107 constexpr _RegexMask
108 operator|(_RegexMask __other) const
110 return _RegexMask(_M_base | __other._M_base,
111 _M_extended | __other._M_extended);
114 constexpr _RegexMask
115 operator^(_RegexMask __other) const
117 return _RegexMask(_M_base ^ __other._M_base,
118 _M_extended ^ __other._M_extended);
121 constexpr _RegexMask
122 operator~() const
123 { return _RegexMask(~_M_base, ~_M_extended); }
125 _RegexMask&
126 operator&=(_RegexMask __other)
127 { return *this = (*this) & __other; }
129 _RegexMask&
130 operator|=(_RegexMask __other)
131 { return *this = (*this) | __other; }
133 _RegexMask&
134 operator^=(_RegexMask __other)
135 { return *this = (*this) ^ __other; }
137 constexpr bool
138 operator==(_RegexMask __other) const
140 return (_M_extended & _S_valid_mask)
141 == (__other._M_extended & _S_valid_mask)
142 && _M_base == __other._M_base;
145 constexpr bool
146 operator!=(_RegexMask __other) const
147 { return !((*this) == __other); }
150 public:
151 typedef _RegexMask char_class_type;
153 public:
155 * @brief Constructs a default traits object.
157 regex_traits() { }
160 * @brief Gives the length of a C-style string starting at @p __p.
162 * @param __p a pointer to the start of a character sequence.
164 * @returns the number of characters between @p *__p and the first
165 * default-initialized value of type @p char_type. In other words, uses
166 * the C-string algorithm for determining the length of a sequence of
167 * characters.
169 static std::size_t
170 length(const char_type* __p)
171 { return string_type::traits_type::length(__p); }
174 * @brief Performs the identity translation.
176 * @param __c A character to the locale-specific character set.
178 * @returns __c.
180 char_type
181 translate(char_type __c) const
182 { return __c; }
185 * @brief Translates a character into a case-insensitive equivalent.
187 * @param __c A character to the locale-specific character set.
189 * @returns the locale-specific lower-case equivalent of __c.
190 * @throws std::bad_cast if the imbued locale does not support the ctype
191 * facet.
193 char_type
194 translate_nocase(char_type __c) const
196 typedef std::ctype<char_type> __ctype_type;
197 const __ctype_type& __fctyp(use_facet<__ctype_type>(_M_locale));
198 return __fctyp.tolower(__c);
202 * @brief Gets a sort key for a character sequence.
204 * @param __first beginning of the character sequence.
205 * @param __last one-past-the-end of the character sequence.
207 * Returns a sort key for the character sequence designated by the
208 * iterator range [F1, F2) such that if the character sequence [G1, G2)
209 * sorts before the character sequence [H1, H2) then
210 * v.transform(G1, G2) < v.transform(H1, H2).
212 * What this really does is provide a more efficient way to compare a
213 * string to multiple other strings in locales with fancy collation
214 * rules and equivalence classes.
216 * @returns a locale-specific sort key equivalent to the input range.
218 * @throws std::bad_cast if the current locale does not have a collate
219 * facet.
221 template<typename _Fwd_iter>
222 string_type
223 transform(_Fwd_iter __first, _Fwd_iter __last) const
225 typedef std::collate<char_type> __collate_type;
226 const __collate_type& __fclt(use_facet<__collate_type>(_M_locale));
227 string_type __s(__first, __last);
228 return __fclt.transform(__s.data(), __s.data() + __s.size());
232 * @brief Gets a sort key for a character sequence, independent of case.
234 * @param __first beginning of the character sequence.
235 * @param __last one-past-the-end of the character sequence.
237 * Effects: if typeid(use_facet<collate<_Ch_type> >) ==
238 * typeid(collate_byname<_Ch_type>) and the form of the sort key
239 * returned by collate_byname<_Ch_type>::transform(__first, __last)
240 * is known and can be converted into a primary sort key
241 * then returns that key, otherwise returns an empty string.
243 * @todo Implement this function correctly.
245 template<typename _Fwd_iter>
246 string_type
247 transform_primary(_Fwd_iter __first, _Fwd_iter __last) const
249 // TODO : this is not entirely correct.
250 // This function requires extra support from the platform.
252 // Read http://gcc.gnu.org/ml/libstdc++/2013-09/msg00117.html and
253 // http://www.open-std.org/Jtc1/sc22/wg21/docs/papers/2003/n1429.htm
254 // for details.
255 typedef std::ctype<char_type> __ctype_type;
256 const __ctype_type& __fctyp(use_facet<__ctype_type>(_M_locale));
257 std::vector<char_type> __s(__first, __last);
258 __fctyp.tolower(__s.data(), __s.data() + __s.size());
259 return this->transform(__s.data(), __s.data() + __s.size());
263 * @brief Gets a collation element by name.
265 * @param __first beginning of the collation element name.
266 * @param __last one-past-the-end of the collation element name.
268 * @returns a sequence of one or more characters that represents the
269 * collating element consisting of the character sequence designated by
270 * the iterator range [__first, __last). Returns an empty string if the
271 * character sequence is not a valid collating element.
273 template<typename _Fwd_iter>
274 string_type
275 lookup_collatename(_Fwd_iter __first, _Fwd_iter __last) const;
278 * @brief Maps one or more characters to a named character
279 * classification.
281 * @param __first beginning of the character sequence.
282 * @param __last one-past-the-end of the character sequence.
283 * @param __icase ignores the case of the classification name.
285 * @returns an unspecified value that represents the character
286 * classification named by the character sequence designated by
287 * the iterator range [__first, __last). If @p icase is true,
288 * the returned mask identifies the classification regardless of
289 * the case of the characters to be matched (for example,
290 * [[:lower:]] is the same as [[:alpha:]]), otherwise a
291 * case-dependent classification is returned. The value
292 * returned shall be independent of the case of the characters
293 * in the character sequence. If the name is not recognized then
294 * returns a value that compares equal to 0.
296 * At least the following names (or their wide-character equivalent) are
297 * supported.
298 * - d
299 * - w
300 * - s
301 * - alnum
302 * - alpha
303 * - blank
304 * - cntrl
305 * - digit
306 * - graph
307 * - lower
308 * - print
309 * - punct
310 * - space
311 * - upper
312 * - xdigit
314 template<typename _Fwd_iter>
315 char_class_type
316 lookup_classname(_Fwd_iter __first, _Fwd_iter __last,
317 bool __icase = false) const;
320 * @brief Determines if @p c is a member of an identified class.
322 * @param __c a character.
323 * @param __f a class type (as returned from lookup_classname).
325 * @returns true if the character @p __c is a member of the classification
326 * represented by @p __f, false otherwise.
328 * @throws std::bad_cast if the current locale does not have a ctype
329 * facet.
331 bool
332 isctype(_Ch_type __c, char_class_type __f) const;
335 * @brief Converts a digit to an int.
337 * @param __ch a character representing a digit.
338 * @param __radix the radix if the numeric conversion (limited to 8, 10,
339 * or 16).
341 * @returns the value represented by the digit __ch in base radix if the
342 * character __ch is a valid digit in base radix; otherwise returns -1.
345 value(_Ch_type __ch, int __radix) const;
348 * @brief Imbues the regex_traits object with a copy of a new locale.
350 * @param __loc A locale.
352 * @returns a copy of the previous locale in use by the regex_traits
353 * object.
355 * @note Calling imbue with a different locale than the one currently in
356 * use invalidates all cached data held by *this.
358 locale_type
359 imbue(locale_type __loc)
361 std::swap(_M_locale, __loc);
362 return __loc;
366 * @brief Gets a copy of the current locale in use by the regex_traits
367 * object.
369 locale_type
370 getloc() const
371 { return _M_locale; }
373 protected:
374 locale_type _M_locale;
377 // [7.8] Class basic_regex
379 * Objects of specializations of this class represent regular expressions
380 * constructed from sequences of character type @p _Ch_type.
382 * Storage for the regular expression is allocated and deallocated as
383 * necessary by the member functions of this class.
385 template<typename _Ch_type, typename _Rx_traits = regex_traits<_Ch_type>>
386 class basic_regex
388 public:
389 static_assert(is_same<_Ch_type, typename _Rx_traits::char_type>::value,
390 "regex traits class must have the same char_type");
392 // types:
393 typedef _Ch_type value_type;
394 typedef _Rx_traits traits_type;
395 typedef typename traits_type::string_type string_type;
396 typedef regex_constants::syntax_option_type flag_type;
397 typedef typename traits_type::locale_type locale_type;
400 * @name Constants
401 * std [28.8.1](1)
403 //@{
404 static constexpr flag_type icase = regex_constants::icase;
405 static constexpr flag_type nosubs = regex_constants::nosubs;
406 static constexpr flag_type optimize = regex_constants::optimize;
407 static constexpr flag_type collate = regex_constants::collate;
408 static constexpr flag_type ECMAScript = regex_constants::ECMAScript;
409 static constexpr flag_type basic = regex_constants::basic;
410 static constexpr flag_type extended = regex_constants::extended;
411 static constexpr flag_type awk = regex_constants::awk;
412 static constexpr flag_type grep = regex_constants::grep;
413 static constexpr flag_type egrep = regex_constants::egrep;
414 //@}
416 // [7.8.2] construct/copy/destroy
418 * Constructs a basic regular expression that does not match any
419 * character sequence.
421 basic_regex()
422 : _M_flags(ECMAScript), _M_loc(), _M_automaton(nullptr)
426 * @brief Constructs a basic regular expression from the
427 * sequence [__p, __p + char_traits<_Ch_type>::length(__p))
428 * interpreted according to the flags in @p __f.
430 * @param __p A pointer to the start of a C-style null-terminated string
431 * containing a regular expression.
432 * @param __f Flags indicating the syntax rules and options.
434 * @throws regex_error if @p __p is not a valid regular expression.
436 explicit
437 basic_regex(const _Ch_type* __p, flag_type __f = ECMAScript)
438 : basic_regex(__p, __p + char_traits<_Ch_type>::length(__p), __f)
442 * @brief Constructs a basic regular expression from the sequence
443 * [p, p + len) interpreted according to the flags in @p f.
445 * @param __p A pointer to the start of a string containing a regular
446 * expression.
447 * @param __len The length of the string containing the regular
448 * expression.
449 * @param __f Flags indicating the syntax rules and options.
451 * @throws regex_error if @p __p is not a valid regular expression.
453 basic_regex(const _Ch_type* __p, std::size_t __len,
454 flag_type __f = ECMAScript)
455 : basic_regex(__p, __p + __len, __f)
459 * @brief Copy-constructs a basic regular expression.
461 * @param __rhs A @p regex object.
463 basic_regex(const basic_regex& __rhs) = default;
466 * @brief Move-constructs a basic regular expression.
468 * @param __rhs A @p regex object.
470 basic_regex(basic_regex&& __rhs) noexcept = default;
473 * @brief Constructs a basic regular expression from the string
474 * @p s interpreted according to the flags in @p f.
476 * @param __s A string containing a regular expression.
477 * @param __f Flags indicating the syntax rules and options.
479 * @throws regex_error if @p __s is not a valid regular expression.
481 template<typename _Ch_traits, typename _Ch_alloc>
482 explicit
483 basic_regex(const std::basic_string<_Ch_type, _Ch_traits,
484 _Ch_alloc>& __s,
485 flag_type __f = ECMAScript)
486 : basic_regex(__s.data(), __s.data() + __s.size(), __f)
490 * @brief Constructs a basic regular expression from the range
491 * [first, last) interpreted according to the flags in @p f.
493 * @param __first The start of a range containing a valid regular
494 * expression.
495 * @param __last The end of a range containing a valid regular
496 * expression.
497 * @param __f The format flags of the regular expression.
499 * @throws regex_error if @p [__first, __last) is not a valid regular
500 * expression.
502 template<typename _FwdIter>
503 basic_regex(_FwdIter __first, _FwdIter __last,
504 flag_type __f = ECMAScript)
505 : basic_regex(std::move(__first), std::move(__last), locale_type(), __f)
509 * @brief Constructs a basic regular expression from an initializer list.
511 * @param __l The initializer list.
512 * @param __f The format flags of the regular expression.
514 * @throws regex_error if @p __l is not a valid regular expression.
516 basic_regex(initializer_list<_Ch_type> __l, flag_type __f = ECMAScript)
517 : basic_regex(__l.begin(), __l.end(), __f)
521 * @brief Destroys a basic regular expression.
523 ~basic_regex()
527 * @brief Assigns one regular expression to another.
529 basic_regex&
530 operator=(const basic_regex& __rhs)
531 { return this->assign(__rhs); }
534 * @brief Move-assigns one regular expression to another.
536 basic_regex&
537 operator=(basic_regex&& __rhs) noexcept
538 { return this->assign(std::move(__rhs)); }
541 * @brief Replaces a regular expression with a new one constructed from
542 * a C-style null-terminated string.
544 * @param __p A pointer to the start of a null-terminated C-style string
545 * containing a regular expression.
547 basic_regex&
548 operator=(const _Ch_type* __p)
549 { return this->assign(__p); }
552 * @brief Replaces a regular expression with a new one constructed from
553 * an initializer list.
555 * @param __l The initializer list.
557 * @throws regex_error if @p __l is not a valid regular expression.
559 basic_regex&
560 operator=(initializer_list<_Ch_type> __l)
561 { return this->assign(__l.begin(), __l.end()); }
564 * @brief Replaces a regular expression with a new one constructed from
565 * a string.
567 * @param __s A pointer to a string containing a regular expression.
569 template<typename _Ch_traits, typename _Alloc>
570 basic_regex&
571 operator=(const basic_string<_Ch_type, _Ch_traits, _Alloc>& __s)
572 { return this->assign(__s); }
574 // [7.8.3] assign
576 * @brief the real assignment operator.
578 * @param __rhs Another regular expression object.
580 basic_regex&
581 assign(const basic_regex& __rhs)
583 basic_regex __tmp(__rhs);
584 this->swap(__tmp);
585 return *this;
589 * @brief The move-assignment operator.
591 * @param __rhs Another regular expression object.
593 basic_regex&
594 assign(basic_regex&& __rhs) noexcept
596 basic_regex __tmp(std::move(__rhs));
597 this->swap(__tmp);
598 return *this;
602 * @brief Assigns a new regular expression to a regex object from a
603 * C-style null-terminated string containing a regular expression
604 * pattern.
606 * @param __p A pointer to a C-style null-terminated string containing
607 * a regular expression pattern.
608 * @param __flags Syntax option flags.
610 * @throws regex_error if __p does not contain a valid regular
611 * expression pattern interpreted according to @p __flags. If
612 * regex_error is thrown, *this remains unchanged.
614 basic_regex&
615 assign(const _Ch_type* __p, flag_type __flags = ECMAScript)
616 { return this->assign(string_type(__p), __flags); }
619 * @brief Assigns a new regular expression to a regex object from a
620 * C-style string containing a regular expression pattern.
622 * @param __p A pointer to a C-style string containing a
623 * regular expression pattern.
624 * @param __len The length of the regular expression pattern string.
625 * @param __flags Syntax option flags.
627 * @throws regex_error if p does not contain a valid regular
628 * expression pattern interpreted according to @p __flags. If
629 * regex_error is thrown, *this remains unchanged.
631 basic_regex&
632 assign(const _Ch_type* __p, std::size_t __len, flag_type __flags)
633 { return this->assign(string_type(__p, __len), __flags); }
636 * @brief Assigns a new regular expression to a regex object from a
637 * string containing a regular expression pattern.
639 * @param __s A string containing a regular expression pattern.
640 * @param __flags Syntax option flags.
642 * @throws regex_error if __s does not contain a valid regular
643 * expression pattern interpreted according to @p __flags. If
644 * regex_error is thrown, *this remains unchanged.
646 template<typename _Ch_traits, typename _Alloc>
647 basic_regex&
648 assign(const basic_string<_Ch_type, _Ch_traits, _Alloc>& __s,
649 flag_type __flags = ECMAScript)
651 return this->assign(basic_regex(__s.data(), __s.data() + __s.size(),
652 _M_loc, __flags));
656 * @brief Assigns a new regular expression to a regex object.
658 * @param __first The start of a range containing a valid regular
659 * expression.
660 * @param __last The end of a range containing a valid regular
661 * expression.
662 * @param __flags Syntax option flags.
664 * @throws regex_error if p does not contain a valid regular
665 * expression pattern interpreted according to @p __flags. If
666 * regex_error is thrown, the object remains unchanged.
668 template<typename _InputIterator>
669 basic_regex&
670 assign(_InputIterator __first, _InputIterator __last,
671 flag_type __flags = ECMAScript)
672 { return this->assign(string_type(__first, __last), __flags); }
675 * @brief Assigns a new regular expression to a regex object.
677 * @param __l An initializer list representing a regular expression.
678 * @param __flags Syntax option flags.
680 * @throws regex_error if @p __l does not contain a valid
681 * regular expression pattern interpreted according to @p
682 * __flags. If regex_error is thrown, the object remains
683 * unchanged.
685 basic_regex&
686 assign(initializer_list<_Ch_type> __l, flag_type __flags = ECMAScript)
687 { return this->assign(__l.begin(), __l.end(), __flags); }
689 // [7.8.4] const operations
691 * @brief Gets the number of marked subexpressions within the regular
692 * expression.
694 unsigned int
695 mark_count() const
697 if (_M_automaton)
698 return _M_automaton->_M_sub_count() - 1;
699 return 0;
703 * @brief Gets the flags used to construct the regular expression
704 * or in the last call to assign().
706 flag_type
707 flags() const
708 { return _M_flags; }
710 // [7.8.5] locale
712 * @brief Imbues the regular expression object with the given locale.
714 * @param __loc A locale.
716 locale_type
717 imbue(locale_type __loc)
719 std::swap(__loc, _M_loc);
720 _M_automaton.reset();
721 return __loc;
725 * @brief Gets the locale currently imbued in the regular expression
726 * object.
728 locale_type
729 getloc() const
730 { return _M_loc; }
732 // [7.8.6] swap
734 * @brief Swaps the contents of two regular expression objects.
736 * @param __rhs Another regular expression object.
738 void
739 swap(basic_regex& __rhs)
741 std::swap(_M_flags, __rhs._M_flags);
742 std::swap(_M_loc, __rhs._M_loc);
743 std::swap(_M_automaton, __rhs._M_automaton);
746 #ifdef _GLIBCXX_DEBUG
747 void
748 _M_dot(std::ostream& __ostr)
749 { _M_automaton->_M_dot(__ostr); }
750 #endif
752 private:
753 typedef std::shared_ptr<const __detail::_NFA<_Rx_traits>> _AutomatonPtr;
755 template<typename _FwdIter>
756 basic_regex(_FwdIter __first, _FwdIter __last, locale_type __loc,
757 flag_type __f)
758 : _M_flags(__f), _M_loc(std::move(__loc)),
759 _M_automaton(__detail::__compile_nfa<_Rx_traits>(
760 std::move(__first), std::move(__last), _M_loc, _M_flags))
763 template<typename _Bp, typename _Ap, typename _Cp, typename _Rp,
764 __detail::_RegexExecutorPolicy, bool>
765 friend bool
766 __detail::__regex_algo_impl(_Bp, _Bp, match_results<_Bp, _Ap>&,
767 const basic_regex<_Cp, _Rp>&,
768 regex_constants::match_flag_type);
770 template<typename, typename, typename, bool>
771 friend class __detail::_Executor;
773 flag_type _M_flags;
774 locale_type _M_loc;
775 _AutomatonPtr _M_automaton;
778 #if __cplusplus < 201703L
779 template<typename _Ch, typename _Tr>
780 constexpr regex_constants::syntax_option_type
781 basic_regex<_Ch, _Tr>::icase;
783 template<typename _Ch, typename _Tr>
784 constexpr regex_constants::syntax_option_type
785 basic_regex<_Ch, _Tr>::nosubs;
787 template<typename _Ch, typename _Tr>
788 constexpr regex_constants::syntax_option_type
789 basic_regex<_Ch, _Tr>::optimize;
791 template<typename _Ch, typename _Tr>
792 constexpr regex_constants::syntax_option_type
793 basic_regex<_Ch, _Tr>::collate;
795 template<typename _Ch, typename _Tr>
796 constexpr regex_constants::syntax_option_type
797 basic_regex<_Ch, _Tr>::ECMAScript;
799 template<typename _Ch, typename _Tr>
800 constexpr regex_constants::syntax_option_type
801 basic_regex<_Ch, _Tr>::basic;
803 template<typename _Ch, typename _Tr>
804 constexpr regex_constants::syntax_option_type
805 basic_regex<_Ch, _Tr>::extended;
807 template<typename _Ch, typename _Tr>
808 constexpr regex_constants::syntax_option_type
809 basic_regex<_Ch, _Tr>::awk;
811 template<typename _Ch, typename _Tr>
812 constexpr regex_constants::syntax_option_type
813 basic_regex<_Ch, _Tr>::grep;
815 template<typename _Ch, typename _Tr>
816 constexpr regex_constants::syntax_option_type
817 basic_regex<_Ch, _Tr>::egrep;
818 #endif // ! C++17
820 #if __cpp_deduction_guides >= 201606
821 template<typename _ForwardIterator>
822 basic_regex(_ForwardIterator, _ForwardIterator,
823 regex_constants::syntax_option_type = {})
824 -> basic_regex<typename iterator_traits<_ForwardIterator>::value_type>;
825 #endif
827 /** @brief Standard regular expressions. */
828 typedef basic_regex<char> regex;
830 #ifdef _GLIBCXX_USE_WCHAR_T
831 /** @brief Standard wide-character regular expressions. */
832 typedef basic_regex<wchar_t> wregex;
833 #endif
836 // [7.8.6] basic_regex swap
838 * @brief Swaps the contents of two regular expression objects.
839 * @param __lhs First regular expression.
840 * @param __rhs Second regular expression.
842 template<typename _Ch_type, typename _Rx_traits>
843 inline void
844 swap(basic_regex<_Ch_type, _Rx_traits>& __lhs,
845 basic_regex<_Ch_type, _Rx_traits>& __rhs)
846 { __lhs.swap(__rhs); }
849 // C++11 28.9 [re.submatch] Class template sub_match
851 * A sequence of characters matched by a particular marked sub-expression.
853 * An object of this class is essentially a pair of iterators marking a
854 * matched subexpression within a regular expression pattern match. Such
855 * objects can be converted to and compared with std::basic_string objects
856 * of a similar base character type as the pattern matched by the regular
857 * expression.
859 * The iterators that make up the pair are the usual half-open interval
860 * referencing the actual original pattern matched.
862 template<typename _BiIter>
863 class sub_match : public std::pair<_BiIter, _BiIter>
865 typedef iterator_traits<_BiIter> __iter_traits;
867 public:
868 typedef typename __iter_traits::value_type value_type;
869 typedef typename __iter_traits::difference_type difference_type;
870 typedef _BiIter iterator;
871 typedef basic_string<value_type> string_type;
873 bool matched;
875 constexpr sub_match() noexcept : matched() { }
877 /// Gets the length of the matching sequence.
878 difference_type
879 length() const noexcept
880 { return this->matched ? std::distance(this->first, this->second) : 0; }
883 * @brief Gets the matching sequence as a string.
885 * @returns the matching sequence as a string.
887 * This is the implicit conversion operator. It is identical to the
888 * str() member function except that it will want to pop up in
889 * unexpected places and cause a great deal of confusion and cursing
890 * from the unwary.
892 operator string_type() const
893 { return str(); }
896 * @brief Gets the matching sequence as a string.
898 * @returns the matching sequence as a string.
900 string_type
901 str() const
903 return this->matched
904 ? string_type(this->first, this->second)
905 : string_type();
909 * @brief Compares this and another matched sequence.
911 * @param __s Another matched sequence to compare to this one.
913 * @retval <0 this matched sequence will collate before @p __s.
914 * @retval =0 this matched sequence is equivalent to @p __s.
915 * @retval <0 this matched sequence will collate after @p __s.
918 compare(const sub_match& __s) const
919 { return this->_M_str().compare(__s._M_str()); }
922 * @{
923 * @brief Compares this sub_match to a string.
925 * @param __s A string to compare to this sub_match.
927 * @retval <0 this matched sequence will collate before @p __s.
928 * @retval =0 this matched sequence is equivalent to @p __s.
929 * @retval <0 this matched sequence will collate after @p __s.
932 compare(const string_type& __s) const
933 { return this->_M_str().compare(__s); }
936 compare(const value_type* __s) const
937 { return this->_M_str().compare(__s); }
938 // @}
940 // Non-standard, used by comparison operators
942 _M_compare(const value_type* __s, size_t __n) const
943 { return this->_M_str().compare({__s, __n}); }
945 private:
946 // Simplified basic_string_view for C++11
947 struct __string_view
949 using traits_type = typename string_type::traits_type;
951 __string_view() = default;
953 __string_view(const value_type* __s, size_t __n) noexcept
954 : _M_data(__s), _M_len(__n) { }
956 __string_view(const value_type* __s) noexcept
957 : _M_data(__s), _M_len(traits_type::length(__s)) { }
959 __string_view(const string_type& __s) noexcept
960 : _M_data(__s.data()), _M_len(__s.length()) { }
963 compare(__string_view __s) const noexcept
965 if (const size_t __n = std::min(_M_len, __s._M_len))
966 if (int __ret = traits_type::compare(_M_data, __s._M_data, __n))
967 return __ret;
968 const difference_type __diff = _M_len - __s._M_len;
969 if (__diff > std::numeric_limits<int>::max())
970 return std::numeric_limits<int>::max();
971 if (__diff < std::numeric_limits<int>::min())
972 return std::numeric_limits<int>::min();
973 return static_cast<int>(__diff);
976 private:
977 const value_type* _M_data = nullptr;
978 size_t _M_len = 0;
981 // Create a __string_view over the iterator range.
982 template<typename _Iter = _BiIter>
983 __enable_if_t<__detail::__is_contiguous_iter<_Iter>::value,
984 __string_view>
985 _M_str() const noexcept
987 if (this->matched)
988 if (auto __len = this->second - this->first)
989 return { std::__addressof(*this->first), __len };
990 return {};
993 // Create a temporary string that can be converted to __string_view.
994 template<typename _Iter = _BiIter>
995 __enable_if_t<!__detail::__is_contiguous_iter<_Iter>::value,
996 string_type>
997 _M_str() const
998 { return str(); }
1002 /** @brief Standard regex submatch over a C-style null-terminated string. */
1003 typedef sub_match<const char*> csub_match;
1005 /** @brief Standard regex submatch over a standard string. */
1006 typedef sub_match<string::const_iterator> ssub_match;
1008 #ifdef _GLIBCXX_USE_WCHAR_T
1009 /** @brief Regex submatch over a C-style null-terminated wide string. */
1010 typedef sub_match<const wchar_t*> wcsub_match;
1012 /** @brief Regex submatch over a standard wide string. */
1013 typedef sub_match<wstring::const_iterator> wssub_match;
1014 #endif
1016 // [7.9.2] sub_match non-member operators
1019 * @brief Tests the equivalence of two regular expression submatches.
1020 * @param __lhs First regular expression submatch.
1021 * @param __rhs Second regular expression submatch.
1022 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1024 template<typename _BiIter>
1025 inline bool
1026 operator==(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
1027 { return __lhs.compare(__rhs) == 0; }
1030 * @brief Tests the inequivalence of two regular expression submatches.
1031 * @param __lhs First regular expression submatch.
1032 * @param __rhs Second regular expression submatch.
1033 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1035 template<typename _BiIter>
1036 inline bool
1037 operator!=(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
1038 { return __lhs.compare(__rhs) != 0; }
1041 * @brief Tests the ordering of two regular expression submatches.
1042 * @param __lhs First regular expression submatch.
1043 * @param __rhs Second regular expression submatch.
1044 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1046 template<typename _BiIter>
1047 inline bool
1048 operator<(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
1049 { return __lhs.compare(__rhs) < 0; }
1052 * @brief Tests the ordering of two regular expression submatches.
1053 * @param __lhs First regular expression submatch.
1054 * @param __rhs Second regular expression submatch.
1055 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1057 template<typename _BiIter>
1058 inline bool
1059 operator<=(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
1060 { return __lhs.compare(__rhs) <= 0; }
1063 * @brief Tests the ordering of two regular expression submatches.
1064 * @param __lhs First regular expression submatch.
1065 * @param __rhs Second regular expression submatch.
1066 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1068 template<typename _BiIter>
1069 inline bool
1070 operator>=(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
1071 { return __lhs.compare(__rhs) >= 0; }
1074 * @brief Tests the ordering of two regular expression submatches.
1075 * @param __lhs First regular expression submatch.
1076 * @param __rhs Second regular expression submatch.
1077 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1079 template<typename _BiIter>
1080 inline bool
1081 operator>(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
1082 { return __lhs.compare(__rhs) > 0; }
1084 // Alias for a basic_string that can be compared to a sub_match.
1085 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1086 using __sub_match_string = basic_string<
1087 typename iterator_traits<_Bi_iter>::value_type,
1088 _Ch_traits, _Ch_alloc>;
1091 * @brief Tests the equivalence of a string and a regular expression
1092 * submatch.
1093 * @param __lhs A string.
1094 * @param __rhs A regular expression submatch.
1095 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1097 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1098 inline bool
1099 operator==(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1100 const sub_match<_Bi_iter>& __rhs)
1101 { return __rhs._M_compare(__lhs.data(), __lhs.size()) == 0; }
1104 * @brief Tests the inequivalence of a string and a regular expression
1105 * submatch.
1106 * @param __lhs A string.
1107 * @param __rhs A regular expression submatch.
1108 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1110 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1111 inline bool
1112 operator!=(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1113 const sub_match<_Bi_iter>& __rhs)
1114 { return !(__lhs == __rhs); }
1117 * @brief Tests the ordering of a string and a regular expression submatch.
1118 * @param __lhs A string.
1119 * @param __rhs A regular expression submatch.
1120 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1122 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1123 inline bool
1124 operator<(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1125 const sub_match<_Bi_iter>& __rhs)
1126 { return __rhs._M_compare(__lhs.data(), __lhs.size()) > 0; }
1129 * @brief Tests the ordering of a string and a regular expression submatch.
1130 * @param __lhs A string.
1131 * @param __rhs A regular expression submatch.
1132 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1134 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1135 inline bool
1136 operator>(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1137 const sub_match<_Bi_iter>& __rhs)
1138 { return __rhs < __lhs; }
1141 * @brief Tests the ordering of a string and a regular expression submatch.
1142 * @param __lhs A string.
1143 * @param __rhs A regular expression submatch.
1144 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1146 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1147 inline bool
1148 operator>=(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1149 const sub_match<_Bi_iter>& __rhs)
1150 { return !(__lhs < __rhs); }
1153 * @brief Tests the ordering of a string and a regular expression submatch.
1154 * @param __lhs A string.
1155 * @param __rhs A regular expression submatch.
1156 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1158 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1159 inline bool
1160 operator<=(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1161 const sub_match<_Bi_iter>& __rhs)
1162 { return !(__rhs < __lhs); }
1165 * @brief Tests the equivalence of a regular expression submatch and a
1166 * string.
1167 * @param __lhs A regular expression submatch.
1168 * @param __rhs A string.
1169 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1171 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1172 inline bool
1173 operator==(const sub_match<_Bi_iter>& __lhs,
1174 const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1175 { return __lhs._M_compare(__rhs.data(), __rhs.size()) == 0; }
1178 * @brief Tests the inequivalence of a regular expression submatch and a
1179 * string.
1180 * @param __lhs A regular expression submatch.
1181 * @param __rhs A string.
1182 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1184 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1185 inline bool
1186 operator!=(const sub_match<_Bi_iter>& __lhs,
1187 const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1188 { return !(__lhs == __rhs); }
1191 * @brief Tests the ordering of a regular expression submatch and a string.
1192 * @param __lhs A regular expression submatch.
1193 * @param __rhs A string.
1194 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1196 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1197 inline bool
1198 operator<(const sub_match<_Bi_iter>& __lhs,
1199 const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1200 { return __lhs._M_compare(__rhs.data(), __rhs.size()) < 0; }
1203 * @brief Tests the ordering of a regular expression submatch and a string.
1204 * @param __lhs A regular expression submatch.
1205 * @param __rhs A string.
1206 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1208 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1209 inline bool
1210 operator>(const sub_match<_Bi_iter>& __lhs,
1211 const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1212 { return __rhs < __lhs; }
1215 * @brief Tests the ordering of a regular expression submatch and a string.
1216 * @param __lhs A regular expression submatch.
1217 * @param __rhs A string.
1218 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1220 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1221 inline bool
1222 operator>=(const sub_match<_Bi_iter>& __lhs,
1223 const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1224 { return !(__lhs < __rhs); }
1227 * @brief Tests the ordering of a regular expression submatch and a string.
1228 * @param __lhs A regular expression submatch.
1229 * @param __rhs A string.
1230 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1232 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1233 inline bool
1234 operator<=(const sub_match<_Bi_iter>& __lhs,
1235 const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1236 { return !(__rhs < __lhs); }
1239 * @brief Tests the equivalence of a C string and a regular expression
1240 * submatch.
1241 * @param __lhs A null-terminated string.
1242 * @param __rhs A regular expression submatch.
1243 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1245 template<typename _Bi_iter>
1246 inline bool
1247 operator==(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1248 const sub_match<_Bi_iter>& __rhs)
1249 { return __rhs.compare(__lhs) == 0; }
1252 * @brief Tests the inequivalence of a C string and a regular
1253 * expression submatch.
1254 * @param __lhs A null-terminated string.
1255 * @param __rhs A regular expression submatch.
1256 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1258 template<typename _Bi_iter>
1259 inline bool
1260 operator!=(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1261 const sub_match<_Bi_iter>& __rhs)
1262 { return !(__lhs == __rhs); }
1265 * @brief Tests the ordering of a C string and a regular expression submatch.
1266 * @param __lhs A null-terminated string.
1267 * @param __rhs A regular expression submatch.
1268 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1270 template<typename _Bi_iter>
1271 inline bool
1272 operator<(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1273 const sub_match<_Bi_iter>& __rhs)
1274 { return __rhs.compare(__lhs) > 0; }
1277 * @brief Tests the ordering of a C string and a regular expression submatch.
1278 * @param __lhs A null-terminated string.
1279 * @param __rhs A regular expression submatch.
1280 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1282 template<typename _Bi_iter>
1283 inline bool
1284 operator>(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1285 const sub_match<_Bi_iter>& __rhs)
1286 { return __rhs < __lhs; }
1289 * @brief Tests the ordering of a C string and a regular expression submatch.
1290 * @param __lhs A null-terminated string.
1291 * @param __rhs A regular expression submatch.
1292 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1294 template<typename _Bi_iter>
1295 inline bool
1296 operator>=(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1297 const sub_match<_Bi_iter>& __rhs)
1298 { return !(__lhs < __rhs); }
1301 * @brief Tests the ordering of a C string and a regular expression submatch.
1302 * @param __lhs A null-terminated string.
1303 * @param __rhs A regular expression submatch.
1304 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1306 template<typename _Bi_iter>
1307 inline bool
1308 operator<=(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1309 const sub_match<_Bi_iter>& __rhs)
1310 { return !(__rhs < __lhs); }
1313 * @brief Tests the equivalence of a regular expression submatch and a C
1314 * string.
1315 * @param __lhs A regular expression submatch.
1316 * @param __rhs A null-terminated string.
1317 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1319 template<typename _Bi_iter>
1320 inline bool
1321 operator==(const sub_match<_Bi_iter>& __lhs,
1322 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1323 { return __lhs.compare(__rhs) == 0; }
1326 * @brief Tests the inequivalence of a regular expression submatch and a
1327 * string.
1328 * @param __lhs A regular expression submatch.
1329 * @param __rhs A null-terminated string.
1330 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1332 template<typename _Bi_iter>
1333 inline bool
1334 operator!=(const sub_match<_Bi_iter>& __lhs,
1335 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1336 { return !(__lhs == __rhs); }
1339 * @brief Tests the ordering of a regular expression submatch and a C string.
1340 * @param __lhs A regular expression submatch.
1341 * @param __rhs A null-terminated string.
1342 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1344 template<typename _Bi_iter>
1345 inline bool
1346 operator<(const sub_match<_Bi_iter>& __lhs,
1347 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1348 { return __lhs.compare(__rhs) < 0; }
1351 * @brief Tests the ordering of a regular expression submatch and a C string.
1352 * @param __lhs A regular expression submatch.
1353 * @param __rhs A null-terminated string.
1354 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1356 template<typename _Bi_iter>
1357 inline bool
1358 operator>(const sub_match<_Bi_iter>& __lhs,
1359 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1360 { return __rhs < __lhs; }
1363 * @brief Tests the ordering of a regular expression submatch and a C string.
1364 * @param __lhs A regular expression submatch.
1365 * @param __rhs A null-terminated string.
1366 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1368 template<typename _Bi_iter>
1369 inline bool
1370 operator>=(const sub_match<_Bi_iter>& __lhs,
1371 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1372 { return !(__lhs < __rhs); }
1375 * @brief Tests the ordering of a regular expression submatch and a C string.
1376 * @param __lhs A regular expression submatch.
1377 * @param __rhs A null-terminated string.
1378 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1380 template<typename _Bi_iter>
1381 inline bool
1382 operator<=(const sub_match<_Bi_iter>& __lhs,
1383 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1384 { return !(__rhs < __lhs); }
1387 * @brief Tests the equivalence of a character and a regular expression
1388 * submatch.
1389 * @param __lhs A character.
1390 * @param __rhs A regular expression submatch.
1391 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1393 template<typename _Bi_iter>
1394 inline bool
1395 operator==(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1396 const sub_match<_Bi_iter>& __rhs)
1397 { return __rhs._M_compare(std::__addressof(__lhs), 1) == 0; }
1400 * @brief Tests the inequivalence of a character and a regular expression
1401 * submatch.
1402 * @param __lhs A character.
1403 * @param __rhs A regular expression submatch.
1404 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1406 template<typename _Bi_iter>
1407 inline bool
1408 operator!=(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1409 const sub_match<_Bi_iter>& __rhs)
1410 { return !(__lhs == __rhs); }
1413 * @brief Tests the ordering of a character and a regular expression
1414 * submatch.
1415 * @param __lhs A character.
1416 * @param __rhs A regular expression submatch.
1417 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1419 template<typename _Bi_iter>
1420 inline bool
1421 operator<(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1422 const sub_match<_Bi_iter>& __rhs)
1423 { return __rhs._M_compare(std::__addressof(__lhs), 1) > 0; }
1426 * @brief Tests the ordering of a character and a regular expression
1427 * submatch.
1428 * @param __lhs A character.
1429 * @param __rhs A regular expression submatch.
1430 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1432 template<typename _Bi_iter>
1433 inline bool
1434 operator>(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1435 const sub_match<_Bi_iter>& __rhs)
1436 { return __rhs < __lhs; }
1439 * @brief Tests the ordering of a character and a regular expression
1440 * submatch.
1441 * @param __lhs A character.
1442 * @param __rhs A regular expression submatch.
1443 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1445 template<typename _Bi_iter>
1446 inline bool
1447 operator>=(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1448 const sub_match<_Bi_iter>& __rhs)
1449 { return !(__lhs < __rhs); }
1452 * @brief Tests the ordering of a character and a regular expression
1453 * submatch.
1454 * @param __lhs A character.
1455 * @param __rhs A regular expression submatch.
1456 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1458 template<typename _Bi_iter>
1459 inline bool
1460 operator<=(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1461 const sub_match<_Bi_iter>& __rhs)
1462 { return !(__rhs < __lhs); }
1465 * @brief Tests the equivalence of a regular expression submatch and a
1466 * character.
1467 * @param __lhs A regular expression submatch.
1468 * @param __rhs A character.
1469 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
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._M_compare(std::__addressof(__rhs), 1) == 0; }
1478 * @brief Tests the inequivalence of a regular expression submatch and a
1479 * character.
1480 * @param __lhs A regular expression submatch.
1481 * @param __rhs A character.
1482 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1484 template<typename _Bi_iter>
1485 inline bool
1486 operator!=(const sub_match<_Bi_iter>& __lhs,
1487 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1488 { return !(__lhs == __rhs); }
1491 * @brief Tests the ordering of a regular expression submatch and a
1492 * character.
1493 * @param __lhs A regular expression submatch.
1494 * @param __rhs A character.
1495 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1497 template<typename _Bi_iter>
1498 inline bool
1499 operator<(const sub_match<_Bi_iter>& __lhs,
1500 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1501 { return __lhs._M_compare(std::__addressof(__rhs), 1) < 0; }
1504 * @brief Tests the ordering of a regular expression submatch and a
1505 * character.
1506 * @param __lhs A regular expression submatch.
1507 * @param __rhs A character.
1508 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1510 template<typename _Bi_iter>
1511 inline bool
1512 operator>(const sub_match<_Bi_iter>& __lhs,
1513 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1514 { return __rhs < __lhs; }
1517 * @brief Tests the ordering of a regular expression submatch and a
1518 * character.
1519 * @param __lhs A regular expression submatch.
1520 * @param __rhs A character.
1521 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1523 template<typename _Bi_iter>
1524 inline bool
1525 operator>=(const sub_match<_Bi_iter>& __lhs,
1526 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1527 { return !(__lhs < __rhs); }
1530 * @brief Tests the ordering of a regular expression submatch and a
1531 * character.
1532 * @param __lhs A regular expression submatch.
1533 * @param __rhs A character.
1534 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1536 template<typename _Bi_iter>
1537 inline bool
1538 operator<=(const sub_match<_Bi_iter>& __lhs,
1539 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1540 { return !(__rhs < __lhs); }
1543 * @brief Inserts a matched string into an output stream.
1545 * @param __os The output stream.
1546 * @param __m A submatch string.
1548 * @returns the output stream with the submatch string inserted.
1550 template<typename _Ch_type, typename _Ch_traits, typename _Bi_iter>
1551 inline
1552 basic_ostream<_Ch_type, _Ch_traits>&
1553 operator<<(basic_ostream<_Ch_type, _Ch_traits>& __os,
1554 const sub_match<_Bi_iter>& __m)
1555 { return __os << __m.str(); }
1557 // [7.10] Class template match_results
1560 * @brief The results of a match or search operation.
1562 * A collection of character sequences representing the result of a regular
1563 * expression match. Storage for the collection is allocated and freed as
1564 * necessary by the member functions of class template match_results.
1566 * This class satisfies the Sequence requirements, with the exception that
1567 * only the operations defined for a const-qualified Sequence are supported.
1569 * The sub_match object stored at index 0 represents sub-expression 0, i.e.
1570 * the whole match. In this case the %sub_match member matched is always true.
1571 * The sub_match object stored at index n denotes what matched the marked
1572 * sub-expression n within the matched expression. If the sub-expression n
1573 * participated in a regular expression match then the %sub_match member
1574 * matched evaluates to true, and members first and second denote the range
1575 * of characters [first, second) which formed that match. Otherwise matched
1576 * is false, and members first and second point to the end of the sequence
1577 * that was searched.
1579 * @nosubgrouping
1581 template<typename _Bi_iter,
1582 typename _Alloc = allocator<sub_match<_Bi_iter> > >
1583 class match_results
1584 : private std::vector<sub_match<_Bi_iter>, _Alloc>
1586 private:
1588 * The vector base is empty if this does not represent a match (!ready());
1589 * Otherwise if it's a match failure, it contains 3 elements:
1590 * [0] unmatched
1591 * [1] prefix
1592 * [2] suffix
1593 * Otherwise it contains n+4 elements where n is the number of marked
1594 * sub-expressions:
1595 * [0] entire match
1596 * [1] 1st marked subexpression
1597 * ...
1598 * [n] nth marked subexpression
1599 * [n+1] unmatched
1600 * [n+2] prefix
1601 * [n+3] suffix
1603 typedef std::vector<sub_match<_Bi_iter>, _Alloc> _Base_type;
1604 typedef std::iterator_traits<_Bi_iter> __iter_traits;
1605 typedef regex_constants::match_flag_type match_flag_type;
1607 public:
1609 * @name 10.? Public Types
1611 //@{
1612 typedef sub_match<_Bi_iter> value_type;
1613 typedef const value_type& const_reference;
1614 typedef value_type& reference;
1615 typedef typename _Base_type::const_iterator const_iterator;
1616 typedef const_iterator iterator;
1617 typedef typename __iter_traits::difference_type difference_type;
1618 typedef typename allocator_traits<_Alloc>::size_type size_type;
1619 typedef _Alloc allocator_type;
1620 typedef typename __iter_traits::value_type char_type;
1621 typedef std::basic_string<char_type> string_type;
1622 //@}
1624 public:
1626 * @name 28.10.1 Construction, Copying, and Destruction
1628 //@{
1631 * @brief Constructs a default %match_results container.
1632 * @post size() returns 0 and str() returns an empty string.
1633 * @{
1635 match_results() : match_results(_Alloc()) { }
1637 explicit
1638 match_results(const _Alloc& __a) noexcept
1639 : _Base_type(__a)
1642 // @}
1645 * @brief Copy constructs a %match_results.
1647 match_results(const match_results&) = default;
1650 * @brief Move constructs a %match_results.
1652 match_results(match_results&&) noexcept = default;
1655 * @brief Assigns rhs to *this.
1657 match_results&
1658 operator=(const match_results&) = default;
1661 * @brief Move-assigns rhs to *this.
1663 match_results&
1664 operator=(match_results&&) = default;
1667 * @brief Destroys a %match_results object.
1669 ~match_results() = default;
1671 //@}
1673 // 28.10.2, state:
1675 * @brief Indicates if the %match_results is ready.
1676 * @retval true The object has a fully-established result state.
1677 * @retval false The object is not ready.
1679 bool ready() const noexcept { return !_Base_type::empty(); }
1682 * @name 28.10.2 Size
1684 //@{
1687 * @brief Gets the number of matches and submatches.
1689 * The number of matches for a given regular expression will be either 0
1690 * if there was no match or mark_count() + 1 if a match was successful.
1691 * Some matches may be empty.
1693 * @returns the number of matches found.
1695 size_type
1696 size() const noexcept
1697 { return _Base_type::empty() ? 0 : _Base_type::size() - 3; }
1699 size_type
1700 max_size() const noexcept
1701 { return _Base_type::max_size(); }
1704 * @brief Indicates if the %match_results contains no results.
1705 * @retval true The %match_results object is empty.
1706 * @retval false The %match_results object is not empty.
1708 bool
1709 empty() const noexcept
1710 { return size() == 0; }
1712 //@}
1715 * @name 10.3 Element Access
1717 //@{
1720 * @brief Gets the length of the indicated submatch.
1721 * @param __sub indicates the submatch.
1722 * @pre ready() == true
1724 * This function returns the length of the indicated submatch, or the
1725 * length of the entire match if @p __sub is zero (the default).
1727 difference_type
1728 length(size_type __sub = 0) const
1729 { return (*this)[__sub].length(); }
1732 * @brief Gets the offset of the beginning of the indicated submatch.
1733 * @param __sub indicates the submatch.
1734 * @pre ready() == true
1736 * This function returns the offset from the beginning of the target
1737 * sequence to the beginning of the submatch, unless the value of @p __sub
1738 * is zero (the default), in which case this function returns the offset
1739 * from the beginning of the target sequence to the beginning of the
1740 * match.
1742 difference_type
1743 position(size_type __sub = 0) const
1744 { return std::distance(_M_begin, (*this)[__sub].first); }
1747 * @brief Gets the match or submatch converted to a string type.
1748 * @param __sub indicates the submatch.
1749 * @pre ready() == true
1751 * This function gets the submatch (or match, if @p __sub is
1752 * zero) extracted from the target range and converted to the
1753 * associated string type.
1755 string_type
1756 str(size_type __sub = 0) const
1757 { return string_type((*this)[__sub]); }
1760 * @brief Gets a %sub_match reference for the match or submatch.
1761 * @param __sub indicates the submatch.
1762 * @pre ready() == true
1764 * This function gets a reference to the indicated submatch, or
1765 * the entire match if @p __sub is zero.
1767 * If @p __sub >= size() then this function returns a %sub_match with a
1768 * special value indicating no submatch.
1770 const_reference
1771 operator[](size_type __sub) const
1773 __glibcxx_assert( ready() );
1774 return __sub < size()
1775 ? _Base_type::operator[](__sub)
1776 : _M_unmatched_sub();
1780 * @brief Gets a %sub_match representing the match prefix.
1781 * @pre ready() == true
1783 * This function gets a reference to a %sub_match object representing the
1784 * part of the target range between the start of the target range and the
1785 * start of the match.
1787 const_reference
1788 prefix() const
1790 __glibcxx_assert( ready() );
1791 return !empty() ? _M_prefix() : _M_unmatched_sub();
1795 * @brief Gets a %sub_match representing the match suffix.
1796 * @pre ready() == true
1798 * This function gets a reference to a %sub_match object representing the
1799 * part of the target range between the end of the match and the end of
1800 * the target range.
1802 const_reference
1803 suffix() const
1805 __glibcxx_assert( ready() );
1806 return !empty() ? _M_suffix() : _M_unmatched_sub();
1810 * @brief Gets an iterator to the start of the %sub_match collection.
1812 const_iterator
1813 begin() const noexcept
1814 { return _Base_type::begin(); }
1817 * @brief Gets an iterator to the start of the %sub_match collection.
1819 const_iterator
1820 cbegin() const noexcept
1821 { return this->begin(); }
1824 * @brief Gets an iterator to one-past-the-end of the collection.
1826 const_iterator
1827 end() const noexcept
1828 { return _Base_type::end() - (empty() ? 0 : 3); }
1831 * @brief Gets an iterator to one-past-the-end of the collection.
1833 const_iterator
1834 cend() const noexcept
1835 { return this->end(); }
1837 //@}
1840 * @name 10.4 Formatting
1842 * These functions perform formatted substitution of the matched
1843 * character sequences into their target. The format specifiers and
1844 * escape sequences accepted by these functions are determined by
1845 * their @p flags parameter as documented above.
1847 //@{
1850 * @pre ready() == true
1852 template<typename _Out_iter>
1853 _Out_iter
1854 format(_Out_iter __out, const char_type* __fmt_first,
1855 const char_type* __fmt_last,
1856 match_flag_type __flags = regex_constants::format_default) const;
1859 * @pre ready() == true
1861 template<typename _Out_iter, typename _St, typename _Sa>
1862 _Out_iter
1863 format(_Out_iter __out, const basic_string<char_type, _St, _Sa>& __fmt,
1864 match_flag_type __flags = regex_constants::format_default) const
1866 return format(__out, __fmt.data(), __fmt.data() + __fmt.size(),
1867 __flags);
1871 * @pre ready() == true
1873 template<typename _St, typename _Sa>
1874 basic_string<char_type, _St, _Sa>
1875 format(const basic_string<char_type, _St, _Sa>& __fmt,
1876 match_flag_type __flags = regex_constants::format_default) const
1878 basic_string<char_type, _St, _Sa> __result;
1879 format(std::back_inserter(__result), __fmt, __flags);
1880 return __result;
1884 * @pre ready() == true
1886 string_type
1887 format(const char_type* __fmt,
1888 match_flag_type __flags = regex_constants::format_default) const
1890 string_type __result;
1891 format(std::back_inserter(__result),
1892 __fmt,
1893 __fmt + char_traits<char_type>::length(__fmt),
1894 __flags);
1895 return __result;
1898 //@}
1901 * @name 10.5 Allocator
1903 //@{
1906 * @brief Gets a copy of the allocator.
1908 allocator_type
1909 get_allocator() const noexcept
1910 { return _Base_type::get_allocator(); }
1912 //@}
1915 * @name 10.6 Swap
1917 //@{
1920 * @brief Swaps the contents of two match_results.
1922 void
1923 swap(match_results& __that) noexcept
1925 using std::swap;
1926 _Base_type::swap(__that);
1927 swap(_M_begin, __that._M_begin);
1929 //@}
1931 private:
1932 template<typename, typename, typename, bool>
1933 friend class __detail::_Executor;
1935 template<typename, typename, typename>
1936 friend class regex_iterator;
1938 template<typename _Bp, typename _Ap, typename _Cp, typename _Rp,
1939 __detail::_RegexExecutorPolicy, bool>
1940 friend bool
1941 __detail::__regex_algo_impl(_Bp, _Bp, match_results<_Bp, _Ap>&,
1942 const basic_regex<_Cp, _Rp>&,
1943 regex_constants::match_flag_type);
1945 void
1946 _M_resize(unsigned int __size)
1947 { _Base_type::resize(__size + 3); }
1949 const_reference
1950 _M_unmatched_sub() const
1951 { return _Base_type::operator[](_Base_type::size() - 3); }
1953 sub_match<_Bi_iter>&
1954 _M_unmatched_sub()
1955 { return _Base_type::operator[](_Base_type::size() - 3); }
1957 const_reference
1958 _M_prefix() const
1959 { return _Base_type::operator[](_Base_type::size() - 2); }
1961 sub_match<_Bi_iter>&
1962 _M_prefix()
1963 { return _Base_type::operator[](_Base_type::size() - 2); }
1965 const_reference
1966 _M_suffix() const
1967 { return _Base_type::operator[](_Base_type::size() - 1); }
1969 sub_match<_Bi_iter>&
1970 _M_suffix()
1971 { return _Base_type::operator[](_Base_type::size() - 1); }
1973 _Bi_iter _M_begin;
1976 typedef match_results<const char*> cmatch;
1977 typedef match_results<string::const_iterator> smatch;
1978 #ifdef _GLIBCXX_USE_WCHAR_T
1979 typedef match_results<const wchar_t*> wcmatch;
1980 typedef match_results<wstring::const_iterator> wsmatch;
1981 #endif
1983 // match_results comparisons
1985 * @brief Compares two match_results for equality.
1986 * @returns true if the two objects refer to the same match,
1987 * false otherwise.
1989 template<typename _Bi_iter, typename _Alloc>
1990 inline bool
1991 operator==(const match_results<_Bi_iter, _Alloc>& __m1,
1992 const match_results<_Bi_iter, _Alloc>& __m2) noexcept
1994 if (__m1.ready() != __m2.ready())
1995 return false;
1996 if (!__m1.ready()) // both are not ready
1997 return true;
1998 if (__m1.empty() != __m2.empty())
1999 return false;
2000 if (__m1.empty()) // both are empty
2001 return true;
2002 return __m1.prefix() == __m2.prefix()
2003 && __m1.size() == __m2.size()
2004 && std::equal(__m1.begin(), __m1.end(), __m2.begin())
2005 && __m1.suffix() == __m2.suffix();
2009 * @brief Compares two match_results for inequality.
2010 * @returns true if the two objects do not refer to the same match,
2011 * false otherwise.
2013 template<typename _Bi_iter, class _Alloc>
2014 inline bool
2015 operator!=(const match_results<_Bi_iter, _Alloc>& __m1,
2016 const match_results<_Bi_iter, _Alloc>& __m2) noexcept
2017 { return !(__m1 == __m2); }
2019 // [7.10.6] match_results swap
2021 * @brief Swaps two match results.
2022 * @param __lhs A match result.
2023 * @param __rhs A match result.
2025 * The contents of the two match_results objects are swapped.
2027 template<typename _Bi_iter, typename _Alloc>
2028 inline void
2029 swap(match_results<_Bi_iter, _Alloc>& __lhs,
2030 match_results<_Bi_iter, _Alloc>& __rhs) noexcept
2031 { __lhs.swap(__rhs); }
2033 _GLIBCXX_END_NAMESPACE_CXX11
2035 // [7.11.2] Function template regex_match
2037 * @name Matching, Searching, and Replacing
2039 //@{
2042 * @brief Determines if there is a match between the regular expression @p e
2043 * and all of the character sequence [first, last).
2045 * @param __s Start of the character sequence to match.
2046 * @param __e One-past-the-end of the character sequence to match.
2047 * @param __m The match results.
2048 * @param __re The regular expression.
2049 * @param __flags Controls how the regular expression is matched.
2051 * @retval true A match exists.
2052 * @retval false Otherwise.
2054 * @throws an exception of type regex_error.
2056 template<typename _Bi_iter, typename _Alloc,
2057 typename _Ch_type, typename _Rx_traits>
2058 inline bool
2059 regex_match(_Bi_iter __s,
2060 _Bi_iter __e,
2061 match_results<_Bi_iter, _Alloc>& __m,
2062 const basic_regex<_Ch_type, _Rx_traits>& __re,
2063 regex_constants::match_flag_type __flags
2064 = regex_constants::match_default)
2066 return __detail::__regex_algo_impl<_Bi_iter, _Alloc, _Ch_type, _Rx_traits,
2067 __detail::_RegexExecutorPolicy::_S_auto, true>
2068 (__s, __e, __m, __re, __flags);
2072 * @brief Indicates if there is a match between the regular expression @p e
2073 * and all of the character sequence [first, last).
2075 * @param __first Beginning of the character sequence to match.
2076 * @param __last One-past-the-end of the character sequence to match.
2077 * @param __re The regular expression.
2078 * @param __flags Controls how the regular expression is matched.
2080 * @retval true A match exists.
2081 * @retval false Otherwise.
2083 * @throws an exception of type regex_error.
2085 template<typename _Bi_iter, typename _Ch_type, typename _Rx_traits>
2086 inline bool
2087 regex_match(_Bi_iter __first, _Bi_iter __last,
2088 const basic_regex<_Ch_type, _Rx_traits>& __re,
2089 regex_constants::match_flag_type __flags
2090 = regex_constants::match_default)
2092 match_results<_Bi_iter> __what;
2093 return regex_match(__first, __last, __what, __re, __flags);
2097 * @brief Determines if there is a match between the regular expression @p e
2098 * and a C-style null-terminated string.
2100 * @param __s The C-style null-terminated string to match.
2101 * @param __m The match results.
2102 * @param __re The regular expression.
2103 * @param __f Controls how the regular expression is matched.
2105 * @retval true A match exists.
2106 * @retval false Otherwise.
2108 * @throws an exception of type regex_error.
2110 template<typename _Ch_type, typename _Alloc, typename _Rx_traits>
2111 inline bool
2112 regex_match(const _Ch_type* __s,
2113 match_results<const _Ch_type*, _Alloc>& __m,
2114 const basic_regex<_Ch_type, _Rx_traits>& __re,
2115 regex_constants::match_flag_type __f
2116 = regex_constants::match_default)
2117 { return regex_match(__s, __s + _Rx_traits::length(__s), __m, __re, __f); }
2120 * @brief Determines if there is a match between the regular expression @p e
2121 * and a string.
2123 * @param __s The string to match.
2124 * @param __m The match results.
2125 * @param __re The regular expression.
2126 * @param __flags Controls how the regular expression is matched.
2128 * @retval true A match exists.
2129 * @retval false Otherwise.
2131 * @throws an exception of type regex_error.
2133 template<typename _Ch_traits, typename _Ch_alloc,
2134 typename _Alloc, typename _Ch_type, typename _Rx_traits>
2135 inline bool
2136 regex_match(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>& __s,
2137 match_results<typename basic_string<_Ch_type,
2138 _Ch_traits, _Ch_alloc>::const_iterator, _Alloc>& __m,
2139 const basic_regex<_Ch_type, _Rx_traits>& __re,
2140 regex_constants::match_flag_type __flags
2141 = regex_constants::match_default)
2142 { return regex_match(__s.begin(), __s.end(), __m, __re, __flags); }
2144 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2145 // 2329. regex_match() with match_results should forbid temporary strings
2146 /// Prevent unsafe attempts to get match_results from a temporary string.
2147 template<typename _Ch_traits, typename _Ch_alloc,
2148 typename _Alloc, typename _Ch_type, typename _Rx_traits>
2149 bool
2150 regex_match(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>&&,
2151 match_results<typename basic_string<_Ch_type,
2152 _Ch_traits, _Ch_alloc>::const_iterator, _Alloc>&,
2153 const basic_regex<_Ch_type, _Rx_traits>&,
2154 regex_constants::match_flag_type
2155 = regex_constants::match_default) = delete;
2158 * @brief Indicates if there is a match between the regular expression @p e
2159 * and a C-style null-terminated string.
2161 * @param __s The C-style null-terminated string to match.
2162 * @param __re The regular expression.
2163 * @param __f Controls how the regular expression is matched.
2165 * @retval true A match exists.
2166 * @retval false Otherwise.
2168 * @throws an exception of type regex_error.
2170 template<typename _Ch_type, class _Rx_traits>
2171 inline bool
2172 regex_match(const _Ch_type* __s,
2173 const basic_regex<_Ch_type, _Rx_traits>& __re,
2174 regex_constants::match_flag_type __f
2175 = regex_constants::match_default)
2176 { return regex_match(__s, __s + _Rx_traits::length(__s), __re, __f); }
2179 * @brief Indicates if there is a match between the regular expression @p e
2180 * and a string.
2182 * @param __s [IN] The string to match.
2183 * @param __re [IN] The regular expression.
2184 * @param __flags [IN] Controls how the regular expression is matched.
2186 * @retval true A match exists.
2187 * @retval false Otherwise.
2189 * @throws an exception of type regex_error.
2191 template<typename _Ch_traits, typename _Str_allocator,
2192 typename _Ch_type, typename _Rx_traits>
2193 inline bool
2194 regex_match(const basic_string<_Ch_type, _Ch_traits, _Str_allocator>& __s,
2195 const basic_regex<_Ch_type, _Rx_traits>& __re,
2196 regex_constants::match_flag_type __flags
2197 = regex_constants::match_default)
2198 { return regex_match(__s.begin(), __s.end(), __re, __flags); }
2200 // [7.11.3] Function template regex_search
2202 * Searches for a regular expression within a range.
2203 * @param __s [IN] The start of the string to search.
2204 * @param __e [IN] One-past-the-end of the string to search.
2205 * @param __m [OUT] The match results.
2206 * @param __re [IN] The regular expression to search for.
2207 * @param __flags [IN] Search policy flags.
2208 * @retval true A match was found within the string.
2209 * @retval false No match was found within the string, the content of %m is
2210 * undefined.
2212 * @throws an exception of type regex_error.
2214 template<typename _Bi_iter, typename _Alloc,
2215 typename _Ch_type, typename _Rx_traits>
2216 inline bool
2217 regex_search(_Bi_iter __s, _Bi_iter __e,
2218 match_results<_Bi_iter, _Alloc>& __m,
2219 const basic_regex<_Ch_type, _Rx_traits>& __re,
2220 regex_constants::match_flag_type __flags
2221 = regex_constants::match_default)
2223 return __detail::__regex_algo_impl<_Bi_iter, _Alloc, _Ch_type, _Rx_traits,
2224 __detail::_RegexExecutorPolicy::_S_auto, false>
2225 (__s, __e, __m, __re, __flags);
2229 * Searches for a regular expression within a range.
2230 * @param __first [IN] The start of the string to search.
2231 * @param __last [IN] One-past-the-end of the string to search.
2232 * @param __re [IN] The regular expression to search for.
2233 * @param __flags [IN] Search policy flags.
2234 * @retval true A match was found within the string.
2235 * @retval false No match was found within the string.
2237 * @throws an exception of type regex_error.
2239 template<typename _Bi_iter, typename _Ch_type, typename _Rx_traits>
2240 inline bool
2241 regex_search(_Bi_iter __first, _Bi_iter __last,
2242 const basic_regex<_Ch_type, _Rx_traits>& __re,
2243 regex_constants::match_flag_type __flags
2244 = regex_constants::match_default)
2246 match_results<_Bi_iter> __what;
2247 return regex_search(__first, __last, __what, __re, __flags);
2251 * @brief Searches for a regular expression within a C-string.
2252 * @param __s [IN] A C-string to search for the regex.
2253 * @param __m [OUT] The set of regex matches.
2254 * @param __e [IN] The regex to search for in @p s.
2255 * @param __f [IN] The search flags.
2256 * @retval true A match was found within the string.
2257 * @retval false No match was found within the string, the content of %m is
2258 * undefined.
2260 * @throws an exception of type regex_error.
2262 template<typename _Ch_type, class _Alloc, class _Rx_traits>
2263 inline bool
2264 regex_search(const _Ch_type* __s,
2265 match_results<const _Ch_type*, _Alloc>& __m,
2266 const basic_regex<_Ch_type, _Rx_traits>& __e,
2267 regex_constants::match_flag_type __f
2268 = regex_constants::match_default)
2269 { return regex_search(__s, __s + _Rx_traits::length(__s), __m, __e, __f); }
2272 * @brief Searches for a regular expression within a C-string.
2273 * @param __s [IN] The C-string to search.
2274 * @param __e [IN] The regular expression to search for.
2275 * @param __f [IN] Search policy flags.
2276 * @retval true A match was found within the string.
2277 * @retval false No match was found within the string.
2279 * @throws an exception of type regex_error.
2281 template<typename _Ch_type, typename _Rx_traits>
2282 inline bool
2283 regex_search(const _Ch_type* __s,
2284 const basic_regex<_Ch_type, _Rx_traits>& __e,
2285 regex_constants::match_flag_type __f
2286 = regex_constants::match_default)
2287 { return regex_search(__s, __s + _Rx_traits::length(__s), __e, __f); }
2290 * @brief Searches for a regular expression within a string.
2291 * @param __s [IN] The string to search.
2292 * @param __e [IN] The regular expression to search for.
2293 * @param __flags [IN] Search policy flags.
2294 * @retval true A match was found within the string.
2295 * @retval false No match was found within the string.
2297 * @throws an exception of type regex_error.
2299 template<typename _Ch_traits, typename _String_allocator,
2300 typename _Ch_type, typename _Rx_traits>
2301 inline bool
2302 regex_search(const basic_string<_Ch_type, _Ch_traits,
2303 _String_allocator>& __s,
2304 const basic_regex<_Ch_type, _Rx_traits>& __e,
2305 regex_constants::match_flag_type __flags
2306 = regex_constants::match_default)
2307 { return regex_search(__s.begin(), __s.end(), __e, __flags); }
2310 * @brief Searches for a regular expression within a string.
2311 * @param __s [IN] A C++ string to search for the regex.
2312 * @param __m [OUT] The set of regex matches.
2313 * @param __e [IN] The regex to search for in @p s.
2314 * @param __f [IN] The search flags.
2315 * @retval true A match was found within the string.
2316 * @retval false No match was found within the string, the content of %m is
2317 * undefined.
2319 * @throws an exception of type regex_error.
2321 template<typename _Ch_traits, typename _Ch_alloc,
2322 typename _Alloc, typename _Ch_type,
2323 typename _Rx_traits>
2324 inline bool
2325 regex_search(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>& __s,
2326 match_results<typename basic_string<_Ch_type,
2327 _Ch_traits, _Ch_alloc>::const_iterator, _Alloc>& __m,
2328 const basic_regex<_Ch_type, _Rx_traits>& __e,
2329 regex_constants::match_flag_type __f
2330 = regex_constants::match_default)
2331 { return regex_search(__s.begin(), __s.end(), __m, __e, __f); }
2333 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2334 // 2329. regex_search() with match_results should forbid temporary strings
2335 /// Prevent unsafe attempts to get match_results from a temporary string.
2336 template<typename _Ch_traits, typename _Ch_alloc,
2337 typename _Alloc, typename _Ch_type,
2338 typename _Rx_traits>
2339 bool
2340 regex_search(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>&&,
2341 match_results<typename basic_string<_Ch_type,
2342 _Ch_traits, _Ch_alloc>::const_iterator, _Alloc>&,
2343 const basic_regex<_Ch_type, _Rx_traits>&,
2344 regex_constants::match_flag_type
2345 = regex_constants::match_default) = delete;
2347 // std [28.11.4] Function template regex_replace
2349 * @brief Search for a regular expression within a range for multiple times,
2350 and replace the matched parts through filling a format string.
2351 * @param __out [OUT] The output iterator.
2352 * @param __first [IN] The start of the string to search.
2353 * @param __last [IN] One-past-the-end of the string to search.
2354 * @param __e [IN] The regular expression to search for.
2355 * @param __fmt [IN] The format string.
2356 * @param __flags [IN] Search and replace policy flags.
2358 * @returns __out
2359 * @throws an exception of type regex_error.
2361 template<typename _Out_iter, typename _Bi_iter,
2362 typename _Rx_traits, typename _Ch_type,
2363 typename _St, typename _Sa>
2364 inline _Out_iter
2365 regex_replace(_Out_iter __out, _Bi_iter __first, _Bi_iter __last,
2366 const basic_regex<_Ch_type, _Rx_traits>& __e,
2367 const basic_string<_Ch_type, _St, _Sa>& __fmt,
2368 regex_constants::match_flag_type __flags
2369 = regex_constants::match_default)
2371 return regex_replace(__out, __first, __last, __e, __fmt.c_str(), __flags);
2375 * @brief Search for a regular expression within a range for multiple times,
2376 and replace the matched parts through filling a format C-string.
2377 * @param __out [OUT] The output iterator.
2378 * @param __first [IN] The start of the string to search.
2379 * @param __last [IN] One-past-the-end of the string to search.
2380 * @param __e [IN] The regular expression to search for.
2381 * @param __fmt [IN] The format C-string.
2382 * @param __flags [IN] Search and replace policy flags.
2384 * @returns __out
2385 * @throws an exception of type regex_error.
2387 template<typename _Out_iter, typename _Bi_iter,
2388 typename _Rx_traits, typename _Ch_type>
2389 _Out_iter
2390 regex_replace(_Out_iter __out, _Bi_iter __first, _Bi_iter __last,
2391 const basic_regex<_Ch_type, _Rx_traits>& __e,
2392 const _Ch_type* __fmt,
2393 regex_constants::match_flag_type __flags
2394 = regex_constants::match_default);
2397 * @brief Search for a regular expression within a string for multiple times,
2398 and replace the matched parts through filling a format string.
2399 * @param __s [IN] The string to search and replace.
2400 * @param __e [IN] The regular expression to search for.
2401 * @param __fmt [IN] The format string.
2402 * @param __flags [IN] Search and replace policy flags.
2404 * @returns The string after replacing.
2405 * @throws an exception of type regex_error.
2407 template<typename _Rx_traits, typename _Ch_type,
2408 typename _St, typename _Sa, typename _Fst, typename _Fsa>
2409 inline basic_string<_Ch_type, _St, _Sa>
2410 regex_replace(const basic_string<_Ch_type, _St, _Sa>& __s,
2411 const basic_regex<_Ch_type, _Rx_traits>& __e,
2412 const basic_string<_Ch_type, _Fst, _Fsa>& __fmt,
2413 regex_constants::match_flag_type __flags
2414 = regex_constants::match_default)
2416 basic_string<_Ch_type, _St, _Sa> __result;
2417 regex_replace(std::back_inserter(__result),
2418 __s.begin(), __s.end(), __e, __fmt, __flags);
2419 return __result;
2423 * @brief Search for a regular expression within a string for multiple times,
2424 and replace the matched parts through filling a format C-string.
2425 * @param __s [IN] The string to search and replace.
2426 * @param __e [IN] The regular expression to search for.
2427 * @param __fmt [IN] The format C-string.
2428 * @param __flags [IN] Search and replace policy flags.
2430 * @returns The string after replacing.
2431 * @throws an exception of type regex_error.
2433 template<typename _Rx_traits, typename _Ch_type,
2434 typename _St, typename _Sa>
2435 inline basic_string<_Ch_type, _St, _Sa>
2436 regex_replace(const basic_string<_Ch_type, _St, _Sa>& __s,
2437 const basic_regex<_Ch_type, _Rx_traits>& __e,
2438 const _Ch_type* __fmt,
2439 regex_constants::match_flag_type __flags
2440 = regex_constants::match_default)
2442 basic_string<_Ch_type, _St, _Sa> __result;
2443 regex_replace(std::back_inserter(__result),
2444 __s.begin(), __s.end(), __e, __fmt, __flags);
2445 return __result;
2449 * @brief Search for a regular expression within a C-string for multiple
2450 times, and replace the matched parts through filling a format string.
2451 * @param __s [IN] The C-string to search and replace.
2452 * @param __e [IN] The regular expression to search for.
2453 * @param __fmt [IN] The format string.
2454 * @param __flags [IN] Search and replace policy flags.
2456 * @returns The string after replacing.
2457 * @throws an exception of type regex_error.
2459 template<typename _Rx_traits, typename _Ch_type,
2460 typename _St, typename _Sa>
2461 inline basic_string<_Ch_type>
2462 regex_replace(const _Ch_type* __s,
2463 const basic_regex<_Ch_type, _Rx_traits>& __e,
2464 const basic_string<_Ch_type, _St, _Sa>& __fmt,
2465 regex_constants::match_flag_type __flags
2466 = regex_constants::match_default)
2468 basic_string<_Ch_type> __result;
2469 regex_replace(std::back_inserter(__result), __s,
2470 __s + char_traits<_Ch_type>::length(__s),
2471 __e, __fmt, __flags);
2472 return __result;
2476 * @brief Search for a regular expression within a C-string for multiple
2477 times, and replace the matched parts through filling a format C-string.
2478 * @param __s [IN] The C-string to search and replace.
2479 * @param __e [IN] The regular expression to search for.
2480 * @param __fmt [IN] The format C-string.
2481 * @param __flags [IN] Search and replace policy flags.
2483 * @returns The string after replacing.
2484 * @throws an exception of type regex_error.
2486 template<typename _Rx_traits, typename _Ch_type>
2487 inline basic_string<_Ch_type>
2488 regex_replace(const _Ch_type* __s,
2489 const basic_regex<_Ch_type, _Rx_traits>& __e,
2490 const _Ch_type* __fmt,
2491 regex_constants::match_flag_type __flags
2492 = regex_constants::match_default)
2494 basic_string<_Ch_type> __result;
2495 regex_replace(std::back_inserter(__result), __s,
2496 __s + char_traits<_Ch_type>::length(__s),
2497 __e, __fmt, __flags);
2498 return __result;
2501 //@}
2503 _GLIBCXX_BEGIN_NAMESPACE_CXX11
2505 // std [28.12] Class template regex_iterator
2507 * An iterator adaptor that will provide repeated calls of regex_search over
2508 * a range until no more matches remain.
2510 template<typename _Bi_iter,
2511 typename _Ch_type = typename iterator_traits<_Bi_iter>::value_type,
2512 typename _Rx_traits = regex_traits<_Ch_type> >
2513 class regex_iterator
2515 public:
2516 typedef basic_regex<_Ch_type, _Rx_traits> regex_type;
2517 typedef match_results<_Bi_iter> value_type;
2518 typedef std::ptrdiff_t difference_type;
2519 typedef const value_type* pointer;
2520 typedef const value_type& reference;
2521 typedef std::forward_iterator_tag iterator_category;
2524 * @brief Provides a singular iterator, useful for indicating
2525 * one-past-the-end of a range.
2527 regex_iterator() = default;
2530 * Constructs a %regex_iterator...
2531 * @param __a [IN] The start of a text range to search.
2532 * @param __b [IN] One-past-the-end of the text range to search.
2533 * @param __re [IN] The regular expression to match.
2534 * @param __m [IN] Policy flags for match rules.
2536 regex_iterator(_Bi_iter __a, _Bi_iter __b, const regex_type& __re,
2537 regex_constants::match_flag_type __m
2538 = regex_constants::match_default)
2539 : _M_begin(__a), _M_end(__b), _M_pregex(&__re), _M_flags(__m), _M_match()
2541 if (!regex_search(_M_begin, _M_end, _M_match, *_M_pregex, _M_flags))
2542 *this = regex_iterator();
2545 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2546 // 2332. regex_iterator should forbid temporary regexes
2547 regex_iterator(_Bi_iter, _Bi_iter, const regex_type&&,
2548 regex_constants::match_flag_type
2549 = regex_constants::match_default) = delete;
2551 /// Copy constructs a %regex_iterator.
2552 regex_iterator(const regex_iterator&) = default;
2554 /// Copy assigns one %regex_iterator to another.
2555 regex_iterator&
2556 operator=(const regex_iterator&) = default;
2558 ~regex_iterator() = default;
2561 * @brief Tests the equivalence of two regex iterators.
2563 bool
2564 operator==(const regex_iterator&) const noexcept;
2567 * @brief Tests the inequivalence of two regex iterators.
2569 bool
2570 operator!=(const regex_iterator& __rhs) const noexcept
2571 { return !(*this == __rhs); }
2574 * @brief Dereferences a %regex_iterator.
2576 const value_type&
2577 operator*() const noexcept
2578 { return _M_match; }
2581 * @brief Selects a %regex_iterator member.
2583 const value_type*
2584 operator->() const noexcept
2585 { return &_M_match; }
2588 * @brief Increments a %regex_iterator.
2590 regex_iterator&
2591 operator++();
2594 * @brief Postincrements a %regex_iterator.
2596 regex_iterator
2597 operator++(int)
2599 auto __tmp = *this;
2600 ++(*this);
2601 return __tmp;
2604 private:
2605 _Bi_iter _M_begin {};
2606 _Bi_iter _M_end {};
2607 const regex_type* _M_pregex = nullptr;
2608 regex_constants::match_flag_type _M_flags {};
2609 match_results<_Bi_iter> _M_match;
2612 typedef regex_iterator<const char*> cregex_iterator;
2613 typedef regex_iterator<string::const_iterator> sregex_iterator;
2614 #ifdef _GLIBCXX_USE_WCHAR_T
2615 typedef regex_iterator<const wchar_t*> wcregex_iterator;
2616 typedef regex_iterator<wstring::const_iterator> wsregex_iterator;
2617 #endif
2619 // [7.12.2] Class template regex_token_iterator
2621 * Iterates over submatches in a range (or @a splits a text string).
2623 * The purpose of this iterator is to enumerate all, or all specified,
2624 * matches of a regular expression within a text range. The dereferenced
2625 * value of an iterator of this class is a std::sub_match object.
2627 template<typename _Bi_iter,
2628 typename _Ch_type = typename iterator_traits<_Bi_iter>::value_type,
2629 typename _Rx_traits = regex_traits<_Ch_type> >
2630 class regex_token_iterator
2632 public:
2633 typedef basic_regex<_Ch_type, _Rx_traits> regex_type;
2634 typedef sub_match<_Bi_iter> value_type;
2635 typedef std::ptrdiff_t difference_type;
2636 typedef const value_type* pointer;
2637 typedef const value_type& reference;
2638 typedef std::forward_iterator_tag iterator_category;
2640 public:
2642 * @brief Default constructs a %regex_token_iterator.
2644 * A default-constructed %regex_token_iterator is a singular iterator
2645 * that will compare equal to the one-past-the-end value for any
2646 * iterator of the same type.
2648 regex_token_iterator()
2649 : _M_position(), _M_subs(), _M_suffix(), _M_n(0), _M_result(nullptr),
2650 _M_has_m1(false)
2654 * Constructs a %regex_token_iterator...
2655 * @param __a [IN] The start of the text to search.
2656 * @param __b [IN] One-past-the-end of the text to search.
2657 * @param __re [IN] The regular expression to search for.
2658 * @param __submatch [IN] Which submatch to return. There are some
2659 * special values for this parameter:
2660 * - -1 each enumerated subexpression does NOT
2661 * match the regular expression (aka field
2662 * splitting)
2663 * - 0 the entire string matching the
2664 * subexpression is returned for each match
2665 * within the text.
2666 * - >0 enumerates only the indicated
2667 * subexpression from a match within the text.
2668 * @param __m [IN] Policy flags for match rules.
2670 regex_token_iterator(_Bi_iter __a, _Bi_iter __b, const regex_type& __re,
2671 int __submatch = 0,
2672 regex_constants::match_flag_type __m
2673 = regex_constants::match_default)
2674 : _M_position(__a, __b, __re, __m), _M_subs(1, __submatch), _M_n(0)
2675 { _M_init(__a, __b); }
2678 * Constructs a %regex_token_iterator...
2679 * @param __a [IN] The start of the text to search.
2680 * @param __b [IN] One-past-the-end of the text to search.
2681 * @param __re [IN] The regular expression to search for.
2682 * @param __submatches [IN] A list of subexpressions to return for each
2683 * regular expression match within the text.
2684 * @param __m [IN] Policy flags for match rules.
2686 regex_token_iterator(_Bi_iter __a, _Bi_iter __b,
2687 const regex_type& __re,
2688 const std::vector<int>& __submatches,
2689 regex_constants::match_flag_type __m
2690 = regex_constants::match_default)
2691 : _M_position(__a, __b, __re, __m), _M_subs(__submatches), _M_n(0)
2692 { _M_init(__a, __b); }
2695 * Constructs a %regex_token_iterator...
2696 * @param __a [IN] The start of the text to search.
2697 * @param __b [IN] One-past-the-end of the text to search.
2698 * @param __re [IN] The regular expression to search for.
2699 * @param __submatches [IN] A list of subexpressions to return for each
2700 * regular expression match within the text.
2701 * @param __m [IN] Policy flags for match rules.
2703 regex_token_iterator(_Bi_iter __a, _Bi_iter __b,
2704 const regex_type& __re,
2705 initializer_list<int> __submatches,
2706 regex_constants::match_flag_type __m
2707 = regex_constants::match_default)
2708 : _M_position(__a, __b, __re, __m), _M_subs(__submatches), _M_n(0)
2709 { _M_init(__a, __b); }
2712 * Constructs a %regex_token_iterator...
2713 * @param __a [IN] The start of the text to search.
2714 * @param __b [IN] One-past-the-end of the text to search.
2715 * @param __re [IN] The regular expression to search for.
2716 * @param __submatches [IN] A list of subexpressions to return for each
2717 * regular expression match within the text.
2718 * @param __m [IN] Policy flags for match rules.
2720 template<std::size_t _Nm>
2721 regex_token_iterator(_Bi_iter __a, _Bi_iter __b,
2722 const regex_type& __re,
2723 const int (&__submatches)[_Nm],
2724 regex_constants::match_flag_type __m
2725 = regex_constants::match_default)
2726 : _M_position(__a, __b, __re, __m),
2727 _M_subs(__submatches, __submatches + _Nm), _M_n(0)
2728 { _M_init(__a, __b); }
2730 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2731 // 2332. regex_token_iterator should forbid temporary regexes
2732 regex_token_iterator(_Bi_iter, _Bi_iter, const regex_type&&, int = 0,
2733 regex_constants::match_flag_type =
2734 regex_constants::match_default) = delete;
2735 regex_token_iterator(_Bi_iter, _Bi_iter, const regex_type&&,
2736 const std::vector<int>&,
2737 regex_constants::match_flag_type =
2738 regex_constants::match_default) = delete;
2739 regex_token_iterator(_Bi_iter, _Bi_iter, const regex_type&&,
2740 initializer_list<int>,
2741 regex_constants::match_flag_type =
2742 regex_constants::match_default) = delete;
2743 template <std::size_t _Nm>
2744 regex_token_iterator(_Bi_iter, _Bi_iter, const regex_type&&,
2745 const int (&)[_Nm],
2746 regex_constants::match_flag_type =
2747 regex_constants::match_default) = delete;
2750 * @brief Copy constructs a %regex_token_iterator.
2751 * @param __rhs [IN] A %regex_token_iterator to copy.
2753 regex_token_iterator(const regex_token_iterator& __rhs)
2754 : _M_position(__rhs._M_position), _M_subs(__rhs._M_subs),
2755 _M_suffix(__rhs._M_suffix), _M_n(__rhs._M_n), _M_has_m1(__rhs._M_has_m1)
2756 { _M_normalize_result(); }
2759 * @brief Assigns a %regex_token_iterator to another.
2760 * @param __rhs [IN] A %regex_token_iterator to copy.
2762 regex_token_iterator&
2763 operator=(const regex_token_iterator& __rhs);
2766 * @brief Compares a %regex_token_iterator to another for equality.
2768 bool
2769 operator==(const regex_token_iterator& __rhs) const;
2772 * @brief Compares a %regex_token_iterator to another for inequality.
2774 bool
2775 operator!=(const regex_token_iterator& __rhs) const
2776 { return !(*this == __rhs); }
2779 * @brief Dereferences a %regex_token_iterator.
2781 const value_type&
2782 operator*() const
2783 { return *_M_result; }
2786 * @brief Selects a %regex_token_iterator member.
2788 const value_type*
2789 operator->() const
2790 { return _M_result; }
2793 * @brief Increments a %regex_token_iterator.
2795 regex_token_iterator&
2796 operator++();
2799 * @brief Postincrements a %regex_token_iterator.
2801 regex_token_iterator
2802 operator++(int)
2804 auto __tmp = *this;
2805 ++(*this);
2806 return __tmp;
2809 private:
2810 typedef regex_iterator<_Bi_iter, _Ch_type, _Rx_traits> _Position;
2812 void
2813 _M_init(_Bi_iter __a, _Bi_iter __b);
2815 const value_type&
2816 _M_current_match() const
2818 if (_M_subs[_M_n] == -1)
2819 return (*_M_position).prefix();
2820 else
2821 return (*_M_position)[_M_subs[_M_n]];
2824 constexpr bool
2825 _M_end_of_seq() const
2826 { return _M_result == nullptr; }
2828 // [28.12.2.2.4]
2829 void
2830 _M_normalize_result()
2832 if (_M_position != _Position())
2833 _M_result = &_M_current_match();
2834 else if (_M_has_m1)
2835 _M_result = &_M_suffix;
2836 else
2837 _M_result = nullptr;
2840 _Position _M_position;
2841 std::vector<int> _M_subs;
2842 value_type _M_suffix;
2843 std::size_t _M_n;
2844 const value_type* _M_result;
2846 // Show whether _M_subs contains -1
2847 bool _M_has_m1;
2850 /** @brief Token iterator for C-style NULL-terminated strings. */
2851 typedef regex_token_iterator<const char*> cregex_token_iterator;
2853 /** @brief Token iterator for standard strings. */
2854 typedef regex_token_iterator<string::const_iterator> sregex_token_iterator;
2856 #ifdef _GLIBCXX_USE_WCHAR_T
2857 /** @brief Token iterator for C-style NULL-terminated wide strings. */
2858 typedef regex_token_iterator<const wchar_t*> wcregex_token_iterator;
2860 /** @brief Token iterator for standard wide-character strings. */
2861 typedef regex_token_iterator<wstring::const_iterator> wsregex_token_iterator;
2862 #endif
2864 //@} // group regex
2866 _GLIBCXX_END_NAMESPACE_CXX11
2867 _GLIBCXX_END_NAMESPACE_VERSION
2868 } // namespace
2870 #include <bits/regex.tcc>