fix(strutil): keep center-aligned Resize within the requested length

The center branch of resize() added the odd-padding space whenever the
input was non-empty, because the parity check omitted the source length
(wantLen - padLn*2 instead of wantLen - sLen - padLn*2). This made Resize,
Align and their utf8 variants overflow wantLen by one for every non-empty
string with an even amount of padding. Subtract sLen so the extra space is
only added when the total padding is actually odd.
This commit is contained in:
dualfroz committed 2026-09-04 09:07:10 +02:00
1 parent b17a55589a
commit e2e5b5495f
3 files changed
+99 -1

No files matched your search

+3 -1
View File
@@ -164,7 +164,9 @@ func resize(s string, sLen, wantLen int, align comdef.Align, cutOverflow bool) s
if align == comdef.Center {
padLn := (wantLen - sLen) / 2
if diff = wantLen - padLn*2; diff > 0 {
// add a single extra space only when the total padding is odd,
// otherwise the result overflows the requested length.
if diff = wantLen - sLen - padLn*2; diff > 0 {
s += " "
}
padStr := string(RepeatBytes(' ', padLn))