BaseExtensions.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Security.Cryptography;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. namespace MediaBrowser.Common.Extensions
  8. {
  9. /// <summary>
  10. /// Class BaseExtensions
  11. /// </summary>
  12. public static class BaseExtensions
  13. {
  14. /// <summary>
  15. /// Strips the HTML.
  16. /// </summary>
  17. /// <param name="htmlString">The HTML string.</param>
  18. /// <returns>System.String.</returns>
  19. public static string StripHtml(this string htmlString)
  20. {
  21. // http://stackoverflow.com/questions/1349023/how-can-i-strip-html-from-text-in-net
  22. const string pattern = @"<(.|\n)*?>";
  23. return Regex.Replace(htmlString, pattern, string.Empty).Trim();
  24. }
  25. /// <summary>
  26. /// Gets the M d5.
  27. /// </summary>
  28. /// <param name="str">The STR.</param>
  29. /// <returns>Guid.</returns>
  30. public static Guid GetMD5(this string str)
  31. {
  32. using (var provider = MD5.Create())
  33. {
  34. return new Guid(provider.ComputeHash(Encoding.Unicode.GetBytes(str)));
  35. }
  36. }
  37. /// <summary>
  38. /// Gets the MB id.
  39. /// </summary>
  40. /// <param name="str">The STR.</param>
  41. /// <param name="aType">A type.</param>
  42. /// <returns>Guid.</returns>
  43. /// <exception cref="System.ArgumentNullException">aType</exception>
  44. public static Guid GetMBId(this string str, Type aType)
  45. {
  46. if (aType == null)
  47. {
  48. throw new ArgumentNullException("aType");
  49. }
  50. return (aType.FullName + str.ToLower()).GetMD5();
  51. }
  52. /// <summary>
  53. /// Helper method for Dictionaries since they throw on not-found keys
  54. /// </summary>
  55. /// <typeparam name="T"></typeparam>
  56. /// <typeparam name="U"></typeparam>
  57. /// <param name="dictionary">The dictionary.</param>
  58. /// <param name="key">The key.</param>
  59. /// <param name="defaultValue">The default value.</param>
  60. /// <returns>``1.</returns>
  61. public static U GetValueOrDefault<T, U>(this Dictionary<T, U> dictionary, T key, U defaultValue)
  62. {
  63. U val;
  64. if (!dictionary.TryGetValue(key, out val))
  65. {
  66. val = defaultValue;
  67. }
  68. return val;
  69. }
  70. /// <summary>
  71. /// Gets the attribute value.
  72. /// </summary>
  73. /// <param name="str">The STR.</param>
  74. /// <param name="attrib">The attrib.</param>
  75. /// <returns>System.String.</returns>
  76. /// <exception cref="System.ArgumentNullException">attrib</exception>
  77. public static string GetAttributeValue(this string str, string attrib)
  78. {
  79. if (attrib == null)
  80. {
  81. throw new ArgumentNullException("attrib");
  82. }
  83. string srch = "[" + attrib + "=";
  84. int start = str.IndexOf(srch, StringComparison.OrdinalIgnoreCase);
  85. if (start > -1)
  86. {
  87. start += srch.Length;
  88. int end = str.IndexOf(']', start);
  89. return str.Substring(start, end - start);
  90. }
  91. return null;
  92. }
  93. }
  94. }