findLastSplit

Array-specialization of findLastSplit with default predicate.

  1. auto findLastSplit(inout(T)[] haystack, const(T)[] needle)
  2. auto findLastSplit(inout(T)[] haystack, T needle)
    findLastSplit
    (
    T
    )
    (
    scope return inout(T)[] haystack
    ,
    scope const T needle
    )
  3. bool startsWith(T[] haystack, T[] needle)
  4. bool startsWith(T[] haystack, T needle)

Examples

const h = "a**b**c";
const r = h.findLastSplit("**");
assert(r);
assert(r.pre is h[0 .. 4]);
assert(r.separator is h[4 .. 6]);
assert(r.post is h[6 .. 7]);

auto f()() @safe pure nothrow { char[1] x = "_"; return x[].findLastSplit(" "); }
static if (hasPreviewDIP1000) static assert(!__traits(compiles, { auto _ = f(); }));
const h = "a**b**c";
const r = h.findLastSplit("_");
static assert(r.sizeof == 2 * 2 * size_t.sizeof);
assert(!r);
assert(r.pre is h);
assert(r.separator is h[$ .. $]);
assert(r.post is h[$ .. $]);
const r = "a*b*c".findLastSplit('*');
static assert(r.sizeof == 3 * size_t.sizeof);
assert(r);
assert(r.pre == "a*b");
assert(r.separator == "*");
assert(r.post == "c");

auto f()() @safe pure nothrow { char[1] x = "_"; return x[].findLastSplit(' '); }
static if (hasPreviewDIP1000) static assert(!__traits(compiles, { auto _ = f(); }));

DIP-1000 scope analysis

char[] f() @safe pure nothrow
{
    char[3] haystack = "a*b";
    auto r = haystack[].findLastSplit('*');
    static assert(is(typeof(r.pre()) == char[]));
    return r.pre();         // TODO: this should fail
}
f();

Meta