From 2afa544d7d10d48216c8c66f7d5b1460ef2f1ff7 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Sun, 15 Jun 2025 02:27:36 +0200 Subject: [PATCH] Correctly format '%c' when given a width. #2199 --- lib/std/io/formatter_private.c3 | 12 +++++++++--- releasenotes.md | 1 + test/unit/stdlib/string.c3 | 5 +++++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/std/io/formatter_private.c3 b/lib/std/io/formatter_private.c3 index ab019d74a..2b6f1750c 100644 --- a/lib/std/io/formatter_private.c3 +++ b/lib/std/io/formatter_private.c3 @@ -623,9 +623,12 @@ fn usz? Formatter.out_char(&self, any arg) @private return self.out_substr(""); } usz len = 1; - uint l = 1; // pre padding - len += self.adjust(l)!; + if (!self.flags.left) + { + len += self.pad(' ', self.width, len)!; + } + // char output Char32 c = types::any_to_int(arg, uint) ?? 0xFFFD; switch (true) @@ -645,7 +648,10 @@ fn usz? Formatter.out_char(&self, any arg) @private self.out((char)(0x80 | (c >> 6 & 0x3F)))!; self.out((char)(0x80 | (c & 0x3F)))!; } - len += self.adjust(l)!; + if (self.flags.left) + { + len += self.pad(' ', self.width, len)!; + } return len; } diff --git a/releasenotes.md b/releasenotes.md index 4b4d39245..7ebb86651 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -37,6 +37,7 @@ - Make `unreachable()` only panic in safe mode. - `cflags` additions for targets was not handed properly. #2209 - `$echo` would suppress warning about unreachable code. #2205 +- Correctly format '%c' when given a width. #2199 ### Stdlib changes - Deprecate `String.is_zstr` and `String.quick_zstr` #2188. diff --git a/test/unit/stdlib/string.c3 b/test/unit/stdlib/string.c3 index 058059c8d..2e84c5ec8 100644 --- a/test/unit/stdlib/string.c3 +++ b/test/unit/stdlib/string.c3 @@ -1,5 +1,10 @@ module string_test; +fn void test_format() @test +{ + test::eq(string::tformat("_%*c_", 5, 'x'), "_ x_"); + test::eq(string::tformat("_%-*c_", 5, 'x'), "_x _"); +} fn void test_clear() @test { DString s = dstring::new_with_capacity(mem, 32);