Skip to content

Commit e19ddd8

Browse files
committed
rle: Use bytestrings instead of arrays
1 parent db25341 commit e19ddd8

1 file changed

Lines changed: 11 additions & 13 deletions

File tree

bin2hpm/rle.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
See LICENSE.txt for license details.
55
"""
66

7-
from array import array
8-
97
RLE_MAX_BLKSIZE=128
108

9+
def to_byte(b):
10+
return bytes([b])
11+
1112
def encode(data):
1213
def check_block(data):
1314
n = 1
@@ -18,9 +19,9 @@ def check_block(data):
1819
print('Performing RLE compression ...')
1920
orig_len = len(data)
2021

21-
result = array('B')
22+
result = b''
2223
while len(data):
23-
tmp = array('B')
24+
tmp = b''
2425
while len(data) and len(tmp) < RLE_MAX_BLKSIZE:
2526
n, val = check_block(data)
2627
if n > 2:
@@ -29,25 +30,22 @@ def check_block(data):
2930
elif n == 2:
3031
n = 1
3132
data = data[n:]
32-
tmp.append(val)
33+
tmp += to_byte(val)
3334
if len(tmp):
34-
result.append(len(tmp) - 1)
35-
result.extend(tmp)
35+
result += to_byte(len(tmp) - 1)
36+
result += tmp
3637
if n < 2:
3738
continue
38-
result.append((n - 2) | 0x80)
39-
result.append(val)
39+
result += to_byte((n - 2) | 0x80)
40+
result += to_byte(val)
4041

4142
comp_len = len(result)
4243
comp_ratio = 100 * comp_len / orig_len
4344

4445
print(f'Compressed size {comp_len} bytes, ratio {comp_ratio:.1f}%')
45-
return bytes(result)
46+
return result
4647

4748
def decode(data):
48-
def to_byte(b):
49-
return b.to_bytes(1, 'little')
50-
5149
result = b''
5250
while (len(data)):
5351
if data[0] & 0x80:

0 commit comments

Comments
 (0)