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 f6b4e89 commit 6d13f66
Showing 1 changed file with 68 additions and 36 deletions.
104 changes: 68 additions & 36 deletions std/format.d
Original file line number Diff line number Diff line change
Expand Up @@ -4302,7 +4302,7 @@ T unformatValue(T, Range, Char)(ref Range input, ref FormatSpec!Char spec)
import std.conv : parse, text;
if (spec.spec == 's')
{
return parse!T(input);
static if(__traits(compiles, parse!T(input)) ) return parse!T(input);
}
enforce(find(acceptedSpecs!long, spec.spec).length,
text("Wrong unformat specifier '%", spec.spec , "' for ", T.stringof));
Expand Down Expand Up @@ -4359,7 +4359,38 @@ T unformatValue(T, Range, Char)(ref Range input, ref FormatSpec!Char spec)
enforce(spec.spec == 's',
text("Wrong unformat specifier '%", spec.spec , "' for ", T.stringof));

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

private T rawReadWrapper(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;

}

/**
Expand All @@ -4368,6 +4399,7 @@ T unformatValue(T, Range, Char)(ref Range input, ref FormatSpec!Char spec)
T unformatValue(T, Range, Char)(ref Range input, ref FormatSpec!Char spec)
if (isInputRange!Range && isIntegral!T && !is(T == enum))
{

import std.algorithm.searching : find;
import std.conv : parse, text;
enforce(find(acceptedSpecs!T, spec.spec).length,
Expand All @@ -4381,7 +4413,31 @@ T unformatValue(T, Range, Char)(ref Range input, ref FormatSpec!Char spec)
spec.spec == 'b' ? 2 :
spec.spec == 's' || spec.spec == 'd' || spec.spec == 'u' ? 10 : 0;
assert(base != 0);
return parse!T(input, base);

// raw read
//enforce(input.length >= T.sizeof);
if (spec.spec == 'r') return rawReadWrapper!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);
}

/**
Expand All @@ -4392,40 +4448,16 @@ T unformatValue(T, Range, Char)(ref Range input, ref FormatSpec!Char spec)
{
import std.algorithm.searching : find;
import std.conv : parse, text;
if (spec.spec == 'r')
{
// raw read
//enforce(input.length >= T.sizeof);
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;
}


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

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

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

version(none)unittest
Expand Down Expand Up @@ -4600,7 +4632,7 @@ T unformatValue(T, Range, Char)(ref Range input, ref FormatSpec!Char spec)
enforce(spec.spec == 's',
text("Wrong unformat specifier '%", spec.spec , "' for ", T.stringof));

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

@system pure unittest
Expand Down Expand Up @@ -4694,7 +4726,7 @@ T unformatValue(T, Range, Char)(ref Range input, ref FormatSpec!Char spec)
enforce(spec.spec == 's',
text("Wrong unformat specifier '%", spec.spec , "' for ", T.stringof));

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

@system pure unittest
Expand Down

0 comments on commit 6d13f66

Please sign in to comment.