Skip to content

Commit 80f487c

Browse files
committed
Address philnik777's suggestions
1 parent afe7f5c commit 80f487c

File tree

1 file changed

+50
-27
lines changed

1 file changed

+50
-27
lines changed

libcxx/include/string

Lines changed: 50 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2352,8 +2352,14 @@ private:
23522352
size_type __old_size = size();
23532353
if (__n > __old_size)
23542354
__annotate_increase(__n - __old_size);
2355-
pointer __p =
2356-
__is_long() ? (__set_long_size(__n), __get_long_pointer()) : (__set_short_size(__n), __get_short_pointer());
2355+
pointer __p;
2356+
if (__is_long()) {
2357+
__set_long_size(__n);
2358+
__p = __get_long_pointer();
2359+
} else {
2360+
__set_short_size(__n);
2361+
__p = __get_short_pointer();
2362+
}
23572363
traits_type::move(std::__to_address(__p), __s, __n);
23582364
traits_type::assign(__p[__n], value_type());
23592365
if (__old_size > __n)
@@ -2693,36 +2699,42 @@ template <class _CharT, class _Traits, class _Allocator>
26932699
template <bool __is_short>
26942700
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE basic_string<_CharT, _Traits, _Allocator>&
26952701
basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias(const value_type* __s, size_type __n) {
2696-
size_type __cap = capacity();
2697-
size_type __old_size = size();
2698-
if (__n <= __cap) {
2699-
if (__n > __old_size)
2700-
__annotate_increase(__n - __old_size);
2701-
pointer __p =
2702-
__is_short ? (__set_short_size(__n), __get_short_pointer()) : (__set_long_size(__n), __get_long_pointer());
2702+
const auto __cap = __is_short ? static_cast<size_type>(__min_cap) : __get_long_cap();
2703+
const auto __sz = __is_short ? __get_short_size() : __get_long_size();
2704+
if (__n < __cap) {
2705+
if (__n > __sz)
2706+
__annotate_increase(__n - __sz);
2707+
pointer __p;
2708+
if (__is_short) {
2709+
__p = __get_short_pointer();
2710+
__set_short_size(__n);
2711+
} else {
2712+
__p = __get_long_pointer();
2713+
__set_long_size(__n);
2714+
}
27032715
traits_type::copy(std::__to_address(__p), __s, __n);
27042716
traits_type::assign(__p[__n], value_type());
2705-
if (__old_size > __n)
2706-
__annotate_shrink(__old_size);
2717+
if (__sz > __n)
2718+
__annotate_shrink(__sz);
27072719
} else {
2708-
__grow_by_and_replace(__cap, __n - __cap, __old_size, 0, __old_size, __n, __s);
2720+
__grow_by_and_replace(__cap - 1, __n - __cap + 1, __sz, 0, __sz, __n, __s);
27092721
}
27102722
return *this;
27112723
}
27122724

27132725
template <class _CharT, class _Traits, class _Allocator>
27142726
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE basic_string<_CharT, _Traits, _Allocator>&
27152727
basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s, size_type __n) {
2716-
size_type __cap = capacity();
2717-
size_type __old_size = size();
2728+
const auto __cap = capacity();
2729+
const auto __sz = size();
27182730
if (__cap >= __n) {
2719-
if (__n > __old_size)
2720-
__annotate_increase(__n - __old_size);
2731+
if (__n > __sz)
2732+
__annotate_increase(__n - __sz);
27212733
value_type* __p = std::__to_address(__get_pointer());
27222734
traits_type::move(__p, __s, __n);
27232735
return __null_terminate_at(__p, __n);
27242736
} else {
2725-
__grow_by_and_replace(__cap, __n - __cap, __old_size, 0, __old_size, __n, __s);
2737+
__grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
27262738
return *this;
27272739
}
27282740
}
@@ -2755,7 +2767,14 @@ basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c) {
27552767
size_type __old_size = size();
27562768
if (__old_size == 0)
27572769
__annotate_increase(1);
2758-
pointer __p = __is_long() ? (__set_long_size(1), __get_long_pointer()) : (__set_short_size(1), __get_short_pointer());
2770+
pointer __p;
2771+
if (__is_long()) {
2772+
__set_long_size(1);
2773+
__p = __get_long_pointer();
2774+
} else {
2775+
__set_short_size(1);
2776+
__p = __get_short_pointer();
2777+
}
27592778
traits_type::assign(*__p, __c);
27602779
traits_type::assign(*++__p, value_type());
27612780
if (__old_size > 1)
@@ -2939,10 +2958,8 @@ template <class _CharT, class _Traits, class _Allocator>
29392958
_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
29402959
basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s) {
29412960
_LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::assign received nullptr");
2942-
if (__builtin_constant_p(*__s)) {
2943-
const size_type __n = traits_type::length(__s);
2944-
return __fits_in_sso(__n) ? __assign_short(__s, __n) : __assign_external(__s, __n);
2945-
}
2961+
if (auto __len = traits_type::length(__s); __builtin_constant_p(__len) && __fits_in_sso(__len))
2962+
return __assign_short(__s, __len);
29462963
return __assign_external(__s);
29472964
}
29482965
// append
@@ -3018,8 +3035,14 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::pu
30183035
__is_short = false; // the string is always long after __grow_by
30193036
}
30203037
__annotate_increase(1);
3021-
pointer __p = __is_short ? (__set_short_size(__sz + 1), __get_short_pointer() + __sz)
3022-
: (__set_long_size(__sz + 1), __get_long_pointer() + __sz);
3038+
pointer __p;
3039+
if (__is_short) {
3040+
__p = __get_short_pointer() + __sz;
3041+
__set_short_size(__sz + 1);
3042+
} else {
3043+
__p = __get_long_pointer() + __sz;
3044+
__set_long_size(__sz + 1);
3045+
}
30233046
traits_type::assign(*__p, __c);
30243047
traits_type::assign(*++__p, value_type());
30253048
}
@@ -3460,9 +3483,9 @@ inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocat
34603483
_LIBCPP_ASSERT_INTERNAL(__is_long(), "Trying to shrink small string");
34613484

34623485
// We're a long string and we're shrinking into the small buffer.
3463-
auto __ptr = __get_long_pointer();
3464-
auto __size = __get_long_size();
3465-
auto __cap = __get_long_cap();
3486+
const auto __ptr = __get_long_pointer();
3487+
const auto __size = __get_long_size();
3488+
const auto __cap = __get_long_cap();
34663489

34673490
if (__fits_in_sso(__target_capacity)) {
34683491
__annotation_guard __g(*this);

0 commit comments

Comments
 (0)