Эх сурвалжийг харах

Convert to latest swift syntax and omit some external parameter name

Norio Nomura 10 жил өмнө
parent
commit
819820e3d7

+ 2 - 0
Base32.xcodeproj/project.pbxproj

@@ -422,6 +422,8 @@
 		6CA0A78F1A74E80600AC539F /* Project object */ = {
 			isa = PBXProject;
 			attributes = {
+				LastSwiftMigration = 0700;
+				LastSwiftUpdateCheck = 0700;
 				LastUpgradeCheck = 0610;
 				ORGANIZATIONNAME = "Norio Nomura";
 				TargetAttributes = {

+ 4 - 4
Base32/Base16.swift

@@ -93,9 +93,9 @@ extension NSData {
 }
 
 // MARK: encode
-private func base16encode(data: UnsafePointer<Void>, length: Int, _ uppercase: Bool = true) -> String {
+private func base16encode(data: UnsafePointer<Void>, _ length: Int, _ uppercase: Bool = true) -> String {
     let array = UnsafeBufferPointer<UInt8>(start: UnsafePointer<UInt8>(data), count: length)
-    return map(array) { String(format: uppercase ? "%02X" : "%02x", $0) }.reduce("", combine: +)
+    return array.map { String(format: uppercase ? "%02X" : "%02x", $0) }.reduce("", combine: +)
 }
 
 // MARK: decode
@@ -106,7 +106,7 @@ extension UnicodeScalar {
         case "a"..."f": return UInt8(value - UnicodeScalar("a").value + 0xa)
         case "A"..."F": return UInt8(value - UnicodeScalar("A").value + 0xa)
         default:
-            println("base16decode: Invalid hex character \(self)")
+            print("base16decode: Invalid hex character \(self)")
             return nil
         }
     }
@@ -116,7 +116,7 @@ private func base16decode(string: String) -> [UInt8]? {
     // validate length
     let lenght = string.nulTerminatedUTF8.count - 1
     if lenght % 2 != 0 {
-        println("base16decode: String must contain even number of characters")
+        print("base16decode: String must contain even number of characters")
         return nil
     }
     var g = string.unicodeScalars.generate()

+ 7 - 12
Base32/Base32.swift

@@ -164,7 +164,7 @@ let alphabetEncodeTable: [Int8] = ["A","B","C","D","E","F","G","H","I","J","K","
 
 let extendedHexAlphabetEncodeTable: [Int8] = ["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V"]
 
-private func base32encode(data: UnsafePointer<Void>, var length: Int, table: [Int8]) -> String {
+private func base32encode(data: UnsafePointer<Void>, var _ length: Int, _ table: [Int8]) -> String {
     if length == 0 {
         return ""
     }
@@ -290,17 +290,12 @@ let extendedHexAlphabetDecodeTable: [UInt8] = [
 ]
 
 
-private func base32decode(string: String, table: [UInt8]) -> [UInt8]? {
-    let length = count(string.unicodeScalars)
+private func base32decode(string: String, _ table: [UInt8]) -> [UInt8]? {
+    let length = string.unicodeScalars.count
     if length == 0 {
         return []
     }
     
-    // search element index that condition is true.
-    func index_of<C : CollectionType where C.Generator.Element : Equatable>(domain: C, condition: C.Generator.Element -> Bool) -> C.Index? {
-        return find(lazy(domain).map(condition), true)
-    }
-    
     // calc padding length
     func getLeastPaddingLength(string: String) -> Int {
         if string.hasSuffix("======") {
@@ -318,12 +313,12 @@ private func base32decode(string: String, table: [UInt8]) -> [UInt8]? {
     
     // validate string
     let leastPaddingLength = getLeastPaddingLength(string)
-    if let index = index_of(string.unicodeScalars, {$0.value > 0xff || table[Int($0.value)] > 31}) {
+    if let index = string.unicodeScalars.indexOf({$0.value > 0xff || table[Int($0.value)] > 31}) {
         // index points padding "=" or invalid character that table does not contain.
-        let pos = distance(string.unicodeScalars.startIndex, index)
+        let pos = string.unicodeScalars.startIndex.distanceTo(index)
         // if pos points padding "=", it's valid.
         if pos != length - leastPaddingLength {
-            println("string contains some invalid characters.")
+            print("string contains some invalid characters.")
             return nil
         }
     }
@@ -338,7 +333,7 @@ private func base32decode(string: String, table: [UInt8]) -> [UInt8]? {
     case 5: additionalBytes = 3
     case 7: additionalBytes = 4
     default:
-        println("string length is invalid.")
+        print("string length is invalid.")
         return nil
     }
     

+ 12 - 12
SecEncodeTransformTests/SecEncodeTransformTests.swift

@@ -40,8 +40,8 @@ class SecEncodeTransformTests: XCTestCase {
 
     // MARK: Using SecEncodeTransform
     func test_RFC4648_Encode_UsingSecEncodeTransform() {
-        var results = Array<String>(count: count(convertedVectors), repeatedValue: "")
-        let vectorsAndIndices = Zip2(convertedVectors, indices(results))
+        var results = Array<String>(count: convertedVectors.count, repeatedValue: "")
+        let vectorsAndIndices = Zip2Sequence(convertedVectors, results.indices)
         self.measureBlock{
             for _ in 0...100 {
                 for ((test, _, _), index) in vectorsAndIndices {
@@ -49,14 +49,14 @@ class SecEncodeTransformTests: XCTestCase {
                 }
             }
         }
-        for ((test, expect, _), result) in Zip2(convertedVectors, results) {
+        for ((test, expect, _), result) in Zip2Sequence(convertedVectors, results) {
             XCTAssertEqual(result, expect, "TTTBase32EncodedStringFromData for \(test)")
         }
     }
     
     func test_RFC4648_Decode_UsingSecEncodeTransform() {
-        var results = Array<NSData>(count: count(convertedVectors), repeatedValue: NSData())
-        let vectorsAndIndices = Zip2(convertedVectors, indices(results))
+        var results = Array<NSData>(count: convertedVectors.count, repeatedValue: NSData())
+        let vectorsAndIndices = Zip2Sequence(convertedVectors, results.indices)
         self.measureBlock{
             for _ in 0...100 {
                 for ((_, test, _), index) in vectorsAndIndices {
@@ -64,15 +64,15 @@ class SecEncodeTransformTests: XCTestCase {
                 }
             }
         }
-        for ((expect, test, _), result) in Zip2(convertedVectors, results) {
+        for ((expect, test, _), result) in Zip2Sequence(convertedVectors, results) {
             XCTAssertEqual(result, expect, "TTTDataFromBase32EncodedString for \(test)")
         }
     }
     
     // MARK: Using Base32
     func test_RFC4648_Encode_UsingBase32() {
-        var results = Array<String>(count: count(convertedVectors), repeatedValue: "")
-        let vectorsAndIndices = Zip2(convertedVectors, indices(results))
+        var results = Array<String>(count: convertedVectors.count, repeatedValue: "")
+        let vectorsAndIndices = Zip2Sequence(convertedVectors, results.indices)
         self.measureBlock{
             for _ in 0...100 {
                 for ((test, _, _), index) in vectorsAndIndices {
@@ -80,14 +80,14 @@ class SecEncodeTransformTests: XCTestCase {
                 }
             }
         }
-        for ((test, expect, _), result) in Zip2(convertedVectors, results) {
+        for ((test, expect, _), result) in Zip2Sequence(convertedVectors, results) {
             XCTAssertEqual(result, expect, "base32Encode for \(test)")
         }
     }
     
     func test_RFC4648_Decode_UsingBase32() {
-        var results = Array<NSData>(count: count(convertedVectors), repeatedValue: NSData())
-        let vectorsAndIndices = Zip2(convertedVectors, indices(results))
+        var results = Array<NSData>(count: convertedVectors.count, repeatedValue: NSData())
+        let vectorsAndIndices = Zip2Sequence(convertedVectors, results.indices)
         self.measureBlock{
             for _ in 0...100 {
                 for ((_, test, _), index) in vectorsAndIndices {
@@ -95,7 +95,7 @@ class SecEncodeTransformTests: XCTestCase {
                 }
             }
         }
-        for ((expect, test, _), result) in Zip2(convertedVectors, results) {
+        for ((expect, test, _), result) in Zip2Sequence(convertedVectors, results) {
             XCTAssertEqual(result, expect, "base32Decode for \(test)")
         }
     }