From c39856f2d66bb5c7a316036fb646d4b8c8595b30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BE=99=E8=85=BE=E7=8C=AB=E8=B7=83?= <1043137532@qq.com> Date: Mon, 27 Apr 2026 20:40:00 +0800 Subject: [PATCH] * --- Extensions/StringExtensions.cs | 42 +++++++++++++++++++--------------- Extensions/WpfExtensions.cs | 8 +++++++ MeloongCore.csproj | 4 ++++ Utils/EscapeUtils.cs | 8 +++++++ 4 files changed, 43 insertions(+), 19 deletions(-) create mode 100644 Extensions/WpfExtensions.cs diff --git a/Extensions/StringExtensions.cs b/Extensions/StringExtensions.cs index 6274c60..f1e8015 100644 --- a/Extensions/StringExtensions.cs +++ b/Extensions/StringExtensions.cs @@ -9,7 +9,7 @@ public static class StringExtensions { /// 高速的 StartsWith。 /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static bool StartsWithF([AllowNull] this string value, string prefix, bool ignoreCase = false) { + public static bool StartsWithF(this string? value, string prefix, bool ignoreCase = false) { if (value is null) return false; return value.StartsWith(prefix, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal); } @@ -17,7 +17,7 @@ public static class StringExtensions { /// 高速的 EndsWith。 /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static bool EndsWithF([AllowNull] this string value, string suffix, bool ignoreCase = false) { + public static bool EndsWithF(this string? value, string suffix, bool ignoreCase = false) { if (value is null) return false; return value.EndsWith(suffix, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal); } @@ -155,13 +155,7 @@ public static class StringExtensions { #endregion - /// - /// 将第一个字符转换为大写,其余字符转换为小写。 - /// - public static string Capitalize(this string word) { - if (string.IsNullOrEmpty(word)) return word; - return $"{word.Substring(0, 1).Upper()}{word.Substring(1).Lower()}"; - } + #region 替换 /// /// 替换字符串中的内容。 @@ -170,6 +164,26 @@ public static class StringExtensions { public static string ReplaceOnDemand(this string str, string oldValue, Func newValue) => str.Contains(oldValue) ? str.Replace(oldValue, newValue()) : str; + /// + /// 将字符串中的换行符统一替换为指定字符。 + /// 若指定了 ,会将多次换行合并成一次换行。 + /// + 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); + + #endregion + + /// + /// 将第一个字符转换为大写,其余字符转换为小写。 + /// + public static string Capitalize(this string word) { + if (string.IsNullOrEmpty(word)) return word; + return $"{word.Substring(0, 1).Upper()}{word.Substring(1).Lower()}"; + } + /// /// 将字符串统一至某个长度。 /// 过短则用 将其左侧填充,过长则截取靠左的指定长度。 @@ -184,14 +198,4 @@ public static class StringExtensions { /// 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/Extensions/WpfExtensions.cs b/Extensions/WpfExtensions.cs new file mode 100644 index 0000000..502877f --- /dev/null +++ b/Extensions/WpfExtensions.cs @@ -0,0 +1,8 @@ +using System.Windows.Controls; + +namespace MeloongCore.Extensions; +public static class WpfExtensions { + + public static bool Any(this UIElementCollection? Arr) => Arr?.Count > 0; + +} diff --git a/MeloongCore.csproj b/MeloongCore.csproj index 2c7f735..f5cfb46 100644 --- a/MeloongCore.csproj +++ b/MeloongCore.csproj @@ -22,6 +22,10 @@ + + + + diff --git a/Utils/EscapeUtils.cs b/Utils/EscapeUtils.cs index 46d5105..94ce003 100644 --- a/Utils/EscapeUtils.cs +++ b/Utils/EscapeUtils.cs @@ -32,41 +32,49 @@ public static class EscapeUtils { /// /// 正则表达式转义。等同于 Regex.Escape。 /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static string? RegexEscape(string? value) => Regex.Escape(value); /// /// 正则表达式去转义。等同于 Regex.Unescape。 /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static string? RegexUnescape(string? value) => Regex.Unescape(value); /// /// URL 转义。等同于 Uri.EscapeDataString。 /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static string? UrlEscape(string? value) => Uri.EscapeDataString(value); /// /// URL 去转义。等同于 Uri.UnescapeDataString。 /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static string? UrlUnescape(string? value) => Uri.UnescapeDataString(value); /// /// 表单转义。等同于 WebUtility.UrlEncode。 /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static string? FormUrlEscape(string? value) => WebUtility.UrlEncode(value); /// /// 表单去转义。等同于 WebUtility.UrlDecode。 /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static string? FormUrlUnescape(string? value) => WebUtility.UrlDecode(value); /// /// HTML 转义。等同于 WebUtility.HtmlEncode。 /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static string? HtmlEscape(string? value) => WebUtility.HtmlEncode(value); /// /// HTML 去转义。等同于 WebUtility.HtmlDecode。 /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static string? HtmlUnescape(string? value) => WebUtility.HtmlDecode(value); } \ No newline at end of file