BaseExtensions.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System;
  2. using System.Globalization;
  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. /// Replaces the specified STR.
  27. /// </summary>
  28. /// <param name="str">The STR.</param>
  29. /// <param name="oldValue">The old value.</param>
  30. /// <param name="newValue">The new value.</param>
  31. /// <param name="comparison">The comparison.</param>
  32. /// <returns>System.String.</returns>
  33. public static string Replace(this string str, string oldValue, string newValue, StringComparison comparison)
  34. {
  35. var sb = new StringBuilder();
  36. var previousIndex = 0;
  37. var index = str.IndexOf(oldValue, comparison);
  38. while (index != -1)
  39. {
  40. sb.Append(str.Substring(previousIndex, index - previousIndex));
  41. sb.Append(newValue);
  42. index += oldValue.Length;
  43. previousIndex = index;
  44. index = str.IndexOf(oldValue, index, comparison);
  45. }
  46. sb.Append(str.Substring(previousIndex));
  47. return sb.ToString();
  48. }
  49. public static string RemoveDiacritics(this string text)
  50. {
  51. return String.Concat(
  52. text.Normalize(NormalizationForm.FormD)
  53. .Where(ch => CharUnicodeInfo.GetUnicodeCategory(ch) !=
  54. UnicodeCategory.NonSpacingMark)
  55. ).Normalize(NormalizationForm.FormC);
  56. }
  57. /// <summary>
  58. /// Gets the M d5.
  59. /// </summary>
  60. /// <param name="str">The STR.</param>
  61. /// <returns>Guid.</returns>
  62. public static Guid GetMD5(this string str)
  63. {
  64. using (var provider = MD5.Create())
  65. {
  66. return new Guid(provider.ComputeHash(Encoding.Unicode.GetBytes(str)));
  67. }
  68. }
  69. /// <summary>
  70. /// Gets the MB id.
  71. /// </summary>
  72. /// <param name="str">The STR.</param>
  73. /// <param name="type">The type.</param>
  74. /// <returns>Guid.</returns>
  75. /// <exception cref="System.ArgumentNullException">type</exception>
  76. [Obsolete("Use LibraryManager.GetNewItemId")]
  77. public static Guid GetMBId(this string str, Type type)
  78. {
  79. if (type == null)
  80. {
  81. throw new ArgumentNullException("type");
  82. }
  83. var key = type.FullName + str.ToLower();
  84. return key.GetMD5();
  85. }
  86. }
  87. }