Workaround gcc stringop-overflow bug

This commit is contained in:
Victor Zverovich 2024-07-22 10:31:13 -07:00
parent 524ca1c715
commit 7f157dca0a

View File

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