Skip to content

[libc++][NFC] Use early returns in basic_string::operator= #137145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 25, 2025

Conversation

philnik777
Copy link
Contributor

This makes the code a lot easier to read.

Copy link

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff HEAD~1 HEAD --extensions  -- libcxx/include/string
View the diff from clang-format here.
diff --git a/libcxx/include/string b/libcxx/include/string
index 8e8186a53..ce36b4952 100644
--- a/libcxx/include/string
+++ b/libcxx/include/string
@@ -2880,7 +2880,7 @@ basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
 
   __annotate_delete();
   auto __guard = std::__make_scope_guard(__annotate_new_size(*this));
-  __rep_ = __str.__rep_;
+  __rep_       = __str.__rep_;
 
   return *this;
 }

@philnik777 philnik777 force-pushed the string_copy_ctor_early_return branch from 8a714a7 to 2d42c08 Compare April 25, 2025 08:38
@philnik777 philnik777 marked this pull request as ready for review April 25, 2025 08:38
@philnik777 philnik777 requested a review from a team as a code owner April 25, 2025 08:38
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Apr 25, 2025
@philnik777 philnik777 merged commit c4d44ec into llvm:main Apr 25, 2025
5 of 8 checks passed
@philnik777 philnik777 deleted the string_copy_ctor_early_return branch April 25, 2025 08:39
@llvmbot
Copy link
Member

llvmbot commented Apr 25, 2025

@llvm/pr-subscribers-libcxx

Author: Nikolas Klauser (philnik777)

Changes

This makes the code a lot easier to read.


Full diff: https://github.com/llvm/llvm-project/pull/137145.diff

1 Files Affected:

  • (modified) libcxx/include/string (+15-17)
diff --git a/libcxx/include/string b/libcxx/include/string
index e7e541e31432d..854f143da9e0f 100644
--- a/libcxx/include/string
+++ b/libcxx/include/string
@@ -2867,23 +2867,21 @@ basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c) {
 template <class _CharT, class _Traits, class _Allocator>
 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS basic_string<_CharT, _Traits, _Allocator>&
 basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str) {
-  if (this != std::addressof(__str)) {
-    __copy_assign_alloc(__str);
-    if (!__is_long()) {
-      if (!__str.__is_long()) {
-        size_type __old_size = __get_short_size();
-        if (__old_size < __str.__get_short_size())
-          __annotate_increase(__str.__get_short_size() - __old_size);
-        __rep_ = __str.__rep_;
-        if (__old_size > __get_short_size())
-          __annotate_shrink(__old_size);
-      } else {
-        return __assign_no_alias<true>(__str.data(), __str.size());
-      }
-    } else {
-      return __assign_no_alias<false>(__str.data(), __str.size());
-    }
-  }
+  if (this == std::addressof(__str))
+    return *this;
+
+  __copy_assign_alloc(__str);
+
+  if (__is_long())
+    return __assign_no_alias<false>(__str.data(), __str.size());
+
+  if (__str.__is_long())
+    return __assign_no_alias<true>(__str.data(), __str.size());
+
+  __annotate_delete();
+  auto __guard = std::__make_scope_guard(__annotate_new_size(*this));
+  __rep_       = __str.__rep_;
+
   return *this;
 }
 

IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
Ankur-0429 pushed a commit to Ankur-0429/llvm-project that referenced this pull request May 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants