lkml.org 
[lkml]   [2018]   [Dec]   [21]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
From
SubjectRe: [for-next][PATCH 23/24] string.h: Add strncmp_prefix() helper macro
On Fri, 21 Dec 2018 12:46:47 -0800
Joe Perches <joe@perches.com> wrote:


> > @@ -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);
> >
> > +/**
> > + * have_prefix - Test if a string has a given prefix
>
> There is a naming mismatch of have/has here
> and has_prefix is probably too generic a name.
>
> How about str_has_prefix?

Sure.

>
> > + * @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.

I don't find them confusing ;-) But I tend to use a lot of macros.

-- Steve

\
 
 \ /
  Last update: 2018-12-21 21:54    [W:0.100 / U:0.604 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site