added iOS source code
[wl-app.git] / iOS / Pods / OAuthSwift / Sources / Int+OAuthSwift.swift
1 //
2 //  Int+OAuthSwift.swift
3 //  OAuthSwift
4 //
5 //  Created by Dongri Jin on 1/28/15.
6 //  Copyright (c) 2015 Dongri Jin. All rights reserved.
7 //
8
9 import Foundation
10
11 extension Int {
12     public func bytes(_ totalBytes: Int = MemoryLayout<Int>.size) -> [UInt8] {
13         return arrayOfBytes(self, length: totalBytes)
14     }
15 }
16
17 private func arrayOfBytes<T>(_ value: T, length: Int? = nil) -> [UInt8] {
18     let totalBytes = length ?? MemoryLayout<T>.size
19
20     let valuePointer = UnsafeMutablePointer<T>.allocate(capacity: 1)
21     valuePointer.pointee = value
22
23     let bytesPointer = UnsafeMutablePointer<UInt8>(OpaquePointer(valuePointer))
24     var bytes = [UInt8](repeating: 0, count: totalBytes)
25     for j in 0..<min(MemoryLayout<T>.size, totalBytes) {
26         bytes[totalBytes - 1 - j] = (bytesPointer + j).pointee
27     }
28
29     valuePointer.deinitialize(count: 1)
30     valuePointer.deallocate()
31
32     return bytes
33 }