Skip to content

Instantly share code, notes, and snippets.

@Burgestrand
Created April 10, 2019 11:33
Show Gist options
  • Save Burgestrand/5a0d742138f6c23a09879b4bae2f8e77 to your computer and use it in GitHub Desktop.
Save Burgestrand/5a0d742138f6c23a09879b4bae2f8e77 to your computer and use it in GitHub Desktop.
import Foundation
func test(count: Int) -> UInt8 {
precondition(count >= 1)
let ptr = UnsafeMutableRawPointer.allocate(byteCount: count, alignment: 1)
ptr.storeBytes(of: 0, toByteOffset: 0, as: UInt8.self)
let data = Data(bytesNoCopy: ptr, count: count, deallocator: .none)
ptr.storeBytes(of: 13, toByteOffset: 0, as: UInt8.self)
return data[0]
}
for i in 1...24 {
print("\(i): \(test(count: i))")
}
1: 0
2: 0
3: 0
4: 0
5: 0
6: 0
7: 0
8: 0
9: 0
10: 0
11: 0
12: 0
13: 0
14: 0
15: 13
16: 13
17: 13
18: 13
19: 13
20: 13
21: 13
22: 13
23: 13
24: 13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment