From: "Radim Krčmář" <rkrcmar@redhat.com>
To: kvm@vger.kernel.org
Cc: Paolo Bonzini <pbonzini@redhat.com>
Subject: [kvm-unit-tests PATCH 5/6] libcflat: support # flag in printf
Date: Wed, 17 May 2017 22:14:04 +0200 [thread overview]
Message-ID: <20170517201405.19867-6-rkrcmar@redhat.com> (raw)
In-Reply-To: <20170517201405.19867-1-rkrcmar@redhat.com>
The only use is with %#x, where we'll automatically get 0x prefix.
Advantage over 0x%x can be seen with padding.
A simple test:
printf(".%#08x.\n", 0);
printf(".%#8x.\n", 0);
printf(".%#-8x.\n", 0);
printf(".%#08x.\n", 1);
printf(".%#8x.\n", 1);
printf(".%#-8x.\n", 1);
printf(".%#08x.\n", 0x123456);
printf(".%#8x.\n", 0x123456);
printf(".%#-8x.\n", 0x123456);
printf(".%#02x.\n", 0);
printf(".%#2x.\n", 0);
printf(".%#-2x.\n", 0);
printf(".%#02x.\n", 1);
printf(".%#2x.\n", 1);
printf(".%#-2x.\n", 1);
looks just like glibc:
.00000000.
. 0.
.0 .
.0x000001.
. 0x1.
.0x1 .
.0x123456.
.0x123456.
.0x123456.
.00.
. 0.
.0 .
.0x1.
.0x1.
.0x1.
Signed-off-by: Radim Krčmář <rkrcmar@redhat.com>
---
lib/printf.c | 15 +++++++++++++--
1 file changed, 13 insertions(+), 2 deletions(-)
diff --git a/lib/printf.c b/lib/printf.c
index 944768f3080d..bfe05f573f08 100644
--- a/lib/printf.c
+++ b/lib/printf.c
@@ -106,8 +106,16 @@ void print_unsigned(pstream_t *ps, unsigned long long n, int base,
if (p == buf)
*p++ = '0';
else if (props.alternate && base == 16) {
- *p++ = 'x';
- *p++ = '0';
+ if (props.pad == '0') {
+ addchar(ps, '0');
+ addchar(ps, 'x');
+
+ if (props.npad > 0)
+ props.npad = MAX(props.npad - 2, 0);
+ } else {
+ *p++ = 'x';
+ *p++ = '0';
+ }
}
for (i = 0; i < (p - buf) / 2; ++i) {
@@ -169,6 +177,9 @@ int vsnprintf(char *buf, int size, const char *fmt, va_list va)
case '\0':
--fmt;
break;
+ case '#':
+ props.alternate = true;
+ goto morefmt;
case '0':
props.pad = '0';
++fmt;
--
2.13.0
next prev parent reply other threads:[~2017-05-17 20:15 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-05-17 20:13 [kvm-unit-tests PATCH 0/6] improve report/printf formatting Radim Krčmář
2017-05-17 20:14 ` [kvm-unit-tests PATCH 1/6] fix conversions in report()s Radim Krčmář
2017-05-18 2:34 ` Thomas Huth
2017-05-18 8:03 ` David Hildenbrand
2017-05-17 20:14 ` [kvm-unit-tests PATCH 2/6] libcflat: add format checking to report() Radim Krčmář
2017-05-18 2:38 ` Thomas Huth
2017-05-17 20:14 ` [kvm-unit-tests PATCH 3/6] libcflat: fix padding in printf Radim Krčmář
2017-05-17 20:14 ` [kvm-unit-tests PATCH 4/6] pci-edu: remove 0x before a decimal value Radim Krčmář
2017-05-18 2:39 ` Thomas Huth
2017-05-17 20:14 ` Radim Krčmář [this message]
2017-05-17 20:14 ` [kvm-unit-tests PATCH 6/6] use %# instead of 0x% in all format strings Radim Krčmář
2017-05-18 16:04 ` [kvm-unit-tests PATCH 0/6] improve report/printf formatting Paolo Bonzini
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=20170517201405.19867-6-rkrcmar@redhat.com \
--to=rkrcmar@redhat.com \
--cc=kvm@vger.kernel.org \
--cc=pbonzini@redhat.com \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.