replace with string
This commit is contained in:
parent
62a77480ff
commit
b541bbebcc
|
@ -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<std::output_iterator_tag, std::string> {
|
||||
public:
|
||||
|
|
30
src/util.cpp
30
src/util.cpp
|
@ -1,6 +1,7 @@
|
|||
#include "util.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <sstream>
|
||||
|
||||
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
|
||||
|
|
|
@ -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};
|
||||
|
|
|
@ -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;
|
||||
}
|
Loading…
Reference in New Issue