Skip to content

Commit 6faae13

Browse files
authored
[libc++] Simplify the definition of string::operator== (#95000)
Instead of hardcoding a loop for small strings, always call char_traits::compare which ends up desugaring to __builtin_memcmp. Note that the original code dates back 11 years, when we didn't lower to intrinsics in `char_traits::compare`. Fixes #94222
1 parent f638f7b commit 6faae13

File tree

1 file changed

+3
-10
lines changed

1 file changed

+3
-10
lines changed

libcxx/include/string

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3746,17 +3746,10 @@ template <class _Allocator>
37463746
inline _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool
37473747
operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
37483748
const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT {
3749-
size_t __lhs_sz = __lhs.size();
3750-
if (__lhs_sz != __rhs.size())
3749+
size_t __sz = __lhs.size();
3750+
if (__sz != __rhs.size())
37513751
return false;
3752-
const char* __lp = __lhs.data();
3753-
const char* __rp = __rhs.data();
3754-
if (__lhs.__is_long())
3755-
return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
3756-
for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
3757-
if (*__lp != *__rp)
3758-
return false;
3759-
return true;
3752+
return char_traits<char>::compare(__lhs.data(), __rhs.data(), __sz) == 0;
37603753
}
37613754

37623755
#if _LIBCPP_STD_VER <= 17

0 commit comments

Comments
 (0)