readln() returns new line charater

Marco Leise Marco.Leise at gmx.de
Tue Dec 31 08:28:50 PST 2013


Am Tue, 31 Dec 2013 16:21:14 +0000
schrieb Stewart Gordon <smjg_1998 at yahoo.com>:

> On 31/12/2013 14:45, Marco Leise wrote:
> <snip>
> > I guess I just don't see what an immutable string buys you.
> > The mutable part in a string is just a pointer and length pair.
> > Just write:
> >
> >    immutable s = readln()[0 .. $-1];
> >
> > and you have an immutable string at no cost.
> 
> What if the line is at EOF and doesn't have a trailing newline?  Then 
> surely you would lose the final byte of the input.

That line of code was out of context. Of course you have to
check for the correct line ending character(s) or use a
more general tailing white-space removal function.

> Moreover, does readln normalise the line break style (CR/LF/CRLF)?

No it doesn't. It gives you verbatim input.

> I'd be inclined to define a function like
> 
> string stripLineBreak(string s) {
>      while (s.length != 0 && s[$-1] != '\n' && s[$-1] != '\r') {
>          s = s[0..$-1];
>      }
>      return s;
> }
> 
> Stewart.

And what happens when you use readln() on a system where the
terminal character encoding is not UTF-8 and you type e.g. ä?
I feel inclined to write a whole new std.terminal!

-- 
Marco



More information about the Digitalmars-d mailing list