Skip to content

Commit 0886132

Browse files
author
Joel Allred
committed
Add replace_all string utility
Utility to search and replace strings inside a string.
1 parent 7823d9c commit 0886132

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/util/string_utils.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,16 @@ std::string escape(const std::string &s)
148148

149149
return result;
150150
}
151+
152+
void replace_all(
153+
std::string &str,
154+
const std::string &from,
155+
const std::string &to)
156+
{
157+
size_t start_pos = 0;
158+
while((start_pos = str.find(from, start_pos)) != std::string::npos)
159+
{
160+
str.replace(start_pos, from.length(), to);
161+
start_pos += to.length();
162+
}
163+
}

src/util/string_utils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,6 @@ Stream &join_strings(
6767
/// programming language.
6868
std::string escape(const std::string &);
6969

70+
void replace_all(std::string &, const std::string &, const std::string &);
71+
7072
#endif

0 commit comments

Comments
 (0)