All of lore.kernel.org
 help / color / mirror / Atom feed
From: Simon Glass <sjg@chromium.org>
To: U-Boot Mailing List <u-boot@lists.denx.de>
Cc: "Eugeniu Rosca" <erosca@de.adit-jv.com>,
	"Roland Gaudig" <roland.gaudig@weidmueller.com>,
	"Heinrich Schuchardt" <xypron.glpk@gmx.de>,
	"Stefan Herbrechtsmeier" <stefan.herbrechtsmeier@weidmueller.com>,
	"Sean Anderson" <seanga2@gmail.com>,
	"Marek Behún" <marek.behun@nic.cz>,
	"Tom Rini" <trini@konsulko.com>, "Marek Vasut" <marex@denx.de>,
	"Simon Glass" <sjg@chromium.org>
Subject: [PATCH 12/15] lib: Support a decimal prefix 0m
Date: Tue, 20 Jul 2021 07:29:36 -0600	[thread overview]
Message-ID: <20210720132940.1171011-13-sjg@chromium.org> (raw)
In-Reply-To: <20210720132940.1171011-1-sjg@chromium.org>

U-Boot mostly uses hex for value input, largely because addresses are much
easier to understand in hex.

However sometimes it is useful to be able to supply a decimal value when a
hex value is expected.

Add this functionality, for increased flexibility.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 doc/usage/cmdline.rst |  7 +++++++
 include/vsprintf.h    |  6 ++++--
 lib/strto.c           |  3 +++
 test/str_ut.c         | 12 ++++++++++++
 4 files changed, 26 insertions(+), 2 deletions(-)

diff --git a/doc/usage/cmdline.rst b/doc/usage/cmdline.rst
index 3228be37bb7..7f2cfad2a0f 100644
--- a/doc/usage/cmdline.rst
+++ b/doc/usage/cmdline.rst
@@ -63,6 +63,13 @@ use of addresses, for example::
   00001000: 2c786f62 00697073 03000000 0c000000  box,spi.........
   00001010: 67020000 00000000                    ...g....
 
+In these cases it is possible to use a `0m` prefix ('deciMal') to use a decimal
+value if that is more convenient. For example, this shows 19 bytes (0x13)::
+
+  => md.b 1000 0m19
+  00001000: 62 6f 78 2c 73 70 69 00 00 00 00 03 00 00 00 0c  box,spi.........
+  00001010: 00 00 02                                         ...
+
 There is no need to add a `0x` prefix to the arguments and the output is shown
 in hex also, without any prefixes. This helps to avoid clutter.
 
diff --git a/include/vsprintf.h b/include/vsprintf.h
index 604963dad61..91822ecbe6a 100644
--- a/include/vsprintf.h
+++ b/include/vsprintf.h
@@ -23,7 +23,8 @@
  * 0 is returned
  *
  * A hex prefix is supported (e.g. 0x123) regardless of the value of @base.
- * If found, the base is set to hex (16).
+ * If found, the base is set to hex (16). Similarly a decimal prefix (e.g. 0d12)
+ * causes the base to be set to decimal (10).
  *
  * If @base is 0:
  *    - an octal '0' prefix (e.g. 0777) sets the base to octal (8).
@@ -76,7 +77,8 @@ unsigned long dectoul(const char *cp, char **endp);
  * echo will append a newline to the tail.
  *
  * A hex prefix is supported (e.g. 0x123) regardless of the value of @base.
- * If found, the base is set to hex (16).
+ * If found, the base is set to hex (16). Similarly a decimal prefix (e.g. 0d12)
+ * causes the base to be set to decimal (10).
  *
  * If @base is 0:
  *    - an octal '0' prefix (e.g. 0777) sets the base to octal (8).
diff --git a/lib/strto.c b/lib/strto.c
index 54ee3e81f6a..120214d83f8 100644
--- a/lib/strto.c
+++ b/lib/strto.c
@@ -23,6 +23,9 @@ static const char *_parse_integer_fixup_radix(const char *s, uint *basep)
 		if (ch == 'x') {
 			*basep = 16;
 			s += 2;
+		} else if (ch == 'm') {
+			*basep = 10;
+			s += 2;
 		} else if (!*basep) {
 			/* Only select octal if we don't have a base */
 			*basep = 8;
diff --git a/test/str_ut.c b/test/str_ut.c
index d2840d51524..716f7c45c38 100644
--- a/test/str_ut.c
+++ b/test/str_ut.c
@@ -111,6 +111,12 @@ static int str_simple_strtoul(struct unit_test_state *uts)
 	/* Check endp being NULL */
 	ut_asserteq(1099, simple_strtoul(str2, NULL, 0));
 
+	/* check decimal */
+	ut_assertok(run_strtoul(uts, "123fg", 0, 123, 3, false));
+	ut_assertok(run_strtoul(uts, "123a", 10, 123, 3, false));
+	ut_assertok(run_strtoul(uts, "0x123fg", 0, 0x123f, 6, false));
+	ut_assertok(run_strtoul(uts, "0m123a", 16, 123, 5, false));
+
 	return 0;
 }
 STR_TEST(str_simple_strtoul, 0);
@@ -174,6 +180,12 @@ static int str_simple_strtoull(struct unit_test_state *uts)
 	/* Check endp being NULL */
 	ut_asserteq(1099, simple_strtoull(str2, NULL, 0));
 
+	/* check decimal */
+	ut_assertok(run_strtoull(uts, "123fg", 0, 123, 3, false));
+	ut_assertok(run_strtoull(uts, "123a", 10, 123, 3, false));
+	ut_assertok(run_strtoull(uts, "0x123fg", 0, 0x123f, 6, false));
+	ut_assertok(run_strtoull(uts, "0m123a", 16, 123, 5, false));
+
 	return 0;
 }
 STR_TEST(str_simple_strtoull, 0);
-- 
2.32.0.402.g57bb445576-goog


  parent reply	other threads:[~2021-07-20 13:32 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-20 13:29 [PATCH 00/15] lib: Add support for a decimal 0m prefix for numbers Simon Glass
2021-07-20 13:29 ` [PATCH 01/15] hash: Ensure verification hex pairs are terminated Simon Glass
2021-07-20 13:29 ` [PATCH 02/15] global: Convert simple_strtoul() with hex to hextoul() Simon Glass
2021-07-20 13:29 ` [PATCH 03/15] global: Convert simple_strtoul() with decimal to dectoul() Simon Glass
2021-07-20 13:29 ` [PATCH 04/15] lib: Comment the base parameter with simple_strtoul/l() Simon Glass
2021-07-20 13:29 ` [PATCH 05/15] lib: Drop unnecessary check for hex digit Simon Glass
2021-07-20 13:29 ` [PATCH 06/15] lib: Add tests for simple_strtoull() Simon Glass
2021-07-20 13:29 ` [PATCH 07/15] lib: Add octal tests for simple_strtoul/l() Simon Glass
2021-07-20 13:29 ` [PATCH 08/15] lib: Move common digit-parsing code into a function Simon Glass
2021-07-20 13:29 ` [PATCH 09/15] doc: Convert command-line info to rST Simon Glass
2021-07-20 13:29 ` [PATCH 10/15] doc: Add a note about number representation Simon Glass
2021-07-20 13:29 ` [PATCH 11/15] lib: Allow using 0x when a decimal value is requested Simon Glass
2021-07-20 13:29 ` Simon Glass [this message]
2021-07-20 13:29 ` [PATCH 13/15] RFC: lib: Support a binary prefix 0y Simon Glass
2021-07-21  8:05   ` Sean Anderson
2021-07-21  8:30     ` Wolfgang Denk
2021-07-21  8:27   ` Wolfgang Denk
2021-07-21 14:23     ` Simon Glass
2021-07-22  9:48       ` Wolfgang Denk
2021-07-22 13:28         ` Simon Glass
2021-07-22 14:44           ` Tom Rini
2021-07-23  3:07             ` Simon Glass
2021-07-23  6:55           ` Wolfgang Denk
2021-07-23 21:41             ` Simon Glass
2021-07-24 13:00               ` Wolfgang Denk
2021-07-20 13:29 ` [PATCH 14/15] RFC: lib: Use 0o prefix for octal Simon Glass
2021-07-20 13:29 ` [PATCH 15/15] RFC: Change simple_strtoul() et al to default to hex Simon Glass
2021-07-20 14:22 ` [PATCH 00/15] lib: Add support for a decimal 0m prefix for numbers Tom Rini
2021-07-20 15:57   ` Simon Glass
2021-07-20 16:05     ` Tom Rini
2021-07-20 16:30       ` Eugeniu Rosca
2021-07-20 18:33       ` Simon Glass
2021-07-20 19:28         ` Tom Rini
2021-07-21 13:57           ` Simon Glass
2021-07-21  7:52       ` Wolfgang Denk
2021-07-21 14:46         ` Simon Glass
2021-07-23 21:41           ` Simon Glass
2021-07-20 15:00 ` Jonathan A. Kollasch
2021-07-20 15:04   ` Tom Rini
2021-07-21  7:42 ` Wolfgang Denk

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=20210720132940.1171011-13-sjg@chromium.org \
    --to=sjg@chromium.org \
    --cc=erosca@de.adit-jv.com \
    --cc=marek.behun@nic.cz \
    --cc=marex@denx.de \
    --cc=roland.gaudig@weidmueller.com \
    --cc=seanga2@gmail.com \
    --cc=stefan.herbrechtsmeier@weidmueller.com \
    --cc=trini@konsulko.com \
    --cc=u-boot@lists.denx.de \
    --cc=xypron.glpk@gmx.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 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.