string-int[] array

Meta via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Mar 8 14:47:00 PDT 2015


On Sunday, 8 March 2015 at 21:41:44 UTC, FG wrote:
> On 2015-03-08 at 20:26, Meta wrote:
>> On Sunday, 8 March 2015 at 18:57:38 UTC, Kagamin wrote:
>>> http://dpaste.dzfl.pl/2c8d4a7d9ef0 like this.
>>
>> What in the world is that code doing? I'm having a hard time 
>> wrapping my head around this.
>
> It's a trick to reuse string internals to store an int.
> A string is a struct with two values (length, ptr).
> ivalue(i) is used to set ptr = i and length = 0.
>
> Except that with this solution you will confuse empty strings 
> with ints.
> You could give such strings special treatment by replacing:
>
>     this(string s){ svalue=s; }
>
> with:
>
>     this(string s){ svalue=s; if (!s.length) svalue = 
> cast(string)(cast(char*)0)[X..X]; }
>     // where X is some magic int value to mark that we are 
> dealing with an empty string,
>
> you'd still be confused if someone actually wanted to store the 
> X value.

Oh, I see. What was tripping me up was

`svalue=cast(string)(cast(char*)0)[i..i];`

But I see now that it's just creating an empty string.


More information about the Digitalmars-d-learn mailing list