Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions libcxx/include/__cxx03/string
Original file line number Diff line number Diff line change
Expand Up @@ -1101,12 +1101,20 @@ public:
_LIBCPP_HIDE_FROM_ABI size_type length() const _NOEXCEPT { return size(); }

_LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT {
size_type __m = __alloc_traits::max_size(__alloc());
if (__m <= std::numeric_limits<size_type>::max() / 2) {
return __m - __alignment;
if (size_type __m = __alloc_traits::max_size(__alloc()); __m <= std::numeric_limits<size_type>::max() / 2) {
size_type __res = __m - __alignment;

// When the __endian_factor == 2, our string representation assumes that the capacity
// (including the null terminator) is always even, so we have to make sure the lowest bit isn't set when the
// string grows to max_size()
if (__endian_factor == 2)
__res &= ~size_type(1);

// We have to allocate space for the null terminator, but max_size() doesn't include it.
return __res - 1;
} else {
bool __uses_lsb = __endian_factor == 2;
return __uses_lsb ? __m - __alignment : (__m / 2) - __alignment;
return __uses_lsb ? __m - __alignment - 1 : (__m / 2) - __alignment - 1;
}
}

Expand Down Expand Up @@ -1970,11 +1978,11 @@ void basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace(
size_type __n_add,
const value_type* __p_new_stuff) {
size_type __ms = max_size();
if (__delta_cap > __ms - __old_cap - 1)
if (__delta_cap > __ms - __old_cap)
__throw_length_error();
pointer __old_p = __get_pointer();
size_type __cap =
__old_cap < __ms / 2 - __alignment ? __recommend(std::max(__old_cap + __delta_cap, 2 * __old_cap)) : __ms - 1;
__old_cap < __ms / 2 - __alignment ? __recommend(std::max(__old_cap + __delta_cap, 2 * __old_cap)) : __ms;
__annotate_delete();
auto __allocation = std::__allocate_at_least(__alloc(), __cap + 1);
pointer __p = __allocation.ptr;
Expand Down Expand Up @@ -2017,7 +2025,7 @@ void
__throw_length_error();
pointer __old_p = __get_pointer();
size_type __cap =
__old_cap < __ms / 2 - __alignment ? __recommend(std::max(__old_cap + __delta_cap, 2 * __old_cap)) : __ms - 1;
__old_cap < __ms / 2 - __alignment ? __recommend(std::max(__old_cap + __delta_cap, 2 * __old_cap)) : __ms;
__annotate_delete();
auto __allocation = std::__allocate_at_least(__alloc(), __cap + 1);
pointer __p = __allocation.ptr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
//
//===----------------------------------------------------------------------===//

// XFAIL: FROZEN-CXX03-HEADERS-FIXME

// <string>

// This test ensures that the correct max_size() is returned depending on the platform.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

// UNSUPPORTED: no-exceptions

// XFAIL: FROZEN-CXX03-HEADERS-FIXME

// <string>

// size_type max_size() const; // constexpr since C++20
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
//
//===----------------------------------------------------------------------===//

// XFAIL: FROZEN-CXX03-HEADERS-FIXME

// <string>

// basic_string<charT,traits,Allocator>&
Expand Down
Loading