From f610691a14c5f3200f19beda0eb343ad745f6e0a Mon Sep 17 00:00:00 2001 From: Guoye Zhang Date: Wed, 12 Jul 2023 16:44:56 -0700 Subject: [PATCH 1/2] Code formatting fixes Adding additional self. missed by SwiftFormat --- .swiftformat | 1 + Sources/HTTPTypes/HTTPFieldName.swift | 4 +- Sources/HTTPTypes/HTTPFields.swift | 46 +++++++++---------- Sources/HTTPTypes/ISOLatin1String.swift | 2 +- .../HTTPTypesFoundation/HTTPRequest+URL.swift | 20 ++++---- .../HTTPTypes+ISOLatin1.swift | 8 ++-- .../URLRequest+HTTPTypes.swift | 4 +- .../URLResponse+HTTPTypes.swift | 6 +-- .../URLSession+HTTPTypes.swift | 17 +++---- 9 files changed, 55 insertions(+), 53 deletions(-) diff --git a/.swiftformat b/.swiftformat index d1f35ac..aceb57f 100644 --- a/.swiftformat +++ b/.swiftformat @@ -16,5 +16,6 @@ # rules +--disable andOperator --disable blankLinesAroundMark --disable wrapMultilineStatementBraces diff --git a/Sources/HTTPTypes/HTTPFieldName.swift b/Sources/HTTPTypes/HTTPFieldName.swift index 62a24dd..a20b5ac 100644 --- a/Sources/HTTPTypes/HTTPFieldName.swift +++ b/Sources/HTTPTypes/HTTPFieldName.swift @@ -57,7 +57,7 @@ extension HTTPField { extension HTTPField.Name: Hashable { public func hash(into hasher: inout Hasher) { - hasher.combine(canonicalName) + hasher.combine(self.canonicalName) } public static func == (lhs: HTTPField.Name, rhs: HTTPField.Name) -> Bool { @@ -67,7 +67,7 @@ extension HTTPField.Name: Hashable { extension HTTPField.Name: LosslessStringConvertible { public var description: String { - rawName + self.rawName } } diff --git a/Sources/HTTPTypes/HTTPFields.swift b/Sources/HTTPTypes/HTTPFields.swift index 1a4ff36..d538ecc 100644 --- a/Sources/HTTPTypes/HTTPFields.swift +++ b/Sources/HTTPTypes/HTTPFields.swift @@ -40,28 +40,28 @@ public struct HTTPFields: Sendable, Hashable { #if canImport(os.lock) self.lock.initialize(to: os_unfair_lock()) #else - let err = pthread_mutex_init(lock, nil) + let err = pthread_mutex_init(self.lock, nil) precondition(err == 0, "pthread_mutex_init failed with error \(err)") #endif } deinit { #if !canImport(os.lock) - let err = pthread_mutex_destroy(lock) + let err = pthread_mutex_destroy(self.lock) precondition(err == 0, "pthread_mutex_destroy failed with error \(err)") #endif - lock.deallocate() + self.lock.deallocate() } var ensureIndex: [String: UInt16] { #if canImport(os.lock) os_unfair_lock_lock(self.lock) - defer { os_unfair_lock_unlock(lock) } + defer { os_unfair_lock_unlock(self.lock) } #else - let err = pthread_mutex_lock(lock) + let err = pthread_mutex_lock(self.lock) precondition(err == 0, "pthread_mutex_lock failed with error \(err)") defer { - let err = pthread_mutex_unlock(lock) + let err = pthread_mutex_unlock(self.lock) precondition(err == 0, "pthread_mutex_unlock failed with error \(err)") } #endif @@ -69,12 +69,12 @@ public struct HTTPFields: Sendable, Hashable { return index } var newIndex = [String: UInt16]() - for i in self.fields.indices.reversed() { - let name = self.fields[i].0.name.canonicalName - self.fields[i].1 = newIndex[name] ?? .max - newIndex[name] = UInt16(i) + for index in self.fields.indices.reversed() { + let name = self.fields[index].0.name.canonicalName + self.fields[index].1 = newIndex[name] ?? .max + newIndex[name] = UInt16(index) } - index = newIndex + self.index = newIndex return newIndex } @@ -85,7 +85,7 @@ public struct HTTPFields: Sendable, Hashable { os_unfair_lock_lock(self.lock) #else do { - let err = pthread_mutex_lock(lock) + let err = pthread_mutex_lock(self.lock) precondition(err == 0, "pthread_mutex_lock failed with error \(err)") } #endif @@ -94,7 +94,7 @@ public struct HTTPFields: Sendable, Hashable { os_unfair_lock_unlock(self.lock) #else do { - let err = pthread_mutex_unlock(lock) + let err = pthread_mutex_unlock(self.lock) precondition(err == 0, "pthread_mutex_unlock failed with error \(err)") } #endif @@ -113,7 +113,7 @@ public struct HTTPFields: Sendable, Hashable { func append(field: HTTPField) { let name = field.name.canonicalName - if var index = index?[name] { + if var index = self.index?[name] { while true { let next = self.fields[Int(index)].1 if next == .max { break } @@ -121,7 +121,7 @@ public struct HTTPFields: Sendable, Hashable { } self.fields[Int(index)].1 = UInt16(self.fields.count) } else { - index?[name] = UInt16(self.fields.count) + self.index?[name] = UInt16(self.fields.count) } self.fields.append((field, .max)) } @@ -291,12 +291,12 @@ extension HTTPFields: RangeReplaceableCollection, RandomAccessCollection, Mutabl } } - public mutating func reserveCapacity(_ n: Int) { + public mutating func reserveCapacity(_ capacity: Int) { if !isKnownUniquelyReferenced(&self._storage) { self._storage = self._storage.copy() } - self._storage.index?.reserveCapacity(n) - self._storage.fields.reserveCapacity(n) + self._storage.index?.reserveCapacity(capacity) + self._storage.fields.reserveCapacity(capacity) } } @@ -312,14 +312,14 @@ extension Array { var offset = 0 var iterator = removalIndices.makeIterator() var nextToRemoveOptional = iterator.next() - for i in indices { - while let nextToRemove = nextToRemoveOptional, index(i, offsetBy: offset) == nextToRemove { + for index in self.indices { + while let nextToRemove = nextToRemoveOptional, self.index(index, offsetBy: offset) == nextToRemove { offset += 1 nextToRemoveOptional = iterator.next() } - let toKeep = index(i, offsetBy: offset) - if toKeep < endIndex { - swapAt(i, toKeep) + let toKeep = self.index(index, offsetBy: offset) + if toKeep < self.endIndex { + self.swapAt(index, toKeep) } else { break } diff --git a/Sources/HTTPTypes/ISOLatin1String.swift b/Sources/HTTPTypes/ISOLatin1String.swift index d3b566e..696f6c2 100644 --- a/Sources/HTTPTypes/ISOLatin1String.swift +++ b/Sources/HTTPTypes/ISOLatin1String.swift @@ -14,7 +14,7 @@ extension String { var isASCII: Bool { - utf8.allSatisfy { $0 & 0x80 == 0 } + self.utf8.allSatisfy { $0 & 0x80 == 0 } } } diff --git a/Sources/HTTPTypesFoundation/HTTPRequest+URL.swift b/Sources/HTTPTypesFoundation/HTTPRequest+URL.swift index db93678..a7cfafb 100644 --- a/Sources/HTTPTypesFoundation/HTTPRequest+URL.swift +++ b/Sources/HTTPTypesFoundation/HTTPRequest+URL.swift @@ -21,9 +21,9 @@ extension HTTPRequest { /// fields. public var url: URL? { get { - if let schemeField = pseudoHeaderFields.scheme, - let authorityField = pseudoHeaderFields.authority, - let pathField = pseudoHeaderFields.path { + if let schemeField = self.pseudoHeaderFields.scheme, + let authorityField = self.pseudoHeaderFields.authority, + let pathField = self.pseudoHeaderFields.path { return schemeField.withUnsafeBytesOfValue { scheme in authorityField.withUnsafeBytesOfValue { authority in pathField.withUnsafeBytesOfValue { path in @@ -42,9 +42,9 @@ extension HTTPRequest { self.authority = authority.map { String(decoding: $0, as: UTF8.self) } self.path = String(decoding: path, as: UTF8.self) } else { - pseudoHeaderFields.scheme = nil - pseudoHeaderFields.authority = nil - pseudoHeaderFields.path = nil + self.pseudoHeaderFields.scheme = nil + self.pseudoHeaderFields.authority = nil + self.pseudoHeaderFields.path = nil } } } @@ -89,10 +89,10 @@ extension URL { return withUnsafeTemporaryAllocation(of: UInt8.self, capacity: length) { buffer in CFURLGetBytes(url, buffer.baseAddress, buffer.count) - func unionRange(_ a: CFRange, _ b: CFRange) -> CFRange { - if a.location == kCFNotFound { return b } - if b.location == kCFNotFound { return a } - return CFRange(location: a.location, length: b.location + b.length - a.location) + func unionRange(_ first: CFRange, _ second: CFRange) -> CFRange { + if first.location == kCFNotFound { return second } + if second.location == kCFNotFound { return first } + return CFRange(location: first.location, length: second.location + second.length - first.location) } func bufferSlice(_ range: CFRange) -> UnsafeMutableBufferPointer { diff --git a/Sources/HTTPTypesFoundation/HTTPTypes+ISOLatin1.swift b/Sources/HTTPTypesFoundation/HTTPTypes+ISOLatin1.swift index 19708cf..bcadf53 100644 --- a/Sources/HTTPTypesFoundation/HTTPTypes+ISOLatin1.swift +++ b/Sources/HTTPTypesFoundation/HTTPTypes+ISOLatin1.swift @@ -16,7 +16,7 @@ import HTTPTypes extension String { var isASCII: Bool { - utf8.allSatisfy { $0 & 0x80 == 0 } + self.utf8.allSatisfy { $0 & 0x80 == 0 } } } @@ -39,10 +39,10 @@ extension HTTPField { } var isoLatin1Value: String { - if value.isASCII { - return value + if self.value.isASCII { + return self.value } else { - return withUnsafeBytesOfValue { buffer in + return self.withUnsafeBytesOfValue { buffer in let scalars = buffer.lazy.map { UnicodeScalar(UInt32($0))! } var string = "" string.unicodeScalars.append(contentsOf: scalars) diff --git a/Sources/HTTPTypesFoundation/URLRequest+HTTPTypes.swift b/Sources/HTTPTypesFoundation/URLRequest+HTTPTypes.swift index 6daab74..05d781d 100644 --- a/Sources/HTTPTypesFoundation/URLRequest+HTTPTypes.swift +++ b/Sources/HTTPTypesFoundation/URLRequest+HTTPTypes.swift @@ -46,13 +46,13 @@ extension URLRequest { /// Convert the `URLRequest` into an `HTTPRequest`. public var httpRequest: HTTPRequest? { - guard let method = HTTPRequest.Method(httpMethod ?? "GET"), + guard let method = HTTPRequest.Method(self.httpMethod ?? "GET"), let url else { return nil } var request = HTTPRequest(method: method, url: url) - if let allHTTPHeaderFields { + if let allHTTPHeaderFields = self.allHTTPHeaderFields { request.headerFields.reserveCapacity(allHTTPHeaderFields.count) for (name, value) in allHTTPHeaderFields { if let name = HTTPField.Name(name) { diff --git a/Sources/HTTPTypesFoundation/URLResponse+HTTPTypes.swift b/Sources/HTTPTypesFoundation/URLResponse+HTTPTypes.swift index bc0ba6a..d605ca6 100644 --- a/Sources/HTTPTypesFoundation/URLResponse+HTTPTypes.swift +++ b/Sources/HTTPTypesFoundation/URLResponse+HTTPTypes.swift @@ -40,11 +40,11 @@ extension HTTPURLResponse { /// Convert the `HTTPURLResponse` into an `HTTPResponse`. public var httpResponse: HTTPResponse? { - guard (0 ... 999).contains(statusCode) else { + guard (0 ... 999).contains(self.statusCode) else { return nil } - var response = HTTPResponse(status: .init(code: statusCode)) - if let fields = allHeaderFields as? [String: String] { + var response = HTTPResponse(status: .init(code: self.statusCode)) + if let fields = self.allHeaderFields as? [String: String] { response.headerFields.reserveCapacity(fields.count) for (name, value) in fields { if let name = HTTPField.Name(name) { diff --git a/Sources/HTTPTypesFoundation/URLSession+HTTPTypes.swift b/Sources/HTTPTypesFoundation/URLSession+HTTPTypes.swift index 655944a..f83ca21 100644 --- a/Sources/HTTPTypesFoundation/URLSession+HTTPTypes.swift +++ b/Sources/HTTPTypesFoundation/URLSession+HTTPTypes.swift @@ -21,17 +21,17 @@ import FoundationNetworking extension URLSessionTask { /// The original HTTP request this task was created with. public var originalHTTPRequest: HTTPRequest? { - originalRequest?.httpRequest + self.originalRequest?.httpRequest } /// The current HTTP request -- may differ from the `originalHTTPRequest` due to HTTP redirection. public var currentHTTPRequest: HTTPRequest? { - currentRequest?.httpRequest + self.currentRequest?.httpRequest } /// The HTTP response received from the server. public var httpResponse: HTTPResponse? { - (response as? HTTPURLResponse)?.httpResponse + (self.response as? HTTPURLResponse)?.httpResponse } } @@ -41,6 +41,7 @@ private enum HTTPTypeConversionError: Error { } #if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) || os(visionOS) + @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *) extension URLSession { /// Convenience method to load data using an `HTTPRequest`; creates and resumes a `URLSessionDataTask` internally. @@ -52,7 +53,7 @@ extension URLSession { guard let urlRequest = URLRequest(httpRequest: request) else { throw HTTPTypeConversionError.failedToConvertHTTPRequestToURLRequest } - let (data, urlResponse) = try await data(for: urlRequest, delegate: delegate) + let (data, urlResponse) = try await self.data(for: urlRequest, delegate: delegate) guard let response = (urlResponse as? HTTPURLResponse)?.httpResponse else { throw HTTPTypeConversionError.failedToConvertURLResponseToHTTPResponse } @@ -69,7 +70,7 @@ extension URLSession { guard let urlRequest = URLRequest(httpRequest: request) else { throw HTTPTypeConversionError.failedToConvertHTTPRequestToURLRequest } - let (data, urlResponse) = try await upload(for: urlRequest, fromFile: fileURL, delegate: delegate) + let (data, urlResponse) = try await self.upload(for: urlRequest, fromFile: fileURL, delegate: delegate) guard let response = (urlResponse as? HTTPURLResponse)?.httpResponse else { throw HTTPTypeConversionError.failedToConvertURLResponseToHTTPResponse } @@ -86,7 +87,7 @@ extension URLSession { guard let urlRequest = URLRequest(httpRequest: request) else { throw HTTPTypeConversionError.failedToConvertHTTPRequestToURLRequest } - let (data, urlResponse) = try await upload(for: urlRequest, from: bodyData, delegate: delegate) + let (data, urlResponse) = try await self.upload(for: urlRequest, from: bodyData, delegate: delegate) guard let response = (urlResponse as? HTTPURLResponse)?.httpResponse else { throw HTTPTypeConversionError.failedToConvertURLResponseToHTTPResponse } @@ -102,7 +103,7 @@ extension URLSession { guard let urlRequest = URLRequest(httpRequest: request) else { throw HTTPTypeConversionError.failedToConvertHTTPRequestToURLRequest } - let (location, urlResponse) = try await download(for: urlRequest, delegate: delegate) + let (location, urlResponse) = try await self.download(for: urlRequest, delegate: delegate) guard let response = (urlResponse as? HTTPURLResponse)?.httpResponse else { throw HTTPTypeConversionError.failedToConvertURLResponseToHTTPResponse } @@ -118,7 +119,7 @@ extension URLSession { guard let urlRequest = URLRequest(httpRequest: request) else { throw HTTPTypeConversionError.failedToConvertHTTPRequestToURLRequest } - let (data, urlResponse) = try await bytes(for: urlRequest, delegate: delegate) + let (data, urlResponse) = try await self.bytes(for: urlRequest, delegate: delegate) guard let response = (urlResponse as? HTTPURLResponse)?.httpResponse else { throw HTTPTypeConversionError.failedToConvertURLResponseToHTTPResponse } From 79ea5d0bc564a39e95373130a30989a1fbc89a27 Mon Sep 17 00:00:00 2001 From: Guoye Zhang Date: Fri, 14 Jul 2023 16:03:05 -0700 Subject: [PATCH 2/2] Update format and contributors --- CONTRIBUTORS.txt | 2 ++ Sources/HTTPTypesFoundation/URLSession+HTTPTypes.swift | 6 +++--- Tests/HTTPTypesTests/HTTPTypesTests.swift | 6 +++--- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index 712576d..ff94868 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -12,6 +12,8 @@ needs to be listed here. ### Contributors - Guoye Zhang +- Sergey Dmitriev +- Tomohiro Kumagai **Updating this list** diff --git a/Sources/HTTPTypesFoundation/URLSession+HTTPTypes.swift b/Sources/HTTPTypesFoundation/URLSession+HTTPTypes.swift index 17a9dd2..0f9e6fb 100644 --- a/Sources/HTTPTypesFoundation/URLSession+HTTPTypes.swift +++ b/Sources/HTTPTypesFoundation/URLSession+HTTPTypes.swift @@ -137,7 +137,7 @@ extension URLSession { guard let urlRequest = URLRequest(httpRequest: request) else { throw HTTPTypeConversionError.failedToConvertHTTPRequestToURLRequest } - let (data, urlResponse) = try await data(for: urlRequest) + let (data, urlResponse) = try await self.data(for: urlRequest) guard let response = (urlResponse as? HTTPURLResponse)?.httpResponse else { throw HTTPTypeConversionError.failedToConvertURLResponseToHTTPResponse } @@ -153,7 +153,7 @@ extension URLSession { guard let urlRequest = URLRequest(httpRequest: request) else { throw HTTPTypeConversionError.failedToConvertHTTPRequestToURLRequest } - let (data, urlResponse) = try await upload(for: urlRequest, fromFile: fileURL) + let (data, urlResponse) = try await self.upload(for: urlRequest, fromFile: fileURL) guard let response = (urlResponse as? HTTPURLResponse)?.httpResponse else { throw HTTPTypeConversionError.failedToConvertURLResponseToHTTPResponse } @@ -169,7 +169,7 @@ extension URLSession { guard let urlRequest = URLRequest(httpRequest: request) else { throw HTTPTypeConversionError.failedToConvertHTTPRequestToURLRequest } - let (data, urlResponse) = try await upload(for: urlRequest, from: bodyData) + let (data, urlResponse) = try await self.upload(for: urlRequest, from: bodyData) guard let response = (urlResponse as? HTTPURLResponse)?.httpResponse else { throw HTTPTypeConversionError.failedToConvertURLResponseToHTTPResponse } diff --git a/Tests/HTTPTypesTests/HTTPTypesTests.swift b/Tests/HTTPTypesTests/HTTPTypesTests.swift index 05caffa..ff3ed79 100644 --- a/Tests/HTTPTypesTests/HTTPTypesTests.swift +++ b/Tests/HTTPTypesTests/HTTPTypesTests.swift @@ -77,14 +77,14 @@ final class HTTPTypesTests: XCTestCase { func isSendable(_ value: some Sendable) -> Bool { true } func isSendable(_ value: Any) -> Bool { false } - let field: HTTPField = HTTPField(name: .userAgent, value: "") + let field: HTTPField = .init(name: .userAgent, value: "") let indexingStrategy: HTTPField.DynamicTableIndexingStrategy = field.indexingStrategy let name: HTTPField.Name = field.name let fields: HTTPFields = [:] - let request: HTTPRequest = HTTPRequest(method: .post, scheme: nil, authority: nil, path: nil) + let request: HTTPRequest = .init(method: .post, scheme: nil, authority: nil, path: nil) let method: HTTPRequest.Method = request.method let requestPseudoHeaderFields: HTTPRequest.PseudoHeaderFields = request.pseudoHeaderFields - let response: HTTPResponse = HTTPResponse(status: .ok) + let response: HTTPResponse = .init(status: .ok) let status: HTTPResponse.Status = response.status let responsePseudoHeaderFields: HTTPResponse.PseudoHeaderFields = response.pseudoHeaderFields