From c69ffa030511623452867c3f6cf2804d0c066848 Mon Sep 17 00:00:00 2001 From: Michael Liebmann Date: Thu, 14 Sep 2017 15:27:45 +0200 Subject: [PATCH] system/helpers.cpp: Add cstr_replace() - This is a faster C version of str_replace() Origin commit data ------------------ Branch: ni/coolstream Commit: https://github.com/neutrino-images/ni-neutrino/commit/020936acbebf1b40f4c51c6674b0c2ca640f5ea8 Author: Michael Liebmann 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 --- src/system/helpers.cpp | 55 ++++++++++++++++++++++++++++++++++++++++++ src/system/helpers.h | 1 + 2 files changed, 56 insertions(+) diff --git a/src/system/helpers.cpp b/src/system/helpers.cpp index 17afa8c1a..cc0f01325 100644 --- a/src/system/helpers.cpp +++ b/src/system/helpers.cpp @@ -523,6 +523,61 @@ std::string& str_replace(const std::string &search, const std::string &replace, 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) { struct decode_table { diff --git a/src/system/helpers.h b/src/system/helpers.h index 57fe0216d..7bd7afb2d 100644 --- a/src/system/helpers.h +++ b/src/system/helpers.h @@ -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, 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& htmlEntityDecode(std::string& text);