limits.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. #ifndef BOOST_CHARCONV_LIMITS_HPP
  5. #define BOOST_CHARCONV_LIMITS_HPP
  6. #include <boost/charconv/detail/config.hpp>
  7. #include <limits>
  8. #include <type_traits>
  9. namespace boost { namespace charconv {
  10. // limits<T>::max_chars10: the minimum size of the buffer that needs to be
  11. // passed to to_chars to guarantee successful conversion for all values of
  12. // type T, when either no base is passed, or base 10 is passed
  13. //
  14. // limits<T>::max_chars: 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, for any value of base
  17. namespace detail
  18. {
  19. constexpr int exp_digits( int exp )
  20. {
  21. return exp < 100? 2: exp < 1000? 3: exp < 10000? 4: 5;
  22. }
  23. #if defined(BOOST_HAS_INT128)
  24. template<class T> struct is_int128: std::is_same<T, boost::int128_type> {};
  25. template<class T> struct is_uint128: std::is_same<T, boost::int128_type> {};
  26. #else
  27. template<class T> struct is_int128: std::false_type {};
  28. template<class T> struct is_uint128: std::false_type {};
  29. #endif
  30. } // namespace detail
  31. template<typename T> struct limits
  32. {
  33. BOOST_ATTRIBUTE_UNUSED static constexpr int max_chars10 =
  34. // int128_t
  35. detail::is_int128<T>::value? 38+2: // digits10 + 1 + sign
  36. // uint128_t
  37. detail::is_uint128<T>::value? 38+1: // digits10 + 1
  38. // integral
  39. std::numeric_limits<T>::is_integer? std::numeric_limits<T>::digits10 + 1 + std::numeric_limits<T>::is_signed:
  40. // floating point
  41. std::numeric_limits<T>::max_digits10 + 3 + 2 + detail::exp_digits( std::numeric_limits<T>::max_exponent10 ); // -1.(max_digits10)e+(max_exp)
  42. BOOST_ATTRIBUTE_UNUSED static constexpr int max_chars =
  43. // int128_t
  44. detail::is_int128<T>::value? 127+2: // digits + 1 + sign
  45. // uint128_t
  46. detail::is_uint128<T>::value? 128+1: // digits + 1
  47. // integral
  48. std::numeric_limits<T>::is_integer? std::numeric_limits<T>::digits + 1 + std::numeric_limits<T>::is_signed:
  49. // floating point
  50. std::numeric_limits<T>::max_digits10 + 3 + 2 + detail::exp_digits( std::numeric_limits<T>::max_exponent10 ); // as above
  51. };
  52. #ifdef BOOST_CHARCONV_HAS_FLOAT128
  53. template <> struct limits<__float128>
  54. {
  55. BOOST_ATTRIBUTE_UNUSED static constexpr int max_chars10 = 33 + 3 + 2 + 5;
  56. BOOST_ATTRIBUTE_UNUSED static constexpr int max_chars = max_chars10;
  57. };
  58. #endif
  59. #if defined(BOOST_NO_CXX17_INLINE_VARIABLES)
  60. // Definitions of in-class constexpr members are allowed but deprecated in C++17
  61. template<typename T> BOOST_ATTRIBUTE_UNUSED constexpr int limits<T>::max_chars10;
  62. template<typename T> BOOST_ATTRIBUTE_UNUSED constexpr int limits<T>::max_chars;
  63. #endif // defined(BOOST_NO_CXX17_INLINE_VARIABLES)
  64. }} // namespace boost::charconv
  65. #endif // BOOST_CHARCONV_LIMITS_HPP