fix: 在混合使用 / 和 \ 时路径裁剪有误
This commit is contained in:
1 parent
883b912104
commit
5ee6f88cde
4 files changed
+129
-31
No files matched your search
@@ -42,11 +42,11 @@ public static class ExceptionExtensions {
|
||||
foreach (var currentEx in ex.Flatten()) {
|
||||
if (showMultilineStacks) {
|
||||
lines.Add((isInnerException ? "→ " : "") + currentEx.Message.ReplaceLineEndings("\r\n", true));
|
||||
if (currentEx.GetType() != typeof(Exception)) lines.Add(" 错误类型:" + currentEx.GetType().FullName);
|
||||
var stackLines = (currentEx.StackTrace?.SplitLines(true) ?? [])
|
||||
.Select(l => l.BeforeLast("(") + l.AfterLast(")"))
|
||||
.Distinct();
|
||||
lines.AddRange(stackLines);
|
||||
if (currentEx.GetType() != typeof(Exception)) lines.Add(" 错误类型:" + currentEx.GetType().FullName);
|
||||
} else {
|
||||
lines.Add(currentEx.Message.ReplaceLineEndings(" ", true));
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Text.RegularExpressions;
|
||||
using static System.Net.Mime.MediaTypeNames;
|
||||
|
||||
namespace MeloongCore.Extensions;
|
||||
public static class StringExtensions {
|
||||
@@ -79,7 +80,7 @@ public static class StringExtensions {
|
||||
|
||||
#endregion
|
||||
|
||||
#region 分割与裁切
|
||||
#region Split
|
||||
|
||||
/// <summary>
|
||||
/// 分割字符串。
|
||||
@@ -111,6 +112,16 @@ public static class StringExtensions {
|
||||
public static string[] Split(this string fullStr, char[] splitChars, bool removeEmptyEntries = false)
|
||||
=> fullStr.Split(splitChars, removeEmptyEntries ? StringSplitOptions.RemoveEmptyEntries : StringSplitOptions.None);
|
||||
|
||||
/// <summary>
|
||||
/// 将字符串分割为多行。
|
||||
/// </summary>
|
||||
public static string[] SplitLines(this string input, bool skipEmptyLines = false)
|
||||
=> input.ReplaceLineEndings("\n").Split('\n', skipEmptyLines);
|
||||
|
||||
#endregion
|
||||
|
||||
#region 裁切
|
||||
|
||||
/// <summary>
|
||||
/// 获取在子字符串第一次出现之前的部分,如果未找到子字符串则不裁切。
|
||||
/// <code>"2024/11/08".BeforeFirst("/") => "2024"</code>
|
||||
@@ -120,6 +131,22 @@ public static class StringExtensions {
|
||||
if (pos < 0) return str;
|
||||
return str.Substring(0, pos);
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取在任意子字符串第一次出现之前的部分,如果未找到任意子字符串则不裁切。
|
||||
/// <code>"2024/11-08".BeforeFirstOfAny(["/", "-"]) => "2024"</code>
|
||||
/// </summary>
|
||||
public static string BeforeFirstOfAny(this string str, IEnumerable<string> texts, bool ignoreCase = false) {
|
||||
if (texts == null) return str;
|
||||
int pos = -1;
|
||||
foreach (string text in texts) {
|
||||
if (string.IsNullOrEmpty(text)) continue;
|
||||
int p = str.IndexOfF(text, ignoreCase);
|
||||
if (p >= 0 && (pos < 0 || pos > p)) pos = p;
|
||||
}
|
||||
if (pos < 0) return str;
|
||||
return str.Substring(0, pos);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取在子字符串最后一次出现之前的部分,如果未找到子字符串则不裁切。
|
||||
/// <code>"2024/11/08".BeforeLast("/") => "2024/11"</code>
|
||||
@@ -129,6 +156,21 @@ public static class StringExtensions {
|
||||
if (pos < 0) return str;
|
||||
return str.Substring(0, pos);
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取在任意子字符串最后一次出现之前的部分,如果未找到任意子字符串则不裁切。
|
||||
/// <code>"2024/11-08".BeforeLastOfAny(["/", "-"]) => "2024/11"</code>
|
||||
/// </summary>
|
||||
public static string BeforeLastOfAny(this string str, IEnumerable<string> texts, bool ignoreCase = false) {
|
||||
if (texts == null) return str;
|
||||
int pos = -1;
|
||||
foreach (string text in texts) {
|
||||
if (string.IsNullOrEmpty(text)) continue;
|
||||
int p = str.LastIndexOfF(text, ignoreCase);
|
||||
if (p >= 0 && (pos < 0 || pos < p)) pos = p;
|
||||
}
|
||||
if (pos < 0) return str;
|
||||
return str.Substring(0, pos);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取在子字符串第一次出现之后的部分,如果未找到子字符串则不裁切。
|
||||
@@ -139,6 +181,26 @@ public static class StringExtensions {
|
||||
if (pos < 0) return str;
|
||||
return str.Substring(pos + text!.Length);
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取在任意子字符串第一次出现之后的部分,如果未找到任意子字符串则不裁切。
|
||||
/// <code>"2024/11-08".AfterFirstOfAny(["/", "-"]) => "11-08"</code>
|
||||
/// </summary>
|
||||
public static string AfterFirstOfAny(this string str, IEnumerable<string> texts, bool ignoreCase = false) {
|
||||
if (texts == null) return str;
|
||||
int pos = -1;
|
||||
int len = 0;
|
||||
foreach (string text in texts) {
|
||||
if (string.IsNullOrEmpty(text)) continue;
|
||||
int p = str.IndexOfF(text, ignoreCase);
|
||||
if (p >= 0 && (pos < 0 || pos > p)) {
|
||||
pos = p;
|
||||
len = text.Length;
|
||||
}
|
||||
}
|
||||
if (pos < 0) return str;
|
||||
return str.Substring(pos + len);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取在子字符串最后一次出现之后的部分,如果未找到子字符串则不裁切。
|
||||
/// <code>"2024/11/08".AfterLast("/") => "08"</code>
|
||||
@@ -148,6 +210,25 @@ public static class StringExtensions {
|
||||
if (pos < 0) return str;
|
||||
return str.Substring(pos + text!.Length);
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取在任意子字符串最后一次出现之后的部分,如果未找到任意子字符串则不裁切。
|
||||
/// <code>"2024/11-08".AfterLastOfAny(["/", "-"]) => "08"</code>
|
||||
/// </summary>
|
||||
public static string AfterLastOfAny(this string str, IEnumerable<string> texts, bool ignoreCase = false) {
|
||||
if (texts == null) return str;
|
||||
int pos = -1;
|
||||
int len = 0;
|
||||
foreach (string text in texts) {
|
||||
if (string.IsNullOrEmpty(text)) continue;
|
||||
int p = str.LastIndexOfF(text, ignoreCase);
|
||||
if (p >= 0 && (pos < 0 || pos < p)) {
|
||||
pos = p;
|
||||
len = text.Length;
|
||||
}
|
||||
}
|
||||
if (pos < 0) return str;
|
||||
return str.Substring(pos + len);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取处于两个子字符串之间的部分,裁切尽可能多的内容。
|
||||
@@ -192,12 +273,6 @@ public static class StringExtensions {
|
||||
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);
|
||||
|
||||
/// <summary>
|
||||
/// 将字符串分割为多行。
|
||||
/// </summary>
|
||||
public static string[] SplitLines(this string input, bool skipEmptyLines = false)
|
||||
=> input.ReplaceLineEndings("\n").Split('\n', skipEmptyLines);
|
||||
|
||||
#endregion
|
||||
|
||||
#region 正则
|
||||
|
||||
@@ -94,10 +94,8 @@ public static class PathUtils {
|
||||
public static string RemoveLastPart(string path) {
|
||||
if (path!.Contains("://")) { // 网络路径
|
||||
path = PathUtils.RemoveSlashSuffix(path.BeforeFirst("#").BeforeFirst("?")); // 去除参数
|
||||
return PathUtils.RemoveSlashSuffix(path).BeforeLast("/");
|
||||
} else { // 文件路径
|
||||
return PathUtils.RemoveSlashSuffix(path!).BeforeLast("\\");
|
||||
}
|
||||
return PathUtils.RemoveSlashSuffix(path!).BeforeLastOfAny([@"\", "/"]);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -111,10 +109,10 @@ public static class PathUtils {
|
||||
public static string GetLastPart(string path) {
|
||||
if (path!.Contains("://")) { // 网络路径
|
||||
path = PathUtils.RemoveSlashSuffix(path.BeforeFirst("#").BeforeFirst("?")); // 去除参数
|
||||
return path.AfterLast("/");
|
||||
} else { // 文件路径
|
||||
return PathUtils.RemoveSlashSuffix(path).AfterLast("\\");
|
||||
path = PathUtils.RemoveSlashSuffix(path);
|
||||
}
|
||||
return path.AfterLastOfAny([@"\", "/"]);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in new issue
Block a user