|
| 1 | +// Copyright 2025 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_DECIMAL_DETAIL_FORMATTING_LIMITS_HPP |
| 6 | +#define BOOST_DECIMAL_DETAIL_FORMATTING_LIMITS_HPP |
| 7 | + |
| 8 | +#include <boost/decimal/detail/config.hpp> |
| 9 | +#include <boost/decimal/detail/concepts.hpp> |
| 10 | +#include <boost/decimal/detail/buffer_sizing.hpp> |
| 11 | +#include <boost/decimal/detail/chars_format.hpp> |
| 12 | +#include <boost/decimal/detail/attributes.hpp> |
| 13 | + |
| 14 | +namespace boost { |
| 15 | +namespace decimal { |
| 16 | + |
| 17 | +template <BOOST_DECIMAL_DECIMAL_FLOATING_TYPE DecimalType, int Precision = -1> |
| 18 | +class formatting_limits |
| 19 | +{ |
| 20 | +private: |
| 21 | + |
| 22 | + // Fix format removes the e+/e- |
| 23 | + static constexpr std::size_t fixed_format_offset {2U}; |
| 24 | + |
| 25 | + static constexpr std::size_t required_characters() noexcept |
| 26 | + { |
| 27 | + static_assert(Precision > 0 || Precision == -1, "A specified precision must be greater than zero"); |
| 28 | + |
| 29 | + const auto local_precision {detail::get_real_precision<DecimalType>(Precision)}; |
| 30 | + return detail::total_buffer_length<DecimalType>(local_precision, detail::max_biased_exp_v<DecimalType>, true); |
| 31 | + } |
| 32 | + |
| 33 | +public: |
| 34 | + |
| 35 | + static constexpr std::size_t scientific_format_max_chars {required_characters()}; |
| 36 | + |
| 37 | + static constexpr std::size_t fixed_format_max_chars { scientific_format_max_chars - fixed_format_offset }; |
| 38 | + |
| 39 | + static constexpr std::size_t hex_format_max_chars { scientific_format_max_chars }; |
| 40 | + |
| 41 | + static constexpr std::size_t cohort_preserving_scientific_max_chars { scientific_format_max_chars }; |
| 42 | + |
| 43 | + static constexpr std::size_t general_format_max_chars { scientific_format_max_chars }; |
| 44 | + |
| 45 | + static constexpr std::size_t max_chars { scientific_format_max_chars }; |
| 46 | +}; |
| 47 | + |
| 48 | +#if !(defined(__cpp_inline_variables) && __cpp_inline_variables >= 201606L) && (!defined(_MSC_VER) || _MSC_VER != 1900) |
| 49 | + |
| 50 | +template <BOOST_DECIMAL_DECIMAL_FLOATING_TYPE DecimalType, int Precision> |
| 51 | +constexpr std::size_t formatting_limits<DecimalType, Precision>::scientific_format_max_chars; |
| 52 | + |
| 53 | +template <BOOST_DECIMAL_DECIMAL_FLOATING_TYPE DecimalType, int Precision> |
| 54 | +constexpr std::size_t formatting_limits<DecimalType, Precision>::fixed_format_max_chars; |
| 55 | + |
| 56 | +template <BOOST_DECIMAL_DECIMAL_FLOATING_TYPE DecimalType, int Precision> |
| 57 | +constexpr std::size_t formatting_limits<DecimalType, Precision>::hex_format_max_chars; |
| 58 | + |
| 59 | +template <BOOST_DECIMAL_DECIMAL_FLOATING_TYPE DecimalType, int Precision> |
| 60 | +constexpr std::size_t formatting_limits<DecimalType, Precision>::cohort_preserving_scientific_max_chars; |
| 61 | + |
| 62 | +template <BOOST_DECIMAL_DECIMAL_FLOATING_TYPE DecimalType, int Precision> |
| 63 | +constexpr std::size_t formatting_limits<DecimalType, Precision>::general_format_max_chars; |
| 64 | + |
| 65 | +#endif |
| 66 | + |
| 67 | +} // namespace decimal |
| 68 | +} // namespace boost |
| 69 | + |
| 70 | +#endif // BOOST_DECIMAL_DETAIL_FORMATTING_LIMITS_HPP |
0 commit comments