From 2ae0d05a23c375112b8a0bf9d091030cf4c5e9f5 Mon Sep 17 00:00:00 2001 From: F1F88 <0xf1f88@gmail.com> Date: Sun, 9 Nov 2025 00:33:30 +0800 Subject: [PATCH] Fixed when the int value of "%0[width]d" is negative, the minus sign should be formatted before the padding '0' (#2329) --- core/logic/sprintf.cpp | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/core/logic/sprintf.cpp b/core/logic/sprintf.cpp index b872194f7..703ee4b66 100644 --- a/core/logic/sprintf.cpp +++ b/core/logic/sprintf.cpp @@ -491,13 +491,15 @@ void AddInt(char **buf_p, size_t &maxlen, int val, int width, int flags) unsignedVal /= 10; } while (unsignedVal); - if (signedVal < 0) - { - text[digits++] = '-'; - } - buf = *buf_p; + // minus sign BEFORE left padding if padding with zeros + if (signedVal < 0 && maxlen && (flags & ZEROPAD)) + { + *buf++ = '-'; + maxlen--; + } + if (!(flags & LADJUST)) { 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) { *buf++ = text[digits];