Fixed when the int value of "%0[width]d" is negative, the minus sign should be formatted before the padding '0' (#2329)

This commit is contained in:
F1F88 2025-11-09 00:33:30 +08:00 committed by GitHub
parent 22cc68808c
commit 2ae0d05a23
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -491,13 +491,15 @@ void AddInt(char **buf_p, size_t &maxlen, int val, int width, int flags)
unsignedVal /= 10; unsignedVal /= 10;
} while (unsignedVal); } while (unsignedVal);
if (signedVal < 0)
{
text[digits++] = '-';
}
buf = *buf_p; buf = *buf_p;
// minus sign BEFORE left padding if padding with zeros
if (signedVal < 0 && maxlen && (flags & ZEROPAD))
{
*buf++ = '-';
maxlen--;
}
if (!(flags & LADJUST)) if (!(flags & LADJUST))
{ {
while ((digits < width) && maxlen) while ((digits < width) && maxlen)
@ -508,6 +510,13 @@ void AddInt(char **buf_p, size_t &maxlen, int val, int width, int flags)
} }
} }
// minus sign AFTER left padding if padding with spaces
if (signedVal < 0 && maxlen && !(flags & ZEROPAD))
{
*buf++ = '-';
maxlen--;
}
while (digits-- && maxlen) while (digits-- && maxlen)
{ {
*buf++ = text[digits]; *buf++ = text[digits];