From: David Laight <david.laight.linux@gmail.com>
To: Cheng Li <im.lechain@gmail.com>
Cc: "Willy Tarreau" <w@1wt.eu>,
"Thomas Weißschuh" <linux@weissschuh.net>,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH 1/2] tools/nolibc: add support zero pad (0) in printf
Date: Fri, 30 Jan 2026 14:58:01 +0000 [thread overview]
Message-ID: <20260130145801.01d3908e@pumpkin> (raw)
In-Reply-To: <CAAZOWcKKvB=SoyCHOkqNbcTOR_BNmrSXUM8NUK0oLcP_C5c11A@mail.gmail.com>
On Fri, 30 Jan 2026 18:12:48 +0800
Cheng Li <im.lechain@gmail.com> wrote:
> David Laight <david.laight.linux@gmail.com> 于2026年1月30日周五 18:02写道:
> >
> > On Fri, 30 Jan 2026 16:37:35 +0800
> > "licheng.li" <im.lechain@gmail.com> wrote:
> >
> > > From: Cheng Li <im.lechain@gmail.com>
> > >
> > > This patch correctly implements the '0' flag in __nolibc_printf() to
> > > allow zero-padding for numeric and pointer outputs.
> > >
> > > Thanks to David for pointing out the errors in the previous implementation.
> > >
> > > The logic ensures that the sign ('-') for negative numbers or the prefix
> > > ('0x') for pointers is printed before the padding zeros, adhering to
> > > standard printf behavior (e.g., producing "-0005" instead of "000-5").
> >
> > I think it would be much better to change the contents of tmpbuf[]
> > where it is filled with the converted number.
> > You'd need to limit the number of zeros added (or the precision) to (say) 32
> > and then set 'outbuf = tmpbuf + 32' so that the zeros and sign/0x can be
> > added at the front.
>
> Hi David,
>
> You are absolutely right. Modifying the buffer generation logic
> (tmpbuf) is indeed
> the robust way to handle sign/prefix insertion combined with padding/precision.
> My current approach of trying to handle it during the output phase is
> too fragile
> and misses edge cases (as I also realized regarding the negative sign issue).
>
> Given the complexity you mentioned (like the subtle differences in
> standard compliance)
> and your plan to implement full field precision, I will drop this
> "Zero Padding" patch entirely.
> It is better implemented as part of a proper precision support overhaul.
>
> I will focus on the "Left Alignment" patch. I am sending out v4 of
> that series later,
> which incorporates the code-swapping optimization you suggested earlier.
>
> Thanks for the detailed feedback.
A good plan :-)
I've written support for "%[#+- 0]*[.*][duxp]" but not tested the new bits yet.
That is at +175 bytes after I used OPTIMZER_HIDE_VAR() to stop the compiler
making a 'pigs breakfast' of the code (added nearly 100 bytes for a simple if()
to avoid everything when the value and precision are both zero).
David
next prev parent reply other threads:[~2026-01-30 14:58 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-01-30 8:37 [PATCH 1/2] tools/nolibc: add support zero pad (0) in printf licheng.li
2026-01-30 8:37 ` [PATCH 2/2] selftests/nolibc: add tests for printf zero padding (0) licheng.li
2026-01-30 10:02 ` [PATCH 1/2] tools/nolibc: add support zero pad (0) in printf David Laight
2026-01-30 10:12 ` Cheng Li
2026-01-30 14:58 ` David Laight [this message]
2026-01-31 3:44 ` Cheng Li
2026-01-31 10:18 ` Thomas Weißschuh
2026-01-31 12:32 ` David Laight
2026-02-01 0:49 ` Cheng Li
2026-02-01 8:56 ` David Laight
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=20260130145801.01d3908e@pumpkin \
--to=david.laight.linux@gmail.com \
--cc=im.lechain@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@weissschuh.net \
--cc=w@1wt.eu \
/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