diff options
| -rw-r--r-- | include/cxxformat/formatters.hpp | 17 |
1 files changed, 15 insertions, 2 deletions
diff --git a/include/cxxformat/formatters.hpp b/include/cxxformat/formatters.hpp index c32d2e8..b248b17 100644 --- a/include/cxxformat/formatters.hpp +++ b/include/cxxformat/formatters.hpp @@ -312,12 +312,25 @@ namespace format { assert(!"Unsupported conversion; should have been caught by formatter<T>::conversionSupported"); } + optional_int<int> intPrecision; + + if (precision) + { + const auto val = *precision; + constexpr auto max = std::numeric_limits<int>::max(); + if (val > max) + { + throw std::out_of_range{"Precision is too big; maximum supported for floating point types is " + std::to_string(max)}; + } + intPrecision = static_cast<int>(val); + } + char* data, *end; std::errc ec; - const auto call_to_chars = [format, precision, t, &data, &end, &ec](char* data_, std::size_t size) + const auto call_to_chars = [format, intPrecision, t, &data, &end, &ec](char* data_, std::size_t size) { data = data_; - const auto [end_, ec_] = precision ? std::to_chars(data, data + size, t, format, *precision) : std::to_chars(data, data + size, t, format); + const auto [end_, ec_] = intPrecision ? std::to_chars(data, data + size, t, format, *intPrecision) : std::to_chars(data, data + size, t, format); end = end_; ec = ec_; }; |
