[libcxx-commits] [libcxx] [libc++][NFC] Simplify the implementation of string and string_views operator== (PR #117184)
Nikolas Klauser via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Dec 10 12:07:57 PST 2024
https://github.com/philnik777 updated https://github.com/llvm/llvm-project/pull/117184
>From ecdc97b40d17c12eac96b368494a57d4a6572c3a Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Thu, 21 Nov 2024 17:29:25 +0100
Subject: [PATCH] [libc++][NFC] Simplify the implementation of string and
string_views operator==
---
libcxx/include/string | 42 ++++++++------------------------------
libcxx/include/string_view | 40 +++++++++++-------------------------
2 files changed, 21 insertions(+), 61 deletions(-)
diff --git a/libcxx/include/string b/libcxx/include/string
index 17bf4b3b98bf34..c7a87053d4856d 100644
--- a/libcxx/include/string
+++ b/libcxx/include/string
@@ -1881,11 +1881,6 @@ public:
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __clear_and_shrink() _NOEXCEPT;
private:
- template <class _Alloc>
- inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool friend
- operator==(const basic_string<char, char_traits<char>, _Alloc>& __lhs,
- const basic_string<char, char_traits<char>, _Alloc>& __rhs) _NOEXCEPT;
-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __shrink_or_extend(size_type __target_capacity);
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS bool
@@ -3842,37 +3837,10 @@ template <class _CharT, class _Traits, class _Allocator>
inline _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool
operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {
-# if _LIBCPP_STD_VER >= 20
- return basic_string_view<_CharT, _Traits>(__lhs) == basic_string_view<_CharT, _Traits>(__rhs);
-# else
size_t __lhs_sz = __lhs.size();
return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(), __rhs.data(), __lhs_sz) == 0;
-# endif
}
-template <class _Allocator>
-inline _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool
-operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
- const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT {
- size_t __sz = __lhs.size();
- if (__sz != __rhs.size())
- return false;
- return char_traits<char>::compare(__lhs.data(), __rhs.data(), __sz) == 0;
-}
-
-# if _LIBCPP_STD_VER <= 17
-template <class _CharT, class _Traits, class _Allocator>
-inline _LIBCPP_HIDE_FROM_ABI bool
-operator==(const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {
- typedef basic_string<_CharT, _Traits, _Allocator> _String;
- _LIBCPP_ASSERT_NON_NULL(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
- size_t __lhs_len = _Traits::length(__lhs);
- if (__lhs_len != __rhs.size())
- return false;
- return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
-}
-# endif // _LIBCPP_STD_VER <= 17
-
template <class _CharT, class _Traits, class _Allocator>
inline _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool
operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs) _NOEXCEPT {
@@ -3890,7 +3858,15 @@ operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT*
return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
}
-# if _LIBCPP_STD_VER >= 20
+#if _LIBCPP_STD_VER <= 17
+template <class _CharT, class _Traits, class _Allocator>
+inline _LIBCPP_HIDE_FROM_ABI bool
+operator==(const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {
+ return __rhs == __lhs;
+}
+#endif // _LIBCPP_STD_VER <= 17
+
+#if _LIBCPP_STD_VER >= 20
template <class _CharT, class _Traits, class _Allocator>
_LIBCPP_HIDE_FROM_ABI constexpr auto operator<=>(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
diff --git a/libcxx/include/string_view b/libcxx/include/string_view
index 27b9f152ea290a..8d817bb5c25b06 100644
--- a/libcxx/include/string_view
+++ b/libcxx/include/string_view
@@ -722,16 +722,19 @@ basic_string_view(_Range) -> basic_string_view<ranges::range_value_t<_Range>>;
// [string.view.comparison]
-# if _LIBCPP_STD_VER >= 20
-
-template <class _CharT, class _Traits>
-_LIBCPP_HIDE_FROM_ABI constexpr bool operator==(basic_string_view<_CharT, _Traits> __lhs,
- type_identity_t<basic_string_view<_CharT, _Traits>> __rhs) noexcept {
+// The dummy default template parameters are used to work around a MSVC issue with mangling, see VSO-409326 for details.
+// This applies to the other sufficient overloads below for the other comparison operators.
+template <class _CharT, class _Traits, int = 1>
+_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
+operator==(basic_string_view<_CharT, _Traits> __lhs,
+ __type_identity_t<basic_string_view<_CharT, _Traits> > __rhs) _NOEXCEPT {
if (__lhs.size() != __rhs.size())
return false;
return __lhs.compare(__rhs) == 0;
}
+#if _LIBCPP_STD_VER >= 20
+
template <class _CharT, class _Traits>
_LIBCPP_HIDE_FROM_ABI constexpr auto operator<=>(basic_string_view<_CharT, _Traits> __lhs,
type_identity_t<basic_string_view<_CharT, _Traits>> __rhs) noexcept {
@@ -757,51 +760,32 @@ operator==(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _
return __lhs.compare(__rhs) == 0;
}
-// The dummy default template parameters are used to work around a MSVC issue with mangling, see VSO-409326 for details.
-// This applies to the other sufficient overloads below for the other comparison operators.
-template <class _CharT, class _Traits, int = 1>
-_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
-operator==(basic_string_view<_CharT, _Traits> __lhs,
- __type_identity_t<basic_string_view<_CharT, _Traits> > __rhs) _NOEXCEPT {
- if (__lhs.size() != __rhs.size())
- return false;
- return __lhs.compare(__rhs) == 0;
-}
-
template <class _CharT, class _Traits, int = 2>
_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
operator==(__type_identity_t<basic_string_view<_CharT, _Traits> > __lhs,
basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT {
- if (__lhs.size() != __rhs.size())
- return false;
- return __lhs.compare(__rhs) == 0;
+ return __lhs == __rhs;
}
// operator !=
template <class _CharT, class _Traits>
_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
operator!=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT {
- if (__lhs.size() != __rhs.size())
- return true;
- return __lhs.compare(__rhs) != 0;
+ return !(__lhs == __rhs);
}
template <class _CharT, class _Traits, int = 1>
_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
operator!=(basic_string_view<_CharT, _Traits> __lhs,
__type_identity_t<basic_string_view<_CharT, _Traits> > __rhs) _NOEXCEPT {
- if (__lhs.size() != __rhs.size())
- return true;
- return __lhs.compare(__rhs) != 0;
+ return !(__lhs == __rhs);
}
template <class _CharT, class _Traits, int = 2>
_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
operator!=(__type_identity_t<basic_string_view<_CharT, _Traits> > __lhs,
basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT {
- if (__lhs.size() != __rhs.size())
- return true;
- return __lhs.compare(__rhs) != 0;
+ return !(__lhs == __rhs);
}
// operator <
More information about the libcxx-commits
mailing list