summaryrefslogtreecommitdiff
path: root/swift
diff options
context:
space:
mode:
authorMatt Grippaldi <mattg@kinematicsystems.com>2022-02-11 16:26:16 -0500
committerGitHub <noreply@github.com>2022-02-11 13:26:16 -0800
commita94132a45faa82d28c67db419f25ab738f3475a5 (patch)
treefe7fc6d85f6d08e4aafe10e166d60eb11ef3ac85 /swift
parent48befb6bef6a81f68356f9da8c7a5934d34a78d0 (diff)
downloadflatbuffers-a94132a45faa82d28c67db419f25ab738f3475a5.tar.gz
flatbuffers-a94132a45faa82d28c67db419f25ab738f3475a5.tar.bz2
flatbuffers-a94132a45faa82d28c67db419f25ab738f3475a5.zip
Swift FlatBufferBuilder.sizedByteArray to ByteBuffer.toArray() (#7093)
* Moved code from FlatBufferBuilder.sizedByteArray to ByteBuffer.toArray() in Swift * ByteBuffer.toArray() to ByteBuffer.underlyingBytes
Diffstat (limited to 'swift')
-rw-r--r--swift/Sources/FlatBuffers/ByteBuffer.swift10
-rw-r--r--swift/Sources/FlatBuffers/FlatBufferBuilder.swift7
2 files changed, 11 insertions, 6 deletions
diff --git a/swift/Sources/FlatBuffers/ByteBuffer.swift b/swift/Sources/FlatBuffers/ByteBuffer.swift
index f0ba5d09..a07f66ae 100644
--- a/swift/Sources/FlatBuffers/ByteBuffer.swift
+++ b/swift/Sources/FlatBuffers/ByteBuffer.swift
@@ -387,6 +387,16 @@ public struct ByteBuffer {
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
/// functions are called `getPrefixedSizeCheckedRoot` & `getPrefixedSizeRoot`
/// which allows us to skip the first 4 bytes instead of recreating the buffer
diff --git a/swift/Sources/FlatBuffers/FlatBufferBuilder.swift b/swift/Sources/FlatBuffers/FlatBufferBuilder.swift
index f9ae83b1..5ccc7e44 100644
--- a/swift/Sources/FlatBuffers/FlatBufferBuilder.swift
+++ b/swift/Sources/FlatBuffers/FlatBufferBuilder.swift
@@ -80,12 +80,7 @@ public struct FlatBufferBuilder {
/// Should only be used after ``finish(offset:addPrefix:)`` is called
public var sizedByteArray: [UInt8] {
assert(finished, "Data shouldn't be called before finish()")
- let cp = _bb.capacity &- _bb.writerIndex
- let start = _bb.memory.advanced(by: _bb.writerIndex)
- .bindMemory(to: UInt8.self, capacity: cp)
-
- let ptr = UnsafeBufferPointer(start: start, count: cp)
- return Array(ptr)
+ return _bb.underlyingBytes
}
/// Returns the original ``ByteBuffer``