Skip to content

Commit 00536b9

Browse files
committed
Use data.endIndex instead of data.size to prevent incorrect preconditions failures
1 parent ad32a77 commit 00536b9

3 files changed

Lines changed: 7 additions & 7 deletions

File tree

Sources/ByteReader.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class ByteReader {
4040
- Precondition: There MUST be enough data left.
4141
*/
4242
public func byte() -> UInt8 {
43-
precondition(self.offset < self.size)
43+
precondition(self.offset < self.data.endIndex)
4444
self.offset += 1
4545
return self.data[self.offset - 1]
4646
}
@@ -55,7 +55,7 @@ public class ByteReader {
5555
precondition(count >= 0)
5656
guard count > 0
5757
else { return [] }
58-
precondition(self.offset + count <= self.size)
58+
precondition(self.offset + count <= self.data.endIndex)
5959
let result = self.data[self.offset..<self.offset + count].toArray(type: UInt8.self, count: count)
6060
self.offset += count
6161
return result
@@ -67,7 +67,7 @@ public class ByteReader {
6767
- Precondition: There MUST be enough data left.
6868
*/
6969
public func uint64() -> UInt64 {
70-
precondition(self.offset + 8 <= self.size)
70+
precondition(self.offset + 8 <= self.data.endIndex)
7171
let result = self.data[self.offset..<self.offset + 8].to(type: UInt64.self)
7272
self.offset += 8
7373
return result
@@ -79,7 +79,7 @@ public class ByteReader {
7979
- Precondition: There MUST be enough data left.
8080
*/
8181
public func uint32() -> UInt32 {
82-
precondition(self.offset + 4 <= self.size)
82+
precondition(self.offset + 4 <= self.data.endIndex)
8383
let result = self.data[self.offset..<self.offset + 4].to(type: UInt32.self)
8484
self.offset += 4
8585
return result
@@ -91,7 +91,7 @@ public class ByteReader {
9191
- Precondition: There MUST be enough data left.
9292
*/
9393
public func uint16() -> UInt16 {
94-
precondition(self.offset + 2 <= self.size)
94+
precondition(self.offset + 2 <= self.data.endIndex)
9595
let result = self.data[self.offset..<self.offset + 2].to(type: UInt16.self)
9696
self.offset += 2
9797
return result

Sources/LsbBitReader.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public final class LsbBitReader: ByteReader, BitReader {
1818
if isFinished {
1919
return 0
2020
} else {
21-
return (self.size - self.offset) * 8 + 8 - bitMask.trailingZeroBitCount
21+
return (self.data.endIndex - self.offset) * 8 + 8 - bitMask.trailingZeroBitCount
2222
}
2323
}
2424

Sources/MsbBitReader.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public final class MsbBitReader: ByteReader, BitReader {
1818
if isFinished {
1919
return 0
2020
} else {
21-
return (self.size - self.offset) * 8 + 8 - bitMask.leadingZeroBitCount
21+
return (self.data.endIndex - self.offset) * 8 + 8 - bitMask.leadingZeroBitCount
2222
}
2323
}
2424

0 commit comments

Comments
 (0)