CleanStringParser.cs 1.2 KB

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