buffer_sizing.hpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright 2024 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_DETAIL_BUFFER_SIZING_HPP
  5. #define BOOST_CHARCONV_DETAIL_BUFFER_SIZING_HPP
  6. #include <boost/charconv/detail/config.hpp>
  7. #include <boost/charconv/detail/integer_search_trees.hpp>
  8. #include <type_traits>
  9. namespace boost {
  10. namespace charconv {
  11. namespace detail {
  12. #ifdef BOOST_MSVC
  13. # pragma warning(push)
  14. # pragma warning(disable: 4127) // Conditional expression for BOOST_IF_CONSTEXPR will be constant in not C++17
  15. #endif
  16. template <typename Real>
  17. inline int get_real_precision(int precision = -1) noexcept
  18. {
  19. // If the user did not specify a precision than we use the maximum representable amount
  20. // and remove trailing zeros at the end
  21. int real_precision;
  22. BOOST_IF_CONSTEXPR (!std::is_same<Real, long double>::value
  23. #ifdef BOOST_CHARCONV_HAS_FLOAT128
  24. && !std::is_same<Real, __float128>::value
  25. #endif
  26. )
  27. {
  28. real_precision = precision == -1 ? std::numeric_limits<Real>::max_digits10 : precision;
  29. }
  30. else
  31. {
  32. #ifdef BOOST_CHARCONV_HAS_FLOAT128
  33. BOOST_CHARCONV_IF_CONSTEXPR (std::is_same<Real, __float128>::value)
  34. {
  35. real_precision = 33;
  36. }
  37. else
  38. #endif
  39. {
  40. #if BOOST_CHARCONV_LDBL_BITS == 128
  41. real_precision = 33;
  42. #else
  43. real_precision = 18;
  44. #endif
  45. }
  46. }
  47. return real_precision;
  48. }
  49. template <typename Int>
  50. inline int total_buffer_length(int real_precision, Int exp, bool signed_value)
  51. {
  52. // Sign + integer part + '.' + precision of fraction part + e+/e- or p+/p- + exponent digits
  53. return static_cast<int>(signed_value) + 1 + real_precision + 2 + num_digits(exp);
  54. }
  55. #ifdef BOOST_MSVC
  56. # pragma warning(pop)
  57. #endif
  58. } //namespace detail
  59. } //namespace charconv
  60. } //namespace boost
  61. #endif //BOOST_CHARCONV_DETAIL_BUFFER_SIZING_HPP