Skip to content

Commit c17eca8

Browse files
committed
Update docs of Lsb/MsbBitReader
1 parent 14d7261 commit c17eca8

2 files changed

Lines changed: 149 additions & 131 deletions

File tree

Sources/LsbBitReader.swift

Lines changed: 74 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Foundation
77

88
/**
99
A type that contains functions for reading `Data` bit-by-bit using "LSB0" bit numbering scheme and byte-by-byte in the
10-
Little endian order.
10+
Little Endian order.
1111
*/
1212
public final class LsbBitReader: BitReader {
1313

@@ -21,9 +21,9 @@ public final class LsbBitReader: BitReader {
2121
public let data: Data
2222

2323
/**
24-
The offset to the byte in `data` right after the `currentByte`.
24+
Offset to a byte in the `data` which will be read next.
2525

26-
- Precondition: The reader MUST be aligned when accessing the setter of `offset`.
26+
- Precondition: The reader must be aligned when accessing the setter of `offset`.
2727
*/
2828
public var offset: Int {
2929
willSet {
@@ -36,7 +36,7 @@ public final class LsbBitReader: BitReader {
3636
}
3737
}
3838

39-
/// True, if reader's BIT pointer is aligned with the BYTE border.
39+
/// True, if a bit pointer is aligned to a byte boundary.
4040
public var isAligned: Bool {
4141
return self.bitMask == 1
4242
}
@@ -53,7 +53,7 @@ public final class LsbBitReader: BitReader {
5353
return bytesRead * 8 + bitMask.trailingZeroBitCount
5454
}
5555

56-
/// Creates an instance for reading bits (and bytes) from `data`.
56+
/// Creates an instance for reading bits (and bytes) from the `data`.
5757
public init(data: Data) {
5858
self.size = data.count
5959
self.data = data
@@ -62,8 +62,8 @@ public final class LsbBitReader: BitReader {
6262
}
6363

6464
/**
65-
Converts a `ByteReader` instance into `LsbBitReader`, enabling bit reading capabilities. Current `offset` value of
66-
`byteReader` is preserved.
65+
Converts a `ByteReader` instance into a `LsbBitReader`, enabling bit reading capabilities. The current `offset`
66+
value of the `byteReader` is preserved.
6767
*/
6868
public convenience init(_ byteReader: ByteReader) {
6969
self.init(data: byteReader.data)
@@ -72,10 +72,10 @@ public final class LsbBitReader: BitReader {
7272
}
7373

7474
/**
75-
Advances reader's BIT pointer by specified amount of bits (default is 1).
75+
Advances a bit pointer by the specified amount of bits (the default value is 1).
7676

77-
- Warning: Doesn't check if there is any data left. It is advised to use `isFinished` AFTER calling this method
78-
to check if the end was reached.
77+
- Warning: This function doesn't check if there is any data left. It is advised to use `isFinished` after calling
78+
this method to check if the end was reached.
7979
*/
8080
public func advance(by count: Int = 1) {
8181
for _ in 0..<count {
@@ -89,9 +89,9 @@ public final class LsbBitReader: BitReader {
8989
}
9090

9191
/**
92-
Reads bit and returns it, advancing by one BIT position.
92+
Reads a bit and returns it, advancing by one bit position.
9393

94-
- Precondition: There MUST be enough data left.
94+
- Precondition: There must be enough data left.
9595
*/
9696
public func bit() -> UInt8 {
9797
precondition(bitsLeft >= 1)
@@ -108,10 +108,10 @@ public final class LsbBitReader: BitReader {
108108
}
109109

110110
/**
111-
Reads `count` bits and returns them as an array of `UInt8`, advancing by `count` BIT positions.
111+
Reads `count` bits and returns them as a `[UInt8]` array, advancing by `count` bit positions.
112112

113-
- Precondition: Parameter `count` MUST not be less than 0.
114-
- Precondition: There MUST be enough data left.
113+
- Precondition: Parameter `count` must non-negative
114+
- Precondition: There must be enough data left.
115115
*/
116116
public func bits(count: Int) -> [UInt8] {
117117
precondition(count >= 0)
@@ -127,11 +127,16 @@ public final class LsbBitReader: BitReader {
127127
}
128128

129129
/**
130-
Reads `fromBits` bits and returns them as an `Int` number, advancing by `fromBits` BIT positions.
130+
Reads `fromBits` bits, treating them as a binary `represenation` of a signed integer, and returns the result as a
131+
`Int` number, advancing by `fromBits` bit positions.
131132

132-
- Precondition: Parameter `fromBits` MUST be from `0...Int.bitWidth` range, i.e. it MUST not exceed maximum bit
133-
width of `Int` type on the current platform.
134-
- Precondition: There MUST be enough data left.
133+
If the `representation` doesn't match the representation that was used to produce the data then the result may be
134+
incorrect.
135+
136+
The default value of `representation` is `SignedNumberRepresentation.twoComplementNegatives`.
137+
138+
- Precondition: Parameter `fromBits` must be in the `0...Int.bitWidth` range.
139+
- Precondition: There must be enough data left.
135140
*/
136141
public func signedInt(fromBits count: Int, representation: SignedNumberRepresentation = .twoComplementNegatives) -> Int {
137142
precondition(0...Int.bitWidth ~= count)
@@ -172,11 +177,10 @@ public final class LsbBitReader: BitReader {
172177
}
173178

174179
/**
175-
Reads `fromBits` bits and returns them as an `UInt8` number, advancing by `fromBits` BIT positions.
180+
Reads `fromBits` bits and returns them as a `UInt8` number, advancing by `fromBits` bit positions.
176181

177-
- Precondition: Parameter `fromBits` MUST be from `0...8` range, i.e. it MUST not exceed maximum bit width of
178-
`UInt8` type on the current platform.
179-
- Precondition: There MUST be enough data left.
182+
- Precondition: Parameter `fromBits` must be in the `0...8` range.
183+
- Precondition: There must be enough data left.
180184
*/
181185
public func byte(fromBits count: Int) -> UInt8 {
182186
precondition(0...8 ~= count)
@@ -199,11 +203,10 @@ public final class LsbBitReader: BitReader {
199203
}
200204

201205
/**
202-
Reads `fromBits` bits and returns them as an `UInt16` number, advancing by `fromBits` BIT positions.
206+
Reads `fromBits` bits and returns them as a `UInt16` number, advancing by `fromBits` bit positions.
203207

204-
- Precondition: Parameter `fromBits` MUST be from `0...16` range, i.e. it MUST not exceed maximum bit width of
205-
`UInt16` type on the current platform.
206-
- Precondition: There MUST be enough data left.
208+
- Precondition: Parameter `fromBits` must be in the `0...16` range.
209+
- Precondition: There must be enough data left.
207210
*/
208211
public func uint16(fromBits count: Int) -> UInt16 {
209212
precondition(0...16 ~= count)
@@ -226,11 +229,10 @@ public final class LsbBitReader: BitReader {
226229
}
227230

228231
/**
229-
Reads `fromBits` bits and returns them as an `UInt32` number, advancing by `fromBits` BIT positions.
232+
Reads `fromBits` bits and returns them as a `UInt32` number, advancing by `fromBits` bit positions.
230233

231-
- Precondition: Parameter `fromBits` MUST be from `0...32` range, i.e. it MUST not exceed maximum bit width of
232-
`UInt32` type on the current platform.
233-
- Precondition: There MUST be enough data left.
234+
- Precondition: Parameter `fromBits` must be in the `0...32` range.
235+
- Precondition: There must be enough data left.
234236
*/
235237
public func uint32(fromBits count: Int) -> UInt32 {
236238
precondition(0...32 ~= count)
@@ -253,11 +255,10 @@ public final class LsbBitReader: BitReader {
253255
}
254256

255257
/**
256-
Reads `fromBits` bits and returns them as an `UInt64` number, advancing by `fromBits` BIT positions.
258+
Reads `fromBits` bits and returns them as a `UInt64` number, advancing by `fromBits` bit positions.
257259

258-
- Precondition: Parameter `fromBits` MUST be from `0...64` range, i.e. it MUST not exceed maximum bit width of
259-
`UInt64` type on the current platform.
260-
- Precondition: There MUST be enough data left.
260+
- Precondition: Parameter `fromBits` must be from `0...64` range.
261+
- Precondition: There must be enough data left.
261262
*/
262263
public func uint64(fromBits count: Int) -> UInt64 {
263264
precondition(0...64 ~= count)
@@ -280,10 +281,11 @@ public final class LsbBitReader: BitReader {
280281
}
281282

282283
/**
283-
Moves BIT pointer to the first BIT of the next BYTE. If the reader is already aligned, then does nothing.
284+
Aligns a bit pointer to a byte boundary, i.e. moves the bit pointer to the first bit of the next byte. If the
285+
reader is already aligned, then does nothing.
284286

285-
- Warning: Doesn't check if there is any data left. It is advised to use `isFinished` after calling this method
286-
to check if the end was reached.
287+
- Warning: This function doesn't check if there is any data left. It is advised to use `isFinished` after calling
288+
this method to check if the end was reached.
287289
*/
288290
public func align() {
289291
if self.bitMask != 1 {
@@ -293,44 +295,47 @@ public final class LsbBitReader: BitReader {
293295
}
294296

295297
/**
296-
Reads byte and returns it, advancing by one BYTE position.
298+
Reads a byte and returns it, advancing by one byte position.
297299

298-
- Precondition: The reader MUST be aligned.
299-
- Precondition: There MUST be enough data left.
300+
- Precondition: The reader must be aligned.
301+
- Precondition: There must be enough bytes left.
300302
*/
301303
public func byte() -> UInt8 {
302304
defer { offset += 1 }
303305
return data[offset]
304306
}
305307

306308
/**
307-
Reads `count` bytes and returns them as an array of `UInt8`, advancing by `count` BYTE positions.
309+
Reads `count` bytes and returns them as a `[UInt8]` array, advancing by `count` byte positions.
308310

309-
- Precondition: The reader MUST be aligned.
310-
- Precondition: There MUST be enough data left.
311+
- Precondition: The reader must be aligned.
312+
- Precondition: Parameter `count` must be non-negative.
313+
- Precondition: There must be enough bytes left.
311314
*/
312315
public func bytes(count: Int) -> [UInt8] {
313316
defer { offset += count }
314317
return data[offset..<offset + count].toByteArray(count)
315318
}
316319

317320
/**
318-
Reads 8 bytes and returns them as a `UInt64` number, advancing by 8 BYTE positions.
321+
Reads 8 bytes and returns them as a `UInt64` number, advancing by 8 byte positions.
319322

320-
- Precondition: The reader MUST be aligned.
321-
- Precondition: There MUST be enough data left.
323+
- Precondition: The reader must be aligned.
324+
- Precondition: There must be enough bytes left.
322325
*/
323326
public func uint64() -> UInt64 {
324327
defer { offset += 8 }
325328
return data[offset..<offset + 8].toU64()
326329
}
327330

328331
/**
329-
Reads `fromBytes` bytes and returns them as a `UInt64` number, advancing by `fromBytes` BYTE positions.
332+
Reads `fromBytes` bytes and returns them as a `UInt64` number, advancing by `fromBytes` byte positions.
330333

331-
- Precondition: The reader MUST be aligned.
332-
- Precondition: Parameter `fromBytes` MUST not be less than 0.
333-
- Precondition: There MUST be enough data left.
334+
- Note: If it is known that the `fromBytes` is exactly 8 then consider using the `uint64()` function (without an
335+
argument), since it may provide better performance.
336+
- Precondition: The reader must be aligned.
337+
- Precondition: Parameter `fromBytes` must be in the `0...8` range.
338+
- Precondition: There must be enough bytes left.
334339
*/
335340
public func uint64(fromBytes count: Int) -> UInt64 {
336341
precondition(0...8 ~= count)
@@ -343,22 +348,24 @@ public final class LsbBitReader: BitReader {
343348
}
344349

345350
/**
346-
Reads 4 bytes and returns them as a `UInt32` number, advancing by 4 BYTE positions.
351+
Reads 4 bytes and returns them as a `UInt32` number, advancing by 4 byte positions.
347352

348-
- Precondition: The reader MUST be aligned.
349-
- Precondition: There MUST be enough data left.
353+
- Precondition: The reader must be aligned.
354+
- Precondition: There must be enough bytes left.
350355
*/
351356
public func uint32() -> UInt32 {
352357
defer { offset += 4 }
353358
return data[offset..<offset + 4].toU32()
354359
}
355360

356361
/**
357-
Reads `fromBytes` bytes and returns them as a `UInt32` number, advancing by `fromBytes` BYTE positions.
362+
Reads `fromBytes` bytes and returns them as a `UInt32` number, advancing by `fromBytes` byte positions.
358363

359-
- Precondition: The reader MUST be aligned.
360-
- Precondition: Parameter `fromBytes` MUST not be less than 0.
361-
- Precondition: There MUST be enough data left.
364+
- Note: If it is known that the `fromBytes` is exactly 4 then consider using the `uint32()` function (without an
365+
argument), since it may provide better performance.
366+
- Precondition: The reader must be aligned.
367+
- Precondition: Parameter `fromBytes` must be in the `0...4` range.
368+
- Precondition: There must be enough bytes left.
362369
*/
363370
public func uint32(fromBytes count: Int) -> UInt32 {
364371
precondition(0...4 ~= count)
@@ -371,22 +378,24 @@ public final class LsbBitReader: BitReader {
371378
}
372379

373380
/**
374-
Reads 2 bytes and returns them as a `UInt16` number, advancing by 2 BYTE positions.
381+
Reads 2 bytes and returns them as a `UInt16` number, advancing by 2 byte positions.
375382

376-
- Precondition: The reader MUST be aligned.
377-
- Precondition: There MUST be enough data left.
383+
- Precondition: The reader must be aligned.
384+
- Precondition: There must be enough data left.
378385
*/
379386
public func uint16() -> UInt16 {
380387
defer { offset += 2 }
381388
return data[offset..<offset + 2].toU16()
382389
}
383390

384391
/**
385-
Reads `fromBytes` bytes and returns them as a `UInt16` number, advancing by `fromBytes` BYTE positions.
392+
Reads `fromBytes` bytes and returns them as a `UInt16` number, advancing by `fromBytes` byte positions.
386393

387-
- Precondition: The reader MUST be aligned.
388-
- Precondition: Parameter `fromBytes` MUST not be less than 0.
389-
- Precondition: There MUST be enough data left.
394+
- Note: If it is known that the `fromBytes` is exactly 2 then consider using the `uint16()` function (without an
395+
argument), since it may provide better performance.
396+
- Precondition: The reader must be aligned.
397+
- Precondition: Parameter `fromBytes` must be in the `0...2` range.
398+
- Precondition: There must be enough bytes left.
390399
*/
391400
public func uint16(fromBytes count: Int) -> UInt16 {
392401
precondition(0...2 ~= count)

0 commit comments

Comments
 (0)