EnumerableExtensions.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System;
  2. using System.Collections.Generic;
  3. namespace Jellyfin.Extensions;
  4. /// <summary>
  5. /// Static extensions for the <see cref="IEnumerable{T}"/> interface.
  6. /// </summary>
  7. public static class EnumerableExtensions
  8. {
  9. /// <summary>
  10. /// Determines whether the value is contained in the source collection.
  11. /// </summary>
  12. /// <param name="source">An instance of the <see cref="IEnumerable{String}"/> interface.</param>
  13. /// <param name="value">The value to look for in the collection.</param>
  14. /// <param name="stringComparison">The string comparison.</param>
  15. /// <returns>A value indicating whether the value is contained in the collection.</returns>
  16. /// <exception cref="ArgumentNullException">The source is null.</exception>
  17. public static bool Contains(this IEnumerable<string> source, ReadOnlySpan<char> value, StringComparison stringComparison)
  18. {
  19. ArgumentNullException.ThrowIfNull(source);
  20. if (source is IList<string> list)
  21. {
  22. int len = list.Count;
  23. for (int i = 0; i < len; i++)
  24. {
  25. if (value.Equals(list[i], stringComparison))
  26. {
  27. return true;
  28. }
  29. }
  30. return false;
  31. }
  32. foreach (string element in source)
  33. {
  34. if (value.Equals(element, stringComparison))
  35. {
  36. return true;
  37. }
  38. }
  39. return false;
  40. }
  41. /// <summary>
  42. /// Gets an IEnumerable from a single item.
  43. /// </summary>
  44. /// <param name="item">The item to return.</param>
  45. /// <typeparam name="T">The type of item.</typeparam>
  46. /// <returns>The IEnumerable{T}.</returns>
  47. public static IEnumerable<T> SingleItemAsEnumerable<T>(this T item)
  48. {
  49. yield return item;
  50. }
  51. }