FileStackRule.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System.Diagnostics.CodeAnalysis;
  2. using System.Text.RegularExpressions;
  3. namespace Emby.Naming.Video;
  4. /// <summary>
  5. /// Regex based rule for file stacking (eg. disc1, disc2).
  6. /// </summary>
  7. public class FileStackRule
  8. {
  9. private readonly Regex _tokenRegex;
  10. /// <summary>
  11. /// Initializes a new instance of the <see cref="FileStackRule"/> class.
  12. /// </summary>
  13. /// <param name="token">Token.</param>
  14. /// <param name="isNumerical">Whether the file stack rule uses numerical or alphabetical numbering.</param>
  15. public FileStackRule(string token, bool isNumerical)
  16. {
  17. _tokenRegex = new Regex(token, RegexOptions.IgnoreCase | RegexOptions.Compiled);
  18. IsNumerical = isNumerical;
  19. }
  20. /// <summary>
  21. /// Gets a value indicating whether the rule uses numerical or alphabetical numbering.
  22. /// </summary>
  23. public bool IsNumerical { get; }
  24. /// <summary>
  25. /// Match the input against the rule regex.
  26. /// </summary>
  27. /// <param name="input">The input.</param>
  28. /// <param name="result">The part type and number or <c>null</c>.</param>
  29. /// <returns>A value indicating whether the input matched the rule.</returns>
  30. public bool Match(string input, [NotNullWhen(true)] out (string StackName, string PartType, string PartNumber)? result)
  31. {
  32. result = null;
  33. var match = _tokenRegex.Match(input);
  34. if (!match.Success)
  35. {
  36. return false;
  37. }
  38. var partType = match.Groups["parttype"].Success ? match.Groups["parttype"].Value : "unknown";
  39. result = (match.Groups["filename"].Value, partType, match.Groups["number"].Value);
  40. return true;
  41. }
  42. }