find

Finds the first occurrence of needle in haystack if any.

@nogc nothrow pure @trusted
inout(void[])
find
(
return inout void[] haystack
,
ubyte needle
)

Parameters

haystack void[]

Memory block.

needle ubyte

A byte.

Return Value

Type: inout(void[])

The subrange of haystack whose first element is the first occurrence of needle. If needle couldn't be found, an empty inout void[] is returned.

Examples

t
{
    const ubyte[9] haystack = ['a', 'b', 'c', 'd', 'e', 'f', 'b', 'g', 'h'];

    assert(equal(find(haystack, 'a'), haystack[]));
    assert(equal(find(haystack, 'b'), haystack[1 .. $]));
    assert(equal(find(haystack, 'c'), haystack[2 .. $]));
    assert(equal(find(haystack, 'd'), haystack[3 .. $]));
    assert(equal(find(haystack, 'e'), haystack[4 .. $]));
    assert(equal(find(haystack, 'f'), haystack[5 .. $]));
    assert(equal(find(haystack, 'h'), haystack[8 .. $]));
    assert(find(haystack, 'i').length == 0);

    assert(find(null, 'a').length == 0

Meta