// // ArrayExtension.swift // CryptoSwift // // Created by Marcin Krzyzanowski on 10/08/14. // Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved. // extension Array { init(reserveCapacity: Int) { self = Array() self.reserveCapacity(reserveCapacity) } } extension Array { /** split in chunks with given chunk size */ func chunks(size chunksize: Int) -> Array> { var words = Array>() words.reserveCapacity(self.count / chunksize) for idx in stride(from: chunksize, through: self.count, by: chunksize) { words.append(Array(self[idx - chunksize ..< idx])) // slow for large table } let reminder = self.suffix(self.count % chunksize) if !reminder.isEmpty { words.append(Array(reminder)) } return words } } extension Array where Element: Integer, Element.IntegerLiteralType == UInt8 { public init(hex: String) { self.init() let utf8 = Array(hex.utf8) let skip0x = hex.hasPrefix("0x") ? 2 : 0 for idx in stride(from: utf8.startIndex.advanced(by: skip0x), to: utf8.endIndex, by: utf8.startIndex.advanced(by: 2)) { let byteHex = "\(UnicodeScalar(utf8[idx]))\(UnicodeScalar(utf8[idx.advanced(by: 1)]))" if let byte = UInt8(byteHex, radix: 16) { self.append(byte as! Element) } } } }