Building a string from n chars
monarch_dodra via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Sep 4 13:57:41 PDT 2014
On Thursday, 4 September 2014 at 20:38:39 UTC, Nordlöw wrote:
> On Thursday, 4 September 2014 at 19:24:00 UTC, Nordlöw wrote:
>> string t1; t1 ~= '*'.repeat(n).array;
>> string t2; t2 ~= "*".replicate(n);
>
> After having read
> http://dlang.org/phobos/std_array.html#.replicate
>
> I came to the conclusion that the lazy std.range:repeat is
> preferred.
If lazy is good enough for you yes. AFAIK, replicate is *very*
close in terms of implementation to what a.repeat(n).array()
would do anyways. Heck, I'd be surprised if it did it
differently, since (again, AFAIK) repeat.array() is "optimal"
anyways.
> I'm still a bit confused about the fact that -vgc gives no
> warnings about GC-allocations, though.
Strange indeed. Both solutions allocate a slice, and then append
that slice. The "s[]='*'" Solution I gave you will not create a
temporary allocation.
More information about the Digitalmars-d-learn
mailing list