|
5 | 5 |
|
6 | 6 | import Foundation |
7 | 7 |
|
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. |
9 | 9 | public protocol BitWriter { |
10 | 10 |
|
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). |
12 | 12 | var data: Data { get } |
13 | 13 |
|
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. |
15 | 15 | var isAligned: Bool { get } |
16 | 16 |
|
17 | | - /// Creates an instance for writing bits (and bytes). |
| 17 | + /// Creates an instance for writing bits and bytes. |
18 | 18 | init() |
19 | 19 |
|
20 | | - /// Writes `bit`, advancing by one BIT position. |
| 20 | + /// Writes a `bit`, advancing by one bit position. |
21 | 21 | func write(bit: UInt8) |
22 | 22 |
|
23 | | - /// Writes `bits`, advancing by `bits.count` BIT positions. |
| 23 | + /// Writes `bits`, advancing by `bits.count` bit positions. |
24 | 24 | func write(bits: [UInt8]) |
25 | 25 |
|
26 | | - /// Writes `number`, using and advancing by `bitsCount` BIT positions. |
| 26 | + /// Writes a `number` into `bitsCount` amount of bits, advancing by `bitsCount` bit positions. |
27 | 27 | func write(number: Int, bitsCount: Int) |
28 | 28 |
|
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 | + */ |
30 | 33 | func write(signedNumber: Int, bitsCount: Int, representation: SignedNumberRepresentation) |
31 | 34 |
|
32 | | - /// Writes unsigned `number`, using and advancing by `bitsCount` BIT positions. |
| 35 | + /// Writes an unsigned `number`, advancing by `bitsCount` bit positions. |
33 | 36 | func write(unsignedNumber: UInt, bitsCount: Int) |
34 | 37 |
|
35 | | - /// Writes `byte`, advancing by one BYTE position. |
| 38 | + /// Writes a `byte`, advancing by one byte position. |
36 | 39 | func append(byte: UInt8) |
37 | 40 |
|
38 | 41 | /** |
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. |
41 | 44 | */ |
42 | 45 | func align() |
43 | 46 |
|
44 | 47 | } |
45 | 48 |
|
46 | 49 | extension BitWriter { |
47 | 50 |
|
| 51 | + /// Writes `bits`, advancing by `bits.count` bit positions, using the `write(bit:)` function. |
48 | 52 | public func write(bits: [UInt8]) { |
49 | 53 | for bit in bits { |
50 | 54 | self.write(bit: bit) |
51 | 55 | } |
52 | 56 | } |
53 | 57 |
|
| 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 | + */ |
54 | 66 | public func write(number: Int, bitsCount: Int) { |
55 | 67 | precondition(0...Int.bitWidth ~= bitsCount) |
56 | 68 | self.write(unsignedNumber: UInt(bitPattern: number), bitsCount: bitsCount) |
57 | 69 | } |
58 | 70 |
|
| 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 | + */ |
59 | 82 | public func write(signedNumber: Int, bitsCount: Int, representation: SignedNumberRepresentation = .twoComplementNegatives) { |
60 | 83 | precondition(signedNumber >= representation.minRepresentableNumber(bitsCount: bitsCount) && |
61 | 84 | signedNumber <= representation.maxRepresentableNumber(bitsCount: bitsCount), |
|
0 commit comments