Fix insert_impl... again

This commit is contained in:
rexy712 2022-07-16 19:55:33 -07:00
parent 13ce1550e2
commit e636ba47fe

View File

@ -696,12 +696,13 @@ namespace rexy{
REXY_CPP20_CONSTEXPR auto basic_string<Char,Alloc>::_insert_impl(size_type pos, InputIt start, size_type insert_count)noexcept(is_nothrow_allocator_v<Alloc>) -> basic_string&{ REXY_CPP20_CONSTEXPR auto basic_string<Char,Alloc>::_insert_impl(size_type pos, InputIt start, size_type insert_count)noexcept(is_nothrow_allocator_v<Alloc>) -> basic_string&{
const size_type len = this->length(); const size_type len = this->length();
const size_type cap = this->capacity(); const size_type cap = this->capacity();
const size_type after_pos_count = len > pos ? len - pos : 0; //add one for null terminator
const size_type after_pos_count = (len > pos ? len - pos : 0) + 1;
if(insert_count + len <= cap){ if(insert_count + len <= cap){
auto* dest_ptr = this->data() + len + insert_count - 1; auto* dest_ptr = this->data() + len + insert_count;
const auto* src_ptr = this->data() + pos + insert_count; const auto* src_ptr = this->data() + len;
for(size_type i = 0;i < after_pos_count;++i){ for(size_type i = 0;i < after_pos_count;++i){
*dest_ptr-- = *src_ptr--; *dest_ptr-- = *src_ptr--;
} }
@ -727,8 +728,6 @@ namespace rexy{
} }
rexy::memcpy(ptr, this->get_pointer() + pos, sizeof(value_type) * after_pos_count); rexy::memcpy(ptr, this->get_pointer() + pos, sizeof(value_type) * after_pos_count);
ptr += after_pos_count;
*ptr = 0; //null terminator
newstr.set_length(len + insert_count); newstr.set_length(len + insert_count);
return (*this = std::move(newstr)); return (*this = std::move(newstr));