[PATCH v4 6/6] string: Make strprefix() const-qualifier-generic
From: Alejandro Colomar
Date: Wed Nov 11 2026 - 15:26:13 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 168ab1b1fc38..c37041ee089a 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -590,11 +590,12 @@ void memcpy_and_pad(void *dest, size_t dest_len, const void *src, size_t count,
* * @s+strlen(@prefix) if @s starts with @prefix
* * NULL if @s does not start with @prefix
*/
-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