From 30c9a8ec8277c3d2d176bd7bbfdb5828ce10b029 Mon Sep 17 00:00:00 2001 From: altalk23 <45172705+altalk23@users.noreply.github.com> Date: Sun, 18 Feb 2024 17:19:25 +0300 Subject: [PATCH] use from chars when it can --- loader/include/Geode/utils/general.hpp | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/loader/include/Geode/utils/general.hpp b/loader/include/Geode/utils/general.hpp index 06dc5054..e95ffe4f 100644 --- a/loader/include/Geode/utils/general.hpp +++ b/loader/include/Geode/utils/general.hpp @@ -105,8 +105,12 @@ namespace geode { */ template Result numFromString(std::string_view const str, int base = 10) { - if constexpr (std::is_floating_point_v) { - Num val = {}; + if constexpr (std::is_floating_point_v + #if defined(__cpp_lib_to_chars) + && false + #endif + ) { + Num val; char* strEnd; errno = 0; if (std::setlocale(LC_NUMERIC, "en_US.utf8")) { @@ -121,13 +125,15 @@ namespace geode { } else { Num result; - auto [_, ec] = std::from_chars(str.data(), str.data() + str.size(), result, base); - switch (ec) { - case std::errc(): return Ok(result); - case std::errc::invalid_argument: return Err("String is not a number"); - case std::errc::result_out_of_range: return Err("Number is too large to fit"); - default: return Err("Unknown error"); - } + std::from_chars_result res; + if constexpr (std::is_floating_point_v) res = std::from_chars(str.data(), str.data() + str.size(), result); + else res = std::from_chars(str.data(), str.data() + str.size(), result, base); + + auto [_, ec] = res; + if (ec == std::errc()) return Ok(result); + else if (ec == std::errc::invalid_argument) return Err("String is not a number"); + else if (ec == std::errc::result_out_of_range) return Err("Number is too large to fit"); + else return Err("Unknown error"); } }