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