diff --git a/inc/util.h b/inc/util.h index ec6b5ea..53e96bb 100644 --- a/inc/util.h +++ b/inc/util.h @@ -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); + } \ No newline at end of file diff --git a/src/Files.cpp b/src/Files.cpp index ac4902d..4a6ba04 100644 --- a/src/Files.cpp +++ b/src/Files.cpp @@ -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! } diff --git a/src/Util.cpp b/src/Util.cpp index e13c9b4..a8cc4b8 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -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()); + } +} diff --git a/tests/main.cpp b/tests/main.cpp index 1e2e9f6..252f875 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -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) { diff --git a/tests/utilstest.cpp b/tests/utilstest.cpp index c0fe73d..dafd73b 100644 --- a/tests/utilstest.cpp +++ b/tests/utilstest.cpp @@ -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; } \ No newline at end of file