Skip to content

Commit bea3f1d

Browse files
committed
Add and use specialized (for UInt16/32/64) versions of Data.to<T>(type:) extension function
1 parent e5ad40d commit bea3f1d

4 files changed

Lines changed: 21 additions & 11 deletions

File tree

Sources/ByteReader.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public class ByteReader {
142142
return { (data: Data, offset: inout Int) -> UInt64 in
143143
precondition(data.endIndex - offset >= 8)
144144
defer { offset += 8 }
145-
return data[offset..<offset + 8].to(type: UInt64.self)
145+
return data[offset..<offset + 8].toU64()
146146
} (self.data, &self.offset)
147147
#else
148148
precondition(bytesLeft >= 8)
@@ -193,7 +193,7 @@ public class ByteReader {
193193
return { (data: Data, offset: inout Int) -> UInt32 in
194194
precondition(data.endIndex - offset >= 4)
195195
defer { offset += 4 }
196-
return data[offset..<offset + 4].to(type: UInt32.self)
196+
return data[offset..<offset + 4].toU32()
197197
} (self.data, &self.offset)
198198
#else
199199
precondition(bytesLeft >= 4)
@@ -244,7 +244,7 @@ public class ByteReader {
244244
return { (data: Data, offset: inout Int) -> UInt16 in
245245
precondition(data.endIndex - offset >= 2)
246246
defer { offset += 2 }
247-
return data[offset..<offset + 2].to(type: UInt16.self)
247+
return data[offset..<offset + 2].toU16()
248248
} (self.data, &self.offset)
249249
#else
250250
precondition(bytesLeft >= 2)

Sources/Extensions.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,18 @@ import Foundation
88
extension Data {
99

1010
@inline(__always)
11-
func to<T>(type: T.Type) -> T {
12-
return self.withUnsafeBytes { $0.bindMemory(to: type)[0] }
11+
func toU16() -> UInt16 {
12+
return self.withUnsafeBytes { $0.bindMemory(to: UInt16.self)[0] }
13+
}
14+
15+
@inline(__always)
16+
func toU32() -> UInt32 {
17+
return self.withUnsafeBytes { $0.bindMemory(to: UInt32.self)[0] }
18+
}
19+
20+
@inline(__always)
21+
func toU64() -> UInt64 {
22+
return self.withUnsafeBytes { $0.bindMemory(to: UInt64.self)[0] }
1323
}
1424

1525
@inline(__always)

Sources/LsbBitReader.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ public final class LsbBitReader: ByteReader, BitReader {
356356
precondition(bitMask == 1, "BitReader is not aligned.")
357357
precondition(data.endIndex - offset >= 8)
358358
defer { offset += 8 }
359-
return data[offset..<offset + 8].to(type: UInt64.self)
359+
return data[offset..<offset + 8].toU64()
360360
} (self.data, &self.offset, self.bitMask)
361361
#else
362362
precondition(bitMask == 1, "BitReader is not aligned.")
@@ -410,7 +410,7 @@ public final class LsbBitReader: ByteReader, BitReader {
410410
precondition(bitMask == 1, "BitReader is not aligned.")
411411
precondition(data.endIndex - offset >= 4)
412412
defer { offset += 4 }
413-
return data[offset..<offset + 4].to(type: UInt32.self)
413+
return data[offset..<offset + 4].toU32()
414414
} (self.data, &self.offset, self.bitMask)
415415
#else
416416
precondition(bitMask == 1, "BitReader is not aligned.")
@@ -464,7 +464,7 @@ public final class LsbBitReader: ByteReader, BitReader {
464464
precondition(bitMask == 1, "BitReader is not aligned.")
465465
precondition(data.endIndex - offset >= 2)
466466
defer { offset += 2 }
467-
return data[offset..<offset + 2].to(type: UInt16.self)
467+
return data[offset..<offset + 2].toU16()
468468
} (self.data, &self.offset, self.bitMask)
469469
#else
470470
precondition(bitMask == 1, "BitReader is not aligned.")

Sources/MsbBitReader.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ public final class MsbBitReader: ByteReader, BitReader {
356356
precondition(bitMask == 128, "BitReader is not aligned.")
357357
precondition(data.endIndex - offset >= 8)
358358
defer { offset += 8 }
359-
return data[offset..<offset + 8].to(type: UInt64.self)
359+
return data[offset..<offset + 8].toU64()
360360
} (self.data, &self.offset, self.bitMask)
361361
#else
362362
precondition(bitMask == 128, "BitReader is not aligned.")
@@ -410,7 +410,7 @@ public final class MsbBitReader: ByteReader, BitReader {
410410
precondition(bitMask == 128, "BitReader is not aligned.")
411411
precondition(data.endIndex - offset >= 4)
412412
defer { offset += 4 }
413-
return data[offset..<offset + 4].to(type: UInt32.self)
413+
return data[offset..<offset + 4].toU32()
414414
} (self.data, &self.offset, self.bitMask)
415415
#else
416416
precondition(bitMask == 128, "BitReader is not aligned.")
@@ -464,7 +464,7 @@ public final class MsbBitReader: ByteReader, BitReader {
464464
precondition(bitMask == 128, "BitReader is not aligned.")
465465
precondition(data.endIndex - offset >= 2)
466466
defer { offset += 2 }
467-
return data[offset..<offset + 2].to(type: UInt16.self)
467+
return data[offset..<offset + 2].toU16()
468468
} (self.data, &self.offset, self.bitMask)
469469
#else
470470
precondition(bitMask == 128, "BitReader is not aligned.")

0 commit comments

Comments
 (0)