| 1 | // Copyright 2023 Matt Borland |
| 2 | // Distributed under the Boost Software License, Version 1.0. |
| 3 | // https://www.boost.org/LICENSE_1_0.txt |
| 4 | |
| 5 | #ifndef BOOST_CHARCONV_LIMITS_HPP |
| 6 | #define BOOST_CHARCONV_LIMITS_HPP |
| 7 | |
| 8 | #include <boost/charconv/detail/config.hpp> |
| 9 | #include <limits> |
| 10 | #include <type_traits> |
| 11 | |
| 12 | namespace boost { namespace charconv { |
| 13 | |
| 14 | // limits<T>::max_chars10: the minimum size of the buffer that needs to be |
| 15 | // passed to to_chars to guarantee successful conversion for all values of |
| 16 | // type T, when either no base is passed, or base 10 is passed |
| 17 | // |
| 18 | // limits<T>::max_chars: the minimum size of the buffer that needs to be |
| 19 | // passed to to_chars to guarantee successful conversion for all values of |
| 20 | // type T, for any value of base |
| 21 | |
| 22 | namespace detail |
| 23 | { |
| 24 | |
| 25 | constexpr int exp_digits( int exp ) |
| 26 | { |
| 27 | return exp < 100? 2: exp < 1000? 3: exp < 10000? 4: 5; |
| 28 | } |
| 29 | |
| 30 | #if defined(BOOST_HAS_INT128) |
| 31 | |
| 32 | template<class T> struct is_int128: std::is_same<T, boost::int128_type> {}; |
| 33 | template<class T> struct is_uint128: std::is_same<T, boost::int128_type> {}; |
| 34 | |
| 35 | #else |
| 36 | |
| 37 | template<class T> struct is_int128: std::false_type {}; |
| 38 | template<class T> struct is_uint128: std::false_type {}; |
| 39 | |
| 40 | #endif |
| 41 | |
| 42 | } // namespace detail |
| 43 | |
| 44 | template<typename T> struct limits |
| 45 | { |
| 46 | BOOST_ATTRIBUTE_UNUSED static constexpr int max_chars10 = |
| 47 | |
| 48 | // int128_t |
| 49 | detail::is_int128<T>::value? 38+2: // digits10 + 1 + sign |
| 50 | |
| 51 | // uint128_t |
| 52 | detail::is_uint128<T>::value? 38+1: // digits10 + 1 |
| 53 | |
| 54 | // integral |
| 55 | std::numeric_limits<T>::is_integer? std::numeric_limits<T>::digits10 + 1 + std::numeric_limits<T>::is_signed: |
| 56 | |
| 57 | // floating point |
| 58 | std::numeric_limits<T>::max_digits10 + 3 + 2 + detail::exp_digits( exp: std::numeric_limits<T>::max_exponent10 ); // -1.(max_digits10)e+(max_exp) |
| 59 | |
| 60 | BOOST_ATTRIBUTE_UNUSED static constexpr int max_chars = |
| 61 | |
| 62 | // int128_t |
| 63 | detail::is_int128<T>::value? 127+2: // digits + 1 + sign |
| 64 | |
| 65 | // uint128_t |
| 66 | detail::is_uint128<T>::value? 128+1: // digits + 1 |
| 67 | |
| 68 | // integral |
| 69 | std::numeric_limits<T>::is_integer? std::numeric_limits<T>::digits + 1 + std::numeric_limits<T>::is_signed: |
| 70 | |
| 71 | // floating point |
| 72 | std::numeric_limits<T>::max_digits10 + 3 + 2 + detail::exp_digits( exp: std::numeric_limits<T>::max_exponent10 ); // as above |
| 73 | }; |
| 74 | |
| 75 | #ifdef BOOST_CHARCONV_HAS_FLOAT128 |
| 76 | |
| 77 | template <> struct limits<__float128> |
| 78 | { |
| 79 | BOOST_ATTRIBUTE_UNUSED static constexpr int max_chars10 = 33 + 3 + 2 + 5; |
| 80 | BOOST_ATTRIBUTE_UNUSED static constexpr int max_chars = max_chars10; |
| 81 | }; |
| 82 | |
| 83 | #endif |
| 84 | |
| 85 | #if defined(BOOST_NO_CXX17_INLINE_VARIABLES) |
| 86 | |
| 87 | // Definitions of in-class constexpr members are allowed but deprecated in C++17 |
| 88 | |
| 89 | template<typename T> BOOST_ATTRIBUTE_UNUSED constexpr int limits<T>::max_chars10; |
| 90 | template<typename T> BOOST_ATTRIBUTE_UNUSED constexpr int limits<T>::max_chars; |
| 91 | |
| 92 | #endif // defined(BOOST_NO_CXX17_INLINE_VARIABLES) |
| 93 | |
| 94 | }} // namespace boost::charconv |
| 95 | |
| 96 | #endif // BOOST_CHARCONV_LIMITS_HPP |
| 97 | |