Added method Upper()

This commit is contained in:
Leonetienne
2021-12-04 20:29:17 +01:00
parent d010caa94a
commit 01c5cd646e
5 changed files with 243 additions and 0 deletions

View File

@@ -96,3 +96,39 @@ std::string StringTools::Lower(const std::string& str)
return ss.str();
}
std::string StringTools::Upper(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

@@ -20,6 +20,9 @@ public:
//! Will make a string all-lowercase. Only works with latin and german umlautes, plus some extras.
static std::string Lower(const std::string& str);
//! Will make a string all-uppercase. Only works with latin and german umlautes, plus some extras.
static std::string Upper(const std::string& str);
private:
// No instanciation! >:(
StringTools();

View File

@@ -165,6 +165,7 @@
<ClCompile Include="Lower.cpp" />
<ClCompile Include="Replace_Char.cpp" />
<ClCompile Include="Replace_String.cpp" />
<ClCompile Include="Upper.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">

View File

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

200
StringTools/Test/Upper.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(_Upper)
{
public:
// Tests that uppering an empty string returns an empty string
TEST_METHOD(EmptyString)
{
// Setup
const std::string in = "";
// Exercise
const std::string out = StringTools::Upper(in);
// Verify
Assert::AreEqual("", out.c_str());
return;
}
// Tests that uppering a string without any letters returns an itself
TEST_METHOD(Symbols)
{
// Setup
const std::string in = "66! _-\n*";
// Exercise
const std::string out = StringTools::Upper(in);
// Verify
Assert::AreEqual("66! _-\n*", out.c_str());
return;
}
// Tests that uppering a string of uppercase letters returns itself
TEST_METHOD(AlreadyUppered)
{
// Setup
const std::string in = "UGHAREYOUSERIOUS";
// Exercise
const std::string out = StringTools::Upper(in);
// Verify
Assert::AreEqual("UGHAREYOUSERIOUS", out.c_str());
return;
}
// Tests that uppering a string of lowercase letters returns the uppercase version
TEST_METHOD(Lowercase)
{
// Setup
const std::string in = "ughareyouserious";
// Exercise
const std::string out = StringTools::Upper(in);
// Verify
Assert::AreEqual("UGHAREYOUSERIOUS", out.c_str());
return;
}
// Tests that uppering a string of uppercase, lowercase letters and symbols returns the uppercase version
TEST_METHOD(Mixed)
{
// Setup
const std::string in = "Ugh, Are You Serious?! DON'T do that!!!";
// Exercise
const std::string out = StringTools::Upper(in);
// Verify
Assert::AreEqual("UGH, ARE YOU SERIOUS?! DON'T DO THAT!!!", out.c_str());
return;
}
// Tests that uppering already uppered umlautes returns itself
TEST_METHOD(Umlautes_already_upper_a)
{
// Setup
const std::string in = "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>";
// Exercise
const std::string out = StringTools::Upper(in);
// Verify
Assert::AreEqual("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", out.c_str());
}
// Tests that uppering uppercase umlautes returns the upper umlautes
TEST_METHOD(Umlautes_upper_a)
{
// Setup
const std::string in = "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>";
// Exercise
const std::string out = StringTools::Upper(in);
// Verify
Assert::AreEqual("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", out.c_str());
}
// Tests that uppering already uppered umlautes returns itself
TEST_METHOD(Umlautes_already_upper_e)
{
// Setup
const std::string in = "<EFBFBD><EFBFBD><EFBFBD>";
// Exercise
const std::string out = StringTools::Upper(in);
// Verify
Assert::AreEqual("<EFBFBD><EFBFBD><EFBFBD>", out.c_str());
}
// Tests that uppering uppercase umlautes returns the upper umlautes
TEST_METHOD(Umlautes_upper_e)
{
// Setup
const std::string in = "<EFBFBD><EFBFBD><EFBFBD>";
// Exercise
const std::string out = StringTools::Upper(in);
// Verify
Assert::AreEqual("<EFBFBD><EFBFBD><EFBFBD>", out.c_str());
}
// Tests that uppering already uppered umlautes returns itself
TEST_METHOD(Umlautes_already_upper_u)
{
// Setup
const std::string in = "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>";
// Exercise
const std::string out = StringTools::Upper(in);
// Verify
Assert::AreEqual("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", out.c_str());
}
// Tests that uppering uppercase umlautes returns the upper umlautes
TEST_METHOD(Umlautes_upper_u)
{
// Setup
const std::string in = "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>";
// Exercise
const std::string out = StringTools::Upper(in);
// Verify
Assert::AreEqual("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", out.c_str());
}
// Tests that uppering already uppered umlautes returns itself
TEST_METHOD(Umlautes_already_upper_o)
{
// Setup
const std::string in = "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>";
// Exercise
const std::string out = StringTools::Upper(in);
// Verify
Assert::AreEqual("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", out.c_str());
}
// Tests that uppering uppercase umlautes returns the upper umlautes
TEST_METHOD(Umlautes_upper_o)
{
// Setup
const std::string in = "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>";
// Exercise
const std::string out = StringTools::Upper(in);
// Verify
Assert::AreEqual("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", out.c_str());
}
// Tests that uppering 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::Upper(in);
// Verify
Assert::AreEqual("<EFBFBD>GH, <20>R<EFBFBD> Y<><59> SERI<52><49>S?! D<>N'T D<> TH<54>T!!!", out.c_str());
return;
}
};
}