diff --git a/Extensions/StringExtensions.cs b/Extensions/StringExtensions.cs index dd70c87..6274c60 100644 --- a/Extensions/StringExtensions.cs +++ b/Extensions/StringExtensions.cs @@ -1,4 +1,6 @@ -namespace MeloongCore.Extensions; +using System.Text.RegularExpressions; + +namespace MeloongCore.Extensions; public static class StringExtensions { #region 区域性 / 大小写 简化 @@ -169,7 +171,8 @@ public static class StringExtensions { str.Contains(oldValue) ? str.Replace(oldValue, newValue()) : str; /// - /// 将字符串统一至某个长度,过短则用 Code 将其左侧填充,过长则截取靠左的指定长度。 + /// 将字符串统一至某个长度。 + /// 过短则用 将其左侧填充,过长则截取靠左的指定长度。 /// public static string EnsureLength(this string? str, char code, int length) { if (str == null) str = ""; @@ -179,6 +182,16 @@ public static class StringExtensions { /// /// 该字符串中的字符是否均为 ASCII 字符。 /// - public static bool IsAsciiOnly([DisallowNull] this string input) => input.All(c => (int) c < 128); + public static bool IsAsciiOnly([DisallowNull] this string input) => input.All(c => c < 128); + + /// + /// 将字符串中的换行符统一替换为指定字符。 + /// 若指定了 ,会将多次换行合并成一次换行。 + /// + public static string ReplaceLineEndings([DisallowNull] this string input, string newValue, bool mergeMultiple = false) => + Regex.Replace(input, + mergeMultiple ? @"(?:\r\n|[\n\r\f\u0085\u2028\u2029])+" : @"\r\n|[\n\r\f\u0085\u2028\u2029]", + newValue.Replace("$", "$$"), // 避免识别成捕获组 + RegexOptions.Compiled); } diff --git a/MeloongCore.csproj b/MeloongCore.csproj index 9f681fd..2c7f735 100644 --- a/MeloongCore.csproj +++ b/MeloongCore.csproj @@ -31,6 +31,7 @@ + diff --git a/Utils/EscapeUtils.cs b/Utils/EscapeUtils.cs new file mode 100644 index 0000000..46d5105 --- /dev/null +++ b/Utils/EscapeUtils.cs @@ -0,0 +1,72 @@ +using System.Text.RegularExpressions; + +namespace MeloongCore; +public static class EscapeUtils { + + /// + /// XML 转义。 + /// + public static string? XmlEscape(string? str) { + if (str is null) return null; + if (str.StartsWithF("{")) str = "{}" + str; // #4187 + return str + .Replace("&", "&").Replace("<", "<").Replace(">", ">").Replace("'", "'") + .Replace("\"", """).ReplaceLineEndings(" "); + } + + /// + /// VB.NET 的 Like 关键字转义。 + /// + public static string LikePatternEscape(string input) { + var builder = new StringBuilder(); + foreach (char c in input) { + if (c is '[' or ']' or '*' or '?' or '#') { + builder.Append('[').Append(c).Append(']'); + } else { + builder.Append(c); + } + } + return builder.ToString(); + } + + /// + /// 正则表达式转义。等同于 Regex.Escape。 + /// + public static string? RegexEscape(string? value) => Regex.Escape(value); + + /// + /// 正则表达式去转义。等同于 Regex.Unescape。 + /// + public static string? RegexUnescape(string? value) => Regex.Unescape(value); + + /// + /// URL 转义。等同于 Uri.EscapeDataString。 + /// + public static string? UrlEscape(string? value) => Uri.EscapeDataString(value); + + /// + /// URL 去转义。等同于 Uri.UnescapeDataString。 + /// + public static string? UrlUnescape(string? value) => Uri.UnescapeDataString(value); + + /// + /// 表单转义。等同于 WebUtility.UrlEncode。 + /// + public static string? FormUrlEscape(string? value) => WebUtility.UrlEncode(value); + + /// + /// 表单去转义。等同于 WebUtility.UrlDecode。 + /// + public static string? FormUrlUnescape(string? value) => WebUtility.UrlDecode(value); + + /// + /// HTML 转义。等同于 WebUtility.HtmlEncode。 + /// + public static string? HtmlEscape(string? value) => WebUtility.HtmlEncode(value); + + /// + /// HTML 去转义。等同于 WebUtility.HtmlDecode。 + /// + public static string? HtmlUnescape(string? value) => WebUtility.HtmlDecode(value); + +} \ No newline at end of file