diff options
author | Mike Yuan <me@yhndnzj.com> | 2024-01-04 13:34:41 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-04 13:34:41 +0100 |
commit | 17b1c60ccd283831cd22f7c6e7144444a2788966 (patch) | |
tree | 174b54ad7397a186f5ba26c49e2da864484892c2 /src | |
parent | network/netdev: call done() per netdev kind before freeing netdev name or so (diff) | |
parent | strv: rename strv_endswith to endswith_strv and dedup ENDSWITH_SET (diff) | |
download | systemd-17b1c60ccd283831cd22f7c6e7144444a2788966.tar.xz systemd-17b1c60ccd283831cd22f7c6e7144444a2788966.zip |
Merge pull request #30725 from YHNdnzj/string-util
string-util,strv: follow-ups
Diffstat (limited to 'src')
-rw-r--r-- | src/basic/string-util.c | 14 | ||||
-rw-r--r-- | src/basic/string-util.h | 5 | ||||
-rw-r--r-- | src/basic/strv.c | 30 | ||||
-rw-r--r-- | src/basic/strv.h | 24 | ||||
-rw-r--r-- | src/fundamental/string-util-fundamental.c | 16 | ||||
-rw-r--r-- | src/fundamental/string-util-fundamental.h | 4 | ||||
-rw-r--r-- | src/shared/discover-image.c | 2 | ||||
-rw-r--r-- | src/test/test-strv.c | 12 |
8 files changed, 48 insertions, 59 deletions
diff --git a/src/basic/string-util.c b/src/basic/string-util.c index f6453990bd..8b039ebd98 100644 --- a/src/basic/string-util.c +++ b/src/basic/string-util.c @@ -1420,18 +1420,6 @@ 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; -} - bool version_is_valid(const char *s) { if (isempty(s)) return false; @@ -1537,7 +1525,7 @@ char *strrstr(const char *haystack, const char *needle) { return strchr(haystack, 0); for (const char *p = haystack; *p; p++) - if (strncmp(p, needle, l) == 0) + if (strneq(p, needle, l)) f = p; return (char*) f; diff --git a/src/basic/string-util.h b/src/basic/string-util.h index bf427cd7f7..e162765aa7 100644 --- a/src/basic/string-util.h +++ b/src/basic/string-util.h @@ -291,11 +291,6 @@ 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__)) - bool version_is_valid(const char *s); bool version_is_valid_versionspec(const char *s); diff --git a/src/basic/strv.c b/src/basic/strv.c index 43a4f569bd..908e9e2513 100644 --- a/src/basic/strv.c +++ b/src/basic/strv.c @@ -706,6 +706,26 @@ int strv_extendf(char ***l, const char *format, ...) { return strv_consume(l, x); } +char* startswith_strv(const char *s, char * const *l) { + STRV_FOREACH(i, l) { + char *found = startswith(s, *i); + if (found) + return found; + } + + return NULL; +} + +char* endswith_strv(const char *s, char * const *l) { + STRV_FOREACH(i, l) { + char *found = endswith(s, *i); + if (found) + return found; + } + + return NULL; +} + char** strv_reverse(char **l) { size_t n; @@ -905,13 +925,3 @@ int _string_strv_ordered_hashmap_put(OrderedHashmap **h, const char *key, const } DEFINE_HASH_OPS_FULL(string_strv_hash_ops, char, string_hash_func, string_compare_func, free, char*, strv_free); - -char* strv_endswith(const char *s, char **l) { - STRV_FOREACH(i, l) { - char *e = endswith(s, *i); - if (e) - return (char*) e; - } - - return NULL; -} diff --git a/src/basic/strv.h b/src/basic/strv.h index 18df0f23f2..f1a8bc4910 100644 --- a/src/basic/strv.h +++ b/src/basic/strv.h @@ -159,6 +159,16 @@ static inline void strv_print(char * const *l) { strv_print_full(l, NULL); } +char* startswith_strv(const char *s, char * const *l); + +#define STARTSWITH_SET(p, ...) \ + startswith_strv(p, STRV_MAKE(__VA_ARGS__)) + +char* endswith_strv(const char *s, char * const *l); + +#define ENDSWITH_SET(p, ...) \ + endswith_strv(p, STRV_MAKE(__VA_ARGS__)) + #define strv_from_stdarg_alloca(first) \ ({ \ char **_l; \ @@ -202,18 +212,6 @@ static inline void strv_print(char * const *l) { _x && strv_contains_case(STRV_MAKE(__VA_ARGS__), _x); \ }) -#define ENDSWITH_SET(p, ...) \ - ({ \ - const char *_p = (p); \ - char *_found = NULL; \ - STRV_FOREACH(_i, STRV_MAKE(__VA_ARGS__)) { \ - _found = endswith(_p, *_i); \ - if (_found) \ - break; \ - } \ - _found; \ - }) - #define _FOREACH_STRING(uniq, x, y, ...) \ for (const char *x, * const*UNIQ_T(l, uniq) = STRV_MAKE_CONST(({ x = y; }), ##__VA_ARGS__); \ x; \ @@ -252,5 +250,3 @@ int _string_strv_hashmap_put(Hashmap **h, const char *key, const char *value HA int _string_strv_ordered_hashmap_put(OrderedHashmap **h, const char *key, const char *value HASHMAP_DEBUG_PARAMS); #define string_strv_hashmap_put(h, k, v) _string_strv_hashmap_put(h, k, v HASHMAP_DEBUG_SRC_ARGS) #define string_strv_ordered_hashmap_put(h, k, v) _string_strv_ordered_hashmap_put(h, k, v HASHMAP_DEBUG_SRC_ARGS) - -char* strv_endswith(const char *s, char **l); diff --git a/src/fundamental/string-util-fundamental.c b/src/fundamental/string-util-fundamental.c index a5bafc63f4..a18b2bc4c9 100644 --- a/src/fundamental/string-util-fundamental.c +++ b/src/fundamental/string-util-fundamental.c @@ -33,14 +33,14 @@ sd_char *startswith_no_case(const sd_char *s, const sd_char *prefix) { return (sd_char*) s + l; } -sd_char* endswith(const sd_char *s, const sd_char *postfix) { +sd_char* endswith(const sd_char *s, const sd_char *suffix) { size_t sl, pl; assert(s); - assert(postfix); + assert(suffix); sl = strlen(s); - pl = strlen(postfix); + pl = strlen(suffix); if (pl == 0) return (sd_char*) s + sl; @@ -48,20 +48,20 @@ sd_char* endswith(const sd_char *s, const sd_char *postfix) { if (sl < pl) return NULL; - if (strcmp(s + sl - pl, postfix) != 0) + if (!streq(s + sl - pl, suffix)) return NULL; return (sd_char*) s + sl - pl; } -sd_char* endswith_no_case(const sd_char *s, const sd_char *postfix) { +sd_char* endswith_no_case(const sd_char *s, const sd_char *suffix) { size_t sl, pl; assert(s); - assert(postfix); + assert(suffix); sl = strlen(s); - pl = strlen(postfix); + pl = strlen(suffix); if (pl == 0) return (sd_char*) s + sl; @@ -69,7 +69,7 @@ sd_char* endswith_no_case(const sd_char *s, const sd_char *postfix) { if (sl < pl) return NULL; - if (strcasecmp(s + sl - pl, postfix) != 0) + if (!strcaseeq(s + sl - pl, suffix)) return NULL; return (sd_char*) s + sl - pl; diff --git a/src/fundamental/string-util-fundamental.h b/src/fundamental/string-util-fundamental.h index b537b2e31c..419f1cc3da 100644 --- a/src/fundamental/string-util-fundamental.h +++ b/src/fundamental/string-util-fundamental.h @@ -59,8 +59,8 @@ static inline size_t strlen_ptr(const sd_char *s) { sd_char *startswith(const sd_char *s, const sd_char *prefix) _pure_; sd_char *startswith_no_case(const sd_char *s, const sd_char *prefix) _pure_; -sd_char *endswith(const sd_char *s, const sd_char *postfix) _pure_; -sd_char *endswith_no_case(const sd_char *s, const sd_char *postfix) _pure_; +sd_char *endswith(const sd_char *s, const sd_char *suffix) _pure_; +sd_char *endswith_no_case(const sd_char *s, const sd_char *suffix) _pure_; static inline bool isempty(const sd_char *a) { return !a || a[0] == '\0'; diff --git a/src/shared/discover-image.c b/src/shared/discover-image.c index e7e3a4d71c..ed89580d82 100644 --- a/src/shared/discover-image.c +++ b/src/shared/discover-image.c @@ -233,7 +233,7 @@ static int extract_image_basename( return r; if (format_suffixes) { - char *e = strv_endswith(name, format_suffixes); + char *e = endswith_strv(name, format_suffixes); if (!e) /* Format suffix is required */ return -EINVAL; diff --git a/src/test/test-strv.c b/src/test/test-strv.c index f4a45703d0..f70e2aa862 100644 --- a/src/test/test-strv.c +++ b/src/test/test-strv.c @@ -1006,12 +1006,12 @@ TEST(strv_find_first_field) { assert_se(streq_ptr(strv_find_first_field(STRV_MAKE("i", "k", "l", "m", "d", "c", "a", "b"), haystack), "j")); } -TEST(strv_endswith) { - assert_se(streq_ptr(strv_endswith("waldo", STRV_MAKE("xxx", "yyy", "ldo", "zzz")), "ldo")); - assert_se(streq_ptr(strv_endswith("waldo", STRV_MAKE("xxx", "yyy", "zzz")), NULL)); - assert_se(streq_ptr(strv_endswith("waldo", STRV_MAKE("waldo")), "waldo")); - assert_se(streq_ptr(strv_endswith("waldo", STRV_MAKE("w", "o", "ldo")), "o")); - assert_se(streq_ptr(strv_endswith("waldo", STRV_MAKE("knurz", "", "waldo")), "")); +TEST(endswith_strv) { + assert_se(streq_ptr(endswith_strv("waldo", STRV_MAKE("xxx", "yyy", "ldo", "zzz")), "ldo")); + assert_se(streq_ptr(endswith_strv("waldo", STRV_MAKE("xxx", "yyy", "zzz")), NULL)); + assert_se(streq_ptr(endswith_strv("waldo", STRV_MAKE("waldo")), "waldo")); + assert_se(streq_ptr(endswith_strv("waldo", STRV_MAKE("w", "o", "ldo")), "o")); + assert_se(streq_ptr(endswith_strv("waldo", STRV_MAKE("knurz", "", "waldo")), "")); } DEFINE_TEST_MAIN(LOG_INFO); |