#include #include #include #include #include #include using namespace ostd; struct Foo { }; /* implementing formatting for custom objects - external function */ template void to_format(Foo const &, R &writer, format_spec const &fs) { switch (fs.spec()) { case 'i': writer.put_string("Foo1"); break; default: writer.put_string("Foo2"); break; } } struct Bar { /* implementing formatting for custom objects - method */ template void to_format(R &writer, format_spec const &fs) const { switch (fs.spec()) { case 'i': writer.put_string("Bar1"); break; default: writer.put_string("Bar2"); break; } } }; int main() { std::vector x = { 5, 10, 15, 20 }; writefln("[%(%s|%)]", x); int y[] = { 2, 4, 8, 16, 32 }; writefln("{ %(%s, %) }", y); writefln("[\n%([ %(%s, %) ]%|,\n%)\n]", map(range(10), [](int v) { return range(v + 1); })); std::unordered_map m = { { "foo", 5 }, { "bar", 10 }, { "baz", 15 } }; /* strings and chars are automatically escaped */ writefln("{ %#(%s: %d, %) }", m); /* can override escaping with the - flag, * # flag expands the element into multiple values */ writefln("{ %-#(%s: %d, %) }", m); /* tuple format test */ std::tuple xt[] = { std::make_tuple(5, 3.14f, "foo"), std::make_tuple(3, 1.23f, "bar"), std::make_tuple(9, 8.66f, "baz") }; writefln("[ %#(<%d|%f|%s>%|, %) ]", xt); /* custom format */ writefln("%s", Foo{}); writefln("%i", Foo{}); /* custom format via method */ writefln("%s", Bar{}); writefln("%i", Bar{}); /* format into string */ auto s = appender(); format(s, "hello %s", "world"); writeln(s.get()); }