Skip to content

Commit ba11e74

Browse files
committed
Remove code paths for pre-4.2 versions of Swift
1 parent bea3f1d commit ba11e74

3 files changed

Lines changed: 148 additions & 396 deletions

File tree

Sources/ByteReader.swift

Lines changed: 52 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -23,35 +23,23 @@ public class ByteReader {
2323
- Note: It generally means that all bytes have been read.
2424
*/
2525
public var isFinished: Bool {
26-
#if swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0))
27-
return { (data: Data, offset: Int) -> Bool in
26+
return { (data: Data, offset: Int) -> Bool in
2827
return data.endIndex <= offset
29-
} (self.data, self.offset)
30-
#else
31-
return self.data.endIndex <= self.offset
32-
#endif
28+
} (self.data, self.offset)
3329
}
3430

3531
/// Amount of bytes left to read.
3632
public var bytesLeft: Int {
37-
#if swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0))
38-
return { (data: Data, offset: Int) -> Int in
33+
return { (data: Data, offset: Int) -> Int in
3934
return data.endIndex - offset
40-
} (self.data, self.offset)
41-
#else
42-
return self.data.endIndex - self.offset
43-
#endif
35+
} (self.data, self.offset)
4436
}
4537

4638
/// Amount of bytes that were already read.
4739
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
40+
return { (data: Data, offset: Int) -> Int in
41+
return offset - data.startIndex
42+
} (self.data, self.offset)
5543
}
5644

5745
/// Creates an instance for reading bytes from `data`.
@@ -67,17 +55,11 @@ public class ByteReader {
6755
- Precondition: There MUST be enough data left.
6856
*/
6957
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
58+
return { (data: Data, offset: inout Int) -> UInt8 in
59+
precondition(offset < data.endIndex)
60+
defer { offset += 1 }
61+
return data[offset]
62+
} (self.data, &self.offset)
8163
}
8264

8365
/**
@@ -88,17 +70,11 @@ public class ByteReader {
8870
*/
8971
public func bytes(count: Int) -> [UInt8] {
9072
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].toByteArray(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
73+
return { (data: Data, offset: inout Int) -> [UInt8] in
74+
precondition(data.endIndex - offset >= count)
75+
defer { offset += count }
76+
return data[offset..<offset + count].toByteArray(count)
77+
} (self.data, &self.offset)
10278
}
10379

10480
/**
@@ -111,25 +87,15 @@ public class ByteReader {
11187
precondition(count >= 0)
11288
// TODO: If uintX() could be force inlined or something in the future then probably it would make sense
11389
// 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)
90+
return { (data: Data, offset: inout Int) -> Int in
91+
precondition(data.endIndex - offset >= count)
12692
var result = 0
12793
for i in 0..<count {
128-
result += Int(truncatingIfNeeded: self.data[self.offset]) << (8 * i)
129-
self.offset += 1
94+
result += Int(truncatingIfNeeded: data[offset]) << (8 * i)
95+
offset += 1
13096
}
13197
return result
132-
#endif
98+
} (self.data, &self.offset)
13399
}
134100

135101
/**
@@ -138,17 +104,11 @@ public class ByteReader {
138104
- Precondition: There MUST be enough data left.
139105
*/
140106
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].toU64()
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
107+
return { (data: Data, offset: inout Int) -> UInt64 in
108+
precondition(data.endIndex - offset >= 8)
109+
defer { offset += 8 }
110+
return data[offset..<offset + 8].toU64()
111+
} (self.data, &self.offset)
152112
}
153113

154114
/**
@@ -162,25 +122,15 @@ public class ByteReader {
162122
*/
163123
public func uint64(fromBytes count: Int) -> UInt64 {
164124
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)
125+
return { (data: Data, offset: inout Int) -> UInt64 in
126+
precondition(data.endIndex - offset >= count)
177127
var result = 0 as UInt64
178128
for i in 0..<count {
179-
result += UInt64(truncatingIfNeeded: self.data[self.offset]) << (8 * i)
180-
self.offset += 1
129+
result += UInt64(truncatingIfNeeded: data[offset]) << (8 * i)
130+
offset += 1
181131
}
182132
return result
183-
#endif
133+
} (self.data, &self.offset)
184134
}
185135

186136
/**
@@ -189,17 +139,11 @@ public class ByteReader {
189139
- Precondition: There MUST be enough data left.
190140
*/
191141
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].toU32()
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
142+
return { (data: Data, offset: inout Int) -> UInt32 in
143+
precondition(data.endIndex - offset >= 4)
144+
defer { offset += 4 }
145+
return data[offset..<offset + 4].toU32()
146+
} (self.data, &self.offset)
203147
}
204148

205149
/**
@@ -213,25 +157,15 @@ public class ByteReader {
213157
*/
214158
public func uint32(fromBytes count: Int) -> UInt32 {
215159
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)
160+
return { (data: Data, offset: inout Int) -> UInt32 in
161+
precondition(data.endIndex - offset >= count)
228162
var result = 0 as UInt32
229163
for i in 0..<count {
230-
result += UInt32(truncatingIfNeeded: self.data[self.offset]) << (8 * i)
231-
self.offset += 1
164+
result += UInt32(truncatingIfNeeded: data[offset]) << (8 * i)
165+
offset += 1
232166
}
233167
return result
234-
#endif
168+
} (self.data, &self.offset)
235169
}
236170

237171
/**
@@ -240,17 +174,11 @@ public class ByteReader {
240174
- Precondition: There MUST be enough data left.
241175
*/
242176
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].toU16()
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
177+
return { (data: Data, offset: inout Int) -> UInt16 in
178+
precondition(data.endIndex - offset >= 2)
179+
defer { offset += 2 }
180+
return data[offset..<offset + 2].toU16()
181+
} (self.data, &self.offset)
254182
}
255183

256184
/**
@@ -264,25 +192,15 @@ public class ByteReader {
264192
*/
265193
public func uint16(fromBytes count: Int) -> UInt16 {
266194
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)
195+
return { (data: Data, offset: inout Int) -> UInt16 in
196+
precondition(data.endIndex - offset >= count)
279197
var result = 0 as UInt16
280198
for i in 0..<count {
281-
result += UInt16(truncatingIfNeeded: self.data[self.offset]) << (8 * i)
282-
self.offset += 1
199+
result += UInt16(truncatingIfNeeded: data[offset]) << (8 * i)
200+
offset += 1
283201
}
284202
return result
285-
#endif
203+
} (self.data, &self.offset)
286204
}
287205

288206
}

0 commit comments

Comments
 (0)