This commit is contained in:
mrbesen 2020-10-08 18:55:55 +02:00
parent c709e9f358
commit 4a12cb7394
Signed by: MrBesen
GPG Key ID: 596B2350DCD67504
4 changed files with 53 additions and 10 deletions

View File

@ -24,7 +24,8 @@ bool insertStart(std::string& str, const std::string& start);
unsigned int split(const std::string& str, const std::string& token, std::string* out, unsigned int count);
void trim(std::string& str, char c = ' ');
bool trim(std::string& str, char c = ' '); //return true if the string was changed
bool trimOnce(std::string& str, char c = ' ');
std::string bytesToBase16(char* buffer, unsigned int len);
std::string bytesToBase64(char* buffer, unsigned int len);

View File

@ -119,14 +119,24 @@ unsigned int mrbesen::util::split(const std::string& str, const std::string& tok
return count;
}
void mrbesen::util::trim(std::string& str, char c) {
bool mrbesen::util::trim(std::string& str, char c) {
size_t first = str.find_first_not_of(c);
if(first == std::string::npos) {
bool changed = !str.empty();
str = "";
return;
return changed;
}
size_t last = str.find_last_not_of(c)+1;
str = str.substr(first, last-first);
bool changed = (last-first) < str.size();
if(changed)
str = str.substr(first, last-first);
return changed;
}
bool mrbesen::util::trimOnce(std::string& str, char c) {
bool a = removeStart(str, std::string(1, c));
bool b = removeEnd(str, std::string(1, c));
return a || b;
}
std::string mrbesen::util::bytesToBase16(char* buffer, unsigned int len) {

View File

@ -15,6 +15,7 @@ int testUtil_start_endWith();
int testUtil_removeStart_End();
int testUtil_insertStart_End();
int testUtil_trim();
int testUtil_trimOnce();
int testUtil_base();
int testStringSpliterator();
@ -23,7 +24,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_base,
testUtil_Count, testUtil_equalsIgnoreCase, testUtil_toLower, testUtil_start_endWith, testUtil_removeStart_End, testUtil_insertStart_End, testUtil_trim, testUtil_trimOnce, testUtil_base,
testStringSpliterator, testUtilSplit,
testGetDoy,
NULL};

View File

@ -152,19 +152,50 @@ int testUtil_insertStart_End() {
int testUtil_trim() {
std::string a = " abc def ";
trim(a);
ASSERT(trim(a), "");
ASSERT(a == "abc def", a);
trim(a, '_');
ASSERT(!trim(a, '_'), "");
ASSERT(a == "abc def", a);
trim(a, 'f');
ASSERT(trim(a, 'f'), "");
ASSERT(a == "abc de", a);
a = " ";
trim(a);
ASSERT(trim(a), "");
ASSERT(a == "", a);
trim(a);
ASSERT(!trim(a), "");
ASSERT(a == "", a);
return TESTGOOD;
}
int testUtil_trimOnce() {
std::string a = " abcd ";
ASSERT(!trimOnce(a, 'a'), "");
ASSERT(a == " abcd ", a);
ASSERT(trimOnce(a), "");
ASSERT(a == " abcd ", a);
ASSERT(trimOnce(a), "");
ASSERT(a == "abcd ", a);
ASSERT(trimOnce(a), "");
ASSERT(a == "abcd", a);
ASSERT(!trimOnce(a), "");
ASSERT(a == "abcd", a);
ASSERT(!trimOnce(a, 'b'), "");
ASSERT(a == "abcd", a);
ASSERT(trimOnce(a, 'a'), "");
ASSERT(a == "bcd", a);
a = " ";
ASSERT(trimOnce(a), "");
ASSERT(a == " ", a);
ASSERT(trimOnce(a), "");
ASSERT(a == "", a);
ASSERT(!trimOnce(a), "");
ASSERT(a == "", a);
ASSERT(!trimOnce(a, 'a'), "");
ASSERT(a == "", a);
return TESTGOOD;