jellyfin/Emby.Naming/Video/Format3DParser.cs

82 lines
2.6 KiB
C#
Raw Normal View History

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