mailing list of musl libc
 help / color / mirror / code / Atom feed
* [musl] musl patch for c2x %b printf/scanf
@ 2023-03-03 10:19 Andreas Dixius
  2023-03-03 11:02 ` Jₑₙₛ Gustedt
  0 siblings, 1 reply; 5+ messages in thread
From: Andreas Dixius @ 2023-03-03 10:19 UTC (permalink / raw)
  To: musl


[-- Attachment #1.1.1: Type: text/plain, Size: 458 bytes --]

(no mailing list member, so please CC in case of replies I should read)

Hello,

as quite useful to us (a lot of binary value printouts), I added some 
(to the best of my knowledge) patch to musl for c2x-style %b 
printf/scanf formatting. In case you find these useful, feel free to 
integrate/adapt into musl.

Both patches (attached) should be applicable to the current git master 
branch of musl (as of writing this mail).

Regards,
Andreas

[-- Attachment #1.1.2: 0001-add-C2X-b-printf-format.patch --]
[-- Type: text/x-patch, Size: 3240 bytes --]

From 34f4035c45bdeb9b60be4920ce610a2fbf2f8b29 Mon Sep 17 00:00:00 2001
From: Andreas <andreas.dixius@posteo.de>
Date: Wed, 3 Aug 2022 16:41:15 +0200
Subject: [PATCH 1/2] add C2X %b printf format

---
 src/stdio/vfprintf.c | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)

diff --git a/src/stdio/vfprintf.c b/src/stdio/vfprintf.c
index 45557951..93fac121 100644
--- a/src/stdio/vfprintf.c
+++ b/src/stdio/vfprintf.c
@@ -49,7 +49,7 @@ enum {
 static const unsigned char states[]['z'-'A'+1] = {
 	{ /* 0: bare types */
 		S('d') = INT, S('i') = INT,
-		S('o') = UINT, S('u') = UINT, S('x') = UINT, S('X') = UINT,
+		S('o') = UINT, S('u') = UINT, S('x') = UINT, S('X') = UINT, S('b') = UINT,
 		S('e') = DBL, S('f') = DBL, S('g') = DBL, S('a') = DBL,
 		S('E') = DBL, S('F') = DBL, S('G') = DBL, S('A') = DBL,
 		S('c') = CHAR, S('C') = INT,
@@ -59,7 +59,7 @@ static const unsigned char states[]['z'-'A'+1] = {
 		S('z') = ZTPRE, S('j') = JPRE, S('t') = ZTPRE,
 	}, { /* 1: l-prefixed */
 		S('d') = LONG, S('i') = LONG,
-		S('o') = ULONG, S('u') = ULONG, S('x') = ULONG, S('X') = ULONG,
+		S('o') = ULONG, S('u') = ULONG, S('x') = ULONG, S('X') = ULONG, S('b') = ULONG,
 		S('e') = DBL, S('f') = DBL, S('g') = DBL, S('a') = DBL,
 		S('E') = DBL, S('F') = DBL, S('G') = DBL, S('A') = DBL,
 		S('c') = INT, S('s') = PTR, S('n') = PTR,
@@ -68,17 +68,20 @@ static const unsigned char states[]['z'-'A'+1] = {
 		S('d') = LLONG, S('i') = LLONG,
 		S('o') = ULLONG, S('u') = ULLONG,
 		S('x') = ULLONG, S('X') = ULLONG,
+		S('b') = ULLONG,
 		S('n') = PTR,
 	}, { /* 3: h-prefixed */
 		S('d') = SHORT, S('i') = SHORT,
 		S('o') = USHORT, S('u') = USHORT,
 		S('x') = USHORT, S('X') = USHORT,
+		S('b') = USHORT,
 		S('n') = PTR,
 		S('h') = HHPRE,
 	}, { /* 4: hh-prefixed */
 		S('d') = CHAR, S('i') = CHAR,
 		S('o') = UCHAR, S('u') = UCHAR,
 		S('x') = UCHAR, S('X') = UCHAR,
+		S('b') = UCHAR,
 		S('n') = PTR,
 	}, { /* 5: L-prefixed */
 		S('e') = LDBL, S('f') = LDBL, S('g') = LDBL, S('a') = LDBL,
@@ -88,11 +91,13 @@ static const unsigned char states[]['z'-'A'+1] = {
 		S('d') = PDIFF, S('i') = PDIFF,
 		S('o') = SIZET, S('u') = SIZET,
 		S('x') = SIZET, S('X') = SIZET,
+		S('b') = SIZET,
 		S('n') = PTR,
 	}, { /* 7: j-prefixed */
 		S('d') = IMAX, S('i') = IMAX,
 		S('o') = UMAX, S('u') = UMAX,
 		S('x') = UMAX, S('X') = UMAX,
+		S('b') = UMAX,
 		S('n') = PTR,
 	}
 };
@@ -162,6 +167,12 @@ static char *fmt_o(uintmax_t x, char *s)
 	return s;
 }
 
+static char *fmt_b(uintmax_t x, char *s)
+{
+	for (; x; x>>=1) *--s = '0' + (x&1);
+	return s;
+}
+
 static char *fmt_u(uintmax_t x, char *s)
 {
 	unsigned long y;
@@ -531,7 +542,7 @@ static int printf_core(FILE *f, const char *fmt, va_list *ap, union arg *nl_arg,
 		if (!f) continue;
 
 		z = buf + sizeof(buf);
-		prefix = "-+   0X0x";
+		prefix = "-+   0X0x0b";
 		pl = 0;
 		t = s[-1];
 
@@ -565,6 +576,10 @@ static int printf_core(FILE *f, const char *fmt, va_list *ap, union arg *nl_arg,
 			a = fmt_o(arg.i, z);
 			if ((fl&ALT_FORM) && p<z-a+1) p=z-a+1;
 			} if (0) {
+		case 'b':
+			a = fmt_b(arg.i, z);
+			if (arg.i && (fl & ALT_FORM)) prefix+=9, pl=2;
+			} if (0) {
 		case 'd': case 'i':
 			pl=1;
 			if (arg.i>INTMAX_MAX) {
-- 
2.39.2


[-- Attachment #1.1.3: 0002-add-C2X-b-scanf-format.patch --]
[-- Type: text/x-patch, Size: 1791 bytes --]

From 1953f6b70c4738c79cea76efda3853beb41bc52b Mon Sep 17 00:00:00 2001
From: Andreas <andreas.dixius@posteo.de>
Date: Thu, 4 Aug 2022 10:28:45 +0200
Subject: [PATCH 2/2] add C2X %b scanf format

---
 src/internal/intscan.c | 10 +++++++---
 src/stdio/vfscanf.c    |  5 ++++-
 2 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/src/internal/intscan.c b/src/internal/intscan.c
index a4a5ae86..74c13be0 100644
--- a/src/internal/intscan.c
+++ b/src/internal/intscan.c
@@ -40,15 +40,19 @@ unsigned long long __intscan(FILE *f, unsigned base, int pok, unsigned long long
 	}
 	if ((base == 0 || base == 16) && c=='0') {
 		c = shgetc(f);
-		if ((c|32)=='x') {
+		if (((c|32)=='x') || (c == 'b')) {
+            if (c == 'b') {
+                base = 2;
+            } else {
+                base = 16;
+            }
 			c = shgetc(f);
-			if (val[c]>=16) {
+			if (val[c]>=base) {
 				shunget(f);
 				if (pok) shunget(f);
 				else shlim(f, 0);
 				return 0;
 			}
-			base = 16;
 		} else if (base == 0) {
 			base = 8;
 		}
diff --git a/src/stdio/vfscanf.c b/src/stdio/vfscanf.c
index b78a374d..420f005a 100644
--- a/src/stdio/vfscanf.c
+++ b/src/stdio/vfscanf.c
@@ -151,7 +151,7 @@ int vfscanf(FILE *restrict f, const char *restrict fmt, va_list ap)
 			size = SIZE_L;
 			break;
 		case 'd': case 'i': case 'o': case 'u': case 'x':
-		case 'a': case 'e': case 'f': case 'g':
+		case 'a': case 'e': case 'f': case 'g': case 'b':
 		case 'A': case 'E': case 'F': case 'G': case 'X':
 		case 's': case 'c': case '[':
 		case 'S': case 'C':
@@ -286,6 +286,9 @@ int vfscanf(FILE *restrict f, const char *restrict fmt, va_list ap)
 		case 'o':
 			base = 8;
 			goto int_common;
+		case 'b':
+			base = 2;
+			goto int_common;
 		case 'd':
 		case 'u':
 			base = 10;
-- 
2.39.2


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 840 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-03-06 22:25 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-03 10:19 [musl] musl patch for c2x %b printf/scanf Andreas Dixius
2023-03-03 11:02 ` Jₑₙₛ Gustedt
2023-03-03 16:43   ` enh
2023-03-04 11:15     ` Jₑₙₛ Gustedt
2023-03-06 22:24       ` enh

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).