aboutsummaryrefslogtreecommitdiff
path: root/Emby.Naming/Video/FileStackRule.cs
diff options
context:
space:
mode:
authoroledfish <88390729+oledfish@users.noreply.github.com>2022-01-16 21:33:18 -0300
committerGitHub <noreply@github.com>2022-01-16 21:33:18 -0300
commit3b075a58027be4a2a3bdf662c70934f6cafafe87 (patch)
treeb4c226f25f843c3f2685c92e1edc3b3999716d34 /Emby.Naming/Video/FileStackRule.cs
parent86a5e72a65df638df2cde349ccd2ad8c5d40f88c (diff)
parentef0708d876434a99ec647473c37295fab45a35fb (diff)
Merge branch 'jellyfin:master' into additional-episode-orders
Diffstat (limited to 'Emby.Naming/Video/FileStackRule.cs')
-rw-r--r--Emby.Naming/Video/FileStackRule.cs48
1 files changed, 48 insertions, 0 deletions
diff --git a/Emby.Naming/Video/FileStackRule.cs b/Emby.Naming/Video/FileStackRule.cs
new file mode 100644
index 0000000000..76b487f428
--- /dev/null
+++ b/Emby.Naming/Video/FileStackRule.cs
@@ -0,0 +1,48 @@
+using System.Diagnostics.CodeAnalysis;
+using System.Text.RegularExpressions;
+
+namespace Emby.Naming.Video;
+
+/// <summary>
+/// Regex based rule for file stacking (eg. disc1, disc2).
+/// </summary>
+public class FileStackRule
+{
+ private readonly Regex _tokenRegex;
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="FileStackRule"/> class.
+ /// </summary>
+ /// <param name="token">Token.</param>
+ /// <param name="isNumerical">Whether the file stack rule uses numerical or alphabetical numbering.</param>
+ public FileStackRule(string token, bool isNumerical)
+ {
+ _tokenRegex = new Regex(token, RegexOptions.IgnoreCase);
+ IsNumerical = isNumerical;
+ }
+
+ /// <summary>
+ /// Gets a value indicating whether the rule uses numerical or alphabetical numbering.
+ /// </summary>
+ public bool IsNumerical { get; }
+
+ /// <summary>
+ /// Match the input against the rule regex.
+ /// </summary>
+ /// <param name="input">The input.</param>
+ /// <param name="result">The part type and number or <c>null</c>.</param>
+ /// <returns>A value indicating whether the input matched the rule.</returns>
+ 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 : "unknown";
+ result = (match.Groups["filename"].Value, partType, match.Groups["number"].Value);
+ return true;
+ }
+}