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);
}
if (_input.empty)
_frontLength = _atEnd;
}
static if (isInfinite!Range)
@ -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 ];
@ -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 ];
@ -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;
@ -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)
{
@ -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;
}
}
@ -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)
@ -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)
@ -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, "]");
@ -4131,7 +4131,10 @@ private struct SplitterResult(alias isTerminator, Range)
static if (!fullSlicing)
_next = _input.save;
if (!_input.empty)
findTerminator();
else
_end = size_t.max;
}
static if (isInfinite!Range)
@ -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"))));
@ -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]]),