Skip to content

Commit

Permalink
Merge pull request #2296 from Element-126/doc_formatValue_add_example
Browse files Browse the repository at this point in the history
Add documentation examples for formatValue()
  • Loading branch information
DmitryOlshansky committed Jul 4, 2014
2 parents cc92017 + aa7022b commit 6883e0e
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions std/format.d
Original file line number Diff line number Diff line change
Expand Up @@ -2697,6 +2697,52 @@ if (is(T == class) && !is(T == enum))
}
}

/++
$(D formatValue) allows to reuse existing format specifiers:
+/
unittest
{
import std.format;
import std.string: format;

struct Point
{
int x, y;

void toString(scope void delegate(const(char)[]) sink,
FormatSpec!char fmt) const
{
sink("(");
sink.formatValue(x, fmt);
sink(",");
sink.formatValue(y, fmt);
sink(")");
}
}

auto p = Point(16,11);
assert(format("%03d", p) == "(016,011)");
assert(format("%02x", p) == "(10,0b)");
}

/++
The following code compares the use of $(D formatValue) and $(D formattedWrite).
+/
unittest
{
import std.format;
import std.string: appender;

auto writer1 = appender!string();
writer1.formattedWrite("%08b", 42);

auto writer2 = appender!string();
auto f = singleSpec("%08b");
writer2.formatValue(42, f);

assert(writer1.data == writer2.data && writer1.data == "00101010");
}

unittest
{
// class range (issue 5154)
Expand Down

0 comments on commit 6883e0e

Please sign in to comment.