Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Swift Buffer
//
// Buffer.swift
// SwiftSerialport
//
// Created by Ladislas de Toldi on 09/10/2018.
//
import Foundation
public class Buffer {
var buffer: Data
let length: Int
public var data: Data {
return buffer
}
init(length: Int) {
self.length = length
self.buffer = Data()
}
@discardableResult
func append(data: Data) -> Int {
buffer.append(data)
if buffer.count > length {
buffer.removeSubrange(0...length - 1)
}
return data.count
}
@discardableResult
func append(value: UInt8) -> Int {
buffer.append(value)
if buffer.count > length {
buffer.removeSubrange(0...length - 1)
}
return 1
}
@discardableResult
func append(array: [UInt8]) -> Int {
buffer.append(Data(array))
if buffer.count > length {
buffer.removeSubrange(0...length - 1)
}
return array.count
}
func clear() {
self.buffer = Data()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment