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

1 int[] a = [ 10, 11, 12, 13, 14 ];
2 int[] b = a[1 .. 3];
3 assert(overlap(a, b) == [ 11, 12 ]);
4 b = b.dup;
5 // overlap disappears even though the content is the same
6 assert(overlap(a, b).empty);
7 
8 static test()() @nogc
9 {
10     auto a = "It's three o'clock"d;
11     auto b = a[5 .. 10];
12     return b.overlap(a);
13 }
14 
15 //works at compile-time
16 static assert(test == "three"d);

Meta

Suggestion Box / Bug Report