Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
helje5 committed Dec 14, 2024
2 parents 9022878 + b75c788 commit 447330b
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 76 deletions.
106 changes: 79 additions & 27 deletions Sources/MacroCore/Buffer/Buffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Macro
//
// Created by Helge Heß.
// Copyright © 2020-2021 ZeeZide GmbH. All rights reserved.
// Copyright © 2020-2024 ZeeZide GmbH. All rights reserved.
//

import struct NIO.ByteBuffer
Expand All @@ -16,40 +16,44 @@ import struct NIO.ByteBufferAllocator
* Node Buffer API: https://nodejs.org/api/buffer.html
*
* Creating buffers:
*
* Buffer.from("Hello") // UTF-8 data
* Buffer() // empty
* Buffer([ 42, 13, 10 ])
* ```swift
* Buffer.from("Hello") // UTF-8 data
* Buffer() // empty
* Buffer([ 42, 13, 10 ])
* ```
*
* Reading:
* ```swift
* buffer.count
* buffer.isEmpty
* let byte = buffer[10]
*
* buffer.count
* buffer.isEmpty
* let byte = buffer[10]
*
* let slice = buffer.consumeFirst(10)
* let slice = buffer.slice(5)
* let slice = buffer.slice(5, 7)
* let slice = buffer.consumeFirst(10)
* let slice = buffer.slice(5)
* let slice = buffer.slice(5, 7)
*
* let idx = buffer.indexOf(42) // -1 on not found
* let idx = buffer.indexOf([ 13, 10 ])
* let idx = buffer.indexOf(42) // -1 on not found
* let idx = buffer.indexOf([ 13, 10 ])
* ```
*
* Writing:
*
* buffer.append(otherBuffer)
* buffer.append([ 42, 10 ])
* ```swift
* buffer.append(otherBuffer)
* buffer.append([ 42, 10 ])
* ```
*
* Converting to strings:
*
* try buffer.toString()
* try buffer.toString("hex")
* try buffer.toString("base64")
* buffer.hexEncodedString()
*
* ```swift
* try buffer.toString()
* try buffer.toString("hex")
* try buffer.toString("base64")
* buffer.hexEncodedString()
* ```
*/
public struct Buffer: Codable, Hashable {
public struct Buffer: Codable, Hashable, Sendable {

public typealias Index = Int
public typealias Index = Int
public typealias Element = UInt8

public var byteBuffer : ByteBuffer

Expand Down Expand Up @@ -82,7 +86,7 @@ public struct Buffer: Codable, Hashable {

@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 @@ -139,7 +143,7 @@ public struct Buffer: Codable, Hashable {
let end = endIndex ?? count
let startOffset = startIndex >= 0 ? startIndex : (count + startIndex)
let endOffset = end >= 0 ? end : (count + end)
let length = max(0, endOffset - startOffset)
let length = Swift.max(0, endOffset - startOffset)
let startIndex = byteBuffer.readerIndex + startOffset
assert(length >= 0, "invalid index parameters to `slice`")
if length < 1 { return Buffer(MacroCore.shared.emptyByteBuffer) }
Expand Down Expand Up @@ -277,6 +281,54 @@ 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 {

@inlinable
public func index(after i: Int) -> Int { return i + 1 }

@inlinable
public var startIndex : Int { return 0 }
@inlinable
public var endIndex : Int { return count }
}

extension Buffer: CustomStringConvertible {

@inlinable
Expand Down
25 changes: 21 additions & 4 deletions Sources/MacroCore/Buffer/BufferData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
// Macro
//
// Created by Helge Heß.
// Copyright © 2020-2023 ZeeZide GmbH. All rights reserved.
// Copyright © 2020-2024 ZeeZide GmbH. All rights reserved.
//

#if canImport(Foundation)

import struct Foundation.Data
import struct Foundation.Data
import protocol Foundation.ContiguousBytes
import NIOCore
import NIOFoundationCompat

Expand All @@ -17,16 +18,32 @@ public extension Buffer {
/**
* Initialize the Buffer with the contents of the given `Data`. Copies the
* bytes.
*
* - Parameters:
* - data: The bytes to copy into the buffer.
*/
@inlinable init(_ data: Data) {
@inlinable
init(_ data: Data) {
self.init(capacity: data.count)
byteBuffer.writeBytes(data)
}

@inlinable var data : Data {
@inlinable
var data : Data {
return byteBuffer.getData(at : byteBuffer.readerIndex,
length : byteBuffer.readableBytes) ?? Data()
}
}

extension Buffer: ContiguousBytes {

@inlinable
public func withUnsafeBytes<R>(_ body: (UnsafeRawBufferPointer) throws -> R)
rethrows -> R
{
return try byteBuffer.readableBytesView.withUnsafeBytes(body)
}

}

#endif // canImport(Foundation)
42 changes: 22 additions & 20 deletions Sources/MacroCore/Buffer/BufferHexEncoding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Macro
//
// Created by Helge Heß.
// Copyright © 2020-2023 ZeeZide GmbH. All rights reserved.
// Copyright © 2020-2024 ZeeZide GmbH. All rights reserved.
//

import NIOCore
Expand All @@ -19,19 +19,21 @@ public extension Buffer {
* Returns the data in the buffer as a hex encoded string.
*
* Example:
*
* let buffer = Buffer("Hello".utf8)
* let string = buffer.hexEncodedString()
* // "48656c6c6f"
* ```swift
* let buffer = Buffer("Hello".utf8)
* let string = buffer.hexEncodedString()
* // "48656c6c6f"
* ```
*
* Each byte is represented by two hex digits, e.g. `6c` in the example.
*
* `hex` is also recognized as a string encoding, this works as well:
* ```swift
* let buffer = Buffer("Hello".utf8)
* let string = try buffer.toString("hex")
* // "48656c6c6f"
* ```
*
* let buffer = Buffer("Hello".utf8)
* let string = try buffer.toString("hex")
* // "48656c6c6f"
*
* - Parameters:
* - uppercase: If true, the a-f hexdigits are generated in
* uppercase (ABCDEF). Defaults to false.
Expand Down Expand Up @@ -67,18 +69,18 @@ public extension Buffer {
* Appends the bytes represented by a hex encoded string to the Buffer.
*
* Example:
*
* let buffer = Buffer()
* buffer.writeHexString("48656c6c6f")
* let string = try buffer.toString()
* // "Hello"
*
* ```swift
* let buffer = Buffer()
* buffer.writeHexString("48656c6c6f")
* let string = try buffer.toString()
* // "Hello"
* ```
* `hex` is also recognized as a string encoding, this works as well:
*
* let buffer = try Buffer.from("48656c6c6f", "hex")
* let string = try buffer.toString()
* // "Hello"
*
* ```swift
* let buffer = try Buffer.from("48656c6c6f", "hex")
* let string = try buffer.toString()
* // "Hello"
* ```
* - Parameters:
* - hexString: A hex encoded string, no spaces etc allowed between the
* bytes.
Expand Down
Loading

0 comments on commit 447330b

Please sign in to comment.