findSplit

Array-specialization of findSplit with default predicate.

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

Examples

const h = "a**b";
const r = h.findSplit("**");
assert(r);
assert(r.pre is h[0 .. 1]);
assert(r.separator is h[1 .. 3]);
assert(r.post is h[3 .. 4]);

auto f()() @safe pure nothrow { char[1] x = "_"; return x[].findSplit(" "); }
static if (isDIP1000) static assert(!__traits(compiles, { auto _ = f(); }));
const h = "a**b";
const r = h.findSplit("_");
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[$ .. $]);
import std.algorithm.searching : findSplit;
const h = "a**b";
const r = h.findSplit("_");
static assert(r.sizeof == 3 * 2 * size_t.sizeof);
assert(!r);
assert(r[0] is h);
assert(r[1] is h[$ .. $]);
assert(r[2] is h[$ .. $]);
const r = "a*b".findSplit('*');
static assert(r.sizeof == 3 * size_t.sizeof);
assert(r);
assert(r.pre == "a");
assert(r.separator == "*");
assert(r.post == "b");

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

DIP-1000 scope analysis

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

See Also

Meta