From: Daan De Meyer Date: Tue, 18 Apr 2023 11:11:45 +0000 (+0200) Subject: string-util: Add startswith_strv() X-Git-Tag: v254-rc1~673^2~3 X-Git-Url: http://git-history.diyao.me/?a=commitdiff_plain;h=70cc7ed97e474a5237f0ea183b9bd85ccb99f192;p=systemd%2F.git string-util: Add startswith_strv() This is the function version of STARTSWITH_SET(). We also move STARTSWITH_SET() to string-util.h as it fits more there than in strv.h and reimplement it using startswith_strv(). --- diff --git a/src/basic/string-util.c b/src/basic/string-util.c index cc2f8ecdab..c74ee67dfe 100644 --- a/src/basic/string-util.c +++ b/src/basic/string-util.c @@ -1283,3 +1283,15 @@ char *find_line_startswith(const char *haystack, const char *needle) { return p + strlen(needle); } + +char *startswith_strv(const char *string, char **strv) { + char *found = NULL; + + STRV_FOREACH(i, strv) { + found = startswith(string, *i); + if (found) + break; + } + + return found; +} diff --git a/src/basic/string-util.h b/src/basic/string-util.h index 75483924af..4430910e22 100644 --- a/src/basic/string-util.h +++ b/src/basic/string-util.h @@ -267,3 +267,8 @@ char *strdupspn(const char *a, const char *accept); char *strdupcspn(const char *a, const char *reject); char *find_line_startswith(const char *haystack, const char *needle); + +char *startswith_strv(const char *string, char **strv); + +#define STARTSWITH_SET(p, ...) \ + startswith_strv(p, STRV_MAKE(__VA_ARGS__)) diff --git a/src/basic/strv.h b/src/basic/strv.h index b4d3f121f9..544d46a3f8 100644 --- a/src/basic/strv.h +++ b/src/basic/strv.h @@ -200,18 +200,6 @@ static inline void strv_print(char * const *l) { _x && strv_contains_case(STRV_MAKE(__VA_ARGS__), _x); \ }) -#define STARTSWITH_SET(p, ...) \ - ({ \ - const char *_p = (p); \ - char *_found = NULL; \ - STRV_FOREACH(_i, STRV_MAKE(__VA_ARGS__)) { \ - _found = startswith(_p, *_i); \ - if (_found) \ - break; \ - } \ - _found; \ - }) - #define ENDSWITH_SET(p, ...) \ ({ \ const char *_p = (p); \