string-util: add str_common_prefix() helper that determines length of common prefix of two strings

This commit is contained in:
Lennart Poettering
2025-02-17 14:57:03 +01:00
parent 42839efb10
commit f77f363c95
3 changed files with 37 additions and 0 deletions
+16
View File
@@ -1470,3 +1470,19 @@ char* strrstr(const char *haystack, const char *needle) {
}
return NULL;
}
size_t str_common_prefix(const char *a, const char *b) {
assert(a);
assert(b);
/* Returns the length of the common prefix of the two specified strings, or SIZE_MAX in case the
* strings are fully identical. */
for (size_t n = 0;; n++) {
char c = a[n];
if (c != b[n])
return n;
if (c == 0)
return SIZE_MAX;
}
}