Skip to content

Commit edc6669

Browse files
committed
Add preconditions to bit readers' bit() and int(fromBits:) functions
These preconditions similar to ByteReader's ones prevent unexpected behaviour and replace general out-of-bounds errors.
1 parent 63afc6e commit edc6669

2 files changed

Lines changed: 12 additions & 8 deletions

File tree

Sources/LsbBitReader.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ public final class LsbBitReader: ByteReader, BitReader {
4040
/**
4141
Reads bit and returns it, advancing by one BIT position.
4242

43-
- Warning: Doesn't check if there is any data left. It is advisable to use `isFinished` BEFORE calling this method
44-
to check if the end is reached.
43+
- Precondition: There MUST be enough data left.
4544
*/
4645
public func bit() -> UInt8 {
46+
precondition(bitsLeft >= 1)
4747
let bit: UInt8 = self.currentByte & self.bitMask > 0 ? 1 : 0
4848

4949
if self.bitMask == 128 {
@@ -59,12 +59,14 @@ public final class LsbBitReader: ByteReader, BitReader {
5959
/**
6060
Reads `count` bits and returns them as a `Int` number, advancing by `count` BIT positions.
6161

62-
- Warning: Doesn't check if there is any data left. It is advisable to use `isFinished` BEFORE calling this method
63-
to check if the end is reached.
62+
- Precondition: There MUST be enough data left.
63+
- Precondition: Parameter `fromBits` MUST not be less than 0.
6464
*/
6565
public func int(fromBits count: Int) -> Int {
66+
precondition(count >= 0)
6667
guard count > 0
6768
else { return 0 }
69+
precondition(bitsLeft >= count)
6870

6971
var result = 0
7072
for i in 0..<count {

Sources/MsbBitReader.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ public final class MsbBitReader: ByteReader, BitReader {
4040
/**
4141
Reads bit and returns it, advancing by one BIT position.
4242

43-
- Warning: Doesn't check if there is any data left. It is advisable to use `isFinished` BEFORE calling this method
44-
to check if the end is reached.
43+
- Precondition: There MUST be enough data left.
4544
*/
4645
public func bit() -> UInt8 {
46+
precondition(bitsLeft >= 1)
4747
let bit: UInt8 = self.currentByte & self.bitMask > 0 ? 1 : 0
4848

4949
if self.bitMask == 1 {
@@ -59,12 +59,14 @@ public final class MsbBitReader: ByteReader, BitReader {
5959
/**
6060
Reads `count` bits and returns them as a `Int` number, advancing by `count` BIT positions.
6161

62-
- Warning: Doesn't check if there is any data left. It is advisable to use `isFinished` BEFORE calling this method
63-
to check if the end is reached.
62+
- Precondition: There MUST be enough data left.
63+
- Precondition: Parameter `fromBits` MUST not be less than 0.
6464
*/
6565
public func int(fromBits count: Int) -> Int {
66+
precondition(count >= 0)
6667
guard count > 0
6768
else { return 0 }
69+
precondition(bitsLeft >= count)
6870

6971
var result = 0
7072
for i in 0..<count {

0 commit comments

Comments
 (0)