Created
October 17, 2018 14:06
-
-
Save ladislas/f75015b0dc99285ebc4499a79519bc89 to your computer and use it in GitHub Desktop.
Swift Buffer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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