system/helpers: Add new Parameter 'start_pos' to str_replace()

This allows flexibility to specify a starting position from where to search
for the first occurrence of the search string. By default,
the position is set to 0 and the function will behave as before.
However, when a position is provided,
the function will replace only the first occurrence of
the search string after that position.


Origin commit data
------------------
Branch: ni/coolstream
Commit: 912f256276
Author: Thilo Graf <dbt@novatux.de>
Date: 2023-03-07 (Tue, 07 Mar 2023)



------------------
This commit was generated by Migit
This commit is contained in:
2023-03-07 23:20:43 +01:00
committed by vanhofen
parent 0ec76028eb
commit de40ee2363
2 changed files with 20 additions and 7 deletions

View File

@@ -791,17 +791,30 @@ time_t toEpoch(std::string &date)
}
std::string& str_replace(const std::string &search, const std::string &replace, std::string &text)
/**
* Replaces all occurrences of a substring with another string in a given text,
* starting from a specified position.
*
* @param search The substring to search for.
* @param replace The string to replace the occurrences of `search` with.
* @param text The text to perform the replacement on.
* @param start_pos The position to start searching for `search` in `text`.
* Default value is 0.
*
* @return The modified text after all replacements have been made.
*/
std::string &str_replace(const std::string &search, const std::string &replace, std::string &text, size_t start_pos)
{
if (search.empty() || text.empty())
return text;
size_t searchLen = search.length();
while (1) {
size_t pos = text.find(search);
if (pos == std::string::npos)
break;
text.replace(pos, searchLen, replace);
size_t startPos = text.find(search, start_pos);
while (startPos != std::string::npos)
{
text.replace(startPos, searchLen, replace);
startPos = text.find(search, startPos + replace.length());
}
return text;
}

View File

@@ -84,7 +84,7 @@ std::string strftime(const char *format, const struct tm *tm);
std::string strftime(const char *format, time_t when, bool gm = false);
time_t toEpoch(std::string &date);
const char *cstr_replace(const char *search, const char *replace, const char *text);
std::string& str_replace(const std::string &search, const std::string &replace, std::string &text);
std::string& str_replace(const std::string &search, const std::string &replace, std::string &text, size_t start_pos = 0);
std::string& htmlEntityDecode(std::string& text);
const char* neutrinoMode_to_string(int mode);