1 // class template regex -*- C++ -*-
3 // Copyright (C) 2010-2013 Free Software Foundation, Inc.
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)
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/>.
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
41 * @brief Class regex_traits. Describes aspects of a regular expression.
43 * A regular expression traits class that satisfies the requirements of
46 * The class %regex is parameterized around a set of related types and
47 * functions used to complete the definition of its semantics. This class
48 * satisfies the requirements of such a traits class.
50 template<typename _Ch_type
>
54 typedef _Ch_type char_type
;
55 typedef std::basic_string
<char_type
> string_type
;
56 typedef std::locale locale_type
;
60 typedef typename
std::ctype
<char_type
>::mask _BaseType
;
62 unsigned char _M_extended
;
63 static constexpr unsigned char _S_under
= 1 << 0;
64 // FIXME: _S_blank should be removed in the future, when locale's complete.
65 static constexpr unsigned char _S_blank
= 1 << 1;
66 static constexpr unsigned char _S_valid_mask
= 0x3;
68 constexpr _RegexMask(_BaseType __base
= 0,
69 unsigned char __extended
= 0)
70 : _M_base(__base
), _M_extended(__extended
)
74 operator&(_RegexMask __other
) const
76 return _RegexMask(_M_base
& __other
._M_base
,
77 _M_extended
& __other
._M_extended
);
81 operator|(_RegexMask __other
) const
83 return _RegexMask(_M_base
| __other
._M_base
,
84 _M_extended
| __other
._M_extended
);
88 operator^(_RegexMask __other
) const
90 return _RegexMask(_M_base
^ __other
._M_base
,
91 _M_extended
^ __other
._M_extended
);
96 { return _RegexMask(~_M_base
, ~_M_extended
); }
99 operator&=(_RegexMask __other
)
100 { return *this = (*this) & __other
; }
103 operator|=(_RegexMask __other
)
104 { return *this = (*this) | __other
; }
107 operator^=(_RegexMask __other
)
108 { return *this = (*this) ^ __other
; }
111 operator==(_RegexMask __other
) const
113 return (_M_extended
& _S_valid_mask
)
114 == (__other
._M_extended
& _S_valid_mask
)
115 && _M_base
== __other
._M_base
;
119 operator!=(_RegexMask __other
) const
120 { return !((*this) == __other
); }
124 typedef _RegexMask char_class_type
;
128 * @brief Constructs a default traits object.
133 * @brief Gives the length of a C-style string starting at @p __p.
135 * @param __p a pointer to the start of a character sequence.
137 * @returns the number of characters between @p *__p and the first
138 * default-initialized value of type @p char_type. In other words, uses
139 * the C-string algorithm for determining the length of a sequence of
143 length(const char_type
* __p
)
144 { return string_type::traits_type::length(__p
); }
147 * @brief Performs the identity translation.
149 * @param __c A character to the locale-specific character set.
154 translate(char_type __c
) const
158 * @brief Translates a character into a case-insensitive equivalent.
160 * @param __c A character to the locale-specific character set.
162 * @returns the locale-specific lower-case equivalent of __c.
163 * @throws std::bad_cast if the imbued locale does not support the ctype
167 translate_nocase(char_type __c
) const
169 typedef std::ctype
<char_type
> __ctype_type
;
170 const __ctype_type
& __fctyp(use_facet
<__ctype_type
>(_M_locale
));
171 return __fctyp
.tolower(__c
);
175 * @brief Gets a sort key for a character sequence.
177 * @param __first beginning of the character sequence.
178 * @param __last one-past-the-end of the character sequence.
180 * Returns a sort key for the character sequence designated by the
181 * iterator range [F1, F2) such that if the character sequence [G1, G2)
182 * sorts before the character sequence [H1, H2) then
183 * v.transform(G1, G2) < v.transform(H1, H2).
185 * What this really does is provide a more efficient way to compare a
186 * string to multiple other strings in locales with fancy collation
187 * rules and equivalence classes.
189 * @returns a locale-specific sort key equivalent to the input range.
191 * @throws std::bad_cast if the current locale does not have a collate
194 template<typename _Fwd_iter
>
196 transform(_Fwd_iter __first
, _Fwd_iter __last
) const
198 typedef std::collate
<char_type
> __collate_type
;
199 const __collate_type
& __fclt(use_facet
<__collate_type
>(_M_locale
));
200 string_type
__s(__first
, __last
);
201 return __fclt
.transform(__s
.data(), __s
.data() + __s
.size());
205 * @brief Gets a sort key for a character sequence, independent of case.
207 * @param __first beginning of the character sequence.
208 * @param __last one-past-the-end of the character sequence.
210 * Effects: if typeid(use_facet<collate<_Ch_type> >) ==
211 * typeid(collate_byname<_Ch_type>) and the form of the sort key
212 * returned by collate_byname<_Ch_type>::transform(__first, __last)
213 * is known and can be converted into a primary sort key
214 * then returns that key, otherwise returns an empty string.
216 * @todo Implement this function.
218 template<typename _Fwd_iter
>
220 transform_primary(_Fwd_iter __first
, _Fwd_iter __last
) const
224 typedef std::ctype
<char_type
> __ctype_type
;
225 const __ctype_type
& __fctyp(use_facet
<__ctype_type
>(_M_locale
));
226 std::vector
<char_type
> __v(__first
, __last
);
227 // FIXME : this is not entirely correct
228 __fctyp
.tolower(&*__v
.begin(), &*__v
.end());
229 return this->transform(&*__v
.begin(), &*__v
.end());
231 __catch (std::bad_cast
)
234 return string_type();
238 * @brief Gets a collation element by name.
240 * @param __first beginning of the collation element name.
241 * @param __last one-past-the-end of the collation element name.
243 * @returns a sequence of one or more characters that represents the
244 * collating element consisting of the character sequence designated by
245 * the iterator range [__first, __last). Returns an empty string if the
246 * character sequence is not a valid collating element.
248 template<typename _Fwd_iter
>
250 lookup_collatename(_Fwd_iter __first
, _Fwd_iter __last
) const;
253 * @brief Maps one or more characters to a named character
256 * @param __first beginning of the character sequence.
257 * @param __last one-past-the-end of the character sequence.
258 * @param __icase ignores the case of the classification name.
260 * @returns an unspecified value that represents the character
261 * classification named by the character sequence designated by
262 * the iterator range [__first, __last). If @p icase is true,
263 * the returned mask identifies the classification regardless of
264 * the case of the characters to be matched (for example,
265 * [[:lower:]] is the same as [[:alpha:]]), otherwise a
266 * case-dependent classification is returned. The value
267 * returned shall be independent of the case of the characters
268 * in the character sequence. If the name is not recognized then
269 * returns a value that compares equal to 0.
271 * At least the following names (or their wide-character equivalent) are
289 template<typename _Fwd_iter
>
291 lookup_classname(_Fwd_iter __first
, _Fwd_iter __last
,
292 bool __icase
= false) const;
295 * @brief Determines if @p c is a member of an identified class.
297 * @param __c a character.
298 * @param __f a class type (as returned from lookup_classname).
300 * @returns true if the character @p __c is a member of the classification
301 * represented by @p __f, false otherwise.
303 * @throws std::bad_cast if the current locale does not have a ctype
307 isctype(_Ch_type __c
, char_class_type __f
) const;
310 * @brief Converts a digit to an int.
312 * @param __ch a character representing a digit.
313 * @param __radix the radix if the numeric conversion (limited to 8, 10,
316 * @returns the value represented by the digit __ch in base radix if the
317 * character __ch is a valid digit in base radix; otherwise returns -1.
320 value(_Ch_type __ch
, int __radix
) const;
323 * @brief Imbues the regex_traits object with a copy of a new locale.
325 * @param __loc A locale.
327 * @returns a copy of the previous locale in use by the regex_traits
330 * @note Calling imbue with a different locale than the one currently in
331 * use invalidates all cached data held by *this.
334 imbue(locale_type __loc
)
336 std::swap(_M_locale
, __loc
);
341 * @brief Gets a copy of the current locale in use by the regex_traits
346 { return _M_locale
; }
349 locale_type _M_locale
;
352 template<typename _Ch_type
>
353 template<typename _Fwd_iter
>
354 typename regex_traits
<_Ch_type
>::string_type
355 regex_traits
<_Ch_type
>::
356 lookup_collatename(_Fwd_iter __first
, _Fwd_iter __last
) const
358 typedef std::ctype
<char_type
> __ctype_type
;
359 const __ctype_type
& __fctyp(use_facet
<__ctype_type
>(_M_locale
));
361 static const char* __collatenames
[] =
454 "left-square-bracket",
456 "right-square-bracket",
486 "left-curly-bracket",
488 "right-curly-bracket",
495 static const char* __digraphs
[] =
521 std::string
__s(__last
- __first
, '?');
522 __fctyp
.narrow(__first
, __last
, '?', &*__s
.begin());
524 for (unsigned int __i
= 0; *__collatenames
[__i
]; __i
++)
525 if (__s
== __collatenames
[__i
])
526 return string_type(1, __fctyp
.widen((char)__i
));
528 for (unsigned int __i
= 0; *__digraphs
[__i
]; __i
++)
530 const char* __now
= __digraphs
[__i
];
533 string_type
ret(__s
.size(), __fctyp
.widen('?'));
534 __fctyp
.widen(__now
, __now
+ 2/* ouch */, &*ret
.begin());
538 return string_type();
541 template<typename _Ch_type
>
542 template<typename _Fwd_iter
>
543 typename regex_traits
<_Ch_type
>::char_class_type
544 regex_traits
<_Ch_type
>::
545 lookup_classname(_Fwd_iter __first
, _Fwd_iter __last
, bool __icase
) const
547 typedef std::ctype
<char_type
> __ctype_type
;
548 typedef std::ctype
<char> __cctype_type
;
549 typedef const pair
<const char*, char_class_type
> _ClassnameEntry
;
550 const __ctype_type
& __fctyp(use_facet
<__ctype_type
>(_M_locale
));
551 const __cctype_type
& __cctyp(use_facet
<__cctype_type
>(_M_locale
));
553 static _ClassnameEntry __classnames
[] =
555 {"d", ctype_base::digit
},
556 {"w", {ctype_base::alnum
, _RegexMask::_S_under
}},
557 {"s", ctype_base::space
},
558 {"alnum", ctype_base::alnum
},
559 {"alpha", ctype_base::alpha
},
560 {"blank", {0, _RegexMask::_S_blank
}},
561 {"cntrl", ctype_base::cntrl
},
562 {"digit", ctype_base::digit
},
563 {"graph", ctype_base::graph
},
564 {"lower", ctype_base::lower
},
565 {"print", ctype_base::print
},
566 {"punct", ctype_base::punct
},
567 {"space", ctype_base::space
},
568 {"upper", ctype_base::upper
},
569 {"xdigit", ctype_base::xdigit
},
572 std::string
__s(__last
- __first
, '?');
573 __fctyp
.narrow(__first
, __last
, '?', &__s
[0]);
574 __cctyp
.tolower(&*__s
.begin(), &*__s
.end());
575 for (_ClassnameEntry
* __it
= __classnames
;
576 __it
< *(&__classnames
+ 1);
579 if (__s
== __it
->first
)
582 && ((__it
->second
& (ctype_base::lower
| ctype_base::upper
)) != 0))
583 return ctype_base::alpha
;
590 template<typename _Ch_type
>
592 regex_traits
<_Ch_type
>::
593 isctype(_Ch_type __c
, char_class_type __f
) const
595 typedef std::ctype
<char_type
> __ctype_type
;
596 const __ctype_type
& __fctyp(use_facet
<__ctype_type
>(_M_locale
));
598 return __fctyp
.is(__f
._M_base
, __c
)
600 || ((__f
._M_extended
& _RegexMask::_S_under
)
601 && __c
== __fctyp
.widen('_'))
603 || ((__f
._M_extended
& _RegexMask::_S_blank
)
604 && (__c
== __fctyp
.widen(' ')
605 || __c
== __fctyp
.widen('\t')));
608 template<typename _Ch_type
>
610 regex_traits
<_Ch_type
>::
611 value(_Ch_type __ch
, int __radix
) const
613 std::basic_istringstream
<char_type
> __is(string_type(1, __ch
));
617 else if (__radix
== 16)
620 return __is
.fail() ? -1 : __v
;
623 // [7.8] Class basic_regex
625 * Objects of specializations of this class represent regular expressions
626 * constructed from sequences of character type @p _Ch_type.
628 * Storage for the regular expression is allocated and deallocated as
629 * necessary by the member functions of this class.
631 template<typename _Ch_type
, typename _Rx_traits
= regex_traits
<_Ch_type
> >
636 typedef _Ch_type value_type
;
637 typedef _Rx_traits traits_type
;
638 typedef typename
traits_type::string_type string_type
;
639 typedef regex_constants::syntax_option_type flag_type
;
640 typedef typename
traits_type::locale_type locale_type
;
647 static constexpr flag_type icase
= regex_constants::icase
;
648 static constexpr flag_type nosubs
= regex_constants::nosubs
;
649 static constexpr flag_type optimize
= regex_constants::optimize
;
650 static constexpr flag_type collate
= regex_constants::collate
;
651 static constexpr flag_type ECMAScript
= regex_constants::ECMAScript
;
652 static constexpr flag_type basic
= regex_constants::basic
;
653 static constexpr flag_type extended
= regex_constants::extended
;
654 static constexpr flag_type awk
= regex_constants::awk
;
655 static constexpr flag_type grep
= regex_constants::grep
;
656 static constexpr flag_type egrep
= regex_constants::egrep
;
659 // [7.8.2] construct/copy/destroy
661 * Constructs a basic regular expression that does not match any
662 * character sequence.
665 : _M_flags(ECMAScript
),
666 _M_automaton(__detail::__compile
<const _Ch_type
*, _Rx_traits
>(0, 0,
667 _M_traits
, _M_flags
))
671 * @brief Constructs a basic regular expression from the
672 * sequence [__p, __p + char_traits<_Ch_type>::length(__p))
673 * interpreted according to the flags in @p __f.
675 * @param __p A pointer to the start of a C-style null-terminated string
676 * containing a regular expression.
677 * @param __f Flags indicating the syntax rules and options.
679 * @throws regex_error if @p __p is not a valid regular expression.
682 basic_regex(const _Ch_type
* __p
, flag_type __f
= ECMAScript
)
684 _M_automaton(__detail::__compile(__p
, __p
+ _Rx_traits::length(__p
),
685 _M_traits
, _M_flags
))
689 * @brief Constructs a basic regular expression from the sequence
690 * [p, p + len) interpreted according to the flags in @p f.
692 * @param __p A pointer to the start of a string containing a regular
694 * @param __len The length of the string containing the regular
696 * @param __f Flags indicating the syntax rules and options.
698 * @throws regex_error if @p __p is not a valid regular expression.
700 basic_regex(const _Ch_type
* __p
, std::size_t __len
, flag_type __f
)
702 _M_automaton(__detail::__compile(__p
, __p
+ __len
, _M_traits
, _M_flags
))
706 * @brief Copy-constructs a basic regular expression.
708 * @param __rhs A @p regex object.
710 basic_regex(const basic_regex
& __rhs
)
711 : _M_flags(__rhs
._M_flags
), _M_traits(__rhs
._M_traits
),
712 _M_automaton(__rhs
._M_automaton
)
716 * @brief Move-constructs a basic regular expression.
718 * @param __rhs A @p regex object.
720 basic_regex(const basic_regex
&& __rhs
) noexcept
721 : _M_flags(__rhs
._M_flags
), _M_traits(__rhs
._M_traits
),
722 _M_automaton(std::move(__rhs
._M_automaton
))
726 * @brief Constructs a basic regular expression from the string
727 * @p s interpreted according to the flags in @p f.
729 * @param __s A string containing a regular expression.
730 * @param __f Flags indicating the syntax rules and options.
732 * @throws regex_error if @p __s is not a valid regular expression.
734 template<typename _Ch_traits
, typename _Ch_alloc
>
736 basic_regex(const std::basic_string
<_Ch_type
, _Ch_traits
,
738 flag_type __f
= ECMAScript
)
740 _M_automaton(__detail::__compile(__s
.begin(), __s
.end(),
741 _M_traits
, _M_flags
))
745 * @brief Constructs a basic regular expression from the range
746 * [first, last) interpreted according to the flags in @p f.
748 * @param __first The start of a range containing a valid regular
750 * @param __last The end of a range containing a valid regular
752 * @param __f The format flags of the regular expression.
754 * @throws regex_error if @p [__first, __last) is not a valid regular
757 template<typename _InputIterator
>
758 basic_regex(_InputIterator __first
, _InputIterator __last
,
759 flag_type __f
= ECMAScript
)
761 _M_automaton(__detail::__compile(__first
, __last
, _M_traits
, _M_flags
))
765 * @brief Constructs a basic regular expression from an initializer list.
767 * @param __l The initializer list.
768 * @param __f The format flags of the regular expression.
770 * @throws regex_error if @p __l is not a valid regular expression.
772 basic_regex(initializer_list
<_Ch_type
> __l
,
773 flag_type __f
= ECMAScript
)
775 _M_automaton(__detail::__compile(__l
.begin(), __l
.end(),
776 _M_traits
, _M_flags
))
780 * @brief Destroys a basic regular expression.
786 * @brief Assigns one regular expression to another.
789 operator=(const basic_regex
& __rhs
)
790 { return this->assign(__rhs
); }
793 * @brief Move-assigns one regular expression to another.
796 operator=(basic_regex
&& __rhs
) noexcept
797 { return this->assign(std::move(__rhs
)); }
800 * @brief Replaces a regular expression with a new one constructed from
801 * a C-style null-terminated string.
803 * @param __p A pointer to the start of a null-terminated C-style string
804 * containing a regular expression.
807 operator=(const _Ch_type
* __p
)
808 { return this->assign(__p
, flags()); }
811 * @brief Replaces a regular expression with a new one constructed from
814 * @param __s A pointer to a string containing a regular expression.
816 template<typename _Ch_typeraits
, typename _Alloc
>
818 operator=(const basic_string
<_Ch_type
, _Ch_typeraits
, _Alloc
>& __s
)
819 { return this->assign(__s
, flags()); }
823 * @brief the real assignment operator.
825 * @param __rhs Another regular expression object.
828 assign(const basic_regex
& __rhs
)
830 basic_regex
__tmp(__rhs
);
836 * @brief The move-assignment operator.
838 * @param __rhs Another regular expression object.
841 assign(basic_regex
&& __rhs
) noexcept
843 basic_regex
__tmp(std::move(__rhs
));
849 * @brief Assigns a new regular expression to a regex object from a
850 * C-style null-terminated string containing a regular expression
853 * @param __p A pointer to a C-style null-terminated string containing
854 * a regular expression pattern.
855 * @param __flags Syntax option flags.
857 * @throws regex_error if __p does not contain a valid regular
858 * expression pattern interpreted according to @p __flags. If
859 * regex_error is thrown, *this remains unchanged.
862 assign(const _Ch_type
* __p
, flag_type __flags
= ECMAScript
)
863 { return this->assign(string_type(__p
), __flags
); }
866 * @brief Assigns a new regular expression to a regex object from a
867 * C-style string containing a regular expression pattern.
869 * @param __p A pointer to a C-style string containing a
870 * regular expression pattern.
871 * @param __len The length of the regular expression pattern string.
872 * @param __flags Syntax option flags.
874 * @throws regex_error if p does not contain a valid regular
875 * expression pattern interpreted according to @p __flags. If
876 * regex_error is thrown, *this remains unchanged.
879 assign(const _Ch_type
* __p
, std::size_t __len
, flag_type __flags
)
880 { return this->assign(string_type(__p
, __len
), __flags
); }
883 * @brief Assigns a new regular expression to a regex object from a
884 * string containing a regular expression pattern.
886 * @param __s A string containing a regular expression pattern.
887 * @param __flags Syntax option flags.
889 * @throws regex_error if __s does not contain a valid regular
890 * expression pattern interpreted according to @p __flags. If
891 * regex_error is thrown, *this remains unchanged.
893 template<typename _Ch_typeraits
, typename _Alloc
>
895 assign(const basic_string
<_Ch_type
, _Ch_typeraits
, _Alloc
>& __s
,
896 flag_type __flags
= ECMAScript
)
898 basic_regex
__tmp(__s
, __flags
);
904 * @brief Assigns a new regular expression to a regex object.
906 * @param __first The start of a range containing a valid regular
908 * @param __last The end of a range containing a valid regular
910 * @param __flags Syntax option flags.
912 * @throws regex_error if p does not contain a valid regular
913 * expression pattern interpreted according to @p __flags. If
914 * regex_error is thrown, the object remains unchanged.
916 template<typename _InputIterator
>
918 assign(_InputIterator __first
, _InputIterator __last
,
919 flag_type __flags
= ECMAScript
)
920 { return this->assign(string_type(__first
, __last
), __flags
); }
923 * @brief Assigns a new regular expression to a regex object.
925 * @param __l An initializer list representing a regular expression.
926 * @param __flags Syntax option flags.
928 * @throws regex_error if @p __l does not contain a valid
929 * regular expression pattern interpreted according to @p
930 * __flags. If regex_error is thrown, the object remains
934 assign(initializer_list
<_Ch_type
> __l
, flag_type __flags
= ECMAScript
)
935 { return this->assign(__l
.begin(), __l
.end(), __flags
); }
937 // [7.8.4] const operations
939 * @brief Gets the number of marked subexpressions within the regular
944 { return _M_automaton
->_M_sub_count() - 1; }
947 * @brief Gets the flags used to construct the regular expression
948 * or in the last call to assign().
956 * @brief Imbues the regular expression object with the given locale.
958 * @param __loc A locale.
961 imbue(locale_type __loc
)
962 { return _M_traits
.imbue(__loc
); }
965 * @brief Gets the locale currently imbued in the regular expression
970 { return _M_traits
.getloc(); }
974 * @brief Swaps the contents of two regular expression objects.
976 * @param __rhs Another regular expression object.
979 swap(basic_regex
& __rhs
)
981 std::swap(_M_flags
, __rhs
._M_flags
);
982 std::swap(_M_traits
, __rhs
._M_traits
);
983 std::swap(_M_automaton
, __rhs
._M_automaton
);
986 #ifdef _GLIBCXX_DEBUG
988 _M_dot(std::ostream
& __ostr
)
989 { _M_automaton
->_M_dot(__ostr
); }
992 const __detail::_AutomatonPtr
&
993 _M_get_automaton() const
994 { return _M_automaton
; }
998 _Rx_traits _M_traits
;
999 __detail::_AutomatonPtr _M_automaton
;
1002 /** @brief Standard regular expressions. */
1003 typedef basic_regex
<char> regex
;
1005 #ifdef _GLIBCXX_USE_WCHAR_T
1006 /** @brief Standard wide-character regular expressions. */
1007 typedef basic_regex
<wchar_t> wregex
;
1011 // [7.8.6] basic_regex swap
1013 * @brief Swaps the contents of two regular expression objects.
1014 * @param __lhs First regular expression.
1015 * @param __rhs Second regular expression.
1017 template<typename _Ch_type
, typename _Rx_traits
>
1019 swap(basic_regex
<_Ch_type
, _Rx_traits
>& __lhs
,
1020 basic_regex
<_Ch_type
, _Rx_traits
>& __rhs
)
1021 { __lhs
.swap(__rhs
); }
1024 // [7.9] Class template sub_match
1026 * A sequence of characters matched by a particular marked sub-expression.
1028 * An object of this class is essentially a pair of iterators marking a
1029 * matched subexpression within a regular expression pattern match. Such
1030 * objects can be converted to and compared with std::basic_string objects
1031 * of a similar base character type as the pattern matched by the regular
1034 * The iterators that make up the pair are the usual half-open interval
1035 * referencing the actual original pattern matched.
1037 template<typename _BiIter
>
1038 class sub_match
: public std::pair
<_BiIter
, _BiIter
>
1040 typedef iterator_traits
<_BiIter
> __iter_traits
;
1043 typedef typename
__iter_traits::value_type value_type
;
1044 typedef typename
__iter_traits::difference_type difference_type
;
1045 typedef _BiIter iterator
;
1046 typedef std::basic_string
<value_type
> string_type
;
1050 constexpr sub_match() : matched() { }
1053 * Gets the length of the matching sequence.
1057 { return this->matched
? std::distance(this->first
, this->second
) : 0; }
1060 * @brief Gets the matching sequence as a string.
1062 * @returns the matching sequence as a string.
1064 * This is the implicit conversion operator. It is identical to the
1065 * str() member function except that it will want to pop up in
1066 * unexpected places and cause a great deal of confusion and cursing
1069 operator string_type() const
1071 return this->matched
1072 ? string_type(this->first
, this->second
)
1077 * @brief Gets the matching sequence as a string.
1079 * @returns the matching sequence as a string.
1084 return this->matched
1085 ? string_type(this->first
, this->second
)
1090 * @brief Compares this and another matched sequence.
1092 * @param __s Another matched sequence to compare to this one.
1094 * @retval <0 this matched sequence will collate before @p __s.
1095 * @retval =0 this matched sequence is equivalent to @p __s.
1096 * @retval <0 this matched sequence will collate after @p __s.
1099 compare(const sub_match
& __s
) const
1100 { return this->str().compare(__s
.str()); }
1103 * @brief Compares this sub_match to a string.
1105 * @param __s A string to compare to this sub_match.
1107 * @retval <0 this matched sequence will collate before @p __s.
1108 * @retval =0 this matched sequence is equivalent to @p __s.
1109 * @retval <0 this matched sequence will collate after @p __s.
1112 compare(const string_type
& __s
) const
1113 { return this->str().compare(__s
); }
1116 * @brief Compares this sub_match to a C-style string.
1118 * @param __s A C-style string to compare to this sub_match.
1120 * @retval <0 this matched sequence will collate before @p __s.
1121 * @retval =0 this matched sequence is equivalent to @p __s.
1122 * @retval <0 this matched sequence will collate after @p __s.
1125 compare(const value_type
* __s
) const
1126 { return this->str().compare(__s
); }
1130 /** @brief Standard regex submatch over a C-style null-terminated string. */
1131 typedef sub_match
<const char*> csub_match
;
1133 /** @brief Standard regex submatch over a standard string. */
1134 typedef sub_match
<string::const_iterator
> ssub_match
;
1136 #ifdef _GLIBCXX_USE_WCHAR_T
1137 /** @brief Regex submatch over a C-style null-terminated wide string. */
1138 typedef sub_match
<const wchar_t*> wcsub_match
;
1140 /** @brief Regex submatch over a standard wide string. */
1141 typedef sub_match
<wstring::const_iterator
> wssub_match
;
1144 // [7.9.2] sub_match non-member operators
1147 * @brief Tests the equivalence of two regular expression submatches.
1148 * @param __lhs First regular expression submatch.
1149 * @param __rhs Second regular expression submatch.
1150 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1152 template<typename _BiIter
>
1154 operator==(const sub_match
<_BiIter
>& __lhs
, const sub_match
<_BiIter
>& __rhs
)
1155 { return __lhs
.compare(__rhs
) == 0; }
1158 * @brief Tests the inequivalence of two regular expression submatches.
1159 * @param __lhs First regular expression submatch.
1160 * @param __rhs Second regular expression submatch.
1161 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1163 template<typename _BiIter
>
1165 operator!=(const sub_match
<_BiIter
>& __lhs
, const sub_match
<_BiIter
>& __rhs
)
1166 { return __lhs
.compare(__rhs
) != 0; }
1169 * @brief Tests the ordering of two regular expression submatches.
1170 * @param __lhs First regular expression submatch.
1171 * @param __rhs Second regular expression submatch.
1172 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1174 template<typename _BiIter
>
1176 operator<(const sub_match
<_BiIter
>& __lhs
, const sub_match
<_BiIter
>& __rhs
)
1177 { return __lhs
.compare(__rhs
) < 0; }
1180 * @brief Tests the ordering of two regular expression submatches.
1181 * @param __lhs First regular expression submatch.
1182 * @param __rhs Second regular expression submatch.
1183 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1185 template<typename _BiIter
>
1187 operator<=(const sub_match
<_BiIter
>& __lhs
, const sub_match
<_BiIter
>& __rhs
)
1188 { return __lhs
.compare(__rhs
) <= 0; }
1191 * @brief Tests the ordering of two regular expression submatches.
1192 * @param __lhs First regular expression submatch.
1193 * @param __rhs Second regular expression submatch.
1194 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1196 template<typename _BiIter
>
1198 operator>=(const sub_match
<_BiIter
>& __lhs
, const sub_match
<_BiIter
>& __rhs
)
1199 { return __lhs
.compare(__rhs
) >= 0; }
1202 * @brief Tests the ordering of two regular expression submatches.
1203 * @param __lhs First regular expression submatch.
1204 * @param __rhs Second regular expression submatch.
1205 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1207 template<typename _BiIter
>
1209 operator>(const sub_match
<_BiIter
>& __lhs
, const sub_match
<_BiIter
>& __rhs
)
1210 { return __lhs
.compare(__rhs
) > 0; }
1212 // Alias for sub_match'd string.
1213 template<typename _Bi_iter
, typename _Ch_traits
, typename _Ch_alloc
>
1214 using __sub_match_string
= basic_string
<
1215 typename iterator_traits
<_Bi_iter
>::value_type
,
1216 _Ch_traits
, _Ch_alloc
>;
1219 * @brief Tests the equivalence of a string and a regular expression
1221 * @param __lhs A string.
1222 * @param __rhs A regular expression submatch.
1223 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1225 template<typename _Bi_iter
, typename _Ch_traits
, typename _Ch_alloc
>
1227 operator==(const __sub_match_string
<_Bi_iter
, _Ch_traits
, _Ch_alloc
>& __lhs
,
1228 const sub_match
<_Bi_iter
>& __rhs
)
1229 { return __rhs
.compare(__lhs
.c_str()) == 0; }
1232 * @brief Tests the inequivalence of a string and a regular expression
1234 * @param __lhs A string.
1235 * @param __rhs A regular expression submatch.
1236 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1238 template<typename _Bi_iter
, typename _Ch_traits
, typename _Ch_alloc
>
1240 operator!=(const __sub_match_string
<_Bi_iter
, _Ch_traits
, _Ch_alloc
>& __lhs
,
1241 const sub_match
<_Bi_iter
>& __rhs
)
1242 { return !(__lhs
== __rhs
); }
1245 * @brief Tests the ordering of a string and a regular expression submatch.
1246 * @param __lhs A string.
1247 * @param __rhs A regular expression submatch.
1248 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1250 template<typename _Bi_iter
, typename _Ch_traits
, typename _Ch_alloc
>
1252 operator<(const __sub_match_string
<_Bi_iter
, _Ch_traits
, _Ch_alloc
>& __lhs
,
1253 const sub_match
<_Bi_iter
>& __rhs
)
1254 { return __rhs
.compare(__lhs
.c_str()) > 0; }
1257 * @brief Tests the ordering of a string and a regular expression submatch.
1258 * @param __lhs A string.
1259 * @param __rhs A regular expression submatch.
1260 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1262 template<typename _Bi_iter
, typename _Ch_traits
, typename _Ch_alloc
>
1264 operator>(const __sub_match_string
<_Bi_iter
, _Ch_traits
, _Ch_alloc
>& __lhs
,
1265 const sub_match
<_Bi_iter
>& __rhs
)
1266 { return __rhs
< __lhs
; }
1269 * @brief Tests the ordering of a string and a regular expression submatch.
1270 * @param __lhs A string.
1271 * @param __rhs A regular expression submatch.
1272 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1274 template<typename _Bi_iter
, typename _Ch_traits
, typename _Ch_alloc
>
1276 operator>=(const __sub_match_string
<_Bi_iter
, _Ch_traits
, _Ch_alloc
>& __lhs
,
1277 const sub_match
<_Bi_iter
>& __rhs
)
1278 { return !(__lhs
< __rhs
); }
1281 * @brief Tests the ordering of a string and a regular expression submatch.
1282 * @param __lhs A string.
1283 * @param __rhs A regular expression submatch.
1284 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1286 template<typename _Bi_iter
, typename _Ch_traits
, typename _Ch_alloc
>
1288 operator<=(const __sub_match_string
<_Bi_iter
, _Ch_traits
, _Ch_alloc
>& __lhs
,
1289 const sub_match
<_Bi_iter
>& __rhs
)
1290 { return !(__rhs
< __lhs
); }
1293 * @brief Tests the equivalence of a regular expression submatch and a
1295 * @param __lhs A regular expression submatch.
1296 * @param __rhs A string.
1297 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1299 template<typename _Bi_iter
, typename _Ch_traits
, typename _Ch_alloc
>
1301 operator==(const sub_match
<_Bi_iter
>& __lhs
,
1302 const __sub_match_string
<_Bi_iter
, _Ch_traits
, _Ch_alloc
>& __rhs
)
1303 { return __lhs
.compare(__rhs
.c_str()) == 0; }
1306 * @brief Tests the inequivalence of a regular expression submatch and a
1308 * @param __lhs A regular expression submatch.
1309 * @param __rhs A string.
1310 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1312 template<typename _Bi_iter
, typename _Ch_traits
, typename _Ch_alloc
>
1314 operator!=(const sub_match
<_Bi_iter
>& __lhs
,
1315 const __sub_match_string
<_Bi_iter
, _Ch_traits
, _Ch_alloc
>& __rhs
)
1316 { return !(__lhs
== __rhs
); }
1319 * @brief Tests the ordering of a regular expression submatch and a string.
1320 * @param __lhs A regular expression submatch.
1321 * @param __rhs A string.
1322 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1324 template<typename _Bi_iter
, class _Ch_traits
, class _Ch_alloc
>
1326 operator<(const sub_match
<_Bi_iter
>& __lhs
,
1327 const __sub_match_string
<_Bi_iter
, _Ch_traits
, _Ch_alloc
>& __rhs
)
1328 { return __lhs
.compare(__rhs
.c_str()) < 0; }
1331 * @brief Tests the ordering of a regular expression submatch and a string.
1332 * @param __lhs A regular expression submatch.
1333 * @param __rhs A string.
1334 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1336 template<typename _Bi_iter
, class _Ch_traits
, class _Ch_alloc
>
1338 operator>(const sub_match
<_Bi_iter
>& __lhs
,
1339 const __sub_match_string
<_Bi_iter
, _Ch_traits
, _Ch_alloc
>& __rhs
)
1340 { return __rhs
< __lhs
; }
1343 * @brief Tests the ordering of a regular expression submatch and a string.
1344 * @param __lhs A regular expression submatch.
1345 * @param __rhs A string.
1346 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1348 template<typename _Bi_iter
, class _Ch_traits
, class _Ch_alloc
>
1350 operator>=(const sub_match
<_Bi_iter
>& __lhs
,
1351 const __sub_match_string
<_Bi_iter
, _Ch_traits
, _Ch_alloc
>& __rhs
)
1352 { return !(__lhs
< __rhs
); }
1355 * @brief Tests the ordering of a regular expression submatch and a string.
1356 * @param __lhs A regular expression submatch.
1357 * @param __rhs A string.
1358 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1360 template<typename _Bi_iter
, class _Ch_traits
, class _Ch_alloc
>
1362 operator<=(const sub_match
<_Bi_iter
>& __lhs
,
1363 const __sub_match_string
<_Bi_iter
, _Ch_traits
, _Ch_alloc
>& __rhs
)
1364 { return !(__rhs
< __lhs
); }
1367 * @brief Tests the equivalence of a C string and a regular expression
1369 * @param __lhs A C string.
1370 * @param __rhs A regular expression submatch.
1371 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1373 template<typename _Bi_iter
>
1375 operator==(typename iterator_traits
<_Bi_iter
>::value_type
const* __lhs
,
1376 const sub_match
<_Bi_iter
>& __rhs
)
1377 { return __rhs
.compare(__lhs
) == 0; }
1380 * @brief Tests the inequivalence of an iterator value and a regular
1381 * expression submatch.
1382 * @param __lhs A regular expression submatch.
1383 * @param __rhs A string.
1384 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1386 template<typename _Bi_iter
>
1388 operator!=(typename iterator_traits
<_Bi_iter
>::value_type
const* __lhs
,
1389 const sub_match
<_Bi_iter
>& __rhs
)
1390 { return !(__lhs
== __rhs
); }
1393 * @brief Tests the ordering of a string and a regular expression submatch.
1394 * @param __lhs A string.
1395 * @param __rhs A regular expression submatch.
1396 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1398 template<typename _Bi_iter
>
1400 operator<(typename iterator_traits
<_Bi_iter
>::value_type
const* __lhs
,
1401 const sub_match
<_Bi_iter
>& __rhs
)
1402 { return __rhs
.compare(__lhs
) > 0; }
1405 * @brief Tests the ordering of a string and a regular expression submatch.
1406 * @param __lhs A string.
1407 * @param __rhs A regular expression submatch.
1408 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1410 template<typename _Bi_iter
>
1412 operator>(typename iterator_traits
<_Bi_iter
>::value_type
const* __lhs
,
1413 const sub_match
<_Bi_iter
>& __rhs
)
1414 { return __rhs
< __lhs
; }
1417 * @brief Tests the ordering of a string and a regular expression submatch.
1418 * @param __lhs A string.
1419 * @param __rhs A regular expression submatch.
1420 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1422 template<typename _Bi_iter
>
1424 operator>=(typename iterator_traits
<_Bi_iter
>::value_type
const* __lhs
,
1425 const sub_match
<_Bi_iter
>& __rhs
)
1426 { return !(__lhs
< __rhs
); }
1429 * @brief Tests the ordering of a string and a regular expression submatch.
1430 * @param __lhs A string.
1431 * @param __rhs A regular expression submatch.
1432 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1434 template<typename _Bi_iter
>
1436 operator<=(typename iterator_traits
<_Bi_iter
>::value_type
const* __lhs
,
1437 const sub_match
<_Bi_iter
>& __rhs
)
1438 { return !(__rhs
< __lhs
); }
1441 * @brief Tests the equivalence of a regular expression submatch and a
1443 * @param __lhs A regular expression submatch.
1444 * @param __rhs A pointer to a string?
1445 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1447 template<typename _Bi_iter
>
1449 operator==(const sub_match
<_Bi_iter
>& __lhs
,
1450 typename iterator_traits
<_Bi_iter
>::value_type
const* __rhs
)
1451 { return __lhs
.compare(__rhs
) == 0; }
1454 * @brief Tests the inequivalence of a regular expression submatch and a
1456 * @param __lhs A regular expression submatch.
1457 * @param __rhs A pointer to a string.
1458 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1460 template<typename _Bi_iter
>
1462 operator!=(const sub_match
<_Bi_iter
>& __lhs
,
1463 typename iterator_traits
<_Bi_iter
>::value_type
const* __rhs
)
1464 { return !(__lhs
== __rhs
); }
1467 * @brief Tests the ordering of a regular expression submatch and a string.
1468 * @param __lhs A regular expression submatch.
1469 * @param __rhs A string.
1470 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1472 template<typename _Bi_iter
>
1474 operator<(const sub_match
<_Bi_iter
>& __lhs
,
1475 typename iterator_traits
<_Bi_iter
>::value_type
const* __rhs
)
1476 { return __lhs
.compare(__rhs
) < 0; }
1479 * @brief Tests the ordering of a regular expression submatch and a string.
1480 * @param __lhs A regular expression submatch.
1481 * @param __rhs A string.
1482 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1484 template<typename _Bi_iter
>
1486 operator>(const sub_match
<_Bi_iter
>& __lhs
,
1487 typename iterator_traits
<_Bi_iter
>::value_type
const* __rhs
)
1488 { return __rhs
< __lhs
; }
1491 * @brief Tests the ordering of a regular expression submatch and a string.
1492 * @param __lhs A regular expression submatch.
1493 * @param __rhs A string.
1494 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1496 template<typename _Bi_iter
>
1498 operator>=(const sub_match
<_Bi_iter
>& __lhs
,
1499 typename iterator_traits
<_Bi_iter
>::value_type
const* __rhs
)
1500 { return !(__lhs
< __rhs
); }
1503 * @brief Tests the ordering of a regular expression submatch and a string.
1504 * @param __lhs A regular expression submatch.
1505 * @param __rhs A string.
1506 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1508 template<typename _Bi_iter
>
1510 operator<=(const sub_match
<_Bi_iter
>& __lhs
,
1511 typename iterator_traits
<_Bi_iter
>::value_type
const* __rhs
)
1512 { return !(__rhs
< __lhs
); }
1515 * @brief Tests the equivalence of a string and a regular expression
1517 * @param __lhs A string.
1518 * @param __rhs A regular expression submatch.
1519 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1521 template<typename _Bi_iter
>
1523 operator==(typename iterator_traits
<_Bi_iter
>::value_type
const& __lhs
,
1524 const sub_match
<_Bi_iter
>& __rhs
)
1526 typedef typename sub_match
<_Bi_iter
>::string_type string_type
;
1527 return __rhs
.compare(string_type(1, __lhs
)) == 0;
1531 * @brief Tests the inequivalence of a string and a regular expression
1533 * @param __lhs A string.
1534 * @param __rhs A regular expression submatch.
1535 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1537 template<typename _Bi_iter
>
1539 operator!=(typename iterator_traits
<_Bi_iter
>::value_type
const& __lhs
,
1540 const sub_match
<_Bi_iter
>& __rhs
)
1541 { return !(__lhs
== __rhs
); }
1544 * @brief Tests the ordering of a string and a regular expression submatch.
1545 * @param __lhs A string.
1546 * @param __rhs A regular expression submatch.
1547 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1549 template<typename _Bi_iter
>
1551 operator<(typename iterator_traits
<_Bi_iter
>::value_type
const& __lhs
,
1552 const sub_match
<_Bi_iter
>& __rhs
)
1554 typedef typename sub_match
<_Bi_iter
>::string_type string_type
;
1555 return __rhs
.compare(string_type(1, __lhs
)) > 0;
1559 * @brief Tests the ordering of a string and a regular expression submatch.
1560 * @param __lhs A string.
1561 * @param __rhs A regular expression submatch.
1562 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1564 template<typename _Bi_iter
>
1566 operator>(typename iterator_traits
<_Bi_iter
>::value_type
const& __lhs
,
1567 const sub_match
<_Bi_iter
>& __rhs
)
1568 { return __rhs
< __lhs
; }
1571 * @brief Tests the ordering of a string and a regular expression submatch.
1572 * @param __lhs A string.
1573 * @param __rhs A regular expression submatch.
1574 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1576 template<typename _Bi_iter
>
1578 operator>=(typename iterator_traits
<_Bi_iter
>::value_type
const& __lhs
,
1579 const sub_match
<_Bi_iter
>& __rhs
)
1580 { return !(__lhs
< __rhs
); }
1583 * @brief Tests the ordering of a string and a regular expression submatch.
1584 * @param __lhs A string.
1585 * @param __rhs A regular expression submatch.
1586 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1588 template<typename _Bi_iter
>
1590 operator<=(typename iterator_traits
<_Bi_iter
>::value_type
const& __lhs
,
1591 const sub_match
<_Bi_iter
>& __rhs
)
1592 { return !(__rhs
< __lhs
); }
1595 * @brief Tests the equivalence of a regular expression submatch and a
1597 * @param __lhs A regular expression submatch.
1598 * @param __rhs A const string reference.
1599 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1601 template<typename _Bi_iter
>
1603 operator==(const sub_match
<_Bi_iter
>& __lhs
,
1604 typename iterator_traits
<_Bi_iter
>::value_type
const& __rhs
)
1606 typedef typename sub_match
<_Bi_iter
>::string_type string_type
;
1607 return __lhs
.compare(string_type(1, __rhs
)) == 0;
1611 * @brief Tests the inequivalence of a regular expression submatch and a
1613 * @param __lhs A regular expression submatch.
1614 * @param __rhs A const string reference.
1615 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1617 template<typename _Bi_iter
>
1619 operator!=(const sub_match
<_Bi_iter
>& __lhs
,
1620 typename iterator_traits
<_Bi_iter
>::value_type
const& __rhs
)
1621 { return !(__lhs
== __rhs
); }
1624 * @brief Tests the ordering of a regular expression submatch and a string.
1625 * @param __lhs A regular expression submatch.
1626 * @param __rhs A const string reference.
1627 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1629 template<typename _Bi_iter
>
1631 operator<(const sub_match
<_Bi_iter
>& __lhs
,
1632 typename iterator_traits
<_Bi_iter
>::value_type
const& __rhs
)
1634 typedef typename sub_match
<_Bi_iter
>::string_type string_type
;
1635 return __lhs
.compare(string_type(1, __rhs
)) < 0;
1639 * @brief Tests the ordering of a regular expression submatch and a string.
1640 * @param __lhs A regular expression submatch.
1641 * @param __rhs A const string reference.
1642 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1644 template<typename _Bi_iter
>
1646 operator>(const sub_match
<_Bi_iter
>& __lhs
,
1647 typename iterator_traits
<_Bi_iter
>::value_type
const& __rhs
)
1648 { return __rhs
< __lhs
; }
1651 * @brief Tests the ordering of a regular expression submatch and a string.
1652 * @param __lhs A regular expression submatch.
1653 * @param __rhs A const string reference.
1654 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1656 template<typename _Bi_iter
>
1658 operator>=(const sub_match
<_Bi_iter
>& __lhs
,
1659 typename iterator_traits
<_Bi_iter
>::value_type
const& __rhs
)
1660 { return !(__lhs
< __rhs
); }
1663 * @brief Tests the ordering of a regular expression submatch and a string.
1664 * @param __lhs A regular expression submatch.
1665 * @param __rhs A const string reference.
1666 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1668 template<typename _Bi_iter
>
1670 operator<=(const sub_match
<_Bi_iter
>& __lhs
,
1671 typename iterator_traits
<_Bi_iter
>::value_type
const& __rhs
)
1672 { return !(__rhs
< __lhs
); }
1675 * @brief Inserts a matched string into an output stream.
1677 * @param __os The output stream.
1678 * @param __m A submatch string.
1680 * @returns the output stream with the submatch string inserted.
1682 template<typename _Ch_type
, typename _Ch_traits
, typename _Bi_iter
>
1684 basic_ostream
<_Ch_type
, _Ch_traits
>&
1685 operator<<(basic_ostream
<_Ch_type
, _Ch_traits
>& __os
,
1686 const sub_match
<_Bi_iter
>& __m
)
1687 { return __os
<< __m
.str(); }
1689 // [7.10] Class template match_results
1692 * Special sub_match object representing an unmatched sub-expression.
1694 template<typename _Bi_iter
>
1695 inline const sub_match
<_Bi_iter
>&
1698 static const sub_match
<_Bi_iter
> __unmatched
= sub_match
<_Bi_iter
>();
1703 * @brief The results of a match or search operation.
1705 * A collection of character sequences representing the result of a regular
1706 * expression match. Storage for the collection is allocated and freed as
1707 * necessary by the member functions of class template match_results.
1709 * This class satisfies the Sequence requirements, with the exception that
1710 * only the operations defined for a const-qualified Sequence are supported.
1712 * The sub_match object stored at index 0 represents sub-expression 0, i.e.
1713 * the whole match. In this case the %sub_match member matched is always true.
1714 * The sub_match object stored at index n denotes what matched the marked
1715 * sub-expression n within the matched expression. If the sub-expression n
1716 * participated in a regular expression match then the %sub_match member
1717 * matched evaluates to true, and members first and second denote the range
1718 * of characters [first, second) which formed that match. Otherwise matched
1719 * is false, and members first and second point to the end of the sequence
1720 * that was searched.
1724 template<typename _Bi_iter
,
1725 typename _Alloc
= allocator
<sub_match
<_Bi_iter
> > >
1727 : private std::vector
<sub_match
<_Bi_iter
>, _Alloc
>
1731 * The vector base is empty if this does not represent a successful match.
1732 * Otherwise it contains n+3 elements where n is the number of marked
1735 * [1] 1st marked subexpression
1737 * [n] nth marked subexpression
1741 typedef std::vector
<sub_match
<_Bi_iter
>, _Alloc
> _Base_type
;
1742 typedef std::iterator_traits
<_Bi_iter
> __iter_traits
;
1743 typedef regex_constants::match_flag_type match_flag_type
;
1747 * @name 10.? Public Types
1750 typedef _Alloc allocator_type
;
1751 typedef sub_match
<_Bi_iter
> value_type
;
1752 typedef const value_type
& const_reference
;
1753 typedef const_reference reference
;
1754 typedef typename
_Base_type::const_iterator const_iterator
;
1755 typedef const_iterator iterator
;
1756 typedef typename
__iter_traits::difference_type difference_type
;
1757 typedef typename
__iter_traits::value_type char_type
;
1758 typedef typename allocator_traits
<_Alloc
>::size_type size_type
;
1761 typedef std::basic_string
<char_type
> string_type
;
1766 * @name 28.10.1 Construction, Copying, and Destruction
1771 * @brief Constructs a default %match_results container.
1772 * @post size() returns 0 and str() returns an empty string.
1775 match_results(const _Alloc
& __a
= _Alloc())
1780 * @brief Copy constructs a %match_results.
1782 match_results(const match_results
& __rhs
)
1787 * @brief Move constructs a %match_results.
1789 match_results(match_results
&& __rhs
) noexcept
1790 : _Base_type(std::move(__rhs
))
1794 * @brief Assigns rhs to *this.
1797 operator=(const match_results
& __rhs
)
1799 match_results(__rhs
).swap(*this);
1804 * @brief Move-assigns rhs to *this.
1807 operator=(match_results
&& __rhs
)
1809 match_results(std::move(__rhs
)).swap(*this);
1814 * @brief Destroys a %match_results object.
1823 * @brief Indicates if the %match_results is ready.
1824 * @retval true The object has a fully-established result state.
1825 * @retval false The object is not ready.
1827 bool ready() const { return !_Base_type::empty(); }
1830 * @name 28.10.2 Size
1835 * @brief Gets the number of matches and submatches.
1837 * The number of matches for a given regular expression will be either 0
1838 * if there was no match or mark_count() + 1 if a match was successful.
1839 * Some matches may be empty.
1841 * @returns the number of matches found.
1846 size_type __size
= _Base_type::size();
1847 return (__size
&& _Base_type::operator[](0).matched
) ? __size
- 2 : 0;
1852 { return _Base_type::max_size(); }
1855 * @brief Indicates if the %match_results contains no results.
1856 * @retval true The %match_results object is empty.
1857 * @retval false The %match_results object is not empty.
1861 { return size() == 0; }
1866 * @name 10.3 Element Access
1871 * @brief Gets the length of the indicated submatch.
1872 * @param __sub indicates the submatch.
1873 * @pre ready() == true
1875 * This function returns the length of the indicated submatch, or the
1876 * length of the entire match if @p __sub is zero (the default).
1879 length(size_type __sub
= 0) const
1880 { return (*this)[__sub
].length(); }
1883 * @brief Gets the offset of the beginning of the indicated submatch.
1884 * @param __sub indicates the submatch.
1885 * @pre ready() == true
1887 * This function returns the offset from the beginning of the target
1888 * sequence to the beginning of the submatch, unless the value of @p __sub
1889 * is zero (the default), in which case this function returns the offset
1890 * from the beginning of the target sequence to the beginning of the
1893 * Returns -1 if @p __sub is out of range.
1896 position(size_type __sub
= 0) const
1898 return __sub
< size() ? std::distance(this->prefix().first
,
1899 (*this)[__sub
].first
) : -1;
1903 * @brief Gets the match or submatch converted to a string type.
1904 * @param __sub indicates the submatch.
1905 * @pre ready() == true
1907 * This function gets the submatch (or match, if @p __sub is
1908 * zero) extracted from the target range and converted to the
1909 * associated string type.
1912 str(size_type __sub
= 0) const
1913 { return (*this)[__sub
].str(); }
1916 * @brief Gets a %sub_match reference for the match or submatch.
1917 * @param __sub indicates the submatch.
1918 * @pre ready() == true
1920 * This function gets a reference to the indicated submatch, or
1921 * the entire match if @p __sub is zero.
1923 * If @p __sub >= size() then this function returns a %sub_match with a
1924 * special value indicating no submatch.
1927 operator[](size_type __sub
) const
1929 _GLIBCXX_DEBUG_ASSERT( ready() );
1930 return __sub
< size()
1931 ? _Base_type::operator[](__sub
)
1932 : __unmatched_sub
<_Bi_iter
>();
1936 * @brief Gets a %sub_match representing the match prefix.
1937 * @pre ready() == true
1939 * This function gets a reference to a %sub_match object representing the
1940 * part of the target range between the start of the target range and the
1941 * start of the match.
1946 _GLIBCXX_DEBUG_ASSERT( ready() );
1948 ? _Base_type::operator[](_Base_type::size() - 2)
1949 : __unmatched_sub
<_Bi_iter
>();
1953 * @brief Gets a %sub_match representing the match suffix.
1954 * @pre ready() == true
1956 * This function gets a reference to a %sub_match object representing the
1957 * part of the target range between the end of the match and the end of
1963 _GLIBCXX_DEBUG_ASSERT( ready() );
1965 ? _Base_type::operator[](_Base_type::size() - 1)
1966 : __unmatched_sub
<_Bi_iter
>();
1970 * @brief Gets an iterator to the start of the %sub_match collection.
1974 { return _Base_type::begin(); }
1977 * @brief Gets an iterator to the start of the %sub_match collection.
1981 { return _Base_type::cbegin(); }
1984 * @brief Gets an iterator to one-past-the-end of the collection.
1988 { return !empty() ? _Base_type::end() - 2 : _Base_type::end(); }
1991 * @brief Gets an iterator to one-past-the-end of the collection.
2000 * @name 10.4 Formatting
2002 * These functions perform formatted substitution of the matched
2003 * character sequences into their target. The format specifiers and
2004 * escape sequences accepted by these functions are determined by
2005 * their @p flags parameter as documented above.
2010 * @pre ready() == true
2011 * @todo Implement this function.
2013 template<typename _Out_iter
>
2015 format(_Out_iter __out
, const char_type
* __fmt_first
,
2016 const char_type
* __fmt_last
,
2017 match_flag_type __flags
= regex_constants::format_default
) const
2021 * @pre ready() == true
2023 template<typename _Out_iter
, typename _St
, typename _Sa
>
2025 format(_Out_iter __out
, const basic_string
<char_type
, _St
, _Sa
>& __fmt
,
2026 match_flag_type __flags
= regex_constants::format_default
) const
2028 return format(__out
, __fmt
.data(), __fmt
.data() + __fmt
.size(),
2033 * @pre ready() == true
2035 template<typename _Out_iter
, typename _St
, typename _Sa
>
2036 basic_string
<char_type
, _St
, _Sa
>
2037 format(const basic_string
<char_type
, _St
, _Sa
>& __fmt
,
2038 match_flag_type __flags
= regex_constants::format_default
) const
2040 basic_string
<char_type
, _St
, _Sa
> __result
;
2041 format(std::back_inserter(__result
), __fmt
, __flags
);
2046 * @pre ready() == true
2049 format(const char_type
* __fmt
,
2050 match_flag_type __flags
= regex_constants::format_default
) const
2052 string_type __result
;
2053 format(std::back_inserter(__result
),
2054 __fmt
+ char_traits
<char_type
>::length(__fmt
),
2062 * @name 10.5 Allocator
2067 * @brief Gets a copy of the allocator.
2070 get_allocator() const
2071 { return _Base_type::get_allocator(); }
2081 * @brief Swaps the contents of two match_results.
2084 swap(match_results
& __that
)
2085 { _Base_type::swap(__that
); }
2089 friend class __detail::_SpecializedResults
<_Bi_iter
, _Alloc
>;
2092 typedef match_results
<const char*> cmatch
;
2093 typedef match_results
<string::const_iterator
> smatch
;
2094 #ifdef _GLIBCXX_USE_WCHAR_T
2095 typedef match_results
<const wchar_t*> wcmatch
;
2096 typedef match_results
<wstring::const_iterator
> wsmatch
;
2099 // match_results comparisons
2101 * @brief Compares two match_results for equality.
2102 * @returns true if the two objects refer to the same match,
2105 template<typename _Bi_iter
, typename _Alloc
>
2107 operator==(const match_results
<_Bi_iter
, _Alloc
>& __m1
,
2108 const match_results
<_Bi_iter
, _Alloc
>& __m2
)
2110 if (__m1
.ready() != __m2
.ready())
2112 if (!__m1
.ready()) // both are not ready
2114 if (__m1
.empty() != __m2
.empty())
2116 if (__m1
.empty()) // both are empty
2118 return __m1
.prefix() == __m2
.prefix()
2119 && __m1
.size() == __m2
.size()
2120 && std::equal(__m1
.begin(), __m1
.end(), __m2
.begin())
2121 && __m1
.suffix() == __m2
.suffix();
2125 * @brief Compares two match_results for inequality.
2126 * @returns true if the two objects do not refer to the same match,
2129 template<typename _Bi_iter
, class _Alloc
>
2131 operator!=(const match_results
<_Bi_iter
, _Alloc
>& __m1
,
2132 const match_results
<_Bi_iter
, _Alloc
>& __m2
)
2133 { return !(__m1
== __m2
); }
2135 // [7.10.6] match_results swap
2137 * @brief Swaps two match results.
2138 * @param __lhs A match result.
2139 * @param __rhs A match result.
2141 * The contents of the two match_results objects are swapped.
2143 template<typename _Bi_iter
, typename _Alloc
>
2145 swap(match_results
<_Bi_iter
, _Alloc
>& __lhs
,
2146 match_results
<_Bi_iter
, _Alloc
>& __rhs
)
2147 { __lhs
.swap(__rhs
); }
2149 // [7.11.2] Function template regex_match
2151 * @name Matching, Searching, and Replacing
2156 * @brief Determines if there is a match between the regular expression @p e
2157 * and all of the character sequence [first, last).
2159 * @param __s Start of the character sequence to match.
2160 * @param __e One-past-the-end of the character sequence to match.
2161 * @param __m The match results.
2162 * @param __re The regular expression.
2163 * @param __flags Controls how the regular expression is matched.
2165 * @retval true A match exists.
2166 * @retval false Otherwise.
2168 * @throws an exception of type regex_error.
2170 * @todo Implement this function.
2172 template<typename _Bi_iter
, typename _Alloc
,
2173 typename _Ch_type
, typename _Rx_traits
>
2175 regex_match(_Bi_iter __s
,
2177 match_results
<_Bi_iter
, _Alloc
>& __m
,
2178 const basic_regex
<_Ch_type
, _Rx_traits
>& __re
,
2179 regex_constants::match_flag_type __flags
2180 = regex_constants::match_default
)
2182 __detail::_AutomatonPtr __a
= __re
._M_get_automaton();
2183 __detail::_Automaton::_SizeT __sz
= __a
->_M_sub_count();
2184 __detail::_SpecializedCursor
<_Bi_iter
> __cs(__s
, __e
);
2185 __detail::_SpecializedResults
<_Bi_iter
, _Alloc
> __r(__sz
, __cs
, __m
);
2186 return __a
->_M_get_matcher(__cs
, __r
, __a
, __flags
)->_M_match();
2190 * @brief Indicates if there is a match between the regular expression @p e
2191 * and all of the character sequence [first, last).
2193 * @param __first Beginning of the character sequence to match.
2194 * @param __last One-past-the-end of the character sequence to match.
2195 * @param __re The regular expression.
2196 * @param __flags Controls how the regular expression is matched.
2198 * @retval true A match exists.
2199 * @retval false Otherwise.
2201 * @throws an exception of type regex_error.
2203 template<typename _Bi_iter
, typename _Ch_type
, typename _Rx_traits
>
2205 regex_match(_Bi_iter __first
, _Bi_iter __last
,
2206 const basic_regex
<_Ch_type
, _Rx_traits
>& __re
,
2207 regex_constants::match_flag_type __flags
2208 = regex_constants::match_default
)
2210 match_results
<_Bi_iter
> __what
;
2211 return regex_match(__first
, __last
, __what
, __re
, __flags
);
2215 * @brief Determines if there is a match between the regular expression @p e
2216 * and a C-style null-terminated string.
2218 * @param __s The C-style null-terminated string to match.
2219 * @param __m The match results.
2220 * @param __re The regular expression.
2221 * @param __f Controls how the regular expression is matched.
2223 * @retval true A match exists.
2224 * @retval false Otherwise.
2226 * @throws an exception of type regex_error.
2228 template<typename _Ch_type
, typename _Alloc
, typename _Rx_traits
>
2230 regex_match(const _Ch_type
* __s
,
2231 match_results
<const _Ch_type
*, _Alloc
>& __m
,
2232 const basic_regex
<_Ch_type
, _Rx_traits
>& __re
,
2233 regex_constants::match_flag_type __f
2234 = regex_constants::match_default
)
2235 { return regex_match(__s
, __s
+ _Rx_traits::length(__s
), __m
, __re
, __f
); }
2238 * @brief Determines if there is a match between the regular expression @p e
2241 * @param __s The string to match.
2242 * @param __m The match results.
2243 * @param __re The regular expression.
2244 * @param __flags Controls how the regular expression is matched.
2246 * @retval true A match exists.
2247 * @retval false Otherwise.
2249 * @throws an exception of type regex_error.
2251 template<typename _Ch_traits
, typename _Ch_alloc
,
2252 typename _Alloc
, typename _Ch_type
, typename _Rx_traits
>
2254 regex_match(const basic_string
<_Ch_type
, _Ch_traits
, _Ch_alloc
>& __s
,
2255 match_results
<typename basic_string
<_Ch_type
,
2256 _Ch_traits
, _Ch_alloc
>::const_iterator
, _Alloc
>& __m
,
2257 const basic_regex
<_Ch_type
, _Rx_traits
>& __re
,
2258 regex_constants::match_flag_type __flags
2259 = regex_constants::match_default
)
2260 { return regex_match(__s
.begin(), __s
.end(), __m
, __re
, __flags
); }
2263 * @brief Indicates if there is a match between the regular expression @p e
2264 * and a C-style null-terminated string.
2266 * @param __s The C-style null-terminated string to match.
2267 * @param __re The regular expression.
2268 * @param __f Controls how the regular expression is matched.
2270 * @retval true A match exists.
2271 * @retval false Otherwise.
2273 * @throws an exception of type regex_error.
2275 template<typename _Ch_type
, class _Rx_traits
>
2277 regex_match(const _Ch_type
* __s
,
2278 const basic_regex
<_Ch_type
, _Rx_traits
>& __re
,
2279 regex_constants::match_flag_type __f
2280 = regex_constants::match_default
)
2281 { return regex_match(__s
, __s
+ _Rx_traits::length(__s
), __re
, __f
); }
2284 * @brief Indicates if there is a match between the regular expression @p e
2287 * @param __s [IN] The string to match.
2288 * @param __re [IN] The regular expression.
2289 * @param __flags [IN] Controls how the regular expression is matched.
2291 * @retval true A match exists.
2292 * @retval false Otherwise.
2294 * @throws an exception of type regex_error.
2296 template<typename _Ch_traits
, typename _Str_allocator
,
2297 typename _Ch_type
, typename _Rx_traits
>
2299 regex_match(const basic_string
<_Ch_type
, _Ch_traits
, _Str_allocator
>& __s
,
2300 const basic_regex
<_Ch_type
, _Rx_traits
>& __re
,
2301 regex_constants::match_flag_type __flags
2302 = regex_constants::match_default
)
2303 { return regex_match(__s
.begin(), __s
.end(), __re
, __flags
); }
2305 // [7.11.3] Function template regex_search
2307 * Searches for a regular expression within a range.
2308 * @param __first [IN] The start of the string to search.
2309 * @param __last [IN] One-past-the-end of the string to search.
2310 * @param __m [OUT] The match results.
2311 * @param __re [IN] The regular expression to search for.
2312 * @param __flags [IN] Search policy flags.
2313 * @retval true A match was found within the string.
2314 * @retval false No match was found within the string, the content of %m is
2317 * @throws an exception of type regex_error.
2319 * @todo Implement this function.
2321 template<typename _Bi_iter
, typename _Alloc
,
2322 typename _Ch_type
, typename _Rx_traits
>
2324 regex_search(_Bi_iter __first
, _Bi_iter __last
,
2325 match_results
<_Bi_iter
, _Alloc
>& __m
,
2326 const basic_regex
<_Ch_type
, _Rx_traits
>& __re
,
2327 regex_constants::match_flag_type __flags
2328 = regex_constants::match_default
)
2330 __detail::_AutomatonPtr __a
= __re
._M_get_automaton();
2331 __detail::_Automaton::_SizeT __sz
= __a
->_M_sub_count();
2332 __detail::_SpecializedCursor
<_Bi_iter
> __cs(__first
, __last
);
2333 __detail::_SpecializedResults
<_Bi_iter
, _Alloc
> __r(__sz
, __cs
, __m
);
2334 for (auto __cur
= __first
; __cur
!= __last
; ++__cur
) // Any KMP-like algo?
2336 __detail::_SpecializedCursor
<_Bi_iter
> __curs(__cur
, __last
);
2337 auto __matcher
= __a
->_M_get_matcher(__curs
, __r
, __a
, __flags
);
2338 if (__matcher
->_M_search_from_first())
2340 __r
._M_set_range(__m
.size(),
2341 __detail::_SpecializedCursor
<_Bi_iter
>
2342 {__first
, __m
[0].first
});
2343 __r
._M_set_range(__m
.size()+1,
2344 __detail::_SpecializedCursor
<_Bi_iter
>
2345 {__m
[0].second
, __last
});
2346 __r
._M_set_matched(__m
.size(),
2347 __m
.prefix().first
!= __m
.prefix().second
);
2348 __r
._M_set_matched(__m
.size()+1,
2349 __m
.suffix().first
!= __m
.suffix().second
);
2358 * Searches for a regular expression within a range.
2359 * @param __first [IN] The start of the string to search.
2360 * @param __last [IN] One-past-the-end of the string to search.
2361 * @param __re [IN] The regular expression to search for.
2362 * @param __flags [IN] Search policy flags.
2363 * @retval true A match was found within the string.
2364 * @retval false No match was found within the string.
2366 * @throws an exception of type regex_error.
2368 template<typename _Bi_iter
, typename _Ch_type
, typename _Rx_traits
>
2370 regex_search(_Bi_iter __first
, _Bi_iter __last
,
2371 const basic_regex
<_Ch_type
, _Rx_traits
>& __re
,
2372 regex_constants::match_flag_type __flags
2373 = regex_constants::match_default
)
2375 match_results
<_Bi_iter
> __what
;
2376 return regex_search(__first
, __last
, __what
, __re
, __flags
);
2380 * @brief Searches for a regular expression within a C-string.
2381 * @param __s [IN] A C-string to search for the regex.
2382 * @param __m [OUT] The set of regex matches.
2383 * @param __e [IN] The regex to search for in @p s.
2384 * @param __f [IN] The search flags.
2385 * @retval true A match was found within the string.
2386 * @retval false No match was found within the string, the content of %m is
2389 * @throws an exception of type regex_error.
2391 template<typename _Ch_type
, class _Alloc
, class _Rx_traits
>
2393 regex_search(const _Ch_type
* __s
,
2394 match_results
<const _Ch_type
*, _Alloc
>& __m
,
2395 const basic_regex
<_Ch_type
, _Rx_traits
>& __e
,
2396 regex_constants::match_flag_type __f
2397 = regex_constants::match_default
)
2398 { return regex_search(__s
, __s
+ _Rx_traits::length(__s
), __m
, __e
, __f
); }
2401 * @brief Searches for a regular expression within a C-string.
2402 * @param __s [IN] The C-string to search.
2403 * @param __e [IN] The regular expression to search for.
2404 * @param __f [IN] Search policy flags.
2405 * @retval true A match was found within the string.
2406 * @retval false No match was found within the string.
2408 * @throws an exception of type regex_error.
2410 template<typename _Ch_type
, typename _Rx_traits
>
2412 regex_search(const _Ch_type
* __s
,
2413 const basic_regex
<_Ch_type
, _Rx_traits
>& __e
,
2414 regex_constants::match_flag_type __f
2415 = regex_constants::match_default
)
2416 { return regex_search(__s
, __s
+ _Rx_traits::length(__s
), __e
, __f
); }
2419 * @brief Searches for a regular expression within a string.
2420 * @param __s [IN] The string to search.
2421 * @param __e [IN] The regular expression to search for.
2422 * @param __flags [IN] Search policy flags.
2423 * @retval true A match was found within the string.
2424 * @retval false No match was found within the string.
2426 * @throws an exception of type regex_error.
2428 template<typename _Ch_traits
, typename _String_allocator
,
2429 typename _Ch_type
, typename _Rx_traits
>
2431 regex_search(const basic_string
<_Ch_type
, _Ch_traits
,
2432 _String_allocator
>& __s
,
2433 const basic_regex
<_Ch_type
, _Rx_traits
>& __e
,
2434 regex_constants::match_flag_type __flags
2435 = regex_constants::match_default
)
2436 { return regex_search(__s
.begin(), __s
.end(), __e
, __flags
); }
2439 * @brief Searches for a regular expression within a string.
2440 * @param __s [IN] A C++ string to search for the regex.
2441 * @param __m [OUT] The set of regex matches.
2442 * @param __e [IN] The regex to search for in @p s.
2443 * @param __f [IN] The search flags.
2444 * @retval true A match was found within the string.
2445 * @retval false No match was found within the string, the content of %m is
2448 * @throws an exception of type regex_error.
2450 template<typename _Ch_traits
, typename _Ch_alloc
,
2451 typename _Alloc
, typename _Ch_type
,
2452 typename _Rx_traits
>
2454 regex_search(const basic_string
<_Ch_type
, _Ch_traits
, _Ch_alloc
>& __s
,
2455 match_results
<typename basic_string
<_Ch_type
,
2456 _Ch_traits
, _Ch_alloc
>::const_iterator
, _Alloc
>& __m
,
2457 const basic_regex
<_Ch_type
, _Rx_traits
>& __e
,
2458 regex_constants::match_flag_type __f
2459 = regex_constants::match_default
)
2460 { return regex_search(__s
.begin(), __s
.end(), __m
, __e
, __f
); }
2462 // std [28.11.4] Function template regex_replace
2473 * @throws an exception of type regex_error.
2475 * @todo Implement this function.
2477 template<typename _Out_iter
, typename _Bi_iter
,
2478 typename _Rx_traits
, typename _Ch_type
>
2480 regex_replace(_Out_iter __out
, _Bi_iter __first
, _Bi_iter __last
,
2481 const basic_regex
<_Ch_type
, _Rx_traits
>& __e
,
2482 const basic_string
<_Ch_type
>& __fmt
,
2483 regex_constants::match_flag_type __flags
2484 = regex_constants::match_default
)
2494 * @returns a copy of string @p s with replacements.
2496 * @throws an exception of type regex_error.
2498 template<typename _Rx_traits
, typename _Ch_type
>
2499 inline basic_string
<_Ch_type
>
2500 regex_replace(const basic_string
<_Ch_type
>& __s
,
2501 const basic_regex
<_Ch_type
, _Rx_traits
>& __e
,
2502 const basic_string
<_Ch_type
>& __fmt
,
2503 regex_constants::match_flag_type __flags
2504 = regex_constants::match_default
)
2506 basic_string
<_Ch_type
> __result
;
2507 regex_replace(std::back_inserter(__result
),
2508 __s
.begin(), __s
.end(), __e
, __fmt
, __flags
);
2514 // std [28.12] Class template regex_iterator
2516 * An iterator adaptor that will provide repeated calls of regex_search over
2517 * a range until no more matches remain.
2519 template<typename _Bi_iter
,
2520 typename _Ch_type
= typename iterator_traits
<_Bi_iter
>::value_type
,
2521 typename _Rx_traits
= regex_traits
<_Ch_type
> >
2522 class regex_iterator
2525 typedef basic_regex
<_Ch_type
, _Rx_traits
> regex_type
;
2526 typedef match_results
<_Bi_iter
> value_type
;
2527 typedef std::ptrdiff_t difference_type
;
2528 typedef const value_type
* pointer
;
2529 typedef const value_type
& reference
;
2530 typedef std::forward_iterator_tag iterator_category
;
2533 * @brief Provides a singular iterator, useful for indicating
2534 * one-past-the-end of a range.
2541 * Constructs a %regex_iterator...
2542 * @param __a [IN] The start of a text range to search.
2543 * @param __b [IN] One-past-the-end of the text range to search.
2544 * @param __re [IN] The regular expression to match.
2545 * @param __m [IN] Policy flags for match rules.
2547 regex_iterator(_Bi_iter __a
, _Bi_iter __b
, const regex_type
& __re
,
2548 regex_constants::match_flag_type __m
2549 = regex_constants::match_default
)
2550 : _M_begin(__a
), _M_end(__b
), _M_pregex(&__re
), _M_flags(__m
), _M_match()
2551 { regex_search(_M_begin
, _M_end
, _M_match
, *_M_pregex
, _M_flags
); }
2554 * Copy constructs a %regex_iterator.
2556 regex_iterator(const regex_iterator
& __rhs
) = default;
2559 * @brief Assigns one %regex_iterator to another.
2562 operator=(const regex_iterator
& __rhs
) = default;
2565 * @brief Tests the equivalence of two regex iterators.
2568 operator==(const regex_iterator
& __rhs
) const;
2571 * @brief Tests the inequivalence of two regex iterators.
2574 operator!=(const regex_iterator
& __rhs
) const
2575 { return !(*this == __rhs
); }
2578 * @brief Dereferences a %regex_iterator.
2582 { return _M_match
; }
2585 * @brief Selects a %regex_iterator member.
2589 { return &_M_match
; }
2592 * @brief Increments a %regex_iterator.
2598 * @brief Postincrements a %regex_iterator.
2611 const regex_type
* _M_pregex
;
2612 regex_constants::match_flag_type _M_flags
;
2613 match_results
<_Bi_iter
> _M_match
;
2616 template<typename _Bi_iter
,
2618 typename _Rx_traits
>
2620 regex_iterator
<_Bi_iter
, _Ch_type
, _Rx_traits
>::
2621 operator==(const regex_iterator
& __rhs
) const
2623 return (_M_match
.empty() && __rhs
._M_match
.empty())
2624 || (_M_begin
== __rhs
._M_begin
2625 && _M_end
== __rhs
._M_end
2626 && _M_pregex
== __rhs
._M_pregex
2627 && _M_flags
== __rhs
._M_flags
2628 && _M_match
[0] == __rhs
._M_match
[0]);
2631 template<typename _Bi_iter
,
2633 typename _Rx_traits
>
2634 regex_iterator
<_Bi_iter
, _Ch_type
, _Rx_traits
>&
2635 regex_iterator
<_Bi_iter
, _Ch_type
, _Rx_traits
>::
2638 // FIXME: In all cases in which the call to regex_search returns true,
2639 // match.prefix().first shall be equal to the previous value of
2640 // match[0].second, and for each index i in the half-open range
2641 // [0, match.size()) for which match[i].matched is true,
2642 // match[i].position() shall return distance(begin, match[i].first).
2644 if (_M_match
[0].matched
)
2646 auto __start
= _M_match
[0].second
;
2647 if (_M_match
[0].first
== _M_match
[0].second
)
2648 if (__start
== _M_end
)
2650 _M_match
= value_type();
2655 if (regex_search(__start
, _M_end
, _M_match
, *_M_pregex
, _M_flags
2656 | regex_constants::match_not_null
2657 | regex_constants::match_continuous
))
2662 _M_flags
|= regex_constants::match_prev_avail
;
2663 if (!regex_search(__start
, _M_end
, _M_match
, *_M_pregex
, _M_flags
))
2664 _M_match
= value_type();
2669 typedef regex_iterator
<const char*> cregex_iterator
;
2670 typedef regex_iterator
<string::const_iterator
> sregex_iterator
;
2671 #ifdef _GLIBCXX_USE_WCHAR_T
2672 typedef regex_iterator
<const wchar_t*> wcregex_iterator
;
2673 typedef regex_iterator
<wstring::const_iterator
> wsregex_iterator
;
2676 // [7.12.2] Class template regex_token_iterator
2678 * Iterates over submatches in a range (or @a splits a text string).
2680 * The purpose of this iterator is to enumerate all, or all specified,
2681 * matches of a regular expression within a text range. The dereferenced
2682 * value of an iterator of this class is a std::sub_match object.
2684 template<typename _Bi_iter
,
2685 typename _Ch_type
= typename iterator_traits
<_Bi_iter
>::value_type
,
2686 typename _Rx_traits
= regex_traits
<_Ch_type
> >
2687 class regex_token_iterator
2690 typedef basic_regex
<_Ch_type
, _Rx_traits
> regex_type
;
2691 typedef sub_match
<_Bi_iter
> value_type
;
2692 typedef std::ptrdiff_t difference_type
;
2693 typedef const value_type
* pointer
;
2694 typedef const value_type
& reference
;
2695 typedef std::forward_iterator_tag iterator_category
;
2699 * @brief Default constructs a %regex_token_iterator.
2701 * A default-constructed %regex_token_iterator is a singular iterator
2702 * that will compare equal to the one-past-the-end value for any
2703 * iterator of the same type.
2705 regex_token_iterator()
2706 : _M_position(), _M_result(nullptr), _M_suffix(), _M_n(0), _M_subs()
2710 * Constructs a %regex_token_iterator...
2711 * @param __a [IN] The start of the text to search.
2712 * @param __b [IN] One-past-the-end of the text to search.
2713 * @param __re [IN] The regular expression to search for.
2714 * @param __submatch [IN] Which submatch to return. There are some
2715 * special values for this parameter:
2716 * - -1 each enumerated subexpression does NOT
2717 * match the regular expression (aka field
2719 * - 0 the entire string matching the
2720 * subexpression is returned for each match
2722 * - >0 enumerates only the indicated
2723 * subexpression from a match within the text.
2724 * @param __m [IN] Policy flags for match rules.
2726 regex_token_iterator(_Bi_iter __a
, _Bi_iter __b
, const regex_type
& __re
,
2728 regex_constants::match_flag_type __m
2729 = regex_constants::match_default
)
2730 : _M_position(__a
, __b
, __re
, __m
), _M_subs(1, __submatch
), _M_n(0)
2731 { _M_init(__a
, __b
); }
2734 * Constructs a %regex_token_iterator...
2735 * @param __a [IN] The start of the text to search.
2736 * @param __b [IN] One-past-the-end of the text to search.
2737 * @param __re [IN] The regular expression to search for.
2738 * @param __submatches [IN] A list of subexpressions to return for each
2739 * regular expression match within the text.
2740 * @param __m [IN] Policy flags for match rules.
2742 regex_token_iterator(_Bi_iter __a
, _Bi_iter __b
,
2743 const regex_type
& __re
,
2744 const std::vector
<int>& __submatches
,
2745 regex_constants::match_flag_type __m
2746 = regex_constants::match_default
)
2747 : _M_position(__a
, __b
, __re
, __m
), _M_subs(__submatches
), _M_n(0)
2748 { _M_init(__a
, __b
); }
2751 * Constructs a %regex_token_iterator...
2752 * @param __a [IN] The start of the text to search.
2753 * @param __b [IN] One-past-the-end of the text to search.
2754 * @param __re [IN] The regular expression to search for.
2755 * @param __submatches [IN] A list of subexpressions to return for each
2756 * regular expression match within the text.
2757 * @param __m [IN] Policy flags for match rules.
2759 regex_token_iterator(_Bi_iter __a
, _Bi_iter __b
,
2760 const regex_type
& __re
,
2761 initializer_list
<int> __submatches
,
2762 regex_constants::match_flag_type __m
2763 = regex_constants::match_default
)
2764 : _M_position(__a
, __b
, __re
, __m
), _M_subs(__submatches
), _M_n(0)
2765 { _M_init(__a
, __b
); }
2768 * Constructs a %regex_token_iterator...
2769 * @param __a [IN] The start of the text to search.
2770 * @param __b [IN] One-past-the-end of the text to search.
2771 * @param __re [IN] The regular expression to search for.
2772 * @param __submatches [IN] A list of subexpressions to return for each
2773 * regular expression match within the text.
2774 * @param __m [IN] Policy flags for match rules.
2776 template<std::size_t _Nm
>
2777 regex_token_iterator(_Bi_iter __a
, _Bi_iter __b
,
2778 const regex_type
& __re
,
2779 const int (&__submatches
)[_Nm
],
2780 regex_constants::match_flag_type __m
2781 = regex_constants::match_default
)
2782 : _M_position(__a
, __b
, __re
, __m
),
2783 _M_subs(__submatches
, *(&__submatches
+1)), _M_n(0)
2784 { _M_init(__a
, __b
); }
2787 * @brief Copy constructs a %regex_token_iterator.
2788 * @param __rhs [IN] A %regex_token_iterator to copy.
2790 regex_token_iterator(const regex_token_iterator
& __rhs
)
2791 : _M_position(__rhs
.position
), _M_subs(__rhs
.subs
), _M_n(__rhs
.N
),
2792 _M_result(__rhs
.result
), _M_suffix(__rhs
.suffix
),
2793 _M_has_m1(__rhs
._M_has_m1
)
2795 if (__rhs
._M_result
== &__rhs
._M_suffix
)
2796 _M_result
= &_M_suffix
;
2800 * @brief Assigns a %regex_token_iterator to another.
2801 * @param __rhs [IN] A %regex_token_iterator to copy.
2803 regex_token_iterator
&
2804 operator=(const regex_token_iterator
& __rhs
);
2807 * @brief Compares a %regex_token_iterator to another for equality.
2810 operator==(const regex_token_iterator
& __rhs
) const;
2813 * @brief Compares a %regex_token_iterator to another for inequality.
2816 operator!=(const regex_token_iterator
& __rhs
) const
2817 { return !(*this == __rhs
); }
2820 * @brief Dereferences a %regex_token_iterator.
2824 { return *_M_result
; }
2827 * @brief Selects a %regex_token_iterator member.
2831 { return _M_result
; }
2834 * @brief Increments a %regex_token_iterator.
2836 regex_token_iterator
&
2840 * @brief Postincrements a %regex_token_iterator.
2842 regex_token_iterator
2851 typedef regex_iterator
<_Bi_iter
, _Ch_type
, _Rx_traits
> _Position
;
2854 _M_init(_Bi_iter __a
, _Bi_iter __b
);
2857 _M_current_match() const
2859 if (_M_subs
[_M_n
] == -1)
2860 return (*_M_position
).prefix();
2862 return (*_M_position
)[_M_subs
[_M_n
]];
2866 _M_end_of_seq() const
2867 { return _M_result
!= nullptr; }
2869 _Position _M_position
;
2870 const value_type
* _M_result
;
2871 value_type _M_suffix
;
2873 std::vector
<int> _M_subs
;
2875 // Show whether _M_subs contains -1
2879 template<typename _Bi_iter
,
2881 typename _Rx_traits
>
2882 regex_token_iterator
<_Bi_iter
, _Ch_type
, _Rx_traits
>&
2883 regex_token_iterator
<_Bi_iter
, _Ch_type
, _Rx_traits
>::
2884 operator=(const regex_token_iterator
& __rhs
)
2886 _M_position
= __rhs
._M_position
;
2887 _M_subs
= __rhs
._M_subs
;
2889 _M_result
= __rhs
._M_result
;
2890 _M_suffix
= __rhs
._M_suffix
;
2891 _M_has_m1
= __rhs
._M_has_m1
;
2892 if (__rhs
._M_result
== &__rhs
._M_suffix
)
2893 _M_result
= &_M_suffix
;
2896 template<typename _Bi_iter
,
2898 typename _Rx_traits
>
2900 regex_token_iterator
<_Bi_iter
, _Ch_type
, _Rx_traits
>::
2901 operator==(const regex_token_iterator
& __rhs
) const
2903 if (_M_end_of_seq() && __rhs
._M_end_of_seq())
2905 if (_M_suffix
.matched
&& __rhs
._M_suffix
.matched
2906 && _M_suffix
== __rhs
._M_suffix
)
2908 if (_M_end_of_seq() || _M_suffix
.matched
2909 || __rhs
._M_end_of_seq() || __rhs
._M_suffix
.matched
)
2911 return _M_position
== __rhs
._M_position
2912 && _M_n
== __rhs
._M_n
2913 && _M_subs
== __rhs
._M_subs
;
2916 template<typename _Bi_iter
,
2918 typename _Rx_traits
>
2919 regex_token_iterator
<_Bi_iter
, _Ch_type
, _Rx_traits
>&
2920 regex_token_iterator
<_Bi_iter
, _Ch_type
, _Rx_traits
>::
2923 _Position __prev
= _M_position
;
2924 if (_M_suffix
.matched
)
2925 *this = regex_token_iterator();
2926 else if (_M_n
+ 1 < _M_subs
.size())
2929 _M_result
= &_M_current_match();
2935 if (_M_position
!= _Position())
2936 _M_result
= &_M_current_match();
2937 else if (_M_has_m1
&& __prev
->suffix().length() != 0)
2939 _M_suffix
.matched
= true;
2940 _M_suffix
.first
= __prev
->suffix().first
;
2941 _M_suffix
.second
= __prev
->suffix().second
;
2942 _M_result
= &_M_suffix
;
2945 *this = regex_token_iterator();
2950 template<typename _Bi_iter
,
2952 typename _Rx_traits
>
2954 regex_token_iterator
<_Bi_iter
, _Ch_type
, _Rx_traits
>::
2955 _M_init(_Bi_iter __a
, _Bi_iter __b
)
2958 for (auto __it
: _M_subs
)
2964 if (_M_position
!= _Position())
2965 _M_result
= &_M_current_match();
2968 _M_suffix
.matched
= true;
2969 _M_suffix
.first
= __a
;
2970 _M_suffix
.second
= __b
;
2971 _M_result
= &_M_suffix
;
2974 _M_result
= nullptr;
2977 /** @brief Token iterator for C-style NULL-terminated strings. */
2978 typedef regex_token_iterator
<const char*> cregex_token_iterator
;
2980 /** @brief Token iterator for standard strings. */
2981 typedef regex_token_iterator
<string::const_iterator
> sregex_token_iterator
;
2983 #ifdef _GLIBCXX_USE_WCHAR_T
2984 /** @brief Token iterator for C-style NULL-terminated wide strings. */
2985 typedef regex_token_iterator
<const wchar_t*> wcregex_token_iterator
;
2987 /** @brief Token iterator for standard wide-character strings. */
2988 typedef regex_token_iterator
<wstring::const_iterator
> wsregex_token_iterator
;
2992 _GLIBCXX_END_NAMESPACE_VERSION