fix printf precision handling

This commit is contained in:
Sergio R. Caprile committed 2024-09-20 20:40:54 -03:00
1 parent 19b06418c5
commit 3742da8770
3 files changed
+29 -14

No files matched your search

+8 -7
View File
@@ -1407,7 +1407,7 @@ static int xisnan(double x) {
0x7ff00000;
}
static size_t mg_dtoa(char *dst, size_t dstlen, double d, int width, bool tz) {
static size_t mg_dtoa(char *dst, size_t dstlen, double d, int pres, bool tz) {
char buf[40];
int i, s = 0, n = 0, e = 0;
double t, mul, saved;
@@ -1421,7 +1421,7 @@ static size_t mg_dtoa(char *dst, size_t dstlen, double d, int width, bool tz) {
mul = 1.0;
while (d >= 10.0 && d / mul >= 10.0) mul *= 10.0;
while (d <= 1.0 && d / mul <= 1.0) mul /= 10.0;
for (i = 0, t = mul * 5; i < width; i++) t /= 10.0;
for (i = 0, t = mul * 5; i < pres; i++) t /= 10.0;
d += t;
// Calculate exponent, and 'mul' for scientific representation
mul = 1.0;
@@ -1429,13 +1429,13 @@ static size_t mg_dtoa(char *dst, size_t dstlen, double d, int width, bool tz) {
while (d < 1.0 && d / mul < 1.0) mul /= 10.0, e--;
// printf(" --> %g %d %g %g\n", saved, e, t, mul);
if (e >= width && width > 1) {
n = (int) mg_dtoa(buf, sizeof(buf), saved / mul, width, tz);
if (tz && e >= pres && pres > 1) {
n = (int) mg_dtoa(buf, sizeof(buf), saved / mul, pres, true);
// printf(" --> %.*g %d [%.*s]\n", 10, d / t, e, n, buf);
n += addexp(buf + s + n, e, '+');
return mg_snprintf(dst, dstlen, "%.*s", n, buf);
} else if (e <= -width && width > 1) {
n = (int) mg_dtoa(buf, sizeof(buf), saved / mul, width, tz);
} else if (tz && e < 0 && e <= -pres + 1 && pres > 1) {
n = (int) mg_dtoa(buf, sizeof(buf), saved / mul, pres, true);
// printf(" --> %.*g %d [%.*s]\n", 10, d / mul, e, n, buf);
n += addexp(buf + s + n, -e, '-');
return mg_snprintf(dst, dstlen, "%.*s", n, buf);
@@ -1451,7 +1451,7 @@ static size_t mg_dtoa(char *dst, size_t dstlen, double d, int width, bool tz) {
while (t >= 1.0 && n + s < (int) sizeof(buf)) buf[n++] = '0', t /= 10.0;
if (s + n < (int) sizeof(buf)) buf[n + s++] = '.';
// printf(" 1--> [%g] -> [%.*s]\n", saved, s + n, buf);
for (i = 0, t = 0.1; s + n < (int) sizeof(buf) && n < width; i++) {
for (i = 0, t = 0.1; s + n < (int) sizeof(buf) && ((tz && (n + 0) < pres) || (!tz && i < pres)); i++) {
int ch = (int) (d / t);
buf[s + n++] = (char) (ch + '0');
d -= ch * t;
@@ -3241,6 +3241,7 @@ static void http_cb(struct mg_connection *c, int ev, void *ev_data) {
mg_call(c, MG_EV_HTTP_HDRS, &hm); // Got all HTTP headers
if (c->recv.len != old_len) {
// User manipulated received data. Wash our hands
MG_DEBUG(("%lu detaching HTTP handler", c->id));
c->pfn = NULL;
return;
}