# HG changeset patch # User Paper # Date 1699497369 18000 # Node ID bc218c9d2ea656bda302237c8697b74aeb84c7a5 # Parent a66c47c1ba3c6e24b78fee8273fd2d8a8c364114 strings: convert ToInt() to be a template ini: conform to new strings.cc changes diff -r a66c47c1ba3c -r bc218c9d2ea6 include/core/ini.h --- a/include/core/ini.h Wed Nov 08 18:13:56 2023 -0500 +++ b/include/core/ini.h Wed Nov 08 21:36:09 2023 -0500 @@ -43,12 +43,9 @@ if constexpr (std::is_same::value) { /* Boolean? */ return Strings::ToBool(val); - } else if constexpr (std::is_unsigned::value) { - /* Unsigned? */ - return Strings::ToUnsignedInt(val); } else { /* Always fall back to long long */ - return Strings::ToInt(val); + return Strings::ToInt(val); } } else { return val; diff -r a66c47c1ba3c -r bc218c9d2ea6 include/core/strings.h --- a/include/core/strings.h Wed Nov 08 18:13:56 2023 -0500 +++ b/include/core/strings.h Wed Nov 08 21:36:09 2023 -0500 @@ -4,6 +4,8 @@ #include #include #include +#include +#include #include class QString; @@ -39,7 +41,24 @@ QString ToQString(const std::wstring& wstring); /* arithmetic :) */ -int ToInt(const std::string& str, int def = 0); +template +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::value) { + return clamp(std::stoll(str), std::numeric_limits::min(), std::numeric_limits::max()); + } else if constexpr (std::is_unsigned::value) { + return clamp(std::stoull(str), std::numeric_limits::max(), std::numeric_limits::max()); + } else { + throw std::invalid_argument("it no worky"); + } + } catch (std::invalid_argument const& ex) { + return def; + } +} bool ToBool(const std::string& s, const bool def = false); std::string ToUtf8String(const bool b);