Jump to page: 1 2
Thread overview
std.stdio.write, writeln, fwrite, fwriteln
Dec 28, 2006
Lionello Lunesu
Dec 28, 2006
Lionello Lunesu
Dec 28, 2006
Lionello Lunesu
Dec 28, 2006
Sean Kelly
Dec 28, 2006
Sean Kelly
Dec 28, 2006
Lionello Lunesu
Dec 28, 2006
Lionello Lunesu
December 28, 2006
This is for the cout lovers out there:

I like writefln, but many times it's doing too much. I often forget to print strings explicitly using "%s", to prevent errors in the (rare) case the string contains %-characters. That's why I've added write, writeln, fwrite, fwriteln to std\stdio.d

Attached the changed files from DMD 0.178 (including ddoc). (I have no idea
what flags to pass to diff to get the usual diff output; do tell)

Changes:
* std.format.doFormat has an optional 4th parameter "bool parse" that
determines whether strings should be parsed for %-characters. It defaults to
'true' (for compatibility).
* std.stdio.writefx has an optional 5th parameter "bool parse". Ditto.
* std.stdio.write, writeln, fwrite, fwriteln call writefx with parse=false,
which in turn calls doFormat with parse=false.

Because the signature of doFormat and writefx has changed, you'll have linker errors when linking to old obj/libs. Since we're <1.0 I thought it would not be a problem. If it is, I'll have to make new functions doFormat2, writefx2 (or so) and make the old doFormat, writefx call those new functions.

By the way: since the "parse" flag is tested early on, printing strings with write,writeln will probably be slightly faster as well.

Also: I hereby release my changes to the public domain.

L.



December 28, 2006
I forgot to mention:

To test, overwrite the files in dmd/src/phobos/std/ and recompile Phobos. (follow the instructions from http://www.prowiki.org/wiki4d/wiki.cgi?HowTo/CompilePhobos )

Copy the resulting phobos.lib to dmd/lib/, overwriting the phobos.lib that's already there.

And an example:

#import std.stdio;
#void main() {
#   writeln("test ", 23, " is %s ", 5.0);
#}

Prints:
test 23 is %s 5

L.


December 28, 2006
Lionello Lunesu wrote:
> This is for the cout lovers out there:
> 
> I like writefln, but many times it's doing too much. I often forget to print
> strings explicitly using "%s", to prevent errors in the (rare) case the
> string contains %-characters. That's why I've added write, writeln, fwrite,
> fwriteln to std\stdio.d

How about the converse read* functions?

Andrei
December 28, 2006
"Andrei Alexandrescu (See Website For Email)" <SeeWebsiteForEmail@erdani.org> wrote in message news:45941A78.50908@erdani.org...
> Lionello Lunesu wrote:
>> This is for the cout lovers out there:
>>
>> I like writefln, but many times it's doing too much. I often forget to
>> print
>> strings explicitly using "%s", to prevent errors in the (rare) case the
>> string contains %-characters. That's why I've added write, writeln,
>> fwrite,
>> fwriteln to std\stdio.d
>
> How about the converse read* functions?

I didn't know they existed? Ah wait, they don't! I see, std.stdio is actually only std-output : )

L.


December 28, 2006
Lionello Lunesu wrote:

> I like writefln, but many times it's doing too much. I often forget to print
> strings explicitly using "%s", to prevent errors in the (rare) case the
> string contains %-characters. That's why I've added write, writeln, fwrite,
> fwriteln to std\stdio.d

I hope that you have better luck with your patch than I did with mine...

http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=digitalmars.D&artnum=15627

> Attached the changed files from DMD 0.178 (including ddoc). (I have no idea
> what flags to pass to diff to get the usual diff output; do tell)

diff -u

> Because the signature of doFormat and writefx has changed, you'll have
> linker errors when linking to old obj/libs. Since we're <1.0 I thought it
> would not be a problem. If it is, I'll have to make new functions doFormat2,
> writefx2 (or so) and make the old doFormat, writefx call those new
> functions.

I hope so. Secretly I also hope that readf / unFormat will make it too ?

(see http://www.digitalmars.com/d/archives/digitalmars/D/21692.html)

--anders
December 28, 2006
"Anders F Björklund" <afb@algonet.se> wrote in message news:en166e$1lou$1@digitaldaemon.com...
> I hope that you have better luck with your patch than I did with mine...
>
> http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=digitalmars.D&artnum=15627

I figured it had to be done before : S
Maybe 'yet another patch' will convince Walter that this is badly needed : )

>> Attached the changed files from DMD 0.178 (including ddoc). (I have no
>> idea
>> what flags to pass to diff to get the usual diff output; do tell)
>
> diff -u

Thanks.. Why isn't that the default, I wonder.

> I hope so. Secretly I also hope that readf / unFormat will make it too ? (see http://www.digitalmars.com/d/archives/digitalmars/D/21692.html)

Hey Andrei! Check that out : )

My wish for the new year will be Phobos being hosted on dsource.

L.


December 28, 2006
Lionello Lunesu wrote:

>>How about the converse read* functions?
> 
> I didn't know they existed? Ah wait, they don't! I see, std.stdio is actually only std-output : )

They exist, but only for std.stream (din) There is
also an "unFormat" implementation there (in vreadf).

--anders
December 28, 2006
Lionello Lunesu wrote:
> "Andrei Alexandrescu (See Website For Email)" <SeeWebsiteForEmail@erdani.org> wrote in message news:45941A78.50908@erdani.org...
>> Lionello Lunesu wrote:
>>> This is for the cout lovers out there:
>>>
>>> I like writefln, but many times it's doing too much. I often forget to print
>>> strings explicitly using "%s", to prevent errors in the (rare) case the
>>> string contains %-characters. That's why I've added write, writeln, fwrite,
>>> fwriteln to std\stdio.d
>> How about the converse read* functions?
> 
> I didn't know they existed? Ah wait, they don't! I see, std.stdio is actually only std-output : )

Yup, they don't exist, though std.stream does have read functions.  As an alternative, I've put a copy of the readf/unFormat routines I wrote ages ago online here:

http://www.invisibleduck.org/~sean/code/stdio.d
http://www.invisibleduck.org/~sean/code/unformat.d
http://www.invisibleduck.org/~sean/code/utf.d

They convert everything to dchar internally during processing and this may not be a terribly optimal decision (though given my limited understanding of Unicode at the time it seemed the most prudent approach), but the code may be useful regardless, as it's a complete implementation of the C99 scanf function with some additions for D.  The utf changes are necessary to preserve the one-char putback guarantee that C requires, though things admittedly get a bit weird with a single Unicode char being represented by up to four bytes.


Sean
December 28, 2006
Sean Kelly wrote:

> As an alternative, I've put a copy of the readf/unFormat routines I wrote ages ago online here:
> 
> http://www.invisibleduck.org/~sean/code/stdio.d
> http://www.invisibleduck.org/~sean/code/unformat.d
> http://www.invisibleduck.org/~sean/code/utf.d

Just for the archives: the ugly workarounds I did at
http://www.algonet.se/~afb/d/stdio/ turned out to be
just because Mac OS X 10.3 / GCC 3.3 doesn't support
templates fully, and GDC uses that for the TypeInfo...

For Mac OS X 10.4 / GCC 4.0, it should be working OK ?
(DMDScript is also broken on the old, for same reason)

--anders
December 28, 2006
Anders F Björklund wrote:
> Sean Kelly wrote:
> 
>> As an alternative, I've put a copy of the readf/unFormat routines I wrote ages ago online here:
>>
>> http://www.invisibleduck.org/~sean/code/stdio.d
>> http://www.invisibleduck.org/~sean/code/unformat.d
>> http://www.invisibleduck.org/~sean/code/utf.d
> 
> Just for the archives: the ugly workarounds I did at
> http://www.algonet.se/~afb/d/stdio/ turned out to be
> just because Mac OS X 10.3 / GCC 3.3 doesn't support
> templates fully, and GDC uses that for the TypeInfo...

Thanks for pointing this out.  I couldn't remember why the workarounds were necessary.

> For Mac OS X 10.4 / GCC 4.0, it should be working OK ?

I hope so.  Aside from some inline asm issues, I haven't seen anything DMD can compile that the new GDC cannot.


Sean
« First   ‹ Prev
1 2