View source code Display the source code in std/algorithm/iteration.d from which this page was generated on github. 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. Page wiki View or edit the community-maintained wiki page associated with this page.

std.algorithm.iteration.splitter - multiple declarations

Function splitter

Lazily splits a range using an element as a separator. This can be used with any narrow string type or sliceable range type, but is most popular with string types.

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.

If the empty range is given, the result is a range with one empty element. If a range with one separator is given, the result is a range with two empty elements.

If splitting a string on whitespace and token compression is desired, consider using splitter without specifying a separator (see fourth overload below).

Prototype

auto splitter(alias pred, Range, Separator)(
  Range r,
  Separator s
)
if (is(typeof(binaryFun!pred(r.front, s)) : bool) && (hasSlicing!Range && hasLength!Range || isNarrowString!Range));

Parameters

NameDescription
pred The predicate for comparing each element with the separator, defaulting to "a == b".
r The input range to be split. Must support slicing and .length.
s The element to be treated as the separator between range segments to be split.

Constraints

The predicate pred needs to accept an element of r and the separator s.

Returns

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.

See Also

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

Example

import std.algorithm.comparison : equal;

assert(equal(splitter("hello  world", ' '), [ "hello", "", "world" ]));
int[] a = [ 1, 2, 0, 0, 3, 0, 4, 5, 0 ];
int[][] w = [ [1, 2], [], [3], [4, 5], [] ];
assert(equal(splitter(a, 0), w));
a = [ 0 ];
assert(equal(splitter(a, 0), [ (int[]).init, (int[]).init ]));
a = [ 0, 1 ];
assert(equal(splitter(a, 0), [ [], [1] ]));
w = [ [0], [1], [2] ];
assert(equal(splitter!"a.front == b"(w, 1), [ [[0]], [[2]] ]));

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

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.

Prototype

auto splitter(alias pred, 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));

Parameters

NameDescription
pred The predicate for comparing each element with the separator, defaulting to "a == b".
r The input range to be split.
s 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.

Returns

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.

See Also

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

Example

import std.algorithm.comparison : equal;

assert(equal(splitter("hello  world", "  "), [ "hello", "world" ]));
int[] a = [ 1, 2, 0, 0, 3, 0, 4, 5, 0 ];
int[][] w = [ [1, 2], [3, 0, 4, 5, 0] ];
assert(equal(splitter(a, [0, 0]), w));
a = [ 0, 0 ];
assert(equal(splitter(a, [0, 0]), [ (int[]).init, (int[]).init ]));
a = [ 0, 0, 1 ];
assert(equal(splitter(a, [0, 0]), [ [], [1] ]));

Function splitter

Similar to the previous overload of splitter, except this one does not use a separator. Instead, the predicate is an unary function on the input range's element type.

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.

Prototype

auto splitter(alias isTerminator, Range)(
  Range input
)
if (isForwardRange!Range && is(typeof(unaryFun!isTerminator(input.front))));

Parameters

NameDescription
isTerminator The predicate for deciding where to split the range.
input The input range to be split.

Constraints

The predicate isTerminator needs to accept an element of input.

Returns

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

See Also

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

Example

import std.algorithm.comparison : equal;

assert(equal(splitter!"a == ' '"("hello  world"), [ "hello", "", "world" ]));
int[] a = [ 1, 2, 0, 0, 3, 0, 4, 5, 0 ];
int[][] w = [ [1, 2], [], [3], [4, 5], [] ];
assert(equal(splitter!"a == 0"(a), w));
a = [ 0 ];
assert(equal(splitter!"a == 0"(a), [ (int[]).init, (int[]).init ]));
a = [ 0, 1 ];
assert(equal(splitter!"a == 0"(a), [ [], [1] ]));
w = [ [0], [1], [2] ];
assert(equal(splitter!"a.front == 1"(w), [ [[0]], [[2]] ]));

Function splitter

Lazily splits the string s into words, using whitespace as the delimiter.

This function is string specific and, contrary to splitter!(std.uni.isWhite), runs of whitespace will be merged together (no empty tokens will be produced).

Prototype

auto splitter(C)(
  C[] s
)
if (isSomeChar!C);

Parameters

NameDescription
s The string to be split.

Returns

An input range of slices of the original string split by whitespace.

Example

import std.algorithm.comparison : equal;
auto a = " a     bcd   ef gh ";
assert(equal(splitter(a), ["a", "bcd", "ef", "gh"][]));

Authors

Andrei Alexandrescu

License

Boost License 1.0.

Comments