@@ -71,22 +71,14 @@ public static bool EndsWith<T>(this IEnumerable<T> first, IEnumerable<T> second,
71
71
if ( first == null ) throw new ArgumentNullException ( nameof ( first ) ) ;
72
72
if ( second == null ) throw new ArgumentNullException ( nameof ( second ) ) ;
73
73
74
- comparer ??= EqualityComparer < T > . Default ;
75
-
76
74
List < T > secondList ;
77
- #pragma warning disable IDE0075 // Simplify conditional expression (makes it worse)
78
75
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 ) ;
84
79
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 ) ;
90
82
}
91
83
}
92
84
}
0 commit comments