// Copyright 2023 Matt Borland // Distributed under the Boost Software License, Version 1.0. // https://www.boost.org/LICENSE_1_0.txt #ifndef BOOST_CHARCONV_LIMITS_HPP #define BOOST_CHARCONV_LIMITS_HPP #include #include #include namespace boost { namespace charconv { // limits::max_chars10: the minimum size of the buffer that needs to be // passed to to_chars to guarantee successful conversion for all values of // type T, when either no base is passed, or base 10 is passed // // limits::max_chars: the minimum size of the buffer that needs to be // passed to to_chars to guarantee successful conversion for all values of // type T, for any value of base namespace detail { constexpr int exp_digits( int exp ) { return exp < 100? 2: exp < 1000? 3: exp < 10000? 4: 5; } #if defined(BOOST_HAS_INT128) template struct is_int128: std::is_same {}; template struct is_uint128: std::is_same {}; #else template struct is_int128: std::false_type {}; template struct is_uint128: std::false_type {}; #endif } // namespace detail template struct limits { BOOST_ATTRIBUTE_UNUSED static constexpr int max_chars10 = // int128_t detail::is_int128::value? 38+2: // digits10 + 1 + sign // uint128_t detail::is_uint128::value? 38+1: // digits10 + 1 // integral std::numeric_limits::is_integer? std::numeric_limits::digits10 + 1 + std::numeric_limits::is_signed: // floating point std::numeric_limits::max_digits10 + 3 + 2 + detail::exp_digits( std::numeric_limits::max_exponent10 ); // -1.(max_digits10)e+(max_exp) BOOST_ATTRIBUTE_UNUSED static constexpr int max_chars = // int128_t detail::is_int128::value? 127+2: // digits + 1 + sign // uint128_t detail::is_uint128::value? 128+1: // digits + 1 // integral std::numeric_limits::is_integer? std::numeric_limits::digits + 1 + std::numeric_limits::is_signed: // floating point std::numeric_limits::max_digits10 + 3 + 2 + detail::exp_digits( std::numeric_limits::max_exponent10 ); // as above }; #ifdef BOOST_CHARCONV_HAS_FLOAT128 template <> struct limits<__float128> { BOOST_ATTRIBUTE_UNUSED static constexpr int max_chars10 = 33 + 3 + 2 + 5; BOOST_ATTRIBUTE_UNUSED static constexpr int max_chars = max_chars10; }; #endif #if defined(BOOST_NO_CXX17_INLINE_VARIABLES) // Definitions of in-class constexpr members are allowed but deprecated in C++17 template BOOST_ATTRIBUTE_UNUSED constexpr int limits::max_chars10; template BOOST_ATTRIBUTE_UNUSED constexpr int limits::max_chars; #endif // defined(BOOST_NO_CXX17_INLINE_VARIABLES) }} // namespace boost::charconv #endif // BOOST_CHARCONV_LIMITS_HPP