overlap

Returns the overlapping portion, if any, of two arrays. Unlike equal, overlap only compares the pointers and lengths in the ranges, not the values referred by them. If r1 and r2 have an overlapping slice, returns that slice. Otherwise, returns the null slice.

@trusted
CommonType!(T[], U[])
overlap
(
T
U
)
(
T[] a
,
U[] b
)
if (
is(typeof(a.ptr < b.ptr) == bool)
)

Parameters

a T[]

The first array to compare

b U[]

The second array to compare

Return Value

Type: CommonType!(T[], U[])

The overlapping portion of the two arrays.

Examples

int[] a = [ 10, 11, 12, 13, 14 ];
int[] b = a[1 .. 3];
assert(overlap(a, b) == [ 11, 12 ]);
b = b.dup;
// overlap disappears even though the content is the same
assert(overlap(a, b).empty);

static test()() @nogc
{
    auto a = "It's three o'clock"d;
    auto b = a[5 .. 10];
    return b.overlap(a);
}

//works at compile-time
static assert(test == "three"d);
import std.meta : AliasSeq;

// can be used as an alternative implementation of overlap that returns
// `true` or `false` instead of a slice of the overlap
bool isSliceOf(T)(const scope T[] part, const scope T[] whole)
{
    return part.overlap(whole) is part;
}

auto x = [1, 2, 3, 4, 5];

assert(isSliceOf(x[3..$], x));
assert(isSliceOf(x[], x));
assert(!isSliceOf(x, x[3..$]));
assert(!isSliceOf([7, 8], x));
assert(isSliceOf(null, x));

// null is a slice of itself
assert(isSliceOf(null, null));

foreach (T; AliasSeq!(int[], const(int)[], immutable(int)[], const int[], immutable int[]))
{
    T a = [1, 2, 3, 4, 5];
    T b = a;
    T c = a[1 .. $];
    T d = a[0 .. 1];
    T e = null;

    assert(isSliceOf(a, a));
    assert(isSliceOf(b, a));
    assert(isSliceOf(a, b));

    assert(isSliceOf(c, a));
    assert(isSliceOf(c, b));
    assert(!isSliceOf(a, c));
    assert(!isSliceOf(b, c));

    assert(isSliceOf(d, a));
    assert(isSliceOf(d, b));
    assert(!isSliceOf(a, d));
    assert(!isSliceOf(b, d));

    assert(isSliceOf(e, a));
    assert(isSliceOf(e, b));
    assert(isSliceOf(e, c));
    assert(isSliceOf(e, d));

    //verifies R-value compatibilty
    assert(!isSliceOf(a[$ .. $], a));
    assert(isSliceOf(a[0 .. 0], a));
    assert(isSliceOf(a, a[0.. $]));
    assert(isSliceOf(a[0 .. $], a));
}

Meta