Skip to content

Commit

Permalink
Merge pull request #10 from henzef/master
Browse files Browse the repository at this point in the history
Allow padding with ones by using the uppercase "P" format character
  • Loading branch information
eerimoq committed Apr 30, 2018
2 parents e99028d + 5ea5585 commit 98dc20e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
11 changes: 7 additions & 4 deletions bitstruct.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def _parse_format(fmt):
if info[0] != "":
endianness = info[0]

if info[1] not in 'supfbtr':
if info[1] not in 'supPfbtr':
raise ValueError("bad char '{}' in format".format(info[1]))

infos.append((info[1], int(info[2]), endianness))
Expand Down Expand Up @@ -147,7 +147,7 @@ def __init__(self, fmt):
self._number_of_arguments = 0

for info in infos:
if info[0] != 'p':
if info[0] not in 'pP':
self._number_of_arguments += 1

def pack(self, *args):
Expand All @@ -173,6 +173,8 @@ def pack(self, *args):
for type_, size, endianness in self._infos:
if type_ == 'p':
bits += size * '0'
elif type_ == 'P':
bits += size * '1'
else:
if type_ == 's':
value_bits = _pack_integer(size, args[i])
Expand Down Expand Up @@ -238,7 +240,7 @@ def unpack(self, data):
offset = 0

for type_, size, endianness in self._infos:
if type_ == 'p':
if type_ in 'pP':
pass
else:
# reverse bytes order for least significant byte first
Expand Down Expand Up @@ -325,7 +327,8 @@ def pack(fmt, *args):
- ``b`` -- boolean
- ``t`` -- text (ascii or utf-8)
- ``r`` -- raw, bytes
- ``p`` -- padding, ignore
- ``p`` -- padding with zeros, ignore
- ``P`` -- padding with ones, ignore
Length is the number of bits to pack the value into.
Expand Down
26 changes: 25 additions & 1 deletion tests/test_bitstruct.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,15 @@ def test_pack(self):
packed = pack('p1u1s6u7u9', 0, -2, 65, 22)
self.assertEqual(packed, b'\x3e\x82\x16')

packed = pack('P1u1s6u7u9', 0, -2, 65, 22)
self.assertEqual(packed, b'\xbe\x82\x16')

packed = pack('p1u1s6p7u9', 0, -2, 22)
self.assertEqual(packed, b'\x3e\x00\x16')

packed = pack('P1u1s6p7u9', 0, -2, 22)
self.assertEqual(packed, b'\xbe\x00\x16')

packed = pack('u1s6f32r43', 0, -2, 3.75, b'\x00\xff\x00\xff\x00\xff')
self.assertEqual(packed, b'\x7c\x80\xe0\x00\x00\x01\xfe\x01\xfe\x01\xc0')

Expand All @@ -45,6 +51,9 @@ def test_pack(self):
packed = pack('b1p6b1', True, True)
self.assertEqual(packed, b'\x81')

packed = pack('b1P6b1', True, True)
self.assertEqual(packed, b'\xff')

packed = pack('u5b2u1', -1, False, 1)
self.assertEqual(packed, b'\xf9')

Expand Down Expand Up @@ -130,6 +139,18 @@ def test_unpack(self):
unpacked = unpack('s4000', packed)
self.assertEqual(unpacked, (0, ))

packed = b'\xbe\x82\x16'
unpacked = unpack('P1u1s6u7u9', packed)
self.assertEqual(unpacked, (0, -2, 65, 22))

packed = b'\x3e\x82\x16'
unpacked = unpack('P1u1s6u7u9', packed)
self.assertEqual(unpacked, (0, -2, 65, 22))

packed = b'\xbe\x82\x16'
unpacked = unpack('p1u1s6u7u9', packed)
self.assertEqual(unpacked, (0, -2, 65, 22))

packed = b'\x3e\x82\x16'
unpacked = unpack('p1u1s6u7u9', packed)
self.assertEqual(unpacked, (0, -2, 65, 22))
Expand Down Expand Up @@ -240,6 +261,9 @@ def test_calcsize(self):
size = calcsize('b1s6u7u9p1t8')
self.assertEqual(size, 32)

size = calcsize('b1s6u7u9P1t8')
self.assertEqual(size, 32)

def test_byteswap(self):
"""Byte swap.
Expand Down Expand Up @@ -525,7 +549,7 @@ def test_empty_format(self):
self.assertEqual(cf.unpack(b'\x00'), ())

def test_byte_order_format(self):
"""Test of a format with only byte order informationx.
"""Test of a format with only byte order information.
"""

Expand Down

0 comments on commit 98dc20e

Please sign in to comment.