Skip to content

Commit

Permalink
Add Buffer.concat static functions
Browse files Browse the repository at this point in the history
Could the writeBytes be more efficient if Collection's would be used?
Or even more specific types?
  • Loading branch information
helje5 committed Dec 12, 2024
1 parent 27c5fb5 commit 84292e7
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion Sources/MacroCore/Buffer/Buffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public struct Buffer: Codable, Hashable, Sendable {

@inlinable
public mutating func append<S>(contentsOf sequence: S)
where S : Sequence, S.Element == UInt8
where S : Sequence, S.Element == UInt8
{
byteBuffer.writeBytes(sequence)
}
Expand Down Expand Up @@ -281,6 +281,42 @@ public extension Buffer {
}
}

public extension Buffer {

@inlinable
static func concat<S>(_ buffers: S...) -> Buffer
where S: Sequence, S.Element == UInt8
{
return concat(buffers)
}

@inlinable
static func concat<S>(_ buffers: S...) -> Buffer
where S: Collection, S.Element == UInt8
{
return concat(buffers)
}

@inlinable
static func concat<S>(_ buffers: S) -> Buffer
where S: Sequence, S.Element: Sequence, S.Element.Element == UInt8
{
var buffer = Buffer()
for sub in buffers {
buffer.append(contentsOf: sub)
}
return buffer
}
@inlinable
static func concat<S>(_ buffers: S) -> Buffer
where S: Collection, S.Element: Collection, S.Element.Element == UInt8
{
let size = buffers.reduce(0) { $0 + $1.count }
var buffer = Buffer(capacity: size)
for sub in buffers { buffer.append(contentsOf: sub)}
return buffer
}
}

extension Buffer: Collection {

Expand Down

0 comments on commit 84292e7

Please sign in to comment.