Skip to content

Commit

Permalink
Issue 5236 - [patch] std.format.formattedRead/unformatValue does not …
Browse files Browse the repository at this point in the history
…support the raw reading of integer types
  • Loading branch information
RazvanN7 committed Dec 6, 2016
1 parent 3ab55e9 commit 58374ac
Showing 1 changed file with 0 additions and 58 deletions.
58 changes: 0 additions & 58 deletions std/format.d
Original file line number Diff line number Diff line change
Expand Up @@ -4362,37 +4362,6 @@ T unformatValue(T, Range, Char)(ref Range input, ref FormatSpec!Char spec)
static if(__traits(compiles, parse!T(input)) ) return parse!T(input);
}

private T rawRead(T, Range)(ref Range input)
{
enforce(
isSomeString!Range || ElementType!(Range).sizeof == 1,
"Cannot parse input of type %s".format(Range.stringof)
);
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;

}

/**
Reads an integral value and returns it.
*/
Expand All @@ -4414,32 +4383,10 @@ T unformatValue(T, Range, Char)(ref Range input, ref FormatSpec!Char spec)
spec.spec == 's' || spec.spec == 'd' || spec.spec == 'u' ? 10 : 0;
assert(base != 0);

// raw read
//enforce(input.length >= T.sizeof);
if (spec.spec == 'r') return rawRead!T(input);

enforce(find(acceptedSpecs!T, spec.spec).length,
text("Wrong unformat specifier '%", spec.spec , "' for ", T.stringof));

static if(__traits(compiles, parse!T(input, base)) ) return parse!T(input, base);

}

version(none)unittest
{
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);
}

/**
Reads a floating-point value and returns it.
*/
Expand All @@ -4449,11 +4396,6 @@ T unformatValue(T, Range, Char)(ref Range input, ref FormatSpec!Char spec)
import std.algorithm.searching : find;
import std.conv : parse, text;


// raw read
//enforce(input.length >= T.sizeof);
if (spec.spec == 'r') return rawRead!T(input);

enforce(find(acceptedSpecs!T, spec.spec).length,
text("Wrong unformat specifier '%", spec.spec , "' for ", T.stringof));

Expand Down

0 comments on commit 58374ac

Please sign in to comment.