Skip to content

Commit d46686c

Browse files
committed
Re-architect const char* constructors of remaining types
1 parent 88ef1cb commit d46686c

File tree

4 files changed

+187
-172
lines changed

4 files changed

+187
-172
lines changed

include/boost/decimal/decimal128_t.hpp

Lines changed: 47 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
#include <boost/decimal/detail/to_chars_result.hpp>
4040
#include <boost/decimal/detail/chars_format.hpp>
4141
#include <boost/decimal/detail/components.hpp>
42-
#include <boost/decimal/detail/from_string.hpp>
4342
#include <boost/decimal/detail/construction_sign.hpp>
43+
#include <boost/decimal/detail/from_chars_impl.hpp>
4444

4545
#ifndef BOOST_DECIMAL_BUILD_MODULE
4646

@@ -971,47 +971,6 @@ constexpr decimal128_t::decimal128_t(const Decimal val) noexcept
971971
*this = to_decimal<decimal128_t>(val);
972972
}
973973

974-
#if !defined(BOOST_DECIMAL_DISABLE_CLIB)
975-
976-
constexpr decimal128_t::decimal128_t(const char* str, std::size_t len)
977-
{
978-
if (str == nullptr || len == 0)
979-
{
980-
bits_ = detail::d128_nan_mask;
981-
BOOST_DECIMAL_THROW_EXCEPTION(std::runtime_error("Can not construct from invalid string"));
982-
return; // LCOV_EXCL_LINE
983-
}
984-
985-
// Normally plus signs aren't allowed
986-
auto first {str};
987-
if (*first == '+')
988-
{
989-
++first;
990-
}
991-
992-
decimal128_t v;
993-
const auto r {from_chars(first, str + len, v)};
994-
if (r)
995-
{
996-
*this = v;
997-
}
998-
else
999-
{
1000-
bits_ = detail::d128_nan_mask;
1001-
BOOST_DECIMAL_THROW_EXCEPTION(std::runtime_error("Can not construct from invalid string"));
1002-
}
1003-
}
1004-
1005-
constexpr decimal128_t::decimal128_t(const char* str) : decimal128_t(str, detail::strlen(str)) {}
1006-
1007-
#ifndef BOOST_DECIMAL_HAS_STD_STRING_VIEW
1008-
inline decimal128_t::decimal128_t(const std::string& str) : decimal128_t(str.c_str(), str.size()) {}
1009-
#else
1010-
constexpr decimal128_t::decimal128_t(std::string_view str) : decimal128_t(str.data(), str.size()) {}
1011-
#endif
1012-
1013-
#endif // BOOST_DECIMAL_DISABLE_CLIB
1014-
1015974
constexpr decimal128_t::operator bool() const noexcept
1016975
{
1017976
constexpr decimal128_t zero {0, 0};
@@ -2320,6 +2279,51 @@ class numeric_limits<boost::decimal::decimal128_t> :
23202279

23212280
} //namespace std
23222281

2323-
#include <boost/decimal/charconv.hpp>
2282+
namespace boost {
2283+
namespace decimal {
2284+
2285+
#if !defined(BOOST_DECIMAL_DISABLE_CLIB)
2286+
2287+
constexpr decimal128_t::decimal128_t(const char* str, std::size_t len)
2288+
{
2289+
if (str == nullptr || len == 0)
2290+
{
2291+
bits_ = detail::d128_nan_mask;
2292+
BOOST_DECIMAL_THROW_EXCEPTION(std::runtime_error("Can not construct from invalid string"));
2293+
return; // LCOV_EXCL_LINE
2294+
}
2295+
2296+
// Normally plus signs aren't allowed
2297+
auto first {str};
2298+
if (*first == '+')
2299+
{
2300+
++first;
2301+
}
2302+
2303+
decimal128_t v;
2304+
const auto r {detail::from_chars_general_impl(first, str + len, v, chars_format::general)};
2305+
if (r)
2306+
{
2307+
*this = v;
2308+
}
2309+
else
2310+
{
2311+
bits_ = detail::d128_nan_mask;
2312+
BOOST_DECIMAL_THROW_EXCEPTION(std::runtime_error("Can not construct from invalid string"));
2313+
}
2314+
}
2315+
2316+
constexpr decimal128_t::decimal128_t(const char* str) : decimal128_t(str, detail::strlen(str)) {}
2317+
2318+
#ifndef BOOST_DECIMAL_HAS_STD_STRING_VIEW
2319+
inline decimal128_t::decimal128_t(const std::string& str) : decimal128_t(str.c_str(), str.size()) {}
2320+
#else
2321+
constexpr decimal128_t::decimal128_t(std::string_view str) : decimal128_t(str.data(), str.size()) {}
2322+
#endif
2323+
2324+
#endif // BOOST_DECIMAL_DISABLE_CLIB
2325+
2326+
} // namespace decimal
2327+
} // namespace boost
23242328

23252329
#endif //BOOST_DECIMAL_decimal128_t_HPP

include/boost/decimal/decimal_fast128_t.hpp

Lines changed: 47 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
#include <boost/decimal/detail/to_chars_result.hpp>
4040
#include <boost/decimal/detail/chars_format.hpp>
4141
#include <boost/decimal/detail/components.hpp>
42-
#include <boost/decimal/detail/from_string.hpp>
4342
#include <boost/decimal/detail/construction_sign.hpp>
43+
#include <boost/decimal/detail/from_chars_impl.hpp>
4444

4545
#ifndef BOOST_DECIMAL_BUILD_MODULE
4646

@@ -590,47 +590,6 @@ constexpr auto direct_init_d128(const decimal_fast128_t::significand_type signif
590590
return val;
591591
}
592592

593-
#if !defined(BOOST_DECIMAL_DISABLE_CLIB)
594-
595-
constexpr decimal_fast128_t::decimal_fast128_t(const char* str, const std::size_t len)
596-
{
597-
if (str == nullptr || len == 0)
598-
{
599-
*this = direct_init_d128(detail::d128_fast_qnan, 0, false);
600-
BOOST_DECIMAL_THROW_EXCEPTION(std::runtime_error("Can not construct from invalid string"));
601-
return; // LCOV_EXCL_LINE
602-
}
603-
604-
// Normally plus signs aren't allowed
605-
auto first {str};
606-
if (*first == '+')
607-
{
608-
++first;
609-
}
610-
611-
decimal_fast128_t v;
612-
const auto r {from_chars(first, str + len, v)};
613-
if (r)
614-
{
615-
*this = v;
616-
}
617-
else
618-
{
619-
*this = direct_init_d128(detail::d128_fast_qnan, 0, false);
620-
BOOST_DECIMAL_THROW_EXCEPTION(std::runtime_error("Can not construct from invalid string"));
621-
}
622-
}
623-
624-
constexpr decimal_fast128_t::decimal_fast128_t(const char* str) : decimal_fast128_t(str, detail::strlen(str)) {}
625-
626-
#ifndef BOOST_DECIMAL_HAS_STD_STRING_VIEW
627-
inline decimal_fast128_t::decimal_fast128_t(const std::string& str) : decimal_fast128_t(str.c_str(), str.size()) {}
628-
#else
629-
constexpr decimal_fast128_t::decimal_fast128_t(std::string_view str) : decimal_fast128_t(str.data(), str.size()) {}
630-
#endif
631-
632-
#endif // BOOST_DECIMAL_DISABLE_CLIB
633-
634593
constexpr auto signbit(const decimal_fast128_t& val) noexcept -> bool
635594
{
636595
return val.sign_;
@@ -1682,6 +1641,51 @@ class numeric_limits<boost::decimal::decimal_fast128_t> :
16821641

16831642
} // namespace std
16841643

1685-
#include <boost/decimal/charconv.hpp>
1644+
namespace boost {
1645+
namespace decimal {
1646+
1647+
#if !defined(BOOST_DECIMAL_DISABLE_CLIB)
1648+
1649+
constexpr decimal_fast128_t::decimal_fast128_t(const char* str, const std::size_t len)
1650+
{
1651+
if (str == nullptr || len == 0)
1652+
{
1653+
*this = direct_init_d128(detail::d128_fast_qnan, 0, false);
1654+
BOOST_DECIMAL_THROW_EXCEPTION(std::runtime_error("Can not construct from invalid string"));
1655+
return; // LCOV_EXCL_LINE
1656+
}
1657+
1658+
// Normally plus signs aren't allowed
1659+
auto first {str};
1660+
if (*first == '+')
1661+
{
1662+
++first;
1663+
}
1664+
1665+
decimal_fast128_t v;
1666+
const auto r {detail::from_chars_general_impl(first, str + len, v, chars_format::general)};
1667+
if (r)
1668+
{
1669+
*this = v;
1670+
}
1671+
else
1672+
{
1673+
*this = direct_init_d128(detail::d128_fast_qnan, 0, false);
1674+
BOOST_DECIMAL_THROW_EXCEPTION(std::runtime_error("Can not construct from invalid string"));
1675+
}
1676+
}
1677+
1678+
constexpr decimal_fast128_t::decimal_fast128_t(const char* str) : decimal_fast128_t(str, detail::strlen(str)) {}
1679+
1680+
#ifndef BOOST_DECIMAL_HAS_STD_STRING_VIEW
1681+
inline decimal_fast128_t::decimal_fast128_t(const std::string& str) : decimal_fast128_t(str.c_str(), str.size()) {}
1682+
#else
1683+
constexpr decimal_fast128_t::decimal_fast128_t(std::string_view str) : decimal_fast128_t(str.data(), str.size()) {}
1684+
#endif
1685+
1686+
#endif // BOOST_DECIMAL_DISABLE_CLIB
1687+
1688+
} // namespace decimal
1689+
} // namespace boost
16861690

16871691
#endif //BOOST_DECIMAL_decimal_fast128_t_HPP

include/boost/decimal/decimal_fast32_t.hpp

Lines changed: 46 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
#include <boost/decimal/detail/cmath/next.hpp>
4141
#include <boost/decimal/detail/to_chars_result.hpp>
4242
#include <boost/decimal/detail/chars_format.hpp>
43-
#include <boost/decimal/detail/from_string.hpp>
4443
#include <boost/decimal/detail/construction_sign.hpp>
4544

4645
#ifndef BOOST_DECIMAL_BUILD_MODULE
@@ -576,47 +575,6 @@ constexpr auto direct_init(const detail::decimal_fast32_t_components& x) noexcep
576575
return val;
577576
}
578577

579-
#if !defined(BOOST_DECIMAL_DISABLE_CLIB)
580-
581-
constexpr decimal_fast32_t::decimal_fast32_t(const char* str, const std::size_t len)
582-
{
583-
if (str == nullptr || len == 0)
584-
{
585-
*this = direct_init(detail::d32_fast_qnan, UINT8_C((0)));
586-
BOOST_DECIMAL_THROW_EXCEPTION(std::runtime_error("Can not construct from invalid string"));
587-
return; // LCOV_EXCL_LINE
588-
}
589-
590-
// Normally plus signs aren't allowed
591-
auto first {str};
592-
if (*first == '+')
593-
{
594-
++first;
595-
}
596-
597-
decimal_fast32_t v;
598-
const auto r {from_chars(first, str + len, v)};
599-
if (r)
600-
{
601-
*this = v;
602-
}
603-
else
604-
{
605-
*this = direct_init(detail::d32_fast_qnan, UINT8_C((0)));
606-
BOOST_DECIMAL_THROW_EXCEPTION(std::runtime_error("Can not construct from invalid string"));
607-
}
608-
}
609-
610-
constexpr decimal_fast32_t::decimal_fast32_t(const char* str) : decimal_fast32_t(str, detail::strlen(str)) {}
611-
612-
#ifndef BOOST_DECIMAL_HAS_STD_STRING_VIEW
613-
inline decimal_fast32_t::decimal_fast32_t(const std::string& str) : decimal_fast32_t(str.c_str(), str.size()) {}
614-
#else
615-
constexpr decimal_fast32_t::decimal_fast32_t(std::string_view str) : decimal_fast32_t(str.data(), str.size()) {}
616-
#endif
617-
618-
#endif // BOOST_DECIMAL_DISABLE_CLIB
619-
620578
constexpr auto signbit(const decimal_fast32_t val) noexcept -> bool
621579
{
622580
return val.sign_;
@@ -1646,6 +1604,51 @@ class numeric_limits<boost::decimal::decimal_fast32_t> :
16461604

16471605
} // Namespace std
16481606

1649-
#include <boost/decimal/charconv.hpp>
1607+
namespace boost {
1608+
namespace decimal {
1609+
1610+
#if !defined(BOOST_DECIMAL_DISABLE_CLIB)
1611+
1612+
constexpr decimal_fast32_t::decimal_fast32_t(const char* str, const std::size_t len)
1613+
{
1614+
if (str == nullptr || len == 0)
1615+
{
1616+
*this = direct_init(detail::d32_fast_qnan, UINT8_C((0)));
1617+
BOOST_DECIMAL_THROW_EXCEPTION(std::runtime_error("Can not construct from invalid string"));
1618+
return; // LCOV_EXCL_LINE
1619+
}
1620+
1621+
// Normally plus signs aren't allowed
1622+
auto first {str};
1623+
if (*first == '+')
1624+
{
1625+
++first;
1626+
}
1627+
1628+
decimal_fast32_t v;
1629+
const auto r {detail::from_chars_general_impl(first, str + len, v, chars_format::general)};
1630+
if (r)
1631+
{
1632+
*this = v;
1633+
}
1634+
else
1635+
{
1636+
*this = direct_init(detail::d32_fast_qnan, UINT8_C((0)));
1637+
BOOST_DECIMAL_THROW_EXCEPTION(std::runtime_error("Can not construct from invalid string"));
1638+
}
1639+
}
1640+
1641+
constexpr decimal_fast32_t::decimal_fast32_t(const char* str) : decimal_fast32_t(str, detail::strlen(str)) {}
1642+
1643+
#ifndef BOOST_DECIMAL_HAS_STD_STRING_VIEW
1644+
inline decimal_fast32_t::decimal_fast32_t(const std::string& str) : decimal_fast32_t(str.c_str(), str.size()) {}
1645+
#else
1646+
constexpr decimal_fast32_t::decimal_fast32_t(std::string_view str) : decimal_fast32_t(str.data(), str.size()) {}
1647+
#endif
1648+
1649+
#endif // BOOST_DECIMAL_DISABLE_CLIB
1650+
1651+
} // namespace decimal
1652+
} // namespace boost
16501653

16511654
#endif //BOOST_DECIMAL_decimal_fast32_t_HPP

0 commit comments

Comments
 (0)