@@ -29,9 +29,9 @@ def decode_into_bit_array(data: bytes, fill_bits: int = 0) -> bitarray:
2929 :param fill_bits: Number of trailing fill bits to be ignored
3030 :return:
3131 """
32- bit_str = ''
3332 length = len (data )
34-
33+ bits = bitarray (len (data ) * 6 - fill_bits )
34+ bit_pos = 0
3535 for i , c in enumerate (data ):
3636 if not 0x20 <= c <= 0x7e :
3737 raise NonPrintableCharacterException (f"Non printable character: '{ hex (c )} '" )
@@ -43,12 +43,20 @@ def decode_into_bit_array(data: bytes, fill_bits: int = 0) -> bitarray:
4343 if i == length - 1 and fill_bits :
4444 # The last part be shorter than 6 bits and contain fill bits
4545 c = c >> fill_bits
46- bit_str += f'{ c :b} ' .zfill (6 - fill_bits )
46+ bits_to_write = 6 - fill_bits
47+ for bit_idx in range (bits_to_write - 1 , - 1 , - 1 ):
48+ bits [bit_pos ] = (c & (1 << bit_idx )) > 0
49+ bit_pos += 1
4750 else :
48- bit_str += f'{ c :06b} '
51+ bits [bit_pos ] = (c & (1 << 5 )) > 0
52+ bits [bit_pos + 1 ] = (c & (1 << 4 )) > 0
53+ bits [bit_pos + 2 ] = (c & (1 << 3 )) > 0
54+ bits [bit_pos + 3 ] = (c & (1 << 2 )) > 0
55+ bits [bit_pos + 4 ] = (c & (1 << 1 )) > 0
56+ bits [bit_pos + 5 ] = (c & 1 ) > 0
57+ bit_pos += 6
4958
50- bit_arr = bitarray (bit_str )
51- return bit_arr
59+ return bits
5260
5361
5462def chunks (sequence : typing .Sequence [T ], n : int ) -> Generator [typing .Sequence [T ], None , None ]:
0 commit comments