CleanDateTimeParser.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #pragma warning disable CS1591
  2. #pragma warning disable SA1600
  3. #nullable enable
  4. using System.Collections.Generic;
  5. using System.Globalization;
  6. using System.Text.RegularExpressions;
  7. namespace Emby.Naming.Video
  8. {
  9. /// <summary>
  10. /// <see href="http://kodi.wiki/view/Advancedsettings.xml#video" />.
  11. /// </summary>
  12. public static class CleanDateTimeParser
  13. {
  14. public static CleanDateTimeResult Clean(string name, IReadOnlyList<Regex> cleanDateTimeRegexes)
  15. {
  16. CleanDateTimeResult result = new CleanDateTimeResult(name);
  17. var len = cleanDateTimeRegexes.Count;
  18. for (int i = 0; i < len; i++)
  19. {
  20. if (TryClean(name, cleanDateTimeRegexes[i], ref result))
  21. {
  22. return result;
  23. }
  24. }
  25. return result;
  26. }
  27. private static bool TryClean(string name, Regex expression, ref CleanDateTimeResult result)
  28. {
  29. var match = expression.Match(name);
  30. if (match.Success
  31. && match.Groups.Count == 5
  32. && match.Groups[1].Success
  33. && match.Groups[2].Success
  34. && int.TryParse(match.Groups[2].Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var year))
  35. {
  36. result = new CleanDateTimeResult(match.Groups[1].Value.TrimEnd(), year);
  37. return true;
  38. }
  39. return false;
  40. }
  41. }
  42. }