forked from jpedrosa/sua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
byte_matcher.swift
304 lines (242 loc) · 8.63 KB
/
byte_matcher.swift
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
public enum ByteMatcherEntry {
case EatWhileDigit
case Next
case SkipToEnd
case MatchEos
case EatOne
case EatUntilOne
case EatBytes
case EatUntilBytes
case EatUntilIncludingBytes
case EatUntil
case EatOn
case EatBytesFromTable
case EatOneFromTable
case EatOneNotFromTable
case EatUntilIncludingBytesFromTable
case EatWhileBytesFromTable
case EatUntilBytesFromTable
case EatBytesFromListAtEnd
}
public protocol ByteMatcherEntryData {
var entry: ByteMatcherEntry { get }
}
public struct ByteMatcher {
struct EmptyParams: ByteMatcherEntryData {
var entry: ByteMatcherEntry
}
struct UInt8Param: ByteMatcherEntryData {
var entry: ByteMatcherEntry
var c: UInt8
}
struct BytesParam: ByteMatcherEntryData {
var entry: ByteMatcherEntry
var bytes: [UInt8]
}
struct UInt8FnParam: ByteMatcherEntryData {
var entry: ByteMatcherEntry
var fn: (c: UInt8) -> Bool
}
struct CtxFnParam: ByteMatcherEntryData {
var entry: ByteMatcherEntry
var fn: (inout ctx: ByteStream) -> Bool
}
struct FirstCharTableParam: ByteMatcherEntryData {
var entry: ByteMatcherEntry
var table: FirstCharTable
}
struct ListParam: ByteMatcherEntryData {
var entry: ByteMatcherEntry
var list: [[UInt8]]
}
var stream = ByteStream()
var list = [ByteMatcherEntryData]()
public mutating func match(string: String) -> Int {
return matchAt(string: string, startIndex: 0)
}
// Returns the length of the match, the difference between the last matched
// index + 1 and the startIndex.
//
// Returns -1 in case the matching was unsuccessful.
public mutating func matchAt(string: String, startIndex: Int) -> Int {
stream.bytes = string.bytes
stream.startIndex = startIndex
stream.currentIndex = startIndex
if doMatch() {
return stream.currentIndex - startIndex
}
return -1
}
public mutating func doDataMatch(data: ByteMatcherEntryData) -> Bool {
switch data.entry {
case .EatWhileDigit:
return stream.eatWhileDigit()
case .SkipToEnd:
return stream.skipToEnd()
case .MatchEos:
return stream.currentIndex >= stream.bytes.count
case .Next:
return stream.next() != nil
case .EatOne:
let c = (data as! UInt8Param).c
return stream.eatOne(c: c)
case .EatUntilOne:
let c = (data as! UInt8Param).c
return stream.eatUntilOne(c: c)
case .EatBytes:
let bytes = (data as! BytesParam).bytes
return stream.eatBytes(bytes: bytes)
case .EatUntilBytes:
let bytes = (data as! BytesParam).bytes
return stream.eatUntilBytes(bytes: bytes)
case .EatUntilIncludingBytes:
let bytes = (data as! BytesParam).bytes
return stream.eatUntilIncludingBytes(bytes: bytes)
case .EatUntil:
let fn = (data as! UInt8FnParam).fn
return stream.eatUntil(fn: fn)
case .EatOn:
let fn = (data as! CtxFnParam).fn
return stream.maybeEat(fn: fn)
case .EatBytesFromTable:
let table = (data as! FirstCharTableParam).table
return stream.eatBytesFromTable(firstCharTable: table)
case .EatOneNotFromTable:
let table = (data as! FirstCharTableParam).table
return stream.eatOneNotFromTable(firstCharTable: table)
case .EatOneFromTable:
let table = (data as! FirstCharTableParam).table
return stream.eatOneFromTable(firstCharTable: table)
case .EatUntilIncludingBytesFromTable:
let table = (data as! FirstCharTableParam).table
return stream.eatUntilIncludingBytesFromTable(firstCharTable: table)
case .EatWhileBytesFromTable:
let table = (data as! FirstCharTableParam).table
return stream.eatWhileBytesFromTable(firstCharTable: table)
case .EatUntilBytesFromTable:
let table = (data as! FirstCharTableParam).table
return stream.eatUntilBytesFromTable(firstCharTable: table)
case .EatBytesFromListAtEnd:
let list = (data as! ListParam).list
return stream.eatBytesFromListAtEnd(bytes: list)
}
}
public mutating func doMatch() -> Bool {
for data in list {
if !doDataMatch(data: data) { // No success.
return false
}
}
return true
}
public mutating func add(entry: ByteMatcherEntry) {
list.append(EmptyParams(entry: entry))
}
public mutating func eatWhileDigit() {
add(entry: .EatWhileDigit)
}
public mutating func next() {
add(entry: .Next)
}
public mutating func skipToEnd() {
add(entry: .SkipToEnd)
}
public mutating func matchEos() {
add(entry: .MatchEos)
}
public mutating func eatOne(c: UInt8) {
list.append(UInt8Param(entry: .EatOne, c: c))
}
public mutating func eatUntilOne(c: UInt8) {
list.append(UInt8Param(entry: .EatUntilOne, c: c))
}
public mutating func eatString(string: String) {
eatBytes(bytes: string.bytes)
}
public mutating func eatBytes(bytes: [UInt8]) {
list.append(BytesParam(entry: .EatBytes, bytes: bytes))
}
public mutating func eatUntilString(string: String) {
eatUntilBytes(bytes: string.bytes)
}
public mutating func eatUntilBytes(bytes: [UInt8]) {
list.append(BytesParam(entry: .EatUntilBytes, bytes: bytes))
}
public mutating func eatUntilIncludingString(string: String) {
eatUntilIncludingBytes(bytes: string.bytes)
}
public mutating func eatUntilIncludingBytes(bytes: [UInt8]) {
list.append(BytesParam(entry: .EatUntilIncludingBytes, bytes: bytes))
}
public mutating func eatUntil(fn: (c: UInt8) -> Bool) {
list.append(UInt8FnParam(entry: .EatUntil, fn: fn))
}
public mutating func eatOn(fn: (inout ctx: ByteStream) -> Bool) {
list.append(CtxFnParam(entry: .EatOn, fn: fn))
}
public mutating func eatStringFromList(list: [String]) {
eatBytesFromTable(table: ByteStream.makeFirstCharTable(strings: list))
}
public mutating func eatBytesFromList(list: [[UInt8]]) {
eatBytesFromTable(table: ByteStream.makeFirstCharTable(list: list))
}
public mutating func eatBytesFromTable(table: FirstCharTable) {
list.append(FirstCharTableParam(entry: .EatBytesFromTable, table: table))
}
public mutating func eatOneNotFromStrings(list: [String]) {
eatOneNotFromTable(table: ByteStream.makeFirstCharTable(strings: list))
}
public mutating func eatOneNotFromBytes(list: [[UInt8]]) {
eatOneNotFromTable(table: ByteStream.makeFirstCharTable(list: list))
}
public mutating func eatOneNotFromTable(table: FirstCharTable) {
list.append(FirstCharTableParam(entry: .EatOneNotFromTable, table: table))
}
public mutating func eatOneFromStrings(list: [String]) {
eatOneFromTable(table: ByteStream.makeFirstCharTable(strings: list))
}
public mutating func eatOneFromBytes(list: [[UInt8]]) {
eatOneFromTable(table: ByteStream.makeFirstCharTable(list: list))
}
public mutating func eatOneFromTable(table: FirstCharTable) {
list.append(FirstCharTableParam(entry: .EatOneFromTable, table: table))
}
public mutating func eatUntilIncludingStringFromList(list: [String]) {
eatUntilIncludingBytesFromTable(table:
ByteStream.makeFirstCharTable(strings: list))
}
public mutating func eatUntilIncludingBytesFromList(list: [[UInt8]]) {
eatUntilIncludingBytesFromTable(table:
ByteStream.makeFirstCharTable(list: list))
}
public mutating func eatUntilIncludingBytesFromTable(table: FirstCharTable) {
list.append(FirstCharTableParam(
entry: .EatUntilIncludingBytesFromTable, table: table))
}
public mutating func eatWhileStringFromList(list: [String]) {
eatWhileBytesFromTable(table: ByteStream.makeFirstCharTable(strings: list))
}
public mutating func eatWhileBytesFromList(list: [[UInt8]]) {
eatWhileBytesFromTable(table: ByteStream.makeFirstCharTable(list: list))
}
public mutating func eatWhileBytesFromTable(table: FirstCharTable) {
list.append(FirstCharTableParam(entry: .EatWhileBytesFromTable,
table: table))
}
public mutating func eatUntilStringFromList(list: [String]) {
eatUntilBytesFromTable(table: ByteStream.makeFirstCharTable(strings: list))
}
public mutating func eatUntilBytesFromList(list: [[UInt8]]) {
eatUntilBytesFromTable(table: ByteStream.makeFirstCharTable(list: list))
}
public mutating func eatUntilBytesFromTable(table: FirstCharTable) {
list.append(FirstCharTableParam(entry: .EatUntilBytesFromTable,
table: table))
}
public mutating func eatStringFromListAtEnd(list: [String]) {
eatBytesFromListAtEnd(list: list.map { $0.bytes })
}
public mutating func eatBytesFromListAtEnd(list: [[UInt8]]) {
self.list.append(ListParam(entry: .EatBytesFromListAtEnd, list: list))
}
}