688 lines
16 KiB
D
688 lines
16 KiB
D
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
/**
|
|
* This module provides functions for converting between different types.
|
|
*
|
|
* Copyright: Eugene Wissner 2017.
|
|
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
|
* Mozilla Public License, v. 2.0).
|
|
* Authors: Jeff Roberts, $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
|
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/format/conv.d,
|
|
* tanya/format/conv.d)
|
|
*/
|
|
module tanya.format.conv;
|
|
|
|
import tanya.container.string;
|
|
import tanya.memory;
|
|
import tanya.memory.op;
|
|
import tanya.meta.trait;
|
|
import tanya.meta.transform;
|
|
|
|
/**
|
|
* Thrown if a type conversion fails.
|
|
*/
|
|
final class ConvException : Exception
|
|
{
|
|
/**
|
|
* Params:
|
|
* msg = The message for the exception.
|
|
* file = The file where the exception occurred.
|
|
* line = The line number where the exception occurred.
|
|
* next = The previous exception in the chain of exceptions, if any.
|
|
*/
|
|
this(string msg,
|
|
string file = __FILE__,
|
|
size_t line = __LINE__,
|
|
Throwable next = null) @nogc @safe pure nothrow
|
|
{
|
|
super(msg, file, line, next);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* If the source type $(D_PARAM From) and the target type $(D_PARAM To) are
|
|
* equal, does nothing. If $(D_PARAM From) can be implicitly converted to
|
|
* $(D_PARAM To), just returns $(D_PARAM from).
|
|
*
|
|
* Params:
|
|
* To = Target type.
|
|
*
|
|
* Returns: $(D_PARAM from).
|
|
*/
|
|
template to(To)
|
|
{
|
|
/**
|
|
* Params:
|
|
* From = Source type.
|
|
* from = Source value.
|
|
*/
|
|
ref To to(From)(ref From from)
|
|
if (is(To == From))
|
|
{
|
|
return from;
|
|
}
|
|
|
|
/// Ditto.
|
|
To to(From)(From from)
|
|
if (is(Unqual!To == Unqual!From) || (isNumeric!From && isFloatingPoint!To))
|
|
{
|
|
return from;
|
|
}
|
|
}
|
|
|
|
///
|
|
pure nothrow @safe @nogc unittest
|
|
{
|
|
auto val = 5.to!int();
|
|
assert(val == 5);
|
|
static assert(is(typeof(val) == int));
|
|
}
|
|
|
|
private pure nothrow @safe @nogc unittest
|
|
{
|
|
int val = 5;
|
|
assert(val.to!int() == 5);
|
|
}
|
|
|
|
/**
|
|
* Performs checked conversion from an integral type $(D_PARAM From) to an
|
|
* integral type $(D_PARAM To).
|
|
*
|
|
* Params:
|
|
* From = Source type.
|
|
* To = Target type.
|
|
* from = Source value.
|
|
*
|
|
* Returns: $(D_PARAM from) converted to $(D_PARAM To).
|
|
*
|
|
* Throws: $(D_PSYMBOL ConvException) if $(D_PARAM from) is too small or too
|
|
* large to be represented by $(D_PARAM To).
|
|
*/
|
|
To to(To, From)(From from)
|
|
if (isIntegral!From
|
|
&& isIntegral!To
|
|
&& !is(Unqual!To == Unqual!From)
|
|
&& !is(To == enum))
|
|
{
|
|
static if ((isUnsigned!From && isSigned!To && From.sizeof == To.sizeof)
|
|
|| From.sizeof > To.sizeof)
|
|
{
|
|
if (from > To.max)
|
|
{
|
|
throw make!ConvException(defaultAllocator,
|
|
"Positive integer overflow");
|
|
}
|
|
}
|
|
static if (isSigned!From)
|
|
{
|
|
static if (isUnsigned!To)
|
|
{
|
|
if (from < 0)
|
|
{
|
|
throw make!ConvException(defaultAllocator,
|
|
"Negative integer overflow");
|
|
}
|
|
}
|
|
else static if (From.sizeof > To.sizeof)
|
|
{
|
|
if (from < To.min)
|
|
{
|
|
throw make!ConvException(defaultAllocator,
|
|
"Negative integer overflow");
|
|
}
|
|
}
|
|
}
|
|
static if (From.sizeof <= To.sizeof)
|
|
{
|
|
return from;
|
|
}
|
|
else static if (isSigned!To)
|
|
{
|
|
return cast(To) from;
|
|
}
|
|
else
|
|
{
|
|
return from & To.max;
|
|
}
|
|
}
|
|
|
|
private pure nothrow @safe @nogc unittest
|
|
{
|
|
// ubyte -> ushort
|
|
assert((cast(ubyte) 0).to!ushort == 0);
|
|
assert((cast(ubyte) 1).to!ushort == 1);
|
|
assert((cast(ubyte) (ubyte.max - 1)).to!ushort == ubyte.max - 1);
|
|
assert((cast(ubyte) ubyte.max).to!ushort == ubyte.max);
|
|
|
|
// ubyte -> short
|
|
assert((cast(ubyte) 0).to!short == 0);
|
|
assert((cast(ubyte) 1).to!short == 1);
|
|
assert((cast(ubyte) (ubyte.max - 1)).to!short == ubyte.max - 1);
|
|
assert((cast(ubyte) ubyte.max).to!short == ubyte.max);
|
|
}
|
|
|
|
private unittest
|
|
{
|
|
// ubyte <- ushort
|
|
assert((cast(ushort) 0).to!ubyte == 0);
|
|
assert((cast(ushort) 1).to!ubyte == 1);
|
|
assert((cast(ushort) (ubyte.max - 1)).to!ubyte == ubyte.max - 1);
|
|
assert((cast(ushort) ubyte.max).to!ubyte == ubyte.max);
|
|
|
|
// ubyte <- short
|
|
assert((cast(short) 0).to!ubyte == 0);
|
|
assert((cast(short) 1).to!ubyte == 1);
|
|
assert((cast(short) (ubyte.max - 1)).to!ubyte == ubyte.max - 1);
|
|
assert((cast(short) ubyte.max).to!ubyte == ubyte.max);
|
|
|
|
// short <-> int
|
|
assert(short.min.to!int == short.min);
|
|
assert((short.min + 1).to!int == short.min + 1);
|
|
assert((cast(short) -1).to!int == -1);
|
|
assert((cast(short) 0).to!int == 0);
|
|
assert((cast(short) 1).to!int == 1);
|
|
assert((short.max - 1).to!int == short.max - 1);
|
|
assert(short.max.to!int == short.max);
|
|
|
|
assert((cast(int) short.min).to!short == short.min);
|
|
assert((cast(int) short.min + 1).to!short == short.min + 1);
|
|
assert((cast(int) -1).to!short == -1);
|
|
assert((cast(int) 0).to!short == 0);
|
|
assert((cast(int) 1).to!short == 1);
|
|
assert((cast(int) short.max - 1).to!short == short.max - 1);
|
|
assert((cast(int) short.max).to!short == short.max);
|
|
|
|
// uint <-> int
|
|
assert((cast(uint) 0).to!int == 0);
|
|
assert((cast(uint) 1).to!int == 1);
|
|
assert((cast(uint) (int.max - 1)).to!int == int.max - 1);
|
|
assert((cast(uint) int.max).to!int == int.max);
|
|
|
|
assert((cast(int) 0).to!uint == 0);
|
|
assert((cast(int) 1).to!uint == 1);
|
|
assert((cast(int) (int.max - 1)).to!uint == int.max - 1);
|
|
assert((cast(int) int.max).to!uint == int.max);
|
|
}
|
|
|
|
private unittest
|
|
{
|
|
ConvException exception;
|
|
try
|
|
{
|
|
assert(int.min.to!short == int.min);
|
|
}
|
|
catch (ConvException e)
|
|
{
|
|
exception = e;
|
|
}
|
|
assert(exception !is null);
|
|
defaultAllocator.dispose(exception);
|
|
}
|
|
|
|
private unittest
|
|
{
|
|
ConvException exception;
|
|
try
|
|
{
|
|
assert(int.max.to!short == int.max);
|
|
}
|
|
catch (ConvException e)
|
|
{
|
|
exception = e;
|
|
}
|
|
assert(exception !is null);
|
|
defaultAllocator.dispose(exception);
|
|
}
|
|
|
|
private unittest
|
|
{
|
|
ConvException exception;
|
|
try
|
|
{
|
|
assert(uint.max.to!ushort == ushort.max);
|
|
}
|
|
catch (ConvException e)
|
|
{
|
|
exception = e;
|
|
}
|
|
assert(exception !is null);
|
|
defaultAllocator.dispose(exception);
|
|
}
|
|
|
|
private unittest
|
|
{
|
|
ConvException exception;
|
|
try
|
|
{
|
|
assert((-1).to!uint == -1);
|
|
}
|
|
catch (ConvException e)
|
|
{
|
|
exception = e;
|
|
}
|
|
assert(exception !is null);
|
|
defaultAllocator.dispose(exception);
|
|
}
|
|
|
|
private @nogc unittest
|
|
{
|
|
enum Test : int
|
|
{
|
|
one,
|
|
two,
|
|
}
|
|
assert(Test.one.to!int == 0);
|
|
assert(Test.two.to!int == 1);
|
|
}
|
|
|
|
/**
|
|
* Converts $(D_PARAM from) to a boolean.
|
|
*
|
|
* If $(D_PARAM From) is a numeric type, then `1` becomes $(D_KEYWORD true),
|
|
* `0` $(D_KEYWORD false). Otherwise $(D_PSYMBOL ConvException) is thrown.
|
|
*
|
|
* If $(D_PARAM To) is a string (built-in string or $(D_PSYMBOL String)),
|
|
* then `"true"` or `"false"` are converted to the appropriate boolean value.
|
|
* Otherwise $(D_PSYMBOL ConvException) is thrown.
|
|
*
|
|
* Params:
|
|
* From = Source type.
|
|
* To = Target type.
|
|
* from = Source value.
|
|
*
|
|
* Returns: $(D_KEYWORD from) converted to a boolean.
|
|
*
|
|
* Throws: $(D_PSYMBOL ConvException) if $(D_PARAM from) isn't convertible.
|
|
*/
|
|
To to(To, From)(From from)
|
|
if (isNumeric!From && is(Unqual!To == bool) && !is(Unqual!To == Unqual!From))
|
|
{
|
|
if (from == 0)
|
|
{
|
|
return false;
|
|
}
|
|
else if (from < 0)
|
|
{
|
|
throw make!ConvException(defaultAllocator,
|
|
"Negative number overflow");
|
|
}
|
|
else if (from <= 1)
|
|
{
|
|
return true;
|
|
}
|
|
throw make!ConvException(defaultAllocator,
|
|
"Positive number overflow");
|
|
}
|
|
|
|
///
|
|
@nogc unittest
|
|
{
|
|
assert(!0.0.to!bool);
|
|
assert(0.2.to!bool);
|
|
assert(0.5.to!bool);
|
|
assert(1.0.to!bool);
|
|
|
|
assert(!0.to!bool);
|
|
assert(1.to!bool);
|
|
}
|
|
|
|
private @nogc unittest
|
|
{
|
|
ConvException exception;
|
|
try
|
|
{
|
|
assert((-1).to!bool);
|
|
}
|
|
catch (ConvException e)
|
|
{
|
|
exception = e;
|
|
}
|
|
assert(exception !is null);
|
|
defaultAllocator.dispose(exception);
|
|
}
|
|
|
|
private @nogc unittest
|
|
{
|
|
ConvException exception;
|
|
try
|
|
{
|
|
assert(2.to!bool);
|
|
}
|
|
catch (ConvException e)
|
|
{
|
|
exception = e;
|
|
}
|
|
assert(exception !is null);
|
|
defaultAllocator.dispose(exception);
|
|
}
|
|
|
|
/// Ditto.
|
|
To to(To, From)(auto ref const From from)
|
|
if ((is(From == String) || isSomeString!From) && is(Unqual!To == bool))
|
|
{
|
|
if (from == "true")
|
|
{
|
|
return true;
|
|
}
|
|
else if (from == "false")
|
|
{
|
|
return false;
|
|
}
|
|
throw make!ConvException(defaultAllocator,
|
|
"String doesn't contain a boolean value");
|
|
}
|
|
|
|
///
|
|
@nogc unittest
|
|
{
|
|
assert("true".to!bool);
|
|
assert(!"false".to!bool);
|
|
assert(String("true").to!bool);
|
|
assert(!String("false").to!bool);
|
|
|
|
}
|
|
|
|
private @nogc unittest
|
|
{
|
|
ConvException exception;
|
|
try
|
|
{
|
|
assert("1".to!bool);
|
|
}
|
|
catch (ConvException e)
|
|
{
|
|
exception = e;
|
|
}
|
|
assert(exception !is null);
|
|
defaultAllocator.dispose(exception);
|
|
}
|
|
|
|
/**
|
|
* Converts a boolean to $(D_PARAM To).
|
|
*
|
|
* If $(D_PARAM To) is a numeric type, then $(D_KEYWORD true) becomes `1`,
|
|
* $(D_KEYWORD false) `0`.
|
|
*
|
|
* If $(D_PARAM To) is a $(D_PSYMBOL String), then `"true"` or `"false"`
|
|
* is returned.
|
|
*
|
|
* Params:
|
|
* From = Source type.
|
|
* To = Target type.
|
|
* from = Source value.
|
|
*
|
|
* Returns: $(D_PARAM from) converted to $(D_PARAM To).
|
|
*/
|
|
To to(To, From)(const From from)
|
|
if (is(Unqual!From == bool) && isNumeric!To && !is(Unqual!To == Unqual!From))
|
|
{
|
|
return from;
|
|
}
|
|
|
|
///
|
|
pure nothrow @safe @nogc unittest
|
|
{
|
|
assert(true.to!float == 1.0);
|
|
assert(true.to!double == 1.0);
|
|
assert(true.to!ubyte == 1);
|
|
assert(true.to!byte == 1);
|
|
assert(true.to!ushort == 1);
|
|
assert(true.to!short == 1);
|
|
assert(true.to!uint == 1);
|
|
assert(true.to!int == 1);
|
|
|
|
assert(false.to!float == 0);
|
|
assert(false.to!double == 0);
|
|
assert(false.to!ubyte == 0);
|
|
assert(false.to!byte == 0);
|
|
assert(false.to!ushort == 0);
|
|
assert(false.to!short == 0);
|
|
assert(false.to!uint == 0);
|
|
assert(false.to!int == 0);
|
|
}
|
|
|
|
/// Ditto.
|
|
To to(To, From)(const From from)
|
|
if (is(Unqual!From == bool) && is(Unqual!To == String))
|
|
{
|
|
return String(from ? "true" : "false");
|
|
}
|
|
|
|
///
|
|
@nogc unittest
|
|
{
|
|
assert(true.to!String == "true");
|
|
assert(false.to!String == "false");
|
|
}
|
|
|
|
private @nogc unittest
|
|
{
|
|
static assert(is(typeof((const String("true")).to!bool)));
|
|
static assert(is(typeof(false.to!(const String) == "false")));
|
|
}
|
|
|
|
/**
|
|
* Converts a floating point number to an integral type.
|
|
*
|
|
* Params:
|
|
* From = Source type.
|
|
* To = Target type.
|
|
* from = Source value.
|
|
*
|
|
* Returns: Truncated $(D_PARAM from) (everything after the decimal point is
|
|
* dropped).
|
|
*
|
|
* Throws: $(D_PSYMBOL ConvException) if
|
|
* $(D_INLINECODE from < To.min || from > To.max).
|
|
*/
|
|
To to(To, From)(From from)
|
|
if (isFloatingPoint!From
|
|
&& isIntegral!To
|
|
&& !is(Unqual!To == Unqual!From)
|
|
&& !is(To == enum))
|
|
{
|
|
if (from > To.max)
|
|
{
|
|
throw make!ConvException(defaultAllocator,
|
|
"Positive number overflow");
|
|
}
|
|
else if (from < To.min)
|
|
{
|
|
throw make!ConvException(defaultAllocator,
|
|
"Negative number overflow");
|
|
}
|
|
return cast(To) from;
|
|
}
|
|
|
|
///
|
|
@nogc unittest
|
|
{
|
|
assert(1.5.to!int == 1);
|
|
assert(2147483646.5.to!int == 2147483646);
|
|
assert((-2147483647.5).to!int == -2147483647);
|
|
assert(2147483646.5.to!uint == 2147483646);
|
|
}
|
|
|
|
private @nogc unittest
|
|
{
|
|
ConvException exception;
|
|
try
|
|
{
|
|
assert(2147483647.5.to!int == 2147483647);
|
|
}
|
|
catch (ConvException e)
|
|
{
|
|
exception = e;
|
|
}
|
|
assert(exception !is null);
|
|
defaultAllocator.dispose(exception);
|
|
}
|
|
|
|
private @nogc unittest
|
|
{
|
|
ConvException exception;
|
|
try
|
|
{
|
|
assert((-2147483648.5).to!int == -2147483648);
|
|
}
|
|
catch (ConvException e)
|
|
{
|
|
exception = e;
|
|
}
|
|
assert(exception !is null);
|
|
defaultAllocator.dispose(exception);
|
|
}
|
|
|
|
private @nogc unittest
|
|
{
|
|
ConvException exception;
|
|
try
|
|
{
|
|
assert((-21474.5).to!uint == -21474);
|
|
}
|
|
catch (ConvException e)
|
|
{
|
|
exception = e;
|
|
}
|
|
assert(exception !is null);
|
|
defaultAllocator.dispose(exception);
|
|
}
|
|
|
|
/**
|
|
* Performs checked conversion from an integral type $(D_PARAM From) to an
|
|
* $(D_KEYWORD enum).
|
|
*
|
|
* Params:
|
|
* From = Source type.
|
|
* To = Target type.
|
|
* from = Source value.
|
|
*
|
|
* Returns: $(D_KEYWORD enum) value.
|
|
*
|
|
* Throws: $(D_PSYMBOL ConvException) if $(D_PARAM from) is not a member of
|
|
* $(D_PSYMBOL To).
|
|
*/
|
|
To to(To, From)(From from)
|
|
if (isIntegral!From && is(To == enum))
|
|
{
|
|
foreach (m; EnumMembers!To)
|
|
{
|
|
if (from == m)
|
|
{
|
|
return m;
|
|
}
|
|
}
|
|
throw make!ConvException(defaultAllocator,
|
|
"Value not found in enum '" ~ To.stringof ~ "'");
|
|
}
|
|
|
|
///
|
|
@nogc unittest
|
|
{
|
|
enum Test : int
|
|
{
|
|
one,
|
|
two,
|
|
}
|
|
static assert(is(typeof(1.to!Test) == Test));
|
|
assert(0.to!Test == Test.one);
|
|
assert(1.to!Test == Test.two);
|
|
}
|
|
|
|
private @nogc unittest
|
|
{
|
|
enum Test : uint
|
|
{
|
|
one,
|
|
two,
|
|
}
|
|
|
|
ConvException exception;
|
|
try
|
|
{
|
|
assert(5.to!Test == Test.one);
|
|
}
|
|
catch (ConvException e)
|
|
{
|
|
exception = e;
|
|
}
|
|
assert(exception !is null);
|
|
defaultAllocator.dispose(exception);
|
|
}
|
|
|
|
// Returns the last part of buffer with converted number.
|
|
package char[] number2String(T)(const T number, out char[20] buffer)
|
|
{
|
|
// abs the integer.
|
|
ulong n64 = number < 0 ? -cast(long) number : number;
|
|
|
|
char* start = buffer.ptr + buffer.sizeof;
|
|
|
|
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)))
|
|
{
|
|
++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) - start);
|
|
if (l == 0)
|
|
{
|
|
*--start = '0';
|
|
l = 1;
|
|
}
|
|
else if (number < 0) // Set the sign.
|
|
{
|
|
*--start = '-';
|
|
++l;
|
|
}
|
|
|
|
return buffer[$ - l .. $];
|
|
}
|
|
|
|
private pure nothrow @system @nogc unittest
|
|
{
|
|
char[20] 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");
|
|
}
|