]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
string-util: modernize first_word a bit
authorMike Yuan <me@yhndnzj.com>
Wed, 5 Jun 2024 14:54:29 +0000 (16:54 +0200)
committerMike Yuan <me@yhndnzj.com>
Sat, 15 Jun 2024 17:19:39 +0000 (19:19 +0200)
src/basic/string-util.c

index d0d33a407a673bfe2fe6ec3027d74569e0fb9547..274a3f3a048fed332a394c670b4fdbe4a4a2058f 100644 (file)
 #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) {