From d010caa94a94d11b8dd2ec88e0887446fd26b95e Mon Sep 17 00:00:00 2001 From: Leonetienne Date: Sat, 4 Dec 2021 20:18:09 +0100 Subject: [PATCH] Added method Lower() --- StringTools/StringTools/StringTools.cpp | 38 ++++- StringTools/StringTools/StringTools.h | 3 + StringTools/Test/Lower.cpp | 200 ++++++++++++++++++++++++ StringTools/Test/Test.vcxproj | 1 + StringTools/Test/Test.vcxproj.filters | 3 + 5 files changed, 244 insertions(+), 1 deletion(-) create mode 100644 StringTools/Test/Lower.cpp diff --git a/StringTools/StringTools/StringTools.cpp b/StringTools/StringTools/StringTools.cpp index 728506a..7e26c5c 100644 --- a/StringTools/StringTools/StringTools.cpp +++ b/StringTools/StringTools/StringTools.cpp @@ -59,4 +59,40 @@ std::string StringTools::Replace(const std::string& str, const std::string& find ss << subst; return Replace(str, find, ss.str()); -} \ No newline at end of file +} + +std::string StringTools::Lower(const std::string& str) +{ + std::stringstream ss; + + for (std::size_t i = 0; i < str.size(); i++) + { + const char c = str[i]; + + // Quick-accept: regular letters + if ((c >= 'A') && (c <= 'Z')) + ss << (char)(c + 32); + + // Damned umlautes: + else if (c == 'Ä') ss << 'ä'; + else if (c == 'Á') ss << 'á'; + else if (c == 'À') ss << 'à'; + else if (c == 'Â') ss << 'â'; + else if (c == 'É') ss << 'é'; + else if (c == 'È') ss << 'è'; + else if (c == 'Ê') ss << 'ê'; + else if (c == 'Ü') ss << 'ü'; + else if (c == 'Ú') ss << 'ú'; + else if (c == 'Ù') ss << 'ù'; + else if (c == 'Û') ss << 'û'; + else if (c == 'Ö') ss << 'ö'; + else if (c == 'Ó') ss << 'ó'; + else if (c == 'Ò') ss << 'ò'; + else if (c == 'Ô') ss << 'ô'; + + // Else: keep the character as is + else ss << c; + } + + return ss.str(); +} diff --git a/StringTools/StringTools/StringTools.h b/StringTools/StringTools/StringTools.h index 730fa3e..39e512f 100644 --- a/StringTools/StringTools/StringTools.h +++ b/StringTools/StringTools/StringTools.h @@ -17,6 +17,9 @@ public: //! Will replace every occurence of `find` in `str` by `subst`. static std::string Replace(const std::string& str, const std::string& find, const char subst); + //! Will make a string all-lowercase. Only works with latin and german umlautes, plus some extras. + static std::string Lower(const std::string& str); + private: // No instanciation! >:( StringTools(); diff --git a/StringTools/Test/Lower.cpp b/StringTools/Test/Lower.cpp new file mode 100644 index 0000000..c45af68 --- /dev/null +++ b/StringTools/Test/Lower.cpp @@ -0,0 +1,200 @@ +#include "CppUnitTest.h" +#include "../StringTools/StringTools.h" + +using namespace Microsoft::VisualStudio::CppUnitTestFramework; + +namespace _StringTools +{ + TEST_CLASS(_Lower) + { + public: + + // Tests that lowering an empty string returns an empty string + TEST_METHOD(EmptyString) + { + // Setup + const std::string in = ""; + + // Exercise + const std::string out = StringTools::Lower(in); + + // Verify + Assert::AreEqual("", out.c_str()); + return; + } + + // Tests that lowering a string without any letters returns an itself + TEST_METHOD(Symbols) + { + // Setup + const std::string in = "66! _-\n*"; + + // Exercise + const std::string out = StringTools::Lower(in); + + // Verify + Assert::AreEqual("66! _-\n*", out.c_str()); + return; + } + + // Tests that lowering a string of lowercase letters returns itself + TEST_METHOD(AlreadyLowered) + { + // Setup + const std::string in = "ughareyouserious"; + + // Exercise + const std::string out = StringTools::Lower(in); + + // Verify + Assert::AreEqual("ughareyouserious", out.c_str()); + return; + } + + // Tests that lowering a string of uppercase letters returns the lowercase version + TEST_METHOD(Uppercase) + { + // Setup + const std::string in = "UGHAREYOUSERIOUS"; + + // Exercise + const std::string out = StringTools::Lower(in); + + // Verify + Assert::AreEqual("ughareyouserious", out.c_str()); + return; + } + + // Tests that lowering a string of uppercase, lowercase letters and symbols returns the lowercase version + TEST_METHOD(Mixed) + { + // Setup + const std::string in = "Ugh, Are You Serious?! DON'T DO THAT!!!"; + + // Exercise + const std::string out = StringTools::Lower(in); + + // Verify + Assert::AreEqual("ugh, are you serious?! don't do that!!!", out.c_str()); + return; + } + + // Tests that lowering already lowered umlautes returns itself + TEST_METHOD(Umlautes_already_lower_a) + { + // Setup + const std::string in = "äáàâ"; + + // Exercise + const std::string out = StringTools::Lower(in); + + // Verify + Assert::AreEqual("äáàâ", out.c_str()); + } + + // Tests that lowering uppercase umlautes returns the lowered umlautes + TEST_METHOD(Umlautes_upper_a) + { + // Setup + const std::string in = "ÄÁÀÂ"; + + // Exercise + const std::string out = StringTools::Lower(in); + + // Verify + Assert::AreEqual("äáàâ", out.c_str()); + } + + // Tests that lowering already lowered umlautes returns itself + TEST_METHOD(Umlautes_already_lower_e) + { + // Setup + const std::string in = "éèê"; + + // Exercise + const std::string out = StringTools::Lower(in); + + // Verify + Assert::AreEqual("éèê", out.c_str()); + } + + // Tests that lowering uppercase umlautes returns the lowered umlautes + TEST_METHOD(Umlautes_upper_e) + { + // Setup + const std::string in = "ÉÈÊ"; + + // Exercise + const std::string out = StringTools::Lower(in); + + // Verify + Assert::AreEqual("éèê", out.c_str()); + } + + // Tests that lowering already lowered umlautes returns itself + TEST_METHOD(Umlautes_already_lower_u) + { + // Setup + const std::string in = "üúùû"; + + // Exercise + const std::string out = StringTools::Lower(in); + + // Verify + Assert::AreEqual("üúùû", out.c_str()); + } + + // Tests that lowering uppercase umlautes returns the lowered umlautes + TEST_METHOD(Umlautes_upper_u) + { + // Setup + const std::string in = "ÜÚÙÛ"; + + // Exercise + const std::string out = StringTools::Lower(in); + + // Verify + Assert::AreEqual("üúùû", out.c_str()); + } + + // Tests that lowering already lowered umlautes returns itself + TEST_METHOD(Umlautes_already_lower_o) + { + // Setup + const std::string in = "öóòô"; + + // Exercise + const std::string out = StringTools::Lower(in); + + // Verify + Assert::AreEqual("öóòô", out.c_str()); + } + + // Tests that lowering uppercase umlautes returns the lowered umlautes + TEST_METHOD(Umlautes_upper_o) + { + // Setup + const std::string in = "ÖÓÒÔ"; + + // Exercise + const std::string out = StringTools::Lower(in); + + // Verify + Assert::AreEqual("öóòô", out.c_str()); + } + + // Tests that lowering a string of uppercase, lowercase letters and symbols returns the lowercase version, even with umlauts + TEST_METHOD(Mixed_with_umlautes) + { + // Setup + const std::string in = "Ügh, Àrä Yóü Seriöûs?! DÓN'T DÒ THÄT!!!"; + + // Exercise + const std::string out = StringTools::Lower(in); + + // Verify + Assert::AreEqual("ügh, àrä yóü seriöûs?! dón't dò thät!!!", out.c_str()); + return; + } + }; +} diff --git a/StringTools/Test/Test.vcxproj b/StringTools/Test/Test.vcxproj index 51fba2c..59e804a 100644 --- a/StringTools/Test/Test.vcxproj +++ b/StringTools/Test/Test.vcxproj @@ -162,6 +162,7 @@ + diff --git a/StringTools/Test/Test.vcxproj.filters b/StringTools/Test/Test.vcxproj.filters index e159865..c10488e 100644 --- a/StringTools/Test/Test.vcxproj.filters +++ b/StringTools/Test/Test.vcxproj.filters @@ -21,5 +21,8 @@ Quelldateien + + Quelldateien + \ No newline at end of file