document format example
This commit is contained in:
parent
81ccca0e52
commit
0ce662131b
|
@ -42,12 +42,25 @@ struct Bar {
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
std::vector<int> x = { 5, 10, 15, 20 };
|
std::vector<int> x = { 5, 10, 15, 20 };
|
||||||
|
/* prints [5|10|15|20] (using | as delimiter and %s for each item),
|
||||||
|
* the syntax for ranges is %(CONTENTS%) where CONTENTS is a sequence
|
||||||
|
* up until and including the last format mark followed by delimiter,
|
||||||
|
* so for example "%s, " has "%s" for formatting and ", " for delimiter
|
||||||
|
* and "%d: %s, " has "%d: %s" for format and ", " for delimiter; if you
|
||||||
|
* need to specify a complicated manual delimiter, you can use the
|
||||||
|
* "FORMAT%|DELIMITER" syntax, where %(%s, %) equals %(%s%|, %)
|
||||||
|
*/
|
||||||
writefln("[%(%s|%)]", x);
|
writefln("[%(%s|%)]", x);
|
||||||
|
/* prints a range with default format {item, item, item, ...} */
|
||||||
writefln("%s", x);
|
writefln("%s", x);
|
||||||
|
|
||||||
int y[] = { 2, 4, 8, 16, 32 };
|
int y[] = { 2, 4, 8, 16, 32 };
|
||||||
|
/* prints { 2, 4, 8, 16, 32 } using ", " as delimiter and %s for items */
|
||||||
writefln("{ %(%s, %) }", y);
|
writefln("{ %(%s, %) }", y);
|
||||||
|
|
||||||
|
/* nested range printing - prints each item of the main
|
||||||
|
* range with [ %(%s, %) ] and ",\n" as a delimiter
|
||||||
|
*/
|
||||||
writefln("[\n%([ %(%s, %) ]%|,\n%)\n]", map(range(10), [](int v) {
|
writefln("[\n%([ %(%s, %) ]%|,\n%)\n]", map(range(10), [](int v) {
|
||||||
return range(v + 1);
|
return range(v + 1);
|
||||||
}));
|
}));
|
||||||
|
@ -57,20 +70,29 @@ int main() {
|
||||||
{ "bar", 10 },
|
{ "bar", 10 },
|
||||||
{ "baz", 15 }
|
{ "baz", 15 }
|
||||||
};
|
};
|
||||||
/* strings and chars are automatically escaped */
|
/* prints something like { "baz": 15, "bar": 10, "foo": 5 }, note that
|
||||||
writefln("{ %#(%s: %d, %) }", m);
|
* the tuple is expanded into two formats (using the # flag) and the
|
||||||
/* can override escaping with the - flag,
|
* items are escaped automatically (strings and chars)
|
||||||
* # flag expands the element into multiple values
|
|
||||||
*/
|
*/
|
||||||
|
writefln("{ %#(%s: %d, %) }", m);
|
||||||
|
/* the - flag overrides escaping, so you get { baz: 15, bar: 10, foo: 5} */
|
||||||
writefln("{ %-#(%s: %d, %) }", m);
|
writefln("{ %-#(%s: %d, %) }", m);
|
||||||
/* without expansion */
|
/* no expansion of the items, print entire tuple with default format,
|
||||||
|
* gets you something like { <"baz", 15>, <"bar", 10>, <"foo", 5> }
|
||||||
|
* because the default tuple format is <item, item, item, ...>
|
||||||
|
*/
|
||||||
writefln("{ %(%s, %) }", m);
|
writefln("{ %(%s, %) }", m);
|
||||||
|
|
||||||
/* tuple formatting */
|
/* you can expand tuples similarly to ranges, with %<CONTENTS%> where
|
||||||
|
* CONTENTS is a regular format string like if the tuple was formatted
|
||||||
|
* separately with each item of the tuple passed as a separate argument
|
||||||
|
*/
|
||||||
std::tuple<std::string, int, float> tup{"hello world", 1337, 3.14f};
|
std::tuple<std::string, int, float> tup{"hello world", 1337, 3.14f};
|
||||||
writefln("the tuple contains %<%s, %d, %f%>.", tup);
|
writefln("the tuple contains %<%s, %d, %f%>.", tup);
|
||||||
|
|
||||||
/* tuple format test */
|
/* formatting a range of tuples, with each tuple expanded
|
||||||
|
* with a nested tuple expansion inside a range expansion
|
||||||
|
*/
|
||||||
std::tuple<int, float, char const *> xt[] = {
|
std::tuple<int, float, char const *> xt[] = {
|
||||||
std::make_tuple(5, 3.14f, "foo"),
|
std::make_tuple(5, 3.14f, "foo"),
|
||||||
std::make_tuple(3, 1.23f, "bar"),
|
std::make_tuple(3, 1.23f, "bar"),
|
||||||
|
@ -78,15 +100,19 @@ int main() {
|
||||||
};
|
};
|
||||||
writefln("[ %#(<%d|%f|%s>%|, %) ]", xt);
|
writefln("[ %#(<%d|%f|%s>%|, %) ]", xt);
|
||||||
|
|
||||||
/* custom format */
|
/* formatting custom objects, the information about the format mark
|
||||||
|
* is passed into the to_format function and the object can read it
|
||||||
|
*/
|
||||||
writefln("%s", Foo{});
|
writefln("%s", Foo{});
|
||||||
writefln("%i", Foo{});
|
writefln("%i", Foo{});
|
||||||
|
|
||||||
/* custom format via method */
|
/* again, but using a method for formatting */
|
||||||
writefln("%s", Bar{});
|
writefln("%s", Bar{});
|
||||||
writefln("%i", Bar{});
|
writefln("%i", Bar{});
|
||||||
|
|
||||||
/* format into string */
|
/* formatting into a string sink (can be any output range, but
|
||||||
|
* appender makes sure the capacity is unlimited so it's safe)
|
||||||
|
*/
|
||||||
auto s = appender_range<std::string>{};
|
auto s = appender_range<std::string>{};
|
||||||
format(s, "hello %s", "world");
|
format(s, "hello %s", "world");
|
||||||
writeln(s.get());
|
writeln(s.get());
|
||||||
|
|
Loading…
Reference in a new issue