Binary predicate for determining when to stop counting, which will be passed each element of haystack and a needle.
The input range to be counted. Must be a forward range to use multiple needles.
A value passed as the 2nd argument to pred.
- 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.
assert(countUntil("hello world", "world") == 6); assert(countUntil("hello world", 'r') == 8); assert(countUntil("hello world", "programming") == -1); assert(countUntil("日本語", "本語") == 1); assert(countUntil("日本語", '語') == 2); assert(countUntil("日本語", "五") == -1); assert(countUntil("日本語", '五') == -1); const arr = [0, 7, 12, 22, 9]; assert(countUntil(arr, [12, 22]) == 2); assert(countUntil(arr, 9) == 4); assert(countUntil!"a > b"(arr, 20) == 3);
Multiple needles
auto res = "...hello".countUntil("ha", "he"); assert(res.steps == 3); assert(res.needle == 1); // the 2nd needle matched // returns -1 if no needle was found res = "hello".countUntil("ha", "hu"); assert(res.steps == -1); assert(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 assert(countUntil(arr, 5, 6) == -1);
Counts elements in the given input range until the given predicate is true for one of the given needles or needle.