From: Willy Tarreau <w@1wt.eu>
To: "licheng.li" <im.lechain@gmail.com>
Cc: "Thomas Weißschuh" <linux@weissschuh.net>, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 1/2] tools/nolibc: support left-aligned printing in printf
Date: Wed, 28 Jan 2026 08:47:28 +0100 [thread overview]
Message-ID: <aXm_EPmtxuGlIHpu@1wt.eu> (raw)
In-Reply-To: <20260126031038.35387-1-im.lechain@gmail.com>
On Mon, Jan 26, 2026 at 11:10:37AM +0800, licheng.li wrote:
> From: Cheng Li <im.lechain@gmail.com>
>
> Currently, __nolibc_printf() in nolibc does not support the '-' flag
> for left alignment. This limits the ability to format tabular output
> nicely.
>
> This patch adds support for the '-' flag. The implementation is kept
> minimal to minimize the binary size impact.
>
> Measuring the nolibc-test binary on x86_64 shows a small increase in
> the text section:
>
> text data bss dec hex filename
> 43552 248 112 43912 ab88 nolibc-test (before)
> 43677 248 112 44037 ac05 nolibc-test (after)
>
> The net increase is 125 bytes.
>
> Signed-off-by: Cheng Li <im.lechain@gmail.com>
> ---
> tools/include/nolibc/stdio.h | 18 +++++++++++++++---
> 1 file changed, 15 insertions(+), 3 deletions(-)
>
> diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h
> index 1f16dab2ac88..b90b59237bda 100644
> --- a/tools/include/nolibc/stdio.h
> +++ b/tools/include/nolibc/stdio.h
> @@ -250,7 +250,7 @@ typedef int (*__nolibc_printf_cb)(intptr_t state, const char *buf, size_t size);
> static __attribute__((unused, format(printf, 4, 0)))
> int __nolibc_printf(__nolibc_printf_cb cb, intptr_t state, size_t n, const char *fmt, va_list args)
> {
> - char escape, lpref, c;
> + char escape, lpref, padc, c;
> unsigned long long v;
> unsigned int written, width;
> size_t len, ofs, w;
> @@ -261,11 +261,17 @@ int __nolibc_printf(__nolibc_printf_cb cb, intptr_t state, size_t n, const char
> while (1) {
> c = fmt[ofs++];
> width = 0;
> + padc = ' ';
>
> if (escape) {
> /* we're in an escape sequence, ofs == 1 */
> escape = 0;
>
> + if (c == '-' || c == '0') {
> + padc = c;
> + c = fmt[ofs++];
> + }
> +
> /* width */
> while (c >= '0' && c <= '9') {
> width *= 10;
> @@ -358,13 +364,19 @@ int __nolibc_printf(__nolibc_printf_cb cb, intptr_t state, size_t n, const char
> if (n) {
> w = len < n ? len : n;
> n -= w;
> - while (width-- > w) {
> - if (cb(state, " ", 1) != 0)
> + while (padc != '-' && width > w) {
> + if (cb(state, &padc, 1) != 0)
> return -1;
> written += 1;
> + width--;
> }
> if (cb(state, outstr, w) != 0)
> return -1;
> + while (width-- > w) {
> + if (cb(state, " ", 1) != 0)
> + return -1;
> + written += 1;
> + }
> }
>
> written += len;
Thank you, this looks good to me. Since you have paid particular attention
to the size increase, I noticed a few tricks which help a little bit here:
- increment written at the beginning of the loop instead of the end
helps the compiler make the code more compact.
- testing width > w before padc != '-' also helps by benefitting from
the result of the last iteration's increment:
while (width > w && padc != '-') {
written += 1;
if (cb(state, &padc, 1) != 0)
return -1;
width--;
}
...
while (width-- > w) {
written += 1;
if (cb(state, " ", 1) != 0)
return -1;
}
$ printf '#include "stdio.h"\nvoid *p = printf;\n' | gcc -Os -xc -c -o printf.o -
$ nm -oa --size printf*o|grep 'printf$'
printf-before.o:000000000000035f t printf
printf-licheng.o:00000000000003b5 t printf
printf.o:00000000000003a3 t printf
=> that's 19 less bytes, or almost 10% smaller ;-)
I also tried changing the 3 blocks for a single loop checking width and w
to call cb() once and adjust written/w once but it gave the exact same
size for less efficient code so that was not worth it.
Anyway, just to say that if you're interested in trying the minor
adjustments above, you're welcome, otherwise I'm fine with this patch.
Thanks!
Willy
next prev parent reply other threads:[~2026-01-28 7:55 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-01-26 3:10 [PATCH 1/2] tools/nolibc: support left-aligned printing in printf licheng.li
2026-01-26 3:10 ` [PATCH 2/2] selftests/nolibc: add tests for printf left alignment licheng.li
2026-01-28 7:51 ` Willy Tarreau
2026-01-28 7:47 ` Willy Tarreau [this message]
2026-01-28 7:53 ` [PATCH 1/2] tools/nolibc: support left-aligned printing in printf Willy Tarreau
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=aXm_EPmtxuGlIHpu@1wt.eu \
--to=w@1wt.eu \
--cc=im.lechain@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@weissschuh.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox