diff 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
line wrap: on
line diff
--- a/include/core/strings.h	Tue Jan 02 02:37:03 2024 -0500
+++ b/include/core/strings.h	Sun Jan 07 09:54:17 2024 -0500
@@ -3,10 +3,7 @@
 
 #include <string>
 #include <vector>
-#include <array>
-#include <limits>
-#include <stdexcept>
-#include <cstdint>
+#include <sstream>
 
 class QString;
 class QByteArray;
@@ -14,7 +11,8 @@
 namespace Strings {
 
 /* Implode function: takes a vector of strings and turns it
-   into a string, separated by delimiters. */
+ * into a string, separated by delimiters.
+*/
 std::string Implode(const std::vector<std::string>& vector, const std::string& delimiter);
 std::vector<std::string> Split(const std::string &text, const std::string& delimiter);
 
@@ -31,7 +29,10 @@
 std::string ToLower(const std::string& string);
 
 /* functions that make the way we convert from and to
-   different string formats universal */
+ * different string formats universal (and these functions
+ * typically do things the right way so we avoid retarded
+ * code)
+*/
 std::wstring ToWstring(const std::string& string);
 std::wstring ToWstring(const QString& string);
 std::string ToUtf8String(const std::wstring& wstring);
@@ -40,28 +41,26 @@
 QString ToQString(const std::string& string);
 QString ToQString(const std::wstring& wstring);
 
-/* arithmetic :) */
-template<typename T = int>
+/* not really an "int"... but who cares? */
+template<typename T = int,
+         std::enable_if_t<std::is_integral<T>::value, bool> = true>
 T ToInt(const std::string& str, T def = 0) {
-   auto clamp = [](const T& val, const T& min, const T& max){
-      return std::max(min, std::min(val, max));
-   };
-
-   try {
-      if constexpr (std::is_signed<T>::value) {
-         return clamp(std::stoll(str), std::numeric_limits<T>::min(), std::numeric_limits<T>::max());
-      } else if constexpr (std::is_unsigned<T>::value) {
-         return clamp(std::stoull(str), std::numeric_limits<T>::max(), std::numeric_limits<T>::max());
-      } else {
-         throw std::invalid_argument("Invalid input to Strings::ToInt()!");
-      }
-   } catch (std::invalid_argument const& ex) {
-      return def;
-   }
+	std::istringstream s(str);
+	s >> std::noboolalpha >> def;
+	return def;
 }
 
-bool ToBool(const std::string& s, const bool def = false);
-std::string ToUtf8String(const bool b);
+template<typename T,
+         std::enable_if_t<std::is_integral<T>::value && !std::is_same<T, bool>::value, bool> = true>
+std::string ToUtf8String(T i) {
+	std::ostringstream s;
+	s << i;
+	return s.str();
+}
+
+bool ToBool(const std::string& s, bool def);
+
+std::string ToUtf8String(bool b);
 
 uint64_t HumanReadableSizeToBytes(const std::string& str);
 
@@ -72,4 +71,4 @@
 
 }; // namespace Strings
 
-#endif // __core__strings_h
\ No newline at end of file
+#endif // __core__strings_h