ref: 添加 ReplaceLineEndings、EscapeUtils

This commit is contained in:
龙腾猫跃 committed 2026-04-27 19:27:15 +08:00
1 parent 9f10b27c6b
commit 5a60ba8a22
3 files changed
+89 -3

No files matched your search

+16 -3
View File
@@ -1,4 +1,6 @@
namespace MeloongCore.Extensions; using System.Text.RegularExpressions;
namespace MeloongCore.Extensions;
public static class StringExtensions { public static class StringExtensions {
#region 区域性 / 大小写 简化 #region 区域性 / 大小写 简化
@@ -169,7 +171,8 @@ public static class StringExtensions {
str.Contains(oldValue) ? str.Replace(oldValue, newValue()) : str; str.Contains(oldValue) ? str.Replace(oldValue, newValue()) : str;
/// <summary> /// <summary>
/// 将字符串统一至某个长度,过短则用 Code 将其左侧填充,过长则截取靠左的指定长度。 /// 将字符串统一至某个长度。
/// 过短则用 <paramref name="code"/> 将其左侧填充,过长则截取靠左的指定长度。
/// </summary> /// </summary>
public static string EnsureLength(this string? str, char code, int length) { public static string EnsureLength(this string? str, char code, int length) {
if (str == null) str = ""; if (str == null) str = "";
@@ -179,6 +182,16 @@ public static class StringExtensions {
/// <summary> /// <summary>
/// 该字符串中的字符是否均为 ASCII 字符。 /// 该字符串中的字符是否均为 ASCII 字符。
/// </summary> /// </summary>
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);
/// <summary>
/// 将字符串中的换行符统一替换为指定字符。
/// 若指定了 <paramref name="mergeMultiple"/>,会将多次换行合并成一次换行。
/// </summary>
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);
} }
+1
View File
@@ -31,6 +31,7 @@
<Using Include="System.Linq" /> <Using Include="System.Linq" />
<Using Include="System.Text" /> <Using Include="System.Text" />
<Using Include="System.IO" /> <Using Include="System.IO" />
<Using Include="System.Net" />
<Using Include="System.Threading" /> <Using Include="System.Threading" />
<Using Include="System.Runtime.CompilerServices" /> <Using Include="System.Runtime.CompilerServices" />
<Using Include="System.Runtime.InteropServices" /> <Using Include="System.Runtime.InteropServices" />
+72
View File
@@ -0,0 +1,72 @@
using System.Text.RegularExpressions;
namespace MeloongCore;
public static class EscapeUtils {
/// <summary>
/// XML 转义。
/// </summary>
public static string? XmlEscape(string? str) {
if (str is null) return null;
if (str.StartsWithF("{")) str = "{}" + str; // #4187
return str
.Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace("'", "&apos;")
.Replace("\"", "&quot;").ReplaceLineEndings("&#xa;");
}
/// <summary>
/// VB.NET 的 Like 关键字转义。
/// </summary>
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();
}
/// <summary>
/// 正则表达式转义。等同于 Regex.Escape。
/// </summary>
public static string? RegexEscape(string? value) => Regex.Escape(value);
/// <summary>
/// 正则表达式去转义。等同于 Regex.Unescape。
/// </summary>
public static string? RegexUnescape(string? value) => Regex.Unescape(value);
/// <summary>
/// URL 转义。等同于 Uri.EscapeDataString。
/// </summary>
public static string? UrlEscape(string? value) => Uri.EscapeDataString(value);
/// <summary>
/// URL 去转义。等同于 Uri.UnescapeDataString。
/// </summary>
public static string? UrlUnescape(string? value) => Uri.UnescapeDataString(value);
/// <summary>
/// 表单转义。等同于 WebUtility.UrlEncode。
/// </summary>
public static string? FormUrlEscape(string? value) => WebUtility.UrlEncode(value);
/// <summary>
/// 表单去转义。等同于 WebUtility.UrlDecode。
/// </summary>
public static string? FormUrlUnescape(string? value) => WebUtility.UrlDecode(value);
/// <summary>
/// HTML 转义。等同于 WebUtility.HtmlEncode。
/// </summary>
public static string? HtmlEscape(string? value) => WebUtility.HtmlEncode(value);
/// <summary>
/// HTML 去转义。等同于 WebUtility.HtmlDecode。
/// </summary>
public static string? HtmlUnescape(string? value) => WebUtility.HtmlDecode(value);
}