From faa8cef700d1f00dea3499a49bb1cae95e3d564b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20B=C3=A9raud?= Date: Tue, 3 Sep 2019 12:11:51 -0400 Subject: [PATCH] string utils: add string_replace Change-Id: I86a2b1564be127f70dd991b86fe0b14440cee299 --- src/string_utils.cpp | 9 +++++++++ src/string_utils.h | 7 +++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/string_utils.cpp b/src/string_utils.cpp index 3e9cbe070..adadbae16 100644 --- a/src/string_utils.cpp +++ b/src/string_utils.cpp @@ -121,4 +121,13 @@ split_string_to_unsigned(const std::string &s, char delim) return result; } +void string_replace(std::string& str, const std::string& from, const std::string& to) +{ + size_t start_pos = 0; + while ((start_pos = str.find(from, start_pos)) != std::string::npos) { + str.replace(start_pos, from.length(), to); + start_pos += to.length(); // Handles case where 'to' is a substring of 'from' + } +} + } // namespace jami diff --git a/src/string_utils.h b/src/string_utils.h index f5c69cc5a..3f0e85fc9 100644 --- a/src/string_utils.h +++ b/src/string_utils.h @@ -19,8 +19,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -#ifndef STRING_UTILS_H -#define STRING_UTILS_H +#pragma once #include #include @@ -67,6 +66,6 @@ split_string(const std::string& s, char sep); std::vector split_string_to_unsigned(const std::string& s, char sep); -} // namespace jami +void string_replace(std::string& str, const std::string& from, const std::string& to); -#endif +} // namespace jami