mirror of
https://github.com/SabreTools/SabreTools.Matching.git
synced 2026-02-06 21:29:41 +00:00
Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
655214503f | ||
|
|
5801424db7 | ||
|
|
afb9ab3e4e | ||
|
|
85006a71bf | ||
|
|
90bb82306b | ||
|
|
c38e10e55b | ||
|
|
e96790d875 | ||
|
|
bf23967f74 | ||
|
|
8debf24a44 | ||
|
|
333b5be76c | ||
|
|
b0fdff2d6e | ||
|
|
3b0e74b9f4 |
Binary file not shown.
44
SabreTools.Matching.Test/MatchUtilTests.cs
Normal file
44
SabreTools.Matching.Test/MatchUtilTests.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using SabreTools.Matching.Content;
|
||||
using Xunit;
|
||||
|
||||
namespace SabreTools.Matching.Test
|
||||
{
|
||||
public class MatchUtilTests
|
||||
{
|
||||
[Fact]
|
||||
public void ExactSizeArrayMatch()
|
||||
{
|
||||
byte[] source = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07];
|
||||
byte?[] check = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07];
|
||||
string expected = "match";
|
||||
|
||||
var matchers = new List<ContentMatchSet>
|
||||
{
|
||||
new(check, expected),
|
||||
};
|
||||
|
||||
string? actual = MatchUtil.GetFirstMatch("testfile", source, matchers);
|
||||
Assert.Equal(expected, actual);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ExactSizeStreamMatch()
|
||||
{
|
||||
byte[] source = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07];
|
||||
var stream = new MemoryStream(source);
|
||||
|
||||
byte?[] check = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07];
|
||||
string expected = "match";
|
||||
|
||||
var matchers = new List<ContentMatchSet>
|
||||
{
|
||||
new(check, expected),
|
||||
};
|
||||
|
||||
string? actual = MatchUtil.GetFirstMatch("testfile", stream, matchers);
|
||||
Assert.Equal(expected, actual);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,25 +11,22 @@
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
#if NET40_OR_GREATER || NETCOREAPP
|
||||
using System.Linq;
|
||||
#endif
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace SabreTools.Matching.Compare
|
||||
{
|
||||
public class NaturalComparer : Comparer<string>, IDisposable
|
||||
{
|
||||
private readonly Dictionary<string, string[]> table;
|
||||
private readonly Dictionary<string, string[]> _table;
|
||||
|
||||
public NaturalComparer()
|
||||
{
|
||||
table = [];
|
||||
_table = [];
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
table.Clear();
|
||||
_table.Clear();
|
||||
}
|
||||
|
||||
public override int Compare(string? x, string? y)
|
||||
@@ -47,46 +44,20 @@ namespace SabreTools.Matching.Compare
|
||||
if (x.ToLowerInvariant() == y.ToLowerInvariant())
|
||||
return x.CompareTo(y);
|
||||
|
||||
if (!table.TryGetValue(x, out string[]? x1))
|
||||
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
|
||||
table.Add(x, x1);
|
||||
x1 = Regex.Split(y.ToLowerInvariant(), "([0-9]+)");
|
||||
x1 = Array.FindAll(x1, s => !string.IsNullOrEmpty(s));
|
||||
_table.Add(x, x1);
|
||||
}
|
||||
|
||||
if (!table.TryGetValue(y, out string[]? y1))
|
||||
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(x.ToLowerInvariant(), "([0-9]+)");
|
||||
foreach (var s in y1)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(s))
|
||||
nonempty.Add(s);
|
||||
}
|
||||
|
||||
y1 = nonempty.ToArray();
|
||||
#else
|
||||
y1 = Regex.Split(x.ToLowerInvariant(), "([0-9]+)")
|
||||
.Where(s => !string.IsNullOrEmpty(s))
|
||||
.ToArray();
|
||||
#endif
|
||||
table.Add(y, y1);
|
||||
y1 = Regex.Split(y.ToLowerInvariant(), "([0-9]+)");
|
||||
y1 = Array.FindAll(y1, s => !string.IsNullOrEmpty(s));
|
||||
_table.Add(y, y1);
|
||||
}
|
||||
|
||||
for (int i = 0; i < x1.Length && i < y1.Length; i++)
|
||||
|
||||
@@ -11,25 +11,22 @@
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
#if NET40_OR_GREATER || NETCOREAPP
|
||||
using System.Linq;
|
||||
#endif
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace SabreTools.Matching.Compare
|
||||
{
|
||||
public class NaturalReversedComparer : Comparer<string>, IDisposable
|
||||
{
|
||||
private readonly Dictionary<string, string[]> table;
|
||||
private readonly Dictionary<string, string[]> _table;
|
||||
|
||||
public NaturalReversedComparer()
|
||||
{
|
||||
table = [];
|
||||
_table = [];
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
table.Clear();
|
||||
_table.Clear();
|
||||
}
|
||||
|
||||
public override int Compare(string? x, string? y)
|
||||
@@ -47,46 +44,20 @@ namespace SabreTools.Matching.Compare
|
||||
if (y.ToLowerInvariant() == x.ToLowerInvariant())
|
||||
return y.CompareTo(x);
|
||||
|
||||
if (!table.TryGetValue(x, out string[]? x1))
|
||||
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
|
||||
table.Add(x, x1);
|
||||
x1 = Regex.Split(y.ToLowerInvariant(), "([0-9]+)");
|
||||
x1 = Array.FindAll(x1, s => !string.IsNullOrEmpty(s));
|
||||
_table.Add(x, x1);
|
||||
}
|
||||
|
||||
if (!table.TryGetValue(y, out string[]? y1))
|
||||
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(x.ToLowerInvariant(), "([0-9]+)");
|
||||
foreach (var s in y1)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(s))
|
||||
nonempty.Add(s);
|
||||
}
|
||||
|
||||
y1 = nonempty.ToArray();
|
||||
#else
|
||||
y1 = Regex.Split(x.ToLowerInvariant(), "([0-9]+)")
|
||||
.Where(s => !string.IsNullOrEmpty(s))
|
||||
.ToArray();
|
||||
#endif
|
||||
table.Add(y, y1);
|
||||
y1 = Regex.Split(y.ToLowerInvariant(), "([0-9]+)");
|
||||
y1 = Array.FindAll(y1, s => !string.IsNullOrEmpty(s));
|
||||
_table.Add(y, y1);
|
||||
}
|
||||
|
||||
for (int i = 0; i < x1.Length && i < y1.Length; i++)
|
||||
|
||||
@@ -10,11 +10,7 @@ namespace SabreTools.Matching.Content
|
||||
/// <summary>
|
||||
/// Content to match
|
||||
/// </summary>
|
||||
#if NETFRAMEWORK || NETCOREAPP
|
||||
public byte?[]? Needle { get; private set; }
|
||||
#else
|
||||
public byte?[]? Needle { get; init; }
|
||||
#endif
|
||||
public byte?[]? Needle { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Starting index for matching
|
||||
@@ -34,9 +30,9 @@ namespace SabreTools.Matching.Content
|
||||
/// <param name="end">Optional ending index</param>
|
||||
public ContentMatch(byte?[]? needle, int start = -1, int end = -1)
|
||||
{
|
||||
this.Needle = needle;
|
||||
this.Start = start;
|
||||
this.End = end;
|
||||
Needle = needle;
|
||||
Start = start;
|
||||
End = end;
|
||||
}
|
||||
|
||||
#region Array Matching
|
||||
@@ -50,22 +46,26 @@ namespace SabreTools.Matching.Content
|
||||
public int Match(byte[]? stack, bool reverse = false)
|
||||
{
|
||||
// If either array is null or empty, we can't do anything
|
||||
if (stack == null || stack.Length == 0 || this.Needle == null || this.Needle.Length == 0)
|
||||
if (stack == null || stack.Length == 0 || Needle == null || Needle.Length == 0)
|
||||
return -1;
|
||||
|
||||
// If the needle array is larger than the stack array, it can't be contained within
|
||||
if (this.Needle.Length > stack.Length)
|
||||
if (Needle.Length > stack.Length)
|
||||
return -1;
|
||||
|
||||
// If the needle and stack are identically sized, short-circuit
|
||||
if (Needle.Length == stack.Length)
|
||||
return EqualAt(stack, 0) ? 0 : -1;
|
||||
|
||||
// Set the default start and end values
|
||||
int start = this.Start;
|
||||
int end = this.End;
|
||||
int start = Start;
|
||||
int end = End;
|
||||
|
||||
// If start or end are not set properly, set them to defaults
|
||||
if (start < 0)
|
||||
start = 0;
|
||||
if (end < 0)
|
||||
end = stack.Length - this.Needle.Length;
|
||||
end = stack.Length - Needle.Length;
|
||||
|
||||
for (int i = reverse ? end : start; reverse ? i > start : i < end; i += reverse ? -1 : 1)
|
||||
{
|
||||
@@ -90,7 +90,7 @@ namespace SabreTools.Matching.Content
|
||||
private bool EqualAt(byte[] stack, int index)
|
||||
{
|
||||
// If the needle is invalid, we can't do anything
|
||||
if (this.Needle == null)
|
||||
if (Needle == null)
|
||||
return false;
|
||||
|
||||
// If the index is invalid, we can't do anything
|
||||
@@ -98,16 +98,16 @@ namespace SabreTools.Matching.Content
|
||||
return false;
|
||||
|
||||
// If we're too close to the end of the stack, return false
|
||||
if (this.Needle.Length > stack.Length - index)
|
||||
if (Needle.Length > stack.Length - index)
|
||||
return false;
|
||||
|
||||
// Loop through and check the value
|
||||
for (int i = 0; i < this.Needle.Length; i++)
|
||||
for (int i = 0; i < Needle.Length; i++)
|
||||
{
|
||||
// A null value is a wildcard
|
||||
if (this.Needle[i] == null)
|
||||
if (Needle[i] == null)
|
||||
continue;
|
||||
else if (stack[i + index] != this.Needle[i])
|
||||
else if (stack[i + index] != Needle[i])
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -127,22 +127,26 @@ namespace SabreTools.Matching.Content
|
||||
public int Match(Stream? stack, bool reverse = false)
|
||||
{
|
||||
// If either array is null or empty, we can't do anything
|
||||
if (stack == null || stack.Length == 0 || this.Needle == null || this.Needle.Length == 0)
|
||||
if (stack == null || stack.Length == 0 || Needle == null || Needle.Length == 0)
|
||||
return -1;
|
||||
|
||||
// If the needle array is larger than the stack array, it can't be contained within
|
||||
if (this.Needle.Length > stack.Length)
|
||||
if (Needle.Length > stack.Length)
|
||||
return -1;
|
||||
|
||||
// If the needle and stack are identically sized, short-circuit
|
||||
if (Needle.Length == stack.Length)
|
||||
return EqualAt(stack, 0) ? 0 : -1;
|
||||
|
||||
// Set the default start and end values
|
||||
int start = this.Start;
|
||||
int end = this.End;
|
||||
int start = Start;
|
||||
int end = End;
|
||||
|
||||
// If start or end are not set properly, set them to defaults
|
||||
if (start < 0)
|
||||
start = 0;
|
||||
if (end < 0)
|
||||
end = (int)(stack.Length - this.Needle.Length);
|
||||
end = (int)(stack.Length - Needle.Length);
|
||||
|
||||
for (int i = reverse ? end : start; reverse ? i > start : i < end; i += reverse ? -1 : 1)
|
||||
{
|
||||
@@ -167,7 +171,7 @@ namespace SabreTools.Matching.Content
|
||||
private bool EqualAt(Stream stack, int index)
|
||||
{
|
||||
// If the needle is invalid, we can't do anything
|
||||
if (this.Needle == null)
|
||||
if (Needle == null)
|
||||
return false;
|
||||
|
||||
// If the index is invalid, we can't do anything
|
||||
@@ -175,7 +179,7 @@ namespace SabreTools.Matching.Content
|
||||
return false;
|
||||
|
||||
// If we're too close to the end of the stack, return false
|
||||
if (this.Needle.Length > stack.Length - index)
|
||||
if (Needle.Length > stack.Length - index)
|
||||
return false;
|
||||
|
||||
// Save the current position and move to the index
|
||||
@@ -186,16 +190,16 @@ namespace SabreTools.Matching.Content
|
||||
bool matched = true;
|
||||
|
||||
// Loop through and check the value
|
||||
for (int i = 0; i < this.Needle.Length; i++)
|
||||
for (int i = 0; i < Needle.Length; i++)
|
||||
{
|
||||
byte stackValue = (byte)stack.ReadByte();
|
||||
|
||||
// A null value is a wildcard
|
||||
if (this.Needle[i] == null)
|
||||
if (Needle[i] == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
else if (stackValue != this.Needle[i])
|
||||
else if (stackValue != Needle[i])
|
||||
{
|
||||
matched = false;
|
||||
break;
|
||||
|
||||
@@ -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
|
||||
{
|
||||
@@ -19,10 +15,10 @@ namespace SabreTools.Matching.Content
|
||||
/// A content version method takes the file path, the file contents,
|
||||
/// and a list of found positions and returns a single string. That
|
||||
/// string is either a version string, in which case it will be appended
|
||||
/// to the protection name, or `null`, in which case it will cause
|
||||
/// the protection to be omitted.
|
||||
/// 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; private set; }
|
||||
public GetArrayVersion? GetArrayVersion { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Function to get a content version
|
||||
@@ -31,93 +27,63 @@ namespace SabreTools.Matching.Content
|
||||
/// A content version method takes the file path, the file contents,
|
||||
/// and a list of found positions and returns a single string. That
|
||||
/// string is either a version string, in which case it will be appended
|
||||
/// to the protection name, or `null`, in which case it will cause
|
||||
/// the protection to be omitted.
|
||||
/// 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; private set; }
|
||||
public GetStreamVersion? GetStreamVersion { get; }
|
||||
|
||||
#region Generic Constructors
|
||||
|
||||
public ContentMatchSet(byte?[] needle, string protectionName)
|
||||
: this(new List<byte?[]> { needle }, getArrayVersion: null, protectionName) { }
|
||||
public ContentMatchSet(byte?[] needle, string matchName)
|
||||
: this([needle], getArrayVersion: null, matchName) { }
|
||||
|
||||
public ContentMatchSet(List<byte?[]> needles, string protectionName)
|
||||
: this(needles, getArrayVersion: null, protectionName) { }
|
||||
public ContentMatchSet(List<byte?[]> needles, string matchName)
|
||||
: this(needles, getArrayVersion: null, matchName) { }
|
||||
|
||||
public ContentMatchSet(ContentMatch needle, string protectionName)
|
||||
: this(new List<ContentMatch>() { needle }, getArrayVersion: null, protectionName) { }
|
||||
public ContentMatchSet(ContentMatch needle, string matchName)
|
||||
: this([needle], getArrayVersion: null, matchName) { }
|
||||
|
||||
public ContentMatchSet(List<ContentMatch> needles, string protectionName)
|
||||
: this(needles, getArrayVersion: null, protectionName) { }
|
||||
public ContentMatchSet(List<ContentMatch> needles, string matchName)
|
||||
: this(needles, getArrayVersion: null, matchName) { }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Array Constructors
|
||||
|
||||
public ContentMatchSet(byte?[] needle, Func<string, byte[]?, List<int>, string?>? getArrayVersion, string protectionName)
|
||||
: this(new List<byte?[]> { needle }, getArrayVersion, protectionName) { }
|
||||
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 protectionName)
|
||||
{
|
||||
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;
|
||||
ProtectionName = protectionName;
|
||||
}
|
||||
#else
|
||||
public ContentMatchSet(List<byte?[]> needles, Func<string, byte[]?, List<int>, string?>? getArrayVersion, string protectionName)
|
||||
: this(needles.Select(n => new ContentMatch(n)).ToList(), getArrayVersion, protectionName) { }
|
||||
#endif
|
||||
public ContentMatchSet(ContentMatch needle, GetArrayVersion? getArrayVersion, string matchName)
|
||||
: this([needle], getArrayVersion, matchName) { }
|
||||
|
||||
public ContentMatchSet(ContentMatch needle, Func<string, byte[]?, List<int>, string?>? getArrayVersion, string protectionName)
|
||||
: this(new List<ContentMatch>() { needle }, getArrayVersion, protectionName) { }
|
||||
|
||||
public ContentMatchSet(List<ContentMatch> needles, Func<string, byte[]?, List<int>, string?>? getArrayVersion, string protectionName)
|
||||
public ContentMatchSet(List<ContentMatch> needles, GetArrayVersion? getArrayVersion, string matchName)
|
||||
{
|
||||
Matchers = needles;
|
||||
GetArrayVersion = getArrayVersion;
|
||||
ProtectionName = protectionName;
|
||||
MatchName = matchName;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Stream Constructors
|
||||
|
||||
public ContentMatchSet(byte?[] needle, Func<string, Stream?, List<int>, string?>? getStreamVersion, string protectionName)
|
||||
: this(new List<byte?[]> { needle }, getStreamVersion, protectionName) { }
|
||||
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 protectionName)
|
||||
{
|
||||
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;
|
||||
ProtectionName = protectionName;
|
||||
}
|
||||
#else
|
||||
public ContentMatchSet(List<byte?[]> needles, Func<string, Stream?, List<int>, string?>? getStreamVersion, string protectionName)
|
||||
: this(needles.Select(n => new ContentMatch(n)).ToList(), getStreamVersion, protectionName) { }
|
||||
#endif
|
||||
public ContentMatchSet(ContentMatch needle, GetStreamVersion? getStreamVersion, string matchName)
|
||||
: this([needle], getStreamVersion, matchName) { }
|
||||
|
||||
public ContentMatchSet(ContentMatch needle, Func<string, Stream?, List<int>, string?>? getStreamVersion, string protectionName)
|
||||
: this(new List<ContentMatch>() { needle }, getStreamVersion, protectionName) { }
|
||||
|
||||
public ContentMatchSet(List<ContentMatch> needles, Func<string, Stream?, List<int>, string?>? getStreamVersion, string protectionName)
|
||||
public ContentMatchSet(List<ContentMatch> needles, GetStreamVersion? getStreamVersion, string matchName)
|
||||
{
|
||||
Matchers = needles;
|
||||
GetStreamVersion = getStreamVersion;
|
||||
ProtectionName = protectionName;
|
||||
MatchName = matchName;
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -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
|
||||
|
||||
32
SabreTools.Matching/Delegates.cs
Normal file
32
SabreTools.Matching/Delegates.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using System;
|
||||
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);
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,8 +13,8 @@ namespace SabreTools.Matching
|
||||
public IEnumerable<T>? Matchers { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Name of the protection to show
|
||||
/// Unique name for the match set
|
||||
/// </summary>
|
||||
public string? ProtectionName { get; set; }
|
||||
public string? MatchName { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -25,15 +19,9 @@ namespace SabreTools.Matching
|
||||
/// <param name="stack">Array to search</param>
|
||||
/// <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);
|
||||
}
|
||||
/// <returns>List of strings representing the matches, null or empty otherwise</returns>
|
||||
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
|
||||
@@ -42,18 +30,14 @@ namespace SabreTools.Matching
|
||||
/// <param name="stack">Array to search</param>
|
||||
/// <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>String representing the matched protection, null otherwise</returns>
|
||||
/// <returns>String representing the match, null otherwise</returns>
|
||||
public static string? GetFirstMatch(string file, byte[]? stack, IEnumerable<ContentMatchSet>? matchers, bool includeDebug = false)
|
||||
{
|
||||
var contentMatches = FindAllMatches(file, stack, matchers, includeDebug, true);
|
||||
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 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
|
||||
/// <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 matched protections
|
||||
#if NET20 || NET35
|
||||
var matchedProtections = new Queue<string>();
|
||||
#else
|
||||
var matchedProtections = 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)
|
||||
@@ -106,10 +78,10 @@ namespace SabreTools.Matching
|
||||
string positionsString = string.Join(", ", positions);
|
||||
#endif
|
||||
|
||||
// If we there is no version method, just return the protection name
|
||||
// If we there is no version method, just return the match name
|
||||
if (matcher.GetArrayVersion == null)
|
||||
{
|
||||
matchedProtections.Enqueue((matcher.ProtectionName ?? "Unknown Protection") + (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;
|
||||
|
||||
matchedProtections.Enqueue($"{matcher.ProtectionName ?? "Unknown Protection"} {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 protection, bail out here
|
||||
// If we're stopping after the first match, bail out here
|
||||
if (stopAfterFirst)
|
||||
return matchedProtections;
|
||||
return matchesList;
|
||||
}
|
||||
|
||||
return matchedProtections;
|
||||
return matchesList;
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -142,15 +114,9 @@ namespace SabreTools.Matching
|
||||
/// <param name="stack">Stream to search</param>
|
||||
/// <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);
|
||||
}
|
||||
/// <returns>List of strings representing the matches, null or empty otherwise</returns>
|
||||
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
|
||||
@@ -159,18 +125,14 @@ namespace SabreTools.Matching
|
||||
/// <param name="stack">Stream to search</param>
|
||||
/// <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>String representing the matched protection, null otherwise</returns>
|
||||
/// <returns>String representing the match, null otherwise</returns>
|
||||
public static string? GetFirstMatch(string file, Stream? stack, IEnumerable<ContentMatchSet>? matchers, bool includeDebug = false)
|
||||
{
|
||||
var contentMatches = FindAllMatches(file, stack, matchers, includeDebug, true);
|
||||
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 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
|
||||
/// <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 matched protections
|
||||
#if NET20 || NET35
|
||||
var matchedProtections = new Queue<string>();
|
||||
#else
|
||||
var matchedProtections = 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)
|
||||
@@ -223,10 +173,10 @@ namespace SabreTools.Matching
|
||||
string positionsString = string.Join(", ", positions);
|
||||
#endif
|
||||
|
||||
// If we there is no version method, just return the protection name
|
||||
// If we there is no version method, just return the match name
|
||||
if (matcher.GetStreamVersion == null)
|
||||
{
|
||||
matchedProtections.Enqueue((matcher.ProtectionName ?? "Unknown Protection") + (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;
|
||||
|
||||
matchedProtections.Enqueue($"{matcher.ProtectionName ?? "Unknown Protection"} {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 protection, bail out here
|
||||
// If we're stopping after the first match, bail out here
|
||||
if (stopAfterFirst)
|
||||
return matchedProtections;
|
||||
return matchesList;
|
||||
}
|
||||
|
||||
return matchedProtections;
|
||||
return matchesList;
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -258,15 +208,9 @@ namespace SabreTools.Matching
|
||||
/// <param name="file">File path to check for matches</param>
|
||||
/// <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);
|
||||
}
|
||||
/// <returns>List of strings representing the matches, null or empty otherwise</returns>
|
||||
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
|
||||
@@ -274,15 +218,9 @@ namespace SabreTools.Matching
|
||||
/// <param name="files">File paths to check for matches</param>
|
||||
/// <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);
|
||||
}
|
||||
/// <returns>List of strings representing the matches, null or empty otherwise</returns>
|
||||
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
|
||||
@@ -290,18 +228,14 @@ namespace SabreTools.Matching
|
||||
/// <param name="file">File path to check for matches</param>
|
||||
/// <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>String representing the matched protection, null otherwise</returns>
|
||||
/// <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>
|
||||
@@ -310,18 +244,14 @@ namespace SabreTools.Matching
|
||||
/// <param name="files">File paths to check for matches</param>
|
||||
/// <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>String representing the matched protection, null otherwise</returns>
|
||||
/// <returns>String representing the match, null otherwise</returns>
|
||||
public static string? GetFirstMatch(IEnumerable<string> files, IEnumerable<PathMatchSet> matchers, bool any = false)
|
||||
{
|
||||
var contentMatches = FindAllMatches(files, 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>
|
||||
@@ -331,27 +261,15 @@ 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>
|
||||
/// <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
|
||||
/// <returns>List of strings representing the matches, null or empty otherwise</returns>
|
||||
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 matched protections
|
||||
#if NET20 || NET35
|
||||
var matchedProtections = new Queue<string>();
|
||||
#else
|
||||
var matchedProtections = 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)
|
||||
@@ -376,10 +294,10 @@ namespace SabreTools.Matching
|
||||
if (!passes || firstMatchedString == null)
|
||||
continue;
|
||||
|
||||
// If we there is no version method, just return the protection name
|
||||
// If we there is no version method, just return the match name
|
||||
if (matcher.GetVersion == null)
|
||||
{
|
||||
matchedProtections.Enqueue(matcher.ProtectionName ?? "Unknown Protection");
|
||||
matchesList.Add(matcher.MatchName ?? "Unknown");
|
||||
}
|
||||
|
||||
// Otherwise, invoke the version method
|
||||
@@ -390,15 +308,15 @@ namespace SabreTools.Matching
|
||||
if (version == null)
|
||||
continue;
|
||||
|
||||
matchedProtections.Enqueue($"{matcher.ProtectionName ?? "Unknown Protection"} {version}".Trim());
|
||||
matchesList.Add($"{matcher.MatchName ?? "Unknown"} {version}".Trim());
|
||||
}
|
||||
|
||||
// If we're stopping after the first protection, bail out here
|
||||
// If we're stopping after the first match, bail out here
|
||||
if (stopAfterFirst)
|
||||
return matchedProtections;
|
||||
return matchesList;
|
||||
}
|
||||
|
||||
return matchedProtections;
|
||||
return matchesList;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
using System.Collections.Generic;
|
||||
#if NET40_OR_GREATER || NETCOREAPP
|
||||
using System.Linq;
|
||||
#endif
|
||||
|
||||
namespace SabreTools.Matching.Paths
|
||||
{
|
||||
@@ -13,11 +10,7 @@ namespace SabreTools.Matching.Paths
|
||||
/// <summary>
|
||||
/// String to match
|
||||
/// </summary>
|
||||
#if NETFRAMEWORK || NETCOREAPP
|
||||
public string? Needle { get; private set; }
|
||||
#else
|
||||
public string? Needle { get; init; }
|
||||
#endif
|
||||
public string? Needle { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Match exact casing instead of invariant
|
||||
@@ -52,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
|
||||
|
||||
@@ -1,8 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
#if NET40_OR_GREATER || NETCOREAPP
|
||||
using System.Linq;
|
||||
#endif
|
||||
|
||||
namespace SabreTools.Matching.Paths
|
||||
{
|
||||
@@ -17,54 +13,39 @@ namespace SabreTools.Matching.Paths
|
||||
/// <remarks>
|
||||
/// A path version method takes the matched path and an enumerable of files
|
||||
/// and returns a single string. That string is either a version string,
|
||||
/// in which case it will be appended to the protection name, or `null`,
|
||||
/// in which case it will cause the protection to be omitted.
|
||||
/// 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; private set; }
|
||||
public GetPathVersion? GetVersion { get; }
|
||||
|
||||
#region Constructors
|
||||
|
||||
public PathMatchSet(string needle, string protectionName)
|
||||
: this(new List<string> { needle }, null, protectionName) { }
|
||||
public PathMatchSet(string needle, string matchName)
|
||||
: this([needle], null, matchName) { }
|
||||
|
||||
public PathMatchSet(List<string> needles, string protectionName)
|
||||
: this(needles, null, protectionName) { }
|
||||
public PathMatchSet(List<string> needles, string matchName)
|
||||
: this(needles, null, matchName) { }
|
||||
|
||||
public PathMatchSet(string needle, Func<string, IEnumerable<string>?, string?>? getVersion, string protectionName)
|
||||
: this(new List<string> { needle }, getVersion, protectionName) { }
|
||||
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 protectionName)
|
||||
{
|
||||
var matchers = new List<PathMatch>();
|
||||
foreach (var n in needles)
|
||||
{
|
||||
matchers.Add(new PathMatch(n));
|
||||
}
|
||||
public PathMatchSet(List<string> needles, GetPathVersion? getVersion, string matchName)
|
||||
: this(needles.ConvertAll(n => new PathMatch(n)), getVersion, matchName) { }
|
||||
|
||||
Matchers = matchers;
|
||||
GetVersion = getVersion;
|
||||
ProtectionName = protectionName;
|
||||
}
|
||||
#else
|
||||
public PathMatchSet(List<string> needles, Func<string, IEnumerable<string>?, string?>? getVersion, string protectionName)
|
||||
: this(needles.Select(n => new PathMatch(n)).ToList(), getVersion, protectionName) { }
|
||||
#endif
|
||||
public PathMatchSet(PathMatch needle, string matchName)
|
||||
: this([needle], null, matchName) { }
|
||||
|
||||
public PathMatchSet(PathMatch needle, string protectionName)
|
||||
: this(new List<PathMatch>() { needle }, null, protectionName) { }
|
||||
public PathMatchSet(List<PathMatch> needles, string matchName)
|
||||
: this(needles, null, matchName) { }
|
||||
|
||||
public PathMatchSet(List<PathMatch> needles, string protectionName)
|
||||
: this(needles, null, protectionName) { }
|
||||
public PathMatchSet(PathMatch needle, GetPathVersion? getVersion, string matchName)
|
||||
: this([needle], getVersion, matchName) { }
|
||||
|
||||
public PathMatchSet(PathMatch needle, Func<string, IEnumerable<string>?, string?>? getVersion, string protectionName)
|
||||
: this(new List<PathMatch>() { needle }, getVersion, protectionName) { }
|
||||
|
||||
public PathMatchSet(List<PathMatch> needles, Func<string, IEnumerable<string>?, string?>? getVersion, string protectionName)
|
||||
public PathMatchSet(List<PathMatch> needles, GetPathVersion? getVersion, string matchName)
|
||||
{
|
||||
Matchers = needles;
|
||||
GetVersion = getVersion;
|
||||
ProtectionName = protectionName;
|
||||
MatchName = matchName;
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -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
|
||||
|
||||
@@ -1,33 +1,41 @@
|
||||
<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.2</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</TargetFrameworks>
|
||||
<LangVersion>latest</LangVersion>
|
||||
<Nullable>enable</Nullable>
|
||||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
|
||||
<Version>1.3.4</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>
|
||||
</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`))">
|
||||
<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</TargetFrameworks>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Include="../README.md" Pack="true" PackagePath="" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user