replace
This commit is contained in:
parent
66a18f27c5
commit
62a77480ff
|
@ -32,6 +32,7 @@ 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);
|
||||
|
||||
class StringSpliterator : public std::iterator<std::output_iterator_tag, std::string> {
|
||||
public:
|
||||
|
|
13
src/util.cpp
13
src/util.cpp
|
@ -183,6 +183,19 @@ 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;
|
||||
}
|
||||
|
||||
|
||||
mrbesen::util::StringSpliterator::StringSpliterator(const std::string& d, const std::string& token) : data(d), token(token) {
|
||||
//trim
|
||||
|
|
|
@ -17,6 +17,7 @@ int testUtil_insertStart_End();
|
|||
int testUtil_trim();
|
||||
int testUtil_trimOnce();
|
||||
int testUtil_base();
|
||||
int testUtil_replace();
|
||||
|
||||
int testStringSpliterator();
|
||||
int testUtilSplit();
|
||||
|
@ -24,7 +25,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,
|
||||
testStringSpliterator, testUtilSplit,
|
||||
testGetDoy,
|
||||
NULL};
|
||||
|
|
|
@ -224,3 +224,18 @@ int testUtil_base() {
|
|||
|
||||
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;
|
||||
}
|
Loading…
Reference in New Issue