Implement istring::insert with given index and count

This commit is contained in:
Myles Busig 2024-03-07 16:07:21 -07:00
parent cb4f9d2861
commit 18246e131f

View File

@ -205,7 +205,33 @@ istring& istring::insert(size_t index, const istring& str) {
// only copy the string if there is room // only copy the string if there is room
size_t num_to_copy = std::min(str.m_size, space); size_t num_to_copy = std::min(str.m_size, space);
mtl::memmove(substr, str.m_str, num_to_copy); mtl::memcpy(substr, str.m_str, num_to_copy);
m_size += num_to_copy;
m_str[m_size] = '\0';
return *this;
}
istring& istring::insert(size_t index, const istring& str, size_t str_index, size_t count) {
if (count == npos) {
count = str.m_size - str_index;
}
char* substr = m_str + index;
size_t space = m_capacity - index;
if (space > count) {
size_t space_to_move = space - count;
size_t items_to_move = m_size - index;
// only move items if there is room, and they exist
size_t num_to_move = std::min(items_to_move, space_to_move);
mtl::memmove(substr + count, substr, num_to_move);
}
// only copy the string if there is room
size_t num_to_copy = std::min(count, space);
mtl::memcpy(substr, str.m_str + str_index, num_to_copy);
m_size += num_to_copy; m_size += num_to_copy;
m_str[m_size] = '\0'; m_str[m_size] = '\0';