finished initial pass on stb_sprintf.h

This commit is contained in:
ed
2025-02-09 10:42:26 -05:00
parent 5798526a54
commit e4f797f594
+104 -45
View File
@@ -1285,7 +1285,8 @@ STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(vsprintfcb)(STBSP_SPRINTFCB *callback,
dp = 0;
// clear tail, and clear leading if value is zero
tail[0] = 0;
if (n64 == 0) {
if (n64 == 0)
{
lead[0] = 0;
if (pr == 0) {
l = 0;
@@ -1294,13 +1295,18 @@ STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(vsprintfcb)(STBSP_SPRINTFCB *callback,
}
}
// convert to string
for (;;) {
for (;;)
{
* -- s = h[n64 & ((1 << (l >> 8)) - 1)];
n64 >>= (l >> 8);
if (!((n64) || ((stbsp__int32)((num + STBSP__NUMSZ) - s) < pr)))
break;
if (fl & STBSP__TRIPLET_COMMA) {
++l;
if ((l & 15) == ((l >> 4) & 15)) {
l &= ~15;
* -- s = stbsp__comma;
@@ -1367,38 +1373,45 @@ STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(vsprintfcb)(STBSP_SPRINTFCB *callback,
if (n64 >= 100000000) {
n = (stbsp__uint32)(n64 % 100000000);
n64 /= 100000000;
} else {
}
else {
n = (stbsp__uint32)n64;
n64 = 0;
}
if ((fl & STBSP__TRIPLET_COMMA) == 0) {
if ((fl & STBSP__TRIPLET_COMMA) == 0)
{
do {
s -= 2;
*(stbsp__uint16 *)s = *(stbsp__uint16 *)&stbsp__digitpair.pair[(n % 100) * 2];
n /= 100;
} while (n);
}
while (n) {
while (n);
}
while (n)
{
if ((fl & STBSP__TRIPLET_COMMA) && (l++ == 3)) {
l = 0;
* -- s = stbsp__comma;
-- o;
} else {
}
else {
* -- s = (char)(n % 10) + '0';
n /= 10;
}
}
if (n64 == 0) {
if (n64 == 0)
{
if ((s[0] == '0') && (s != (num + STBSP__NUMSZ)))
++ s;
break;
}
while (s != o)
if ((fl & STBSP__TRIPLET_COMMA) && (l++ == 3)) {
while (s != o) if ((fl & STBSP__TRIPLET_COMMA) && (l++ == 3))
{
l = 0;
* -- s = stbsp__comma;
-- o;
} else {
}
else {
* -- s = '0';
}
}
@@ -1420,19 +1433,24 @@ STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(vsprintfcb)(STBSP_SPRINTFCB *callback,
// get fw=leading/trailing space, pr=leading zeros
if (pr < (stbsp__int32)l)
pr = l;
n = pr + lead[0] + tail[0] + tz;
if (fw < (stbsp__int32)n)
fw = n;
fw -= n;
pr -= l;
// handle right justify and leading zeros
if ((fl & STBSP__LEFTJUST) == 0) {
if (fl & STBSP__LEADINGZERO) // if leading zeros, everything is in pr
if ((fl & STBSP__LEFTJUST) == 0)
{
if (fl & STBSP__LEADINGZERO) {
// if leading zeros, everything is in pr
pr = (fw > pr) ? fw : pr;
fw = 0;
} else {
}
else {
fl &= ~STBSP__TRIPLET_COMMA; // if no leading zeros, then no commas
}
}
@@ -1444,8 +1462,8 @@ STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(vsprintfcb)(STBSP_SPRINTFCB *callback,
stbsp__uint32 c;
// copy leading spaces (or when doing %8.4d stuff)
if ((fl & STBSP__LEFTJUST) == 0)
while (fw > 0) {
if ((fl & STBSP__LEFTJUST) == 0) while (fw > 0)
{
stbsp__cb_buf_clamp(i, fw);
fw -= i;
while (i) {
@@ -1468,7 +1486,9 @@ STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(vsprintfcb)(STBSP_SPRINTFCB *callback,
// copy leader
sn = lead + 1;
while (lead[0]) {
while (lead[0])
{
stbsp__cb_buf_clamp(i, lead[0]);
lead[0] -= (char)i;
while (i) {
@@ -1482,11 +1502,15 @@ STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(vsprintfcb)(STBSP_SPRINTFCB *callback,
c = cs >> 24;
cs &= 0xffffff;
cs = (fl & STBSP__TRIPLET_COMMA) ? ((stbsp__uint32)(c - ((pr + cs) % (c + 1)))) : 0;
while (pr > 0) {
while (pr > 0)
{
stbsp__cb_buf_clamp(i, pr);
pr -= i;
if ((fl & STBSP__TRIPLET_COMMA) == 0) {
while (i) {
if ((fl & STBSP__TRIPLET_COMMA) == 0)
{
while (i)
{
if ((((stbsp__uintptr)bf) & 3) == 0)
break;
*bf ++ = '0';
@@ -1498,11 +1522,13 @@ STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(vsprintfcb)(STBSP_SPRINTFCB *callback,
i -= 4;
}
}
while (i) {
while (i)
{
if ((fl & STBSP__TRIPLET_COMMA) && (cs++ == c)) {
cs = 0;
*bf ++ = stbsp__comma;
} else
}
else
*bf ++ = '0';
-- i;
}
@@ -1512,7 +1538,8 @@ STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(vsprintfcb)(STBSP_SPRINTFCB *callback,
// copy leader if there is still one
sn = lead + 1;
while (lead[0]) {
while (lead[0])
{
stbsp__int32 i;
stbsp__cb_buf_clamp(i, lead[0]);
lead[0] -= (char)i;
@@ -1684,16 +1711,20 @@ static char *stbsp__clamp_callback(const char *buf, void *user, int len)
if (len > c->count)
len = c->count;
if (len) {
if (buf != c->buf) {
const char *s, *se;
if (len)
{
if (buf != c->buf)
{
const char* s;
const char* se;
char* d;
d = c->buf;
s = buf;
se = buf + len;
do {
*d ++ = *s ++;
} while (s < se);
}
while (s < se);
}
c->buf += len;
c->count -= len;
@@ -1701,6 +1732,7 @@ static char *stbsp__clamp_callback(const char *buf, void *user, int len)
if (c->count <= 0)
return c->tmp;
return (c->count >= STB_SPRINTF_MIN) ? c->buf : c->tmp; // go direct into buffer if you can
}
@@ -1720,7 +1752,6 @@ STBSP__PUBLICDEF int STB_SPRINTF_DECORATE( vsnprintf )( char * buf, int count, c
if ( (count == 0) && !buf )
{
c.length = 0;
STB_SPRINTF_DECORATE( vsprintfcb )( stbsp__count_clamp_callback, &c, c.tmp, fmt, va );
}
else
@@ -1730,7 +1761,6 @@ STBSP__PUBLICDEF int STB_SPRINTF_DECORATE( vsnprintf )( char * buf, int count, c
c.buf = buf;
c.count = count;
c.length = 0;
STB_SPRINTF_DECORATE( vsprintfcb )( stbsp__clamp_callback, &c, stbsp__clamp_callback(0, &c, 0), fmt, va );
// zero-terminate
@@ -1739,7 +1769,6 @@ STBSP__PUBLICDEF int STB_SPRINTF_DECORATE( vsnprintf )( char * buf, int count, c
l = count - 1;
buf[l] = 0;
}
return c.length;
}
@@ -1907,29 +1936,36 @@ static stbsp__uint64 const stbsp__powten[20] = {
oh = s; \
}
#define stbsp__ddmultlo (oh, ol, xh, xl, yh, yl) ol = ol + (xh * yl + xl * yh);
#define stbsp__ddmultlos(oh, ol, xh, yl) ol = ol + (xh * yl);
#define stbsp__ddmultlo(oh, ol, xh, xl, yh, yl) ol = ol + (xh * yl + xl * yh)
#define stbsp__ddmultlos(oh, ol, xh, yl) ol = ol + (xh * yl)
static void stbsp__raise_to_power10(double* ohi, double* olo, double d, stbsp__int32 power) // power can be -323 to +350
{
double ph, pl;
if ((power >= 0) && (power <= 22)) {
stbsp__ddmulthi(ph, pl, d, stbsp__bot[power]);
} else {
}
else
{
stbsp__int32 e, et, eb;
double p2h, p2l;
e = power;
if (power < 0)
e = -e;
et = (e * 0x2c9) >> 14; /* %23 */
if (et > 13)
et = 13;
eb = e - (et * 23);
ph = d;
pl = 0.0;
if (power < 0) {
if (power < 0)
{
if (eb) {
-- eb;
stbsp__ddmulthi(ph, pl, d, stbsp__negbot[eb]);
@@ -1943,8 +1979,11 @@ static void stbsp__raise_to_power10(double *ohi, double *olo, double d, stbsp__i
ph = p2h;
pl = p2l;
}
} else {
if (eb) {
}
else
{
if (eb)
{
e = eb;
if (eb > 22)
eb = 22;
@@ -1958,7 +1997,8 @@ static void stbsp__raise_to_power10(double *ohi, double *olo, double d, stbsp__i
pl = p2l;
}
}
if (et) {
if (et)
{
stbsp__ddrenorm(ph, pl);
-- et;
stbsp__ddmulthi(p2h, p2l, ph, stbsp__top[et]);
@@ -1985,13 +2025,14 @@ static stbsp__int32 stbsp__real_to_str(char const **start, stbsp__uint32 *len, c
d = value;
STBSP__COPYFP(bits, d);
expo = (stbsp__int32)((bits >> 52) & 2047);
ng = (stbsp__int32)((stbsp__uint64) bits >> 63);
if (ng)
d = -d;
if (expo == 2047) // is nan or inf?
{
if (expo == 2047) { // is nan or inf?
*start = (bits & ((((stbsp__uint64)1) << 52) - 1)) ? "NaN" : "Inf";
*decimal_pos = STBSP__SPECIAL;
*len = 3;
@@ -2039,32 +2080,42 @@ static stbsp__int32 stbsp__real_to_str(char const **start, stbsp__uint32 *len, c
// now do the rounding in integer land
frac_digits = (frac_digits & 0x80000000) ? ((frac_digits & 0x7ffffff) + 1) : (tens + frac_digits);
if ((frac_digits < 24)) {
if ((frac_digits < 24))
{
stbsp__uint32 dg = 1;
if ((stbsp__uint64)bits >= stbsp__powten[9])
dg = 10;
while ((stbsp__uint64)bits >= stbsp__powten[dg]) {
++ dg;
if (dg == 20)
goto noround;
}
if (frac_digits < dg) {
if (frac_digits < dg)
{
stbsp__uint64 r;
// add 0.5 at the right position and round
e = dg - frac_digits;
if ((stbsp__uint32)e >= 24)
goto noround;
r = stbsp__powten[e];
bits = bits + (r / 2);
if ((stbsp__uint64)bits >= stbsp__powten[dg])
++ tens;
bits /= r;
}
noround:;
}
// kill long trailing runs of zeros
if (bits) {
if (bits)
{
stbsp__uint32 n;
for (;;) {
if (bits <= 0xffffffff)
@@ -2073,19 +2124,23 @@ static stbsp__int32 stbsp__real_to_str(char const **start, stbsp__uint32 *len, c
goto donez;
bits /= 1000;
}
n = (stbsp__uint32)bits;
while ((n % 1000) == 0)
n /= 1000;
bits = n;
donez:;
}
// convert to string
out += 64;
e = 0;
for (;;) {
for (;;)
{
stbsp__uint32 n;
char* o = out - 8;
// do the conversion in chunks of U32s (avoid most 64-bit divides, worth it, constant denomiators be damned)
if (bits >= 100000000) {
n = (stbsp__uint32)(bits % 100000000);
@@ -2094,20 +2149,24 @@ static stbsp__int32 stbsp__real_to_str(char const **start, stbsp__uint32 *len, c
n = (stbsp__uint32)bits;
bits = 0;
}
while (n) {
out -= 2;
*(stbsp__uint16 *)out = *(stbsp__uint16 *)&stbsp__digitpair.pair[(n % 100) * 2];
n /= 100;
e += 2;
}
if (bits == 0) {
if (bits == 0)
{
if ((e) && (out[0] == '0')) {
++ out;
-- e;
}
break;
}
while (out != o) {
while (out != o)
{
* -- out = '0';
++ e;
}