From 910e963d138e4fc033be37bc5d8c942a3c90ffde Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Fri, 15 Nov 2024 20:30:15 -0500 Subject: [PATCH] Port extension attribute instead of framework gating --- SabreTools.Matching/ExtensionAttribute.cs | 9 +++++ SabreTools.Matching/Extensions.cs | 40 ----------------------- SabreTools.Matching/MatchUtil.cs | 22 ++----------- 3 files changed, 11 insertions(+), 60 deletions(-) create mode 100644 SabreTools.Matching/ExtensionAttribute.cs diff --git a/SabreTools.Matching/ExtensionAttribute.cs b/SabreTools.Matching/ExtensionAttribute.cs new file mode 100644 index 0000000..f7d035b --- /dev/null +++ b/SabreTools.Matching/ExtensionAttribute.cs @@ -0,0 +1,9 @@ +#if NET20 + +namespace System.Runtime.CompilerServices +{ + [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method)] + internal sealed class ExtensionAttribute : Attribute {} +} + +#endif \ No newline at end of file diff --git a/SabreTools.Matching/Extensions.cs b/SabreTools.Matching/Extensions.cs index f12fe4e..0a2d4f7 100644 --- a/SabreTools.Matching/Extensions.cs +++ b/SabreTools.Matching/Extensions.cs @@ -9,11 +9,7 @@ namespace SabreTools.Matching /// /// Indicates whether the specified array is null or has a length of zero /// -#if NET20 - public static bool IsNullOrEmpty(Array? array) -#else public static bool IsNullOrEmpty(this Array? array) -#endif { return array == null || array.Length == 0; } @@ -21,11 +17,7 @@ namespace SabreTools.Matching /// /// Find all positions of one array in another, if possible, if possible /// -#if NET20 - public static List FindAllPositions(byte[] stack, byte?[]? needle, int start = 0, int end = -1) -#else public static List FindAllPositions(this byte[] stack, byte?[]? needle, int start = 0, int end = -1) -#endif { // Get the outgoing list List positions = []; @@ -51,11 +43,7 @@ namespace SabreTools.Matching /// /// Find the first position of one array in another, if possible /// -#if NET20 - public static bool FirstPosition(byte[] stack, byte[]? needle, out int position, int start = 0, int end = -1) -#else public static bool FirstPosition(this byte[] stack, byte[]? needle, out int position, int start = 0, int end = -1) -#endif { // Convert the needle to a nullable byte array byte?[]? nullableNeedle = null; @@ -68,11 +56,7 @@ namespace SabreTools.Matching /// /// Find the first position of one array in another, if possible /// -#if NET20 - public static bool FirstPosition(byte[] stack, byte?[]? needle, out int position, int start = 0, int end = -1) -#else public static bool FirstPosition(this byte[] stack, byte?[]? needle, out int position, int start = 0, int end = -1) -#endif { var matcher = new ContentMatch(needle, start, end); position = matcher.Match(stack, false); @@ -82,11 +66,7 @@ namespace SabreTools.Matching /// /// Find the last position of one array in another, if possible /// -#if NET20 - public static bool LastPosition(byte[] stack, byte[]? needle, out int position, int start = 0, int end = -1) -#else public static bool LastPosition(this byte[] stack, byte[]? needle, out int position, int start = 0, int end = -1) -#endif { // Convert the needle to a nullable byte array byte?[]? nullableNeedle = null; @@ -99,11 +79,7 @@ namespace SabreTools.Matching /// /// Find the last position of one array in another, if possible /// -#if NET20 - public static bool LastPosition(byte[] stack, byte?[]? needle, out int position, int start = 0, int end = -1) -#else public static bool LastPosition(this byte[] stack, byte?[]? needle, out int position, int start = 0, int end = -1) -#endif { var matcher = new ContentMatch(needle, start, end); position = matcher.Match(stack, true); @@ -113,11 +89,7 @@ namespace SabreTools.Matching /// /// See if a byte array starts with another /// -#if NET20 - public static bool StartsWith(byte[] stack, byte[]? needle, bool exact = false) -#else public static bool StartsWith(this byte[] stack, byte[]? needle, bool exact = false) -#endif { // If we have any invalid inputs, we return false if (needle == null @@ -134,11 +106,7 @@ namespace SabreTools.Matching /// /// See if a byte array starts with another /// -#if NET20 - public static bool StartsWith(byte[] stack, byte?[]? needle, bool exact = false) -#else public static bool StartsWith(this byte[] stack, byte?[]? needle, bool exact = false) -#endif { // If we have any invalid inputs, we return false if (needle == null @@ -155,11 +123,7 @@ namespace SabreTools.Matching /// /// See if a byte array ends with another /// -#if NET20 - public static bool EndsWith(byte[] stack, byte[]? needle, bool exact = false) -#else public static bool EndsWith(this byte[] stack, byte[]? needle, bool exact = false) -#endif { // If we have any invalid inputs, we return false if (needle == null @@ -176,11 +140,7 @@ namespace SabreTools.Matching /// /// See if a byte array ends with another /// -#if NET20 - public static bool EndsWith(byte[] stack, byte?[]? needle, bool exact = false) -#else public static bool EndsWith(this byte[] stack, byte?[]? needle, bool exact = false) -#endif { // If we have any invalid inputs, we return false if (needle == null diff --git a/SabreTools.Matching/MatchUtil.cs b/SabreTools.Matching/MatchUtil.cs index b06896a..83e03d2 100644 --- a/SabreTools.Matching/MatchUtil.cs +++ b/SabreTools.Matching/MatchUtil.cs @@ -67,16 +67,7 @@ namespace SabreTools.Matching continue; // Format the list of all positions found -#if NET20 || NET35 - var positionStrs = new List(); - foreach (int pos in positions) - { - positionStrs.Add(pos.ToString()); - } - string positionsString = string.Join(", ", [.. positionStrs]); -#else - string positionsString = string.Join(", ", positions); -#endif + string positionsString = string.Join(", ", [.. positions.ConvertAll(p => p.ToString())]); // If we there is no version method, just return the match name if (matcher.GetArrayVersion == null) @@ -162,16 +153,7 @@ namespace SabreTools.Matching continue; // Format the list of all positions found -#if NET20 || NET35 - var positionStrs = new List(); - foreach (int pos in positions) - { - positionStrs.Add(pos.ToString()); - } - string positionsString = string.Join(", ", [.. positionStrs]); -#else - string positionsString = string.Join(", ", positions); -#endif + string positionsString = string.Join(", ", [.. positions.ConvertAll(p => p.ToString())]); // If we there is no version method, just return the match name if (matcher.GetStreamVersion == null)