[Issue 5236] New: [patch] std.format.formattedRead/unformatValue does not support the raw reading of integer types
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Thu Nov 18 11:36:56 PST 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5236
Summary: [patch] std.format.formattedRead/unformatValue does
not support the raw reading of integer types
Product: D
Version: D2
Platform: Other
OS/Version: Windows
Status: NEW
Keywords: patch
Severity: normal
Priority: P2
Component: Phobos
AssignedTo: nobody at puremagic.com
ReportedBy: sandford at jhu.edu
--- Comment #0 from Rob Jacques <sandford at jhu.edu> 2010-11-18 11:35:40 PST ---
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.");
}
--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
More information about the Digitalmars-d-bugs
mailing list