[Mono-bugs] [Bug 655934] TextReader: ReadToEnd and ReadLine not implemented correctly

bugzilla_noreply at novell.com bugzilla_noreply at novell.com
Sat Feb 5 16:42:30 EST 2011


https://bugzilla.novell.com/show_bug.cgi?id=655934

https://bugzilla.novell.com/show_bug.cgi?id=655934#c2


--- Comment #2 from Steve Bjorg <steveb at mindtouch.com> 2011-02-05 21:42:29 UTC ---
Ok, here's a version of ReadLine() that doesn't use string copy over-and-over
again.

public override string ReadLine() {

    // NOTE (steveb): Mono 2.8.2 does not implement TextReader.ReadLine()
properly (see https://bugzilla.novell.com/show_bug.cgi?id=655934);
    //                once fixed, this code can be removed.

    StringBuilder result = null;
    for(var c = Read(); c >= 0; c = Read()) {

        // lazy initialize string buffer so we can detect the case where we had
already reached the end of the reader
        result = result ?? new StringBuilder();

        // check simple character line ending
        if(c == '\r') {
            if(Peek() == '\n') {
                Read();
            }
            break;
        } else if(c == '\n') {
            break;
        } else {
            result.Append((char)c);

            // check if buffered sequence matches Environment.NewLine
            if(result.Length >= Environment.NewLine.Length) {
                var match = true;
                for(int resultIndex = result.Length - 1, newlineIndex =
Environment.NewLine.Length - 1; newlineIndex >= 0 && match; --resultIndex,
--newlineIndex) {
                    match = (result[resultIndex] ==
Environment.NewLine[newlineIndex]);
                }
                if(match) {
                    result.Remove(result.Length - Environment.NewLine.Length,
Environment.NewLine.Length);
                    break;
                }
            }
        }
    }
    return (result != null) ? result.ToString() : null;
}

-- 
Configure bugmail: https://bugzilla.novell.com/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the QA contact for the bug.
You are the assignee for the bug.


More information about the mono-bugs mailing list