diff --git a/libcxx/include/__string b/libcxx/include/__string --- a/libcxx/include/__string +++ b/libcxx/include/__string @@ -170,7 +170,7 @@ _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>::size_type basic_string<_CharType>::rfind(value_type, size_type) const) \ _Func(_LIBCPP_FUNC_VIS const basic_string<_CharType>::size_type basic_string<_CharType>::npos) \ _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::assign(size_type, value_type)) \ - _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::erase(size_type, size_type)) \ + _Func(_LIBCPP_FUNC_VIS void basic_string<_CharType>::__erase_external_with_move(size_type, size_type)) \ _Func(_LIBCPP_FUNC_VIS basic_string<_CharType>& basic_string<_CharType>::append(basic_string const&, size_type, size_type)) \ _Func(_LIBCPP_FUNC_VIS int basic_string<_CharType>::compare(value_type const*) const) \ _Func(_LIBCPP_FUNC_VIS int basic_string<_CharType>::compare(size_type, size_type, value_type const*) const) \ diff --git a/libcxx/include/string b/libcxx/include/string --- a/libcxx/include/string +++ b/libcxx/include/string @@ -1574,6 +1574,10 @@ _LIBCPP_INLINE_VISIBILITY void __erase_to_end(size_type __pos); + // __erase_external_with_move is invoked for erase() invocations where + // `n ~= npos`, likely requiring memory moves on the string data. + void __erase_external_with_move(size_type __pos, size_type __n); + _LIBCPP_INLINE_VISIBILITY void __copy_assign_alloc(const basic_string& __str) {__copy_assign_alloc(__str, integral_constant -basic_string<_CharT, _Traits, _Allocator>& -basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos, size_type __n) +void +basic_string<_CharT, _Traits, _Allocator>::__erase_external_with_move( + size_type __pos, size_type __n) { - size_type __sz = size(); - if (__pos > __sz) - this->__throw_out_of_range(); if (__n) { + size_type __sz = size(); value_type* __p = _VSTD::__to_address(__get_pointer()); __n = _VSTD::min(__n, __sz - __pos); size_type __n_move = __sz - __pos - __n; @@ -3016,7 +3021,19 @@ __invalidate_iterators_past(__sz); traits_type::assign(__p[__sz], value_type()); } - return *this; +} + +template +basic_string<_CharT, _Traits, _Allocator>& +basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos, + size_type __n) { + if (__pos > size()) this->__throw_out_of_range(); + if (__n == npos) { + __erase_to_end(__pos); + } else { + __erase_external_with_move(__pos, __n); + } + return *this; } template