Merge pull request #6751 from dukc/emptySubstitute

Corrected initialization of empty substitute
This commit is contained in:
Nicholas Wilson 2018-11-15 17:38:59 +08:00 committed by GitHub
commit 4804e7d9c7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -5518,10 +5518,13 @@ if (isInputRange!R && Substs.length >= 2 && !is(CommonType!(Substs) == void))
this(R haystack, Ins needles)
{
hasHit = !haystack.empty;
this.rest = haystack.drop(0);
this.needles = needles;
popFront;
if (!haystack.empty)
{
hasHit = true;
popFront;
}
static if (hasNested!(typeof(skip)))
skip = rest.take(0);
}
@ -5936,6 +5939,25 @@ if (isInputRange!R && Substs.length >= 2 && !is(CommonType!(Substs) == void))
assert([1, 2, 3, 4].substitute([3, 4], [7, 8]).equal([1, 2, 7, 8]));
}
// tests recognizing empty base ranges
nothrow pure @safe unittest
{
import std.utf : byCodeUnit;
import std.algorithm.comparison : equal;
assert("".byCodeUnit.substitute('4', 'A').empty);
assert("".byCodeUnit.substitute('0', 'O', '5', 'S', '1', 'l').empty);
assert("".byCodeUnit.substitute("PKM".byCodeUnit, "PoKeMon".byCodeUnit).empty);
assert("".byCodeUnit.substitute
( "ding".byCodeUnit,
"dong".byCodeUnit,
"click".byCodeUnit,
"clack".byCodeUnit,
"ping".byCodeUnit,
"latency".byCodeUnit
).empty);
}
// sum
/**
Sums elements of `r`, which must be a finite