string-util: add minor optimization to strrstr()

This commit is contained in:
Lennart Poettering
2026-04-17 18:32:21 +02:00
parent 2561159d39
commit d7b6d89d90
+6 -2
View 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;
}