@@ -65,37 +65,15 @@ internal extension ByteBuffer {
6565 }
6666 return array
6767 }
68-
69- mutating func readFloat< T: BinaryFloatingPoint > ( as: T . Type = T . self) -> T ? {
70- guard self . readableBytes >= MemoryLayout< T> . size else {
71- return nil
72- }
73-
74- let value : T = self . getFloat ( at: self . readerIndex) ! /* must work as we have enough bytes */
75- // should be MoveReaderIndex
76- self . moveReaderIndex ( forwardBy: MemoryLayout< T> . size)
77- return value
68+
69+ mutating func readFloat( ) -> Float ? {
70+ return self . readInteger ( as: UInt32 . self) . map { Float ( bitPattern: $0) }
7871 }
7972
80- func getFloat< T: BinaryFloatingPoint > ( at index: Int , as: T . Type = T . self) -> T ? {
81- precondition ( index >= 0 , " index must not be negative " )
82- return self . withVeryUnsafeBytes { ptr in
83- guard index <= ptr. count - MemoryLayout< T> . size else {
84- return nil
85- }
86- var value : T = 0
87- withUnsafeMutableBytes ( of: & value) { valuePtr in
88- valuePtr. copyBytes (
89- from: UnsafeRawBufferPointer (
90- start: ptr. baseAddress!. advanced ( by: index) ,
91- count: MemoryLayout< T> . size
92- ) . reversed ( )
93- )
94- }
95- return value
96- }
73+ mutating func readDouble( ) -> Double ? {
74+ return self . readInteger ( as: UInt64 . self) . map { Double ( bitPattern: $0) }
9775 }
98-
76+
9977 mutating func readUUID( ) -> UUID ? {
10078 guard self . readableBytes >= MemoryLayout< UUID> . size else {
10179 return nil
@@ -108,21 +86,13 @@ internal extension ByteBuffer {
10886 }
10987
11088 func getUUID( at index: Int ) -> UUID ? {
111- precondition ( index >= 0 , " index must not be negative " )
112- return self . withVeryUnsafeBytes { ptr in
113- guard index <= ptr. count - MemoryLayout< uuid_t> . size else {
114- return nil
115- }
116- var value : uuid_t = ( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 )
117- withUnsafeMutableBytes ( of: & value) { valuePtr in
118- valuePtr. copyMemory (
119- from: UnsafeRawBufferPointer (
120- start: ptr. baseAddress!. advanced ( by: index) ,
121- count: MemoryLayout< UUID> . size
122- )
123- )
89+ var uuid : uuid_t = ( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 )
90+ return self . viewBytes ( at: index, length: MemoryLayout . size ( ofValue: uuid) ) . map { bufferBytes in
91+ withUnsafeMutableBytes ( of: & uuid) { target in
92+ precondition ( target. count <= bufferBytes. count)
93+ target. copyBytes ( from: bufferBytes)
12494 }
125- return UUID ( uuid: value )
95+ return UUID ( uuid: uuid )
12696 }
12797 }
12898}
0 commit comments