Skip to content

Commit 3ade64a

Browse files
committed
Add private ByteReader._offset property which is always zero-based and make offset a computeable (from _offset) property
This fixes non-zero-start-index tests.
1 parent 2877611 commit 3ade64a

1 file changed

Lines changed: 33 additions & 24 deletions

File tree

Sources/ByteReader.swift

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,16 @@ public class ByteReader {
1515
public let data: Data
1616

1717
/// Offset to the byte in `data` which will be read next.
18-
public var offset: Int
18+
public var offset: Int {
19+
get {
20+
return self._offset + self.data.startIndex
21+
}
22+
set {
23+
self._offset = newValue - self.data.startIndex
24+
}
25+
}
26+
27+
private var _offset: Int
1928

2029
var ptr: UnsafeBufferPointer<UInt8>
2130

@@ -26,35 +35,35 @@ public class ByteReader {
2635
*/
2736
public var isFinished: Bool {
2837
#if swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0))
29-
return { (data: Data, offset: Int) -> Bool in
30-
return data.endIndex <= offset
31-
} (self.data, self.offset)
38+
return { (data: Data, _offset: Int) -> Bool in
39+
return data.endIndex <= _offset
40+
} (self.data, self._offset)
3241
#else
33-
return self.data.endIndex <= self.offset
42+
return self.data.endIndex <= self._offset
3443
#endif
3544
}
3645

3746
/// Amount of bytes left to read.
3847
public var bytesLeft: Int {
3948
#if swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0))
40-
return { (data: Data, offset: Int) -> Int in
41-
return data.endIndex - offset
42-
} (self.data, self.offset)
49+
return { (data: Data, _offset: Int) -> Int in
50+
return data.endIndex - _offset
51+
} (self.data, self._offset)
4352
#else
44-
return self.data.endIndex - self.offset
53+
return self.data.endIndex - self._offset
4554
#endif
4655
}
4756

4857
/// Amount of bytes that were already read.
4958
public var bytesRead: Int {
50-
return self.offset
59+
return self._offset
5160
}
5261

5362
/// Creates an instance for reading bytes from `data`.
5463
public init(data: Data) {
5564
self.size = data.count
5665
self.data = data
57-
self.offset = 0
66+
self._offset = 0
5867
self.ptr = data.withUnsafeBytes { (ptr: UnsafePointer<UInt8>) -> UnsafeBufferPointer<UInt8> in
5968
return UnsafeBufferPointer<UInt8>(start: ptr, count: data.count)
6069
}
@@ -66,9 +75,9 @@ public class ByteReader {
6675
- Precondition: There MUST be enough data left.
6776
*/
6877
public func byte() -> UInt8 {
69-
precondition(self.offset < self.data.endIndex)
70-
defer { self.offset += 1 }
71-
return self.ptr[self.offset]
78+
precondition(self._offset < self.size)
79+
defer { self._offset += 1 }
80+
return self.ptr[self._offset]
7281
}
7382

7483
/**
@@ -83,8 +92,8 @@ public class ByteReader {
8392
var result = [UInt8]()
8493
result.reserveCapacity(count)
8594
for _ in 0..<count {
86-
result.append(self.ptr[self.offset])
87-
self.offset += 1
95+
result.append(self.ptr[self._offset])
96+
self._offset += 1
8897
}
8998
return result
9099
}
@@ -101,8 +110,8 @@ public class ByteReader {
101110
// to use them for `count` == 2, 4 or 8.
102111
var result = 0
103112
for i in 0..<count {
104-
result += Int(truncatingIfNeeded: self.ptr[self.offset]) << (8 * i)
105-
self.offset += 1
113+
result += Int(truncatingIfNeeded: self.ptr[self._offset]) << (8 * i)
114+
self._offset += 1
106115
}
107116
return result
108117
}
@@ -130,8 +139,8 @@ public class ByteReader {
130139
precondition(bytesLeft >= count)
131140
var result = 0 as UInt64
132141
for i in 0..<count {
133-
result += UInt64(truncatingIfNeeded: self.ptr[self.offset]) << (8 * i)
134-
self.offset += 1
142+
result += UInt64(truncatingIfNeeded: self.ptr[self._offset]) << (8 * i)
143+
self._offset += 1
135144
}
136145
return result
137146
}
@@ -159,8 +168,8 @@ public class ByteReader {
159168
precondition(bytesLeft >= count)
160169
var result = 0 as UInt32
161170
for i in 0..<count {
162-
result += UInt32(truncatingIfNeeded: self.ptr[self.offset]) << (8 * i)
163-
self.offset += 1
171+
result += UInt32(truncatingIfNeeded: self.ptr[self._offset]) << (8 * i)
172+
self._offset += 1
164173
}
165174
return result
166175
}
@@ -188,8 +197,8 @@ public class ByteReader {
188197
precondition(bytesLeft >= count)
189198
var result = 0 as UInt16
190199
for i in 0..<count {
191-
result += UInt16(truncatingIfNeeded: self.ptr[self.offset]) << (8 * i)
192-
self.offset += 1
200+
result += UInt16(truncatingIfNeeded: self.ptr[self._offset]) << (8 * i)
201+
self._offset += 1
193202
}
194203
return result
195204
}

0 commit comments

Comments
 (0)