ContainerHelper.cs 6.5 KB

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