From 4aaa71a7d007f434975fdd5337316fac9c61ed40 Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Fri, 2 Feb 2018 14:34:36 +0100 Subject: [PATCH] Format ranges --- source/tanya/format.d | 44 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/source/tanya/format.d b/source/tanya/format.d index cc59416..8b80876 100644 --- a/source/tanya/format.d +++ b/source/tanya/format.d @@ -728,6 +728,23 @@ if (is(T == struct)) } } +private void formatRange(T)(ref T arg, ref String result) +if (isInputRange!T && !isInfinite!T) +{ + result.insertBack('['); + if (!arg.empty) + { + printToString!"{}"(result, arg.front); + arg.popFront(); + } + foreach (e; arg) + { + result.insertBack(", "); + printToString!"{}"(result, e); + } + result.insertBack(']'); +} + private ref String printToString(string fmt, Args...)(return ref String result, auto ref Args args) { @@ -761,6 +778,10 @@ private ref String printToString(string fmt, Args...)(return ref String result, { result.insertBack(args[0]); } + else static if (isInputRange!Arg && !isInfinite!Arg) + { + formatRange(args[0], result); + } else static if (is(Unqual!(typeof(args[0].stringify())) == String)) { result.insertBack(args[0].stringify()[]); @@ -969,7 +990,7 @@ package(tanya) String format(string fmt, Args...)(auto ref Args args) { static struct Stringish { - string content = "Some content"; + private string content = "Some content"; immutable(char) front() const @nogc nothrow pure @safe { @@ -987,6 +1008,27 @@ package(tanya) String format(string fmt, Args...)(auto ref Args args) } } assert(format!"{}"(Stringish()) == "Some content"); + + static struct Intish + { + private int front_ = 3; + + int front() const @nogc nothrow pure @safe + { + return this.front_; + } + + void popFront() @nogc nothrow pure @safe + { + --this.front_; + } + + bool empty() const @nogc nothrow pure @safe + { + return this.front == 0; + } + } + assert(format!"{}"(Intish()) == "[3, 2, 1]"); } // Typeid.