findSkip

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

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.

  1. bool findSkip(R1 haystack, R2 needle)
    bool
    findSkip
    (
    alias pred = "a == b"
    R1
    R2
    )
    (
    ref R1 haystack
    ,)
    if (
    is(typeof(binaryFun!pred(haystack.front, needle.front)))
    )
  2. size_t findSkip(R1 haystack)

Parameters

haystack R1

The forward range to search in.

needle R2

The forward range to search for.

pred

Custom predicate for comparison of haystack and needle

Return Value

Type: bool

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.

Examples

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);
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);

See Also

Meta

Suggestion Box / Bug Report