diff options
author | Lennart Poettering <lennart@poettering.net> | 2020-04-30 15:07:45 +0200 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2020-05-04 10:11:19 +0200 |
commit | ddd6a22a0fb034c763495fa1f3b6ebdd6a669e27 (patch) | |
tree | ac7bfb9eec91128813c0c9a54522f3983d411a36 | |
parent | 2d4f8cf467b6825c91276808250823a29ab461fe (diff) | |
download | systemd-ddd6a22a0fb034c763495fa1f3b6ebdd6a669e27.tar.gz |
basic: add STRCASE_IN_SET() which is to STR_IN_SET() what strcaseeq() is to streq()
-rw-r--r-- | src/basic/hostname-util.c | 19 | ||||
-rw-r--r-- | src/basic/parse-util.c | 18 | ||||
-rw-r--r-- | src/basic/strv.c | 12 | ||||
-rw-r--r-- | src/basic/strv.h | 13 | ||||
-rw-r--r-- | src/shared/calendarspec.c | 17 |
5 files changed, 60 insertions, 19 deletions
diff --git a/src/basic/hostname-util.c b/src/basic/hostname-util.c index 8932c552c8..5a2d60f21d 100644 --- a/src/basic/hostname-util.c +++ b/src/basic/hostname-util.c @@ -12,6 +12,7 @@ #include "hostname-util.h" #include "macro.h" #include "string-util.h" +#include "strv.h" bool hostname_is_set(void) { struct utsname u; @@ -180,14 +181,16 @@ bool is_localhost(const char *hostname) { /* This tries to identify local host and domain names * described in RFC6761 plus the redhatism of localdomain */ - return strcaseeq(hostname, "localhost") || - strcaseeq(hostname, "localhost.") || - strcaseeq(hostname, "localhost.localdomain") || - strcaseeq(hostname, "localhost.localdomain.") || - endswith_no_case(hostname, ".localhost") || - endswith_no_case(hostname, ".localhost.") || - endswith_no_case(hostname, ".localhost.localdomain") || - endswith_no_case(hostname, ".localhost.localdomain."); + return STRCASE_IN_SET( + hostname, + "localhost", + "localhost.", + "localhost.localdomain", + "localhost.localdomain.") || + endswith_no_case(hostname, ".localhost") || + endswith_no_case(hostname, ".localhost.") || + endswith_no_case(hostname, ".localhost.localdomain") || + endswith_no_case(hostname, ".localhost.localdomain."); } bool is_gateway_hostname(const char *hostname) { diff --git a/src/basic/parse-util.c b/src/basic/parse-util.c index 8de5cd5c56..59f8a31cec 100644 --- a/src/basic/parse-util.c +++ b/src/basic/parse-util.c @@ -18,14 +18,28 @@ #include "process-util.h" #include "stat-util.h" #include "string-util.h" +#include "strv.h" int parse_boolean(const char *v) { if (!v) return -EINVAL; - if (streq(v, "1") || strcaseeq(v, "yes") || strcaseeq(v, "y") || strcaseeq(v, "true") || strcaseeq(v, "t") || strcaseeq(v, "on")) + if (STRCASE_IN_SET(v, + "1", + "yes", + "y", + "true", + "t", + "on")) return 1; - else if (streq(v, "0") || strcaseeq(v, "no") || strcaseeq(v, "n") || strcaseeq(v, "false") || strcaseeq(v, "f") || strcaseeq(v, "off")) + + if (STRCASE_IN_SET(v, + "0", + "no", + "n", + "false", + "f", + "off")) return 0; return -EINVAL; diff --git a/src/basic/strv.c b/src/basic/strv.c index 096cb4e5d4..f1d2bb5190 100644 --- a/src/basic/strv.c +++ b/src/basic/strv.c @@ -28,6 +28,18 @@ char *strv_find(char * const *l, const char *name) { return NULL; } +char *strv_find_case(char * const *l, const char *name) { + char * const *i; + + assert(name); + + STRV_FOREACH(i, l) + if (strcaseeq(*i, name)) + return *i; + + return NULL; +} + char *strv_find_prefix(char * const *l, const char *name) { char * const *i; diff --git a/src/basic/strv.h b/src/basic/strv.h index d7166ce5f3..0837e65a18 100644 --- a/src/basic/strv.h +++ b/src/basic/strv.h @@ -14,9 +14,13 @@ #include "string-util.h" char *strv_find(char * const *l, const char *name) _pure_; +char *strv_find_case(char * const *l, const char *name) _pure_; char *strv_find_prefix(char * const *l, const char *name) _pure_; char *strv_find_startswith(char * const *l, const char *name) _pure_; +#define strv_contains(l, s) (!!strv_find((l), (s))) +#define strv_contains_case(l, s) (!!strv_find_case((l), (s))) + char **strv_free(char **l); DEFINE_TRIVIAL_CLEANUP_FUNC(char**, strv_free); #define _cleanup_strv_free_ _cleanup_(strv_freep) @@ -54,8 +58,6 @@ static inline bool strv_equal(char * const *a, char * const *b) { return strv_compare(a, b) == 0; } -#define strv_contains(l, s) (!!strv_find((l), (s))) - char **strv_new_internal(const char *x, ...) _sentinel_; char **strv_new_ap(const char *x, va_list ap); #define strv_new(...) strv_new_internal(__VA_ARGS__, NULL) @@ -156,6 +158,13 @@ void strv_print(char * const *l); _x && strv_contains(STRV_MAKE(__VA_ARGS__), _x); \ }) +#define STRCASE_IN_SET(x, ...) strv_contains_case(STRV_MAKE(__VA_ARGS__), x) +#define STRCASEPTR_IN_SET(x, ...) \ + ({ \ + const char* _x = (x); \ + _x && strv_contains_case(STRV_MAKE(__VA_ARGS__), _x); \ + }) + #define STARTSWITH_SET(p, ...) \ ({ \ const char *_p = (p); \ diff --git a/src/shared/calendarspec.c b/src/shared/calendarspec.c index 217ab3fbaf..4103cf3ef0 100644 --- a/src/shared/calendarspec.c +++ b/src/shared/calendarspec.c @@ -17,6 +17,7 @@ #include "process-util.h" #include "sort-util.h" #include "string-util.h" +#include "strv.h" #include "time-util.h" #define BITS_WEEKDAYS 127 @@ -961,9 +962,10 @@ int calendar_spec_from_string(const char *p, CalendarSpec **spec) { if (r < 0) return r; - } else if (strcaseeq(p, "annually") || - strcaseeq(p, "yearly") || - strcaseeq(p, "anually") /* backwards compatibility */ ) { + } else if (STRCASE_IN_SET(p, + "annually", + "yearly", + "anually") /* backwards compatibility */ ) { r = const_chain(1, &c->month); if (r < 0) @@ -1022,10 +1024,11 @@ int calendar_spec_from_string(const char *p, CalendarSpec **spec) { if (r < 0) return r; - } else if (strcaseeq(p, "biannually") || - strcaseeq(p, "bi-annually") || - strcaseeq(p, "semiannually") || - strcaseeq(p, "semi-annually")) { + } else if (STRCASE_IN_SET(p, + "biannually", + "bi-annually", + "semiannually", + "semi-annually")) { r = const_chain(1, &c->month); if (r < 0) |