[libcxx-commits] [libcxx] [libc++][NFC] Simplify string a bit (PR #127135)

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Sun Feb 16 01:52:46 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);
----------------
philnik777 wrote:

```suggestion
  if (auto __len = traits_type::length(__s); __builtin_constant_p(__len) && __fits_in_sso(__n))
    return __assign_short(__s, __len);
  return __assign_external(__s);
```

WDYT?

https://github.com/llvm/llvm-project/pull/127135


More information about the libcxx-commits mailing list