diff --git a/helpers/common_helpers.cpp b/helpers/common_helpers.cpp index 198dc763..58ac1e15 100644 --- a/helpers/common_helpers.cpp +++ b/helpers/common_helpers.cpp @@ -499,3 +499,31 @@ std::string common_helpers::to_str(std::wstring_view wstr) 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; +} diff --git a/helpers/common_helpers/common_helpers.hpp b/helpers/common_helpers/common_helpers.hpp index ea30c4ed..7d6707a0 100644 --- a/helpers/common_helpers/common_helpers.hpp +++ b/helpers/common_helpers/common_helpers.hpp @@ -110,4 +110,6 @@ std::string get_utc_time(); std::wstring to_wstr(std::string_view str); 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); + }