浏览代码

sizeof, sizeofValue -> MemoryLayout<>.size

Marcin Krzyżanowski 9 年之前
父节点
当前提交
e2f19bea70

+ 3 - 3
CryptoSwiftTests/ExtensionsTest.swift

@@ -43,7 +43,7 @@ final class ExtensionsTest: XCTestCase {
     }
     
     func testBytes() {
-        let size = sizeof(UInt32.self) // 32 or 64  bit
+        let size = MemoryLayout<UInt32>.size // 32 or 64  bit
         
         let i:UInt32 = 1024
         var bytes = i.bytes()
@@ -67,8 +67,8 @@ final class ExtensionsTest: XCTestCase {
         let ii:Int = 21
         XCTAssert(ii &<< 1 == ii << 1, "shift left failed")
         XCTAssert(ii &<< 8 == ii << 8, "shift left failed")
-        XCTAssert(ii &<< ((sizeofValue(ii) * 8) - 1) == ii << ((sizeofValue(ii) * 8) - 1), "shift left failed")
-        XCTAssert(ii &<< ((sizeofValue(ii) * 8)) == 0, "shift left failed")
+        XCTAssert(ii &<< ((MemoryLayout<Int>.size * 8) - 1) == ii << ((MemoryLayout<Int>.size * 8) - 1), "shift left failed")
+        XCTAssert(ii &<< ((MemoryLayout<Int>.size * 8)) == 0, "shift left failed")
         
         let iii:UInt32 = 21
         XCTAssert(iii &<< 1 == iii << 1, "shift left failed")

+ 1 - 1
CryptoSwiftTests/HashTests.swift

@@ -51,7 +51,7 @@ final class CryptoSwiftTests: XCTestCase {
     
     func testMD5PerformanceCommonCrypto() {
         self.measureMetrics([XCTPerformanceMetric_WallClockTime], automaticallyStartMeasuring: false, for: { () -> Void in
-            let buf: UnsafeMutableRawPointer = calloc(1024 * 1024, sizeof(UInt8.self))
+            let buf: UnsafeMutableRawPointer = calloc(1024 * 1024, MemoryLayout<UInt8>.size)
             let data = NSData(bytes: buf, length: 1024 * 1024)
             let md = UnsafeMutablePointer<UInt8>.allocate(capacity: Int(CC_MD5_DIGEST_LENGTH))
             self.startMeasuring()

+ 1 - 1
Sources/CryptoSwift/AES.swift

@@ -326,7 +326,7 @@ fileprivate extension AES {
                 tmp[wordIdx] = w[4*(i-1)+wordIdx]
             }
             if ((i % variant.Nk) == 0) {
-                tmp = subWord(rotateLeft(UInt32.with(tmp), by: 8).bytes(totalBytes: sizeof(UInt32.self)))
+                tmp = subWord(rotateLeft(UInt32.with(tmp), by: 8).bytes(totalBytes: MemoryLayout<UInt32>.size))
                 tmp[0] = tmp.first! ^ Rcon[i/variant.Nk]
             } else if (variant.Nk > 6 && (i % variant.Nk) == 4) {
                 tmp = subWord(tmp)

+ 5 - 5
Sources/CryptoSwift/Collection+Extension.swift

@@ -10,7 +10,7 @@ extension Collection where Self.Iterator.Element == UInt8, Self.Index == Int {
     func toUInt32Array() -> Array<UInt32> {
         var result = Array<UInt32>()
         result.reserveCapacity(16)
-        for idx in stride(from: self.startIndex, to: self.endIndex, by: sizeof(UInt32.self)) {
+        for idx in stride(from: self.startIndex, to: self.endIndex, by: MemoryLayout<UInt32>.size) {
             let val1:UInt32 = (UInt32(self[idx.advanced(by: 3)]) << 24)
             let val2:UInt32 = (UInt32(self[idx.advanced(by: 2)]) << 16)
             let val3:UInt32 = (UInt32(self[idx.advanced(by: 1)]) << 8)
@@ -24,7 +24,7 @@ extension Collection where Self.Iterator.Element == UInt8, Self.Index == Int {
     func toUInt64Array() -> Array<UInt64> {
         var result = Array<UInt64>()
         result.reserveCapacity(32)
-        for idx in stride(from: self.startIndex, to: self.endIndex, by: sizeof(UInt64.self)) {
+        for idx in stride(from: self.startIndex, to: self.endIndex, by: MemoryLayout<UInt64>.size) {
             var val:UInt64 = 0
             val |= UInt64(self[idx.advanced(by: 7)]) << 56
             val |= UInt64(self[idx.advanced(by: 6)]) << 48
@@ -46,14 +46,14 @@ extension Collection where Self.Iterator.Element == UInt8, Self.Index == Int {
         }
 
         var bytes = self.reversed() //FIXME: check it this is equivalent of Array(...)
-        if bytes.count < sizeof(T.self) {
-            let paddingCount = sizeof(T.self) - bytes.count
+        if bytes.count < MemoryLayout<T>.size {
+            let paddingCount = MemoryLayout<T>.size - bytes.count
             if (paddingCount > 0) {
                 bytes += Array<UInt8>(repeating: 0, count: paddingCount)
             }
         }
 
-        if sizeof(T.self) == 1 {
+        if MemoryLayout<T>.size == 1 {
             return T(truncatingBitPattern: UInt64(bytes[0]))
         }
 

+ 3 - 3
Sources/CryptoSwift/Generics.swift

@@ -36,14 +36,14 @@ func integerFrom<T: UnsignedInteger>(_ bits: Array<Bit>) -> T
 /// Array of bytes, little-endian representation. Don't use if not necessary.
 /// I found this method slow
 func arrayOfBytes<T>(value:T, length:Int? = nil) -> Array<UInt8> {
-    let totalBytes = length ?? sizeof(T.self)
+    let totalBytes = length ?? MemoryLayout<T>.size
 
     let valuePointer = UnsafeMutablePointer<T>.allocate(capacity: 1)
     valuePointer.pointee = value
 
     let bytesPointer = UnsafeMutablePointer<UInt8>(OpaquePointer(valuePointer))
     var bytes = Array<UInt8>(repeating: 0, count: totalBytes)
-    for j in 0..<min(sizeof(T.self),totalBytes) {
+    for j in 0..<min(MemoryLayout<T>.size,totalBytes) {
         bytes[totalBytes - 1 - j] = (bytesPointer + j).pointee
     }
     
@@ -78,7 +78,7 @@ func shiftLeft<T: SignedInteger>(_ value: T, by count: Int) -> T where T: Initia
         return 0;
     }
     
-    let bitsCount = (sizeofValue(value) * 8)
+    let bitsCount = (MemoryLayout<T>.size * 8)
     let shiftCount = Int(Swift.min(count, bitsCount - 1))
     
     var shiftedValue:T = 0;

+ 2 - 2
Sources/CryptoSwift/IntExtension.swift

@@ -31,7 +31,7 @@ extension Int {
 /* array of bytes */
 extension Int {
     /** Array of bytes with optional padding (little-endian) */
-    public func bytes(totalBytes: Int = sizeof(Int.self)) -> Array<UInt8> {
+    public func bytes(totalBytes: Int = MemoryLayout<Int>.size) -> Array<UInt8> {
         return arrayOfBytes(value: self, length: totalBytes)
     }
 
@@ -57,7 +57,7 @@ extension Int {
             return
         }
         
-        let bitsCount = sizeofValue(self) * 8
+        let bitsCount = MemoryLayout<Int>.size * 8
 
         if (count >= bitsCount) {
             return

+ 2 - 2
Sources/CryptoSwift/SHA1.swift

@@ -34,8 +34,8 @@ final class SHA1 : HashProtocol {
             for x in 0..<M.count {
                 switch (x) {
                 case 0...15:
-                    let start = chunk.startIndex + (x * sizeofValue(M[x]))
-                    let end = start + sizeofValue(M[x])
+                    let start = chunk.startIndex + (x * MemoryLayout<UInt32>.size)
+                    let end = start + MemoryLayout<UInt32>.size
                     let le = chunk[start..<end].toUInt32Array()[0]
                     M[x] = le.bigEndian
                     break

+ 5 - 5
Sources/CryptoSwift/SHA2.swift

@@ -130,12 +130,12 @@ final class SHA2 : HashProtocol {
         for chunk in BytesSequence(chunkSize: chunkSizeBytes, data: tmpMessage) {
             // break chunk into sixteen 32-bit words M[j], 0 ≤ j ≤ 15, big-endian
             // Extend the sixteen 32-bit words into sixty-four 32-bit words:
-            var M:Array<UInt32> = Array<UInt32>(repeating: 0, count: variant.k.count)
+            var M = Array<UInt32>(repeating: 0, count: variant.k.count)
             for x in 0..<M.count {
                 switch (x) {
                 case 0...15:
-                    let start = chunk.startIndex + (x * sizeofValue(M[x]))
-                    let end = start + sizeofValue(M[x])
+                    let start = chunk.startIndex + (x * MemoryLayout<UInt32>.size)
+                    let end = start + MemoryLayout<UInt32>.size
                     let le = chunk[start..<end].toUInt32Array()[0]
                     M[x] = le.bigEndian
                     break
@@ -217,8 +217,8 @@ final class SHA2 : HashProtocol {
             for x in 0..<M.count {
                 switch (x) {
                 case 0...15:
-                    let start = chunk.startIndex + (x * sizeofValue(M[x]))
-                    let end = start + sizeofValue(M[x])
+                    let start = chunk.startIndex + (x * MemoryLayout<UInt64>.size)
+                    let end = start + MemoryLayout<UInt64>.size
                     let le = chunk[start..<end].toUInt64Array()[0]
                     M[x] = le.bigEndian
                     break

+ 3 - 3
Sources/CryptoSwift/UInt32Extension.swift

@@ -18,7 +18,7 @@ extension UInt32: _UInt32Type {}
 
 /** array of bytes */
 extension UInt32 {
-    public func bytes(totalBytes: Int = sizeof(UInt32.self)) -> Array<UInt8> {
+    public func bytes(totalBytes: Int = MemoryLayout<UInt32>.size) -> Array<UInt8> {
         return arrayOfBytes(value: self, length: totalBytes)
     }
 
@@ -37,7 +37,7 @@ extension UInt32 {
             return
         }
         
-        let bitsCount = UInt32(sizeof(UInt32.self) * 8)
+        let bitsCount = UInt32(MemoryLayout<UInt32>.size * 8)
         let shiftCount = Swift.min(count, bitsCount - 1)
         var shiftedValue:UInt32 = 0;
         
@@ -63,7 +63,7 @@ extension UInt32 {
             return
         }
         
-        let bitsCount = UInt32(sizeofValue(self) * 8)
+        let bitsCount = UInt32(MemoryLayout<UInt32>.size * 8)
 
         if (count >= bitsCount) {
             return

+ 1 - 1
Sources/CryptoSwift/UInt64Extension.swift

@@ -8,7 +8,7 @@
 
 /** array of bytes */
 extension UInt64 {
-    public func bytes(totalBytes: Int = sizeof(UInt64.self)) -> Array<UInt8> {
+    public func bytes(totalBytes: Int = MemoryLayout<UInt64>.size) -> Array<UInt8> {
         return arrayOfBytes(value: self, length: totalBytes)
     }
 

+ 2 - 2
Sources/CryptoSwift/UInt8Extension.swift

@@ -46,7 +46,7 @@ extension UInt8 {
     
     /** array of bits */
     func bits() -> [Bit] {
-        let totalBitsCount = sizeofValue(self) * 8
+        let totalBitsCount = MemoryLayout<UInt8>.size * 8
         
         var bitsArray = [Bit](repeating: Bit.zero, count: totalBitsCount)
         
@@ -80,7 +80,7 @@ extension UInt8 {
             return
         }
 
-        let bitsCount = UInt8(sizeof(UInt8.self) * 8)
+        let bitsCount = UInt8(MemoryLayout<UInt8>.size * 8)
 
         if (count >= bitsCount) {
             return