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