Skip to content

Commit b737d19

Browse files
committed
Add docs for ByteReader
1 parent 3668829 commit b737d19

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

Sources/ByteReader.swift

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,45 +5,87 @@
55

66
import Foundation
77

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

11+
/// Size of the `data` (in bytes).
1012
public let size: Int
13+
14+
/// Data which is being read.
1115
public let data: Data
16+
17+
/// Offset to the byte in `data` which will be read next.
1218
public var offset: Int
1319

20+
/**
21+
True, if `offset` points at any position after the last byte in `data`.
22+
23+
- Note: It generally means that all bytes have been read.
24+
*/
25+
1426
public var isFinished: Bool {
1527
return self.data.endIndex <= self.offset
1628
}
1729

30+
/// Creates an instance for reading `data`.
1831
public init(data: Data) {
1932
self.size = data.count
2033
self.data = data
2134
self.offset = data.startIndex
2235
}
2336

37+
/**
38+
Reads byte and returns it, advancing by one position.
39+
40+
- Warning: Doesn't check for potential out of bounds error, i.e. doesn't check if `isFinished` is true.
41+
*/
2442
public func byte() -> UInt8 {
2543
self.offset += 1
2644
return self.data[self.offset - 1]
2745
}
2846

47+
/**
48+
Reads `count` bytes and returns them as an array of `UInt8`, advancing by `count` positions.
49+
50+
- Warning: Doesn't check for potential out of bounds errors, i.e. doesn't check if `isFinished` will be true
51+
at any point during the reading of these bytes.
52+
*/
2953
public func bytes(count: Int) -> [UInt8] {
3054
let result = self.data[self.offset..<self.offset + count].toArray(type: UInt8.self, count: count)
3155
self.offset += count
3256
return result
3357
}
3458

59+
/**
60+
Reads 8 bytes and returns them as a `UInt64` number, advancing by 8 positions.
61+
62+
- Warning: Doesn't check for potential out of bounds errors, i.e. doesn't check if `isFinished` will be true
63+
at any point during the reading of this number.
64+
*/
3565
public func uint64() -> UInt64 {
3666
let result = self.data[self.offset..<self.offset + 8].to(type: UInt64.self)
3767
self.offset += 8
3868
return result
3969
}
4070

71+
/**
72+
Reads 4 bytes and returns them as a `UInt32` number, advancing by 4 positions.
73+
74+
- Warning: Doesn't check for potential out of bounds errors, i.e. doesn't check if `isFinished` will be true
75+
at any point during the reading of this number.
76+
*/
4177
public func uint32() -> UInt32 {
4278
let result = self.data[self.offset..<self.offset + 4].to(type: UInt32.self)
4379
self.offset += 4
4480
return result
4581
}
4682

83+
/**
84+
Reads 2 bytes and returns them as a `UInt16` number, advancing by 2 positions.
85+
86+
- Warning: Doesn't check for potential out of bounds errors, i.e. doesn't check if `isFinished` will be true
87+
at any point during the reading of this number.
88+
*/
4789
public func uint16() -> UInt16 {
4890
let result = self.data[self.offset..<self.offset + 2].to(type: UInt16.self)
4991
self.offset += 2

0 commit comments

Comments
 (0)