aboutsummaryrefslogtreecommitdiffstats
path: root/include/linux/string.h
diff options
context:
space:
mode:
Diffstat (limited to 'include/linux/string.h')
-rw-r--r--include/linux/string.h20
1 files changed, 20 insertions, 0 deletions
diff --git a/include/linux/string.h b/include/linux/string.h
index 27d0482e5e05..7927b875f80c 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -456,4 +456,24 @@ static inline void memcpy_and_pad(void *dest, size_t dest_len,
456 memcpy(dest, src, dest_len); 456 memcpy(dest, src, dest_len);
457} 457}
458 458
459/**
460 * str_has_prefix - Test if a string has a given prefix
461 * @str: The string to test
462 * @prefix: The string to see if @str starts with
463 *
464 * A common way to test a prefix of a string is to do:
465 * strncmp(str, prefix, sizeof(prefix) - 1)
466 *
467 * But this can lead to bugs due to typos, or if prefix is a pointer
468 * and not a constant. Instead use str_has_prefix().
469 *
470 * Returns: 0 if @str does not start with @prefix
471 strlen(@prefix) if @str does start with @prefix
472 */
473static __always_inline size_t str_has_prefix(const char *str, const char *prefix)
474{
475 size_t len = strlen(prefix);
476 return strncmp(str, prefix, len) == 0 ? len : 0;
477}
478
459#endif /* _LINUX_STRING_H_ */ 479#endif /* _LINUX_STRING_H_ */