summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEugen Wissner <belka@caraus.de>2018-02-02 14:34:36 +0100
committerEugen Wissner <belka@caraus.de>2018-02-02 14:34:36 +0100
commit4aaa71a7d007f434975fdd5337316fac9c61ed40 (patch)
treef406529d45cf1ee9dd4d078e38ebfd17af1b566c
parentcbc68c2c436a6be322ff4ad480bdd99578dd2e3b (diff)
downloadtanya-4aaa71a7d007f434975fdd5337316fac9c61ed40.tar.gz
Format ranges
-rw-r--r--source/tanya/format.d44
1 files changed, 43 insertions, 1 deletions
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.