mirror of
https://github.com/cesanta/mongoose.git
synced 2025-01-14 01:38:01 +08:00
mg_dtoa, mg_atod, mg_lld made static
This commit is contained in:
parent
d2a27d3d91
commit
6b69bef2ef
96
mongoose.c
96
mongoose.c
@ -475,52 +475,6 @@ static bool is_digit(int c) {
|
||||
return c >= '0' && c <= '9';
|
||||
}
|
||||
|
||||
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';
|
||||
@ -552,7 +506,7 @@ static int xisnan(double x) {
|
||||
0x7ff00000;
|
||||
}
|
||||
|
||||
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) {
|
||||
char buf[40];
|
||||
int i, s = 0, n = 0, e = 0;
|
||||
double t, mul, saved;
|
||||
@ -609,7 +563,7 @@ size_t mg_dtoa(char *dst, size_t dstlen, double d, int width) {
|
||||
return mg_snprintf(dst, dstlen, "%s", buf);
|
||||
}
|
||||
|
||||
size_t mg_lld(char *buf, int64_t val, bool is_signed, bool is_hex) {
|
||||
static 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;
|
||||
@ -2461,6 +2415,52 @@ static int mg_pass_string(const char *s, int len) {
|
||||
return MG_JSON_INVALID;
|
||||
}
|
||||
|
||||
static 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;
|
||||
}
|
||||
|
||||
int mg_json_get(struct mg_str json, const char *path, int *toklen) {
|
||||
const char *s = json.ptr;
|
||||
int len = (int) json.len;
|
||||
|
@ -787,10 +787,6 @@ typedef void (*mg_pfn_t)(char, void *); // Custom putchar
|
||||
typedef size_t (*mg_pm_t)(mg_pfn_t, void *, va_list *); // %M printer
|
||||
void mg_pfn_iobuf(char ch, void *param); // iobuf printer
|
||||
|
||||
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);
|
||||
|
||||
size_t mg_vxprintf(void (*)(char, void *), void *, const char *fmt, va_list *);
|
||||
size_t mg_xprintf(void (*fn)(char, void *), void *, const char *fmt, ...);
|
||||
size_t mg_vsnprintf(char *buf, size_t len, const char *fmt, va_list *ap);
|
||||
|
50
src/fmt.c
50
src/fmt.c
@ -64,52 +64,6 @@ static bool is_digit(int c) {
|
||||
return c >= '0' && c <= '9';
|
||||
}
|
||||
|
||||
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';
|
||||
@ -141,7 +95,7 @@ static int xisnan(double x) {
|
||||
0x7ff00000;
|
||||
}
|
||||
|
||||
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) {
|
||||
char buf[40];
|
||||
int i, s = 0, n = 0, e = 0;
|
||||
double t, mul, saved;
|
||||
@ -198,7 +152,7 @@ size_t mg_dtoa(char *dst, size_t dstlen, double d, int width) {
|
||||
return mg_snprintf(dst, dstlen, "%s", buf);
|
||||
}
|
||||
|
||||
size_t mg_lld(char *buf, int64_t val, bool is_signed, bool is_hex) {
|
||||
static 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;
|
||||
|
@ -7,10 +7,6 @@ typedef void (*mg_pfn_t)(char, void *); // Custom putchar
|
||||
typedef size_t (*mg_pm_t)(mg_pfn_t, void *, va_list *); // %M printer
|
||||
void mg_pfn_iobuf(char ch, void *param); // iobuf printer
|
||||
|
||||
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);
|
||||
|
||||
size_t mg_vxprintf(void (*)(char, void *), void *, const char *fmt, va_list *);
|
||||
size_t mg_xprintf(void (*fn)(char, void *), void *, const char *fmt, ...);
|
||||
size_t mg_vsnprintf(char *buf, size_t len, const char *fmt, va_list *ap);
|
||||
|
46
src/json.c
46
src/json.c
@ -28,6 +28,52 @@ static int mg_pass_string(const char *s, int len) {
|
||||
return MG_JSON_INVALID;
|
||||
}
|
||||
|
||||
static 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;
|
||||
}
|
||||
|
||||
int mg_json_get(struct mg_str json, const char *path, int *toklen) {
|
||||
const char *s = json.ptr;
|
||||
int len = (int) json.len;
|
||||
|
@ -1424,6 +1424,11 @@ static size_t pf2(void (*out)(char, void *), void *ptr, va_list *ap) {
|
||||
return n;
|
||||
}
|
||||
|
||||
static bool chkdbl(struct mg_str s, double val) {
|
||||
double d, tolerance = 1e-14;
|
||||
return mg_json_get_num(s, "$", &d) && fabs(val - d) < tolerance;
|
||||
}
|
||||
|
||||
static void test_str(void) {
|
||||
{
|
||||
struct mg_str s = mg_strdup(mg_str("a"));
|
||||
@ -1445,15 +1450,11 @@ static void test_str(void) {
|
||||
ASSERT(sccmp("a", "A1", -49));
|
||||
|
||||
{
|
||||
int n;
|
||||
double tolerance = 1e-14;
|
||||
ASSERT(mg_atod("1.23", 4, &n) == 1.23 && n == 4);
|
||||
ASSERT(mg_atod("1.23", 3, &n) == 1.2 && n == 3);
|
||||
ASSERT(mg_atod("1.23", 2, &n) == 1 && n == 2);
|
||||
ASSERT(mg_atod("1.23 ", 5, &n) - 1.23 < tolerance && n == 4);
|
||||
ASSERT(mg_atod("-0.01 ", 6, &n) + 0.01 < tolerance);
|
||||
ASSERT(mg_atod("-0.5e2", 6, &n) + 50 < tolerance);
|
||||
ASSERT(mg_atod("123e-3", 6, &n) - 0.123 < tolerance);
|
||||
ASSERT(chkdbl(mg_str_n("1.23", 3), 1.2));
|
||||
ASSERT(chkdbl(mg_str("1.23 "), 1.23));
|
||||
ASSERT(chkdbl(mg_str("-0.01 "), -0.01));
|
||||
ASSERT(chkdbl(mg_str("-0.5e2"), -50.0));
|
||||
ASSERT(chkdbl(mg_str("123e-3"), 0.123));
|
||||
}
|
||||
|
||||
ASSERT(sn("%d", 0));
|
||||
|
Loading…
x
Reference in New Issue
Block a user