basic: add STRCASE_IN_SET() which is to STR_IN_SET() what strcaseeq() is to streq()

This commit is contained in:
Lennart Poettering
2020-05-04 10:11:19 +02:00
committed by Zbigniew Jędrzejewski-Szmek
parent 2d4f8cf467
commit ddd6a22a0f
5 changed files with 60 additions and 19 deletions
+11 -8
View File
@@ -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) {
+16 -2
View File
@@ -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;
+12
View File
@@ -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;
+11 -2
View File
@@ -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); \
+10 -7
View File
@@ -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)