std.string.format call from varyargs

H. S. Teoh via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jul 2 15:13:37 PDT 2017


On Sun, Jul 02, 2017 at 11:38:34AM +0300, drug via Digitalmars-d-learn wrote:
> 02.07.2017 09:52, H. S. Teoh via Digitalmars-d-learn пишет:
[...]
> > Take a look at the docs that describe the "%(...%)" nested format
> > specifiers.  For example:
> > 
> > 	int[] arr = [ 1, 2, 3 ];
> > 	writefln("%(%s | %)", arr);
> > 
> > Output:
> > 
> > 	1 | 2 | 3
> > 
> > Explanation: %(...%) means a nested format specifier, where the
> > stuff enclosed between %( and %) are applied to each array element
> > (actually, range element -- it works for arbitrary input ranges). In
> > this case, the stuff in between is "%s | ", which is treated as "%s"
> > followed by the delimiter " | ". So each array element is formatted
> > with %s, and " | " is inserted as a delimiter.
> > 
> > A slightly more interesting example:
> > 
> > 	int[] arr = [ 1, 2, 3 ];
> > 	writefln("%(<%s>%|, %)", arr);
> > 
> > Output:
> > 
> > 	<1>, <2>, <3>
> > 
> > Explanation: the stuff between %( and %) is "<%s>%|, ", which is
> > understood as applying "<%s>" to each array element, and treating ",
> > " as the delimiter. The "%|" separates the per-element component
> > from the delimiter; this distinction is important because we want
> > the ">" to appear after every element including the last one, but we
> > don't want the ", " to appear after the last element.
> > 
> > You can also nest %(...%) to handle multidimensional arrays. Here's
> > my favorite example:
> > 
> > 	auto m = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ];
> > 	writefln("%([ %(%s, %) ]%|\n%)", m);
> > 
> > Output:
> > 
> > 	[ 1, 2, 3 ]
> > 	[ 4, 5, 6 ]
> > 	[ 7, 8, 9 ]
> > 
> > Hope this helps!
[...]
> Cool! Is it D only or could be used in printf (C/C++)?

AFAIK, this is a D-specific extension.


T

-- 
Why do conspiracy theories always come from the same people??


More information about the Digitalmars-d-learn mailing list