Add mg_rprintf, mg_vrprintf

This commit is contained in:
Sergey Lyubka 2022-06-28 00:45:52 +01:00
parent 4864a6e618
commit d65ce3b596
8 changed files with 2341 additions and 2246 deletions

File diff suppressed because it is too large Load Diff

View File

@ -107,6 +107,145 @@ int mg_base64_decode(const char *src, int n, char *dst) {
return len;
}
#ifdef MG_ENABLE_LINES
#line 1 "src/dbl.c"
#endif
double mg_atod(const char *p, int len, int *numlen) {
double d = 0.0;
int i = 0, sign = 1;
// Sign
if (i < len && *p == '-') {
sign = -1, i++;
} else if (i < len && *p == '+') {
i++;
}
// Decimal
for (; i < len && p[i] >= '0' && p[i] <= '9'; i++) {
d *= 10.0;
d += p[i] - '0';
}
d *= sign;
// Fractional
if (i < len && p[i] == '.') {
double frac = 0.0, base = 0.1;
i++;
for (; i < len && p[i] >= '0' && p[i] <= '9'; i++) {
frac += base * (p[i] - '0');
base /= 10.0;
}
d += frac * sign;
}
// Exponential
if (i < len && (p[i] == 'e' || p[i] == 'E')) {
int j, exp = 0, minus = 0;
i++;
if (i < len && p[i] == '-') minus = 1, i++;
if (i < len && p[i] == '+') i++;
while (i < len && p[i] >= '0' && p[i] <= '9' && exp < 308)
exp = exp * 10 + (p[i++] - '0');
if (minus) exp = -exp;
for (j = 0; j < exp; j++) d *= 10.0;
for (j = 0; j < -exp; j++) d /= 10.0;
}
if (numlen != NULL) *numlen = i;
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
#line 1 "src/dns.c"
#endif
@ -407,6 +546,228 @@ void mg_error(struct mg_connection *c, const char *fmt, ...) {
if (buf != mem) free(buf);
}
#ifdef MG_ENABLE_LINES
#line 1 "src/fmt.c"
#endif
size_t mg_rprintf(void (*out)(char, void *), void *ptr, const char *fmt, ...) {
size_t len = 0;
va_list ap;
va_start(ap, fmt);
len = mg_vrprintf(out, ptr, fmt, &ap);
va_end(ap);
return len;
}
static void mg_putchar_iobuf(char ch, void *param) {
struct mg_iobuf *io = (struct mg_iobuf *) param;
if (io->len < io->size) io->buf[io->len++] = (uint8_t) ch;
}
void mg_putchar_stralloc(char ch, void *param) {
char *s, *buf = *(char **) param;
size_t len = buf == NULL ? 0 : strlen(buf), chunksize = 256;
size_t new_size = len + 1 + 1 + chunksize;
new_size -= new_size % chunksize;
if ((s = (char *) realloc(buf, new_size)) != NULL) {
s[len] = ch;
s[len + 1] = '\0';
*(char **) param = s;
}
}
size_t mg_vsnprintf(char *buf, size_t len, const char *fmt, va_list *ap) {
struct mg_iobuf io = {(uint8_t *) buf, len, 0};
size_t n = mg_vrprintf(mg_putchar_iobuf, &io, fmt, ap);
if (n < len) buf[n] = '\0';
return n;
}
static bool is_digit(int c) {
return c >= '0' && c <= '9';
}
size_t mg_lld(char *buf, int64_t val, bool is_signed, bool is_hex) {
const char *letters = "0123456789abcdef";
uint64_t v = (uint64_t) val;
size_t s = 0, n, i;
if (is_signed && val < 0) buf[s++] = '-', v = (uint64_t) (-val);
// This loop prints a number in reverse order. I guess this is because we
// write numbers from right to left: least significant digit comes last.
// Maybe because we use Arabic numbers, and Arabs write RTL?
if (is_hex) {
for (n = 0; v; v >>= 4) buf[s + n++] = letters[v & 15];
} else {
for (n = 0; v; v /= 10) buf[s + n++] = letters[v % 10];
}
// Reverse a string
for (i = 0; i < n / 2; i++) {
char t = buf[s + i];
buf[s + i] = buf[s + n - i - 1], buf[s + n - i - 1] = t;
}
if (val == 0) buf[n++] = '0'; // Handle special case
return n + s;
}
static size_t scpy(void (*out)(char, void *), void *ptr, char *buf,
size_t len) {
size_t i = 0;
while (i < len && buf[i] != '\0') out(buf[i++], ptr);
return i;
}
static char mg_esc(int c, bool esc) {
const char *p, *esc1 = "\b\f\n\r\t\\\"", *esc2 = "bfnrt\\\"";
for (p = esc ? esc1 : esc2; *p != '\0'; p++) {
if (*p == c) return esc ? esc2[p - esc1] : esc1[p - esc2];
}
return 0;
}
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) {
size_t i = 0, extra = 2;
out('"', ptr);
for (i = 0; i < len && buf[i] != '\0'; i++) {
char c = mg_escape(buf[i]);
if (c) {
out('\\', ptr), out(c, ptr), extra++;
} else {
out(buf[i], ptr);
}
}
out('"', ptr);
return i + extra;
}
static size_t bcpy(void (*out)(char, void *), void *ptr, uint8_t *buf,
size_t len) {
size_t i, n = 0;
const char *t =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
out('"', ptr), n++;
for (i = 0; i < len; i += 3) {
uint8_t c1 = buf[i], c2 = i + 1 < len ? buf[i + 1] : 0,
c3 = i + 2 < len ? buf[i + 2] : 0;
char tmp[4] = {t[c1 >> 2], t[(c1 & 3) << 4 | (c2 >> 4)], '=', '='};
if (i + 1 < len) tmp[2] = t[(c2 & 15) << 2 | (c3 >> 6)];
if (i + 2 < len) tmp[3] = t[c3 & 63];
n += scpy(out, ptr, tmp, sizeof(tmp));
}
out('"', ptr), n++;
return n;
}
size_t mg_vrprintf(void (*out)(char, void *), void *param, const char *fmt,
va_list *ap) {
size_t i = 0, n = 0;
while (fmt[i] != '\0') {
if (fmt[i] == '%') {
size_t j, k, x = 0, is_long = 0, w = 0 /* width */, pr = ~0U /* prec */;
char pad = ' ', minus = 0, c = fmt[++i];
if (c == '#') x++, c = fmt[++i];
if (c == '-') minus++, c = fmt[++i];
if (c == '0') pad = '0', c = fmt[++i];
while (is_digit(c)) w *= 10, w += (size_t) (c - '0'), c = fmt[++i];
if (c == '.') {
c = fmt[++i];
if (c == '*') {
pr = (size_t) va_arg(*ap, int);
c = fmt[++i];
} else {
pr = 0;
while (is_digit(c)) pr *= 10, pr += (size_t) (c - '0'), c = fmt[++i];
}
}
while (c == 'h') c = fmt[++i]; // Treat h and hh as int
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 == 'd' || c == 'u' || c == 'x' || c == 'X' || c == 'p' ||
c == 'g') {
bool s = (c == 'd'), h = (c == 'x' || c == 'X' || c == 'p');
char tmp[40];
size_t xl = x ? 2 : 0;
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);
} else if (is_long == 2) {
int64_t v = va_arg(*ap, int64_t);
k = mg_lld(tmp, v, s, h);
} else if (is_long == 1) {
long v = va_arg(*ap, long);
k = mg_lld(tmp, s ? (int64_t) v : (int64_t) (unsigned long) v, s, h);
} else {
int v = va_arg(*ap, int);
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; pad == ' ' && !minus && k < w && j + k < w; j++)
n += scpy(out, param, &pad, 1);
n += scpy(out, param, (char *) "0x", xl);
for (j = 0; pad == '0' && k < w && j + k < w; j++)
n += scpy(out, param, &pad, 1);
n += scpy(out, param, tmp, k);
for (j = 0; pad == ' ' && minus && k < w && j + k < w; j++)
n += scpy(out, param, &pad, 1);
} else if (c == 'M') {
mg_pm_t f = va_arg(*ap, mg_pm_t);
n += f(out, param, ap);
} else if (c == 'c') {
int ch = va_arg(*ap, int);
out((char) ch, param);
n++;
} else if (c == 'H') {
// Print hex-encoded double-quoted string
size_t bl = (size_t) va_arg(*ap, int);
uint8_t *p = va_arg(*ap, uint8_t *), dquote = '"';
const char *hex = "0123456789abcdef";
n += scpy(out, param, (char *) &dquote, 1);
for (j = 0; j < bl; j++) {
n += scpy(out, param, (char *) &hex[(p[j] >> 4) & 15], 1);
n += scpy(out, param, (char *) &hex[p[j] & 15], 1);
}
n += scpy(out, param, (char *) &dquote, 1);
} else if (c == 'V') {
// Print base64-encoded double-quoted string
size_t len = (size_t) va_arg(*ap, int);
uint8_t *buf = va_arg(*ap, uint8_t *);
n += bcpy(out, param, buf, len);
} else if (c == 's' || c == 'Q') {
char *p = va_arg(*ap, char *);
size_t (*f)(void (*)(char, void *), void *, char *, size_t) =
c == 's' ? scpy : qcpy;
if (pr == ~0U) pr = p == NULL ? 0 : strlen(p);
for (j = 0; !minus && pr < w && j + pr < w; j++)
n += f(out, param, &pad, 1);
n += f(out, param, p, pr);
for (j = 0; minus && pr < w && j + pr < w; j++)
n += f(out, param, &pad, 1);
} else if (c == '%') {
out('%', param);
n++;
} else {
out('%', param);
out(c, param);
n += 2;
}
i++;
} else {
out(fmt[i], param), n++, i++;
}
}
return n;
}
#ifdef MG_ENABLE_LINES
#line 1 "src/fs.c"
#endif
@ -4230,10 +4591,6 @@ const char *mg_strstr(const struct mg_str haystack,
return NULL;
}
static bool is_digit(int c) {
return c >= '0' && c <= '9';
}
static bool is_space(int c) {
return c == ' ' || c == '\r' || c == '\n' || c == '\t';
}
@ -4419,321 +4776,6 @@ int64_t mg_to64(struct mg_str str) {
return result * neg;
}
size_t mg_lld(char *buf, int64_t val, bool is_signed, bool is_hex) {
const char *letters = "0123456789abcdef";
uint64_t v = (uint64_t) val;
size_t s = 0, n, i;
if (is_signed && val < 0) buf[s++] = '-', v = (uint64_t) (-val);
// This loop prints a number in reverse order. I guess this is because we
// write numbers from right to left: least significant digit comes last.
// Maybe because we use Arabic numbers, and Arabs write RTL?
if (is_hex) {
for (n = 0; v; v >>= 4) buf[s + n++] = letters[v & 15];
} else {
for (n = 0; v; v /= 10) buf[s + n++] = letters[v % 10];
}
// Reverse a string
for (i = 0; i < n / 2; i++) {
char t = buf[s + i];
buf[s + i] = buf[s + n - i - 1], buf[s + n - i - 1] = t;
}
if (val == 0) buf[n++] = '0'; // Handle special case
return n + s;
}
static size_t mg_copys(char *buf, size_t len, size_t n, char *p, size_t k) {
size_t j = 0;
for (j = 0; j < k && p[j]; j++)
if (j + n < len) buf[n + j] = p[j];
return j;
}
static char mg_esc(int c, bool esc) {
const char *p, *esc1 = "\b\f\n\r\t\\\"", *esc2 = "bfnrt\\\"";
for (p = esc ? esc1 : esc2; *p != '\0'; p++) {
if (*p == c) return esc ? esc2[p - esc1] : esc1[p - esc2];
}
return 0;
}
static char mg_escape(int c) {
return mg_esc(c, true);
}
static size_t mg_copyq(char *buf, size_t len, size_t n, char *p, size_t k) {
size_t j = 0, extra = 2;
if (n < len) buf[n++] = '"';
for (j = 0; j < k && p[j]; j++) {
char c = mg_escape(p[j]);
if (c) {
if (j + n < len) buf[n + j] = '\\';
n++;
extra++;
if (j + n < len) buf[n + j] = c;
} else {
if (j + n < len) buf[n + j] = p[j];
}
}
if (j + n < len) buf[n + j] = '"';
return j + extra;
}
// %M specifier printer function
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;
while (fmt[i] != '\0') {
if (fmt[i] == '%') {
size_t j, k, x = 0, is_long = 0, w = 0 /* width */, pr = ~0U /* prec */;
char pad = ' ', minus = 0, c = fmt[++i];
if (c == '#') x++, c = fmt[++i];
if (c == '-') minus++, c = fmt[++i];
if (c == '0') pad = '0', c = fmt[++i];
while (is_digit(c)) w *= 10, w += (size_t) (c - '0'), c = fmt[++i];
if (c == '.') {
c = fmt[++i];
if (c == '*') {
pr = (size_t) va_arg(*ap, int);
c = fmt[++i];
} else {
pr = 0;
while (is_digit(c)) pr *= 10, pr += (size_t) (c - '0'), c = fmt[++i];
}
}
while (c == 'h') c = fmt[++i]; // Treat h and hh as int
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 == 'd' || c == 'u' || c == 'x' || c == 'X' || c == 'p' ||
c == 'g') {
bool s = (c == 'd'), h = (c == 'x' || c == 'X' || c == 'p');
char tmp[40];
size_t xl = x ? 2 : 0;
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);
} else if (is_long == 2) {
int64_t v = va_arg(*ap, int64_t);
k = mg_lld(tmp, v, s, h);
} else if (is_long == 1) {
long v = va_arg(*ap, long);
k = mg_lld(tmp, s ? (int64_t) v : (int64_t) (unsigned long) v, s, h);
} else {
int v = va_arg(*ap, int);
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; pad == ' ' && !minus && k < w && j + k < w; j++)
n += mg_copys(buf, len, n, &pad, 1);
n += mg_copys(buf, len, n, (char *) "0x", xl);
for (j = 0; pad == '0' && k < w && j + k < w; j++)
n += mg_copys(buf, len, n, &pad, 1);
n += mg_copys(buf, len, n, tmp, k);
for (j = 0; pad == ' ' && minus && k < w && j + k < w; j++)
n += mg_copys(buf, len, n, &pad, 1);
} else if (c == 'M') {
char *dst = buf ? buf + n : NULL;
size_t dstlen = n < len ? len - n : 0;
mg_spfn_t fn = va_arg(*ap, mg_spfn_t);
n += fn(dst, dstlen, ap);
} else if (c == 'c') {
int p = va_arg(*ap, int);
if (n < len) buf[n] = (char) p;
n++;
} else if (c == 'H') {
// Print hex-encoded double-quoted string
size_t bl = (size_t) va_arg(*ap, int);
uint8_t *p = va_arg(*ap, uint8_t *), dquote = '"';
const char *hex = "0123456789abcdef";
n += mg_copys(buf, len, n, (char *) &dquote, 1);
for (j = 0; j < bl; j++) {
n += mg_copys(buf, len, n, (char *) &hex[(p[j] >> 4) & 15], 1);
n += mg_copys(buf, len, n, (char *) &hex[p[j] & 15], 1);
}
n += mg_copys(buf, len, n, (char *) &dquote, 1);
} else if (c == 'V') {
// Print base64-encoded double-quoted string
size_t bl = (size_t) va_arg(*ap, int);
uint8_t *p = va_arg(*ap, uint8_t *), dquote = '"';
const char *t =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
n += mg_copys(buf, len, n, (char *) &dquote, 1);
for (j = 0; j < bl; j += 3) {
uint8_t c1 = p[j], c2 = j + 1 < bl ? p[j + 1] : 0,
c3 = j + 2 < bl ? p[j + 2] : 0;
char tmp[4] = {t[c1 >> 2], t[(c1 & 3) << 4 | (c2 >> 4)], '=', '='};
if (j + 1 < bl) tmp[2] = t[(c2 & 15) << 2 | (c3 >> 6)];
if (j + 2 < bl) tmp[3] = t[c3 & 63];
n += mg_copys(buf, len, n, tmp, sizeof(tmp));
}
n += mg_copys(buf, len, n, (char *) &dquote, 1);
} else if (c == 's' || c == 'Q') {
char *p = va_arg(*ap, char *);
size_t (*fn)(char *, size_t, size_t, char *, size_t) =
c == 's' ? mg_copys : mg_copyq;
if (pr == ~0U) pr = p == NULL ? 0 : strlen(p);
for (j = 0; !minus && pr < w && j + pr < w; j++)
n += fn(buf, len, n, &pad, 1);
n += fn(buf, len, n, p, pr);
for (j = 0; minus && pr < w && j + pr < w; j++)
n += fn(buf, len, n, &pad, 1);
} else if (c == '%') {
if (n < len) buf[n] = '%';
n++;
} else {
if (n < len) buf[n] = '%';
n++;
if (n < len) buf[n] = c;
n++;
}
i++;
} else {
if (n < len) buf[n] = fmt[i];
n++, i++;
}
}
if (n < len) buf[n] = '\0';
return n;
}
double mg_atod(const char *p, int len, int *numlen) {
double d = 0.0;
int i = 0, sign = 1;
// Sign
if (i < len && *p == '-') {
sign = -1, i++;
} else if (i < len && *p == '+') {
i++;
}
// Decimal
for (; i < len && is_digit(p[i]); i++) {
d *= 10.0;
d += p[i] - '0';
}
d *= sign;
// Fractional
if (i < len && p[i] == '.') {
double frac = 0.0, base = 0.1;
i++;
for (; i < len && is_digit(p[i]); i++) {
frac += base * (p[i] - '0');
base /= 10.0;
}
d += frac * sign;
}
// Exponential
if (i < len && (p[i] == 'e' || p[i] == 'E')) {
int j, exp = 0, minus = 0;
i++;
if (i < len && p[i] == '-') minus = 1, i++;
if (i < len && p[i] == '+') i++;
while (i < len && is_digit(p[i]) && exp < 308)
exp = exp * 10 + (p[i++] - '0');
if (minus) exp = -exp;
for (j = 0; j < exp; j++) d *= 10.0;
for (j = 0; j < -exp; j++) d /= 10.0;
}
if (numlen != NULL) *numlen = i;
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
#line 1 "src/timer.c"
#endif
@ -5090,7 +5132,7 @@ void mg_tls_init(struct mg_connection *c, const struct mg_tls_opts *opts) {
SSL_set_verify(tls->ssl, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
NULL);
if ((rc = SSL_CTX_load_verify_locations(tls->ctx, opts->ca, NULL)) != 1) {
mg_error(c, "parse(%s): err %d", opts->ca, mg_tls_err(tls, rc));
mg_error(c, "load('%s') %d err %d", opts->ca, rc, mg_tls_err(tls, rc));
goto fail;
}
}
@ -5119,9 +5161,9 @@ void mg_tls_init(struct mg_connection *c, const struct mg_tls_opts *opts) {
#if OPENSSL_VERSION_NUMBER > 0x10002000L
if (opts->srvname.len > 0) {
char mem[128], *buf = mem;
size_t len = mg_asprintf(&buf, sizeof(mem), "%.*s", (int) opts->srvname.len,
mg_asprintf(&buf, sizeof(mem), "%.*s", (int) opts->srvname.len,
opts->srvname.ptr);
X509_VERIFY_PARAM_set1_host(SSL_get0_param(tls->ssl), buf, len);
SSL_set1_host(tls->ssl, buf);
if (buf != mem) free(buf);
}
#endif

View File

@ -715,15 +715,9 @@ 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_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);
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, ...);
char *mg_hex(const void *buf, size_t len, char *dst);
void mg_unhex(const char *buf, size_t len, unsigned char *to);
unsigned long mg_unhexn(const char *s, size_t len);
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);
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);
int64_t mg_to64(struct mg_str str);
uint64_t mg_tou64(struct mg_str str);
@ -731,6 +725,19 @@ 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);
size_t mg_dtoa(char *buf, size_t len, double d, int width);
typedef void (*mg_pc_t)(char, void *); // Custom putchar
typedef size_t (*mg_pm_t)(mg_pc_t, void *, va_list *); // %M printer
void mg_putchar_stralloc(char ch, void *param); // Print to malloced str
size_t mg_vrprintf(void (*)(char, void *), void *, const char *fmt, va_list *);
size_t mg_rprintf(void (*fn)(char, void *), void *, const char *fmt, ...);
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_asprintf(char **, size_t, const char *fmt, ...);
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);

135
src/dbl.c Normal file
View File

@ -0,0 +1,135 @@
#include "str.h"
double mg_atod(const char *p, int len, int *numlen) {
double d = 0.0;
int i = 0, sign = 1;
// Sign
if (i < len && *p == '-') {
sign = -1, i++;
} else if (i < len && *p == '+') {
i++;
}
// Decimal
for (; i < len && p[i] >= '0' && p[i] <= '9'; i++) {
d *= 10.0;
d += p[i] - '0';
}
d *= sign;
// Fractional
if (i < len && p[i] == '.') {
double frac = 0.0, base = 0.1;
i++;
for (; i < len && p[i] >= '0' && p[i] <= '9'; i++) {
frac += base * (p[i] - '0');
base /= 10.0;
}
d += frac * sign;
}
// Exponential
if (i < len && (p[i] == 'e' || p[i] == 'E')) {
int j, exp = 0, minus = 0;
i++;
if (i < len && p[i] == '-') minus = 1, i++;
if (i < len && p[i] == '+') i++;
while (i < len && p[i] >= '0' && p[i] <= '9' && exp < 308)
exp = exp * 10 + (p[i++] - '0');
if (minus) exp = -exp;
for (j = 0; j < exp; j++) d *= 10.0;
for (j = 0; j < -exp; j++) d /= 10.0;
}
if (numlen != NULL) *numlen = i;
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);
}

218
src/fmt.c Normal file
View File

@ -0,0 +1,218 @@
#include "iobuf.h"
#include "str.h"
size_t mg_rprintf(void (*out)(char, void *), void *ptr, const char *fmt, ...) {
size_t len = 0;
va_list ap;
va_start(ap, fmt);
len = mg_vrprintf(out, ptr, fmt, &ap);
va_end(ap);
return len;
}
static void mg_putchar_iobuf(char ch, void *param) {
struct mg_iobuf *io = (struct mg_iobuf *) param;
if (io->len < io->size) io->buf[io->len++] = (uint8_t) ch;
}
void mg_putchar_stralloc(char ch, void *param) {
char *s, *buf = *(char **) param;
size_t len = buf == NULL ? 0 : strlen(buf), chunksize = 256;
size_t new_size = len + 1 + 1 + chunksize;
new_size -= new_size % chunksize;
if ((s = (char *) realloc(buf, new_size)) != NULL) {
s[len] = ch;
s[len + 1] = '\0';
*(char **) param = s;
}
}
size_t mg_vsnprintf(char *buf, size_t len, const char *fmt, va_list *ap) {
struct mg_iobuf io = {(uint8_t *) buf, len, 0};
size_t n = mg_vrprintf(mg_putchar_iobuf, &io, fmt, ap);
if (n < len) buf[n] = '\0';
return n;
}
static bool is_digit(int c) {
return c >= '0' && c <= '9';
}
size_t mg_lld(char *buf, int64_t val, bool is_signed, bool is_hex) {
const char *letters = "0123456789abcdef";
uint64_t v = (uint64_t) val;
size_t s = 0, n, i;
if (is_signed && val < 0) buf[s++] = '-', v = (uint64_t) (-val);
// This loop prints a number in reverse order. I guess this is because we
// write numbers from right to left: least significant digit comes last.
// Maybe because we use Arabic numbers, and Arabs write RTL?
if (is_hex) {
for (n = 0; v; v >>= 4) buf[s + n++] = letters[v & 15];
} else {
for (n = 0; v; v /= 10) buf[s + n++] = letters[v % 10];
}
// Reverse a string
for (i = 0; i < n / 2; i++) {
char t = buf[s + i];
buf[s + i] = buf[s + n - i - 1], buf[s + n - i - 1] = t;
}
if (val == 0) buf[n++] = '0'; // Handle special case
return n + s;
}
static size_t scpy(void (*out)(char, void *), void *ptr, char *buf,
size_t len) {
size_t i = 0;
while (i < len && buf[i] != '\0') out(buf[i++], ptr);
return i;
}
static char mg_esc(int c, bool esc) {
const char *p, *esc1 = "\b\f\n\r\t\\\"", *esc2 = "bfnrt\\\"";
for (p = esc ? esc1 : esc2; *p != '\0'; p++) {
if (*p == c) return esc ? esc2[p - esc1] : esc1[p - esc2];
}
return 0;
}
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) {
size_t i = 0, extra = 2;
out('"', ptr);
for (i = 0; i < len && buf[i] != '\0'; i++) {
char c = mg_escape(buf[i]);
if (c) {
out('\\', ptr), out(c, ptr), extra++;
} else {
out(buf[i], ptr);
}
}
out('"', ptr);
return i + extra;
}
static size_t bcpy(void (*out)(char, void *), void *ptr, uint8_t *buf,
size_t len) {
size_t i, n = 0;
const char *t =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
out('"', ptr), n++;
for (i = 0; i < len; i += 3) {
uint8_t c1 = buf[i], c2 = i + 1 < len ? buf[i + 1] : 0,
c3 = i + 2 < len ? buf[i + 2] : 0;
char tmp[4] = {t[c1 >> 2], t[(c1 & 3) << 4 | (c2 >> 4)], '=', '='};
if (i + 1 < len) tmp[2] = t[(c2 & 15) << 2 | (c3 >> 6)];
if (i + 2 < len) tmp[3] = t[c3 & 63];
n += scpy(out, ptr, tmp, sizeof(tmp));
}
out('"', ptr), n++;
return n;
}
size_t mg_vrprintf(void (*out)(char, void *), void *param, const char *fmt,
va_list *ap) {
size_t i = 0, n = 0;
while (fmt[i] != '\0') {
if (fmt[i] == '%') {
size_t j, k, x = 0, is_long = 0, w = 0 /* width */, pr = ~0U /* prec */;
char pad = ' ', minus = 0, c = fmt[++i];
if (c == '#') x++, c = fmt[++i];
if (c == '-') minus++, c = fmt[++i];
if (c == '0') pad = '0', c = fmt[++i];
while (is_digit(c)) w *= 10, w += (size_t) (c - '0'), c = fmt[++i];
if (c == '.') {
c = fmt[++i];
if (c == '*') {
pr = (size_t) va_arg(*ap, int);
c = fmt[++i];
} else {
pr = 0;
while (is_digit(c)) pr *= 10, pr += (size_t) (c - '0'), c = fmt[++i];
}
}
while (c == 'h') c = fmt[++i]; // Treat h and hh as int
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 == 'd' || c == 'u' || c == 'x' || c == 'X' || c == 'p' ||
c == 'g') {
bool s = (c == 'd'), h = (c == 'x' || c == 'X' || c == 'p');
char tmp[40];
size_t xl = x ? 2 : 0;
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);
} else if (is_long == 2) {
int64_t v = va_arg(*ap, int64_t);
k = mg_lld(tmp, v, s, h);
} else if (is_long == 1) {
long v = va_arg(*ap, long);
k = mg_lld(tmp, s ? (int64_t) v : (int64_t) (unsigned long) v, s, h);
} else {
int v = va_arg(*ap, int);
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; pad == ' ' && !minus && k < w && j + k < w; j++)
n += scpy(out, param, &pad, 1);
n += scpy(out, param, (char *) "0x", xl);
for (j = 0; pad == '0' && k < w && j + k < w; j++)
n += scpy(out, param, &pad, 1);
n += scpy(out, param, tmp, k);
for (j = 0; pad == ' ' && minus && k < w && j + k < w; j++)
n += scpy(out, param, &pad, 1);
} else if (c == 'M') {
mg_pm_t f = va_arg(*ap, mg_pm_t);
n += f(out, param, ap);
} else if (c == 'c') {
int ch = va_arg(*ap, int);
out((char) ch, param);
n++;
} else if (c == 'H') {
// Print hex-encoded double-quoted string
size_t bl = (size_t) va_arg(*ap, int);
uint8_t *p = va_arg(*ap, uint8_t *), dquote = '"';
const char *hex = "0123456789abcdef";
n += scpy(out, param, (char *) &dquote, 1);
for (j = 0; j < bl; j++) {
n += scpy(out, param, (char *) &hex[(p[j] >> 4) & 15], 1);
n += scpy(out, param, (char *) &hex[p[j] & 15], 1);
}
n += scpy(out, param, (char *) &dquote, 1);
} else if (c == 'V') {
// Print base64-encoded double-quoted string
size_t len = (size_t) va_arg(*ap, int);
uint8_t *buf = va_arg(*ap, uint8_t *);
n += bcpy(out, param, buf, len);
} else if (c == 's' || c == 'Q') {
char *p = va_arg(*ap, char *);
size_t (*f)(void (*)(char, void *), void *, char *, size_t) =
c == 's' ? scpy : qcpy;
if (pr == ~0U) pr = p == NULL ? 0 : strlen(p);
for (j = 0; !minus && pr < w && j + pr < w; j++)
n += f(out, param, &pad, 1);
n += f(out, param, p, pr);
for (j = 0; minus && pr < w && j + pr < w; j++)
n += f(out, param, &pad, 1);
} else if (c == '%') {
out('%', param);
n++;
} else {
out('%', param);
out(c, param);
n += 2;
}
i++;
} else {
out(fmt[i], param), n++, i++;
}
}
return n;
}

319
src/str.c
View File

@ -83,10 +83,6 @@ const char *mg_strstr(const struct mg_str haystack,
return NULL;
}
static bool is_digit(int c) {
return c >= '0' && c <= '9';
}
static bool is_space(int c) {
return c == ' ' || c == '\r' || c == '\n' || c == '\t';
}
@ -271,318 +267,3 @@ int64_t mg_to64(struct mg_str str) {
}
return result * neg;
}
size_t mg_lld(char *buf, int64_t val, bool is_signed, bool is_hex) {
const char *letters = "0123456789abcdef";
uint64_t v = (uint64_t) val;
size_t s = 0, n, i;
if (is_signed && val < 0) buf[s++] = '-', v = (uint64_t) (-val);
// This loop prints a number in reverse order. I guess this is because we
// write numbers from right to left: least significant digit comes last.
// Maybe because we use Arabic numbers, and Arabs write RTL?
if (is_hex) {
for (n = 0; v; v >>= 4) buf[s + n++] = letters[v & 15];
} else {
for (n = 0; v; v /= 10) buf[s + n++] = letters[v % 10];
}
// Reverse a string
for (i = 0; i < n / 2; i++) {
char t = buf[s + i];
buf[s + i] = buf[s + n - i - 1], buf[s + n - i - 1] = t;
}
if (val == 0) buf[n++] = '0'; // Handle special case
return n + s;
}
static size_t mg_copys(char *buf, size_t len, size_t n, char *p, size_t k) {
size_t j = 0;
for (j = 0; j < k && p[j]; j++)
if (j + n < len) buf[n + j] = p[j];
return j;
}
static char mg_esc(int c, bool esc) {
const char *p, *esc1 = "\b\f\n\r\t\\\"", *esc2 = "bfnrt\\\"";
for (p = esc ? esc1 : esc2; *p != '\0'; p++) {
if (*p == c) return esc ? esc2[p - esc1] : esc1[p - esc2];
}
return 0;
}
static char mg_escape(int c) {
return mg_esc(c, true);
}
static size_t mg_copyq(char *buf, size_t len, size_t n, char *p, size_t k) {
size_t j = 0, extra = 2;
if (n < len) buf[n++] = '"';
for (j = 0; j < k && p[j]; j++) {
char c = mg_escape(p[j]);
if (c) {
if (j + n < len) buf[n + j] = '\\';
n++;
extra++;
if (j + n < len) buf[n + j] = c;
} else {
if (j + n < len) buf[n + j] = p[j];
}
}
if (j + n < len) buf[n + j] = '"';
return j + extra;
}
// %M specifier printer function
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;
while (fmt[i] != '\0') {
if (fmt[i] == '%') {
size_t j, k, x = 0, is_long = 0, w = 0 /* width */, pr = ~0U /* prec */;
char pad = ' ', minus = 0, c = fmt[++i];
if (c == '#') x++, c = fmt[++i];
if (c == '-') minus++, c = fmt[++i];
if (c == '0') pad = '0', c = fmt[++i];
while (is_digit(c)) w *= 10, w += (size_t) (c - '0'), c = fmt[++i];
if (c == '.') {
c = fmt[++i];
if (c == '*') {
pr = (size_t) va_arg(*ap, int);
c = fmt[++i];
} else {
pr = 0;
while (is_digit(c)) pr *= 10, pr += (size_t) (c - '0'), c = fmt[++i];
}
}
while (c == 'h') c = fmt[++i]; // Treat h and hh as int
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 == 'd' || c == 'u' || c == 'x' || c == 'X' || c == 'p' ||
c == 'g') {
bool s = (c == 'd'), h = (c == 'x' || c == 'X' || c == 'p');
char tmp[40];
size_t xl = x ? 2 : 0;
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);
} else if (is_long == 2) {
int64_t v = va_arg(*ap, int64_t);
k = mg_lld(tmp, v, s, h);
} else if (is_long == 1) {
long v = va_arg(*ap, long);
k = mg_lld(tmp, s ? (int64_t) v : (int64_t) (unsigned long) v, s, h);
} else {
int v = va_arg(*ap, int);
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; pad == ' ' && !minus && k < w && j + k < w; j++)
n += mg_copys(buf, len, n, &pad, 1);
n += mg_copys(buf, len, n, (char *) "0x", xl);
for (j = 0; pad == '0' && k < w && j + k < w; j++)
n += mg_copys(buf, len, n, &pad, 1);
n += mg_copys(buf, len, n, tmp, k);
for (j = 0; pad == ' ' && minus && k < w && j + k < w; j++)
n += mg_copys(buf, len, n, &pad, 1);
} else if (c == 'M') {
char *dst = buf ? buf + n : NULL;
size_t dstlen = n < len ? len - n : 0;
mg_spfn_t fn = va_arg(*ap, mg_spfn_t);
n += fn(dst, dstlen, ap);
} else if (c == 'c') {
int p = va_arg(*ap, int);
if (n < len) buf[n] = (char) p;
n++;
} else if (c == 'H') {
// Print hex-encoded double-quoted string
size_t bl = (size_t) va_arg(*ap, int);
uint8_t *p = va_arg(*ap, uint8_t *), dquote = '"';
const char *hex = "0123456789abcdef";
n += mg_copys(buf, len, n, (char *) &dquote, 1);
for (j = 0; j < bl; j++) {
n += mg_copys(buf, len, n, (char *) &hex[(p[j] >> 4) & 15], 1);
n += mg_copys(buf, len, n, (char *) &hex[p[j] & 15], 1);
}
n += mg_copys(buf, len, n, (char *) &dquote, 1);
} else if (c == 'V') {
// Print base64-encoded double-quoted string
size_t bl = (size_t) va_arg(*ap, int);
uint8_t *p = va_arg(*ap, uint8_t *), dquote = '"';
const char *t =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
n += mg_copys(buf, len, n, (char *) &dquote, 1);
for (j = 0; j < bl; j += 3) {
uint8_t c1 = p[j], c2 = j + 1 < bl ? p[j + 1] : 0,
c3 = j + 2 < bl ? p[j + 2] : 0;
char tmp[4] = {t[c1 >> 2], t[(c1 & 3) << 4 | (c2 >> 4)], '=', '='};
if (j + 1 < bl) tmp[2] = t[(c2 & 15) << 2 | (c3 >> 6)];
if (j + 2 < bl) tmp[3] = t[c3 & 63];
n += mg_copys(buf, len, n, tmp, sizeof(tmp));
}
n += mg_copys(buf, len, n, (char *) &dquote, 1);
} else if (c == 's' || c == 'Q') {
char *p = va_arg(*ap, char *);
size_t (*fn)(char *, size_t, size_t, char *, size_t) =
c == 's' ? mg_copys : mg_copyq;
if (pr == ~0U) pr = p == NULL ? 0 : strlen(p);
for (j = 0; !minus && pr < w && j + pr < w; j++)
n += fn(buf, len, n, &pad, 1);
n += fn(buf, len, n, p, pr);
for (j = 0; minus && pr < w && j + pr < w; j++)
n += fn(buf, len, n, &pad, 1);
} else if (c == '%') {
if (n < len) buf[n] = '%';
n++;
} else {
if (n < len) buf[n] = '%';
n++;
if (n < len) buf[n] = c;
n++;
}
i++;
} else {
if (n < len) buf[n] = fmt[i];
n++, i++;
}
}
if (n < len) buf[n] = '\0';
return n;
}
double mg_atod(const char *p, int len, int *numlen) {
double d = 0.0;
int i = 0, sign = 1;
// Sign
if (i < len && *p == '-') {
sign = -1, i++;
} else if (i < len && *p == '+') {
i++;
}
// Decimal
for (; i < len && is_digit(p[i]); i++) {
d *= 10.0;
d += p[i] - '0';
}
d *= sign;
// Fractional
if (i < len && p[i] == '.') {
double frac = 0.0, base = 0.1;
i++;
for (; i < len && is_digit(p[i]); i++) {
frac += base * (p[i] - '0');
base /= 10.0;
}
d += frac * sign;
}
// Exponential
if (i < len && (p[i] == 'e' || p[i] == 'E')) {
int j, exp = 0, minus = 0;
i++;
if (i < len && p[i] == '-') minus = 1, i++;
if (i < len && p[i] == '+') i++;
while (i < len && is_digit(p[i]) && exp < 308)
exp = exp * 10 + (p[i++] - '0');
if (minus) exp = -exp;
for (j = 0; j < exp; j++) d *= 10.0;
for (j = 0; j < -exp; j++) d /= 10.0;
}
if (numlen != NULL) *numlen = i;
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,18 +31,25 @@ 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_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);
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, ...);
char *mg_hex(const void *buf, size_t len, char *dst);
void mg_unhex(const char *buf, size_t len, unsigned char *to);
unsigned long mg_unhexn(const char *s, size_t len);
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);
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);
int64_t mg_to64(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);
double mg_atod(const char *buf, int len, int *numlen);
size_t mg_dtoa(char *buf, size_t len, double d, int width);
typedef void (*mg_pc_t)(char, void *); // Custom putchar
typedef size_t (*mg_pm_t)(mg_pc_t, void *, va_list *); // %M printer
void mg_putchar_stralloc(char ch, void *param); // Print to malloced str
size_t mg_vrprintf(void (*)(char, void *), void *, const char *fmt, va_list *);
size_t mg_rprintf(void (*fn)(char, void *), void *, const char *fmt, ...);
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_asprintf(char **, size_t, const char *fmt, ...);
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);

View File

@ -1374,17 +1374,16 @@ static bool sccmp(const char *s1, const char *s2, int expected) {
return n1 == expected;
}
static size_t pf1(char *buf, size_t len, va_list *ap) {
static size_t pf1(void (*out)(char, void *), void *ptr, va_list *ap) {
int a = va_arg(*ap, int);
int b = va_arg(*ap, int);
return mg_snprintf(buf, len, "%d", a + b);
return mg_rprintf(out, ptr, "%d", a + b);
}
static size_t pf2(char *buf, size_t len, va_list *ap) {
static size_t pf2(void (*out)(char, void *), void *ptr, va_list *ap) {
int cnt = va_arg(*ap, int);
size_t n = 0;
while (cnt-- > 0)
n += mg_snprintf(buf ? buf + n : 0, n < len ? len - n : 0, "%d", cnt);
while (cnt-- > 0) n += mg_rprintf(out, ptr, "%d", cnt);
return n;
}
@ -1491,7 +1490,13 @@ static void test_str(void) {
free(p);
p = mg_mprintf("[%M,%d]", pf2, 10, 7);
// printf("-> %s\n", p);
ASSERT(strcmp(p, "[9876543210,7]") == 0);
free(p);
p = mg_mprintf("[%M", pf2, 10);
mg_rprintf(mg_putchar_stralloc, &p, ",");
mg_rprintf(mg_putchar_stralloc, &p, "%d]", 7);
printf("-> %s\n", p);
ASSERT(strcmp(p, "[9876543210,7]") == 0);
free(p);
}