Use ctype_base::blank in regex_traits.
[official-gcc.git] / libstdc++-v3 / include / bits / regex.h
blob30189e3645fff008f393aa18cdd92ae81ede62ce
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)
1009 { return __rhs.compare(__lhs.c_str()) == 0; }
1012 * @brief Tests the inequivalence of a string and a regular expression
1013 * submatch.
1014 * @param __lhs A string.
1015 * @param __rhs A regular expression submatch.
1016 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1018 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1019 inline bool
1020 operator!=(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1021 const sub_match<_Bi_iter>& __rhs)
1022 { return !(__lhs == __rhs); }
1025 * @brief Tests the ordering of a string and a regular expression submatch.
1026 * @param __lhs A string.
1027 * @param __rhs A regular expression submatch.
1028 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1030 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1031 inline bool
1032 operator<(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1033 const sub_match<_Bi_iter>& __rhs)
1034 { return __rhs.compare(__lhs.c_str()) > 0; }
1037 * @brief Tests the ordering of a string and a regular expression submatch.
1038 * @param __lhs A string.
1039 * @param __rhs A regular expression submatch.
1040 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1042 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1043 inline bool
1044 operator>(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1045 const sub_match<_Bi_iter>& __rhs)
1046 { return __rhs < __lhs; }
1049 * @brief Tests the ordering of a string and a regular expression submatch.
1050 * @param __lhs A string.
1051 * @param __rhs A regular expression submatch.
1052 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1054 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1055 inline bool
1056 operator>=(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1057 const sub_match<_Bi_iter>& __rhs)
1058 { return !(__lhs < __rhs); }
1061 * @brief Tests the ordering of a string and a regular expression submatch.
1062 * @param __lhs A string.
1063 * @param __rhs A regular expression submatch.
1064 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1066 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1067 inline bool
1068 operator<=(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1069 const sub_match<_Bi_iter>& __rhs)
1070 { return !(__rhs < __lhs); }
1073 * @brief Tests the equivalence of a regular expression submatch and a
1074 * string.
1075 * @param __lhs A regular expression submatch.
1076 * @param __rhs A string.
1077 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1079 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1080 inline bool
1081 operator==(const sub_match<_Bi_iter>& __lhs,
1082 const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1083 { return __lhs.compare(__rhs.c_str()) == 0; }
1086 * @brief Tests the inequivalence of a regular expression submatch and a
1087 * string.
1088 * @param __lhs A regular expression submatch.
1089 * @param __rhs A string.
1090 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1092 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1093 inline bool
1094 operator!=(const sub_match<_Bi_iter>& __lhs,
1095 const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1096 { return !(__lhs == __rhs); }
1099 * @brief Tests the ordering of a regular expression submatch and a string.
1100 * @param __lhs A regular expression submatch.
1101 * @param __rhs A string.
1102 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1104 template<typename _Bi_iter, class _Ch_traits, class _Ch_alloc>
1105 inline bool
1106 operator<(const sub_match<_Bi_iter>& __lhs,
1107 const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1108 { return __lhs.compare(__rhs.c_str()) < 0; }
1111 * @brief Tests the ordering of a regular expression submatch and a string.
1112 * @param __lhs A regular expression submatch.
1113 * @param __rhs A string.
1114 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1116 template<typename _Bi_iter, class _Ch_traits, class _Ch_alloc>
1117 inline bool
1118 operator>(const sub_match<_Bi_iter>& __lhs,
1119 const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1120 { return __rhs < __lhs; }
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 does not precede @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 !(__lhs < __rhs); }
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 succeed @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 !(__rhs < __lhs); }
1147 * @brief Tests the equivalence of a C string and a regular expression
1148 * submatch.
1149 * @param __lhs A C string.
1150 * @param __rhs A regular expression submatch.
1151 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1153 template<typename _Bi_iter>
1154 inline bool
1155 operator==(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1156 const sub_match<_Bi_iter>& __rhs)
1157 { return __rhs.compare(__lhs) == 0; }
1160 * @brief Tests the inequivalence of an iterator value and a regular
1161 * expression submatch.
1162 * @param __lhs A regular expression submatch.
1163 * @param __rhs A string.
1164 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1166 template<typename _Bi_iter>
1167 inline bool
1168 operator!=(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1169 const sub_match<_Bi_iter>& __rhs)
1170 { return !(__lhs == __rhs); }
1173 * @brief Tests the ordering of a string and a regular expression submatch.
1174 * @param __lhs A string.
1175 * @param __rhs A regular expression submatch.
1176 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
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 __rhs.compare(__lhs) > 0; }
1185 * @brief Tests the ordering of a string and a regular expression submatch.
1186 * @param __lhs A string.
1187 * @param __rhs A regular expression submatch.
1188 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
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 < __lhs; }
1197 * @brief Tests the ordering of a string and a regular expression submatch.
1198 * @param __lhs A string.
1199 * @param __rhs A regular expression submatch.
1200 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
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 !(__lhs < __rhs); }
1209 * @brief Tests the ordering of a string and a regular expression submatch.
1210 * @param __lhs A string.
1211 * @param __rhs A regular expression submatch.
1212 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
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 !(__rhs < __lhs); }
1221 * @brief Tests the equivalence of a regular expression submatch and a
1222 * string.
1223 * @param __lhs A regular expression submatch.
1224 * @param __rhs A pointer to a string?
1225 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1227 template<typename _Bi_iter>
1228 inline bool
1229 operator==(const sub_match<_Bi_iter>& __lhs,
1230 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1231 { return __lhs.compare(__rhs) == 0; }
1234 * @brief Tests the inequivalence of a regular expression submatch and a
1235 * string.
1236 * @param __lhs A regular expression submatch.
1237 * @param __rhs A pointer to a string.
1238 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1240 template<typename _Bi_iter>
1241 inline bool
1242 operator!=(const sub_match<_Bi_iter>& __lhs,
1243 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1244 { return !(__lhs == __rhs); }
1247 * @brief Tests the ordering of a regular expression submatch and a string.
1248 * @param __lhs A regular expression submatch.
1249 * @param __rhs A string.
1250 * @returns true if @a __lhs precedes @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.compare(__rhs) < 0; }
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 succeeds @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 __rhs < __lhs; }
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 does not precede @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 !(__lhs < __rhs); }
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 succeed @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 !(__rhs < __lhs); }
1295 * @brief Tests the equivalence of a string and a regular expression
1296 * submatch.
1297 * @param __lhs A string.
1298 * @param __rhs A regular expression submatch.
1299 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1301 template<typename _Bi_iter>
1302 inline bool
1303 operator==(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1304 const sub_match<_Bi_iter>& __rhs)
1306 typedef typename sub_match<_Bi_iter>::string_type string_type;
1307 return __rhs.compare(string_type(1, __lhs)) == 0;
1311 * @brief Tests the inequivalence of a string and a regular expression
1312 * submatch.
1313 * @param __lhs A string.
1314 * @param __rhs A regular expression submatch.
1315 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1317 template<typename _Bi_iter>
1318 inline bool
1319 operator!=(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1320 const sub_match<_Bi_iter>& __rhs)
1321 { return !(__lhs == __rhs); }
1324 * @brief Tests the ordering of a string and a regular expression submatch.
1325 * @param __lhs A string.
1326 * @param __rhs A regular expression submatch.
1327 * @returns true if @a __lhs precedes @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)
1334 typedef typename sub_match<_Bi_iter>::string_type string_type;
1335 return __rhs.compare(string_type(1, __lhs)) > 0;
1339 * @brief Tests the ordering of a string and a regular expression submatch.
1340 * @param __lhs A string.
1341 * @param __rhs A regular expression submatch.
1342 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1344 template<typename _Bi_iter>
1345 inline bool
1346 operator>(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1347 const sub_match<_Bi_iter>& __rhs)
1348 { return __rhs < __lhs; }
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 does not precede @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 !(__lhs < __rhs); }
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 succeed @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 !(__rhs < __lhs); }
1375 * @brief Tests the equivalence of a regular expression submatch and a
1376 * string.
1377 * @param __lhs A regular expression submatch.
1378 * @param __rhs A const string reference.
1379 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1381 template<typename _Bi_iter>
1382 inline bool
1383 operator==(const sub_match<_Bi_iter>& __lhs,
1384 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1386 typedef typename sub_match<_Bi_iter>::string_type string_type;
1387 return __lhs.compare(string_type(1, __rhs)) == 0;
1391 * @brief Tests the inequivalence of a regular expression submatch and a
1392 * string.
1393 * @param __lhs A regular expression submatch.
1394 * @param __rhs A const string reference.
1395 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1397 template<typename _Bi_iter>
1398 inline bool
1399 operator!=(const sub_match<_Bi_iter>& __lhs,
1400 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1401 { return !(__lhs == __rhs); }
1404 * @brief Tests the ordering of a regular expression submatch and a string.
1405 * @param __lhs A regular expression submatch.
1406 * @param __rhs A const string reference.
1407 * @returns true if @a __lhs precedes @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)
1414 typedef typename sub_match<_Bi_iter>::string_type string_type;
1415 return __lhs.compare(string_type(1, __rhs)) < 0;
1419 * @brief Tests the ordering of a regular expression submatch and a string.
1420 * @param __lhs A regular expression submatch.
1421 * @param __rhs A const string reference.
1422 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1424 template<typename _Bi_iter>
1425 inline bool
1426 operator>(const sub_match<_Bi_iter>& __lhs,
1427 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1428 { return __rhs < __lhs; }
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 does not precede @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 !(__lhs < __rhs); }
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 succeed @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 !(__rhs < __lhs); }
1455 * @brief Inserts a matched string into an output stream.
1457 * @param __os The output stream.
1458 * @param __m A submatch string.
1460 * @returns the output stream with the submatch string inserted.
1462 template<typename _Ch_type, typename _Ch_traits, typename _Bi_iter>
1463 inline
1464 basic_ostream<_Ch_type, _Ch_traits>&
1465 operator<<(basic_ostream<_Ch_type, _Ch_traits>& __os,
1466 const sub_match<_Bi_iter>& __m)
1467 { return __os << __m.str(); }
1469 // [7.10] Class template match_results
1472 * Special sub_match object representing an unmatched sub-expression.
1474 template<typename _Bi_iter>
1475 inline const sub_match<_Bi_iter>&
1476 __unmatched_sub()
1478 static const sub_match<_Bi_iter> __unmatched = sub_match<_Bi_iter>();
1479 return __unmatched;
1483 * @brief The results of a match or search operation.
1485 * A collection of character sequences representing the result of a regular
1486 * expression match. Storage for the collection is allocated and freed as
1487 * necessary by the member functions of class template match_results.
1489 * This class satisfies the Sequence requirements, with the exception that
1490 * only the operations defined for a const-qualified Sequence are supported.
1492 * The sub_match object stored at index 0 represents sub-expression 0, i.e.
1493 * the whole match. In this case the %sub_match member matched is always true.
1494 * The sub_match object stored at index n denotes what matched the marked
1495 * sub-expression n within the matched expression. If the sub-expression n
1496 * participated in a regular expression match then the %sub_match member
1497 * matched evaluates to true, and members first and second denote the range
1498 * of characters [first, second) which formed that match. Otherwise matched
1499 * is false, and members first and second point to the end of the sequence
1500 * that was searched.
1502 * @nosubgrouping
1504 template<typename _Bi_iter,
1505 typename _Alloc = allocator<sub_match<_Bi_iter> > >
1506 class match_results
1507 : private std::vector<sub_match<_Bi_iter>, _Alloc>
1509 private:
1511 * The vector base is empty if this does not represent a successful match.
1512 * Otherwise it contains n+3 elements where n is the number of marked
1513 * sub-expressions:
1514 * [0] entire match
1515 * [1] 1st marked subexpression
1516 * ...
1517 * [n] nth marked subexpression
1518 * [n+1] prefix
1519 * [n+2] suffix
1521 typedef std::vector<sub_match<_Bi_iter>, _Alloc> _Base_type;
1522 typedef std::iterator_traits<_Bi_iter> __iter_traits;
1523 typedef regex_constants::match_flag_type match_flag_type;
1525 public:
1527 * @name 10.? Public Types
1529 //@{
1530 typedef sub_match<_Bi_iter> value_type;
1531 typedef const value_type& const_reference;
1532 typedef const_reference reference;
1533 typedef typename _Base_type::const_iterator const_iterator;
1534 typedef const_iterator iterator;
1535 typedef typename __iter_traits::difference_type difference_type;
1536 typedef typename allocator_traits<_Alloc>::size_type size_type;
1537 typedef _Alloc allocator_type;
1538 typedef typename __iter_traits::value_type char_type;
1539 typedef std::basic_string<char_type> string_type;
1540 //@}
1542 public:
1544 * @name 28.10.1 Construction, Copying, and Destruction
1546 //@{
1549 * @brief Constructs a default %match_results container.
1550 * @post size() returns 0 and str() returns an empty string.
1552 explicit
1553 match_results(const _Alloc& __a = _Alloc())
1554 : _Base_type(__a), _M_in_iterator(false)
1558 * @brief Copy constructs a %match_results.
1560 match_results(const match_results& __rhs)
1561 : _Base_type(__rhs), _M_in_iterator(false)
1565 * @brief Move constructs a %match_results.
1567 match_results(match_results&& __rhs) noexcept
1568 : _Base_type(std::move(__rhs)), _M_in_iterator(false)
1572 * @brief Assigns rhs to *this.
1574 match_results&
1575 operator=(const match_results& __rhs)
1577 match_results(__rhs).swap(*this);
1578 return *this;
1582 * @brief Move-assigns rhs to *this.
1584 match_results&
1585 operator=(match_results&& __rhs)
1587 match_results(std::move(__rhs)).swap(*this);
1588 return *this;
1592 * @brief Destroys a %match_results object.
1594 ~match_results()
1597 //@}
1599 // 28.10.2, state:
1601 * @brief Indicates if the %match_results is ready.
1602 * @retval true The object has a fully-established result state.
1603 * @retval false The object is not ready.
1605 bool ready() const { return !_Base_type::empty(); }
1608 * @name 28.10.2 Size
1610 //@{
1613 * @brief Gets the number of matches and submatches.
1615 * The number of matches for a given regular expression will be either 0
1616 * if there was no match or mark_count() + 1 if a match was successful.
1617 * Some matches may be empty.
1619 * @returns the number of matches found.
1621 size_type
1622 size() const
1624 size_type __size = _Base_type::size();
1625 return (__size && _Base_type::operator[](0).matched) ? __size - 2 : 0;
1628 size_type
1629 max_size() const
1630 { return _Base_type::max_size(); }
1633 * @brief Indicates if the %match_results contains no results.
1634 * @retval true The %match_results object is empty.
1635 * @retval false The %match_results object is not empty.
1637 bool
1638 empty() const
1639 { return size() == 0; }
1641 //@}
1644 * @name 10.3 Element Access
1646 //@{
1649 * @brief Gets the length of the indicated submatch.
1650 * @param __sub indicates the submatch.
1651 * @pre ready() == true
1653 * This function returns the length of the indicated submatch, or the
1654 * length of the entire match if @p __sub is zero (the default).
1656 difference_type
1657 length(size_type __sub = 0) const
1658 { return (*this)[__sub].length(); }
1661 * @brief Gets the offset of the beginning of the indicated submatch.
1662 * @param __sub indicates the submatch.
1663 * @pre ready() == true
1665 * This function returns the offset from the beginning of the target
1666 * sequence to the beginning of the submatch, unless the value of @p __sub
1667 * is zero (the default), in which case this function returns the offset
1668 * from the beginning of the target sequence to the beginning of the
1669 * match.
1671 * Returns -1 if @p __sub is out of range.
1673 difference_type
1674 position(size_type __sub = 0) const
1676 // [28.12.1.4.5]
1677 if (_M_in_iterator)
1678 return __sub < size() ? std::distance(_M_begin,
1679 (*this)[__sub].first) : -1;
1680 else
1681 return __sub < size() ? std::distance(this->prefix().first,
1682 (*this)[__sub].first) : -1;
1686 * @brief Gets the match or submatch converted to a string type.
1687 * @param __sub indicates the submatch.
1688 * @pre ready() == true
1690 * This function gets the submatch (or match, if @p __sub is
1691 * zero) extracted from the target range and converted to the
1692 * associated string type.
1694 string_type
1695 str(size_type __sub = 0) const
1696 { return (*this)[__sub].str(); }
1699 * @brief Gets a %sub_match reference for the match or submatch.
1700 * @param __sub indicates the submatch.
1701 * @pre ready() == true
1703 * This function gets a reference to the indicated submatch, or
1704 * the entire match if @p __sub is zero.
1706 * If @p __sub >= size() then this function returns a %sub_match with a
1707 * special value indicating no submatch.
1709 const_reference
1710 operator[](size_type __sub) const
1712 _GLIBCXX_DEBUG_ASSERT( ready() );
1713 return __sub < size()
1714 ? _Base_type::operator[](__sub)
1715 : __unmatched_sub<_Bi_iter>();
1719 * @brief Gets a %sub_match representing the match prefix.
1720 * @pre ready() == true
1722 * This function gets a reference to a %sub_match object representing the
1723 * part of the target range between the start of the target range and the
1724 * start of the match.
1726 const_reference
1727 prefix() const
1729 _GLIBCXX_DEBUG_ASSERT( ready() );
1730 return !empty()
1731 ? _Base_type::operator[](_Base_type::size() - 2)
1732 : __unmatched_sub<_Bi_iter>();
1736 * @brief Gets a %sub_match representing the match suffix.
1737 * @pre ready() == true
1739 * This function gets a reference to a %sub_match object representing the
1740 * part of the target range between the end of the match and the end of
1741 * the target range.
1743 const_reference
1744 suffix() const
1746 _GLIBCXX_DEBUG_ASSERT( ready() );
1747 return !empty()
1748 ? _Base_type::operator[](_Base_type::size() - 1)
1749 : __unmatched_sub<_Bi_iter>();
1753 * @brief Gets an iterator to the start of the %sub_match collection.
1755 const_iterator
1756 begin() const
1757 { return _Base_type::begin(); }
1760 * @brief Gets an iterator to the start of the %sub_match collection.
1762 const_iterator
1763 cbegin() const
1764 { return _Base_type::cbegin() + 2; }
1767 * @brief Gets an iterator to one-past-the-end of the collection.
1769 const_iterator
1770 end() const
1771 { return _Base_type::end() - 2; }
1774 * @brief Gets an iterator to one-past-the-end of the collection.
1776 const_iterator
1777 cend() const
1778 { return _Base_type::cend(); }
1780 //@}
1783 * @name 10.4 Formatting
1785 * These functions perform formatted substitution of the matched
1786 * character sequences into their target. The format specifiers and
1787 * escape sequences accepted by these functions are determined by
1788 * their @p flags parameter as documented above.
1790 //@{
1793 * @pre ready() == true
1795 template<typename _Out_iter>
1796 _Out_iter
1797 format(_Out_iter __out, const char_type* __fmt_first,
1798 const char_type* __fmt_last,
1799 match_flag_type __flags = regex_constants::format_default) const;
1802 * @pre ready() == true
1804 template<typename _Out_iter, typename _St, typename _Sa>
1805 _Out_iter
1806 format(_Out_iter __out, const basic_string<char_type, _St, _Sa>& __fmt,
1807 match_flag_type __flags = regex_constants::format_default) const
1809 return format(__out, __fmt.data(), __fmt.data() + __fmt.size(),
1810 __flags);
1814 * @pre ready() == true
1816 template<typename _St, typename _Sa>
1817 basic_string<char_type, _St, _Sa>
1818 format(const basic_string<char_type, _St, _Sa>& __fmt,
1819 match_flag_type __flags = regex_constants::format_default) const
1821 basic_string<char_type, _St, _Sa> __result;
1822 format(std::back_inserter(__result), __fmt, __flags);
1823 return __result;
1827 * @pre ready() == true
1829 string_type
1830 format(const char_type* __fmt,
1831 match_flag_type __flags = regex_constants::format_default) const
1833 string_type __result;
1834 format(std::back_inserter(__result),
1835 __fmt,
1836 __fmt + char_traits<char_type>::length(__fmt),
1837 __flags);
1838 return __result;
1841 //@}
1844 * @name 10.5 Allocator
1846 //@{
1849 * @brief Gets a copy of the allocator.
1851 allocator_type
1852 get_allocator() const
1853 { return _Base_type::get_allocator(); }
1855 //@}
1858 * @name 10.6 Swap
1860 //@{
1863 * @brief Swaps the contents of two match_results.
1865 void
1866 swap(match_results& __that)
1867 { _Base_type::swap(__that); }
1868 //@}
1870 private:
1871 template<typename, typename, typename, bool>
1872 friend class __detail::_Executor;
1874 template<typename, typename, typename>
1875 friend class regex_iterator;
1877 template<typename _Bp, typename _Ap, typename _Cp, typename _Rp,
1878 __detail::_RegexExecutorPolicy, bool>
1879 friend bool
1880 __detail::__regex_algo_impl(_Bp, _Bp, match_results<_Bp, _Ap>&,
1881 const basic_regex<_Cp, _Rp>&,
1882 regex_constants::match_flag_type);
1884 _Bi_iter _M_begin;
1885 bool _M_in_iterator;
1888 typedef match_results<const char*> cmatch;
1889 typedef match_results<string::const_iterator> smatch;
1890 #ifdef _GLIBCXX_USE_WCHAR_T
1891 typedef match_results<const wchar_t*> wcmatch;
1892 typedef match_results<wstring::const_iterator> wsmatch;
1893 #endif
1895 // match_results comparisons
1897 * @brief Compares two match_results for equality.
1898 * @returns true if the two objects refer to the same match,
1899 * false otherwise.
1901 template<typename _Bi_iter, typename _Alloc>
1902 inline bool
1903 operator==(const match_results<_Bi_iter, _Alloc>& __m1,
1904 const match_results<_Bi_iter, _Alloc>& __m2)
1906 if (__m1.ready() != __m2.ready())
1907 return false;
1908 if (!__m1.ready()) // both are not ready
1909 return true;
1910 if (__m1.empty() != __m2.empty())
1911 return false;
1912 if (__m1.empty()) // both are empty
1913 return true;
1914 return __m1.prefix() == __m2.prefix()
1915 && __m1.size() == __m2.size()
1916 && std::equal(__m1.begin(), __m1.end(), __m2.begin())
1917 && __m1.suffix() == __m2.suffix();
1921 * @brief Compares two match_results for inequality.
1922 * @returns true if the two objects do not refer to the same match,
1923 * false otherwise.
1925 template<typename _Bi_iter, class _Alloc>
1926 inline bool
1927 operator!=(const match_results<_Bi_iter, _Alloc>& __m1,
1928 const match_results<_Bi_iter, _Alloc>& __m2)
1929 { return !(__m1 == __m2); }
1931 // [7.10.6] match_results swap
1933 * @brief Swaps two match results.
1934 * @param __lhs A match result.
1935 * @param __rhs A match result.
1937 * The contents of the two match_results objects are swapped.
1939 template<typename _Bi_iter, typename _Alloc>
1940 inline void
1941 swap(match_results<_Bi_iter, _Alloc>& __lhs,
1942 match_results<_Bi_iter, _Alloc>& __rhs)
1943 { __lhs.swap(__rhs); }
1945 // [7.11.2] Function template regex_match
1947 * @name Matching, Searching, and Replacing
1949 //@{
1952 * @brief Determines if there is a match between the regular expression @p e
1953 * and all of the character sequence [first, last).
1955 * @param __s Start of the character sequence to match.
1956 * @param __e One-past-the-end of the character sequence to match.
1957 * @param __m The match results.
1958 * @param __re The regular expression.
1959 * @param __flags Controls how the regular expression is matched.
1961 * @retval true A match exists.
1962 * @retval false Otherwise.
1964 * @throws an exception of type regex_error.
1966 template<typename _Bi_iter, typename _Alloc,
1967 typename _Ch_type, typename _Rx_traits>
1968 inline bool
1969 regex_match(_Bi_iter __s,
1970 _Bi_iter __e,
1971 match_results<_Bi_iter, _Alloc>& __m,
1972 const basic_regex<_Ch_type, _Rx_traits>& __re,
1973 regex_constants::match_flag_type __flags
1974 = regex_constants::match_default)
1976 return __detail::__regex_algo_impl<_Bi_iter, _Alloc, _Ch_type, _Rx_traits,
1977 __detail::_RegexExecutorPolicy::_S_auto, true>
1978 (__s, __e, __m, __re, __flags);
1982 * @brief Indicates if there is a match between the regular expression @p e
1983 * and all of the character sequence [first, last).
1985 * @param __first Beginning of the character sequence to match.
1986 * @param __last One-past-the-end of the character sequence to match.
1987 * @param __re The regular expression.
1988 * @param __flags Controls how the regular expression is matched.
1990 * @retval true A match exists.
1991 * @retval false Otherwise.
1993 * @throws an exception of type regex_error.
1995 template<typename _Bi_iter, typename _Ch_type, typename _Rx_traits>
1996 inline bool
1997 regex_match(_Bi_iter __first, _Bi_iter __last,
1998 const basic_regex<_Ch_type, _Rx_traits>& __re,
1999 regex_constants::match_flag_type __flags
2000 = regex_constants::match_default)
2002 match_results<_Bi_iter> __what;
2003 return regex_match(__first, __last, __what, __re, __flags);
2007 * @brief Determines if there is a match between the regular expression @p e
2008 * and a C-style null-terminated string.
2010 * @param __s The C-style null-terminated string to match.
2011 * @param __m The match results.
2012 * @param __re The regular expression.
2013 * @param __f Controls how the regular expression is matched.
2015 * @retval true A match exists.
2016 * @retval false Otherwise.
2018 * @throws an exception of type regex_error.
2020 template<typename _Ch_type, typename _Alloc, typename _Rx_traits>
2021 inline bool
2022 regex_match(const _Ch_type* __s,
2023 match_results<const _Ch_type*, _Alloc>& __m,
2024 const basic_regex<_Ch_type, _Rx_traits>& __re,
2025 regex_constants::match_flag_type __f
2026 = regex_constants::match_default)
2027 { return regex_match(__s, __s + _Rx_traits::length(__s), __m, __re, __f); }
2030 * @brief Determines if there is a match between the regular expression @p e
2031 * and a string.
2033 * @param __s The string to match.
2034 * @param __m The match results.
2035 * @param __re The regular expression.
2036 * @param __flags Controls how the regular expression is matched.
2038 * @retval true A match exists.
2039 * @retval false Otherwise.
2041 * @throws an exception of type regex_error.
2043 template<typename _Ch_traits, typename _Ch_alloc,
2044 typename _Alloc, typename _Ch_type, typename _Rx_traits>
2045 inline bool
2046 regex_match(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>& __s,
2047 match_results<typename basic_string<_Ch_type,
2048 _Ch_traits, _Ch_alloc>::const_iterator, _Alloc>& __m,
2049 const basic_regex<_Ch_type, _Rx_traits>& __re,
2050 regex_constants::match_flag_type __flags
2051 = regex_constants::match_default)
2052 { return regex_match(__s.begin(), __s.end(), __m, __re, __flags); }
2055 * @brief Indicates if there is a match between the regular expression @p e
2056 * and a C-style null-terminated string.
2058 * @param __s The C-style null-terminated string to match.
2059 * @param __re The regular expression.
2060 * @param __f Controls how the regular expression is matched.
2062 * @retval true A match exists.
2063 * @retval false Otherwise.
2065 * @throws an exception of type regex_error.
2067 template<typename _Ch_type, class _Rx_traits>
2068 inline bool
2069 regex_match(const _Ch_type* __s,
2070 const basic_regex<_Ch_type, _Rx_traits>& __re,
2071 regex_constants::match_flag_type __f
2072 = regex_constants::match_default)
2073 { return regex_match(__s, __s + _Rx_traits::length(__s), __re, __f); }
2076 * @brief Indicates if there is a match between the regular expression @p e
2077 * and a string.
2079 * @param __s [IN] The string to match.
2080 * @param __re [IN] The regular expression.
2081 * @param __flags [IN] Controls how the regular expression is matched.
2083 * @retval true A match exists.
2084 * @retval false Otherwise.
2086 * @throws an exception of type regex_error.
2088 template<typename _Ch_traits, typename _Str_allocator,
2089 typename _Ch_type, typename _Rx_traits>
2090 inline bool
2091 regex_match(const basic_string<_Ch_type, _Ch_traits, _Str_allocator>& __s,
2092 const basic_regex<_Ch_type, _Rx_traits>& __re,
2093 regex_constants::match_flag_type __flags
2094 = regex_constants::match_default)
2095 { return regex_match(__s.begin(), __s.end(), __re, __flags); }
2097 // [7.11.3] Function template regex_search
2099 * Searches for a regular expression within a range.
2100 * @param __s [IN] The start of the string to search.
2101 * @param __e [IN] One-past-the-end of the string to search.
2102 * @param __m [OUT] The match results.
2103 * @param __re [IN] The regular expression to search for.
2104 * @param __flags [IN] Search policy flags.
2105 * @retval true A match was found within the string.
2106 * @retval false No match was found within the string, the content of %m is
2107 * undefined.
2109 * @throws an exception of type regex_error.
2111 template<typename _Bi_iter, typename _Alloc,
2112 typename _Ch_type, typename _Rx_traits>
2113 inline bool
2114 regex_search(_Bi_iter __s, _Bi_iter __e,
2115 match_results<_Bi_iter, _Alloc>& __m,
2116 const basic_regex<_Ch_type, _Rx_traits>& __re,
2117 regex_constants::match_flag_type __flags
2118 = regex_constants::match_default)
2120 return __detail::__regex_algo_impl<_Bi_iter, _Alloc, _Ch_type, _Rx_traits,
2121 __detail::_RegexExecutorPolicy::_S_auto, false>
2122 (__s, __e, __m, __re, __flags);
2126 * Searches for a regular expression within a range.
2127 * @param __first [IN] The start of the string to search.
2128 * @param __last [IN] One-past-the-end of the string to search.
2129 * @param __re [IN] The regular expression to search for.
2130 * @param __flags [IN] Search policy flags.
2131 * @retval true A match was found within the string.
2132 * @retval false No match was found within the string.
2134 * @throws an exception of type regex_error.
2136 template<typename _Bi_iter, typename _Ch_type, typename _Rx_traits>
2137 inline bool
2138 regex_search(_Bi_iter __first, _Bi_iter __last,
2139 const basic_regex<_Ch_type, _Rx_traits>& __re,
2140 regex_constants::match_flag_type __flags
2141 = regex_constants::match_default)
2143 match_results<_Bi_iter> __what;
2144 return regex_search(__first, __last, __what, __re, __flags);
2148 * @brief Searches for a regular expression within a C-string.
2149 * @param __s [IN] A C-string to search for the regex.
2150 * @param __m [OUT] The set of regex matches.
2151 * @param __e [IN] The regex to search for in @p s.
2152 * @param __f [IN] The search flags.
2153 * @retval true A match was found within the string.
2154 * @retval false No match was found within the string, the content of %m is
2155 * undefined.
2157 * @throws an exception of type regex_error.
2159 template<typename _Ch_type, class _Alloc, class _Rx_traits>
2160 inline bool
2161 regex_search(const _Ch_type* __s,
2162 match_results<const _Ch_type*, _Alloc>& __m,
2163 const basic_regex<_Ch_type, _Rx_traits>& __e,
2164 regex_constants::match_flag_type __f
2165 = regex_constants::match_default)
2166 { return regex_search(__s, __s + _Rx_traits::length(__s), __m, __e, __f); }
2169 * @brief Searches for a regular expression within a C-string.
2170 * @param __s [IN] The C-string to search.
2171 * @param __e [IN] The regular expression to search for.
2172 * @param __f [IN] Search policy flags.
2173 * @retval true A match was found within the string.
2174 * @retval false No match was found within the string.
2176 * @throws an exception of type regex_error.
2178 template<typename _Ch_type, typename _Rx_traits>
2179 inline bool
2180 regex_search(const _Ch_type* __s,
2181 const basic_regex<_Ch_type, _Rx_traits>& __e,
2182 regex_constants::match_flag_type __f
2183 = regex_constants::match_default)
2184 { return regex_search(__s, __s + _Rx_traits::length(__s), __e, __f); }
2187 * @brief Searches for a regular expression within a string.
2188 * @param __s [IN] The string to search.
2189 * @param __e [IN] The regular expression to search for.
2190 * @param __flags [IN] Search policy flags.
2191 * @retval true A match was found within the string.
2192 * @retval false No match was found within the string.
2194 * @throws an exception of type regex_error.
2196 template<typename _Ch_traits, typename _String_allocator,
2197 typename _Ch_type, typename _Rx_traits>
2198 inline bool
2199 regex_search(const basic_string<_Ch_type, _Ch_traits,
2200 _String_allocator>& __s,
2201 const basic_regex<_Ch_type, _Rx_traits>& __e,
2202 regex_constants::match_flag_type __flags
2203 = regex_constants::match_default)
2204 { return regex_search(__s.begin(), __s.end(), __e, __flags); }
2207 * @brief Searches for a regular expression within a string.
2208 * @param __s [IN] A C++ string to search for the regex.
2209 * @param __m [OUT] The set of regex matches.
2210 * @param __e [IN] The regex to search for in @p s.
2211 * @param __f [IN] The search flags.
2212 * @retval true A match was found within the string.
2213 * @retval false No match was found within the string, the content of %m is
2214 * undefined.
2216 * @throws an exception of type regex_error.
2218 template<typename _Ch_traits, typename _Ch_alloc,
2219 typename _Alloc, typename _Ch_type,
2220 typename _Rx_traits>
2221 inline bool
2222 regex_search(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>& __s,
2223 match_results<typename basic_string<_Ch_type,
2224 _Ch_traits, _Ch_alloc>::const_iterator, _Alloc>& __m,
2225 const basic_regex<_Ch_type, _Rx_traits>& __e,
2226 regex_constants::match_flag_type __f
2227 = regex_constants::match_default)
2228 { return regex_search(__s.begin(), __s.end(), __m, __e, __f); }
2230 // std [28.11.4] Function template regex_replace
2232 * @brief Search for a regular expression within a range for multiple times,
2233 and replace the matched parts through filling a format string.
2234 * @param __out [OUT] The output iterator.
2235 * @param __first [IN] The start of the string to search.
2236 * @param __last [IN] One-past-the-end of the string to search.
2237 * @param __e [IN] The regular expression to search for.
2238 * @param __fmt [IN] The format string.
2239 * @param __flags [IN] Search and replace policy flags.
2241 * @returns __out
2242 * @throws an exception of type regex_error.
2244 template<typename _Out_iter, typename _Bi_iter,
2245 typename _Rx_traits, typename _Ch_type,
2246 typename _St, typename _Sa>
2247 inline _Out_iter
2248 regex_replace(_Out_iter __out, _Bi_iter __first, _Bi_iter __last,
2249 const basic_regex<_Ch_type, _Rx_traits>& __e,
2250 const basic_string<_Ch_type, _St, _Sa>& __fmt,
2251 regex_constants::match_flag_type __flags
2252 = regex_constants::match_default)
2254 return regex_replace(__out, __first, __last, __e, __fmt.c_str(), __flags);
2258 * @brief Search for a regular expression within a range for multiple times,
2259 and replace the matched parts through filling a format C-string.
2260 * @param __out [OUT] The output iterator.
2261 * @param __first [IN] The start of the string to search.
2262 * @param __last [IN] One-past-the-end of the string to search.
2263 * @param __e [IN] The regular expression to search for.
2264 * @param __fmt [IN] The format C-string.
2265 * @param __flags [IN] Search and replace policy flags.
2267 * @returns __out
2268 * @throws an exception of type regex_error.
2270 template<typename _Out_iter, typename _Bi_iter,
2271 typename _Rx_traits, typename _Ch_type>
2272 _Out_iter
2273 regex_replace(_Out_iter __out, _Bi_iter __first, _Bi_iter __last,
2274 const basic_regex<_Ch_type, _Rx_traits>& __e,
2275 const _Ch_type* __fmt,
2276 regex_constants::match_flag_type __flags
2277 = regex_constants::match_default);
2280 * @brief Search for a regular expression within a string for multiple times,
2281 and replace the matched parts through filling a format string.
2282 * @param __s [IN] The string to search and replace.
2283 * @param __e [IN] The regular expression to search for.
2284 * @param __fmt [IN] The format string.
2285 * @param __flags [IN] Search and replace policy flags.
2287 * @returns The string after replacing.
2288 * @throws an exception of type regex_error.
2290 template<typename _Rx_traits, typename _Ch_type,
2291 typename _St, typename _Sa, typename _Fst, typename _Fsa>
2292 inline basic_string<_Ch_type, _St, _Sa>
2293 regex_replace(const basic_string<_Ch_type, _St, _Sa>& __s,
2294 const basic_regex<_Ch_type, _Rx_traits>& __e,
2295 const basic_string<_Ch_type, _Fst, _Fsa>& __fmt,
2296 regex_constants::match_flag_type __flags
2297 = regex_constants::match_default)
2299 basic_string<_Ch_type, _St, _Sa> __result;
2300 regex_replace(std::back_inserter(__result),
2301 __s.begin(), __s.end(), __e, __fmt, __flags);
2302 return __result;
2306 * @brief Search for a regular expression within a string for multiple times,
2307 and replace the matched parts through filling a format C-string.
2308 * @param __s [IN] The string to search and replace.
2309 * @param __e [IN] The regular expression to search for.
2310 * @param __fmt [IN] The format C-string.
2311 * @param __flags [IN] Search and replace policy flags.
2313 * @returns The string after replacing.
2314 * @throws an exception of type regex_error.
2316 template<typename _Rx_traits, typename _Ch_type,
2317 typename _St, typename _Sa>
2318 inline basic_string<_Ch_type, _St, _Sa>
2319 regex_replace(const basic_string<_Ch_type, _St, _Sa>& __s,
2320 const basic_regex<_Ch_type, _Rx_traits>& __e,
2321 const _Ch_type* __fmt,
2322 regex_constants::match_flag_type __flags
2323 = regex_constants::match_default)
2325 basic_string<_Ch_type, _St, _Sa> __result;
2326 regex_replace(std::back_inserter(__result),
2327 __s.begin(), __s.end(), __e, __fmt, __flags);
2328 return __result;
2332 * @brief Search for a regular expression within a C-string for multiple
2333 times, and replace the matched parts through filling a format string.
2334 * @param __s [IN] The C-string to search and replace.
2335 * @param __e [IN] The regular expression to search for.
2336 * @param __fmt [IN] The format string.
2337 * @param __flags [IN] Search and replace policy flags.
2339 * @returns The string after replacing.
2340 * @throws an exception of type regex_error.
2342 template<typename _Rx_traits, typename _Ch_type,
2343 typename _St, typename _Sa>
2344 inline basic_string<_Ch_type>
2345 regex_replace(const _Ch_type* __s,
2346 const basic_regex<_Ch_type, _Rx_traits>& __e,
2347 const basic_string<_Ch_type, _St, _Sa>& __fmt,
2348 regex_constants::match_flag_type __flags
2349 = regex_constants::match_default)
2351 basic_string<_Ch_type> __result;
2352 regex_replace(std::back_inserter(__result), __s,
2353 __s + char_traits<_Ch_type>::length(__s),
2354 __e, __fmt, __flags);
2355 return __result;
2359 * @brief Search for a regular expression within a C-string for multiple
2360 times, and replace the matched parts through filling a format C-string.
2361 * @param __s [IN] The C-string to search and replace.
2362 * @param __e [IN] The regular expression to search for.
2363 * @param __fmt [IN] The format C-string.
2364 * @param __flags [IN] Search and replace policy flags.
2366 * @returns The string after replacing.
2367 * @throws an exception of type regex_error.
2369 template<typename _Rx_traits, typename _Ch_type>
2370 inline basic_string<_Ch_type>
2371 regex_replace(const _Ch_type* __s,
2372 const basic_regex<_Ch_type, _Rx_traits>& __e,
2373 const _Ch_type* __fmt,
2374 regex_constants::match_flag_type __flags
2375 = regex_constants::match_default)
2377 basic_string<_Ch_type> __result;
2378 regex_replace(std::back_inserter(__result), __s,
2379 __s + char_traits<_Ch_type>::length(__s),
2380 __e, __fmt, __flags);
2381 return __result;
2384 //@}
2386 // std [28.12] Class template regex_iterator
2388 * An iterator adaptor that will provide repeated calls of regex_search over
2389 * a range until no more matches remain.
2391 template<typename _Bi_iter,
2392 typename _Ch_type = typename iterator_traits<_Bi_iter>::value_type,
2393 typename _Rx_traits = regex_traits<_Ch_type> >
2394 class regex_iterator
2396 public:
2397 typedef basic_regex<_Ch_type, _Rx_traits> regex_type;
2398 typedef match_results<_Bi_iter> value_type;
2399 typedef std::ptrdiff_t difference_type;
2400 typedef const value_type* pointer;
2401 typedef const value_type& reference;
2402 typedef std::forward_iterator_tag iterator_category;
2405 * @brief Provides a singular iterator, useful for indicating
2406 * one-past-the-end of a range.
2408 regex_iterator()
2409 : _M_match()
2413 * Constructs a %regex_iterator...
2414 * @param __a [IN] The start of a text range to search.
2415 * @param __b [IN] One-past-the-end of the text range to search.
2416 * @param __re [IN] The regular expression to match.
2417 * @param __m [IN] Policy flags for match rules.
2419 regex_iterator(_Bi_iter __a, _Bi_iter __b, const regex_type& __re,
2420 regex_constants::match_flag_type __m
2421 = regex_constants::match_default)
2422 : _M_begin(__a), _M_end(__b), _M_pregex(&__re), _M_flags(__m), _M_match()
2424 if (!regex_search(_M_begin, _M_end, _M_match, *_M_pregex, _M_flags))
2425 *this = regex_iterator();
2429 * Copy constructs a %regex_iterator.
2431 regex_iterator(const regex_iterator& __rhs) = default;
2434 * @brief Assigns one %regex_iterator to another.
2436 regex_iterator&
2437 operator=(const regex_iterator& __rhs) = default;
2440 * @brief Tests the equivalence of two regex iterators.
2442 bool
2443 operator==(const regex_iterator& __rhs) const;
2446 * @brief Tests the inequivalence of two regex iterators.
2448 bool
2449 operator!=(const regex_iterator& __rhs) const
2450 { return !(*this == __rhs); }
2453 * @brief Dereferences a %regex_iterator.
2455 const value_type&
2456 operator*() const
2457 { return _M_match; }
2460 * @brief Selects a %regex_iterator member.
2462 const value_type*
2463 operator->() const
2464 { return &_M_match; }
2467 * @brief Increments a %regex_iterator.
2469 regex_iterator&
2470 operator++();
2473 * @brief Postincrements a %regex_iterator.
2475 regex_iterator
2476 operator++(int)
2478 auto __tmp = *this;
2479 ++(*this);
2480 return __tmp;
2483 private:
2484 _Bi_iter _M_begin;
2485 _Bi_iter _M_end;
2486 const regex_type* _M_pregex;
2487 regex_constants::match_flag_type _M_flags;
2488 match_results<_Bi_iter> _M_match;
2491 typedef regex_iterator<const char*> cregex_iterator;
2492 typedef regex_iterator<string::const_iterator> sregex_iterator;
2493 #ifdef _GLIBCXX_USE_WCHAR_T
2494 typedef regex_iterator<const wchar_t*> wcregex_iterator;
2495 typedef regex_iterator<wstring::const_iterator> wsregex_iterator;
2496 #endif
2498 // [7.12.2] Class template regex_token_iterator
2500 * Iterates over submatches in a range (or @a splits a text string).
2502 * The purpose of this iterator is to enumerate all, or all specified,
2503 * matches of a regular expression within a text range. The dereferenced
2504 * value of an iterator of this class is a std::sub_match object.
2506 template<typename _Bi_iter,
2507 typename _Ch_type = typename iterator_traits<_Bi_iter>::value_type,
2508 typename _Rx_traits = regex_traits<_Ch_type> >
2509 class regex_token_iterator
2511 public:
2512 typedef basic_regex<_Ch_type, _Rx_traits> regex_type;
2513 typedef sub_match<_Bi_iter> value_type;
2514 typedef std::ptrdiff_t difference_type;
2515 typedef const value_type* pointer;
2516 typedef const value_type& reference;
2517 typedef std::forward_iterator_tag iterator_category;
2519 public:
2521 * @brief Default constructs a %regex_token_iterator.
2523 * A default-constructed %regex_token_iterator is a singular iterator
2524 * that will compare equal to the one-past-the-end value for any
2525 * iterator of the same type.
2527 regex_token_iterator()
2528 : _M_position(), _M_subs(), _M_suffix(), _M_n(0), _M_result(nullptr),
2529 _M_has_m1(false)
2533 * Constructs a %regex_token_iterator...
2534 * @param __a [IN] The start of the text to search.
2535 * @param __b [IN] One-past-the-end of the text to search.
2536 * @param __re [IN] The regular expression to search for.
2537 * @param __submatch [IN] Which submatch to return. There are some
2538 * special values for this parameter:
2539 * - -1 each enumerated subexpression does NOT
2540 * match the regular expression (aka field
2541 * splitting)
2542 * - 0 the entire string matching the
2543 * subexpression is returned for each match
2544 * within the text.
2545 * - >0 enumerates only the indicated
2546 * subexpression from a match within the text.
2547 * @param __m [IN] Policy flags for match rules.
2549 regex_token_iterator(_Bi_iter __a, _Bi_iter __b, const regex_type& __re,
2550 int __submatch = 0,
2551 regex_constants::match_flag_type __m
2552 = regex_constants::match_default)
2553 : _M_position(__a, __b, __re, __m), _M_subs(1, __submatch), _M_n(0)
2554 { _M_init(__a, __b); }
2557 * Constructs a %regex_token_iterator...
2558 * @param __a [IN] The start of the text to search.
2559 * @param __b [IN] One-past-the-end of the text to search.
2560 * @param __re [IN] The regular expression to search for.
2561 * @param __submatches [IN] A list of subexpressions to return for each
2562 * regular expression match within the text.
2563 * @param __m [IN] Policy flags for match rules.
2565 regex_token_iterator(_Bi_iter __a, _Bi_iter __b,
2566 const regex_type& __re,
2567 const std::vector<int>& __submatches,
2568 regex_constants::match_flag_type __m
2569 = regex_constants::match_default)
2570 : _M_position(__a, __b, __re, __m), _M_subs(__submatches), _M_n(0)
2571 { _M_init(__a, __b); }
2574 * Constructs a %regex_token_iterator...
2575 * @param __a [IN] The start of the text to search.
2576 * @param __b [IN] One-past-the-end of the text to search.
2577 * @param __re [IN] The regular expression to search for.
2578 * @param __submatches [IN] A list of subexpressions to return for each
2579 * regular expression match within the text.
2580 * @param __m [IN] Policy flags for match rules.
2582 regex_token_iterator(_Bi_iter __a, _Bi_iter __b,
2583 const regex_type& __re,
2584 initializer_list<int> __submatches,
2585 regex_constants::match_flag_type __m
2586 = regex_constants::match_default)
2587 : _M_position(__a, __b, __re, __m), _M_subs(__submatches), _M_n(0)
2588 { _M_init(__a, __b); }
2591 * Constructs a %regex_token_iterator...
2592 * @param __a [IN] The start of the text to search.
2593 * @param __b [IN] One-past-the-end of the text to search.
2594 * @param __re [IN] The regular expression to search for.
2595 * @param __submatches [IN] A list of subexpressions to return for each
2596 * regular expression match within the text.
2597 * @param __m [IN] Policy flags for match rules.
2599 template<std::size_t _Nm>
2600 regex_token_iterator(_Bi_iter __a, _Bi_iter __b,
2601 const regex_type& __re,
2602 const int (&__submatches)[_Nm],
2603 regex_constants::match_flag_type __m
2604 = regex_constants::match_default)
2605 : _M_position(__a, __b, __re, __m),
2606 _M_subs(__submatches, *(&__submatches+1)), _M_n(0)
2607 { _M_init(__a, __b); }
2610 * @brief Copy constructs a %regex_token_iterator.
2611 * @param __rhs [IN] A %regex_token_iterator to copy.
2613 regex_token_iterator(const regex_token_iterator& __rhs)
2614 : _M_position(__rhs._M_position), _M_subs(__rhs._M_subs),
2615 _M_suffix(__rhs._M_suffix), _M_n(__rhs._M_n), _M_result(__rhs._M_result),
2616 _M_has_m1(__rhs._M_has_m1)
2618 if (__rhs._M_result == &__rhs._M_suffix)
2619 _M_result = &_M_suffix;
2623 * @brief Assigns a %regex_token_iterator to another.
2624 * @param __rhs [IN] A %regex_token_iterator to copy.
2626 regex_token_iterator&
2627 operator=(const regex_token_iterator& __rhs);
2630 * @brief Compares a %regex_token_iterator to another for equality.
2632 bool
2633 operator==(const regex_token_iterator& __rhs) const;
2636 * @brief Compares a %regex_token_iterator to another for inequality.
2638 bool
2639 operator!=(const regex_token_iterator& __rhs) const
2640 { return !(*this == __rhs); }
2643 * @brief Dereferences a %regex_token_iterator.
2645 const value_type&
2646 operator*() const
2647 { return *_M_result; }
2650 * @brief Selects a %regex_token_iterator member.
2652 const value_type*
2653 operator->() const
2654 { return _M_result; }
2657 * @brief Increments a %regex_token_iterator.
2659 regex_token_iterator&
2660 operator++();
2663 * @brief Postincrements a %regex_token_iterator.
2665 regex_token_iterator
2666 operator++(int)
2668 auto __tmp = *this;
2669 ++(*this);
2670 return __tmp;
2673 private:
2674 typedef regex_iterator<_Bi_iter, _Ch_type, _Rx_traits> _Position;
2676 void
2677 _M_init(_Bi_iter __a, _Bi_iter __b);
2679 const value_type&
2680 _M_current_match() const
2682 if (_M_subs[_M_n] == -1)
2683 return (*_M_position).prefix();
2684 else
2685 return (*_M_position)[_M_subs[_M_n]];
2688 constexpr bool
2689 _M_end_of_seq() const
2690 { return _M_result == nullptr; }
2692 _Position _M_position;
2693 std::vector<int> _M_subs;
2694 value_type _M_suffix;
2695 std::size_t _M_n;
2696 const value_type* _M_result;
2698 // Show whether _M_subs contains -1
2699 bool _M_has_m1;
2702 /** @brief Token iterator for C-style NULL-terminated strings. */
2703 typedef regex_token_iterator<const char*> cregex_token_iterator;
2705 /** @brief Token iterator for standard strings. */
2706 typedef regex_token_iterator<string::const_iterator> sregex_token_iterator;
2708 #ifdef _GLIBCXX_USE_WCHAR_T
2709 /** @brief Token iterator for C-style NULL-terminated wide strings. */
2710 typedef regex_token_iterator<const wchar_t*> wcregex_token_iterator;
2712 /** @brief Token iterator for standard wide-character strings. */
2713 typedef regex_token_iterator<wstring::const_iterator> wsregex_token_iterator;
2714 #endif
2716 //@} // group regex
2717 _GLIBCXX_END_NAMESPACE_VERSION
2718 } // namespace
2720 #include <bits/regex.tcc>