[Swift] Flexbuffers native swift port (#8577)

* Offical Swift port for FlexBuffers

This is the offical port for FlexBuffers within
swift, and it introcudes a Common Module where code
is shared between flatbuffers and flexbuffers.

Writing most supported values like maps, vectors,
nil and scalars into a flexbuffer buffer. And includes
tests to verify that its similar to cpp

* Reading a flexbuffer

Implementing reading from a flexbuffer, enabling
most of the buffers features, like most types, maps, vectors,
typedvectors, and fixedtypedvectors.

Currently, if an offset/object cant be read we default to a swift
nil instead of the default flexbuffers 'null' with all values.

* Fixes bazel breaking due to new project structure

Address warnings within the library

* Adds comment on why we added the code & properly enforce the amout of bytes needed
This commit is contained in:
mustiikhalil
2025-06-22 08:36:38 +02:00
committed by GitHub
parent 595ac94a6a
commit 5a95b7b6bc
34 changed files with 3423 additions and 100 deletions

View File

@@ -0,0 +1,48 @@
/*
* Copyright 2024 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import Foundation
@usableFromInline
enum BitWidth: UInt64, CaseIterable {
case w8 = 0, w16 = 1, w32 = 2, w64 = 3
}
extension BitWidth: Comparable {
@usableFromInline
static func < (lhs: BitWidth, rhs: BitWidth) -> Bool {
lhs.rawValue < rhs.rawValue
}
@inline(__always)
static func widthB(_ v: Int) -> BitWidth {
switch v {
case 1: return .w8
case 2: return .w16
case 4: return .w32
case 8: return .w64
default:
assert(false, "We shouldn't reach here")
return .w64
}
}
@inline(__always)
static func max(_ lhs: BitWidth, rhs: BitWidth) -> BitWidth {
if lhs.rawValue > rhs.rawValue { return lhs }
return rhs
}
}

View File

@@ -0,0 +1,58 @@
/*
* Copyright 2024 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#if canImport(Common)
@_exported import Common
#endif
import Foundation
extension UInt64 {
static let one: UInt64 = 1
}
extension UInt32 {
static let one: UInt32 = 1
}
public enum BuilderFlag: UInt8 {
case none = 0
case shareKeys = 1
case shareStrings = 2
case shareKeysAndStrings = 3
case shareKeyVectors = 4
case shareAll = 7
}
extension BuilderFlag: Comparable {
public static func < (lhs: BuilderFlag, rhs: BuilderFlag) -> Bool {
lhs.rawValue < rhs.rawValue
}
}
enum StaticJSON {
static let null = "null"
}
extension Optional {
var valueOrNull: String {
if let value = self {
return "\(value)"
} else {
return StaticJSON.null
}
}
}

View File

@@ -0,0 +1,137 @@
/*
* Copyright 2024 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import Foundation
public struct Value: Equatable {
@usableFromInline
enum Union: Equatable {
case i(Int64)
case u(UInt64)
case f(Double)
}
var sloc: Union
let type: FlexBufferType
let bitWidth: BitWidth
@inline(__always)
private init() {
sloc = .i(0)
type = .null
bitWidth = .w8
}
@inline(__always)
init(bool: Bool) {
sloc = .u(bool ? 1 : 0)
type = .bool
bitWidth = .w8
}
@inline(__always)
init(v: UInt64, type: FlexBufferType, bitWidth: BitWidth) {
sloc = .u(v)
self.type = type
self.bitWidth = bitWidth
}
@inline(__always)
init(v: Int64, type: FlexBufferType, bitWidth: BitWidth) {
sloc = .i(v)
self.type = type
self.bitWidth = bitWidth
}
@inline(__always)
init(v: Double, type: FlexBufferType, bitWidth: BitWidth) {
sloc = .f(v)
self.type = type
self.bitWidth = bitWidth
}
@inline(__always)
init(sloc: Union, type: FlexBufferType, bitWidth: BitWidth) {
self.sloc = sloc
self.type = type
self.bitWidth = bitWidth
}
@usableFromInline
var i: Int64 {
switch sloc {
case .i(let v): v
default: 0
}
}
@usableFromInline
var u: UInt64 {
switch sloc {
case .u(let v): v
default: 0
}
}
@usableFromInline
var f: Double {
switch sloc {
case .f(let v): v
default: 0
}
}
static let `nil` = Value()
}
extension Value {
@usableFromInline
@inline(__always)
func elementWidth(size: Int, index: UInt64) -> BitWidth {
if isInline(type) {
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 = _offsetLoc &+ (index &* numericCast(byteWidth))
let offset = offsetLoc &- u
let bitWidth = widthU(offset)
if (UInt32.one << bitWidth.rawValue) == byteWidth {
return bitWidth
}
}
return .w64
}
}
@inline(__always)
func storedPackedType(width: BitWidth = .w8) -> UInt8 {
packedType(bitWidth: storedWidth(width: width), type: type)
}
@inline(__always)
private func storedWidth(width: BitWidth) -> BitWidth {
if isInline(type) {
return max(bitWidth, width)
} else {
return bitWidth
}
}
}

View File

@@ -0,0 +1,158 @@
/*
* Copyright 2024 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import Foundation
@inline(__always)
internal func isInline(_ t: FlexBufferType) -> Bool {
return t <= .float || t == .bool
}
@inline(__always)
private func check(_ v: UInt64, width: UInt64) -> Bool {
(v & ~((.one << width) &- 1)) == 0
}
@inline(__always)
internal func widthI(_ v: Int64) -> BitWidth {
let u = UInt64(bitPattern: v) << 1
return widthU(v >= 0 ? u : ~u)
}
@inline(__always)
internal func widthF(_ v: Double) -> BitWidth {
Double(Float(v)) == v ? .w32 : .w64
}
@inline(__always)
internal func widthU(_ v: UInt64) -> BitWidth {
if check(v, width: 8) { return .w8 }
if check(v, width: 16) { return .w16 }
if check(v, width: 32) { return .w32 }
return .w64
}
@inline(__always)
internal func packedType(bitWidth: BitWidth, type: FlexBufferType) -> UInt8 {
numericCast(bitWidth.rawValue | (type.rawValue << 2))
}
@inline(__always)
func getScalarType<T>(type: T.Type) -> FlexBufferType where T: Scalar {
if T.self is (any BinaryFloatingPoint.Type) {
return .float
}
if T.self is Bool.Type {
return .bool
}
if T.self is (any UnsignedInteger.Type) {
return .uint
}
return .int
}
@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
}
return FlexBufferType(rawValue: type) ?? .null
}
@inline(__always)
func isTypedVectorElementType(type: FlexBufferType) -> Bool {
return type >= .int && type <= .string || type == .bool
}
@inline(__always)
func isTypedVectorType(type: FlexBufferType) -> Bool {
return type >= .vectorInt && type <= .vectorString || type == .vectorBool
}
@inline(__always)
func toTypedVectorElementType(type: FlexBufferType) -> FlexBufferType? {
return FlexBufferType(
rawValue: type.rawValue &- FlexBufferType.vectorInt
.rawValue &+ FlexBufferType.int.rawValue)
}
@inline(__always)
func isFixedTypedVectorType(type: FlexBufferType) -> Bool {
return type >= .vectorInt2 && type <= .vectorFloat4
}
@inline(__always)
func toFixedTypedVectorElementType(type: FlexBufferType)
-> (type: FlexBufferType?, count: Int)
{
assert(isFixedTypedVectorType(type: type))
let fixedType: UInt64 = numericCast(
type.rawValue &- FlexBufferType.vectorInt2
.rawValue)
let len: Int = numericCast((fixedType / 3) + 2)
return (
FlexBufferType(rawValue: (fixedType % 3) + FlexBufferType.int.rawValue),
len)
}
// MARK: - Reader functions
@inline(__always)
func binarySearch(
vector: TypedVector,
target: String) -> Int?
{
var left = 0
var right = vector.count
while left <= right {
let mid = left &+ (right &- left) / 2
let comp = vector.compare(offset: mid, target: target)
if comp == 0 {
return mid
} else if comp < 0 {
left = mid &+ 1
} else {
right = mid &- 1
}
}
return nil
}
@inline(__always)
func readIndirect(buffer: ByteBuffer, offset: Int, _ byteWidth: UInt8) -> Int {
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))
}