BytesSequence.swift 610 B

123456789101112131415161718192021222324
  1. //
  2. // BytesSequence.swift
  3. // CryptoSwift
  4. //
  5. // Created by Marcin Krzyzanowski on 26/09/15.
  6. // Copyright © 2015 Marcin Krzyzanowski. All rights reserved.
  7. //
  8. struct BytesSequence: SequenceType {
  9. let chunkSize: Int
  10. let data: [UInt8]
  11. func generate() -> AnyGenerator<ArraySlice<UInt8>> {
  12. var offset:Int = 0
  13. return anyGenerator {
  14. let end = min(self.chunkSize, self.data.count - offset)
  15. let result = self.data[offset..<offset + end]
  16. offset += result.count
  17. return result.count > 0 ? result : nil
  18. }
  19. }
  20. }