2
0

PathExtensions.cs 1.4 KB

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