From a0360d8f6dcb1ebe71cedcdfd16b346da28cbf89 Mon Sep 17 00:00:00 2001 From: Matt Borland Date: Tue, 16 May 2023 15:40:01 +0200 Subject: [PATCH] Use charconv in convert_from_string for arithmetic types --- .../boost/math/tools/convert_from_string.hpp | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/include/boost/math/tools/convert_from_string.hpp b/include/boost/math/tools/convert_from_string.hpp index 4a0a25144..f6fe08aee 100644 --- a/include/boost/math/tools/convert_from_string.hpp +++ b/include/boost/math/tools/convert_from_string.hpp @@ -1,4 +1,5 @@ // Copyright John Maddock 2016. +// Copyright Matt Borland 2023. // Use, modification and distribution are subject to the // Boost Software License, Version 1.0. (See accompanying file // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) @@ -10,6 +11,7 @@ #pragma once #endif +#include #include #ifndef BOOST_MATH_STANDALONE #include @@ -26,14 +28,32 @@ namespace boost{ namespace math{ namespace tools{ template Real convert_from_string(const char* p, const std::false_type&) { -#ifdef BOOST_MATH_NO_LEXICAL_CAST + #ifdef BOOST_MATH_NO_LEXICAL_CAST + // This function should not compile, we don't have the necessary functionality to support it: static_assert(sizeof(Real) == 0, "boost.lexical_cast is not supported in standalone mode."); (void)p; // Suppresses -Wunused-parameter return Real(0); -#else + + #elif defined(BOOST_MATH_USE_CHARCONV_FOR_CONVERSION) + + if constexpr (std::is_arithmetic_v) + { + Real v {}; + std::from_chars(p, p + std::strlen(p), v); + + return v; + } + else + { + return boost::lexical_cast(p); + } + + #else + return boost::lexical_cast(p); -#endif + + #endif } template constexpr const char* convert_from_string(const char* p, const std::true_type&) noexcept