Code Review

This commit is contained in:
龙腾猫跃 committed 2026-05-07 08:46:48 +08:00
1 parent be6336f548
commit 9158a69b62
11 files changed
+102 -64

No files matched your search

+9 -7
View File
@@ -11,7 +11,7 @@ public static class EnumerableExtensions {
/// 该方法的效率较低,建议仅在小型列表上使用,或换用 DistinctBy。 /// 该方法的效率较低,建议仅在小型列表上使用,或换用 DistinctBy。
/// </summary> /// </summary>
public static IEnumerable<T> Distinct<T>(this IList<T> list, Func<T, T, bool> comparer) { public static IEnumerable<T> Distinct<T>(this IList<T> list, Func<T, T, bool> comparer) {
for (int i = 0; i < list.Count; i++) { for (int i = list.Count - 1; i >= 0; i--) { // 反向遍历以保留最早出现的重复项
if (!list.Skip(i + 1).Any(item => comparer(list[i], item))) yield return list[i]; if (!list.Skip(i + 1).Any(item => comparer(list[i], item))) yield return list[i];
} }
} }
@@ -108,13 +108,13 @@ public static class EnumerableExtensions {
/// 选择最小值对应的对象。 /// 选择最小值对应的对象。
/// 若没有元素则返回 null。 /// 若没有元素则返回 null。
/// </summary> /// </summary>
public static TKey? MinBy<TKey, TValue>(this IEnumerable<TKey> List, Func<TKey, TValue> Selector) where TValue : IComparable<TValue> { public static TKey? MinBy<TKey, TValue>(this IEnumerable<TKey> source, Func<TKey, TValue> selector) where TValue : IComparable<TValue> {
using var enumerator = List.GetEnumerator(); using var enumerator = source.GetEnumerator();
if (!enumerator.MoveNext()) { return default; } if (!enumerator.MoveNext()) { return default; }
TKey minItem = enumerator.Current; TKey minItem = enumerator.Current;
TValue minValue = Selector(minItem); TValue minValue = selector(minItem);
while (enumerator.MoveNext()) { while (enumerator.MoveNext()) {
TValue value = Selector(enumerator.Current); TValue value = selector(enumerator.Current);
if (value.CompareTo(minValue) >= 0) { continue; } if (value.CompareTo(minValue) >= 0) { continue; }
minItem = enumerator.Current; minItem = enumerator.Current;
minValue = value; minValue = value;
@@ -231,8 +231,10 @@ public static class EnumerableExtensions {
/// 对集合的每个元素执行指定操作。 /// 对集合的每个元素执行指定操作。
/// </summary> /// </summary>
public static IEnumerable<T> ForAll<T>(this IEnumerable<T> source, Action<T> action) { public static IEnumerable<T> ForAll<T>(this IEnumerable<T> source, Action<T> action) {
foreach (T item in source) action(item); foreach (T item in source) {
return source; action(item);
yield return item;
}
} }
} }
+23 -23
View File
@@ -6,59 +6,59 @@
public class ConcurrentList<T> : IList<T>, IList, IEnumerable, IEnumerable<T> { public class ConcurrentList<T> : IList<T>, IList, IEnumerable, IEnumerable<T> {
private readonly List<T> _items = []; private readonly List<T> _items = [];
public readonly object syncLock = new();
// 构造函数 // 构造函数
public ConcurrentList() { } public ConcurrentList() { }
public ConcurrentList(IEnumerable<T> data) => _items = new(data); public ConcurrentList(IEnumerable<T> data) => _items = new(data);
public static implicit operator ConcurrentList<T>(List<T> data) => new(data); public static implicit operator ConcurrentList<T>(List<T> data) => new(data);
public static implicit operator List<T>(ConcurrentList<T> data) { lock (data.syncLock) { return new(data._items); } } public static implicit operator List<T>(ConcurrentList<T> data) { lock (data._syncLock) { return new(data._items); } }
// 枚举器 // 枚举器
/// <summary> /// <summary>
/// 返回此列表的副本。 /// 返回此列表的副本。
/// </summary> /// </summary>
public IEnumerator<T> GetEnumerator() { lock (syncLock) return _items.ToList().GetEnumerator(); } public IEnumerator<T> GetEnumerator() { lock (_syncLock) return _items.ToList().GetEnumerator(); }
/// <summary> /// <summary>
/// 返回此列表的副本。 /// 返回此列表的副本。
/// </summary> /// </summary>
IEnumerator IEnumerable.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; } } } public int Count { get { lock (_syncLock) { return _items.Count; } } }
public bool IsReadOnly => false; public bool IsReadOnly => false;
bool IList.IsFixedSize => false; bool IList.IsFixedSize => false;
bool ICollection.IsSynchronized => true; bool ICollection.IsSynchronized => true;
object ICollection.SyncRoot => syncLock; private readonly object _syncLock = new();
object ICollection.SyncRoot => _syncLock;
public T this[int index] { public T this[int index] {
get { lock (syncLock) { return _items[index]; } } get { lock (_syncLock) { return _items[index]; } }
set { lock (syncLock) { _items[index] = value; } } set { lock (_syncLock) { _items[index] = value; } }
} }
object? IList.this[int index] { object? IList.this[int index] {
get => this[index]; get => this[index];
set { lock (syncLock) { _items[index] = (T) value!; } } set { lock (_syncLock) { _items[index] = (T) value!; } }
} }
public void Add(T item) { lock (syncLock) { _items.Add(item); } } public void Add(T item) { lock (_syncLock) { _items.Add(item); } }
int IList.Add(object? value) { lock (syncLock) { _items.Add((T) value!); return _items.Count - 1; } } int IList.Add(object? value) { lock (_syncLock) { _items.Add((T) value!); return _items.Count - 1; } }
public void Insert(int index, T item) { lock (syncLock) { _items.Insert(index, item); } } public void Insert(int index, T item) { lock (_syncLock) { _items.Insert(index, item); } }
void IList.Insert(int index, object? value) { lock (syncLock) { _items.Insert(index, (T) value!); } } void IList.Insert(int index, object? value) { lock (_syncLock) { _items.Insert(index, (T) value!); } }
public bool Remove(T item) { lock (syncLock) { return _items.Remove(item); } } public bool Remove(T item) { lock (_syncLock) { return _items.Remove(item); } }
void IList.Remove(object? value) { lock (syncLock) { if (value is T t) _items.Remove(t); } } void IList.Remove(object? value) { lock (_syncLock) { if (value is T t) _items.Remove(t); } }
public void RemoveAt(int index) { lock (syncLock) { _items.RemoveAt(index); } } public void RemoveAt(int index) { lock (_syncLock) { _items.RemoveAt(index); } }
public void Clear() { lock (syncLock) { _items.Clear(); } } public void Clear() { lock (_syncLock) { _items.Clear(); } }
public bool Contains(T item) { lock (syncLock) { return _items.Contains(item); } } public bool Contains(T item) { lock (_syncLock) { return _items.Contains(item); } }
bool IList.Contains(object? value) { lock (syncLock) { return value is T t && _items.Contains(t); } } bool IList.Contains(object? value) { lock (_syncLock) { return value is T t && _items.Contains(t); } }
public int IndexOf(T item) { lock (syncLock) { return _items.IndexOf(item); } } public int IndexOf(T item) { lock (_syncLock) { return _items.IndexOf(item); } }
int IList.IndexOf(object? value) { lock (syncLock) { return value is T t ? _items.IndexOf(t) : -1; } } int IList.IndexOf(object? value) { lock (_syncLock) { return value is T t ? _items.IndexOf(t) : -1; } }
public void CopyTo(T[] array, int arrayIndex) { lock (syncLock) { _items.CopyTo(array, arrayIndex); } } public void CopyTo(T[] array, int arrayIndex) { lock (_syncLock) { _items.CopyTo(array, arrayIndex); } }
void ICollection.CopyTo(Array array, int index) { lock (syncLock) { ((ICollection) _items).CopyTo(array, index); } } void ICollection.CopyTo(Array array, int index) { lock (_syncLock) { ((ICollection) _items).CopyTo(array, index); } }
} }
+32 -10
View File
@@ -91,6 +91,20 @@ public static class Logger {
=> Log(ex, message, LogLevel.Warning, behavior, filePath); => Log(ex, message, LogLevel.Warning, behavior, filePath);
public static void Error(Exception ex, string? message = null, LogBehavior behavior = LogBehavior.AlertThenFeedback, [CallerFilePath] string filePath = "") public static void Error(Exception ex, string? message = null, LogBehavior behavior = LogBehavior.AlertThenFeedback, [CallerFilePath] string filePath = "")
=> Log(ex, message, LogLevel.Error, behavior, filePath); => Log(ex, message, LogLevel.Error, behavior, filePath);
/// <summary>
/// 在不调用 Logger 的情况下,将信息直接发送到输出窗口。
/// </summary>
[Conditional("DEBUG")]
public static void SendToDebug(string message, LogLevel level = LogLevel.Error, [CallerFilePath] string filePath = "")
=> Debug.WriteLine($"{BaseLogger.GetLogPrefix(level, filePath)}{message}");
/// <summary>
/// 在不调用 Logger 的情况下,将信息直接发送到输出窗口。
/// </summary>
[Conditional("DEBUG")]
public static void SendToDebug(Exception ex, string? message = null, LogLevel level = LogLevel.Error, [CallerFilePath] string filePath = "")
=> Debug.WriteLine($"{BaseLogger.GetLogPrefix(level, filePath)}{(message is null ? "" : $"{message}:")}{ex.GetDisplay(true)}");
} }
/// <summary> /// <summary>
@@ -111,7 +125,9 @@ public class BaseLogger {
var formattedMessage = Format(message, level, filePath, null); var formattedMessage = Format(message, level, filePath, null);
Output(formattedMessage, level); Output(formattedMessage, level);
HandleBehavior(message, formattedMessage, behavior, null); HandleBehavior(message, formattedMessage, behavior, null);
} catch {} } catch (Exception logEx) {
Logger.SendToDebug(logEx, "打印日志失败");
}
} }
public virtual void Log(Exception ex, string? message, LogLevel level, LogBehavior behavior, string filePath) { public virtual void Log(Exception ex, string? message, LogLevel level, LogBehavior behavior, string filePath) {
if (MinLevel > level) return; if (MinLevel > level) return;
@@ -121,19 +137,23 @@ public class BaseLogger {
formattedMessage = Format(formattedMessage, level, filePath, ex); formattedMessage = Format(formattedMessage, level, filePath, ex);
Output(formattedMessage, level); Output(formattedMessage, level);
HandleBehavior(message, formattedMessage, behavior, ex); HandleBehavior(message, formattedMessage, behavior, ex);
} catch { } } catch (Exception logEx) {
Logger.SendToDebug(logEx, "打印日志失败");
}
} }
/// <summary> /// <summary>
/// 格式化日志文本。 /// 格式化日志文本。
/// </summary> /// </summary>
protected virtual string Format(string text, LogLevel 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(".")}] "; string prefix = GetLogPrefix(level, filePath);
return text return text
.ReplaceLineEndings("\n", mergeMultiple: true).Split(['\n'], StringSplitOptions.RemoveEmptyEntries) .ReplaceLineEndings("\n", mergeMultiple: true).Split(['\n'], StringSplitOptions.RemoveEmptyEntries)
.Select(t => prefix + t) .Select(t => prefix + t)
.Join("\r\n"); .Join("\r\n");
} }
public static string GetLogPrefix(LogLevel level, string filePath)
=> $"{DateTime.Now:HH':'mm':'ss'.'fff} {level.ToString().First()} {(Thread.CurrentThread.Name is null ? "" : $"<{Thread.CurrentThread.Name}> ")}[{Path.GetFileName(filePath).BeforeFirst(".")}] ";
/// <summary> /// <summary>
/// 输出格式化后的日志文本。 /// 输出格式化后的日志文本。
@@ -159,7 +179,7 @@ public class FileLogger : BaseLogger, IDisposable {
/// <inheritdoc/> /// <inheritdoc/>
protected override void Output(string formattedText, LogLevel level) { protected override void Output(string formattedText, LogLevel level) {
base.Output(formattedText, level); base.Output(formattedText, level);
if (!writerAvaliable && queue.Count >= 100) return; // 在 writer 就绪前,最多缓存 100 条日志 if (!writerAvailable && queue.Count >= 100) return; // 在 writer 就绪前,最多缓存 100 条日志
queue.Enqueue(formattedText); queue.Enqueue(formattedText);
queuedEvent.Set(); queuedEvent.Set();
} }
@@ -167,7 +187,7 @@ public class FileLogger : BaseLogger, IDisposable {
private readonly ConcurrentQueue<string> queue = new(); private readonly ConcurrentQueue<string> queue = new();
private readonly AutoResetEvent queuedEvent = new(false); private readonly AutoResetEvent queuedEvent = new(false);
private StreamWriter? writer; private StreamWriter? writer;
private volatile bool writerAvaliable = false; private volatile bool writerAvailable = false;
/// <summary> /// <summary>
/// 将日志立即写入文件。 /// 将日志立即写入文件。
@@ -175,10 +195,12 @@ public class FileLogger : BaseLogger, IDisposable {
/// </summary> /// </summary>
public void Flush() { public void Flush() {
try { try {
if (writer is null || !writerAvaliable) return; if (writer is null || !writerAvailable) return;
lock (flushLock) lock (flushLock)
while (queue.TryDequeue(out string line)) writer.WriteLine(line); // writer 指定了 AutoFlush while (queue.TryDequeue(out string line)) writer.WriteLine(line); // writer 指定了 AutoFlush
} catch { } } catch (Exception logEx) {
Logger.SendToDebug(logEx, "写入日志失败");
}
} }
private readonly object flushLock = new(); private readonly object flushLock = new();
@@ -206,7 +228,7 @@ public class FileLogger : BaseLogger, IDisposable {
// 写入新日志文件 // 写入新日志文件
try { try {
writer = new StreamWriter(PathUtils.WithLongPath(Path.Combine(logFolder, $"{fileNamePrefix}1{fileNameSuffix}")), append: true) { AutoFlush = true }; writer = new StreamWriter(PathUtils.WithLongPath(Path.Combine(logFolder, $"{fileNamePrefix}1{fileNameSuffix}")), append: true) { AutoFlush = true };
writerAvaliable = true; writerAvailable = true;
Logger.Info("日志初始化成功"); Logger.Info("日志初始化成功");
while (true) { while (true) {
queuedEvent.WaitOne(); queuedEvent.WaitOne();
@@ -214,14 +236,14 @@ public class FileLogger : BaseLogger, IDisposable {
} }
} catch (Exception ex) { } catch (Exception ex) {
Logger.Warning(ex, "写入日志文件失败", LogBehavior.Toast); Logger.Warning(ex, "写入日志文件失败", LogBehavior.Toast);
writerAvaliable = false; writerAvailable = false;
} }
}) { Name = nameof(FileLogger), Priority = ThreadPriority.Lowest, IsBackground = true }; }) { Name = nameof(FileLogger), Priority = ThreadPriority.Lowest, IsBackground = true };
thread.Start(); thread.Start();
} }
public void Dispose() { public void Dispose() {
writerAvaliable = false; writerAvailable = false;
if (writer is not null) ((IDisposable) writer).Dispose(); if (writer is not null) ((IDisposable) writer).Dispose();
queuedEvent.Dispose(); queuedEvent.Dispose();
} }
+13 -6
View File
@@ -1,6 +1,6 @@
namespace MeloongCore; namespace MeloongCore;
public readonly struct OneOf<T0, T1> { public readonly struct OneOf<T0, T1> {
private readonly int _index; private readonly int _index = -1; // -1 表示未初始化(这是一个 struct,所以这可以让默认值不为 0)
private readonly T0? _value0; private readonly T0? _value0;
private readonly T1? _value1; private readonly T1? _value1;
public static implicit operator OneOf<T0, T1>(T0 value) => new(0, value, default); public static implicit operator OneOf<T0, T1>(T0 value) => new(0, value, default);
@@ -17,7 +17,9 @@ public readonly struct OneOf<T0, T1> {
/// 对不同类型执行不同的操作,并返回同一个值。 /// 对不同类型执行不同的操作,并返回同一个值。
/// </summary> /// </summary>
public TResult Switch<TResult>(Func<T0, TResult> f0, Func<T1, TResult> f1) => _index switch { public TResult Switch<TResult>(Func<T0, TResult> f0, Func<T1, TResult> f1) => _index switch {
0 => f0(_value0!), _ => f1(_value1!) 0 => f0(_value0!),
1 => f1(_value1!),
_ => throw new InvalidOperationException()
}; };
/// <summary> /// <summary>
/// 对不同类型执行不同的操作。 /// 对不同类型执行不同的操作。
@@ -25,6 +27,7 @@ public readonly struct OneOf<T0, T1> {
public void Switch(Action<T0> f0, Action<T1> f1) { public void Switch(Action<T0> f0, Action<T1> f1) {
if (_index == 0) f0(_value0!); if (_index == 0) f0(_value0!);
else if (_index == 1) f1(_value1!); else if (_index == 1) f1(_value1!);
else throw new InvalidOperationException();
} }
/// <summary> /// <summary>
/// 判断当前的类型是否为 T。 /// 判断当前的类型是否为 T。
@@ -36,7 +39,7 @@ public readonly struct OneOf<T0, T1> {
}; };
/// <summary> /// <summary>
/// 假定当前的类型为 T,并返回该值。 /// 假定当前的类型为 T,并返回该值。
/// 若当前值的类型不为 T,则抛出 InvalidOperationException。 /// 若当前值的类型不为 T,则抛出 <see cref="InvalidOperationException"/>。
/// </summary> /// </summary>
public T As<T>() => _index switch { public T As<T>() => _index switch {
0 when typeof(T) == typeof(T0) => (T)(object)_value0!, 0 when typeof(T) == typeof(T0) => (T)(object)_value0!,
@@ -46,7 +49,7 @@ public readonly struct OneOf<T0, T1> {
} }
public readonly struct OneOf<T0, T1, T2> { public readonly struct OneOf<T0, T1, T2> {
private readonly int _index; private readonly int _index = -1; // -1 表示未初始化(这是一个 struct,所以这可以让默认值不为 0)
private readonly T0? _value0; private readonly T0? _value0;
private readonly T1? _value1; private readonly T1? _value1;
private readonly T2? _value2; private readonly T2? _value2;
@@ -67,7 +70,10 @@ public readonly struct OneOf<T0, T1, T2> {
/// 对不同类型执行不同的操作,并返回同一个值。 /// 对不同类型执行不同的操作,并返回同一个值。
/// </summary> /// </summary>
public TResult Switch<TResult>(Func<T0, TResult> f0, Func<T1, TResult> f1, Func<T2, TResult> f2) => _index switch { public TResult Switch<TResult>(Func<T0, TResult> f0, Func<T1, TResult> f1, Func<T2, TResult> f2) => _index switch {
0 => f0(_value0!), 1 => f1(_value1!), _ => f2(_value2!) 0 => f0(_value0!),
1 => f1(_value1!),
2 => f2(_value2!),
_ => throw new InvalidOperationException()
}; };
/// <summary> /// <summary>
/// 对不同类型执行不同的操作。 /// 对不同类型执行不同的操作。
@@ -76,6 +82,7 @@ public readonly struct OneOf<T0, T1, T2> {
if (_index == 0) f0(_value0!); if (_index == 0) f0(_value0!);
else if (_index == 1) f1(_value1!); else if (_index == 1) f1(_value1!);
else if (_index == 2) f2(_value2!); else if (_index == 2) f2(_value2!);
else throw new InvalidOperationException();
} }
/// <summary> /// <summary>
/// 判断当前的类型是否为 T。 /// 判断当前的类型是否为 T。
@@ -88,7 +95,7 @@ public readonly struct OneOf<T0, T1, T2> {
}; };
/// <summary> /// <summary>
/// 假定当前的类型为 T,并返回该值。 /// 假定当前的类型为 T,并返回该值。
/// 若当前值的类型不为 T,则抛出 InvalidOperationException。 /// 若当前值的类型不为 T,则抛出 <see cref="InvalidOperationException"/>。
/// </summary> /// </summary>
public T As<T>() => _index switch { public T As<T>() => _index switch {
0 when typeof(T) == typeof(T0) => (T) (object) _value0!, 0 when typeof(T) == typeof(T0) => (T) (object) _value0!,
+2 -2
View File
@@ -59,7 +59,7 @@ public static class DirectoryUtils {
/// </summary> /// </summary>
public static void Move(string sourceDirName, string destDirName) { public static void Move(string sourceDirName, string destDirName) {
if (!Exists(sourceDirName)) return; if (!Exists(sourceDirName)) return;
Create(destDirName); DirectoryUtils.Delete(destDirName); // Move 要求不存在对应文件夹
Logger.Trace($"剪切文件夹:{sourceDirName} → {destDirName}"); Logger.Trace($"剪切文件夹:{sourceDirName} → {destDirName}");
Directory.Move(PathUtils.WithLongPath(sourceDirName), PathUtils.WithLongPath(destDirName)); Directory.Move(PathUtils.WithLongPath(sourceDirName), PathUtils.WithLongPath(destDirName));
} }
@@ -73,7 +73,7 @@ public static class DirectoryUtils {
if (toRecycleBin) { if (toRecycleBin) {
FileUtils.DeleteToRecycleBin(dirPath); FileUtils.DeleteToRecycleBin(dirPath);
} else { } else {
Directory.Delete(PathUtils.WithLongPath(dirPath)); Directory.Delete(PathUtils.WithLongPath(dirPath), recursive:true);
} }
} }
} }
+9 -9
View File
@@ -4,7 +4,7 @@ namespace MeloongCore;
public static class EscapeUtils { public static class EscapeUtils {
/// <summary> /// <summary>
/// XML 转义。 /// WPF XML 转义。
/// </summary> /// </summary>
public static string? XmlEscape(string? str) { public static string? XmlEscape(string? str) {
if (str is null) return null; if (str is null) return null;
@@ -34,7 +34,7 @@ public static class EscapeUtils {
/// 等同于 <see cref="Regex.Escape"/>。 /// 等同于 <see cref="Regex.Escape"/>。
/// </summary> /// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string? RegexEscape(string? value) public static string? RegexEscape(string value)
=> Regex.Escape(value); => Regex.Escape(value);
/// <summary> /// <summary>
@@ -42,7 +42,7 @@ public static class EscapeUtils {
/// 等同于 <see cref="Regex.Unescape"/>。 /// 等同于 <see cref="Regex.Unescape"/>。
/// </summary> /// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string? RegexUnescape(string? value) public static string? RegexUnescape(string value)
=> Regex.Unescape(value); => Regex.Unescape(value);
/// <summary> /// <summary>
@@ -50,7 +50,7 @@ public static class EscapeUtils {
/// 等同于 <see cref="Uri.EscapeDataString"/>。 /// 等同于 <see cref="Uri.EscapeDataString"/>。
/// </summary> /// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string? UrlEscape(string? value) public static string? UrlEscape(string value)
=> Uri.EscapeDataString(value); => Uri.EscapeDataString(value);
/// <summary> /// <summary>
@@ -58,7 +58,7 @@ public static class EscapeUtils {
/// 等同于 <see cref="Uri.UnescapeDataString"/>。 /// 等同于 <see cref="Uri.UnescapeDataString"/>。
/// </summary> /// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string? UrlUnescape(string? value) public static string? UrlUnescape(string value)
=> Uri.UnescapeDataString(value); => Uri.UnescapeDataString(value);
/// <summary> /// <summary>
@@ -66,7 +66,7 @@ public static class EscapeUtils {
/// 等同于 <see cref="WebUtility.UrlEncode"/>。 /// 等同于 <see cref="WebUtility.UrlEncode"/>。
/// </summary> /// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string? FormUrlEscape(string? value) public static string? FormUrlEscape(string value)
=> WebUtility.UrlEncode(value); => WebUtility.UrlEncode(value);
/// <summary> /// <summary>
@@ -74,7 +74,7 @@ public static class EscapeUtils {
/// 等同于 <see cref="WebUtility.UrlDecode"/>。 /// 等同于 <see cref="WebUtility.UrlDecode"/>。
/// </summary> /// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string? FormUrlUnescape(string? value) public static string? FormUrlUnescape(string value)
=> WebUtility.UrlDecode(value); => WebUtility.UrlDecode(value);
/// <summary> /// <summary>
@@ -82,7 +82,7 @@ public static class EscapeUtils {
/// 等同于 <see cref="WebUtility.HtmlEncode"/>。 /// 等同于 <see cref="WebUtility.HtmlEncode"/>。
/// </summary> /// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string? HtmlEscape(string? value) public static string? HtmlEscape(string value)
=> WebUtility.HtmlEncode(value); => WebUtility.HtmlEncode(value);
/// <summary> /// <summary>
@@ -90,7 +90,7 @@ public static class EscapeUtils {
/// 等同于 <see cref="WebUtility.HtmlDecode"/>。 /// 等同于 <see cref="WebUtility.HtmlDecode"/>。
/// </summary> /// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string? HtmlUnescape(string? value) public static string? HtmlUnescape(string value)
=> WebUtility.HtmlDecode(value); => WebUtility.HtmlDecode(value);
} }
+3 -2
View File
@@ -37,7 +37,7 @@ public static class FileUtils {
public static void Write(string filePath, Stream stream) { public static void Write(string filePath, Stream stream) {
using FileStream fileStream = CreateAsStream(PathUtils.WithLongPath(filePath)); using FileStream fileStream = CreateAsStream(PathUtils.WithLongPath(filePath));
if (stream.CanSeek && stream.Position != 0) stream.Seek(0, SeekOrigin.Begin); if (stream.CanSeek && stream.Position != 0) stream.Seek(0, SeekOrigin.Begin);
Logger.Trace(() => $"写入文件:{filePath}({stream.GetType().Name} {stream.Length} 字节)"); Logger.Trace(() => $"写入文件:{filePath}({stream.GetType().Name}{(stream.CanSeek ? $" {stream.Length} 字节" : "")})");
stream.CopyTo(fileStream); stream.CopyTo(fileStream);
} }
@@ -49,7 +49,7 @@ public static class FileUtils {
/// 删除文件。 /// 删除文件。
/// </summary> /// </summary>
public static void Delete(string filePath, bool toRecycleBin = false) { public static void Delete(string filePath, bool toRecycleBin = false) {
if (!Exists(filePath)) return; if (!FileUtils.Exists(filePath)) return;
Logger.Trace($"{(toRecycleBin ? "将文件删除到回收站" : "删除文件")}:{filePath}"); Logger.Trace($"{(toRecycleBin ? "将文件删除到回收站" : "删除文件")}:{filePath}");
if (toRecycleBin) { if (toRecycleBin) {
DeleteToRecycleBin(filePath); DeleteToRecycleBin(filePath);
@@ -284,6 +284,7 @@ public static class FileUtils {
public static void Move(string sourceFilePath, string destFilePath) { public static void Move(string sourceFilePath, string destFilePath) {
if (sourceFilePath == destFilePath) return; // 如果移动同一个文件则跳过 if (sourceFilePath == destFilePath) return; // 如果移动同一个文件则跳过
DirectoryUtils.Create(destFilePath, isFilePath: true); DirectoryUtils.Create(destFilePath, isFilePath: true);
FileUtils.Delete(destFilePath);
Logger.Trace($"剪切文件:{sourceFilePath} → {destFilePath}"); Logger.Trace($"剪切文件:{sourceFilePath} → {destFilePath}");
File.Move(PathUtils.WithLongPath(sourceFilePath), PathUtils.WithLongPath(destFilePath)); File.Move(PathUtils.WithLongPath(sourceFilePath), PathUtils.WithLongPath(destFilePath));
} }
+1 -1
View File
@@ -7,7 +7,7 @@ public class HttpRequestCodeException(string message, HttpStatusCode statusCode,
/// <summary> /// <summary>
/// HTTP 请求的状态码。 /// HTTP 请求的状态码。
/// </summary> /// </summary>
public HttpStatusCode StatusCode = statusCode; public HttpStatusCode StatusCode { get; set; } = statusCode;
/// <summary> /// <summary>
/// 远程服务器给予的回复。 /// 远程服务器给予的回复。
/// </summary> /// </summary>
+3 -1
View File
@@ -91,10 +91,12 @@ public static class MathUtils {
if (t > 0.9999999) return 1; if (t > 0.9999999) return 1;
double a = t; double a = t;
double b; double b;
int maxIterations = 1000;
do { do {
b = 3 * a * ((0.33333333 + x1 - x2) * a * a + (x2 - 2 * x1) * a + x1); b = 3 * a * ((0.33333333 + x1 - x2) * a * a + (x2 - 2 * x1) * a + x1);
a += (t - b) * 0.5; a += (t - b) * 0.5;
} while (!(Math.Abs(b - t) < accuracy)); // 精度 maxIterations--;
} while (!(Math.Abs(b - t) < accuracy) && maxIterations > 0);
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);
} }
+5 -2
View File
@@ -74,8 +74,11 @@ public static class PathUtils {
/// <summary> /// <summary>
/// 去除路径开头的 <c>\\?\</c>。 /// 去除路径开头的 <c>\\?\</c>。
/// </summary> /// </summary>
public static string WithoutLongPath(string path) public static string WithoutLongPath(string path) {
=> path.AfterLast(@"\\?\"); if (path.StartsWithF(@"\\?\UNC\")) return @"\\" + path.AfterLast(@"\\?\UNC\");
if (path.StartsWithF(@"\\?\")) return path.AfterLast(@"\\?\");
return path;
}
#endregion #endregion
+2 -1
View File
@@ -3,6 +3,7 @@
namespace MeloongCore.Wpf.Extensions; namespace MeloongCore.Wpf.Extensions;
public static class WpfExtensions { public static class WpfExtensions {
public static bool Any(this UIElementCollection? Arr) => Arr?.Count > 0; public static bool Any(this UIElementCollection? arr)
=> arr?.Count > 0;
} }