@@ -59,9 +59,6 @@ $(TR $(TH Function Name) $(TH Description)
5959 $(TD Checks if the final segments of two arrays refer to the same place
6060 in memory.
6161 ))
62- $(TR $(TD $(LREF isSliceOf))
63- $(TD Checks if the first array is a slice of the second array.
64- ))
6562 $(TR $(TD $(LREF split))
6663 $(TD Eagerly split a range or string into an array.
6764 ))
@@ -1271,6 +1268,63 @@ if (is(typeof(a.ptr < b.ptr) == bool))
12711268 static assert (test == " three" d);
12721269}
12731270
1271+ // /
1272+ @safe pure nothrow unittest
1273+ {
1274+
1275+ // can be used as an alternative implementation of overlap that returns
1276+ // `true` or `false` instead of a slice of the overlap
1277+ bool isSliceOf (T)(const scope T[] part, const scope T[] whole)
1278+ {
1279+ return part.overlap(whole) is part;
1280+ }
1281+
1282+ auto x = [1 , 2 , 3 , 4 , 5 ];
1283+
1284+ assert (isSliceOf(x[3 .. $], x));
1285+ assert (isSliceOf(x[], x));
1286+ assert (! isSliceOf(x, x[3 .. $]));
1287+ assert (! isSliceOf([7 , 8 ], x));
1288+ assert (isSliceOf(null , x));
1289+
1290+ // null is a slice of itself
1291+ assert (isSliceOf(null , null ));
1292+
1293+ foreach (T; AliasSeq! (int [], const (int )[], immutable (int )[], const int [], immutable int []))
1294+ {
1295+ T a = [1 , 2 , 3 , 4 , 5 ];
1296+ T b = a;
1297+ T c = a[1 .. $];
1298+ T d = a[0 .. 1 ];
1299+ T e = null ;
1300+
1301+ assert (isSliceOf(a, a));
1302+ assert (isSliceOf(b, a));
1303+ assert (isSliceOf(a, b));
1304+
1305+ assert (isSliceOf(c, a));
1306+ assert (isSliceOf(c, b));
1307+ assert (! isSliceOf(a, c));
1308+ assert (! isSliceOf(b, c));
1309+
1310+ assert (isSliceOf(d, a));
1311+ assert (isSliceOf(d, b));
1312+ assert (! isSliceOf(a, d));
1313+ assert (! isSliceOf(b, d));
1314+
1315+ assert (isSliceOf(e, a));
1316+ assert (isSliceOf(e, b));
1317+ assert (isSliceOf(e, c));
1318+ assert (isSliceOf(e, d));
1319+
1320+ // verifies R-value compatibilty
1321+ assert (! isSliceOf(a[$ .. $], a));
1322+ assert (isSliceOf(a[0 .. 0 ], a));
1323+ assert (isSliceOf(a, a[0 .. $]));
1324+ assert (isSliceOf(a[0 .. $], a));
1325+ }
1326+ }
1327+
12741328@safe pure nothrow unittest
12751329{
12761330 static void test (L, R)(L l, R r)
@@ -1816,84 +1870,6 @@ pure nothrow @nogc bool sameTail(T)(in T[] lhs, in T[] rhs)
18161870 }}
18171871}
18181872
1819- /**
1820- Checks whether a `part` is part of a `whole` array.
1821-
1822- Params:
1823-
1824- part = The array to check for its origin
1825- whole = The origin array to be queried
1826-
1827- Returns: `true` if `part` is a slice of `whole`.
1828- */
1829- bool isSliceOf (T)(const scope T[] part, const scope T[] whole) @trusted
1830- {
1831- return whole.ptr <= part.ptr &&
1832- part.ptr + part.length <= whole.ptr + whole.length;
1833- }
1834-
1835- // /
1836- @safe pure nothrow unittest
1837- {
1838- auto a = [1 , 2 , 3 , 4 , 5 ];
1839-
1840- assert (a[3 .. $].isSliceOf(a));
1841- assert (a[].isSliceOf(a));
1842- assert (! a.isSliceOf(a[3 .. $]));
1843- assert (! [7 , 8 ].isSliceOf(a));
1844- assert (! null .isSliceOf(a));
1845-
1846- // null is a slice of itself
1847- assert (null .isSliceOf(null ));
1848- }
1849-
1850- @safe pure nothrow unittest
1851- {
1852- static foreach (T; AliasSeq! (int [], const (int )[], immutable (int )[], const int [], immutable int []))
1853- {{
1854- T a = [1 , 2 , 3 , 4 , 5 ];
1855- T b = a;
1856- T c = a[1 .. $];
1857- T d = a[0 .. 1 ];
1858- T e = null ;
1859-
1860- assert (a.isSliceOf(a));
1861- assert (b.isSliceOf(a));
1862- assert (a.isSliceOf(b));
1863-
1864- assert (c.isSliceOf(a));
1865- assert (c.isSliceOf(b));
1866- assert (! a.isSliceOf(c));
1867- assert (! b.isSliceOf(c));
1868-
1869- assert (d.isSliceOf(a));
1870- assert (d.isSliceOf(b));
1871- assert (! a.isSliceOf(d));
1872- assert (! b.isSliceOf(d));
1873-
1874- assert (! e.isSliceOf(a));
1875- assert (! e.isSliceOf(b));
1876- assert (! e.isSliceOf(c));
1877- assert (! e.isSliceOf(d));
1878-
1879- // verifies R-value compatibilty
1880- assert (a[$ .. $].isSliceOf(a));
1881- assert (a[0 .. 0 ].isSliceOf(a));
1882- assert (a.isSliceOf(a[0 .. $]));
1883- assert (a[0 .. $].isSliceOf(a));
1884- }}
1885- }
1886-
1887- // null is a slice of itself
1888- @safe pure nothrow unittest
1889- {
1890- assert (null .isSliceOf(null ));
1891-
1892- int [] arr;
1893- assert (null .isSliceOf(arr));
1894- assert (arr.isSliceOf(null ));
1895- }
1896-
18971873/**
18981874Params:
18991875 s = an $(REF_ALTTEXT input range, isInputRange, std,range,primitives)
0 commit comments