Merge pull request #6132 from JackStouffer/startswith-ascii

Remove auto-decoding from startsWith when Needles are all narrow chars
merged-on-behalf-of: Jack Stouffer <jack@jackstouffer.com>
This commit is contained in:
The Dlang Bot 2018-02-12 23:13:00 +01:00 committed by GitHub
commit 15da6f6aca
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -4099,12 +4099,29 @@ list matches).
In the case when no needle parameters are given, return `true` iff front of In the case when no needle parameters are given, return `true` iff front of
`doesThisStart` fulfils predicate `pred`. `doesThisStart` fulfils predicate `pred`.
*/ */
uint startsWith(alias pred = "a == b", Range, Needles...)(Range doesThisStart, Needles withOneOfThese) uint startsWith(alias pred = (a, b) => a == b, Range, Needles...)(Range doesThisStart, Needles withOneOfThese)
if (isInputRange!Range && Needles.length > 1 && if (isInputRange!Range && Needles.length > 1 &&
is(typeof(.startsWith!pred(doesThisStart, withOneOfThese[0])) : bool ) && is(typeof(.startsWith!pred(doesThisStart, withOneOfThese[0])) : bool ) &&
is(typeof(.startsWith!pred(doesThisStart, withOneOfThese[1 .. $])) : uint)) is(typeof(.startsWith!pred(doesThisStart, withOneOfThese[1 .. $])) : uint))
{ {
alias haystack = doesThisStart; import std.meta : allSatisfy;
template checkType(T)
{
enum checkType = is(Unqual!(ElementEncodingType!Range) == Unqual!T);
}
// auto-decoding special case
static if (__traits(isSame, binaryFun!pred, (a, b) => a == b) &&
isNarrowString!Range && allSatisfy!(checkType, Needles))
{
import std.utf : byCodeUnit;
auto haystack = doesThisStart.byCodeUnit;
}
else
{
alias haystack = doesThisStart;
}
alias needles = withOneOfThese; alias needles = withOneOfThese;
// Make one pass looking for empty ranges in needles // Make one pass looking for empty ranges in needles