Skip to content

Commit ecdc97b

Browse files
committed
[libc++][NFC] Simplify the implementation of string and string_views operator==
1 parent 5a0d73b commit ecdc97b

File tree

2 files changed

+21
-61
lines changed

2 files changed

+21
-61
lines changed

libcxx/include/string

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1881,11 +1881,6 @@ public:
18811881
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __clear_and_shrink() _NOEXCEPT;
18821882

18831883
private:
1884-
template <class _Alloc>
1885-
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool friend
1886-
operator==(const basic_string<char, char_traits<char>, _Alloc>& __lhs,
1887-
const basic_string<char, char_traits<char>, _Alloc>& __rhs) _NOEXCEPT;
1888-
18891884
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __shrink_or_extend(size_type __target_capacity);
18901885

18911886
_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>
38423837
inline _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool
38433838
operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
38443839
const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {
3845-
# if _LIBCPP_STD_VER >= 20
3846-
return basic_string_view<_CharT, _Traits>(__lhs) == basic_string_view<_CharT, _Traits>(__rhs);
3847-
# else
38483840
size_t __lhs_sz = __lhs.size();
38493841
return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(), __rhs.data(), __lhs_sz) == 0;
3850-
# endif
38513842
}
38523843

3853-
template <class _Allocator>
3854-
inline _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool
3855-
operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
3856-
const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT {
3857-
size_t __sz = __lhs.size();
3858-
if (__sz != __rhs.size())
3859-
return false;
3860-
return char_traits<char>::compare(__lhs.data(), __rhs.data(), __sz) == 0;
3861-
}
3862-
3863-
# if _LIBCPP_STD_VER <= 17
3864-
template <class _CharT, class _Traits, class _Allocator>
3865-
inline _LIBCPP_HIDE_FROM_ABI bool
3866-
operator==(const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {
3867-
typedef basic_string<_CharT, _Traits, _Allocator> _String;
3868-
_LIBCPP_ASSERT_NON_NULL(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
3869-
size_t __lhs_len = _Traits::length(__lhs);
3870-
if (__lhs_len != __rhs.size())
3871-
return false;
3872-
return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
3873-
}
3874-
# endif // _LIBCPP_STD_VER <= 17
3875-
38763844
template <class _CharT, class _Traits, class _Allocator>
38773845
inline _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool
38783846
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*
38903858
return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
38913859
}
38923860

3893-
# if _LIBCPP_STD_VER >= 20
3861+
#if _LIBCPP_STD_VER <= 17
3862+
template <class _CharT, class _Traits, class _Allocator>
3863+
inline _LIBCPP_HIDE_FROM_ABI bool
3864+
operator==(const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {
3865+
return __rhs == __lhs;
3866+
}
3867+
#endif // _LIBCPP_STD_VER <= 17
3868+
3869+
#if _LIBCPP_STD_VER >= 20
38943870

38953871
template <class _CharT, class _Traits, class _Allocator>
38963872
_LIBCPP_HIDE_FROM_ABI constexpr auto operator<=>(const basic_string<_CharT, _Traits, _Allocator>& __lhs,

libcxx/include/string_view

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -722,16 +722,19 @@ basic_string_view(_Range) -> basic_string_view<ranges::range_value_t<_Range>>;
722722

723723
// [string.view.comparison]
724724

725-
# if _LIBCPP_STD_VER >= 20
726-
727-
template <class _CharT, class _Traits>
728-
_LIBCPP_HIDE_FROM_ABI constexpr bool operator==(basic_string_view<_CharT, _Traits> __lhs,
729-
type_identity_t<basic_string_view<_CharT, _Traits>> __rhs) noexcept {
725+
// The dummy default template parameters are used to work around a MSVC issue with mangling, see VSO-409326 for details.
726+
// This applies to the other sufficient overloads below for the other comparison operators.
727+
template <class _CharT, class _Traits, int = 1>
728+
_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
729+
operator==(basic_string_view<_CharT, _Traits> __lhs,
730+
__type_identity_t<basic_string_view<_CharT, _Traits> > __rhs) _NOEXCEPT {
730731
if (__lhs.size() != __rhs.size())
731732
return false;
732733
return __lhs.compare(__rhs) == 0;
733734
}
734735

736+
#if _LIBCPP_STD_VER >= 20
737+
735738
template <class _CharT, class _Traits>
736739
_LIBCPP_HIDE_FROM_ABI constexpr auto operator<=>(basic_string_view<_CharT, _Traits> __lhs,
737740
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, _
757760
return __lhs.compare(__rhs) == 0;
758761
}
759762

760-
// The dummy default template parameters are used to work around a MSVC issue with mangling, see VSO-409326 for details.
761-
// This applies to the other sufficient overloads below for the other comparison operators.
762-
template <class _CharT, class _Traits, int = 1>
763-
_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
764-
operator==(basic_string_view<_CharT, _Traits> __lhs,
765-
__type_identity_t<basic_string_view<_CharT, _Traits> > __rhs) _NOEXCEPT {
766-
if (__lhs.size() != __rhs.size())
767-
return false;
768-
return __lhs.compare(__rhs) == 0;
769-
}
770-
771763
template <class _CharT, class _Traits, int = 2>
772764
_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
773765
operator==(__type_identity_t<basic_string_view<_CharT, _Traits> > __lhs,
774766
basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT {
775-
if (__lhs.size() != __rhs.size())
776-
return false;
777-
return __lhs.compare(__rhs) == 0;
767+
return __lhs == __rhs;
778768
}
779769

780770
// operator !=
781771
template <class _CharT, class _Traits>
782772
_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
783773
operator!=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT {
784-
if (__lhs.size() != __rhs.size())
785-
return true;
786-
return __lhs.compare(__rhs) != 0;
774+
return !(__lhs == __rhs);
787775
}
788776

789777
template <class _CharT, class _Traits, int = 1>
790778
_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
791779
operator!=(basic_string_view<_CharT, _Traits> __lhs,
792780
__type_identity_t<basic_string_view<_CharT, _Traits> > __rhs) _NOEXCEPT {
793-
if (__lhs.size() != __rhs.size())
794-
return true;
795-
return __lhs.compare(__rhs) != 0;
781+
return !(__lhs == __rhs);
796782
}
797783

798784
template <class _CharT, class _Traits, int = 2>
799785
_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
800786
operator!=(__type_identity_t<basic_string_view<_CharT, _Traits> > __lhs,
801787
basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT {
802-
if (__lhs.size() != __rhs.size())
803-
return true;
804-
return __lhs.compare(__rhs) != 0;
788+
return !(__lhs == __rhs);
805789
}
806790

807791
// operator <

0 commit comments

Comments
 (0)