Implement LWG DR 2329 and DR 2332.
[official-gcc.git] / libstdc++-v3 / include / bits / regex.h
blobcb6bc93251a86a8734ffd0c4ac4422d529129bed
1 // class template regex -*- C++ -*-
3 // Copyright (C) 2010-2014 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 template<typename, typename>
35 class basic_regex;
37 template<typename, typename>
38 class match_results;
40 _GLIBCXX_END_NAMESPACE_VERSION
42 namespace __detail
44 _GLIBCXX_BEGIN_NAMESPACE_VERSION
46 enum class _RegexExecutorPolicy : int
47 { _S_auto, _S_alternate };
49 template<typename _BiIter, typename _Alloc,
50 typename _CharT, typename _TraitsT,
51 _RegexExecutorPolicy __policy,
52 bool __match_mode>
53 bool
54 __regex_algo_impl(_BiIter __s,
55 _BiIter __e,
56 match_results<_BiIter, _Alloc>& __m,
57 const basic_regex<_CharT, _TraitsT>& __re,
58 regex_constants::match_flag_type __flags);
60 template<typename, typename, typename, bool>
61 class _Executor;
63 template<typename _TraitsT>
64 inline std::shared_ptr<_NFA<_TraitsT>>
65 __compile_nfa(const typename _TraitsT::char_type* __first,
66 const typename _TraitsT::char_type* __last,
67 const typename _TraitsT::locale_type& __loc,
68 regex_constants::syntax_option_type __flags);
70 _GLIBCXX_END_NAMESPACE_VERSION
73 _GLIBCXX_BEGIN_NAMESPACE_VERSION
75 /**
76 * @addtogroup regex
77 * @{
80 /**
81 * @brief Describes aspects of a regular expression.
83 * A regular expression traits class that satisfies the requirements of
84 * section [28.7].
86 * The class %regex is parameterized around a set of related types and
87 * functions used to complete the definition of its semantics. This class
88 * satisfies the requirements of such a traits class.
90 template<typename _Ch_type>
91 struct regex_traits
93 public:
94 typedef _Ch_type char_type;
95 typedef std::basic_string<char_type> string_type;
96 typedef std::locale locale_type;
97 private:
98 struct _RegexMask
100 typedef std::ctype_base::mask _BaseType;
101 _BaseType _M_base;
102 unsigned char _M_extended;
103 static constexpr unsigned char _S_under = 1 << 0;
104 static constexpr unsigned char _S_valid_mask = 0x1;
106 constexpr _RegexMask(_BaseType __base = 0,
107 unsigned char __extended = 0)
108 : _M_base(__base), _M_extended(__extended)
111 constexpr _RegexMask
112 operator&(_RegexMask __other) const
114 return _RegexMask(_M_base & __other._M_base,
115 _M_extended & __other._M_extended);
118 constexpr _RegexMask
119 operator|(_RegexMask __other) const
121 return _RegexMask(_M_base | __other._M_base,
122 _M_extended | __other._M_extended);
125 constexpr _RegexMask
126 operator^(_RegexMask __other) const
128 return _RegexMask(_M_base ^ __other._M_base,
129 _M_extended ^ __other._M_extended);
132 constexpr _RegexMask
133 operator~() const
134 { return _RegexMask(~_M_base, ~_M_extended); }
136 _RegexMask&
137 operator&=(_RegexMask __other)
138 { return *this = (*this) & __other; }
140 _RegexMask&
141 operator|=(_RegexMask __other)
142 { return *this = (*this) | __other; }
144 _RegexMask&
145 operator^=(_RegexMask __other)
146 { return *this = (*this) ^ __other; }
148 constexpr bool
149 operator==(_RegexMask __other) const
151 return (_M_extended & _S_valid_mask)
152 == (__other._M_extended & _S_valid_mask)
153 && _M_base == __other._M_base;
156 constexpr bool
157 operator!=(_RegexMask __other) const
158 { return !((*this) == __other); }
161 public:
162 typedef _RegexMask char_class_type;
164 public:
166 * @brief Constructs a default traits object.
168 regex_traits() { }
171 * @brief Gives the length of a C-style string starting at @p __p.
173 * @param __p a pointer to the start of a character sequence.
175 * @returns the number of characters between @p *__p and the first
176 * default-initialized value of type @p char_type. In other words, uses
177 * the C-string algorithm for determining the length of a sequence of
178 * characters.
180 static std::size_t
181 length(const char_type* __p)
182 { return string_type::traits_type::length(__p); }
185 * @brief Performs the identity translation.
187 * @param __c A character to the locale-specific character set.
189 * @returns __c.
191 char_type
192 translate(char_type __c) const
193 { return __c; }
196 * @brief Translates a character into a case-insensitive equivalent.
198 * @param __c A character to the locale-specific character set.
200 * @returns the locale-specific lower-case equivalent of __c.
201 * @throws std::bad_cast if the imbued locale does not support the ctype
202 * facet.
204 char_type
205 translate_nocase(char_type __c) const
207 typedef std::ctype<char_type> __ctype_type;
208 const __ctype_type& __fctyp(use_facet<__ctype_type>(_M_locale));
209 return __fctyp.tolower(__c);
213 * @brief Gets a sort key for a character sequence.
215 * @param __first beginning of the character sequence.
216 * @param __last one-past-the-end of the character sequence.
218 * Returns a sort key for the character sequence designated by the
219 * iterator range [F1, F2) such that if the character sequence [G1, G2)
220 * sorts before the character sequence [H1, H2) then
221 * v.transform(G1, G2) < v.transform(H1, H2).
223 * What this really does is provide a more efficient way to compare a
224 * string to multiple other strings in locales with fancy collation
225 * rules and equivalence classes.
227 * @returns a locale-specific sort key equivalent to the input range.
229 * @throws std::bad_cast if the current locale does not have a collate
230 * facet.
232 template<typename _Fwd_iter>
233 string_type
234 transform(_Fwd_iter __first, _Fwd_iter __last) const
236 typedef std::collate<char_type> __collate_type;
237 const __collate_type& __fclt(use_facet<__collate_type>(_M_locale));
238 string_type __s(__first, __last);
239 return __fclt.transform(__s.data(), __s.data() + __s.size());
243 * @brief Gets a sort key for a character sequence, independent of case.
245 * @param __first beginning of the character sequence.
246 * @param __last one-past-the-end of the character sequence.
248 * Effects: if typeid(use_facet<collate<_Ch_type> >) ==
249 * typeid(collate_byname<_Ch_type>) and the form of the sort key
250 * returned by collate_byname<_Ch_type>::transform(__first, __last)
251 * is known and can be converted into a primary sort key
252 * then returns that key, otherwise returns an empty string.
254 * @todo Implement this function correctly.
256 template<typename _Fwd_iter>
257 string_type
258 transform_primary(_Fwd_iter __first, _Fwd_iter __last) const
260 // TODO : this is not entirely correct.
261 // This function requires extra support from the platform.
263 // Read http://gcc.gnu.org/ml/libstdc++/2013-09/msg00117.html and
264 // http://www.open-std.org/Jtc1/sc22/wg21/docs/papers/2003/n1429.htm
265 // for details.
266 typedef std::ctype<char_type> __ctype_type;
267 const __ctype_type& __fctyp(use_facet<__ctype_type>(_M_locale));
268 std::vector<char_type> __s(__first, __last);
269 __fctyp.tolower(__s.data(), __s.data() + __s.size());
270 return this->transform(__s.data(), __s.data() + __s.size());
274 * @brief Gets a collation element by name.
276 * @param __first beginning of the collation element name.
277 * @param __last one-past-the-end of the collation element name.
279 * @returns a sequence of one or more characters that represents the
280 * collating element consisting of the character sequence designated by
281 * the iterator range [__first, __last). Returns an empty string if the
282 * character sequence is not a valid collating element.
284 template<typename _Fwd_iter>
285 string_type
286 lookup_collatename(_Fwd_iter __first, _Fwd_iter __last) const;
289 * @brief Maps one or more characters to a named character
290 * classification.
292 * @param __first beginning of the character sequence.
293 * @param __last one-past-the-end of the character sequence.
294 * @param __icase ignores the case of the classification name.
296 * @returns an unspecified value that represents the character
297 * classification named by the character sequence designated by
298 * the iterator range [__first, __last). If @p icase is true,
299 * the returned mask identifies the classification regardless of
300 * the case of the characters to be matched (for example,
301 * [[:lower:]] is the same as [[:alpha:]]), otherwise a
302 * case-dependent classification is returned. The value
303 * returned shall be independent of the case of the characters
304 * in the character sequence. If the name is not recognized then
305 * returns a value that compares equal to 0.
307 * At least the following names (or their wide-character equivalent) are
308 * supported.
309 * - d
310 * - w
311 * - s
312 * - alnum
313 * - alpha
314 * - blank
315 * - cntrl
316 * - digit
317 * - graph
318 * - lower
319 * - print
320 * - punct
321 * - space
322 * - upper
323 * - xdigit
325 template<typename _Fwd_iter>
326 char_class_type
327 lookup_classname(_Fwd_iter __first, _Fwd_iter __last,
328 bool __icase = false) const;
331 * @brief Determines if @p c is a member of an identified class.
333 * @param __c a character.
334 * @param __f a class type (as returned from lookup_classname).
336 * @returns true if the character @p __c is a member of the classification
337 * represented by @p __f, false otherwise.
339 * @throws std::bad_cast if the current locale does not have a ctype
340 * facet.
342 bool
343 isctype(_Ch_type __c, char_class_type __f) const;
346 * @brief Converts a digit to an int.
348 * @param __ch a character representing a digit.
349 * @param __radix the radix if the numeric conversion (limited to 8, 10,
350 * or 16).
352 * @returns the value represented by the digit __ch in base radix if the
353 * character __ch is a valid digit in base radix; otherwise returns -1.
356 value(_Ch_type __ch, int __radix) const;
359 * @brief Imbues the regex_traits object with a copy of a new locale.
361 * @param __loc A locale.
363 * @returns a copy of the previous locale in use by the regex_traits
364 * object.
366 * @note Calling imbue with a different locale than the one currently in
367 * use invalidates all cached data held by *this.
369 locale_type
370 imbue(locale_type __loc)
372 std::swap(_M_locale, __loc);
373 return __loc;
377 * @brief Gets a copy of the current locale in use by the regex_traits
378 * object.
380 locale_type
381 getloc() const
382 { return _M_locale; }
384 protected:
385 locale_type _M_locale;
388 // [7.8] Class basic_regex
390 * Objects of specializations of this class represent regular expressions
391 * constructed from sequences of character type @p _Ch_type.
393 * Storage for the regular expression is allocated and deallocated as
394 * necessary by the member functions of this class.
396 template<typename _Ch_type, typename _Rx_traits = regex_traits<_Ch_type>>
397 class basic_regex
399 public:
400 static_assert(is_same<_Ch_type, typename _Rx_traits::char_type>::value,
401 "regex traits class must have the same char_type");
403 // types:
404 typedef _Ch_type value_type;
405 typedef _Rx_traits traits_type;
406 typedef typename traits_type::string_type string_type;
407 typedef regex_constants::syntax_option_type flag_type;
408 typedef typename traits_type::locale_type locale_type;
411 * @name Constants
412 * std [28.8.1](1)
414 //@{
415 static constexpr flag_type icase = regex_constants::icase;
416 static constexpr flag_type nosubs = regex_constants::nosubs;
417 static constexpr flag_type optimize = regex_constants::optimize;
418 static constexpr flag_type collate = regex_constants::collate;
419 static constexpr flag_type ECMAScript = regex_constants::ECMAScript;
420 static constexpr flag_type basic = regex_constants::basic;
421 static constexpr flag_type extended = regex_constants::extended;
422 static constexpr flag_type awk = regex_constants::awk;
423 static constexpr flag_type grep = regex_constants::grep;
424 static constexpr flag_type egrep = regex_constants::egrep;
425 //@}
427 // [7.8.2] construct/copy/destroy
429 * Constructs a basic regular expression that does not match any
430 * character sequence.
432 basic_regex()
433 : _M_flags(ECMAScript), _M_loc(), _M_original_str(), _M_automaton(nullptr)
437 * @brief Constructs a basic regular expression from the
438 * sequence [__p, __p + char_traits<_Ch_type>::length(__p))
439 * interpreted according to the flags in @p __f.
441 * @param __p A pointer to the start of a C-style null-terminated string
442 * containing a regular expression.
443 * @param __f Flags indicating the syntax rules and options.
445 * @throws regex_error if @p __p is not a valid regular expression.
447 explicit
448 basic_regex(const _Ch_type* __p, flag_type __f = ECMAScript)
449 : basic_regex(__p, __p + _Rx_traits::length(__p), __f)
453 * @brief Constructs a basic regular expression from the sequence
454 * [p, p + len) interpreted according to the flags in @p f.
456 * @param __p A pointer to the start of a string containing a regular
457 * expression.
458 * @param __len The length of the string containing the regular
459 * expression.
460 * @param __f Flags indicating the syntax rules and options.
462 * @throws regex_error if @p __p is not a valid regular expression.
464 basic_regex(const _Ch_type* __p, std::size_t __len,
465 flag_type __f = ECMAScript)
466 : basic_regex(__p, __p + __len, __f)
470 * @brief Copy-constructs a basic regular expression.
472 * @param __rhs A @p regex object.
474 basic_regex(const basic_regex& __rhs) = default;
477 * @brief Move-constructs a basic regular expression.
479 * @param __rhs A @p regex object.
481 basic_regex(basic_regex&& __rhs) noexcept = default;
484 * @brief Constructs a basic regular expression from the string
485 * @p s interpreted according to the flags in @p f.
487 * @param __s A string containing a regular expression.
488 * @param __f Flags indicating the syntax rules and options.
490 * @throws regex_error if @p __s is not a valid regular expression.
492 template<typename _Ch_traits, typename _Ch_alloc>
493 explicit
494 basic_regex(const std::basic_string<_Ch_type, _Ch_traits,
495 _Ch_alloc>& __s,
496 flag_type __f = ECMAScript)
497 : basic_regex(__s.begin(), __s.end(), __f)
501 * @brief Constructs a basic regular expression from the range
502 * [first, last) interpreted according to the flags in @p f.
504 * @param __first The start of a range containing a valid regular
505 * expression.
506 * @param __last The end of a range containing a valid regular
507 * expression.
508 * @param __f The format flags of the regular expression.
510 * @throws regex_error if @p [__first, __last) is not a valid regular
511 * expression.
513 template<typename _FwdIter>
514 basic_regex(_FwdIter __first, _FwdIter __last,
515 flag_type __f = ECMAScript)
516 : _M_flags(__f),
517 _M_loc(),
518 _M_original_str(__first, __last),
519 _M_automaton(__detail::__compile_nfa<_Rx_traits>(
520 _M_original_str.c_str(),
521 _M_original_str.c_str() + _M_original_str.size(),
522 _M_loc,
523 _M_flags))
527 * @brief Constructs a basic regular expression from an initializer list.
529 * @param __l The initializer list.
530 * @param __f The format flags of the regular expression.
532 * @throws regex_error if @p __l is not a valid regular expression.
534 basic_regex(initializer_list<_Ch_type> __l, flag_type __f = ECMAScript)
535 : basic_regex(__l.begin(), __l.end(), __f)
539 * @brief Destroys a basic regular expression.
541 ~basic_regex()
545 * @brief Assigns one regular expression to another.
547 basic_regex&
548 operator=(const basic_regex& __rhs)
549 { return this->assign(__rhs); }
552 * @brief Move-assigns one regular expression to another.
554 basic_regex&
555 operator=(basic_regex&& __rhs) noexcept
556 { return this->assign(std::move(__rhs)); }
559 * @brief Replaces a regular expression with a new one constructed from
560 * a C-style null-terminated string.
562 * @param __p A pointer to the start of a null-terminated C-style string
563 * containing a regular expression.
565 basic_regex&
566 operator=(const _Ch_type* __p)
567 { return this->assign(__p, flags()); }
570 * @brief Replaces a regular expression with a new one constructed from
571 * a string.
573 * @param __s A pointer to a string containing a regular expression.
575 template<typename _Ch_traits, typename _Alloc>
576 basic_regex&
577 operator=(const basic_string<_Ch_type, _Ch_traits, _Alloc>& __s)
578 { return this->assign(__s, flags()); }
580 // [7.8.3] assign
582 * @brief the real assignment operator.
584 * @param __rhs Another regular expression object.
586 basic_regex&
587 assign(const basic_regex& __rhs)
589 basic_regex __tmp(__rhs);
590 this->swap(__tmp);
591 return *this;
595 * @brief The move-assignment operator.
597 * @param __rhs Another regular expression object.
599 basic_regex&
600 assign(basic_regex&& __rhs) noexcept
602 basic_regex __tmp(std::move(__rhs));
603 this->swap(__tmp);
604 return *this;
608 * @brief Assigns a new regular expression to a regex object from a
609 * C-style null-terminated string containing a regular expression
610 * pattern.
612 * @param __p A pointer to a C-style null-terminated string containing
613 * a regular expression pattern.
614 * @param __flags Syntax option flags.
616 * @throws regex_error if __p does not contain a valid regular
617 * expression pattern interpreted according to @p __flags. If
618 * regex_error is thrown, *this remains unchanged.
620 basic_regex&
621 assign(const _Ch_type* __p, flag_type __flags = ECMAScript)
622 { return this->assign(string_type(__p), __flags); }
625 * @brief Assigns a new regular expression to a regex object from a
626 * C-style string containing a regular expression pattern.
628 * @param __p A pointer to a C-style string containing a
629 * regular expression pattern.
630 * @param __len The length of the regular expression pattern string.
631 * @param __flags Syntax option flags.
633 * @throws regex_error if p does not contain a valid regular
634 * expression pattern interpreted according to @p __flags. If
635 * regex_error is thrown, *this remains unchanged.
637 basic_regex&
638 assign(const _Ch_type* __p, std::size_t __len, flag_type __flags)
639 { return this->assign(string_type(__p, __len), __flags); }
642 * @brief Assigns a new regular expression to a regex object from a
643 * string containing a regular expression pattern.
645 * @param __s A string containing a regular expression pattern.
646 * @param __flags Syntax option flags.
648 * @throws regex_error if __s does not contain a valid regular
649 * expression pattern interpreted according to @p __flags. If
650 * regex_error is thrown, *this remains unchanged.
652 template<typename _Ch_traits, typename _Alloc>
653 basic_regex&
654 assign(const basic_string<_Ch_type, _Ch_traits, _Alloc>& __s,
655 flag_type __flags = ECMAScript)
657 _M_flags = __flags;
658 _M_original_str.assign(__s.begin(), __s.end());
659 auto __p = _M_original_str.c_str();
660 _M_automaton = __detail::__compile_nfa<_Rx_traits>(
661 __p,
662 __p + _M_original_str.size(),
663 _M_loc,
664 _M_flags);
665 return *this;
669 * @brief Assigns a new regular expression to a regex object.
671 * @param __first The start of a range containing a valid regular
672 * expression.
673 * @param __last The end of a range containing a valid regular
674 * expression.
675 * @param __flags Syntax option flags.
677 * @throws regex_error if p does not contain a valid regular
678 * expression pattern interpreted according to @p __flags. If
679 * regex_error is thrown, the object remains unchanged.
681 template<typename _InputIterator>
682 basic_regex&
683 assign(_InputIterator __first, _InputIterator __last,
684 flag_type __flags = ECMAScript)
685 { return this->assign(string_type(__first, __last), __flags); }
688 * @brief Assigns a new regular expression to a regex object.
690 * @param __l An initializer list representing a regular expression.
691 * @param __flags Syntax option flags.
693 * @throws regex_error if @p __l does not contain a valid
694 * regular expression pattern interpreted according to @p
695 * __flags. If regex_error is thrown, the object remains
696 * unchanged.
698 basic_regex&
699 assign(initializer_list<_Ch_type> __l, flag_type __flags = ECMAScript)
700 { return this->assign(__l.begin(), __l.end(), __flags); }
702 // [7.8.4] const operations
704 * @brief Gets the number of marked subexpressions within the regular
705 * expression.
707 unsigned int
708 mark_count() const
709 { return _M_automaton->_M_sub_count() - 1; }
712 * @brief Gets the flags used to construct the regular expression
713 * or in the last call to assign().
715 flag_type
716 flags() const
717 { return _M_flags; }
719 // [7.8.5] locale
721 * @brief Imbues the regular expression object with the given locale.
723 * @param __loc A locale.
725 locale_type
726 imbue(locale_type __loc)
728 std::swap(__loc, _M_loc);
729 if (_M_automaton != nullptr)
730 this->assign(_M_original_str, _M_flags);
731 return __loc;
735 * @brief Gets the locale currently imbued in the regular expression
736 * object.
738 locale_type
739 getloc() const
740 { return _M_loc; }
742 // [7.8.6] swap
744 * @brief Swaps the contents of two regular expression objects.
746 * @param __rhs Another regular expression object.
748 void
749 swap(basic_regex& __rhs)
751 std::swap(_M_flags, __rhs._M_flags);
752 std::swap(_M_loc, __rhs._M_loc);
753 std::swap(_M_original_str, __rhs._M_original_str);
754 std::swap(_M_automaton, __rhs._M_automaton);
757 #ifdef _GLIBCXX_DEBUG
758 void
759 _M_dot(std::ostream& __ostr)
760 { _M_automaton->_M_dot(__ostr); }
761 #endif
763 private:
764 typedef std::shared_ptr<__detail::_NFA<_Rx_traits>> _AutomatonPtr;
766 template<typename _Bp, typename _Ap, typename _Cp, typename _Rp,
767 __detail::_RegexExecutorPolicy, bool>
768 friend bool
769 __detail::__regex_algo_impl(_Bp, _Bp, match_results<_Bp, _Ap>&,
770 const basic_regex<_Cp, _Rp>&,
771 regex_constants::match_flag_type);
773 template<typename, typename, typename, bool>
774 friend class __detail::_Executor;
776 flag_type _M_flags;
777 locale_type _M_loc;
778 basic_string<_Ch_type> _M_original_str;
779 _AutomatonPtr _M_automaton;
782 /** @brief Standard regular expressions. */
783 typedef basic_regex<char> regex;
785 #ifdef _GLIBCXX_USE_WCHAR_T
786 /** @brief Standard wide-character regular expressions. */
787 typedef basic_regex<wchar_t> wregex;
788 #endif
791 // [7.8.6] basic_regex swap
793 * @brief Swaps the contents of two regular expression objects.
794 * @param __lhs First regular expression.
795 * @param __rhs Second regular expression.
797 template<typename _Ch_type, typename _Rx_traits>
798 inline void
799 swap(basic_regex<_Ch_type, _Rx_traits>& __lhs,
800 basic_regex<_Ch_type, _Rx_traits>& __rhs)
801 { __lhs.swap(__rhs); }
804 // [7.9] Class template sub_match
806 * A sequence of characters matched by a particular marked sub-expression.
808 * An object of this class is essentially a pair of iterators marking a
809 * matched subexpression within a regular expression pattern match. Such
810 * objects can be converted to and compared with std::basic_string objects
811 * of a similar base character type as the pattern matched by the regular
812 * expression.
814 * The iterators that make up the pair are the usual half-open interval
815 * referencing the actual original pattern matched.
817 template<typename _BiIter>
818 class sub_match : public std::pair<_BiIter, _BiIter>
820 typedef iterator_traits<_BiIter> __iter_traits;
822 public:
823 typedef typename __iter_traits::value_type value_type;
824 typedef typename __iter_traits::difference_type difference_type;
825 typedef _BiIter iterator;
826 typedef std::basic_string<value_type> string_type;
828 bool matched;
830 constexpr sub_match() : matched() { }
833 * Gets the length of the matching sequence.
835 difference_type
836 length() const
837 { return this->matched ? std::distance(this->first, this->second) : 0; }
840 * @brief Gets the matching sequence as a string.
842 * @returns the matching sequence as a string.
844 * This is the implicit conversion operator. It is identical to the
845 * str() member function except that it will want to pop up in
846 * unexpected places and cause a great deal of confusion and cursing
847 * from the unwary.
849 operator string_type() const
851 return this->matched
852 ? string_type(this->first, this->second)
853 : string_type();
857 * @brief Gets the matching sequence as a string.
859 * @returns the matching sequence as a string.
861 string_type
862 str() const
864 return this->matched
865 ? string_type(this->first, this->second)
866 : string_type();
870 * @brief Compares this and another matched sequence.
872 * @param __s Another matched sequence to compare to this one.
874 * @retval <0 this matched sequence will collate before @p __s.
875 * @retval =0 this matched sequence is equivalent to @p __s.
876 * @retval <0 this matched sequence will collate after @p __s.
879 compare(const sub_match& __s) const
880 { return this->str().compare(__s.str()); }
883 * @brief Compares this sub_match to a string.
885 * @param __s A string to compare to this sub_match.
887 * @retval <0 this matched sequence will collate before @p __s.
888 * @retval =0 this matched sequence is equivalent to @p __s.
889 * @retval <0 this matched sequence will collate after @p __s.
892 compare(const string_type& __s) const
893 { return this->str().compare(__s); }
896 * @brief Compares this sub_match to a C-style string.
898 * @param __s A C-style string to compare to this sub_match.
900 * @retval <0 this matched sequence will collate before @p __s.
901 * @retval =0 this matched sequence is equivalent to @p __s.
902 * @retval <0 this matched sequence will collate after @p __s.
905 compare(const value_type* __s) const
906 { return this->str().compare(__s); }
910 /** @brief Standard regex submatch over a C-style null-terminated string. */
911 typedef sub_match<const char*> csub_match;
913 /** @brief Standard regex submatch over a standard string. */
914 typedef sub_match<string::const_iterator> ssub_match;
916 #ifdef _GLIBCXX_USE_WCHAR_T
917 /** @brief Regex submatch over a C-style null-terminated wide string. */
918 typedef sub_match<const wchar_t*> wcsub_match;
920 /** @brief Regex submatch over a standard wide string. */
921 typedef sub_match<wstring::const_iterator> wssub_match;
922 #endif
924 // [7.9.2] sub_match non-member operators
927 * @brief Tests the equivalence of two regular expression submatches.
928 * @param __lhs First regular expression submatch.
929 * @param __rhs Second regular expression submatch.
930 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
932 template<typename _BiIter>
933 inline bool
934 operator==(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
935 { return __lhs.compare(__rhs) == 0; }
938 * @brief Tests the inequivalence of two regular expression submatches.
939 * @param __lhs First regular expression submatch.
940 * @param __rhs Second regular expression submatch.
941 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
943 template<typename _BiIter>
944 inline bool
945 operator!=(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
946 { return __lhs.compare(__rhs) != 0; }
949 * @brief Tests the ordering of two regular expression submatches.
950 * @param __lhs First regular expression submatch.
951 * @param __rhs Second regular expression submatch.
952 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
954 template<typename _BiIter>
955 inline bool
956 operator<(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
957 { return __lhs.compare(__rhs) < 0; }
960 * @brief Tests the ordering of two regular expression submatches.
961 * @param __lhs First regular expression submatch.
962 * @param __rhs Second regular expression submatch.
963 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
965 template<typename _BiIter>
966 inline bool
967 operator<=(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
968 { return __lhs.compare(__rhs) <= 0; }
971 * @brief Tests the ordering of two regular expression submatches.
972 * @param __lhs First regular expression submatch.
973 * @param __rhs Second regular expression submatch.
974 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
976 template<typename _BiIter>
977 inline bool
978 operator>=(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
979 { return __lhs.compare(__rhs) >= 0; }
982 * @brief Tests the ordering of two regular expression submatches.
983 * @param __lhs First regular expression submatch.
984 * @param __rhs Second regular expression submatch.
985 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
987 template<typename _BiIter>
988 inline bool
989 operator>(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
990 { return __lhs.compare(__rhs) > 0; }
992 // Alias for sub_match'd string.
993 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
994 using __sub_match_string = basic_string<
995 typename iterator_traits<_Bi_iter>::value_type,
996 _Ch_traits, _Ch_alloc>;
999 * @brief Tests the equivalence of a string and a regular expression
1000 * submatch.
1001 * @param __lhs A string.
1002 * @param __rhs A regular expression submatch.
1003 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1005 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1006 inline bool
1007 operator==(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1008 const sub_match<_Bi_iter>& __rhs)
1010 typedef typename sub_match<_Bi_iter>::string_type string_type;
1011 return __rhs.compare(string_type(__lhs.data(), __lhs.size())) == 0;
1015 * @brief Tests the inequivalence of a string and a regular expression
1016 * submatch.
1017 * @param __lhs A string.
1018 * @param __rhs A regular expression submatch.
1019 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1021 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1022 inline bool
1023 operator!=(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1024 const sub_match<_Bi_iter>& __rhs)
1025 { return !(__lhs == __rhs); }
1028 * @brief Tests the ordering of a string and a regular expression submatch.
1029 * @param __lhs A string.
1030 * @param __rhs A regular expression submatch.
1031 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1033 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1034 inline bool
1035 operator<(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1036 const sub_match<_Bi_iter>& __rhs)
1038 typedef typename sub_match<_Bi_iter>::string_type string_type;
1039 return __rhs.compare(string_type(__lhs.data(), __lhs.size())) > 0;
1043 * @brief Tests the ordering of a string and a regular expression submatch.
1044 * @param __lhs A string.
1045 * @param __rhs A regular expression submatch.
1046 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1048 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1049 inline bool
1050 operator>(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1051 const sub_match<_Bi_iter>& __rhs)
1052 { return __rhs < __lhs; }
1055 * @brief Tests the ordering of a string and a regular expression submatch.
1056 * @param __lhs A string.
1057 * @param __rhs A regular expression submatch.
1058 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1060 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1061 inline bool
1062 operator>=(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1063 const sub_match<_Bi_iter>& __rhs)
1064 { return !(__lhs < __rhs); }
1067 * @brief Tests the ordering of a string and a regular expression submatch.
1068 * @param __lhs A string.
1069 * @param __rhs A regular expression submatch.
1070 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1072 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1073 inline bool
1074 operator<=(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1075 const sub_match<_Bi_iter>& __rhs)
1076 { return !(__rhs < __lhs); }
1079 * @brief Tests the equivalence of a regular expression submatch and a
1080 * string.
1081 * @param __lhs A regular expression submatch.
1082 * @param __rhs A string.
1083 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1085 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1086 inline bool
1087 operator==(const sub_match<_Bi_iter>& __lhs,
1088 const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1090 typedef typename sub_match<_Bi_iter>::string_type string_type;
1091 return __lhs.compare(string_type(__rhs.data(), __rhs.size())) == 0;
1095 * @brief Tests the inequivalence of a regular expression submatch and a
1096 * string.
1097 * @param __lhs A regular expression submatch.
1098 * @param __rhs A string.
1099 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1101 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1102 inline bool
1103 operator!=(const sub_match<_Bi_iter>& __lhs,
1104 const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1105 { return !(__lhs == __rhs); }
1108 * @brief Tests the ordering of a regular expression submatch and a string.
1109 * @param __lhs A regular expression submatch.
1110 * @param __rhs A string.
1111 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1113 template<typename _Bi_iter, class _Ch_traits, class _Ch_alloc>
1114 inline bool
1115 operator<(const sub_match<_Bi_iter>& __lhs,
1116 const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1118 typedef typename sub_match<_Bi_iter>::string_type string_type;
1119 return __lhs.compare(string_type(__rhs.data(), __rhs.size())) < 0;
1123 * @brief Tests the ordering of a regular expression submatch and a string.
1124 * @param __lhs A regular expression submatch.
1125 * @param __rhs A string.
1126 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1128 template<typename _Bi_iter, class _Ch_traits, class _Ch_alloc>
1129 inline bool
1130 operator>(const sub_match<_Bi_iter>& __lhs,
1131 const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1132 { return __rhs < __lhs; }
1135 * @brief Tests the ordering of a regular expression submatch and a string.
1136 * @param __lhs A regular expression submatch.
1137 * @param __rhs A string.
1138 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1140 template<typename _Bi_iter, class _Ch_traits, class _Ch_alloc>
1141 inline bool
1142 operator>=(const sub_match<_Bi_iter>& __lhs,
1143 const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1144 { return !(__lhs < __rhs); }
1147 * @brief Tests the ordering of a regular expression submatch and a string.
1148 * @param __lhs A regular expression submatch.
1149 * @param __rhs A string.
1150 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1152 template<typename _Bi_iter, class _Ch_traits, class _Ch_alloc>
1153 inline bool
1154 operator<=(const sub_match<_Bi_iter>& __lhs,
1155 const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1156 { return !(__rhs < __lhs); }
1159 * @brief Tests the equivalence of a C string and a regular expression
1160 * submatch.
1161 * @param __lhs A C string.
1162 * @param __rhs A regular expression submatch.
1163 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1165 template<typename _Bi_iter>
1166 inline bool
1167 operator==(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1168 const sub_match<_Bi_iter>& __rhs)
1169 { return __rhs.compare(__lhs) == 0; }
1172 * @brief Tests the inequivalence of an iterator value and a regular
1173 * expression submatch.
1174 * @param __lhs A regular expression submatch.
1175 * @param __rhs A string.
1176 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1178 template<typename _Bi_iter>
1179 inline bool
1180 operator!=(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1181 const sub_match<_Bi_iter>& __rhs)
1182 { return !(__lhs == __rhs); }
1185 * @brief Tests the ordering of a string and a regular expression submatch.
1186 * @param __lhs A string.
1187 * @param __rhs A regular expression submatch.
1188 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1190 template<typename _Bi_iter>
1191 inline bool
1192 operator<(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1193 const sub_match<_Bi_iter>& __rhs)
1194 { return __rhs.compare(__lhs) > 0; }
1197 * @brief Tests the ordering of a string and a regular expression submatch.
1198 * @param __lhs A string.
1199 * @param __rhs A regular expression submatch.
1200 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1202 template<typename _Bi_iter>
1203 inline bool
1204 operator>(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1205 const sub_match<_Bi_iter>& __rhs)
1206 { return __rhs < __lhs; }
1209 * @brief Tests the ordering of a string and a regular expression submatch.
1210 * @param __lhs A string.
1211 * @param __rhs A regular expression submatch.
1212 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1214 template<typename _Bi_iter>
1215 inline bool
1216 operator>=(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1217 const sub_match<_Bi_iter>& __rhs)
1218 { return !(__lhs < __rhs); }
1221 * @brief Tests the ordering of a string and a regular expression submatch.
1222 * @param __lhs A string.
1223 * @param __rhs A regular expression submatch.
1224 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1226 template<typename _Bi_iter>
1227 inline bool
1228 operator<=(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1229 const sub_match<_Bi_iter>& __rhs)
1230 { return !(__rhs < __lhs); }
1233 * @brief Tests the equivalence of a regular expression submatch and a
1234 * string.
1235 * @param __lhs A regular expression submatch.
1236 * @param __rhs A pointer to a string?
1237 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1239 template<typename _Bi_iter>
1240 inline bool
1241 operator==(const sub_match<_Bi_iter>& __lhs,
1242 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1243 { return __lhs.compare(__rhs) == 0; }
1246 * @brief Tests the inequivalence of a regular expression submatch and a
1247 * string.
1248 * @param __lhs A regular expression submatch.
1249 * @param __rhs A pointer to a string.
1250 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1252 template<typename _Bi_iter>
1253 inline bool
1254 operator!=(const sub_match<_Bi_iter>& __lhs,
1255 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1256 { return !(__lhs == __rhs); }
1259 * @brief Tests the ordering of a regular expression submatch and a string.
1260 * @param __lhs A regular expression submatch.
1261 * @param __rhs A string.
1262 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1264 template<typename _Bi_iter>
1265 inline bool
1266 operator<(const sub_match<_Bi_iter>& __lhs,
1267 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1268 { return __lhs.compare(__rhs) < 0; }
1271 * @brief Tests the ordering of a regular expression submatch and a string.
1272 * @param __lhs A regular expression submatch.
1273 * @param __rhs A string.
1274 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1276 template<typename _Bi_iter>
1277 inline bool
1278 operator>(const sub_match<_Bi_iter>& __lhs,
1279 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1280 { return __rhs < __lhs; }
1283 * @brief Tests the ordering of a regular expression submatch and a string.
1284 * @param __lhs A regular expression submatch.
1285 * @param __rhs A string.
1286 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1288 template<typename _Bi_iter>
1289 inline bool
1290 operator>=(const sub_match<_Bi_iter>& __lhs,
1291 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1292 { return !(__lhs < __rhs); }
1295 * @brief Tests the ordering of a regular expression submatch and a string.
1296 * @param __lhs A regular expression submatch.
1297 * @param __rhs A string.
1298 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1300 template<typename _Bi_iter>
1301 inline bool
1302 operator<=(const sub_match<_Bi_iter>& __lhs,
1303 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1304 { return !(__rhs < __lhs); }
1307 * @brief Tests the equivalence of a string and a regular expression
1308 * submatch.
1309 * @param __lhs A string.
1310 * @param __rhs A regular expression submatch.
1311 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1313 template<typename _Bi_iter>
1314 inline bool
1315 operator==(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1316 const sub_match<_Bi_iter>& __rhs)
1318 typedef typename sub_match<_Bi_iter>::string_type string_type;
1319 return __rhs.compare(string_type(1, __lhs)) == 0;
1323 * @brief Tests the inequivalence of a string and a regular expression
1324 * submatch.
1325 * @param __lhs A string.
1326 * @param __rhs A regular expression submatch.
1327 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1329 template<typename _Bi_iter>
1330 inline bool
1331 operator!=(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1332 const sub_match<_Bi_iter>& __rhs)
1333 { return !(__lhs == __rhs); }
1336 * @brief Tests the ordering of a string and a regular expression submatch.
1337 * @param __lhs A string.
1338 * @param __rhs A regular expression submatch.
1339 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1341 template<typename _Bi_iter>
1342 inline bool
1343 operator<(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1344 const sub_match<_Bi_iter>& __rhs)
1346 typedef typename sub_match<_Bi_iter>::string_type string_type;
1347 return __rhs.compare(string_type(1, __lhs)) > 0;
1351 * @brief Tests the ordering of a string and a regular expression submatch.
1352 * @param __lhs A string.
1353 * @param __rhs A regular expression submatch.
1354 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1356 template<typename _Bi_iter>
1357 inline bool
1358 operator>(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1359 const sub_match<_Bi_iter>& __rhs)
1360 { return __rhs < __lhs; }
1363 * @brief Tests the ordering of a string and a regular expression submatch.
1364 * @param __lhs A string.
1365 * @param __rhs A regular expression submatch.
1366 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1368 template<typename _Bi_iter>
1369 inline bool
1370 operator>=(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1371 const sub_match<_Bi_iter>& __rhs)
1372 { return !(__lhs < __rhs); }
1375 * @brief Tests the ordering of a string and a regular expression submatch.
1376 * @param __lhs A string.
1377 * @param __rhs A regular expression submatch.
1378 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1380 template<typename _Bi_iter>
1381 inline bool
1382 operator<=(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1383 const sub_match<_Bi_iter>& __rhs)
1384 { return !(__rhs < __lhs); }
1387 * @brief Tests the equivalence of a regular expression submatch and a
1388 * string.
1389 * @param __lhs A regular expression submatch.
1390 * @param __rhs A const string reference.
1391 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1393 template<typename _Bi_iter>
1394 inline bool
1395 operator==(const sub_match<_Bi_iter>& __lhs,
1396 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1398 typedef typename sub_match<_Bi_iter>::string_type string_type;
1399 return __lhs.compare(string_type(1, __rhs)) == 0;
1403 * @brief Tests the inequivalence of a regular expression submatch and a
1404 * string.
1405 * @param __lhs A regular expression submatch.
1406 * @param __rhs A const string reference.
1407 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1409 template<typename _Bi_iter>
1410 inline bool
1411 operator!=(const sub_match<_Bi_iter>& __lhs,
1412 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1413 { return !(__lhs == __rhs); }
1416 * @brief Tests the ordering of a regular expression submatch and a string.
1417 * @param __lhs A regular expression submatch.
1418 * @param __rhs A const string reference.
1419 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1421 template<typename _Bi_iter>
1422 inline bool
1423 operator<(const sub_match<_Bi_iter>& __lhs,
1424 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1426 typedef typename sub_match<_Bi_iter>::string_type string_type;
1427 return __lhs.compare(string_type(1, __rhs)) < 0;
1431 * @brief Tests the ordering of a regular expression submatch and a string.
1432 * @param __lhs A regular expression submatch.
1433 * @param __rhs A const string reference.
1434 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1436 template<typename _Bi_iter>
1437 inline bool
1438 operator>(const sub_match<_Bi_iter>& __lhs,
1439 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1440 { return __rhs < __lhs; }
1443 * @brief Tests the ordering of a regular expression submatch and a string.
1444 * @param __lhs A regular expression submatch.
1445 * @param __rhs A const string reference.
1446 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1448 template<typename _Bi_iter>
1449 inline bool
1450 operator>=(const sub_match<_Bi_iter>& __lhs,
1451 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1452 { return !(__lhs < __rhs); }
1455 * @brief Tests the ordering of a regular expression submatch and a string.
1456 * @param __lhs A regular expression submatch.
1457 * @param __rhs A const string reference.
1458 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1460 template<typename _Bi_iter>
1461 inline bool
1462 operator<=(const sub_match<_Bi_iter>& __lhs,
1463 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1464 { return !(__rhs < __lhs); }
1467 * @brief Inserts a matched string into an output stream.
1469 * @param __os The output stream.
1470 * @param __m A submatch string.
1472 * @returns the output stream with the submatch string inserted.
1474 template<typename _Ch_type, typename _Ch_traits, typename _Bi_iter>
1475 inline
1476 basic_ostream<_Ch_type, _Ch_traits>&
1477 operator<<(basic_ostream<_Ch_type, _Ch_traits>& __os,
1478 const sub_match<_Bi_iter>& __m)
1479 { return __os << __m.str(); }
1481 // [7.10] Class template match_results
1484 * Special sub_match object representing an unmatched sub-expression.
1486 template<typename _Bi_iter>
1487 inline const sub_match<_Bi_iter>&
1488 __unmatched_sub()
1490 static const sub_match<_Bi_iter> __unmatched = sub_match<_Bi_iter>();
1491 return __unmatched;
1495 * @brief The results of a match or search operation.
1497 * A collection of character sequences representing the result of a regular
1498 * expression match. Storage for the collection is allocated and freed as
1499 * necessary by the member functions of class template match_results.
1501 * This class satisfies the Sequence requirements, with the exception that
1502 * only the operations defined for a const-qualified Sequence are supported.
1504 * The sub_match object stored at index 0 represents sub-expression 0, i.e.
1505 * the whole match. In this case the %sub_match member matched is always true.
1506 * The sub_match object stored at index n denotes what matched the marked
1507 * sub-expression n within the matched expression. If the sub-expression n
1508 * participated in a regular expression match then the %sub_match member
1509 * matched evaluates to true, and members first and second denote the range
1510 * of characters [first, second) which formed that match. Otherwise matched
1511 * is false, and members first and second point to the end of the sequence
1512 * that was searched.
1514 * @nosubgrouping
1516 template<typename _Bi_iter,
1517 typename _Alloc = allocator<sub_match<_Bi_iter> > >
1518 class match_results
1519 : private std::vector<sub_match<_Bi_iter>, _Alloc>
1521 private:
1523 * The vector base is empty if this does not represent a successful match.
1524 * Otherwise it contains n+3 elements where n is the number of marked
1525 * sub-expressions:
1526 * [0] entire match
1527 * [1] 1st marked subexpression
1528 * ...
1529 * [n] nth marked subexpression
1530 * [n+1] prefix
1531 * [n+2] suffix
1533 typedef std::vector<sub_match<_Bi_iter>, _Alloc> _Base_type;
1534 typedef std::iterator_traits<_Bi_iter> __iter_traits;
1535 typedef regex_constants::match_flag_type match_flag_type;
1537 public:
1539 * @name 10.? Public Types
1541 //@{
1542 typedef sub_match<_Bi_iter> value_type;
1543 typedef const value_type& const_reference;
1544 typedef const_reference reference;
1545 typedef typename _Base_type::const_iterator const_iterator;
1546 typedef const_iterator iterator;
1547 typedef typename __iter_traits::difference_type difference_type;
1548 typedef typename allocator_traits<_Alloc>::size_type size_type;
1549 typedef _Alloc allocator_type;
1550 typedef typename __iter_traits::value_type char_type;
1551 typedef std::basic_string<char_type> string_type;
1552 //@}
1554 public:
1556 * @name 28.10.1 Construction, Copying, and Destruction
1558 //@{
1561 * @brief Constructs a default %match_results container.
1562 * @post size() returns 0 and str() returns an empty string.
1564 explicit
1565 match_results(const _Alloc& __a = _Alloc())
1566 : _Base_type(__a), _M_in_iterator(false)
1570 * @brief Copy constructs a %match_results.
1572 match_results(const match_results& __rhs)
1573 : _Base_type(__rhs), _M_in_iterator(false)
1577 * @brief Move constructs a %match_results.
1579 match_results(match_results&& __rhs) noexcept
1580 : _Base_type(std::move(__rhs)), _M_in_iterator(false)
1584 * @brief Assigns rhs to *this.
1586 match_results&
1587 operator=(const match_results& __rhs)
1589 match_results(__rhs).swap(*this);
1590 return *this;
1594 * @brief Move-assigns rhs to *this.
1596 match_results&
1597 operator=(match_results&& __rhs)
1599 match_results(std::move(__rhs)).swap(*this);
1600 return *this;
1604 * @brief Destroys a %match_results object.
1606 ~match_results()
1609 //@}
1611 // 28.10.2, state:
1613 * @brief Indicates if the %match_results is ready.
1614 * @retval true The object has a fully-established result state.
1615 * @retval false The object is not ready.
1617 bool ready() const { return !_Base_type::empty(); }
1620 * @name 28.10.2 Size
1622 //@{
1625 * @brief Gets the number of matches and submatches.
1627 * The number of matches for a given regular expression will be either 0
1628 * if there was no match or mark_count() + 1 if a match was successful.
1629 * Some matches may be empty.
1631 * @returns the number of matches found.
1633 size_type
1634 size() const
1636 size_type __size = _Base_type::size();
1637 return (__size && _Base_type::operator[](0).matched) ? __size - 2 : 0;
1640 size_type
1641 max_size() const
1642 { return _Base_type::max_size(); }
1645 * @brief Indicates if the %match_results contains no results.
1646 * @retval true The %match_results object is empty.
1647 * @retval false The %match_results object is not empty.
1649 bool
1650 empty() const
1651 { return size() == 0; }
1653 //@}
1656 * @name 10.3 Element Access
1658 //@{
1661 * @brief Gets the length of the indicated submatch.
1662 * @param __sub indicates the submatch.
1663 * @pre ready() == true
1665 * This function returns the length of the indicated submatch, or the
1666 * length of the entire match if @p __sub is zero (the default).
1668 difference_type
1669 length(size_type __sub = 0) const
1670 { return (*this)[__sub].length(); }
1673 * @brief Gets the offset of the beginning of the indicated submatch.
1674 * @param __sub indicates the submatch.
1675 * @pre ready() == true
1677 * This function returns the offset from the beginning of the target
1678 * sequence to the beginning of the submatch, unless the value of @p __sub
1679 * is zero (the default), in which case this function returns the offset
1680 * from the beginning of the target sequence to the beginning of the
1681 * match.
1683 * Returns -1 if @p __sub is out of range.
1685 difference_type
1686 position(size_type __sub = 0) const
1688 // [28.12.1.4.5]
1689 if (_M_in_iterator)
1690 return __sub < size() ? std::distance(_M_begin,
1691 (*this)[__sub].first) : -1;
1692 else
1693 return __sub < size() ? std::distance(this->prefix().first,
1694 (*this)[__sub].first) : -1;
1698 * @brief Gets the match or submatch converted to a string type.
1699 * @param __sub indicates the submatch.
1700 * @pre ready() == true
1702 * This function gets the submatch (or match, if @p __sub is
1703 * zero) extracted from the target range and converted to the
1704 * associated string type.
1706 string_type
1707 str(size_type __sub = 0) const
1708 { return (*this)[__sub].str(); }
1711 * @brief Gets a %sub_match reference for the match or submatch.
1712 * @param __sub indicates the submatch.
1713 * @pre ready() == true
1715 * This function gets a reference to the indicated submatch, or
1716 * the entire match if @p __sub is zero.
1718 * If @p __sub >= size() then this function returns a %sub_match with a
1719 * special value indicating no submatch.
1721 const_reference
1722 operator[](size_type __sub) const
1724 _GLIBCXX_DEBUG_ASSERT( ready() );
1725 return __sub < size()
1726 ? _Base_type::operator[](__sub)
1727 : __unmatched_sub<_Bi_iter>();
1731 * @brief Gets a %sub_match representing the match prefix.
1732 * @pre ready() == true
1734 * This function gets a reference to a %sub_match object representing the
1735 * part of the target range between the start of the target range and the
1736 * start of the match.
1738 const_reference
1739 prefix() const
1741 _GLIBCXX_DEBUG_ASSERT( ready() );
1742 return !empty()
1743 ? _Base_type::operator[](_Base_type::size() - 2)
1744 : __unmatched_sub<_Bi_iter>();
1748 * @brief Gets a %sub_match representing the match suffix.
1749 * @pre ready() == true
1751 * This function gets a reference to a %sub_match object representing the
1752 * part of the target range between the end of the match and the end of
1753 * the target range.
1755 const_reference
1756 suffix() const
1758 _GLIBCXX_DEBUG_ASSERT( ready() );
1759 return !empty()
1760 ? _Base_type::operator[](_Base_type::size() - 1)
1761 : __unmatched_sub<_Bi_iter>();
1765 * @brief Gets an iterator to the start of the %sub_match collection.
1767 const_iterator
1768 begin() const
1769 { return _Base_type::begin(); }
1772 * @brief Gets an iterator to the start of the %sub_match collection.
1774 const_iterator
1775 cbegin() const
1776 { return _Base_type::cbegin() + 2; }
1779 * @brief Gets an iterator to one-past-the-end of the collection.
1781 const_iterator
1782 end() const
1783 { return _Base_type::end() - 2; }
1786 * @brief Gets an iterator to one-past-the-end of the collection.
1788 const_iterator
1789 cend() const
1790 { return _Base_type::cend(); }
1792 //@}
1795 * @name 10.4 Formatting
1797 * These functions perform formatted substitution of the matched
1798 * character sequences into their target. The format specifiers and
1799 * escape sequences accepted by these functions are determined by
1800 * their @p flags parameter as documented above.
1802 //@{
1805 * @pre ready() == true
1807 template<typename _Out_iter>
1808 _Out_iter
1809 format(_Out_iter __out, const char_type* __fmt_first,
1810 const char_type* __fmt_last,
1811 match_flag_type __flags = regex_constants::format_default) const;
1814 * @pre ready() == true
1816 template<typename _Out_iter, typename _St, typename _Sa>
1817 _Out_iter
1818 format(_Out_iter __out, const basic_string<char_type, _St, _Sa>& __fmt,
1819 match_flag_type __flags = regex_constants::format_default) const
1821 return format(__out, __fmt.data(), __fmt.data() + __fmt.size(),
1822 __flags);
1826 * @pre ready() == true
1828 template<typename _St, typename _Sa>
1829 basic_string<char_type, _St, _Sa>
1830 format(const basic_string<char_type, _St, _Sa>& __fmt,
1831 match_flag_type __flags = regex_constants::format_default) const
1833 basic_string<char_type, _St, _Sa> __result;
1834 format(std::back_inserter(__result), __fmt, __flags);
1835 return __result;
1839 * @pre ready() == true
1841 string_type
1842 format(const char_type* __fmt,
1843 match_flag_type __flags = regex_constants::format_default) const
1845 string_type __result;
1846 format(std::back_inserter(__result),
1847 __fmt,
1848 __fmt + char_traits<char_type>::length(__fmt),
1849 __flags);
1850 return __result;
1853 //@}
1856 * @name 10.5 Allocator
1858 //@{
1861 * @brief Gets a copy of the allocator.
1863 allocator_type
1864 get_allocator() const
1865 { return _Base_type::get_allocator(); }
1867 //@}
1870 * @name 10.6 Swap
1872 //@{
1875 * @brief Swaps the contents of two match_results.
1877 void
1878 swap(match_results& __that)
1879 { _Base_type::swap(__that); }
1880 //@}
1882 private:
1883 template<typename, typename, typename, bool>
1884 friend class __detail::_Executor;
1886 template<typename, typename, typename>
1887 friend class regex_iterator;
1889 template<typename _Bp, typename _Ap, typename _Cp, typename _Rp,
1890 __detail::_RegexExecutorPolicy, bool>
1891 friend bool
1892 __detail::__regex_algo_impl(_Bp, _Bp, match_results<_Bp, _Ap>&,
1893 const basic_regex<_Cp, _Rp>&,
1894 regex_constants::match_flag_type);
1896 _Bi_iter _M_begin;
1897 bool _M_in_iterator;
1900 typedef match_results<const char*> cmatch;
1901 typedef match_results<string::const_iterator> smatch;
1902 #ifdef _GLIBCXX_USE_WCHAR_T
1903 typedef match_results<const wchar_t*> wcmatch;
1904 typedef match_results<wstring::const_iterator> wsmatch;
1905 #endif
1907 // match_results comparisons
1909 * @brief Compares two match_results for equality.
1910 * @returns true if the two objects refer to the same match,
1911 * false otherwise.
1913 template<typename _Bi_iter, typename _Alloc>
1914 inline bool
1915 operator==(const match_results<_Bi_iter, _Alloc>& __m1,
1916 const match_results<_Bi_iter, _Alloc>& __m2)
1918 if (__m1.ready() != __m2.ready())
1919 return false;
1920 if (!__m1.ready()) // both are not ready
1921 return true;
1922 if (__m1.empty() != __m2.empty())
1923 return false;
1924 if (__m1.empty()) // both are empty
1925 return true;
1926 return __m1.prefix() == __m2.prefix()
1927 && __m1.size() == __m2.size()
1928 && std::equal(__m1.begin(), __m1.end(), __m2.begin())
1929 && __m1.suffix() == __m2.suffix();
1933 * @brief Compares two match_results for inequality.
1934 * @returns true if the two objects do not refer to the same match,
1935 * false otherwise.
1937 template<typename _Bi_iter, class _Alloc>
1938 inline bool
1939 operator!=(const match_results<_Bi_iter, _Alloc>& __m1,
1940 const match_results<_Bi_iter, _Alloc>& __m2)
1941 { return !(__m1 == __m2); }
1943 // [7.10.6] match_results swap
1945 * @brief Swaps two match results.
1946 * @param __lhs A match result.
1947 * @param __rhs A match result.
1949 * The contents of the two match_results objects are swapped.
1951 template<typename _Bi_iter, typename _Alloc>
1952 inline void
1953 swap(match_results<_Bi_iter, _Alloc>& __lhs,
1954 match_results<_Bi_iter, _Alloc>& __rhs)
1955 { __lhs.swap(__rhs); }
1957 // [7.11.2] Function template regex_match
1959 * @name Matching, Searching, and Replacing
1961 //@{
1964 * @brief Determines if there is a match between the regular expression @p e
1965 * and all of the character sequence [first, last).
1967 * @param __s Start of the character sequence to match.
1968 * @param __e One-past-the-end of the character sequence to match.
1969 * @param __m The match results.
1970 * @param __re The regular expression.
1971 * @param __flags Controls how the regular expression is matched.
1973 * @retval true A match exists.
1974 * @retval false Otherwise.
1976 * @throws an exception of type regex_error.
1978 template<typename _Bi_iter, typename _Alloc,
1979 typename _Ch_type, typename _Rx_traits>
1980 inline bool
1981 regex_match(_Bi_iter __s,
1982 _Bi_iter __e,
1983 match_results<_Bi_iter, _Alloc>& __m,
1984 const basic_regex<_Ch_type, _Rx_traits>& __re,
1985 regex_constants::match_flag_type __flags
1986 = regex_constants::match_default)
1988 return __detail::__regex_algo_impl<_Bi_iter, _Alloc, _Ch_type, _Rx_traits,
1989 __detail::_RegexExecutorPolicy::_S_auto, true>
1990 (__s, __e, __m, __re, __flags);
1994 * @brief Indicates if there is a match between the regular expression @p e
1995 * and all of the character sequence [first, last).
1997 * @param __first Beginning of the character sequence to match.
1998 * @param __last One-past-the-end of the character sequence to match.
1999 * @param __re The regular expression.
2000 * @param __flags Controls how the regular expression is matched.
2002 * @retval true A match exists.
2003 * @retval false Otherwise.
2005 * @throws an exception of type regex_error.
2007 template<typename _Bi_iter, typename _Ch_type, typename _Rx_traits>
2008 inline bool
2009 regex_match(_Bi_iter __first, _Bi_iter __last,
2010 const basic_regex<_Ch_type, _Rx_traits>& __re,
2011 regex_constants::match_flag_type __flags
2012 = regex_constants::match_default)
2014 match_results<_Bi_iter> __what;
2015 return regex_match(__first, __last, __what, __re, __flags);
2019 * @brief Determines if there is a match between the regular expression @p e
2020 * and a C-style null-terminated string.
2022 * @param __s The C-style null-terminated string to match.
2023 * @param __m The match results.
2024 * @param __re The regular expression.
2025 * @param __f Controls how the regular expression is matched.
2027 * @retval true A match exists.
2028 * @retval false Otherwise.
2030 * @throws an exception of type regex_error.
2032 template<typename _Ch_type, typename _Alloc, typename _Rx_traits>
2033 inline bool
2034 regex_match(const _Ch_type* __s,
2035 match_results<const _Ch_type*, _Alloc>& __m,
2036 const basic_regex<_Ch_type, _Rx_traits>& __re,
2037 regex_constants::match_flag_type __f
2038 = regex_constants::match_default)
2039 { return regex_match(__s, __s + _Rx_traits::length(__s), __m, __re, __f); }
2042 * @brief Determines if there is a match between the regular expression @p e
2043 * and a string.
2045 * @param __s The string to match.
2046 * @param __m The match results.
2047 * @param __re The regular expression.
2048 * @param __flags Controls how the regular expression is matched.
2050 * @retval true A match exists.
2051 * @retval false Otherwise.
2053 * @throws an exception of type regex_error.
2055 template<typename _Ch_traits, typename _Ch_alloc,
2056 typename _Alloc, typename _Ch_type, typename _Rx_traits>
2057 inline bool
2058 regex_match(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>& __s,
2059 match_results<typename basic_string<_Ch_type,
2060 _Ch_traits, _Ch_alloc>::const_iterator, _Alloc>& __m,
2061 const basic_regex<_Ch_type, _Rx_traits>& __re,
2062 regex_constants::match_flag_type __flags
2063 = regex_constants::match_default)
2064 { return regex_match(__s.begin(), __s.end(), __m, __re, __flags); }
2066 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2067 // 2329. regex_match() with match_results should forbid temporary strings
2068 /// Prevent unsafe attempts to get match_results from a temporary string.
2069 template<typename _Ch_traits, typename _Ch_alloc,
2070 typename _Alloc, typename _Ch_type, typename _Rx_traits>
2071 bool
2072 regex_match(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>&&,
2073 match_results<typename basic_string<_Ch_type,
2074 _Ch_traits, _Ch_alloc>::const_iterator, _Alloc>&,
2075 const basic_regex<_Ch_type, _Rx_traits>&,
2076 regex_constants::match_flag_type
2077 = regex_constants::match_default) = delete;
2080 * @brief Indicates if there is a match between the regular expression @p e
2081 * and a C-style null-terminated string.
2083 * @param __s The C-style null-terminated string to match.
2084 * @param __re The regular expression.
2085 * @param __f Controls how the regular expression is matched.
2087 * @retval true A match exists.
2088 * @retval false Otherwise.
2090 * @throws an exception of type regex_error.
2092 template<typename _Ch_type, class _Rx_traits>
2093 inline bool
2094 regex_match(const _Ch_type* __s,
2095 const basic_regex<_Ch_type, _Rx_traits>& __re,
2096 regex_constants::match_flag_type __f
2097 = regex_constants::match_default)
2098 { return regex_match(__s, __s + _Rx_traits::length(__s), __re, __f); }
2101 * @brief Indicates if there is a match between the regular expression @p e
2102 * and a string.
2104 * @param __s [IN] The string to match.
2105 * @param __re [IN] The regular expression.
2106 * @param __flags [IN] Controls how the regular expression is matched.
2108 * @retval true A match exists.
2109 * @retval false Otherwise.
2111 * @throws an exception of type regex_error.
2113 template<typename _Ch_traits, typename _Str_allocator,
2114 typename _Ch_type, typename _Rx_traits>
2115 inline bool
2116 regex_match(const basic_string<_Ch_type, _Ch_traits, _Str_allocator>& __s,
2117 const basic_regex<_Ch_type, _Rx_traits>& __re,
2118 regex_constants::match_flag_type __flags
2119 = regex_constants::match_default)
2120 { return regex_match(__s.begin(), __s.end(), __re, __flags); }
2122 // [7.11.3] Function template regex_search
2124 * Searches for a regular expression within a range.
2125 * @param __s [IN] The start of the string to search.
2126 * @param __e [IN] One-past-the-end of the string to search.
2127 * @param __m [OUT] The match results.
2128 * @param __re [IN] The regular expression to search for.
2129 * @param __flags [IN] Search policy flags.
2130 * @retval true A match was found within the string.
2131 * @retval false No match was found within the string, the content of %m is
2132 * undefined.
2134 * @throws an exception of type regex_error.
2136 template<typename _Bi_iter, typename _Alloc,
2137 typename _Ch_type, typename _Rx_traits>
2138 inline bool
2139 regex_search(_Bi_iter __s, _Bi_iter __e,
2140 match_results<_Bi_iter, _Alloc>& __m,
2141 const basic_regex<_Ch_type, _Rx_traits>& __re,
2142 regex_constants::match_flag_type __flags
2143 = regex_constants::match_default)
2145 return __detail::__regex_algo_impl<_Bi_iter, _Alloc, _Ch_type, _Rx_traits,
2146 __detail::_RegexExecutorPolicy::_S_auto, false>
2147 (__s, __e, __m, __re, __flags);
2151 * Searches for a regular expression within a range.
2152 * @param __first [IN] The start of the string to search.
2153 * @param __last [IN] One-past-the-end of the string to search.
2154 * @param __re [IN] The regular expression to search for.
2155 * @param __flags [IN] Search policy flags.
2156 * @retval true A match was found within the string.
2157 * @retval false No match was found within the string.
2159 * @throws an exception of type regex_error.
2161 template<typename _Bi_iter, typename _Ch_type, typename _Rx_traits>
2162 inline bool
2163 regex_search(_Bi_iter __first, _Bi_iter __last,
2164 const basic_regex<_Ch_type, _Rx_traits>& __re,
2165 regex_constants::match_flag_type __flags
2166 = regex_constants::match_default)
2168 match_results<_Bi_iter> __what;
2169 return regex_search(__first, __last, __what, __re, __flags);
2173 * @brief Searches for a regular expression within a C-string.
2174 * @param __s [IN] A C-string to search for the regex.
2175 * @param __m [OUT] The set of regex matches.
2176 * @param __e [IN] The regex to search for in @p s.
2177 * @param __f [IN] The search flags.
2178 * @retval true A match was found within the string.
2179 * @retval false No match was found within the string, the content of %m is
2180 * undefined.
2182 * @throws an exception of type regex_error.
2184 template<typename _Ch_type, class _Alloc, class _Rx_traits>
2185 inline bool
2186 regex_search(const _Ch_type* __s,
2187 match_results<const _Ch_type*, _Alloc>& __m,
2188 const basic_regex<_Ch_type, _Rx_traits>& __e,
2189 regex_constants::match_flag_type __f
2190 = regex_constants::match_default)
2191 { return regex_search(__s, __s + _Rx_traits::length(__s), __m, __e, __f); }
2194 * @brief Searches for a regular expression within a C-string.
2195 * @param __s [IN] The C-string to search.
2196 * @param __e [IN] The regular expression to search for.
2197 * @param __f [IN] Search policy flags.
2198 * @retval true A match was found within the string.
2199 * @retval false No match was found within the string.
2201 * @throws an exception of type regex_error.
2203 template<typename _Ch_type, typename _Rx_traits>
2204 inline bool
2205 regex_search(const _Ch_type* __s,
2206 const basic_regex<_Ch_type, _Rx_traits>& __e,
2207 regex_constants::match_flag_type __f
2208 = regex_constants::match_default)
2209 { return regex_search(__s, __s + _Rx_traits::length(__s), __e, __f); }
2212 * @brief Searches for a regular expression within a string.
2213 * @param __s [IN] The string to search.
2214 * @param __e [IN] The regular expression to search for.
2215 * @param __flags [IN] Search policy flags.
2216 * @retval true A match was found within the string.
2217 * @retval false No match was found within the string.
2219 * @throws an exception of type regex_error.
2221 template<typename _Ch_traits, typename _String_allocator,
2222 typename _Ch_type, typename _Rx_traits>
2223 inline bool
2224 regex_search(const basic_string<_Ch_type, _Ch_traits,
2225 _String_allocator>& __s,
2226 const basic_regex<_Ch_type, _Rx_traits>& __e,
2227 regex_constants::match_flag_type __flags
2228 = regex_constants::match_default)
2229 { return regex_search(__s.begin(), __s.end(), __e, __flags); }
2232 * @brief Searches for a regular expression within a string.
2233 * @param __s [IN] A C++ string to search for the regex.
2234 * @param __m [OUT] The set of regex matches.
2235 * @param __e [IN] The regex to search for in @p s.
2236 * @param __f [IN] The search flags.
2237 * @retval true A match was found within the string.
2238 * @retval false No match was found within the string, the content of %m is
2239 * undefined.
2241 * @throws an exception of type regex_error.
2243 template<typename _Ch_traits, typename _Ch_alloc,
2244 typename _Alloc, typename _Ch_type,
2245 typename _Rx_traits>
2246 inline bool
2247 regex_search(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>& __s,
2248 match_results<typename basic_string<_Ch_type,
2249 _Ch_traits, _Ch_alloc>::const_iterator, _Alloc>& __m,
2250 const basic_regex<_Ch_type, _Rx_traits>& __e,
2251 regex_constants::match_flag_type __f
2252 = regex_constants::match_default)
2253 { return regex_search(__s.begin(), __s.end(), __m, __e, __f); }
2255 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2256 // 2329. regex_search() with match_results should forbid temporary strings
2257 /// Prevent unsafe attempts to get match_results from a temporary string.
2258 template<typename _Ch_traits, typename _Ch_alloc,
2259 typename _Alloc, typename _Ch_type,
2260 typename _Rx_traits>
2261 bool
2262 regex_search(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>&&,
2263 match_results<typename basic_string<_Ch_type,
2264 _Ch_traits, _Ch_alloc>::const_iterator, _Alloc>&,
2265 const basic_regex<_Ch_type, _Rx_traits>&,
2266 regex_constants::match_flag_type
2267 = regex_constants::match_default) = delete;
2269 // std [28.11.4] Function template regex_replace
2271 * @brief Search for a regular expression within a range for multiple times,
2272 and replace the matched parts through filling a format string.
2273 * @param __out [OUT] The output iterator.
2274 * @param __first [IN] The start of the string to search.
2275 * @param __last [IN] One-past-the-end of the string to search.
2276 * @param __e [IN] The regular expression to search for.
2277 * @param __fmt [IN] The format string.
2278 * @param __flags [IN] Search and replace policy flags.
2280 * @returns __out
2281 * @throws an exception of type regex_error.
2283 template<typename _Out_iter, typename _Bi_iter,
2284 typename _Rx_traits, typename _Ch_type,
2285 typename _St, typename _Sa>
2286 inline _Out_iter
2287 regex_replace(_Out_iter __out, _Bi_iter __first, _Bi_iter __last,
2288 const basic_regex<_Ch_type, _Rx_traits>& __e,
2289 const basic_string<_Ch_type, _St, _Sa>& __fmt,
2290 regex_constants::match_flag_type __flags
2291 = regex_constants::match_default)
2293 return regex_replace(__out, __first, __last, __e, __fmt.c_str(), __flags);
2297 * @brief Search for a regular expression within a range for multiple times,
2298 and replace the matched parts through filling a format C-string.
2299 * @param __out [OUT] The output iterator.
2300 * @param __first [IN] The start of the string to search.
2301 * @param __last [IN] One-past-the-end of the string to search.
2302 * @param __e [IN] The regular expression to search for.
2303 * @param __fmt [IN] The format C-string.
2304 * @param __flags [IN] Search and replace policy flags.
2306 * @returns __out
2307 * @throws an exception of type regex_error.
2309 template<typename _Out_iter, typename _Bi_iter,
2310 typename _Rx_traits, typename _Ch_type>
2311 _Out_iter
2312 regex_replace(_Out_iter __out, _Bi_iter __first, _Bi_iter __last,
2313 const basic_regex<_Ch_type, _Rx_traits>& __e,
2314 const _Ch_type* __fmt,
2315 regex_constants::match_flag_type __flags
2316 = regex_constants::match_default);
2319 * @brief Search for a regular expression within a string for multiple times,
2320 and replace the matched parts through filling a format string.
2321 * @param __s [IN] The string to search and replace.
2322 * @param __e [IN] The regular expression to search for.
2323 * @param __fmt [IN] The format string.
2324 * @param __flags [IN] Search and replace policy flags.
2326 * @returns The string after replacing.
2327 * @throws an exception of type regex_error.
2329 template<typename _Rx_traits, typename _Ch_type,
2330 typename _St, typename _Sa, typename _Fst, typename _Fsa>
2331 inline basic_string<_Ch_type, _St, _Sa>
2332 regex_replace(const basic_string<_Ch_type, _St, _Sa>& __s,
2333 const basic_regex<_Ch_type, _Rx_traits>& __e,
2334 const basic_string<_Ch_type, _Fst, _Fsa>& __fmt,
2335 regex_constants::match_flag_type __flags
2336 = regex_constants::match_default)
2338 basic_string<_Ch_type, _St, _Sa> __result;
2339 regex_replace(std::back_inserter(__result),
2340 __s.begin(), __s.end(), __e, __fmt, __flags);
2341 return __result;
2345 * @brief Search for a regular expression within a string for multiple times,
2346 and replace the matched parts through filling a format C-string.
2347 * @param __s [IN] The string to search and replace.
2348 * @param __e [IN] The regular expression to search for.
2349 * @param __fmt [IN] The format C-string.
2350 * @param __flags [IN] Search and replace policy flags.
2352 * @returns The string after replacing.
2353 * @throws an exception of type regex_error.
2355 template<typename _Rx_traits, typename _Ch_type,
2356 typename _St, typename _Sa>
2357 inline basic_string<_Ch_type, _St, _Sa>
2358 regex_replace(const basic_string<_Ch_type, _St, _Sa>& __s,
2359 const basic_regex<_Ch_type, _Rx_traits>& __e,
2360 const _Ch_type* __fmt,
2361 regex_constants::match_flag_type __flags
2362 = regex_constants::match_default)
2364 basic_string<_Ch_type, _St, _Sa> __result;
2365 regex_replace(std::back_inserter(__result),
2366 __s.begin(), __s.end(), __e, __fmt, __flags);
2367 return __result;
2371 * @brief Search for a regular expression within a C-string for multiple
2372 times, and replace the matched parts through filling a format string.
2373 * @param __s [IN] The C-string to search and replace.
2374 * @param __e [IN] The regular expression to search for.
2375 * @param __fmt [IN] The format string.
2376 * @param __flags [IN] Search and replace policy flags.
2378 * @returns The string after replacing.
2379 * @throws an exception of type regex_error.
2381 template<typename _Rx_traits, typename _Ch_type,
2382 typename _St, typename _Sa>
2383 inline basic_string<_Ch_type>
2384 regex_replace(const _Ch_type* __s,
2385 const basic_regex<_Ch_type, _Rx_traits>& __e,
2386 const basic_string<_Ch_type, _St, _Sa>& __fmt,
2387 regex_constants::match_flag_type __flags
2388 = regex_constants::match_default)
2390 basic_string<_Ch_type> __result;
2391 regex_replace(std::back_inserter(__result), __s,
2392 __s + char_traits<_Ch_type>::length(__s),
2393 __e, __fmt, __flags);
2394 return __result;
2398 * @brief Search for a regular expression within a C-string for multiple
2399 times, and replace the matched parts through filling a format C-string.
2400 * @param __s [IN] The C-string to search and replace.
2401 * @param __e [IN] The regular expression to search for.
2402 * @param __fmt [IN] The format C-string.
2403 * @param __flags [IN] Search and replace policy flags.
2405 * @returns The string after replacing.
2406 * @throws an exception of type regex_error.
2408 template<typename _Rx_traits, typename _Ch_type>
2409 inline basic_string<_Ch_type>
2410 regex_replace(const _Ch_type* __s,
2411 const basic_regex<_Ch_type, _Rx_traits>& __e,
2412 const _Ch_type* __fmt,
2413 regex_constants::match_flag_type __flags
2414 = regex_constants::match_default)
2416 basic_string<_Ch_type> __result;
2417 regex_replace(std::back_inserter(__result), __s,
2418 __s + char_traits<_Ch_type>::length(__s),
2419 __e, __fmt, __flags);
2420 return __result;
2423 //@}
2425 // std [28.12] Class template regex_iterator
2427 * An iterator adaptor that will provide repeated calls of regex_search over
2428 * a range until no more matches remain.
2430 template<typename _Bi_iter,
2431 typename _Ch_type = typename iterator_traits<_Bi_iter>::value_type,
2432 typename _Rx_traits = regex_traits<_Ch_type> >
2433 class regex_iterator
2435 public:
2436 typedef basic_regex<_Ch_type, _Rx_traits> regex_type;
2437 typedef match_results<_Bi_iter> value_type;
2438 typedef std::ptrdiff_t difference_type;
2439 typedef const value_type* pointer;
2440 typedef const value_type& reference;
2441 typedef std::forward_iterator_tag iterator_category;
2444 * @brief Provides a singular iterator, useful for indicating
2445 * one-past-the-end of a range.
2447 regex_iterator()
2448 : _M_match()
2452 * Constructs a %regex_iterator...
2453 * @param __a [IN] The start of a text range to search.
2454 * @param __b [IN] One-past-the-end of the text range to search.
2455 * @param __re [IN] The regular expression to match.
2456 * @param __m [IN] Policy flags for match rules.
2458 regex_iterator(_Bi_iter __a, _Bi_iter __b, const regex_type& __re,
2459 regex_constants::match_flag_type __m
2460 = regex_constants::match_default)
2461 : _M_begin(__a), _M_end(__b), _M_pregex(&__re), _M_flags(__m), _M_match()
2463 if (!regex_search(_M_begin, _M_end, _M_match, *_M_pregex, _M_flags))
2464 *this = regex_iterator();
2467 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2468 // 2332. regex_iterator should forbid temporary regexes
2469 regex_iterator(_Bi_iter, _Bi_iter, const regex_type&&,
2470 regex_constants::match_flag_type
2471 = regex_constants::match_default) = delete;
2473 * Copy constructs a %regex_iterator.
2475 regex_iterator(const regex_iterator& __rhs) = default;
2478 * @brief Assigns one %regex_iterator to another.
2480 regex_iterator&
2481 operator=(const regex_iterator& __rhs) = default;
2484 * @brief Tests the equivalence of two regex iterators.
2486 bool
2487 operator==(const regex_iterator& __rhs) const;
2490 * @brief Tests the inequivalence of two regex iterators.
2492 bool
2493 operator!=(const regex_iterator& __rhs) const
2494 { return !(*this == __rhs); }
2497 * @brief Dereferences a %regex_iterator.
2499 const value_type&
2500 operator*() const
2501 { return _M_match; }
2504 * @brief Selects a %regex_iterator member.
2506 const value_type*
2507 operator->() const
2508 { return &_M_match; }
2511 * @brief Increments a %regex_iterator.
2513 regex_iterator&
2514 operator++();
2517 * @brief Postincrements a %regex_iterator.
2519 regex_iterator
2520 operator++(int)
2522 auto __tmp = *this;
2523 ++(*this);
2524 return __tmp;
2527 private:
2528 _Bi_iter _M_begin;
2529 _Bi_iter _M_end;
2530 const regex_type* _M_pregex;
2531 regex_constants::match_flag_type _M_flags;
2532 match_results<_Bi_iter> _M_match;
2535 typedef regex_iterator<const char*> cregex_iterator;
2536 typedef regex_iterator<string::const_iterator> sregex_iterator;
2537 #ifdef _GLIBCXX_USE_WCHAR_T
2538 typedef regex_iterator<const wchar_t*> wcregex_iterator;
2539 typedef regex_iterator<wstring::const_iterator> wsregex_iterator;
2540 #endif
2542 // [7.12.2] Class template regex_token_iterator
2544 * Iterates over submatches in a range (or @a splits a text string).
2546 * The purpose of this iterator is to enumerate all, or all specified,
2547 * matches of a regular expression within a text range. The dereferenced
2548 * value of an iterator of this class is a std::sub_match object.
2550 template<typename _Bi_iter,
2551 typename _Ch_type = typename iterator_traits<_Bi_iter>::value_type,
2552 typename _Rx_traits = regex_traits<_Ch_type> >
2553 class regex_token_iterator
2555 public:
2556 typedef basic_regex<_Ch_type, _Rx_traits> regex_type;
2557 typedef sub_match<_Bi_iter> value_type;
2558 typedef std::ptrdiff_t difference_type;
2559 typedef const value_type* pointer;
2560 typedef const value_type& reference;
2561 typedef std::forward_iterator_tag iterator_category;
2563 public:
2565 * @brief Default constructs a %regex_token_iterator.
2567 * A default-constructed %regex_token_iterator is a singular iterator
2568 * that will compare equal to the one-past-the-end value for any
2569 * iterator of the same type.
2571 regex_token_iterator()
2572 : _M_position(), _M_subs(), _M_suffix(), _M_n(0), _M_result(nullptr),
2573 _M_has_m1(false)
2577 * Constructs a %regex_token_iterator...
2578 * @param __a [IN] The start of the text to search.
2579 * @param __b [IN] One-past-the-end of the text to search.
2580 * @param __re [IN] The regular expression to search for.
2581 * @param __submatch [IN] Which submatch to return. There are some
2582 * special values for this parameter:
2583 * - -1 each enumerated subexpression does NOT
2584 * match the regular expression (aka field
2585 * splitting)
2586 * - 0 the entire string matching the
2587 * subexpression is returned for each match
2588 * within the text.
2589 * - >0 enumerates only the indicated
2590 * subexpression from a match within the text.
2591 * @param __m [IN] Policy flags for match rules.
2593 regex_token_iterator(_Bi_iter __a, _Bi_iter __b, const regex_type& __re,
2594 int __submatch = 0,
2595 regex_constants::match_flag_type __m
2596 = regex_constants::match_default)
2597 : _M_position(__a, __b, __re, __m), _M_subs(1, __submatch), _M_n(0)
2598 { _M_init(__a, __b); }
2601 * Constructs a %regex_token_iterator...
2602 * @param __a [IN] The start of the text to search.
2603 * @param __b [IN] One-past-the-end of the text to search.
2604 * @param __re [IN] The regular expression to search for.
2605 * @param __submatches [IN] A list of subexpressions to return for each
2606 * regular expression match within the text.
2607 * @param __m [IN] Policy flags for match rules.
2609 regex_token_iterator(_Bi_iter __a, _Bi_iter __b,
2610 const regex_type& __re,
2611 const std::vector<int>& __submatches,
2612 regex_constants::match_flag_type __m
2613 = regex_constants::match_default)
2614 : _M_position(__a, __b, __re, __m), _M_subs(__submatches), _M_n(0)
2615 { _M_init(__a, __b); }
2618 * Constructs a %regex_token_iterator...
2619 * @param __a [IN] The start of the text to search.
2620 * @param __b [IN] One-past-the-end of the text to search.
2621 * @param __re [IN] The regular expression to search for.
2622 * @param __submatches [IN] A list of subexpressions to return for each
2623 * regular expression match within the text.
2624 * @param __m [IN] Policy flags for match rules.
2626 regex_token_iterator(_Bi_iter __a, _Bi_iter __b,
2627 const regex_type& __re,
2628 initializer_list<int> __submatches,
2629 regex_constants::match_flag_type __m
2630 = regex_constants::match_default)
2631 : _M_position(__a, __b, __re, __m), _M_subs(__submatches), _M_n(0)
2632 { _M_init(__a, __b); }
2635 * Constructs a %regex_token_iterator...
2636 * @param __a [IN] The start of the text to search.
2637 * @param __b [IN] One-past-the-end of the text to search.
2638 * @param __re [IN] The regular expression to search for.
2639 * @param __submatches [IN] A list of subexpressions to return for each
2640 * regular expression match within the text.
2641 * @param __m [IN] Policy flags for match rules.
2643 template<std::size_t _Nm>
2644 regex_token_iterator(_Bi_iter __a, _Bi_iter __b,
2645 const regex_type& __re,
2646 const int (&__submatches)[_Nm],
2647 regex_constants::match_flag_type __m
2648 = regex_constants::match_default)
2649 : _M_position(__a, __b, __re, __m),
2650 _M_subs(__submatches, *(&__submatches+1)), _M_n(0)
2651 { _M_init(__a, __b); }
2653 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2654 // 2332. regex_token_iterator should forbid temporary regexes
2655 regex_token_iterator(_Bi_iter, _Bi_iter, const regex_type&&, int = 0,
2656 regex_constants::match_flag_type =
2657 regex_constants::match_default) = delete;
2658 regex_token_iterator(_Bi_iter, _Bi_iter, const regex_type&&,
2659 const std::vector<int>&,
2660 regex_constants::match_flag_type =
2661 regex_constants::match_default) = delete;
2662 regex_token_iterator(_Bi_iter, _Bi_iter, const regex_type&&,
2663 initializer_list<int>,
2664 regex_constants::match_flag_type =
2665 regex_constants::match_default) = delete;
2666 template <std::size_t N>
2667 regex_token_iterator(_Bi_iter, _Bi_iter, const regex_type&&,
2668 const int (&)[N],
2669 regex_constants::match_flag_type =
2670 regex_constants::match_default) = delete;
2673 * @brief Copy constructs a %regex_token_iterator.
2674 * @param __rhs [IN] A %regex_token_iterator to copy.
2676 regex_token_iterator(const regex_token_iterator& __rhs)
2677 : _M_position(__rhs._M_position), _M_subs(__rhs._M_subs),
2678 _M_suffix(__rhs._M_suffix), _M_n(__rhs._M_n), _M_result(__rhs._M_result),
2679 _M_has_m1(__rhs._M_has_m1)
2681 if (__rhs._M_result == &__rhs._M_suffix)
2682 _M_result = &_M_suffix;
2686 * @brief Assigns a %regex_token_iterator to another.
2687 * @param __rhs [IN] A %regex_token_iterator to copy.
2689 regex_token_iterator&
2690 operator=(const regex_token_iterator& __rhs);
2693 * @brief Compares a %regex_token_iterator to another for equality.
2695 bool
2696 operator==(const regex_token_iterator& __rhs) const;
2699 * @brief Compares a %regex_token_iterator to another for inequality.
2701 bool
2702 operator!=(const regex_token_iterator& __rhs) const
2703 { return !(*this == __rhs); }
2706 * @brief Dereferences a %regex_token_iterator.
2708 const value_type&
2709 operator*() const
2710 { return *_M_result; }
2713 * @brief Selects a %regex_token_iterator member.
2715 const value_type*
2716 operator->() const
2717 { return _M_result; }
2720 * @brief Increments a %regex_token_iterator.
2722 regex_token_iterator&
2723 operator++();
2726 * @brief Postincrements a %regex_token_iterator.
2728 regex_token_iterator
2729 operator++(int)
2731 auto __tmp = *this;
2732 ++(*this);
2733 return __tmp;
2736 private:
2737 typedef regex_iterator<_Bi_iter, _Ch_type, _Rx_traits> _Position;
2739 void
2740 _M_init(_Bi_iter __a, _Bi_iter __b);
2742 const value_type&
2743 _M_current_match() const
2745 if (_M_subs[_M_n] == -1)
2746 return (*_M_position).prefix();
2747 else
2748 return (*_M_position)[_M_subs[_M_n]];
2751 constexpr bool
2752 _M_end_of_seq() const
2753 { return _M_result == nullptr; }
2755 _Position _M_position;
2756 std::vector<int> _M_subs;
2757 value_type _M_suffix;
2758 std::size_t _M_n;
2759 const value_type* _M_result;
2761 // Show whether _M_subs contains -1
2762 bool _M_has_m1;
2765 /** @brief Token iterator for C-style NULL-terminated strings. */
2766 typedef regex_token_iterator<const char*> cregex_token_iterator;
2768 /** @brief Token iterator for standard strings. */
2769 typedef regex_token_iterator<string::const_iterator> sregex_token_iterator;
2771 #ifdef _GLIBCXX_USE_WCHAR_T
2772 /** @brief Token iterator for C-style NULL-terminated wide strings. */
2773 typedef regex_token_iterator<const wchar_t*> wcregex_token_iterator;
2775 /** @brief Token iterator for standard wide-character strings. */
2776 typedef regex_token_iterator<wstring::const_iterator> wsregex_token_iterator;
2777 #endif
2779 //@} // group regex
2780 _GLIBCXX_END_NAMESPACE_VERSION
2781 } // namespace
2783 #include <bits/regex.tcc>