Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewritten replace char algorithm #6

Merged
merged 3 commits into from
Mar 21, 2024
Merged
Changes from 1 commit
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
Next Next commit
improved replace_char algorithm
b1tflyyyy committed Mar 21, 2024
commit f27c0d7ec23a1a48d031a8eaf814207a689ca1f7
16 changes: 13 additions & 3 deletions utf8-string-lib/utf8_string.cpp
Original file line number Diff line number Diff line change
@@ -170,9 +170,19 @@ namespace utf8
std::basic_string<char> new_string{ };
new_string.resize(old_string_size + new_symbol_size - old_symbol_info.Symbol_Size); // calculate & resize new string size

std::memcpy(new_string.data(), String.c_str(), replace_position); // copy bytes to a new string before idx
std::memcpy((new_string.data() + replace_position), new_symbol.data(), new_symbol_size); // insert a new symbol by idx
std::memcpy((new_string.data() + replace_position + new_symbol_size), (String.c_str() + replace_position + old_symbol_info.Symbol_Size), old_string_size - replace_position); // copy bytes after inserted symbol
for (std::size_t i{}, counter{}; i < old_string_size;)
{
if (i == replace_position)
{
std::memcpy(new_string.data() + i, new_symbol.data(), new_symbol_size);
counter += new_symbol_size;
i += old_symbol_info.Symbol_Size;

continue;
}

new_string[counter++] = String[i++];
}

auto new_offset{ static_cast<std::ptrdiff_t>(new_symbol_size) - static_cast<std::ptrdiff_t>(old_symbol_info.Symbol_Size) };
Recalculate_Symbols_Offset(idx + 1, new_offset);