From 35a6311208a61bf2b09850bb7731aa0c968066e4 Mon Sep 17 00:00:00 2001 From: Justin Date: Sat, 13 Apr 2019 23:50:23 +1000 Subject: [PATCH] Update simple-serialize.md --- specs/simple-serialize.md | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/specs/simple-serialize.md b/specs/simple-serialize.md index bc1fa6ccb..5aba9aff5 100644 --- a/specs/simple-serialize.md +++ b/specs/simple-serialize.md @@ -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