add simgear::strutils::replace

This commit is contained in:
Richard Harrison
2019-03-17 18:20:27 +01:00
parent f964374027
commit 0792962591
2 changed files with 22 additions and 1 deletions

View File

@@ -1007,6 +1007,16 @@ std::string unescape(const char* s)
}
return r;
}
std::string replace(std::string source, const std::string search, const std::string replacement, std::size_t start_pos)
{
if (start_pos < source.length()) {
while ((start_pos = source.find(search, start_pos)) != std::string::npos) {
source.replace(start_pos, search.length(), replacement);
start_pos += replacement.length();
}
}
return source;
}
string sanitizePrintfFormat(const string& input)
{

View File

@@ -333,7 +333,18 @@ namespace simgear {
inline std::string unescape(const std::string& str)
{ return unescape(str.c_str()); }
/**
/**
* Replace matching elements of string.
*
* @param source source string
* @param search search string
* @param replace replacement string
* @param start_pos starting position for replacement in source. Checked to ensure less than length of source.
* @return string with all occurrences of search changed to replace
*/
std::string replace(std::string source, const std::string search, const std::string replacement, std::size_t start_pos = 0);
/**
* Check a printf-style format string for dangerous (buffer-overflowing,
* memory re-writing) format tokens. If a problematic token is
* found, logs an error (SG_WARN) and returns an empty format string.