Skip to content

Commit 16e184f

Browse files
committed
Move bits(count:) function to classes from extension to allow preconditions
1 parent 0c359c3 commit 16e184f

4 files changed

Lines changed: 45 additions & 23 deletions

File tree

Sources/BitReader.swift

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,3 @@ public protocol BitReader: class {
4444
func uint16() -> UInt16
4545

4646
}
47-
48-
extension BitReader {
49-
50-
/**
51-
- Warning: Doesn't check if there is any data left.
52-
*/
53-
public func bits(count: Int) -> [UInt8] {
54-
guard count > 0
55-
else { return [] }
56-
57-
var array = [UInt8]()
58-
array.reserveCapacity(count)
59-
for _ in 0..<count {
60-
array.append(self.bit())
61-
}
62-
63-
return array
64-
}
65-
66-
}

Sources/ByteReader.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ public class ByteReader {
4848
/**
4949
Reads `count` bytes and returns them as an array of `UInt8`, advancing by `count` positions.
5050

51-
- Precondition: There MUST be enough data left.
5251
- Precondition: Parameter `count` MUST not be less than 0.
52+
- Precondition: There MUST be enough data left.
5353
*/
5454
public func bytes(count: Int) -> [UInt8] {
5555
precondition(count >= 0)

Sources/LsbBitReader.swift

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,31 @@ public final class LsbBitReader: ByteReader, BitReader {
5757
}
5858

5959
/**
60-
Reads `count` bits and returns them as a `Int` number, advancing by `count` BIT positions.
60+
Reads `count` bits and returns them as an array of `UInt8`, advancing by `count` BIT positions.
6161

62+
- Precondition: Parameter `count` MUST not be less than 0.
6263
- Precondition: There MUST be enough data left.
64+
*/
65+
public func bits(count: Int) -> [UInt8] {
66+
precondition(count >= 0)
67+
guard count > 0
68+
else { return [] }
69+
precondition(bitsLeft >= count)
70+
71+
var array = [UInt8]()
72+
array.reserveCapacity(count)
73+
for _ in 0..<count {
74+
array.append(self.bit())
75+
}
76+
77+
return array
78+
}
79+
80+
/**
81+
Reads `count` bits and returns them as a `Int` number, advancing by `count` BIT positions.
82+
6383
- Precondition: Parameter `fromBits` MUST not be less than 0.
84+
- Precondition: There MUST be enough data left.
6485
*/
6586
public func int(fromBits count: Int) -> Int {
6687
precondition(count >= 0)

Sources/MsbBitReader.swift

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,28 @@ public final class MsbBitReader: ByteReader, BitReader {
5757
}
5858

5959
/**
60-
Reads `count` bits and returns them as a `Int` number, advancing by `count` BIT positions.
60+
Reads `count` bits and returns them as an array of `UInt8`, advancing by `count` BIT positions.
61+
62+
- Precondition: Parameter `count` MUST not be less than 0.
63+
- Precondition: There MUST be enough data left.
64+
*/
65+
public func bits(count: Int) -> [UInt8] {
66+
precondition(count >= 0)
67+
guard count > 0
68+
else { return [] }
69+
precondition(bitsLeft >= count)
70+
71+
var array = [UInt8]()
72+
array.reserveCapacity(count)
73+
for _ in 0..<count {
74+
array.append(self.bit())
75+
}
76+
77+
return array
78+
}
79+
80+
/**
81+
Reads `fromBits` bits and returns them as a `Int` number, advancing by `count` BIT positions.
6182

6283
- Precondition: Parameter `fromBits` MUST not be less than 0.
6384
- Precondition: There MUST be enough data left.

0 commit comments

Comments
 (0)