From: Lennart Poettering Date: Fri, 26 Aug 2022 15:08:37 +0000 (+0200) Subject: compare: rework table in parse_compare_operator() to be array of structs X-Git-Tag: v252-rc1~273^2~9 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=38c09fa008b6c02fb01eb6b5b2d9931f04ff890c;p=thirdparty%2Fsystemd.git compare: rework table in parse_compare_operator() to be array of structs 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. --- diff --git a/src/shared/compare-operator.c b/src/shared/compare-operator.c index 2bd64add7f5..43f876cabe0 100644 --- a/src/shared/compare-operator.c +++ b/src/shared/compare-operator.c @@ -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; } }