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.findSplitAfter

These functions find the first occurrence of needle in haystack and then split haystack as follows.

auto findSplitAfter(alias pred, R1, R2) (
  R1 haystack,
  R2 needle
)
if (isForwardRange!R1 && isForwardRange!R2);

findSplit returns a tuple result containing three ranges. result[0] is the portion of haystack before needle, result[1] is the portion of haystack that matches needle, and result[2] is the portion of haystack after the match. If needle was not found, result[0] comprehends haystack entirely and result[1] and result[2] are empty.

findSplitBefore returns a tuple result containing two ranges. result[0] is the portion of haystack before needle, and result[1] is the balance of haystack starting with the match. If needle was not found, result[0] comprehends haystack entirely and result[1] is empty.

findSplitAfter returns a tuple result containing two ranges. result[0] is the portion of haystack up to and including the match, and result[1] is the balance of haystack starting after the match. If needle was not found, result[0] is empty and result[1] is haystack.

In all cases, the concatenation of the returned ranges spans the entire haystack.

If haystack is a random-access range, all three components of the tuple have the same type as haystack. Otherwise, haystack must be a forward range and the type of result[0] and result[1] is the same as std.range.takeExactly.

Parameters

NameDescription
pred Predicate to use for comparing needle against haystack.
haystack The range to search.
needle What to look for.

Returns

A sub-type of Tuple!() of the split portions of haystack (see above for details). This sub-type of Tuple!() has opCast defined for bool. This opCast returns true when the separating needle was found and false otherwise.

See Also

find

Example

Returning a subtype of Tuple enables the following convenient idiom:

// findSplit returns a triplet
if (auto split = "dlang-rocks".findSplit("-"))
{
    writeln(split[0]); // "dlang"
    writeln(split[1]); // "-"
    writeln(split[2]); // "rocks"
}
else assert(0);

// works with const aswell
if (const split = "dlang-rocks".findSplit("-"))
{
    writeln(split[0]); // "dlang"
    writeln(split[1]); // "-"
    writeln(split[2]); // "rocks"
}
else assert(0);

Example

import std.range.primitives : empty;

auto a = "Carl Sagan Memorial Station";
auto r = findSplit(a, "Velikovsky");
import std.typecons : isTuple;
static assert(isTuple!(typeof(r.asTuple)));
static assert(isTuple!(typeof(r)));
assert(!r);
writeln(r[0]); // a
assert(r[1].empty);
assert(r[2].empty);
r = findSplit(a, " ");
writeln(r[0]); // "Carl"
writeln(r[1]); // " "
writeln(r[2]); // "Sagan Memorial Station"
if (const r1 = findSplitBefore(a, "Sagan"))
{
    assert(r1);
    writeln(r1[0]); // "Carl "
    writeln(r1[1]); // "Sagan Memorial Station"
}
if (const r2 = findSplitAfter(a, "Sagan"))
{
    assert(r2);
    writeln(r2[0]); // "Carl Sagan"
    writeln(r2[1]); // " Memorial Station"
}

Example

Use std.range.only to find single elements:

import std.range : only;
writeln([1, 2, 3, 4].findSplitBefore(only(3))[0]); // [1, 2]

Authors

Andrei Alexandrescu

License

Boost License 1.0.