Thread overview
bug: concatenation doesn't create a copy
Jul 31, 2004
Nick
Jul 31, 2004
From
Jul 31, 2004
Ben Hinkle
Jul 31, 2004
Nick
Aug 02, 2004
Stewart Gordon
Aug 03, 2004
Nick
Aug 04, 2004
Stewart Gordon
July 31, 2004
Maybe this is a known bug already, in that case consider this a friendly reminder :)

The documentation says the following:

# Concatenation always creates a copy of its operands, even if one of
# the operands is a 0 length array, so:
#
#	a = b			a refers to b
#	a = b ~ c[0..0]		a refers to a copy of b

However, this isn't the case, as the following example illustrates:

# void main()
# {
#   char[] a, b;
#   a.length = 3;
#   a[] = "foo";
#
#   b = a ~ "";   // b SHOULD be a copy of a
#   b[0] = 'm';   // so this shouldn't affect a
#   assert(a == "moo"); // but it does
# }

Regards,
Nick


July 31, 2004
Oh, forgot to add: Even if it did work, having to write something like

a = b ~ "";

to copy a string seems a bit arcane to me. And the obvious "proper" way to copy arrays (and indeed the only way to do it at the moment) is

a.length = b.length;
a[] = b[];

Why isn't there a .copy property? Something like

a = b.copy;  // a is a copy of b


Nick

In article <ceh4l7$1ddi$1@digitaldaemon.com>, Nick says...
>
>Maybe this is a known bug already, in that case consider this a friendly reminder :)
>
>The documentation says the following:
>
># Concatenation always creates a copy of its operands, even if one of
># the operands is a 0 length array, so:
>#
>#	a = b			a refers to b
>#	a = b ~ c[0..0]		a refers to a copy of b
>
>However, this isn't the case, as the following example illustrates:
>
># void main()
># {
>#   char[] a, b;
>#   a.length = 3;
>#   a[] = "foo";
>#
>#   b = a ~ "";   // b SHOULD be a copy of a
>#   b[0] = 'm';   // so this shouldn't affect a
>#   assert(a == "moo"); // but it does
># }
>
>Regards,
>Nick


July 31, 2004
From wrote:

> Oh, forgot to add: Even if it did work, having to write something like
> 
> a = b ~ "";
> 
> to copy a string seems a bit arcane to me. And the obvious "proper" way to copy arrays (and indeed the only way to do it at the moment) is
> 
> a.length = b.length;
> a[] = b[];
> 
> Why isn't there a .copy property? Something like
> 
> a = b.copy;  // a is a copy of b

try .dup

July 31, 2004
In article <ceh71j$1eci$1@digitaldaemon.com>, Ben Hinkle says...
>
>> Oh, forgot to add: Even if it did work, having to write something like
>> 
>> a = b ~ "";
>> 
>> to copy a string seems a bit arcane to me. And the obvious "proper" way to copy arrays (and indeed the only way to do it at the moment) is
>> 
>> a.length = b.length;
>> a[] = b[];
>> 
>> Why isn't there a .copy property? Something like
>> 
>> a = b.copy;  // a is a copy of b
>
>try .dup

And it's even documented :) Silly me. Thanks.

Nick


August 02, 2004
Nick wrote:

> In article <ceh71j$1eci$1@digitaldaemon.com>, Ben Hinkle says...
<snip>
>> try .dup
> 
> And it's even documented :) Silly me. Thanks.

Indeed.  But wasn't your original post about the fact that you can't rely on concatenation to create a copy, if at runtime one of the operands may or may not be zero-length?

Stewart.

-- 
My e-mail is valid but not my primary mailbox.  Please keep replies on the 'group where everyone may benefit.
August 03, 2004
In article <cel5j3$2vk3$1@digitaldaemon.com>, Stewart Gordon says...
>
>Indeed.  But wasn't your original post about the fact that you can't rely on concatenation to create a copy, if at runtime one of the operands may or may not be zero-length?
>

Yes, that's correct.

I'm not quite sure how I feel about that one, though. The "copy-on-write" principle that D seems to follow says that string1 ~ string2 should be a reference, not a copy, if one of the strings are empty. This might be more efficient in some cases.

On the other hand, if you want to be sure to get a copy, you would then have to write (string1 ~ string2).dup, which might cause double copying if both strings are nonempty (unless the compiler can optimize one of them away automatically.)

In any case, dmd does not follow with the docs at the moment.

Nick


August 04, 2004
Nick wrote:

<snip>
> On the other hand, if you want to be sure to get a copy, you would then have to write (string1 ~ string2).dup, which might cause double copying if both strings are nonempty (unless the compiler can optimize one of them away automatically.)

After checking that it doesn't already optimise away the .dup under the false belief that concatenation works correctly.

Stewart.

-- 
My e-mail is valid but not my primary mailbox.  Please keep replies on the 'group where everyone may benefit.