remove start, end

This commit is contained in:
mrbesen 2020-10-01 14:01:54 +02:00
parent 6c927109ec
commit 357783a082
Signed by: MrBesen
GPG Key ID: 596B2350DCD67504
5 changed files with 50 additions and 8 deletions

View File

@ -9,11 +9,12 @@ unsigned int count(const std::string& str, char c); //count occurances of c in s
bool equalsIgnoreCase(const std::string& a, const std::string& b, size_t max = std::string::npos);
void toLower(const std::string& in, std::string& out); //copy toLower
void toLower(std::string& in); //inplace toLower
bool endsWith(const std::string& str, const std::string& ending);
bool startsWith(const std::string& str, const std::string& start);
void removeEnd(std::string& str, const std::string& ending);
void removeStart(std::string& str, const std::string& start);
}

View File

@ -33,10 +33,5 @@ void mrbesen::Files::extention(const std::string& path, std::string& ext) {
}
ext = filestr.substr(pos+1);
//TODO:
//was ist mit dotfiles?? -> prüfen auf '/' for '.'? oder auf string position (damit der punkt icht ganz links sei kann)?
//problem: "a.b/file" würde .b/file zurückgeben -> muss also auch auf '/' geprüft werden!
}

View File

@ -70,3 +70,19 @@ bool mrbesen::Util::startsWith(const std::string& str, const std::string& start)
return str.find(start) == 0;
}
void mrbesen::Util::removeEnd(std::string& str, const std::string& ending) {
if(ending.empty()) return;
if(endsWith(str, ending)) {
str.resize(str.size() - ending.length());
}
}
void mrbesen::Util::removeStart(std::string& str, const std::string& start) {
if(start.empty()) return;
if(startsWith(str, start)) {
str = str.substr(start.length());
}
}

View File

@ -10,8 +10,9 @@ int testUtil_Count();
int testUtil_equalsIgnoreCase();
int testUtil_toLower();
int testUtil_start_endWith();
int testUtil_removeStart_End();
test_t tests[] = {testFiles_parent, testFiles_file, testFiles_extention, testUtil_Count, testUtil_equalsIgnoreCase, testUtil_toLower, testUtil_start_endWith, NULL};
test_t tests[] = {testFiles_parent, testFiles_file, testFiles_extention, testUtil_Count, testUtil_equalsIgnoreCase, testUtil_toLower, testUtil_start_endWith, testUtil_removeStart_End, NULL};
int main(int argc, char** argv) {

View File

@ -87,5 +87,34 @@ int testUtil_start_endWith() {
ASSERT(!endsWith(a, "abcdefhhijklm"), "");
ASSERT(endsWith(a, a), "");
return TESTGOOD;
}
int testUtil_removeStart_End() {
std::string a = "abcdefg";
removeStart(a, "");
ASSERT(a == "abcdefg", "");
removeStart(a, "b");
ASSERT(a == "abcdefg", "");
removeStart(a, "a");
ASSERT(a == "bcdefg", "");
removeStart(a, "bcd");
ASSERT(a == "efg", "");
removeEnd(a, "xyz");
ASSERT(a == "efg", "");
removeEnd(a, "");
ASSERT(a == "efg", "");
removeEnd(a, "fg");
ASSERT(a == "e", "");
removeEnd(a, "e");
ASSERT(a == "", "");
return TESTGOOD;
}