From b83d3885231b3b5688afcbc2b4395c3eb6eea7f8 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Thu, 1 Aug 2024 01:20:42 +0200 Subject: [PATCH] Incorrect justify formatting of integers. --- lib/std/io/formatter_private.c3 | 6 +++--- releasenotes.md | 1 + test/unit/stdlib/io/printf.c3 | 15 +++++++++++++++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/std/io/formatter_private.c3 b/lib/std/io/formatter_private.c3 index 57075cfd0..793afe62a 100644 --- a/lib/std/io/formatter_private.c3 +++ b/lib/std/io/formatter_private.c3 @@ -628,15 +628,15 @@ fn usz! Formatter.out_reverse(&self, char[] buf) @private usz buffer_start_idx = self.idx; usz len = buf.len; // pad spaces up to given width - if (!self.flags.zeropad) + if (!self.flags.zeropad && !self.flags.left) { - n += self.adjust(len)!; + n += self.pad(' ', self.width, len)!; } // reverse string while (len) n += self.out(buf[--len])!; // append pad spaces up to given width - n += self.adjust(self.idx - buffer_start_idx)!; + n += self.adjust(n)!; return n; } diff --git a/releasenotes.md b/releasenotes.md index 9b91ac2e0..6b695ac9d 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -27,6 +27,7 @@ - Fix incorrect linker selection on some platforms. - Struct members declared in a single line declaration were not sharing attributes. #1266 - `opt` project setting now properly documented. +- Incorrect justify formatting of integers. ### Stdlib changes diff --git a/test/unit/stdlib/io/printf.c3 b/test/unit/stdlib/io/printf.c3 index 6edfdf9e1..d52373019 100644 --- a/test/unit/stdlib/io/printf.c3 +++ b/test/unit/stdlib/io/printf.c3 @@ -1,5 +1,20 @@ module std::io @test; +fn void printf_int() +{ + String s; + s = string::new_format("[%-10d]", 78); + assert(s == "[78 ]"); + s = string::new_format("[%10d]", 78); + assert(s == "[ 78]"); + s = string::new_format("[%010d]", 78); + assert(s == "[0000000078]"); + s = string::new_format("[%+10d]", 78); + assert(s == "[ +78]"); + s = string::new_format("[%-+10d]", 78); + assert(s == "[+78 ]"); +} + fn void printf_a() { String s;