[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20181221155819.0513c6dd@gandalf.local.home>
Date: Fri, 21 Dec 2018 15:58:19 -0500
From: Steven Rostedt <rostedt@...dmis.org>
To: Joe Perches <joe@...ches.com>
Cc: Linus Torvalds <torvalds@...ux-foundation.org>,
Linux List Kernel Mailing <linux-kernel@...r.kernel.org>,
Ingo Molnar <mingo@...nel.org>,
Andrew Morton <akpm@...ux-foundation.org>,
Namhyung Kim <namhyung@...nel.org>,
Masami Hiramatsu <mhiramat@...nel.org>,
Tom Zanussi <zanussi@...nel.org>,
Greg Kroah-Hartman <gregkh@...uxfoundation.org>
Subject: Re: [for-next][PATCH 23/24] string.h: Add strncmp_prefix() helper
macro
On Fri, 21 Dec 2018 12:46:47 -0800
Joe Perches <joe@...ches.com> wrote:
> I think all the underscores are unnecessary and confusing.
Here. I removed a beginning and ending underscore from each variable ;-)
-- Steve
diff --git a/include/linux/string.h b/include/linux/string.h
index 27d0482e5e05..7f88444471f7 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -14,6 +14,28 @@ extern void *memdup_user(const void __user *, size_t);
extern void *vmemdup_user(const void __user *, size_t);
extern void *memdup_user_nul(const void __user *, size_t);
+/**
+ * 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
+ */
+#define str_has_prefix(str, prefix) \
+ ({ \
+ const char *___prefix___ = (const char *)(prefix); \
+ int ___len___ = strlen(___prefix___); \
+ strncmp(str, ___prefix___, ___len___) == 0 ? \
+ ___len___ : 0; \
+ })
+
/*
* Include machine specific inline routines
*/
Powered by blists - more mailing lists