Building a string from n chars

monarch_dodra via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Sep 4 14:01:49 PDT 2014


On Thursday, 4 September 2014 at 20:57:43 UTC, monarch_dodra 
wrote:
> 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 re-read the doc and implementation: replicate replicates a 
*range*. It is a bit optimized to detect the case where the range 
is a single element, but it still has to do the check, and even 
then (implementation detail), it is less efficient. I might 
create a pull to tweak that.


More information about the Digitalmars-d-learn mailing list