Moved objects into namespace Leonetienne

This commit is contained in:
Leonetienne 2022-05-17 00:03:39 +02:00
parent 954629f6bc
commit 7d9193e3ee
No known key found for this signature in database
GPG Key ID: C33879CD92E9708C
20 changed files with 294 additions and 228 deletions

View File

@ -1,6 +1,8 @@
#include <iostream>
#include <StringTools/StringTools.h>
using namespace Leonetienne::StringTools;
int main()
{
std::vector<std::string> foo =

View File

@ -3,10 +3,12 @@
#include <string>
/* Handy utensils to manipulate characters */
class CharTools {
public:
namespace Leonetienne::StringTools {
/* Handy utensils to manipulate characters */
class CharTools {
public:
//! Checks whether or not `c` is a vowel. You can define custom vowel characters.
static bool IsVowel(const char c, const std::string &vowels = "euioay");
@ -30,6 +32,8 @@ public:
//! Will return `c` with the same capitalization as `sign`.
static char CopySign(char sign, char c);
};
};
}
#endif //STRINGTOOLS_CHARTOOLS_H

View File

@ -4,10 +4,13 @@
#include <string>
#include <vector>
/* Handy utensils to manipulate strings */
class StringTools
{
public:
namespace Leonetienne::StringTools {
/* Handy utensils to manipulate strings */
class StringTools
{
public:
//! Will replace every occurence of `find` in `str` by `subst`.
static std::string Replace(const std::string& str, const char find, const std::string& subst);
@ -35,9 +38,11 @@ public:
//! Will pad a string to the right to length l
static std::string PadRight(const std::string& str, const char pad, const std::size_t len);
private:
private:
// No instanciation! >:(
StringTools();
};
};
}
#endif //STRINGTOOLS_STRINGTOOLS_H

View File

@ -1,7 +1,9 @@
#include "StringTools/CharTools.h"
#include <algorithm>
bool CharTools::IsVowel(const char c, const std::string &vowels) {
namespace Leonetienne::StringTools {
bool CharTools::IsVowel(const char c, const std::string &vowels) {
const char lc = MakeLower(c);
return std::any_of(
@ -11,58 +13,61 @@ bool CharTools::IsVowel(const char c, const std::string &vowels) {
return lc == vowel;
}
);
}
}
bool CharTools::IsLetter(const char c) {
bool CharTools::IsLetter(const char c) {
// Re-implementing IsUpper and MakeLower to prevent stack-overflow by endless recursion
const char lowercase_c = !(c & (1<<5)) ? (c | (1<<5)) : c;
return (lowercase_c >= 'a') && (lowercase_c <= 'z');
}
}
bool CharTools::IsDigit(const char c) {
bool CharTools::IsDigit(const char c) {
return (c >= '0') && (c <= '9');
}
}
bool CharTools::IsUpper(const char c) {
bool CharTools::IsUpper(const char c) {
if (!IsLetter(c))
return false;
else
return !(c & (1<<5));
}
}
bool CharTools::IsLower(const char c) {
bool CharTools::IsLower(const char c) {
// Can't just return !IsUpper(c), because it should still return false for digits and symbols...
if (!IsLetter(c))
return false;
else
return (c & (1<<5));
}
}
char CharTools::MakeUpper(char c) {
char CharTools::MakeUpper(char c) {
if (!IsLetter(c))
return c;
else if (IsUpper(c))
return c;
else
return c & ~(1<<5);
}
}
char CharTools::MakeLower(char c) {
char CharTools::MakeLower(char c) {
if (!IsLetter(c))
return c;
else if (!IsUpper(c))
return c;
else
return c | (1<<5);
}
}
char CharTools::CopySign(char sign, char c) {
char CharTools::CopySign(char sign, char c) {
if ((!IsLetter(c)) || (!IsLetter(sign)))
return c;
if (IsUpper(sign))
return MakeUpper(c);
else
return MakeLower(c);
}
}

View File

@ -1,7 +1,9 @@
#include "StringTools/StringTools.h"
#include <sstream>
std::string StringTools::Replace(const std::string& str, const char find, const std::string& subst) {
namespace Leonetienne::StringTools {
std::string StringTools::Replace(const std::string& str, const char find, const std::string& subst) {
std::stringstream ss;
for (std::size_t i = 0; i < str.length(); i++)
@ -13,9 +15,9 @@ std::string StringTools::Replace(const std::string& str, const char find, const
}
return ss.str();
}
}
std::string StringTools::Replace(const std::string& str, const std::string& find, const std::string& subst) {
std::string StringTools::Replace(const std::string& str, const std::string& find, const std::string& subst) {
if (find.length() == 0)
return str;
@ -41,23 +43,23 @@ std::string StringTools::Replace(const std::string& str, const std::string& find
}
return ss.str();
}
}
std::string StringTools::Replace(const std::string& str, const char find, const char subst) {
std::string StringTools::Replace(const std::string& str, const char find, const char subst) {
std::stringstream ss;
ss << subst;
return Replace(str, find, ss.str());
}
}
std::string StringTools::Replace(const std::string& str, const std::string& find, const char subst) {
std::string StringTools::Replace(const std::string& str, const std::string& find, const char subst) {
std::stringstream ss;
ss << subst;
return Replace(str, find, ss.str());
}
}
std::string StringTools::Lower(const std::string& str) {
std::string StringTools::Lower(const std::string& str) {
std::stringstream ss;
for (std::size_t i = 0; i < str.size(); i++)
@ -73,9 +75,9 @@ std::string StringTools::Lower(const std::string& str) {
}
return ss.str();
}
}
std::string StringTools::Upper(const std::string& str) {
std::string StringTools::Upper(const std::string& str) {
std::stringstream ss;
for (std::size_t i = 0; i < str.size(); i++)
@ -91,9 +93,9 @@ std::string StringTools::Upper(const std::string& str) {
}
return ss.str();
}
}
std::vector<std::string> StringTools::Split(const std::string& str, const std::string& seperator) {
std::vector<std::string> StringTools::Split(const std::string& str, const std::string& seperator) {
std::vector<std::string> toRet;
// Quick-accept: str length is 0
if (str.length() == 0)
@ -130,9 +132,9 @@ std::vector<std::string> StringTools::Split(const std::string& str, const std::s
}
return toRet;
}
}
std::string StringTools::PadLeft(const std::string& str, const char pad, const std::size_t len) {
std::string StringTools::PadLeft(const std::string& str, const char pad, const std::size_t len) {
std::stringstream ss;
for (std::size_t i = str.length(); i < len; i++)
@ -141,9 +143,9 @@ std::string StringTools::PadLeft(const std::string& str, const char pad, const s
ss << str;
return ss.str();
}
}
std::string StringTools::PadRight(const std::string& str, const char pad, const std::size_t len) {
std::string StringTools::PadRight(const std::string& str, const char pad, const std::size_t len) {
std::stringstream ss;
ss << str;
@ -152,4 +154,7 @@ std::string StringTools::PadRight(const std::string& str, const char pad, const
ss << pad;
return ss.str();
}
}

View File

@ -1,6 +1,9 @@
#include <StringTools/CharTools.h>
#include "Catch2.h"
using namespace Leonetienne::StringTools;
TEST_CASE(__FILE__"/JustChars", "[Char][CopySign]")
{
// Setup

View File

@ -1,6 +1,9 @@
#include <StringTools/CharTools.h>
#include "Catch2.h"
using namespace Leonetienne::StringTools;
// Tests character digit-ness by checking it against a map
TEST_CASE(__FILE__"/MapTest", "[Char][IsUpper]")
{

View File

@ -1,6 +1,9 @@
#include <StringTools/CharTools.h>
#include "Catch2.h"
using namespace Leonetienne::StringTools;
// Tests character letter-ness by checking it against a map
TEST_CASE(__FILE__"/MapTest", "[Char][IsLetter]")
{

View File

@ -1,6 +1,9 @@
#include <StringTools/CharTools.h>
#include "Catch2.h"
using namespace Leonetienne::StringTools;
// Tests character sign by checking it against a map
TEST_CASE(__FILE__"/MapTest", "[Char][IsLower]")
{

View File

@ -2,6 +2,9 @@
#include <iostream>
#include "Catch2.h"
using namespace Leonetienne::StringTools;
// Tests character sign by checking it against a map
TEST_CASE(__FILE__"/MapTest", "[Char][IsUpper]")
{

View File

@ -1,6 +1,9 @@
#include <StringTools/CharTools.h>
#include "Catch2.h"
using namespace Leonetienne::StringTools;
// Tests character vowel-ness by checking it against a map
TEST_CASE(__FILE__"/MapTest", "[Char][IsVowel]")
{

View File

@ -1,6 +1,9 @@
#include <StringTools/CharTools.h>
#include "Catch2.h"
using namespace Leonetienne::StringTools;
TEST_CASE(__FILE__"/LowerToUpper_NoSymbols", "[Char][MakeLower]")
{
// Setup

View File

@ -1,6 +1,9 @@
#include <StringTools/CharTools.h>
#include "Catch2.h"
using namespace Leonetienne::StringTools;
TEST_CASE(__FILE__"/LowerToUpper_NoSymbols", "[Char][MakeUpper]")
{
// Setup

View File

@ -1,6 +1,9 @@
#include <StringTools/StringTools.h>
#include "Catch2.h"
using namespace Leonetienne::StringTools;
// Tests that lowering an empty string returns an empty string
TEST_CASE(__FILE__"/EmptyString", "[Strings][Lower]")
{

View File

@ -1,6 +1,9 @@
#include <StringTools/StringTools.h>
#include "Catch2.h"
using namespace Leonetienne::StringTools;
// Tests that padding to a length shorter adds no padding
TEST_CASE(__FILE__"/PadToShorterLength", "[Strings][PadLeft]")
{

View File

@ -1,6 +1,9 @@
#include <StringTools/StringTools.h>
#include "Catch2.h"
using namespace Leonetienne::StringTools;
// Tests that padding to a length shorter adds no padding
TEST_CASE(__FILE__"/PadToShorterLength", "[Strings][PadRight]")
{

View File

@ -1,6 +1,9 @@
#include <StringTools/StringTools.h>
#include "Catch2.h"
using namespace Leonetienne::StringTools;
// Tests that replacing something in an empty string returns an empty string
TEST_CASE(__FILE__"/EmptyString", "[Strings][ReplaceChar]")
{

View File

@ -1,6 +1,9 @@
#include <StringTools/StringTools.h>
#include "Catch2.h"
using namespace Leonetienne::StringTools;
// Tests that replacing something in an empty string returns an empty string
TEST_CASE(__FILE__"/EmptyString", "[Strings][ReplaceString]")
{

View File

@ -1,6 +1,9 @@
#include <StringTools/StringTools.h>
#include "Catch2.h"
using namespace Leonetienne::StringTools;
// Tests that splitting an empty string always returns {""}
TEST_CASE(__FILE__"/EmptyString", "[Strings][Split]")
{

View File

@ -1,6 +1,9 @@
#include <StringTools/StringTools.h>
#include "Catch2.h"
using namespace Leonetienne::StringTools;
// Tests that uppering an empty string returns an empty string
TEST_CASE(__FILE__"/EmptyString", "[Strings][Upper]")
{