mirror of
https://github.com/TES3MP/openmw-tes3mp.git
synced 2025-01-16 18:19:55 +00:00
Added StringUtil::replaceAll()
This commit is contained in:
parent
92aaff3b78
commit
09aac22782
1 changed files with 30 additions and 0 deletions
|
@ -2,6 +2,7 @@
|
|||
#define MISC_STRINGOPS_H
|
||||
|
||||
#include <cctype>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
|
||||
|
@ -138,6 +139,35 @@ public:
|
|||
|
||||
return notFound;
|
||||
}
|
||||
|
||||
/** @brief Replaces all occurrences of a string in another string.
|
||||
*
|
||||
* @param str The string to operate on.
|
||||
* @param what The string to replace.
|
||||
* @param with The replacement string.
|
||||
* @param what_len The length of the string to replace.
|
||||
* @param with_len The length of the replacement string.
|
||||
*
|
||||
* @return A reference to the string passed in @p str.
|
||||
*/
|
||||
static std::string &replaceAll(std::string &str, const char *what, const char *with,
|
||||
std::size_t what_len=std::string::npos, std::size_t with_len=std::string::npos)
|
||||
{
|
||||
if (what_len == std::string::npos)
|
||||
what_len = strlen(what);
|
||||
|
||||
if (with_len == std::string::npos)
|
||||
with_len = strlen(with);
|
||||
|
||||
std::size_t found;
|
||||
std::size_t offset = 0;
|
||||
while((found = str.find(what, offset, what_len)) != std::string::npos)
|
||||
{
|
||||
str.replace(found, what_len, with, with_len);
|
||||
offset = found + with_len;
|
||||
}
|
||||
return str;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue