Output range of ranges to single buffer

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jan 13 13:46:44 PST 2016


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

> 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);

And buf can be an Appender:

import std.stdio;
import std.format;
import std.array;

void main() {
     auto data = [ 1, 2, 3, 4, 5 ];

     auto buf = appender!string(); // Or appender!(char[]) if needed
     formattedWrite(buf, "[%(%d, %)]", data);

     assert(buf.data == "[1, 2, 3, 4, 5]");
}

Ali



More information about the Digitalmars-d-learn mailing list