|
Message-Id: <eea930bc948c6f70a75d10ddc1b53730d4496f0b.1681516116.git.gabravier@gmail.com> Date: Sat, 15 Apr 2023 14:28:28 +0200 From: Gabriel Ravier <gabravier@...il.com> To: musl@...ts.openwall.com Cc: Gabriel Ravier <gabravier@...il.com> Subject: [PATCH v2 1/1] vfprintf: support C2x %b and %B conversion specifiers These specifiers allow for formatted input/output of binary integers, and have been added to C2x with N2630. The uppercase B specifier is not formally required by C2x, as only lowercase specifiers were reserved by C, and thus an implementation could have been using uppercase B for an extension of their own, but C2x still has a note saying that it is recommended practice to implement it as the logical counterpart to b. --- src/stdio/vfprintf.c | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/stdio/vfprintf.c b/src/stdio/vfprintf.c index a712d80f..3d7e0aeb 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('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('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('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('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('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('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('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; @@ -437,7 +448,12 @@ static int printf_core(FILE *f, const char *fmt, va_list *ap, union arg *nl_arg, unsigned st, ps; int cnt=0, l=0; size_t i; - char buf[sizeof(uintmax_t)*3+3+LDBL_MANT_DIG/4]; + /* This buffer is used for integer conversions. As such, it needs + * to be able to contain the full representation of a number in base 2, + * 8, 10 or 16, with base 2 having the largest possible requirement of + * as many characters as the amount of bits in the largest possible + * integer type */ + char buf[sizeof(uintmax_t)*CHAR_BIT]; const char *prefix; int t, pl; wchar_t wc[2], *ws; @@ -534,7 +550,7 @@ static int printf_core(FILE *f, const char *fmt, va_list *ap, union arg *nl_arg, if (ferror(f)) return -1; z = buf + sizeof(buf); - prefix = "-+ 0X0x"; + prefix = "-+ 0X0x0B0b"; pl = 0; t = s[-1]; @@ -564,6 +580,10 @@ static int printf_core(FILE *f, const char *fmt, va_list *ap, union arg *nl_arg, a = fmt_x(arg.i, z, t&32); if (arg.i && (fl & ALT_FORM)) prefix+=(t>>4), pl=2; if (0) { + case 'b': case 'B': + a = fmt_b(arg.i, z); + if (arg.i && (fl & ALT_FORM)) prefix+=9+((t=='b')<<1), pl=2; + } if (0) { case 'o': a = fmt_o(arg.i, z); if ((fl&ALT_FORM) && p<z-a+1) p=z-a+1; -- 2.39.2
Powered by blists - more mailing lists
Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.