weird strncmp usage?
Johannes Schindelin escreveu:
Quoted 1 line
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.
Quoted 9 lines · Han-Wen Nienhuys
Hi,
the git source seems full of calls similar to
strncmp (x, "constant string", 15)
is there a reason not to use something like
Quoted 7 lines
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
Hi,
Quoted 5 lines · Jeff King
Quoted 3 lines · Andy Whitcroft
That said if you know its constant you can also use
sizeof("foo")and that is done at compile time. Something like:
Quoted 8 lines
Quoted 1 line
#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
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?
Signature · 1 line
Han-Wen Nienhuys - [email protected] - http://www.xs4all.nl/~hanwen
Dear diary, on Thu, Nov 02, 2006 at 02:44:36AM CET, I got a letter where Andy Whitcroft <[email protected]> said that...
Quoted 18 lines
Quoted 9 lines · Han-Wen Nienhuys
Hi,
the git source seems full of calls similar to
strncmp (x, "constant string", 15)
is there a reason not to use something like
Quoted 7 lines
int
strxmp (char const *x, char const *y){ return strncmp (x, y, strlen (y));}
everywhere?
Quoted 7 lines
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.
Signature · 4 lines
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* Andy Whitcroft:
Quoted 2 lines
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
Quoted 4 lines · Andy Whitcroft
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).