[PATCH v6 07/19] string: Make strprefix() const-qualifier-generic
From: Alejandro Colomar
Date: Sat Nov 14 2026 - 10:23:07 EST
Add a macro of the same name, which returns a pointer that is
const-qualified iff the input string is const-qualified. The return
type is a possibly-const-qualified pointer to char, even if the input
is a possibly-const-qualified pointer to void, which is why we require
the CQChar_of() macro.
The function now needs to return 'const char *', because const_cast()
can only drop const, but never add it.
Suggested-by: Geert Uytterhoeven <geert@xxxxxxxxxxxxxx>
Cc: Joe Perches <joe@xxxxxxxxxxx>
Cc: David Laight <david.laight.linux@xxxxxxxxx>
Signed-off-by: Alejandro Colomar <alx@xxxxxxxxxx>
---
include/linux/string.h | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/include/linux/string.h b/include/linux/string.h
index ede8883c99a1..013c6169def0 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -603,11 +603,12 @@ void memcpy_and_pad(void *dest, size_t dest_len, const void *src, size_t count,
* * NULL if @s does not start with @prefix
*/
__attribute__((nonnull(1, 2)))
-static inline char *strprefix(const char *s, const char *prefix)
+static inline const char *strprefix(const char *s, const char *prefix)
{
size_t len = strlen(prefix);
- return strncmp(s, prefix, len) == 0 ? (char *) s + len : NULL;
+ return strncmp(s, prefix, len) == 0 ? s + len : NULL;
}
+#define strprefix(s, pfx) const_cast(CQChar_of(s) *, strprefix(s, pfx))
/**
* str_has_prefix - Test if a string has a given prefix
--
2.55.0
Attachment:
signature.asc
Description: PGP signature