ContainerHelper.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System;
  2. using System.Collections.Generic;
  3. using Jellyfin.Extensions;
  4. namespace MediaBrowser.Model.Extensions;
  5. /// <summary>
  6. /// Defines the <see cref="ContainerHelper"/> class.
  7. /// </summary>
  8. public static class ContainerHelper
  9. {
  10. /// <summary>
  11. /// Compares two containers, returning true if an item in <paramref name="inputContainer"/> exists
  12. /// in <paramref name="profileContainers"/>.
  13. /// </summary>
  14. /// <param name="profileContainers">The comma-delimited string being searched.
  15. /// If the parameter begins with the <c>-</c> character, the operation is reversed.</param>
  16. /// <param name="inputContainer">The comma-delimited string being matched.</param>
  17. /// <returns>The result of the operation.</returns>
  18. public static bool ContainsContainer(string? profileContainers, string? inputContainer)
  19. {
  20. var isNegativeList = false;
  21. if (profileContainers != null && profileContainers.StartsWith('-'))
  22. {
  23. isNegativeList = true;
  24. profileContainers = profileContainers[1..];
  25. }
  26. return ContainsContainer(profileContainers, isNegativeList, inputContainer);
  27. }
  28. /// <summary>
  29. /// Compares two containers, returning true if an item in <paramref name="inputContainer"/> exists
  30. /// in <paramref name="profileContainers"/>.
  31. /// </summary>
  32. /// <param name="profileContainers">The comma-delimited string being searched.
  33. /// If the parameter begins with the <c>-</c> character, the operation is reversed.</param>
  34. /// <param name="inputContainer">The comma-delimited string being matched.</param>
  35. /// <returns>The result of the operation.</returns>
  36. public static bool ContainsContainer(string? profileContainers, ReadOnlySpan<char> inputContainer)
  37. {
  38. var isNegativeList = false;
  39. if (profileContainers != null && profileContainers.StartsWith('-'))
  40. {
  41. isNegativeList = true;
  42. profileContainers = profileContainers[1..];
  43. }
  44. return ContainsContainer(profileContainers, isNegativeList, inputContainer);
  45. }
  46. /// <summary>
  47. /// Compares two containers, returning <paramref name="isNegativeList"/> if an item in <paramref name="inputContainer"/>
  48. /// does not exist in <paramref name="profileContainers"/>.
  49. /// </summary>
  50. /// <param name="profileContainers">The comma-delimited string being searched.</param>
  51. /// <param name="isNegativeList">The boolean result to return if a match is not found.</param>
  52. /// <param name="inputContainer">The comma-delimited string being matched.</param>
  53. /// <returns>The result of the operation.</returns>
  54. public static bool ContainsContainer(string? profileContainers, bool isNegativeList, string? inputContainer)
  55. {
  56. if (string.IsNullOrEmpty(inputContainer))
  57. {
  58. return isNegativeList;
  59. }
  60. return ContainsContainer(profileContainers, isNegativeList, inputContainer.AsSpan());
  61. }
  62. /// <summary>
  63. /// Compares two containers, returning <paramref name="isNegativeList"/> if an item in <paramref name="inputContainer"/>
  64. /// does not exist in <paramref name="profileContainers"/>.
  65. /// </summary>
  66. /// <param name="profileContainers">The comma-delimited string being searched.</param>
  67. /// <param name="isNegativeList">The boolean result to return if a match is not found.</param>
  68. /// <param name="inputContainer">The comma-delimited string being matched.</param>
  69. /// <returns>The result of the operation.</returns>
  70. public static bool ContainsContainer(string? profileContainers, bool isNegativeList, ReadOnlySpan<char> inputContainer)
  71. {
  72. if (string.IsNullOrEmpty(profileContainers))
  73. {
  74. // Empty profiles always support all containers/codecs.
  75. return true;
  76. }
  77. var allInputContainers = inputContainer.Split(',');
  78. var allProfileContainers = profileContainers.SpanSplit(',');
  79. foreach (var container in allInputContainers)
  80. {
  81. if (!container.IsEmpty)
  82. {
  83. foreach (var profile in allProfileContainers)
  84. {
  85. if (!profile.IsEmpty && container.Equals(profile, StringComparison.OrdinalIgnoreCase))
  86. {
  87. return !isNegativeList;
  88. }
  89. }
  90. }
  91. }
  92. return isNegativeList;
  93. }
  94. /// <summary>
  95. /// Compares two containers, returning <paramref name="isNegativeList"/> if an item in <paramref name="inputContainer"/>
  96. /// does not exist in <paramref name="profileContainers"/>.
  97. /// </summary>
  98. /// <param name="profileContainers">The profile containers being matched searched.</param>
  99. /// <param name="isNegativeList">The boolean result to return if a match is not found.</param>
  100. /// <param name="inputContainer">The comma-delimited string being matched.</param>
  101. /// <returns>The result of the operation.</returns>
  102. public static bool ContainsContainer(IReadOnlyList<string>? profileContainers, bool isNegativeList, string inputContainer)
  103. {
  104. if (profileContainers is null)
  105. {
  106. // Empty profiles always support all containers/codecs.
  107. return true;
  108. }
  109. var allInputContainers = Split(inputContainer);
  110. foreach (var container in allInputContainers)
  111. {
  112. foreach (var profile in profileContainers)
  113. {
  114. if (string.Equals(profile, container, StringComparison.OrdinalIgnoreCase))
  115. {
  116. return !isNegativeList;
  117. }
  118. }
  119. }
  120. return isNegativeList;
  121. }
  122. /// <summary>
  123. /// Splits and input string.
  124. /// </summary>
  125. /// <param name="input">The input string.</param>
  126. /// <returns>The result of the operation.</returns>
  127. public static string[] Split(string? input)
  128. {
  129. return input?.Split(',', StringSplitOptions.RemoveEmptyEntries) ?? [];
  130. }
  131. }