Skip to content

Commit

Permalink
Merge pull request #5543 from RazvanN7/Issue_17525
Browse files Browse the repository at this point in the history
Fix Issue 17525 - std.algorithm.searching.skipOver should have a single argument with pred version
merged-on-behalf-of: Sebastian Wilzbach <sebi.wilzbach@gmail.com>
  • Loading branch information
dlang-bot committed Jul 7, 2017
2 parents 35e0f84 + 1d1b42f commit 37d15a8
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions std/algorithm/searching.d
Expand Up @@ -3907,7 +3907,8 @@ if (isInputRange!Range && !isInfinite!Range &&

/**
Skip over the initial portion of the first given range that matches the second
range, or do nothing if there is no match.
range, or if no second range is given skip over the elements that fullfil pred.
Do nothing if there is no match.
Params:
pred = The predicate that determines whether elements from each respective
Expand All @@ -3918,9 +3919,9 @@ Params:
representing the initial segment of $(D r1) to skip over.
Returns:
true if the initial segment of $(D r1) matches $(D r2), and $(D r1) has been
advanced to the point past this segment; otherwise false, and $(D r1) is left
in its original position.
true if the initial segment of $(D r1) matches $(D r2) or $(D pred) evaluates to true,
and $(D r1) has been advanced to the point past this segment; otherwise false, and
$(D r1) is left in its original position.
*/
bool skipOver(R1, R2)(ref R1 r1, R2 r2)
if (isForwardRange!R1 && isInputRange!R2
Expand Down Expand Up @@ -3966,6 +3967,20 @@ if (is(typeof(binaryFun!pred(r1.front, r2.front))) &&
return r2.empty;
}

/// Ditto
bool skipOver(alias pred, R)(ref R r1)
if (isForwardRange!R &&
ifTestable!(typeof(r1.front), unaryFun!pred))
{
if (r1.empty || !unaryFun!pred(r1.front))
return false;

do
r1.popFront();
while (!r1.empty && unaryFun!pred(r1.front));
return true;
}

///
@safe unittest
{
Expand All @@ -3982,6 +3997,16 @@ if (is(typeof(binaryFun!pred(r1.front, r2.front))) &&
assert(r1 == ["abc", "def", "hij"]);
assert(skipOver!((a, b) => a.equal(b))(r1, r2));
assert(r1 == ["def", "hij"]);

import std.ascii : isWhite;
import std.range.primitives : empty;

auto s2 = "\t\tvalue";
auto s3 = "";
auto s4 = "\t\t\t";
assert(s2.skipOver!isWhite && s2 == "value");
assert(!s3.skipOver!isWhite);
assert(s4.skipOver!isWhite && s3.empty);
}

/**
Expand Down

0 comments on commit 37d15a8

Please sign in to comment.