Output range of ranges to single buffer

Jacob Carlborg via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jan 13 23:41:23 PST 2016


On 2016-01-13 22:20, H. S. Teoh via Digitalmars-d-learn wrote:

> Isn't that just a matter of replacing each of the segments with their
> range equivalents? Also, std.format.formattedWrite will do
> writeln-formatting into a buffer (well, any output range, really) -- I'm
> pretty sure it doesn't allocate, at least for the simplest cases like
> converting an integer. So you should be able to do something like this:
>
> 	auto data = [ 1, 2, 3, 4, 5 ];
> 	char[] buf = ...;
> 	formattedWrite(buf, "[%(%d, %)]", data);

Aha, interesting. I didn't know formattedWrite could format an 
array/range directly like that.

The more complex example works by defining "toString" which takes an 
output range (delegate). But what if I need to format a third party type 
that I cannot add methods to? UFCS does not seem to work.

-- 
/Jacob Carlborg


More information about the Digitalmars-d-learn mailing list