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); 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 bytesToBase16(char* buffer, unsigned int len);
std::string bytesToBase64(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; 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); size_t first = str.find_first_not_of(c);
if(first == std::string::npos) { if(first == std::string::npos) {
bool changed = !str.empty();
str = ""; str = "";
return; return changed;
} }
size_t last = str.find_last_not_of(c)+1; 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) { 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_removeStart_End();
int testUtil_insertStart_End(); int testUtil_insertStart_End();
int testUtil_trim(); int testUtil_trim();
int testUtil_trimOnce();
int testUtil_base(); int testUtil_base();
int testStringSpliterator(); int testStringSpliterator();
@ -23,7 +24,7 @@ int testUtilSplit();
int testGetDoy(); int testGetDoy();
test_t tests[] = {testFiles_parent, testFiles_file, testFiles_extention, testFiles_scan, testFiles_readFile, 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, testStringSpliterator, testUtilSplit,
testGetDoy, testGetDoy,
NULL}; NULL};

View File

@ -152,19 +152,50 @@ int testUtil_insertStart_End() {
int testUtil_trim() { int testUtil_trim() {
std::string a = " abc def "; std::string a = " abc def ";
trim(a); ASSERT(trim(a), "");
ASSERT(a == "abc def", a); ASSERT(a == "abc def", a);
trim(a, '_'); ASSERT(!trim(a, '_'), "");
ASSERT(a == "abc def", a); ASSERT(a == "abc def", a);
trim(a, 'f'); ASSERT(trim(a, 'f'), "");
ASSERT(a == "abc de", a); ASSERT(a == "abc de", a);
a = " "; a = " ";
trim(a); ASSERT(trim(a), "");
ASSERT(a == "", 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); ASSERT(a == "", a);
return TESTGOOD; return TESTGOOD;