EscapeUtils.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System.Text.RegularExpressions;
  2. namespace MeloongCore;
  3. public static class EscapeUtils {
  4. /// <summary>
  5. /// WPF XML 转义。
  6. /// </summary>
  7. public static string? XmlEscape(string? str) {
  8. if (str is null) return null;
  9. if (str.StartsWithF("{")) str = "{}" + str; // #4187
  10. return str
  11. .Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace("'", "&apos;")
  12. .Replace("\"", "&quot;").ReplaceLineEndings("&#xa;");
  13. }
  14. /// <summary>
  15. /// VB.NET 的 Like 关键字转义。
  16. /// </summary>
  17. public static string LikePatternEscape(string input) {
  18. var builder = new StringBuilder();
  19. foreach (char c in input) {
  20. if (c is '[' or ']' or '*' or '?' or '#') {
  21. builder.Append('[').Append(c).Append(']');
  22. } else {
  23. builder.Append(c);
  24. }
  25. }
  26. return builder.ToString();
  27. }
  28. /// <summary>
  29. /// 正则表达式转义。
  30. /// 等同于 <see cref="Regex.Escape"/>。
  31. /// </summary>
  32. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  33. public static string? RegexEscape(string value)
  34. => Regex.Escape(value);
  35. /// <summary>
  36. /// 正则表达式去转义。
  37. /// 等同于 <see cref="Regex.Unescape"/>。
  38. /// </summary>
  39. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  40. public static string? RegexUnescape(string value)
  41. => Regex.Unescape(value);
  42. /// <summary>
  43. /// URL 转义。
  44. /// 等同于 <see cref="Uri.EscapeDataString"/>。
  45. /// </summary>
  46. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  47. public static string? UrlEscape(string value)
  48. => Uri.EscapeDataString(value);
  49. /// <summary>
  50. /// URL 去转义。
  51. /// 等同于 <see cref="Uri.UnescapeDataString"/>。
  52. /// </summary>
  53. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  54. public static string? UrlUnescape(string value)
  55. => Uri.UnescapeDataString(value);
  56. /// <summary>
  57. /// 表单转义。
  58. /// 等同于 <see cref="WebUtility.UrlEncode"/>。
  59. /// </summary>
  60. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  61. public static string? FormUrlEscape(string value)
  62. => WebUtility.UrlEncode(value);
  63. /// <summary>
  64. /// 表单去转义。
  65. /// 等同于 <see cref="WebUtility.UrlDecode"/>。
  66. /// </summary>
  67. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  68. public static string? FormUrlUnescape(string value)
  69. => WebUtility.UrlDecode(value);
  70. /// <summary>
  71. /// HTML 转义。
  72. /// 等同于 <see cref="WebUtility.HtmlEncode"/>。
  73. /// </summary>
  74. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  75. public static string? HtmlEscape(string value)
  76. => WebUtility.HtmlEncode(value);
  77. /// <summary>
  78. /// HTML 去转义。
  79. /// 等同于 <see cref="WebUtility.HtmlDecode"/>。
  80. /// </summary>
  81. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  82. public static string? HtmlUnescape(string value)
  83. => WebUtility.HtmlDecode(value);
  84. }