From 4316d25292dbd148dce9b378c02b7c867d276ebd Mon Sep 17 00:00:00 2001 From: mrbesen Date: Tue, 29 Sep 2020 14:12:41 +0200 Subject: [PATCH] inplace tolower --- inc/util.h | 4 +++- src/Util.cpp | 13 +++++++++++++ tests/utilstest.cpp | 8 ++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/inc/util.h b/inc/util.h index 65a8489..da2aa52 100644 --- a/inc/util.h +++ b/inc/util.h @@ -8,6 +8,8 @@ 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); +void toLower(const std::string& in, std::string& out); //copy toLower + +void toLower(std::string& in); //inplace toLower } \ No newline at end of file diff --git a/src/Util.cpp b/src/Util.cpp index f6c33d1..d759bf1 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -35,9 +35,22 @@ bool mrbesen::Util::equalsIgnoreCase(const std::string& a, const std::string& b, } void mrbesen::Util::toLower(const std::string& in, std::string& out) { + + if(&in == &out) { + //wenn beide strings identisch sind, inplace funktion verwenden; + toLower(out); + return; + } + out = ""; out.reserve(in.size()); for(char c : in) { out += std::tolower(c); } } + +void mrbesen::Util::toLower(std::string& in) { + for(size_t p = 0; p < in.size(); p++) { + in[p] = std::tolower(in[p]); + } +} diff --git a/tests/utilstest.cpp b/tests/utilstest.cpp index 43e45a0..8712d9e 100644 --- a/tests/utilstest.cpp +++ b/tests/utilstest.cpp @@ -58,5 +58,13 @@ int testUtil_toLower() { toLower("._:DAd-", out); ASSERT(out == "._:dad-", ""); + //inplace testen + toLower(a, a); + ASSERT(a == "abc", ""); + + a = "ABC"; + toLower(a); + ASSERT(a == "abc", ""); + return TESTGOOD; }