From fabab69e9e864053dfd75acf2ab4ba7420970d05 Mon Sep 17 00:00:00 2001 From: MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> Date: Fri, 27 Mar 2026 14:28:53 +0100 Subject: [PATCH] refactor: Return std::optional from ParseDouble --- src/univalue/lib/univalue_get.cpp | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/univalue/lib/univalue_get.cpp b/src/univalue/lib/univalue_get.cpp index 146cc421ef6..e82ac4cbaed 100644 --- a/src/univalue/lib/univalue_get.cpp +++ b/src/univalue/lib/univalue_get.cpp @@ -7,6 +7,7 @@ #include #include +#include #include #include #include @@ -25,18 +26,20 @@ static bool ParsePrechecks(const std::string& str) return true; } -bool ParseDouble(const std::string& str, double *out) +std::optional ParseDouble(const std::string& str) { if (!ParsePrechecks(str)) - return false; + return std::nullopt; if (str.size() >= 2 && str[0] == '0' && str[1] == 'x') // No hexadecimal floats allowed - return false; + return std::nullopt; std::istringstream text(str); text.imbue(std::locale::classic()); double result; text >> result; - if(out) *out = result; - return text.eof() && !text.fail(); + if (!text.eof() || text.fail()) { + return std::nullopt; + } + return result; } } @@ -68,10 +71,10 @@ const std::string& UniValue::get_str() const double UniValue::get_real() const { checkType(VNUM); - double retval; - if (!ParseDouble(getValStr(), &retval)) - throw std::runtime_error("JSON double out of range"); - return retval; + if (const auto retval{ParseDouble(getValStr())}) { + return *retval; + } + throw std::runtime_error("JSON double out of range"); } const UniValue& UniValue::get_obj() const