splitter

Similar to the previous overload of splitter, except this one uses another range as a separator. This can be used with any narrow string type or sliceable range type, but is most popular with string types. The predicate is passed to std.functional.binaryFun, and can either accept a string, or any callable that can be executed via pred(r.front, s.front).

Two adjacent separators are considered to surround an empty element in the split range. Use filter!(a => !a.empty) on the result to compress empty elements.

  1. auto splitter(Range r, Separator s)
  2. auto splitter(Range r, Separator s)
    splitter
    (
    alias pred = "a == b"
    Range
    Separator
    )
    (
    Range r
    ,
    Separator s
    )
    if (
    is(typeof(binaryFun!pred(r.front, s.front)) : bool) &&
    (
    hasSlicing!Range ||
    isNarrowString!Range
    )
    &&
    isForwardRange!Separator
    &&
    (
    hasLength!Separator ||
    isNarrowString!Separator
    )
    )
  3. auto splitter(Range input)
  4. auto splitter(C[] s)

Parameters

pred

The predicate for comparing each element with the separator, defaulting to "a == b".

r
Type: Range

The input range to be split.

s
Type: Separator

The forward range to be treated as the separator between segments of r to be split.

Constraints: The predicate pred needs to accept an element of r and an element of s.

Return Value

Type: auto

An input range of the subranges of elements between separators. If r is a forward range or bidirectional range, the returned range will be likewise.

Examples

1 import std.algorithm.comparison : equal;
2 
3 assert(equal(splitter("hello  world", "  "), [ "hello", "world" ]));
4 int[] a = [ 1, 2, 0, 0, 3, 0, 4, 5, 0 ];
5 int[][] w = [ [1, 2], [3, 0, 4, 5, 0] ];
6 assert(equal(splitter(a, [0, 0]), w));
7 a = [ 0, 0 ];
8 assert(equal(splitter(a, [0, 0]), [ (int[]).init, (int[]).init ]));
9 a = [ 0, 0, 1 ];
10 assert(equal(splitter(a, [0, 0]), [ [], [1] ]));

See Also

std.regex._splitter for a version that splits using a regular expression defined separator.

Meta