BytesSequence.swift 955 B

123456789101112131415161718192021222324252627282930313233
  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. //TODO: func anyGenerator is renamed to AnyGenerator in Swift 2.2, until then it's just dirty hack for linux (because swift >= 2.2 is available for Linux)
  9. private func CS_AnyGenerator<Element>(body: () -> Element?) -> AnyGenerator<Element> {
  10. #if os(Linux)
  11. return AnyGenerator(body: body)
  12. #else
  13. return anyGenerator(body)
  14. #endif
  15. }
  16. struct BytesSequence: SequenceType {
  17. let chunkSize: Int
  18. let data: [UInt8]
  19. func generate() -> AnyGenerator<ArraySlice<UInt8>> {
  20. var offset:Int = 0
  21. return CS_AnyGenerator {
  22. let end = min(self.chunkSize, self.data.count - offset)
  23. let result = self.data[offset..<offset + end]
  24. offset += result.count
  25. return result.count > 0 ? result : nil
  26. }
  27. }
  28. }