]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
compare: rework table in parse_compare_operator() to be array of structs
authorLennart Poettering <lennart@poettering.net>
Fri, 26 Aug 2022 15:08:37 +0000 (17:08 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 1 Sep 2022 21:16:10 +0000 (23:16 +0200)
Let's change the lookup table to contain pairs of operator/strings,
instead of being indexed by operator.

The table isn't dense anymore, and this allows us to add alias strings
sooner or later.

src/shared/compare-operator.c

index 2bd64add7f5c899ebe212956630c810498c085cb..43f876cabe0c8256f8212a621e68eb1f65efb765 100644 (file)
@@ -6,16 +6,23 @@
 #include "string-util.h"
 
 CompareOperator parse_compare_operator(const char **s, CompareOperatorParseFlags flags) {
-        static const char *const prefix[_COMPARE_OPERATOR_MAX] = {
-                [COMPARE_FNMATCH_EQUAL] = "=$",
-                [COMPARE_FNMATCH_UNEQUAL] = "!=$",
-
-                [COMPARE_LOWER_OR_EQUAL] = "<=",
-                [COMPARE_GREATER_OR_EQUAL] = ">=",
-                [COMPARE_LOWER] = "<",
-                [COMPARE_GREATER] = ">",
-                [COMPARE_EQUAL] = "=",
-                [COMPARE_UNEQUAL] = "!=",
+        static const struct {
+                CompareOperator op;
+                const char *str;
+                CompareOperatorParseFlags valid_mask; /* If this operator appears when flags in mask not set, fail */
+                CompareOperatorParseFlags need_mask;  /* Skip over this operattor when flags in mask not set */
+        } table[] = {
+                { COMPARE_FNMATCH_EQUAL,    "=$",  .valid_mask = COMPARE_ALLOW_FNMATCH   },
+                { COMPARE_FNMATCH_UNEQUAL,  "!=$", .valid_mask = COMPARE_ALLOW_FNMATCH   },
+
+                { COMPARE_LOWER_OR_EQUAL,   "<="                                         },
+                { COMPARE_GREATER_OR_EQUAL, ">="                                         },
+                { COMPARE_LOWER,            "<"                                          },
+                { COMPARE_GREATER,          ">"                                          },
+                { COMPARE_STRING_EQUAL,     "=",   .need_mask = COMPARE_EQUAL_BY_STRING  },
+                { COMPARE_EQUAL,            "="                                          },
+                { COMPARE_STRING_UNEQUAL,   "!=",  .need_mask = COMPARE_EQUAL_BY_STRING  },
+                { COMPARE_UNEQUAL,          "!="                                         },
         };
 
         assert(s);
@@ -24,27 +31,19 @@ CompareOperator parse_compare_operator(const char **s, CompareOperatorParseFlags
                   * parse_compare_operator() are use on the same string? */
                 return _COMPARE_OPERATOR_INVALID;
 
-        for (CompareOperator i = 0; i < _COMPARE_OPERATOR_MAX; i++) {
+        for (size_t i = 0; i < ELEMENTSOF(table); i ++) {
                 const char *e;
 
-                if (!prefix[i])
+                if (table[i].need_mask != 0 && !FLAGS_SET(flags, table[i].need_mask))
                         continue;
 
-                e = startswith(*s, prefix[i]);
+                e = startswith(*s, table[i].str);
                 if (e) {
-                        if (!FLAGS_SET(flags, COMPARE_ALLOW_FNMATCH) && COMPARE_OPERATOR_IS_FNMATCH(i))
+                        if (table[i].valid_mask != 0 && !FLAGS_SET(flags, table[i].valid_mask))
                                 return _COMPARE_OPERATOR_INVALID;
 
                         *s = e;
-
-                        if (FLAGS_SET(flags, COMPARE_EQUAL_BY_STRING)) {
-                                if (i == COMPARE_EQUAL)
-                                        return COMPARE_STRING_EQUAL;
-                                if (i == COMPARE_UNEQUAL)
-                                        return COMPARE_STRING_UNEQUAL;
-                        }
-
-                        return i;
+                        return table[i].op;
                 }
         }