On Fri, Nov 07, 2008 at 03:28:40PM -0700, chromatic wrote:
> On Friday 22 June 2007 02:07:32 Nicholas Clark wrote:
> 
> > > We have a lot of string_from_cstring() calls with constant second
> > > parameters and third parameters of 0 that could use updating.  There's no
> > > sense in calling strlen() all the time.
> 
> > I think that you need something like this
> >
> > /* concatenating with "" ensures that only literal strings are accepted as
> > argument */ #define STR_WITH_LEN(s)  (s ""), (sizeof(s)-1)
> >
> > /* STR_WITH_LEN() shortcuts */
> > #define newSVpvs(str) Perl_newSVpvn(aTHX_ STR_WITH_LEN(str))
> 
> I'm not sure that's what I was asking.
> 
> string_from_cstring()'s third parameter can be either the length of the 
> string 
> or zero.  If it's zero, the function will call strlen() to get the string's 
> length.
> 
> If we're passing in a string literal, it seems silly to pass in a length of 
> 0, 
> as we're recalculating a constant on every call.  I don't see that this macro 
> fixes that.

long(er) hand, one would write

string_from_cstring(INTERP, STR_WITH_LEN("Bool"));

but that's a bit obfuscated, because the macro really is a text substitution
that returns 2 values separated by a comma. And quite deliberately not wrapped
in ().

So the next step is to make another macro that behaves like a conventional
function:

#define string_from_cstring_constant(I, S) \
    string_from_cstring(I, STR_WITH_LEN(S))


or something like that, so that the messiness is all in one header. At which
point

string_from_cstring_constant(INTERP, "Bool");

looks like a regular function call, but is optimally efficient.
(Although names that long will start to scare the VMS porters, as it's getting
close to 32 characters)

> I do agree that updating strings and string lengths can be tedious, but I'm 
> not aware of any C89-compliant solution to keep the two synchronized.

If I've explained it clearly, then I think that it meets your requirements.
No strlen() call, but the correct length. And a syntax error from the compiler
if someone mistakenly passes in anything other than a string constant.

Nicholas Clark

Reply via email to