mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-24 06:51:47 +00:00
bulk code format fix (#8707)
This commit is contained in:
@@ -29,8 +29,8 @@ public struct ByteBuffer {
|
||||
@usableFromInline
|
||||
enum Blob {
|
||||
#if !os(WASI)
|
||||
case data(Data)
|
||||
case bytes(ContiguousBytes)
|
||||
case data(Data)
|
||||
case bytes(ContiguousBytes)
|
||||
#endif
|
||||
|
||||
case byteBuffer(_InternalByteBuffer)
|
||||
@@ -96,16 +96,16 @@ public struct ByteBuffer {
|
||||
@inline(__always)
|
||||
func withUnsafeBytes<T>(
|
||||
_ body: (UnsafeRawBufferPointer) throws
|
||||
-> T) rethrows -> T
|
||||
{
|
||||
-> T
|
||||
) rethrows -> T {
|
||||
switch retainedBlob {
|
||||
case .byteBuffer(let byteBuffer):
|
||||
return try byteBuffer.withUnsafeBytes(body)
|
||||
#if !os(WASI)
|
||||
case .data(let data):
|
||||
return try data.withUnsafeBytes(body)
|
||||
case .bytes(let contiguousBytes):
|
||||
return try contiguousBytes.withUnsafeBytes(body)
|
||||
case .data(let data):
|
||||
return try data.withUnsafeBytes(body)
|
||||
case .bytes(let contiguousBytes):
|
||||
return try contiguousBytes.withUnsafeBytes(body)
|
||||
#endif
|
||||
case .array(let array):
|
||||
return try array.withUnsafeBytes(body)
|
||||
@@ -118,25 +118,28 @@ public struct ByteBuffer {
|
||||
@inline(__always)
|
||||
func withUnsafeRawPointer<T>(
|
||||
_ body: (UnsafeMutableRawPointer) throws
|
||||
-> T) rethrows -> T
|
||||
{
|
||||
-> T
|
||||
) rethrows -> T {
|
||||
switch retainedBlob {
|
||||
case .byteBuffer(let byteBuffer):
|
||||
return try byteBuffer.withUnsafeRawPointer(body)
|
||||
#if !os(WASI)
|
||||
case .data(let data):
|
||||
return try data
|
||||
.withUnsafeBytes {
|
||||
try body(UnsafeMutableRawPointer(mutating: $0.baseAddress!))
|
||||
}
|
||||
case .bytes(let contiguousBytes):
|
||||
return try contiguousBytes
|
||||
.withUnsafeBytes {
|
||||
try body(UnsafeMutableRawPointer(mutating: $0.baseAddress!))
|
||||
}
|
||||
case .data(let data):
|
||||
return
|
||||
try data
|
||||
.withUnsafeBytes {
|
||||
try body(UnsafeMutableRawPointer(mutating: $0.baseAddress!))
|
||||
}
|
||||
case .bytes(let contiguousBytes):
|
||||
return
|
||||
try contiguousBytes
|
||||
.withUnsafeBytes {
|
||||
try body(UnsafeMutableRawPointer(mutating: $0.baseAddress!))
|
||||
}
|
||||
#endif
|
||||
case .array(let array):
|
||||
return try array
|
||||
return
|
||||
try array
|
||||
.withUnsafeBytes {
|
||||
try body(UnsafeMutableRawPointer(mutating: $0.baseAddress!))
|
||||
}
|
||||
@@ -149,20 +152,20 @@ public struct ByteBuffer {
|
||||
@inline(__always)
|
||||
func readWithUnsafeRawPointer<T>(
|
||||
position: Int,
|
||||
_ body: (UnsafeRawPointer) throws -> T) rethrows -> T
|
||||
{
|
||||
_ body: (UnsafeRawPointer) throws -> T
|
||||
) rethrows -> T {
|
||||
switch retainedBlob {
|
||||
case .byteBuffer(let byteBuffer):
|
||||
return try byteBuffer.readWithUnsafeRawPointer(position: position, body)
|
||||
#if !os(WASI)
|
||||
case .data(let data):
|
||||
return try data.withUnsafeBytes {
|
||||
try body($0.baseAddress!.advanced(by: position))
|
||||
}
|
||||
case .bytes(let contiguousBytes):
|
||||
return try contiguousBytes.withUnsafeBytes {
|
||||
try body($0.baseAddress!.advanced(by: position))
|
||||
}
|
||||
case .data(let data):
|
||||
return try data.withUnsafeBytes {
|
||||
try body($0.baseAddress!.advanced(by: position))
|
||||
}
|
||||
case .bytes(let contiguousBytes):
|
||||
return try contiguousBytes.withUnsafeBytes {
|
||||
try body($0.baseAddress!.advanced(by: position))
|
||||
}
|
||||
#endif
|
||||
case .array(let array):
|
||||
return try array.withUnsafeBytes {
|
||||
@@ -204,8 +207,8 @@ public struct ByteBuffer {
|
||||
@inline(__always)
|
||||
public init(
|
||||
copyingMemoryBound memory: UnsafeRawPointer,
|
||||
capacity: Int)
|
||||
{
|
||||
capacity: Int
|
||||
) {
|
||||
_storage = Storage(count: capacity)
|
||||
_storage.copy(from: memory, count: capacity)
|
||||
_readerIndex = capacity
|
||||
@@ -223,29 +226,29 @@ public struct ByteBuffer {
|
||||
}
|
||||
|
||||
#if !os(WASI)
|
||||
/// Constructor that creates a Flatbuffer from the Swift Data type object
|
||||
/// - Parameter
|
||||
/// - data: Swift data Object
|
||||
@inline(__always)
|
||||
public init(data: Data) {
|
||||
_storage = Storage(blob: .data(data), capacity: data.count)
|
||||
_readerIndex = data.count
|
||||
capacity = data.count
|
||||
}
|
||||
/// Constructor that creates a Flatbuffer from the Swift Data type object
|
||||
/// - Parameter
|
||||
/// - data: Swift data Object
|
||||
@inline(__always)
|
||||
public init(data: Data) {
|
||||
_storage = Storage(blob: .data(data), capacity: data.count)
|
||||
_readerIndex = data.count
|
||||
capacity = data.count
|
||||
}
|
||||
|
||||
/// Constructor that creates a Flatbuffer object from a ContiguousBytes
|
||||
/// - Parameters:
|
||||
/// - contiguousBytes: Binary stripe to use as the buffer
|
||||
/// - count: amount of readable bytes
|
||||
@inline(__always)
|
||||
public init<Bytes: ContiguousBytes>(
|
||||
contiguousBytes: Bytes,
|
||||
count: Int)
|
||||
{
|
||||
_storage = Storage(blob: .bytes(contiguousBytes), capacity: count)
|
||||
_readerIndex = count
|
||||
capacity = count
|
||||
}
|
||||
/// Constructor that creates a Flatbuffer object from a ContiguousBytes
|
||||
/// - Parameters:
|
||||
/// - contiguousBytes: Binary stripe to use as the buffer
|
||||
/// - count: amount of readable bytes
|
||||
@inline(__always)
|
||||
public init<Bytes: ContiguousBytes>(
|
||||
contiguousBytes: Bytes,
|
||||
count: Int
|
||||
) {
|
||||
_storage = Storage(blob: .bytes(contiguousBytes), capacity: count)
|
||||
_readerIndex = count
|
||||
capacity = count
|
||||
}
|
||||
#endif
|
||||
|
||||
/// Constructor that creates a Flatbuffer from unsafe memory region without copying
|
||||
@@ -257,8 +260,8 @@ public struct ByteBuffer {
|
||||
@inline(__always)
|
||||
public init(
|
||||
assumingMemoryBound memory: UnsafeMutableRawPointer,
|
||||
capacity: Int)
|
||||
{
|
||||
capacity: Int
|
||||
) {
|
||||
_storage = Storage(
|
||||
blob: .pointer(memory),
|
||||
capacity: capacity)
|
||||
@@ -275,8 +278,8 @@ public struct ByteBuffer {
|
||||
init(
|
||||
blob: Storage.Blob,
|
||||
count: Int,
|
||||
removing removeBytes: Int)
|
||||
{
|
||||
removing removeBytes: Int
|
||||
) {
|
||||
_storage = Storage(blob: blob, capacity: count)
|
||||
_readerIndex = removeBytes
|
||||
capacity = count
|
||||
@@ -368,8 +371,8 @@ public struct ByteBuffer {
|
||||
t3: T3.Type,
|
||||
t4: T4.Type,
|
||||
position: Int,
|
||||
byteWidth: UInt8) -> T
|
||||
{
|
||||
byteWidth: UInt8
|
||||
) -> T {
|
||||
switch byteWidth {
|
||||
case 1:
|
||||
numericCast(read(def: T1.self, position: position))
|
||||
@@ -389,8 +392,8 @@ public struct ByteBuffer {
|
||||
@inline(__always)
|
||||
public func readSlice<T>(
|
||||
index: Int,
|
||||
count: Int) -> [T]
|
||||
{
|
||||
count: Int
|
||||
) -> [T] {
|
||||
assert(
|
||||
index + count <= capacity,
|
||||
"Reading out of bounds is illegal")
|
||||
@@ -407,8 +410,8 @@ public struct ByteBuffer {
|
||||
public func readString(
|
||||
at index: Int,
|
||||
count: Int,
|
||||
type: String.Encoding) -> String?
|
||||
{
|
||||
type: String.Encoding
|
||||
) -> String? {
|
||||
assert(
|
||||
index + count <= capacity,
|
||||
"Reading out of bounds is illegal")
|
||||
@@ -429,8 +432,8 @@ public struct ByteBuffer {
|
||||
@inline(__always)
|
||||
public func readString(
|
||||
at index: Int,
|
||||
count: Int) -> String?
|
||||
{
|
||||
count: Int
|
||||
) -> String? {
|
||||
assert(
|
||||
index + count <= capacity,
|
||||
"Reading out of bounds is illegal")
|
||||
@@ -448,8 +451,8 @@ public struct ByteBuffer {
|
||||
public func withUnsafePointerToSlice<T>(
|
||||
index: Int,
|
||||
count: Int,
|
||||
body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T
|
||||
{
|
||||
body: (UnsafeRawBufferPointer) throws -> T
|
||||
) rethrows -> T {
|
||||
assert(
|
||||
index + count <= capacity,
|
||||
"Reading out of bounds is illegal")
|
||||
@@ -462,8 +465,8 @@ public struct ByteBuffer {
|
||||
@inline(__always)
|
||||
public func withUnsafeBytes<T>(
|
||||
body: (UnsafeRawBufferPointer) throws
|
||||
-> T) rethrows -> T
|
||||
{
|
||||
-> T
|
||||
) rethrows -> T {
|
||||
try _storage.withUnsafeBytes(body)
|
||||
}
|
||||
|
||||
@@ -471,8 +474,8 @@ public struct ByteBuffer {
|
||||
@inline(__always)
|
||||
func withUnsafeMutableRawPointer<T>(
|
||||
body: (UnsafeMutableRawPointer) throws
|
||||
-> T) rethrows -> T
|
||||
{
|
||||
-> T
|
||||
) rethrows -> T {
|
||||
try _storage.withUnsafeRawPointer(body)
|
||||
}
|
||||
|
||||
@@ -480,8 +483,8 @@ public struct ByteBuffer {
|
||||
@inline(__always)
|
||||
func readWithUnsafeRawPointer<T>(
|
||||
position: Int,
|
||||
_ body: (UnsafeRawPointer) throws -> T) rethrows -> T
|
||||
{
|
||||
_ body: (UnsafeRawPointer) throws -> T
|
||||
) rethrows -> T {
|
||||
try _storage.readWithUnsafeRawPointer(position: position, body)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,7 +46,8 @@ public enum FlexBufferType: UInt64 {
|
||||
@available(
|
||||
*,
|
||||
deprecated,
|
||||
message: "use FBT_VECTOR or FBT_VECTOR_KEY instead.")
|
||||
message: "use FBT_VECTOR or FBT_VECTOR_KEY instead."
|
||||
)
|
||||
case vectorString = 15
|
||||
|
||||
/// Typed tuples (no type table, no size field).
|
||||
|
||||
@@ -31,8 +31,8 @@ public struct FixedTypedVector: FlexBufferVector {
|
||||
offset: Int,
|
||||
byteWidth: UInt8,
|
||||
type: FlexBufferType,
|
||||
count: Int)
|
||||
{
|
||||
count: Int
|
||||
) {
|
||||
self.byteBuffer = byteBuffer
|
||||
self.offset = offset
|
||||
self.byteWidth = byteWidth
|
||||
|
||||
@@ -39,13 +39,14 @@ public protocol FlexBufferContiguousBytes {
|
||||
var count: Int { get }
|
||||
|
||||
func withUnsafeRawBufferPointer<Result>(
|
||||
_ body: (UnsafeRawBufferPointer) throws -> Result) rethrows -> Result
|
||||
_ body: (UnsafeRawBufferPointer) throws -> Result
|
||||
) rethrows -> Result
|
||||
}
|
||||
|
||||
extension FlexBufferContiguousBytes {
|
||||
public func withUnsafeRawBufferPointer<Result>(
|
||||
_ body: (UnsafeRawBufferPointer) throws -> Result) rethrows -> Result
|
||||
{
|
||||
_ body: (UnsafeRawBufferPointer) throws -> Result
|
||||
) rethrows -> Result {
|
||||
try byteBuffer.withUnsafePointerToSlice(
|
||||
index: offset,
|
||||
count: count,
|
||||
|
||||
@@ -58,8 +58,8 @@ public struct Reference {
|
||||
byteBuffer: ByteBuffer,
|
||||
offset: Int,
|
||||
parentWidth: UInt8,
|
||||
packedType: UInt8)
|
||||
{
|
||||
packedType: UInt8
|
||||
) {
|
||||
guard let type = FlexBufferType(rawValue: UInt64(packedType >> 2)) else {
|
||||
return nil
|
||||
}
|
||||
@@ -76,8 +76,8 @@ public struct Reference {
|
||||
offset: Int,
|
||||
parentWidth: UInt8,
|
||||
byteWidth: UInt8,
|
||||
type: FlexBufferType)
|
||||
{
|
||||
type: FlexBufferType
|
||||
) {
|
||||
self.byteBuffer = byteBuffer
|
||||
self.offset = offset
|
||||
self.parentWidth = parentWidth
|
||||
@@ -97,7 +97,8 @@ public struct Reference {
|
||||
public var uint: UInt64? {
|
||||
return switch type {
|
||||
case .uint: byteBuffer.readUInt64(offset: offset, byteWidth: byteWidth)
|
||||
case .indirectUInt: byteBuffer.readUInt64(
|
||||
case .indirectUInt:
|
||||
byteBuffer.readUInt64(
|
||||
offset: indirect(),
|
||||
byteWidth: byteWidth)
|
||||
default: nil
|
||||
@@ -108,7 +109,8 @@ public struct Reference {
|
||||
public var int: Int64? {
|
||||
return switch type {
|
||||
case .int: byteBuffer.readInt64(offset: offset, byteWidth: byteWidth)
|
||||
case .indirectInt: byteBuffer.readInt64(
|
||||
case .indirectInt:
|
||||
byteBuffer.readInt64(
|
||||
offset: indirect(),
|
||||
byteWidth: byteWidth)
|
||||
default: nil
|
||||
@@ -119,7 +121,8 @@ public struct Reference {
|
||||
public var double: Double? {
|
||||
return switch type {
|
||||
case .float: byteBuffer.readDouble(offset: offset, byteWidth: byteWidth)
|
||||
case .indirectFloat: byteBuffer.readDouble(
|
||||
case .indirectFloat:
|
||||
byteBuffer.readDouble(
|
||||
offset: indirect(),
|
||||
byteWidth: byteWidth)
|
||||
default: nil
|
||||
@@ -238,7 +241,8 @@ public struct Reference {
|
||||
@inline(__always)
|
||||
public func withUnsafeRawPointer<Result>(
|
||||
_ completion: (UnsafeRawPointer) throws
|
||||
-> Result)
|
||||
-> Result
|
||||
)
|
||||
rethrows -> Result?
|
||||
{
|
||||
return try byteBuffer.readWithUnsafeRawPointer(
|
||||
|
||||
@@ -30,8 +30,8 @@ public struct TypedVector: FlexBufferVector {
|
||||
byteBuffer: ByteBuffer,
|
||||
offset: Int,
|
||||
byteWidth: UInt8,
|
||||
type: FlexBufferType)
|
||||
{
|
||||
type: FlexBufferType
|
||||
) {
|
||||
self.byteBuffer = byteBuffer
|
||||
self.offset = offset
|
||||
self.byteWidth = byteWidth
|
||||
@@ -54,8 +54,8 @@ public struct TypedVector: FlexBufferVector {
|
||||
static func mapKeys(
|
||||
byteBuffer: ByteBuffer,
|
||||
offset: Int,
|
||||
byteWidth: UInt8) -> TypedVector
|
||||
{
|
||||
byteWidth: UInt8
|
||||
) -> TypedVector {
|
||||
let prefixedFields = 3
|
||||
let keysOffset = offset &- (numericCast(byteWidth) &* prefixedFields)
|
||||
|
||||
@@ -88,12 +88,11 @@ extension TypedVector {
|
||||
byteWidth)
|
||||
|
||||
return byteBuffer.readWithUnsafeRawPointer(
|
||||
position: indirectoffset)
|
||||
{ bufPointer in
|
||||
position: indirectoffset
|
||||
) { bufPointer in
|
||||
target.withCString { strPointer in
|
||||
Int(strcmp(bufPointer, strPointer))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -18,7 +18,10 @@ import Foundation
|
||||
|
||||
@usableFromInline
|
||||
enum BitWidth: UInt64, CaseIterable {
|
||||
case w8 = 0, w16 = 1, w32 = 2, w64 = 3
|
||||
case w8 = 0
|
||||
case w16 = 1
|
||||
case w32 = 2
|
||||
case w64 = 3
|
||||
}
|
||||
|
||||
extension BitWidth: Comparable {
|
||||
|
||||
@@ -14,11 +14,12 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#if canImport(Common)
|
||||
import Common
|
||||
#endif
|
||||
import Foundation
|
||||
|
||||
#if canImport(Common)
|
||||
import Common
|
||||
#endif
|
||||
|
||||
extension UInt64 {
|
||||
static let one: UInt64 = 1
|
||||
}
|
||||
@@ -55,4 +56,3 @@ extension Optional {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -14,12 +14,12 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#if canImport(Common)
|
||||
import Common
|
||||
#endif
|
||||
|
||||
import Foundation
|
||||
|
||||
#if canImport(Common)
|
||||
import Common
|
||||
#endif
|
||||
|
||||
public struct Value: Equatable {
|
||||
|
||||
@usableFromInline
|
||||
@@ -110,9 +110,11 @@ extension Value {
|
||||
return bitWidth
|
||||
} else {
|
||||
for byteWidth in stride(from: 1, to: MemoryLayout<UInt64>.size, by: 2) {
|
||||
let _offsetLoc: UInt64 = numericCast(numericCast(size) &+ padding(
|
||||
bufSize: numericCast(size),
|
||||
elementSize: numericCast(byteWidth)))
|
||||
let _offsetLoc: UInt64 = numericCast(
|
||||
numericCast(size)
|
||||
&+ padding(
|
||||
bufSize: numericCast(size),
|
||||
elementSize: numericCast(byteWidth)))
|
||||
let offsetLoc = _offsetLoc &+ (index &* numericCast(byteWidth))
|
||||
let offset = offsetLoc &- u
|
||||
|
||||
|
||||
@@ -14,12 +14,12 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#if canImport(Common)
|
||||
import Common
|
||||
#endif
|
||||
|
||||
import Foundation
|
||||
|
||||
#if canImport(Common)
|
||||
import Common
|
||||
#endif
|
||||
|
||||
@inline(__always)
|
||||
internal func isInline(_ t: FlexBufferType) -> Bool {
|
||||
return t <= .float || t == .bool
|
||||
@@ -73,17 +73,26 @@ func getScalarType<T>(type: T.Type) -> FlexBufferType where T: Scalar {
|
||||
|
||||
@inline(__always)
|
||||
func toTypedVector(type: FlexBufferType, length: UInt64) -> FlexBufferType {
|
||||
let type: UInt64 = switch length {
|
||||
case 0: type.rawValue &- FlexBufferType.int.rawValue &+ FlexBufferType
|
||||
.vectorInt.rawValue
|
||||
case 2: type.rawValue &- FlexBufferType.int.rawValue &+ FlexBufferType
|
||||
.vectorInt2.rawValue
|
||||
case 3: type.rawValue &- FlexBufferType.int.rawValue &+ FlexBufferType
|
||||
.vectorInt3.rawValue
|
||||
case 4: type.rawValue &- FlexBufferType.int.rawValue &+ FlexBufferType
|
||||
.vectorInt4.rawValue
|
||||
default: 0
|
||||
}
|
||||
let type: UInt64 =
|
||||
switch length {
|
||||
case 0:
|
||||
type.rawValue &- FlexBufferType.int.rawValue
|
||||
&+ FlexBufferType
|
||||
.vectorInt.rawValue
|
||||
case 2:
|
||||
type.rawValue &- FlexBufferType.int.rawValue
|
||||
&+ FlexBufferType
|
||||
.vectorInt2.rawValue
|
||||
case 3:
|
||||
type.rawValue &- FlexBufferType.int.rawValue
|
||||
&+ FlexBufferType
|
||||
.vectorInt3.rawValue
|
||||
case 4:
|
||||
type.rawValue &- FlexBufferType.int.rawValue
|
||||
&+ FlexBufferType
|
||||
.vectorInt4.rawValue
|
||||
default: 0
|
||||
}
|
||||
return FlexBufferType(rawValue: type) ?? .null
|
||||
}
|
||||
|
||||
@@ -100,7 +109,8 @@ func isTypedVectorType(type: FlexBufferType) -> Bool {
|
||||
@inline(__always)
|
||||
func toTypedVectorElementType(type: FlexBufferType) -> FlexBufferType? {
|
||||
return FlexBufferType(
|
||||
rawValue: type.rawValue &- FlexBufferType.vectorInt
|
||||
rawValue: type.rawValue
|
||||
&- FlexBufferType.vectorInt
|
||||
.rawValue &+ FlexBufferType.int.rawValue)
|
||||
}
|
||||
|
||||
@@ -115,12 +125,16 @@ func toFixedTypedVectorElementType(type: FlexBufferType)
|
||||
{
|
||||
assert(isFixedTypedVectorType(type: type))
|
||||
let fixedType: UInt64 = numericCast(
|
||||
type.rawValue &- FlexBufferType.vectorInt2
|
||||
type.rawValue
|
||||
&- FlexBufferType.vectorInt2
|
||||
.rawValue)
|
||||
let len: Int = numericCast(fixedType.dividedReportingOverflow(by: 3).partialValue &+ 2)
|
||||
return (
|
||||
FlexBufferType(rawValue: (fixedType.quotientAndRemainder(dividingBy: 3).remainder) &+ FlexBufferType.int.rawValue),
|
||||
len)
|
||||
FlexBufferType(
|
||||
rawValue: (fixedType.quotientAndRemainder(dividingBy: 3).remainder)
|
||||
&+ FlexBufferType.int.rawValue),
|
||||
len
|
||||
)
|
||||
}
|
||||
|
||||
// MARK: - Reader functions
|
||||
@@ -128,8 +142,8 @@ func toFixedTypedVectorElementType(type: FlexBufferType)
|
||||
@inline(__always)
|
||||
func binarySearch(
|
||||
vector: TypedVector,
|
||||
target: String) -> Int?
|
||||
{
|
||||
target: String
|
||||
) -> Int? {
|
||||
var left = 0
|
||||
var right = vector.count
|
||||
|
||||
@@ -149,14 +163,17 @@ func binarySearch(
|
||||
|
||||
@inline(__always)
|
||||
func readIndirect(buffer: ByteBuffer, offset: Int, _ byteWidth: UInt8) -> Int {
|
||||
return offset &- numericCast(buffer.readUInt64(
|
||||
offset: offset,
|
||||
byteWidth: byteWidth))
|
||||
return offset
|
||||
&- numericCast(
|
||||
buffer.readUInt64(
|
||||
offset: offset,
|
||||
byteWidth: byteWidth))
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
func getCount(buffer: ByteBuffer, offset: Int, byteWidth: UInt8) -> Int {
|
||||
Int(buffer.readUInt64(
|
||||
offset: offset &- numericCast(byteWidth),
|
||||
byteWidth: byteWidth))
|
||||
Int(
|
||||
buffer.readUInt64(
|
||||
offset: offset &- numericCast(byteWidth),
|
||||
byteWidth: byteWidth))
|
||||
}
|
||||
|
||||
@@ -14,12 +14,12 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#if canImport(Common)
|
||||
import Common
|
||||
#endif
|
||||
|
||||
import Foundation
|
||||
|
||||
#if canImport(Common)
|
||||
import Common
|
||||
#endif
|
||||
|
||||
private let twentyFourBytes: Int = 24
|
||||
public typealias FlexBuffersWriterBuilder = (inout FlexBuffersWriter) -> Void
|
||||
|
||||
@@ -74,21 +74,21 @@ public struct FlexBuffersWriter {
|
||||
}
|
||||
|
||||
#if !os(WASI)
|
||||
/// Data representation of the buffer
|
||||
///
|
||||
/// Should only be used after ``finish(offset:addPrefix:)`` is called
|
||||
public var data: Data {
|
||||
assert(finished, "Data shouldn't be called before finish()")
|
||||
return _bb.withUnsafeSlicedBytes { ptr in
|
||||
var data = Data()
|
||||
data.append(
|
||||
ptr.baseAddress!.bindMemory(
|
||||
to: UInt8.self,
|
||||
capacity: ptr.count),
|
||||
count: ptr.count)
|
||||
return data
|
||||
/// Data representation of the buffer
|
||||
///
|
||||
/// Should only be used after ``finish(offset:addPrefix:)`` is called
|
||||
public var data: Data {
|
||||
assert(finished, "Data shouldn't be called before finish()")
|
||||
return _bb.withUnsafeSlicedBytes { ptr in
|
||||
var data = Data()
|
||||
data.append(
|
||||
ptr.baseAddress!.bindMemory(
|
||||
to: UInt8.self,
|
||||
capacity: ptr.count),
|
||||
count: ptr.count)
|
||||
return data
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/// Resets the internal state. Automatically called before building a new flexbuffer.
|
||||
@@ -139,8 +139,8 @@ public struct FlexBuffersWriter {
|
||||
public mutating func endVector(
|
||||
start: Int,
|
||||
typed: Bool = false,
|
||||
fixed: Bool = false) -> UInt64
|
||||
{
|
||||
fixed: Bool = false
|
||||
) -> UInt64 {
|
||||
let vec = createVector(
|
||||
start: start,
|
||||
count: stack.count &- start,
|
||||
@@ -162,8 +162,7 @@ public struct FlexBuffersWriter {
|
||||
@discardableResult
|
||||
@inline(__always)
|
||||
public mutating func create<T>(vector: [T], key: borrowing String) -> Int
|
||||
where T: Scalar
|
||||
{
|
||||
where T: Scalar {
|
||||
add(key: key)
|
||||
return create(vector: vector, fixed: false)
|
||||
}
|
||||
@@ -179,8 +178,8 @@ public struct FlexBuffersWriter {
|
||||
@inline(__always)
|
||||
public mutating func createFixed<T>(
|
||||
vector: [T],
|
||||
key: borrowing String) -> Int where T: Scalar
|
||||
{
|
||||
key: borrowing String
|
||||
) -> Int where T: Scalar {
|
||||
assert(vector.count >= 2 && vector.count <= 4)
|
||||
add(key: key)
|
||||
return create(vector: vector, fixed: true)
|
||||
@@ -302,8 +301,8 @@ public struct FlexBuffersWriter {
|
||||
@inline(__always)
|
||||
public mutating func add(
|
||||
uint64 value: borrowing UInt64,
|
||||
key: borrowing String)
|
||||
{
|
||||
key: borrowing String
|
||||
) {
|
||||
add(key: key)
|
||||
add(uint64: value)
|
||||
}
|
||||
@@ -316,8 +315,8 @@ public struct FlexBuffersWriter {
|
||||
@inline(__always)
|
||||
public mutating func indirect(
|
||||
uint64 val: borrowing UInt64,
|
||||
key: borrowing String)
|
||||
{
|
||||
key: borrowing String
|
||||
) {
|
||||
add(key: key)
|
||||
indirect(uint64: val)
|
||||
}
|
||||
@@ -325,8 +324,8 @@ public struct FlexBuffersWriter {
|
||||
@inline(__always)
|
||||
public mutating func indirect(
|
||||
uint val: borrowing UInt,
|
||||
key: borrowing String)
|
||||
{
|
||||
key: borrowing String
|
||||
) {
|
||||
add(key: key)
|
||||
indirect(uint64: numericCast(val))
|
||||
}
|
||||
@@ -385,8 +384,8 @@ public struct FlexBuffersWriter {
|
||||
@inline(__always)
|
||||
public mutating func add(
|
||||
int64 value: borrowing Int64,
|
||||
key: borrowing String)
|
||||
{
|
||||
key: borrowing String
|
||||
) {
|
||||
add(key: key)
|
||||
add(int64: value)
|
||||
}
|
||||
@@ -399,8 +398,8 @@ public struct FlexBuffersWriter {
|
||||
@inline(__always)
|
||||
public mutating func indirect(
|
||||
int64 val: borrowing Int64,
|
||||
key: borrowing String)
|
||||
{
|
||||
key: borrowing String
|
||||
) {
|
||||
add(key: key)
|
||||
indirect(int64: val)
|
||||
}
|
||||
@@ -408,8 +407,8 @@ public struct FlexBuffersWriter {
|
||||
@inline(__always)
|
||||
public mutating func indirect(
|
||||
int val: borrowing Int,
|
||||
key: borrowing String)
|
||||
{
|
||||
key: borrowing String
|
||||
) {
|
||||
add(key: key)
|
||||
indirect(int64: numericCast(val))
|
||||
}
|
||||
@@ -424,8 +423,8 @@ public struct FlexBuffersWriter {
|
||||
@inline(__always)
|
||||
public mutating func add(
|
||||
float32 value: borrowing Float32,
|
||||
key: borrowing String)
|
||||
{
|
||||
key: borrowing String
|
||||
) {
|
||||
add(key: key)
|
||||
add(float32: value)
|
||||
}
|
||||
@@ -438,8 +437,8 @@ public struct FlexBuffersWriter {
|
||||
@inline(__always)
|
||||
public mutating func indirect(
|
||||
float32 val: borrowing Float32,
|
||||
key: borrowing String)
|
||||
{
|
||||
key: borrowing String
|
||||
) {
|
||||
add(key: key)
|
||||
indirect(float32: val)
|
||||
}
|
||||
@@ -452,8 +451,8 @@ public struct FlexBuffersWriter {
|
||||
@inline(__always)
|
||||
public mutating func add(
|
||||
double value: borrowing Double,
|
||||
key: borrowing String)
|
||||
{
|
||||
key: borrowing String
|
||||
) {
|
||||
add(key: key)
|
||||
add(double: value)
|
||||
}
|
||||
@@ -466,8 +465,8 @@ public struct FlexBuffersWriter {
|
||||
@inline(__always)
|
||||
public mutating func indirect(
|
||||
double val: borrowing Double,
|
||||
key: borrowing String)
|
||||
{
|
||||
key: borrowing String
|
||||
) {
|
||||
add(key: key)
|
||||
indirect(double: val)
|
||||
}
|
||||
@@ -489,8 +488,8 @@ public struct FlexBuffersWriter {
|
||||
@inline(__always)
|
||||
public mutating func add<T>(
|
||||
blob: borrowing T,
|
||||
length l: Int) -> UInt where T: ContiguousBytes
|
||||
{
|
||||
length l: Int
|
||||
) -> UInt where T: ContiguousBytes {
|
||||
storeBlob(blob, len: l, type: .blob)
|
||||
}
|
||||
|
||||
@@ -499,8 +498,8 @@ public struct FlexBuffersWriter {
|
||||
public mutating func add<T>(
|
||||
blob: borrowing T,
|
||||
key: borrowing String,
|
||||
length l: Int) -> UInt where T: ContiguousBytes
|
||||
{
|
||||
length l: Int
|
||||
) -> UInt where T: ContiguousBytes {
|
||||
add(key: key)
|
||||
return storeBlob(blob, len: l, type: .blob)
|
||||
}
|
||||
@@ -541,8 +540,8 @@ public struct FlexBuffersWriter {
|
||||
mutating func pushIndirect<T>(
|
||||
value: T,
|
||||
type: FlexBufferType,
|
||||
bitWidth: BitWidth)
|
||||
{
|
||||
bitWidth: BitWidth
|
||||
) {
|
||||
let byteWidth = align(width: bitWidth)
|
||||
let iloc = writerIndex
|
||||
_bb.ensureSpace(size: byteWidth)
|
||||
@@ -619,8 +618,8 @@ public struct FlexBuffersWriter {
|
||||
mutating func storeBlob<T>(
|
||||
_ bytes: T,
|
||||
len: Int,
|
||||
type: FlexBufferType) -> UInt where T: ContiguousBytes
|
||||
{
|
||||
type: FlexBufferType
|
||||
) -> UInt where T: ContiguousBytes {
|
||||
return bytes.withUnsafeBytes {
|
||||
storeBlob(pointer: $0.baseAddress!, len: len, type: type)
|
||||
}
|
||||
@@ -632,8 +631,8 @@ public struct FlexBuffersWriter {
|
||||
pointer: borrowing UnsafeRawPointer,
|
||||
len: Int,
|
||||
trailing: Int = 0,
|
||||
type: FlexBufferType) -> UInt
|
||||
{
|
||||
type: FlexBufferType
|
||||
) -> UInt {
|
||||
_bb.ensureSpace(size: len &+ trailing)
|
||||
let bitWidth = widthU(numericCast(len))
|
||||
|
||||
@@ -656,8 +655,7 @@ public struct FlexBuffersWriter {
|
||||
@discardableResult
|
||||
@usableFromInline
|
||||
mutating func create<T>(vector: [T], fixed: Bool) -> Int
|
||||
where T: Scalar
|
||||
{
|
||||
where T: Scalar {
|
||||
let length: UInt64 = numericCast(vector.count)
|
||||
let vectorType = getScalarType(type: T.self)
|
||||
let byteWidth = MemoryLayout<T>.size
|
||||
@@ -693,8 +691,8 @@ public struct FlexBuffersWriter {
|
||||
step: Int,
|
||||
typed: Bool,
|
||||
fixed: Bool,
|
||||
keys: Value? = nil) -> Value
|
||||
{
|
||||
keys: Value? = nil
|
||||
) -> Value {
|
||||
assert(
|
||||
!fixed || typed,
|
||||
"Typed false and fixed true is a combination not supported currently")
|
||||
@@ -736,16 +734,17 @@ public struct FlexBuffersWriter {
|
||||
let byteWidth = align(width: bitWidth)
|
||||
|
||||
let currentSize: Int = count &* step &* byteWidth
|
||||
let requiredSize: Int = if !typed {
|
||||
// We ensure that we have enough space
|
||||
// for loop two write operations &
|
||||
// 24 bytes for when its not fixed,
|
||||
// and keys isn't null. As an extra safe
|
||||
// guard
|
||||
(currentSize &* 2) &+ twentyFourBytes
|
||||
} else {
|
||||
currentSize
|
||||
}
|
||||
let requiredSize: Int =
|
||||
if !typed {
|
||||
// We ensure that we have enough space
|
||||
// for loop two write operations &
|
||||
// 24 bytes for when its not fixed,
|
||||
// and keys isn't null. As an extra safe
|
||||
// guard
|
||||
(currentSize &* 2) &+ twentyFourBytes
|
||||
} else {
|
||||
currentSize
|
||||
}
|
||||
|
||||
_bb.ensureSpace(
|
||||
size: requiredSize)
|
||||
@@ -858,8 +857,8 @@ extension FlexBuffersWriter {
|
||||
@discardableResult
|
||||
public mutating func vector(
|
||||
key: String,
|
||||
_ closure: FlexBuffersWriterBuilder) -> UInt64
|
||||
{
|
||||
_ closure: FlexBuffersWriterBuilder
|
||||
) -> UInt64 {
|
||||
let start = startVector(key: key)
|
||||
closure(&self)
|
||||
return endVector(start: start)
|
||||
@@ -880,8 +879,8 @@ extension FlexBuffersWriter {
|
||||
@discardableResult
|
||||
public mutating func map(
|
||||
key: String,
|
||||
_ closure: FlexBuffersWriterBuilder) -> UInt64
|
||||
{
|
||||
_ closure: FlexBuffersWriterBuilder
|
||||
) -> UInt64 {
|
||||
let start = startMap(key: key)
|
||||
closure(&self)
|
||||
return endMap(start: start)
|
||||
|
||||
@@ -14,12 +14,12 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#if canImport(Common)
|
||||
import Common
|
||||
#endif
|
||||
|
||||
import Foundation
|
||||
|
||||
#if canImport(Common)
|
||||
import Common
|
||||
#endif
|
||||
|
||||
/// `ByteBuffer` is the interface that stores the data for a `Flatbuffers` object
|
||||
/// it allows users to write and read data directly from memory thus the use of its
|
||||
/// functions should be used
|
||||
@@ -124,7 +124,7 @@ struct _InternalByteBuffer {
|
||||
@usableFromInline
|
||||
mutating func ensureSpace(size: Int) {
|
||||
guard size &+ writerIndex > capacity else { return }
|
||||
|
||||
|
||||
while capacity <= writerIndex &+ size {
|
||||
capacity = capacity << 1
|
||||
}
|
||||
@@ -141,9 +141,12 @@ struct _InternalByteBuffer {
|
||||
|
||||
@inline(__always)
|
||||
mutating func addPadding(bytes: Int) {
|
||||
writerIndex = writerIndex &+ numericCast(padding(
|
||||
bufSize: numericCast(writerIndex),
|
||||
elementSize: numericCast(bytes)))
|
||||
writerIndex =
|
||||
writerIndex
|
||||
&+ numericCast(
|
||||
padding(
|
||||
bufSize: numericCast(writerIndex),
|
||||
elementSize: numericCast(bytes)))
|
||||
ensureSpace(size: writerIndex)
|
||||
}
|
||||
|
||||
@@ -171,30 +174,32 @@ struct _InternalByteBuffer {
|
||||
@inline(__always)
|
||||
func withUnsafeBytes<T>(
|
||||
_ body: (UnsafeRawBufferPointer) throws
|
||||
-> T) rethrows -> T
|
||||
{
|
||||
try body(UnsafeRawBufferPointer(
|
||||
start: _storage.memory,
|
||||
count: capacity))
|
||||
-> T
|
||||
) rethrows -> T {
|
||||
try body(
|
||||
UnsafeRawBufferPointer(
|
||||
start: _storage.memory,
|
||||
count: capacity))
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
@inline(__always)
|
||||
func withUnsafeSlicedBytes<T>(
|
||||
_ body: (UnsafeRawBufferPointer) throws
|
||||
-> T) rethrows -> T
|
||||
{
|
||||
try body(UnsafeRawBufferPointer(
|
||||
start: _storage.memory,
|
||||
count: writerIndex))
|
||||
-> T
|
||||
) rethrows -> T {
|
||||
try body(
|
||||
UnsafeRawBufferPointer(
|
||||
start: _storage.memory,
|
||||
count: writerIndex))
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
@inline(__always)
|
||||
func withUnsafeRawPointer<T>(
|
||||
_ body: (UnsafeMutableRawPointer) throws
|
||||
-> T) rethrows -> T
|
||||
{
|
||||
-> T
|
||||
) rethrows -> T {
|
||||
try body(_storage.memory)
|
||||
}
|
||||
|
||||
@@ -202,8 +207,8 @@ struct _InternalByteBuffer {
|
||||
@inline(__always)
|
||||
func readWithUnsafeRawPointer<T>(
|
||||
position: Int,
|
||||
_ body: (UnsafeRawPointer) throws -> T) rethrows -> T
|
||||
{
|
||||
_ body: (UnsafeRawPointer) throws -> T
|
||||
) rethrows -> T {
|
||||
try body(_storage.memory.advanced(by: position))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user