mirror of
https://github.com/dlang/phobos.git
synced 2025-05-02 08:00:48 +03:00
Provide pred-only overload to std.algorithm.searching.findSkip
This commit is contained in:
parent
8089602180
commit
42236b711d
1 changed files with 48 additions and 0 deletions
|
@ -2712,6 +2712,7 @@ if (isInputRange!InputRange && isForwardRange!ForwardRange)
|
|||
* needle = The
|
||||
* $(REF_ALTTEXT forward range, isForwardRange, std,range,primitives) to search
|
||||
* for.
|
||||
* pred = Custom predicate for comparison of haystack and needle
|
||||
*
|
||||
* Returns: $(D true) if the needle was found, in which case $(D haystack) is
|
||||
* positioned after the end of the first occurrence of $(D needle); otherwise
|
||||
|
@ -2746,6 +2747,53 @@ if (isForwardRange!R1 && isForwardRange!R2
|
|||
assert(findSkip(s, "def") && s.empty);
|
||||
}
|
||||
|
||||
/**
|
||||
* Advances the `haystack` as long as `pred` evaluates to `true`.
|
||||
* The `haystack` is positioned before position of first falsely evaluation of `pred`.
|
||||
*
|
||||
* Params:
|
||||
* haystack = The
|
||||
* $(REF_ALTTEXT forward range, isForwardRange, std,range,primitives) to search
|
||||
* in.
|
||||
* pred = Custom predicate for comparison of haystack and needle
|
||||
*
|
||||
* Returns: The number of times `pred` was truthfully evaluated.
|
||||
*/
|
||||
size_t findSkip(alias pred, R1)(ref R1 haystack)
|
||||
if (isForwardRange!R1 && ifTestable!(typeof(haystack.front), unaryFun!pred))
|
||||
{
|
||||
size_t result;
|
||||
while (!haystack.empty && unaryFun!pred(haystack.front))
|
||||
{
|
||||
result++;
|
||||
haystack.popFront;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
///
|
||||
@safe unittest
|
||||
{
|
||||
import std.ascii : isWhite;
|
||||
string s = " abc";
|
||||
assert(findSkip!isWhite(s) && s == "abc");
|
||||
assert(!findSkip!isWhite(s) && s == "abc");
|
||||
|
||||
s = " ";
|
||||
assert(findSkip!isWhite(s) == 2);
|
||||
import std.stdio;
|
||||
s = " ";
|
||||
findSkip!isWhite(s).writeln;
|
||||
}
|
||||
|
||||
@safe unittest
|
||||
{
|
||||
import std.ascii : isWhite;
|
||||
|
||||
auto s = " ";
|
||||
assert(findSkip!isWhite(s) == 2);
|
||||
}
|
||||
|
||||
/**
|
||||
These functions find the first occurrence of `needle` in `haystack` and then
|
||||
split `haystack` as follows.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue