15 Commits
1.3.3 ... 1.4.1

Author SHA1 Message Date
Matt Nadareski
e596c7c2c1 Bump version 2024-11-15 20:32:23 -05:00
Matt Nadareski
910e963d13 Port extension attribute instead of framework gating 2024-11-15 20:30:15 -05:00
Matt Nadareski
751519fadb Framework only matters for executable 2024-11-15 20:20:33 -05:00
Matt Nadareski
f86f565136 Ensure tests pass 2024-11-13 00:43:29 -05:00
Matt Nadareski
1ad45c6d59 Ensure tests pass 2024-11-13 00:42:49 -05:00
Matt Nadareski
38796776ee Add .NET 9 to target frameworks 2024-11-13 00:34:24 -05:00
Matt Nadareski
c60f587b69 Bump version 2024-11-13 00:31:36 -05:00
Matt Nadareski
8844ba0ae3 Fix ordering bug in comparers 2024-11-13 00:31:00 -05:00
Matt Nadareski
cdd41e8bec Add .NET 9 to target frameworks 2024-11-13 00:26:42 -05:00
Matt Nadareski
655214503f Bump version 2024-11-05 20:22:40 -05:00
Matt Nadareski
5801424db7 Remove all external package requirements 2024-11-05 20:21:27 -05:00
Matt Nadareski
afb9ab3e4e Define delegates instead of anonymous funcs 2024-11-05 20:10:33 -05:00
Matt Nadareski
85006a71bf Replace Linq calls with cheaper methods 2024-11-05 20:01:59 -05:00
Matt Nadareski
90bb82306b Properly support all frameworks 2024-11-05 19:48:08 -05:00
Matt Nadareski
c38e10e55b Simplify enumerable types 2024-11-04 11:38:46 -05:00
15 changed files with 199 additions and 398 deletions

View File

@@ -16,10 +16,16 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.0.x
dotnet-version: 9.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build library
run: dotnet build
- name: Run tests
run: dotnet test
- name: Pack
run: dotnet pack

View File

@@ -11,7 +11,10 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.0.x
dotnet-version: 9.0.x
- name: Build
run: dotnet build
run: dotnet build
- name: Run tests
run: dotnet test

View File

@@ -1,29 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
<IsPackable>false</IsPackable>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.6.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.4">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SabreTools.Matching\SabreTools.Matching.csproj" />
</ItemGroup>
<PropertyGroup>
<TargetFrameworks>net6.0;net8.0;net9.0</TargetFrameworks>
<IsPackable>false</IsPackable>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<WarningsNotAsErrors>NU1903</WarningsNotAsErrors>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.6.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.4">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SabreTools.Matching\SabreTools.Matching.csproj" />
</ItemGroup>
</Project>

View File

@@ -11,9 +11,6 @@
using System;
using System.Collections.Generic;
#if NET40_OR_GREATER || NETCOREAPP
using System.Linq;
#endif
using System.Text.RegularExpressions;
namespace SabreTools.Matching.Compare
@@ -50,42 +47,16 @@ namespace SabreTools.Matching.Compare
if (!_table.TryGetValue(x, out string[]? x1))
{
//x1 = Regex.Split(x.Replace(" ", string.Empty), "([0-9]+)");
#if NET20 || NET35
var nonempty = new List<string>();
x1 = Regex.Split(x.ToLowerInvariant(), "([0-9]+)");
foreach (var s in x1)
{
if (!string.IsNullOrEmpty(s))
nonempty.Add(s);
}
x1 = nonempty.ToArray();
#else
x1 = Regex.Split(x.ToLowerInvariant(), "([0-9]+)")
.Where(s => !string.IsNullOrEmpty(s))
.ToArray();
#endif
x1 = Array.FindAll(x1, s => !string.IsNullOrEmpty(s));
_table.Add(x, x1);
}
if (!_table.TryGetValue(y, out string[]? y1))
{
//y1 = Regex.Split(y.Replace(" ", string.Empty), "([0-9]+)");
#if NET20 || NET35
var nonempty = new List<string>();
y1 = Regex.Split(y.ToLowerInvariant(), "([0-9]+)");
foreach (var s in y1)
{
if (!string.IsNullOrEmpty(s))
nonempty.Add(s);
}
y1 = nonempty.ToArray();
#else
y1 = Regex.Split(y.ToLowerInvariant(), "([0-9]+)")
.Where(s => !string.IsNullOrEmpty(s))
.ToArray();
#endif
y1 = Array.FindAll(y1, s => !string.IsNullOrEmpty(s));
_table.Add(y, y1);
}

View File

@@ -11,9 +11,6 @@
using System;
using System.Collections.Generic;
#if NET40_OR_GREATER || NETCOREAPP
using System.Linq;
#endif
using System.Text.RegularExpressions;
namespace SabreTools.Matching.Compare
@@ -50,42 +47,16 @@ namespace SabreTools.Matching.Compare
if (!_table.TryGetValue(x, out string[]? x1))
{
//x1 = Regex.Split(x.Replace(" ", string.Empty), "([0-9]+)");
#if NET20 || NET35
var nonempty = new List<string>();
x1 = Regex.Split(x.ToLowerInvariant(), "([0-9]+)");
foreach (var s in x1)
{
if (!string.IsNullOrEmpty(s))
nonempty.Add(s);
}
x1 = nonempty.ToArray();
#else
x1 = Regex.Split(x.ToLowerInvariant(), "([0-9]+)")
.Where(s => !string.IsNullOrEmpty(s))
.ToArray();
#endif
x1 = Array.FindAll(x1, s => !string.IsNullOrEmpty(s));
_table.Add(x, x1);
}
if (!_table.TryGetValue(y, out string[]? y1))
{
//y1 = Regex.Split(y.Replace(" ", string.Empty), "([0-9]+)");
#if NET20 || NET35
var nonempty = new List<string>();
y1 = Regex.Split(y.ToLowerInvariant(), "([0-9]+)");
foreach (var s in y1)
{
if (!string.IsNullOrEmpty(s))
nonempty.Add(s);
}
y1 = nonempty.ToArray();
#else
y1 = Regex.Split(y.ToLowerInvariant(), "([0-9]+)")
.Where(s => !string.IsNullOrEmpty(s))
.ToArray();
#endif
y1 = Array.FindAll(y1, s => !string.IsNullOrEmpty(s));
_table.Add(y, y1);
}

View File

@@ -1,9 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
#if NET40_OR_GREATER || NETCOREAPP
using System.Linq;
#endif
namespace SabreTools.Matching.Content
{
@@ -22,7 +18,7 @@ namespace SabreTools.Matching.Content
/// to the match name, or `null`, in which case it will cause
/// the match name to be omitted.
/// </remarks>
public Func<string, byte[]?, List<int>, string?>? GetArrayVersion { get; }
public GetArrayVersion? GetArrayVersion { get; }
/// <summary>
/// Function to get a content version
@@ -34,7 +30,7 @@ namespace SabreTools.Matching.Content
/// to the match name, or `null`, in which case it will cause
/// the match name to be omitted.
/// </remarks>
public Func<string, Stream?, List<int>, string?>? GetStreamVersion { get; }
public GetStreamVersion? GetStreamVersion { get; }
#region Generic Constructors
@@ -54,31 +50,16 @@ namespace SabreTools.Matching.Content
#region Array Constructors
public ContentMatchSet(byte?[] needle, Func<string, byte[]?, List<int>, string?>? getArrayVersion, string matchName)
public ContentMatchSet(byte?[] needle, GetArrayVersion? getArrayVersion, string matchName)
: this([needle], getArrayVersion, matchName) { }
#if NET20 || NET35
public ContentMatchSet(List<byte?[]> needles, Func<string, byte[]?, List<int>, string?>? getArrayVersion, string matchName)
{
var matchers = new List<ContentMatch>();
foreach (var n in needles)
{
matchers.Add(new ContentMatch(n));
}
public ContentMatchSet(List<byte?[]> needles, GetArrayVersion? getArrayVersion, string matchName)
: this(needles.ConvertAll(n => new ContentMatch(n)), getArrayVersion, matchName) { }
Matchers = matchers;
GetArrayVersion = getArrayVersion;
MatchName = matchName;
}
#else
public ContentMatchSet(List<byte?[]> needles, Func<string, byte[]?, List<int>, string?>? getArrayVersion, string matchName)
: this(needles.Select(n => new ContentMatch(n)).ToList(), getArrayVersion, matchName) { }
#endif
public ContentMatchSet(ContentMatch needle, Func<string, byte[]?, List<int>, string?>? getArrayVersion, string matchName)
public ContentMatchSet(ContentMatch needle, GetArrayVersion? getArrayVersion, string matchName)
: this([needle], getArrayVersion, matchName) { }
public ContentMatchSet(List<ContentMatch> needles, Func<string, byte[]?, List<int>, string?>? getArrayVersion, string matchName)
public ContentMatchSet(List<ContentMatch> needles, GetArrayVersion? getArrayVersion, string matchName)
{
Matchers = needles;
GetArrayVersion = getArrayVersion;
@@ -89,31 +70,16 @@ namespace SabreTools.Matching.Content
#region Stream Constructors
public ContentMatchSet(byte?[] needle, Func<string, Stream?, List<int>, string?>? getStreamVersion, string matchName)
public ContentMatchSet(byte?[] needle, GetStreamVersion? getStreamVersion, string matchName)
: this([needle], getStreamVersion, matchName) { }
#if NET20 || NET35
public ContentMatchSet(List<byte?[]> needles, Func<string, Stream?, List<int>, string?>? getStreamVersion, string matchName)
{
var matchers = new List<ContentMatch>();
foreach (var n in needles)
{
matchers.Add(new ContentMatch(n));
}
public ContentMatchSet(List<byte?[]> needles, GetStreamVersion? getStreamVersion, string matchName)
: this(needles.ConvertAll(n => new ContentMatch(n)), getStreamVersion, matchName) { }
Matchers = matchers;
GetStreamVersion = getStreamVersion;
MatchName = matchName;
}
#else
public ContentMatchSet(List<byte?[]> needles, Func<string, Stream?, List<int>, string?>? getStreamVersion, string matchName)
: this(needles.Select(n => new ContentMatch(n)).ToList(), getStreamVersion, matchName) { }
#endif
public ContentMatchSet(ContentMatch needle, Func<string, Stream?, List<int>, string?>? getStreamVersion, string matchName)
public ContentMatchSet(ContentMatch needle, GetStreamVersion? getStreamVersion, string matchName)
: this([needle], getStreamVersion, matchName) { }
public ContentMatchSet(List<ContentMatch> needles, Func<string, Stream?, List<int>, string?>? getStreamVersion, string matchName)
public ContentMatchSet(List<ContentMatch> needles, GetStreamVersion? getStreamVersion, string matchName)
{
Matchers = needles;
GetStreamVersion = getStreamVersion;
@@ -132,11 +98,7 @@ namespace SabreTools.Matching.Content
public List<int> MatchesAll(byte[]? stack)
{
// If no content matches are defined, we fail out
#if NET20 || NET35
if (Matchers == null || new List<ContentMatch>(Matchers).Count == 0)
#else
if (Matchers == null || !Matchers.Any())
#endif
if (Matchers == null)
return [];
// Initialize the position list
@@ -163,11 +125,7 @@ namespace SabreTools.Matching.Content
public int MatchesAny(byte[]? stack)
{
// If no content matches are defined, we fail out
#if NET20 || NET35
if (Matchers == null || new List<ContentMatch>(Matchers).Count == 0)
#else
if (Matchers == null || !Matchers.Any())
#endif
if (Matchers == null)
return -1;
// Loop through all content matches and make sure all pass
@@ -193,11 +151,7 @@ namespace SabreTools.Matching.Content
public List<int> MatchesAll(Stream? stack)
{
// If no content matches are defined, we fail out
#if NET20 || NET35
if (Matchers == null || new List<ContentMatch>(Matchers).Count == 0)
#else
if (Matchers == null || !Matchers.Any())
#endif
if (Matchers == null)
return [];
// Initialize the position list
@@ -224,11 +178,7 @@ namespace SabreTools.Matching.Content
public int MatchesAny(Stream? stack)
{
// If no content matches are defined, we fail out
#if NET20 || NET35
if (Matchers == null || new List<ContentMatch>(Matchers).Count == 0)
#else
if (Matchers == null || !Matchers.Any())
#endif
if (Matchers == null)
return -1;
// Loop through all content matches and make sure all pass

View File

@@ -0,0 +1,31 @@
using System.Collections.Generic;
using System.IO;
namespace SabreTools.Matching
{
/// <summary>
/// Get a version number from a file
/// </summary>
/// <param name="path">File path to get the version from</param>
/// <param name="content">Optional file contents as a byte array</param>
/// <param name="positions">List of positions in the array that were matched</param>
/// <returns>Version string on success, null on failure</returns>
public delegate string? GetArrayVersion(string path, byte[]? content, List<int> positions);
/// <summary>
/// Get a version number from an input path
/// </summary>
/// <param name="path">File or directory path to get the version from</param>
/// <param name="files">Optional set of files in the directory</param>
/// <returns>Version string on success, null on failure</returns>
public delegate string? GetPathVersion(string path, IEnumerable<string>? files);
/// <summary>
/// Get a version number from a file
/// </summary>
/// <param name="path">File path to get the version from</param>
/// <param name="content">Optional file contents as a Stream</param>
/// <param name="positions">List of positions in the Stream that were matched</param>
/// <returns>Version string on success, null on failure</returns>
public delegate string? GetStreamVersion(string path, Stream? content, List<int> positions);
}

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

@@ -1,8 +1,5 @@
using System;
using System.Collections.Generic;
#if NET40_OR_GREATER || NETCOREAPP
using System.Linq;
#endif
using SabreTools.Matching.Content;
namespace SabreTools.Matching
@@ -48,24 +45,12 @@ namespace SabreTools.Matching
/// </summary>
public static bool FirstPosition(this byte[] stack, byte[]? needle, out int position, int start = 0, int end = -1)
{
#if NET20 || NET35
byte?[]? nullableNeedle;
if (needle == null)
{
nullableNeedle = null;
}
else
{
nullableNeedle = new byte?[needle.Length];
for (int i = 0; i < needle.Length; i++)
{
nullableNeedle[i] = needle[i];
}
}
#else
byte?[]? nullableNeedle = needle?.Select(b => (byte?)b).ToArray();
#endif
return stack.FirstPosition(nullableNeedle, out position, start, end);
// Convert the needle to a nullable byte array
byte?[]? nullableNeedle = null;
if (needle != null)
nullableNeedle = Array.ConvertAll(needle, b => (byte?)b);
return FirstPosition(stack, nullableNeedle, out position, start, end);
}
/// <summary>
@@ -78,6 +63,19 @@ namespace SabreTools.Matching
return position >= 0;
}
/// <summary>
/// Find the last position of one array in another, if possible
/// </summary>
public static bool LastPosition(this byte[] stack, byte[]? needle, out int position, int start = 0, int end = -1)
{
// Convert the needle to a nullable byte array
byte?[]? nullableNeedle = null;
if (needle != null)
nullableNeedle = Array.ConvertAll(needle, b => (byte?)b);
return LastPosition(stack, nullableNeedle, out position, start, end);
}
/// <summary>
/// Find the last position of one array in another, if possible
/// </summary>
@@ -102,7 +100,7 @@ namespace SabreTools.Matching
return false;
}
return stack.FirstPosition(needle, out int _, start: 0, end: 1);
return FirstPosition(stack, needle, out int _, start: 0, end: 1);
}
/// <summary>
@@ -119,7 +117,7 @@ namespace SabreTools.Matching
return false;
}
return stack.FirstPosition(needle, out int _, start: 0, end: 1);
return FirstPosition(stack, needle, out int _, start: 0, end: 1);
}
/// <summary>
@@ -136,7 +134,7 @@ namespace SabreTools.Matching
return false;
}
return stack.FirstPosition(needle, out int _, start: stack.Length - needle.Length);
return FirstPosition(stack, needle, out int _, start: stack.Length - needle.Length);
}
/// <summary>
@@ -153,7 +151,7 @@ namespace SabreTools.Matching
return false;
}
return stack.FirstPosition(needle, out int _, start: stack.Length - needle.Length);
return FirstPosition(stack, needle, out int _, start: stack.Length - needle.Length);
}
}
}

View File

@@ -1,11 +1,5 @@
#if NET40_OR_GREATER || NETCOREAPP
using System.Collections.Concurrent;
#endif
using System.Collections.Generic;
using System.IO;
#if NET40_OR_GREATER || NETCOREAPP
using System.Linq;
#endif
using SabreTools.Matching.Content;
using SabreTools.Matching.Paths;
@@ -26,14 +20,8 @@ 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 matches, 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);
}
public static List<string>? GetAllMatches(string file, byte[]? stack, IEnumerable<ContentMatchSet>? matchers, bool includeDebug = false)
=> FindAllMatches(file, stack, matchers, includeDebug, false);
/// <summary>
/// Get first content match for a given list of matchers
@@ -49,11 +37,7 @@ namespace SabreTools.Matching
if (contentMatches == null || contentMatches.Count == 0)
return null;
#if NET20 || NET35
return contentMatches.Peek();
#else
return contentMatches.First();
#endif
return contentMatches[0];
}
/// <summary>
@@ -64,27 +48,15 @@ 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>
/// <param name="stopAfterFirst">True to stop after the first match, false otherwise</param>
/// <returns>List of strings representing the matches, 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
/// <returns>List of strings representing the matches, empty otherwise</returns>
private static List<string> FindAllMatches(string file, byte[]? stack, IEnumerable<ContentMatchSet>? matchers, bool includeDebug, bool stopAfterFirst)
{
// If there's no mappings, we can't match
#if NET20 || NET35
if (matchers == null || new List<ContentMatchSet>(matchers).Count == 0)
#else
if (matchers == null || !matchers.Any())
#endif
return null;
if (matchers == null)
return [];
// Initialize the queue of matches
#if NET20 || NET35
var matchesQueue = new Queue<string>();
#else
var matchesQueue = new ConcurrentQueue<string>();
#endif
// Initialize the list of matches
var matchesList = new List<string>();
// Loop through and try everything otherwise
foreach (var matcher in matchers)
@@ -95,21 +67,12 @@ 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)
{
matchesQueue.Enqueue((matcher.MatchName ?? "Unknown") + (includeDebug ? $" (Index {positionsString})" : string.Empty));
matchesList.Add((matcher.MatchName ?? "Unknown") + (includeDebug ? $" (Index {positionsString})" : string.Empty));
}
// Otherwise, invoke the version method
@@ -120,15 +83,15 @@ namespace SabreTools.Matching
if (version == null)
continue;
matchesQueue.Enqueue($"{matcher.MatchName ?? "Unknown"} {version}".Trim() + (includeDebug ? $" (Index {positionsString})" : string.Empty));
matchesList.Add($"{matcher.MatchName ?? "Unknown"} {version}".Trim() + (includeDebug ? $" (Index {positionsString})" : string.Empty));
}
// If we're stopping after the first match, bail out here
if (stopAfterFirst)
return matchesQueue;
return matchesList;
}
return matchesQueue;
return matchesList;
}
#endregion
@@ -143,14 +106,8 @@ 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 matches, 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);
}
public static List<string>? GetAllMatches(string file, Stream? stack, IEnumerable<ContentMatchSet>? matchers, bool includeDebug = false)
=> FindAllMatches(file, stack, matchers, includeDebug, false);
/// <summary>
/// Get first content match for a given list of matchers
@@ -166,11 +123,7 @@ namespace SabreTools.Matching
if (contentMatches == null || contentMatches.Count == 0)
return null;
#if NET20 || NET35
return contentMatches.Peek();
#else
return contentMatches.First();
#endif
return contentMatches[0];
}
/// <summary>
@@ -181,27 +134,15 @@ 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>
/// <param name="stopAfterFirst">True to stop after the first match, false otherwise</param>
/// <returns>List of strings representing the matches, 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
/// <returns>List of strings representing the matches, empty otherwise</returns>
private static List<string> FindAllMatches(string file, Stream? stack, IEnumerable<ContentMatchSet>? matchers, bool includeDebug, bool stopAfterFirst)
{
// If there's no mappings, we can't match
#if NET20 || NET35
if (matchers == null || new List<ContentMatchSet>(matchers).Count == 0)
#else
if (matchers == null || !matchers.Any())
#endif
return null;
if (matchers == null)
return [];
// Initialize the queue of matches
#if NET20 || NET35
var matchesQueue = new Queue<string>();
#else
var matchesQueue = new ConcurrentQueue<string>();
#endif
// Initialize the list of matches
var matchesList = new List<string>();
// Loop through and try everything otherwise
foreach (var matcher in matchers)
@@ -212,21 +153,12 @@ 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)
{
matchesQueue.Enqueue((matcher.MatchName ?? "Unknown") + (includeDebug ? $" (Index {positionsString})" : string.Empty));
matchesList.Add((matcher.MatchName ?? "Unknown") + (includeDebug ? $" (Index {positionsString})" : string.Empty));
}
// Otherwise, invoke the version method
@@ -237,15 +169,15 @@ namespace SabreTools.Matching
if (version == null)
continue;
matchesQueue.Enqueue($"{matcher.MatchName ?? "Unknown"} {version}".Trim() + (includeDebug ? $" (Index {positionsString})" : string.Empty));
matchesList.Add($"{matcher.MatchName ?? "Unknown"} {version}".Trim() + (includeDebug ? $" (Index {positionsString})" : string.Empty));
}
// If we're stopping after the first match, bail out here
if (stopAfterFirst)
return matchesQueue;
return matchesList;
}
return matchesQueue;
return matchesList;
}
#endregion
@@ -259,14 +191,8 @@ 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 matches, 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([file], matchers, any, false);
}
public static List<string> GetAllMatches(string file, IEnumerable<PathMatchSet>? matchers, bool any = false)
=> FindAllMatches([file], matchers, any, false);
// <summary>
/// Get all path matches for a given list of matchers
@@ -275,14 +201,8 @@ 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 matches, 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);
}
public static List<string> GetAllMatches(IEnumerable<string>? files, IEnumerable<PathMatchSet>? matchers, bool any = false)
=> FindAllMatches(files, matchers, any, false);
/// <summary>
/// Get first path match for a given list of matchers
@@ -293,15 +213,11 @@ namespace SabreTools.Matching
/// <returns>String representing the match, null otherwise</returns>
public static string? GetFirstMatch(string file, IEnumerable<PathMatchSet> matchers, bool any = false)
{
var contentMatches = FindAllMatches(new List<string> { file }, matchers, any, true);
var contentMatches = FindAllMatches([file], matchers, any, true);
if (contentMatches == null || contentMatches.Count == 0)
return null;
#if NET20 || NET35
return contentMatches.Peek();
#else
return contentMatches.First();
#endif
return contentMatches[0];
}
/// <summary>
@@ -317,11 +233,7 @@ namespace SabreTools.Matching
if (contentMatches == null || contentMatches.Count == 0)
return null;
#if NET20 || NET35
return contentMatches.Peek();
#else
return contentMatches.First();
#endif
return contentMatches[0];
}
/// <summary>
@@ -332,26 +244,14 @@ 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 matches, 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
private static List<string> FindAllMatches(IEnumerable<string>? files, IEnumerable<PathMatchSet>? matchers, bool any, bool stopAfterFirst)
{
// If there's no mappings, we can't match
#if NET20 || NET35
if (matchers == null || new List<PathMatchSet>(matchers).Count == 0)
#else
if (matchers == null || !matchers.Any())
#endif
return new();
if (matchers == null)
return [];
// Initialize the list of matches
#if NET20 || NET35
var matchesQueue = new Queue<string>();
#else
var matchesQueue = new ConcurrentQueue<string>();
#endif
var matchesList = new List<string>();
// Loop through and try everything otherwise
foreach (var matcher in matchers)
@@ -379,7 +279,7 @@ namespace SabreTools.Matching
// If we there is no version method, just return the match name
if (matcher.GetVersion == null)
{
matchesQueue.Enqueue(matcher.MatchName ?? "Unknown");
matchesList.Add(matcher.MatchName ?? "Unknown");
}
// Otherwise, invoke the version method
@@ -390,15 +290,15 @@ namespace SabreTools.Matching
if (version == null)
continue;
matchesQueue.Enqueue($"{matcher.MatchName ?? "Unknown"} {version}".Trim());
matchesList.Add($"{matcher.MatchName ?? "Unknown"} {version}".Trim());
}
// If we're stopping after the first match, bail out here
if (stopAfterFirst)
return matchesQueue;
return matchesList;
}
return matchesQueue;
return matchesList;
}
#endregion

View File

@@ -1,7 +1,4 @@
using System.Collections.Generic;
#if NET40_OR_GREATER || NETCOREAPP
using System.Linq;
#endif
namespace SabreTools.Matching.Paths
{
@@ -48,11 +45,7 @@ namespace SabreTools.Matching.Paths
public string? Match(IEnumerable<string>? stack)
{
// If either array is null or empty, we can't do anything
#if NET20 || NET35
if (stack == null || new List<string>(stack).Count == 0 || Needle == null || Needle.Length == 0)
#else
if (stack == null || !stack.Any() || Needle == null || Needle.Length == 0)
#endif
if (stack == null || Needle == null || Needle.Length == 0)
return null;
// Preprocess the needle, if necessary

View File

@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
#if NET40_OR_GREATER || NETCOREAPP
using System.Linq;
#endif
namespace SabreTools.Matching.Paths
{
@@ -20,7 +16,7 @@ namespace SabreTools.Matching.Paths
/// in which case it will be appended to the match name, or `null`,
/// in which case it will cause the match name to be omitted.
/// </remarks>
public Func<string, IEnumerable<string>?, string?>? GetVersion { get; }
public GetPathVersion? GetVersion { get; }
#region Constructors
@@ -30,26 +26,11 @@ namespace SabreTools.Matching.Paths
public PathMatchSet(List<string> needles, string matchName)
: this(needles, null, matchName) { }
public PathMatchSet(string needle, Func<string, IEnumerable<string>?, string?>? getVersion, string matchName)
public PathMatchSet(string needle, GetPathVersion? getVersion, string matchName)
: this([needle], getVersion, matchName) { }
#if NET20 || NET35
public PathMatchSet(List<string> needles, Func<string, IEnumerable<string>?, string?>? getVersion, string matchName)
{
var matchers = new List<PathMatch>();
foreach (var n in needles)
{
matchers.Add(new PathMatch(n));
}
Matchers = matchers;
GetVersion = getVersion;
MatchName = matchName;
}
#else
public PathMatchSet(List<string> needles, Func<string, IEnumerable<string>?, string?>? getVersion, string matchName)
: this(needles.Select(n => new PathMatch(n)).ToList(), getVersion, matchName) { }
#endif
public PathMatchSet(List<string> needles, GetPathVersion? getVersion, string matchName)
: this(needles.ConvertAll(n => new PathMatch(n)), getVersion, matchName) { }
public PathMatchSet(PathMatch needle, string matchName)
: this([needle], null, matchName) { }
@@ -57,10 +38,10 @@ namespace SabreTools.Matching.Paths
public PathMatchSet(List<PathMatch> needles, string matchName)
: this(needles, null, matchName) { }
public PathMatchSet(PathMatch needle, Func<string, IEnumerable<string>?, string?>? getVersion, string matchName)
public PathMatchSet(PathMatch needle, GetPathVersion? getVersion, string matchName)
: this([needle], getVersion, matchName) { }
public PathMatchSet(List<PathMatch> needles, Func<string, IEnumerable<string>?, string?>? getVersion, string matchName)
public PathMatchSet(List<PathMatch> needles, GetPathVersion? getVersion, string matchName)
{
Matchers = needles;
GetVersion = getVersion;
@@ -79,11 +60,7 @@ namespace SabreTools.Matching.Paths
public List<string> MatchesAll(IEnumerable<string>? stack)
{
// If no path matches are defined, we fail out
#if NET20 || NET35
if (Matchers == null || new List<PathMatch>(Matchers).Count == 0)
#else
if (Matchers == null || !Matchers.Any())
#endif
if (Matchers == null)
return [];
// Initialize the value list
@@ -110,11 +87,7 @@ namespace SabreTools.Matching.Paths
public string? MatchesAny(IEnumerable<string>? stack)
{
// If no path matches are defined, we fail out
#if NET20 || NET35
if (Matchers == null || new List<PathMatch>(Matchers).Count == 0)
#else
if (Matchers == null || !Matchers.Any())
#endif
if (Matchers == null)
return null;
// Loop through all path matches and make sure all pass

View File

@@ -1,33 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<!-- Assembly Properties -->
<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.3.3</Version>
<!-- Package Properties -->
<Authors>Matt Nadareski</Authors>
<Description>Byte array and stream matching library</Description>
<Copyright>Copyright (c) Matt Nadareski 2018-2024</Copyright>
<PackageProjectUrl>https://github.com/SabreTools/</PackageProjectUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
<RepositoryUrl>https://github.com/SabreTools/SabreTools.Matching</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>byte array stream match matching</PackageTags>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
</PropertyGroup>
<ItemGroup>
<None Include="../README.md" Pack="true" PackagePath=""/>
</ItemGroup>
<PropertyGroup>
<!-- Assembly Properties -->
<TargetFrameworks>net20;net35;net40;net452;net462;net472;net48;netcoreapp3.1;net5.0;net6.0;net7.0;net8.0;net9.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<Version>1.4.1</Version>
<!-- Support for old .NET versions -->
<ItemGroup Condition="$(TargetFramework.StartsWith(`net2`))">
<PackageReference Include="Net30.LinqBridge" Version="1.3.0" />
</ItemGroup>
<!-- Package Properties -->
<Authors>Matt Nadareski</Authors>
<Description>Byte array and stream matching library</Description>
<Copyright>Copyright (c) Matt Nadareski 2018-2024</Copyright>
<PackageProjectUrl>https://github.com/SabreTools/</PackageProjectUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
<RepositoryUrl>https://github.com/SabreTools/SabreTools.Matching</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>byte array stream match matching</PackageTags>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<WarningsNotAsErrors>NU1903</WarningsNotAsErrors>
</PropertyGroup>
</Project>
<ItemGroup>
<None Include="../README.md" Pack="true" PackagePath="" />
</ItemGroup>
</Project>

View File

@@ -1,7 +1,7 @@
#! /bin/bash
# This batch file assumes the following:
# - .NET 8.0 (or newer) SDK is installed and in PATH
# - .NET 9.0 (or newer) SDK is installed and in PATH
#
# If any of these are not satisfied, the operation may fail
# in an unpredictable way and result in an incomplete output.

View File

@@ -1,5 +1,5 @@
# This batch file assumes the following:
# - .NET 8.0 (or newer) SDK is installed and in PATH
# - .NET 9.0 (or newer) SDK is installed and in PATH
#
# If any of these are not satisfied, the operation may fail
# in an unpredictable way and result in an incomplete output.