Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions std/algorithm/searching.d
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 so as pred evaluates to false for haystack.front.
*
* 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(haystack.front)` returned true.
*/
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.
Expand Down