git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* weird strncmp usage?
@ 2006-11-02  1:26 Han-Wen Nienhuys
  2006-11-02  1:44 ` Andy Whitcroft
  0 siblings, 1 reply; 7+ messages in thread
From: Han-Wen Nienhuys @ 2006-11-02  1:26 UTC (permalink / raw)
  To: git


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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: weird strncmp usage?
  2006-11-02  1:26 weird strncmp usage? Han-Wen Nienhuys
@ 2006-11-02  1:44 ` Andy Whitcroft
  2006-11-02  6:51   ` Jeff King
                     ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Andy Whitcroft @ 2006-11-02  1:44 UTC (permalink / raw)
  To: hanwen; +Cc: git

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))

-apw

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: weird strncmp usage?
  2006-11-02  1:44 ` Andy Whitcroft
@ 2006-11-02  6:51   ` Jeff King
  2006-11-02  9:15     ` Johannes Schindelin
  2006-11-02 11:04   ` Petr Baudis
  2006-11-03 21:05   ` Florian Weimer
  2 siblings, 1 reply; 7+ messages in thread
From: Jeff King @ 2006-11-02  6:51 UTC (permalink / raw)
  To: Andy Whitcroft; +Cc: hanwen, git

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).


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: weird strncmp usage?
  2006-11-02  6:51   ` Jeff King
@ 2006-11-02  9:15     ` Johannes Schindelin
  2006-11-02  9:59       ` Han-Wen Nienhuys
  0 siblings, 1 reply; 7+ messages in thread
From: Johannes Schindelin @ 2006-11-02  9:15 UTC (permalink / raw)
  To: Jeff King; +Cc: Andy Whitcroft, hanwen, git

Hi,

On Thu, 2 Nov 2006, Jeff King wrote:

> 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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: weird strncmp usage?
  2006-11-02  9:15     ` Johannes Schindelin
@ 2006-11-02  9:59       ` Han-Wen Nienhuys
  0 siblings, 0 replies; 7+ messages in thread
From: Han-Wen Nienhuys @ 2006-11-02  9:59 UTC (permalink / raw)
  To: Johannes Schindelin; +Cc: Andy Whitcroft, git

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.

-- 

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: weird strncmp usage?
  2006-11-02  1:44 ` Andy Whitcroft
  2006-11-02  6:51   ` Jeff King
@ 2006-11-02 11:04   ` Petr Baudis
  2006-11-03 21:05   ` Florian Weimer
  2 siblings, 0 replies; 7+ messages in thread
From: Petr Baudis @ 2006-11-02 11:04 UTC (permalink / raw)
  To: Andy Whitcroft; +Cc: hanwen, git

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: weird strncmp usage?
  2006-11-02  1:44 ` Andy Whitcroft
  2006-11-02  6:51   ` Jeff King
  2006-11-02 11:04   ` Petr Baudis
@ 2006-11-03 21:05   ` Florian Weimer
  2 siblings, 0 replies; 7+ messages in thread
From: Florian Weimer @ 2006-11-03 21:05 UTC (permalink / raw)
  To: git

* 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

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2006-11-03 21:05 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-11-02  1:26 weird strncmp usage? Han-Wen Nienhuys
2006-11-02  1:44 ` Andy Whitcroft
2006-11-02  6:51   ` Jeff King
2006-11-02  9:15     ` Johannes Schindelin
2006-11-02  9:59       ` Han-Wen Nienhuys
2006-11-02 11:04   ` Petr Baudis
2006-11-03 21:05   ` Florian Weimer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).