summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEugen Wissner <belka@caraus.de>2017-12-15 22:42:18 +0100
committerEugen Wissner <belka@caraus.de>2017-12-15 22:42:18 +0100
commit78a8afdf75404526ea02d6bc80d130e85682dd81 (patch)
tree08e6d14a054427b87bc70b9ec8cf36de27dcbd6b
parent3c996d7c579666f6ce208f921e229b873e818102 (diff)
downloadtanya-78a8afdf75404526ea02d6bc80d130e85682dd81.tar.gz
Format stringish ranges
-rw-r--r--source/tanya/format/package.d80
1 files changed, 54 insertions, 26 deletions
diff --git a/source/tanya/format/package.d b/source/tanya/format/package.d
index d73919c..b444cbf 100644
--- a/source/tanya/format/package.d
+++ b/source/tanya/format/package.d
@@ -732,53 +732,56 @@ if (is(T == struct))
private ref String printToString(string fmt, Args...)(return ref String result,
auto ref Args args)
{
- static if (is(Unqual!(Args[0]) == typeof(null)))
+ alias Arg = Args[0];
+
+ static if (is(Unqual!Arg == typeof(null))) // null
{
result.insertBack("null");
}
- else static if(is(Unqual!(Args[0]) == bool))
+ else static if(is(Unqual!Arg == bool)) // Boolean
{
result.insertBack(args[0] ? "true" : "false");
}
+ else static if (isSomeString!Arg) // String
+ {
+ if (args[0] is null)
+ {
+ result.insertBack("null");
+ }
+ else
+ {
+ result.insertBack(args[0]);
+ }
+ }
+ else static if (isSomeChar!Arg // Supported by String.insertBack()
+ || (isInputRange!Arg && isSomeChar!(ElementType!Arg)))
+ {
+ result.insertBack(args[0]);
+ }
else static if (is(Unqual!(typeof(args[0].stringify())) == String))
{
result.insertBack(args[0].stringify()[]);
}
- else static if (is(Args[0] == class))
+ else static if (is(Arg == class))
{
result.insertBack(args[0].toString());
}
- else static if (is(Args[0] == interface))
+ else static if (is(Arg == interface))
{
- result.insertBack(Args[0].classinfo.name);
+ result.insertBack(Arg.classinfo.name);
}
- else static if (is(Args[0] == struct))
+ else static if (is(Arg == struct))
{
- result.insertBack(Args[0].stringof);
+ result.insertBack(Arg.stringof);
result.insertBack('(');
formatStruct(args[0], result);
result.insertBack(')');
}
- else static if (isSomeString!(Args[0])) // String
- {
- if (args[0] is null)
- {
- result.insertBack("null");
- }
- else
- {
- result.insertBack(args[0]);
- }
- }
- else static if (isSomeChar!(Args[0])) // Char
- {
- result.insertBack(args[0]);
- }
- else static if (isFloatingPoint!(Args[0])) // Float
+ else static if (isFloatingPoint!Arg) // Float
{
formatReal(args[0], result);
}
- else static if (isPointer!(Args[0])) // Pointer
+ else static if (isPointer!Arg) // Pointer
{
char[size_t.sizeof * 2] buffer;
size_t position = buffer.length;
@@ -794,14 +797,14 @@ private ref String printToString(string fmt, Args...)(return ref String result,
result.insertBack("0x");
result.insertBack(buffer[position .. $]);
}
- else static if (isIntegral!(Args[0])) // Integer
+ else static if (isIntegral!Arg) // Integer
{
char[21] buffer;
result.insertBack(integral2String(args[0], buffer));
}
else
{
- result.insertBack(Args[0].stringof);
+ result.insertBack(Arg.stringof);
}
return result;
@@ -942,6 +945,31 @@ package(tanya) String format(string fmt, Args...)(auto ref Args args)
assert(format!"{}"(cast(I) instance.get()) == I.classinfo.name);
}
+// Ranges.
+@nogc pure @safe unittest
+{
+ static struct Stringish
+ {
+ string content = "Some content";
+
+ immutable(char) front() const @nogc nothrow pure @safe
+ {
+ return this.content[0];
+ }
+
+ void popFront() @nogc nothrow pure @safe
+ {
+ this.content = this.content[1 .. $];
+ }
+
+ bool empty() const @nogc nothrow pure @safe
+ {
+ return this.content.length == 0;
+ }
+ }
+ assert(format!"{}"(Stringish()) == "Some content");
+}
+
private struct FormatSpec
{
}