From a9cc1353183f5f6b05a2f3f174944c5470d3c4a9 Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Wed, 26 Jul 2017 06:49:33 +0200 Subject: [PATCH] format.conv: Add conversion from bool to String --- source/tanya/format/conv.d | 29 ++++++++++++++++++++++++----- 1 file 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))