]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
string-util: add minor optimization to strrstr()
authorLennart Poettering <lennart@amutable.com>
Wed, 15 Apr 2026 14:06:04 +0000 (16:06 +0200)
committerLennart Poettering <lennart@amutable.com>
Fri, 17 Apr 2026 16:32:21 +0000 (18:32 +0200)
src/basic/string-util.c

index c3298bc8eeebd8c9f8f0f57e8f8ac3a3a0975c53..6168855a7579c8dc1fe40574269fbb75ac0f7037 100644 (file)
@@ -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;
 }