CleanDateTimeParser.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. var len = cleanDateTimeRegexes.Count;
  17. for (int i = 0; i < len; i++)
  18. {
  19. if (TryClean(name, cleanDateTimeRegexes[i], ref result))
  20. {
  21. return result;
  22. }
  23. }
  24. return result;
  25. }
  26. private static bool TryClean(string name, Regex expression, ref CleanDateTimeResult result)
  27. {
  28. var match = expression.Match(name);
  29. if (match.Success
  30. && match.Groups.Count == 5
  31. && match.Groups[1].Success
  32. && match.Groups[2].Success
  33. && int.TryParse(match.Groups[2].Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var year))
  34. {
  35. result = new CleanDateTimeResult(match.Groups[1].Value.TrimEnd(), year);
  36. return true;
  37. }
  38. return false;
  39. }
  40. }
  41. }