Put real formatting code into a separate function
This commit is contained in:
parent
907f7a4e61
commit
2a68048fc1
@ -505,50 +505,9 @@ if (T.sizeof == U.sizeof)
|
|||||||
copy((&src)[0 .. 1], (&dest)[0 .. 1]);
|
copy((&src)[0 .. 1], (&dest)[0 .. 1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
private ref String printToString(string fmt, Args...)(return ref String result,
|
private void formatReal(T)(ref T arg ,ref String result)
|
||||||
auto ref Args args)
|
if (isFloatingPoint!T)
|
||||||
{
|
{
|
||||||
static if (is(Unqual!(Args[0]) == typeof(null)))
|
|
||||||
{
|
|
||||||
result.insertBack("null");
|
|
||||||
}
|
|
||||||
else static if(is(Unqual!(Args[0]) == bool))
|
|
||||||
{
|
|
||||||
result.insertBack(args[0] ? "true" : "false");
|
|
||||||
}
|
|
||||||
else static if (is(Unqual!(typeof(args[0].stringify())) == String))
|
|
||||||
{
|
|
||||||
result.insertBack(args[0].stringify()[]);
|
|
||||||
}
|
|
||||||
else static if (isClass!(Args[0]))
|
|
||||||
{
|
|
||||||
result.insertBack(args[0].toString());
|
|
||||||
}
|
|
||||||
else static if (isInterface!(Args[0]))
|
|
||||||
{
|
|
||||||
result.insertBack(Args[0].classinfo.name);
|
|
||||||
}
|
|
||||||
else static if (isStruct!(Args[0]))
|
|
||||||
{
|
|
||||||
result.insertBack(typeid(Args[0]).name);
|
|
||||||
}
|
|
||||||
else static if (isSomeString!(Args[0])) // String
|
|
||||||
{
|
|
||||||
if (args[0] is null)
|
|
||||||
{
|
|
||||||
result.insertBack("null");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
result.insertBack(args[0]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else static if (isSomeChar!(Args[0])) // Char
|
|
||||||
{
|
|
||||||
result.insertBack(args[0]);
|
|
||||||
}
|
|
||||||
else static if (isFloatingPoint!(Args[0])) // Float
|
|
||||||
{
|
|
||||||
char[512] buffer; // Big enough for e+308 or e-307.
|
char[512] buffer; // Big enough for e+308 or e-307.
|
||||||
char[8] tail = 0;
|
char[8] tail = 0;
|
||||||
char[] bufferSlice = buffer[64 .. $];
|
char[] bufferSlice = buffer[64 .. $];
|
||||||
@ -557,7 +516,7 @@ private ref String printToString(string fmt, Args...)(return ref String result,
|
|||||||
int decimalPoint;
|
int decimalPoint;
|
||||||
|
|
||||||
// Read the double into a string.
|
// Read the double into a string.
|
||||||
auto realString = real2String(args[0], buffer, decimalPoint, negative);
|
auto realString = real2String(arg, buffer, decimalPoint, negative);
|
||||||
auto length = cast(uint) realString.length;
|
auto length = cast(uint) realString.length;
|
||||||
|
|
||||||
// Clamp the precision and delete extra zeros after clamp.
|
// Clamp the precision and delete extra zeros after clamp.
|
||||||
@ -566,9 +525,7 @@ private ref String printToString(string fmt, Args...)(return ref String result,
|
|||||||
{
|
{
|
||||||
length = precision;
|
length = precision;
|
||||||
}
|
}
|
||||||
while ((length > 1)
|
while ((length > 1) && (precision != 0) && (realString[length - 1] == '0'))
|
||||||
&& (precision != 0)
|
|
||||||
&& (realString[length - 1] == '0'))
|
|
||||||
{
|
{
|
||||||
--precision;
|
--precision;
|
||||||
--length;
|
--length;
|
||||||
@ -581,7 +538,7 @@ private ref String printToString(string fmt, Args...)(return ref String result,
|
|||||||
if (decimalPoint == special)
|
if (decimalPoint == special)
|
||||||
{
|
{
|
||||||
result.insertBack(realString);
|
result.insertBack(realString);
|
||||||
goto ParamEnd;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Should we use sceintific notation?
|
// Should we use sceintific notation?
|
||||||
@ -742,6 +699,53 @@ private ref String printToString(string fmt, Args...)(return ref String result,
|
|||||||
|
|
||||||
result.insertBack(buffer[64 .. length]); // Number.
|
result.insertBack(buffer[64 .. length]); // Number.
|
||||||
result.insertBack(tail[1 .. tail[0] + 1]); // Tail.
|
result.insertBack(tail[1 .. tail[0] + 1]); // Tail.
|
||||||
|
}
|
||||||
|
|
||||||
|
private ref String printToString(string fmt, Args...)(return ref String result,
|
||||||
|
auto ref Args args)
|
||||||
|
{
|
||||||
|
static if (is(Unqual!(Args[0]) == typeof(null)))
|
||||||
|
{
|
||||||
|
result.insertBack("null");
|
||||||
|
}
|
||||||
|
else static if(is(Unqual!(Args[0]) == bool))
|
||||||
|
{
|
||||||
|
result.insertBack(args[0] ? "true" : "false");
|
||||||
|
}
|
||||||
|
else static if (is(Unqual!(typeof(args[0].stringify())) == String))
|
||||||
|
{
|
||||||
|
result.insertBack(args[0].stringify()[]);
|
||||||
|
}
|
||||||
|
else static if (isClass!(Args[0]))
|
||||||
|
{
|
||||||
|
result.insertBack(args[0].toString());
|
||||||
|
}
|
||||||
|
else static if (isInterface!(Args[0]))
|
||||||
|
{
|
||||||
|
result.insertBack(Args[0].classinfo.name);
|
||||||
|
}
|
||||||
|
else static if (isStruct!(Args[0]))
|
||||||
|
{
|
||||||
|
result.insertBack(typeid(Args[0]).name);
|
||||||
|
}
|
||||||
|
else static if (isSomeString!(Args[0])) // String
|
||||||
|
{
|
||||||
|
if (args[0] is null)
|
||||||
|
{
|
||||||
|
result.insertBack("null");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
result.insertBack(args[0]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else static if (isSomeChar!(Args[0])) // Char
|
||||||
|
{
|
||||||
|
result.insertBack(args[0]);
|
||||||
|
}
|
||||||
|
else static if (isFloatingPoint!(Args[0])) // Float
|
||||||
|
{
|
||||||
|
formatReal(args[0], result);
|
||||||
}
|
}
|
||||||
else static if (isPointer!(Args[0])) // Pointer
|
else static if (isPointer!(Args[0])) // Pointer
|
||||||
{
|
{
|
||||||
@ -768,7 +772,6 @@ private ref String printToString(string fmt, Args...)(return ref String result,
|
|||||||
{
|
{
|
||||||
result.insertBack(Args[0].stringof);
|
result.insertBack(Args[0].stringof);
|
||||||
}
|
}
|
||||||
ParamEnd:
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user