mirror of
https://github.com/HaishinKit/HaishinKit.swift.git
synced 2026-05-07 20:12:28 +00:00
19 lines
493 B
Swift
19 lines
493 B
Swift
import Foundation
|
|
|
|
extension Data {
|
|
func chunk(_ size: Int) -> [Data] {
|
|
if count < size {
|
|
return [self]
|
|
}
|
|
var chunks: [Data] = []
|
|
let length = count
|
|
var offset = 0
|
|
repeat {
|
|
let thisChunkSize = ((length - offset) > size) ? size : (length - offset)
|
|
chunks.append(subdata(in: offset..<offset + thisChunkSize))
|
|
offset += thisChunkSize
|
|
} while offset < length
|
|
return chunks
|
|
}
|
|
}
|