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 From 6d6fb1bba309f7566a856be7f9b74e7b48f54231 Mon Sep 17 00:00:00 2001 From: cvium Date: Sat, 11 Dec 2021 16:02:51 +0100 Subject: Rename unknown parttype to "unknown" --- Emby.Naming/Video/FileStackRule.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Emby.Naming/Video/FileStackRule.cs') diff --git a/Emby.Naming/Video/FileStackRule.cs b/Emby.Naming/Video/FileStackRule.cs index 36a765dfb..76b487f42 100644 --- a/Emby.Naming/Video/FileStackRule.cs +++ b/Emby.Naming/Video/FileStackRule.cs @@ -41,7 +41,7 @@ public class FileStackRule return false; } - var partType = match.Groups["parttype"].Success ? match.Groups["parttype"].Value : "vol"; + var partType = match.Groups["parttype"].Success ? match.Groups["parttype"].Value : "unknown"; result = (match.Groups["filename"].Value, partType, match.Groups["number"].Value); return true; } -- cgit v1.2.3