From e900d735bb3e3fcf27d1f2ab58e44c364c0b305c Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sun, 14 Jun 2020 08:30:13 -0700 Subject: [PATCH] Re-enable assert in format_decimal --- include/fmt/format.h | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index da56015c..9937aaa7 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -895,7 +895,7 @@ template struct format_decimal_result { template inline format_decimal_result format_decimal(Char* out, UInt value, int size) { - //FMT_ASSERT(size >= count_digits(value), "invalid digit count"); + FMT_ASSERT(size >= count_digits(value), "invalid digit count"); out += size; Char* end = out; while (value >= 100) { @@ -2971,25 +2971,28 @@ class format_int { mutable char buffer_[buffer_size]; char* str_; - char* format_decimal(unsigned long long value) { - return detail::format_decimal(buffer_, value, buffer_size - 1).begin; + template char* format_unsigned(UInt value) { + auto n = static_cast>(value); + return detail::format_decimal(buffer_, n, buffer_size - 1).begin; } - void format_signed(long long value) { - auto abs_value = static_cast(value); + template char* format_signed(Int value) { + auto abs_value = static_cast>(value); bool negative = value < 0; if (negative) abs_value = 0 - abs_value; - str_ = format_decimal(abs_value); - if (negative) *--str_ = '-'; + auto begin = format_unsigned(abs_value); + if (negative) *--begin = '-'; + return begin; } public: - explicit format_int(int value) { format_signed(value); } - explicit format_int(long value) { format_signed(value); } - explicit format_int(long long value) { format_signed(value); } - explicit format_int(unsigned value) : str_(format_decimal(value)) {} - explicit format_int(unsigned long value) : str_(format_decimal(value)) {} - explicit format_int(unsigned long long value) : str_(format_decimal(value)) {} + explicit format_int(int value) : str_(format_signed(value)) {} + explicit format_int(long value) : str_(format_signed(value)) {} + explicit format_int(long long value) : str_(format_signed(value)) {} + explicit format_int(unsigned value) : str_(format_unsigned(value)) {} + explicit format_int(unsigned long value) : str_(format_unsigned(value)) {} + explicit format_int(unsigned long long value) + : str_(format_unsigned(value)) {} /** Returns the number of characters written to the output buffer. */ size_t size() const { @@ -3370,13 +3373,15 @@ template ::value)> inline std::string to_string(const T& value) { return format("{}", value); } + template ::value)> inline std::string to_string(T value) { - // Buffer should be large enough to store the number or "false" (for bool). - char buffer[(std::max)(detail::digits10() + 2, 5)]; + // The buffer should be large enough to store the number including the sign or + // "false" for bool. + constexpr int max_size = detail::digits10() + 2; + char buffer[max_size > 5 ? max_size : 5]; char* begin = buffer; - char* end = detail::write(begin, value); - return std::string(begin, end); + return std::string(begin, detail::write(begin, value)); } /**