PathExtensions.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #nullable enable
  2. using System;
  3. using System.Text.RegularExpressions;
  4. namespace Emby.Server.Implementations.Library
  5. {
  6. /// <summary>
  7. /// Class providing extension methods for working with paths.
  8. /// </summary>
  9. public static class PathExtensions
  10. {
  11. /// <summary>
  12. /// Gets the attribute value.
  13. /// </summary>
  14. /// <param name="str">The STR.</param>
  15. /// <param name="attribute">The attrib.</param>
  16. /// <returns>System.String.</returns>
  17. /// <exception cref="ArgumentException"><paramref name="str" /> or <paramref name="attribute" /> is empty.</exception>
  18. public static string? GetAttributeValue(this string str, string attribute)
  19. {
  20. if (str.Length == 0)
  21. {
  22. throw new ArgumentException("String can't be empty.", nameof(str));
  23. }
  24. if (attribute.Length == 0)
  25. {
  26. throw new ArgumentException("String can't be empty.", nameof(attribute));
  27. }
  28. string srch = "[" + attribute + "=";
  29. int start = str.IndexOf(srch, StringComparison.OrdinalIgnoreCase);
  30. if (start != -1)
  31. {
  32. start += srch.Length;
  33. int end = str.IndexOf(']', start);
  34. return str.Substring(start, end - start);
  35. }
  36. // for imdbid we also accept pattern matching
  37. if (string.Equals(attribute, "imdbid", StringComparison.OrdinalIgnoreCase))
  38. {
  39. var m = Regex.Match(str, "tt([0-9]{7,8})", RegexOptions.IgnoreCase);
  40. return m.Success ? m.Value : null;
  41. }
  42. return null;
  43. }
  44. }
  45. }