string-util: modernize string_contains_word_strv a bit

Also correct the comment on flags.
This commit is contained in:
Mike Yuan 2024-03-27 00:15:40 +08:00
parent a6fe020ab8
commit f8c700791e
No known key found for this signature in database
GPG key ID: 417471C0A40F58B3
2 changed files with 6 additions and 8 deletions

View file

@ -1268,18 +1268,16 @@ int string_extract_line(const char *s, size_t i, char **ret) {
}
}
int string_contains_word_strv(const char *string, const char *separators, char **words, const char **ret_word) {
/* In the default mode with no separators specified, we split on whitespace and
* don't coalesce separators. */
int string_contains_word_strv(const char *string, const char *separators, char * const *words, const char **ret_word) {
/* In the default mode with no separators specified, we split on whitespace and coalesce separators. */
const ExtractFlags flags = separators ? EXTRACT_DONT_COALESCE_SEPARATORS : 0;
const char *found = NULL;
int r;
for (const char *p = string;;) {
for (;;) {
_cleanup_free_ char *w = NULL;
int r;
r = extract_first_word(&p, &w, separators, flags);
r = extract_first_word(&string, &w, separators, flags);
if (r < 0)
return r;
if (r == 0)

View file

@ -271,7 +271,7 @@ char* string_erase(char *x);
int string_truncate_lines(const char *s, size_t n_lines, char **ret);
int string_extract_line(const char *s, size_t i, char **ret);
int string_contains_word_strv(const char *string, const char *separators, char **words, const char **ret_word);
int string_contains_word_strv(const char *string, const char *separators, char * const *words, const char **ret_word);
static inline int string_contains_word(const char *string, const char *separators, const char *word) {
return string_contains_word_strv(string, separators, STRV_MAKE(word), NULL);
}