Skip to content

Commit ffe385f

Browse files
committed
Add bitsRead computed property to bit readers
1 parent c1aee2a commit ffe385f

3 files changed

Lines changed: 23 additions & 0 deletions

File tree

Sources/BitReader.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ public protocol BitReader: class {
1414
// Amount of bits left to read.
1515
var bitsLeft: Int { get }
1616

17+
// Amount of bits that were already read.
18+
var bitsRead: Int { get }
19+
1720
/// Creates an instance for reading bits (and bytes) from `data`.
1821
init(data: Data)
1922

Sources/LsbBitReader.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ public final class LsbBitReader: ByteReader, BitReader {
2525
}
2626
}
2727

28+
// Amount of bits that were already read.
29+
public var bitsRead: Int {
30+
if self.isFinished {
31+
return 8 * self.size
32+
} else {
33+
return (self.offset - self.data.startIndex) * 8 + self.bitMask.trailingZeroBitCount
34+
}
35+
36+
}
37+
2838
/// Creates an instance for reading bits (and bytes) from `data`.
2939
public override init(data: Data) {
3040
self.currentByte = data.first ?? 0

Sources/MsbBitReader.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ public final class MsbBitReader: ByteReader, BitReader {
2525
}
2626
}
2727

28+
// Amount of bits that were already read.
29+
public var bitsRead: Int {
30+
if self.isFinished {
31+
return 8 * self.size
32+
} else {
33+
return (self.offset - self.data.startIndex) * 8 + self.bitMask.leadingZeroBitCount
34+
}
35+
36+
}
37+
2838
/// Creates an instance for reading bits (and bytes) from `data`.
2939
public override init(data: Data) {
3040
self.currentByte = data.first ?? 0

0 commit comments

Comments
 (0)