Skip to content

Commit f13ddd5

Browse files
committed
Fix bitfield alignment on last group
--- + Update README example
1 parent 9984e9f commit f13ddd5

2 files changed

Lines changed: 26 additions & 18 deletions

File tree

README.md

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Caterpillar - 🐛
22

33
[![python](https://img.shields.io/badge/Python-3.12+-blue?logo=python&logoColor=yellow)](https://www.python.org/downloads/)
4-
![![Latest Version](https://pypi.org/project/caterpillar-py/)](https://img.shields.io/github/v/release/MatrixEditor/caterpillar.svg?logo=github)
4+
![![Latest Version](https://pypi.org/project/caterpillar-py/)](https://img.shields.io/github/v/release/MatrixEditor/caterpillar.svg?logo=github&label=Latest+Version)
55
[![Build and Deploy Docs](https://github.com/MatrixEditor/caterpillar/actions/workflows/python-sphinx.yml/badge.svg)](https://github.com/MatrixEditor/caterpillar/actions/workflows/python-sphinx.yml)
66
[![Run Tests](https://github.com/MatrixEditor/caterpillar/actions/workflows/python-test.yml/badge.svg)](https://github.com/MatrixEditor/caterpillar/actions/workflows/python-test.yml)
77
![GitHub issues](https://img.shields.io/github/issues/MatrixEditor/caterpillar?logo=github)
@@ -40,35 +40,42 @@ options will be added in the future. Documentation is [here >](https://matrixedi
4040
## Give me some code!
4141

4242
```python
43-
from caterpillar.py import *
43+
@bitfield(order=LittleEndian)
44+
class Header:
45+
version : 4 # 4bit integer
46+
valid : 1 # 1bit flag (boolean)
47+
ident : (8, CharFactory) # 8bit char
48+
# automatic alignment to 16bits
4449

4550
@struct(order=LittleEndian)
4651
class Format:
47-
magic: b"ITS MAGIC" # Supports string and byte constants directly
48-
a: uint8 # Primitive data types
49-
b: int32
50-
length: uint8 # String fields with computed lengths
51-
name: String(this.length) # -> you can also use Prefixed(uint8)
52+
magic : b"ITS MAGIC" # Supports string and byte constants directly
53+
header : Header
54+
a : uint8 # Primitive data types
55+
b : int32
56+
length : uint8 # String fields with computed lengths
57+
name : String(this.length) # -> you can also use Prefixed(uint8)
5258

53-
# custom actions, e.g. for hashes
54-
_hash_begin: DigestField.begin("hash", Md5_Algo)
55-
56-
# Sequences with prefixed, computed lengths
57-
names: CString[uint8::]
58-
59-
# automatic hash creation and verification + default value
60-
hash: Md5_Field("hash", verify=True) = None
59+
_hash_begin : DigestField.begin("hash", Md5_Algo) # custom actions, e.g. for hashes
60+
names : CString[uint8::] # Sequences with prefixed, computed lengths
61+
hash : Md5_Field("hash", verify=True) = None # automatic hash creation and verification + default value
6162

6263
# Instantiation (keyword-only arguments, magic is auto-inferred):
63-
obj = Format(a=1, b=2, length=3, name="foo", names=["a", "b"])
64+
obj = Format(
65+
header=Header(version=2, valid=True, ident="F"),
66+
a=1,
67+
b=2,
68+
length=3,
69+
name="foo",
70+
names=["a", "b"]
71+
)
6472
# Packing the object, reads as 'PACK obj FROM Format'
6573
# objects of struct classes can be packed right away
6674
blob = pack(obj, Format)
67-
# results in: b'ITS MAGIC\x01\x02\x00\x00\x00\x03foo\x02a\x00b\x00\xf55...
75+
# results in: b'ITS MAGIC0*\x01\x02\x00\x00\x00\x03foo\x02a\x00b\x00)\x9a...'
6876

6977
# Unpacking the binary data, reads as 'UNPACK Format FROM blob'
7078
obj2 = unpack(Format, blob)
71-
assert obj2.hash == obj.hash
7279
```
7380

7481
This library offers extensive functionality beyond basic struct handling. For further details

src/caterpillar/model/_bitfield.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,7 @@ def __init__(
580580
self.options.update(GLOBAL_BITFIELD_FLAGS)
581581

582582
self.groups = [group for group in self.groups if not group.is_empty()]
583+
self.groups[-1].align_to(self._current_alignment)
583584
# REVISIT: should be enable modification after processing?
584585
del self._bit_pos
585586
del self._current_alignment

0 commit comments

Comments
 (0)