View source code
Display the source code in std/algorithm/searching.d from which this page was generated on
github.
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.
Page wiki
View or edit the community-maintained wiki page associated with this page.
std.algorithm.searching.count_until
- multiple declarations
- Function countUntil
- Function countUntil
Function countUntil
Counts elements in the given
forward range
until
the given predicate is true for one of the given
.
needles
Prototypes
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));
Parameters
Name | Description |
---|---|
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 under the given predicate. |
Returns
The number of elements which must be popped from the front of
before reaching an element for which
haystack
is startsWith
!pred(haystack
, needles
)true
. If
is not startsWith
!pred(haystack
, needles
)true
for any
element in
, then haystack
-1
is returned.
Example
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); assert(countUntil([0, 7, 12, 22, 9], [12, 22]) == 2); assert(countUntil([0, 7, 12, 22, 9], 9) == 4); assert(countUntil!"a > b"([0, 7, 12, 22, 9], 20) == 3);
Function countUntil
Similar to the previous overload of
, except that this one
evaluates only the predicate countUntil
pred
.
Prototype
ptrdiff_t countUntil(alias pred, R)( R haystack ) if (isInputRange!R && is(typeof(unaryFun!pred(haystack.front)) : bool));
Parameters
Name | Description |
---|---|
pred | Predicate to when to stop counting. |
haystack | An input range of elements to be counted. |
Returns
The number of elements which must be popped from
before haystack
pred(haystack.front)
is true
.
Example
import std.ascii : isDigit; import std.uni : isWhite; assert(countUntil!(std.uni.isWhite)("hello world") == 5); assert(countUntil!(std.ascii.isDigit)("hello world") == -1); assert(countUntil!"a > 20"([0, 7, 12, 22, 9]) == 3);