readln() returns new line charater

Stewart Gordon smjg_1998 at yahoo.com
Tue Dec 31 08:21:14 PST 2013


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.

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

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.


More information about the Digitalmars-d mailing list