bdk: sprintf: allow padding > 9

This commit is contained in:
CTCaer 2024-02-16 16:01:54 +02:00
parent abeafb9a67
commit 6d69ef3cf6

View file

@ -39,7 +39,7 @@ static void _s_putn(u32 v, int base, char fill, int fcnt)
static const char digits[] = "0123456789ABCDEF"; static const char digits[] = "0123456789ABCDEF";
char *p; char *p;
char buf[65]; char buf[65]; // Number char size + leftover for padding.
int c = fcnt; int c = fcnt;
bool negative = false; bool negative = false;
@ -93,22 +93,36 @@ void s_printf(char *out_buf, const char *fmt, ...)
fmt++; fmt++;
fill = 0; fill = 0;
fcnt = 0; fcnt = 0;
// Check for padding. Number or space based.
if ((*fmt >= '0' && *fmt <= '9') || *fmt == ' ') if ((*fmt >= '0' && *fmt <= '9') || *fmt == ' ')
{ {
fcnt = *fmt; fcnt = *fmt; // Padding size or padding type.
fmt++; fmt++;
if (*fmt >= '0' && *fmt <= '9') if (*fmt >= '0' && *fmt <= '9')
{ {
// Padding size exists. Previous char was type.
fill = fcnt; fill = fcnt;
fcnt = *fmt - '0'; fcnt = *fmt - '0';
fmt++; fmt++;
parse_padding_dec:
// Parse padding size extra digits.
if (*fmt >= '0' && *fmt <= '9')
{
fcnt = fcnt * 10 + *fmt - '0';
fmt++;
goto parse_padding_dec;
}
} }
else else
{ {
// No padding type, use space. (Max padding size is 9).
fill = ' '; fill = ' ';
fcnt -= '0'; fcnt -= '0';
} }
} }
switch (*fmt) switch (*fmt)
{ {
case 'c': case 'c':
@ -116,23 +130,29 @@ void s_printf(char *out_buf, const char *fmt, ...)
if (c != '\0') if (c != '\0')
_s_putc(c); _s_putc(c);
break; break;
case 's': case 's':
_s_puts(va_arg(ap, char *)); _s_puts(va_arg(ap, char *));
break; break;
case 'd': case 'd':
_s_putn(va_arg(ap, u32), 10, fill, fcnt); _s_putn(va_arg(ap, u32), 10, fill, fcnt);
break; break;
case 'p': case 'p':
case 'P': case 'P':
case 'x': case 'x':
case 'X': case 'X':
_s_putn(va_arg(ap, u32), 16, fill, fcnt); _s_putn(va_arg(ap, u32), 16, fill, fcnt);
break; break;
case '%': case '%':
_s_putc('%'); _s_putc('%');
break; break;
case '\0': case '\0':
goto out; goto out;
default: default:
_s_putc('%'); _s_putc('%');
_s_putc(*fmt); _s_putc(*fmt);
@ -162,44 +182,66 @@ void s_vprintf(char *out_buf, const char *fmt, va_list ap)
fmt++; fmt++;
fill = 0; fill = 0;
fcnt = 0; fcnt = 0;
// Check for padding. Number or space based.
if ((*fmt >= '0' && *fmt <= '9') || *fmt == ' ') if ((*fmt >= '0' && *fmt <= '9') || *fmt == ' ')
{ {
fcnt = *fmt; fcnt = *fmt; // Padding size or padding type.
fmt++; fmt++;
if (*fmt >= '0' && *fmt <= '9') if (*fmt >= '0' && *fmt <= '9')
{ {
// Padding size exists. Previous char was type.
fill = fcnt; fill = fcnt;
fcnt = *fmt - '0'; fcnt = *fmt - '0';
fmt++; fmt++;
parse_padding_dec:
// Parse padding size extra digits.
if (*fmt >= '0' && *fmt <= '9')
{
fcnt = fcnt * 10 + *fmt - '0';
fmt++;
goto parse_padding_dec;
}
} }
else else
{ {
// No padding type, use space. (Max padding size is 9).
fill = ' '; fill = ' ';
fcnt -= '0'; fcnt -= '0';
} }
} }
switch(*fmt)
switch (*fmt)
{ {
case 'c': case 'c':
_s_putc(va_arg(ap, u32)); char c = va_arg(ap, u32);
if (c != '\0')
_s_putc(c);
break; break;
case 's': case 's':
_s_puts(va_arg(ap, char *)); _s_puts(va_arg(ap, char *));
break; break;
case 'd': case 'd':
_s_putn(va_arg(ap, u32), 10, fill, fcnt); _s_putn(va_arg(ap, u32), 10, fill, fcnt);
break; break;
case 'p': case 'p':
case 'P': case 'P':
case 'x': case 'x':
case 'X': case 'X':
_s_putn(va_arg(ap, u32), 16, fill, fcnt); _s_putn(va_arg(ap, u32), 16, fill, fcnt);
break; break;
case '%': case '%':
_s_putc('%'); _s_putc('%');
break; break;
case '\0': case '\0':
goto out; goto out;
default: default:
_s_putc('%'); _s_putc('%');
_s_putc(*fmt); _s_putc(*fmt);