Skip to content

Commit be7d1dc

Browse files
committed
Add conversion initializers from ByteReader to Lsb/MsbBitReader
1 parent 4024351 commit be7d1dc

3 files changed

Lines changed: 24 additions & 8 deletions

File tree

Sources/BitReader.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ public protocol BitReader: class {
1414
/// Creates an instance for reading bits (and bytes) from `data`.
1515
init(data: Data)
1616

17+
// TODO: Add `init(_ byteReader: ByteReader)`?
18+
1719
/// Reads bit and returns it, advancing by one BIT position.
1820
func bit() -> UInt8
1921

Sources/LsbBitReader.swift

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55

66
import Foundation
77

8-
/**
9-
A type that contains functions for reading `Data` bit-by-bit and byte-by-byte,
10-
assuming "LSB 0" bit numbering scheme.
11-
*/
8+
/// A type that contains functions for reading `Data` bit-by-bit and byte-by-byte, assuming "LSB0" bit numbering scheme.
129
public final class LsbBitReader: ByteReader, BitReader {
1310

1411
private var bitMask: UInt8 = 1
@@ -28,6 +25,16 @@ public final class LsbBitReader: ByteReader, BitReader {
2825
super.init(data: data)
2926
}
3027

28+
/**
29+
Converts a `ByteReader` instance into `LsbBitReader`, enabling bits reading capabilities.
30+
Current `offset` value in `byteReader` is preserved.
31+
*/
32+
public init(_ byteReader: ByteReader) {
33+
self.currentByte = byteReader.offset < byteReader.data.endIndex ? byteReader.data[byteReader.offset] : 0
34+
super.init(data: byteReader.data)
35+
self.offset = byteReader.offset
36+
}
37+
3138
/// True, if reader's BIT pointer is aligned with the BYTE border.
3239
public var isAligned: Bool {
3340
return self.bitMask == 1

Sources/MsbBitReader.swift

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55

66
import Foundation
77

8-
/**
9-
A type that contains functions for reading `Data` bit-by-bit and byte-by-byte,
10-
assuming "MSB 0" bit numbering scheme.
11-
*/
8+
/// A type that contains functions for reading `Data` bit-by-bit and byte-by-byte, assuming "MSB0" bit numbering scheme.
129
public final class MsbBitReader: ByteReader, BitReader {
1310

1411
private var bitMask: UInt8 = 128
@@ -28,6 +25,16 @@ public final class MsbBitReader: ByteReader, BitReader {
2825
super.init(data: data)
2926
}
3027

28+
/**
29+
Converts a `ByteReader` instance into `MsbBitReader`, enabling bit reading capabilities.
30+
Current `offset` value in `byteReader` is preserved.
31+
*/
32+
public init(_ byteReader: ByteReader) {
33+
self.currentByte = byteReader.offset < byteReader.data.endIndex ? byteReader.data[byteReader.offset] : 0
34+
super.init(data: byteReader.data)
35+
self.offset = byteReader.offset
36+
}
37+
3138
/// True, if reader's BIT pointer is aligned with the BYTE border.
3239
public var isAligned: Bool {
3340
return self.bitMask == 128

0 commit comments

Comments
 (0)