haystack advanced such that the front element satisfies pred.
If no such element exists, returns an empty haystack.
InputRange find(alias pred, InputRange)(InputRange haystack) if (isInputRange!InputRange)Finds an element e of an input range where pred(e) is true.
find behaves similarly to dropWhile in other languages.haystack,
call find!pred(retro(haystack)). See retro.
Complexity: find performs walkLength(haystack) evaluations of pred.
pred | The predicate to match an element. |
haystack | The input range searched in. |
haystack advanced such that the front element satisfies pred.
If no such element exists, returns an empty haystack.
InputRange find(alias pred = "a == b", InputRange, Element)(InputRange haystack, scope Element needle) if (isInputRange!InputRange &&
is (typeof(binaryFun!pred(haystack.front, needle)) : bool) &&
!is (typeof(binaryFun!pred(haystack.front, needle.front)) : bool))Finds an individual element in an input range. Elements of haystack are compared with needle by using predicate pred with pred(haystack.front, needle). The predicate is passed to binaryFun, and can either accept a string, or any callable that can be executed via pred(element, element).
If haystack is a forward range, needle can be a forward range too. In this case startsWith!pred(haystack, needle) is evaluated on each evaluation.
"a != b".Complexity: find performs walkLength(haystack) evaluations of pred. There are specializations that improve performance by taking advantage of bidirectional or random access ranges (where possible).
pred | The predicate for comparing each element with the needle, defaulting to equality "a == b". |
haystack | The input range searched in. |
needle | The element searched for. |
haystack advanced such that the front element is the one searched for;
that is, until binaryFun!pred(haystack.front, needle) is true. If no such position exists, returns an empty haystack.
R1 find(alias pred = "a == b", R1, R2)(R1 haystack, scope R2 needle) if (isForwardRange!R1 && isForwardRange!R2
&& is(typeof(binaryFun!pred(haystack.front, needle.front)) : bool))ditto
Tuple!(Range, size_t) find(alias pred = "a == b", Range, Needles...)(Range haystack, Needles needles) if (Needles.length > 1 && is(typeof(startsWith!pred(haystack, needles))))Finds two or more needles into a haystack. The predicate pred is used throughout to compare elements. By default, elements are compared for equality.
pred | The predicate to use for comparing elements. |
haystack | The target of the search. Must be an input range. If any of needles is a range with elements comparable to elements in haystack, then haystack must be a forward range such that the search can backtrack. |
needles | One or more items to search for. Each of needles must be either comparable to one element in haystack, or be itself a forward range with elements comparable with elements in haystack. |
haystack positioned to match one of the
needles and also the 1-based index of the matching element in needles (0 if none of needles matched, 1 if needles[0] matched, 2 if needles[1] matched...). The first needle to be found will be the one that matches. If multiple needles are found at the same spot in the range, then the shortest one is the one which matches (if multiple needles of the same length are found at the same spot (e.g "a" and 'a'), then the left-most of them in the argument list matches).
The relationship between haystack and needles simply means that one can e.g. search for individual ints or arrays of ints in an array of ints. In addition, if elements are individually comparable, searches of heterogeneous types are allowed as well: a double[] can be searched for an int or a short[], and conversely a long can be searched for a float or a double[]. This makes for efficient searches without the need to coerce one side of the comparison into the other's side type.
The complexity of the search is haystack.length * max(needles.length). (For needles that are individual items, length is considered to be 1.) The strategy used in searching several subranges at once maximizes cache usage by moving in haystack as few times as possible.
RandomAccessRange find(RandomAccessRange, alias pred, InputRange)(
RandomAccessRange haystack, scope BoyerMooreFinder!(pred, InputRange) needle)Finds needle in haystack efficiently using the
haystack | A random-access range with length and slicing. |
needle | A BoyerMooreFinder. |
haystack advanced such that needle is a prefix of it (if no
such position exists, returns haystack advanced to termination).