View source code
Display the source code in std/algorithm/searching.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

Function std.algorithm.searching.countUntil

Counts elements in the given forward range until the given predicate is true for one of the given needles.

ptrdiff_t countUntil(alias pred, R, Rs...) (
  R haystack,
  Rs needles
)
if (isForwardRange!R && (Rs.length > 0) && (isForwardRange!(Rs[0]) == isInputRange!(Rs[0])) && is(typeof(startsWith!pred(haystack, needles[0]))) && (Rs.length == 1 || is(typeof(countUntil!pred(haystack, needles[1..__dollar])))));

ptrdiff_t countUntil(alias pred, R, N) (
  R haystack,
  N needle
)
if (isInputRange!R && is(typeof(binaryFun!pred(haystack.front, needle)) : bool));

ptrdiff_t countUntil(alias pred, R) (
  R haystack
)
if (isInputRange!R && is(typeof(unaryFun!pred(haystack.front)) : bool));

Parameters

NameDescription
pred The predicate for determining when to stop counting.
haystack The input range to be counted.
needles Either a single element, or a forward range of elements, to be evaluated in turn against each element in haystack under the given predicate.

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 only pred is provided, pred(haystack) is tested for each element.

See Also

indexOf

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
writeln(countUntil([0, 7, 12, 22, 9], [12, 22])); // 2
writeln(countUntil([0, 7, 12, 22, 9], 9)); // 4
writeln(countUntil!"a > b"([0, 7, 12, 22, 9], 20)); // 3

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

Authors

Andrei Alexandrescu

License

Boost License 1.0.