Skip to content

Commit 0d6ce42

Browse files
committed
Update for Swift 5 compiler
1 parent c3231ac commit 0d6ce42

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

Sources/HttpParser.swift

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,16 @@ public class HttpParser {
3333
}
3434

3535
private func extractQueryParams(_ url: String) -> [(String, String)] {
36-
guard let questionMark = url.index(of: "?") else {
36+
#if compiler(>=5.0)
37+
guard let questionMarkIndex = url.firstIndex(of: "?") else {
3738
return []
3839
}
39-
let queryStart = url.index(after: questionMark)
40+
#else
41+
guard let questionMarkIndex = url.index(of: "?") else {
42+
return []
43+
}
44+
#endif
45+
let queryStart = url.index(after: questionMarkIndex)
4046

4147
guard url.endIndex > queryStart else { return [] }
4248

@@ -48,9 +54,15 @@ public class HttpParser {
4854

4955
return query.components(separatedBy: "&")
5056
.reduce([(String, String)]()) { (c, s) -> [(String, String)] in
57+
#if compiler(>=5.0)
58+
guard let nameEndIndex = s.firstIndex(of: "=") else {
59+
return c
60+
}
61+
#else
5162
guard let nameEndIndex = s.index(of: "=") else {
5263
return c
5364
}
65+
#endif
5466
guard let name = String(s[s.startIndex..<nameEndIndex]).removingPercentEncoding else {
5567
return c
5668
}

Sources/Socket.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ open class Socket: Hashable, Equatable {
3535
close()
3636
}
3737

38-
public var hashValue: Int { return Int(self.socketFileDescriptor) }
38+
public func hash(into hasher: inout Hasher) {
39+
hasher.combine(self.socketFileDescriptor)
40+
}
3941

4042
public func close() {
4143
if shutdown {
@@ -91,9 +93,18 @@ open class Socket: Hashable, Equatable {
9193
}
9294

9395
public func writeData(_ data: Data) throws {
96+
#if compiler(>=5.0)
97+
try data.withUnsafeBytes { (body: UnsafeRawBufferPointer) -> Void in
98+
if let baseAddress = body.baseAddress, body.count > 0 {
99+
let pointer = baseAddress.assumingMemoryBound(to: UInt8.self)
100+
try self.writeBuffer(pointer, length: data.count)
101+
}
102+
}
103+
#else
94104
try data.withUnsafeBytes { (pointer: UnsafePointer<UInt8>) -> Void in
95105
try self.writeBuffer(pointer, length: data.count)
96106
}
107+
#endif
97108
}
98109

99110
private func writeBuffer(_ pointer: UnsafeRawPointer, length: Int) throws {

Sources/WebSockets.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -282,11 +282,9 @@ public class WebSocketSession: Hashable, Equatable {
282282
}
283283
return frm
284284
}
285-
286-
public var hashValue: Int {
287-
get {
288-
return socket.hashValue
289-
}
285+
286+
public func hash(into hasher: inout Hasher) {
287+
hasher.combine(socket)
290288
}
291289
}
292290

0 commit comments

Comments
 (0)