浏览代码

Merge pull request #203 from gilt/xcode-7.3-deprecations

Remove all C-style for loops
Marcin Krzyzanowski 9 年之前
父节点
当前提交
29142fe258
共有 2 个文件被更改,包括 13 次插入7 次删除
  1. 2 1
      Sources/CryptoSwift/Array+Extension.swift
  2. 11 6
      Sources/CryptoSwift/Rabbit.swift

+ 2 - 1
Sources/CryptoSwift/Array+Extension.swift

@@ -12,9 +12,10 @@ extension Array {
     func chunks(chunksize:Int) -> [Array<Element>] {
         var words = [[Element]]()
         words.reserveCapacity(self.count / chunksize)        
-        for var idx = chunksize; idx <= self.count; idx = idx + chunksize {
+        for var idx in chunksize...self.count {
             let word = Array(self[idx - chunksize..<idx]) // this is slow for large table
             words.append(word)
+            idx = idx + chunksize
         }
         let reminder = Array(self.suffix(self.count % chunksize))
         if (reminder.count > 0) {

+ 11 - 6
Sources/CryptoSwift/Rabbit.swift

@@ -64,12 +64,12 @@ final public class Rabbit {
         
         // Key divided into 8 subkeys
         var k = [UInt32](count: 8, repeatedValue: 0)
-        for var j = 0; j < 8; j += 1 {
+        for j in 0..<8 {
             k[j] = UInt32(key[Rabbit.blockSize - (2*j + 1)]) | (UInt32(key[Rabbit.blockSize - (2*j + 2)]) << 8)
         }
         
         // Initialize state and counter variables from subkeys
-        for var j = 0; j < 8; j += 1 {
+        for j in 0..<8 {
             if j % 2 == 0 {
                 x[j] = (k[(j+1) % 8] << 16) | k[j]
                 c[j] = (k[(j+4) % 8] << 16) | k[(j+5) % 8]
@@ -86,7 +86,7 @@ final public class Rabbit {
         nextState()
         
         // Reinitialize counter variables
-        for var j = 0; j < 8; j += 1 {
+        for j in 0..<8 {
             c[j] = c[j] ^ x[(j+4) % 8]
         }
         
@@ -123,7 +123,7 @@ final public class Rabbit {
     private func nextState() {
         // Before an iteration the counters are incremented
         var carry = p7
-        for var j = 0; j < 8; j += 1 {
+        for j in 0..<8 {
             let prev = c[j]
             c[j] = prev &+ a[j] &+ carry
             carry = prev > c[j] ? 1 : 0 // detect overflow
@@ -163,7 +163,7 @@ final public class Rabbit {
         output16[0] = UInt16(truncatingBitPattern: x[6] >> 16) ^ UInt16(truncatingBitPattern: x[1])
         
         var output8 = [UInt8](count: Rabbit.blockSize, repeatedValue: 0)
-        for var j = 0; j < output16.count; j += 1 {
+        for j in 0..<output16.count {
             output8[j * 2] = UInt8(truncatingBitPattern: output16[j] >> 8)
             output8[j * 2 + 1] = UInt8(truncatingBitPattern: output16[j])
         }
@@ -176,13 +176,18 @@ final public class Rabbit {
         
         var result = [UInt8](count: bytes.count, repeatedValue: 0)
         var output = nextOutput()
-        for var byteIdx = 0, outputIdx = 0; byteIdx < bytes.count; byteIdx += 1, outputIdx += 1 {
+        var byteIdx = 0
+        var outputIdx = 0
+        while byteIdx < bytes.count {
             if (outputIdx == Rabbit.blockSize) {
                 output = nextOutput()
                 outputIdx = 0
             }
             
             result[byteIdx] = bytes[byteIdx] ^ output[outputIdx]
+
+            byteIdx += 1
+            outputIdx += 1
         }
         return result
     }