std.string: Optimize indexOf with start index a bit

Use of early returns improves readability and reduces the instruction
count in DMD's output.
This commit is contained in:
Vladimir Panteleev 2018-11-24 08:30:14 +00:00
parent 2f7854cc9a
commit 8889ef2f07
No known key found for this signature in database
GPG key ID: 5004F0FAD051576D

View file

@ -895,15 +895,12 @@ ptrdiff_t indexOf(Char1, Char2)(const(Char1)[] s, const(Char2)[] sub,
@safe
if (isSomeChar!Char1 && isSomeChar!Char2)
{
if (startIdx < s.length)
{
ptrdiff_t foundIdx = indexOf(s[startIdx .. $], sub);
if (foundIdx != -1)
{
return foundIdx + cast(ptrdiff_t) startIdx;
}
}
return -1;
if (startIdx >= s.length)
return -1;
ptrdiff_t foundIdx = indexOf(s[startIdx .. $], sub);
if (foundIdx == -1)
return -1;
return foundIdx + cast(ptrdiff_t) startIdx;
}
/// Ditto
@ -912,15 +909,12 @@ ptrdiff_t indexOf(Char1, Char2)(const(Char1)[] s, const(Char2)[] sub,
@safe
if (isSomeChar!Char1 && isSomeChar!Char2)
{
if (startIdx < s.length)
{
ptrdiff_t foundIdx = indexOf(s[startIdx .. $], sub, cs);
if (foundIdx != -1)
{
return foundIdx + cast(ptrdiff_t) startIdx;
}
}
return -1;
if (startIdx >= s.length)
return -1;
ptrdiff_t foundIdx = indexOf(s[startIdx .. $], sub, cs);
if (foundIdx == -1)
return -1;
return foundIdx + cast(ptrdiff_t) startIdx;
}
///