Re: weird strncmp usage?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Dear diary, on Thu, Nov 02, 2006 at 02:44:36AM CET, I got a letter
where Andy Whitcroft <apw@xxxxxxxxxxxx> 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
lK[d2%Sa2/d0$^Ixp"|dc`;s/\W//g;$_=pack('H*',/((..)*)$/)
-
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]