Skip to content

Commit

Permalink
Merge pull request #4836 from CyberShadow/pull-20161003-223010
Browse files Browse the repository at this point in the history
Fix Issue 16587 - split("", "x") should be ""
  • Loading branch information
andralex authored Oct 4, 2016
2 parents 30aca42 + d6572c2 commit e3f842d
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions std/algorithm/iteration.d
Original file line number Diff line number Diff line change
Expand Up @@ -3573,6 +3573,8 @@ if (is(typeof(binaryFun!pred(r.front, s)) : bool)

_separatorLength = codeLength!(ElementEncodingType!Range)(separator);
}
if (_input.empty)
_frontLength = _atEnd;
}

static if (isInfinite!Range)
Expand Down Expand Up @@ -3704,7 +3706,6 @@ if (is(typeof(binaryFun!pred(r.front, s)) : bool)

debug(std_algorithm) scope(success)
writeln("unittest @", __FILE__, ":", __LINE__, " done.");
assert(equal(splitter("", ' '), [ "" ]));
assert(equal(splitter("hello world", ' '), [ "hello", "", "world" ]));
assert(equal(splitter("žlutoučkýřkůň", 'ř'), [ "žlutoučký", "kůň" ]));
int[] a = [ 1, 2, 0, 0, 3, 0, 4, 5, 0 ];
Expand All @@ -3717,7 +3718,7 @@ if (is(typeof(binaryFun!pred(r.front, s)) : bool)
// }
assert(equal(splitter(a, 0), w));
a = null;
assert(equal(splitter(a, 0), [ (int[]).init ][]));
assert(equal(splitter(a, 0), (int[][]).init));
a = [ 0 ];
assert(equal(splitter(a, 0), [ (int[]).init, (int[]).init ][]));
a = [ 0, 1 ];
Expand Down Expand Up @@ -3821,17 +3822,17 @@ if (is(typeof(binaryFun!pred(r.front, s.front)) : bool)
private:
Range _input;
Separator _separator;
enum size_t _unComputed = size_t.max - 1, _atEnd = size_t.max;
// _frontLength == _atEnd means empty
size_t _frontLength = _unComputed;
// _frontLength == size_t.max means empty
size_t _frontLength = size_t.max;
static if (isBidirectionalRange!Range)
size_t _backLength = _unComputed;
size_t _backLength = size_t.max;

@property auto separatorLength() { return _separator.length; }

void ensureFrontLength()
{
if (_frontLength != _unComputed) return;
if (_frontLength != _frontLength.max) return;
assert(!_input.empty);
// compute front length
_frontLength = (_separator.empty) ? 1 :
_input.length - find!pred(_input, _separator).length;
Expand All @@ -3842,7 +3843,8 @@ if (is(typeof(binaryFun!pred(r.front, s.front)) : bool)
void ensureBackLength()
{
static if (isBidirectionalRange!Range)
if (_backLength != _unComputed) return;
if (_backLength != _backLength.max) return;
assert(!_input.empty);
// compute back length
static if (isBidirectionalRange!Range && isBidirectionalRange!Separator)
{
Expand Down Expand Up @@ -3874,7 +3876,7 @@ if (is(typeof(binaryFun!pred(r.front, s.front)) : bool)
{
@property bool empty()
{
return _frontLength == _atEnd;
return _frontLength == size_t.max && _input.empty;
}
}

Expand All @@ -3886,9 +3888,9 @@ if (is(typeof(binaryFun!pred(r.front, s.front)) : bool)
{
// done, there's no separator in sight
_input = _input[_frontLength .. _frontLength];
_frontLength = _atEnd;
_frontLength = _frontLength.max;
static if (isBidirectionalRange!Range)
_backLength = _atEnd;
_backLength = _backLength.max;
return;
}
if (_frontLength + separatorLength == _input.length)
Expand All @@ -3905,7 +3907,7 @@ if (is(typeof(binaryFun!pred(r.front, s.front)) : bool)
// reading the next item
_input = _input[_frontLength + separatorLength .. _input.length];
// mark _frontLength as uninitialized
_frontLength = _unComputed;
_frontLength = _frontLength.max;
}

static if (isForwardRange!Range)
Expand Down Expand Up @@ -3955,8 +3957,6 @@ if (is(typeof(binaryFun!pred(r.front, s.front)) : bool)

debug(std_algorithm) scope(success)
writeln("unittest @", __FILE__, ":", __LINE__, " done.");
assert(equal(splitter("", " "), [ "" ]));

auto s = ",abc, de, fg,hi,";
auto sp0 = splitter(s, ',');
// //foreach (e; sp0) writeln("[", e, "]");
Expand Down Expand Up @@ -4131,7 +4131,10 @@ private struct SplitterResult(alias isTerminator, Range)
static if (!fullSlicing)
_next = _input.save;

findTerminator();
if (!_input.empty)
findTerminator();
else
_end = size_t.max;
}

static if (isInfinite!Range)
Expand Down Expand Up @@ -4240,7 +4243,7 @@ private struct SplitterResult(alias isTerminator, Range)
["Mary", "", "has", "a", "little", "lamb.", "", "", ""]);
compare("Mary has a little lamb.",
["Mary", "", "has", "a", "little", "lamb."]);
compare("", [""]);
compare("", (string[]).init);
compare(" ", ["", ""]);

static assert(isForwardRange!(typeof(splitter!"a == ' '"("ABC"))));
Expand Down Expand Up @@ -4270,7 +4273,7 @@ private struct SplitterResult(alias isTerminator, Range)
int[][] result;
}
Entry[] entries = [
Entry(0, 0, [[]]),
Entry(0, 0, []),
Entry(0, 1, [[0]]),
Entry(1, 2, [[], []]),
Entry(2, 7, [[2], [4], [6]]),
Expand Down

0 comments on commit e3f842d

Please sign in to comment.