mirror of
https://github.com/cesanta/mongoose.git
synced 2026-11-08 16:11:18 +00:00
Fix #2104 - %.*f formatting
This commit is contained in:
1 parent
f79edeb053
commit
6b29a2a724
3 files changed
+44
-36
No files matched your search
@@ -2,9 +2,7 @@
|
||||
#include "iobuf.h"
|
||||
#include "util.h"
|
||||
|
||||
static bool is_digit(int c) {
|
||||
return c >= '0' && c <= '9';
|
||||
}
|
||||
static bool is_digit(int c) { return c >= '0' && c <= '9'; }
|
||||
|
||||
static int addexp(char *buf, int e, int sign) {
|
||||
int n = 0;
|
||||
@@ -37,7 +35,7 @@ static int xisnan(double x) {
|
||||
0x7ff00000;
|
||||
}
|
||||
|
||||
static size_t mg_dtoa(char *dst, size_t dstlen, double d, int width) {
|
||||
static size_t mg_dtoa(char *dst, size_t dstlen, double d, int width, bool tz) {
|
||||
char buf[40];
|
||||
int i, s = 0, n = 0, e = 0;
|
||||
double t, mul, saved;
|
||||
@@ -59,13 +57,13 @@ static size_t mg_dtoa(char *dst, size_t dstlen, double d, int width) {
|
||||
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) {
|
||||
n = (int) mg_dtoa(buf, sizeof(buf), saved / mul, width);
|
||||
if (e >= width && width > 1) {
|
||||
n = (int) mg_dtoa(buf, sizeof(buf), saved / mul, width, tz);
|
||||
// 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) {
|
||||
n = (int) mg_dtoa(buf, sizeof(buf), saved / mul, width);
|
||||
} else if (e <= -width && width > 1) {
|
||||
n = (int) mg_dtoa(buf, sizeof(buf), saved / mul, width, tz);
|
||||
// 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);
|
||||
@@ -88,8 +86,8 @@ static size_t mg_dtoa(char *dst, size_t dstlen, double d, int width) {
|
||||
t /= 10.0;
|
||||
}
|
||||
}
|
||||
while (n > 0 && buf[s + n - 1] == '0') n--; // Trim trailing zeros
|
||||
if (n > 0 && buf[s + n - 1] == '.') n--; // Trim trailing dot
|
||||
while (tz && n > 0 && buf[s + n - 1] == '0') n--; // Trim trailing zeroes
|
||||
if (n > 0 && buf[s + n - 1] == '.') n--; // Trim trailing dot
|
||||
n += s;
|
||||
if (n >= (int) sizeof(buf)) n = (int) sizeof(buf) - 1;
|
||||
buf[n] = '\0';
|
||||
@@ -133,9 +131,7 @@ static char mg_esc(int c, bool esc) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static char mg_escape(int c) {
|
||||
return mg_esc(c, true);
|
||||
}
|
||||
static char mg_escape(int c) { return mg_esc(c, true); }
|
||||
|
||||
static size_t qcpy(void (*out)(char, void *), void *ptr, char *buf,
|
||||
size_t len) {
|
||||
@@ -222,7 +218,7 @@ size_t mg_vxprintf(void (*out)(char, void *), void *param, const char *fmt,
|
||||
if (c == 'g' || c == 'f') {
|
||||
double v = va_arg(*ap, double);
|
||||
if (pr == ~0U) pr = 6;
|
||||
k = mg_dtoa(tmp, sizeof(tmp), v, (int) pr);
|
||||
k = mg_dtoa(tmp, sizeof(tmp), v, (int) pr, c == 'g');
|
||||
} else if (is_long == 2) {
|
||||
int64_t v = va_arg(*ap, int64_t);
|
||||
k = mg_lld(tmp, v, s, h);
|
||||
|
||||
Reference in new issue
Block a user