Behaviour of append (~=)

Derek Parnell derek at psych.ward
Tue May 30 08:40:57 PDT 2006


On Tue, 30 May 2006 23:20:45 +1000, Oskar Linde  
<oskar.lindeREM at OVEgmail.com> wrote:

>>> Wouldn't it be a good optimization for ~= to check for null first, to  
>>> prevent the copy?
>>  Not so sure, because for consistency sake its good to know that  
>> 'append' will always do a copy. Then we can rely on this to happen.
>
> But does it? Isn't n repeated appends guaranteed to give at most log(n)  
> allocations? How can that be if all appends force a copy? A simple test:

I didn't mention allocations. I was talking about copying.

> char[] a = "abcdefgh";
> char[] b = a[0..3];
> b ~= "xx";
> writefln("a = %s",a);
>
> prints:
> abcxxfgh

Yes, but the "xx" was copied wasn't it. That is, 'a' does not contain a  
slice to the literal "xx".


-- 
Derek Parnell
Melbourne, Australia



More information about the Digitalmars-d-learn mailing list