jellyfin/Emby.Naming/Video/Format3DParser.cs

82 lines
2.6 KiB
C#
Raw Normal View History

2018-09-12 17:26:21 +00:00
using System;
2019-01-13 19:17:29 +00:00
using Emby.Naming.Common;
2018-09-12 17:26:21 +00:00
namespace Emby.Naming.Video
{
2020-11-10 18:23:10 +00:00
/// <summary>
2021-05-23 22:30:41 +00:00
/// Parse 3D format related flags.
2020-11-10 18:23:10 +00:00
/// </summary>
2021-05-23 22:30:41 +00:00
public static class Format3DParser
2018-09-12 17:26:21 +00:00
{
2021-05-23 22:30:41 +00:00
// Static default result to save on allocation costs.
private static readonly Format3DResult _defaultResult = new (false, null);
2018-09-12 17:26:21 +00:00
2020-11-10 18:23:10 +00:00
/// <summary>
/// Parse 3D format related flags.
/// </summary>
/// <param name="path">Path to file.</param>
2021-05-23 22:30:41 +00:00
/// <param name="namingOptions">The naming options.</param>
2020-11-10 18:23:10 +00:00
/// <returns>Returns <see cref="Format3DResult"/> object.</returns>
2021-06-11 22:16:33 +00:00
public static Format3DResult Parse(ReadOnlySpan<char> path, NamingOptions namingOptions)
2018-09-12 17:26:21 +00:00
{
2021-05-23 22:30:41 +00:00
int oldLen = namingOptions.VideoFlagDelimiters.Length;
2021-06-11 22:16:33 +00:00
Span<char> delimiters = stackalloc char[oldLen + 1];
namingOptions.VideoFlagDelimiters.AsSpan().CopyTo(delimiters);
2020-11-01 10:19:22 +00:00
delimiters[oldLen] = ' ';
2018-09-12 17:26:21 +00:00
2021-05-23 22:30:41 +00:00
return Parse(path, delimiters, namingOptions);
2018-09-12 17:26:21 +00:00
}
2021-06-11 22:16:33 +00:00
private static Format3DResult Parse(ReadOnlySpan<char> path, ReadOnlySpan<char> delimiters, NamingOptions namingOptions)
2018-09-12 17:26:21 +00:00
{
2021-05-23 22:30:41 +00:00
foreach (var rule in namingOptions.Format3DRules)
2018-09-12 17:26:21 +00:00
{
2021-05-23 22:30:41 +00:00
var result = Parse(path, rule, delimiters);
2018-09-12 17:26:21 +00:00
if (result.Is3D)
{
return result;
}
}
2021-05-23 22:30:41 +00:00
return _defaultResult;
2018-09-12 17:26:21 +00:00
}
2021-06-11 22:16:33 +00:00
private static Format3DResult Parse(ReadOnlySpan<char> path, Format3DRule rule, ReadOnlySpan<char> delimiters)
2018-09-12 17:26:21 +00:00
{
2021-05-23 22:30:41 +00:00
bool is3D = false;
string? format3D = null;
2018-09-12 17:26:21 +00:00
2021-05-23 22:30:41 +00:00
// If there's no preceding token we just consider it found
var foundPrefix = string.IsNullOrEmpty(rule.PrecedingToken);
while (path.Length > 0)
2018-09-12 17:26:21 +00:00
{
2021-05-23 22:30:41 +00:00
var index = path.IndexOfAny(delimiters);
if (index == -1)
2018-09-12 17:26:21 +00:00
{
2021-05-23 22:30:41 +00:00
index = path.Length - 1;
2018-09-12 17:26:21 +00:00
}
2021-05-23 22:30:41 +00:00
var currentSlice = path[..index];
path = path[(index + 1)..];
2018-09-12 17:26:21 +00:00
2021-05-23 22:30:41 +00:00
if (!foundPrefix)
{
foundPrefix = currentSlice.Equals(rule.PrecedingToken, StringComparison.OrdinalIgnoreCase);
continue;
}
2019-05-10 18:37:42 +00:00
2021-05-23 22:30:41 +00:00
is3D = foundPrefix && currentSlice.Equals(rule.Token, StringComparison.OrdinalIgnoreCase);
2019-05-10 18:37:42 +00:00
2021-05-23 22:30:41 +00:00
if (is3D)
{
format3D = rule.Token;
break;
2018-09-12 17:26:21 +00:00
}
}
2021-05-23 22:30:41 +00:00
return is3D ? new Format3DResult(true, format3D) : _defaultResult;
2018-09-12 17:26:21 +00:00
}
}
}