-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathPopData.cs
More file actions
107 lines (87 loc) · 2.05 KB
/
PopData.cs
File metadata and controls
107 lines (87 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace PopX
{
public static class Data
{
static public byte[] SubArray(this System.Array ParentArray, long Start, long Count)
{
var ChildArray = new byte[Count];
System.Array.Copy(ParentArray, Start, ChildArray, 0, Count);
return ChildArray;
}
public class NotFound : System.Exception
{
public NotFound()
{
}
}
// minimal interface for reading through arrays of data (avoiding conversions)
public interface IIndexer<T>
{
T this [int index] {
get;
}
int Count {
get;
}
};
// char->byte conversion without copying all the chars and converting to a byte array
public class CharsAsBytes : IIndexer<byte>
{
IList<char> Data;
public byte this [int index] {
get { return (byte)Data [index]; }
}
public int Count {
get { return Data.Count; }
}
public CharsAsBytes(IList<char> Data)
{
this.Data = Data;
}
};
// list to indexer
public class ListIndexer<T> : IIndexer<T>
{
IList<T> Data;
public T this [int index] {
get { return Data [index]; }
}
public int Count {
get { return Data.Count; }
}
public ListIndexer(IList<T> Data)
{
this.Data = Data;
}
}
static public int FindPattern(IList<byte> Data,IList<byte> Match,int Start=0)
{
return FindPattern ( Data, new ListIndexer<byte>(Match), Start);
}
static public int FindPattern(IList<byte> Data,IList<char> Match,int Start=0)
{
return FindPattern ( Data, new CharsAsBytes (Match), Start);
}
// throws Data.NotFound if not present
static public int FindPattern(IList<byte> Data,IIndexer<byte> Match,int Start=0)
{
var PrefixLen = Match.Count;
int Position = Start;
while (Position + PrefixLen < Data.Count) {
var IsMatch = true;
for (int i = 0; i < Match.Count; i++) {
var d = Data [Position + i];
var m = Match [i];
IsMatch = IsMatch && (d == m);
}
if (IsMatch)
return Position;
Position++;
}
throw new Data.NotFound();
}
};
}