Port extension attribute instead of framework gating

This commit is contained in:
Matt Nadareski
2024-11-15 20:30:15 -05:00
parent 751519fadb
commit 910e963d13
3 changed files with 11 additions and 60 deletions

View File

@@ -0,0 +1,9 @@
#if NET20
namespace System.Runtime.CompilerServices
{
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method)]
internal sealed class ExtensionAttribute : Attribute {}
}
#endif

View File

@@ -9,11 +9,7 @@ namespace SabreTools.Matching
/// <summary>
/// Indicates whether the specified array is null or has a length of zero
/// </summary>
#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
/// <summary>
/// Find all positions of one array in another, if possible, if possible
/// </summary>
#if NET20
public static List<int> FindAllPositions(byte[] stack, byte?[]? needle, int start = 0, int end = -1)
#else
public static List<int> FindAllPositions(this byte[] stack, byte?[]? needle, int start = 0, int end = -1)
#endif
{
// Get the outgoing list
List<int> positions = [];
@@ -51,11 +43,7 @@ namespace SabreTools.Matching
/// <summary>
/// Find the first position of one array in another, if possible
/// </summary>
#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
/// <summary>
/// Find the first position of one array in another, if possible
/// </summary>
#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
/// <summary>
/// Find the last position of one array in another, if possible
/// </summary>
#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
/// <summary>
/// Find the last position of one array in another, if possible
/// </summary>
#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
/// <summary>
/// See if a byte array starts with another
/// </summary>
#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
/// <summary>
/// See if a byte array starts with another
/// </summary>
#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
/// <summary>
/// See if a byte array ends with another
/// </summary>
#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
/// <summary>
/// See if a byte array ends with another
/// </summary>
#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

View File

@@ -67,16 +67,7 @@ namespace SabreTools.Matching
continue;
// Format the list of all positions found
#if NET20 || NET35
var positionStrs = new List<string>();
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<string>();
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)