Add %g support to mg_snprintf()

This commit is contained in:
Sergey Lyubka 2022-06-10 09:38:36 +01:00
parent 106662b1d7
commit 33567bab41
6 changed files with 400 additions and 63 deletions

View File

@ -2469,34 +2469,6 @@ char *buf[sizeof(data)/2];
unsigned long val = mg_unhex(data, sizeof(data) - 1); // val is now 123 unsigned long val = mg_unhex(data, sizeof(data) - 1); // val is now 123
``` ```
### mg\_asprintf(), mg\_vasprintf()
```c
int mg_asprintf(char **buf, size_t size, const char *fmt, ...);
int mg_vasprintf(char **buf, size_t size, const char *fmt, va_list ap);
```
Print message specified by printf-like format string `fmt` into a buffer
pointed by `buf` of size `size`. If `size` is large enough to hold the whole
message, then a message is stored in `*buf`. If it does not fit, then a large
enough buffer is allocated to hold a message, and `buf` is changed to point to
that buffer.
Parameters:
- `buf` - Pointer to pointer to output buffer
- `size` - Pre-allocated buffer size
- `fmt` - printf-like format string
Return value: Number of bytes printed
Usage example:
```c
char buf[1024], *pbuf = &buf;
mg_asprintf(&pbuf, sizeof(buf), "Hello, %s!", "world"); // buf is now "Hello, world!"
```
### mg\_snprintf(), mg\_vsnprintf() ### mg\_snprintf(), mg\_vsnprintf()
```c ```c
size_t mg_snprintf(char *buf, size_t len, const char *fmt, ...); size_t mg_snprintf(char *buf, size_t len, const char *fmt, ...);
@ -2521,10 +2493,12 @@ Supported format specifiers:
- `hhd`, `hd`, `d`, `ld`, `lld` - for `char`, `short`, `int`, `long`, `int64_t` - `hhd`, `hd`, `d`, `ld`, `lld` - for `char`, `short`, `int`, `long`, `int64_t`
- `hhu`, `hu`, `u`, `lu`, `llu` - same but for unsigned variants - `hhu`, `hu`, `u`, `lu`, `llu` - same but for unsigned variants
- `hhx`, `hx`, `x`, `lx`, `llx` - same, unsigned and hex output - `hhx`, `hx`, `x`, `lx`, `llx` - same, unsigned and hex output
- `s` - `for char *` - `s` - for `char *`
- `Q` - `for char *`, outputs double-quoted JSON-escaped string (extension) - `Q` - for `char *`, outputs double-quoted JSON-escaped string (extension)
- `c` - `for char` - `M` - for `size_t (*)(char *, size_t, va_list *)`, calls another print function (extension)
- `%` - `the `%` character itself - `g`, `f` - for `double`
- `c` - for `char`
- `%` - the `%` character itself
- `p` - for any pointer, prints `0x.....` hex value - `p` - for any pointer, prints `0x.....` hex value
- `%X.Y` - optional width and precision modifiers - `%X.Y` - optional width and precision modifiers
- `%.*` - optional precision modifier specified as `int` argument - `%.*` - optional precision modifier specified as `int` argument
@ -2539,6 +2513,64 @@ mg_snprintf(buf, sizeof(buf), "%.2s", "abcdef"); // ab
mg_snprintf(buf, sizeof(buf), "%.*s", 2, "abcdef"); // ab mg_snprintf(buf, sizeof(buf), "%.*s", 2, "abcdef"); // ab
mg_snprintf(buf, sizeof(buf), "%05x", 123); // 00123 mg_snprintf(buf, sizeof(buf), "%05x", 123); // 00123
mg_snprintf(buf, sizeof(buf), "%%-%3s", "a"); // %- a mg_snprintf(buf, sizeof(buf), "%%-%3s", "a"); // %- a
mg_snprintf(buf, sizeof(buf), "hi, %Q", "a"); // hi, "a"
mg_snprintf(buf, sizeof(buf), "r: %M, %d", f,1,2,7); // r: 3, 7
// Printing sub-function for %M specifier. Grabs two int parameters
size_t f(char *buf, size_t len, va_list *ap) {
int a = va_arg(*ap, int);
int a = va_arg(*ap, int);
return mg_snprintf(buf, len, "%d", a + b);
}
```
### mg\_asprintf(), mg\_vasprintf()
```c
int mg_asprintf(char **buf, size_t size, const char *fmt, ...);
int mg_vasprintf(char **buf, size_t size, const char *fmt, va_list ap);
```
Print message specified by printf-like format string `fmt` into a buffer
pointed by `buf` of size `size`. If `size` is large enough to hold the whole
message, then a message is stored in `*buf`. If it does not fit, then a large
enough buffer is allocated to hold a message, and `buf` is changed to point to
that buffer.
Parameters:
- `buf` - Pointer to pointer to output buffer
- `size` - Pre-allocated buffer size
- `fmt` - printf-like format string
Return value: Number of bytes printed
Usage example:
```c
char buf[16], *pbuf = buf;
mg_asprintf(&pbuf, sizeof(buf), "Hello, %s!", "world"); // buf is now "Hello, world!"
if (pbuf != buf) free(pbuf);
```
### mg\_mprintf(), mg\_vmprintf()
```c
char *mg_mprintf(const char *fmt, ...);
char *mg_vmprintf(const char *fmt, va_list ap);
```
Print message into an allocated memory buffer. Caller must free the result.
Parameters:
- `fmt` - printf-like format string
Return value: allocated memory buffer
Usage example:
```c
char *msg = mg_mprintf("Double quoted string: %Q!", "hi");
free(msg);
``` ```
### mg\_to64() ### mg\_to64()

View File

@ -5066,7 +5066,7 @@ size_t mg_snprintf(char *buf, size_t len, const char *fmt, ...) {
va_list ap; va_list ap;
size_t n; size_t n;
va_start(ap, fmt); va_start(ap, fmt);
n = mg_vsnprintf(buf, len, fmt, ap); n = mg_vsnprintf(buf, len, fmt, &ap);
va_end(ap); va_end(ap);
return n; return n;
} }
@ -5107,7 +5107,7 @@ size_t mg_vasprintf(char **buf, size_t size, const char *fmt, va_list ap) {
size_t len; size_t len;
va_copy(ap_copy, ap); va_copy(ap_copy, ap);
len = mg_vsnprintf(*buf, size, fmt, ap_copy); len = mg_vsnprintf(*buf, size, fmt, &ap_copy);
va_end(ap_copy); va_end(ap_copy);
if (len >= size) { if (len >= size) {
@ -5116,7 +5116,7 @@ size_t mg_vasprintf(char **buf, size_t size, const char *fmt, va_list ap) {
len = 0; len = 0;
} else { } else {
va_copy(ap_copy, ap); va_copy(ap_copy, ap);
len = mg_vsnprintf(*buf, len + 1, fmt, ap_copy); len = mg_vsnprintf(*buf, len + 1, fmt, &ap_copy);
va_end(ap_copy); va_end(ap_copy);
} }
} }
@ -5133,6 +5133,21 @@ size_t mg_asprintf(char **buf, size_t size, const char *fmt, ...) {
return ret; return ret;
} }
char *mg_vmprintf(const char *fmt, va_list ap) {
char *s = NULL;
mg_vasprintf(&s, 0, fmt, ap);
return s;
}
char *mg_mprintf(const char *fmt, ...) {
char *s = NULL;
va_list ap;
va_start(ap, fmt);
mg_vasprintf(&s, 0, fmt, ap);
va_end(ap);
return s;
}
uint64_t mg_tou64(struct mg_str str) { uint64_t mg_tou64(struct mg_str str) {
uint64_t result = 0; uint64_t result = 0;
size_t i = 0; size_t i = 0;
@ -5218,7 +5233,9 @@ static size_t mg_copyq(char *buf, size_t len, size_t n, char *p, size_t k) {
return j + extra; return j + extra;
} }
size_t mg_vsnprintf(char *buf, size_t len, const char *fmt, va_list ap) { typedef size_t (*mg_spfn_t)(char *, size_t, va_list *);
size_t mg_vsnprintf(char *buf, size_t len, const char *fmt, va_list *ap) {
size_t i = 0, n = 0; size_t i = 0, n = 0;
while (fmt[i] != '\0') { while (fmt[i] != '\0') {
if (fmt[i] == '%') { if (fmt[i] == '%') {
@ -5231,7 +5248,7 @@ size_t mg_vsnprintf(char *buf, size_t len, const char *fmt, va_list ap) {
if (c == '.') { if (c == '.') {
c = fmt[++i]; c = fmt[++i];
if (c == '*') { if (c == '*') {
pr = (size_t) va_arg(ap, int); pr = (size_t) va_arg(*ap, int);
c = fmt[++i]; c = fmt[++i];
} else { } else {
pr = 0; pr = 0;
@ -5244,18 +5261,23 @@ size_t mg_vsnprintf(char *buf, size_t len, const char *fmt, va_list ap) {
if (c == 'l') is_long++, c = fmt[++i]; if (c == 'l') is_long++, c = fmt[++i];
} }
if (c == 'p') x = 1, is_long = 1; if (c == 'p') x = 1, is_long = 1;
if (c == 'd' || c == 'u' || c == 'x' || c == 'X' || c == 'p') { if (c == 'd' || c == 'u' || c == 'x' || c == 'X' || c == 'p' ||
c == 'g') {
bool s = (c == 'd'), h = (c == 'x' || c == 'X' || c == 'p'); bool s = (c == 'd'), h = (c == 'x' || c == 'X' || c == 'p');
char tmp[30]; char tmp[40];
size_t xl = x ? 2 : 0; size_t xl = x ? 2 : 0;
if (is_long == 2) { if (c == 'g' || c == 'f') {
int64_t v = va_arg(ap, int64_t); double v = va_arg(*ap, double);
if (pr == ~0U) pr = 6;
k = mg_dtoa(tmp, sizeof(tmp), v, (int) pr);
} else if (is_long == 2) {
int64_t v = va_arg(*ap, int64_t);
k = mg_lld(tmp, v, s, h); k = mg_lld(tmp, v, s, h);
} else if (is_long == 1) { } else if (is_long == 1) {
long v = va_arg(ap, long); long v = va_arg(*ap, long);
k = mg_lld(tmp, s ? (int64_t) v : (int64_t) (unsigned long) v, s, h); k = mg_lld(tmp, s ? (int64_t) v : (int64_t) (unsigned long) v, s, h);
} else { } else {
int v = va_arg(ap, int); int v = va_arg(*ap, int);
k = mg_lld(tmp, s ? (int64_t) v : (int64_t) (unsigned) v, s, h); k = mg_lld(tmp, s ? (int64_t) v : (int64_t) (unsigned) v, s, h);
} }
for (j = 0; j < xl && w > 0; j++) w--; for (j = 0; j < xl && w > 0; j++) w--;
@ -5267,12 +5289,15 @@ size_t mg_vsnprintf(char *buf, size_t len, const char *fmt, va_list ap) {
n += mg_copys(buf, len, n, tmp, k); n += mg_copys(buf, len, n, tmp, k);
for (j = 0; pad == ' ' && minus && k < w && j + k < w; j++) for (j = 0; pad == ' ' && minus && k < w && j + k < w; j++)
n += mg_copys(buf, len, n, &pad, 1); n += mg_copys(buf, len, n, &pad, 1);
} else if (c == 'M') {
mg_spfn_t fn = va_arg(*ap, mg_spfn_t);
n += fn(buf + n, n < len ? len - n : 0, ap);
} else if (c == 'c') { } else if (c == 'c') {
int p = va_arg(ap, int); int p = va_arg(*ap, int);
if (n < len) buf[n] = (char) p; if (n < len) buf[n] = (char) p;
n++; n++;
} else if (c == 's' || c == 'Q') { } else if (c == 's' || c == 'Q') {
char *p = va_arg(ap, char *); char *p = va_arg(*ap, char *);
size_t (*fn)(char *, size_t, size_t, char *, size_t) = size_t (*fn)(char *, size_t, size_t, char *, size_t) =
c == 's' ? mg_copys : mg_copyq; c == 's' ? mg_copys : mg_copyq;
if (pr == ~0U) pr = p == NULL ? 0 : strlen(p); if (pr == ~0U) pr = p == NULL ? 0 : strlen(p);
@ -5346,6 +5371,94 @@ double mg_atod(const char *p, int len, int *numlen) {
return d; return d;
} }
static int addexp(char *buf, int e, int sign) {
int n = 0;
buf[n++] = 'e';
buf[n++] = (char) sign;
if (e > 400) return 0;
if (e < 10) buf[n++] = '0';
if (e >= 100) buf[n++] = (char) (e / 100 + '0'), e -= 100 * (e / 100);
if (e >= 10) buf[n++] = (char) (e / 10 + '0'), e -= 10 * (e / 10);
buf[n++] = (char) (e + '0');
return n;
}
static int xisinf(double x) {
union {
double f;
uint64_t u;
} ieee754 = {x};
return ((unsigned) (ieee754.u >> 32) & 0x7fffffff) == 0x7ff00000 &&
((unsigned) ieee754.u == 0);
}
static int xisnan(double x) {
union {
double f;
uint64_t u;
} ieee754 = {x};
return ((unsigned) (ieee754.u >> 32) & 0x7fffffff) +
((unsigned) ieee754.u != 0) >
0x7ff00000;
}
size_t mg_dtoa(char *dst, size_t dstlen, double d, int width) {
char buf[40];
int i, s = 0, n = 0, e = 0;
double t, mul, saved;
if (d == 0.0) return mg_snprintf(dst, dstlen, "%s", "0");
if (xisinf(d)) return mg_snprintf(dst, dstlen, "%s", d > 0 ? "inf" : "-inf");
if (xisnan(d)) return mg_snprintf(dst, dstlen, "%s", "nan");
if (d < 0.0) d = -d, buf[s++] = '-';
// Round
saved = d;
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;
d += t;
// Calculate exponent, and 'mul' for scientific representation
mul = 1.0;
while (d >= 10.0 && d / mul >= 10.0) mul *= 10.0, e++;
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);
// 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);
// 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);
} else {
for (i = 0, t = mul; t >= 1.0 && s + n < (int) sizeof(buf); i++) {
int ch = (int) (d / t);
if (n > 0 || ch > 0) buf[s + n++] = (char) (ch + '0');
d -= ch * t;
t /= 10.0;
}
// printf(" --> [%g] -> %g %g (%d) [%.*s]\n", saved, d, t, n, s + n, buf);
if (n == 0) buf[s++] = '0';
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++) {
int ch = (int) (d / t);
buf[s + n++] = (char) (ch + '0');
d -= ch * t;
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
buf[s + n] = '\0';
return mg_snprintf(dst, dstlen, "%s", buf);
}
#ifdef MG_ENABLE_LINES #ifdef MG_ENABLE_LINES
#line 1 "src/timer.c" #line 1 "src/timer.c"
#endif #endif

View File

@ -715,18 +715,21 @@ bool mg_match(struct mg_str str, struct mg_str pattern, struct mg_str *caps);
bool mg_globmatch(const char *pattern, size_t plen, const char *s, size_t n); bool mg_globmatch(const char *pattern, size_t plen, const char *s, size_t n);
bool mg_commalist(struct mg_str *s, struct mg_str *k, struct mg_str *v); bool mg_commalist(struct mg_str *s, struct mg_str *k, struct mg_str *v);
bool mg_split(struct mg_str *s, struct mg_str *k, struct mg_str *v, char delim); bool mg_split(struct mg_str *s, struct mg_str *k, struct mg_str *v, char delim);
size_t mg_vsnprintf(char *buf, size_t len, const char *fmt, va_list ap); size_t mg_vsnprintf(char *buf, size_t len, const char *fmt, va_list *ap);
size_t mg_snprintf(char *, size_t, const char *fmt, ...); size_t mg_snprintf(char *, size_t, const char *fmt, ...);
char *mg_hex(const void *buf, size_t len, char *dst); char *mg_hex(const void *buf, size_t len, char *dst);
void mg_unhex(const char *buf, size_t len, unsigned char *to); void mg_unhex(const char *buf, size_t len, unsigned char *to);
unsigned long mg_unhexn(const char *s, size_t len); unsigned long mg_unhexn(const char *s, size_t len);
size_t mg_asprintf(char **, size_t, const char *fmt, ...); size_t mg_asprintf(char **, size_t, const char *fmt, ...);
size_t mg_vasprintf(char **buf, size_t size, const char *fmt, va_list ap); size_t mg_vasprintf(char **buf, size_t size, const char *fmt, va_list ap);
char *mg_mprintf(const char *fmt, ...);
char *mg_vmprintf(const char *fmt, va_list ap);
int mg_check_ip_acl(struct mg_str acl, uint32_t remote_ip); int mg_check_ip_acl(struct mg_str acl, uint32_t remote_ip);
int64_t mg_to64(struct mg_str str); int64_t mg_to64(struct mg_str str);
uint64_t mg_tou64(struct mg_str str); uint64_t mg_tou64(struct mg_str str);
size_t mg_lld(char *buf, int64_t val, bool is_signed, bool is_hex); size_t mg_lld(char *buf, int64_t val, bool is_signed, bool is_hex);
double mg_atod(const char *buf, int len, int *numlen); double mg_atod(const char *buf, int len, int *numlen);
size_t mg_dtoa(char *buf, size_t len, double d, int width);

139
src/str.c
View File

@ -164,7 +164,7 @@ size_t mg_snprintf(char *buf, size_t len, const char *fmt, ...) {
va_list ap; va_list ap;
size_t n; size_t n;
va_start(ap, fmt); va_start(ap, fmt);
n = mg_vsnprintf(buf, len, fmt, ap); n = mg_vsnprintf(buf, len, fmt, &ap);
va_end(ap); va_end(ap);
return n; return n;
} }
@ -205,7 +205,7 @@ size_t mg_vasprintf(char **buf, size_t size, const char *fmt, va_list ap) {
size_t len; size_t len;
va_copy(ap_copy, ap); va_copy(ap_copy, ap);
len = mg_vsnprintf(*buf, size, fmt, ap_copy); len = mg_vsnprintf(*buf, size, fmt, &ap_copy);
va_end(ap_copy); va_end(ap_copy);
if (len >= size) { if (len >= size) {
@ -214,7 +214,7 @@ size_t mg_vasprintf(char **buf, size_t size, const char *fmt, va_list ap) {
len = 0; len = 0;
} else { } else {
va_copy(ap_copy, ap); va_copy(ap_copy, ap);
len = mg_vsnprintf(*buf, len + 1, fmt, ap_copy); len = mg_vsnprintf(*buf, len + 1, fmt, &ap_copy);
va_end(ap_copy); va_end(ap_copy);
} }
} }
@ -231,6 +231,21 @@ size_t mg_asprintf(char **buf, size_t size, const char *fmt, ...) {
return ret; return ret;
} }
char *mg_vmprintf(const char *fmt, va_list ap) {
char *s = NULL;
mg_vasprintf(&s, 0, fmt, ap);
return s;
}
char *mg_mprintf(const char *fmt, ...) {
char *s = NULL;
va_list ap;
va_start(ap, fmt);
mg_vasprintf(&s, 0, fmt, ap);
va_end(ap);
return s;
}
uint64_t mg_tou64(struct mg_str str) { uint64_t mg_tou64(struct mg_str str) {
uint64_t result = 0; uint64_t result = 0;
size_t i = 0; size_t i = 0;
@ -316,7 +331,9 @@ static size_t mg_copyq(char *buf, size_t len, size_t n, char *p, size_t k) {
return j + extra; return j + extra;
} }
size_t mg_vsnprintf(char *buf, size_t len, const char *fmt, va_list ap) { typedef size_t (*mg_spfn_t)(char *, size_t, va_list *);
size_t mg_vsnprintf(char *buf, size_t len, const char *fmt, va_list *ap) {
size_t i = 0, n = 0; size_t i = 0, n = 0;
while (fmt[i] != '\0') { while (fmt[i] != '\0') {
if (fmt[i] == '%') { if (fmt[i] == '%') {
@ -329,7 +346,7 @@ size_t mg_vsnprintf(char *buf, size_t len, const char *fmt, va_list ap) {
if (c == '.') { if (c == '.') {
c = fmt[++i]; c = fmt[++i];
if (c == '*') { if (c == '*') {
pr = (size_t) va_arg(ap, int); pr = (size_t) va_arg(*ap, int);
c = fmt[++i]; c = fmt[++i];
} else { } else {
pr = 0; pr = 0;
@ -342,18 +359,23 @@ size_t mg_vsnprintf(char *buf, size_t len, const char *fmt, va_list ap) {
if (c == 'l') is_long++, c = fmt[++i]; if (c == 'l') is_long++, c = fmt[++i];
} }
if (c == 'p') x = 1, is_long = 1; if (c == 'p') x = 1, is_long = 1;
if (c == 'd' || c == 'u' || c == 'x' || c == 'X' || c == 'p') { if (c == 'd' || c == 'u' || c == 'x' || c == 'X' || c == 'p' ||
c == 'g') {
bool s = (c == 'd'), h = (c == 'x' || c == 'X' || c == 'p'); bool s = (c == 'd'), h = (c == 'x' || c == 'X' || c == 'p');
char tmp[30]; char tmp[40];
size_t xl = x ? 2 : 0; size_t xl = x ? 2 : 0;
if (is_long == 2) { if (c == 'g' || c == 'f') {
int64_t v = va_arg(ap, int64_t); double v = va_arg(*ap, double);
if (pr == ~0U) pr = 6;
k = mg_dtoa(tmp, sizeof(tmp), v, (int) pr);
} else if (is_long == 2) {
int64_t v = va_arg(*ap, int64_t);
k = mg_lld(tmp, v, s, h); k = mg_lld(tmp, v, s, h);
} else if (is_long == 1) { } else if (is_long == 1) {
long v = va_arg(ap, long); long v = va_arg(*ap, long);
k = mg_lld(tmp, s ? (int64_t) v : (int64_t) (unsigned long) v, s, h); k = mg_lld(tmp, s ? (int64_t) v : (int64_t) (unsigned long) v, s, h);
} else { } else {
int v = va_arg(ap, int); int v = va_arg(*ap, int);
k = mg_lld(tmp, s ? (int64_t) v : (int64_t) (unsigned) v, s, h); k = mg_lld(tmp, s ? (int64_t) v : (int64_t) (unsigned) v, s, h);
} }
for (j = 0; j < xl && w > 0; j++) w--; for (j = 0; j < xl && w > 0; j++) w--;
@ -365,12 +387,15 @@ size_t mg_vsnprintf(char *buf, size_t len, const char *fmt, va_list ap) {
n += mg_copys(buf, len, n, tmp, k); n += mg_copys(buf, len, n, tmp, k);
for (j = 0; pad == ' ' && minus && k < w && j + k < w; j++) for (j = 0; pad == ' ' && minus && k < w && j + k < w; j++)
n += mg_copys(buf, len, n, &pad, 1); n += mg_copys(buf, len, n, &pad, 1);
} else if (c == 'M') {
mg_spfn_t fn = va_arg(*ap, mg_spfn_t);
n += fn(buf + n, n < len ? len - n : 0, ap);
} else if (c == 'c') { } else if (c == 'c') {
int p = va_arg(ap, int); int p = va_arg(*ap, int);
if (n < len) buf[n] = (char) p; if (n < len) buf[n] = (char) p;
n++; n++;
} else if (c == 's' || c == 'Q') { } else if (c == 's' || c == 'Q') {
char *p = va_arg(ap, char *); char *p = va_arg(*ap, char *);
size_t (*fn)(char *, size_t, size_t, char *, size_t) = size_t (*fn)(char *, size_t, size_t, char *, size_t) =
c == 's' ? mg_copys : mg_copyq; c == 's' ? mg_copys : mg_copyq;
if (pr == ~0U) pr = p == NULL ? 0 : strlen(p); if (pr == ~0U) pr = p == NULL ? 0 : strlen(p);
@ -443,3 +468,91 @@ double mg_atod(const char *p, int len, int *numlen) {
if (numlen != NULL) *numlen = i; if (numlen != NULL) *numlen = i;
return d; return d;
} }
static int addexp(char *buf, int e, int sign) {
int n = 0;
buf[n++] = 'e';
buf[n++] = (char) sign;
if (e > 400) return 0;
if (e < 10) buf[n++] = '0';
if (e >= 100) buf[n++] = (char) (e / 100 + '0'), e -= 100 * (e / 100);
if (e >= 10) buf[n++] = (char) (e / 10 + '0'), e -= 10 * (e / 10);
buf[n++] = (char) (e + '0');
return n;
}
static int xisinf(double x) {
union {
double f;
uint64_t u;
} ieee754 = {x};
return ((unsigned) (ieee754.u >> 32) & 0x7fffffff) == 0x7ff00000 &&
((unsigned) ieee754.u == 0);
}
static int xisnan(double x) {
union {
double f;
uint64_t u;
} ieee754 = {x};
return ((unsigned) (ieee754.u >> 32) & 0x7fffffff) +
((unsigned) ieee754.u != 0) >
0x7ff00000;
}
size_t mg_dtoa(char *dst, size_t dstlen, double d, int width) {
char buf[40];
int i, s = 0, n = 0, e = 0;
double t, mul, saved;
if (d == 0.0) return mg_snprintf(dst, dstlen, "%s", "0");
if (xisinf(d)) return mg_snprintf(dst, dstlen, "%s", d > 0 ? "inf" : "-inf");
if (xisnan(d)) return mg_snprintf(dst, dstlen, "%s", "nan");
if (d < 0.0) d = -d, buf[s++] = '-';
// Round
saved = d;
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;
d += t;
// Calculate exponent, and 'mul' for scientific representation
mul = 1.0;
while (d >= 10.0 && d / mul >= 10.0) mul *= 10.0, e++;
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);
// 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);
// 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);
} else {
for (i = 0, t = mul; t >= 1.0 && s + n < (int) sizeof(buf); i++) {
int ch = (int) (d / t);
if (n > 0 || ch > 0) buf[s + n++] = (char) (ch + '0');
d -= ch * t;
t /= 10.0;
}
// printf(" --> [%g] -> %g %g (%d) [%.*s]\n", saved, d, t, n, s + n, buf);
if (n == 0) buf[s++] = '0';
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++) {
int ch = (int) (d / t);
buf[s + n++] = (char) (ch + '0');
d -= ch * t;
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
buf[s + n] = '\0';
return mg_snprintf(dst, dstlen, "%s", buf);
}

View File

@ -31,15 +31,18 @@ bool mg_match(struct mg_str str, struct mg_str pattern, struct mg_str *caps);
bool mg_globmatch(const char *pattern, size_t plen, const char *s, size_t n); bool mg_globmatch(const char *pattern, size_t plen, const char *s, size_t n);
bool mg_commalist(struct mg_str *s, struct mg_str *k, struct mg_str *v); bool mg_commalist(struct mg_str *s, struct mg_str *k, struct mg_str *v);
bool mg_split(struct mg_str *s, struct mg_str *k, struct mg_str *v, char delim); bool mg_split(struct mg_str *s, struct mg_str *k, struct mg_str *v, char delim);
size_t mg_vsnprintf(char *buf, size_t len, const char *fmt, va_list ap); size_t mg_vsnprintf(char *buf, size_t len, const char *fmt, va_list *ap);
size_t mg_snprintf(char *, size_t, const char *fmt, ...); size_t mg_snprintf(char *, size_t, const char *fmt, ...);
char *mg_hex(const void *buf, size_t len, char *dst); char *mg_hex(const void *buf, size_t len, char *dst);
void mg_unhex(const char *buf, size_t len, unsigned char *to); void mg_unhex(const char *buf, size_t len, unsigned char *to);
unsigned long mg_unhexn(const char *s, size_t len); unsigned long mg_unhexn(const char *s, size_t len);
size_t mg_asprintf(char **, size_t, const char *fmt, ...); size_t mg_asprintf(char **, size_t, const char *fmt, ...);
size_t mg_vasprintf(char **buf, size_t size, const char *fmt, va_list ap); size_t mg_vasprintf(char **buf, size_t size, const char *fmt, va_list ap);
char *mg_mprintf(const char *fmt, ...);
char *mg_vmprintf(const char *fmt, va_list ap);
int mg_check_ip_acl(struct mg_str acl, uint32_t remote_ip); int mg_check_ip_acl(struct mg_str acl, uint32_t remote_ip);
int64_t mg_to64(struct mg_str str); int64_t mg_to64(struct mg_str str);
uint64_t mg_tou64(struct mg_str str); uint64_t mg_tou64(struct mg_str str);
size_t mg_lld(char *buf, int64_t val, bool is_signed, bool is_hex); size_t mg_lld(char *buf, int64_t val, bool is_signed, bool is_hex);
double mg_atod(const char *buf, int len, int *numlen); double mg_atod(const char *buf, int len, int *numlen);
size_t mg_dtoa(char *buf, size_t len, double d, int width);

View File

@ -1,3 +1,5 @@
#include "float.h" // For DBL_EPSILON and HUGE_VAL
#include "math.h"
#include "mongoose.h" #include "mongoose.h"
static int s_num_tests = 0; static int s_num_tests = 0;
@ -1354,10 +1356,10 @@ static bool sn(const char *fmt, ...) {
n = (size_t) vsnprintf(buf2, sizeof(buf2), fmt, ap); n = (size_t) vsnprintf(buf2, sizeof(buf2), fmt, ap);
va_end(ap); va_end(ap);
va_start(ap, fmt); va_start(ap, fmt);
n1 = mg_vsnprintf(buf, sizeof(buf), fmt, ap); n1 = mg_vsnprintf(buf, sizeof(buf), fmt, &ap);
va_end(ap); va_end(ap);
va_start(ap, fmt); va_start(ap, fmt);
n2 = mg_vsnprintf(tmp, 0, fmt, ap); n2 = mg_vsnprintf(tmp, 0, fmt, &ap);
va_end(ap); va_end(ap);
result = n1 == n2 && n1 == n && strcmp(buf, buf2) == 0; result = n1 == n2 && n1 == n && strcmp(buf, buf2) == 0;
if (!result) if (!result)
@ -1372,6 +1374,12 @@ static bool sccmp(const char *s1, const char *s2, int expected) {
return n1 == expected; return n1 == expected;
} }
static size_t pf1(char *buf, size_t len, va_list *ap) {
int a = va_arg(*ap, int);
int b = va_arg(*ap, int);
return mg_snprintf(buf, len, "%d", a + b);
}
static void test_str(void) { static void test_str(void) {
struct mg_str s = mg_strdup(mg_str("a")); struct mg_str s = mg_strdup(mg_str("a"));
ASSERT(mg_strcmp(s, mg_str("a")) == 0); ASSERT(mg_strcmp(s, mg_str("a")) == 0);
@ -1447,7 +1455,7 @@ static void test_str(void) {
// Non-standard formatting // Non-standard formatting
{ {
char buf[100]; char buf[100], *p;
const char *expected; const char *expected;
expected = "\"\""; expected = "\"\"";
@ -1465,6 +1473,71 @@ static void test_str(void) {
expected = "\"abc\""; expected = "\"abc\"";
mg_snprintf(buf, sizeof(buf), "%.*Q", 3, "abcdef"); mg_snprintf(buf, sizeof(buf), "%.*Q", 3, "abcdef");
ASSERT(strcmp(buf, expected) == 0); ASSERT(strcmp(buf, expected) == 0);
p = mg_mprintf("[%s,%M,%s]", "null", pf1, 2, 3, "hi");
printf("-> %s\n", p);
ASSERT(strcmp(p, "[null,5,hi]") == 0);
free(p);
}
{
char tmp[40];
#define DBLWIDTH(a, b) a, b
#define TESTDOUBLE(fmt_, num_, res_) \
do { \
const char *N = #num_; \
size_t n = mg_snprintf(tmp, sizeof(tmp), fmt_, num_); \
printf("[%s] [%s] -> [%s] [%.*s]\n", fmt_, N, res_, (int) n, tmp); \
ASSERT(n == strlen(res_)); \
ASSERT(strcmp(tmp, res_) == 0); \
} while (0)
TESTDOUBLE("%g", 0.0, "0");
TESTDOUBLE("%g", 0.123, "0.123");
TESTDOUBLE("%g", 0.00123, "0.00123");
TESTDOUBLE("%g", 0.123456333, "0.123456");
TESTDOUBLE("%g", 123.0, "123");
TESTDOUBLE("%g", 11.5454, "11.5454");
TESTDOUBLE("%g", 11.0001, "11.0001");
TESTDOUBLE("%g", 0.999, "0.999");
TESTDOUBLE("%g", 0.999999, "0.999999");
TESTDOUBLE("%g", 0.9999999, "1");
TESTDOUBLE("%g", 10.9, "10.9");
TESTDOUBLE("%g", 10.01, "10.01");
TESTDOUBLE("%g", 1.0, "1");
TESTDOUBLE("%g", 10.0, "10");
TESTDOUBLE("%g", 100.0, "100");
TESTDOUBLE("%g", 1000.0, "1000");
TESTDOUBLE("%g", 10000.0, "10000");
TESTDOUBLE("%g", 100000.0, "100000");
TESTDOUBLE("%g", 1000000.0, "1e+06");
TESTDOUBLE("%g", 10000000.0, "1e+07");
TESTDOUBLE("%g", 100000001.0, "1e+08");
TESTDOUBLE("%g", 10.5454, "10.5454");
TESTDOUBLE("%g", 999999.0, "999999");
TESTDOUBLE("%g", 9999999.0, "1e+07");
TESTDOUBLE("%g", 44556677.0, "4.45567e+07");
TESTDOUBLE("%g", 1234567.2, "1.23457e+06");
TESTDOUBLE("%g", -987.65432, "-987.654");
TESTDOUBLE("%g", 0.0000000001, "1e-10");
TESTDOUBLE("%g", 2.34567e-57, "2.34567e-57");
TESTDOUBLE("%.*g", DBLWIDTH(7, 9999999.0), "9999999");
TESTDOUBLE("%.*g", DBLWIDTH(10, 0.123456333), "0.123456333");
TESTDOUBLE("%g", 123.456222, "123.456");
TESTDOUBLE("%.*g", DBLWIDTH(10, 123.456222), "123.456222");
TESTDOUBLE("%g", 600.1234, "600.123");
TESTDOUBLE("%g", -600.1234, "-600.123");
TESTDOUBLE("%g", 599.1234, "599.123");
TESTDOUBLE("%g", -599.1234, "-599.123");
#ifndef _WIN32
TESTDOUBLE("%g", (double) INFINITY, "inf");
TESTDOUBLE("%g", (double) -INFINITY, "-inf");
TESTDOUBLE("%g", (double) NAN, "nan");
#else
TESTDOUBLE("%g", HUGE_VAL, "inf");
TESTDOUBLE("%g", -HUGE_VAL, "-inf");
#endif
} }
} }