mirror of
https://github.com/dlang/phobos.git
synced 2025-04-29 14:40:30 +03:00
Revert "Removed new, unnecessary overload of countUntil."
This reverts commit b1765dc0cb
.
Okay. So, I misread what count does and reverted my previous changes
prematurely. So I'm putting them back. count counts the number of
elements in the entire range which match the predicate, not just until
the predicate fails. So, it does _not_ do what the new overload of
countUntil does.
This commit is contained in:
parent
7eee94d2a8
commit
3d559d88a8
1 changed files with 45 additions and 0 deletions
|
@ -3655,6 +3655,51 @@ unittest
|
|||
assert(countUntil!"a > b"([0, 7, 12, 22, 9], 20) == 3);
|
||||
}
|
||||
|
||||
/++
|
||||
Returns the number of elements which must be popped from $(D haystack)
|
||||
before $(D pred(hastack.front)) is $(D true.).
|
||||
|
||||
Examples:
|
||||
--------------------
|
||||
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);
|
||||
--------------------
|
||||
+/
|
||||
sizediff_t countUntil(alias pred, R)(R haystack)
|
||||
if (isForwardRange!R && is(typeof(unaryFun!pred(haystack.front)) == bool))
|
||||
{
|
||||
static if (isNarrowString!R)
|
||||
{
|
||||
// Narrow strings are handled a bit differently
|
||||
auto length = haystack.length;
|
||||
for (; !haystack.empty; haystack.popFront())
|
||||
{
|
||||
if (unaryFun!pred(haystack.front))
|
||||
{
|
||||
return length - haystack.length;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
typeof(return) result;
|
||||
for (; !haystack.empty; ++result, haystack.popFront())
|
||||
{
|
||||
if (unaryFun!pred(haystack.front)) return result;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
//Verify Examples.
|
||||
unittest
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Same as $(D countUntil). This symbol has been scheduled for
|
||||
* deprecation because it is easily confused with the homonym function
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue