www.digitalmars.com Home | Search | C & C++ | D | DMDScript | News Groups | index | prev | next
Archives

D Programming
D
D.gnu
digitalmars.D
digitalmars.D.bugs
digitalmars.D.dtl
digitalmars.D.dwt
digitalmars.D.announce
digitalmars.D.learn
digitalmars.D.debugger

C/C++ Programming
c++
c++.announce
c++.atl
c++.beta
c++.chat
c++.command-line
c++.dos
c++.dos.16-bits
c++.dos.32-bits
c++.idde
c++.mfc
c++.rtl
c++.stl
c++.stl.hp
c++.stl.port
c++.stl.sgi
c++.stlsoft
c++.windows
c++.windows.16-bits
c++.windows.32-bits
c++.wxwindows

digitalmars.empire
digitalmars.DMDScript
electronics



digitalmars.D.bugs - [Issue 1106] New: Using Stream.readLine() and Stream.read(ubyte[]) requires understanding implementation details

http://d.puremagic.com/issues/show_bug.cgi?id=1106

           Summary: Using Stream.readLine() and Stream.read(ubyte[])
                    requires understanding implementation details
           Product: D
           Version: 1.009
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: digitalmars-com baysmith.com


There is a bug in the Stream class. If you mix, readLine() and read(ubyte[])
calls to a stream, readLine() may not appear to perform as documented. As
documented, readLine() stop at "some combination of carriage return [CR] and
line feed [LF]". However, this is not necessarily true. If the stream is
non-seekable, readLine() will stop at CR and set a prevCr flag indicating that
a LF should be consumed on the next read. However, this only takes place in the
getc() method and methods using getc(). Some methods, like read(ubyte[]), read
the stream directly and the prevCr flag is not processed.

At first, I thought this might just be odd behavior rather than a bug, but I
decided that it is a bug. In order to understand how to use the stream
effectively, one must understand the implementation details. One must know that
the behavior for seekable streams is different that non-seekable streams, and
adjust the client code accordingly. For this reason, it should be considered a
bug.

Code example:
import std.stream;

void main() {
  char[] testString = "line one\r\nline two\r\n";
  File testFile = new File("testFile", FileMode.OutNew);
  testFile.writeString(testString);
  testFile.close();

  File input = new File("testFile", FileMode.In);
  input.seekable = false;
  char[] line1 = input.readLine();
  assert(line1 == "line one");  // correct
  ubyte[1] b;
  input.read(b);
  assert(b[0] == '\n');  // incorrect
}


-- 
Apr 06 2007