Jelajahi Sumber

Update for Swift 5 compiler

Alan Zeino 7 tahun lalu
induk
melakukan
0d6ce42f01
3 mengubah file dengan 29 tambahan dan 8 penghapusan
  1. 14 2
      Sources/HttpParser.swift
  2. 12 1
      Sources/Socket.swift
  3. 3 5
      Sources/WebSockets.swift

+ 14 - 2
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..<nameEndIndex]).removingPercentEncoding else {
                     return c
                 }

+ 12 - 1
Sources/Socket.swift

@@ -35,7 +35,9 @@ open class Socket: Hashable, Equatable {
         close()
     }
     
-    public var hashValue: Int { return Int(self.socketFileDescriptor) }
+    public func hash(into hasher: inout Hasher) {
+        hasher.combine(self.socketFileDescriptor)
+    }
     
     public func close() {
         if shutdown {
@@ -91,9 +93,18 @@ open class Socket: Hashable, Equatable {
     }
     
     public func writeData(_ data: Data) throws {
+        #if compiler(>=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<UInt8>) -> Void in
             try self.writeBuffer(pointer, length: data.count)
         }
+        #endif
     }
 
     private func writeBuffer(_ pointer: UnsafeRawPointer, length: Int) throws {

+ 3 - 5
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)
     }
 }