Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions PatternScanBench/Implementations/PatternScanALittleBitNaiveFor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System.Collections.Generic;

namespace PatternScanBench.Implementations
{
/// <summary>
/// Pattern scan implementation 'ALittleBitNaiveFor' - by DI20ID
/// https://github.com/DI20ID
/// </summary>
internal class PatternScanALittleBitNaiveFor : PatternScanAlgorithm
{
/// <summary>
/// Authors name.
/// </summary>
internal override string Creator => "DI20ID";
internal PatternScanALittleBitNaiveFor() { }

/// <summary>
/// Initializes a new 'PatternScanTest'.
/// </summary>
/// <param name="cbMemory">The byte array to scan.</param>
/// <returns>An optional string to display next to benchmark results.</returns>
internal override string Init(in byte[] cbMemory)
{
return "";
}

/// <summary>
/// Returns address of pattern using 'ALittleBitNaiveFor' implementation by DI20ID. Can match 0.
/// </summary>
/// <param name="cbMemory">The byte array to scan.</param>
/// <param name="cbPattern">The byte pattern to look for, wildcard positions are replaced by 0.</param>
/// <param name="szMask">A string that determines how pattern should be matched, 'x' is match, '?' acts as wildcard.</param>
/// <returns>-1 if pattern is not found.</returns>
internal override long FindPattern(in byte[] cbMemory, in byte[] cbPattern, string szMask)
{
long ix;
int iy;

List<byte> ls = new List<byte>();
List<int> ini = new List<int>();

int dataLength = cbMemory.Length - cbPattern.Length;

for (iy = cbPattern.Length - 1; iy > -1; iy--)
{
if (szMask[iy] == 'x')
{

ls.Add(cbPattern[iy]);
ini.Add(iy);
}
}

byte[] arr = ls.ToArray();
byte[] arr1 = new byte[arr.Length];

for (ix = 0; ix < dataLength; ix++)
{
if (arr[arr.Length - 1] == cbMemory[ix])
{
bool check = true;

for (iy = arr.Length - 1; iy > -1; iy--)
{
if (arr[iy] == cbMemory[ix + ini[iy]])
continue;
check = false;
break;
}

if (check)
{
return ix;
}
}
}

return -1;
}
}
}
1 change: 1 addition & 0 deletions PatternScanBench/PatternScanBench.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Implementations\PatternScanALittleBitNaiveFor.cs" />
<Compile Include="Implementations\PatternScanNaiveFor.cs" />
<Compile Include="Implementations\PatternScanNaiveLINQ.cs" />
<Compile Include="Implementations\PatternScanBoyerMooreHorspool.cs" />
Expand Down
1 change: 1 addition & 0 deletions PatternScanBench/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Add your implementation here
*/
static readonly Dictionary<string, PatternScanAlgorithm> PATTERN_SCAN_ALGORITHMS = new Dictionary<string, PatternScanAlgorithm>
{
{ "ALittleBitNaiveFor", new PatternScanALittleBitNaiveFor() }, // by DI20ID
{ "LearnMore", new PatternScanLearnMore() }, // by learn_more
{ "Trainer", new PatternScanTrainer() }, // by erfg12
{ "NaiveSIMD", new PatternScanNaiveSIMD() }, // by uberhalit
Expand Down