From a5921d1ca964971ada83be2c7c65bb84504fe179 Mon Sep 17 00:00:00 2001 From: Mohamed Gaber Date: Fri, 3 Apr 2026 20:21:57 +0200 Subject: [PATCH] util/StringUtil.cc: feature-test std::from_chars (#415) Support for std::{from,to}_chars isn't finalized in libcxx as of the time of writing, see __cpp_lib_to_chars in https://github.com/llvm/llvm-project/blob/6331bfa41ab529558ec9d645c0effb7a4146591c/libcxx/docs/FeatureTestMacroTable.rst This patch adds a fallback using strtof. There are two differences: * strtof is locale-dependent * strtof tolerates leading spaces Signed-off-by: Mohamed Gaber --- util/StringUtil.cc | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/util/StringUtil.cc b/util/StringUtil.cc index 00a36ec4..d1f7fae6 100644 --- a/util/StringUtil.cc +++ b/util/StringUtil.cc @@ -28,6 +28,7 @@ #include #include #include +#include namespace sta { @@ -56,12 +57,21 @@ std::pair stringFloat(const std::string &str) { float value; +#if defined(__cpp_lib_to_chars) && __cpp_lib_to_chars >= 201611L auto [ptr, ec] = std::from_chars(str.data(), str.data() + str.size(), value); if (ec == std::errc() && *ptr == '\0') return {value, true}; else return {0.0, false}; +#else + char *ptr; + value = strtof(str.data(), &ptr); + if (!errno || *ptr != '\0') + return {0.0, false}; + else + return {value, true}; +#endif } void