Skip to content

Commit e52d278

Browse files
committed
Simplify internal implementation details of "EndsWith"
1 parent 5dc9e18 commit e52d278

File tree

1 file changed

+5
-13
lines changed

1 file changed

+5
-13
lines changed

MoreLinq/EndsWith.cs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,22 +71,14 @@ public static bool EndsWith<T>(this IEnumerable<T> first, IEnumerable<T> second,
7171
if (first == null) throw new ArgumentNullException(nameof(first));
7272
if (second == null) throw new ArgumentNullException(nameof(second));
7373

74-
comparer ??= EqualityComparer<T>.Default;
75-
7674
List<T> secondList;
77-
#pragma warning disable IDE0075 // Simplify conditional expression (makes it worse)
7875
return second.TryAsCollectionLike() is { Count: var secondCount }
79-
? first.TryAsCollectionLike() is { Count: var firstCount } && secondCount > firstCount
80-
? false
81-
: Impl(first, second, secondCount, comparer)
82-
: Impl(first, secondList = second.ToList(), secondList.Count, comparer);
83-
#pragma warning restore IDE0075 // Simplify conditional expression
76+
? first.TryAsCollectionLike() is not { Count: var firstCount } || secondCount <= firstCount
77+
&& EndsWith(second, secondCount)
78+
: EndsWith(secondList = second.ToList(), secondList.Count);
8479

85-
static bool Impl(IEnumerable<T> first, IEnumerable<T> second, int count, IEqualityComparer<T> comparer)
86-
{
87-
using var firstIter = first.TakeLast(count).GetEnumerator();
88-
return second.All(item => firstIter.MoveNext() && comparer.Equals(firstIter.Current, item));
89-
}
80+
bool EndsWith(IEnumerable<T> second, int count) =>
81+
first.TakeLast(count).SequenceEqual(second, comparer);
9082
}
9183
}
9284
}

0 commit comments

Comments
 (0)