format.conv: Add conversion from bool to String
This commit is contained in:
parent
1389b03842
commit
a9cc135318
@ -13,6 +13,7 @@
|
|||||||
module tanya.format.conv;
|
module tanya.format.conv;
|
||||||
|
|
||||||
import std.traits;
|
import std.traits;
|
||||||
|
import tanya.container.string;
|
||||||
import tanya.memory;
|
import tanya.memory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -348,17 +349,22 @@ private @nogc unittest
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts a boolean to a number. $(D_KEYWORD true) is `1`, $(D_KEYWORD false)
|
* Converts a boolean to $(D_PARAM To).
|
||||||
* is `0`.
|
*
|
||||||
|
* 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:
|
* Params:
|
||||||
* From = Source type.
|
* From = Source type.
|
||||||
* To = Target type.
|
* To = Target type.
|
||||||
* from = Source value.
|
* from = Source value.
|
||||||
*
|
*
|
||||||
* Returns: `1` if $(D_PARAM from) is $(D_KEYWORD true), otherwise `0`.
|
* Returns: $(D_PARAM from) converted to $(D_PARAM To).
|
||||||
*/
|
*/
|
||||||
To to(To, From)(From from)
|
To to(To, From)(const From from)
|
||||||
if (is(Unqual!From == bool) && isNumeric!To && !is(Unqual!To == Unqual!From))
|
if (is(Unqual!From == bool) && isNumeric!To && !is(Unqual!To == Unqual!From))
|
||||||
{
|
{
|
||||||
return from;
|
return from;
|
||||||
@ -386,6 +392,20 @@ pure nothrow @safe @nogc unittest
|
|||||||
assert(false.to!int == 0);
|
assert(false.to!int == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Ditto.
|
||||||
|
To to(To, From)(const From from)
|
||||||
|
if (is(Unqual!From == bool) && is(To == String))
|
||||||
|
{
|
||||||
|
return String(from ? "true" : "false");
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
@nogc unittest
|
||||||
|
{
|
||||||
|
assert(true.to!String == "true");
|
||||||
|
assert(false.to!String == "false");
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts a floating point number to an integral type.
|
* Converts a floating point number to an integral type.
|
||||||
*
|
*
|
||||||
@ -486,7 +506,6 @@ private @nogc unittest
|
|||||||
*
|
*
|
||||||
* Throws: $(D_PSYMBOL ConvException) if $(D_PARAM from) is not a member of
|
* Throws: $(D_PSYMBOL ConvException) if $(D_PARAM from) is not a member of
|
||||||
* $(D_PSYMBOL To).
|
* $(D_PSYMBOL To).
|
||||||
|
|
||||||
*/
|
*/
|
||||||
To to(To, From)(From from)
|
To to(To, From)(From from)
|
||||||
if (isIntegral!From && is(To == enum))
|
if (isIntegral!From && is(To == enum))
|
||||||
|
Loading…
Reference in New Issue
Block a user