View source code
Display the source code in std/algorithm/searching.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
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.

Function std.algorithm.searching.findSkip

Finds needle in haystack and positions haystack right after the first occurrence of needle.

bool findSkip(alias pred, R1, R2) (
  ref R1 haystack,
  R2 needle
)
if (isForwardRange!R1 && isForwardRange!R2 && is(typeof(binaryFun!pred(haystack.front, needle.front))));

size_t findSkip(alias pred, R1) (
  ref R1 haystack
)
if (isForwardRange!R1 && ifTestable!(typeof(haystack.front), unaryFun!pred));

If no needle is provided, the haystack is advanced as long as pred evaluates to true. Similarly, the haystack is positioned so as pred evaluates to false for haystack.front.

Parameters

NameDescription
haystack The forward range to search in.
needle The forward range to search for.
pred Custom predicate for comparison of haystack and needle

Returns

true if the needle was found, in which case haystack is positioned after the end of the first occurrence of needle; otherwise false, leaving haystack untouched. If no needle is provided, it returns the number of times pred(haystack.front) returned true.

See Also

find

Example

import std.range.primitives : empty;
// Needle is found; s is replaced by the substring following the first
// occurrence of the needle.
string s = "abcdef";
assert(findSkip(s, "cd") && s == "ef");

// Needle is not found; s is left untouched.
s = "abcdef";
assert(!findSkip(s, "cxd") && s == "abcdef");

// If the needle occurs at the end of the range, the range is left empty.
s = "abcdef";
assert(findSkip(s, "def") && s.empty);

Example

import std.ascii : isWhite;
string s = "    abc";
assert(findSkip!isWhite(s) && s == "abc");
assert(!findSkip!isWhite(s) && s == "abc");

s = "  ";
writeln(findSkip!isWhite(s)); // 2

Authors

Andrei Alexandrescu

License

Boost License 1.0.