| 12345678910111213141516171819202122232425262728293031323334353637383940 |
- //
- // ArrayExtension.swift
- // CryptoSwift
- //
- // Created by Marcin Krzyzanowski on 10/08/14.
- // Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
- //
- extension Array {
- /** split in chunks with given chunk size */
- func chunks(size chunksize: Int) -> Array<Array<Element>> {
- var words = Array<Array<Element>>()
- 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<Element.IntegerLiteralType>(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)
- }
- }
- }
- }
|