From 885b94bd138fd7bb94127598f18d907dbba5c402 Mon Sep 17 00:00:00 2001 From: Guoye Zhang Date: Thu, 6 Jul 2023 22:47:21 -0700 Subject: [PATCH 1/2] Reduce Swift version requirement to 5.7 --- .swiftformat | 2 +- Package.swift | 2 +- Sources/HTTPTypes/ISOLatin1String.swift | 4 ++++ Sources/HTTPTypesFoundation/HTTPRequest+URL.swift | 12 +++++++++--- .../HTTPTypesFoundation/HTTPTypes+ISOLatin1.swift | 8 ++++++++ 5 files changed, 23 insertions(+), 5 deletions(-) diff --git a/.swiftformat b/.swiftformat index bf31db9..d1f35ac 100644 --- a/.swiftformat +++ b/.swiftformat @@ -1,4 +1,4 @@ ---swiftversion 5.8 +--swiftversion 5.7 # file options diff --git a/Package.swift b/Package.swift index f5f8403..ebe6d92 100644 --- a/Package.swift +++ b/Package.swift @@ -1,4 +1,4 @@ -// swift-tools-version: 5.8 +// swift-tools-version: 5.7 import PackageDescription diff --git a/Sources/HTTPTypes/ISOLatin1String.swift b/Sources/HTTPTypes/ISOLatin1String.swift index 5f501a4..506f145 100644 --- a/Sources/HTTPTypes/ISOLatin1String.swift +++ b/Sources/HTTPTypes/ISOLatin1String.swift @@ -32,7 +32,11 @@ struct ISOLatin1String: Sendable, Hashable { try withUnsafeTemporaryAllocation(of: UInt8.self, capacity: self._storage.unicodeScalars.count) { buffer in for (index, scalar) in self._storage.unicodeScalars.enumerated() { assert(scalar.value <= UInt8.max) + #if swift(>=5.8) buffer.initializeElement(at: index, to: UInt8(truncatingIfNeeded: scalar.value)) + #else + buffer[index] = UInt8(truncatingIfNeeded: scalar.value) + #endif } return try body(UnsafeBufferPointer(buffer)) } diff --git a/Sources/HTTPTypesFoundation/HTTPRequest+URL.swift b/Sources/HTTPTypesFoundation/HTTPRequest+URL.swift index 7e4eca2..1513a5c 100644 --- a/Sources/HTTPTypesFoundation/HTTPRequest+URL.swift +++ b/Sources/HTTPTypesFoundation/HTTPRequest+URL.swift @@ -95,12 +95,12 @@ extension URL { return CFRange(location: a.location, length: b.location + b.length - a.location) } - func bufferSlice(_ range: CFRange) -> some Collection { - buffer[range.location ..< range.location + range.length] + func bufferSlice(_ range: CFRange) -> UnsafeMutableBufferPointer { + UnsafeMutableBufferPointer(rebasing: buffer[range.location ..< range.location + range.length]) } let schemeRange = CFURLGetByteRangeForComponent(url, .scheme, nil) - precondition(schemeRange.location != kCFNotFound, "") + precondition(schemeRange.location != kCFNotFound, "Schemeless URL is not supported") let scheme = Array(bufferSlice(schemeRange)) let authority: [UInt8]? @@ -123,8 +123,14 @@ extension URL { } else { let pathBuffer = bufferSlice(requestPathRange) path = [UInt8](unsafeUninitializedCapacity: pathBuffer.count + 1) { buffer, initializedCount in + #if swift(>=5.8) buffer.initializeElement(at: 0, to: 0x2F) let endIndex = buffer[1...].initialize(fromContentsOf: pathBuffer) + #else + buffer[0] = 0x2F + memcpy(buffer.baseAddress!.advanced(by: 1), pathBuffer.baseAddress!, pathBuffer.count) + let endIndex = buffer.startIndex.advanced(by: pathBuffer.count + 1) + #endif initializedCount = buffer.distance(from: buffer.startIndex, to: endIndex) } } diff --git a/Sources/HTTPTypesFoundation/HTTPTypes+ISOLatin1.swift b/Sources/HTTPTypesFoundation/HTTPTypes+ISOLatin1.swift index b2063cf..e5605ec 100644 --- a/Sources/HTTPTypesFoundation/HTTPTypes+ISOLatin1.swift +++ b/Sources/HTTPTypesFoundation/HTTPTypes+ISOLatin1.swift @@ -28,9 +28,17 @@ extension HTTPField { self = withUnsafeTemporaryAllocation(of: UInt8.self, capacity: isoLatin1Value.unicodeScalars.count) { buffer in for (index, scalar) in isoLatin1Value.unicodeScalars.enumerated() { if scalar.value > UInt8.max { + #if swift(>=5.8) buffer.initializeElement(at: index, to: 0x20) + #else + buffer[index] = 0x20 + #endif } else { + #if swift(>=5.8) buffer.initializeElement(at: index, to: UInt8(truncatingIfNeeded: scalar.value)) + #else + buffer[index] = UInt8(truncatingIfNeeded: scalar.value) + #endif } } return HTTPField(name: name, value: buffer) From 9ff7d8f4d651fe9a606a83139592a48524e78317 Mon Sep 17 00:00:00 2001 From: Guoye Zhang Date: Sun, 9 Jul 2023 03:39:17 -0700 Subject: [PATCH 2/2] Use fallback functions --- Sources/HTTPTypes/ISOLatin1String.swift | 4 ---- Sources/HTTPTypesFoundation/HTTPRequest+URL.swift | 10 ++-------- Sources/HTTPTypesFoundation/HTTPTypes+ISOLatin1.swift | 8 -------- 3 files changed, 2 insertions(+), 20 deletions(-) diff --git a/Sources/HTTPTypes/ISOLatin1String.swift b/Sources/HTTPTypes/ISOLatin1String.swift index 506f145..d3b566e 100644 --- a/Sources/HTTPTypes/ISOLatin1String.swift +++ b/Sources/HTTPTypes/ISOLatin1String.swift @@ -32,11 +32,7 @@ struct ISOLatin1String: Sendable, Hashable { try withUnsafeTemporaryAllocation(of: UInt8.self, capacity: self._storage.unicodeScalars.count) { buffer in for (index, scalar) in self._storage.unicodeScalars.enumerated() { assert(scalar.value <= UInt8.max) - #if swift(>=5.8) - buffer.initializeElement(at: index, to: UInt8(truncatingIfNeeded: scalar.value)) - #else buffer[index] = UInt8(truncatingIfNeeded: scalar.value) - #endif } return try body(UnsafeBufferPointer(buffer)) } diff --git a/Sources/HTTPTypesFoundation/HTTPRequest+URL.swift b/Sources/HTTPTypesFoundation/HTTPRequest+URL.swift index 1513a5c..db93678 100644 --- a/Sources/HTTPTypesFoundation/HTTPRequest+URL.swift +++ b/Sources/HTTPTypesFoundation/HTTPRequest+URL.swift @@ -123,15 +123,9 @@ extension URL { } else { let pathBuffer = bufferSlice(requestPathRange) path = [UInt8](unsafeUninitializedCapacity: pathBuffer.count + 1) { buffer, initializedCount in - #if swift(>=5.8) - buffer.initializeElement(at: 0, to: 0x2F) - let endIndex = buffer[1...].initialize(fromContentsOf: pathBuffer) - #else buffer[0] = 0x2F - memcpy(buffer.baseAddress!.advanced(by: 1), pathBuffer.baseAddress!, pathBuffer.count) - let endIndex = buffer.startIndex.advanced(by: pathBuffer.count + 1) - #endif - initializedCount = buffer.distance(from: buffer.startIndex, to: endIndex) + UnsafeMutableRawBufferPointer(UnsafeMutableBufferPointer(rebasing: buffer[1...])).copyMemory(from: UnsafeRawBufferPointer(pathBuffer)) + initializedCount = pathBuffer.count + 1 } } } else { diff --git a/Sources/HTTPTypesFoundation/HTTPTypes+ISOLatin1.swift b/Sources/HTTPTypesFoundation/HTTPTypes+ISOLatin1.swift index e5605ec..19708cf 100644 --- a/Sources/HTTPTypesFoundation/HTTPTypes+ISOLatin1.swift +++ b/Sources/HTTPTypesFoundation/HTTPTypes+ISOLatin1.swift @@ -28,17 +28,9 @@ extension HTTPField { self = withUnsafeTemporaryAllocation(of: UInt8.self, capacity: isoLatin1Value.unicodeScalars.count) { buffer in for (index, scalar) in isoLatin1Value.unicodeScalars.enumerated() { if scalar.value > UInt8.max { - #if swift(>=5.8) - buffer.initializeElement(at: index, to: 0x20) - #else buffer[index] = 0x20 - #endif } else { - #if swift(>=5.8) - buffer.initializeElement(at: index, to: UInt8(truncatingIfNeeded: scalar.value)) - #else buffer[index] = UInt8(truncatingIfNeeded: scalar.value) - #endif } } return HTTPField(name: name, value: buffer)