Skip to content

Commit 3a181bf

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

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/util/string_utils.cpp

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

149149
return result;
150150
}
151+
152+
/// Copyright notice:
153+
/// Attributed to Gauthier Boaglio
154+
/// Source: https://stackoverflow.com/a/24315631/7501486
155+
/// Used under MIT license
156+
void replace_all(
157+
std::string &str,
158+
const std::string &from,
159+
const std::string &to)
160+
{
161+
size_t start_pos = 0;
162+
while((start_pos = str.find(from, start_pos)) != std::string::npos)
163+
{
164+
str.replace(start_pos, from.length(), to);
165+
start_pos += to.length();
166+
}
167+
}

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)