From: Lennart Poettering Date: Wed, 15 Apr 2026 14:06:04 +0000 (+0200) Subject: string-util: add minor optimization to strrstr() X-Git-Tag: v261-rc1~456^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d7b6d89d903758f60a9ead5349257f59b3184955;p=thirdparty%2Fsystemd.git string-util: add minor optimization to strrstr() --- diff --git a/src/basic/string-util.c b/src/basic/string-util.c index c3298bc8eee..6168855a757 100644 --- a/src/basic/string-util.c +++ b/src/basic/string-util.c @@ -1519,13 +1519,17 @@ char* strrstr_internal(const char *haystack, const char *needle) { /* Special case: for the empty string we return the very last possible occurrence, i.e. *after* the * last char, not before. */ - if (*needle == 0) + if (needle[0] == 0) return (char*) strchr(haystack, 0); + /* Special case: for single character strings, just use optimized strrchr() */ + if (needle[1] == 0) + return (char*) strrchr(haystack, needle[0]); + for (const char *p = strstr(haystack, needle), *q; p; p = q) { q = strstr(p + 1, needle); if (!q) - return (char *) p; + return (char*) p; } return NULL; }