Skip to content

Commit 4c603cd

Browse files
committed
Add bitsLeft to BitReader protocol and make it public
1 parent 199e221 commit 4c603cd

3 files changed

Lines changed: 19 additions & 14 deletions

File tree

Sources/BitReader.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ public protocol BitReader: class {
1111
/// True, if reader's BIT pointer is aligned with the BYTE border.
1212
var isAligned: Bool { get }
1313

14+
// Amount of bits left to read.
15+
var bitsLeft: Int { get }
16+
1417
/// Creates an instance for reading bits (and bytes) from `data`.
1518
init(data: Data)
1619

Sources/LsbBitReader.swift

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@ public final class LsbBitReader: ByteReader, BitReader {
1111
private var bitMask: UInt8 = 1
1212
private var currentByte: UInt8
1313

14-
private var bitsLeft: Int {
14+
/// True, if reader's BIT pointer is aligned with the BYTE border.
15+
public var isAligned: Bool {
16+
return self.bitMask == 1
17+
}
18+
19+
// Amount of bits left to read.
20+
public var bitsLeft: Int {
1521
if isFinished {
1622
return 0
1723
} else {
@@ -27,19 +33,14 @@ public final class LsbBitReader: ByteReader, BitReader {
2733

2834
/**
2935
Converts a `ByteReader` instance into `LsbBitReader`, enabling bits reading capabilities.
30-
Current `offset` value in `byteReader` is preserved.
36+
Current `offset` value of `byteReader` is preserved.
3137
*/
3238
public init(_ byteReader: ByteReader) {
3339
self.currentByte = byteReader.offset < byteReader.data.endIndex ? byteReader.data[byteReader.offset] : 0
3440
super.init(data: byteReader.data)
3541
self.offset = byteReader.offset
3642
}
3743

38-
/// True, if reader's BIT pointer is aligned with the BYTE border.
39-
public var isAligned: Bool {
40-
return self.bitMask == 1
41-
}
42-
4344
/**
4445
Reads bit and returns it, advancing by one BIT position.
4546

Sources/MsbBitReader.swift

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@ public final class MsbBitReader: ByteReader, BitReader {
1111
private var bitMask: UInt8 = 128
1212
private var currentByte: UInt8
1313

14-
private var bitsLeft: Int {
14+
/// True, if reader's BIT pointer is aligned with the BYTE border.
15+
public var isAligned: Bool {
16+
return self.bitMask == 128
17+
}
18+
19+
// Amount of bits left to read.
20+
public var bitsLeft: Int {
1521
if isFinished {
1622
return 0
1723
} else {
@@ -27,19 +33,14 @@ public final class MsbBitReader: ByteReader, BitReader {
2733

2834
/**
2935
Converts a `ByteReader` instance into `MsbBitReader`, enabling bit reading capabilities.
30-
Current `offset` value in `byteReader` is preserved.
36+
Current `offset` value of `byteReader` is preserved.
3137
*/
3238
public init(_ byteReader: ByteReader) {
3339
self.currentByte = byteReader.offset < byteReader.data.endIndex ? byteReader.data[byteReader.offset] : 0
3440
super.init(data: byteReader.data)
3541
self.offset = byteReader.offset
3642
}
3743

38-
/// True, if reader's BIT pointer is aligned with the BYTE border.
39-
public var isAligned: Bool {
40-
return self.bitMask == 128
41-
}
42-
4344
/**
4445
Reads bit and returns it, advancing by one BIT position.
4546

0 commit comments

Comments
 (0)