| 1 |
// Copyright John Maddock 2016. |
| 2 |
// Copyright Matt Borland 2023. |
| 3 |
// Use, modification and distribution are subject to the |
| 4 |
// Boost Software License, Version 1.0. (See accompanying file |
| 5 |
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 |
|
| 7 |
#ifndef BOOST_MATH_TOOLS_CONVERT_FROM_STRING_INCLUDED |
| 8 |
#define BOOST_MATH_TOOLS_CONVERT_FROM_STRING_INCLUDED |
| 9 |
|
| 10 |
#ifdef _MSC_VER |
| 11 |
#pragma once |
| 12 |
#endif |
| 13 |
|
| 14 |
#include <boost/math/tools/config.hpp> |
| 15 |
#include <type_traits> |
| 16 |
#ifndef BOOST_MATH_STANDALONE |
| 17 |
|
| 18 |
#if defined(_MSC_VER) || defined(__GNUC__) |
| 19 |
# pragma push_macro( "I" ) |
| 20 |
# undef I |
| 21 |
#endif |
| 22 |
|
| 23 |
#include <boost/lexical_cast.hpp> |
| 24 |
|
| 25 |
#if defined(_MSC_VER) || defined(__GNUC__) |
| 26 |
# pragma pop_macro( "I" ) |
| 27 |
#endif |
| 28 |
|
| 29 |
#endif |
| 30 |
|
| 31 |
namespace boost{ namespace math{ namespace tools{ |
| 32 |
|
| 33 |
template <class T> |
| 34 |
struct convert_from_string_result |
| 35 |
{ |
| 36 |
typedef typename std::conditional<std::is_constructible<T, const char*>::value, const char*, T>::type type; |
| 37 |
}; |
| 38 |
|
| 39 |
template <class Real> |
| 40 |
Real convert_from_string(const char* p, const std::false_type&) |
| 41 |
{ |
| 42 |
#ifdef BOOST_MATH_NO_LEXICAL_CAST |
| 43 |
|
| 44 |
// This function should not compile, we don't have the necessary functionality to support it: |
| 45 |
static_assert(sizeof(Real) == 0, "boost.lexical_cast is not supported in standalone mode."); |
| 46 |
(void)p; // Suppresses -Wunused-parameter |
| 47 |
return Real(0); |
| 48 |
|
| 49 |
#elif defined(BOOST_MATH_USE_CHARCONV_FOR_CONVERSION) |
| 50 |
|
| 51 |
if constexpr (std::is_arithmetic_v<Real>) |
| 52 |
{ |
| 53 |
Real v {}; |
| 54 |
std::from_chars(p, p + std::strlen(p), v); |
| 55 |
|
| 56 |
return v; |
| 57 |
} |
| 58 |
else |
| 59 |
{ |
| 60 |
return boost::lexical_cast<Real>(p); |
| 61 |
} |
| 62 |
|
| 63 |
#else |
| 64 |
|
| 65 |
return boost::lexical_cast<Real>(p); |
| 66 |
|
| 67 |
#endif |
| 68 |
} |
| 69 |
template <class Real> |
| 70 |
constexpr const char* convert_from_string(const char* p, const std::true_type&) noexcept |
| 71 |
{ |
| 72 |
return p; |
| 73 |
} |
| 74 |
template <class Real> |
| 75 |
constexpr typename convert_from_string_result<Real>::type convert_from_string(const char* p) noexcept((std::is_constructible<Real, const char*>::value)) |
| 76 |
{ |
| 77 |
return convert_from_string<Real>(p, std::is_constructible<Real, const char*>()); |
| 78 |
} |
| 79 |
|
| 80 |
} // namespace tools |
| 81 |
} // namespace math |
| 82 |
} // namespace boost |
| 83 |
|
| 84 |
#endif // BOOST_MATH_TOOLS_CONVERT_FROM_STRING_INCLUDED |
| 85 |
|