Skip to content

Commit 53efb5d

Browse files
committed
Update docs of Lsb/MsbBitWriter, BitWriter protocol and its extension
1 parent e84a313 commit 53efb5d

3 files changed

Lines changed: 73 additions & 48 deletions

File tree

Sources/BitWriter.swift

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,57 +5,80 @@
55

66
import Foundation
77

8-
/// A type that contains functions for writing `Data` bit-by-bit (and byte-by-byte).
8+
/// A type that contains functions for writing `Data` bit-by-bit and byte-by-byte.
99
public protocol BitWriter {
1010

11-
/// Data which contains writer's output (the last byte in progress is not included).
11+
/// Data which contains the writer's output (the last byte, that is currently being written, is not included).
1212
var data: Data { get }
1313

14-
/// True, if writer's BIT pointer is aligned with the BYTE border.
14+
/// True, if a bit pointer is aligned to a byte boundary.
1515
var isAligned: Bool { get }
1616

17-
/// Creates an instance for writing bits (and bytes).
17+
/// Creates an instance for writing bits and bytes.
1818
init()
1919

20-
/// Writes `bit`, advancing by one BIT position.
20+
/// Writes a `bit`, advancing by one bit position.
2121
func write(bit: UInt8)
2222

23-
/// Writes `bits`, advancing by `bits.count` BIT positions.
23+
/// Writes `bits`, advancing by `bits.count` bit positions.
2424
func write(bits: [UInt8])
2525

26-
/// Writes `number`, using and advancing by `bitsCount` BIT positions.
26+
/// Writes a `number` into `bitsCount` amount of bits, advancing by `bitsCount` bit positions.
2727
func write(number: Int, bitsCount: Int)
2828

29-
/// Writes signed `number`, using and advancing by `bitsCount` BIT positions.
29+
/**
30+
Writes a signed integer `number` into `bitsCount` amount of bits, advancing by `bitsCount` bit positions, while
31+
using a `representation` as a method to represent the signed integer in a binary format.
32+
*/
3033
func write(signedNumber: Int, bitsCount: Int, representation: SignedNumberRepresentation)
3134

32-
/// Writes unsigned `number`, using and advancing by `bitsCount` BIT positions.
35+
/// Writes an unsigned `number`, advancing by `bitsCount` bit positions.
3336
func write(unsignedNumber: UInt, bitsCount: Int)
3437

35-
/// Writes `byte`, advancing by one BYTE position.
38+
/// Writes a `byte`, advancing by one byte position.
3639
func append(byte: UInt8)
3740

3841
/**
39-
Aligns writer's BIT pointer to the BYTE border, i.e. moves BIT pointer to the first BIT of the next BYTE,
40-
filling all skipped BIT positions with zeros.
42+
Aligns a bit pointer to a byte boundary, i.e. moves the bit pointer to the first bit of the next byte, filling all
43+
skipped bit positions with zeros.
4144
*/
4245
func align()
4346

4447
}
4548

4649
extension BitWriter {
4750

51+
/// Writes `bits`, advancing by `bits.count` bit positions, using the `write(bit:)` function.
4852
public func write(bits: [UInt8]) {
4953
for bit in bits {
5054
self.write(bit: bit)
5155
}
5256
}
5357

58+
/**
59+
Converts a `number` into an `UInt` integer, and writes it into `bitsCount` amount of bits by using the
60+
`write(unsignedNumber:bitsCount:)` function, advancing by `bitsCount` bit positions.
61+
62+
- Note: If the data is supposed to represent a signed integer (i.e. it is important to preserve the sign), it is
63+
recommended to use the `write(signedNumber:bitsCount:representation:)` function.
64+
- Precondition: Parameter `bitsCount` must be in the `0...Int.bitWidth` range.
65+
*/
5466
public func write(number: Int, bitsCount: Int) {
5567
precondition(0...Int.bitWidth ~= bitsCount)
5668
self.write(unsignedNumber: UInt(bitPattern: number), bitsCount: bitsCount)
5769
}
5870

71+
/**
72+
Writes a signed integer `number` into `bitsCount` amount of bits, advancing by `bitsCount` bit positions, while
73+
using a `representation` as a method to represent the signed integer in a binary format. This implementation uses
74+
the `write(unsignedNumber:bitsCount:)` function in the final stage of writing.
75+
76+
The default value of `representation` is `SignedNumberRepresentation.twoComplementNegatives`.
77+
78+
- Precondition: The `signedNumber` must be representable within `bitsCount` bits using the `representation`, i.e.
79+
it must be in the `representation.minRepresentableNumber...representation.maxRepresentableNumber` range.
80+
- Precondition: For the `SignedNumberRepresentation.biased` representation, the `bias` must be non-negative.
81+
*/
5982
public func write(signedNumber: Int, bitsCount: Int, representation: SignedNumberRepresentation = .twoComplementNegatives) {
6083
precondition(signedNumber >= representation.minRepresentableNumber(bitsCount: bitsCount) &&
6184
signedNumber <= representation.maxRepresentableNumber(bitsCount: bitsCount),

Sources/LsbBitWriter.swift

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,29 @@
66
import Foundation
77

88
/**
9-
A type that contains functions for writing `Data` bit-by-bit (and byte-by-byte),
10-
assuming "LSB 0" bit numbering scheme.
9+
A type that contains functions for writing `Data` bit-by-bit and byte-by-byte using "LSB 0" bit numbering scheme.
1110
*/
1211
public final class LsbBitWriter: BitWriter {
1312

14-
/// Data which contains writer's output (the last byte in progress is not included).
13+
/// Data which contains the writer's output (the last byte, that is currently being written, is not included).
1514
public private(set) var data: Data = Data()
1615

1716
private var bitMask: UInt8 = 1
1817
private var currentByte: UInt8 = 0
1918

20-
/// True, if writer's BIT pointer is aligned with the BYTE border.
19+
/// True, if a bit pointer is aligned to a byte boundary.
2120
public var isAligned: Bool {
2221
return self.bitMask == 1
2322
}
2423

25-
/// Creates an instance for writing bits (and bytes).
24+
/// Creates an instance for writing bits and bytes.
2625
public init() { }
2726

28-
/// Writes `bit`, advancing by one BIT position.
27+
/**
28+
Writes a `bit`, advancing by one bit position.
29+
30+
- Precondition: The `bit` must be either 0 or 1.
31+
*/
2932
public func write(bit: UInt8) {
3033
precondition(bit <= 1, "A bit must be either 0 or 1.")
3134

@@ -41,13 +44,13 @@ public final class LsbBitWriter: BitWriter {
4144
}
4245

4346
/**
44-
Writes unsigned `number`, using and advancing by `bitsCount` BIT positions.
47+
Writes an unsigned `number`, advancing by `bitsCount` bit positions.
4548

46-
- Note: If `bitsCount` is smaller than the actual amount of `number`'s bits then the `number` will be truncated to
47-
fit into `bitsCount` amount of bits.
48-
- Note: Bits of `number` are processed using the same bit-numbering scheme as of the writer (i.e. "LSB 0").
49-
- Note: This method is especially useful when it is necessary to write an unsigned number which overflows and,
50-
thus, crashes when converting to an `Int` if `write(number:bitsCount:)` method is used.
49+
This method may be useful for writing numbers, that would cause an integer overflow crash if converted to `Int`.
50+
51+
- Note: The `number` will be truncated if the `bitsCount` is less than the amount of bits required to fully
52+
represent the value of `number`.
53+
- Note: Bits of the `number` are processed using the same bit-numbering scheme as of the writer (i.e. "LSB 0").
5154
*/
5255
public func write(unsignedNumber: UInt, bitsCount: Int) {
5356
precondition(0...UInt.bitWidth ~= bitsCount)
@@ -59,20 +62,18 @@ public final class LsbBitWriter: BitWriter {
5962
}
6063

6164
/**
62-
Writes `byte`, advancing by one BYTE position.
65+
Writes a `byte`, advancing by one byte position.
6366

64-
- Precondition: Writer MUST be aligned.
67+
- Precondition: The writer must be aligned.
6568
*/
6669
public func append(byte: UInt8) {
6770
precondition(isAligned, "BitWriter is not aligned.")
6871
self.data.append(byte)
6972
}
7073

7174
/**
72-
Aligns writer's BIT pointer to the BYTE border, i.e. moves BIT pointer to the first BIT of the next BYTE,
73-
filling all skipped BIT positions with zeros.
74-
75-
- Note: If writer is already aligned, then does nothing.
75+
Aligns a bit pointer to a byte boundary, i.e. moves the bit pointer to the first bit of the next byte, filling all
76+
skipped bit positions with zeros. If the writer is already aligned, then does nothing.
7677
*/
7778
public func align() {
7879
guard self.bitMask != 1

Sources/MsbBitWriter.swift

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,29 @@
66
import Foundation
77

88
/**
9-
A type that contains functions for writing `Data` bit-by-bit (and byte-by-byte),
10-
assuming "MSB 0" bit numbering scheme.
9+
A type that contains functions for writing `Data` bit-by-bit and byte-by-byte using "MSB 0" bit numbering scheme.
1110
*/
1211
public final class MsbBitWriter: BitWriter {
1312

14-
/// Data which contains writer's output (the last byte in progress is not included).
13+
/// Data which contains the writer's output (the last byte, that is currently being written, is not included).
1514
public private(set) var data: Data = Data()
1615

1716
private var bitMask: UInt8 = 128
1817
private var currentByte: UInt8 = 0
1918

20-
/// True, if writer's BIT pointer is aligned with the BYTE border.
19+
/// True, if a bit pointer is aligned to a byte boundary.
2120
public var isAligned: Bool {
2221
return self.bitMask == 128
2322
}
2423

25-
/// Creates an instance for writing bits (and bytes).
24+
/// Creates an instance for writing bits and bytes.
2625
public init() { }
2726

28-
/// Writes `bit`, advancing by one BIT position.
27+
/**
28+
Writes a `bit`, advancing by one bit position.
29+
30+
- Precondition: The `bit` must be either 0 or 1.
31+
*/
2932
public func write(bit: UInt8) {
3033
precondition(bit <= 1, "A bit must be either 0 or 1.")
3134

@@ -41,13 +44,13 @@ public final class MsbBitWriter: BitWriter {
4144
}
4245

4346
/**
44-
Writes unsigned `number`, using and advancing by `bitsCount` BIT positions.
47+
Writes an unsigned `number`, advancing by `bitsCount` bit positions.
4548

46-
- Note: If `bitsCount` is smaller than the actual amount of `number`'s bits then the `number` will be truncated to
47-
fit into `bitsCount` amount of bits.
48-
- Note: Bits of `number` are processed using the same bit-numbering scheme as of the writer (i.e. "MSB 0").
49-
- Note: This method is especially useful when it is necessary to write an unsigned number which overflows and,
50-
thus, crashes when converting to an `Int` if `write(number:bitsCount:)` method is used.
49+
This method may be useful for writing numbers, that would cause an integer overflow crash if converted to `Int`.
50+
51+
- Note: The `number` will be truncated if the `bitsCount` is less than the amount of bits required to fully
52+
represent the value of `number`.
53+
- Note: Bits of the `number` are processed using the same bit-numbering scheme as of the writer (i.e. "MSB 0").
5154
*/
5255
public func write(unsignedNumber: UInt, bitsCount: Int) {
5356
precondition(0...UInt.bitWidth ~= bitsCount)
@@ -59,20 +62,18 @@ public final class MsbBitWriter: BitWriter {
5962
}
6063

6164
/**
62-
Writes `byte`, advancing by one BYTE position.
65+
Writes a `byte`, advancing by one byte position.
6366

64-
- Precondition: Writer MUST be aligned.
67+
- Precondition: The writer must be aligned.
6568
*/
6669
public func append(byte: UInt8) {
6770
precondition(isAligned, "BitWriter is not aligned.")
6871
self.data.append(byte)
6972
}
7073

7174
/**
72-
Aligns writer's BIT pointer to the BYTE border, i.e. moves BIT pointer to the first BIT of the next BYTE,
73-
filling all skipped BIT positions with zeros.
74-
75-
- Note: If writer is already aligned, then does nothing.
75+
Aligns a bit pointer to a byte boundary, i.e. moves the bit pointer to the first bit of the next byte, filling all
76+
skipped bit positions with zeros. If the writer is already aligned, then does nothing.
7677
*/
7778
public func align() {
7879
guard self.bitMask != 128

0 commit comments

Comments
 (0)