forked from vapor/postgres-nio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathByteBuffer+PSQL.swift
41 lines (32 loc) · 1.18 KB
/
ByteBuffer+PSQL.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import NIO
internal extension ByteBuffer {
mutating func writeNullTerminatedString(_ string: String) {
self.writeString(string)
self.writeInteger(0, as: UInt8.self)
}
mutating func readNullTerminatedString() -> String? {
guard let nullIndex = readableBytesView.firstIndex(of: 0) else {
return nil
}
defer { moveReaderIndex(forwardBy: 1) }
return readString(length: nullIndex - readerIndex)
}
mutating func writeBackendMessageID(_ messageID: PSQLBackendMessage.ID) {
self.writeInteger(messageID.rawValue)
}
mutating func writeFrontendMessageID(_ messageID: PSQLFrontendMessage.ID) {
self.writeInteger(messageID.byte)
}
mutating func readFloat() -> Float? {
return self.readInteger(as: UInt32.self).map { Float(bitPattern: $0) }
}
mutating func readDouble() -> Double? {
return self.readInteger(as: UInt64.self).map { Double(bitPattern: $0) }
}
mutating func writeFloat(_ float: Float) {
self.writeInteger(float.bitPattern)
}
mutating func writeDouble(_ double: Double) {
self.writeInteger(double.bitPattern)
}
}