|
From: | Elliott Hughes |
Subject: | Re: [bug-grep] search.c clean-up |
Date: | Tue, 23 Nov 2004 09:21:31 -0800 |
On Nov 23, 2004, at 00:53, Jim Meyering wrote:
and it seems to call strlen()on constant strings even more times than it did before, which is only a smalland constant cost but not very tasteful.It is better practice to use strlen in such macros, in case the caller passes non-literal strings. I think of it as defensive programming. More about this, below. The compiler we typically care about (gcc) can optimize away calls to strlen with a literal argument.
even if it can't optimize it out, this code is only executed once per invocation of grep. since the only way sizeof could be a valid alternative is if the strings are known at compile-time, we're guaranteed not to have a particularly large string to scan.
strlen's added safety at no cost sounds good to me.i like this partial factorization -- even though there's more stuff to also come out -- because it's a meaningful chunk of functionality in its own right, and deserves a name.
i'd rather see a function than a multi-line macro, though. -- http://www.jessies.org/~enh/
[Prev in Thread] | Current Thread | [Next in Thread] |