From 49c2a9c8ffb6c66979a0d43e61e42f9d95d39c6e 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: Thu, 7 May 2026 07:33:06 +0800 Subject: [PATCH] format --- Shared/Extensions/EnumerableExtensions.cs | 7 +- Shared/Extensions/StringExtensions.cs | 90 ++++++++++------------- Shared/General/ConcurrentList.cs | 25 +++---- Shared/General/Logger.cs | 70 ++++++++++-------- Shared/General/MsDelta.cs | 2 +- Shared/Utils/DirectoryUtils.cs | 10 +-- Shared/Utils/EscapeUtils.cs | 48 ++++++++---- Shared/Utils/FileUtils.cs | 27 +++---- Shared/Utils/HttpUtils.cs | 4 - Shared/Utils/MathUtils.cs | 14 ++-- Shared/Utils/PathUtils.cs | 18 ++--- 11 files changed, 161 insertions(+), 154 deletions(-) diff --git a/Shared/Extensions/EnumerableExtensions.cs b/Shared/Extensions/EnumerableExtensions.cs index 0041430..df4a368 100644 --- a/Shared/Extensions/EnumerableExtensions.cs +++ b/Shared/Extensions/EnumerableExtensions.cs @@ -166,7 +166,8 @@ public static class EnumerableExtensions { /// 从 中移除具有指定键的元素。 /// 返回是否确实移除了元素。 /// - public static bool Remove(this ConcurrentDictionary dict, TKey key) => dict.TryRemove(key, out _); + public static bool Remove(this ConcurrentDictionary dict, TKey key) + => dict.TryRemove(key, out _); /// /// 将二维数组转换为字典。 @@ -182,8 +183,8 @@ public static class EnumerableExtensions { /// /// 尝试从字典中获取某项,如果该项不存在,则返回默认值。 /// - public static TValue GetOrDefault(this IDictionary dict, TKey key, TValue defaultValue = default!) => - dict.TryGetValue(key, out var result) ? result : defaultValue; + public static TValue GetOrDefault(this IDictionary dict, TKey key, TValue defaultValue = default!) + => dict.TryGetValue(key, out var result) ? result : defaultValue; /// /// 将值添加到字典的对应项的列表中。 diff --git a/Shared/Extensions/StringExtensions.cs b/Shared/Extensions/StringExtensions.cs index b032307..e272cb6 100644 --- a/Shared/Extensions/StringExtensions.cs +++ b/Shared/Extensions/StringExtensions.cs @@ -6,86 +6,76 @@ public static class StringExtensions { #region 区域性 / 大小写 简化 /// - /// 高速的 StartsWith。 + /// 非区域性的 。 /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - 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); - } + public static bool StartsWithF(this string? value, string prefix, bool ignoreCase = false) + => value?.StartsWith(prefix, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal) ?? false; /// - /// 高速的 StartsWith。 + /// 判断字符串是否以 开头。 /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static bool StartsWithF(this string? value, char prefix, bool ignoreCase = false) { - if (value is null) return false; - return value.StartsWith(prefix.ToString(), ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal); - } + public static bool StartsWithF(this string? value, char prefix, bool ignoreCase = false) + => value?.StartsWith(prefix.ToString(), ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal) ?? false; /// - /// 高速的 EndsWith。 + /// 非区域性的 。 /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - 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); - } + public static bool EndsWithF(this string? value, string suffix, bool ignoreCase = false) + => value?.EndsWith(suffix, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal) ?? false; /// - /// 高速的 EndsWith。 + /// 判断字符串是否以 结尾。 /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static bool EndsWithF(this string? value, char suffix, bool ignoreCase = false) { - if (value is null) return false; - return value.EndsWith(suffix.ToString(), ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal); - } + public static bool EndsWithF(this string? value, char suffix, bool ignoreCase = false) + => value?.EndsWith(suffix.ToString(), ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal) ?? false; /// - /// 忽略大小写的 Contains。 + /// 忽略大小写的 。 /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static bool ContainsIgnoreCase(this string value, string subString) { - return value.IndexOf(subString, StringComparison.OrdinalIgnoreCase) >= 0; - } + public static bool ContainsIgnoreCase(this string value, string subString) + => value.IndexOf(subString, StringComparison.OrdinalIgnoreCase) >= 0; + /// - /// 高速的 IndexOf。 + /// 非区域性的 。 /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static int IndexOfF(this string value, string subString, bool ignoreCase = false) { - return value.IndexOf(subString, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal); - } + public static int IndexOfF(this string value, string subString, bool ignoreCase = false) + => value.IndexOf(subString, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal); /// - /// 高速的 IndexOf。 + /// 非区域性的 。 /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static int IndexOfF(this string value, string subString, int startIndex, bool ignoreCase = false) { - return value.IndexOf(subString, startIndex, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal); - } + public static int IndexOfF(this string value, string subString, int startIndex, bool ignoreCase = false) + => value.IndexOf(subString, startIndex, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal); /// - /// 高速的 LastIndexOf。 + /// 非区域性的 。 /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static int LastIndexOfF(this string value, string subString, bool ignoreCase = false) { - return value.LastIndexOf(subString, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal); - } + public static int LastIndexOfF(this string value, string subString, bool ignoreCase = false) + => value.LastIndexOf(subString, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal); /// - /// 高速的 LastIndexOf。 + /// 非区域性的 。 /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static int LastIndexOfF(this string value, string subString, int startIndex, bool ignoreCase = false) { - return value.LastIndexOf(subString, startIndex, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal); - } + public static int LastIndexOfF(this string value, string subString, int startIndex, bool ignoreCase = false) + => value.LastIndexOf(subString, startIndex, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal); /// - /// ToLowerInvariant 的简略写法。 + /// 的简略写法。 /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static string Lower(this string str) => str.ToLowerInvariant(); + public static string Lower(this string str) + => str.ToLowerInvariant(); /// - /// ToUpperInvariant 的简略写法。 + /// 的简略写法。 /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static string Upper(this string str) => str.ToUpperInvariant(); + public static string Upper(this string str) + => str.ToUpperInvariant(); #endregion @@ -172,16 +162,15 @@ public static class StringExtensions { /// 替换字符串中的内容。 /// 仅当需要替换时,才调用 获取结果字符串。 /// - public static string Replace(this string str, string oldValue, Func newValueGetter) => - str.Contains(oldValue) ? str.Replace(oldValue, newValueGetter()) : str; + public static string Replace(this string str, string oldValue, Func newValueGetter) + => str.Contains(oldValue) ? str.Replace(oldValue, newValueGetter()) : str; /// /// 将字符串中的换行符统一替换为指定字符。 /// 若指定了 ,会将多个连续换行符仅替换为一个目标内容。 /// - public static string ReplaceLineEndings(this string input, string newValue, bool mergeMultiple = false) { - return (mergeMultiple ? regexLineEndingAndMerge : regexLineEnding).Replace(input, newValue.Replace("$", "$$")); // 避免识别成捕获组 - } + public static string ReplaceLineEndings(this string input, string newValue, bool mergeMultiple = false) + => (mergeMultiple ? regexLineEndingAndMerge : regexLineEnding).Replace(input, newValue.Replace("$", "$$")); // 避免识别成捕获组 private static readonly Regex regexLineEndingAndMerge = new(@"(?:\r\n|[\n\r\f\u0085\u2028\u2029])+", RegexOptions.Compiled); private static readonly Regex regexLineEnding = new(@"\r\n|[\n\r\f\u0085\u2028\u2029]", RegexOptions.Compiled); @@ -207,6 +196,7 @@ public static class StringExtensions { /// /// 该字符串中的字符是否均为 ASCII 字符。 /// - public static bool IsAsciiOnly(this string input) => input.All(c => c < 128); + public static bool IsAsciiOnly(this string input) + => input.All(c => c < 128); } diff --git a/Shared/General/ConcurrentList.cs b/Shared/General/ConcurrentList.cs index 276428a..d03a3fd 100644 --- a/Shared/General/ConcurrentList.cs +++ b/Shared/General/ConcurrentList.cs @@ -1,8 +1,7 @@ namespace MeloongCore; /// -/// 线程安全的 实现。 -/// 当调用枚举器时返回一个临时副本,以避免列表在枚举过程中被修改导致异常。 +/// 使用粗粒度锁的线程安全 实现。 /// public class ConcurrentList : IList, IList, IEnumerable, IEnumerable { @@ -11,21 +10,19 @@ public class ConcurrentList : IList, IList, IEnumerable, IEnumerable { // 构造函数 public ConcurrentList() { } - public ConcurrentList(IEnumerable data) => _items = new List(data); + public ConcurrentList(IEnumerable data) => _items = new(data); public static implicit operator ConcurrentList(List data) => new(data); - public static implicit operator List(ConcurrentList data) { lock (data.syncLock) { return new List(data._items); } } + public static implicit operator List(ConcurrentList data) { lock (data.syncLock) { return new(data._items); } } // 枚举器 - public IEnumerator GetEnumerator() { - lock (syncLock) { - return _items.ToList().GetEnumerator(); - } - } - IEnumerator IEnumerable.GetEnumerator() { - lock (syncLock) { - return _items.ToList().GetEnumerator(); - } - } + /// + /// 返回此列表的副本。 + /// + public IEnumerator GetEnumerator() { lock (syncLock) return _items.ToList().GetEnumerator(); } + /// + /// 返回此列表的副本。 + /// + IEnumerator IEnumerable.GetEnumerator() { lock (syncLock) return _items.ToList().GetEnumerator(); } // 成员 public int Count { get { lock (syncLock) { return _items.Count; } } } diff --git a/Shared/General/Logger.cs b/Shared/General/Logger.cs index be13b33..2101db4 100644 --- a/Shared/General/Logger.cs +++ b/Shared/General/Logger.cs @@ -6,7 +6,7 @@ namespace MeloongCore; /// /// 日志等级。 /// -public enum LogLevels { +public enum LogLevel { /// /// 追踪。 /// @@ -28,7 +28,7 @@ public enum LogLevels { /// /// 输出日志时执行的错误汇报行为。 /// -public enum LogBehaviors { +public enum LogBehavior { /// /// 无提示。 /// @@ -59,41 +59,53 @@ public static class Logger { public static BaseLogger Instance { get; set; } = new(); // 转发给实例的方法包装 - public static void Log(string message, LogLevels level = LogLevels.Info, LogBehaviors behavior = LogBehaviors.None, [CallerFilePath] string filePath = "") + public static void Log(string message, LogLevel level = LogLevel.Info, LogBehavior behavior = LogBehavior.None, [CallerFilePath] string filePath = "") => Instance.Log(message, level, behavior, filePath); - public static void Trace(string message, LogBehaviors behavior = LogBehaviors.None, [CallerFilePath] string filePath = "") => Log(message, LogLevels.Trace, behavior, filePath); - public static void Info(string message, LogBehaviors behavior = LogBehaviors.None, [CallerFilePath] string filePath = "") => Log(message, LogLevels.Info, behavior, filePath); - public static void Warning(string message, LogBehaviors behavior = LogBehaviors.ToastIfDebug, [CallerFilePath] string filePath = "") => Log(message, LogLevels.Warning, behavior, filePath); - public static void Error(string message, LogBehaviors behavior = LogBehaviors.AlertThenFeedback, [CallerFilePath] string filePath = "") => Log(message, LogLevels.Error, behavior, filePath); + public static void Trace(string message, LogBehavior behavior = LogBehavior.None, [CallerFilePath] string filePath = "") + => Log(message, LogLevel.Trace, behavior, filePath); + public static void Info(string message, LogBehavior behavior = LogBehavior.None, [CallerFilePath] string filePath = "") + => Log(message, LogLevel.Info, behavior, filePath); + public static void Warning(string message, LogBehavior behavior = LogBehavior.ToastIfDebug, [CallerFilePath] string filePath = "") + => Log(message, LogLevel.Warning, behavior, filePath); + public static void Error(string message, LogBehavior behavior = LogBehavior.AlertThenFeedback, [CallerFilePath] string filePath = "") + => Log(message, LogLevel.Error, behavior, filePath); - public static void Log(Func messageGetter, LogLevels level = LogLevels.Info, LogBehaviors behavior = LogBehaviors.None, [CallerFilePath] string filePath = "") { + public static void Log(Func messageGetter, LogLevel level = LogLevel.Info, LogBehavior behavior = LogBehavior.None, [CallerFilePath] string filePath = "") { if (Instance.MinLevel <= level) Instance.Log(messageGetter(), level, behavior, filePath); } - public static void Trace(Func messageGetter, LogBehaviors behavior = LogBehaviors.None, [CallerFilePath] string filePath = "") => Log(messageGetter, LogLevels.Trace, behavior, filePath); - public static void Info(Func messageGetter, LogBehaviors behavior = LogBehaviors.None, [CallerFilePath] string filePath = "") => Log(messageGetter, LogLevels.Info, behavior, filePath); - public static void Warning(Func messageGetter, LogBehaviors behavior = LogBehaviors.ToastIfDebug, [CallerFilePath] string filePath = "") => Log(messageGetter, LogLevels.Warning, behavior, filePath); - public static void Error(Func messageGetter, LogBehaviors behavior = LogBehaviors.AlertThenFeedback, [CallerFilePath] string filePath = "") => Log(messageGetter, LogLevels.Error, behavior, filePath); + public static void Trace(Func messageGetter, LogBehavior behavior = LogBehavior.None, [CallerFilePath] string filePath = "") + => Log(messageGetter, LogLevel.Trace, behavior, filePath); + public static void Info(Func messageGetter, LogBehavior behavior = LogBehavior.None, [CallerFilePath] string filePath = "") + => Log(messageGetter, LogLevel.Info, behavior, filePath); + public static void Warning(Func messageGetter, LogBehavior behavior = LogBehavior.ToastIfDebug, [CallerFilePath] string filePath = "") + => Log(messageGetter, LogLevel.Warning, behavior, filePath); + public static void Error(Func messageGetter, LogBehavior behavior = LogBehavior.AlertThenFeedback, [CallerFilePath] string filePath = "") + => Log(messageGetter, LogLevel.Error, behavior, filePath); - public static void Log(Exception ex, string? message = null, LogLevels level = LogLevels.Warning, LogBehaviors behavior = LogBehaviors.ToastIfDebug, [CallerFilePath] string filePath = "") + public static void Log(Exception ex, string? message = null, LogLevel level = LogLevel.Warning, LogBehavior behavior = LogBehavior.ToastIfDebug, [CallerFilePath] string filePath = "") => Instance.Log(ex, message, level, behavior, filePath); - public static void Trace(Exception ex, string? message = null, LogBehaviors behavior = LogBehaviors.None, [CallerFilePath] string filePath = "") => Log(ex, message, LogLevels.Trace, behavior, filePath); - public static void Info(Exception ex, string? message = null, LogBehaviors behavior = LogBehaviors.None, [CallerFilePath] string filePath = "") => Log(ex, message, LogLevels.Info, behavior, filePath); - public static void Warning(Exception ex, string? message = null, LogBehaviors behavior = LogBehaviors.ToastIfDebug, [CallerFilePath] string filePath = "") => Log(ex, message, LogLevels.Warning, behavior, filePath); - public static void Error(Exception ex, string? message = null, LogBehaviors behavior = LogBehaviors.AlertThenFeedback, [CallerFilePath] string filePath = "") => Log(ex, message, LogLevels.Error, behavior, filePath); + public static void Trace(Exception ex, string? message = null, LogBehavior behavior = LogBehavior.None, [CallerFilePath] string filePath = "") + => Log(ex, message, LogLevel.Trace, behavior, filePath); + public static void Info(Exception ex, string? message = null, LogBehavior behavior = LogBehavior.None, [CallerFilePath] string filePath = "") + => Log(ex, message, LogLevel.Info, behavior, filePath); + public static void Warning(Exception ex, string? message = null, LogBehavior behavior = LogBehavior.ToastIfDebug, [CallerFilePath] string filePath = "") + => Log(ex, message, LogLevel.Warning, behavior, filePath); + public static void Error(Exception ex, string? message = null, LogBehavior behavior = LogBehavior.AlertThenFeedback, [CallerFilePath] string filePath = "") + => Log(ex, message, LogLevel.Error, behavior, filePath); } /// /// 最基础的日志实现。 -/// 仅将日志输出到 ,不实现 。 +/// 仅将日志输出到 ,不实现 。 /// public class BaseLogger { /// /// 最低日志输出等级,低于此等级的日志将被忽略。 /// - public LogLevels MinLevel { get; set; } = LogLevels.Trace; + public LogLevel MinLevel { get; set; } = LogLevel.Trace; // 核心方法 - public virtual void Log(string message, LogLevels level, LogBehaviors behavior, string filePath) { + public virtual void Log(string message, LogLevel level, LogBehavior behavior, string filePath) { if (MinLevel > level) return; try { var formattedMessage = Format(message, level, filePath, null); @@ -101,7 +113,7 @@ public class BaseLogger { HandleBehavior(message, formattedMessage, behavior, null); } catch {} } - public virtual void Log(Exception ex, string? message, LogLevels level, LogBehaviors behavior, string filePath) { + public virtual void Log(Exception ex, string? message, LogLevel level, LogBehavior behavior, string filePath) { if (MinLevel > level) return; if (ex is ThreadInterruptedException) return; try { @@ -115,7 +127,7 @@ public class BaseLogger { /// /// 格式化日志文本。 /// - protected virtual string Format(string text, LogLevels level, string filePath, Exception? ex) { + protected virtual string Format(string text, LogLevel level, string filePath, Exception? ex) { string prefix = $"{DateTime.Now:HH':'mm':'ss'.'fff} {level.ToString().First()} {(Thread.CurrentThread.Name is null ? "" : $"<{Thread.CurrentThread.Name}> ")}[{Path.GetFileName(filePath).BeforeFirst(".")}] "; return text .ReplaceLineEndings("\n", mergeMultiple: true).Split(['\n'], StringSplitOptions.RemoveEmptyEntries) @@ -126,26 +138,26 @@ public class BaseLogger { /// /// 输出格式化后的日志文本。 /// - protected virtual void Output(string formattedMessage, LogLevels level) { + protected virtual void Output(string formattedMessage, LogLevel level) { Debug.WriteLine(formattedMessage); } /// - /// 在调用 Log 方法的线程执行 。 + /// 在调用 Log 方法的线程执行 。 /// - protected virtual void HandleBehavior(string? rawMessage, string formattedMessage, LogBehaviors behavior, Exception? ex) { + protected virtual void HandleBehavior(string? rawMessage, string formattedMessage, LogBehavior behavior, Exception? ex) { } } /// /// 将日志输出到 并写入指定的文件夹,最多保留 5 个文件。 -/// 不实现 。 +/// 不实现 。 /// public class FileLogger : BaseLogger, IDisposable { /// - protected override void Output(string formattedText, LogLevels level) { + protected override void Output(string formattedText, LogLevel level) { base.Output(formattedText, level); if (!writerAvaliable && queue.Count >= 100) return; // 在 writer 就绪前,最多缓存 100 条日志 queue.Enqueue(formattedText); @@ -187,7 +199,7 @@ public class FileLogger : BaseLogger, IDisposable { FileUtils.Delete(olderFile); } } catch (IOException ex) { - Logger.Warning(ex, "可能同时开启了多个程序,这或许会导致未知问题", LogBehaviors.Toast); + Logger.Warning(ex, "可能同时开启了多个程序,这或许会导致未知问题", LogBehavior.Toast); } catch (Exception ex) { Logger.Warning(ex, "整理日志文件失败"); } @@ -201,7 +213,7 @@ public class FileLogger : BaseLogger, IDisposable { Flush(); } } catch (Exception ex) { - Logger.Warning(ex, "写入日志文件失败", LogBehaviors.Toast); + Logger.Warning(ex, "写入日志文件失败", LogBehavior.Toast); writerAvaliable = false; } }) { Name = nameof(FileLogger), Priority = ThreadPriority.Lowest, IsBackground = true }; diff --git a/Shared/General/MsDelta.cs b/Shared/General/MsDelta.cs index 945cad2..5b4417b 100644 --- a/Shared/General/MsDelta.cs +++ b/Shared/General/MsDelta.cs @@ -39,7 +39,7 @@ public static class MsDelta { throw new Win32Exception(); } - + // API [StructLayout(LayoutKind.Sequential)] private struct DeltaInput { public IntPtr Start; diff --git a/Shared/Utils/DirectoryUtils.cs b/Shared/Utils/DirectoryUtils.cs index 40b11bf..01a6cea 100644 --- a/Shared/Utils/DirectoryUtils.cs +++ b/Shared/Utils/DirectoryUtils.cs @@ -16,16 +16,14 @@ public static class DirectoryUtils { /// /// 判断文件夹是否存在。 /// - public static bool Exists(string path) { - return Directory.Exists(PathUtils.WithLongPath(path)); - } + public static bool Exists(string path) + => Directory.Exists(PathUtils.WithLongPath(path)); /// /// 创建 对象。 /// - public static DirectoryInfo GetInfo(string path) { - return new(PathUtils.WithLongPath(path)); - } + public static DirectoryInfo GetInfo(string path) + => new(PathUtils.WithLongPath(path)); /// /// 返回指定路径下的所有文件,不以 \\?\ 开头。 diff --git a/Shared/Utils/EscapeUtils.cs b/Shared/Utils/EscapeUtils.cs index 94ce003..244c496 100644 --- a/Shared/Utils/EscapeUtils.cs +++ b/Shared/Utils/EscapeUtils.cs @@ -30,51 +30,67 @@ public static class EscapeUtils { } /// - /// 正则表达式转义。等同于 Regex.Escape。 + /// 正则表达式转义。 + /// 等同于 。 /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static string? RegexEscape(string? value) => Regex.Escape(value); + public static string? RegexEscape(string? value) + => Regex.Escape(value); /// - /// 正则表达式去转义。等同于 Regex.Unescape。 + /// 正则表达式去转义。 + /// 等同于 。 /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static string? RegexUnescape(string? value) => Regex.Unescape(value); + public static string? RegexUnescape(string? value) + => Regex.Unescape(value); /// - /// URL 转义。等同于 Uri.EscapeDataString。 + /// URL 转义。 + /// 等同于 。 /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static string? UrlEscape(string? value) => Uri.EscapeDataString(value); + public static string? UrlEscape(string? value) + => Uri.EscapeDataString(value); /// - /// URL 去转义。等同于 Uri.UnescapeDataString。 + /// URL 去转义。 + /// 等同于 。 /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static string? UrlUnescape(string? value) => Uri.UnescapeDataString(value); + public static string? UrlUnescape(string? value) + => Uri.UnescapeDataString(value); /// - /// 表单转义。等同于 WebUtility.UrlEncode。 + /// 表单转义。 + /// 等同于 。 /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static string? FormUrlEscape(string? value) => WebUtility.UrlEncode(value); + public static string? FormUrlEscape(string? value) + => WebUtility.UrlEncode(value); /// - /// 表单去转义。等同于 WebUtility.UrlDecode。 + /// 表单去转义。 + /// 等同于 。 /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static string? FormUrlUnescape(string? value) => WebUtility.UrlDecode(value); + public static string? FormUrlUnescape(string? value) + => WebUtility.UrlDecode(value); /// - /// HTML 转义。等同于 WebUtility.HtmlEncode。 + /// HTML 转义。 + /// 等同于 。 /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static string? HtmlEscape(string? value) => WebUtility.HtmlEncode(value); + public static string? HtmlEscape(string? value) + => WebUtility.HtmlEncode(value); /// - /// HTML 去转义。等同于 WebUtility.HtmlDecode。 + /// HTML 去转义。 + /// 等同于 。 /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static string? HtmlUnescape(string? value) => WebUtility.HtmlDecode(value); + public static string? HtmlUnescape(string? value) + => WebUtility.HtmlDecode(value); } \ No newline at end of file diff --git a/Shared/Utils/FileUtils.cs b/Shared/Utils/FileUtils.cs index 886b85f..2584e21 100644 --- a/Shared/Utils/FileUtils.cs +++ b/Shared/Utils/FileUtils.cs @@ -9,17 +9,15 @@ public static class FileUtils { /// 写入文件。 /// 如果文件或文件夹不存在,则会自动创建。若已存在,则会覆盖原文件。 /// - public static void Write(string filePath, string text, Encoding? encoding = null) { - Write(filePath, (encoding ?? new UTF8Encoding()).GetBytes(text)); - } + public static void Write(string filePath, string text, Encoding? encoding = null) + => Write(filePath, (encoding ?? new UTF8Encoding()).GetBytes(text)); /// /// 写入文件。 /// 如果文件或文件夹不存在,则会自动创建。若已存在,则会覆盖原文件。 /// - public static void Write(string filePath, IEnumerable content) { - Write(filePath, [..content]); - } + public static void Write(string filePath, IEnumerable content) + => Write(filePath, [.. content]); /// /// 写入文件。 @@ -94,7 +92,7 @@ public static class FileUtils { internalEx?.Throw(); } } - // 用于删除到回收站的接口 + // API [DllImport("shell32.dll", CharSet = CharSet.Unicode)] static extern int SHCreateItemFromParsingName([MarshalAs(UnmanagedType.LPWStr)] string pszPath, IntPtr pbc, ref Guid riid, [MarshalAs(UnmanagedType.Interface)] out IShellItem ppv); [ComImport, Guid("43826D1E-E718-42EE-BC55-A1E261C37BFE"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] @@ -132,9 +130,8 @@ public static class FileUtils { /// /// 打开该文件的只读 。 /// - public static FileStream ReadAsStream(string filePath) { - return new(PathUtils.WithLongPath(filePath), FileMode.Open, FileAccess.Read, FileShare.ReadWrite); - } + public static FileStream ReadAsStream(string filePath) + => new(PathUtils.WithLongPath(filePath), FileMode.Open, FileAccess.Read, FileShare.ReadWrite); /// /// 在指定路径创建文件,并打开 。 @@ -260,16 +257,14 @@ public static class FileUtils { /// /// 确定指定的文件是否存在。 /// - public static bool Exists(string filePath) { - return File.Exists(PathUtils.WithLongPath(filePath)); - } + public static bool Exists(string filePath) + => File.Exists(PathUtils.WithLongPath(filePath)); /// /// 创建 对象。 /// - public static FileInfo GetInfo(string path) { - return new(PathUtils.WithLongPath(path)); - } + public static FileInfo GetInfo(string path) + => new(PathUtils.WithLongPath(path)); /// /// 复制文件。 diff --git a/Shared/Utils/HttpUtils.cs b/Shared/Utils/HttpUtils.cs index 8298e31..086e7b0 100644 --- a/Shared/Utils/HttpUtils.cs +++ b/Shared/Utils/HttpUtils.cs @@ -13,7 +13,3 @@ public class HttpRequestCodeException(string message, HttpStatusCode statusCode, /// public string? Response { get; set; } = response; } - -public static class HttpUtils { - -} diff --git a/Shared/Utils/MathUtils.cs b/Shared/Utils/MathUtils.cs index aad6321..1e7fb4d 100644 --- a/Shared/Utils/MathUtils.cs +++ b/Shared/Utils/MathUtils.cs @@ -30,17 +30,21 @@ public static class MathUtils { /// 转换为 2 到 86 进制的字符串。 /// 进制参考:含大写字母 36,含大小写字母 62,含大小写字母和特殊符号 86。 /// - public static string ConvertRadix(this long input, int toRadix) => ConvertRadix((BigInteger) input, toRadix); + public static string ConvertRadix(this long input, int toRadix) + => ConvertRadix((BigInteger) input, toRadix); /// /// 转换为 2 到 86 进制的字符串。 /// 进制参考:含大写字母 36,含大小写字母 62,含大小写字母和特殊符号 86。 /// - public static string ConvertRadix(this int input, int toRadix) => ConvertRadix((BigInteger) input, toRadix); + public static string ConvertRadix(this int input, int toRadix) + => ConvertRadix((BigInteger) input, toRadix); /// /// 转换为 2 到 86 进制的字符串。 /// 进制参考:含大写字母 36,含大小写字母 62,含大小写字母和特殊符号 86。 /// - public static string ConvertRadix(this byte input, int toRadix) => ConvertRadix((BigInteger) input, toRadix); + public static string ConvertRadix(this byte input, int toRadix) + => ConvertRadix((BigInteger) input, toRadix); + private static string ConvertRadix(this BigInteger input, int toRadix) { const string digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/+=!?@#$%^&*()[]{}<>;:',"; if (toRadix < 2 || toRadix > digits.Length) throw new ArgumentOutOfRangeException(nameof(toRadix), $"{nameof(toRadix)} must be between 2 and 86."); @@ -76,8 +80,8 @@ public static class MathUtils { /// 在一个范围中进行线性插值。 /// 返回值将被舍入到小数点后 7 位,以解决浮点数误差问题。 /// - public static double Lerp(double start, double end, double percentage) => - Math.Round(start + (end - start) * percentage, 7); + public static double Lerp(double start, double end, double percentage) + => Math.Round(start + (end - start) * percentage, 7); /// /// 计算二阶贝塞尔曲线。 diff --git a/Shared/Utils/PathUtils.cs b/Shared/Utils/PathUtils.cs index 7f74c6c..212501e 100644 --- a/Shared/Utils/PathUtils.cs +++ b/Shared/Utils/PathUtils.cs @@ -6,16 +6,14 @@ public static class PathUtils { /// /// 确保路径的结尾包含文件夹分隔符。 /// - public static string WithSeparator(string folder) { - return folder.EndsWithF(Path.DirectorySeparatorChar) ? folder : folder + Path.DirectorySeparatorChar; - } + public static string WithSeparator(string folder) + => folder.EndsWithF(Path.DirectorySeparatorChar) ? folder : folder + Path.DirectorySeparatorChar; /// /// 确保路径的结尾不包含文件夹分隔符。 /// - public static string WithoutSeparator(string folder) { - return folder.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); - } + public static string WithoutSeparator(string folder) + => folder.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); #endregion @@ -59,7 +57,7 @@ public static class PathUtils { private static extern int GetShortPathNameW(string lpszLongPath, [Out] char[] buffer, int cchBuffer); /// - /// 将完整路径转换为以 \\?\ 开头的标准长路径格式。 + /// 将完整路径转换为以 \\?\ 开头的标准长路径格式。 /// 这会去除路径末尾的分隔符,且将 / 替换为 \。 /// public static string WithLongPath(string path) { @@ -74,10 +72,10 @@ public static class PathUtils { } /// - /// 去除路径开头的 \\?\。 + /// 去除路径开头的 \\?\。 /// - public static string WithoutLongPath(string path) => - path.AfterLast(@"\\?\"); + public static string WithoutLongPath(string path) + => path.AfterLast(@"\\?\"); #endregion