mirror of
https://github.com/CTCaer/hekate
synced 2024-12-22 11:21:23 +00:00
bdk: sprintf: add right padding support
This commit is contained in:
parent
77782b974c
commit
856994e4f4
2 changed files with 66 additions and 7 deletions
|
@ -28,10 +28,32 @@ static void _s_putc(char c)
|
||||||
*sout_buf += 1;
|
*sout_buf += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void _s_puts(char *s)
|
static void _s_putspace(int fcnt)
|
||||||
{
|
{
|
||||||
|
if (fcnt <= 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
for (int i = 0; i < fcnt; i++)
|
||||||
|
_s_putc(' ');
|
||||||
|
}
|
||||||
|
|
||||||
|
static void _s_puts(char *s, char fill, int fcnt)
|
||||||
|
{
|
||||||
|
if (fcnt)
|
||||||
|
{
|
||||||
|
fcnt = fcnt - strlen(s);
|
||||||
|
|
||||||
|
// Left padding. Check if padding is not space based (dot counts as such).
|
||||||
|
if (fill != '.')
|
||||||
|
_s_putspace(fcnt);
|
||||||
|
}
|
||||||
|
|
||||||
for (; *s; s++)
|
for (; *s; s++)
|
||||||
_s_putc(*s);
|
_s_putc(*s);
|
||||||
|
|
||||||
|
// Right padding. Check if padding is space based (dot counts as such).
|
||||||
|
if (fill == '.')
|
||||||
|
_s_putspace(fcnt);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void _s_putn(u32 v, int base, char fill, int fcnt)
|
static void _s_putn(u32 v, int base, char fill, int fcnt)
|
||||||
|
@ -75,9 +97,28 @@ static void _s_putn(u32 v, int base, char fill, int fcnt)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_s_puts(p);
|
_s_puts(p, 0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Padding:
|
||||||
|
* Numbers:
|
||||||
|
* %3d: Fill: ' ', Count: 3.
|
||||||
|
* % 3d: Fill: ' ', Count: 3.
|
||||||
|
* %.3d: Fill: '.', Count: 3.
|
||||||
|
* %23d: Fill: '2', Count: 3.
|
||||||
|
* % 23d: Fill: ' ', Count: 23.
|
||||||
|
* %223d: Fill: '2', Count: 23.
|
||||||
|
*
|
||||||
|
* Strings, Fill: ' ':
|
||||||
|
* %3s: Count: 5, Left.
|
||||||
|
* %23s: Count: 5, Left.
|
||||||
|
* %223s: Count: 25, Left.
|
||||||
|
* %.3s: Count: 5, Right.
|
||||||
|
* %.23s: Count: 25, Right.
|
||||||
|
* %.223s: Count: 225, Right.
|
||||||
|
*/
|
||||||
|
|
||||||
void s_printf(char *out_buf, const char *fmt, ...)
|
void s_printf(char *out_buf, const char *fmt, ...)
|
||||||
{
|
{
|
||||||
va_list ap;
|
va_list ap;
|
||||||
|
@ -94,8 +135,8 @@ void s_printf(char *out_buf, const char *fmt, ...)
|
||||||
fill = 0;
|
fill = 0;
|
||||||
fcnt = 0;
|
fcnt = 0;
|
||||||
|
|
||||||
// Check for padding. Number or space based.
|
// Check for padding. Number or space based (dot count as space for string).
|
||||||
if ((*fmt >= '0' && *fmt <= '9') || *fmt == ' ')
|
if ((*fmt >= '0' && *fmt <= '9') || *fmt == ' ' || *fmt == '.')
|
||||||
{
|
{
|
||||||
fcnt = *fmt; // Padding size or padding type.
|
fcnt = *fmt; // Padding size or padding type.
|
||||||
fmt++;
|
fmt++;
|
||||||
|
@ -132,7 +173,7 @@ parse_padding_dec:
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 's':
|
case 's':
|
||||||
_s_puts(va_arg(ap, char *));
|
_s_puts(va_arg(ap, char *), fill, fcnt);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'd':
|
case 'd':
|
||||||
|
@ -221,7 +262,7 @@ parse_padding_dec:
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 's':
|
case 's':
|
||||||
_s_puts(va_arg(ap, char *));
|
_s_puts(va_arg(ap, char *), fill, fcnt);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'd':
|
case 'd':
|
||||||
|
|
|
@ -21,6 +21,24 @@
|
||||||
|
|
||||||
#include <utils/types.h>
|
#include <utils/types.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Padding:
|
||||||
|
* Numbers:
|
||||||
|
* %3d: Fill: ' ', Count: 3.
|
||||||
|
* % 3d: Fill: ' ', Count: 3.
|
||||||
|
* %23d: Fill: '2', Count: 3.
|
||||||
|
* % 23d: Fill: ' ', Count: 23.
|
||||||
|
* %223d: Fill: '2', Count: 23.
|
||||||
|
*
|
||||||
|
* Strings, Fill: ' ':
|
||||||
|
* %3s: Count: 5, Left.
|
||||||
|
* %23s: Count: 5, Left.
|
||||||
|
* %223s: Count: 25, Left.
|
||||||
|
* %.3s: Count: 5, Right.
|
||||||
|
* %.23s: Count: 25, Right.
|
||||||
|
* %.223s: Count: 225, Right.
|
||||||
|
*/
|
||||||
|
|
||||||
void s_printf(char *out_buf, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
|
void s_printf(char *out_buf, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
|
||||||
void s_vprintf(char *out_buf, const char *fmt, va_list ap);
|
void s_vprintf(char *out_buf, const char *fmt, va_list ap);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue