[libc-commits] [libc] [llvm] [libc][cpp::string] Allocate fewer temp strings in operator= and += (PR #210895)

Jackson Stogel via libc-commits libc-commits at lists.llvm.org
Fri Jul 24 17:53:26 PDT 2026


================
@@ -189,20 +244,25 @@ class string {
     return res;
   }
 
-  LIBC_INLINE string &operator+=(const string &rhs) {
-    const size_t new_size = size_ + rhs.size();
-    reserve(new_size);
-    inline_memcpy(buffer_ + size_, rhs.data(), rhs.size());
+  LIBC_INLINE string &append(cpp::string_view view) {
+    if (view.empty())
+      return *this;
+
+    if (capacity_ - size_ <= view.size()) {
----------------
jtstogel wrote:

I think it was fine, but switched to calling `capacity()` and it's much clearer

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


More information about the libc-commits mailing list