Poll on improved format strings.
renoX
renosky at free.fr
Tue Mar 6 12:25:09 PST 2007
Don Clugston a écrit :
> renoX wrote:
>> Hello,
>>
>> I've made a templated format string templates
>>
>> The idea is: printf format string are interesting because they are
>> powerful but they suck because the %d,%s, etc are in one part of the
>> function and the corresponding variable are in a different part of the
>> function (Tango has the same problem), writef improve this by allowing
>> "... %d",var," ... %s",var2 but it's still not ideal because in the
>> gluing of the various strings, it's easy to forget a space or a comma
>> thus providing a not very good output.
>>
>> So my idea would be to have embedded expression like this "...
>> %08d{var1+var2} ...", but it's not easy to provide a good
>> syntax/semantic, so I'd like some remarks:
>
> I like this better than anything else I've ever seen.
>
>>
>> -Should the mix of printf format and new style format string be
>> allowed? (It is in the current implementation).
>> This has the advantage of nearly keeping the compatibility, the
>> problem is with the format string "..%d{..." in printf this means a
>> number followed by '{' but with the new format this creates an error.
>> It is possible to escape the '{' to allow this, ie to say that '%{' is
>> '{' so now '%d{' would need to be '%d%{', this has the inconvenient
>> that it's not possible to have the embedding format '%{var}' which
>> would be the shortest syntax..
>> Another possibility would be to say that if you want to have '... %d{
>> ...' one need to write it has '.... %d',var,'{ ....', this would
>> permit to have the '..%{var}...' embedding syntax.
>
> Personally I'd rather get an error if I leave off the {}.
> (I'm someone who uses {} inside printf debugging strings a lot, so it's
> far from compatible for me).
> I think it's better to minimise features wherever possible.
And this has the benefit that %{var} is the 'default' embedded format
string no need to use %s{var}.
>
>> -What to do with non-const char[]?
>
> Aren't they a security risk?
> eg,
> char [] a = getFromUser();
> writefln(a);
Yes, they might be a security risk if they are not 'sanitized' before usage.
>
> if a is "%d", you get an access violation. Apart from security, it just
> hides an insidious bug -- the code works fine until someone innocently
> enters a % followed by one of the allowable letters....
> I've always thought that the first argument to printf() should be forced
> to be a string literal.
> I would see it as an *advantage*, to only support const char [] !
Interesting.
Thanks for sharing your opinion, it's true that supporting only
const char[] and the 'embedded format string' makes the usage of
putf/sputf easier for the programmer..
I think that I will follow your ideas.
Regards,
renoX
More information about the Digitalmars-d
mailing list