From 2864a39c10f6049c83d5dd0bcf27f508a6e8355c Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Thu, 28 Sep 2023 11:23:22 +0200 Subject: [PATCH] Add parsing for floats and doubles --- src/numberparsing.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/numberparsing.h b/src/numberparsing.h index 17205aa..632802e 100644 --- a/src/numberparsing.h +++ b/src/numberparsing.h @@ -94,4 +94,20 @@ template<> inline std::expected fromString(std: return std::unexpected(fmt::format("invalid uint64_t {}", str)); return val; } + +template<> inline std::expected fromString(std::string_view str) +{ + float val; + if (std::sscanf(str.data(), "%f", &val) != 1) + return std::unexpected(fmt::format("invalid float {}", str)); + return val; +} + +template<> inline std::expected fromString(std::string_view str) +{ + double val; + if (std::sscanf(str.data(), "%lf", &val) != 1) + return std::unexpected(fmt::format("invalid double {}", str)); + return val; +} } // namespace cpputils