std.algorithm.searching.countUntil - multiple declarations
Function countUntil
Counts elements in the given
input range
until the given predicate is true for one of the given needles or needle.
auto countUntil(alias pred, R, Rs...)(
R haystack,
Rs needles
)
if (isForwardRange!R && (Rs .length > 0) && (isForwardRange!(Rs[0]) == isInputRange!(Rs[0])) && allSatisfy!(canTestStartsWith!(pred, R), Rs));
ptrdiff_t countUntil(alias pred, R, N)(
R haystack,
N needle
)
if (isInputRange!R && is(typeof(binaryFun!pred(haystack .front, needle)) : bool));
Parameters
| Name | Description |
|---|---|
| pred | Binary predicate for determining when to stop counting,
which will be passed each element of haystack and a needle. |
| haystack | The input range to be counted. Must be a forward range to use multiple needles. |
| needles | A sequence of values or
forward ranges
of values, to be evaluated in turn by calling pred(element, needle)
with each element of haystack. |
| needle | A value passed as the 2nd argument to pred. |
Returns
- The number of elements which must be popped from the front of
haystack before reaching an element for which
startsWith!pred(haystack, needles) is true.
- If
startsWith!pred(haystack, needles) is not true for any element in
haystack, then -1 is returned.
- If more than one needle is provided,
countUntil will wrap the result in a pseudo-tuple similar to
Tuple!(ptrdiff_t, "steps", ptrdiff_t, "needle").
- steps is the count value, which can be implicitly tested for equality.
- needle is the index into needles which matched.
- Both are -1 if there was no match.
Warning
Due to auto-decoding, the return value of this function may not correspond
to the array index for strings. To find the index of an element matching
the predicate in a string, use indexOf instead.
See Also
Example
writeln(countUntil("hello world", "world")); // 6
writeln(countUntil("hello world", 'r')); // 8
writeln(countUntil("hello world", "programming")); // -1
writeln(countUntil("日本語", "本語")); // 1
writeln(countUntil("日本語", '語')); // 2
writeln(countUntil("日本語", "五")); // -1
writeln(countUntil("日本語", '五')); // -1
const arr = [0, 7, 12, 22, 9];
writeln(countUntil(arr, [12, 22])); // 2
writeln(countUntil(arr, 9)); // 4
writeln(countUntil!"a > b"(arr, 20)); // 3
Example
Multiple needles
auto res = "...hello" .countUntil("ha", "he");
writeln(res .steps); // 3
assert(res .needle == 1); // the 2nd needle matched
// returns -1 if no needle was found
res = "hello" .countUntil("ha", "hu");
writeln(res .steps); // -1
writeln(res .needle); // -1
// `steps` can also be implicitly compared
const arr = [0, 7, 12, 22, 9];
assert(countUntil(arr, 22, 12) == 2); // `12` found after 2 elements
writeln(countUntil(arr, 5, 6)); // -1
Function countUntil
Counts elements in the given
input range
until the given predicate is true for one of the elements of haystack.
ptrdiff_t countUntil(alias pred, R)(
R haystack
)
if (isInputRange!R && is(typeof(unaryFun!pred(haystack .front)) : bool));
Parameters
| Name | Description |
|---|---|
| pred | Unary predicate for determining when to stop counting. |
| haystack | The input range to be counted. |
Returns
- The number of elements which must be popped from the front of
haystack before reaching an element for which
startsWith!pred(haystack) is true.
- If startsWith!pred(haystack) is not true for any element in
haystack, then -1 is returned.
Warning
Due to auto-decoding, the return value of this function may not correspond
to the array index for strings. To find the index of an element matching
the predicate in a string, use indexOf instead.
Example
import std .ascii : isDigit;
import std .uni : isWhite;
writeln(countUntil!(isWhite)("hello world")); // 5
writeln(countUntil!(isDigit)("hello world")); // -1
writeln(countUntil!"a > 20"([0, 7, 12, 22, 9])); // 3