diff --git a/StringTools/exec/main.cpp b/StringTools/exec/main.cpp index 2be6aa4..79fd873 100644 --- a/StringTools/exec/main.cpp +++ b/StringTools/exec/main.cpp @@ -1,6 +1,8 @@ #include #include +using namespace Leonetienne::StringTools; + int main() { std::vector foo = diff --git a/StringTools/include/StringTools/CharTools.h b/StringTools/include/StringTools/CharTools.h index 66c32c0..42b7e61 100644 --- a/StringTools/include/StringTools/CharTools.h +++ b/StringTools/include/StringTools/CharTools.h @@ -3,33 +3,37 @@ #include -/* 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"); +namespace Leonetienne::StringTools { - //! Returns whether or not `c` is a letter. - static bool IsLetter(const char c); + /* 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"); - //! Returns whether or not `c` is a digit. - static bool IsDigit(const char c); + //! Returns whether or not `c` is a letter. + static bool IsLetter(const char c); - //! Checks whether or not `c` is an uppercase character. - static bool IsUpper(const char c); + //! Returns whether or not `c` is a digit. + static bool IsDigit(const char c); - //! Checks whether or not `c` is a lowercase character. - static bool IsLower(const char c); + //! Checks whether or not `c` is an uppercase character. + static bool IsUpper(const char c); - //! Will return `c` as an uppercase character. - static char MakeUpper(char c); + //! Checks whether or not `c` is a lowercase character. + static bool IsLower(const char c); - //! Will return `c` as a lowercase character. - static char MakeLower(char c); + //! Will return `c` as an uppercase character. + static char MakeUpper(char c); - //! Will return `c` with the same capitalization as `sign`. - static char CopySign(char sign, char c); -}; + //! Will return `c` as a lowercase character. + static char MakeLower(char c); + + //! Will return `c` with the same capitalization as `sign`. + static char CopySign(char sign, char c); + }; + +} #endif //STRINGTOOLS_CHARTOOLS_H diff --git a/StringTools/include/StringTools/StringTools.h b/StringTools/include/StringTools/StringTools.h index 818df4c..e984755 100644 --- a/StringTools/include/StringTools/StringTools.h +++ b/StringTools/include/StringTools/StringTools.h @@ -4,40 +4,45 @@ #include #include -/* 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); - //! Will replace every occurence of `find` in `str` by `subst`. - static std::string Replace(const std::string& str, const std::string& find, const std::string& subst); +namespace Leonetienne::StringTools { - //! Will replace every occurence of `find` in `str` by `subst`. - static std::string Replace(const std::string& str, const char find, const char subst); + /* 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); - //! 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 replace every occurence of `find` in `str` by `subst`. + static std::string Replace(const std::string& str, const std::string& find, const std::string& subst); - //! Will make a string all-lowercase. - static std::string Lower(const std::string& str); + //! Will replace every occurence of `find` in `str` by `subst`. + static std::string Replace(const std::string& str, const char find, const char subst); - //! Will make a string all-uppercase. - static std::string Upper(const std::string& str); + //! 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 split a string by a string seperator - static std::vector Split(const std::string& str, const std::string& seperator); + //! Will make a string all-lowercase. + static std::string Lower(const std::string& str); - //! Will pad a string to the left to length l - static std::string PadLeft(const std::string& str, const char pad, const std::size_t len); + //! Will make a string all-uppercase. + static std::string Upper(const std::string& str); - //! 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); + //! Will split a string by a string seperator + static std::vector Split(const std::string& str, const std::string& seperator); -private: - // No instanciation! >:( - StringTools(); -}; + //! Will pad a string to the left to length l + static std::string PadLeft(const std::string& str, const char pad, const std::size_t len); + + //! 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: + // No instanciation! >:( + StringTools(); + }; + +} #endif //STRINGTOOLS_STRINGTOOLS_H diff --git a/StringTools/src/CharTools.cpp b/StringTools/src/CharTools.cpp index 6484da9..a2ff135 100644 --- a/StringTools/src/CharTools.cpp +++ b/StringTools/src/CharTools.cpp @@ -1,68 +1,73 @@ #include "StringTools/CharTools.h" #include -bool CharTools::IsVowel(const char c, const std::string &vowels) { - const char lc = MakeLower(c); +namespace Leonetienne::StringTools { + + bool CharTools::IsVowel(const char c, const std::string &vowels) { + const char lc = MakeLower(c); + + return std::any_of( + vowels.cbegin(), + vowels.cend(), + [lc](const char vowel) { + return lc == vowel; + } + ); + } + + 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) { + return (c >= '0') && (c <= '9'); + } + + bool CharTools::IsUpper(const char c) { + if (!IsLetter(c)) + return false; + else + return !(c & (1<<5)); + } + + 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) { + if (!IsLetter(c)) + return c; + else if (IsUpper(c)) + return c; + else + return c & ~(1<<5); + } + + 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) { + if ((!IsLetter(c)) || (!IsLetter(sign))) + return c; + if (IsUpper(sign)) + return MakeUpper(c); + else + return MakeLower(c); + } - return std::any_of( - vowels.cbegin(), - vowels.cend(), - [lc](const char vowel) { - return lc == vowel; - } - ); } -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) { - return (c >= '0') && (c <= '9'); -} - -bool CharTools::IsUpper(const char c) { - if (!IsLetter(c)) - return false; - else - return !(c & (1<<5)); -} - -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) { - if (!IsLetter(c)) - return c; - else if (IsUpper(c)) - return c; - else - return c & ~(1<<5); -} - -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) { - if ((!IsLetter(c)) || (!IsLetter(sign))) - return c; - if (IsUpper(sign)) - return MakeUpper(c); - else - return MakeLower(c); -} diff --git a/StringTools/src/StringTools.cpp b/StringTools/src/StringTools.cpp index ac33664..8828f72 100644 --- a/StringTools/src/StringTools.cpp +++ b/StringTools/src/StringTools.cpp @@ -1,155 +1,160 @@ #include "StringTools/StringTools.h" #include -std::string StringTools::Replace(const std::string& str, const char find, const std::string& subst) { - std::stringstream ss; +namespace Leonetienne::StringTools { - for (std::size_t i = 0; i < str.length(); i++) - { - if (str[i] != find) - ss << str[i]; - else - ss << subst; - } + std::string StringTools::Replace(const std::string& str, const char find, const std::string& subst) { + std::stringstream ss; - return ss.str(); -} + for (std::size_t i = 0; i < str.length(); i++) + { + if (str[i] != find) + ss << str[i]; + else + ss << subst; + } -std::string StringTools::Replace(const std::string& str, const std::string& find, const std::string& subst) { - if (find.length() == 0) - return str; + return ss.str(); + } - std::stringstream ss; + std::string StringTools::Replace(const std::string& str, const std::string& find, const std::string& subst) { + if (find.length() == 0) + return str; - std::size_t posFound = 0; - std::size_t lastFound = 0; + std::stringstream ss; - while (posFound != std::string::npos) - { - lastFound = posFound; - posFound = str.find(find, posFound); + std::size_t posFound = 0; + std::size_t lastFound = 0; - if (posFound != std::string::npos) - { - ss << str.substr(lastFound, posFound - lastFound) << subst; - posFound += find.length(); - } - else - { - ss << str.substr(lastFound, (str.length()) - lastFound); - } - } + while (posFound != std::string::npos) + { + lastFound = posFound; + posFound = str.find(find, posFound); - return ss.str(); -} + if (posFound != std::string::npos) + { + ss << str.substr(lastFound, posFound - lastFound) << subst; + posFound += find.length(); + } + else + { + ss << str.substr(lastFound, (str.length()) - lastFound); + } + } -std::string StringTools::Replace(const std::string& str, const char find, const char subst) { - std::stringstream ss; - ss << subst; + return ss.str(); + } - return Replace(str, find, ss.str()); -} + std::string StringTools::Replace(const std::string& str, const char find, const char subst) { + std::stringstream ss; + ss << 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()); + } - return Replace(str, find, ss.str()); -} + std::string StringTools::Replace(const std::string& str, const std::string& find, const char subst) { + std::stringstream ss; + ss << subst; -std::string StringTools::Lower(const std::string& str) { - std::stringstream ss; + return Replace(str, find, ss.str()); + } - for (std::size_t i = 0; i < str.size(); i++) - { - const char c = str[i]; + std::string StringTools::Lower(const std::string& str) { + std::stringstream ss; - // Quick-accept: regular letters - if ((c >= 'A') && (c <= 'Z')) - ss << (char)(c | 32); + for (std::size_t i = 0; i < str.size(); i++) + { + const char c = str[i]; - // Else: keep the character as is - else ss << c; - } + // Quick-accept: regular letters + if ((c >= 'A') && (c <= 'Z')) + ss << (char)(c | 32); - return ss.str(); -} + // Else: keep the character as is + else ss << c; + } -std::string StringTools::Upper(const std::string& str) { - std::stringstream ss; + return ss.str(); + } - for (std::size_t i = 0; i < str.size(); i++) - { - const char c = str[i]; + std::string StringTools::Upper(const std::string& str) { + std::stringstream ss; - // Quick-accept: regular letters - if ((c >= 'a') && (c <= 'z')) - ss << (char)(c & ~32); + for (std::size_t i = 0; i < str.size(); i++) + { + const char c = str[i]; - // Else: keep the character as is - else ss << c; - } + // Quick-accept: regular letters + if ((c >= 'a') && (c <= 'z')) + ss << (char)(c & ~32); - return ss.str(); -} + // Else: keep the character as is + else ss << c; + } -std::vector StringTools::Split(const std::string& str, const std::string& seperator) { - std::vector toRet; - // Quick-accept: str length is 0 - if (str.length() == 0) - toRet.push_back(""); + return ss.str(); + } - // Quick-accept: seperator length is 0 - else if (seperator.length() == 0) { - for (const char c : str) - toRet.push_back(std::string(&c, (&c) + 1)); - } + std::vector StringTools::Split(const std::string& str, const std::string& seperator) { + std::vector toRet; + // Quick-accept: str length is 0 + if (str.length() == 0) + toRet.push_back(""); - else { - std::size_t idx = 0; - while (idx != std::string::npos) { - std::size_t lastIdx = idx; - idx = str.find(seperator, idx); + // Quick-accept: seperator length is 0 + else if (seperator.length() == 0) { + for (const char c : str) + toRet.push_back(std::string(&c, (&c) + 1)); + } - // Grab our substring until the next finding of sep - if (idx != std::string::npos) { - toRet.push_back(str.substr( - lastIdx, - idx - lastIdx - )); + else { + std::size_t idx = 0; + while (idx != std::string::npos) { + std::size_t lastIdx = idx; + idx = str.find(seperator, idx); - idx += seperator.length(); - } - // No more seperator found. Grab the rest until the end of the string - else { - toRet.push_back(str.substr( - lastIdx - )); + // Grab our substring until the next finding of sep + if (idx != std::string::npos) { + toRet.push_back(str.substr( + lastIdx, + idx - lastIdx + )); + + idx += seperator.length(); + } + // No more seperator found. Grab the rest until the end of the string + else { + toRet.push_back(str.substr( + lastIdx + )); + } } } - } - return toRet; + return toRet; + } + + 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++) + ss << pad; + + ss << str; + + return ss.str(); + } + + std::string StringTools::PadRight(const std::string& str, const char pad, const std::size_t len) { + std::stringstream ss; + + ss << str; + + for (std::size_t i = str.length(); i < len; i++) + ss << pad; + + return ss.str(); + } + } -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++) - ss << pad; - - ss << str; - - return ss.str(); -} - -std::string StringTools::PadRight(const std::string& str, const char pad, const std::size_t len) { - std::stringstream ss; - - ss << str; - - for (std::size_t i = str.length(); i < len; i++) - ss << pad; - - return ss.str(); -} diff --git a/StringTools/test/Char__CopySign.cpp b/StringTools/test/Char__CopySign.cpp index 177e718..a35bdbd 100644 --- a/StringTools/test/Char__CopySign.cpp +++ b/StringTools/test/Char__CopySign.cpp @@ -1,6 +1,9 @@ #include #include "Catch2.h" +using namespace Leonetienne::StringTools; + + TEST_CASE(__FILE__"/JustChars", "[Char][CopySign]") { // Setup diff --git a/StringTools/test/Char__IsDigit.cpp b/StringTools/test/Char__IsDigit.cpp index baac1ed..643130d 100644 --- a/StringTools/test/Char__IsDigit.cpp +++ b/StringTools/test/Char__IsDigit.cpp @@ -1,6 +1,9 @@ #include #include "Catch2.h" +using namespace Leonetienne::StringTools; + + // Tests character digit-ness by checking it against a map TEST_CASE(__FILE__"/MapTest", "[Char][IsUpper]") { diff --git a/StringTools/test/Char__IsLetter.cpp b/StringTools/test/Char__IsLetter.cpp index bb1a533..c8f8e39 100644 --- a/StringTools/test/Char__IsLetter.cpp +++ b/StringTools/test/Char__IsLetter.cpp @@ -1,6 +1,9 @@ #include #include "Catch2.h" +using namespace Leonetienne::StringTools; + + // Tests character letter-ness by checking it against a map TEST_CASE(__FILE__"/MapTest", "[Char][IsLetter]") { diff --git a/StringTools/test/Char__IsLower.cpp b/StringTools/test/Char__IsLower.cpp index 81660f7..51a1ee0 100644 --- a/StringTools/test/Char__IsLower.cpp +++ b/StringTools/test/Char__IsLower.cpp @@ -1,6 +1,9 @@ #include #include "Catch2.h" +using namespace Leonetienne::StringTools; + + // Tests character sign by checking it against a map TEST_CASE(__FILE__"/MapTest", "[Char][IsLower]") { diff --git a/StringTools/test/Char__IsUpper.cpp b/StringTools/test/Char__IsUpper.cpp index c5ffbbc..2d9540a 100644 --- a/StringTools/test/Char__IsUpper.cpp +++ b/StringTools/test/Char__IsUpper.cpp @@ -2,6 +2,9 @@ #include #include "Catch2.h" +using namespace Leonetienne::StringTools; + + // Tests character sign by checking it against a map TEST_CASE(__FILE__"/MapTest", "[Char][IsUpper]") { diff --git a/StringTools/test/Char__IsVowel.cpp b/StringTools/test/Char__IsVowel.cpp index 4168de2..30ad5e7 100644 --- a/StringTools/test/Char__IsVowel.cpp +++ b/StringTools/test/Char__IsVowel.cpp @@ -1,6 +1,9 @@ #include #include "Catch2.h" +using namespace Leonetienne::StringTools; + + // Tests character vowel-ness by checking it against a map TEST_CASE(__FILE__"/MapTest", "[Char][IsVowel]") { diff --git a/StringTools/test/Char__MakeLower.cpp b/StringTools/test/Char__MakeLower.cpp index 152b56f..3341707 100644 --- a/StringTools/test/Char__MakeLower.cpp +++ b/StringTools/test/Char__MakeLower.cpp @@ -1,6 +1,9 @@ #include #include "Catch2.h" +using namespace Leonetienne::StringTools; + + TEST_CASE(__FILE__"/LowerToUpper_NoSymbols", "[Char][MakeLower]") { // Setup diff --git a/StringTools/test/Char__MakeUpper.cpp b/StringTools/test/Char__MakeUpper.cpp index c4fe2db..8dfb7e6 100644 --- a/StringTools/test/Char__MakeUpper.cpp +++ b/StringTools/test/Char__MakeUpper.cpp @@ -1,6 +1,9 @@ #include #include "Catch2.h" +using namespace Leonetienne::StringTools; + + TEST_CASE(__FILE__"/LowerToUpper_NoSymbols", "[Char][MakeUpper]") { // Setup diff --git a/StringTools/test/String__Lower.cpp b/StringTools/test/String__Lower.cpp index a9e819b..ee9c7c5 100644 --- a/StringTools/test/String__Lower.cpp +++ b/StringTools/test/String__Lower.cpp @@ -1,6 +1,9 @@ #include #include "Catch2.h" +using namespace Leonetienne::StringTools; + + // Tests that lowering an empty string returns an empty string TEST_CASE(__FILE__"/EmptyString", "[Strings][Lower]") { diff --git a/StringTools/test/String__PadLeft.cpp b/StringTools/test/String__PadLeft.cpp index 6297dce..f56784c 100644 --- a/StringTools/test/String__PadLeft.cpp +++ b/StringTools/test/String__PadLeft.cpp @@ -1,6 +1,9 @@ #include #include "Catch2.h" +using namespace Leonetienne::StringTools; + + // Tests that padding to a length shorter adds no padding TEST_CASE(__FILE__"/PadToShorterLength", "[Strings][PadLeft]") { diff --git a/StringTools/test/String__PadRight.cpp b/StringTools/test/String__PadRight.cpp index 60e3ab1..d17d88d 100644 --- a/StringTools/test/String__PadRight.cpp +++ b/StringTools/test/String__PadRight.cpp @@ -1,6 +1,9 @@ #include #include "Catch2.h" +using namespace Leonetienne::StringTools; + + // Tests that padding to a length shorter adds no padding TEST_CASE(__FILE__"/PadToShorterLength", "[Strings][PadRight]") { diff --git a/StringTools/test/String__Replace_Char.cpp b/StringTools/test/String__Replace_Char.cpp index c62e59e..13f99de 100644 --- a/StringTools/test/String__Replace_Char.cpp +++ b/StringTools/test/String__Replace_Char.cpp @@ -1,6 +1,9 @@ #include #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]") { diff --git a/StringTools/test/String__Replace_String.cpp b/StringTools/test/String__Replace_String.cpp index 7b5c038..08af1b8 100644 --- a/StringTools/test/String__Replace_String.cpp +++ b/StringTools/test/String__Replace_String.cpp @@ -1,6 +1,9 @@ #include #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]") { diff --git a/StringTools/test/String__Split.cpp b/StringTools/test/String__Split.cpp index ed1bc82..5342bfb 100644 --- a/StringTools/test/String__Split.cpp +++ b/StringTools/test/String__Split.cpp @@ -1,6 +1,9 @@ #include #include "Catch2.h" +using namespace Leonetienne::StringTools; + + // Tests that splitting an empty string always returns {""} TEST_CASE(__FILE__"/EmptyString", "[Strings][Split]") { diff --git a/StringTools/test/String__Upper.cpp b/StringTools/test/String__Upper.cpp index 3cadceb..b88f117 100644 --- a/StringTools/test/String__Upper.cpp +++ b/StringTools/test/String__Upper.cpp @@ -1,6 +1,9 @@ #include #include "Catch2.h" +using namespace Leonetienne::StringTools; + + // Tests that uppering an empty string returns an empty string TEST_CASE(__FILE__"/EmptyString", "[Strings][Upper]") {