From 55146db7e1bbc7bc8cf9fb67b8973b45a00dde8b Mon Sep 17 00:00:00 2001 From: Martchus Date: Sat, 25 Nov 2023 23:18:30 +0100 Subject: [PATCH] Add workaround for missing FP overload for `std::from_chars()` in libc++ --- chrono/timespan.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/chrono/timespan.cpp b/chrono/timespan.cpp index 72efe57..45a1b24 100644 --- a/chrono/timespan.cpp +++ b/chrono/timespan.cpp @@ -6,6 +6,7 @@ #include "../conversion/stringconversion.h" #include +#include #include #include #include @@ -15,6 +16,32 @@ using namespace std; namespace CppUtilities { +/// \cond +inline std::from_chars_result from_chars(const char *first, const char *last, double &value, + std::chars_format fmt = std::chars_format::general) noexcept +{ +#if _LIBCPP_VERSION + // workaround std::from_chars() not being implemented for floating point numbers in libc++ + CPP_UTILITIES_UNUSED(fmt) + auto r = std::from_chars_result{nullptr, std::errc()}; + auto s = std::string(first, last); + auto l = s.data() + s.size(); + auto d = std::strtod(s.data(), &l); + if (errno == ERANGE) { + r.ec = std::errc::result_out_of_range; + } else if (s.data() == l) { + r.ec = std::errc::invalid_argument; + } else { + value = d; + r.ptr = first + (l - s.data()); + } + return r; +#else + return std::from_chars(first, last, value, fmt); +#endif +} +/// \endcond + /*! * \class TimeSpan * \brief Represents a time interval.