# Re: weird strncmp usage?

7 messages from 2006-11-02 to 2006-11-03. Participants: Han-Wen Nienhuys, Andy Whitcroft, Johannes Schindelin, Petr Baudis, Florian Weimer, Jeff King.
Thread: https://gitlist.dev/t/43342

## Han-Wen Nienhuys, 2006-11-02 01:26

Subject: weird strncmp usage?
Message-ID: <eibhga$tpg$1@sea.gmane.org>
URL: https://gitlist.dev/e/eibhga%24tpg%241%40sea.gmane.org

```

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

```

## Andy Whitcroft, 2006-11-02 01:44

Subject: Re: weird strncmp usage?
Message-ID: <45494D84.2060402@shadowen.org>
URL: https://gitlist.dev/e/45494D84.2060402%40shadowen.org
In-Reply-To: <eibhga$tpg$1@sea.gmane.org>

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

```

## Jeff King, 2006-11-02 06:51

Subject: Re: weird strncmp usage?
Message-ID: <20061102065127.GA23105@coredump.intra.peff.net>
URL: https://gitlist.dev/e/20061102065127.GA23105%40coredump.intra.peff.net
In-Reply-To: <45494D84.2060402@shadowen.org>

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


```

## Johannes Schindelin, 2006-11-02 09:15

Subject: Re: weird strncmp usage?
Message-ID: <Pine.LNX.4.63.0611021008180.1670@wbgn013.biozentrum.uni-wuerzburg.de>
URL: https://gitlist.dev/e/Pine.LNX.4.63.0611021008180.1670%40wbgn013.biozentrum.uni-wuerzburg.de
In-Reply-To: <20061102065127.GA23105@coredump.intra.peff.net>

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

```

## Han-Wen Nienhuys, 2006-11-02 09:59

Subject: Re: weird strncmp usage?
Message-ID: <4549C172.3090604@xs4all.nl>
URL: https://gitlist.dev/e/4549C172.3090604%40xs4all.nl
In-Reply-To: <Pine.LNX.4.63.0611021008180.1670@wbgn013.biozentrum.uni-wuerzburg.de>

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

-- 

```

## Petr Baudis, 2006-11-02 11:04

Subject: Re: weird strncmp usage?
Message-ID: <20061102110450.GK20017@pasky.or.cz>
URL: https://gitlist.dev/e/20061102110450.GK20017%40pasky.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...
> 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

```

## Florian Weimer, 2006-11-03 21:05

Subject: Re: weird strncmp usage?
Message-ID: <8764dwb5ro.fsf@mid.deneb.enyo.de>
URL: https://gitlist.dev/e/8764dwb5ro.fsf%40mid.deneb.enyo.de
In-Reply-To: <45494D84.2060402@shadowen.org>

```
* 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

```
