]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: Outfactor numeric event comparison
authorKarl Fleischmann <karl.fleischmann@open-xchange.com>
Thu, 10 Nov 2022 12:27:35 +0000 (13:27 +0100)
committeraki.tuomi <aki.tuomi@open-xchange.com>
Mon, 21 Nov 2022 17:50:26 +0000 (17:50 +0000)
This function is later reused for size comparisons.

src/lib/event-filter.c

index 82f5f2535baa0c4aaf126f3d920d7eaaf6ade3ed..e33dcb2b0608cdd5b0cdc78329c8383f865e3cb8 100644 (file)
@@ -534,6 +534,29 @@ event_match_strlist(struct event *event, const struct event_field *wanted_field,
 
 }
 
+static bool
+event_filter_handle_numeric_operation(enum event_filter_node_op op,
+                                     intmax_t a, intmax_t b)
+{
+       switch (op) {
+       case EVENT_FILTER_OP_CMP_EQ:
+               return a == b;
+       case EVENT_FILTER_OP_CMP_GT:
+               return a > b;
+       case EVENT_FILTER_OP_CMP_LT:
+               return a < b;
+       case EVENT_FILTER_OP_CMP_GE:
+               return a >= b;
+       case EVENT_FILTER_OP_CMP_LE:
+               return a <= b;
+       case EVENT_FILTER_OP_AND:
+       case EVENT_FILTER_OP_OR:
+       case EVENT_FILTER_OP_NOT:
+               i_unreached();
+       }
+       i_unreached();
+}
+
 static bool
 event_match_field(struct event *event, const struct event_field *wanted_field,
                  enum event_filter_node_op op, bool use_strcmp)
@@ -575,23 +598,8 @@ event_match_field(struct event *event, const struct event_field *wanted_field,
        case EVENT_FIELD_VALUE_TYPE_INTMAX:
                if (wanted_field->value_type == EVENT_FIELD_VALUE_TYPE_INTMAX) {
                        /* compare against an integer */
-                       switch (op) {
-                       case EVENT_FILTER_OP_CMP_EQ:
-                               return field->value.intmax == wanted_field->value.intmax;
-                       case EVENT_FILTER_OP_CMP_GT:
-                               return field->value.intmax > wanted_field->value.intmax;
-                       case EVENT_FILTER_OP_CMP_LT:
-                               return field->value.intmax < wanted_field->value.intmax;
-                       case EVENT_FILTER_OP_CMP_GE:
-                               return field->value.intmax >= wanted_field->value.intmax;
-                       case EVENT_FILTER_OP_CMP_LE:
-                               return field->value.intmax <= wanted_field->value.intmax;
-                       case EVENT_FILTER_OP_AND:
-                       case EVENT_FILTER_OP_OR:
-                       case EVENT_FILTER_OP_NOT:
-                               i_unreached();
-                       }
-                       i_unreached();
+                       return event_filter_handle_numeric_operation(
+                               op, field->value.intmax, wanted_field->value.intmax);
                } else if (op != EVENT_FILTER_OP_CMP_EQ) {
                        /* we only support string equality comparisons */
                        return FALSE;