diff options
| author | Leonetienne <leon@etiennes.de> | 2022-05-17 00:03:39 +0200 |
|---|---|---|
| committer | Leonetienne <leon@etiennes.de> | 2022-05-17 00:03:39 +0200 |
| commit | 7d9193e3eec9129298db3ac288432105135e18dd (patch) | |
| tree | 8c3bad33953b73419ade3c4912a9bf4d2de2d70f /StringTools/src | |
| parent | 954629f6bc3b7753c5be0c08e0cdb5caf1056d23 (diff) | |
Moved objects into namespace Leonetienne
Diffstat (limited to 'StringTools/src')
| -rw-r--r-- | StringTools/src/CharTools.cpp | 113 | ||||
| -rw-r--r-- | StringTools/src/StringTools.cpp | 239 |
2 files changed, 181 insertions, 171 deletions
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 <algorithm> -bool CharTools::IsVowel(const char c, const std::string &vowels) { - const char lc = MakeLower(c); +namespace Leonetienne::StringTools { - return std::any_of( - vowels.cbegin(), - vowels.cend(), - [lc](const char vowel) { - return lc == vowel; - } - ); -} + bool CharTools::IsVowel(const char c, const std::string &vowels) { + const char lc = MakeLower(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 std::any_of( + vowels.cbegin(), + vowels.cend(), + [lc](const char vowel) { + return lc == vowel; + } + ); + } - return (lowercase_c >= 'a') && (lowercase_c <= 'z'); -} + 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; -bool CharTools::IsDigit(const char c) { - return (c >= '0') && (c <= '9'); -} + return (lowercase_c >= 'a') && (lowercase_c <= 'z'); + } -bool CharTools::IsUpper(const char c) { - if (!IsLetter(c)) - return false; - else - return !(c & (1<<5)); -} + bool CharTools::IsDigit(const char c) { + return (c >= '0') && (c <= '9'); + } -bool CharTools::IsLower(const char c) { - // Can't just return !IsUpper(c), because it should still return false for digits and symbols... + bool CharTools::IsUpper(const char c) { + if (!IsLetter(c)) + return false; + else + return !(c & (1<<5)); + } - 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... -char CharTools::MakeUpper(char c) { - if (!IsLetter(c)) - return c; - else if (IsUpper(c)) - return c; - else - return c & ~(1<<5); -} + if (!IsLetter(c)) + return false; + 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::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); + } -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 <sstream> -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<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) - toRet.push_back(""); - - // Quick-accept: seperator length is 0 - else if (seperator.length() == 0) { - for (const char c : str) - toRet.push_back(std::string(&c, (&c) + 1)); - } - - else { - std::size_t idx = 0; - while (idx != std::string::npos) { - std::size_t lastIdx = idx; - idx = str.find(seperator, idx); - - // 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 ss.str(); + } + + 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) + toRet.push_back(""); + + // Quick-accept: seperator length is 0 + else if (seperator.length() == 0) { + for (const char c : str) + toRet.push_back(std::string(&c, (&c) + 1)); + } + + else { + std::size_t idx = 0; + while (idx != std::string::npos) { + std::size_t lastIdx = idx; + idx = str.find(seperator, idx); + + // 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; + 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; + for (std::size_t i = str.length(); i < len; i++) + ss << pad; - ss << str; + ss << str; - return ss.str(); -} + return ss.str(); + } -std::string StringTools::PadRight(const std::string& str, const char pad, const std::size_t len) { - std::stringstream ss; + std::string StringTools::PadRight(const std::string& str, const char pad, const std::size_t len) { + std::stringstream ss; + + ss << str; - ss << str; + for (std::size_t i = str.length(); i < len; i++) + ss << pad; - for (std::size_t i = str.length(); i < len; i++) - ss << pad; + return ss.str(); + } - return ss.str(); } + |
