From: Mike Yuan Date: Wed, 5 Jun 2024 14:54:29 +0000 (+0200) Subject: string-util: modernize first_word a bit X-Git-Tag: v257-rc1~1130^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c1bf0571c0f94bbc530f99ae2d6044bd6fca4366;p=thirdparty%2Fsystemd.git string-util: modernize first_word a bit --- diff --git a/src/basic/string-util.c b/src/basic/string-util.c index d0d33a407a6..274a3f3a048 100644 --- a/src/basic/string-util.c +++ b/src/basic/string-util.c @@ -24,37 +24,26 @@ #include "utf8.h" char* first_word(const char *s, const char *word) { - size_t sl, wl; - const char *p; - assert(s); assert(word); - /* Checks if the string starts with the specified word, either - * followed by NUL or by whitespace. Returns a pointer to the - * NUL or the first character after the whitespace. */ - - sl = strlen(s); - wl = strlen(word); - - if (sl < wl) - return NULL; + /* Checks if the string starts with the specified word, either followed by NUL or by whitespace. + * Returns a pointer to the NUL or the first character after the whitespace. */ - if (wl == 0) + if (isempty(word)) return (char*) s; - if (memcmp(s, word, wl) != 0) + const char *p = startswith(s, word); + if (!p) return NULL; - - p = s + wl; - if (*p == 0) + if (*p == '\0') return (char*) p; - if (!strchr(WHITESPACE, *p)) + const char *nw = skip_leading_chars(p, WHITESPACE); + if (p == nw) return NULL; - p += strspn(p, WHITESPACE); - return (char*) p; + return (char*) nw; } char *strnappend(const char *s, const char *suffix, size_t b) {