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.skip_over - multiple declarations

Function skipOver

Skip over the initial portion of the first given range that matches the second range, or do nothing if there is no match.

Prototypes

bool skipOver(R1, R2)(
  R1 r1,
  R2 r2
)
if (isForwardRange!R1 && isInputRange!R2 && is(typeof(r1.front == r2.front)));

bool skipOver(alias pred, R1, R2)(
  R1 r1,
  R2 r2
)
if (is(typeof(binaryFun!pred(r1.front, r2.front))) && isForwardRange!R1 && isInputRange!R2);

Parameters

NameDescription
pred The predicate that determines whether elements from each respective range match. Defaults to equality "a == b".
r1 The forward range to move forward.
r2 The input range representing the initial segment of r1 to skip over.

Returns

true if the initial segment of r1 matches r2, and r1 has been advanced to the point past this segment; otherwise false, and r1 is left in its original position.

Example

import std.algorithm.comparison : equal;

auto s1 = "Hello world";
assert(!skipOver(s1, "Ha"));
assert(s1 == "Hello world");
assert(skipOver(s1, "Hell") && s1 == "o world");

string[]  r1 = ["abc", "def", "hij"];
dstring[] r2 = ["abc"d];
assert(!skipOver!((a, b) => a.equal(b))(r1, ["def"d]));
assert(r1 == ["abc", "def", "hij"]);
assert(skipOver!((a, b) => a.equal(b))(r1, r2));
assert(r1 == ["def", "hij"]);

Function skipOver

Skip over the first element of the given range if it matches the given element, otherwise do nothing.

Prototypes

bool skipOver(R, E)(
  R r,
  E e
)
if (isInputRange!R && is(typeof(r.front == e) : bool));

bool skipOver(alias pred, R, E)(
  R r,
  E e
)
if (is(typeof(binaryFun!pred(r.front, e))) && isInputRange!R);

Parameters

NameDescription
pred The predicate that determines whether an element from the range matches the given element.
r The input range to skip over.
e The element to match.

Returns

true if the first element matches the given element according to the given predicate, and the range has been advanced by one element; otherwise false, and the range is left untouched.

Example

import std.algorithm.comparison : equal;

auto s1 = "Hello world";
assert(!skipOver(s1, 'a'));
assert(s1 == "Hello world");
assert(skipOver(s1, 'H') && s1 == "ello world");

string[] r = ["abc", "def", "hij"];
dstring e = "abc"d;
assert(!skipOver!((a, b) => a.equal(b))(r, "def"d));
assert(r == ["abc", "def", "hij"]);
assert(skipOver!((a, b) => a.equal(b))(r, e));
assert(r == ["def", "hij"]);

auto s2 = "";
assert(!s2.skipOver('a'));

Authors

Andrei Alexandrescu

License

Boost License 1.0.

Comments