From: Christoph Niedermaier <cniedermaier@dh-electronics.com>
To: <u-boot@lists.denx.de>
Cc: Christoph Niedermaier <cniedermaier@dh-electronics.com>,
Marek Vasut <marex@denx.de>, Tom Rini <trini@konsulko.com>,
Benedikt Spranger <b.spranger@linutronix.de>,
Simon Glass <sjg@chromium.org>,
John Ogness <john.ogness@linutronix.de>,
Jerome Forissier <jerome.forissier@linaro.org>,
Ilias Apalodimas <ilias.apalodimas@linaro.org>
Subject: [PATCH] tiny-printf: Add support for upper case hex values
Date: Thu, 20 Mar 2025 11:23:46 +0100 [thread overview]
Message-ID: <20250320102346.13564-1-cniedermaier@dh-electronics.com> (raw)
If tiny printf is used with 0x%08X (upper case X) the output is
always 0x00000000. It could be confusing if upper case instead
of lower case is used intentionally or accidentally because the
actual value is not output. To avoid this confusion, tiny printf
is extended to support also the formatting with %X.
Signed-off-by: Christoph Niedermaier <cniedermaier@dh-electronics.com>
---
Cc: Marek Vasut <marex@denx.de>
Cc: Tom Rini <trini@konsulko.com>
Cc: Benedikt Spranger <b.spranger@linutronix.de>
Cc: Simon Glass <sjg@chromium.org>
Cc: John Ogness <john.ogness@linutronix.de>
Cc: Jerome Forissier <jerome.forissier@linaro.org>
Cc: Ilias Apalodimas <ilias.apalodimas@linaro.org>
---
lib/tiny-printf.c | 31 +++++++++++++++++++++++++------
1 file changed, 25 insertions(+), 6 deletions(-)
diff --git a/lib/tiny-printf.c b/lib/tiny-printf.c
index 0503c17341f..81c51d12f6c 100644
--- a/lib/tiny-printf.c
+++ b/lib/tiny-printf.c
@@ -13,6 +13,11 @@
#include <stdarg.h>
#include <linux/ctype.h>
+enum case_style {
+ LOWERCASE = 0,
+ UPPERCASE = 1,
+};
+
struct printf_info {
char *bf; /* Digit buffer */
char zs; /* non-zero if a digit has been written */
@@ -27,14 +32,19 @@ static void out(struct printf_info *info, char c)
*info->bf++ = c;
}
-static void out_dgt(struct printf_info *info, char dgt)
+static void out_dgt_case(struct printf_info *info, char dgt, enum case_style style)
{
- out(info, dgt + (dgt < 10 ? '0' : 'a' - 10));
+ out(info, dgt + (dgt < 10 ? '0' : (style ? 'A' : 'a') - 10));
info->zs = 1;
}
-static void div_out(struct printf_info *info, unsigned long *num,
- unsigned long div)
+static void out_dgt(struct printf_info *info, char dgt)
+{
+ out_dgt_case(info, dgt, LOWERCASE);
+}
+
+static void div_out_case(struct printf_info *info, unsigned long *num,
+ unsigned long div, enum case_style style)
{
unsigned char dgt = 0;
@@ -44,7 +54,13 @@ static void div_out(struct printf_info *info, unsigned long *num,
}
if (info->zs || dgt > 0)
- out_dgt(info, dgt);
+ out_dgt_case(info, dgt, style);
+}
+
+static void div_out(struct printf_info *info, unsigned long *num,
+ unsigned long div)
+{
+ div_out_case(info, num, div, LOWERCASE);
}
#ifdef CONFIG_SPL_NET
@@ -203,6 +219,7 @@ static int _vprintf(struct printf_info *info, const char *fmt, va_list va)
unsigned long num;
char buf[12];
unsigned long div;
+ enum case_style style = LOWERCASE;
while ((ch = *(fmt++))) {
if (ch != '%') {
@@ -283,6 +300,8 @@ static int _vprintf(struct printf_info *info, const char *fmt, va_list va)
}
islong = true;
/* no break */
+ case 'X':
+ style = UPPERCASE;
case 'x':
if (islong) {
num = va_arg(va, unsigned long);
@@ -295,7 +314,7 @@ static int _vprintf(struct printf_info *info, const char *fmt, va_list va)
out_dgt(info, 0);
} else {
for (; div; div /= 0x10)
- div_out(info, &num, div);
+ div_out_case(info, &num, div, style);
}
break;
case 'c':
--
2.30.2
next reply other threads:[~2025-03-20 10:25 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-20 10:23 Christoph Niedermaier [this message]
2025-03-20 11:49 ` [PATCH] tiny-printf: Add support for upper case hex values Marek Vasut
2025-03-20 13:53 ` Tom Rini
2025-03-20 14:00 ` Quentin Schulz
2025-03-20 14:14 ` Marek Vasut
2025-03-20 14:18 ` Tom Rini
2025-03-20 17:58 ` Christoph Niedermaier
2025-03-20 18:28 ` Tom Rini
2025-03-20 18:41 ` Christoph Niedermaier
2025-04-01 8:55 ` Michael Walle
2025-04-01 14:08 ` Christoph Niedermaier
2025-04-02 7:01 ` Michael Walle
2025-04-02 9:03 ` Christoph Niedermaier
2025-04-04 8:33 ` Michael Walle
2025-04-04 14:58 ` Tom Rini
2025-04-07 5:27 ` Michael Walle
2025-04-07 6:55 ` Christoph Niedermaier
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=20250320102346.13564-1-cniedermaier@dh-electronics.com \
--to=cniedermaier@dh-electronics.com \
--cc=b.spranger@linutronix.de \
--cc=ilias.apalodimas@linaro.org \
--cc=jerome.forissier@linaro.org \
--cc=john.ogness@linutronix.de \
--cc=marex@denx.de \
--cc=sjg@chromium.org \
--cc=trini@konsulko.com \
--cc=u-boot@lists.denx.de \
/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