Skip to content

Commit f8d5576

Browse files
committed
[libc++][NFC] Simplify the implementation of string and string_views operator==
1 parent 4acf935 commit f8d5576

File tree

2 files changed

+20
-60
lines changed

2 files changed

+20
-60
lines changed

libcxx/include/string

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1879,11 +1879,6 @@ public:
18791879
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __clear_and_shrink() _NOEXCEPT;
18801880

18811881
private:
1882-
template <class _Alloc>
1883-
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool friend
1884-
operator==(const basic_string<char, char_traits<char>, _Alloc>& __lhs,
1885-
const basic_string<char, char_traits<char>, _Alloc>& __rhs) _NOEXCEPT;
1886-
18871882
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __shrink_or_extend(size_type __target_capacity);
18881883

18891884
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS bool
@@ -3840,36 +3835,9 @@ template <class _CharT, class _Traits, class _Allocator>
38403835
inline _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool
38413836
operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
38423837
const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {
3843-
#if _LIBCPP_STD_VER >= 20
3844-
return basic_string_view<_CharT, _Traits>(__lhs) == basic_string_view<_CharT, _Traits>(__rhs);
3845-
#else
38463838
size_t __lhs_sz = __lhs.size();
38473839
return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(), __rhs.data(), __lhs_sz) == 0;
3848-
#endif
3849-
}
3850-
3851-
template <class _Allocator>
3852-
inline _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool
3853-
operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
3854-
const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT {
3855-
size_t __sz = __lhs.size();
3856-
if (__sz != __rhs.size())
3857-
return false;
3858-
return char_traits<char>::compare(__lhs.data(), __rhs.data(), __sz) == 0;
3859-
}
3860-
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-
typedef basic_string<_CharT, _Traits, _Allocator> _String;
3866-
_LIBCPP_ASSERT_NON_NULL(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
3867-
size_t __lhs_len = _Traits::length(__lhs);
3868-
if (__lhs_len != __rhs.size())
3869-
return false;
3870-
return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
38713840
}
3872-
#endif // _LIBCPP_STD_VER <= 17
38733841

38743842
template <class _CharT, class _Traits, class _Allocator>
38753843
inline _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool
@@ -3888,6 +3856,14 @@ operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT*
38883856
return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
38893857
}
38903858

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

38933869
template <class _CharT, class _Traits, class _Allocator>

libcxx/include/string_view

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

721721
// [string.view.comparison]
722722

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

734+
#if _LIBCPP_STD_VER >= 20
735+
733736
template <class _CharT, class _Traits>
734737
_LIBCPP_HIDE_FROM_ABI constexpr auto operator<=>(basic_string_view<_CharT, _Traits> __lhs,
735738
type_identity_t<basic_string_view<_CharT, _Traits>> __rhs) noexcept {
@@ -755,51 +758,32 @@ operator==(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _
755758
return __lhs.compare(__rhs) == 0;
756759
}
757760

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

778768
// operator !=
779769
template <class _CharT, class _Traits>
780770
_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
781771
operator!=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT {
782-
if (__lhs.size() != __rhs.size())
783-
return true;
784-
return __lhs.compare(__rhs) != 0;
772+
return !(__lhs == __rhs);
785773
}
786774

787775
template <class _CharT, class _Traits, int = 1>
788776
_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
789777
operator!=(basic_string_view<_CharT, _Traits> __lhs,
790778
__type_identity_t<basic_string_view<_CharT, _Traits> > __rhs) _NOEXCEPT {
791-
if (__lhs.size() != __rhs.size())
792-
return true;
793-
return __lhs.compare(__rhs) != 0;
779+
return !(__lhs == __rhs);
794780
}
795781

796782
template <class _CharT, class _Traits, int = 2>
797783
_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
798784
operator!=(__type_identity_t<basic_string_view<_CharT, _Traits> > __lhs,
799785
basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT {
800-
if (__lhs.size() != __rhs.size())
801-
return true;
802-
return __lhs.compare(__rhs) != 0;
786+
return !(__lhs == __rhs);
803787
}
804788

805789
// operator <

0 commit comments

Comments
 (0)