From 884bbfc681030b80385af01819003f48b1e8daa7 Mon Sep 17 00:00:00 2001 From: Bob Polis Date: Fri, 2 Jul 2021 11:37:54 +0200 Subject: [PATCH] Added desctructive replace_all for std::string --- string_utils.cpp | 12 ++++++++++++ string_utils.hpp | 4 ++++ 2 files changed, 16 insertions(+) diff --git a/string_utils.cpp b/string_utils.cpp index 3bce5d8..b2c5722 100644 --- a/string_utils.cpp +++ b/string_utils.cpp @@ -106,6 +106,18 @@ map sc::parse_ini_file(const string& path) return result; } +void sc::replace_all(const std::string& what, const std::string& replacement, std::string& target) { + std::string::size_type pos = std::string::npos; + std::string::size_type from = 0; + do { + pos = target.find(what, from); + if (pos != std::string::npos) { + target.replace(pos, what.length(), replacement); + from = pos + replacement.length(); + } + } while (pos != std::string::npos); +} + string sc::str_replace(const string& what, const string& replacement, const string& target) { string result; diff --git a/string_utils.hpp b/string_utils.hpp index 45f4c03..34059f8 100644 --- a/string_utils.hpp +++ b/string_utils.hpp @@ -36,6 +36,10 @@ namespace sc { const std::wstring& replacement, const std::wstring& target); + void replace_all (const std::string& what, + const std::string& replacement, + std::string& target); + bool file_exists (const std::string& path); std::string file_get_contents (const std::string& path);