format.conv.number2String -> format.integral2String (intern)
This commit is contained in:
parent
4bbc8b510a
commit
7797f0a1fe
@ -355,81 +355,3 @@ if (isIntegral!From && is(To == enum))
|
||||
throw make!ConvException(defaultAllocator,
|
||||
"Value not found in enum '" ~ To.stringof ~ "'");
|
||||
}
|
||||
|
||||
// Returns the last part of buffer with converted number.
|
||||
package(tanya) char[] number2String(T)(const T number,
|
||||
return ref char[21] buffer)
|
||||
if (isIntegral!T)
|
||||
{
|
||||
// abs the integer.
|
||||
ulong n64 = number < 0 ? -cast(long) number : number;
|
||||
|
||||
char* start = buffer[].ptr + buffer.sizeof - 1;
|
||||
|
||||
while (true)
|
||||
{
|
||||
// Do in 32-bit chunks (avoid lots of 64-bit divides even with constant
|
||||
// denominators).
|
||||
char* o = start - 8;
|
||||
uint n;
|
||||
if (n64 >= 100000000)
|
||||
{
|
||||
n = n64 % 100000000;
|
||||
n64 /= 100000000;
|
||||
}
|
||||
else
|
||||
{
|
||||
n = cast(uint) n64;
|
||||
n64 = 0;
|
||||
}
|
||||
|
||||
while (n)
|
||||
{
|
||||
*--start = cast(char) (n % 10) + '0';
|
||||
n /= 10;
|
||||
}
|
||||
// Ignore the leading zero if it was the last part of the integer.
|
||||
if (n64 == 0)
|
||||
{
|
||||
if ((start[0] == '0')
|
||||
&& (start != (buffer[].ptr + buffer.sizeof -1)))
|
||||
{
|
||||
++start;
|
||||
}
|
||||
break;
|
||||
}
|
||||
// Copy leading zeros if it wasn't the most significant part of the
|
||||
// integer.
|
||||
while (start != o)
|
||||
{
|
||||
*--start = '0';
|
||||
}
|
||||
}
|
||||
|
||||
// Get the length that we have copied.
|
||||
uint l = cast(uint) ((buffer[].ptr + buffer.sizeof - 1) - start);
|
||||
if (l == 0)
|
||||
{
|
||||
*--start = '0';
|
||||
l = 1;
|
||||
}
|
||||
else if (number < 0) // Set the sign.
|
||||
{
|
||||
*--start = '-';
|
||||
++l;
|
||||
}
|
||||
|
||||
return buffer[$ - l - 1 .. $ - 1];
|
||||
}
|
||||
|
||||
// Converting an integer to string.
|
||||
@nogc nothrow pure @system unittest
|
||||
{
|
||||
char[21] buf;
|
||||
|
||||
assert(number2String(80, buf) == "80");
|
||||
assert(number2String(-80, buf) == "-80");
|
||||
assert(number2String(0, buf) == "0");
|
||||
assert(number2String(uint.max, buf) == "4294967295");
|
||||
assert(number2String(int.min, buf) == "-2147483648");
|
||||
}
|
||||
|
@ -15,3 +15,81 @@
|
||||
module tanya.format;
|
||||
|
||||
public import tanya.format.conv;
|
||||
import tanya.meta.trait;
|
||||
|
||||
// Returns the last part of buffer with converted number.
|
||||
package(tanya) char[] integral2String(T)(T number, return ref char[21] buffer)
|
||||
if (isIntegral!T)
|
||||
{
|
||||
// abs the integer.
|
||||
ulong n64 = number < 0 ? -cast(long) number : number;
|
||||
|
||||
char* start = buffer[].ptr + buffer.sizeof - 1;
|
||||
|
||||
while (true)
|
||||
{
|
||||
// Do in 32-bit chunks (avoid lots of 64-bit divides even with constant
|
||||
// denominators).
|
||||
char* o = start - 8;
|
||||
uint n;
|
||||
if (n64 >= 100000000)
|
||||
{
|
||||
n = n64 % 100000000;
|
||||
n64 /= 100000000;
|
||||
}
|
||||
else
|
||||
{
|
||||
n = cast(uint) n64;
|
||||
n64 = 0;
|
||||
}
|
||||
|
||||
while (n)
|
||||
{
|
||||
*--start = cast(char) (n % 10) + '0';
|
||||
n /= 10;
|
||||
}
|
||||
// Ignore the leading zero if it was the last part of the integer.
|
||||
if (n64 == 0)
|
||||
{
|
||||
if ((start[0] == '0')
|
||||
&& (start != (buffer[].ptr + buffer.sizeof -1)))
|
||||
{
|
||||
++start;
|
||||
}
|
||||
break;
|
||||
}
|
||||
// Copy leading zeros if it wasn't the most significant part of the
|
||||
// integer.
|
||||
while (start != o)
|
||||
{
|
||||
*--start = '0';
|
||||
}
|
||||
}
|
||||
|
||||
// Get the length that we have copied.
|
||||
uint l = cast(uint) ((buffer[].ptr + buffer.sizeof - 1) - start);
|
||||
if (l == 0)
|
||||
{
|
||||
*--start = '0';
|
||||
l = 1;
|
||||
}
|
||||
else if (number < 0) // Set the sign.
|
||||
{
|
||||
*--start = '-';
|
||||
++l;
|
||||
}
|
||||
|
||||
return buffer[$ - l - 1 .. $ - 1];
|
||||
}
|
||||
|
||||
// Converting an integer to string.
|
||||
@nogc nothrow pure @system unittest
|
||||
{
|
||||
char[21] buf;
|
||||
|
||||
assert(integral2String(80, buf) == "80");
|
||||
assert(integral2String(-80, buf) == "-80");
|
||||
assert(integral2String(0, buf) == "0");
|
||||
assert(integral2String(uint.max, buf) == "4294967295");
|
||||
assert(integral2String(int.min, buf) == "-2147483648");
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user