@@ -174,32 +174,35 @@ extension UInt: BinaryCodable {
174174
175175extension String : BinaryCodable {
176176 public func encode( into data: inout ContiguousArray < UInt8 > , version: BinaryCodableVersion ) throws {
177- self . cString ( using: . utf8) !. withUnsafeBytes { utf8Bytes in
178- data. append ( contentsOf: utf8Bytes)
177+ try self . utf8. withContiguousStorageIfAvailable { buffer in
178+ try buffer. count. encode ( into: & data, version: version)
179+ data. append ( contentsOf: buffer)
179180 }
180181 }
181182
182183 public init ( decoding data: UnsafeRawBufferPointer , at offset: inout Int , version: BinaryCodableVersion ) throws {
183- let pointer = data. baseAddress!. advanced ( by: offset) . assumingMemoryBound ( to: CChar . self)
184+ let count = try Int ( decoding: data, at: & offset, version: version)
185+ let pointer = data. baseAddress!. advanced ( by: offset) . assumingMemoryBound ( to: UInt8 . self)
186+ offset += count
184187 self . init ( cString: pointer)
185- offset += self . utf8. count
186188 }
187189}
188190
189191#if canImport(Foundation)
190192public import struct Foundation. Data
191193extension Data : BinaryCodable {
192194 public func encode( into data: inout ContiguousArray < UInt8 > , version: BinaryCodableVersion ) throws {
193- try data . withUnsafeBytes { bytes in
194- try bytes. count . encode ( into : & data , version : version )
195+ try self . count . encode ( into : & data , version : version )
196+ self . withUnsafeBytes { bytes in
195197 data. append ( contentsOf: bytes)
196198 }
197199 }
198200
199201 public init ( decoding data: UnsafeRawBufferPointer , at offset: inout Int , version: BinaryCodableVersion ) throws {
200202 let count = try Int ( decoding: data, at: & offset, version: version)
201- self . init ( bytes : data. baseAddress!. advanced ( by: offset) , count : count )
203+ let pointer = data. baseAddress!. advanced ( by: offset)
202204 offset += count
205+ self . init ( bytes: pointer, count: count)
203206 }
204207}
205208#endif
0 commit comments