From 220443eca1dfef08a2a0e000ac5963e18af073fc Mon Sep 17 00:00:00 2001 From: cvium Date: Fri, 10 Dec 2021 14:23:31 +0100 Subject: Simplify StackResolver --- Emby.Naming/Video/FileStackRule.cs | 48 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 Emby.Naming/Video/FileStackRule.cs (limited to 'Emby.Naming/Video/FileStackRule.cs') diff --git a/Emby.Naming/Video/FileStackRule.cs b/Emby.Naming/Video/FileStackRule.cs new file mode 100644 index 000000000..36a765dfb --- /dev/null +++ b/Emby.Naming/Video/FileStackRule.cs @@ -0,0 +1,48 @@ +using System.Diagnostics.CodeAnalysis; +using System.Text.RegularExpressions; + +namespace Emby.Naming.Video; + +/// +/// Regex based rule for file stacking (eg. disc1, disc2). +/// +public class FileStackRule +{ + private readonly Regex _tokenRegex; + + /// + /// Initializes a new instance of the class. + /// + /// Token. + /// Whether the file stack rule uses numerical or alphabetical numbering. + public FileStackRule(string token, bool isNumerical) + { + _tokenRegex = new Regex(token, RegexOptions.IgnoreCase); + IsNumerical = isNumerical; + } + + /// + /// Gets a value indicating whether the rule uses numerical or alphabetical numbering. + /// + public bool IsNumerical { get; } + + /// + /// Match the input against the rule regex. + /// + /// The input. + /// The part type and number or null. + /// A value indicating whether the input matched the rule. + public bool Match(string input, [NotNullWhen(true)] out (string StackName, string PartType, string PartNumber)? result) + { + result = null; + var match = _tokenRegex.Match(input); + if (!match.Success) + { + return false; + } + + var partType = match.Groups["parttype"].Success ? match.Groups["parttype"].Value : "vol"; + result = (match.Groups["filename"].Value, partType, match.Groups["number"].Value); + return true; + } +} -- cgit v1.2.3