Fix Issue 21724 - std.algorithm.mutation.copy fails on overlapping arrays if the source array's pointer is less than the destination array's pointer

This commit is contained in:
Nathan Sashihara 2021-03-18 02:03:39 -07:00 committed by Nicholas Lindsay Wilson
parent d3a1433921
commit edd9007c35

View file

@ -374,14 +374,22 @@ if (isInputRange!SourceRange && isOutputRange!(TargetRange, ElementType!SourceRa
assert(tlen >= slen,
"Cannot copy a source range into a smaller target range.");
immutable overlaps = __ctfe || () @trusted {
immutable overlaps = () @trusted {
return source.ptr < target.ptr + tlen &&
target.ptr < source.ptr + slen; }();
if (overlaps)
{
if (source.ptr < target.ptr)
{
foreach_reverse (idx; 0 .. slen)
target[idx] = source[idx];
}
else
{
foreach (idx; 0 .. slen)
target[idx] = source[idx];
}
return target[slen .. tlen];
}
else
@ -507,6 +515,13 @@ $(LINK2 http://en.cppreference.com/w/cpp/algorithm/copy_backward, STL's `copy_ba
assert(a[4 .. 9] == [6, 7, 8, 9, 10]);
}
// https://issues.dlang.org/show_bug.cgi?id=21724
{
int[] a = [1, 2, 3, 4];
copy(a[0 .. 2], a[1 .. 3]);
assert(a == [1, 1, 2, 4]);
}
// https://issues.dlang.org/show_bug.cgi?id=7898
{
enum v =