IsNear、ReadAsJson

This commit is contained in:
龙腾猫跃 committed 2026-07-10 08:23:31 +08:00
1 parent 4f7f154b25
commit 0e2d24c0cd
2 files changed
+17 -4

No files matched your search

+10 -4
View File
@@ -69,6 +69,12 @@ public static class FileUtils {
/// </summary> /// </summary>
public static JToken? ReadAsJson(string filePath, Encoding? encoding = null, Type? type = null) public static JToken? ReadAsJson(string filePath, Encoding? encoding = null, Type? type = null)
=> FileUtils.ReadAsString(filePath, encoding, type).DeserializeJson(); => FileUtils.ReadAsString(filePath, encoding, type).DeserializeJson();
/// <summary>
/// 读取 JSON 文件中的所有内容。
/// <para/>若指定了 <paramref name="type"/>,则会改为从该类型的程序集中读取嵌入的资源。
/// </summary>
public static T? ReadAsJson<T>(string filePath, Encoding? encoding = null, Type? type = null) where T : JToken
=> FileUtils.ReadAsString(filePath, encoding, type).DeserializeJson<T>();
#endregion #endregion
@@ -145,10 +151,10 @@ public static class FileUtils {
public static void Copy(string sourceFilePath, string destFilePath) { public static void Copy(string sourceFilePath, string destFilePath) {
sourceFilePath = PathUtils.ForCompare(sourceFilePath); sourceFilePath = PathUtils.ForCompare(sourceFilePath);
destFilePath = PathUtils.ForCompare(destFilePath); destFilePath = PathUtils.ForCompare(destFilePath);
if (string.Compare(sourceFilePath, destFilePath, ignoreCase:false) == 0) { if (String.CompareOrdinal(sourceFilePath, destFilePath) == 0) {
// 复制自身到自身,则不执行操作 // 复制自身到自身,则不执行操作
Logger.Trace($"复制文件到自身,不执行操作:{sourceFilePath} → {destFilePath}"); 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}"); Logger.Trace($"复制文件到自身,但大小写不同,等效于重命名文件:{sourceFilePath} → {destFilePath}");
FileUtils.Move(sourceFilePath, destFilePath); FileUtils.Move(sourceFilePath, destFilePath);
@@ -169,10 +175,10 @@ public static class FileUtils {
public static void Move(string sourceFilePath, string destFilePath) { public static void Move(string sourceFilePath, string destFilePath) {
sourceFilePath = PathUtils.ForCompare(sourceFilePath); sourceFilePath = PathUtils.ForCompare(sourceFilePath);
destFilePath = PathUtils.ForCompare(destFilePath); destFilePath = PathUtils.ForCompare(destFilePath);
if (string.Compare(sourceFilePath, destFilePath, ignoreCase: false) == 0) { if (String.CompareOrdinal(sourceFilePath, destFilePath) == 0) {
// 剪切自身到自身,则不执行操作 // 剪切自身到自身,则不执行操作
Logger.Trace($"剪切文件到自身,不执行操作:{sourceFilePath} → {destFilePath}"); 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}"); Logger.Trace($"剪切文件到自身,但大小写不同:{sourceFilePath} → {destFilePath}");
Retrier.Attempt(delay: _ => TimeSpan.FromMilliseconds(200), isRetryAllowed: ex => ex is IOException, action: _ => { Retrier.Attempt(delay: _ => TimeSpan.FromMilliseconds(200), isRetryAllowed: ex => ex is IOException, action: _ => {
+7
View File
@@ -109,6 +109,13 @@ public static class MathUtils {
return 3 * a * ((0.33333333 + y1 - y2) * a * a + (y2 - 2 * y1) * a + y1); return 3 * a * ((0.33333333 + y1 - y2) * a * a + (y2 - 2 * y1) * a + y1);
} }
/// <summary>
/// 判断其是否近似等于目标值。
/// </summary>
/// <param name="tolerance">允许的最大容差,默认为 1e-6。</param>
public static bool IsNear(this double value, double target, double tolerance = 1e-6)
=> Math.Abs(value - target) < tolerance;
/// <summary> /// <summary>
/// 对质数进行分解。 /// 对质数进行分解。
/// </summary> /// </summary>