mirror of
https://github.com/SabreTools/SabreTools.Matching.git
synced 2026-02-10 21:31:14 +00:00
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fb27246fef |
@@ -55,7 +55,11 @@ namespace SabreTools.Matching
|
||||
/// <param name="stack">Array to search for the given content</param>
|
||||
/// <param name="reverse">True to search from the end of the array, false from the start</param>
|
||||
/// <returns>Tuple of success and found position</returns>
|
||||
#if NET48
|
||||
public (bool success, int position) Match(byte[] stack, bool reverse = false)
|
||||
#else
|
||||
public (bool success, int position) Match(byte[]? stack, bool reverse = false)
|
||||
#endif
|
||||
{
|
||||
// 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)
|
||||
@@ -132,7 +136,11 @@ namespace SabreTools.Matching
|
||||
/// <param name="stack">Stream to search for the given content</param>
|
||||
/// <param name="reverse">True to search from the end of the array, false from the start</param>
|
||||
/// <returns>Tuple of success and found position</returns>
|
||||
#if NET48
|
||||
public (bool success, int position) Match(Stream stack, bool reverse = false)
|
||||
#else
|
||||
public (bool success, int position) Match(Stream? stack, bool reverse = false)
|
||||
#endif
|
||||
{
|
||||
// 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)
|
||||
|
||||
@@ -23,7 +23,7 @@ namespace SabreTools.Matching
|
||||
#if NET48
|
||||
public Func<string, byte[], List<int>, string> GetArrayVersion { get; private set; }
|
||||
#else
|
||||
public Func<string, byte[], List<int>, string>? GetArrayVersion { get; init; }
|
||||
public Func<string, byte[]?, List<int>, string?>? GetArrayVersion { get; init; }
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
@@ -39,7 +39,7 @@ namespace SabreTools.Matching
|
||||
#if NET48
|
||||
public Func<string, Stream, List<int>, string> GetStreamVersion { get; private set; }
|
||||
#else
|
||||
public Func<string, Stream, List<int>, string>? GetStreamVersion { get; init; }
|
||||
public Func<string, Stream?, List<int>, string?>? GetStreamVersion { get; init; }
|
||||
#endif
|
||||
|
||||
#region Generic Constructors
|
||||
@@ -77,16 +77,16 @@ namespace SabreTools.Matching
|
||||
ProtectionName = protectionName;
|
||||
}
|
||||
#else
|
||||
public ContentMatchSet(byte?[] needle, Func<string, byte[], List<int>, string>? getArrayVersion, string protectionName)
|
||||
public ContentMatchSet(byte?[] needle, Func<string, byte[]?, List<int>, string?>? getArrayVersion, string protectionName)
|
||||
: this(new List<byte?[]> { needle }, getArrayVersion, protectionName) { }
|
||||
|
||||
public ContentMatchSet(List<byte?[]> needles, Func<string, byte[], List<int>, string>? getArrayVersion, string protectionName)
|
||||
public ContentMatchSet(List<byte?[]> needles, Func<string, byte[]?, List<int>, string?>? getArrayVersion, string protectionName)
|
||||
: this(needles.Select(n => new ContentMatch(n)).ToList(), getArrayVersion, protectionName) { }
|
||||
|
||||
public ContentMatchSet(ContentMatch needle, Func<string, byte[], List<int>, string>? getArrayVersion, string protectionName)
|
||||
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, Func<string, byte[]?, List<int>, string?>? getArrayVersion, string protectionName)
|
||||
{
|
||||
Matchers = needles;
|
||||
GetArrayVersion = getArrayVersion;
|
||||
@@ -115,16 +115,16 @@ namespace SabreTools.Matching
|
||||
ProtectionName = protectionName;
|
||||
}
|
||||
#else
|
||||
public ContentMatchSet(byte?[] needle, Func<string, Stream, List<int>, string>? getStreamVersion, string protectionName)
|
||||
public ContentMatchSet(byte?[] needle, Func<string, Stream?, List<int>, string?>? getStreamVersion, string protectionName)
|
||||
: this(new List<byte?[]> { needle }, getStreamVersion, protectionName) { }
|
||||
|
||||
public ContentMatchSet(List<byte?[]> needles, Func<string, Stream, List<int>, string>? getStreamVersion, string protectionName)
|
||||
public ContentMatchSet(List<byte?[]> needles, Func<string, Stream?, List<int>, string?>? getStreamVersion, string protectionName)
|
||||
: this(needles.Select(n => new ContentMatch(n)).ToList(), getStreamVersion, protectionName) { }
|
||||
|
||||
public ContentMatchSet(ContentMatch needle, Func<string, Stream, List<int>, string>? getStreamVersion, string protectionName)
|
||||
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, Func<string, Stream?, List<int>, string?>? getStreamVersion, string protectionName)
|
||||
{
|
||||
Matchers = needles;
|
||||
GetStreamVersion = getStreamVersion;
|
||||
@@ -141,7 +141,11 @@ namespace SabreTools.Matching
|
||||
/// </summary>
|
||||
/// <param name="stack">Array to search</param>
|
||||
/// <returns>Tuple of passing status and matching positions</returns>
|
||||
#if NET48
|
||||
public (bool, List<int>) MatchesAll(byte[] stack)
|
||||
#else
|
||||
public (bool, List<int>) MatchesAll(byte[]? stack)
|
||||
#endif
|
||||
{
|
||||
// If no content matches are defined, we fail out
|
||||
if (Matchers == null || !Matchers.Any())
|
||||
@@ -168,7 +172,11 @@ namespace SabreTools.Matching
|
||||
/// </summary>
|
||||
/// <param name="stack">Array to search</param>
|
||||
/// <returns>Tuple of passing status and first matching position</returns>
|
||||
#if NET48
|
||||
public (bool, int) MatchesAny(byte[] stack)
|
||||
#else
|
||||
public (bool, int) MatchesAny(byte[]? stack)
|
||||
#endif
|
||||
{
|
||||
// If no content matches are defined, we fail out
|
||||
if (Matchers == null || !Matchers.Any())
|
||||
@@ -194,14 +202,18 @@ namespace SabreTools.Matching
|
||||
/// </summary>
|
||||
/// <param name="stack">Stream to search</param>
|
||||
/// <returns>Tuple of passing status and matching positions</returns>
|
||||
#if NET48
|
||||
public (bool, List<int>) MatchesAll(Stream stack)
|
||||
#else
|
||||
public (bool, List<int>) MatchesAll(Stream? stack)
|
||||
#endif
|
||||
{
|
||||
// If no content matches are defined, we fail out
|
||||
if (Matchers == null || !Matchers.Any())
|
||||
return (false, new List<int>());
|
||||
|
||||
// Initialize the position list
|
||||
List<int> positions = new List<int>();
|
||||
var positions = new List<int>();
|
||||
|
||||
// Loop through all content matches and make sure all pass
|
||||
foreach (var contentMatch in Matchers)
|
||||
@@ -221,7 +233,11 @@ namespace SabreTools.Matching
|
||||
/// </summary>
|
||||
/// <param name="stack">Stream to search</param>
|
||||
/// <returns>Tuple of passing status and first matching position</returns>
|
||||
#if NET48
|
||||
public (bool, int) MatchesAny(Stream stack)
|
||||
#else
|
||||
public (bool, int) MatchesAny(Stream? stack)
|
||||
#endif
|
||||
{
|
||||
// If no content matches are defined, we fail out
|
||||
if (Matchers == null || !Matchers.Any())
|
||||
|
||||
@@ -8,7 +8,11 @@ namespace SabreTools.Matching
|
||||
/// <summary>
|
||||
/// Find all positions of one array in another, if possible, if possible
|
||||
/// </summary>
|
||||
#if NET48
|
||||
public static List<int> FindAllPositions(this 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 = new List<int>();
|
||||
@@ -80,32 +84,60 @@ namespace SabreTools.Matching
|
||||
/// <summary>
|
||||
/// See if a byte array starts with another
|
||||
/// </summary>
|
||||
#if NET48
|
||||
public static bool StartsWith(this byte[] stack, byte[] needle)
|
||||
#else
|
||||
public static bool StartsWith(this byte[] stack, byte[]? needle)
|
||||
#endif
|
||||
{
|
||||
if (needle == null)
|
||||
return false;
|
||||
|
||||
return stack.FirstPosition(needle, out int _, start: 0, end: 1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// See if a byte array starts with another
|
||||
/// </summary>
|
||||
#if NET48
|
||||
public static bool StartsWith(this byte[] stack, byte?[] needle)
|
||||
#else
|
||||
public static bool StartsWith(this byte[] stack, byte?[]? needle)
|
||||
#endif
|
||||
{
|
||||
if (needle == null)
|
||||
return false;
|
||||
|
||||
return stack.FirstPosition(needle, out int _, start: 0, end: 1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// See if a byte array ends with another
|
||||
/// </summary>
|
||||
#if NET48
|
||||
public static bool EndsWith(this byte[] stack, byte[] needle)
|
||||
#else
|
||||
public static bool EndsWith(this byte[] stack, byte[]? needle)
|
||||
#endif
|
||||
{
|
||||
if (needle == null)
|
||||
return false;
|
||||
|
||||
return stack.FirstPosition(needle, out int _, start: stack.Length - needle.Length);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// See if a byte array ends with another
|
||||
/// </summary>
|
||||
#if NET48
|
||||
public static bool EndsWith(this byte[] stack, byte?[] needle)
|
||||
#else
|
||||
public static bool EndsWith(this byte[] stack, byte?[]? needle)
|
||||
#endif
|
||||
{
|
||||
if (needle == null)
|
||||
return false;
|
||||
|
||||
return stack.FirstPosition(needle, out int _, start: stack.Length - needle.Length);
|
||||
}
|
||||
}
|
||||
|
||||
30
MatchUtil.cs
30
MatchUtil.cs
@@ -23,7 +23,7 @@ namespace SabreTools.Matching
|
||||
#if NET48
|
||||
public static ConcurrentQueue<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)
|
||||
public static ConcurrentQueue<string>? GetAllMatches(string file, byte[]? stack, IEnumerable<ContentMatchSet>? matchers, bool includeDebug = false)
|
||||
#endif
|
||||
{
|
||||
return FindAllMatches(file, stack, matchers, includeDebug, false);
|
||||
@@ -40,7 +40,7 @@ namespace SabreTools.Matching
|
||||
#if NET48
|
||||
public static string GetFirstMatch(string file, byte[] stack, IEnumerable<ContentMatchSet> matchers, bool includeDebug = false)
|
||||
#else
|
||||
public static string? GetFirstMatch(string file, byte[] stack, IEnumerable<ContentMatchSet>? matchers, bool includeDebug = false)
|
||||
public static string? GetFirstMatch(string file, byte[]? stack, IEnumerable<ContentMatchSet>? matchers, bool includeDebug = false)
|
||||
#endif
|
||||
{
|
||||
var contentMatches = FindAllMatches(file, stack, matchers, includeDebug, true);
|
||||
@@ -62,7 +62,7 @@ namespace SabreTools.Matching
|
||||
#if NET48
|
||||
private static ConcurrentQueue<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)
|
||||
private static ConcurrentQueue<string>? FindAllMatches(string file, byte[]? stack, IEnumerable<ContentMatchSet>? matchers, bool includeDebug, bool stopAfterFirst)
|
||||
#endif
|
||||
{
|
||||
// If there's no mappings, we can't match
|
||||
@@ -93,7 +93,7 @@ namespace SabreTools.Matching
|
||||
else
|
||||
{
|
||||
// A null version returned means the check didn't pass at the version step
|
||||
string version = matcher.GetArrayVersion(file, stack, positions);
|
||||
var version = matcher.GetArrayVersion(file, stack, positions);
|
||||
if (version == null)
|
||||
continue;
|
||||
|
||||
@@ -123,7 +123,7 @@ namespace SabreTools.Matching
|
||||
#if NET48
|
||||
public static ConcurrentQueue<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)
|
||||
public static ConcurrentQueue<string>? GetAllMatches(string file, Stream? stack, IEnumerable<ContentMatchSet>? matchers, bool includeDebug = false)
|
||||
#endif
|
||||
{
|
||||
return FindAllMatches(file, stack, matchers, includeDebug, false);
|
||||
@@ -140,7 +140,7 @@ namespace SabreTools.Matching
|
||||
#if NET48
|
||||
public static string GetFirstMatch(string file, Stream stack, IEnumerable<ContentMatchSet> matchers, bool includeDebug = false)
|
||||
#else
|
||||
public static string? GetFirstMatch(string file, Stream stack, IEnumerable<ContentMatchSet>? matchers, bool includeDebug = false)
|
||||
public static string? GetFirstMatch(string file, Stream? stack, IEnumerable<ContentMatchSet>? matchers, bool includeDebug = false)
|
||||
#endif
|
||||
{
|
||||
var contentMatches = FindAllMatches(file, stack, matchers, includeDebug, true);
|
||||
@@ -162,7 +162,7 @@ namespace SabreTools.Matching
|
||||
#if NET48
|
||||
private static ConcurrentQueue<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)
|
||||
private static ConcurrentQueue<string>? FindAllMatches(string file, Stream? stack, IEnumerable<ContentMatchSet>? matchers, bool includeDebug, bool stopAfterFirst)
|
||||
#endif
|
||||
{
|
||||
// If there's no mappings, we can't match
|
||||
@@ -193,7 +193,7 @@ namespace SabreTools.Matching
|
||||
else
|
||||
{
|
||||
// A null version returned means the check didn't pass at the version step
|
||||
string version = matcher.GetStreamVersion(file, stack, positions);
|
||||
var version = matcher.GetStreamVersion(file, stack, positions);
|
||||
if (version == null)
|
||||
continue;
|
||||
|
||||
@@ -298,18 +298,10 @@ namespace SabreTools.Matching
|
||||
{
|
||||
// Determine if the matcher passes
|
||||
bool passes;
|
||||
#if NET48
|
||||
string firstMatchedString;
|
||||
#else
|
||||
string? firstMatchedString;
|
||||
#endif
|
||||
var firstMatchedString = string.Empty;
|
||||
if (any)
|
||||
{
|
||||
#if NET48
|
||||
(bool anyPasses, string matchedString) = matcher.MatchesAny(files);
|
||||
#else
|
||||
(bool anyPasses, string? matchedString) = matcher.MatchesAny(files);
|
||||
#endif
|
||||
(bool anyPasses, var matchedString) = matcher.MatchesAny(files);
|
||||
passes = anyPasses;
|
||||
firstMatchedString = matchedString;
|
||||
}
|
||||
@@ -334,7 +326,7 @@ namespace SabreTools.Matching
|
||||
else
|
||||
{
|
||||
// A null version returned means the check didn't pass at the version step
|
||||
string version = matcher.GetVersion(firstMatchedString, files);
|
||||
var version = matcher.GetVersion(firstMatchedString, files);
|
||||
if (version == null)
|
||||
continue;
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ namespace SabreTools.Matching
|
||||
#if NET48
|
||||
public Func<string, IEnumerable<string>, string> GetVersion { get; private set; }
|
||||
#else
|
||||
public Func<string, IEnumerable<string>, string>? GetVersion { get; init; }
|
||||
public Func<string, IEnumerable<string>, string?>? GetVersion { get; init; }
|
||||
#endif
|
||||
|
||||
#region Constructors
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
<!-- Assembly Properties -->
|
||||
<TargetFrameworks>net48;net6.0;net7.0;net8.0</TargetFrameworks>
|
||||
<RuntimeIdentifiers>win-x86;win-x64;linux-x64;osx-x64</RuntimeIdentifiers>
|
||||
<Version>1.1.0</Version>
|
||||
<Version>1.1.1</Version>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
|
||||
<!-- Package Properties -->
|
||||
|
||||
Reference in New Issue
Block a user