2
0

CleanDateTimeParser.cs 1.5 KB

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