Re: weird strncmp usage?
- From
Andy Whitcroft <apw@shadowen.org>
- Date
- Nov 2, 2006, 01:44 UTC
- Message-ID
- <45494D84.2060402@shadowen.org>
- In-Reply-To
- <eibhga$tpg$1@sea.gmane.org>
Han-Wen Nienhuys wrote:
Show 16 quoted lines
>
> 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