std.stream readf can't read int, nan, inf as floats.
This means that readf can't read in everything that writef prints out, even if you know the exact order of elements in the stream.
Here are some tests:
//===========================================================================
module rwnans;
import std.stdio;
import std.stream;
void try_rwop(float x, string fmt="")
{
writefln("--- Test x=%f", x);
auto S = new MemoryStream();
S.writef(fmt, x);
S.position = 0;
writefln("S buffer contains '%s'", S.readLine());
S.position = 0;
float y;
int got = S.readf(&y);
writefln("read %d bytes, got y = %s", got, y);
}
void main()
{
try_rwop(1.0);
try_rwop(1.0,"%f");
float x; // nan
try_rwop(x);
try_rwop(x,"%f");
x = 1.0/0.0 ; // inf
try_rwop(x);
try_rwop(x,"%f");
try_rwop(-x);
try_rwop(-x,"%f");
}
Comment #1 by wbaxter — 2007-11-13T19:38:11Z
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!
}