fix numFromString on floats

This commit is contained in:
HJfod 2024-02-18 00:13:34 +02:00
parent 453fac9653
commit 8cccb4ce5c

View file

@ -105,7 +105,13 @@ namespace geode {
template <class Num>
Result<Num> numFromString(std::string_view const str, int base = 10) {
Num result;
auto [_, ec] = std::from_chars(str.data(), str.data() + str.size(), result, base);
std::errc ec;
if constexpr (std::is_floating_point_v<Num>) {
ec = std::from_chars(str.data(), str.data() + str.size(), result).ec;
}
else {
ec = std::from_chars(str.data(), str.data() + str.size(), result, base).ec;
}
switch (ec) {
case std::errc(): return Ok(result);
case std::errc::invalid_argument: return Err("String is not a number");