How to place char* of stringZ to ubyte[]?

bearophile bearophileHUGS at lycos.com
Tue Oct 30 05:38:33 PDT 2012


denizzzka:

> I am concerned about the extra allocations of temp arrays. here 
> is it, or not? compiler optimizes it?

There is one allocation, and usually the D compilers can't 
optimize it away.


> In my case it does not matter but for the future I would like 
> to know how it can be implemented without overhead.

If you don't want overhead you need some static buffer, bigger 
than any possible string, and to copy data on it and slice it as 
needed. Otherwise appending one null char risks allocating. If 
you append it in-place in the original string the risk of 
allocations is lower, but not zero.


>>    byte[] data = [0];
>
> Why not "byte[] data;" ?

I was thinking about keeping the protocol invariant, but you are 
right, it's probably useless. A null is enough to denote an empty 
string.


>>    alias this = data; // new syntax
>
> What difference between this syntax and "alias Something this"?

There's no difference. Instead of focusing on other syntaxes that 
actually do something useful (like [$] to define fixed-size 
arrays automatically, or somehting else), Walter has accepted to 
add one more way to do the same thing. I don't like this change.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list