Skip to content

Commit ca966d0

Browse files
committed
Use UnsafeBufferPointer instead of Data in ByteReader (note: it currently breaks non-zero-start-index tests)
Also remove "4.2 compiler workaround" for ByteReader
1 parent 043b272 commit ca966d0

1 file changed

Lines changed: 48 additions & 139 deletions

File tree

Sources/ByteReader.swift

Lines changed: 48 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public class ByteReader {
1717
/// Offset to the byte in `data` which will be read next.
1818
public var offset: Int
1919

20+
var ptr: UnsafeBufferPointer<UInt8>
21+
2022
/**
2123
True, if `offset` points at any position after the last byte in `data`.
2224

@@ -45,20 +47,17 @@ public class ByteReader {
4547

4648
/// Amount of bytes that were already read.
4749
public var bytesRead: Int {
48-
#if swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0))
49-
return { (data: Data, offset: Int) -> Int in
50-
return offset - data.startIndex
51-
} (self.data, self.offset)
52-
#else
53-
return self.offset - self.data.startIndex
54-
#endif
50+
return self.offset
5551
}
5652

5753
/// Creates an instance for reading bytes from `data`.
5854
public init(data: Data) {
5955
self.size = data.count
6056
self.data = data
61-
self.offset = data.startIndex
57+
self.offset = 0
58+
self.ptr = data.withUnsafeBytes { (ptr: UnsafePointer<UInt8>) -> UnsafeBufferPointer<UInt8> in
59+
return UnsafeBufferPointer<UInt8>(start: ptr, count: data.count)
60+
}
6261
}
6362

6463
/**
@@ -67,17 +66,9 @@ public class ByteReader {
6766
- Precondition: There MUST be enough data left.
6867
*/
6968
public func byte() -> UInt8 {
70-
#if swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0))
71-
return { (data: Data, offset: inout Int) -> UInt8 in
72-
precondition(offset < data.endIndex)
73-
defer { offset += 1 }
74-
return data[offset]
75-
} (self.data, &self.offset)
76-
#else
77-
precondition(self.offset < self.data.endIndex)
78-
defer { self.offset += 1 }
79-
return self.data[self.offset]
80-
#endif
69+
precondition(self.offset < self.data.endIndex)
70+
defer { self.offset += 1 }
71+
return self.ptr[self.offset]
8172
}
8273

8374
/**
@@ -88,17 +79,14 @@ public class ByteReader {
8879
*/
8980
public func bytes(count: Int) -> [UInt8] {
9081
precondition(count >= 0)
91-
#if swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0))
92-
return { (data: Data, offset: inout Int) -> [UInt8] in
93-
precondition(data.endIndex - offset >= count)
94-
defer { offset += count }
95-
return data[offset..<offset + count].toArray(type: UInt8.self, count: count)
96-
} (self.data, &self.offset)
97-
#else
98-
precondition(bytesLeft >= count)
99-
defer { self.offset += count }
100-
return self.data[self.offset..<self.offset + count].toArray(type: UInt8.self, count: count)
101-
#endif
82+
precondition(bytesLeft >= count)
83+
var result = [UInt8]()
84+
result.reserveCapacity(count)
85+
for _ in 0..<count {
86+
result.append(self.ptr[self.offset])
87+
self.offset += 1
88+
}
89+
return result
10290
}
10391

10492
/**
@@ -111,25 +99,12 @@ public class ByteReader {
11199
precondition(count >= 0)
112100
// TODO: If uintX() could be force inlined or something in the future then probably it would make sense
113101
// to use them for `count` == 2, 4 or 8.
114-
#if swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0))
115-
return { (data: Data, offset: inout Int) -> Int in
116-
precondition(data.endIndex - offset >= count)
117-
var result = 0
118-
for i in 0..<count {
119-
result += Int(truncatingIfNeeded: data[offset]) << (8 * i)
120-
offset += 1
121-
}
122-
return result
123-
} (self.data, &self.offset)
124-
#else
125-
precondition(bytesLeft >= count)
126-
var result = 0
127-
for i in 0..<count {
128-
result += Int(truncatingIfNeeded: self.data[self.offset]) << (8 * i)
129-
self.offset += 1
130-
}
131-
return result
132-
#endif
102+
var result = 0
103+
for i in 0..<count {
104+
result += Int(truncatingIfNeeded: self.ptr[self.offset]) << (8 * i)
105+
self.offset += 1
106+
}
107+
return result
133108
}
134109

135110
/**
@@ -138,17 +113,7 @@ public class ByteReader {
138113
- Precondition: There MUST be enough data left.
139114
*/
140115
public func uint64() -> UInt64 {
141-
#if swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0))
142-
return { (data: Data, offset: inout Int) -> UInt64 in
143-
precondition(data.endIndex - offset >= 8)
144-
defer { offset += 8 }
145-
return data[offset..<offset + 8].to(type: UInt64.self)
146-
} (self.data, &self.offset)
147-
#else
148-
precondition(bytesLeft >= 8)
149-
defer { self.offset += 8 }
150-
return self.data[self.offset..<self.offset + 8].to(type: UInt64.self)
151-
#endif
116+
return self.uint64(fromBytes: 8)
152117
}
153118

154119
/**
@@ -162,25 +127,13 @@ public class ByteReader {
162127
*/
163128
public func uint64(fromBytes count: Int) -> UInt64 {
164129
precondition(0...8 ~= count)
165-
#if swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0))
166-
return { (data: Data, offset: inout Int) -> UInt64 in
167-
precondition(data.endIndex - offset >= count)
168-
var result = 0 as UInt64
169-
for i in 0..<count {
170-
result += UInt64(truncatingIfNeeded: data[offset]) << (8 * i)
171-
offset += 1
172-
}
173-
return result
174-
} (self.data, &self.offset)
175-
#else
176-
precondition(bytesLeft >= count)
177-
var result = 0 as UInt64
178-
for i in 0..<count {
179-
result += UInt64(truncatingIfNeeded: self.data[self.offset]) << (8 * i)
180-
self.offset += 1
181-
}
182-
return result
183-
#endif
130+
precondition(bytesLeft >= count)
131+
var result = 0 as UInt64
132+
for i in 0..<count {
133+
result += UInt64(truncatingIfNeeded: self.ptr[self.offset]) << (8 * i)
134+
self.offset += 1
135+
}
136+
return result
184137
}
185138

186139
/**
@@ -189,17 +142,7 @@ public class ByteReader {
189142
- Precondition: There MUST be enough data left.
190143
*/
191144
public func uint32() -> UInt32 {
192-
#if swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0))
193-
return { (data: Data, offset: inout Int) -> UInt32 in
194-
precondition(data.endIndex - offset >= 4)
195-
defer { offset += 4 }
196-
return data[offset..<offset + 4].to(type: UInt32.self)
197-
} (self.data, &self.offset)
198-
#else
199-
precondition(bytesLeft >= 4)
200-
defer { self.offset += 4 }
201-
return self.data[self.offset..<self.offset + 4].to(type: UInt32.self)
202-
#endif
145+
return self.uint32(fromBytes: 4)
203146
}
204147

205148
/**
@@ -213,25 +156,13 @@ public class ByteReader {
213156
*/
214157
public func uint32(fromBytes count: Int) -> UInt32 {
215158
precondition(0...4 ~= count)
216-
#if swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0))
217-
return { (data: Data, offset: inout Int) -> UInt32 in
218-
precondition(data.endIndex - offset >= count)
219-
var result = 0 as UInt32
220-
for i in 0..<count {
221-
result += UInt32(truncatingIfNeeded: data[offset]) << (8 * i)
222-
offset += 1
223-
}
224-
return result
225-
} (self.data, &self.offset)
226-
#else
227-
precondition(bytesLeft >= count)
228-
var result = 0 as UInt32
229-
for i in 0..<count {
230-
result += UInt32(truncatingIfNeeded: self.data[self.offset]) << (8 * i)
231-
self.offset += 1
232-
}
233-
return result
234-
#endif
159+
precondition(bytesLeft >= count)
160+
var result = 0 as UInt32
161+
for i in 0..<count {
162+
result += UInt32(truncatingIfNeeded: self.ptr[self.offset]) << (8 * i)
163+
self.offset += 1
164+
}
165+
return result
235166
}
236167

237168
/**
@@ -240,17 +171,7 @@ public class ByteReader {
240171
- Precondition: There MUST be enough data left.
241172
*/
242173
public func uint16() -> UInt16 {
243-
#if swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0))
244-
return { (data: Data, offset: inout Int) -> UInt16 in
245-
precondition(data.endIndex - offset >= 2)
246-
defer { offset += 2 }
247-
return data[offset..<offset + 2].to(type: UInt16.self)
248-
} (self.data, &self.offset)
249-
#else
250-
precondition(bytesLeft >= 2)
251-
defer { self.offset += 2 }
252-
return self.data[self.offset..<self.offset + 2].to(type: UInt16.self)
253-
#endif
174+
return self.uint16(fromBytes: 2)
254175
}
255176

256177
/**
@@ -264,25 +185,13 @@ public class ByteReader {
264185
*/
265186
public func uint16(fromBytes count: Int) -> UInt16 {
266187
precondition(0...2 ~= count)
267-
#if swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0))
268-
return { (data: Data, offset: inout Int) -> UInt16 in
269-
precondition(data.endIndex - offset >= count)
270-
var result = 0 as UInt16
271-
for i in 0..<count {
272-
result += UInt16(truncatingIfNeeded: data[offset]) << (8 * i)
273-
offset += 1
274-
}
275-
return result
276-
} (self.data, &self.offset)
277-
#else
278-
precondition(bytesLeft >= count)
279-
var result = 0 as UInt16
280-
for i in 0..<count {
281-
result += UInt16(truncatingIfNeeded: self.data[self.offset]) << (8 * i)
282-
self.offset += 1
283-
}
284-
return result
285-
#endif
188+
precondition(bytesLeft >= count)
189+
var result = 0 as UInt16
190+
for i in 0..<count {
191+
result += UInt16(truncatingIfNeeded: self.ptr[self.offset]) << (8 * i)
192+
self.offset += 1
193+
}
194+
return result
286195
}
287196

288197
}

0 commit comments

Comments
 (0)