BaseExtensions.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. /// Tries the add.
  16. /// </summary>
  17. /// <typeparam name="TKey">The type of the T key.</typeparam>
  18. /// <typeparam name="TValue">The type of the T value.</typeparam>
  19. /// <param name="dictionary">The dictionary.</param>
  20. /// <param name="key">The key.</param>
  21. /// <param name="value">The value.</param>
  22. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  23. public static bool TryAdd<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, TValue value)
  24. {
  25. if (dictionary.ContainsKey(key))
  26. {
  27. return false;
  28. }
  29. dictionary.Add(key, value);
  30. return true;
  31. }
  32. /// <summary>
  33. /// Provides an additional overload for string.split
  34. /// </summary>
  35. /// <param name="val">The val.</param>
  36. /// <param name="separator">The separator.</param>
  37. /// <param name="options">The options.</param>
  38. /// <returns>System.String[][].</returns>
  39. public static string[] Split(this string val, char separator, StringSplitOptions options)
  40. {
  41. return val.Split(new[] { separator }, options);
  42. }
  43. /// <summary>
  44. /// Strips the HTML.
  45. /// </summary>
  46. /// <param name="htmlString">The HTML string.</param>
  47. /// <returns>System.String.</returns>
  48. public static string StripHtml(this string htmlString)
  49. {
  50. // http://stackoverflow.com/questions/1349023/how-can-i-strip-html-from-text-in-net
  51. const string pattern = @"<(.|\n)*?>";
  52. return Regex.Replace(htmlString, pattern, string.Empty);
  53. }
  54. /// <summary>
  55. /// Shuffles an IEnumerable
  56. /// </summary>
  57. /// <typeparam name="T"></typeparam>
  58. /// <param name="list">The list.</param>
  59. /// <returns>IEnumerable{``0}.</returns>
  60. public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> list)
  61. {
  62. return list.OrderBy(x => Guid.NewGuid());
  63. }
  64. /// <summary>
  65. /// Gets the M d5.
  66. /// </summary>
  67. /// <param name="str">The STR.</param>
  68. /// <returns>Guid.</returns>
  69. public static Guid GetMD5(this string str)
  70. {
  71. using (var provider = MD5.Create())
  72. {
  73. return new Guid(provider.ComputeHash(Encoding.Unicode.GetBytes(str)));
  74. }
  75. }
  76. /// <summary>
  77. /// Gets the MB id.
  78. /// </summary>
  79. /// <param name="str">The STR.</param>
  80. /// <param name="aType">A type.</param>
  81. /// <returns>Guid.</returns>
  82. /// <exception cref="System.ArgumentNullException">aType</exception>
  83. public static Guid GetMBId(this string str, Type aType)
  84. {
  85. if (aType == null)
  86. {
  87. throw new ArgumentNullException("aType");
  88. }
  89. return (aType.FullName + str.ToLower()).GetMD5();
  90. }
  91. /// <summary>
  92. /// Helper method for Dictionaries since they throw on not-found keys
  93. /// </summary>
  94. /// <typeparam name="T"></typeparam>
  95. /// <typeparam name="U"></typeparam>
  96. /// <param name="dictionary">The dictionary.</param>
  97. /// <param name="key">The key.</param>
  98. /// <param name="defaultValue">The default value.</param>
  99. /// <returns>``1.</returns>
  100. public static U GetValueOrDefault<T, U>(this Dictionary<T, U> dictionary, T key, U defaultValue)
  101. {
  102. U val;
  103. if (!dictionary.TryGetValue(key, out val))
  104. {
  105. val = defaultValue;
  106. }
  107. return val;
  108. }
  109. /// <summary>
  110. /// Gets the attribute value.
  111. /// </summary>
  112. /// <param name="str">The STR.</param>
  113. /// <param name="attrib">The attrib.</param>
  114. /// <returns>System.String.</returns>
  115. /// <exception cref="System.ArgumentNullException">attrib</exception>
  116. public static string GetAttributeValue(this string str, string attrib)
  117. {
  118. if (attrib == null)
  119. {
  120. throw new ArgumentNullException("attrib");
  121. }
  122. string srch = "[" + attrib + "=";
  123. int start = str.IndexOf(srch, StringComparison.OrdinalIgnoreCase);
  124. if (start > -1)
  125. {
  126. start += srch.Length;
  127. int end = str.IndexOf(']', start);
  128. return str.Substring(start, end - start);
  129. }
  130. return null;
  131. }
  132. }
  133. }