[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <871s6ad2br.fsf@igel.home>
Date: Fri, 21 Dec 2018 21:58:32 +0100
From: Andreas Schwab <schwab@...ux-m68k.org>
To: Steven Rostedt <rostedt@...dmis.org>
Cc: Joe Perches <joe@...ches.com>,
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 Dez 21 2018, Steven Rostedt <rostedt@...dmis.org> wrote:
> On Fri, 21 Dec 2018 12:46:47 -0800
> Joe Perches <joe@...ches.com> wrote:
>
>> > + * @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 has_prefix().
>> > + *
>> > + * Returns: 0 if @str does not start with @prefix
>> > + strlen(@prefix) if @str does start with @prefix
>> > + */
>> > +#define has_prefix(str, prefix) \
>> > + ({ \
>> > + const char *____prefix____ = (const char *)(prefix); \
>> > + int ____len____ = strlen(____prefix____); \
>> > + strncmp(str, ____prefix____, ____len____) == 0 ? \
>> > + ____len____ : 0; \
>> > + })
>>
>> I think all the underscores are unnecessary and confusing.
>>
>
> Well, perhaps I can just remove the ending ones. I get paranoid with
> macro variables, and tend to over do it so that there's no question.
Why not make it an inline function?
Andreas.
--
Andreas Schwab, schwab@...ux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510 2552 DF73 E780 A9DA AEC1
"And now for something completely different."
Powered by blists - more mailing lists