Skip to content

Commit 4e9d3a6

Browse files
committed
Add docs for ByteReader protocol and its extensions
1 parent eea5267 commit 4e9d3a6

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

Sources/ByteReader.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,49 +5,71 @@
55

66
import Foundation
77

8+
/// A type that contains functions for reading `Data` byte-by-byte.
89
public protocol ByteReader: AnyObject {
910

11+
/// Size of the `data` (in bytes).
1012
var size: Int { get }
1113

14+
/// Data which is being read.
1215
var data: Data { get }
1316

17+
/// Offset to a byte in the `data` which will be read next.
1418
var offset: Int { get set }
1519

20+
/// Creates an instance for reading bytes from the `data`.
1621
init(data: Data)
1722

23+
/// Reads a byte and returns it, advancing by one position.
1824
func byte() -> UInt8
1925

26+
/// Reads `count` bytes and returns them as a `[UInt8]` array, advancing by `count` positions.
2027
func bytes(count: Int) -> [UInt8]
2128

29+
/// Reads `fromBytes` bytes and returns them as a `Int` number, advancing by `fromBytes` positions.
2230
func int(fromBytes count: Int) -> Int
2331

32+
/// Reads `fromBytes` bytes and returns them as a `UInt64` number, advancing by `fromBytes` positions.
2433
func uint64(fromBytes count: Int) -> UInt64
2534

35+
/// Reads `fromBytes` bytes and returns them as a `UInt32` number, advancing by `fromBytes` positions.
2636
func uint32(fromBytes count: Int) -> UInt32
2737

38+
/// Reads `fromBytes` bytes and returns them as a `UInt16` number, advancing by `fromBytes` positions.
2839
func uint16(fromBytes count: Int) -> UInt16
2940

3041
}
3142

3243
extension ByteReader {
3344

45+
/// Creates an instance for reading bytes by using the `data` and the `offset` of the specified `BitReader`.
3446
public init(_ bitReader: BitReader) {
3547
self.init(data: bitReader.data)
3648
self.offset = bitReader.offset
3749
}
3850

51+
/// Amount of bytes left to read.
3952
public var bytesLeft: Int {
4053
return data.endIndex - offset
4154
}
4255

56+
/// Amount of bytes that were already read.
4357
public var bytesRead: Int {
4458
return offset - data.startIndex
4559
}
4660

61+
/**
62+
True, if the `offset` points at any position after the last byte in `data`, which generally means that all data
63+
has been read.
64+
*/
4765
public var isFinished: Bool {
4866
return data.endIndex <= offset
4967
}
5068

69+
/**
70+
Reads `fromBytes` bytes by either using `uint64(fromBytes:)` or `uint32(fromBytes:)` depending on the platform's
71+
integer bit width, converts the result to `Int`, and returns it, advancing by `fromBytes` positions.
72+
*/
5173
public func int(fromBytes count: Int) -> Int {
5274
if MemoryLayout<Int>.size == 8 {
5375
return Int(truncatingIfNeeded: self.uint64(fromBytes: count))

0 commit comments

Comments
 (0)