BaseExtensions.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. public static string RemoveDiacritics(this string text)
  26. {
  27. return String.Concat(
  28. text.Normalize(NormalizationForm.FormD)
  29. .Where(ch => CharUnicodeInfo.GetUnicodeCategory(ch) !=
  30. UnicodeCategory.NonSpacingMark)
  31. ).Normalize(NormalizationForm.FormC);
  32. }
  33. /// <summary>
  34. /// Gets the M d5.
  35. /// </summary>
  36. /// <param name="str">The STR.</param>
  37. /// <returns>Guid.</returns>
  38. public static Guid GetMD5(this string str)
  39. {
  40. using (var provider = MD5.Create())
  41. {
  42. return new Guid(provider.ComputeHash(Encoding.Unicode.GetBytes(str)));
  43. }
  44. }
  45. /// <summary>
  46. /// Gets the MB id.
  47. /// </summary>
  48. /// <param name="str">The STR.</param>
  49. /// <param name="type">The type.</param>
  50. /// <returns>Guid.</returns>
  51. /// <exception cref="System.ArgumentNullException">type</exception>
  52. [Obsolete("Use LibraryManager.GetNewItemId")]
  53. public static Guid GetMBId(this string str, Type type)
  54. {
  55. if (type == null)
  56. {
  57. throw new ArgumentNullException("type");
  58. }
  59. var key = type.FullName + str.ToLower();
  60. return key.GetMD5();
  61. }
  62. }
  63. }