Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions std/algorithm/iteration.d
Original file line number Diff line number Diff line change
Expand Up @@ -5547,7 +5547,10 @@ auto splitter(alias pred = "a == b",
Range,
Separator)(Range r, Separator s)
if (is(typeof(binaryFun!pred(r.front, s)) : bool)
&& ((hasSlicing!Range && hasLength!Range) || isNarrowString!Range))
&& ((hasSlicing!Range && hasLength!Range) || isNarrowString!Range)
&& (is(ElementType!Range : Separator)
|| !(isForwardRange!Separator && (hasLength!Separator
|| isNarrowString!Separator))))
{
import std.algorithm.searching : find;
import std.conv : unsigned;
Expand Down Expand Up @@ -5619,6 +5622,7 @@ if (is(typeof(binaryFun!pred(r.front, s)) : bool)
assert(!empty, "Attempting to fetch the front of an empty splitter.");
static if (keepSeparators)
{
if (_frontLength != _unComputed && !_wasSeparator) return _input[0 .. _frontLength];
if (!_wasSeparator)
{
_frontLength = _separatorLength;
Expand Down Expand Up @@ -6078,6 +6082,20 @@ if (is(typeof(binaryFun!pred(r.front, s)) : bool)
assert("abXcdxef".splitter!((a, b) => a.toLower == b)('x').equal(["ab", "cd", "ef"]));
}

// https://github.com/dlang/phobos/issues/10759
@safe unittest
{
import std.algorithm.iteration : splitter;
import std.algorithm.searching : canFind;
import std.range.primitives;
import std.typecons : Yes;

auto range = "16x13+0-2".splitter!((a, b) => "x+-".canFind(a), Yes.keepSeparators)(0);
assert(range.front == "16");
assert(range.front == "16");
assert(range.front == "16");
}

/// ditto
auto splitter(alias pred = "a == b",
Flag!"keepSeparators" keepSeparators = No.keepSeparators,
Expand All @@ -6091,7 +6109,7 @@ if (is(typeof(binaryFun!pred(r.front, s.front)) : bool)
import std.algorithm.searching : find;
import std.conv : unsigned;

static struct Result
struct Result
{
private:
Range _input;
Expand Down Expand Up @@ -6306,6 +6324,17 @@ if (is(typeof(binaryFun!pred(r.front, s.front)) : bool)
assert(words.equal([ "i", "am", "pointing" ]));
}

// https://github.com/dlang/phobos/issues/10760
@safe unittest
{
import std.algorithm.searching : canFind;
import std.typecons : Yes;
import std.algorithm.comparison : equal;

auto r = "16x16+0-2".splitter!((a, b) => "x+-".canFind(a), Yes.keepSeparators)("x");
assert(r.equal(["16", "x", "16", "+", "0", "-", "2"]));
}

/++
Lazily splits a range `r` whenever a predicate `isTerminator` returns true for an element.

Expand Down
Loading