Skip to content

Commit afe7f5c

Browse files
committed
Simplify string a bit
1 parent a72616d commit afe7f5c

File tree

1 file changed

+37
-52
lines changed

1 file changed

+37
-52
lines changed

libcxx/include/string

Lines changed: 37 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,9 @@ public:
235235
template <class T>
236236
basic_string& insert(size_type pos1, const T& t); // constexpr since C++20
237237
basic_string& insert(size_type pos1, const basic_string& str,
238-
size_type pos2, size_type n); // constexpr since C++20
238+
size_type pos2, size_type n2=npos); // constexpr since C++20
239239
template <class T>
240-
basic_string& insert(size_type pos1, const T& t, size_type pos2, size_type n); // C++17, constexpr since C++20
240+
basic_string& insert(size_type pos1, const T& t, size_type pos2, size_type n=npos); // C++17, constexpr since C++20
241241
basic_string& insert(size_type pos, const value_type* s, size_type n=npos); // C++14, constexpr since C++20
242242
basic_string& insert(size_type pos, const value_type* s); // constexpr since C++20
243243
basic_string& insert(size_type pos, size_type n, value_type c); // constexpr since C++20
@@ -260,7 +260,7 @@ public:
260260
size_type pos2, size_type n2=npos); // C++14, constexpr since C++20
261261
template <class T>
262262
basic_string& replace(size_type pos1, size_type n1, const T& t,
263-
size_type pos2, size_type n); // C++17, constexpr since C++20
263+
size_type pos2, size_type n2=npos); // C++17, constexpr since C++20
264264
basic_string& replace(size_type pos, size_type n1, const value_type* s, size_type n2); // constexpr since C++20
265265
basic_string& replace(size_type pos, size_type n1, const value_type* s); // constexpr since C++20
266266
basic_string& replace(size_type pos, size_type n1, size_type n2, value_type c); // constexpr since C++20
@@ -2295,7 +2295,7 @@ private:
22952295
if (!__str.__is_long()) {
22962296
if (__is_long()) {
22972297
__annotate_delete();
2298-
__alloc_traits::deallocate(__alloc_, __get_long_pointer(), capacity() + 1);
2298+
__alloc_traits::deallocate(__alloc_, __get_long_pointer(), __get_long_cap());
22992299
__rep_ = __rep();
23002300
}
23012301
__alloc_ = __str.__alloc_;
@@ -2310,7 +2310,7 @@ private:
23102310
__alloc_ = std::move(__a);
23112311
__set_long_pointer(__allocation.ptr);
23122312
__set_long_cap(__allocation.count);
2313-
__set_long_size(__str.size());
2313+
__set_long_size(__str.__get_long_size());
23142314
}
23152315
}
23162316
}
@@ -2429,8 +2429,8 @@ template <class _CharT,
24292429
class _Traits,
24302430
class _Allocator = allocator<_CharT>,
24312431
class = enable_if_t<__is_allocator<_Allocator>::value> >
2432-
explicit basic_string(basic_string_view<_CharT, _Traits>,
2433-
const _Allocator& = _Allocator()) -> basic_string<_CharT, _Traits, _Allocator>;
2432+
explicit basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
2433+
-> basic_string<_CharT, _Traits, _Allocator>;
24342434

24352435
template <class _CharT,
24362436
class _Traits,
@@ -2693,38 +2693,36 @@ template <class _CharT, class _Traits, class _Allocator>
26932693
template <bool __is_short>
26942694
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE basic_string<_CharT, _Traits, _Allocator>&
26952695
basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias(const value_type* __s, size_type __n) {
2696-
size_type __cap = __is_short ? static_cast<size_type>(__min_cap) : __get_long_cap();
2697-
if (__n < __cap) {
2698-
size_type __old_size = __is_short ? __get_short_size() : __get_long_size();
2696+
size_type __cap = capacity();
2697+
size_type __old_size = size();
2698+
if (__n <= __cap) {
26992699
if (__n > __old_size)
27002700
__annotate_increase(__n - __old_size);
2701-
pointer __p = __is_short ? __get_short_pointer() : __get_long_pointer();
2702-
__is_short ? __set_short_size(__n) : __set_long_size(__n);
2701+
pointer __p =
2702+
__is_short ? (__set_short_size(__n), __get_short_pointer()) : (__set_long_size(__n), __get_long_pointer());
27032703
traits_type::copy(std::__to_address(__p), __s, __n);
27042704
traits_type::assign(__p[__n], value_type());
27052705
if (__old_size > __n)
27062706
__annotate_shrink(__old_size);
27072707
} else {
2708-
size_type __sz = __is_short ? __get_short_size() : __get_long_size();
2709-
__grow_by_and_replace(__cap - 1, __n - __cap + 1, __sz, 0, __sz, __n, __s);
2708+
__grow_by_and_replace(__cap, __n - __cap, __old_size, 0, __old_size, __n, __s);
27102709
}
27112710
return *this;
27122711
}
27132712

27142713
template <class _CharT, class _Traits, class _Allocator>
27152714
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE basic_string<_CharT, _Traits, _Allocator>&
27162715
basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s, size_type __n) {
2717-
size_type __cap = capacity();
2716+
size_type __cap = capacity();
2717+
size_type __old_size = size();
27182718
if (__cap >= __n) {
2719-
size_type __old_size = size();
27202719
if (__n > __old_size)
27212720
__annotate_increase(__n - __old_size);
27222721
value_type* __p = std::__to_address(__get_pointer());
27232722
traits_type::move(__p, __s, __n);
27242723
return __null_terminate_at(__p, __n);
27252724
} else {
2726-
size_type __sz = size();
2727-
__grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
2725+
__grow_by_and_replace(__cap, __n - __cap, __old_size, 0, __old_size, __n, __s);
27282726
return *this;
27292727
}
27302728
}
@@ -2742,8 +2740,7 @@ basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
27422740
size_type __cap = capacity();
27432741
size_type __old_size = size();
27442742
if (__cap < __n) {
2745-
size_type __sz = size();
2746-
__grow_by_without_replace(__cap, __n - __cap, __sz, 0, __sz);
2743+
__grow_by_without_replace(__cap, __n - __cap, __old_size, 0, __old_size);
27472744
__annotate_increase(__n);
27482745
} else if (__n > __old_size)
27492746
__annotate_increase(__n - __old_size);
@@ -2755,17 +2752,10 @@ basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
27552752
template <class _CharT, class _Traits, class _Allocator>
27562753
_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
27572754
basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c) {
2758-
pointer __p;
27592755
size_type __old_size = size();
27602756
if (__old_size == 0)
27612757
__annotate_increase(1);
2762-
if (__is_long()) {
2763-
__p = __get_long_pointer();
2764-
__set_long_size(1);
2765-
} else {
2766-
__p = __get_short_pointer();
2767-
__set_short_size(1);
2768-
}
2758+
pointer __p = __is_long() ? (__set_long_size(1), __get_long_pointer()) : (__set_short_size(1), __get_short_pointer());
27692759
traits_type::assign(*__p, __c);
27702760
traits_type::assign(*++__p, value_type());
27712761
if (__old_size > 1)
@@ -2781,8 +2771,8 @@ basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
27812771
if (!__is_long()) {
27822772
if (!__str.__is_long()) {
27832773
size_type __old_size = __get_short_size();
2784-
if (__get_short_size() < __str.__get_short_size())
2785-
__annotate_increase(__str.__get_short_size() - __get_short_size());
2774+
if (__old_size < __str.__get_short_size())
2775+
__annotate_increase(__str.__get_short_size() - __old_size);
27862776
__rep_ = __str.__rep_;
27872777
if (__old_size > __get_short_size())
27882778
__annotate_shrink(__old_size);
@@ -2949,10 +2939,11 @@ template <class _CharT, class _Traits, class _Allocator>
29492939
_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
29502940
basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s) {
29512941
_LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::assign received nullptr");
2952-
return __builtin_constant_p(*__s)
2953-
? (__fits_in_sso(traits_type::length(__s)) ? __assign_short(__s, traits_type::length(__s))
2954-
: __assign_external(__s, traits_type::length(__s)))
2955-
: __assign_external(__s);
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+
}
2946+
return __assign_external(__s);
29562947
}
29572948
// append
29582949

@@ -3024,18 +3015,11 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::pu
30243015
}
30253016
if (__sz == __cap) {
30263017
__grow_by_without_replace(__cap, 1, __sz, __sz, 0);
3027-
__annotate_increase(1);
30283018
__is_short = false; // the string is always long after __grow_by
3029-
} else
3030-
__annotate_increase(1);
3031-
pointer __p = __get_pointer();
3032-
if (__is_short) {
3033-
__p = __get_short_pointer() + __sz;
3034-
__set_short_size(__sz + 1);
3035-
} else {
3036-
__p = __get_long_pointer() + __sz;
3037-
__set_long_size(__sz + 1);
30383019
}
3020+
__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);
30393023
traits_type::assign(*__p, __c);
30403024
traits_type::assign(*++__p, value_type());
30413025
}
@@ -3412,11 +3396,13 @@ inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocat
34123396

34133397
template <class _CharT, class _Traits, class _Allocator>
34143398
inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT {
3415-
size_type __old_size = size();
3399+
size_type __old_size;
34163400
if (__is_long()) {
3401+
__old_size = __get_long_size();
34173402
traits_type::assign(*__get_long_pointer(), value_type());
34183403
__set_long_size(0);
34193404
} else {
3405+
__old_size = __get_short_size();
34203406
traits_type::assign(*__get_short_pointer(), value_type());
34213407
__set_short_size(0);
34223408
}
@@ -3474,11 +3460,12 @@ inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocat
34743460
_LIBCPP_ASSERT_INTERNAL(__is_long(), "Trying to shrink small string");
34753461

34763462
// 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();
3466+
34773467
if (__fits_in_sso(__target_capacity)) {
34783468
__annotation_guard __g(*this);
3479-
auto __ptr = __get_long_pointer();
3480-
auto __size = __get_long_size();
3481-
auto __cap = __get_long_cap();
34823469
traits_type::copy(std::__to_address(__get_short_pointer()), std::__to_address(__ptr), __size + 1);
34833470
__set_short_size(__size);
34843471
__alloc_traits::deallocate(__alloc_, __ptr, __cap);
@@ -3489,21 +3476,19 @@ inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocat
34893476
try {
34903477
# endif // _LIBCPP_HAS_EXCEPTIONS
34913478
__annotation_guard __g(*this);
3492-
auto __size = size();
34933479
auto __allocation = std::__allocate_at_least(__alloc_, __target_capacity + 1);
34943480

34953481
// The Standard mandates shrink_to_fit() does not increase the capacity.
34963482
// With equal capacity keep the existing buffer. This avoids extra work
34973483
// due to swapping the elements.
3498-
if (__allocation.count - 1 > capacity()) {
3484+
if (__allocation.count > __cap) {
34993485
__alloc_traits::deallocate(__alloc_, __allocation.ptr, __allocation.count);
35003486
return;
35013487
}
35023488

35033489
__begin_lifetime(__allocation.ptr, __allocation.count);
3504-
auto __ptr = __get_long_pointer();
35053490
traits_type::copy(std::__to_address(__allocation.ptr), std::__to_address(__ptr), __size + 1);
3506-
__alloc_traits::deallocate(__alloc_, __ptr, __get_long_cap());
3491+
__alloc_traits::deallocate(__alloc_, __ptr, __cap);
35073492
__set_long_cap(__allocation.count);
35083493
__set_long_pointer(__allocation.ptr);
35093494
# if _LIBCPP_HAS_EXCEPTIONS

0 commit comments

Comments
 (0)