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 == 'ä') 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();
}

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 = "ÄÁÀÂ";
// Exercise
const std::string out = StringTools::Upper(in);
// Verify
Assert::AreEqual("ÄÁÀÂ", out.c_str());
}
// Tests that uppering uppercase umlautes returns the upper umlautes
TEST_METHOD(Umlautes_upper_a)
{
// Setup
const std::string in = "äáàâ";
// Exercise
const std::string out = StringTools::Upper(in);
// Verify
Assert::AreEqual("ÄÁÀÂ", out.c_str());
}
// Tests that uppering already uppered umlautes returns itself
TEST_METHOD(Umlautes_already_upper_e)
{
// Setup
const std::string in = "ÉÈÊ";
// Exercise
const std::string out = StringTools::Upper(in);
// Verify
Assert::AreEqual("ÉÈÊ", out.c_str());
}
// Tests that uppering uppercase umlautes returns the upper umlautes
TEST_METHOD(Umlautes_upper_e)
{
// Setup
const std::string in = "éèê";
// Exercise
const std::string out = StringTools::Upper(in);
// Verify
Assert::AreEqual("ÉÈÊ", out.c_str());
}
// Tests that uppering already uppered umlautes returns itself
TEST_METHOD(Umlautes_already_upper_u)
{
// Setup
const std::string in = "ÜÚÙÛ";
// Exercise
const std::string out = StringTools::Upper(in);
// Verify
Assert::AreEqual("ÜÚÙÛ", out.c_str());
}
// Tests that uppering uppercase umlautes returns the upper umlautes
TEST_METHOD(Umlautes_upper_u)
{
// Setup
const std::string in = "üúùû";
// Exercise
const std::string out = StringTools::Upper(in);
// Verify
Assert::AreEqual("ÜÚÙÛ", out.c_str());
}
// Tests that uppering already uppered umlautes returns itself
TEST_METHOD(Umlautes_already_upper_o)
{
// Setup
const std::string in = "ÖÓÒÔ";
// Exercise
const std::string out = StringTools::Upper(in);
// Verify
Assert::AreEqual("ÖÓÒÔ", out.c_str());
}
// Tests that uppering uppercase umlautes returns the upper umlautes
TEST_METHOD(Umlautes_upper_o)
{
// Setup
const std::string in = "öóòô";
// Exercise
const std::string out = StringTools::Upper(in);
// Verify
Assert::AreEqual("ÖÓÒÔ", 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 = "Ügh, Àrä Yóü Seriöûs?! DÒN'T DÔ THÄT!!!";
// Exercise
const std::string out = StringTools::Upper(in);
// Verify
Assert::AreEqual("ÜGH, ÀRÄ YÓÜ SERIÖÛS?! DÒN'T DÔ THÄT!!!", out.c_str());
return;
}
};
}