Fix Issue 16587 - split("", "x") should be ""

This reverts commit b438bf5a06.
This commit is contained in:
Vladimir Panteleev 2016-10-03 22:30:00 +00:00
parent 30aca423cd
commit d6572c2a44

View file

@ -3573,6 +3573,8 @@ if (is(typeof(binaryFun!pred(r.front, s)) : bool)
_separatorLength = codeLength!(ElementEncodingType!Range)(separator); _separatorLength = codeLength!(ElementEncodingType!Range)(separator);
} }
if (_input.empty)
_frontLength = _atEnd;
} }
static if (isInfinite!Range) static if (isInfinite!Range)
@ -3704,7 +3706,6 @@ if (is(typeof(binaryFun!pred(r.front, s)) : bool)
debug(std_algorithm) scope(success) debug(std_algorithm) scope(success)
writeln("unittest @", __FILE__, ":", __LINE__, " done."); writeln("unittest @", __FILE__, ":", __LINE__, " done.");
assert(equal(splitter("", ' '), [ "" ]));
assert(equal(splitter("hello world", ' '), [ "hello", "", "world" ])); assert(equal(splitter("hello world", ' '), [ "hello", "", "world" ]));
assert(equal(splitter("žlutoučkýřkůň", 'ř'), [ "žlutoučký", "kůň" ])); assert(equal(splitter("žlutoučkýřkůň", 'ř'), [ "žlutoučký", "kůň" ]));
int[] a = [ 1, 2, 0, 0, 3, 0, 4, 5, 0 ]; int[] a = [ 1, 2, 0, 0, 3, 0, 4, 5, 0 ];
@ -3717,7 +3718,7 @@ if (is(typeof(binaryFun!pred(r.front, s)) : bool)
// } // }
assert(equal(splitter(a, 0), w)); assert(equal(splitter(a, 0), w));
a = null; a = null;
assert(equal(splitter(a, 0), [ (int[]).init ][])); assert(equal(splitter(a, 0), (int[][]).init));
a = [ 0 ]; a = [ 0 ];
assert(equal(splitter(a, 0), [ (int[]).init, (int[]).init ][])); assert(equal(splitter(a, 0), [ (int[]).init, (int[]).init ][]));
a = [ 0, 1 ]; a = [ 0, 1 ];
@ -3821,17 +3822,17 @@ if (is(typeof(binaryFun!pred(r.front, s.front)) : bool)
private: private:
Range _input; Range _input;
Separator _separator; Separator _separator;
enum size_t _unComputed = size_t.max - 1, _atEnd = size_t.max; // _frontLength == size_t.max means empty
// _frontLength == _atEnd means empty size_t _frontLength = size_t.max;
size_t _frontLength = _unComputed;
static if (isBidirectionalRange!Range) static if (isBidirectionalRange!Range)
size_t _backLength = _unComputed; size_t _backLength = size_t.max;
@property auto separatorLength() { return _separator.length; } @property auto separatorLength() { return _separator.length; }
void ensureFrontLength() void ensureFrontLength()
{ {
if (_frontLength != _unComputed) return; if (_frontLength != _frontLength.max) return;
assert(!_input.empty);
// compute front length // compute front length
_frontLength = (_separator.empty) ? 1 : _frontLength = (_separator.empty) ? 1 :
_input.length - find!pred(_input, _separator).length; _input.length - find!pred(_input, _separator).length;
@ -3842,7 +3843,8 @@ if (is(typeof(binaryFun!pred(r.front, s.front)) : bool)
void ensureBackLength() void ensureBackLength()
{ {
static if (isBidirectionalRange!Range) static if (isBidirectionalRange!Range)
if (_backLength != _unComputed) return; if (_backLength != _backLength.max) return;
assert(!_input.empty);
// compute back length // compute back length
static if (isBidirectionalRange!Range && isBidirectionalRange!Separator) static if (isBidirectionalRange!Range && isBidirectionalRange!Separator)
{ {
@ -3874,7 +3876,7 @@ if (is(typeof(binaryFun!pred(r.front, s.front)) : bool)
{ {
@property bool empty() @property bool empty()
{ {
return _frontLength == _atEnd; return _frontLength == size_t.max && _input.empty;
} }
} }
@ -3886,9 +3888,9 @@ if (is(typeof(binaryFun!pred(r.front, s.front)) : bool)
{ {
// done, there's no separator in sight // done, there's no separator in sight
_input = _input[_frontLength .. _frontLength]; _input = _input[_frontLength .. _frontLength];
_frontLength = _atEnd; _frontLength = _frontLength.max;
static if (isBidirectionalRange!Range) static if (isBidirectionalRange!Range)
_backLength = _atEnd; _backLength = _backLength.max;
return; return;
} }
if (_frontLength + separatorLength == _input.length) if (_frontLength + separatorLength == _input.length)
@ -3905,7 +3907,7 @@ if (is(typeof(binaryFun!pred(r.front, s.front)) : bool)
// reading the next item // reading the next item
_input = _input[_frontLength + separatorLength .. _input.length]; _input = _input[_frontLength + separatorLength .. _input.length];
// mark _frontLength as uninitialized // mark _frontLength as uninitialized
_frontLength = _unComputed; _frontLength = _frontLength.max;
} }
static if (isForwardRange!Range) static if (isForwardRange!Range)
@ -3955,8 +3957,6 @@ if (is(typeof(binaryFun!pred(r.front, s.front)) : bool)
debug(std_algorithm) scope(success) debug(std_algorithm) scope(success)
writeln("unittest @", __FILE__, ":", __LINE__, " done."); writeln("unittest @", __FILE__, ":", __LINE__, " done.");
assert(equal(splitter("", " "), [ "" ]));
auto s = ",abc, de, fg,hi,"; auto s = ",abc, de, fg,hi,";
auto sp0 = splitter(s, ','); auto sp0 = splitter(s, ',');
// //foreach (e; sp0) writeln("[", e, "]"); // //foreach (e; sp0) writeln("[", e, "]");
@ -4131,7 +4131,10 @@ private struct SplitterResult(alias isTerminator, Range)
static if (!fullSlicing) static if (!fullSlicing)
_next = _input.save; _next = _input.save;
findTerminator(); if (!_input.empty)
findTerminator();
else
_end = size_t.max;
} }
static if (isInfinite!Range) static if (isInfinite!Range)
@ -4240,7 +4243,7 @@ private struct SplitterResult(alias isTerminator, Range)
["Mary", "", "has", "a", "little", "lamb.", "", "", ""]); ["Mary", "", "has", "a", "little", "lamb.", "", "", ""]);
compare("Mary has a little lamb.", compare("Mary has a little lamb.",
["Mary", "", "has", "a", "little", "lamb."]); ["Mary", "", "has", "a", "little", "lamb."]);
compare("", [""]); compare("", (string[]).init);
compare(" ", ["", ""]); compare(" ", ["", ""]);
static assert(isForwardRange!(typeof(splitter!"a == ' '"("ABC")))); static assert(isForwardRange!(typeof(splitter!"a == ' '"("ABC"))));
@ -4270,7 +4273,7 @@ private struct SplitterResult(alias isTerminator, Range)
int[][] result; int[][] result;
} }
Entry[] entries = [ Entry[] entries = [
Entry(0, 0, [[]]), Entry(0, 0, []),
Entry(0, 1, [[0]]), Entry(0, 1, [[0]]),
Entry(1, 2, [[], []]), Entry(1, 2, [[], []]),
Entry(2, 7, [[2], [4], [6]]), Entry(2, 7, [[2], [4], [6]]),