remove start, end
This commit is contained in:
parent
6c927109ec
commit
fa59de25f7
|
@ -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);
|
||||
|
||||
bool removeEnd(std::string& str, const std::string& ending); //returns true on change (ending = "" -> returns false)
|
||||
bool removeStart(std::string& str, const std::string& start);
|
||||
|
||||
}
|
|
@ -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!
|
||||
}
|
||||
|
||||
|
|
20
src/Util.cpp
20
src/Util.cpp
|
@ -70,3 +70,23 @@ bool mrbesen::Util::startsWith(const std::string& str, const std::string& start)
|
|||
|
||||
return str.find(start) == 0;
|
||||
}
|
||||
|
||||
bool mrbesen::Util::removeEnd(std::string& str, const std::string& ending) {
|
||||
if(ending.empty()) return false;
|
||||
|
||||
if(endsWith(str, ending)) {
|
||||
str.resize(str.size() - ending.length());
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool mrbesen::Util::removeStart(std::string& str, const std::string& start) {
|
||||
if(start.empty()) return false;
|
||||
|
||||
if(startsWith(str, start)) {
|
||||
str = str.substr(start.length());
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
||||
|
|
|
@ -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";
|
||||
ASSERT(!removeStart(a, ""), "");
|
||||
ASSERT(a == "abcdefg", "");
|
||||
|
||||
ASSERT(!removeStart(a, "b"), "");
|
||||
ASSERT(a == "abcdefg", "");
|
||||
|
||||
ASSERT(removeStart(a, "a"), "");
|
||||
ASSERT(a == "bcdefg", "");
|
||||
|
||||
ASSERT(removeStart(a, "bcd"), "");
|
||||
ASSERT(a == "efg", "");
|
||||
|
||||
ASSERT(!removeEnd(a, "xyz"), "");
|
||||
ASSERT(a == "efg", "");
|
||||
|
||||
ASSERT(!removeEnd(a, ""), "");
|
||||
ASSERT(a == "efg", "");
|
||||
|
||||
ASSERT(removeEnd(a, "fg"), "");
|
||||
ASSERT(a == "e", "");
|
||||
|
||||
ASSERT(removeEnd(a, "e"), "");
|
||||
ASSERT(a == "", "");
|
||||
|
||||
return TESTGOOD;
|
||||
}
|
Loading…
Reference in New Issue