Merge pull request #30725 from YHNdnzj/string-util

string-util,strv: follow-ups
This commit is contained in:
Mike Yuan 2024-01-04 20:34:41 +08:00 committed by GitHub
commit 17b1c60ccd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 48 additions and 59 deletions

View file

@ -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;

View file

@ -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);

View file

@ -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;
}

View file

@ -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);

View file

@ -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;

View file

@ -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';

View file

@ -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;

View file

@ -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);