diff --git a/Shared/Utils/FileUtils.cs b/Shared/Utils/FileUtils.cs index 471bf4a..3bf5d43 100644 --- a/Shared/Utils/FileUtils.cs +++ b/Shared/Utils/FileUtils.cs @@ -69,6 +69,12 @@ public static class FileUtils { /// public static JToken? ReadAsJson(string filePath, Encoding? encoding = null, Type? type = null) => FileUtils.ReadAsString(filePath, encoding, type).DeserializeJson(); + /// + /// 读取 JSON 文件中的所有内容。 + /// 若指定了 ,则会改为从该类型的程序集中读取嵌入的资源。 + /// + public static T? ReadAsJson(string filePath, Encoding? encoding = null, Type? type = null) where T : JToken + => FileUtils.ReadAsString(filePath, encoding, type).DeserializeJson(); #endregion @@ -145,10 +151,10 @@ public static class FileUtils { public static void Copy(string sourceFilePath, string destFilePath) { sourceFilePath = PathUtils.ForCompare(sourceFilePath); destFilePath = PathUtils.ForCompare(destFilePath); - if (string.Compare(sourceFilePath, destFilePath, ignoreCase:false) == 0) { + if (String.CompareOrdinal(sourceFilePath, destFilePath) == 0) { // 复制自身到自身,则不执行操作 Logger.Trace($"复制文件到自身,不执行操作:{sourceFilePath} → {destFilePath}"); - } else if (string.Compare(sourceFilePath, destFilePath, ignoreCase:true) == 0) { + } else if (String.Compare(sourceFilePath, destFilePath, StringComparison.OrdinalIgnoreCase) == 0) { // 路径仅大小写不同,等效于重命名 Logger.Trace($"复制文件到自身,但大小写不同,等效于重命名文件:{sourceFilePath} → {destFilePath}"); FileUtils.Move(sourceFilePath, destFilePath); @@ -169,10 +175,10 @@ public static class FileUtils { public static void Move(string sourceFilePath, string destFilePath) { sourceFilePath = PathUtils.ForCompare(sourceFilePath); destFilePath = PathUtils.ForCompare(destFilePath); - if (string.Compare(sourceFilePath, destFilePath, ignoreCase: false) == 0) { + if (String.CompareOrdinal(sourceFilePath, destFilePath) == 0) { // 剪切自身到自身,则不执行操作 Logger.Trace($"剪切文件到自身,不执行操作:{sourceFilePath} → {destFilePath}"); - } else if (string.Compare(sourceFilePath, destFilePath, ignoreCase: true) == 0) { + } else if (String.Compare(sourceFilePath, destFilePath, StringComparison.OrdinalIgnoreCase) == 0) { // 路径仅大小写不同 Logger.Trace($"剪切文件到自身,但大小写不同:{sourceFilePath} → {destFilePath}"); Retrier.Attempt(delay: _ => TimeSpan.FromMilliseconds(200), isRetryAllowed: ex => ex is IOException, action: _ => { diff --git a/Shared/Utils/MathUtils.cs b/Shared/Utils/MathUtils.cs index 10416fc..8ad7b6c 100644 --- a/Shared/Utils/MathUtils.cs +++ b/Shared/Utils/MathUtils.cs @@ -108,6 +108,13 @@ public static class MathUtils { } while (!(Math.Abs(b - t) < accuracy) && maxIterations > 0); return 3 * a * ((0.33333333 + y1 - y2) * a * a + (y2 - 2 * y1) * a + y1); } + + /// + /// 判断其是否近似等于目标值。 + /// + /// 允许的最大容差,默认为 1e-6。 + public static bool IsNear(this double value, double target, double tolerance = 1e-6) + => Math.Abs(value - target) < tolerance; /// /// 对质数进行分解。