9 Commits
1.3.3 ... 1.4.0

Author SHA1 Message Date
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
12 changed files with 230 additions and 375 deletions

View File

@@ -16,7 +16,7 @@ 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

View File

@@ -11,7 +11,7 @@ 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

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

@@ -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
@@ -12,7 +9,11 @@ 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;
}
@@ -20,7 +21,11 @@ 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 = [];
@@ -46,32 +51,28 @@ namespace SabreTools.Matching
/// <summary>
/// Find the first position of one array in another, if possible
/// </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];
}
}
#if NET20
public static bool FirstPosition(byte[] stack, byte[]? needle, out int position, int start = 0, int end = -1)
#else
byte?[]? nullableNeedle = needle?.Select(b => (byte?)b).ToArray();
public static bool FirstPosition(this byte[] stack, byte[]? needle, out int position, int start = 0, int end = -1)
#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>
/// 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);
@@ -81,7 +82,28 @@ 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;
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>
#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);
@@ -91,7 +113,11 @@ 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
@@ -102,13 +128,17 @@ 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>
/// 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
@@ -119,13 +149,17 @@ 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>
/// 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
@@ -136,13 +170,17 @@ 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>
/// 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
@@ -153,7 +191,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)
@@ -109,7 +81,7 @@ namespace SabreTools.Matching
// 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 +92,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 +115,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 +132,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 +143,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)
@@ -226,7 +176,7 @@ namespace SabreTools.Matching
// 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 +187,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 +209,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 +219,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 +231,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 +251,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 +262,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 +297,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 +308,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,42 @@
<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.0</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>
<!-- Support All Frameworks -->
<PropertyGroup Condition="$(TargetFramework.StartsWith(`net2`)) OR $(TargetFramework.StartsWith(`net3`)) OR $(TargetFramework.StartsWith(`net4`))">
<RuntimeIdentifiers>win-x86;win-x64</RuntimeIdentifiers>
</PropertyGroup>
<PropertyGroup Condition="$(TargetFramework.StartsWith(`netcoreapp`)) OR $(TargetFramework.StartsWith(`net5`))">
<RuntimeIdentifiers>win-x86;win-x64;win-arm64;linux-x64;linux-arm64;osx-x64</RuntimeIdentifiers>
</PropertyGroup>
<PropertyGroup Condition="$(TargetFramework.StartsWith(`net6`)) OR $(TargetFramework.StartsWith(`net7`)) OR $(TargetFramework.StartsWith(`net8`)) OR $(TargetFramework.StartsWith(`net9`))">
<RuntimeIdentifiers>win-x86;win-x64;win-arm64;linux-x64;linux-arm64;osx-x64;osx-arm64</RuntimeIdentifiers>
</PropertyGroup>
<PropertyGroup Condition="$(RuntimeIdentifier.StartsWith(`osx-arm`))">
<TargetFrameworks>net6.0;net7.0;net8.0;net9.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<None Include="../README.md" Pack="true" PackagePath="" />
</ItemGroup>
</Project>