Commit 7b048402 authored by Poul-Henning Kamp's avatar Poul-Henning Kamp

More strtod() -> VNUM() conversions.

Add a VNUMpfx() variant which does not barf NAN on suffixes.
parent 7300ca4c
......@@ -30,4 +30,5 @@
/* from libvarnish/vnum.c */
double VNUM(const char *p);
double VNUMpfx(const char *p, const char **e);
const char *VNUM_2bytes(const char *p, uintmax_t *r, uintmax_t rel);
......@@ -44,7 +44,7 @@ TESTS = vnum_c_test
noinst_PROGRAMS = ${TESTS}
vnum_c_test_SOURCES = vnum.c
vnum_c_test_SOURCES = vnum.c vas.c
vnum_c_test_CFLAGS = -DNUM_C_TEST -include config.h
vnum_c_test_LDADD = ${LIBM}
......
......@@ -31,12 +31,14 @@
#include "config.h"
#include <ctype.h>
#include <errno.h>
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "vnum.h"
#include "vas.h"
static const char err_miss_num[] = "Missing number";
static const char err_invalid_num[] = "Invalid number";
......@@ -49,12 +51,15 @@ static const char err_invalid_suff[] = "Invalid suffix";
*/
double
VNUM(const char *p)
VNUMpfx(const char *p, const char **t)
{
intmax_t m = 0, ee = 0;
double ms = 1.0;
double es = 1.0, e = 1.0, ne = 0.0;
AN(p);
AN(t);
*t = NULL;
while (isspace(*p))
p++;
......@@ -86,26 +91,38 @@ VNUM(const char *p)
while (isspace(*p))
p++;
if (*p != '\0')
return (nan(""));
*t = p;
return (ms * m * pow(10., e + es * ee));
}
double
VNUM(const char *p)
{
const char *t;
double r;
r = VNUMpfx(p, &t);
if (t != NULL)
r = nan("");
return (r);
}
/**********************************************************************/
const char *
VNUM_2bytes(const char *p, uintmax_t *r, uintmax_t rel)
{
double fval;
char *end;
const char *end;
if (p == NULL || *p == '\0')
return (err_miss_num);
fval = strtod(p, &end);
if (end == p || !isfinite(fval))
fval = VNUMpfx(p, &end);
if (!isfinite(fval))
return (err_invalid_num);
if (*end == '\0') {
if (end == NULL) {
*r = (uintmax_t)fval;
return (NULL);
}
......
......@@ -39,6 +39,7 @@
#include "cache/cache.h"
#include "vnum.h"
#include "vrt.h"
#include "vsa.h"
#include "vtim.h"
......@@ -47,7 +48,7 @@
VCL_DURATION __match_proto__(td_std_duration)
vmod_duration(VRT_CTX, VCL_STRING p, VCL_DURATION d)
{
char *e;
const char *e;
double r;
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
......@@ -63,12 +64,9 @@ vmod_duration(VRT_CTX, VCL_STRING p, VCL_DURATION d)
e = NULL;
r = strtod(p, &e);
if (!isfinite(r))
return (d);
r = VNUMpfx(p, &e);
if (e == NULL)
if (!isfinite(r) || e == NULL)
return (d);
while(isspace(*e))
......@@ -170,7 +168,6 @@ vmod_ip(VRT_CTX, VCL_STRING s, VCL_IP d)
VCL_REAL __match_proto__(td_std_real)
vmod_real(VRT_CTX, VCL_STRING p, VCL_REAL d)
{
char *e;
double r;
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
......@@ -178,22 +175,11 @@ vmod_real(VRT_CTX, VCL_STRING p, VCL_REAL d)
if (p == NULL)
return (d);
while (isspace(*p))
p++;
if (*p != '+' && *p != '-' && !isdigit(*p))
return (d);
e = NULL;
r = strtod(p, &e);
r = VNUM(p);
if (!isfinite(r))
return (d);
if (e == NULL || *e != '\0')
return (d);
return (r);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment