浏览代码

Remove bit shifting code. It's Implemented as part of SE-0104 in current Swift.

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

+ 0 - 65
Sources/CryptoSwift/Int+Extension.swift

@@ -40,68 +40,3 @@ extension Int {
         return arrayOfBytes(value: self, length: totalBytes)
     }
 }
-
-
-
-/** Shift bits */
-extension Int {
-    
-    /** Shift bits to the left. All bits are shifted (including sign bit) */
-    mutating func shiftLeft(by count: Int) {
-        self = CryptoSwift.shiftLeft(self, by: count) //FIXME: count:
-    }
-    
-    /** Shift bits to the right. All bits are shifted (including sign bit) */
-    mutating func shiftRight(by count: Int) {
-        if (self == 0) {
-            return
-        }
-        
-        let bitsCount = MemoryLayout<Int>.size * 8
-
-        if (count >= bitsCount) {
-            return
-        }
-
-        let maxBitsForValue = Int(floor(log2(Double(self)) + 1))
-        let shiftCount = Swift.min(count, maxBitsForValue - 1)
-        var shiftedValue:Int = 0;
-        
-        for bitIdx in 0..<bitsCount {
-            // if bit is set then copy to result and shift left 1
-            let bit = 1 << bitIdx
-            if ((self & bit) == bit) {
-                shiftedValue = shiftedValue | (bit >> shiftCount)
-            }
-        }
-        self = Int(shiftedValue)
-    }
-}
-
-// Left operator
-
-/** shift left and assign with bits truncation */
-func &<<= (lhs: inout Int, rhs: Int) {
-    lhs.shiftLeft(by: rhs)
-}
-
-/** shift left with bits truncation */
-func &<< (lhs: Int, rhs: Int) -> Int {
-    var l = lhs;
-    l.shiftLeft(by: rhs)
-    return l
-}
-
-// Right operator
-
-/** shift right and assign with bits truncation */
-func &>>= (lhs: inout Int, rhs: Int) {
-    lhs.shiftRight(by: rhs)
-}
-
-/** shift right and assign with bits truncation */
-func &>> (lhs: Int, rhs: Int) -> Int {
-    var l = lhs;
-    l.shiftRight(by: rhs)
-    return l
-}

+ 2 - 6
Sources/CryptoSwift/Operators.swift

@@ -18,11 +18,7 @@ var i = 1       // init
 var j = i &<< 2 //shift left
 j &<<= 2        //shift left and assign
 
-
 @see: https://medium.com/@krzyzanowskim/swiftly-shift-bits-and-protect-yourself-be33016ce071
+ 
+This fuctonality is now implemented as part of Swift 3, SE-0104 https://github.com/apple/swift-evolution/blob/master/proposals/0104-improved-integers.md
 */
-
-infix operator &<<= : BitwiseShiftPrecedence
-infix operator &<< : BitwiseShiftPrecedence
-infix operator &>>= : BitwiseShiftPrecedence
-infix operator &>> : BitwiseShiftPrecedence

+ 0 - 81
Sources/CryptoSwift/UInt32+Extension.swift

@@ -26,84 +26,3 @@ extension UInt32 {
         return arrayOfBytes(value: self, length: totalBytes)
     }
 }
-
-/** Shift bits */
-extension UInt32 {
-    
-    /** Shift bits to the left. All bits are shifted (including sign bit) */
-    mutating func shiftLeft(by count: UInt32) {
-        if (self == 0) {
-            return
-        }
-        
-        let bitsCount = UInt32(MemoryLayout<UInt32>.size * 8)
-        let shiftCount = Swift.min(count, bitsCount - 1)
-        var shiftedValue:UInt32 = 0;
-        
-        for bitIdx in 0..<bitsCount {
-            // if bit is set then copy to result and shift left 1
-            let bit = 1 << bitIdx
-            if ((self & bit) == bit) {
-                shiftedValue = shiftedValue | (bit << shiftCount)
-            }
-        }
-        
-        if (shiftedValue != 0 && count >= bitsCount) {
-            // clear last bit that couldn't be shifted out of range
-            shiftedValue = shiftedValue & (~(1 << (bitsCount - 1)))
-        }
-
-        self = shiftedValue
-    }
-    
-    /** Shift bits to the right. All bits are shifted (including sign bit) */
-    mutating func shiftRight(by count: UInt32) {
-        if (self == 0) {
-            return
-        }
-        
-        let bitsCount = UInt32(MemoryLayout<UInt32>.size * 8)
-
-        if (count >= bitsCount) {
-            return
-        }
-
-        let maxBitsForValue = UInt32(floor(log2(Double(self)) + 1))
-        let shiftCount = Swift.min(count, maxBitsForValue - 1)
-        var shiftedValue:UInt32 = 0;
-        
-        for bitIdx in 0..<bitsCount {
-            // if bit is set then copy to result and shift left 1
-            let bit = 1 << bitIdx
-            if ((self & bit) == bit) {
-                shiftedValue = shiftedValue | (bit >> shiftCount)
-            }
-        }
-        self = shiftedValue
-    }
-
-}
-
-/** shift left and assign with bits truncation */
-func &<<= (lhs: inout UInt32, rhs: UInt32) {
-    lhs.shiftLeft(by: rhs)
-}
-
-/** shift left with bits truncation */
-func &<< (lhs: UInt32, rhs: UInt32) -> UInt32 {
-    var l = lhs;
-    l.shiftLeft(by: rhs)
-    return l
-}
-
-/** shift right and assign with bits truncation */
-func &>>= (lhs: inout UInt32, rhs: UInt32) {
-    lhs.shiftRight(by: rhs)
-}
-
-/** shift right and assign with bits truncation */
-func &>> (lhs: UInt32, rhs: UInt32) -> UInt32 {
-    var l = lhs;
-    l.shiftRight(by: rhs)
-    return l
-}

+ 0 - 35
Sources/CryptoSwift/UInt8+Extension.swift

@@ -71,38 +71,3 @@ extension UInt8 {
         return s
     }
 }
-
-/** Shift bits */
-extension UInt8 {
-    /** Shift bits to the right. All bits are shifted (including sign bit) */
-    mutating func shiftRight(by count: UInt8) {
-        if (self == 0) {
-            return
-        }
-
-        let bitsCount = UInt8(MemoryLayout<UInt8>.size * 8)
-
-        if (count >= bitsCount) {
-            return
-        }
-
-        let maxBitsForValue = UInt8(floor(log2(Double(self) + 1)))
-        let shiftCount = Swift.min(count, maxBitsForValue - 1)
-        var shiftedValue:UInt8 = 0;
-        
-        for bitIdx in 0..<bitsCount {
-            let byte = 1 << bitIdx
-            if ((self & byte) == byte) {
-                shiftedValue = shiftedValue | (byte >> shiftCount)
-            }
-        }
-        self = shiftedValue
-    }
-}
-
-/** shift right and assign with bits truncation */
-func &>> (lhs: UInt8, rhs: UInt8) -> UInt8 {
-    var l = lhs;
-    l.shiftRight(by: rhs)
-    return l
-}

+ 0 - 24
Tests/CryptoSwiftTests/ExtensionsTest.swift

@@ -48,29 +48,6 @@ final class ExtensionsTest: XCTestCase {
         XCTAssertTrue(bytes[14] == 4, "Invalid return type \(bytes.count)")
     }
     
-    func testShiftLeft() {
-        // Unsigned
-        let i:UInt32 = 1
-        XCTAssert(i &<< 1 == 2, "shift left failed")
-        XCTAssert(i &<< 8 == 256, "shift left failed")
-        XCTAssert(i &<< 31 == i << 31, "shift left failed")
-        XCTAssert(i &<< 32 == 0, "shift left failed")
-
-        // Signed
-        let ii:Int = 21
-        XCTAssert(ii &<< 1 == ii << 1, "shift left failed")
-        XCTAssert(ii &<< 8 == ii << 8, "shift left failed")
-        let shiftLeft1 = ii &<< ((MemoryLayout<Int>.size * 8) - 1)
-        let shiftLeft2 = ii << ((MemoryLayout<Int>.size * 8) - 1)
-        XCTAssert(shiftLeft1 == shiftLeft2, "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")
-        XCTAssert(iii &<< 8 == iii << 8, "shift left failed")
-        XCTAssert((iii &<< 32) == 0, "shift left failed")
-    }
-    
     func testToUInt32Array() {
         let chunk:ArraySlice<UInt8> = [1,1,1,7,2,3,4,5]
         let result = chunk.toUInt32Array()
@@ -114,7 +91,6 @@ final class ExtensionsTest: XCTestCase {
         ("testArrayChunksPerformance", testArrayChunksPerformance),
         ("testIntExtension", testIntExtension),
         ("testBytes", testBytes),
-        ("testShiftLeft", testShiftLeft),
         ("testToUInt32Array", testToUInt32Array),
         ("testDataInit", testDataInit),
         ("testStringEncrypt", testStringEncrypt),