[Issue 1668] std.stream readf can't read int, nan, inf as floats

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Nov 13 17:38:12 PST 2007


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





------- Comment #1 from wbaxter at gmail.com  2007-11-13 19:38 -------
More info.  The failure to parse something like "1" as a float seems to only
happen when there's only a single byte in the stream.  And then parsing as an
int fails too.  

Another bug: if you rewind the stream after failing to parse that as a float,
then the next attempt to parse as an int will get two characters instead of the
one and only one that was in the stream to begin with.

Here's an updated test program:

//===========================================================================

module rwnans;

import std.stdio;
import std.stream;


float try_rwop(float x, string fmt="", string pad="")
{
    writefln("--- Test x=%f", x);
    auto S = new MemoryStream();
    S.writef(fmt, x, pad);
    S.position = 0;
    writefln("S buffer contains '%s'", S.readLine());
    S.position = 0;

    float y;
    int got;
    do {
        got = S.readf(&y);
        writefln("read %d bytes, got y = %s", got, y);
        if (got) break;

        S.position=0;

        // try as integer
        int iy;
        got = S.readf(&iy);
        writefln("read %d bytes as int, got iy = %s", got, iy);
        if(got) break;

        S.position=0;

        // try nan inf,-inf
        got = S.readf("nan");
        if (got) break;

        S.position=0;

        got = S.readf("-inf");
        if (got) break;

        S.position=0;

        got = S.readf("inf");
        if (got) break;

    } while(false);
    if (!got)
        writefln("Couldn't parse it!");
    else
        writefln("finally got y=%s", y);

    return y;
}

void main()
{
    string pad = " ";
    try_rwop(1.0);       // fails!
    try_rwop(1.0,"%f");  // ok
    try_rwop(1.0,"",pad);// ok
    try_rwop(1.0,"%f");

    try_rwop(12.0);      // ok
    try_rwop(12.0,"%f"); // ok

    try_rwop(1.001);     // ok
    try_rwop(1.001,"%f");// ok

    float x; // nan
    try_rwop(x);         // fails!
    try_rwop(x,"%f");    // fails!
    try_rwop(x,"%f",pad);// fails!
    x = 1.0/0.0 ; // inf  
    try_rwop(x);         // fails!
    try_rwop(x,"%f");    // fails!
    try_rwop(x,"%f",pad);// fails!

    try_rwop(-x);        // fails!
    try_rwop(-x,"%f");   // fails!
    try_rwop(x,"%f",pad);// fails!

}


-- 



More information about the Digitalmars-d-bugs mailing list