From b541bbebcc26fe4a7ecc5943da065e41ff33f100 Mon Sep 17 00:00:00 2001 From: mrbesen Date: Tue, 3 Nov 2020 18:52:57 +0100 Subject: [PATCH] replace with string --- inc/util.h | 1 + src/util.cpp | 30 ++++++++++++++++++++++++++++++ tests/main.cpp | 3 ++- tests/utilstest.cpp | 23 +++++++++++++++++++++++ 4 files changed, 56 insertions(+), 1 deletion(-) diff --git a/inc/util.h b/inc/util.h index 7a32667..49ea5be 100644 --- a/inc/util.h +++ b/inc/util.h @@ -33,6 +33,7 @@ std::string bytesToBase16(char* buffer, unsigned int len); std::string bytesToBase64(char* buffer, unsigned int len); bool replace(std::string& modify, const std::string& search, char replace); +bool replace(std::string& modify, const std::string& search, const std::string& replacement); class StringSpliterator : public std::iterator { public: diff --git a/src/util.cpp b/src/util.cpp index 5e4445b..138c842 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -1,6 +1,7 @@ #include "util.h" #include +#include unsigned int mrbesen::util::count(const std::string& str, char c) { size_t pos = 0; @@ -196,6 +197,35 @@ bool mrbesen::util::replace(std::string& modify, const std::string& search, char return changed; } +bool mrbesen::util::replace(std::string& modify, const std::string& search, const std::string& replacement) { + if(search.empty()) return false; + if(search.size() > modify.size()) return false; + if(modify.find(search) == std::string::npos) return false; + + std::ostringstream out; + size_t pos = 0; + while(pos < modify.size()) { + //search for next occurance of search + size_t nextp = modify.find(search, pos); + bool end = (nextp == std::string::npos); + if(end) nextp = modify.size(); + + if(nextp > pos) { + out << modify.substr(pos, (nextp)-pos); + pos = nextp; + } + + if(!end) + out << replacement; + + pos += search.size(); + } + + modify = out.str(); + + return true; +} + mrbesen::util::StringSpliterator::StringSpliterator(const std::string& d, const std::string& token) : data(d), token(token) { //trim diff --git a/tests/main.cpp b/tests/main.cpp index 7a80cec..a114124 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -18,6 +18,7 @@ int testUtil_trim(); int testUtil_trimOnce(); int testUtil_base(); int testUtil_replace(); +int testUtil_replace2(); int testStringSpliterator(); int testUtilSplit(); @@ -25,7 +26,7 @@ int testUtilSplit(); int testGetDoy(); test_t tests[] = {testFiles_parent, testFiles_file, testFiles_extention, testFiles_scan, testFiles_readFile, - testUtil_Count, testUtil_equalsIgnoreCase, testUtil_toLower, testUtil_start_endWith, testUtil_removeStart_End, testUtil_insertStart_End, testUtil_trim, testUtil_trimOnce, testUtil_base, testUtil_replace, + testUtil_Count, testUtil_equalsIgnoreCase, testUtil_toLower, testUtil_start_endWith, testUtil_removeStart_End, testUtil_insertStart_End, testUtil_trim, testUtil_trimOnce, testUtil_base, testUtil_replace, testUtil_replace2, testStringSpliterator, testUtilSplit, testGetDoy, NULL}; diff --git a/tests/utilstest.cpp b/tests/utilstest.cpp index 7914398..1b24713 100644 --- a/tests/utilstest.cpp +++ b/tests/utilstest.cpp @@ -237,5 +237,28 @@ int testUtil_replace() { ASSERT(replace(test, "#q ", ' '), ""); ASSERT(test == " bc de ", test); + return TESTGOOD; +} + +int testUtil_replace2() { + std::string test = "#abc#def#"; + + ASSERT(!replace(test, "", ""), ""); + ASSERT(test == "#abc#def#", test); + ASSERT(!replace(test, "#z", ""), ""); + ASSERT(test == "#abc#def#", test); + ASSERT(replace(test, "#", "+"), ""); + ASSERT(test == "+abc+def+", test); + ASSERT(replace(test, "+", ""), ""); + ASSERT(test == "abcdef", test); + ASSERT(replace(test, "ab", "de"), ""); + ASSERT(test == "decdef", test); + ASSERT(replace(test, "de", ""), ""); + ASSERT(test == "cf", test); + ASSERT(replace(test, "cf", ""), ""); + ASSERT(test == "", test); + ASSERT(!replace(test, "cf", ""), ""); + ASSERT(test == "", test); + return TESTGOOD; } \ No newline at end of file