From 0d6ce42f01ba728759c8e168c1832c5efc3a4338 Mon Sep 17 00:00:00 2001 From: Alan Zeino Date: Tue, 12 Mar 2019 14:15:33 -0700 Subject: [PATCH] Update for Swift 5 compiler --- Sources/HttpParser.swift | 16 ++++++++++++++-- Sources/Socket.swift | 13 ++++++++++++- Sources/WebSockets.swift | 8 +++----- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/Sources/HttpParser.swift b/Sources/HttpParser.swift index 392b2f0f..bf07ea00 100644 --- a/Sources/HttpParser.swift +++ b/Sources/HttpParser.swift @@ -33,10 +33,16 @@ public class HttpParser { } private func extractQueryParams(_ url: String) -> [(String, String)] { - guard let questionMark = url.index(of: "?") else { + #if compiler(>=5.0) + guard let questionMarkIndex = url.firstIndex(of: "?") else { return [] } - let queryStart = url.index(after: questionMark) + #else + guard let questionMarkIndex = url.index(of: "?") else { + return [] + } + #endif + let queryStart = url.index(after: questionMarkIndex) guard url.endIndex > queryStart else { return [] } @@ -48,9 +54,15 @@ public class HttpParser { return query.components(separatedBy: "&") .reduce([(String, String)]()) { (c, s) -> [(String, String)] in + #if compiler(>=5.0) + guard let nameEndIndex = s.firstIndex(of: "=") else { + return c + } + #else guard let nameEndIndex = s.index(of: "=") else { return c } + #endif guard let name = String(s[s.startIndex..=5.0) + try data.withUnsafeBytes { (body: UnsafeRawBufferPointer) -> Void in + if let baseAddress = body.baseAddress, body.count > 0 { + let pointer = baseAddress.assumingMemoryBound(to: UInt8.self) + try self.writeBuffer(pointer, length: data.count) + } + } + #else try data.withUnsafeBytes { (pointer: UnsafePointer) -> Void in try self.writeBuffer(pointer, length: data.count) } + #endif } private func writeBuffer(_ pointer: UnsafeRawPointer, length: Int) throws { diff --git a/Sources/WebSockets.swift b/Sources/WebSockets.swift index 81760eba..5eaf32b9 100644 --- a/Sources/WebSockets.swift +++ b/Sources/WebSockets.swift @@ -282,11 +282,9 @@ public class WebSocketSession: Hashable, Equatable { } return frm } - - public var hashValue: Int { - get { - return socket.hashValue - } + + public func hash(into hasher: inout Hasher) { + hasher.combine(socket) } }