using System; using System.Collections.Generic; using System.Linq; namespace Jellyfin.Extensions; /// /// Static extensions for the interface. /// public static class EnumerableExtensions { /// /// Determines whether the value is contained in the source collection. /// /// An instance of the interface. /// The value to look for in the collection. /// The string comparison. /// A value indicating whether the value is contained in the collection. /// The source is null. public static bool Contains(this IEnumerable source, ReadOnlySpan value, StringComparison stringComparison) { ArgumentNullException.ThrowIfNull(source); if (source is IList list) { int len = list.Count; for (int i = 0; i < len; i++) { if (value.Equals(list[i], stringComparison)) { return true; } } return false; } foreach (string element in source) { if (value.Equals(element, stringComparison)) { return true; } } return false; } /// /// Gets an IEnumerable from a single item. /// /// The item to return. /// The type of item. /// The IEnumerable{T}. public static IEnumerable SingleItemAsEnumerable(this T item) { yield return item; } /// /// Gets an IEnumerable consisting of all flags of an enum. /// /// The flags enum. /// The type of item. /// The IEnumerable{Enum}. public static IEnumerable GetUniqueFlags(this T flags) where T : Enum { foreach (Enum value in Enum.GetValues(flags.GetType())) { if (flags.HasFlag(value)) { yield return (T)value; } } } }