Skip to content

Commit c4d44ec

Browse files
authored
[libc++][NFC] Use early returns in basic_string::operator= (#137145)
This makes the code a lot easier to read.
1 parent 911cb60 commit c4d44ec

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

libcxx/include/string

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2867,23 +2867,21 @@ basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c) {
28672867
template <class _CharT, class _Traits, class _Allocator>
28682868
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS basic_string<_CharT, _Traits, _Allocator>&
28692869
basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str) {
2870-
if (this != std::addressof(__str)) {
2871-
__copy_assign_alloc(__str);
2872-
if (!__is_long()) {
2873-
if (!__str.__is_long()) {
2874-
size_type __old_size = __get_short_size();
2875-
if (__old_size < __str.__get_short_size())
2876-
__annotate_increase(__str.__get_short_size() - __old_size);
2877-
__rep_ = __str.__rep_;
2878-
if (__old_size > __get_short_size())
2879-
__annotate_shrink(__old_size);
2880-
} else {
2881-
return __assign_no_alias<true>(__str.data(), __str.size());
2882-
}
2883-
} else {
2884-
return __assign_no_alias<false>(__str.data(), __str.size());
2885-
}
2886-
}
2870+
if (this == std::addressof(__str))
2871+
return *this;
2872+
2873+
__copy_assign_alloc(__str);
2874+
2875+
if (__is_long())
2876+
return __assign_no_alias<false>(__str.data(), __str.size());
2877+
2878+
if (__str.__is_long())
2879+
return __assign_no_alias<true>(__str.data(), __str.size());
2880+
2881+
__annotate_delete();
2882+
auto __guard = std::__make_scope_guard(__annotate_new_size(*this));
2883+
__rep_ = __str.__rep_;
2884+
28872885
return *this;
28882886
}
28892887

0 commit comments

Comments
 (0)