summaryrefslogtreecommitdiff
path: root/libstdc++-v3/include/bits/regex.h
diff options
context:
space:
mode:
authorJonathan Wakely <jwakely@redhat.com>2020-04-17 16:24:49 +0100
committerJonathan Wakely <jwakely@redhat.com>2020-04-17 16:40:11 +0100
commit875d6cb3b4919b58ae5e6313db715bc4dd3ddd6c (patch)
tree60ec809bcce439236e5778bc5b6a8c9d4d1fbcf0 /libstdc++-v3/include/bits/regex.h
parent8b50d7a47624030d87645237c60bd8f7ac78b2ec (diff)
downloadgcc-875d6cb3b4919b58ae5e6313db715bc4dd3ddd6c.tar.gz
libstdc++: Add comparison operators for string and regex types
Some more C++20 changes from P1614R2, "The Mothership has Landed". This adds three-way comparison support to std::char_traits, std::basic_string, std::basic_string_view, and std::sub_match. * include/bits/basic_string.h (basic_string): Define operator<=> and remove redundant comparison operators for C++20. * include/bits/char_traits.h (__gnu_cxx::char_traits, char_traits): Add comparison_category members. (__detail::__char_traits_cmp_cat): New helper to get comparison category from char traits class. * include/bits/regex.h (regex_traits::_RegexMask::operator!=): Do not define for C++20. (sub_match): Define operator<=> and remove redundant comparison operators for C++20. (match_results): Remove redundant operator!= for C++20. * include/std/string_view (basic_string_view): Define operator<=> and remove redundant comparison operators for C++20. * testsuite/21_strings/basic_string/operators/char/cmp_c++20.cc: New test. * testsuite/21_strings/basic_string/operators/wchar_t/cmp_c++20.cc: New test. * testsuite/21_strings/basic_string_view/operations/copy/char/ constexpr.cc: Initialize variable. * testsuite/21_strings/basic_string_view/operations/copy/wchar_t/ constexpr.cc: Likewise. * testsuite/21_strings/basic_string_view/operators/char/2.cc: Add dg-do directive and remove comments showing incorrect signatures. * testsuite/21_strings/basic_string_view/operators/wchar_t/2.cc: Likewise. * testsuite/21_strings/basic_string_view/operators/char/cmp_c++20.cc: New test. * testsuite/21_strings/basic_string_view/operators/wchar_t/cmp_c++20.cc: New test. * testsuite/28_regex/sub_match/compare_c++20.cc: New test.
Diffstat (limited to 'libstdc++-v3/include/bits/regex.h')
-rw-r--r--libstdc++-v3/include/bits/regex.h88
1 files changed, 87 insertions, 1 deletions
diff --git a/libstdc++-v3/include/bits/regex.h b/libstdc++-v3/include/bits/regex.h
index dcae83eea4e..6db05889e8c 100644
--- a/libstdc++-v3/include/bits/regex.h
+++ b/libstdc++-v3/include/bits/regex.h
@@ -142,11 +142,13 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
&& _M_base == __other._M_base;
}
+#if __cpp_impl_three_way_comparison < 201907L
constexpr bool
operator!=(_RegexMask __other) const
{ return !((*this) == __other); }
-
+#endif
};
+
public:
typedef _RegexMask char_class_type;
@@ -1033,6 +1035,24 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
operator==(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
{ return __lhs.compare(__rhs) == 0; }
+#if __cpp_lib_three_way_comparison
+ /**
+ * @brief Three-way comparison of two regular expression submatches.
+ * @param __lhs First regular expression submatch.
+ * @param __rhs Second regular expression submatch.
+ * @returns A value indicating whether `__lhs` is less than, equal to,
+ * greater than, or incomparable with `__rhs`.
+ */
+ template<typename _BiIter>
+ inline auto
+ operator<=>(const sub_match<_BiIter>& __lhs,
+ const sub_match<_BiIter>& __rhs)
+ noexcept(__detail::__is_contiguous_iter<_BiIter>::value)
+ {
+ using _Tr = char_traits<typename iterator_traits<_BiIter>::value_type>;
+ return __detail::__char_traits_cmp_cat<_Tr>(__lhs.compare(__rhs));
+ }
+#else
/**
* @brief Tests the inequivalence of two regular expression submatches.
* @param __lhs First regular expression submatch.
@@ -1087,6 +1107,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
inline bool
operator>(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
{ return __lhs.compare(__rhs) > 0; }
+#endif // three-way comparison
/// @cond undocumented
@@ -1097,6 +1118,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
_Ch_traits, _Ch_alloc>;
/// @endcond
+#if ! __cpp_lib_three_way_comparison
/**
* @brief Tests the equivalence of a string and a regular expression
* submatch.
@@ -1170,6 +1192,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
operator<=(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
const sub_match<_Bi_iter>& __rhs)
{ return !(__rhs < __lhs); }
+#endif // three-way comparison
/**
* @brief Tests the equivalence of a regular expression submatch and a
@@ -1184,6 +1207,24 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
{ return __lhs._M_compare(__rhs.data(), __rhs.size()) == 0; }
+#if __cpp_lib_three_way_comparison
+ /**
+ * @brief Three-way comparison of a regular expression submatch and a string.
+ * @param __lhs A regular expression submatch.
+ * @param __rhs A string.
+ * @returns A value indicating whether `__lhs` is less than, equal to,
+ * greater than, or incomparable with `__rhs`.
+ */
+ template<typename _Bi_iter, typename _Ch_traits, typename _Alloc>
+ inline auto
+ operator<=>(const sub_match<_Bi_iter>& __lhs,
+ const __sub_match_string<_Bi_iter, _Ch_traits, _Alloc>& __rhs)
+ noexcept(__detail::__is_contiguous_iter<_Bi_iter>::value)
+ {
+ return __detail::__char_traits_cmp_cat<_Ch_traits>(
+ __lhs._M_compare(__rhs.data(), __rhs.size()));
+ }
+#else
/**
* @brief Tests the inequivalence of a regular expression submatch and a
* string.
@@ -1318,6 +1359,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
operator<=(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
const sub_match<_Bi_iter>& __rhs)
{ return !(__rhs < __lhs); }
+#endif // three-way comparison
/**
* @brief Tests the equivalence of a regular expression submatch and a C
@@ -1332,6 +1374,25 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
typename iterator_traits<_Bi_iter>::value_type const* __rhs)
{ return __lhs.compare(__rhs) == 0; }
+#if __cpp_lib_three_way_comparison
+ /**
+ * @brief Three-way comparison of a regular expression submatch and a C
+ * string.
+ * @param __lhs A regular expression submatch.
+ * @param __rhs A null-terminated string.
+ * @returns A value indicating whether `__lhs` is less than, equal to,
+ * greater than, or incomparable with `__rhs`.
+ */
+ template<typename _Bi_iter>
+ inline auto
+ operator<=>(const sub_match<_Bi_iter>& __lhs,
+ typename iterator_traits<_Bi_iter>::value_type const* __rhs)
+ noexcept(__detail::__is_contiguous_iter<_Bi_iter>::value)
+ {
+ using _Tr = char_traits<typename iterator_traits<_Bi_iter>::value_type>;
+ return __detail::__char_traits_cmp_cat<_Tr>(__lhs.compare(__rhs));
+ }
+#else
/**
* @brief Tests the inequivalence of a regular expression submatch and a
* string.
@@ -1470,6 +1531,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
operator<=(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
const sub_match<_Bi_iter>& __rhs)
{ return !(__rhs < __lhs); }
+#endif // three-way comparison
/**
* @brief Tests the equivalence of a regular expression submatch and a
@@ -1484,6 +1546,27 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
typename iterator_traits<_Bi_iter>::value_type const& __rhs)
{ return __lhs._M_compare(std::__addressof(__rhs), 1) == 0; }
+#if __cpp_lib_three_way_comparison
+ /**
+ * @brief Three-way comparison of a regular expression submatch and a
+ * character.
+ * @param __lhs A regular expression submatch.
+ * @param __rhs A character.
+ * @returns A value indicating whether `__lhs` is less than, equal to,
+ * greater than, or incomparable with `__rhs`.
+ */
+
+ template<typename _Bi_iter>
+ inline auto
+ operator<=>(const sub_match<_Bi_iter>& __lhs,
+ typename iterator_traits<_Bi_iter>::value_type const& __rhs)
+ noexcept(__detail::__is_contiguous_iter<_Bi_iter>::value)
+ {
+ using _Tr = char_traits<typename iterator_traits<_Bi_iter>::value_type>;
+ return __detail::__char_traits_cmp_cat<_Tr>(
+ __lhs._M_compare(std::__addressof(__rhs), 1));
+ }
+#else
/**
* @brief Tests the inequivalence of a regular expression submatch and a
* character.
@@ -1548,6 +1631,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
operator<=(const sub_match<_Bi_iter>& __lhs,
typename iterator_traits<_Bi_iter>::value_type const& __rhs)
{ return !(__rhs < __lhs); }
+#endif // three-way comparison
/**
* @brief Inserts a matched string into an output stream.
@@ -2031,6 +2115,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
&& __m1.suffix() == __m2.suffix();
}
+#if ! __cpp_lib_three_way_comparison
/**
* @brief Compares two match_results for inequality.
* @returns true if the two objects do not refer to the same match,
@@ -2041,6 +2126,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
operator!=(const match_results<_Bi_iter, _Alloc>& __m1,
const match_results<_Bi_iter, _Alloc>& __m2) noexcept
{ return !(__m1 == __m2); }
+#endif
// [7.10.6] match_results swap
/**