CleanStringParser.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #pragma warning disable CS1591
  2. #pragma warning disable SA1600
  3. #nullable enable
  4. using System;
  5. using System.Collections.Generic;
  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 CleanStringParser
  13. {
  14. public static bool TryClean(string name, IReadOnlyList<Regex> expressions, out ReadOnlySpan<char> newName)
  15. {
  16. var len = expressions.Count;
  17. for (int i = 0; i < len; i++)
  18. {
  19. if (TryClean(name, expressions[i], out newName))
  20. {
  21. return true;
  22. }
  23. }
  24. newName = ReadOnlySpan<char>.Empty;
  25. return false;
  26. }
  27. private static bool TryClean(string name, Regex expression, out ReadOnlySpan<char> newName)
  28. {
  29. var match = expression.Match(name);
  30. int index = match.Index;
  31. if (match.Success && index != 0)
  32. {
  33. newName = name.AsSpan().Slice(0, match.Index);
  34. return true;
  35. }
  36. newName = string.Empty;
  37. return false;
  38. }
  39. }
  40. }