Why does readln include the line terminator?
Georg Wrede
georg.wrede at iki.fi
Tue Apr 14 08:44:21 PDT 2009
Christopher Wright wrote:
> Georg Wrede wrote:
>> Readln returns a string which contains the line terminator.
>>
>> Is there a grand reason for this?
>>
>>
>> Currently there are a few drawbacks with this. The naive user doesn't
>> expect it, and the seasoned user has to keep stripping it. And then he
>> has to search the docs (or get hold of other OSs) to determine what
>> terminator to expect on other systems.
>>
>> And it can't really be a speed optimization either, because to do
>> anything useful with a string, you have to strip the terminator anyway
>> at some point.
>
> By default, tango does not exhibit this behavior. If you wish, you can
> include newlines:
>
> auto str = Cin.copyln; // no newline in str
> auto str2 = Cin.copyln(true); // has system-dependent newline
Now this is more like it. The default should really be (in Phobos too)
to not return the newline. (Hint to Walter: Tango is for users, by
users, and if they have no newline as the default, it should be
considered a serious hint as to what the programmer prefers.)
If one is really interested in doing some file manipulation which might
*preserve* varying line terminators in files that might have been edited
in both linux and dos, then he should use "the non-default" line
reading, like the Cin.copyln(true) above. Not that I'd see the point.
I'm certain that the overwhelming majority of cases where one reads
lines (_especially_ from the console, but from text files, too), one
just wants the contents of the string.
More information about the Digitalmars-d
mailing list