[PATCH v6 16/19] string: Add strsuffix()

From: Alejandro Colomar

Date: Sat Nov 14 2026 - 10:24:57 EST


This is drop-in compatible with strends(), but is more useful, as it
gives more information.

Signed-off-by: Alejandro Colomar <alx@xxxxxxxxxx>
---
include/linux/string.h | 28 ++++++++++++++++++++++++++--
1 file changed, 26 insertions(+), 2 deletions(-)

diff --git a/include/linux/string.h b/include/linux/string.h
index 6b0de932e23a..2f7fb2c70662 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -47,8 +47,8 @@
/**
* streq - strings equal
*
- * @s1: string to compare
- * @s2: string to compare
+ * @s1: String to compare
+ * @s2: String to compare
*
* Compare two strings for equality.
*
@@ -610,6 +610,30 @@ static inline const char *strprefix(const char *s, const char *prefix)
}
#define strprefix(s, pfx) const_cast(CQChar_of(s) *, strprefix(s, pfx))

+/**
+ * strsuffix - string suffix
+ * @s: The string to be tested
+ * @suffix: The string to see if @s ends with
+ *
+ * Returns:
+ * * @s + strlen(@s) - strlen(@suffix) if @s ends with @suffix
+ * * NULL if @s does not end with @suffix
+ */
+__attribute__((nonnull(1, 2)))
+static inline const char *strsuffix(const char *s, const char *suffix)
+{
+ size_t len, sfxlen, off;
+
+ len = strlen(s);
+ sfxlen = strlen(suffix);
+ if (len < sfxlen)
+ return NULL;
+
+ off = len - sfxlen;
+ return streq(s + off, suffix) ? s + off : NULL;
+}
+#define strsuffix(s, sfx) const_cast(CQChar_of(s) *, strsuffix(s, sfx))
+
/**
* str_has_prefix - Test if a string has a given prefix
* @str: The string to test
--
2.55.0

Attachment: signature.asc
Description: PGP signature