From c1bf0571c0f94bbc530f99ae2d6044bd6fca4366 Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Wed, 5 Jun 2024 16:54:29 +0200 Subject: [PATCH] string-util: modernize first_word a bit --- src/basic/string-util.c | 29 +++++++++-------------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/src/basic/string-util.c b/src/basic/string-util.c index d0d33a407a..274a3f3a04 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) { -- 2.25.1