On Fri, 7 Nov 2008 15:28:40 -0700
chromatic <[EMAIL PROTECTED]> wrote:
> On Friday 22 June 2007 02:07:32 Nicholas Clark wrote:
> > 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.

It uses sizeof, not strlen.  So, it pushes the calculation to
compile-time, so you only have to do it once, and never at runtime.

Also, using sizeof() will fix some cases that strlen() doesn't handle
correctly, specifically, strings containing explicit null characters.
src/objects.c has a few examples of that.  string_to_cstring(interp,
"\0", 0) will get the size wrong, but string_to_cstring_literal(interp,
"\0") will get it right.  So I don't really see a good excuse for not
using it everywhere.


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

sizeof() isn't C89?

Mark

Reply via email to