Update simple-serialize.md

This commit is contained in:
Justin 2019-04-13 23:50:23 +10:00 committed by GitHub
parent 0695d0ad1c
commit 35a6311208
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 8 deletions

View File

@ -74,19 +74,17 @@ return b"\x01" if value is True else b"\x00"
### Vectors, containers, lists
```python
# Reccursively serialize fixed-size elements
# Reccursively serialize
fixed_parts = [serialize(element) if is_fixed_size(element) else None for element in value]
fixed_lengths = [len(part) if part != None else BYTES_PER_LENGTH_PREFIX for part in fixed_parts]
# Reccursively serialize variable-size elements
variable_parts = [serialize(element) if is_variable_size(element) else None for element in value]
# Compute and check lengths
fixed_lengths = [len(part) if part != None else BYTES_PER_LENGTH_PREFIX for part in fixed_parts]
variable_lengths = [len(part) if part != None else 0 for part in variable_parts]
# Compute offsets of variable-size elements
assert sum(fixed_lengths + variable_lengths) < 2**(BYTES_PER_LENGTH_PREFIX * BITS_PER_BYTE)
offsets = [sum(fixed_lengths) + sum(variable_lengths[:i]) for i in range(len(value))]
# Interleave offsets with fixed parts
# Compute offsets of variable-size elements, and interleave with fixed parts
offsets = [sum(fixed_lengths) + sum(variable_lengths[:i]) for i in range(len(value))]
fixed_parts = [part if part != None else offsets[i] for i, part in enumerate(fixed_parts)]
# Return the fixed parts (with offsets interleaved) followed by variable parts