bdk: sprintf: add negative number support for %d

This will now force a number as negative if bit31 is set and properly create the relevant string.

That means that external handling in order to show sign is now not needed.
This commit is contained in:
CTCaer 2022-05-08 05:26:01 +03:00
parent 81730c5f7e
commit ebf0db77ee

View file

@ -40,10 +40,19 @@ static void _s_putn(u32 v, int base, char fill, int fcnt)
static const char digits[] = "0123456789ABCDEFghijklmnopqrstuvwxyz"; static const char digits[] = "0123456789ABCDEFghijklmnopqrstuvwxyz";
char *p; char *p;
int c = fcnt; int c = fcnt;
bool negative = false;
if (base > 36) if (base > 36)
return; return;
// Account for negative numbers.
if (base == 10 && v & 0x80000000)
{
negative = true;
v = (int)v * -1;
c--;
}
p = buf + 64; p = buf + 64;
*p = 0; *p = 0;
do do
@ -53,9 +62,12 @@ static void _s_putn(u32 v, int base, char fill, int fcnt)
v /= base; v /= base;
} while (v); } while (v);
if (negative)
*--p = '-';
if (fill != 0) if (fill != 0)
{ {
while (c > 0) while (c > 0 && p > buf)
{ {
*--p = fill; *--p = fill;
c--; c--;