Various useful helpers (#41684)

Split out of #41543.

These don't make too much sense on their own, but they also don't really
hurt. They are preparation for #41543, but in order to make things
either to review I split these four commits out, since they are not
directly part of what the PR shall achieve
This commit is contained in:
Lennart Poettering
2026-04-18 09:50:19 +02:00
committed by GitHub
7 changed files with 71 additions and 5 deletions
+17 -2
View File
@@ -1519,17 +1519,32 @@ 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;
}
char* strrstr_no_case_internal(const char *haystack, const char *needle) {
if (!haystack || !needle)
return NULL;
for (const char *p = strchr(haystack, 0); p > haystack; p--)
if (startswith_no_case(p, needle))
return (char*) p;
return startswith_no_case(haystack, needle) ? (char*) haystack : NULL;
}
size_t str_common_prefix(const char *a, const char *b) {
assert(a);
assert(b);