mirror of
https://github.com/dlang/phobos.git
synced 2025-05-10 22:18:03 +03:00
Merge pull request #1013 from Poita/endsWithRefactor
Refactored endsWith to use retro + startsWith.
This commit is contained in:
commit
c1f8609e1f
1 changed files with 4 additions and 27 deletions
|
@ -4971,35 +4971,9 @@ if (isBidirectionalRange!R1 &&
|
||||||
|
|
||||||
return haystack[$ - needle.length .. $] == needle;
|
return haystack[$ - needle.length .. $] == needle;
|
||||||
}
|
}
|
||||||
else static if (isArray!R1 && isArray!R2 &&
|
|
||||||
!isNarrowString!R1 && !isNarrowString!R2)
|
|
||||||
{
|
|
||||||
if (haystack.length < needle.length) return false;
|
|
||||||
immutable diff = haystack.length - needle.length;
|
|
||||||
foreach (j; 0 .. needle.length)
|
|
||||||
{
|
|
||||||
if (!binaryFun!pred(needle[j], haystack[j + diff]))
|
|
||||||
// not found
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
// found!
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
static if (hasLength!R1 && hasLength!R2)
|
return startsWith!pred(retro(doesThisEnd), retro(withThis));
|
||||||
{
|
|
||||||
if (haystack.length < needle.length) return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (needle.empty) return true;
|
|
||||||
for (; !haystack.empty; haystack.popBack())
|
|
||||||
{
|
|
||||||
if (!binaryFun!pred(haystack.back, needle.back)) break;
|
|
||||||
needle.popBack();
|
|
||||||
if (needle.empty) return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5092,6 +5066,9 @@ unittest
|
||||||
assert(endsWith(arr, wrap([4, 5]), 7) == 1);
|
assert(endsWith(arr, wrap([4, 5]), 7) == 1);
|
||||||
assert(!endsWith(arr, wrap([2, 4, 5])));
|
assert(!endsWith(arr, wrap([2, 4, 5])));
|
||||||
assert(endsWith(arr, [2, 4, 5], wrap([3, 4, 5])) == 2);
|
assert(endsWith(arr, [2, 4, 5], wrap([3, 4, 5])) == 2);
|
||||||
|
|
||||||
|
assert(endsWith!("a%10 == b%10")(arr, [14, 15]));
|
||||||
|
assert(!endsWith!("a%10 == b%10")(arr, [15, 14]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue