From 7b561f668363afc2a4b23b03edf7ec13736e02ec Mon Sep 17 00:00:00 2001 From: mrbesen Date: Tue, 29 Sep 2020 14:04:19 +0200 Subject: [PATCH] toLower --- .vscode/launch.json | 2 +- inc/util.h | 2 ++ src/Files.cpp | 10 +++++++--- src/Util.cpp | 10 ++++++++++ tests/filestests.cpp | 6 ++++++ tests/main.cpp | 3 ++- tests/utilstest.cpp | 24 +++++++++++++++++++++++- 7 files changed, 51 insertions(+), 6 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 82aa769..2151f3f 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -12,7 +12,7 @@ "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", - "environment": [], + "environment": [{"name": "LD_LIBRARY_PATH", "value": "./"}], "externalConsole": false, "MIMode": "gdb", "setupCommands": [ diff --git a/inc/util.h b/inc/util.h index 5c29b88..65a8489 100644 --- a/inc/util.h +++ b/inc/util.h @@ -8,4 +8,6 @@ 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); + } \ No newline at end of file diff --git a/src/Files.cpp b/src/Files.cpp index 3309599..ac4902d 100644 --- a/src/Files.cpp +++ b/src/Files.cpp @@ -23,12 +23,16 @@ void mrbesen::Files::file(const std::string& path, std::string& out) { } void mrbesen::Files::extention(const std::string& path, std::string& ext) { - size_t pos = path.rfind('.'); - if(pos == std::string::npos || pos+1 == path.size()) { + std::string filestr; + file(path, filestr); + size_t pos = filestr.rfind('.'); + + if(pos == std::string::npos || pos+1 == filestr.size()) { ext = ""; return; } - ext = path.substr(pos+1); + + 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)? diff --git a/src/Util.cpp b/src/Util.cpp index 9ce7598..f6c33d1 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -1,5 +1,7 @@ #include "util.h" +#include + unsigned int mrbesen::Util::count(const std::string& str, char c) { size_t pos = 0; long count = -1; @@ -31,3 +33,11 @@ bool mrbesen::Util::equalsIgnoreCase(const std::string& a, const std::string& b, return std::equal(b.begin(), b.begin()+max, a.begin(), icompare_pred); } } + +void mrbesen::Util::toLower(const std::string& in, std::string& out) { + out = ""; + out.reserve(in.size()); + for(char c : in) { + out += std::tolower(c); + } +} diff --git a/tests/filestests.cpp b/tests/filestests.cpp index 9ebf496..b4b9377 100644 --- a/tests/filestests.cpp +++ b/tests/filestests.cpp @@ -91,5 +91,11 @@ int testFiles_extention() { Files::extention("/a.b", a); ASSERT(a == "b", ""); + Files::extention("/sad.asd/ab", a); + ASSERT(a == "", ""); + + Files::extention("sad.asd/ab", a); + ASSERT(a == "", ""); + return TESTGOOD; } diff --git a/tests/main.cpp b/tests/main.cpp index 71e7484..9e0774a 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -8,8 +8,9 @@ int testFiles_extention(); int testUtil_Count(); int testUtil_equalsIgnoreCase(); +int testUtil_toLower(); -test_t tests[] = {testFiles_parent, testFiles_file, testFiles_extention, testUtil_Count, testUtil_equalsIgnoreCase, NULL}; +test_t tests[] = {testFiles_parent, testFiles_file, testFiles_extention, testUtil_Count, testUtil_equalsIgnoreCase, testUtil_toLower, NULL}; int main(int argc, char** argv) { diff --git a/tests/utilstest.cpp b/tests/utilstest.cpp index d4f341d..43e45a0 100644 --- a/tests/utilstest.cpp +++ b/tests/utilstest.cpp @@ -37,4 +37,26 @@ int testUtil_equalsIgnoreCase() { ASSERT(equalsIgnoreCase(a, c, 5), ""); return TESTGOOD; -} \ No newline at end of file +} + +int testUtil_toLower() { + std::string a = "abc"; + std::string out; + + toLower(a, out); + ASSERT(out == a, ""); + + toLower("", out); + ASSERT(out == "", ""); + + toLower("ABC", out); + ASSERT(out == a, ""); + + toLower("123", out); + ASSERT(out == "123", ""); + + toLower("._:DAd-", out); + ASSERT(out == "._:dad-", ""); + + return TESTGOOD; +}