| | 1 | | using System; |
| | 2 | | using Emby.Naming.Common; |
| | 3 | |
|
| | 4 | | namespace Emby.Naming.Video |
| | 5 | | { |
| | 6 | | /// <summary> |
| | 7 | | /// Parse 3D format related flags. |
| | 8 | | /// </summary> |
| | 9 | | public static class Format3DParser |
| | 10 | | { |
| | 11 | | // Static default result to save on allocation costs. |
| 2 | 12 | | private static readonly Format3DResult _defaultResult = new(false, null); |
| | 13 | |
|
| | 14 | | /// <summary> |
| | 15 | | /// Parse 3D format related flags. |
| | 16 | | /// </summary> |
| | 17 | | /// <param name="path">Path to file.</param> |
| | 18 | | /// <param name="namingOptions">The naming options.</param> |
| | 19 | | /// <returns>Returns <see cref="Format3DResult"/> object.</returns> |
| | 20 | | public static Format3DResult Parse(ReadOnlySpan<char> path, NamingOptions namingOptions) |
| | 21 | | { |
| 325 | 22 | | int oldLen = namingOptions.VideoFlagDelimiters.Length; |
| 325 | 23 | | Span<char> delimiters = stackalloc char[oldLen + 1]; |
| 325 | 24 | | namingOptions.VideoFlagDelimiters.AsSpan().CopyTo(delimiters); |
| 325 | 25 | | delimiters[oldLen] = ' '; |
| | 26 | |
|
| 325 | 27 | | return Parse(path, delimiters, namingOptions); |
| | 28 | | } |
| | 29 | |
|
| | 30 | | private static Format3DResult Parse(ReadOnlySpan<char> path, ReadOnlySpan<char> delimiters, NamingOptions naming |
| | 31 | | { |
| 7997 | 32 | | foreach (var rule in namingOptions.Format3DRules) |
| | 33 | | { |
| 3688 | 34 | | var result = Parse(path, rule, delimiters); |
| | 35 | |
|
| 3688 | 36 | | if (result.Is3D) |
| | 37 | | { |
| 29 | 38 | | return result; |
| | 39 | | } |
| | 40 | | } |
| | 41 | |
|
| 296 | 42 | | return _defaultResult; |
| | 43 | | } |
| | 44 | |
|
| | 45 | | private static Format3DResult Parse(ReadOnlySpan<char> path, Format3DRule rule, ReadOnlySpan<char> delimiters) |
| | 46 | | { |
| 3688 | 47 | | bool is3D = false; |
| 3688 | 48 | | string? format3D = null; |
| | 49 | |
|
| | 50 | | // If there's no preceding token we just consider it found |
| 3688 | 51 | | var foundPrefix = string.IsNullOrEmpty(rule.PrecedingToken); |
| 33173 | 52 | | while (path.Length > 0) |
| | 53 | | { |
| 29514 | 54 | | var index = path.IndexOfAny(delimiters); |
| 29514 | 55 | | if (index == -1) |
| | 56 | | { |
| 3659 | 57 | | index = path.Length - 1; |
| | 58 | | } |
| | 59 | |
|
| 29514 | 60 | | var currentSlice = path[..index]; |
| 29514 | 61 | | path = path[(index + 1)..]; |
| | 62 | |
|
| 29514 | 63 | | if (!foundPrefix) |
| | 64 | | { |
| 9996 | 65 | | foundPrefix = currentSlice.Equals(rule.PrecedingToken, StringComparison.OrdinalIgnoreCase); |
| 9996 | 66 | | continue; |
| | 67 | | } |
| | 68 | |
|
| 19518 | 69 | | is3D = foundPrefix && currentSlice.Equals(rule.Token, StringComparison.OrdinalIgnoreCase); |
| | 70 | |
|
| 19518 | 71 | | if (is3D) |
| | 72 | | { |
| 29 | 73 | | format3D = rule.Token; |
| 29 | 74 | | break; |
| | 75 | | } |
| | 76 | | } |
| | 77 | |
|
| 3688 | 78 | | return is3D ? new Format3DResult(true, format3D) : _defaultResult; |
| | 79 | | } |
| | 80 | | } |
| | 81 | | } |