3 Commits
1.2.0 ... 1.3.0

Author SHA1 Message Date
Matt Nadareski
cd04d2d64c Bump version 2023-11-21 11:23:35 -05:00
Matt Nadareski
3e0267eedb Support .NET Framework 2.0 2023-11-21 00:27:36 -05:00
Matt Nadareski
70aeabd355 Support .NET Framework 3.5 2023-11-20 22:17:21 -05:00
4 changed files with 55 additions and 9 deletions

View File

@@ -11,7 +11,7 @@ namespace SabreTools.Matching
public static List<int> FindAllPositions(this byte[] stack, byte?[]? needle, int start = 0, int end = -1)
{
// Get the outgoing list
List<int> positions = new List<int>();
List<int> positions = [];
// Initialize the loop variables
bool found = true;
@@ -35,7 +35,7 @@ namespace SabreTools.Matching
/// </summary>
public static bool FirstPosition(this byte[] stack, byte[]? needle, out int position, int start = 0, int end = -1)
{
byte?[]? nullableNeedle = needle != null ? needle.Select(b => (byte?)b).ToArray() : null;
byte?[]? nullableNeedle = needle?.Select(b => (byte?)b).ToArray();
return stack.FirstPosition(nullableNeedle, out position, start, end);
}

View File

@@ -1,4 +1,6 @@
#if NET40_OR_GREATER || NETCOREAPP
using System.Collections.Concurrent;
#endif
using System.Collections.Generic;
using System.IO;
using System.Linq;
@@ -20,7 +22,11 @@ namespace SabreTools.Matching
/// <param name="matchers">Enumerable of ContentMatchSets to be run on the file</param>
/// <param name="includeDebug">True to include positional data, false otherwise</param>
/// <returns>List of strings representing the matched protections, null or empty otherwise</returns>
#if NET20 || NET35
public static Queue<string>? GetAllMatches(string file, byte[]? stack, IEnumerable<ContentMatchSet>? matchers, bool includeDebug = false)
#else
public static ConcurrentQueue<string>? GetAllMatches(string file, byte[]? stack, IEnumerable<ContentMatchSet>? matchers, bool includeDebug = false)
#endif
{
return FindAllMatches(file, stack, matchers, includeDebug, false);
}
@@ -51,14 +57,22 @@ namespace SabreTools.Matching
/// <param name="includeDebug">True to include positional data, false otherwise</param>
/// <param name="stopAfterFirst">True to stop after the first match, false otherwise</param>
/// <returns>List of strings representing the matched protections, null or empty otherwise</returns>
#if NET20 || NET35
private static Queue<string>? FindAllMatches(string file, byte[]? stack, IEnumerable<ContentMatchSet>? matchers, bool includeDebug, bool stopAfterFirst)
#else
private static ConcurrentQueue<string>? FindAllMatches(string file, byte[]? stack, IEnumerable<ContentMatchSet>? matchers, bool includeDebug, bool stopAfterFirst)
#endif
{
// If there's no mappings, we can't match
if (matchers == null || !matchers.Any())
return null;
// Initialize the queue of matched protections
#if NET20 || NET35
var matchedProtections = new Queue<string>();
#else
var matchedProtections = new ConcurrentQueue<string>();
#endif
// Loop through and try everything otherwise
foreach (var matcher in matchers)
@@ -75,7 +89,7 @@ namespace SabreTools.Matching
{
positionStrs.Add(pos.ToString());
}
string positionsString = string.Join(", ", positionStrs.ToArray());
string positionsString = string.Join(", ", [.. positionStrs]);
#else
string positionsString = string.Join(", ", positions);
#endif
@@ -117,7 +131,11 @@ namespace SabreTools.Matching
/// <param name="matchers">Enumerable of ContentMatchSets to be run on the file</param>
/// <param name="includeDebug">True to include positional data, false otherwise</param>
/// <returns>List of strings representing the matched protections, null or empty otherwise</returns>
#if NET20 || NET35
public static Queue<string>? GetAllMatches(string file, Stream? stack, IEnumerable<ContentMatchSet>? matchers, bool includeDebug = false)
#else
public static ConcurrentQueue<string>? GetAllMatches(string file, Stream? stack, IEnumerable<ContentMatchSet>? matchers, bool includeDebug = false)
#endif
{
return FindAllMatches(file, stack, matchers, includeDebug, false);
}
@@ -148,14 +166,22 @@ namespace SabreTools.Matching
/// <param name="includeDebug">True to include positional data, false otherwise</param>
/// <param name="stopAfterFirst">True to stop after the first match, false otherwise</param>
/// <returns>List of strings representing the matched protections, null or empty otherwise</returns>
#if NET20 || NET35
private static Queue<string>? FindAllMatches(string file, Stream? stack, IEnumerable<ContentMatchSet>? matchers, bool includeDebug, bool stopAfterFirst)
#else
private static ConcurrentQueue<string>? FindAllMatches(string file, Stream? stack, IEnumerable<ContentMatchSet>? matchers, bool includeDebug, bool stopAfterFirst)
#endif
{
// If there's no mappings, we can't match
if (matchers == null || !matchers.Any())
return null;
// Initialize the queue of matched protections
#if NET20 || NET35
var matchedProtections = new Queue<string>();
#else
var matchedProtections = new ConcurrentQueue<string>();
#endif
// Loop through and try everything otherwise
foreach (var matcher in matchers)
@@ -172,7 +198,7 @@ namespace SabreTools.Matching
{
positionStrs.Add(pos.ToString());
}
string positionsString = string.Join(", ", positionStrs.ToArray());
string positionsString = string.Join(", ", [.. positionStrs]);
#else
string positionsString = string.Join(", ", positions);
#endif
@@ -213,7 +239,11 @@ namespace SabreTools.Matching
/// <param name="matchers">Enumerable of PathMatchSets to be run on the file</param>
/// <param name="any">True if any path match is a success, false if all have to match</param>
/// <returns>List of strings representing the matched protections, null or empty otherwise</returns>
#if NET20 || NET35
public static Queue<string> GetAllMatches(string file, IEnumerable<PathMatchSet>? matchers, bool any = false)
#else
public static ConcurrentQueue<string> GetAllMatches(string file, IEnumerable<PathMatchSet>? matchers, bool any = false)
#endif
{
return FindAllMatches(new List<string> { file }, matchers, any, false);
}
@@ -225,7 +255,11 @@ namespace SabreTools.Matching
/// <param name="matchers">Enumerable of PathMatchSets to be run on the file</param>
/// <param name="any">True if any path match is a success, false if all have to match</param>
/// <returns>List of strings representing the matched protections, null or empty otherwise</returns>
#if NET20 || NET35
public static Queue<string> GetAllMatches(IEnumerable<string>? files, IEnumerable<PathMatchSet>? matchers, bool any = false)
#else
public static ConcurrentQueue<string> GetAllMatches(IEnumerable<string>? files, IEnumerable<PathMatchSet>? matchers, bool any = false)
#endif
{
return FindAllMatches(files, matchers, any, false);
}
@@ -270,21 +304,29 @@ namespace SabreTools.Matching
/// <param name="any">True if any path match is a success, false if all have to match</param>
/// <param name="stopAfterFirst">True to stop after the first match, false otherwise</param>
/// <returns>List of strings representing the matched protections, null or empty otherwise</returns>
#if NET20 || NET35
private static Queue<string> FindAllMatches(IEnumerable<string>? files, IEnumerable<PathMatchSet>? matchers, bool any, bool stopAfterFirst)
#else
private static ConcurrentQueue<string> FindAllMatches(IEnumerable<string>? files, IEnumerable<PathMatchSet>? matchers, bool any, bool stopAfterFirst)
#endif
{
// If there's no mappings, we can't match
if (matchers == null || !matchers.Any())
return new ConcurrentQueue<string>();
return new();
// Initialize the list of matched protections
#if NET20 || NET35
var matchedProtections = new Queue<string>();
#else
var matchedProtections = new ConcurrentQueue<string>();
#endif
// Loop through and try everything otherwise
foreach (var matcher in matchers)
{
// Determine if the matcher passes
bool passes;
var firstMatchedString = string.Empty;
string? firstMatchedString;
if (any)
{
(bool anyPasses, var matchedString) = matcher.MatchesAny(files);

View File

@@ -66,7 +66,7 @@ namespace SabreTools.Matching
return (false, new List<string>());
// Initialize the value list
List<string> values = new List<string>();
List<string> values = [];
// Loop through all path matches and make sure all pass
foreach (var pathMatch in Matchers)

View File

@@ -2,12 +2,12 @@
<PropertyGroup>
<!-- Assembly Properties -->
<TargetFrameworks>net40;net452;net462;net472;net48;netcoreapp3.1;net5.0;net6.0;net7.0;net8.0</TargetFrameworks>
<TargetFrameworks>net20;net35;net40;net452;net462;net472;net48;netcoreapp3.1;net5.0;net6.0;net7.0;net8.0</TargetFrameworks>
<RuntimeIdentifiers>win-x86;win-x64;win-arm64;linux-x64;linux-arm64;osx-x64;osx-arm64</RuntimeIdentifiers>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<Version>1.2.0</Version>
<Version>1.3.0</Version>
<!-- Package Properties -->
<Authors>Matt Nadareski</Authors>
@@ -26,6 +26,10 @@
</ItemGroup>
<!-- Support for old .NET versions -->
<ItemGroup Condition="$(TargetFramework.StartsWith(`net2`)) OR $(TargetFramework.StartsWith(`net3`))">
<PackageReference Include="Net30.LinqBridge" Version="1.3.0" />
<PackageReference Include="MinValueTupleBridge" Version="0.2.0" />
</ItemGroup>
<ItemGroup Condition="$(TargetFramework.StartsWith(`net4`))">
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
</ItemGroup>