diff options
| author | Eugen Wissner <belka@caraus.de> | 2017-07-26 06:49:33 +0200 |
|---|---|---|
| committer | Eugen Wissner <belka@caraus.de> | 2017-07-26 06:49:33 +0200 |
| commit | a9cc1353183f5f6b05a2f3f174944c5470d3c4a9 (patch) | |
| tree | a02dd669ad478a6f340b7f864f77b8a50bdad3de /source | |
| parent | 1389b03842ede1ea73d38f41e6575f1055dc0a83 (diff) | |
| download | tanya-a9cc1353183f5f6b05a2f3f174944c5470d3c4a9.tar.gz | |
format.conv: Add conversion from bool to String
Diffstat (limited to 'source')
| -rw-r--r-- | source/tanya/format/conv.d | 29 |
1 files changed, 24 insertions, 5 deletions
diff --git a/source/tanya/format/conv.d b/source/tanya/format/conv.d index fc33355..ddbb130 100644 --- a/source/tanya/format/conv.d +++ b/source/tanya/format/conv.d @@ -13,6 +13,7 @@ module tanya.format.conv; import std.traits; +import tanya.container.string; import tanya.memory; /** @@ -348,17 +349,22 @@ private @nogc unittest } /** - * Converts a boolean to a number. $(D_KEYWORD true) is `1`, $(D_KEYWORD false) - * is `0`. + * 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: `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)) { return from; @@ -386,6 +392,20 @@ pure nothrow @safe @nogc unittest 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. * @@ -486,7 +506,6 @@ private @nogc unittest * * 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)) |
