Mercurial > minori
comparison include/core/strings.h @ 211:7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
| author | Paper <mrpapersonic@gmail.com> |
|---|---|
| date | Sun, 07 Jan 2024 09:54:17 -0500 |
| parents | bc8d2ccff09c |
| children | c39ad2a8587d |
comparison
equal
deleted
inserted
replaced
| 200:da91af31ae73 | 211:7cf53145de11 |
|---|---|
| 1 #ifndef __core__strings_h | 1 #ifndef __core__strings_h |
| 2 #define __core__strings_h | 2 #define __core__strings_h |
| 3 | 3 |
| 4 #include <string> | 4 #include <string> |
| 5 #include <vector> | 5 #include <vector> |
| 6 #include <array> | 6 #include <sstream> |
| 7 #include <limits> | |
| 8 #include <stdexcept> | |
| 9 #include <cstdint> | |
| 10 | 7 |
| 11 class QString; | 8 class QString; |
| 12 class QByteArray; | 9 class QByteArray; |
| 13 | 10 |
| 14 namespace Strings { | 11 namespace Strings { |
| 15 | 12 |
| 16 /* Implode function: takes a vector of strings and turns it | 13 /* Implode function: takes a vector of strings and turns it |
| 17 into a string, separated by delimiters. */ | 14 * into a string, separated by delimiters. |
| 15 */ | |
| 18 std::string Implode(const std::vector<std::string>& vector, const std::string& delimiter); | 16 std::string Implode(const std::vector<std::string>& vector, const std::string& delimiter); |
| 19 std::vector<std::string> Split(const std::string &text, const std::string& delimiter); | 17 std::vector<std::string> Split(const std::string &text, const std::string& delimiter); |
| 20 | 18 |
| 21 /* Substring removal functions */ | 19 /* Substring removal functions */ |
| 22 std::string ReplaceAll(std::string string, const std::string& find, const std::string& replace); | 20 std::string ReplaceAll(std::string string, const std::string& find, const std::string& replace); |
| 29 | 27 |
| 30 std::string ToUpper(const std::string& string); | 28 std::string ToUpper(const std::string& string); |
| 31 std::string ToLower(const std::string& string); | 29 std::string ToLower(const std::string& string); |
| 32 | 30 |
| 33 /* functions that make the way we convert from and to | 31 /* functions that make the way we convert from and to |
| 34 different string formats universal */ | 32 * different string formats universal (and these functions |
| 33 * typically do things the right way so we avoid retarded | |
| 34 * code) | |
| 35 */ | |
| 35 std::wstring ToWstring(const std::string& string); | 36 std::wstring ToWstring(const std::string& string); |
| 36 std::wstring ToWstring(const QString& string); | 37 std::wstring ToWstring(const QString& string); |
| 37 std::string ToUtf8String(const std::wstring& wstring); | 38 std::string ToUtf8String(const std::wstring& wstring); |
| 38 std::string ToUtf8String(const QString& string); | 39 std::string ToUtf8String(const QString& string); |
| 39 std::string ToUtf8String(const QByteArray& ba); | 40 std::string ToUtf8String(const QByteArray& ba); |
| 40 QString ToQString(const std::string& string); | 41 QString ToQString(const std::string& string); |
| 41 QString ToQString(const std::wstring& wstring); | 42 QString ToQString(const std::wstring& wstring); |
| 42 | 43 |
| 43 /* arithmetic :) */ | 44 /* not really an "int"... but who cares? */ |
| 44 template<typename T = int> | 45 template<typename T = int, |
| 46 std::enable_if_t<std::is_integral<T>::value, bool> = true> | |
| 45 T ToInt(const std::string& str, T def = 0) { | 47 T ToInt(const std::string& str, T def = 0) { |
| 46 auto clamp = [](const T& val, const T& min, const T& max){ | 48 std::istringstream s(str); |
| 47 return std::max(min, std::min(val, max)); | 49 s >> std::noboolalpha >> def; |
| 48 }; | 50 return def; |
| 49 | |
| 50 try { | |
| 51 if constexpr (std::is_signed<T>::value) { | |
| 52 return clamp(std::stoll(str), std::numeric_limits<T>::min(), std::numeric_limits<T>::max()); | |
| 53 } else if constexpr (std::is_unsigned<T>::value) { | |
| 54 return clamp(std::stoull(str), std::numeric_limits<T>::max(), std::numeric_limits<T>::max()); | |
| 55 } else { | |
| 56 throw std::invalid_argument("Invalid input to Strings::ToInt()!"); | |
| 57 } | |
| 58 } catch (std::invalid_argument const& ex) { | |
| 59 return def; | |
| 60 } | |
| 61 } | 51 } |
| 62 | 52 |
| 63 bool ToBool(const std::string& s, const bool def = false); | 53 template<typename T, |
| 64 std::string ToUtf8String(const bool b); | 54 std::enable_if_t<std::is_integral<T>::value && !std::is_same<T, bool>::value, bool> = true> |
| 55 std::string ToUtf8String(T i) { | |
| 56 std::ostringstream s; | |
| 57 s << i; | |
| 58 return s.str(); | |
| 59 } | |
| 60 | |
| 61 bool ToBool(const std::string& s, bool def); | |
| 62 | |
| 63 std::string ToUtf8String(bool b); | |
| 65 | 64 |
| 66 uint64_t HumanReadableSizeToBytes(const std::string& str); | 65 uint64_t HumanReadableSizeToBytes(const std::string& str); |
| 67 | 66 |
| 68 std::string RemoveLeadingChars(std::string s, const char c); | 67 std::string RemoveLeadingChars(std::string s, const char c); |
| 69 std::string RemoveTrailingChars(std::string s, const char c); | 68 std::string RemoveTrailingChars(std::string s, const char c); |
