Poll on improved format strings.

Daniel Keep daniel.keep.lists at gmail.com
Wed Mar 7 18:45:34 PST 2007



Don Clugston wrote:
> renoX wrote:
>> Daniel Keep Wrote:
>>> To be honest, I think the type suffix needs to go.  After all, if you
>>> know what the type is at compile-time, why do I need to repeat it?
>>
>> If the compatibility with printf(allowing %d without {...} format
>> string) is removed (and I think I'll do this) then %{var} would be
>> also allowed.
>>
>>> Of course, doing that leaves you with the problem of how to specify
>>> formatting options... but then in the majority of cases, you probably
>>> don't care; you just want the thing output.
>>>
>>> So how about something like this:
>>>
>>> Expansion ::= "$" ExpansionSpec
>>>
>>> ExpansionSpec ::= FormattingOptions ExpansionExpr
>>> ExpansionSpec ::= ExpansionExpr
>>>
>>> FormattingOptions ::= "(" string ")"
>>>
>>> ExpansionExpr ::= "{" D_Expression "}"
>>> ExpansionExpr ::= D_Identifier
>>>
>>> So the example above becomes "... $(08){var1+var2} ...": one character
>>> longer, but gives you more freedom as to what you can put in the
>>> formatting options.  Plus, if you don't care how it's formatted, you can
>>> use "... ${var1+var2} ...", and if you just want to print a variable
>>> out, you can use "... $somevar ...".
>>
>> Printf-Format string are quite powerful, and they're well known so I
>> think their syntax should be kept, just with the obligation to follow
>> by {...}: so the following would work:
>> %{var}, %08X{var}, etc.. (%d{var} and %s{var} wouldn't be useful
>> anymore, I agree but to avoid surprising C programmers, they should be
>> kept).
>>
>> $somevar or %somevar without {...}, I don't know, it's shorter, sure
>> but less readable I think.
> 
> I agree. You'd also need to worry about "%f{abc}" -- is this a local
> variable called 'f', followed by the text "{abc}", or is it printing a
> floating point number called abc ?

All that trouble to post a BNF grammar, wasted!  :P

'f' is a valid identifier, so it's printing a variable called 'f',
followed by the text '{abc}'.  Incidentally, I used '$' so that people
wouldn't confuse it with a C printf format string (so the third possible
case doesn't really count in my version :P).

	-- Daniel

-- 
Unlike Knuth, I have neither proven or tried the above; it may not even
make sense.

v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D
i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP  http://hackerkey.com/



More information about the Digitalmars-d mailing list