[libcxx-commits] [libcxx] [libc++][NFC] Simplify string a bit (PR #127135)
Peng Liu via libcxx-commits
libcxx-commits at lists.llvm.org
Thu Feb 20 08:49:09 PST 2025
================
@@ -3014,10 +3004,11 @@ template <class _CharT, class _Traits, class _Allocator>
_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s) {
_LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::assign received nullptr");
- return __builtin_constant_p(*__s)
- ? (__fits_in_sso(traits_type::length(__s)) ? __assign_short(__s, traits_type::length(__s))
- : __assign_external(__s, traits_type::length(__s)))
- : __assign_external(__s);
+ if (__builtin_constant_p(*__s)) {
+ const size_type __n = traits_type::length(__s);
+ return __fits_in_sso(__n) ? __assign_short(__s, __n) : __assign_external(__s, __n);
+ }
+ return __assign_external(__s);
----------------
winner245 wrote:
Looks great—adopted! I have also double-checked that even with basic compiler optimization level (-O1), they yield the same code (https://godbolt.org/z/3jqM8cYEv), and the way you suggested is definitely the simplest.
https://github.com/llvm/llvm-project/pull/127135
More information about the libcxx-commits
mailing list