mirror of
https://github.com/tuxbox-fork-migrations/recycled-ni-neutrino.git
synced 2025-08-28 16:01:10 +02:00
system/helpers.cpp: Add cstr_replace()
- This is a faster C version of str_replace()
Origin commit data
------------------
Branch: ni/coolstream
Commit: 020936acbe
Author: Michael Liebmann <tuxcode.bbg@gmail.com>
Date: 2017-09-14 (Thu, 14 Sep 2017)
Origin message was:
------------------
system/helpers.cpp: Add cstr_replace()
- This is a faster C version of str_replace()
------------------
This commit was generated by Migit
This commit is contained in:
@@ -523,6 +523,61 @@ std::string& str_replace(const std::string &search, const std::string &replace,
|
|||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* ported from:
|
||||||
|
* https://stackoverflow.com/questions/779875/what-is-the-function-to-replace-string-in-c
|
||||||
|
*
|
||||||
|
* You must delete the result if result is non-NULL
|
||||||
|
*/
|
||||||
|
const char *cstr_replace(const char *search, const char *replace, const char *text)
|
||||||
|
{
|
||||||
|
const char *result; // the return string
|
||||||
|
const char *ins; // the next insert point
|
||||||
|
char *tmp; // varies
|
||||||
|
int len_search; // length of search (the string to remove)
|
||||||
|
int len_replace; // length of replace (the string to replace search with)
|
||||||
|
int len_front; // distance between search and end of last search
|
||||||
|
int count; // number of replacements
|
||||||
|
|
||||||
|
// sanity checks and initialization
|
||||||
|
if (!text || !search)
|
||||||
|
return NULL;
|
||||||
|
len_search = strlen(search);
|
||||||
|
if (len_search == 0)
|
||||||
|
return NULL; // empty search causes infinite loop during count
|
||||||
|
if (!replace)
|
||||||
|
replace = "";
|
||||||
|
len_replace = strlen(replace);
|
||||||
|
|
||||||
|
// count the number of replacements needed
|
||||||
|
ins = text;
|
||||||
|
for (count = 0; (tmp = (char*)strstr(ins, search)); ++count)
|
||||||
|
ins = tmp + len_search;
|
||||||
|
|
||||||
|
int len_tmp = strlen(text) + (len_replace - len_search) * count + 1;
|
||||||
|
tmp = new char[len_tmp];
|
||||||
|
memset(tmp, '\0', len_tmp);
|
||||||
|
result = (const char*)tmp;
|
||||||
|
|
||||||
|
if (!result)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
// first time through the loop, all the variable are set correctly
|
||||||
|
// from here on,
|
||||||
|
// tmp points to the end of the result string
|
||||||
|
// ins points to the next occurrence of search in text
|
||||||
|
// text points to the remainder of text after "end of search"
|
||||||
|
while (count--) {
|
||||||
|
ins = strstr(text, search);
|
||||||
|
len_front = ins - text;
|
||||||
|
tmp = strncpy(tmp, text, len_front) + len_front;
|
||||||
|
tmp = strncpy(tmp, replace, len_replace) + len_replace;
|
||||||
|
text += len_front + len_search; // move to next "end of search"
|
||||||
|
}
|
||||||
|
strncpy(tmp, text, strlen(text));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
std::string& htmlEntityDecode(std::string& text)
|
std::string& htmlEntityDecode(std::string& text)
|
||||||
{
|
{
|
||||||
struct decode_table {
|
struct decode_table {
|
||||||
|
@@ -68,6 +68,7 @@ std::string cutString(const std::string str, int msgFont, const int width);
|
|||||||
std::string strftime(const char *format, const struct tm *tm);
|
std::string strftime(const char *format, const struct tm *tm);
|
||||||
std::string strftime(const char *format, time_t when, bool gm = false);
|
std::string strftime(const char *format, time_t when, bool gm = false);
|
||||||
time_t toEpoch(std::string &date);
|
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);
|
||||||
std::string& htmlEntityDecode(std::string& text);
|
std::string& htmlEntityDecode(std::string& text);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user