Skip to content

Commit 1a640a0

Browse files
committed
Address philnik777's suggestions
1 parent 43a2800 commit 1a640a0

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
@@ -2364,8 +2364,14 @@ private:
23642364
size_type __old_size = size();
23652365
if (__n > __old_size)
23662366
__annotate_increase(__n - __old_size);
2367-
pointer __p =
2368-
__is_long() ? (__set_long_size(__n), __get_long_pointer()) : (__set_short_size(__n), __get_short_pointer());
2367+
pointer __p;
2368+
if (__is_long()) {
2369+
__set_long_size(__n);
2370+
__p = __get_long_pointer();
2371+
} else {
2372+
__set_short_size(__n);
2373+
__p = __get_short_pointer();
2374+
}
23692375
traits_type::move(std::__to_address(__p), __s, __n);
23702376
traits_type::assign(__p[__n], value_type());
23712377
if (__old_size > __n)
@@ -2705,36 +2711,42 @@ template <class _CharT, class _Traits, class _Allocator>
27052711
template <bool __is_short>
27062712
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE basic_string<_CharT, _Traits, _Allocator>&
27072713
basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias(const value_type* __s, size_type __n) {
2708-
size_type __cap = capacity();
2709-
size_type __old_size = size();
2710-
if (__n <= __cap) {
2711-
if (__n > __old_size)
2712-
__annotate_increase(__n - __old_size);
2713-
pointer __p =
2714-
__is_short ? (__set_short_size(__n), __get_short_pointer()) : (__set_long_size(__n), __get_long_pointer());
2714+
const auto __cap = __is_short ? static_cast<size_type>(__min_cap) : __get_long_cap();
2715+
const auto __sz = __is_short ? __get_short_size() : __get_long_size();
2716+
if (__n < __cap) {
2717+
if (__n > __sz)
2718+
__annotate_increase(__n - __sz);
2719+
pointer __p;
2720+
if (__is_short) {
2721+
__p = __get_short_pointer();
2722+
__set_short_size(__n);
2723+
} else {
2724+
__p = __get_long_pointer();
2725+
__set_long_size(__n);
2726+
}
27152727
traits_type::copy(std::__to_address(__p), __s, __n);
27162728
traits_type::assign(__p[__n], value_type());
2717-
if (__old_size > __n)
2718-
__annotate_shrink(__old_size);
2729+
if (__sz > __n)
2730+
__annotate_shrink(__sz);
27192731
} else {
2720-
__grow_by_and_replace(__cap, __n - __cap, __old_size, 0, __old_size, __n, __s);
2732+
__grow_by_and_replace(__cap - 1, __n - __cap + 1, __sz, 0, __sz, __n, __s);
27212733
}
27222734
return *this;
27232735
}
27242736

27252737
template <class _CharT, class _Traits, class _Allocator>
27262738
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE basic_string<_CharT, _Traits, _Allocator>&
27272739
basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s, size_type __n) {
2728-
size_type __cap = capacity();
2729-
size_type __old_size = size();
2740+
const auto __cap = capacity();
2741+
const auto __sz = size();
27302742
if (__cap >= __n) {
2731-
if (__n > __old_size)
2732-
__annotate_increase(__n - __old_size);
2743+
if (__n > __sz)
2744+
__annotate_increase(__n - __sz);
27332745
value_type* __p = std::__to_address(__get_pointer());
27342746
traits_type::move(__p, __s, __n);
27352747
return __null_terminate_at(__p, __n);
27362748
} else {
2737-
__grow_by_and_replace(__cap, __n - __cap, __old_size, 0, __old_size, __n, __s);
2749+
__grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
27382750
return *this;
27392751
}
27402752
}
@@ -2767,7 +2779,14 @@ basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c) {
27672779
size_type __old_size = size();
27682780
if (__old_size == 0)
27692781
__annotate_increase(1);
2770-
pointer __p = __is_long() ? (__set_long_size(1), __get_long_pointer()) : (__set_short_size(1), __get_short_pointer());
2782+
pointer __p;
2783+
if (__is_long()) {
2784+
__set_long_size(1);
2785+
__p = __get_long_pointer();
2786+
} else {
2787+
__set_short_size(1);
2788+
__p = __get_short_pointer();
2789+
}
27712790
traits_type::assign(*__p, __c);
27722791
traits_type::assign(*++__p, value_type());
27732792
if (__old_size > 1)
@@ -2951,10 +2970,8 @@ template <class _CharT, class _Traits, class _Allocator>
29512970
_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
29522971
basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s) {
29532972
_LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::assign received nullptr");
2954-
if (__builtin_constant_p(*__s)) {
2955-
const size_type __n = traits_type::length(__s);
2956-
return __fits_in_sso(__n) ? __assign_short(__s, __n) : __assign_external(__s, __n);
2957-
}
2973+
if (auto __len = traits_type::length(__s); __builtin_constant_p(__len) && __fits_in_sso(__len))
2974+
return __assign_short(__s, __len);
29582975
return __assign_external(__s);
29592976
}
29602977
// append
@@ -3030,8 +3047,14 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::pu
30303047
__is_short = false; // the string is always long after __grow_by
30313048
}
30323049
__annotate_increase(1);
3033-
pointer __p = __is_short ? (__set_short_size(__sz + 1), __get_short_pointer() + __sz)
3034-
: (__set_long_size(__sz + 1), __get_long_pointer() + __sz);
3050+
pointer __p;
3051+
if (__is_short) {
3052+
__p = __get_short_pointer() + __sz;
3053+
__set_short_size(__sz + 1);
3054+
} else {
3055+
__p = __get_long_pointer() + __sz;
3056+
__set_long_size(__sz + 1);
3057+
}
30353058
traits_type::assign(*__p, __c);
30363059
traits_type::assign(*++__p, value_type());
30373060
}
@@ -3472,9 +3495,9 @@ inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocat
34723495
_LIBCPP_ASSERT_INTERNAL(__is_long(), "Trying to shrink small string");
34733496

34743497
// We're a long string and we're shrinking into the small buffer.
3475-
auto __ptr = __get_long_pointer();
3476-
auto __size = __get_long_size();
3477-
auto __cap = __get_long_cap();
3498+
const auto __ptr = __get_long_pointer();
3499+
const auto __size = __get_long_size();
3500+
const auto __cap = __get_long_cap();
34783501

34793502
if (__fits_in_sso(__target_capacity)) {
34803503
__annotation_guard __g(*this);

0 commit comments

Comments
 (0)