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
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
43 changes: 31 additions & 12 deletions source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,40 @@

int main()
{
std::string str{ };
str.reserve(1'000);

#if 1
utf8::ustring str{ "привет" };
std::string a{ "s" };
std::string b{ "п" };
std::string c{ "\xf0\x92\x8d\x87" };

str.replace_char("s", 0);
str.replace_char("g", 0);
str.replace_char("п", 0);
str.replace_char("s", 3);
str.replace_char("g", 4);
str.replace_char("п", 3);
str.replace_char("\xf0\x92\x8d\x87", 2);
str.replace_char("g", 2);
str.replace_char("مرحبا", 2);
for (std::size_t i{}; i < 5'000; ++i)
{
str += a;
str += b;
str += c;
}

std::cout << str << '\n';
std::chrono::duration<double> res{ };

#if 0
auto start = std::chrono::high_resolution_clock::now();

utf8::ustring tmp{ str };
for (std::size_t i{}; i < 1'000'000'000; ++i)
{
tmp.replace_char("s", 0);
tmp.replace_char("ы", 300);
tmp.replace_char("\xf0\x92\x8d\x87", 1234);
tmp.replace_char("ы", 789);
tmp.replace_char("\xf0\x92\x8d\x87", 1900);
}

auto end = std::chrono::high_resolution_clock::now();
res = end - start;

std::cout << res.count() << "s\n";
std::cout << tmp << '\n';
#endif

return 0;
Expand Down
16 changes: 13 additions & 3 deletions utf8-string-lib/utf8_string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading