Squashed 'third_party/flatbuffers/' changes from d6a8dbd26..338393f85
338393f85 Documentation updates for Optional Scalars (#6014) (#6270)
c27bc2d76 [C++] Add ParseJson(), Parser(Parser&&), update fuzzers (#6284)
bc518a512 Fixed FlexBufferBuilder asserting on duplicate keys
100c59054 Added a few more paths for auto labeler (#6281)
e58c18244 Add --require-explicit-ids to require explicit ids (#6277)
69a8b2a57 idl_gen_json_schema.cpp: Changed generation of array element types (#6253)
25eba6f35 fix typo (#6280)
e1f0f75ba Updated Ms build Action to fix build issue (#6279)
faeb04fbe Add type annotation to unspecified array (#6264)
537212afe [Swift] Adds a format file and reformats the swift project (#6250)
6764f25d9 Adds a fix for enum generation (#6263)
Change-Id: I716bd4d2521fb0a673e50a699cef761e042052b2
git-subtree-dir: third_party/flatbuffers
git-subtree-split: 338393f854eb5ba24761a22cd9316ff5cee4eab0
diff --git a/swift/Sources/FlatBuffers/Table.swift b/swift/Sources/FlatBuffers/Table.swift
index 0f783bf..451398c 100644
--- a/swift/Sources/FlatBuffers/Table.swift
+++ b/swift/Sources/FlatBuffers/Table.swift
@@ -1,144 +1,166 @@
+/*
+ * Copyright 2020 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 Table {
- public private(set) var bb: ByteBuffer
- public private(set) var postion: Int32
-
- public init(bb: ByteBuffer, position: Int32 = 0) {
- guard isLitteEndian else {
- fatalError("Reading/Writing a buffer in big endian machine is not supported on swift")
- }
- self.bb = bb
- self.postion = position
- }
-
- public func offset(_ o: Int32) -> Int32 {
- let vtable = postion - bb.read(def: Int32.self, position: Int(postion))
- return o < bb.read(def: VOffset.self, position: Int(vtable)) ? Int32(bb.read(def: Int16.self, position: Int(vtable + o))) : 0
- }
-
- public func indirect(_ o: Int32) -> Int32 { return o + bb.read(def: Int32.self, position: Int(o)) }
+ public private(set) var bb: ByteBuffer
+ public private(set) var postion: Int32
- /// String reads from the buffer with respect to position of the current table.
- /// - Parameter offset: Offset of the string
- public func string(at offset: Int32) -> String? {
- return directString(at: offset + postion)
+ public init(bb: ByteBuffer, position: Int32 = 0) {
+ guard isLitteEndian else {
+ fatalError("Reading/Writing a buffer in big endian machine is not supported on swift")
}
-
- /// Direct string reads from the buffer disregarding the position of the table.
- /// It would be preferable to use string unless the current position of the table is not needed
- /// - Parameter offset: Offset of the string
- public func directString(at offset: Int32) -> String? {
- var offset = offset
- offset += bb.read(def: Int32.self, position: Int(offset))
- let count = bb.read(def: Int32.self, position: Int(offset))
- let position = offset + Int32(MemoryLayout<Int32>.size)
- return bb.readString(at: position, count: count)
- }
-
- /// Reads from the buffer with respect to the position in the table.
- /// - Parameters:
- /// - type: Type of Scalar that needs to be read from the buffer
- /// - o: Offset of the Element
- public func readBuffer<T: Scalar>(of type: T.Type, at o: Int32) -> T {
- return directRead(of: T.self, offset: o + postion)
- }
-
- /// Reads from the buffer disregarding the position of the table.
- /// It would be used when reading from an
- /// ```
- /// let offset = __t.offset(10)
- /// //Only used when the we already know what is the
- /// // position in the table since __t.vector(at:)
- /// // returns the index with respect to the position
- /// __t.directRead(of: Byte.self,
- /// offset: __t.vector(at: offset) + index * 1)
- /// ```
- /// - Parameters:
- /// - type: Type of Scalar that needs to be read from the buffer
- /// - o: Offset of the Element
- public func directRead<T: Scalar>(of type: T.Type, offset o: Int32) -> T {
- let r = bb.read(def: T.self, position: Int(o))
- return r
- }
-
- public func union<T: FlatBufferObject>(_ o: Int32) -> T {
- let o = o + postion
- return directUnion(o)
- }
+ self.bb = bb
+ postion = position
+ }
- public func directUnion<T: FlatBufferObject>(_ o: Int32) -> T {
- return T.init(bb, o: o + bb.read(def: Int32.self, position: Int(o)))
- }
-
- public func getVector<T>(at off: Int32) -> [T]? {
- let o = offset(off)
- guard o != 0 else { return nil }
- return bb.readSlice(index: vector(at: o), count: vector(count: o))
- }
-
- /// Vector count gets the count of Elements within the array
- /// - Parameter o: start offset of the vector
- /// - returns: Count of elements
- public func vector(count o: Int32) -> Int32 {
- var o = o
- o += postion
- o += bb.read(def: Int32.self, position: Int(o))
- return bb.read(def: Int32.self, position: Int(o))
- }
-
- /// Vector start index in the buffer
- /// - Parameter o:start offset of the vector
- /// - returns: the start index of the vector
- public func vector(at o: Int32) -> Int32 {
- var o = o
- o += postion
- return o + bb.read(def: Int32.self, position: Int(o)) + 4
- }
+ public func offset(_ o: Int32) -> Int32 {
+ let vtable = postion - bb.read(def: Int32.self, position: Int(postion))
+ return o < bb.read(def: VOffset.self, position: Int(vtable)) ? Int32(bb.read(
+ def: Int16.self,
+ position: Int(vtable + o))) : 0
+ }
+
+ public func indirect(_ o: Int32) -> Int32 { o + bb.read(def: Int32.self, position: Int(o)) }
+
+ /// String reads from the buffer with respect to position of the current table.
+ /// - Parameter offset: Offset of the string
+ public func string(at offset: Int32) -> String? {
+ directString(at: offset + postion)
+ }
+
+ /// Direct string reads from the buffer disregarding the position of the table.
+ /// It would be preferable to use string unless the current position of the table is not needed
+ /// - Parameter offset: Offset of the string
+ public func directString(at offset: Int32) -> String? {
+ var offset = offset
+ offset += bb.read(def: Int32.self, position: Int(offset))
+ let count = bb.read(def: Int32.self, position: Int(offset))
+ let position = offset + Int32(MemoryLayout<Int32>.size)
+ return bb.readString(at: position, count: count)
+ }
+
+ /// Reads from the buffer with respect to the position in the table.
+ /// - Parameters:
+ /// - type: Type of Scalar that needs to be read from the buffer
+ /// - o: Offset of the Element
+ public func readBuffer<T: Scalar>(of type: T.Type, at o: Int32) -> T {
+ directRead(of: T.self, offset: o + postion)
+ }
+
+ /// Reads from the buffer disregarding the position of the table.
+ /// It would be used when reading from an
+ /// ```
+ /// let offset = __t.offset(10)
+ /// //Only used when the we already know what is the
+ /// // position in the table since __t.vector(at:)
+ /// // returns the index with respect to the position
+ /// __t.directRead(of: Byte.self,
+ /// offset: __t.vector(at: offset) + index * 1)
+ /// ```
+ /// - Parameters:
+ /// - type: Type of Scalar that needs to be read from the buffer
+ /// - o: Offset of the Element
+ public func directRead<T: Scalar>(of type: T.Type, offset o: Int32) -> T {
+ let r = bb.read(def: T.self, position: Int(o))
+ return r
+ }
+
+ public func union<T: FlatBufferObject>(_ o: Int32) -> T {
+ let o = o + postion
+ return directUnion(o)
+ }
+
+ public func directUnion<T: FlatBufferObject>(_ o: Int32) -> T {
+ T.init(bb, o: o + bb.read(def: Int32.self, position: Int(o)))
+ }
+
+ public func getVector<T>(at off: Int32) -> [T]? {
+ let o = offset(off)
+ guard o != 0 else { return nil }
+ return bb.readSlice(index: vector(at: o), count: vector(count: o))
+ }
+
+ /// Vector count gets the count of Elements within the array
+ /// - Parameter o: start offset of the vector
+ /// - returns: Count of elements
+ public func vector(count o: Int32) -> Int32 {
+ var o = o
+ o += postion
+ o += bb.read(def: Int32.self, position: Int(o))
+ return bb.read(def: Int32.self, position: Int(o))
+ }
+
+ /// Vector start index in the buffer
+ /// - Parameter o:start offset of the vector
+ /// - returns: the start index of the vector
+ public func vector(at o: Int32) -> Int32 {
+ var o = o
+ o += postion
+ return o + bb.read(def: Int32.self, position: Int(o)) + 4
+ }
}
extension Table {
-
- static public func indirect(_ o: Int32, _ fbb: ByteBuffer) -> Int32 { return o + fbb.read(def: Int32.self, position: Int(o)) }
-
- static public func offset(_ o: Int32, vOffset: Int32, fbb: ByteBuffer) -> Int32 {
- let vTable = Int32(fbb.capacity) - o
- return vTable + Int32(fbb.read(def: Int16.self, position: Int(vTable + vOffset - fbb.read(def: Int32.self, position: Int(vTable)))))
+
+ static public func indirect(_ o: Int32, _ fbb: ByteBuffer) -> Int32 { o + fbb.read(
+ def: Int32.self,
+ position: Int(o)) }
+
+ static public func offset(_ o: Int32, vOffset: Int32, fbb: ByteBuffer) -> Int32 {
+ let vTable = Int32(fbb.capacity) - o
+ return vTable + Int32(fbb.read(
+ def: Int16.self,
+ position: Int(vTable + vOffset - fbb.read(def: Int32.self, position: Int(vTable)))))
+ }
+
+ static public func compare(_ off1: Int32, _ off2: Int32, fbb: ByteBuffer) -> Int32 {
+ let memorySize = Int32(MemoryLayout<Int32>.size)
+ let _off1 = off1 + fbb.read(def: Int32.self, position: Int(off1))
+ let _off2 = off2 + fbb.read(def: Int32.self, position: Int(off2))
+ let len1 = fbb.read(def: Int32.self, position: Int(_off1))
+ let len2 = fbb.read(def: Int32.self, position: Int(_off2))
+ let startPos1 = _off1 + memorySize
+ let startPos2 = _off2 + memorySize
+ let minValue = min(len1, len2)
+ for i in 0...minValue {
+ let b1 = fbb.read(def: Int8.self, position: Int(i + startPos1))
+ let b2 = fbb.read(def: Int8.self, position: Int(i + startPos2))
+ if b1 != b2 {
+ return Int32(b2 - b1)
+ }
}
-
- static public func compare(_ off1: Int32, _ off2: Int32, fbb: ByteBuffer) -> Int32 {
- let memorySize = Int32(MemoryLayout<Int32>.size)
- let _off1 = off1 + fbb.read(def: Int32.self, position: Int(off1))
- let _off2 = off2 + fbb.read(def: Int32.self, position: Int(off2))
- let len1 = fbb.read(def: Int32.self, position: Int(_off1))
- let len2 = fbb.read(def: Int32.self, position: Int(_off2))
- let startPos1 = _off1 + memorySize
- let startPos2 = _off2 + memorySize
- let minValue = min(len1, len2)
- for i in 0...minValue {
- let b1 = fbb.read(def: Int8.self, position: Int(i + startPos1))
- let b2 = fbb.read(def: Int8.self, position: Int(i + startPos2))
- if b1 != b2 {
- return Int32(b2 - b1)
- }
- }
- return len1 - len2
+ return len1 - len2
+ }
+
+ static public func compare(_ off1: Int32, _ key: [Byte], fbb: ByteBuffer) -> Int32 {
+ let memorySize = Int32(MemoryLayout<Int32>.size)
+ let _off1 = off1 + fbb.read(def: Int32.self, position: Int(off1))
+ let len1 = fbb.read(def: Int32.self, position: Int(_off1))
+ let len2 = Int32(key.count)
+ let startPos1 = _off1 + memorySize
+ let minValue = min(len1, len2)
+ for i in 0..<minValue {
+ let b = fbb.read(def: Int8.self, position: Int(i + startPos1))
+ let byte = key[Int(i)]
+ if b != byte {
+ return Int32(b - Int8(byte))
+ }
}
-
- static public func compare(_ off1: Int32, _ key: [Byte], fbb: ByteBuffer) -> Int32 {
- let memorySize = Int32(MemoryLayout<Int32>.size)
- let _off1 = off1 + fbb.read(def: Int32.self, position: Int(off1))
- let len1 = fbb.read(def: Int32.self, position: Int(_off1))
- let len2 = Int32(key.count)
- let startPos1 = _off1 + memorySize
- let minValue = min(len1, len2)
- for i in 0..<minValue {
- let b = fbb.read(def: Int8.self, position: Int(i + startPos1))
- let byte = key[Int(i)]
- if b != byte {
- return Int32(b - Int8(byte))
- }
- }
- return len1 - len2
- }
+ return len1 - len2
+ }
}