mirror of
https://github.com/SabreTools/SabreTools.Matching.git
synced 2026-07-09 02:16:09 +00:00
Find and fix equal size issue in streams
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.IO;
|
||||
using SabreTools.Matching.Content;
|
||||
using Xunit;
|
||||
|
||||
@@ -9,7 +8,7 @@ namespace SabreTools.Matching.Test
|
||||
public class MatchUtilTests
|
||||
{
|
||||
[Fact]
|
||||
public void ExactSizeMatch()
|
||||
public void ExactSizeArrayMatch()
|
||||
{
|
||||
byte[] source = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07];
|
||||
byte?[] check = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07];
|
||||
@@ -23,5 +22,23 @@ namespace SabreTools.Matching.Test
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -134,6 +134,10 @@ namespace SabreTools.Matching.Content
|
||||
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 = Start;
|
||||
int end = End;
|
||||
|
||||
Reference in New Issue
Block a user