Commit ff1e5a8
authored
Add LSB parsing support to bitmask (#29)
## Goal
This PR adds LSB parsing support to bitmask.
The bitmask parsing is defaulted to MSB (BE). To opt in LSB (LE)
parsing, one needs to specify in the `@ParseStrucut`, `@ParseEnum`, and
`@ParseBitmask` macro with `bitEndian: .little`.
## Design
This PR replaces the old RawBitsInteger-based approach with a new
RawBitsSpan type and uses it to enable endianness-aware bit-level
parsing.
### RawBitsSpan
RawBitsSpan is a ~Escapable, ~Copyable span type that provides a
zero-copy view into a contiguous byte buffer with bit-level granularity.
It tracks a _bitOffset and _bitCount into an underlying Span<UInt8>.
Key operations:
- `load(as:bitCount:)` — extracts bits as a right-aligned fixed-width
integer
- `extracting(first:)` / `extracting(last:)` — non-mutating sub-span
extraction
- `slicing(first:)` / `slicing(last:)` — mutating extraction that
consumes bits from the span
This replaces the previous design where all bits were pre-extracted into
a FixedWidthInteger (RawBitsInteger associated type). The span-based
approach removes the 64/128-bit size limit, avoids copying bits into an
integer container, and decouples conforming types from a specific
integer width.
### Endianness flow
The bitEndian parameter flows through the system as follows:
1. Declaration — user specifies bitEndian: .little on the macro
attribute
2. Macro expansion — MacroConfigurationVisitor extracts the value and
stores it in AccessorInfo
3. Code generation — the macro generates RawBitsSpan initialization and
field slicing calls:
- Big endian (default): span starts at bit offset 0, fields are sliced
with `__slicing(unchecked: (), first:)`, consuming from MSB toward LSB
- Little endian: span starts at bit offset totalBytes * 8 - totalBits,
fields are sliced with `__slicing(unchecked: (), last:)`, consuming from
LSB toward MSB
4. Runtime — `__createFromBits` adjusts the span when
`fieldRequestedBitCount > typeBitCount`, extracting from first (big) or
last (little) to select the significant bits
### ExpressibleByRawBits protocol change
The protocol no longer has an associated `RawBitsInteger` type. The
initializer now takes borrowing RawBitsSpan:
```swift
public protocol ExpressibleByRawBits {
init(bits: borrowing RawBitsSpan) throws
}
```
Conformances for `Bool`, `UInt8`, `Int8`, and other integer types
extract only the bits they need via RawBitsSpan.load().
### Other changes
- CI updated to use Swiftly
- Benchmarks disabled by default
- Removed `__maskParsing` and `ExtractBitsAsIntegerTests` /
`MaskParsingTests` (superseded by `RawBitsSpan` and new tests)
- Refactored macro utility naming (`MacroConfigurationVisitor`,
`extractMacroConfiguration`)
- Added comprehensive test coverage for both endianness modes across
bitmask, enum, and struct parsing1 parent 17e5440 commit ff1e5a8
File tree
47 files changed
+3379
-2574
lines changed- .github/workflows
- Benchmarks
- BenchmarkTypesTests
- BenchmarkTypes
- ParsingBenchmarks
- PrintingBenchmarks
- Sources
- BinaryParseKitClient
- BinaryParseKitMacros/Macros
- Extensions
- ParseBitmask
- ParseEnum
- ParseStruct
- Supports
- BinaryParseKit
- Documentation.docc
- Articles
- Extensions
- Protocols
- Types
- Utils
- Tests
- BinaryParseKitMacroTests
- BinaryParseKitTests
- Extensions
- Parsing
- Printing
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
47 files changed
+3379
-2574
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
19 | | - | |
20 | | - | |
21 | 19 | | |
22 | 20 | | |
23 | 21 | | |
24 | 22 | | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
25 | 37 | | |
26 | | - | |
| 38 | + | |
27 | 39 | | |
28 | 40 | | |
29 | 41 | | |
30 | 42 | | |
31 | 43 | | |
32 | 44 | | |
33 | | - | |
| 45 | + | |
| 46 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
26 | | - | |
27 | | - | |
28 | 26 | | |
29 | 27 | | |
30 | 28 | | |
31 | | - | |
32 | | - | |
33 | 29 | | |
34 | 30 | | |
35 | 31 | | |
36 | 32 | | |
37 | 33 | | |
38 | | - | |
| 34 | + | |
39 | 35 | | |
40 | | - | |
| 36 | + | |
41 | 37 | | |
42 | 38 | | |
43 | 39 | | |
44 | 40 | | |
45 | | - | |
46 | | - | |
47 | 41 | | |
48 | 42 | | |
49 | 43 | | |
50 | | - | |
51 | | - | |
| 44 | + | |
52 | 45 | | |
53 | 46 | | |
54 | 47 | | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
55 | 62 | | |
56 | | - | |
57 | | - | |
| 63 | + | |
| 64 | + | |
58 | 65 | | |
59 | 66 | | |
60 | 67 | | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
66 | 75 | | |
67 | 76 | | |
68 | | - | |
| 77 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
17 | | - | |
18 | | - | |
19 | 17 | | |
20 | 18 | | |
21 | 19 | | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
22 | 34 | | |
23 | | - | |
| 35 | + | |
24 | 36 | | |
25 | 37 | | |
26 | | - | |
27 | | - | |
| 38 | + | |
| 39 | + | |
28 | 40 | | |
29 | 41 | | |
30 | 42 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
23 | | - | |
| 23 | + | |
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
| |||
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
| 36 | + | |
36 | 37 | | |
37 | 38 | | |
38 | 39 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
14 | | - | |
15 | | - | |
16 | 14 | | |
17 | 15 | | |
18 | 16 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
14 | | - | |
15 | | - | |
16 | 14 | | |
17 | 15 | | |
18 | 16 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
14 | | - | |
15 | | - | |
16 | 14 | | |
17 | 15 | | |
18 | 16 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
11 | | - | |
12 | | - | |
13 | | - | |
14 | | - | |
| 11 | + | |
| 12 | + | |
15 | 13 | | |
16 | 14 | | |
17 | 15 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
0 commit comments