{"thread":{"id":"43342","subject":"Re: weird strncmp usage?","startedAt":"2006-11-02T01:26:54Z","lastAt":"2006-11-03T21:05:15Z","messageCount":7,"participants":["Han-Wen Nienhuys","Andy Whitcroft","Johannes Schindelin","Petr Baudis","Florian Weimer","Jeff King"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"296037","messageId":"eibhga$tpg$1@sea.gmane.org","threadId":"43342","inReplyTo":null,"subject":"weird strncmp usage?","fromName":"Han-Wen Nienhuys","fromEmail":"hanwen@xs4all.nl","sentAt":"2006-11-02T01:26:54Z","receivedAt":"2006-11-02T01:26:54Z","isPatch":false,"sender":{"key":"hanwen@google.com","avatar":"https://avatars.githubusercontent.com/u/31547?v=4"},"body":"\nHi,\n\nthe git source seems full of calls similar to\n\n   strncmp (x, \"constant string\", 15)\n\nis there a reason not to use something like\n\n   int\n   strxmp (char const *x, char const *y)\n   {\n     return strncmp (x, y, strlen (y));\n   }\n\neverywhere?\n\n-- \n  Han-Wen Nienhuys - hanwen@xs4all.nl - http://www.xs4all.nl/~hanwen\n"},{"id":"294597","messageId":"45494D84.2060402@shadowen.org","threadId":"43342","inReplyTo":"eibhga$tpg$1@sea.gmane.org","subject":"Re: weird strncmp usage?","fromName":"Andy Whitcroft","fromEmail":"apw@shadowen.org","sentAt":"2006-11-02T01:44:36Z","receivedAt":"2006-11-02T01:44:36Z","isPatch":false,"sender":{"key":"apw@shadowen.org","avatar":"https://gravatar.com/avatar/d3088262854661a913ef35cc40fedcc270142d4461791142bc1ea0b2a4e2e147?d=mp&s=160"},"body":"Han-Wen Nienhuys wrote:\n> \n> Hi,\n> \n> the git source seems full of calls similar to\n> \n>   strncmp (x, \"constant string\", 15)\n> \n> is there a reason not to use something like\n> \n>   int\n>   strxmp (char const *x, char const *y)\n>   {\n>     return strncmp (x, y, strlen (y));\n>   }\n> \n> everywhere?\n\nIf you are doing these a _lot_ then there is a significant additional\ncost to using strlen on a constant string.\n\nThat said if you know its constant you can also use sizeof(\"foo\") and\nthat is done at compile time.  Something like:\n\n#define strxcmp(x, y)\tstrncmp((x), (y), sizeof((y))\n\n-apw\n"},{"id":"298670","messageId":"20061102065127.GA23105@coredump.intra.peff.net","threadId":"43342","inReplyTo":"45494D84.2060402@shadowen.org","subject":"Re: weird strncmp usage?","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2006-11-02T06:51:27Z","receivedAt":"2006-11-02T06:51:27Z","isPatch":false,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Thu, Nov 02, 2006 at 01:44:36AM +0000, Andy Whitcroft wrote:\n\n> That said if you know its constant you can also use sizeof(\"foo\") and\n> that is done at compile time.  Something like:\n> \n> #define strxcmp(x, y)\tstrncmp((x), (y), sizeof((y))\n\nYou would, of course, need to use sizeof(y)-1 to avoid comparing the NUL\ntermination. :)\n\nThis is a slightly dangerous macro, because it _only_ works for string\nliterals, but not pointers (which is fine in this case, but its\nlimitations need to be documented).\n\n"},{"id":"294780","messageId":"Pine.LNX.4.63.0611021008180.1670@wbgn013.biozentrum.uni-wuerzburg.de","threadId":"43342","inReplyTo":"20061102065127.GA23105@coredump.intra.peff.net","subject":"Re: weird strncmp usage?","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2006-11-02T09:15:12Z","receivedAt":"2006-11-02T09:15:12Z","isPatch":false,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi,\n\nOn Thu, 2 Nov 2006, Jeff King wrote:\n\n> On Thu, Nov 02, 2006 at 01:44:36AM +0000, Andy Whitcroft wrote:\n> \n> > That said if you know its constant you can also use sizeof(\"foo\") and\n> > that is done at compile time.  Something like:\n> > \n> > #define strxcmp(x, y)\tstrncmp((x), (y), sizeof((y))\n> \n> You would, of course, need to use sizeof(y)-1 to avoid comparing the NUL\n> termination. :)\n> \n> This is a slightly dangerous macro, because it _only_ works for string\n> literals, but not pointers (which is fine in this case, but its\n> limitations need to be documented).\n\nIt would be even better to avoid these errors by doing something like\n\n\t#define starts_with(x, y) !strncmp((x), #y, sizeof(#y) - 1)\n\nwhich would be used like this:\n\n\tif (starts_with(arg, --abbrev=))\n\nHowever, in this case, you would need another macro, which automatically \nextracts the argument, and soon you will end up with yet another getopt \npackage.\n\nCiao,\nDscho\n"},{"id":"294413","messageId":"4549C172.3090604@xs4all.nl","threadId":"43342","inReplyTo":"Pine.LNX.4.63.0611021008180.1670@wbgn013.biozentrum.uni-wuerzburg.de","subject":"Re: weird strncmp usage?","fromName":"Han-Wen Nienhuys","fromEmail":"hanwen@xs4all.nl","sentAt":"2006-11-02T09:59:14Z","receivedAt":"2006-11-02T09:59:14Z","isPatch":false,"sender":{"key":"hanwen@google.com","avatar":"https://avatars.githubusercontent.com/u/31547?v=4"},"body":"Johannes Schindelin escreveu:\n\n> However, in this case, you would need another macro, which automatically \n> extracts the argument, and soon you will end up with yet another getopt \n> package.\n\nIt puzzles me that git doesn't use getopt.  Git is rare in not accepting\n\n  --option arg\n\nbut only\n\n  --option=arg\n\nAlso, -abc doesn't seem to work for combining options.\n\n-- \n"},{"id":"296504","messageId":"20061102110450.GK20017@pasky.or.cz","threadId":"43342","inReplyTo":"45494D84.2060402@shadowen.org","subject":"Re: weird strncmp usage?","fromName":"Petr Baudis","fromEmail":"pasky@suse.cz","sentAt":"2006-11-02T11:04:50Z","receivedAt":"2006-11-02T11:04:50Z","isPatch":false,"sender":{"key":"pasky@ucw.cz","avatar":"https://avatars.githubusercontent.com/u/18439?v=4"},"body":"Dear diary, on Thu, Nov 02, 2006 at 02:44:36AM CET, I got a letter\nwhere Andy Whitcroft <apw@shadowen.org> said that...\n> Han-Wen Nienhuys wrote:\n> > \n> > Hi,\n> > \n> > the git source seems full of calls similar to\n> > \n> >   strncmp (x, \"constant string\", 15)\n> > \n> > is there a reason not to use something like\n> > \n> >   int\n> >   strxmp (char const *x, char const *y)\n> >   {\n> >     return strncmp (x, y, strlen (y));\n> >   }\n> > \n> > everywhere?\n> \n> If you are doing these a _lot_ then there is a significant additional\n> cost to using strlen on a constant string.\n> \n> That said if you know its constant you can also use sizeof(\"foo\") and\n> that is done at compile time.  Something like:\n> \n> #define strxcmp(x, y)\tstrncmp((x), (y), sizeof((y))\n\nAt least in the #define, reasonable compilers should optimize out\nstrlen(\"foo\"). Hopefully as well if you would make strxmp() inlinable\nthough I'm not so sure there.\n\n-- \n\t\t\t\tPetr \"Pasky\" Baudis\nStuff: http://pasky.or.cz/\n#!/bin/perl -sp0777i<X+d*lMLa^*lN%0]dsXx++lMlN/dsM0<j]dsj\n$/=unpack('H*',$_);$_=`echo 16dio\\U$k\"SK$/SM$n\\EsN0p[lN*1\n"},{"id":"297991","messageId":"8764dwb5ro.fsf@mid.deneb.enyo.de","threadId":"43342","inReplyTo":"45494D84.2060402@shadowen.org","subject":"Re: weird strncmp usage?","fromName":"Florian Weimer","fromEmail":"fw@deneb.enyo.de","sentAt":"2006-11-03T21:05:15Z","receivedAt":"2006-11-03T21:05:15Z","isPatch":false,"sender":{"key":"fw@deneb.enyo.de","avatar":null},"body":"* Andy Whitcroft:\n\n> If you are doing these a _lot_ then there is a significant additional\n> cost to using strlen on a constant string.\n\nstrlen has been a GCC builtin for quite some time.  If the wrapper\nfunction is inlined, GCC will optimize it away.  (It also turns the\n"}]}