diff src/core/strings.cc @ 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 9613d72b097e
children 53211cb1e7f5
line wrap: on
line diff
--- a/src/core/strings.cc	Tue Jan 02 02:37:03 2024 -0500
+++ b/src/core/strings.cc	Sun Jan 07 09:54:17 2024 -0500
@@ -159,33 +159,17 @@
 	return QString::fromWCharArray(wstring.c_str(), wstring.length());
 }
 
-/* not really an "int"... but who cares? */
-int ToInt(const std::string& str, int def) {
-	int tmp = 0;
-	try {
-		tmp = std::stoi(str);
-	} catch (std::invalid_argument const& ex) {
-		qDebug() << "Failed to parse int from std::string: no number found in " << ToQString(str) << " defaulting to " << def;
-		tmp = def;
-	}
-	return tmp;
+std::string ToUtf8String(const bool b) {
+	return b ? "true" : "false"; // lol
 }
 
-bool ToBool(const std::string& s, const bool def) {
-	if (s.length() < 4)
-		return def;
-	const std::string l = Strings::ToLower(s);
-	if (Strings::BeginningMatchesSubstring(l, "true"))
-		return true;
-	else if (Strings::BeginningMatchesSubstring(l, "false"))
-		return false;
+bool ToBool(const std::string& str, bool def) {
+	std::istringstream s(Strings::ToLower(str));
+	s >> std::boolalpha >> def;
 	return def;
 }
 
-std::string ToUtf8String(const bool b) {
-	return b ? "true" : "false";
-}
-
+/* util funcs */
 uint64_t HumanReadableSizeToBytes(const std::string& str) {
 	static const std::unordered_map<std::string, uint64_t> bytes_map = {
 		{"KB", 1ull << 10},