threads / discuss / 43342

Re: weird strncmp usage?

Subject: Re: weird strncmp usage?

## tl;dr

7 messages between Nov 2, 2006 and Nov 3, 2006.

replies: 6people: 6as markdown or json

Han-Wen Nienhuys· Nov 2, 2006, 01:26 UTC · lore

weird strncmp usage?

Hi,
the git source seems full of calls similar to
   strncmp (x, "constant string", 15)
is there a reason not to use something like
   int
   strxmp (char const *x, char const *y)
   {
     return strncmp (x, y, strlen (y));
   }
everywhere?
-- 
  Han-Wen Nienhuys - hanwen@xs4all.nl - http://www.xs4all.nl/~hanwen
Andy Whitcroft· Nov 2, 2006, 01:44 UTC · re: Han-Wen Nienhuys · lore
Han-Wen Nienhuys wrote:
Show 16 quoted lines
> 
> Hi,
> 
> the git source seems full of calls similar to
> 
>   strncmp (x, "constant string", 15)
> 
> is there a reason not to use something like
> 
>   int
>   strxmp (char const *x, char const *y)
>   {
>     return strncmp (x, y, strlen (y));
>   }
> 
> everywhere?

If you are doing these a _lot_ then there is a significant additional cost to using strlen on a constant string.

That said if you know its constant you can also use sizeof("foo") and that is done at compile time. Something like:

#define strxcmp(x, y)	strncmp((x), (y), sizeof((y))
-apw
Jeff King· Nov 2, 2006, 06:51 UTC · re: Andy Whitcroft · lore
On Thu, Nov 02, 2006 at 01:44:36AM +0000, Andy Whitcroft wrote:
> That said if you know its constant you can also use sizeof("foo") and
> that is done at compile time.  Something like:
> 
> #define strxcmp(x, y)	strncmp((x), (y), sizeof((y))

You would, of course, need to use sizeof(y)-1 to avoid comparing the NUL termination. :)

This is a slightly dangerous macro, because it _only_ works for string literals, but not pointers (which is fine in this case, but its limitations need to be documented).

Johannes Schindelin· Nov 2, 2006, 09:15 UTC · re: Jeff King · lore
Hi,
On Thu, 2 Nov 2006, Jeff King wrote:
Show 13 quoted lines
> On Thu, Nov 02, 2006 at 01:44:36AM +0000, Andy Whitcroft wrote:
> 
> > That said if you know its constant you can also use sizeof("foo") and
> > that is done at compile time.  Something like:
> > 
> > #define strxcmp(x, y)	strncmp((x), (y), sizeof((y))
> 
> You would, of course, need to use sizeof(y)-1 to avoid comparing the NUL
> termination. :)
> 
> This is a slightly dangerous macro, because it _only_ works for string
> literals, but not pointers (which is fine in this case, but its
> limitations need to be documented).
It would be even better to avoid these errors by doing something like
	#define starts_with(x, y) !strncmp((x), #y, sizeof(#y) - 1)
which would be used like this:
	if (starts_with(arg, --abbrev=))

However, in this case, you would need another macro, which automatically extracts the argument, and soon you will end up with yet another getopt package.

Ciao, Dscho

Han-Wen Nienhuys· Nov 2, 2006, 09:59 UTC · re: Johannes Schindelin · lore
Johannes Schindelin escreveu:
> However, in this case, you would need another macro, which automatically 
> extracts the argument, and soon you will end up with yet another getopt 
> package.
It puzzles me that git doesn't use getopt.  Git is rare in not accepting
  --option arg
but only
  --option=arg
Also, -abc doesn't seem to work for combining options.
Petr Baudis· Nov 2, 2006, 11:04 UTC · re: Andy Whitcroft · lore

Dear diary, on Thu, Nov 02, 2006 at 02:44:36AM CET, I got a letter where Andy Whitcroft <apw@shadowen.org> said that...

Show 25 quoted lines
> Han-Wen Nienhuys wrote:
> > 
> > Hi,
> > 
> > the git source seems full of calls similar to
> > 
> >   strncmp (x, "constant string", 15)
> > 
> > is there a reason not to use something like
> > 
> >   int
> >   strxmp (char const *x, char const *y)
> >   {
> >     return strncmp (x, y, strlen (y));
> >   }
> > 
> > everywhere?
> 
> If you are doing these a _lot_ then there is a significant additional
> cost to using strlen on a constant string.
> 
> That said if you know its constant you can also use sizeof("foo") and
> that is done at compile time.  Something like:
> 
> #define strxcmp(x, y)	strncmp((x), (y), sizeof((y))

At least in the #define, reasonable compilers should optimize out strlen("foo"). Hopefully as well if you would make strxmp() inlinable though I'm not so sure there.

-- 
				Petr "Pasky" Baudis
Stuff: http://pasky.or.cz/
#!/bin/perl -sp0777i<X+d*lMLa^*lN%0]dsXx++lMlN/dsM0<j]dsj
$/=unpack('H*',$_);$_=`echo 16dio\U$k"SK$/SM$n\EsN0p[lN*1
Florian Weimer· Nov 3, 2006, 21:05 UTC · re: Andy Whitcroft · lore
* Andy Whitcroft:
> If you are doing these a _lot_ then there is a significant additional
> cost to using strlen on a constant string.

strlen has been a GCC builtin for quite some time. If the wrapper function is inlined, GCC will optimize it away. (It also turns the

← back to recent threads