mirror of
https://github.com/dlang/phobos.git
synced 2025-04-27 21:51:40 +03:00
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:
parent
2f7854cc9a
commit
8889ef2f07
1 changed files with 12 additions and 18 deletions
30
std/string.d
30
std/string.d
|
@ -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;
|
||||
}
|
||||
|
||||
///
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue