string: provide strends()

Implement a function for checking if a string ends with a different
string and add its kunit test cases.

Acked-by: Linus Walleij <linus.walleij@linaro.org>
Link: https://lore.kernel.org/r/20251112-gpio-shared-v4-1-b51f97b1abd8@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
This commit is contained in:
Bartosz Golaszewski
2025-11-12 14:55:30 +01:00
parent 3a86608788
commit 197b3f3c70
2 changed files with 31 additions and 0 deletions

View File

@@ -562,4 +562,22 @@ static inline bool strstarts(const char *str, const char *prefix)
return strncmp(str, prefix, strlen(prefix)) == 0;
}
/**
* strends - Check if a string ends with another string.
* @str - NULL-terminated string to check against @suffix
* @suffix - NULL-terminated string defining the suffix to look for in @str
*
* Returns:
* True if @str ends with @suffix. False in all other cases.
*/
static inline bool strends(const char *str, const char *suffix)
{
unsigned int str_len = strlen(str), suffix_len = strlen(suffix);
if (str_len < suffix_len)
return false;
return !(strcmp(str + str_len - suffix_len, suffix));
}
#endif /* _LINUX_STRING_H_ */