Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bugfix] buffer concatenation #27

Merged
merged 1 commit into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions microschc/binary/buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,13 +321,14 @@ def __add__(self, other: 'Buffer') -> 'Buffer':
bit_shift: int = abs(right.padding_length - left.padding_length)
if right.padding_length > left.padding_length:
new_content: bytes = b''
# shift right to the left
# shift right to the right
carry: int = 0
for b in right.content[::-1]:
sb:int = (b << bit_shift) & 0xff + carry
new_content = sb.to_bytes(1, 'big') + new_content
carry = b >> (8-bit_shift)
new_content = left.content[0:-1] + (left.content[-1] + new_content[0]).to_bytes(1, 'big') + new_content[1:]
carry_mask: int = (1 << bit_shift) - 1
for b in right.content[::]:
sb:int = (b >> bit_shift) + carry
carry = (b & carry_mask) << (8 - bit_shift)
new_content += sb.to_bytes(1, 'big')
new_content = left.content[0:-1] + (left.content[-1] + new_content[0]).to_bytes(1, 'big') + new_content[1:] + carry.to_bytes(1, 'big')
else:
# shift right to the left, careful with the carry that will spill over right's left boundary
new_content: bytes = b''
Expand Down
8 changes: 8 additions & 0 deletions tests/binary/test_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,14 @@ def test_add():

expected: Buffer = Buffer(content=b'\x06', length=4, padding=Padding.LEFT)
assert left_right == expected

left:Buffer = Buffer(content=b'\xe0', length=7, padding=Padding.RIGHT)
right: Buffer = Buffer(content=b'\x05', length=4, padding=Padding.LEFT)
left_right: Buffer = left + right

expected: Buffer = Buffer(content=b'\xe0\xa0', length=11, padding=Padding.RIGHT)
assert left_right == expected


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