Re: [PATCH] linux/string.h: Introduce streq macro.

From: Steven Rostedt
Date: Wed Apr 27 2011 - 15:02:08 EST


On Wed, 2011-04-27 at 11:33 -0700, H. Peter Anvin wrote:
> On 04/27/2011 10:49 AM, Steven Rostedt wrote:
> > On Tue, 2011-04-26 at 16:45 -0300, Thiago Farina wrote:
> >> This macro is arguably more readable than its variants:
> >> - !strcmp(a, b)
> >> - strcmp(a, b) == 0
> >
> > Actually, this was proposed way back in 2002 my Rusty and I did not see
> > anyone arguing against it. I wonder why it never was incorporated back
> > then?
> >
> > http://marc.info/?l=linux-kernel&m=103284339813100&w=2
> >
> > [ added Cc's of some of those that replied to this thread ]
> >
>
> Because !strcmp() is idiomatic C.
>
> This is the same kind of stupidity as
>
> #define BEGIN {
> #define END }

I argue that this is totally different than your example. Your example
demonstrates changing the syntax of C to simulate another language. This
has nothing to do with simulating any other language. The problem with
!strcmp() is that it goes against the semantics of C, as '!' means not.
And to think '!' is an equal can get a bit confusing.

It is also the reason we already have two semantics in the kernel for
this:

!strcmp(a, b) and strcmp(a, b) == 0

Personally, I'm fine with just using strcmp(a, b) == 0, as I have
learned to understand it. And when reading code, I've actually been able
to teach myself !strcmp(a, b) is equality (although with a slight hiccup
in my thought process).

But I still get stuck when I see the use of strcmp(a, b) meaning a != b.
This is where my brain stops completely to analyze if this is really
what the author of the code meant.

>
> It doesn't matter if it is more readable *to you*... learn the language,
> please.

I have learned the language (it's my mother tongue), but I think
strcmp() is an anomaly of it. It was a mistake that libc never included
a streq(). If it had, we would not even be having this discussion.
Another note is that strcmp is not really part of the language itself as
we must write it ourselves.

Heck, we could add:

#ifndef __HAVE_ARCH_STREQ
/**
* streq - Test if two strings are equal
* @cs: One string
* @ct: Another string
*/
#undef streq
int streq(const char *cs, const char *ct)
{
unsigned char c1, c2;

while (1) {
c1 = *cs++;
c2 = *ct++;
if (c1 != c2)
return 0;
if (!c1)
break;
}
return 1;
}
EXPORT_SYMBOL(streq);
#endif

And this would be just like adding another helper function.

-- Steve


--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/