Skip to content

Commit d897bac

Browse files
committed
Address philnik777's suggestions
1 parent 596189e commit d897bac

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
@@ -2360,8 +2360,14 @@ private:
23602360
size_type __old_size = size();
23612361
if (__n > __old_size)
23622362
__annotate_increase(__n - __old_size);
2363-
pointer __p =
2364-
__is_long() ? (__set_long_size(__n), __get_long_pointer()) : (__set_short_size(__n), __get_short_pointer());
2363+
pointer __p;
2364+
if (__is_long()) {
2365+
__set_long_size(__n);
2366+
__p = __get_long_pointer();
2367+
} else {
2368+
__set_short_size(__n);
2369+
__p = __get_short_pointer();
2370+
}
23652371
traits_type::move(std::__to_address(__p), __s, __n);
23662372
traits_type::assign(__p[__n], value_type());
23672373
if (__old_size > __n)
@@ -2701,36 +2707,42 @@ template <class _CharT, class _Traits, class _Allocator>
27012707
template <bool __is_short>
27022708
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE basic_string<_CharT, _Traits, _Allocator>&
27032709
basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias(const value_type* __s, size_type __n) {
2704-
size_type __cap = capacity();
2705-
size_type __old_size = size();
2706-
if (__n <= __cap) {
2707-
if (__n > __old_size)
2708-
__annotate_increase(__n - __old_size);
2709-
pointer __p =
2710-
__is_short ? (__set_short_size(__n), __get_short_pointer()) : (__set_long_size(__n), __get_long_pointer());
2710+
const auto __cap = __is_short ? static_cast<size_type>(__min_cap) : __get_long_cap();
2711+
const auto __sz = __is_short ? __get_short_size() : __get_long_size();
2712+
if (__n < __cap) {
2713+
if (__n > __sz)
2714+
__annotate_increase(__n - __sz);
2715+
pointer __p;
2716+
if (__is_short) {
2717+
__p = __get_short_pointer();
2718+
__set_short_size(__n);
2719+
} else {
2720+
__p = __get_long_pointer();
2721+
__set_long_size(__n);
2722+
}
27112723
traits_type::copy(std::__to_address(__p), __s, __n);
27122724
traits_type::assign(__p[__n], value_type());
2713-
if (__old_size > __n)
2714-
__annotate_shrink(__old_size);
2725+
if (__sz > __n)
2726+
__annotate_shrink(__sz);
27152727
} else {
2716-
__grow_by_and_replace(__cap, __n - __cap, __old_size, 0, __old_size, __n, __s);
2728+
__grow_by_and_replace(__cap - 1, __n - __cap + 1, __sz, 0, __sz, __n, __s);
27172729
}
27182730
return *this;
27192731
}
27202732

27212733
template <class _CharT, class _Traits, class _Allocator>
27222734
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE basic_string<_CharT, _Traits, _Allocator>&
27232735
basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s, size_type __n) {
2724-
size_type __cap = capacity();
2725-
size_type __old_size = size();
2736+
const auto __cap = capacity();
2737+
const auto __sz = size();
27262738
if (__cap >= __n) {
2727-
if (__n > __old_size)
2728-
__annotate_increase(__n - __old_size);
2739+
if (__n > __sz)
2740+
__annotate_increase(__n - __sz);
27292741
value_type* __p = std::__to_address(__get_pointer());
27302742
traits_type::move(__p, __s, __n);
27312743
return __null_terminate_at(__p, __n);
27322744
} else {
2733-
__grow_by_and_replace(__cap, __n - __cap, __old_size, 0, __old_size, __n, __s);
2745+
__grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
27342746
return *this;
27352747
}
27362748
}
@@ -2763,7 +2775,14 @@ basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c) {
27632775
size_type __old_size = size();
27642776
if (__old_size == 0)
27652777
__annotate_increase(1);
2766-
pointer __p = __is_long() ? (__set_long_size(1), __get_long_pointer()) : (__set_short_size(1), __get_short_pointer());
2778+
pointer __p;
2779+
if (__is_long()) {
2780+
__set_long_size(1);
2781+
__p = __get_long_pointer();
2782+
} else {
2783+
__set_short_size(1);
2784+
__p = __get_short_pointer();
2785+
}
27672786
traits_type::assign(*__p, __c);
27682787
traits_type::assign(*++__p, value_type());
27692788
if (__old_size > 1)
@@ -2947,10 +2966,8 @@ template <class _CharT, class _Traits, class _Allocator>
29472966
_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
29482967
basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s) {
29492968
_LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::assign received nullptr");
2950-
if (__builtin_constant_p(*__s)) {
2951-
const size_type __n = traits_type::length(__s);
2952-
return __fits_in_sso(__n) ? __assign_short(__s, __n) : __assign_external(__s, __n);
2953-
}
2969+
if (auto __len = traits_type::length(__s); __builtin_constant_p(__len) && __fits_in_sso(__len))
2970+
return __assign_short(__s, __len);
29542971
return __assign_external(__s);
29552972
}
29562973
// append
@@ -3026,8 +3043,14 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::pu
30263043
__is_short = false; // the string is always long after __grow_by
30273044
}
30283045
__annotate_increase(1);
3029-
pointer __p = __is_short ? (__set_short_size(__sz + 1), __get_short_pointer() + __sz)
3030-
: (__set_long_size(__sz + 1), __get_long_pointer() + __sz);
3046+
pointer __p;
3047+
if (__is_short) {
3048+
__p = __get_short_pointer() + __sz;
3049+
__set_short_size(__sz + 1);
3050+
} else {
3051+
__p = __get_long_pointer() + __sz;
3052+
__set_long_size(__sz + 1);
3053+
}
30313054
traits_type::assign(*__p, __c);
30323055
traits_type::assign(*++__p, value_type());
30333056
}
@@ -3468,9 +3491,9 @@ inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocat
34683491
_LIBCPP_ASSERT_INTERNAL(__is_long(), "Trying to shrink small string");
34693492

34703493
// We're a long string and we're shrinking into the small buffer.
3471-
auto __ptr = __get_long_pointer();
3472-
auto __size = __get_long_size();
3473-
auto __cap = __get_long_cap();
3494+
const auto __ptr = __get_long_pointer();
3495+
const auto __size = __get_long_size();
3496+
const auto __cap = __get_long_cap();
34743497

34753498
if (__fits_in_sso(__target_capacity)) {
34763499
__annotation_guard __g(*this);

0 commit comments

Comments
 (0)