diff --git a/include/fmt/format.h b/include/fmt/format.h index a84fc717..18a009d8 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -1295,23 +1295,23 @@ template FMT_CONSTEXPR20 auto format_decimal(Char* out, UInt value, int size) -> format_decimal_result { FMT_ASSERT(size >= count_digits(value), "invalid digit count"); - out += size; - Char* end = out; + auto begin = out, end = out + size; + unsigned n = to_unsigned(size); while (value >= 100) { // Integer division is slow so do it for a group of two digits instead // of for every digit. The idea comes from the talk by Alexandrescu // "Three Optimization Tips for C++". See speed-test for a comparison. - out -= 2; - copy2(out, digits2(static_cast(value % 100))); + n -= 2; + copy2(begin + n, digits2(static_cast(value % 100))); value /= 100; } - if (value < 10) { - *--out = static_cast('0' + value); - return {out, end}; + if (value >= 10) { + n -= 2; + copy2(begin + n, digits2(static_cast(value))); + } else { + begin[--n] = static_cast('0' + value); } - out -= 2; - copy2(out, digits2(static_cast(value))); - return {out, end}; + return {begin + n, end}; } template