@@ -98,6 +98,40 @@ s.decode([10, 5, 6])
9898# MyStruct(myInt=10, myInts=[5, 6])
9999```
100100
101+ # String / char[ ] Type
102+
103+ Defining c-string types is a little different. Instead of using
104+ ` size ` in the ` TypeMeta ` , we need to instead use ` chunk_size ` .
105+
106+ This is because the way the struct format is defined for c-strings needs
107+ to know how big the string data is expected to be so that it can put the
108+ whole string in a single variable.
109+
110+ The ` chunk_size ` is also introduced to allow for ` char[][] ` for converting
111+ a list of strings.
112+
113+ ``` c
114+ struct MyStruct {
115+ char myStr[3];
116+ char myStrList[2][3];
117+ };
118+ ```
119+ ``` python
120+ @struct_dataclass
121+ class MyStruct (StructDataclass ):
122+ myStr: Annotated[string_t, TypeMeta[str ](chunk_size = 3 )]
123+ myStrList: Annotated[list[string_t], TypeMeta[str ](size = 2 , chunk_size = 3 )]
124+
125+
126+ s = MyStruct()
127+ s.decode([65 , 66 , 67 , 68 , 69 , 70 , 71 , 72 , 73 ])
128+ # MyStruct(myStr=b"ABC", myStrList=[b"DEF", b"GHI"])
129+ ```
130+
131+ If you instead try to define this as a list of ` char_t ` types,
132+ you would only be able to end up with
133+ ` MyStruct(myStr=[b"A", b"B", b"C"], myStrList=[b"D", b"E", b"F", b"G", b"H", b"I"]) `
134+
101135# The Bits Abstraction
102136
103137This library includes a ` bits ` abstraction to map bits to variables for easier access.
@@ -205,7 +239,6 @@ s.decode([15, 15, 15, 15, 0])
205239# [False, False, False, False],
206240# [True, True, True, True],
207241# [False, False, False, False],
208- # [False, False, False, False]
209242# ]
210243
211244# With the get/set functioned defined, we can access the data
@@ -249,7 +282,6 @@ l.decode([1, 2, 3, 4, 5, 6, 7, 8, 9])
249282# Future Updates
250283
251284- Bitfield: Similar to the ` Bits ` abstraction. An easy way to define bitfields
252- - C-Strings: Make a base class to handle C strings (arrays of chars)
253285- Potentially more ways to define bits (dicts/lists/etc).
254286- Potentially allowing list defaults to be entire pre-defined lists.
255287- ???
0 commit comments