55
66import 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+ */
812public final class LsbBitReader : ByteReader , BitReader {
913
1014 private var bitMask : UInt8 = 1
1115
16+ /// True, if reader's BIT pointer is aligned with the BYTE border.
1217 public var isAligned : Bool {
1318 return self . bitMask == 1
1419 }
1520
16- public func bits( count: Int ) -> [ UInt8 ] {
17- guard count > 0 else {
18- return [ ]
19- }
21+ /**
22+ Reads bit and returns it, advancing by one BIT position.
2023
21- var array : [ UInt8 ] = Array ( repeating: 0 , count: count)
22- for i in 0 ..< count {
23- array [ i] = self . data [ self . offset] & self . bitMask > 0 ? 1 : 0
24+ - Warning: Doesn't check if there is any data left. It is advisable to use `isFinished` BEFORE calling this method
25+ to check if the end is reached.
26+ */
27+ public func bit( ) -> UInt8 {
28+ let bit : UInt8 = self . data [ self . offset] & self . bitMask > 0 ? 1 : 0
2429
25- if self . bitMask == 128 {
26- self . offset += 1
27- self . bitMask = 1
28- } else {
29- self . bitMask <<= 1
30- }
30+ if self . bitMask == 128 {
31+ self . offset += 1
32+ self . bitMask = 1
33+ } else {
34+ self . bitMask <<= 1
3135 }
3236
33- return array
37+ return bit
3438 }
3539
36- public func intFromBits( count: Int ) -> Int {
37- guard count > 0 else {
38- return 0
39- }
40+ /**
41+ Reads `count` bits and returns them as a `Int` number, advancing by `count` BIT positions.
42+
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.
45+ */
46+ public func int( fromBits count: Int ) -> Int {
47+ guard count > 0
48+ else { return 0 }
4049
4150 var result = 0
4251 for i in 0 ..< count {
@@ -56,47 +65,78 @@ public final class LsbBitReader: ByteReader, BitReader {
5665 return result
5766 }
5867
59- public func bit( ) -> Int {
60- let bit = self . data [ self . offset] & self . bitMask > 0 ? 1 : 0
61-
62- if self . bitMask == 128 {
63- self . offset += 1
64- self . bitMask = 1
65- } else {
66- self . bitMask <<= 1
67- }
68-
69- return bit
70- }
68+ /**
69+ Aligns reader's BIT pointer to the BYTE border, i.e. moves BIT pointer to the first BIT of the next BYTE.
7170
71+ - Note: If reader is already aligned, then does nothing.
72+ - Warning: Doesn't check if there is any data left. It is advisable to use `isFinished` AFTER calling this method
73+ to check if the end was reached.
74+ */
7275 public func align( ) {
73- guard self . bitMask != 1 else {
74- return
75- }
76+ guard self . bitMask != 1
77+ else { return }
78+
7679 self . bitMask = 1
7780 self . offset += 1
7881 }
7982
83+ // MARK: ByteReader's methods.
84+
85+ /**
86+ Reads byte and returns it, advancing by one BYTE position.
87+
88+ - Warning: Doesn't check if there is any data left. It is advisable to use `isFinished` BEFORE calling this method
89+ to check if the end is reached.
90+ - Precondition: Reader MUST be aligned.
91+ */
8092 public override func byte( ) -> UInt8 {
8193 precondition ( isAligned, " BitReader is not aligned. " )
8294 return super. byte ( )
8395 }
8496
97+ /**
98+ Reads `count` bytes and returns them as an array of `UInt8`, advancing by `count` BYTE positions.
99+
100+ - Warning: Doesn't check if there is any data left. It is advisable to use `isFinished` BEFORE calling this method
101+ to check if the end is reached.
102+ - Precondition: Reader MUST be aligned.
103+ */
85104 public override func bytes( count: Int ) -> [ UInt8 ] {
86105 precondition ( isAligned, " BitReader is not aligned. " )
87106 return super. bytes ( count: count)
88107 }
89108
109+ /**
110+ Reads 8 bytes and returns them as a `UInt64` number, advancing by 8 BYTE positions.
111+
112+ - Warning: Doesn't check if there is any data left. It is advisable to use `isFinished` BEFORE calling this method
113+ to check if the end is reached.
114+ - Precondition: Reader MUST be aligned.
115+ */
90116 public override func uint64( ) -> UInt64 {
91117 precondition ( isAligned, " BitReader is not aligned. " )
92118 return super. uint64 ( )
93119 }
94120
121+ /**
122+ Reads 4 bytes and returns them as a `UInt32` number, advancing by 4 BYTE positions.
123+
124+ - Warning: Doesn't check if there is any data left. It is advisable to use `isFinished` BEFORE calling this method
125+ to check if the end is reached.
126+ - Precondition: Reader MUST be aligned.
127+ */
95128 public override func uint32( ) -> UInt32 {
96129 precondition ( isAligned, " BitReader is not aligned. " )
97130 return super. uint32 ( )
98131 }
99132
133+ /**
134+ Reads 2 bytes and returns them as a `UInt16` number, advancing by 2 BYTE positions.
135+
136+ - Warning: Doesn't check if there is any data left. It is advisable to use `isFinished` BEFORE calling this method
137+ to check if the end is reached.
138+ - Precondition: Reader MUST be aligned.
139+ */
100140 public override func uint16( ) -> UInt16 {
101141 precondition ( isAligned, " BitReader is not aligned. " )
102142 return super. uint16 ( )
0 commit comments