Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .swift-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6.2.0
6.2.4
5 changes: 1 addition & 4 deletions Embedded/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ var pkgDependencies:[Package.Dependency] = [
.trait(name: "UnwrapSubtraction", condition: .when(traits: ["UnwrapSubtraction"])),
.trait(name: "UnwrapArithmetic", condition: .when(traits: ["UnwrapArithmetic"]))
]
),

// Variable-length arrays
.package(url: "https://github.com/RandomHashTags/swift-variablelengtharray", from: "0.2.0", traits: [])
)
]

#if os(Linux)
Expand Down
8 changes: 0 additions & 8 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,6 @@ var pkgDependencies:[Package.Dependency] = [
.trait(name: "UnwrapSubtraction", condition: .when(traits: ["UnwrapSubtraction"])),
.trait(name: "UnwrapArithmetic", condition: .when(traits: ["UnwrapArithmetic"]))
]
),

// Variable-length arrays
.package(
url: "https://github.com/RandomHashTags/swift-variablelengtharray",
from: "0.2.0",
traits: []
)
]

Expand Down Expand Up @@ -412,7 +405,6 @@ var targets = [
.product(name: "Logging", package: "swift-log", condition: .when(traits: ["Logging"])),
.product(name: "MediaTypes", package: "swift-media-types", condition: .when(traits: ["MediaTypes"])),
.product(name: "UnwrapArithmeticOperators", package: "swift-unwrap-arithmetic-operators"),
.product(name: "VariableLengthArray", package: "swift-variablelengtharray"),
.product(name: "SwiftCompressionUtilities", package: "swift-compression", condition: .when(traits: ["Compression"]))
]
),
Expand Down
66 changes: 30 additions & 36 deletions Sources/Destiny/extensions/InlineArrayExtensions.swift
Original file line number Diff line number Diff line change
@@ -1,33 +1,5 @@

import UnwrapArithmeticOperators
import VariableLengthArray

// MARK: VLArray
extension VLArray where Element == UInt8 {
/// - Returns: A case-literal `String` initialized from `storage`.
/// - Warning: The returned `String` is the exact size of this `VLArray`! This function doesn't look for a null-terminator!
public func unsafeString() -> String {
return String.init(unsafeUninitializedCapacity: storage.count, initializingUTF8With: {
return $0.initialize(from: storage).index
})
}

/// Efficiently initializes a `String` from `storage`.
///
/// - Returns: A case-literal `String` initialized from `storage` with start and end indexes.
/// - Warning: `endIndex` MUST be greater than `startIndex`.
public func unsafeString(startIndex: Int, endIndex: Int) -> String {
let count = endIndex -! startIndex
let slice = storage[startIndex..<endIndex]
return String.init(unsafeUninitializedCapacity: count, initializingUTF8With: {
return $0.initialize(from: slice).index
})
}
}





// MARK: InlineArray

Expand Down Expand Up @@ -72,27 +44,49 @@ extension InlineArray where Element == UInt8 {
/// - Returns: A case-literal `String` initialized from `span`.
/// - Warning: The returned `String` is the exact size of this `InlineArray`! This function doesn't look for a null-terminator!
public func unsafeString() -> String {
return self.span.withUnsafeBufferPointer { pointer in
return span.unsafeString()
}

/// Efficiently initializes a `String` from `span`.
///
/// - Returns: A case-literal `String` initialized from `span` with start and end indexes.
/// - Warning: `endIndex` MUST be greater than `startIndex`.
public func unsafeString(startIndex: Int, endIndex: Int) -> String {
return span.unsafeString(startIndex: startIndex, endIndex: endIndex)
}
}

// MARK: HTTPSocketWritable
extension InlineArray: HTTPSocketWritable {}




// MARK: Span<UInt8>
extension Span<UInt8> {
/// Efficiently initializes a `String` from this span.
///
/// - Returns: A case-literal `String`.
/// - Warning: The returned `String` is the exact size of this `Span`! This function doesn't look for a null-terminator!
public func unsafeString() -> String {
return self.withUnsafeBufferPointer { pointer in
return String.init(unsafeUninitializedCapacity: pointer.count, initializingUTF8With: {
return $0.initialize(from: pointer).index
})
}
}

/// Efficiently initializes a `String` from `span`.
/// Efficiently initializes a `String` from this span.
///
/// - Returns: A case-literal `String` initialized from `span` with start and end indexes.
/// - Returns: A case-literal `String` initialized from this span with start and end indexes.
/// - Warning: `endIndex` MUST be greater than `startIndex`.
public func unsafeString(startIndex: Int, endIndex: Int) -> String {
return self.span.withUnsafeBufferPointer {
return self.withUnsafeBufferPointer {
let count = endIndex -! startIndex
let slice = $0[startIndex..<endIndex]
return String.init(unsafeUninitializedCapacity: count, initializingUTF8With: {
return $0.initialize(from: slice).index
})
}
}
}

// MARK: HTTPSocketWritable
extension InlineArray: HTTPSocketWritable {}
}
8 changes: 0 additions & 8 deletions Sources/Destiny/extensions/StringExtensions.swift

This file was deleted.

17 changes: 8 additions & 9 deletions Sources/Destiny/request/AbstractHTTPRequest+_Storage.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

import UnwrapArithmeticOperators
import VariableLengthArray

extension AbstractHTTPRequest {
/// Underlying storage for the default request implementation.
Expand Down Expand Up @@ -147,7 +146,7 @@ extension AbstractHTTPRequest._Storage {
return methodString
}
requestLine!.method(buffer: buffer.buffer) {
methodString = $0.unsafeString()
methodString = $0
}
return methodString!
}
Expand All @@ -160,21 +159,21 @@ extension AbstractHTTPRequest._Storage {
if let path {
return path
}
requestLine!.path(buffer: buffer.buffer, {
requestLine!.path(buffer: buffer.buffer.span) { span in
path = [String]()
var startIndex = 0
for i in $0.indices {
if $0.storage[i] == .forwardSlash {
for i in span.indices {
if span[i] == .forwardSlash {
if startIndex < i {
path!.append($0.unsafeString(startIndex: startIndex, endIndex: i))
path!.append(span.unsafeString(startIndex: startIndex, endIndex: i))
}
startIndex = i +! 1
}
}
if startIndex < $0.count {
path!.append($0.unsafeString(startIndex: startIndex, endIndex: $0.count))
if startIndex < span.count {
path!.append(span.unsafeString(startIndex: startIndex, endIndex: span.count))
}
})
}
return path!
}
}
Expand Down
31 changes: 7 additions & 24 deletions Sources/Destiny/request/HTTPRequestLine.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

import UnwrapArithmeticOperators
import VariableLengthArray

/// Default HTTP Request Line implementation that includes the request method, target and HTTP version.
public struct HTTPRequestLine: Sendable, ~Copyable {
Expand Down Expand Up @@ -38,35 +37,19 @@ public struct HTTPRequestLine: Sendable, ~Copyable {
endIndex -! 9
}

public func path<let count: Int>(
buffer: InlineArray<count, UInt8>,
_ closure: (consuming VLArray<UInt8>) -> Void
public func path(
buffer: Span<UInt8>,
_ closure: (Span<UInt8>) -> Void
) {
let pathCount = pathCount
withUnsafeTemporaryAllocation(of: UInt8.self, capacity: pathCount, { pathBuffer in
var offset = methodEndIndex +! 1
if pathCount <= 128 {
for i in 0..<pathCount {
pathBuffer[i] = buffer[unchecked: offset]
offset +=! 1
}
} else {
buffer.span.withUnsafeBufferPointer {
copyMemory(pathBuffer.baseAddress!, $0.baseAddress! + offset, pathCount)
}
}
let pathArray = VLArray<UInt8>(_storage: pathBuffer)
closure(pathArray)
})
let offset = methodEndIndex +! 1
closure(buffer.extracting(unchecked: offset..<offset + pathCount))
}

public func method<let count: Int>(
buffer: InlineArray<count, UInt8>,
_ closure: (consuming VLArray<UInt8>) -> Void
_ closure: (String) -> Void
) {
VLArray.create(amount: methodEndIndex, initialize: {
buffer[unchecked: $0]
}, closure)
closure(buffer.unsafeString(startIndex: 0, endIndex: methodEndIndex))
}

public func simd<let count: Int>(
Expand Down
10 changes: 4 additions & 6 deletions Sources/Destiny/routes/DynamicResponse.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@

import VariableLengthArray

#if hasFeature(Embedded) || EMBEDDED

/// Default Dynamic Response implementation that builds an HTTP Message for dynamic requests.
Expand Down Expand Up @@ -54,12 +52,12 @@ extension DynamicResponse {
parameters[index]
}

public mutating func setParameter(at index: Int, value: consuming VLArray<UInt8>) {
parameters[index] = value.unsafeString()
public mutating func setParameter(at index: Int, value: String) {
parameters[index] = value
}

public mutating func appendParameter(value: consuming VLArray<UInt8>) {
parameters.append(value.unsafeString())
public mutating func appendParameter(value: String) {
parameters.append(value)
}

public func yieldParameters(_ yield: (String) -> Void) {
Expand Down
6 changes: 2 additions & 4 deletions Sources/Destiny/routes/DynamicResponseProtocol.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@

import VariableLengthArray

/// Core protocol that builds a HTTP Message for dynamic routes before sending it to the client.
public protocol DynamicResponseProtocol: HTTPSocketWritable, ~Copyable {
/// - Parameters:
Expand All @@ -10,10 +8,10 @@ public protocol DynamicResponseProtocol: HTTPSocketWritable, ~Copyable {

mutating func setParameter(
at index: Int,
value: consuming VLArray<UInt8>
value: String
)

mutating func appendParameter(value: consuming VLArray<UInt8>)
mutating func appendParameter(value: String)

func yieldParameters(_ yield: (String) -> Void)

Expand Down
14 changes: 5 additions & 9 deletions Sources/DestinyMacros/router/CompiledRouterStorage+Build.swift
Original file line number Diff line number Diff line change
Expand Up @@ -618,19 +618,15 @@ extension CompiledRouterStorage {
err = error
return
}
pathAtIndex.inlineVLArray {
response.setParameter(at: index, value: $0)
}
response.setParameter(at: index, value: pathAtIndex)
if responder.pathComponent(at: parameterIndex) == .catchall {
do throws(DestinyError) {
var i = parameterIndex+1
try request.forEachPath(offset: i) { path in
path.inlineVLArray {
if i < maximumParameters {
response.setParameter(at: i, value: $0)
} else {
response.appendParameter(value: $0)
}
if i < maximumParameters {
response.setParameter(at: i, value: path)
} else {
response.appendParameter(value: path)
}
i += 1
}
Expand Down