20-Mar-2013 18:34, Nick Sabalausky пишет:
Since *at least* as far back as XP, Windows has handled "\n" newlines
perfectly fine. The command line displays them properly, .BAT scripts
handle them properly, every code editor in existence handles them
properly. The *only* thing I've found that doesn't is Windows Notepad,
but really, whoTF uses that anyway?

So, why are silently and forcefully converting "\n" to "\r\n" on
windows by default? All it does is cause bugs. For example:
https://github.com/repeatedly/mustache-d/issues/3

And that's definitely not the first time I've run into problems due
using the "write*" functions instead of rawWrite.

Consider this straightforward code:

--------------------------------------
import std.file;
import std.stdio;

void transform(string str)
{
        /+ ...perform some modification of 'str'... +/
        return str;
}

void main()
{
        auto str = cast(string) read(args[1]);
        str = transform(str);
        write(str);
}
--------------------------------------

That simple code is *wrong*:

It works correctly for all input on Unix: Output newlines match input
newlines. Always. The code never asks for newlines to be messed with,
and therefore they never are.

But on Windows the behavior is just plain weird: Unix-style newlines in
the input are silently and forcefully converted to Windows-style
newlines behind the user's back. And even worse yet, *Windows*-style
newlines on the input are converted to a bizarre "Mac9 plus
Windows-style" combination of "\r\r\n" (not only is that wrong period,
but this sequence is often interpreted as two newlines which makes it
even worse). Using rawWrite fixes the problem, and creates *no* problem.
I feel like the current "write*" behavior is a design Steve Jobs would
have come up with.

So how is this useful to anyone, and how is it worthy of being the
default output behavior?

No. Let's obliterate it. The only problem is compatibility and the fact that Windows has had this "text mode" for I/O in e.g. MSVCRT for a long time. It never helped me in any sensible way except that "yay! I can view it in notepad!" but broken many things far too often.


Even with Windows as my primary system, there hasn't been one single
time "write*"'s output-altering "feature" has helped me by doing
something more correctly than rawWrite would have done. And that's just
when I'm dealing with pure text. I'm literally better off just using
rawWrite everywhere, which is exactly what I intend to do from now on
(and I'll be viewing uses of "write*" with suspicion as latent bugs) -
unlike "write*", rawWrite *never* does the wrong thing.

Can we please get rid of this "text-mode output"? Or at least eliminate
it as the default?



--
Dmitry Olshansky

Reply via email to