[master] 71a5a71 Implement numerical tests by macros to eliminate code duplication.
Martin Blix Grydeland
martin at varnish-cache.org
Tue Oct 1 14:48:18 CEST 2013
commit 71a5a7183a938aa41217339c209b5c6c65592e86
Author: Martin Blix Grydeland <martin at varnish-software.com>
Date: Fri Sep 20 14:30:38 2013 +0200
Implement numerical tests by macros to eliminate code duplication.
diff --git a/lib/libvarnishapi/vsl_query.c b/lib/libvarnishapi/vsl_query.c
index d025237..43eb7ba 100644
--- a/lib/libvarnishapi/vsl_query.c
+++ b/lib/libvarnishapi/vsl_query.c
@@ -53,6 +53,20 @@ struct vslq_query {
struct vex *vex;
};
+#define VSLQ_TEST_NUMOP(TYPE, PRE_LHS, OP, PRE_RHS) \
+ switch (TYPE) { \
+ case VEX_INT: \
+ if (PRE_LHS##_int OP PRE_RHS##_int) \
+ return (1); \
+ return (0); \
+ case VEX_FLOAT: \
+ if (PRE_LHS##_float OP PRE_RHS##_float) \
+ return (1); \
+ return (0); \
+ default: \
+ WRONG("Wrong RHS type"); \
+ }
+
static int
vslq_test_rec(const struct vex *vex, const struct VSLC_ptr *rec)
{
@@ -100,83 +114,17 @@ vslq_test_rec(const struct vex *vex, const struct VSLC_ptr *rec)
/* Compare */
switch (vex->tok) {
case T_EQ: /* == */
- switch (rhs->type) {
- case VEX_INT:
- if (lhs_int == rhs->val_int)
- return (1);
- return (0);
- case VEX_FLOAT:
- if (lhs_float == rhs->val_float)
- return (1);
- return (0);
- default:
- WRONG("Wrong RHS type");
- }
+ VSLQ_TEST_NUMOP(rhs->type, lhs, ==, rhs->val);
case T_NEQ: /* != */
- switch (rhs->type) {
- case VEX_INT:
- if (lhs_int != rhs->val_int)
- return (1);
- return (0);
- case VEX_FLOAT:
- if (lhs_float != rhs->val_float)
- return (1);
- return (0);
- default:
- WRONG("Wrong RHS type");
- }
+ VSLQ_TEST_NUMOP(rhs->type, lhs, !=, rhs->val);
case '<': /* < */
- switch (rhs->type) {
- case VEX_INT:
- if (lhs_int < rhs->val_int)
- return (1);
- return (0);
- case VEX_FLOAT:
- if (lhs_float < rhs->val_float)
- return (1);
- return (0);
- default:
- WRONG("Wrong RHS type");
- }
+ VSLQ_TEST_NUMOP(rhs->type, lhs, <, rhs->val);
case '>':
- switch (rhs->type) {
- case VEX_INT:
- if (lhs_int > rhs->val_int)
- return (1);
- return (0);
- case VEX_FLOAT:
- if (lhs_float > rhs->val_float)
- return (1);
- return (0);
- default:
- WRONG("Wrong RHS type");
- }
+ VSLQ_TEST_NUMOP(rhs->type, lhs, >, rhs->val);
case T_LEQ: /* <= */
- switch (rhs->type) {
- case VEX_INT:
- if (lhs_int <= rhs->val_int)
- return (1);
- return (0);
- case VEX_FLOAT:
- if (lhs_float <= rhs->val_float)
- return (1);
- return (0);
- default:
- WRONG("Wrong RHS type");
- }
+ VSLQ_TEST_NUMOP(rhs->type, lhs, <=, rhs->val);
case T_GEQ: /* >= */
- switch (rhs->type) {
- case VEX_INT:
- if (lhs_int >= rhs->val_int)
- return (1);
- return (0);
- case VEX_FLOAT:
- if (lhs_float >= rhs->val_float)
- return (1);
- return (0);
- default:
- WRONG("Wrong RHS type");
- }
+ VSLQ_TEST_NUMOP(rhs->type, lhs, >=, rhs->val);
case T_SEQ: /* eq */
assert(rhs->type == VEX_STRING);
if (!strcmp(lhs_string, rhs->val_string))
More information about the varnish-commit
mailing list