PathExtensions.cs 1.5 KB

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