Skip to content

Commit

Permalink
Merge pull request #32 from quentinlampin/bugfix-concatenate-empty-bu…
Browse files Browse the repository at this point in the history
…ffer

[bugfix] concatenate zero-length buffer
  • Loading branch information
quentinlampin authored Jan 14, 2025
2 parents 7981e3e + 3018fc1 commit 9fafb7e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
4 changes: 4 additions & 0 deletions microschc/binary/buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,10 @@ def __add__(self, other: 'Buffer') -> 'Buffer':
left:Buffer = self
right:Buffer = other

if right.length == 0:
new_buffer: Buffer = left.copy()
return new_buffer

# Calculate new length
new_length: int = left.length + right.length

Expand Down
5 changes: 5 additions & 0 deletions tests/binary/test_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,11 @@ def test_add():
left_right: Buffer = left + right
expected: Buffer = Buffer(content=b'\x00\x8a\xf9\x10', length=29, padding=Padding.RIGHT)
assert left_right == expected

left: Buffer = Buffer(content=b'\x00\x8a\xf8', length=23, padding=Padding.RIGHT)
right: Buffer = Buffer(content=b'', length=0)
left_right: Buffer = left + right
assert left_right == left

def test_or():
# 0x08 0x68
Expand Down

0 comments on commit 9fafb7e

Please sign in to comment.