mirror of
https://gitlab.nic.cz/labs/bird.git
synced 2025-03-21 22:07:03 +00:00
Lib: Extend bsnprintf() for byte strings
Add support for %Xb directive to print fixed-length byte strings, formatted as hexadecimal sequences separated by colon.
This commit is contained in:
parent
f40e2bc270
commit
32d6855a3f
18
lib/printf.c
18
lib/printf.c
@ -214,6 +214,24 @@ int bvsnprintf(char *buf, int size, const char *fmt, va_list args)
|
|||||||
if (field_width > size)
|
if (field_width > size)
|
||||||
return -1;
|
return -1;
|
||||||
switch (*fmt) {
|
switch (*fmt) {
|
||||||
|
case 'b': {
|
||||||
|
const char *digits="0123456789abcdef";
|
||||||
|
const byte *bs = va_arg(args, const byte *);
|
||||||
|
len = field_width;
|
||||||
|
|
||||||
|
if (3*len > size)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
for (i = 0; i < len; i++) {
|
||||||
|
const byte b = *bs++;
|
||||||
|
*str++ = digits[b >> 4];
|
||||||
|
*str++ = digits[b & 0xf];
|
||||||
|
*str++ = ':';
|
||||||
|
}
|
||||||
|
|
||||||
|
str -= !!i;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
case 'c':
|
case 'c':
|
||||||
if (!(flags & LEFT))
|
if (!(flags & LEFT))
|
||||||
while (--field_width > 0)
|
while (--field_width > 0)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user