Bug 5236 – [patch] std.format.formattedRead/unformatValue does not support the raw reading of integer types
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
Other
OS
Windows
Creation time
2010-11-18T11:35:00Z
Last change time
2017-01-16T23:25:29Z
Keywords
bootcamp, patch
Assigned to
razvan.nitu1305
Creator
sandford
Comments
Comment #0 by sandford — 2010-11-18T11:35:40Z
The raw value reading code was never duplicated in the isIntegral version of unformatValue. Given the amount of code overlap for raw value reading, I'd recommend a re-factor of the unformatValue, but in the mean time, here is a patch and unit test.
Unit test:
union B
{
char[int.sizeof] untyped;
int typed;
};
B b;
b.typed = 5;
char[] input = b.untyped[];
int witness;
formattedRead(input, "%r", &witness);
assert(witness == b.typed);
Patch:
/**
Reads an integral value and returns it.
*/
T unformatValue(T, Range, Char)(ref Range input, ref FormatSpec!Char spec)
if (isIntegral!T && isInputRange!Range)
{
if (spec.spec == 'r')
{
// raw read
//enforce(input.length >= T.sizeof);
enforce(isSomeString!Range || ElementType!(Range).sizeof == 1);
union X
{
ubyte[T.sizeof] raw;
T typed;
}
X x;
foreach (i; 0 .. T.sizeof)
{
static if (isSomeString!Range)
{
x.raw[i] = input[0];
input = input[1 .. $];
}
else
{
// TODO: recheck this
x.raw[i] = cast(ubyte) input.front;
input.popFront();
}
}
return x.typed;
}
enforce(std.algorithm.find("cdosuxX", spec.spec).length,
text("Wrong integral type specifier: `", spec.spec, "'"));
if (std.algorithm.find("dsu", spec.spec).length)
{
return parse!T(input);
}
assert(0, "Parsing spec '"~spec.spec~"' not implemented.");
}
Comment #1 by sandford — 2010-11-18T14:05:23Z
Also, the unformatValue routines need to wrap the parse statements in a traits compiles test, in order to support raw reading from byte streams.
i.e.
static if(__traits(compiles, parse!T(input) )) {
return parse!T(input);
}
Comment #2 by github-bugzilla — 2016-12-06T22:57:11Z