Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add replacements for boost::algorithm::replace_all[_copy].
- Loading branch information
1 parent
a25a3fb
commit ca3a589
Showing
5 changed files
with
49 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
|
||
namespace string | ||
{ | ||
|
||
/** | ||
* Replaces all occurrences of the given search string in the subject | ||
* with the given replacement, in-place. | ||
*/ | ||
inline void replace_all(std::string& subject, const std::string& search, const std::string& replacement) | ||
{ | ||
std::size_t pos = 0; | ||
|
||
while ((pos = subject.find(search, pos)) != std::string::npos) | ||
{ | ||
subject.replace(pos, search.length(), replacement); | ||
pos += replacement.length(); | ||
} | ||
} | ||
|
||
/** | ||
* Replaces all occurrences of of the given search string with | ||
* the given replacement and returns a new string instance | ||
* containing the result. The incoming subject is passed by value such | ||
* that the original string is not altered. | ||
*/ | ||
inline std::string replace_all_copy(std::string subject, const std::string& search, const std::string& replacement) | ||
{ | ||
std::size_t pos = 0; | ||
|
||
while ((pos = subject.find(search, pos)) != std::string::npos) | ||
{ | ||
subject.replace(pos, search.length(), replacement); | ||
pos += replacement.length(); | ||
} | ||
|
||
return subject; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters