helper function to str replace substr

This commit is contained in:
a 2024-08-03 07:02:39 +03:00
parent c0bc32849a
commit 08f2bc36e0
2 changed files with 30 additions and 0 deletions

View File

@ -499,3 +499,31 @@ std::string common_helpers::to_str(std::wstring_view wstr)
return {}; return {};
} }
std::string common_helpers::str_replace_all(std::string_view source, std::string_view substr, std::string_view replace)
{
if (source.empty() || substr.empty()) return std::string(source);
std::string out{};
out.reserve(source.size() / 4); // out could be bigger or smaller than source, start small
size_t start_offset = 0;
auto f_idx = source.find(substr);
while (std::string::npos != f_idx) {
// copy the chars before the match
auto chars_count_until_match = f_idx - start_offset;
out.append(source, start_offset, chars_count_until_match);
// copy the replace str
out.append(replace);
// adjust the start offset to point at the char after this match
start_offset = f_idx + substr.size();
// search for next match
f_idx = source.find(substr, start_offset);
}
// copy last remaining part
out.append(source, start_offset, std::string::npos);
return out;
}

View File

@ -110,4 +110,6 @@ std::string get_utc_time();
std::wstring to_wstr(std::string_view str); std::wstring to_wstr(std::string_view str);
std::string to_str(std::wstring_view wstr); std::string to_str(std::wstring_view wstr);
std::string str_replace_all(std::string_view source, std::string_view substr, std::string_view replace);
} }