Skip to content

Commit 3e800ce

Browse files
committed
Add generic impl of conversion from string
1 parent 2bc8e5f commit 3e800ce

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

include/boost/decimal/string.hpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,38 @@
1313
namespace boost {
1414
namespace decimal {
1515

16+
namespace detail {
17+
18+
template <typename DecimalType>
19+
auto from_string_impl(const std::string& str, std::size_t* idx = nullptr)
20+
BOOST_DECIMAL_REQUIRES(detail::is_decimal_floating_point_v, DecimalType)
21+
{
22+
DecimalType val;
23+
const auto r {from_chars(str, val)};
24+
25+
if (r.ec == std::errc::result_out_of_range)
26+
{
27+
val = std::numeric_limits<DecimalType>::signaling_NaN();
28+
BOOST_DECIMAL_THROW_EXCEPTION(std::out_of_range("Conversion is outside the range of the type"));
29+
}
30+
else if (r.ec != std::errc{})
31+
{
32+
val = std::numeric_limits<DecimalType>::signaling_NaN();
33+
BOOST_DECIMAL_THROW_EXCEPTION(std::invalid_argument("Conversion could not be performed"));
34+
}
35+
else
36+
{
37+
if (idx != nullptr)
38+
{
39+
*idx = static_cast<std::size_t>(r.ptr - str.data());
40+
}
41+
}
42+
43+
return val;
44+
}
45+
46+
} // namespace detail
47+
1648
BOOST_DECIMAL_EXPORT template <typename DecimalType>
1749
auto to_string(const DecimalType value)
1850
BOOST_DECIMAL_REQUIRES_RETURN(detail::is_decimal_floating_point_v, DecimalType, std::string)

0 commit comments

Comments
 (0)