[master] 94209b5 move duration conversion to libvarnish for reuse outside vmod_std

Nils Goroll nils.goroll at uplex.de
Fri Oct 13 20:03:07 UTC 2017


commit 94209b5895966a6e2d9f01127f8a455b63d51e70
Author: Nils Goroll <nils.goroll at uplex.de>
Date:   Fri Oct 13 14:25:34 2017 +0200

    move duration conversion to libvarnish for reuse outside vmod_std

diff --git a/include/vnum.h b/include/vnum.h
index 8446dae..b245450 100644
--- a/include/vnum.h
+++ b/include/vnum.h
@@ -31,4 +31,5 @@
 /* from libvarnish/vnum.c */
 double VNUM(const char *p);
 double VNUMpfx(const char *p, const char **e);
+double VNUM_duration(const char *p);
 const char *VNUM_2bytes(const char *p, uintmax_t *r, uintmax_t rel);
diff --git a/lib/libvarnish/vnum.c b/lib/libvarnish/vnum.c
index d7e1343..5e30fc3 100644
--- a/lib/libvarnish/vnum.c
+++ b/lib/libvarnish/vnum.c
@@ -108,6 +108,61 @@ VNUM(const char *p)
 
 /**********************************************************************/
 
+double
+VNUM_duration(const char *p)
+{
+	const char *t;
+	double r, sc = 1.0;
+
+	if (p == NULL)
+		return (nan(""));
+
+	r = VNUMpfx(p, &t);
+
+	if (isnan(r) || t == NULL)
+		return (nan(""));
+
+	while (isspace(*t))
+		t++;
+
+	// keep in sync with vcc_expr.c vcc_TimeUnit()
+	switch (*t++) {
+	case 's':
+		break;
+	case 'm':
+		if (*t == 's') {
+			sc = 1e-3;
+			t++;
+		} else
+			sc = 60.0;
+		break;
+	case 'h':
+		sc = 60.0 * 60.0;
+		break;
+	case 'd':
+		sc = 60.0 * 60.0 * 24.0;
+		break;
+	case 'w':
+		sc = 60.0 * 60.0 * 24.0 * 7.0;
+		break;
+	case 'y':
+		sc = 60.0 * 60.0 * 24.0 * 365.0;
+		break;
+	default:
+		return (nan(""));
+	}
+
+	while (isspace(*t))
+		t++;
+
+	if (*t != '\0')
+		return (nan(""));
+
+	return (r * sc);
+}
+
+/**********************************************************************/
+
 const char *
 VNUM_2bytes(const char *p, uintmax_t *r, uintmax_t rel)
 {
diff --git a/lib/libvmod_std/vmod_std_conversions.c b/lib/libvmod_std/vmod_std_conversions.c
index 13b85de..18ac13d 100644
--- a/lib/libvmod_std/vmod_std_conversions.c
+++ b/lib/libvmod_std/vmod_std_conversions.c
@@ -48,55 +48,11 @@
 VCL_DURATION __match_proto__(td_std_duration)
 vmod_duration(VRT_CTX, VCL_STRING p, VCL_DURATION d)
 {
-	const char *e;
-	double r;
-
-	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
+	double r = VNUM_duration(p);
 
-	if (p == NULL)
-		return (d);
+	(void) ctx;
 
-	while (isspace(*p))
-		p++;
-
-	if (*p != '+' && *p != '-' && !isdigit(*p))
-		return (d);
-
-	e = NULL;
-
-	r = VNUMpfx(p, &e);
-
-	if (isnan(r) || e == NULL)
-		return (d);
-
-	while (isspace(*e))
-		e++;
-
-	/* NB: Keep this list synchronized with VCC */
-	switch (*e++) {
-	case 's': break;
-	case 'm':
-		if (*e == 's') {
-			r *= 1e-3;
-			e++;
-		} else
-			r *= 60.;
-		break;
-	case 'h': r *= 60.*60.; break;
-	case 'd': r *= 60.*60.*24.; break;
-	case 'w': r *= 60.*60.*24.*7.; break;
-	case 'y': r *= 60.*60.*24.*365.; break;
-	default:
-		return (d);
-	}
-
-	while (isspace(*e))
-		e++;
-
-	if (*e != '\0')
-		return (d);
-
-	return (r);
+	return (isnan(r) ? d : r);
 }
 
 VCL_INT __match_proto__(td_std_integer)


More information about the varnish-commit mailing list