Compare commits
2 Commits
66a18f27c5
...
b541bbebcc
Author | SHA1 | Date |
---|---|---|
mrbesen | b541bbebcc | |
mrbesen | 62a77480ff |
|
@ -32,6 +32,8 @@ bool trimOnce(std::string& str, char c = ' ');
|
|||
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:
|
||||
|
|
43
src/util.cpp
43
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;
|
||||
|
@ -183,6 +184,48 @@ std::string mrbesen::util::bytesToBase64(char* buffer, unsigned int len) {
|
|||
return ret;
|
||||
}
|
||||
|
||||
bool mrbesen::util::replace(std::string& modify, const std::string& search, char replace) {
|
||||
bool changed = false;
|
||||
for(size_t pos = modify.find_first_of(search); pos < std::string::npos; ) {
|
||||
modify[pos] = replace;
|
||||
changed = true;
|
||||
|
||||
//search next
|
||||
pos = modify.find_first_of(search, pos+1);
|
||||
}
|
||||
|
||||
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
|
||||
|
|
|
@ -17,6 +17,8 @@ int testUtil_insertStart_End();
|
|||
int testUtil_trim();
|
||||
int testUtil_trimOnce();
|
||||
int testUtil_base();
|
||||
int testUtil_replace();
|
||||
int testUtil_replace2();
|
||||
|
||||
int testStringSpliterator();
|
||||
int testUtilSplit();
|
||||
|
@ -24,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_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};
|
||||
|
|
|
@ -222,5 +222,43 @@ int testUtil_base() {
|
|||
ret = bytesToBase64(buf, 6);
|
||||
ASSERT(ret == "AKsSzTT_", ret);
|
||||
|
||||
return TESTGOOD;
|
||||
}
|
||||
|
||||
int testUtil_replace() {
|
||||
std::string test = "#abc#def#";
|
||||
|
||||
ASSERT(!replace(test, "", ' '), "");
|
||||
ASSERT(test == "#abc#def#", test);
|
||||
ASSERT(!replace(test, "xyz", ' '), "");
|
||||
ASSERT(test == "#abc#def#", test);
|
||||
ASSERT(replace(test, "af", ' '), "");
|
||||
ASSERT(test == "# bc#de #", test);
|
||||
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