From a207d4f8a4b19380e1db12ba26f8b9b2cfa21655 Mon Sep 17 00:00:00 2001 From: Brett Date: Mon, 26 Dec 2022 00:34:48 -0500 Subject: [PATCH] Strings --- design.txt | 2 + include/blt/std/string.h | 135 +++++++++++++++++++++++++++++++++++++++ src/blt/std/string.cpp | 6 ++ 3 files changed, 143 insertions(+) create mode 100644 design.txt create mode 100644 include/blt/std/string.h create mode 100644 src/blt/std/string.cpp diff --git a/design.txt b/design.txt new file mode 100644 index 0000000..262b4e6 --- /dev/null +++ b/design.txt @@ -0,0 +1,2 @@ +Utility classes start with captials (IE classes which are purely static) +everything else is camel cased underscore to conform to what most of the C libs seem to use diff --git a/include/blt/std/string.h b/include/blt/std/string.h new file mode 100644 index 0000000..12f40b5 --- /dev/null +++ b/include/blt/std/string.h @@ -0,0 +1,135 @@ +/* + * Created by Brett on 26/12/22. + * Licensed under GNU General Public License V3.0 + * See LICENSE file for license detail + */ + +#ifndef BLT_STRING_H +#define BLT_STRING_H + +#include +#include +#include +#include +#include + +namespace BLT { + class String { + public: + /** + * Converts the string into lower case + * @param s string to lower case + * @return a string copy that is all lower case + */ + static inline std::string toLowerCase(const std::string& s) { + std::stringstream str; + std::for_each( + s.begin(), s.end(), [&str](unsigned char ch) { + str << (char) std::tolower(ch); + } + ); + return str.str(); + } + + /** + * Converts the string into upper case + * @param s string to upper case + * @return a string copy that is all upper case + */ + static inline std::string toUpperCase(const std::string& s) { + std::stringstream str; + std::for_each( + s.begin(), s.end(), [&str](unsigned char ch) { + str << (char) std::toupper(ch); + } + ); + return str.str(); + } + + /** + * @return the standard string of time.now + */ + static inline std::string getTimeString() { + auto t = std::time(nullptr); + auto now = std::localtime(&t); + std::stringstream timeString; + timeString << (1900 + now->tm_year); + timeString << "-"; + timeString << (1 + now->tm_mon); + timeString << "-"; + timeString << now->tm_mday; + timeString << " "; + timeString << now->tm_hour; + timeString << ":"; + timeString << now->tm_min; + timeString << ":"; + timeString << now->tm_sec; + return timeString.str(); + } + + // taken from https://stackoverflow.com/questions/14265581/parse-split-a-string-in-c-using-string-delimiter-standard-c + // extended to return a vector + static inline std::vector split(std::string s, const std::string& delim) { + size_t pos = 0; + std::vector tokens; + while ((pos = s.find(delim)) != std::string::npos) { + auto token = s.substr(0, pos); + tokens.push_back(token); + s.erase(0, pos + delim.length()); + } + tokens.push_back(s); + return tokens; + } + + // taken from https://stackoverflow.com/questions/216823/how-to-trim-an-stdstring + // would've preferred to use boost lib but instructions said to avoid external libs + // trim from start (in place) + static inline std::string& ltrim(std::string& s) { + s.erase( + s.begin(), std::find_if( + s.begin(), s.end(), [](unsigned char ch) { + return !std::isspace(ch); + } + )); + return s; + } + + // trim from end (in place) + static inline std::string& rtrim(std::string& s) { + s.erase( + std::find_if( + s.rbegin(), s.rend(), [](unsigned char ch) { + return !std::isspace(ch); + } + ).base(), s.end()); + return s; + } + + // trim from both ends (in place) + static inline std::string& trim(std::string& s) { + ltrim(s); + rtrim(s); + return s; + } + + // trim from start (copying) + static inline std::string ltrim_copy(std::string s) { + ltrim(s); + return s; + } + + // trim from end (copying) + static inline std::string rtrim_copy(std::string s) { + rtrim(s); + return s; + } + + // trim from both ends (copying) + static inline std::string trim_copy(std::string s) { + trim(s); + return s; + } + }; +} + +#endif //BLT_STRING_H diff --git a/src/blt/std/string.cpp b/src/blt/std/string.cpp new file mode 100644 index 0000000..66d8a99 --- /dev/null +++ b/src/blt/std/string.cpp @@ -0,0 +1,6 @@ +/* + * Created by Brett on 26/12/22. + * Licensed under GNU General Public License V3.0 + * See LICENSE file for license detail + */ +#include \ No newline at end of file