龙腾猫跃 5 месяцев назад
Родитель
Сommit
b26e2c77e6
3 измененных файлов с 29 добавлено и 15 удалено
  1. 12 12
      Shared/Extensions/StringExtensions.cs
  2. 1 1
      Shared/Utils/DirectoryUtils.cs
  3. 16 2
      Shared/Utils/PathUtils.cs

+ 12 - 12
Shared/Extensions/StringExtensions.cs

@@ -94,38 +94,38 @@ public static class StringExtensions {
     }
 
     /// <summary>
-    /// 获取在子字符串第一次出现之前的部分,例如对 2024/11/08 拆切 / 会得到 2024。
-    /// 如果未找到子字符串则不裁切。
+    /// 获取在子字符串第一次出现之前的部分,如果未找到子字符串则不裁切。
+    /// <code>"2024/11/08".BeforeFirst("/") => "2024"</code>
     /// </summary>
-    public static string BeforeFirst(this string str, [AllowNull] string text, bool ignoreCase = false) {
+    public static string BeforeFirst(this string str, string text, bool ignoreCase = false) {
         int pos = string.IsNullOrEmpty(text) ? -1 : str.IndexOfF(text!, ignoreCase);
         if (pos < 0) return str;
         return str.Substring(0, pos);
     }
     /// <summary>
-    /// 获取在子字符串最后一次出现之前的部分,例如对 2024/11/08 拆切 / 会得到 2024/11。
-    /// 如果未找到子字符串则不裁切。
+    /// 获取在子字符串最后一次出现之前的部分,如果未找到子字符串则不裁切。
+    /// <code>"2024/11/08".BeforeLast("/") => "2024/11"</code>
     /// </summary>
-    public static string BeforeLast(this string str, [AllowNull] string text, bool ignoreCase = false) {
+    public static string BeforeLast(this string str, string text, bool ignoreCase = false) {
         int pos = string.IsNullOrEmpty(text) ? -1 : str.LastIndexOfF(text!, ignoreCase);
         if (pos < 0) return str;
         return str.Substring(0, pos);
     }
 
     /// <summary>
-    /// 获取在子字符串第一次出现之后的部分,例如对 2024/11/08 拆切 / 会得到 11/08。
-    /// 如果未找到子字符串则不裁切。
+    /// 获取在子字符串第一次出现之后的部分,如果未找到子字符串则不裁切。
+    /// <code>"2024/11/08".AfterFirst("/") => "11/08"</code>
     /// </summary>
-    public static string AfterFirst(this string str, [AllowNull] string text, bool ignoreCase = false) {
+    public static string AfterFirst(this string str, string text, bool ignoreCase = false) {
         int pos = string.IsNullOrEmpty(text) ? -1 : str.IndexOfF(text!, ignoreCase);
         if (pos < 0) return str;
         return str.Substring(pos + text!.Length);
     }
     /// <summary>
-    /// 获取在子字符串最后一次出现之后的部分,例如对 2024/11/08 拆切 / 会得到 08。
-    /// 如果未找到子字符串则不裁切。
+    /// 获取在子字符串最后一次出现之后的部分,如果未找到子字符串则不裁切。
+    /// <code>"2024/11/08".AfterLast("/") => "08"</code>
     /// </summary>
-    public static string AfterLast(this string str, [AllowNull] string text, bool ignoreCase = false) {
+    public static string AfterLast(this string str, string text, bool ignoreCase = false) {
         int pos = string.IsNullOrEmpty(text) ? -1 : str.LastIndexOfF(text!, ignoreCase);
         if (pos < 0) return str;
         return str.Substring(pos + text!.Length);

+ 1 - 1
Shared/Utils/DirectoryUtils.cs

@@ -6,7 +6,7 @@ public static class DirectoryUtils {
     /// 文件夹已存在时不会抛出异常。
     /// </summary>
     public static void Create(string path, bool isFilePath = false) {
-        if (isFilePath) path = Path.GetDirectoryName(path);
+        if (isFilePath) path = PathUtils.RemoveFileName(path);
         if (Exists(path)) return;
         Logger.Trace($"新建文件夹:{path}");
         path = PathUtils.WithLongPath(path);

+ 16 - 2
Shared/Utils/PathUtils.cs

@@ -7,7 +7,7 @@ public static class PathUtils {
     /// 确保路径的结尾包含文件夹分隔符。
     /// </summary>
     public static string WithSeparator(string folder) 
-        => folder.EndsWithF(Path.DirectorySeparatorChar) ? folder : folder + Path.DirectorySeparatorChar;
+        => folder.EndsWithF(Path.DirectorySeparatorChar) || folder.EndsWithF(Path.AltDirectorySeparatorChar) ? folder : folder + Path.DirectorySeparatorChar;
 
     /// <summary>
     /// 确保路径的结尾不包含文件夹分隔符。
@@ -34,7 +34,7 @@ public static class PathUtils {
         if (fullName.EndsWithF(".jar", true)) keepFileName = true; // jar 文件的文件名需要保留原样,否则会导致 Forge 1.20.1 无法通过文件名识别模块名
         if (keepFileName && FileUtils.Exists(fullName)) {
             pathToKeep = Path.GetFileName(fullName);
-            pathToShorten = Path.GetDirectoryName(fullName);
+            pathToShorten = PathUtils.RemoveFileName(fullName);
         }
 
         // 逐级向上寻找已存在的文件夹,将不存在的部分挪到 suffix,不再缩短
@@ -82,4 +82,18 @@ public static class PathUtils {
 
     #endregion
 
+    #region 路径处理
+
+    /// <summary>
+    /// 去除路径末尾的文件名。
+    /// 若路径以分隔符结尾,则不作处理。
+    /// </summary>
+    public static string? RemoveFileName(string? path) {
+        if (path is null || string.IsNullOrEmpty(path)) return path;
+        if (path.EndsWithF(Path.DirectorySeparatorChar) || path.EndsWithF(Path.AltDirectorySeparatorChar)) return path;
+        return Path.GetDirectoryName(path) ?? "";
+    }
+
+    #endregion
+
 }