string literal error?

badmadevil badmadevil at gmail.com
Thu May 8 12:53:36 PDT 2008


Simen Kjaeraas wrote:
> On Thu, 08 May 2008 20:37:17 +0200, badmadevil <badmadevil at gmail.com> 
> wrote:
> 
>> badmadevil wrote:
>>> Hi, sorry if already discussed.
>>>  codes:
>>>   string x = "bug?" ;
>>>   string y = "bug?" ;
>>>   string z = "bug?".reverse ;
>>>   writefln("%s : %s : %s", x, y, z) ;
>>>   x.reverse ;
>>
>> same effect if x is replace with literal "bug?", ie.
>>    "bug?".reverse ;
>>
>>>   writefln("%s : %s : %s", x, y, z) ;
>>>  output - D 1.027 & 2.013:
>>>  >?gub : ?gub : ?gub
>>>  >bug? : bug? : bug?
>>>  Should x, y, z be independent to each others?
> 
> This happens because .reverse does in-place reversal. The
> correct way to do it would be:
> 
>   string x = "bug?".idup;
>   string y = "bug?".idup;
>   string z = "bug?".idup.reverse;
>   writefln(x,y,z);
>   x.reverse;
>   writefln(x,y,z);
>   writefln(typeof("bug?").stringof);
> 
> Now, seeing as the array contents are invariant in this example,
> .reverse is not following the D rules of conduct. (i.e. it's buggy)
> 

I see, .reverse is buggy in D2 situation.

How about D1?
string in D1 is just alias of char[]. It should be valid in the case
of x.reverse. (x is of type char[])

It seems that the reason x,y,z reference to same string literal is to
reduce bloated string data in the .exe (repeated literal array of other
type, eg. integer, may not be reduced).
dup/idup each literal array may avoid such bug, but would it be handy
that idup/dup be implictly called during runtime?

(btw, the title should read "string literal bug?")

> -- Simen


More information about the Digitalmars-d-learn mailing list