std.string: Optimize implementation for same-type case-sensitive search

Add a special case for case-sensitive search when the character size
of substrings matches. We do not need to decode in this case, so
perform a basic array search on the strings' representations.
This commit is contained in:
Vladimir Panteleev 2018-11-23 21:42:38 +00:00
parent 606465af0b
commit f12e43e3b3
No known key found for this signature in database
GPG key ID: 5004F0FAD051576D

View file

@ -766,20 +766,28 @@ private template _indexOfStr(CaseSensitive cs)
static if (isSomeString!Range)
{
import std.algorithm.searching : find;
const(Char1)[] balance;
static if (cs == Yes.caseSensitive)
static if (is(Char1 == Char) && cs == Yes.caseSensitive)
{
balance = find(s, sub);
import std.algorithm.searching : countUntil;
return s.representation.countUntil(sub.representation);
}
else
{
balance = find!
((a, b) => toLower(a) == toLower(b))
(s, sub);
import std.algorithm.searching : find;
const(Char1)[] balance;
static if (cs == Yes.caseSensitive)
{
balance = find(s, sub);
}
else
{
balance = find!
((a, b) => toLower(a) == toLower(b))
(s, sub);
}
return () @trusted { return balance.empty ? -1 : balance.ptr - s.ptr; } ();
}
return () @trusted { return balance.empty ? -1 : balance.ptr - s.ptr; } ();
}
else
{