From ff8cbe2ccc4a27a78b4e326bfe8e3da882b69599 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BE=99=E8=85=BE=E7=8C=AB=E8=B7=83?= <1043137532@qq.com> Date: Tue, 21 Apr 2026 21:17:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20nullable=20=E6=A3=80?= =?UTF-8?q?=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Extensions/EnumerableExtensions.cs | 11 ++++++----- Extensions/StringExtensions.cs | 24 ++++++++++++------------ General/MsDelta.cs | 4 ++-- General/OneOf.cs | 28 ++++++++++++++-------------- MeloongCore.csproj | 9 +++++++++ 5 files changed, 43 insertions(+), 33 deletions(-) diff --git a/Extensions/EnumerableExtensions.cs b/Extensions/EnumerableExtensions.cs index ff279d0..3efb12d 100644 --- a/Extensions/EnumerableExtensions.cs +++ b/Extensions/EnumerableExtensions.cs @@ -79,9 +79,9 @@ public static class EnumerableExtensions { /// /// 选择最大值对应的对象。 - /// 若没有元素则返回 Nothing。 + /// 若没有元素则返回 null。 /// - public static T MaxBy(this IEnumerable source, Func selector) where C : IComparable { + public static T? MaxBy(this IEnumerable source, Func selector) where C : IComparable { using var enumerator = source.GetEnumerator(); if (!enumerator.MoveNext()) return default; T maxItem = enumerator.Current; @@ -96,9 +96,9 @@ public static class EnumerableExtensions { } /// /// 选择最小值对应的对象。 - /// 若没有元素则返回 Nothing。 + /// 若没有元素则返回 null。 /// - public static T MinBy(this IEnumerable List, Func Selector) where C : IComparable { + public static T? MinBy(this IEnumerable List, Func Selector) where C : IComparable { using var enumerator = List.GetEnumerator(); if (!enumerator.MoveNext()) { return default; } T minItem = enumerator.Current; @@ -152,8 +152,9 @@ public static class EnumerableExtensions { /// /// 判断集合是否有且仅有一个元素。 + /// 在输入 null 时返回 false。 /// - public static bool IsSingle(this IEnumerable source) { + public static bool IsSingle([AllowNull] this IEnumerable source) { if (source is null) return false; if (source is IList list) return list.Count == 1; using var enumerator = source.GetEnumerator(); diff --git a/Extensions/StringExtensions.cs b/Extensions/StringExtensions.cs index 06490b4..dc842e0 100644 --- a/Extensions/StringExtensions.cs +++ b/Extensions/StringExtensions.cs @@ -7,7 +7,7 @@ public static class StringExtensions { /// 高速的 StartsWith。 /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static bool StartsWithF(this string value, string prefix, bool ignoreCase = false) { + public static bool StartsWithF([AllowNull] this string value, string prefix, bool ignoreCase = false) { if (value is null) return false; return value.StartsWith(prefix, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal); } @@ -15,7 +15,7 @@ public static class StringExtensions { /// 高速的 EndsWith。 /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static bool EndsWithF(this string value, string suffix, bool ignoreCase = false) { + public static bool EndsWithF([AllowNull] this string value, string suffix, bool ignoreCase = false) { if (value is null) return false; return value.EndsWith(suffix, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal); } @@ -88,8 +88,8 @@ public static class StringExtensions { /// 获取在子字符串第一次出现之前的部分,例如对 2024/11/08 拆切 / 会得到 2024。 /// 如果未找到子字符串则不裁切。 /// - public static string BeforeFirst(this string str, string text, bool ignoreCase = false) { - int pos = string.IsNullOrEmpty(text) ? -1 : str.IndexOfF(text, ignoreCase); + public static string BeforeFirst(this string str, [AllowNull] string text, bool ignoreCase = false) { + int pos = string.IsNullOrEmpty(text) ? -1 : str.IndexOfF(text!, ignoreCase); if (pos >= 0) return str.Substring(0, pos); else @@ -99,8 +99,8 @@ public static class StringExtensions { /// 获取在子字符串最后一次出现之前的部分,例如对 2024/11/08 拆切 / 会得到 2024/11。 /// 如果未找到子字符串则不裁切。 /// - public static string BeforeLast(this string str, string text, bool ignoreCase = false) { - int pos = string.IsNullOrEmpty(text) ? -1 : str.LastIndexOfF(text, ignoreCase); + public static string BeforeLast(this string str, [AllowNull] string text, bool ignoreCase = false) { + int pos = string.IsNullOrEmpty(text) ? -1 : str.LastIndexOfF(text!, ignoreCase); if (pos >= 0) return str.Substring(0, pos); else @@ -111,10 +111,10 @@ public static class StringExtensions { /// 获取在子字符串第一次出现之后的部分,例如对 2024/11/08 拆切 / 会得到 11/08。 /// 如果未找到子字符串则不裁切。 /// - public static string AfterFirst(this string str, string text, bool ignoreCase = false) { - int pos = string.IsNullOrEmpty(text) ? -1 : str.IndexOfF(text, ignoreCase); + public static string AfterFirst(this string str, [AllowNull] string text, bool ignoreCase = false) { + int pos = string.IsNullOrEmpty(text) ? -1 : str.IndexOfF(text!, ignoreCase); if (pos >= 0) - return str.Substring(pos + text.Length); + return str.Substring(pos + text!.Length); else return str; } @@ -122,10 +122,10 @@ public static class StringExtensions { /// 获取在子字符串最后一次出现之后的部分,例如对 2024/11/08 拆切 / 会得到 08。 /// 如果未找到子字符串则不裁切。 /// - public static string AfterLast(this string str, string text, bool ignoreCase = false) { - int pos = string.IsNullOrEmpty(text) ? -1 : str.LastIndexOfF(text, ignoreCase); + public static string AfterLast(this string str, [AllowNull] string text, bool ignoreCase = false) { + int pos = string.IsNullOrEmpty(text) ? -1 : str.LastIndexOfF(text!, ignoreCase); if (pos >= 0) - return str.Substring(pos + text.Length); + return str.Substring(pos + text!.Length); else return str; } diff --git a/General/MsDelta.cs b/General/MsDelta.cs index ac3690c..119040a 100644 --- a/General/MsDelta.cs +++ b/General/MsDelta.cs @@ -46,8 +46,8 @@ public static class MsDelta { [return: MarshalAs(UnmanagedType.Bool)] private static extern bool CreateDelta( long fileTypeSet, long setFlags, long resetFlags, - string sourceName, string targetName, string sourceOptionsName, - string targetOptionsName, DeltaInput globalOptions, + string sourceName, string targetName, string? sourceOptionsName, + string? targetOptionsName, DeltaInput globalOptions, IntPtr targetFileTime, uint hashAlgId, string deltaName); [DllImport("msdelta.dll", CharSet = CharSet.Unicode, SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] diff --git a/General/OneOf.cs b/General/OneOf.cs index f695d6d..0ea51cb 100644 --- a/General/OneOf.cs +++ b/General/OneOf.cs @@ -2,33 +2,33 @@ public readonly struct OneOf { private readonly int _index; - private readonly T0 _value0; - private readonly T1 _value1; + private readonly T0? _value0; + private readonly T1? _value1; public static implicit operator OneOf(T0 value) => new(0, value, default); public static implicit operator OneOf(T1 value) => new(1, default, value); - private OneOf(int index, T0 value0, T1 value1) { + private OneOf(int index, T0? value0, T1? value1) { _index = index; _value0 = value0; _value1 = value1; } public TResult Match(Func f0, Func f1) => - _index switch {0 => f0(_value0), _ => f1(_value1)}; + _index switch {0 => f0(_value0!), _ => f1(_value1!)}; public void Switch(Action f0, Action f1) { - if (_index == 0) f0(_value0); - else if (_index == 1) f1(_value1); + if (_index == 0) f0(_value0!); + else if (_index == 1) f1(_value1!); } } public readonly struct OneOf { private readonly int _index; - private readonly T0 _value0; - private readonly T1 _value1; - private readonly T2 _value2; + private readonly T0? _value0; + private readonly T1? _value1; + private readonly T2? _value2; public static implicit operator OneOf(T0 value) => new(0, value, default, default); public static implicit operator OneOf(T1 value) => new(1, default, value, default); public static implicit operator OneOf(T2 value) => new(2, default, default, value); - private OneOf(int index, T0 value0, T1 value1, T2 value2) { + private OneOf(int index, T0? value0, T1? value1, T2? value2) { _index = index; _value0 = value0; _value1 = value1; @@ -36,10 +36,10 @@ public readonly struct OneOf { } public TResult Match(Func f0, Func f1, Func f2) => - _index switch {0 => f0(_value0), 1 => f1(_value1), _ => f2(_value2)}; + _index switch {0 => f0(_value0!), 1 => f1(_value1!), _ => f2(_value2!)}; public void Switch(Action f0, Action f1, Action f2) { - if (_index == 0) f0(_value0); - else if (_index == 1) f1(_value1); - else if (_index == 2) f2(_value2); + if (_index == 0) f0(_value0!); + else if (_index == 1) f1(_value1!); + else if (_index == 2) f2(_value2!); } } diff --git a/MeloongCore.csproj b/MeloongCore.csproj index 68800d7..26ccf56 100644 --- a/MeloongCore.csproj +++ b/MeloongCore.csproj @@ -10,13 +10,22 @@ Debug;Release;Snapshot true 13.0 + enable + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + +