about summary refs log tree commit diff
path: root/src/libutil
diff options
context:
space:
mode:
Diffstat (limited to 'src/libutil')
-rw-r--r--src/libutil/util.cc14
-rw-r--r--src/libutil/util.hh5
2 files changed, 19 insertions, 0 deletions
diff --git a/src/libutil/util.cc b/src/libutil/util.cc
index 903b97100b93..596b79e10e69 100644
--- a/src/libutil/util.cc
+++ b/src/libutil/util.cc
@@ -1095,6 +1095,20 @@ string trim(const string & s, const string & whitespace)
 }
 
 
+string replaceStrings(const std::string & s,
+    const std::string & from, const std::string & to)
+{
+    if (from.empty()) return s;
+    string res = s;
+    size_t pos = 0;
+    while ((pos = res.find(from, pos)) != std::string::npos) {
+        res.replace(pos, from.size(), to);
+        pos += to.size();
+    }
+    return res;
+}
+
+
 string statusToString(int status)
 {
     if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
diff --git a/src/libutil/util.hh b/src/libutil/util.hh
index 6e20a22d9da8..187e05ece050 100644
--- a/src/libutil/util.hh
+++ b/src/libutil/util.hh
@@ -341,6 +341,11 @@ string chomp(const string & s);
 string trim(const string & s, const string & whitespace = " \n\r\t");
 
 
+/* Replace all occurrences of a string inside another string. */
+string replaceStrings(const std::string & s,
+    const std::string & from, const std::string & to);
+
+
 /* Convert the exit status of a child as returned by wait() into an
    error string. */
 string statusToString(int status);