Fixed {:c} formatting with negative integrals

std::format uses the two's complement for formatting while
   FML used the absolute value, failing to correctly write when
   using values like -104 (FMT used 104 while std uses 152)
This commit is contained in:
David Xchel Morales Hurtado 2026-07-10 21:45:05 -06:00
parent 6dac6cad05
commit d34e749f6e

View File

@ -2124,6 +2124,10 @@ FMT_CONSTEXPR FMT_INLINE auto write_int(OutputIt out, write_int_arg<T> arg,
prefix_append(prefix, unsigned(specs.upper() ? 'B' : 'b') << 8 | '0');
break;
case presentation_type::chr:
// Handles negative cases on casts to smaller types using Two's complement
// One example is int{-104} to char which std::format uses as 152
if (sizeof(Char) < sizeof(abs_value) && (arg.prefix & 0xff) == '-')
abs_value = (1 << (8*sizeof(Char))) - abs_value;
return write_char<Char>(out, static_cast<Char>(abs_value), specs);
}