You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This PR provides a more general protocol `Matchable` along side of
`MatchableRawRepresentable` and provides a conformance when the
`RawValue` conforms to `Matchable`.
More documentation is provided.
Use `@matchDefault` to handle unrecognized values:
252
+
253
+
```swift
254
+
@ParseEnum
255
+
enumPacketType {
256
+
@match(byte:0x01)
257
+
caseknown
258
+
259
+
@matchDefault
260
+
caseunknown
261
+
}
262
+
```
263
+
264
+
Available enum parsing macros:
265
+
266
+
-**`@ParseEnum`** - Mark an enum for binary parsing
267
+
-**`@match(byte: UInt8)`** - Match a single byte
268
+
-**`@match(bytes: [UInt8])`** - Match a sequence of bytes, but doesn't shrink the remaining buffer
269
+
-**`@matchAndTake(byte:)`** and **`@matchAndTake(bytes:)`** - Match and consume bytes in the remaining buffer
270
+
-**`@matchDefault`** - Default case for unrecognized patterns, which doesn't consume any bytes in the remaining buffer
271
+
-**`@parse`** - Parse associated values (same options as struct fields)
272
+
-**`@skip`** - Skip bytes (same options as struct fields)
273
+
274
+
### Protocol Conformances
275
+
276
+
BinaryParseKit defines four parsing protocols:
277
+
278
+
-**`Parsable`** - Basic parsing without additional parameters
279
+
-**`EndianParsable`** - Parsing with endianness specification
280
+
-**`SizedParsable`** - Parsing with byte count specification
281
+
-**`EndianSizedParsable`** - Parsing with both endianness and byte count
282
+
283
+
Most built-in types already conform to these protocols. For custom types, implement the appropriate protocol(s).
284
+
285
+
In addition, as mentioned in the previous enum parsing section,
286
+
`Matchable` and `MatchableRawRepresentable` is introduced to allow each case to provide bytes for matching in the process.
190
287
191
288
## Contributing
192
289
@@ -208,10 +305,10 @@ If you encounter any issues, have feature requests, or want to suggest improveme
208
305
209
306
Some areas we're considering for future development:
210
307
211
-
-**Enum parsing** - Support for parsing enums with associated values
212
-
-**More convenient APIs** - Shorter syntax and better autocomplete support
213
-
-**Advanced validation** - Runtime validation of parsing constraints
214
-
-**Performance optimizations** - Further optimization of generated parsing code
308
+
-**More convenient APIs** - Shorter syntax and better autocomplete support, such as merging `ParseStruct` and `ParseEnum`
309
+
-**Advanced validation** - Runtime validation of parsing constraints, such as require minimal byte size checking in front instead of at each parsing
310
+
-**Performance optimizations** - Further optimization of generated parsing code, such as linear time enum matching for constant bytes provided (`O( max(n, m) )` instead of `O(n * m)`, where `n` is the number of cases and `m` is the max number of bytes provided for each case)
311
+
-**Porting to prio iOS/macOS 26** - because `Span` is introduced only in iOS/macOS 26, port of using `withUnsafePointer` can be provided to prior versions of OSes for better compatibility
215
312
216
313
Your feedback on these directions and other ideas is highly appreciated!
0 commit comments