Function std.algorithm.searching.startsWith
Checks whether the given
input range starts with (one
of) the given needle(s) or, if no needles are given,
if its front element fulfils predicate pred
.
uint startsWith(alias pred, Range, Needles...)
(
Range doesThisStart,
Needles withOneOfThese
)
if (isInputRange!Range && (Needles .length > 1) && is(typeof( .startsWith!pred(doesThisStart, withOneOfThese[0])) : bool) && is(typeof( .startsWith!pred(doesThisStart, withOneOfThese[1..__dollar])) : uint));
bool startsWith(alias pred, R1, R2)
(
R1 doesThisStart,
R2 withThis
)
if (isInputRange!R1 && isInputRange!R2 && is(typeof(binaryFun!pred(doesThisStart .front, withThis .front)) : bool));
bool startsWith(alias pred, R, E)
(
R doesThisStart,
E withThis
)
if (isInputRange!R && is(typeof(binaryFun!pred(doesThisStart .front, withThis)) : bool));
bool startsWith(alias pred, R)
(
R doesThisStart
)
if (isInputRange!R && ifTestable!(typeof(doesThisStart .front), unaryFun!pred));
Parameters
Name | Description |
---|---|
pred | Predicate to use in comparing the elements of the haystack and the needle(s). Mandatory if no needles are given. |
doesThisStart | The input range to check. |
withOneOfThese | The needles against which the range is to be checked, which may be individual elements or input ranges of elements. |
withThis | The single needle to check, which may be either a single element or an input range of elements. |
Returns
0 if the needle(s) do not occur at the beginning of the given range;
otherwise the position of the matching needle, that is, 1 if the range starts
with withOneOfThese[0]
, 2 if it starts with withOneOfThese[1]
, and so
on.
In the case where doesThisStart
starts with multiple of the ranges or
elements in withOneOfThese
, then the shortest one matches (if there are
two which match which are of the same length (e.g. "a"
and 'a'
), then
the left-most of them in the argument
list matches).
In the case when no needle parameters are given, return true
iff front of
doesThisStart
fulfils predicate pred
.
Example
import std .ascii : isAlpha;
assert("abc" .startsWith!(a => a .isAlpha));
assert("abc" .startsWith!isAlpha);
assert(!"1ab" .startsWith!(a => a .isAlpha));
assert(!"" .startsWith!(a => a .isAlpha));
import std .algorithm .comparison : among;
assert("abc" .startsWith!(a => a .among('a', 'b') != 0));
assert(!"abc" .startsWith!(a => a .among('b', 'c') != 0));
assert(startsWith("abc", ""));
assert(startsWith("abc", "a"));
assert(!startsWith("abc", "b"));
writeln(startsWith("abc", 'a', "b")); // 1
writeln(startsWith("abc", "b", "a")); // 2
writeln(startsWith("abc", "a", "a")); // 1
writeln(startsWith("abc", "ab", "a")); // 2
writeln(startsWith("abc", "x", "a", "b")); // 2
writeln(startsWith("abc", "x", "aa", "ab")); // 3
writeln(startsWith("abc", "x", "aaa", "sab")); // 0
writeln(startsWith("abc", "x", "aaa", "a", "sab")); // 3
import std .typecons : Tuple;
alias C = Tuple!(int, "x", int, "y");
assert(startsWith!"a.x == b"([ C(1,1), C(1,2), C(2,2) ], [1, 1]));
writeln(startsWith!"a.x == b"([C(1, 1), C(2, 1), C(2, 2)], [1, 1], [1, 2], [1, 3])); // 2