Added method Lower()

This commit is contained in:
Leonetienne
2021-12-04 20:18:09 +01:00
parent f6b96b2271
commit d010caa94a
5 changed files with 244 additions and 1 deletions

View File

@@ -59,4 +59,40 @@ std::string StringTools::Replace(const std::string& str, const std::string& find
ss << subst; ss << subst;
return Replace(str, find, ss.str()); return Replace(str, find, ss.str());
} }
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 == '<EFBFBD>') ss << '<EFBFBD>';
else if (c == '<EFBFBD>') ss << '<EFBFBD>';
else if (c == '<EFBFBD>') ss << '<EFBFBD>';
else if (c == '<EFBFBD>') ss << '<EFBFBD>';
else if (c == '<EFBFBD>') ss << '<EFBFBD>';
else if (c == '<EFBFBD>') ss << '<EFBFBD>';
else if (c == '<EFBFBD>') ss << '<EFBFBD>';
else if (c == '<EFBFBD>') ss << '<EFBFBD>';
else if (c == '<EFBFBD>') ss << '<EFBFBD>';
else if (c == '<EFBFBD>') ss << '<EFBFBD>';
else if (c == '<EFBFBD>') ss << '<EFBFBD>';
else if (c == '<EFBFBD>') ss << '<EFBFBD>';
else if (c == '<EFBFBD>') ss << '<EFBFBD>';
else if (c == '<EFBFBD>') ss << '<EFBFBD>';
else if (c == '<EFBFBD>') ss << '<EFBFBD>';
// Else: keep the character as is
else ss << c;
}
return ss.str();
}

View File

@@ -17,6 +17,9 @@ public:
//! Will replace every occurence of `find` in `str` by `subst`. //! 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); 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: private:
// No instanciation! >:( // No instanciation! >:(
StringTools(); StringTools();

200
StringTools/Test/Lower.cpp Normal file
View File

@@ -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 = "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>";
// Exercise
const std::string out = StringTools::Lower(in);
// Verify
Assert::AreEqual("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", out.c_str());
}
// Tests that lowering uppercase umlautes returns the lowered umlautes
TEST_METHOD(Umlautes_upper_a)
{
// Setup
const std::string in = "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>";
// Exercise
const std::string out = StringTools::Lower(in);
// Verify
Assert::AreEqual("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", out.c_str());
}
// Tests that lowering already lowered umlautes returns itself
TEST_METHOD(Umlautes_already_lower_e)
{
// Setup
const std::string in = "<EFBFBD><EFBFBD><EFBFBD>";
// Exercise
const std::string out = StringTools::Lower(in);
// Verify
Assert::AreEqual("<EFBFBD><EFBFBD><EFBFBD>", out.c_str());
}
// Tests that lowering uppercase umlautes returns the lowered umlautes
TEST_METHOD(Umlautes_upper_e)
{
// Setup
const std::string in = "<EFBFBD><EFBFBD><EFBFBD>";
// Exercise
const std::string out = StringTools::Lower(in);
// Verify
Assert::AreEqual("<EFBFBD><EFBFBD><EFBFBD>", out.c_str());
}
// Tests that lowering already lowered umlautes returns itself
TEST_METHOD(Umlautes_already_lower_u)
{
// Setup
const std::string in = "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>";
// Exercise
const std::string out = StringTools::Lower(in);
// Verify
Assert::AreEqual("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", out.c_str());
}
// Tests that lowering uppercase umlautes returns the lowered umlautes
TEST_METHOD(Umlautes_upper_u)
{
// Setup
const std::string in = "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>";
// Exercise
const std::string out = StringTools::Lower(in);
// Verify
Assert::AreEqual("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", out.c_str());
}
// Tests that lowering already lowered umlautes returns itself
TEST_METHOD(Umlautes_already_lower_o)
{
// Setup
const std::string in = "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>";
// Exercise
const std::string out = StringTools::Lower(in);
// Verify
Assert::AreEqual("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", out.c_str());
}
// Tests that lowering uppercase umlautes returns the lowered umlautes
TEST_METHOD(Umlautes_upper_o)
{
// Setup
const std::string in = "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>";
// Exercise
const std::string out = StringTools::Lower(in);
// Verify
Assert::AreEqual("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", 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 = "<EFBFBD>gh, <20>r<EFBFBD> Y<><59> Seri<72><69>s?! D<>N'T D<> TH<54>T!!!";
// Exercise
const std::string out = StringTools::Lower(in);
// Verify
Assert::AreEqual("<EFBFBD>gh, <20>r<EFBFBD> y<><79> seri<72><69>s?! d<>n't d<> th<74>t!!!", out.c_str());
return;
}
};
}

View File

@@ -162,6 +162,7 @@
</ProjectReference> </ProjectReference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="Lower.cpp" />
<ClCompile Include="Replace_Char.cpp" /> <ClCompile Include="Replace_Char.cpp" />
<ClCompile Include="Replace_String.cpp" /> <ClCompile Include="Replace_String.cpp" />
</ItemGroup> </ItemGroup>

View File

@@ -21,5 +21,8 @@
<ClCompile Include="Replace_String.cpp"> <ClCompile Include="Replace_String.cpp">
<Filter>Quelldateien</Filter> <Filter>Quelldateien</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="Lower.cpp">
<Filter>Quelldateien</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
</Project> </Project>