Sfoglia il codice sorgente

Removed hexToBytes() and streamingHexBytes() from String Extension moving code into the array initializer Array(hex)

jose 8 anni fa
parent
commit
fbfb0f1519

+ 21 - 7
Sources/CryptoSwift/Array+Extension.swift

@@ -33,14 +33,28 @@ extension Array {
 extension Array where Element: Integer, Element.IntegerLiteralType == UInt8 {
 extension Array where Element: Integer, Element.IntegerLiteralType == UInt8 {
 
 
     public init(hex: String) {
     public init(hex: String) {
-        self.init()
-        
-        do{
-            try hex.streamHexBytes { (byte) in
-                self.append(byte as! Element)
+        self.init(reserveCapacity: hex.unicodeScalars.lazy.underestimatedCount)
+        let unicodeHexMap:[UnicodeScalar:UInt8] = ["0":0,"1":1,"2":2,"3":3,"4":4,"5":5,"6":6,"7":7,"8":8,"9":9,"a":10,"b":11,"c":12,"d":13,"e":14,"f":15,"A":10,"B":11,"C":12,"D":13,"E":14,"F":15]
+        var buffer:UInt8?
+        var skip = hex.hasPrefix("0x") ? 2 : 0
+        for char in hex.unicodeScalars.lazy {
+            guard skip == 0 else {
+                skip -= 1
+                continue
             }
             }
-        } catch _{
-            self.removeAll()
+            guard let value = unicodeHexMap[char] else {
+                self.removeAll()
+                return
+            }
+            if let b = buffer {
+                self.append(b << 4 | value as! Element)
+                buffer = nil
+            } else {
+                buffer = value
+            }
+        }
+        if let b = buffer{
+            self.append(b as! Element)
         }
         }
     }
     }
 }
 }

+ 0 - 50
Sources/CryptoSwift/String+Extension.swift

@@ -59,54 +59,4 @@ extension String {
         return try self.utf8.lazy.map({ $0 as UInt8 }).authenticate(with: authenticator).toHexString()
         return try self.utf8.lazy.map({ $0 as UInt8 }).authenticate(with: authenticator).toHexString()
     }
     }
     
     
-    
-    private static var unicodeHexMap:[UnicodeScalar:UInt8] = ["0":0,"1":1,"2":2,"3":3,"4":4,"5":5,"6":6,"7":7,"8":8,"9":9,"a":10,"b":11,"c":12,"d":13,"e":14,"f":15,"A":10,"B":11,"C":12,"D":13,"E":14,"F":15]
-    /**
-     Converts the a hexadecimal string to a corresponding array of bytes.
-     
-     So the string "ff00" would get converted to [255, 0].
-     Supports odd number of characters by interpreting the last character as its' hex value ("f" produces [16] as if it was "0f").
-     
-     - Returns: An array of 8 bit unsigned integers (bytes).
-     */
-    public func hexToBytes() -> Array<UInt8>{
-        var bytes:Array<UInt8> = []
-        do{
-            try self.streamHexBytes { (byte) in
-                bytes.append(byte)
-            }
-        }catch _{
-            return []
-        }
-        
-        return bytes
-    }
-    
-    // Allows str.hexToBytes() and Array<UInt8>(hex: str) to share the same code
-    // Old functionality returns a blank array upon encountering a non hex character so this method must throw in order to replicate that in methods relying on this method. (ex. "ffn" should return [] instead of [255])
-    // Changed NSError to custom Error enum
-    public func streamHexBytes(_ block:(UInt8)->Void) throws{
-        var buffer:UInt8?
-        var skip = hasPrefix("0x") ? 2 : 0
-        for char in unicodeScalars.lazy {
-            guard skip == 0 else {
-                skip -= 1
-                continue
-            }
-            guard let value = String.unicodeHexMap[char] else {
-                throw HexError.invalidCharacter
-            }
-            if let b = buffer {
-                block(b << 4 | value)
-                buffer = nil
-            } else {
-                buffer = value
-            }
-        }
-        if let b = buffer{block(b)}
-    }
-    
-    private enum HexError:Error {
-        case invalidCharacter
-    }
 }
 }

+ 0 - 10
Tests/CryptoSwiftTests/ExtensionsTest.swift

@@ -73,16 +73,6 @@ final class ExtensionsTest: XCTestCase {
         }
         }
     }
     }
     
     
-    func testhexToBytesPerformance(){
-        var str = "b1b2b3b3b3b3b3b3b1b2b3b3b3b3b3b3"
-        for _ in 0...9{
-            str += str
-        }
-        measure {
-            let _ = str.hexToBytes()
-        }
-        
-    }
 }
 }
 
 
 #if !CI
 #if !CI