aboutsummaryrefslogtreecommitdiff
path: root/include/linux/string.h
diff options
context:
space:
mode:
authorBartlomiej Zolnierkiewicz <[email protected]>2019-02-07 16:44:43 +0100
committerBartlomiej Zolnierkiewicz <[email protected]>2019-02-07 16:44:43 +0100
commit82ffd0454bd9bd57780966d47bfd56d579dd4fb3 (patch)
treea735cfea934b7c4eac4a2c228cd95620c1daf969 /include/linux/string.h
parent890d14d2d4b57ff5a149309da3ed36c8a529987f (diff)
parent8834f5600cf3c8db365e18a3d5cac2c2780c81e5 (diff)
Merge tag 'v5.0-rc5' of https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux into fbdev-for-next
Linux 5.0-rc5 Sync with upstream (which now contains fbdev-v5.0-rc3 changes) to prepare a base for fbdev-v5.1 changes.
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,
memcpy(dest, src, dest_len);
}
+/**
+ * str_has_prefix - Test if a string has a given prefix
+ * @str: The string to test
+ * @prefix: The string to see if @str starts with
+ *
+ * A common way to test a prefix of a string is to do:
+ * strncmp(str, prefix, sizeof(prefix) - 1)
+ *
+ * But this can lead to bugs due to typos, or if prefix is a pointer
+ * and not a constant. Instead use str_has_prefix().
+ *
+ * Returns: 0 if @str does not start with @prefix
+ strlen(@prefix) if @str does start with @prefix
+ */
+static __always_inline size_t str_has_prefix(const char *str, const char *prefix)
+{
+ size_t len = strlen(prefix);
+ return strncmp(str, prefix, len) == 0 ? len : 0;
+}
+
#endif /* _LINUX_STRING_H_ */