* testsuite/26_numerics/headers/cmath/hypot.cc: XFAIL on AIX.
[official-gcc.git] / libstdc++-v3 / include / bits / regex.h
blob224d3dbb68f71440c57b9b9d0872cbb329718b88
1 // class template regex -*- C++ -*-
3 // Copyright (C) 2010-2016 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
42 _GLIBCXX_END_NAMESPACE_VERSION
44 namespace __detail
46 _GLIBCXX_BEGIN_NAMESPACE_VERSION
48 enum class _RegexExecutorPolicy : int
49 { _S_auto, _S_alternate };
51 template<typename _BiIter, typename _Alloc,
52 typename _CharT, typename _TraitsT,
53 _RegexExecutorPolicy __policy,
54 bool __match_mode>
55 bool
56 __regex_algo_impl(_BiIter __s,
57 _BiIter __e,
58 match_results<_BiIter, _Alloc>& __m,
59 const basic_regex<_CharT, _TraitsT>& __re,
60 regex_constants::match_flag_type __flags);
62 template<typename, typename, typename, bool>
63 class _Executor;
65 _GLIBCXX_END_NAMESPACE_VERSION
68 _GLIBCXX_BEGIN_NAMESPACE_VERSION
69 _GLIBCXX_BEGIN_NAMESPACE_CXX11
71 /**
72 * @addtogroup regex
73 * @{
76 /**
77 * @brief Describes aspects of a regular expression.
79 * A regular expression traits class that satisfies the requirements of
80 * section [28.7].
82 * The class %regex is parameterized around a set of related types and
83 * functions used to complete the definition of its semantics. This class
84 * satisfies the requirements of such a traits class.
86 template<typename _Ch_type>
87 struct regex_traits
89 public:
90 typedef _Ch_type char_type;
91 typedef std::basic_string<char_type> string_type;
92 typedef std::locale locale_type;
93 private:
94 struct _RegexMask
96 typedef std::ctype_base::mask _BaseType;
97 _BaseType _M_base;
98 unsigned char _M_extended;
99 static constexpr unsigned char _S_under = 1 << 0;
100 static constexpr unsigned char _S_valid_mask = 0x1;
102 constexpr _RegexMask(_BaseType __base = 0,
103 unsigned char __extended = 0)
104 : _M_base(__base), _M_extended(__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^(_RegexMask __other) const
124 return _RegexMask(_M_base ^ __other._M_base,
125 _M_extended ^ __other._M_extended);
128 constexpr _RegexMask
129 operator~() const
130 { return _RegexMask(~_M_base, ~_M_extended); }
132 _RegexMask&
133 operator&=(_RegexMask __other)
134 { return *this = (*this) & __other; }
136 _RegexMask&
137 operator|=(_RegexMask __other)
138 { return *this = (*this) | __other; }
140 _RegexMask&
141 operator^=(_RegexMask __other)
142 { return *this = (*this) ^ __other; }
144 constexpr bool
145 operator==(_RegexMask __other) const
147 return (_M_extended & _S_valid_mask)
148 == (__other._M_extended & _S_valid_mask)
149 && _M_base == __other._M_base;
152 constexpr bool
153 operator!=(_RegexMask __other) const
154 { return !((*this) == __other); }
157 public:
158 typedef _RegexMask char_class_type;
160 public:
162 * @brief Constructs a default traits object.
164 regex_traits() { }
167 * @brief Gives the length of a C-style string starting at @p __p.
169 * @param __p a pointer to the start of a character sequence.
171 * @returns the number of characters between @p *__p and the first
172 * default-initialized value of type @p char_type. In other words, uses
173 * the C-string algorithm for determining the length of a sequence of
174 * characters.
176 static std::size_t
177 length(const char_type* __p)
178 { return string_type::traits_type::length(__p); }
181 * @brief Performs the identity translation.
183 * @param __c A character to the locale-specific character set.
185 * @returns __c.
187 char_type
188 translate(char_type __c) const
189 { return __c; }
192 * @brief Translates a character into a case-insensitive equivalent.
194 * @param __c A character to the locale-specific character set.
196 * @returns the locale-specific lower-case equivalent of __c.
197 * @throws std::bad_cast if the imbued locale does not support the ctype
198 * facet.
200 char_type
201 translate_nocase(char_type __c) const
203 typedef std::ctype<char_type> __ctype_type;
204 const __ctype_type& __fctyp(use_facet<__ctype_type>(_M_locale));
205 return __fctyp.tolower(__c);
209 * @brief Gets a sort key for a character sequence.
211 * @param __first beginning of the character sequence.
212 * @param __last one-past-the-end of the character sequence.
214 * Returns a sort key for the character sequence designated by the
215 * iterator range [F1, F2) such that if the character sequence [G1, G2)
216 * sorts before the character sequence [H1, H2) then
217 * v.transform(G1, G2) < v.transform(H1, H2).
219 * What this really does is provide a more efficient way to compare a
220 * string to multiple other strings in locales with fancy collation
221 * rules and equivalence classes.
223 * @returns a locale-specific sort key equivalent to the input range.
225 * @throws std::bad_cast if the current locale does not have a collate
226 * facet.
228 template<typename _Fwd_iter>
229 string_type
230 transform(_Fwd_iter __first, _Fwd_iter __last) const
232 typedef std::collate<char_type> __collate_type;
233 const __collate_type& __fclt(use_facet<__collate_type>(_M_locale));
234 string_type __s(__first, __last);
235 return __fclt.transform(__s.data(), __s.data() + __s.size());
239 * @brief Gets a sort key for a character sequence, independent of case.
241 * @param __first beginning of the character sequence.
242 * @param __last one-past-the-end of the character sequence.
244 * Effects: if typeid(use_facet<collate<_Ch_type> >) ==
245 * typeid(collate_byname<_Ch_type>) and the form of the sort key
246 * returned by collate_byname<_Ch_type>::transform(__first, __last)
247 * is known and can be converted into a primary sort key
248 * then returns that key, otherwise returns an empty string.
250 * @todo Implement this function correctly.
252 template<typename _Fwd_iter>
253 string_type
254 transform_primary(_Fwd_iter __first, _Fwd_iter __last) const
256 // TODO : this is not entirely correct.
257 // This function requires extra support from the platform.
259 // Read http://gcc.gnu.org/ml/libstdc++/2013-09/msg00117.html and
260 // http://www.open-std.org/Jtc1/sc22/wg21/docs/papers/2003/n1429.htm
261 // for details.
262 typedef std::ctype<char_type> __ctype_type;
263 const __ctype_type& __fctyp(use_facet<__ctype_type>(_M_locale));
264 std::vector<char_type> __s(__first, __last);
265 __fctyp.tolower(__s.data(), __s.data() + __s.size());
266 return this->transform(__s.data(), __s.data() + __s.size());
270 * @brief Gets a collation element by name.
272 * @param __first beginning of the collation element name.
273 * @param __last one-past-the-end of the collation element name.
275 * @returns a sequence of one or more characters that represents the
276 * collating element consisting of the character sequence designated by
277 * the iterator range [__first, __last). Returns an empty string if the
278 * character sequence is not a valid collating element.
280 template<typename _Fwd_iter>
281 string_type
282 lookup_collatename(_Fwd_iter __first, _Fwd_iter __last) const;
285 * @brief Maps one or more characters to a named character
286 * classification.
288 * @param __first beginning of the character sequence.
289 * @param __last one-past-the-end of the character sequence.
290 * @param __icase ignores the case of the classification name.
292 * @returns an unspecified value that represents the character
293 * classification named by the character sequence designated by
294 * the iterator range [__first, __last). If @p icase is true,
295 * the returned mask identifies the classification regardless of
296 * the case of the characters to be matched (for example,
297 * [[:lower:]] is the same as [[:alpha:]]), otherwise a
298 * case-dependent classification is returned. The value
299 * returned shall be independent of the case of the characters
300 * in the character sequence. If the name is not recognized then
301 * returns a value that compares equal to 0.
303 * At least the following names (or their wide-character equivalent) are
304 * supported.
305 * - d
306 * - w
307 * - s
308 * - alnum
309 * - alpha
310 * - blank
311 * - cntrl
312 * - digit
313 * - graph
314 * - lower
315 * - print
316 * - punct
317 * - space
318 * - upper
319 * - xdigit
321 template<typename _Fwd_iter>
322 char_class_type
323 lookup_classname(_Fwd_iter __first, _Fwd_iter __last,
324 bool __icase = false) const;
327 * @brief Determines if @p c is a member of an identified class.
329 * @param __c a character.
330 * @param __f a class type (as returned from lookup_classname).
332 * @returns true if the character @p __c is a member of the classification
333 * represented by @p __f, false otherwise.
335 * @throws std::bad_cast if the current locale does not have a ctype
336 * facet.
338 bool
339 isctype(_Ch_type __c, char_class_type __f) const;
342 * @brief Converts a digit to an int.
344 * @param __ch a character representing a digit.
345 * @param __radix the radix if the numeric conversion (limited to 8, 10,
346 * or 16).
348 * @returns the value represented by the digit __ch in base radix if the
349 * character __ch is a valid digit in base radix; otherwise returns -1.
352 value(_Ch_type __ch, int __radix) const;
355 * @brief Imbues the regex_traits object with a copy of a new locale.
357 * @param __loc A locale.
359 * @returns a copy of the previous locale in use by the regex_traits
360 * object.
362 * @note Calling imbue with a different locale than the one currently in
363 * use invalidates all cached data held by *this.
365 locale_type
366 imbue(locale_type __loc)
368 std::swap(_M_locale, __loc);
369 return __loc;
373 * @brief Gets a copy of the current locale in use by the regex_traits
374 * object.
376 locale_type
377 getloc() const
378 { return _M_locale; }
380 protected:
381 locale_type _M_locale;
384 // [7.8] Class basic_regex
386 * Objects of specializations of this class represent regular expressions
387 * constructed from sequences of character type @p _Ch_type.
389 * Storage for the regular expression is allocated and deallocated as
390 * necessary by the member functions of this class.
392 template<typename _Ch_type, typename _Rx_traits = regex_traits<_Ch_type>>
393 class basic_regex
395 public:
396 static_assert(is_same<_Ch_type, typename _Rx_traits::char_type>::value,
397 "regex traits class must have the same char_type");
399 // types:
400 typedef _Ch_type value_type;
401 typedef _Rx_traits traits_type;
402 typedef typename traits_type::string_type string_type;
403 typedef regex_constants::syntax_option_type flag_type;
404 typedef typename traits_type::locale_type locale_type;
407 * @name Constants
408 * std [28.8.1](1)
410 //@{
411 static constexpr flag_type icase = regex_constants::icase;
412 static constexpr flag_type nosubs = regex_constants::nosubs;
413 static constexpr flag_type optimize = regex_constants::optimize;
414 static constexpr flag_type collate = regex_constants::collate;
415 static constexpr flag_type ECMAScript = regex_constants::ECMAScript;
416 static constexpr flag_type basic = regex_constants::basic;
417 static constexpr flag_type extended = regex_constants::extended;
418 static constexpr flag_type awk = regex_constants::awk;
419 static constexpr flag_type grep = regex_constants::grep;
420 static constexpr flag_type egrep = regex_constants::egrep;
421 //@}
423 // [7.8.2] construct/copy/destroy
425 * Constructs a basic regular expression that does not match any
426 * character sequence.
428 basic_regex()
429 : _M_flags(ECMAScript), _M_loc(), _M_automaton(nullptr)
433 * @brief Constructs a basic regular expression from the
434 * sequence [__p, __p + char_traits<_Ch_type>::length(__p))
435 * interpreted according to the flags in @p __f.
437 * @param __p A pointer to the start of a C-style null-terminated string
438 * containing a regular expression.
439 * @param __f Flags indicating the syntax rules and options.
441 * @throws regex_error if @p __p is not a valid regular expression.
443 explicit
444 basic_regex(const _Ch_type* __p, flag_type __f = ECMAScript)
445 : basic_regex(__p, __p + char_traits<_Ch_type>::length(__p), __f)
449 * @brief Constructs a basic regular expression from the sequence
450 * [p, p + len) interpreted according to the flags in @p f.
452 * @param __p A pointer to the start of a string containing a regular
453 * expression.
454 * @param __len The length of the string containing the regular
455 * expression.
456 * @param __f Flags indicating the syntax rules and options.
458 * @throws regex_error if @p __p is not a valid regular expression.
460 basic_regex(const _Ch_type* __p, std::size_t __len,
461 flag_type __f = ECMAScript)
462 : basic_regex(__p, __p + __len, __f)
466 * @brief Copy-constructs a basic regular expression.
468 * @param __rhs A @p regex object.
470 basic_regex(const basic_regex& __rhs) = default;
473 * @brief Move-constructs a basic regular expression.
475 * @param __rhs A @p regex object.
477 basic_regex(basic_regex&& __rhs) noexcept = default;
480 * @brief Constructs a basic regular expression from the string
481 * @p s interpreted according to the flags in @p f.
483 * @param __s A string containing a regular expression.
484 * @param __f Flags indicating the syntax rules and options.
486 * @throws regex_error if @p __s is not a valid regular expression.
488 template<typename _Ch_traits, typename _Ch_alloc>
489 explicit
490 basic_regex(const std::basic_string<_Ch_type, _Ch_traits,
491 _Ch_alloc>& __s,
492 flag_type __f = ECMAScript)
493 : basic_regex(__s.data(), __s.data() + __s.size(), __f)
497 * @brief Constructs a basic regular expression from the range
498 * [first, last) interpreted according to the flags in @p f.
500 * @param __first The start of a range containing a valid regular
501 * expression.
502 * @param __last The end of a range containing a valid regular
503 * expression.
504 * @param __f The format flags of the regular expression.
506 * @throws regex_error if @p [__first, __last) is not a valid regular
507 * expression.
509 template<typename _FwdIter>
510 basic_regex(_FwdIter __first, _FwdIter __last,
511 flag_type __f = ECMAScript)
512 : basic_regex(std::move(__first), std::move(__last), locale_type(), __f)
516 * @brief Constructs a basic regular expression from an initializer list.
518 * @param __l The initializer list.
519 * @param __f The format flags of the regular expression.
521 * @throws regex_error if @p __l is not a valid regular expression.
523 basic_regex(initializer_list<_Ch_type> __l, flag_type __f = ECMAScript)
524 : basic_regex(__l.begin(), __l.end(), __f)
528 * @brief Destroys a basic regular expression.
530 ~basic_regex()
534 * @brief Assigns one regular expression to another.
536 basic_regex&
537 operator=(const basic_regex& __rhs)
538 { return this->assign(__rhs); }
541 * @brief Move-assigns one regular expression to another.
543 basic_regex&
544 operator=(basic_regex&& __rhs) noexcept
545 { return this->assign(std::move(__rhs)); }
548 * @brief Replaces a regular expression with a new one constructed from
549 * a C-style null-terminated string.
551 * @param __p A pointer to the start of a null-terminated C-style string
552 * containing a regular expression.
554 basic_regex&
555 operator=(const _Ch_type* __p)
556 { return this->assign(__p); }
559 * @brief Replaces a regular expression with a new one constructed from
560 * an initializer list.
562 * @param __l The initializer list.
564 * @throws regex_error if @p __l is not a valid regular expression.
566 basic_regex&
567 operator=(initializer_list<_Ch_type> __l)
568 { return this->assign(__l.begin(), __l.end()); }
571 * @brief Replaces a regular expression with a new one constructed from
572 * a string.
574 * @param __s A pointer to a string containing a regular expression.
576 template<typename _Ch_traits, typename _Alloc>
577 basic_regex&
578 operator=(const basic_string<_Ch_type, _Ch_traits, _Alloc>& __s)
579 { return this->assign(__s); }
581 // [7.8.3] assign
583 * @brief the real assignment operator.
585 * @param __rhs Another regular expression object.
587 basic_regex&
588 assign(const basic_regex& __rhs)
590 basic_regex __tmp(__rhs);
591 this->swap(__tmp);
592 return *this;
596 * @brief The move-assignment operator.
598 * @param __rhs Another regular expression object.
600 basic_regex&
601 assign(basic_regex&& __rhs) noexcept
603 basic_regex __tmp(std::move(__rhs));
604 this->swap(__tmp);
605 return *this;
609 * @brief Assigns a new regular expression to a regex object from a
610 * C-style null-terminated string containing a regular expression
611 * pattern.
613 * @param __p A pointer to a C-style null-terminated string containing
614 * a regular expression pattern.
615 * @param __flags Syntax option flags.
617 * @throws regex_error if __p does not contain a valid regular
618 * expression pattern interpreted according to @p __flags. If
619 * regex_error is thrown, *this remains unchanged.
621 basic_regex&
622 assign(const _Ch_type* __p, flag_type __flags = ECMAScript)
623 { return this->assign(string_type(__p), __flags); }
626 * @brief Assigns a new regular expression to a regex object from a
627 * C-style string containing a regular expression pattern.
629 * @param __p A pointer to a C-style string containing a
630 * regular expression pattern.
631 * @param __len The length of the regular expression pattern string.
632 * @param __flags Syntax option flags.
634 * @throws regex_error if p does not contain a valid regular
635 * expression pattern interpreted according to @p __flags. If
636 * regex_error is thrown, *this remains unchanged.
638 basic_regex&
639 assign(const _Ch_type* __p, std::size_t __len, flag_type __flags)
640 { return this->assign(string_type(__p, __len), __flags); }
643 * @brief Assigns a new regular expression to a regex object from a
644 * string containing a regular expression pattern.
646 * @param __s A string containing a regular expression pattern.
647 * @param __flags Syntax option flags.
649 * @throws regex_error if __s does not contain a valid regular
650 * expression pattern interpreted according to @p __flags. If
651 * regex_error is thrown, *this remains unchanged.
653 template<typename _Ch_traits, typename _Alloc>
654 basic_regex&
655 assign(const basic_string<_Ch_type, _Ch_traits, _Alloc>& __s,
656 flag_type __flags = ECMAScript)
658 return this->assign(basic_regex(__s.data(), __s.data() + __s.size(),
659 _M_loc, __flags));
663 * @brief Assigns a new regular expression to a regex object.
665 * @param __first The start of a range containing a valid regular
666 * expression.
667 * @param __last The end of a range containing a valid regular
668 * expression.
669 * @param __flags Syntax option flags.
671 * @throws regex_error if p does not contain a valid regular
672 * expression pattern interpreted according to @p __flags. If
673 * regex_error is thrown, the object remains unchanged.
675 template<typename _InputIterator>
676 basic_regex&
677 assign(_InputIterator __first, _InputIterator __last,
678 flag_type __flags = ECMAScript)
679 { return this->assign(string_type(__first, __last), __flags); }
682 * @brief Assigns a new regular expression to a regex object.
684 * @param __l An initializer list representing a regular expression.
685 * @param __flags Syntax option flags.
687 * @throws regex_error if @p __l does not contain a valid
688 * regular expression pattern interpreted according to @p
689 * __flags. If regex_error is thrown, the object remains
690 * unchanged.
692 basic_regex&
693 assign(initializer_list<_Ch_type> __l, flag_type __flags = ECMAScript)
694 { return this->assign(__l.begin(), __l.end(), __flags); }
696 // [7.8.4] const operations
698 * @brief Gets the number of marked subexpressions within the regular
699 * expression.
701 unsigned int
702 mark_count() const
704 if (_M_automaton)
705 return _M_automaton->_M_sub_count() - 1;
706 return 0;
710 * @brief Gets the flags used to construct the regular expression
711 * or in the last call to assign().
713 flag_type
714 flags() const
715 { return _M_flags; }
717 // [7.8.5] locale
719 * @brief Imbues the regular expression object with the given locale.
721 * @param __loc A locale.
723 locale_type
724 imbue(locale_type __loc)
726 std::swap(__loc, _M_loc);
727 _M_automaton.reset();
728 return __loc;
732 * @brief Gets the locale currently imbued in the regular expression
733 * object.
735 locale_type
736 getloc() const
737 { return _M_loc; }
739 // [7.8.6] swap
741 * @brief Swaps the contents of two regular expression objects.
743 * @param __rhs Another regular expression object.
745 void
746 swap(basic_regex& __rhs)
748 std::swap(_M_flags, __rhs._M_flags);
749 std::swap(_M_loc, __rhs._M_loc);
750 std::swap(_M_automaton, __rhs._M_automaton);
753 #ifdef _GLIBCXX_DEBUG
754 void
755 _M_dot(std::ostream& __ostr)
756 { _M_automaton->_M_dot(__ostr); }
757 #endif
759 private:
760 typedef std::shared_ptr<const __detail::_NFA<_Rx_traits>> _AutomatonPtr;
762 template<typename _FwdIter>
763 basic_regex(_FwdIter __first, _FwdIter __last, locale_type __loc,
764 flag_type __f)
765 : _M_flags((__f & (ECMAScript | basic | extended | awk | grep | egrep))
766 ? __f : (__f | ECMAScript)),
767 _M_loc(std::move(__loc)),
768 _M_automaton(__detail::__compile_nfa<_FwdIter, _Rx_traits>(
769 std::move(__first), std::move(__last), _M_loc, _M_flags))
772 template<typename _Bp, typename _Ap, typename _Cp, typename _Rp,
773 __detail::_RegexExecutorPolicy, bool>
774 friend bool __detail::
775 #if _GLIBCXX_INLINE_VERSION
776 __7:: // Required due to PR c++/59256
777 #endif
778 __regex_algo_impl(_Bp, _Bp, match_results<_Bp, _Ap>&,
779 const basic_regex<_Cp, _Rp>&,
780 regex_constants::match_flag_type);
782 template<typename, typename, typename, bool>
783 friend class __detail::_Executor;
785 flag_type _M_flags;
786 locale_type _M_loc;
787 _AutomatonPtr _M_automaton;
790 /** @brief Standard regular expressions. */
791 typedef basic_regex<char> regex;
793 #ifdef _GLIBCXX_USE_WCHAR_T
794 /** @brief Standard wide-character regular expressions. */
795 typedef basic_regex<wchar_t> wregex;
796 #endif
799 // [7.8.6] basic_regex swap
801 * @brief Swaps the contents of two regular expression objects.
802 * @param __lhs First regular expression.
803 * @param __rhs Second regular expression.
805 template<typename _Ch_type, typename _Rx_traits>
806 inline void
807 swap(basic_regex<_Ch_type, _Rx_traits>& __lhs,
808 basic_regex<_Ch_type, _Rx_traits>& __rhs)
809 { __lhs.swap(__rhs); }
812 // [7.9] Class template sub_match
814 * A sequence of characters matched by a particular marked sub-expression.
816 * An object of this class is essentially a pair of iterators marking a
817 * matched subexpression within a regular expression pattern match. Such
818 * objects can be converted to and compared with std::basic_string objects
819 * of a similar base character type as the pattern matched by the regular
820 * expression.
822 * The iterators that make up the pair are the usual half-open interval
823 * referencing the actual original pattern matched.
825 template<typename _BiIter>
826 class sub_match : public std::pair<_BiIter, _BiIter>
828 typedef iterator_traits<_BiIter> __iter_traits;
830 public:
831 typedef typename __iter_traits::value_type value_type;
832 typedef typename __iter_traits::difference_type difference_type;
833 typedef _BiIter iterator;
834 typedef std::basic_string<value_type> string_type;
836 bool matched;
838 constexpr sub_match() : matched() { }
841 * Gets the length of the matching sequence.
843 difference_type
844 length() const
845 { return this->matched ? std::distance(this->first, this->second) : 0; }
848 * @brief Gets the matching sequence as a string.
850 * @returns the matching sequence as a string.
852 * This is the implicit conversion operator. It is identical to the
853 * str() member function except that it will want to pop up in
854 * unexpected places and cause a great deal of confusion and cursing
855 * from the unwary.
857 operator string_type() const
859 return this->matched
860 ? string_type(this->first, this->second)
861 : string_type();
865 * @brief Gets the matching sequence as a string.
867 * @returns the matching sequence as a string.
869 string_type
870 str() const
872 return this->matched
873 ? string_type(this->first, this->second)
874 : string_type();
878 * @brief Compares this and another matched sequence.
880 * @param __s Another matched sequence to compare to this one.
882 * @retval <0 this matched sequence will collate before @p __s.
883 * @retval =0 this matched sequence is equivalent to @p __s.
884 * @retval <0 this matched sequence will collate after @p __s.
887 compare(const sub_match& __s) const
888 { return this->str().compare(__s.str()); }
891 * @brief Compares this sub_match to a string.
893 * @param __s A string to compare to this sub_match.
895 * @retval <0 this matched sequence will collate before @p __s.
896 * @retval =0 this matched sequence is equivalent to @p __s.
897 * @retval <0 this matched sequence will collate after @p __s.
900 compare(const string_type& __s) const
901 { return this->str().compare(__s); }
904 * @brief Compares this sub_match to a C-style string.
906 * @param __s A C-style string to compare to this sub_match.
908 * @retval <0 this matched sequence will collate before @p __s.
909 * @retval =0 this matched sequence is equivalent to @p __s.
910 * @retval <0 this matched sequence will collate after @p __s.
913 compare(const value_type* __s) const
914 { return this->str().compare(__s); }
918 /** @brief Standard regex submatch over a C-style null-terminated string. */
919 typedef sub_match<const char*> csub_match;
921 /** @brief Standard regex submatch over a standard string. */
922 typedef sub_match<string::const_iterator> ssub_match;
924 #ifdef _GLIBCXX_USE_WCHAR_T
925 /** @brief Regex submatch over a C-style null-terminated wide string. */
926 typedef sub_match<const wchar_t*> wcsub_match;
928 /** @brief Regex submatch over a standard wide string. */
929 typedef sub_match<wstring::const_iterator> wssub_match;
930 #endif
932 // [7.9.2] sub_match non-member operators
935 * @brief Tests the equivalence of two regular expression submatches.
936 * @param __lhs First regular expression submatch.
937 * @param __rhs Second regular expression submatch.
938 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
940 template<typename _BiIter>
941 inline bool
942 operator==(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
943 { return __lhs.compare(__rhs) == 0; }
946 * @brief Tests the inequivalence of two regular expression submatches.
947 * @param __lhs First regular expression submatch.
948 * @param __rhs Second regular expression submatch.
949 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
951 template<typename _BiIter>
952 inline bool
953 operator!=(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
954 { return __lhs.compare(__rhs) != 0; }
957 * @brief Tests the ordering of two regular expression submatches.
958 * @param __lhs First regular expression submatch.
959 * @param __rhs Second regular expression submatch.
960 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
962 template<typename _BiIter>
963 inline bool
964 operator<(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
965 { return __lhs.compare(__rhs) < 0; }
968 * @brief Tests the ordering of two regular expression submatches.
969 * @param __lhs First regular expression submatch.
970 * @param __rhs Second regular expression submatch.
971 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
973 template<typename _BiIter>
974 inline bool
975 operator<=(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
976 { return __lhs.compare(__rhs) <= 0; }
979 * @brief Tests the ordering of two regular expression submatches.
980 * @param __lhs First regular expression submatch.
981 * @param __rhs Second regular expression submatch.
982 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
984 template<typename _BiIter>
985 inline bool
986 operator>=(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
987 { return __lhs.compare(__rhs) >= 0; }
990 * @brief Tests the ordering of two regular expression submatches.
991 * @param __lhs First regular expression submatch.
992 * @param __rhs Second regular expression submatch.
993 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
995 template<typename _BiIter>
996 inline bool
997 operator>(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
998 { return __lhs.compare(__rhs) > 0; }
1000 // Alias for sub_match'd string.
1001 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1002 using __sub_match_string = basic_string<
1003 typename iterator_traits<_Bi_iter>::value_type,
1004 _Ch_traits, _Ch_alloc>;
1007 * @brief Tests the equivalence of a string and a regular expression
1008 * submatch.
1009 * @param __lhs A string.
1010 * @param __rhs A regular expression submatch.
1011 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1013 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1014 inline bool
1015 operator==(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1016 const sub_match<_Bi_iter>& __rhs)
1018 typedef typename sub_match<_Bi_iter>::string_type string_type;
1019 return __rhs.compare(string_type(__lhs.data(), __lhs.size())) == 0;
1023 * @brief Tests the inequivalence of a string and a regular expression
1024 * submatch.
1025 * @param __lhs A string.
1026 * @param __rhs A regular expression submatch.
1027 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1029 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1030 inline bool
1031 operator!=(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1032 const sub_match<_Bi_iter>& __rhs)
1033 { return !(__lhs == __rhs); }
1036 * @brief Tests the ordering of a string and a regular expression submatch.
1037 * @param __lhs A string.
1038 * @param __rhs A regular expression submatch.
1039 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1041 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1042 inline bool
1043 operator<(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1044 const sub_match<_Bi_iter>& __rhs)
1046 typedef typename sub_match<_Bi_iter>::string_type string_type;
1047 return __rhs.compare(string_type(__lhs.data(), __lhs.size())) > 0;
1051 * @brief Tests the ordering of a string and a regular expression submatch.
1052 * @param __lhs A string.
1053 * @param __rhs A regular expression submatch.
1054 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1056 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1057 inline bool
1058 operator>(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1059 const sub_match<_Bi_iter>& __rhs)
1060 { return __rhs < __lhs; }
1063 * @brief Tests the ordering of a string and a regular expression submatch.
1064 * @param __lhs A string.
1065 * @param __rhs A regular expression submatch.
1066 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1068 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1069 inline bool
1070 operator>=(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1071 const sub_match<_Bi_iter>& __rhs)
1072 { return !(__lhs < __rhs); }
1075 * @brief Tests the ordering of a string and a regular expression submatch.
1076 * @param __lhs A string.
1077 * @param __rhs A regular expression submatch.
1078 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1080 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1081 inline bool
1082 operator<=(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1083 const sub_match<_Bi_iter>& __rhs)
1084 { return !(__rhs < __lhs); }
1087 * @brief Tests the equivalence of a regular expression submatch and a
1088 * string.
1089 * @param __lhs A regular expression submatch.
1090 * @param __rhs A string.
1091 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1093 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1094 inline bool
1095 operator==(const sub_match<_Bi_iter>& __lhs,
1096 const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1098 typedef typename sub_match<_Bi_iter>::string_type string_type;
1099 return __lhs.compare(string_type(__rhs.data(), __rhs.size())) == 0;
1103 * @brief Tests the inequivalence of a regular expression submatch and a
1104 * string.
1105 * @param __lhs A regular expression submatch.
1106 * @param __rhs A string.
1107 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1109 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1110 inline bool
1111 operator!=(const sub_match<_Bi_iter>& __lhs,
1112 const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1113 { return !(__lhs == __rhs); }
1116 * @brief Tests the ordering of a regular expression submatch and a string.
1117 * @param __lhs A regular expression submatch.
1118 * @param __rhs A string.
1119 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1121 template<typename _Bi_iter, class _Ch_traits, class _Ch_alloc>
1122 inline bool
1123 operator<(const sub_match<_Bi_iter>& __lhs,
1124 const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1126 typedef typename sub_match<_Bi_iter>::string_type string_type;
1127 return __lhs.compare(string_type(__rhs.data(), __rhs.size())) < 0;
1131 * @brief Tests the ordering of a regular expression submatch and a string.
1132 * @param __lhs A regular expression submatch.
1133 * @param __rhs A string.
1134 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1136 template<typename _Bi_iter, class _Ch_traits, class _Ch_alloc>
1137 inline bool
1138 operator>(const sub_match<_Bi_iter>& __lhs,
1139 const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1140 { return __rhs < __lhs; }
1143 * @brief Tests the ordering of a regular expression submatch and a string.
1144 * @param __lhs A regular expression submatch.
1145 * @param __rhs A string.
1146 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1148 template<typename _Bi_iter, class _Ch_traits, class _Ch_alloc>
1149 inline bool
1150 operator>=(const sub_match<_Bi_iter>& __lhs,
1151 const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1152 { return !(__lhs < __rhs); }
1155 * @brief Tests the ordering of a regular expression submatch and a string.
1156 * @param __lhs A regular expression submatch.
1157 * @param __rhs A string.
1158 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1160 template<typename _Bi_iter, class _Ch_traits, class _Ch_alloc>
1161 inline bool
1162 operator<=(const sub_match<_Bi_iter>& __lhs,
1163 const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1164 { return !(__rhs < __lhs); }
1167 * @brief Tests the equivalence of a C string and a regular expression
1168 * submatch.
1169 * @param __lhs A C string.
1170 * @param __rhs A regular expression submatch.
1171 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1173 template<typename _Bi_iter>
1174 inline bool
1175 operator==(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1176 const sub_match<_Bi_iter>& __rhs)
1177 { return __rhs.compare(__lhs) == 0; }
1180 * @brief Tests the inequivalence of an iterator value and a regular
1181 * expression submatch.
1182 * @param __lhs A regular expression submatch.
1183 * @param __rhs A string.
1184 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1186 template<typename _Bi_iter>
1187 inline bool
1188 operator!=(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1189 const sub_match<_Bi_iter>& __rhs)
1190 { return !(__lhs == __rhs); }
1193 * @brief Tests the ordering of a string and a regular expression submatch.
1194 * @param __lhs A string.
1195 * @param __rhs A regular expression submatch.
1196 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1198 template<typename _Bi_iter>
1199 inline bool
1200 operator<(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1201 const sub_match<_Bi_iter>& __rhs)
1202 { return __rhs.compare(__lhs) > 0; }
1205 * @brief Tests the ordering of a string and a regular expression submatch.
1206 * @param __lhs A string.
1207 * @param __rhs A regular expression submatch.
1208 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1210 template<typename _Bi_iter>
1211 inline bool
1212 operator>(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1213 const sub_match<_Bi_iter>& __rhs)
1214 { return __rhs < __lhs; }
1217 * @brief Tests the ordering of a string and a regular expression submatch.
1218 * @param __lhs A string.
1219 * @param __rhs A regular expression submatch.
1220 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1222 template<typename _Bi_iter>
1223 inline bool
1224 operator>=(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1225 const sub_match<_Bi_iter>& __rhs)
1226 { return !(__lhs < __rhs); }
1229 * @brief Tests the ordering of a string and a regular expression submatch.
1230 * @param __lhs A string.
1231 * @param __rhs A regular expression submatch.
1232 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1234 template<typename _Bi_iter>
1235 inline bool
1236 operator<=(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1237 const sub_match<_Bi_iter>& __rhs)
1238 { return !(__rhs < __lhs); }
1241 * @brief Tests the equivalence of a regular expression submatch and a
1242 * string.
1243 * @param __lhs A regular expression submatch.
1244 * @param __rhs A pointer to a string?
1245 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1247 template<typename _Bi_iter>
1248 inline bool
1249 operator==(const sub_match<_Bi_iter>& __lhs,
1250 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1251 { return __lhs.compare(__rhs) == 0; }
1254 * @brief Tests the inequivalence of a regular expression submatch and a
1255 * string.
1256 * @param __lhs A regular expression submatch.
1257 * @param __rhs A pointer to a string.
1258 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1260 template<typename _Bi_iter>
1261 inline bool
1262 operator!=(const sub_match<_Bi_iter>& __lhs,
1263 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1264 { return !(__lhs == __rhs); }
1267 * @brief Tests the ordering of a regular expression submatch and a string.
1268 * @param __lhs A regular expression submatch.
1269 * @param __rhs A string.
1270 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1272 template<typename _Bi_iter>
1273 inline bool
1274 operator<(const sub_match<_Bi_iter>& __lhs,
1275 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1276 { return __lhs.compare(__rhs) < 0; }
1279 * @brief Tests the ordering of a regular expression submatch and a string.
1280 * @param __lhs A regular expression submatch.
1281 * @param __rhs A string.
1282 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1284 template<typename _Bi_iter>
1285 inline bool
1286 operator>(const sub_match<_Bi_iter>& __lhs,
1287 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1288 { return __rhs < __lhs; }
1291 * @brief Tests the ordering of a regular expression submatch and a string.
1292 * @param __lhs A regular expression submatch.
1293 * @param __rhs A string.
1294 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1296 template<typename _Bi_iter>
1297 inline bool
1298 operator>=(const sub_match<_Bi_iter>& __lhs,
1299 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1300 { return !(__lhs < __rhs); }
1303 * @brief Tests the ordering of a regular expression submatch and a string.
1304 * @param __lhs A regular expression submatch.
1305 * @param __rhs A string.
1306 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1308 template<typename _Bi_iter>
1309 inline bool
1310 operator<=(const sub_match<_Bi_iter>& __lhs,
1311 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1312 { return !(__rhs < __lhs); }
1315 * @brief Tests the equivalence of a string and a regular expression
1316 * submatch.
1317 * @param __lhs A string.
1318 * @param __rhs A regular expression submatch.
1319 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1321 template<typename _Bi_iter>
1322 inline bool
1323 operator==(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1324 const sub_match<_Bi_iter>& __rhs)
1326 typedef typename sub_match<_Bi_iter>::string_type string_type;
1327 return __rhs.compare(string_type(1, __lhs)) == 0;
1331 * @brief Tests the inequivalence of a string and a regular expression
1332 * submatch.
1333 * @param __lhs A string.
1334 * @param __rhs A regular expression submatch.
1335 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1337 template<typename _Bi_iter>
1338 inline bool
1339 operator!=(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1340 const sub_match<_Bi_iter>& __rhs)
1341 { return !(__lhs == __rhs); }
1344 * @brief Tests the ordering of a string and a regular expression submatch.
1345 * @param __lhs A string.
1346 * @param __rhs A regular expression submatch.
1347 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1349 template<typename _Bi_iter>
1350 inline bool
1351 operator<(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1352 const sub_match<_Bi_iter>& __rhs)
1354 typedef typename sub_match<_Bi_iter>::string_type string_type;
1355 return __rhs.compare(string_type(1, __lhs)) > 0;
1359 * @brief Tests the ordering of a string and a regular expression submatch.
1360 * @param __lhs A string.
1361 * @param __rhs A regular expression submatch.
1362 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1364 template<typename _Bi_iter>
1365 inline bool
1366 operator>(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1367 const sub_match<_Bi_iter>& __rhs)
1368 { return __rhs < __lhs; }
1371 * @brief Tests the ordering of a string and a regular expression submatch.
1372 * @param __lhs A string.
1373 * @param __rhs A regular expression submatch.
1374 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1376 template<typename _Bi_iter>
1377 inline bool
1378 operator>=(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1379 const sub_match<_Bi_iter>& __rhs)
1380 { return !(__lhs < __rhs); }
1383 * @brief Tests the ordering of a string and a regular expression submatch.
1384 * @param __lhs A string.
1385 * @param __rhs A regular expression submatch.
1386 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1388 template<typename _Bi_iter>
1389 inline bool
1390 operator<=(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1391 const sub_match<_Bi_iter>& __rhs)
1392 { return !(__rhs < __lhs); }
1395 * @brief Tests the equivalence of a regular expression submatch and a
1396 * string.
1397 * @param __lhs A regular expression submatch.
1398 * @param __rhs A const string reference.
1399 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1401 template<typename _Bi_iter>
1402 inline bool
1403 operator==(const sub_match<_Bi_iter>& __lhs,
1404 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1406 typedef typename sub_match<_Bi_iter>::string_type string_type;
1407 return __lhs.compare(string_type(1, __rhs)) == 0;
1411 * @brief Tests the inequivalence of a regular expression submatch and a
1412 * string.
1413 * @param __lhs A regular expression submatch.
1414 * @param __rhs A const string reference.
1415 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1417 template<typename _Bi_iter>
1418 inline bool
1419 operator!=(const sub_match<_Bi_iter>& __lhs,
1420 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1421 { return !(__lhs == __rhs); }
1424 * @brief Tests the ordering of a regular expression submatch and a string.
1425 * @param __lhs A regular expression submatch.
1426 * @param __rhs A const string reference.
1427 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1429 template<typename _Bi_iter>
1430 inline bool
1431 operator<(const sub_match<_Bi_iter>& __lhs,
1432 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1434 typedef typename sub_match<_Bi_iter>::string_type string_type;
1435 return __lhs.compare(string_type(1, __rhs)) < 0;
1439 * @brief Tests the ordering of a regular expression submatch and a string.
1440 * @param __lhs A regular expression submatch.
1441 * @param __rhs A const string reference.
1442 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1444 template<typename _Bi_iter>
1445 inline bool
1446 operator>(const sub_match<_Bi_iter>& __lhs,
1447 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1448 { return __rhs < __lhs; }
1451 * @brief Tests the ordering of a regular expression submatch and a string.
1452 * @param __lhs A regular expression submatch.
1453 * @param __rhs A const string reference.
1454 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1456 template<typename _Bi_iter>
1457 inline bool
1458 operator>=(const sub_match<_Bi_iter>& __lhs,
1459 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1460 { return !(__lhs < __rhs); }
1463 * @brief Tests the ordering of a regular expression submatch and a string.
1464 * @param __lhs A regular expression submatch.
1465 * @param __rhs A const string reference.
1466 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1468 template<typename _Bi_iter>
1469 inline bool
1470 operator<=(const sub_match<_Bi_iter>& __lhs,
1471 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1472 { return !(__rhs < __lhs); }
1475 * @brief Inserts a matched string into an output stream.
1477 * @param __os The output stream.
1478 * @param __m A submatch string.
1480 * @returns the output stream with the submatch string inserted.
1482 template<typename _Ch_type, typename _Ch_traits, typename _Bi_iter>
1483 inline
1484 basic_ostream<_Ch_type, _Ch_traits>&
1485 operator<<(basic_ostream<_Ch_type, _Ch_traits>& __os,
1486 const sub_match<_Bi_iter>& __m)
1487 { return __os << __m.str(); }
1489 // [7.10] Class template match_results
1492 * @brief The results of a match or search operation.
1494 * A collection of character sequences representing the result of a regular
1495 * expression match. Storage for the collection is allocated and freed as
1496 * necessary by the member functions of class template match_results.
1498 * This class satisfies the Sequence requirements, with the exception that
1499 * only the operations defined for a const-qualified Sequence are supported.
1501 * The sub_match object stored at index 0 represents sub-expression 0, i.e.
1502 * the whole match. In this case the %sub_match member matched is always true.
1503 * The sub_match object stored at index n denotes what matched the marked
1504 * sub-expression n within the matched expression. If the sub-expression n
1505 * participated in a regular expression match then the %sub_match member
1506 * matched evaluates to true, and members first and second denote the range
1507 * of characters [first, second) which formed that match. Otherwise matched
1508 * is false, and members first and second point to the end of the sequence
1509 * that was searched.
1511 * @nosubgrouping
1513 template<typename _Bi_iter,
1514 typename _Alloc = allocator<sub_match<_Bi_iter> > >
1515 class match_results
1516 : private std::vector<sub_match<_Bi_iter>, _Alloc>
1518 private:
1520 * The vector base is empty if this does not represent a match (!ready());
1521 * Otherwise if it's a match failure, it contains 3 elements:
1522 * [0] unmatched
1523 * [1] prefix
1524 * [2] suffix
1525 * Otherwise it contains n+4 elements where n is the number of marked
1526 * sub-expressions:
1527 * [0] entire match
1528 * [1] 1st marked subexpression
1529 * ...
1530 * [n] nth marked subexpression
1531 * [n+1] unmatched
1532 * [n+2] prefix
1533 * [n+3] suffix
1535 typedef std::vector<sub_match<_Bi_iter>, _Alloc> _Base_type;
1536 typedef std::iterator_traits<_Bi_iter> __iter_traits;
1537 typedef regex_constants::match_flag_type match_flag_type;
1539 public:
1541 * @name 10.? Public Types
1543 //@{
1544 typedef sub_match<_Bi_iter> value_type;
1545 typedef const value_type& const_reference;
1546 typedef const_reference reference;
1547 typedef typename _Base_type::const_iterator const_iterator;
1548 typedef const_iterator iterator;
1549 typedef typename __iter_traits::difference_type difference_type;
1550 typedef typename allocator_traits<_Alloc>::size_type size_type;
1551 typedef _Alloc allocator_type;
1552 typedef typename __iter_traits::value_type char_type;
1553 typedef std::basic_string<char_type> string_type;
1554 //@}
1556 public:
1558 * @name 28.10.1 Construction, Copying, and Destruction
1560 //@{
1563 * @brief Constructs a default %match_results container.
1564 * @post size() returns 0 and str() returns an empty string.
1566 explicit
1567 match_results(const _Alloc& __a = _Alloc())
1568 : _Base_type(__a)
1572 * @brief Copy constructs a %match_results.
1574 match_results(const match_results& __rhs) = default;
1577 * @brief Move constructs a %match_results.
1579 match_results(match_results&& __rhs) noexcept = default;
1582 * @brief Assigns rhs to *this.
1584 match_results&
1585 operator=(const match_results& __rhs) = default;
1588 * @brief Move-assigns rhs to *this.
1590 match_results&
1591 operator=(match_results&& __rhs) = default;
1594 * @brief Destroys a %match_results object.
1596 ~match_results()
1599 //@}
1601 // 28.10.2, state:
1603 * @brief Indicates if the %match_results is ready.
1604 * @retval true The object has a fully-established result state.
1605 * @retval false The object is not ready.
1607 bool ready() const { return !_Base_type::empty(); }
1610 * @name 28.10.2 Size
1612 //@{
1615 * @brief Gets the number of matches and submatches.
1617 * The number of matches for a given regular expression will be either 0
1618 * if there was no match or mark_count() + 1 if a match was successful.
1619 * Some matches may be empty.
1621 * @returns the number of matches found.
1623 size_type
1624 size() const
1625 { return _Base_type::empty() ? 0 : _Base_type::size() - 3; }
1627 size_type
1628 max_size() const
1629 { return _Base_type::max_size(); }
1632 * @brief Indicates if the %match_results contains no results.
1633 * @retval true The %match_results object is empty.
1634 * @retval false The %match_results object is not empty.
1636 bool
1637 empty() const
1638 { return size() == 0; }
1640 //@}
1643 * @name 10.3 Element Access
1645 //@{
1648 * @brief Gets the length of the indicated submatch.
1649 * @param __sub indicates the submatch.
1650 * @pre ready() == true
1652 * This function returns the length of the indicated submatch, or the
1653 * length of the entire match if @p __sub is zero (the default).
1655 difference_type
1656 length(size_type __sub = 0) const
1657 { return (*this)[__sub].length(); }
1660 * @brief Gets the offset of the beginning of the indicated submatch.
1661 * @param __sub indicates the submatch.
1662 * @pre ready() == true
1664 * This function returns the offset from the beginning of the target
1665 * sequence to the beginning of the submatch, unless the value of @p __sub
1666 * is zero (the default), in which case this function returns the offset
1667 * from the beginning of the target sequence to the beginning of the
1668 * match.
1670 difference_type
1671 position(size_type __sub = 0) const
1672 { return std::distance(_M_begin, (*this)[__sub].first); }
1675 * @brief Gets the match or submatch converted to a string type.
1676 * @param __sub indicates the submatch.
1677 * @pre ready() == true
1679 * This function gets the submatch (or match, if @p __sub is
1680 * zero) extracted from the target range and converted to the
1681 * associated string type.
1683 string_type
1684 str(size_type __sub = 0) const
1685 { return string_type((*this)[__sub]); }
1688 * @brief Gets a %sub_match reference for the match or submatch.
1689 * @param __sub indicates the submatch.
1690 * @pre ready() == true
1692 * This function gets a reference to the indicated submatch, or
1693 * the entire match if @p __sub is zero.
1695 * If @p __sub >= size() then this function returns a %sub_match with a
1696 * special value indicating no submatch.
1698 const_reference
1699 operator[](size_type __sub) const
1701 __glibcxx_assert( ready() );
1702 return __sub < size()
1703 ? _Base_type::operator[](__sub)
1704 : _M_unmatched_sub();
1708 * @brief Gets a %sub_match representing the match prefix.
1709 * @pre ready() == true
1711 * This function gets a reference to a %sub_match object representing the
1712 * part of the target range between the start of the target range and the
1713 * start of the match.
1715 const_reference
1716 prefix() const
1718 __glibcxx_assert( ready() );
1719 return !empty() ? _M_prefix() : _M_unmatched_sub();
1723 * @brief Gets a %sub_match representing the match suffix.
1724 * @pre ready() == true
1726 * This function gets a reference to a %sub_match object representing the
1727 * part of the target range between the end of the match and the end of
1728 * the target range.
1730 const_reference
1731 suffix() const
1733 __glibcxx_assert( ready() );
1734 return !empty() ? _M_suffix() : _M_unmatched_sub();
1738 * @brief Gets an iterator to the start of the %sub_match collection.
1740 const_iterator
1741 begin() const
1742 { return _Base_type::begin(); }
1745 * @brief Gets an iterator to the start of the %sub_match collection.
1747 const_iterator
1748 cbegin() const
1749 { return this->begin(); }
1752 * @brief Gets an iterator to one-past-the-end of the collection.
1754 const_iterator
1755 end() const
1756 { return _Base_type::end() - 3; }
1759 * @brief Gets an iterator to one-past-the-end of the collection.
1761 const_iterator
1762 cend() const
1763 { return this->end(); }
1765 //@}
1768 * @name 10.4 Formatting
1770 * These functions perform formatted substitution of the matched
1771 * character sequences into their target. The format specifiers and
1772 * escape sequences accepted by these functions are determined by
1773 * their @p flags parameter as documented above.
1775 //@{
1778 * @pre ready() == true
1780 template<typename _Out_iter>
1781 _Out_iter
1782 format(_Out_iter __out, const char_type* __fmt_first,
1783 const char_type* __fmt_last,
1784 match_flag_type __flags = regex_constants::format_default) const;
1787 * @pre ready() == true
1789 template<typename _Out_iter, typename _St, typename _Sa>
1790 _Out_iter
1791 format(_Out_iter __out, const basic_string<char_type, _St, _Sa>& __fmt,
1792 match_flag_type __flags = regex_constants::format_default) const
1794 return format(__out, __fmt.data(), __fmt.data() + __fmt.size(),
1795 __flags);
1799 * @pre ready() == true
1801 template<typename _St, typename _Sa>
1802 basic_string<char_type, _St, _Sa>
1803 format(const basic_string<char_type, _St, _Sa>& __fmt,
1804 match_flag_type __flags = regex_constants::format_default) const
1806 basic_string<char_type, _St, _Sa> __result;
1807 format(std::back_inserter(__result), __fmt, __flags);
1808 return __result;
1812 * @pre ready() == true
1814 string_type
1815 format(const char_type* __fmt,
1816 match_flag_type __flags = regex_constants::format_default) const
1818 string_type __result;
1819 format(std::back_inserter(__result),
1820 __fmt,
1821 __fmt + char_traits<char_type>::length(__fmt),
1822 __flags);
1823 return __result;
1826 //@}
1829 * @name 10.5 Allocator
1831 //@{
1834 * @brief Gets a copy of the allocator.
1836 allocator_type
1837 get_allocator() const
1838 { return _Base_type::get_allocator(); }
1840 //@}
1843 * @name 10.6 Swap
1845 //@{
1848 * @brief Swaps the contents of two match_results.
1850 void
1851 swap(match_results& __that)
1853 using std::swap;
1854 _Base_type::swap(__that);
1855 swap(_M_begin, __that._M_begin);
1857 //@}
1859 private:
1860 template<typename, typename, typename, bool>
1861 friend class __detail::_Executor;
1863 template<typename, typename, typename>
1864 friend class regex_iterator;
1866 template<typename _Bp, typename _Ap, typename _Cp, typename _Rp,
1867 __detail::_RegexExecutorPolicy, bool>
1868 friend bool __detail::
1869 #if _GLIBCXX_INLINE_VERSION
1870 __7:: // Required due to PR c++/59256
1871 #endif
1872 __regex_algo_impl(_Bp, _Bp, match_results<_Bp, _Ap>&,
1873 const basic_regex<_Cp, _Rp>&,
1874 regex_constants::match_flag_type);
1876 void
1877 _M_resize(unsigned int __size)
1878 { _Base_type::resize(__size + 3); }
1880 const_reference
1881 _M_unmatched_sub() const
1882 { return _Base_type::operator[](_Base_type::size() - 3); }
1884 sub_match<_Bi_iter>&
1885 _M_unmatched_sub()
1886 { return _Base_type::operator[](_Base_type::size() - 3); }
1888 const_reference
1889 _M_prefix() const
1890 { return _Base_type::operator[](_Base_type::size() - 2); }
1892 sub_match<_Bi_iter>&
1893 _M_prefix()
1894 { return _Base_type::operator[](_Base_type::size() - 2); }
1896 const_reference
1897 _M_suffix() const
1898 { return _Base_type::operator[](_Base_type::size() - 1); }
1900 sub_match<_Bi_iter>&
1901 _M_suffix()
1902 { return _Base_type::operator[](_Base_type::size() - 1); }
1904 _Bi_iter _M_begin;
1907 typedef match_results<const char*> cmatch;
1908 typedef match_results<string::const_iterator> smatch;
1909 #ifdef _GLIBCXX_USE_WCHAR_T
1910 typedef match_results<const wchar_t*> wcmatch;
1911 typedef match_results<wstring::const_iterator> wsmatch;
1912 #endif
1914 // match_results comparisons
1916 * @brief Compares two match_results for equality.
1917 * @returns true if the two objects refer to the same match,
1918 * false otherwise.
1920 template<typename _Bi_iter, typename _Alloc>
1921 inline bool
1922 operator==(const match_results<_Bi_iter, _Alloc>& __m1,
1923 const match_results<_Bi_iter, _Alloc>& __m2)
1925 if (__m1.ready() != __m2.ready())
1926 return false;
1927 if (!__m1.ready()) // both are not ready
1928 return true;
1929 if (__m1.empty() != __m2.empty())
1930 return false;
1931 if (__m1.empty()) // both are empty
1932 return true;
1933 return __m1.prefix() == __m2.prefix()
1934 && __m1.size() == __m2.size()
1935 && std::equal(__m1.begin(), __m1.end(), __m2.begin())
1936 && __m1.suffix() == __m2.suffix();
1940 * @brief Compares two match_results for inequality.
1941 * @returns true if the two objects do not refer to the same match,
1942 * false otherwise.
1944 template<typename _Bi_iter, class _Alloc>
1945 inline bool
1946 operator!=(const match_results<_Bi_iter, _Alloc>& __m1,
1947 const match_results<_Bi_iter, _Alloc>& __m2)
1948 { return !(__m1 == __m2); }
1950 // [7.10.6] match_results swap
1952 * @brief Swaps two match results.
1953 * @param __lhs A match result.
1954 * @param __rhs A match result.
1956 * The contents of the two match_results objects are swapped.
1958 template<typename _Bi_iter, typename _Alloc>
1959 inline void
1960 swap(match_results<_Bi_iter, _Alloc>& __lhs,
1961 match_results<_Bi_iter, _Alloc>& __rhs)
1962 { __lhs.swap(__rhs); }
1964 _GLIBCXX_END_NAMESPACE_CXX11
1966 // [7.11.2] Function template regex_match
1968 * @name Matching, Searching, and Replacing
1970 //@{
1973 * @brief Determines if there is a match between the regular expression @p e
1974 * and all of the character sequence [first, last).
1976 * @param __s Start of the character sequence to match.
1977 * @param __e One-past-the-end of the character sequence to match.
1978 * @param __m The match results.
1979 * @param __re The regular expression.
1980 * @param __flags Controls how the regular expression is matched.
1982 * @retval true A match exists.
1983 * @retval false Otherwise.
1985 * @throws an exception of type regex_error.
1987 template<typename _Bi_iter, typename _Alloc,
1988 typename _Ch_type, typename _Rx_traits>
1989 inline bool
1990 regex_match(_Bi_iter __s,
1991 _Bi_iter __e,
1992 match_results<_Bi_iter, _Alloc>& __m,
1993 const basic_regex<_Ch_type, _Rx_traits>& __re,
1994 regex_constants::match_flag_type __flags
1995 = regex_constants::match_default)
1997 return __detail::__regex_algo_impl<_Bi_iter, _Alloc, _Ch_type, _Rx_traits,
1998 __detail::_RegexExecutorPolicy::_S_auto, true>
1999 (__s, __e, __m, __re, __flags);
2003 * @brief Indicates if there is a match between the regular expression @p e
2004 * and all of the character sequence [first, last).
2006 * @param __first Beginning of the character sequence to match.
2007 * @param __last One-past-the-end of the character sequence to match.
2008 * @param __re The regular expression.
2009 * @param __flags Controls how the regular expression is matched.
2011 * @retval true A match exists.
2012 * @retval false Otherwise.
2014 * @throws an exception of type regex_error.
2016 template<typename _Bi_iter, typename _Ch_type, typename _Rx_traits>
2017 inline bool
2018 regex_match(_Bi_iter __first, _Bi_iter __last,
2019 const basic_regex<_Ch_type, _Rx_traits>& __re,
2020 regex_constants::match_flag_type __flags
2021 = regex_constants::match_default)
2023 match_results<_Bi_iter> __what;
2024 return regex_match(__first, __last, __what, __re, __flags);
2028 * @brief Determines if there is a match between the regular expression @p e
2029 * and a C-style null-terminated string.
2031 * @param __s The C-style null-terminated string to match.
2032 * @param __m The match results.
2033 * @param __re The regular expression.
2034 * @param __f Controls how the regular expression is matched.
2036 * @retval true A match exists.
2037 * @retval false Otherwise.
2039 * @throws an exception of type regex_error.
2041 template<typename _Ch_type, typename _Alloc, typename _Rx_traits>
2042 inline bool
2043 regex_match(const _Ch_type* __s,
2044 match_results<const _Ch_type*, _Alloc>& __m,
2045 const basic_regex<_Ch_type, _Rx_traits>& __re,
2046 regex_constants::match_flag_type __f
2047 = regex_constants::match_default)
2048 { return regex_match(__s, __s + _Rx_traits::length(__s), __m, __re, __f); }
2051 * @brief Determines if there is a match between the regular expression @p e
2052 * and a string.
2054 * @param __s The string to match.
2055 * @param __m The match results.
2056 * @param __re The regular expression.
2057 * @param __flags Controls how the regular expression is matched.
2059 * @retval true A match exists.
2060 * @retval false Otherwise.
2062 * @throws an exception of type regex_error.
2064 template<typename _Ch_traits, typename _Ch_alloc,
2065 typename _Alloc, typename _Ch_type, typename _Rx_traits>
2066 inline bool
2067 regex_match(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>& __s,
2068 match_results<typename basic_string<_Ch_type,
2069 _Ch_traits, _Ch_alloc>::const_iterator, _Alloc>& __m,
2070 const basic_regex<_Ch_type, _Rx_traits>& __re,
2071 regex_constants::match_flag_type __flags
2072 = regex_constants::match_default)
2073 { return regex_match(__s.begin(), __s.end(), __m, __re, __flags); }
2075 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2076 // 2329. regex_match() with match_results should forbid temporary strings
2077 /// Prevent unsafe attempts to get match_results from a temporary string.
2078 template<typename _Ch_traits, typename _Ch_alloc,
2079 typename _Alloc, typename _Ch_type, typename _Rx_traits>
2080 bool
2081 regex_match(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>&&,
2082 match_results<typename basic_string<_Ch_type,
2083 _Ch_traits, _Ch_alloc>::const_iterator, _Alloc>&,
2084 const basic_regex<_Ch_type, _Rx_traits>&,
2085 regex_constants::match_flag_type
2086 = regex_constants::match_default) = delete;
2089 * @brief Indicates if there is a match between the regular expression @p e
2090 * and a C-style null-terminated string.
2092 * @param __s The C-style null-terminated string to match.
2093 * @param __re The regular expression.
2094 * @param __f Controls how the regular expression is matched.
2096 * @retval true A match exists.
2097 * @retval false Otherwise.
2099 * @throws an exception of type regex_error.
2101 template<typename _Ch_type, class _Rx_traits>
2102 inline bool
2103 regex_match(const _Ch_type* __s,
2104 const basic_regex<_Ch_type, _Rx_traits>& __re,
2105 regex_constants::match_flag_type __f
2106 = regex_constants::match_default)
2107 { return regex_match(__s, __s + _Rx_traits::length(__s), __re, __f); }
2110 * @brief Indicates if there is a match between the regular expression @p e
2111 * and a string.
2113 * @param __s [IN] The string to match.
2114 * @param __re [IN] The regular expression.
2115 * @param __flags [IN] Controls how the regular expression is matched.
2117 * @retval true A match exists.
2118 * @retval false Otherwise.
2120 * @throws an exception of type regex_error.
2122 template<typename _Ch_traits, typename _Str_allocator,
2123 typename _Ch_type, typename _Rx_traits>
2124 inline bool
2125 regex_match(const basic_string<_Ch_type, _Ch_traits, _Str_allocator>& __s,
2126 const basic_regex<_Ch_type, _Rx_traits>& __re,
2127 regex_constants::match_flag_type __flags
2128 = regex_constants::match_default)
2129 { return regex_match(__s.begin(), __s.end(), __re, __flags); }
2131 // [7.11.3] Function template regex_search
2133 * Searches for a regular expression within a range.
2134 * @param __s [IN] The start of the string to search.
2135 * @param __e [IN] One-past-the-end of the string to search.
2136 * @param __m [OUT] The match results.
2137 * @param __re [IN] The regular expression to search for.
2138 * @param __flags [IN] Search policy flags.
2139 * @retval true A match was found within the string.
2140 * @retval false No match was found within the string, the content of %m is
2141 * undefined.
2143 * @throws an exception of type regex_error.
2145 template<typename _Bi_iter, typename _Alloc,
2146 typename _Ch_type, typename _Rx_traits>
2147 inline bool
2148 regex_search(_Bi_iter __s, _Bi_iter __e,
2149 match_results<_Bi_iter, _Alloc>& __m,
2150 const basic_regex<_Ch_type, _Rx_traits>& __re,
2151 regex_constants::match_flag_type __flags
2152 = regex_constants::match_default)
2154 return __detail::__regex_algo_impl<_Bi_iter, _Alloc, _Ch_type, _Rx_traits,
2155 __detail::_RegexExecutorPolicy::_S_auto, false>
2156 (__s, __e, __m, __re, __flags);
2160 * Searches for a regular expression within a range.
2161 * @param __first [IN] The start of the string to search.
2162 * @param __last [IN] One-past-the-end of the string to search.
2163 * @param __re [IN] The regular expression to search for.
2164 * @param __flags [IN] Search policy flags.
2165 * @retval true A match was found within the string.
2166 * @retval false No match was found within the string.
2168 * @throws an exception of type regex_error.
2170 template<typename _Bi_iter, typename _Ch_type, typename _Rx_traits>
2171 inline bool
2172 regex_search(_Bi_iter __first, _Bi_iter __last,
2173 const basic_regex<_Ch_type, _Rx_traits>& __re,
2174 regex_constants::match_flag_type __flags
2175 = regex_constants::match_default)
2177 match_results<_Bi_iter> __what;
2178 return regex_search(__first, __last, __what, __re, __flags);
2182 * @brief Searches for a regular expression within a C-string.
2183 * @param __s [IN] A C-string to search for the regex.
2184 * @param __m [OUT] The set of regex matches.
2185 * @param __e [IN] The regex to search for in @p s.
2186 * @param __f [IN] The search flags.
2187 * @retval true A match was found within the string.
2188 * @retval false No match was found within the string, the content of %m is
2189 * undefined.
2191 * @throws an exception of type regex_error.
2193 template<typename _Ch_type, class _Alloc, class _Rx_traits>
2194 inline bool
2195 regex_search(const _Ch_type* __s,
2196 match_results<const _Ch_type*, _Alloc>& __m,
2197 const basic_regex<_Ch_type, _Rx_traits>& __e,
2198 regex_constants::match_flag_type __f
2199 = regex_constants::match_default)
2200 { return regex_search(__s, __s + _Rx_traits::length(__s), __m, __e, __f); }
2203 * @brief Searches for a regular expression within a C-string.
2204 * @param __s [IN] The C-string to search.
2205 * @param __e [IN] The regular expression to search for.
2206 * @param __f [IN] Search policy flags.
2207 * @retval true A match was found within the string.
2208 * @retval false No match was found within the string.
2210 * @throws an exception of type regex_error.
2212 template<typename _Ch_type, typename _Rx_traits>
2213 inline bool
2214 regex_search(const _Ch_type* __s,
2215 const basic_regex<_Ch_type, _Rx_traits>& __e,
2216 regex_constants::match_flag_type __f
2217 = regex_constants::match_default)
2218 { return regex_search(__s, __s + _Rx_traits::length(__s), __e, __f); }
2221 * @brief Searches for a regular expression within a string.
2222 * @param __s [IN] The string to search.
2223 * @param __e [IN] The regular expression to search for.
2224 * @param __flags [IN] Search policy flags.
2225 * @retval true A match was found within the string.
2226 * @retval false No match was found within the string.
2228 * @throws an exception of type regex_error.
2230 template<typename _Ch_traits, typename _String_allocator,
2231 typename _Ch_type, typename _Rx_traits>
2232 inline bool
2233 regex_search(const basic_string<_Ch_type, _Ch_traits,
2234 _String_allocator>& __s,
2235 const basic_regex<_Ch_type, _Rx_traits>& __e,
2236 regex_constants::match_flag_type __flags
2237 = regex_constants::match_default)
2238 { return regex_search(__s.begin(), __s.end(), __e, __flags); }
2241 * @brief Searches for a regular expression within a string.
2242 * @param __s [IN] A C++ string to search for the regex.
2243 * @param __m [OUT] The set of regex matches.
2244 * @param __e [IN] The regex to search for in @p s.
2245 * @param __f [IN] The search flags.
2246 * @retval true A match was found within the string.
2247 * @retval false No match was found within the string, the content of %m is
2248 * undefined.
2250 * @throws an exception of type regex_error.
2252 template<typename _Ch_traits, typename _Ch_alloc,
2253 typename _Alloc, typename _Ch_type,
2254 typename _Rx_traits>
2255 inline bool
2256 regex_search(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>& __s,
2257 match_results<typename basic_string<_Ch_type,
2258 _Ch_traits, _Ch_alloc>::const_iterator, _Alloc>& __m,
2259 const basic_regex<_Ch_type, _Rx_traits>& __e,
2260 regex_constants::match_flag_type __f
2261 = regex_constants::match_default)
2262 { return regex_search(__s.begin(), __s.end(), __m, __e, __f); }
2264 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2265 // 2329. regex_search() with match_results should forbid temporary strings
2266 /// Prevent unsafe attempts to get match_results from a temporary string.
2267 template<typename _Ch_traits, typename _Ch_alloc,
2268 typename _Alloc, typename _Ch_type,
2269 typename _Rx_traits>
2270 bool
2271 regex_search(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>&&,
2272 match_results<typename basic_string<_Ch_type,
2273 _Ch_traits, _Ch_alloc>::const_iterator, _Alloc>&,
2274 const basic_regex<_Ch_type, _Rx_traits>&,
2275 regex_constants::match_flag_type
2276 = regex_constants::match_default) = delete;
2278 // std [28.11.4] Function template regex_replace
2280 * @brief Search for a regular expression within a range for multiple times,
2281 and replace the matched parts through filling a format string.
2282 * @param __out [OUT] The output iterator.
2283 * @param __first [IN] The start of the string to search.
2284 * @param __last [IN] One-past-the-end of the string to search.
2285 * @param __e [IN] The regular expression to search for.
2286 * @param __fmt [IN] The format string.
2287 * @param __flags [IN] Search and replace policy flags.
2289 * @returns __out
2290 * @throws an exception of type regex_error.
2292 template<typename _Out_iter, typename _Bi_iter,
2293 typename _Rx_traits, typename _Ch_type,
2294 typename _St, typename _Sa>
2295 inline _Out_iter
2296 regex_replace(_Out_iter __out, _Bi_iter __first, _Bi_iter __last,
2297 const basic_regex<_Ch_type, _Rx_traits>& __e,
2298 const basic_string<_Ch_type, _St, _Sa>& __fmt,
2299 regex_constants::match_flag_type __flags
2300 = regex_constants::match_default)
2302 return regex_replace(__out, __first, __last, __e, __fmt.c_str(), __flags);
2306 * @brief Search for a regular expression within a range for multiple times,
2307 and replace the matched parts through filling a format C-string.
2308 * @param __out [OUT] The output iterator.
2309 * @param __first [IN] The start of the string to search.
2310 * @param __last [IN] One-past-the-end of the string to search.
2311 * @param __e [IN] The regular expression to search for.
2312 * @param __fmt [IN] The format C-string.
2313 * @param __flags [IN] Search and replace policy flags.
2315 * @returns __out
2316 * @throws an exception of type regex_error.
2318 template<typename _Out_iter, typename _Bi_iter,
2319 typename _Rx_traits, typename _Ch_type>
2320 _Out_iter
2321 regex_replace(_Out_iter __out, _Bi_iter __first, _Bi_iter __last,
2322 const basic_regex<_Ch_type, _Rx_traits>& __e,
2323 const _Ch_type* __fmt,
2324 regex_constants::match_flag_type __flags
2325 = regex_constants::match_default);
2328 * @brief Search for a regular expression within a string for multiple times,
2329 and replace the matched parts through filling a format string.
2330 * @param __s [IN] The string to search and replace.
2331 * @param __e [IN] The regular expression to search for.
2332 * @param __fmt [IN] The format string.
2333 * @param __flags [IN] Search and replace policy flags.
2335 * @returns The string after replacing.
2336 * @throws an exception of type regex_error.
2338 template<typename _Rx_traits, typename _Ch_type,
2339 typename _St, typename _Sa, typename _Fst, typename _Fsa>
2340 inline basic_string<_Ch_type, _St, _Sa>
2341 regex_replace(const basic_string<_Ch_type, _St, _Sa>& __s,
2342 const basic_regex<_Ch_type, _Rx_traits>& __e,
2343 const basic_string<_Ch_type, _Fst, _Fsa>& __fmt,
2344 regex_constants::match_flag_type __flags
2345 = regex_constants::match_default)
2347 basic_string<_Ch_type, _St, _Sa> __result;
2348 regex_replace(std::back_inserter(__result),
2349 __s.begin(), __s.end(), __e, __fmt, __flags);
2350 return __result;
2354 * @brief Search for a regular expression within a string for multiple times,
2355 and replace the matched parts through filling a format C-string.
2356 * @param __s [IN] The string to search and replace.
2357 * @param __e [IN] The regular expression to search for.
2358 * @param __fmt [IN] The format C-string.
2359 * @param __flags [IN] Search and replace policy flags.
2361 * @returns The string after replacing.
2362 * @throws an exception of type regex_error.
2364 template<typename _Rx_traits, typename _Ch_type,
2365 typename _St, typename _Sa>
2366 inline basic_string<_Ch_type, _St, _Sa>
2367 regex_replace(const basic_string<_Ch_type, _St, _Sa>& __s,
2368 const basic_regex<_Ch_type, _Rx_traits>& __e,
2369 const _Ch_type* __fmt,
2370 regex_constants::match_flag_type __flags
2371 = regex_constants::match_default)
2373 basic_string<_Ch_type, _St, _Sa> __result;
2374 regex_replace(std::back_inserter(__result),
2375 __s.begin(), __s.end(), __e, __fmt, __flags);
2376 return __result;
2380 * @brief Search for a regular expression within a C-string for multiple
2381 times, and replace the matched parts through filling a format string.
2382 * @param __s [IN] The C-string to search and replace.
2383 * @param __e [IN] The regular expression to search for.
2384 * @param __fmt [IN] The format string.
2385 * @param __flags [IN] Search and replace policy flags.
2387 * @returns The string after replacing.
2388 * @throws an exception of type regex_error.
2390 template<typename _Rx_traits, typename _Ch_type,
2391 typename _St, typename _Sa>
2392 inline basic_string<_Ch_type>
2393 regex_replace(const _Ch_type* __s,
2394 const basic_regex<_Ch_type, _Rx_traits>& __e,
2395 const basic_string<_Ch_type, _St, _Sa>& __fmt,
2396 regex_constants::match_flag_type __flags
2397 = regex_constants::match_default)
2399 basic_string<_Ch_type> __result;
2400 regex_replace(std::back_inserter(__result), __s,
2401 __s + char_traits<_Ch_type>::length(__s),
2402 __e, __fmt, __flags);
2403 return __result;
2407 * @brief Search for a regular expression within a C-string for multiple
2408 times, and replace the matched parts through filling a format C-string.
2409 * @param __s [IN] The C-string to search and replace.
2410 * @param __e [IN] The regular expression to search for.
2411 * @param __fmt [IN] The format C-string.
2412 * @param __flags [IN] Search and replace policy flags.
2414 * @returns The string after replacing.
2415 * @throws an exception of type regex_error.
2417 template<typename _Rx_traits, typename _Ch_type>
2418 inline basic_string<_Ch_type>
2419 regex_replace(const _Ch_type* __s,
2420 const basic_regex<_Ch_type, _Rx_traits>& __e,
2421 const _Ch_type* __fmt,
2422 regex_constants::match_flag_type __flags
2423 = regex_constants::match_default)
2425 basic_string<_Ch_type> __result;
2426 regex_replace(std::back_inserter(__result), __s,
2427 __s + char_traits<_Ch_type>::length(__s),
2428 __e, __fmt, __flags);
2429 return __result;
2432 //@}
2434 _GLIBCXX_BEGIN_NAMESPACE_CXX11
2436 // std [28.12] Class template regex_iterator
2438 * An iterator adaptor that will provide repeated calls of regex_search over
2439 * a range until no more matches remain.
2441 template<typename _Bi_iter,
2442 typename _Ch_type = typename iterator_traits<_Bi_iter>::value_type,
2443 typename _Rx_traits = regex_traits<_Ch_type> >
2444 class regex_iterator
2446 public:
2447 typedef basic_regex<_Ch_type, _Rx_traits> regex_type;
2448 typedef match_results<_Bi_iter> value_type;
2449 typedef std::ptrdiff_t difference_type;
2450 typedef const value_type* pointer;
2451 typedef const value_type& reference;
2452 typedef std::forward_iterator_tag iterator_category;
2455 * @brief Provides a singular iterator, useful for indicating
2456 * one-past-the-end of a range.
2458 regex_iterator()
2459 : _M_pregex()
2463 * Constructs a %regex_iterator...
2464 * @param __a [IN] The start of a text range to search.
2465 * @param __b [IN] One-past-the-end of the text range to search.
2466 * @param __re [IN] The regular expression to match.
2467 * @param __m [IN] Policy flags for match rules.
2469 regex_iterator(_Bi_iter __a, _Bi_iter __b, const regex_type& __re,
2470 regex_constants::match_flag_type __m
2471 = regex_constants::match_default)
2472 : _M_begin(__a), _M_end(__b), _M_pregex(&__re), _M_flags(__m), _M_match()
2474 if (!regex_search(_M_begin, _M_end, _M_match, *_M_pregex, _M_flags))
2475 *this = regex_iterator();
2478 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2479 // 2332. regex_iterator should forbid temporary regexes
2480 regex_iterator(_Bi_iter, _Bi_iter, const regex_type&&,
2481 regex_constants::match_flag_type
2482 = regex_constants::match_default) = delete;
2484 * Copy constructs a %regex_iterator.
2486 regex_iterator(const regex_iterator& __rhs) = default;
2489 * @brief Assigns one %regex_iterator to another.
2491 regex_iterator&
2492 operator=(const regex_iterator& __rhs) = default;
2495 * @brief Tests the equivalence of two regex iterators.
2497 bool
2498 operator==(const regex_iterator& __rhs) const;
2501 * @brief Tests the inequivalence of two regex iterators.
2503 bool
2504 operator!=(const regex_iterator& __rhs) const
2505 { return !(*this == __rhs); }
2508 * @brief Dereferences a %regex_iterator.
2510 const value_type&
2511 operator*() const
2512 { return _M_match; }
2515 * @brief Selects a %regex_iterator member.
2517 const value_type*
2518 operator->() const
2519 { return &_M_match; }
2522 * @brief Increments a %regex_iterator.
2524 regex_iterator&
2525 operator++();
2528 * @brief Postincrements a %regex_iterator.
2530 regex_iterator
2531 operator++(int)
2533 auto __tmp = *this;
2534 ++(*this);
2535 return __tmp;
2538 private:
2539 _Bi_iter _M_begin;
2540 _Bi_iter _M_end;
2541 const regex_type* _M_pregex;
2542 regex_constants::match_flag_type _M_flags;
2543 match_results<_Bi_iter> _M_match;
2546 typedef regex_iterator<const char*> cregex_iterator;
2547 typedef regex_iterator<string::const_iterator> sregex_iterator;
2548 #ifdef _GLIBCXX_USE_WCHAR_T
2549 typedef regex_iterator<const wchar_t*> wcregex_iterator;
2550 typedef regex_iterator<wstring::const_iterator> wsregex_iterator;
2551 #endif
2553 // [7.12.2] Class template regex_token_iterator
2555 * Iterates over submatches in a range (or @a splits a text string).
2557 * The purpose of this iterator is to enumerate all, or all specified,
2558 * matches of a regular expression within a text range. The dereferenced
2559 * value of an iterator of this class is a std::sub_match object.
2561 template<typename _Bi_iter,
2562 typename _Ch_type = typename iterator_traits<_Bi_iter>::value_type,
2563 typename _Rx_traits = regex_traits<_Ch_type> >
2564 class regex_token_iterator
2566 public:
2567 typedef basic_regex<_Ch_type, _Rx_traits> regex_type;
2568 typedef sub_match<_Bi_iter> value_type;
2569 typedef std::ptrdiff_t difference_type;
2570 typedef const value_type* pointer;
2571 typedef const value_type& reference;
2572 typedef std::forward_iterator_tag iterator_category;
2574 public:
2576 * @brief Default constructs a %regex_token_iterator.
2578 * A default-constructed %regex_token_iterator is a singular iterator
2579 * that will compare equal to the one-past-the-end value for any
2580 * iterator of the same type.
2582 regex_token_iterator()
2583 : _M_position(), _M_subs(), _M_suffix(), _M_n(0), _M_result(nullptr),
2584 _M_has_m1(false)
2588 * Constructs a %regex_token_iterator...
2589 * @param __a [IN] The start of the text to search.
2590 * @param __b [IN] One-past-the-end of the text to search.
2591 * @param __re [IN] The regular expression to search for.
2592 * @param __submatch [IN] Which submatch to return. There are some
2593 * special values for this parameter:
2594 * - -1 each enumerated subexpression does NOT
2595 * match the regular expression (aka field
2596 * splitting)
2597 * - 0 the entire string matching the
2598 * subexpression is returned for each match
2599 * within the text.
2600 * - >0 enumerates only the indicated
2601 * subexpression from a match within the text.
2602 * @param __m [IN] Policy flags for match rules.
2604 regex_token_iterator(_Bi_iter __a, _Bi_iter __b, const regex_type& __re,
2605 int __submatch = 0,
2606 regex_constants::match_flag_type __m
2607 = regex_constants::match_default)
2608 : _M_position(__a, __b, __re, __m), _M_subs(1, __submatch), _M_n(0)
2609 { _M_init(__a, __b); }
2612 * Constructs a %regex_token_iterator...
2613 * @param __a [IN] The start of the text to search.
2614 * @param __b [IN] One-past-the-end of the text to search.
2615 * @param __re [IN] The regular expression to search for.
2616 * @param __submatches [IN] A list of subexpressions to return for each
2617 * regular expression match within the text.
2618 * @param __m [IN] Policy flags for match rules.
2620 regex_token_iterator(_Bi_iter __a, _Bi_iter __b,
2621 const regex_type& __re,
2622 const std::vector<int>& __submatches,
2623 regex_constants::match_flag_type __m
2624 = regex_constants::match_default)
2625 : _M_position(__a, __b, __re, __m), _M_subs(__submatches), _M_n(0)
2626 { _M_init(__a, __b); }
2629 * Constructs a %regex_token_iterator...
2630 * @param __a [IN] The start of the text to search.
2631 * @param __b [IN] One-past-the-end of the text to search.
2632 * @param __re [IN] The regular expression to search for.
2633 * @param __submatches [IN] A list of subexpressions to return for each
2634 * regular expression match within the text.
2635 * @param __m [IN] Policy flags for match rules.
2637 regex_token_iterator(_Bi_iter __a, _Bi_iter __b,
2638 const regex_type& __re,
2639 initializer_list<int> __submatches,
2640 regex_constants::match_flag_type __m
2641 = regex_constants::match_default)
2642 : _M_position(__a, __b, __re, __m), _M_subs(__submatches), _M_n(0)
2643 { _M_init(__a, __b); }
2646 * Constructs a %regex_token_iterator...
2647 * @param __a [IN] The start of the text to search.
2648 * @param __b [IN] One-past-the-end of the text to search.
2649 * @param __re [IN] The regular expression to search for.
2650 * @param __submatches [IN] A list of subexpressions to return for each
2651 * regular expression match within the text.
2652 * @param __m [IN] Policy flags for match rules.
2654 template<std::size_t _Nm>
2655 regex_token_iterator(_Bi_iter __a, _Bi_iter __b,
2656 const regex_type& __re,
2657 const int (&__submatches)[_Nm],
2658 regex_constants::match_flag_type __m
2659 = regex_constants::match_default)
2660 : _M_position(__a, __b, __re, __m),
2661 _M_subs(__submatches, __submatches + _Nm), _M_n(0)
2662 { _M_init(__a, __b); }
2664 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2665 // 2332. regex_token_iterator should forbid temporary regexes
2666 regex_token_iterator(_Bi_iter, _Bi_iter, const regex_type&&, int = 0,
2667 regex_constants::match_flag_type =
2668 regex_constants::match_default) = delete;
2669 regex_token_iterator(_Bi_iter, _Bi_iter, const regex_type&&,
2670 const std::vector<int>&,
2671 regex_constants::match_flag_type =
2672 regex_constants::match_default) = delete;
2673 regex_token_iterator(_Bi_iter, _Bi_iter, const regex_type&&,
2674 initializer_list<int>,
2675 regex_constants::match_flag_type =
2676 regex_constants::match_default) = delete;
2677 template <std::size_t N>
2678 regex_token_iterator(_Bi_iter, _Bi_iter, const regex_type&&,
2679 const int (&)[N],
2680 regex_constants::match_flag_type =
2681 regex_constants::match_default) = delete;
2684 * @brief Copy constructs a %regex_token_iterator.
2685 * @param __rhs [IN] A %regex_token_iterator to copy.
2687 regex_token_iterator(const regex_token_iterator& __rhs)
2688 : _M_position(__rhs._M_position), _M_subs(__rhs._M_subs),
2689 _M_suffix(__rhs._M_suffix), _M_n(__rhs._M_n), _M_has_m1(__rhs._M_has_m1)
2690 { _M_normalize_result(); }
2693 * @brief Assigns a %regex_token_iterator to another.
2694 * @param __rhs [IN] A %regex_token_iterator to copy.
2696 regex_token_iterator&
2697 operator=(const regex_token_iterator& __rhs);
2700 * @brief Compares a %regex_token_iterator to another for equality.
2702 bool
2703 operator==(const regex_token_iterator& __rhs) const;
2706 * @brief Compares a %regex_token_iterator to another for inequality.
2708 bool
2709 operator!=(const regex_token_iterator& __rhs) const
2710 { return !(*this == __rhs); }
2713 * @brief Dereferences a %regex_token_iterator.
2715 const value_type&
2716 operator*() const
2717 { return *_M_result; }
2720 * @brief Selects a %regex_token_iterator member.
2722 const value_type*
2723 operator->() const
2724 { return _M_result; }
2727 * @brief Increments a %regex_token_iterator.
2729 regex_token_iterator&
2730 operator++();
2733 * @brief Postincrements a %regex_token_iterator.
2735 regex_token_iterator
2736 operator++(int)
2738 auto __tmp = *this;
2739 ++(*this);
2740 return __tmp;
2743 private:
2744 typedef regex_iterator<_Bi_iter, _Ch_type, _Rx_traits> _Position;
2746 void
2747 _M_init(_Bi_iter __a, _Bi_iter __b);
2749 const value_type&
2750 _M_current_match() const
2752 if (_M_subs[_M_n] == -1)
2753 return (*_M_position).prefix();
2754 else
2755 return (*_M_position)[_M_subs[_M_n]];
2758 constexpr bool
2759 _M_end_of_seq() const
2760 { return _M_result == nullptr; }
2762 // [28.12.2.2.4]
2763 void
2764 _M_normalize_result()
2766 if (_M_position != _Position())
2767 _M_result = &_M_current_match();
2768 else if (_M_has_m1)
2769 _M_result = &_M_suffix;
2770 else
2771 _M_result = nullptr;
2774 _Position _M_position;
2775 std::vector<int> _M_subs;
2776 value_type _M_suffix;
2777 std::size_t _M_n;
2778 const value_type* _M_result;
2780 // Show whether _M_subs contains -1
2781 bool _M_has_m1;
2784 /** @brief Token iterator for C-style NULL-terminated strings. */
2785 typedef regex_token_iterator<const char*> cregex_token_iterator;
2787 /** @brief Token iterator for standard strings. */
2788 typedef regex_token_iterator<string::const_iterator> sregex_token_iterator;
2790 #ifdef _GLIBCXX_USE_WCHAR_T
2791 /** @brief Token iterator for C-style NULL-terminated wide strings. */
2792 typedef regex_token_iterator<const wchar_t*> wcregex_token_iterator;
2794 /** @brief Token iterator for standard wide-character strings. */
2795 typedef regex_token_iterator<wstring::const_iterator> wsregex_token_iterator;
2796 #endif
2798 //@} // group regex
2800 _GLIBCXX_END_NAMESPACE_CXX11
2801 _GLIBCXX_END_NAMESPACE_VERSION
2802 } // namespace
2804 #include <bits/regex.tcc>