mirror of
https://github.com/google/flatbuffers.git
synced 2026-07-01 23:03:57 +00:00
Swift FlatBufferBuilder.sizedByteArray to ByteBuffer.toArray() (#7093)
* Moved code from FlatBufferBuilder.sizedByteArray to ByteBuffer.toArray() in Swift * ByteBuffer.toArray() to ByteBuffer.underlyingBytes
This commit is contained in:
@@ -387,6 +387,16 @@ public struct ByteBuffer {
|
|||||||
removing: _writerSize &- removeBytes)
|
removing: _writerSize &- removeBytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns the written bytes into the ``ByteBuffer``
|
||||||
|
public var underlyingBytes: [UInt8] {
|
||||||
|
let cp = capacity &- writerIndex
|
||||||
|
let start = memory.advanced(by: writerIndex)
|
||||||
|
.bindMemory(to: UInt8.self, capacity: cp)
|
||||||
|
|
||||||
|
let ptr = UnsafeBufferPointer<UInt8>(start: start, count: cp)
|
||||||
|
return Array(ptr)
|
||||||
|
}
|
||||||
|
|
||||||
/// SkipPrefix Skips the first 4 bytes in case one of the following
|
/// SkipPrefix Skips the first 4 bytes in case one of the following
|
||||||
/// functions are called `getPrefixedSizeCheckedRoot` & `getPrefixedSizeRoot`
|
/// functions are called `getPrefixedSizeCheckedRoot` & `getPrefixedSizeRoot`
|
||||||
/// which allows us to skip the first 4 bytes instead of recreating the buffer
|
/// which allows us to skip the first 4 bytes instead of recreating the buffer
|
||||||
|
|||||||
@@ -80,12 +80,7 @@ public struct FlatBufferBuilder {
|
|||||||
/// Should only be used after ``finish(offset:addPrefix:)`` is called
|
/// Should only be used after ``finish(offset:addPrefix:)`` is called
|
||||||
public var sizedByteArray: [UInt8] {
|
public var sizedByteArray: [UInt8] {
|
||||||
assert(finished, "Data shouldn't be called before finish()")
|
assert(finished, "Data shouldn't be called before finish()")
|
||||||
let cp = _bb.capacity &- _bb.writerIndex
|
return _bb.underlyingBytes
|
||||||
let start = _bb.memory.advanced(by: _bb.writerIndex)
|
|
||||||
.bindMemory(to: UInt8.self, capacity: cp)
|
|
||||||
|
|
||||||
let ptr = UnsafeBufferPointer(start: start, count: cp)
|
|
||||||
return Array(ptr)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the original ``ByteBuffer``
|
/// Returns the original ``ByteBuffer``
|
||||||
|
|||||||
Reference in New Issue
Block a user