From 6ac5332e4f6988112f4272cfe7a2eee31173ff3f Mon Sep 17 00:00:00 2001 From: Werner Koch Date: Thu, 14 Aug 2025 17:03:22 +0200 Subject: estream: Support the %b conversion specifier from C23 * src/estream-printf.c (CONSSPEC_BIN): New. (compute_type): Handle it. (parse_format): Support 'b'. (do_format): Divert 'b' to pr_integer. (pr_integer): Print binary format. * tests/t-printf.c (check_fprintf_sf): Add two simple testcases. -- Might be handy in some cases and is easy to implement. Minor drawback is that this increases a stack based buffer from 100 to 150 bytes. --- tests/t-printf.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'tests') diff --git a/tests/t-printf.c b/tests/t-printf.c index 87c201d..eb54750 100644 --- a/tests/t-printf.c +++ b/tests/t-printf.c @@ -486,6 +486,32 @@ check_fprintf_sf (void) } free (result); + gpgrt_fprintf_sf (stream, string_filter, &sfstate, + "a=%x b=%b c=%X", + 19410909u, 19410909u, 19410909u); + expect = "a=1282fdd b=1001010000010111111011101 c=1282FDD"; + result = stream_to_string (stream); + if (strcmp (result, expect)) + { + show ("expect: '%s'\n", expect); + show ("result: '%s'\n", result); + fail ("fprintf_sf failed at %d\n", __LINE__); + } + free (result); + + gpgrt_fprintf_sf (stream, string_filter, &sfstate, + "a=%#x b=%#b c=%#X", + 19420130u, 19420130u, 19420130u); + expect = "a=0x12853e2 b=0b1001010000101001111100010 c=0X12853E2"; + result = stream_to_string (stream); + if (strcmp (result, expect)) + { + show ("expect: '%s'\n", expect); + show ("result: '%s'\n", result); + fail ("fprintf_sf failed at %d\n", __LINE__); + } + free (result); + gpgrt_fclose (stream); } -- cgit v1.2.3