git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: weird strncmp usage?

From
Petr Baudis <pasky@suse.cz>
Date
Nov 2, 2006, 11:04 UTC
Message-ID
<20061102110450.GK20017@pasky.or.cz>
In-Reply-To
<45494D84.2060402@shadowen.org>

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
Previous: Han-Wen NienhuysNext: Florian Weimer
Message 6 of 7 in “weird strncmp usage?”
  1. Han-Wen NienhuysNov 2, 2006
  2. Andy WhitcroftNov 2, 2006
  3. Jeff KingNov 2, 2006
  4. Johannes SchindelinNov 2, 2006
  5. Han-Wen NienhuysNov 2, 2006
  6. Petr BaudisNov 2, 2006
  7. Florian WeimerNov 3, 2006

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.