CleanStringParser.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text.RegularExpressions;
  4. namespace Emby.Naming.Video
  5. {
  6. /// <summary>
  7. /// <see href="http://kodi.wiki/view/Advancedsettings.xml#video" />.
  8. /// </summary>
  9. public static class CleanStringParser
  10. {
  11. /// <summary>
  12. /// Attempts to extract clean name with regular expressions.
  13. /// </summary>
  14. /// <param name="name">Name of file.</param>
  15. /// <param name="expressions">List of regex to parse name and year from.</param>
  16. /// <param name="newName">Parsing result string.</param>
  17. /// <returns>True if parsing was successful.</returns>
  18. public static bool TryClean(string name, IReadOnlyList<Regex> expressions, out ReadOnlySpan<char> newName)
  19. {
  20. var len = expressions.Count;
  21. for (int i = 0; i < len; i++)
  22. {
  23. if (TryClean(name, expressions[i], out newName))
  24. {
  25. return true;
  26. }
  27. }
  28. newName = ReadOnlySpan<char>.Empty;
  29. return false;
  30. }
  31. private static bool TryClean(string name, Regex expression, out ReadOnlySpan<char> newName)
  32. {
  33. var match = expression.Match(name);
  34. int index = match.Index;
  35. if (match.Success && index != 0)
  36. {
  37. newName = name.AsSpan().Slice(0, match.Index);
  38. return true;
  39. }
  40. newName = string.Empty;
  41. return false;
  42. }
  43. }
  44. }