xref: /aosp_15_r20/external/flatbuffers/swift/Sources/FlatBuffers/FlatBuffersUtils.swift (revision 890232f25432b36107d06881e0a25aaa6b473652)
1*890232f2SAndroid Build Coastguard Worker /*
2*890232f2SAndroid Build Coastguard Worker  * Copyright 2021 Google Inc. All rights reserved.
3*890232f2SAndroid Build Coastguard Worker  *
4*890232f2SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*890232f2SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*890232f2SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*890232f2SAndroid Build Coastguard Worker  *
8*890232f2SAndroid Build Coastguard Worker  *     http://www.apache.org/licenses/LICENSE-2.0
9*890232f2SAndroid Build Coastguard Worker  *
10*890232f2SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*890232f2SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*890232f2SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*890232f2SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*890232f2SAndroid Build Coastguard Worker  * limitations under the License.
15*890232f2SAndroid Build Coastguard Worker  */
16*890232f2SAndroid Build Coastguard Worker 
17*890232f2SAndroid Build Coastguard Worker #if !os(WASI)
18*890232f2SAndroid Build Coastguard Worker import Foundation
19*890232f2SAndroid Build Coastguard Worker #else
20*890232f2SAndroid Build Coastguard Worker import SwiftOverlayShims
21*890232f2SAndroid Build Coastguard Worker #endif
22*890232f2SAndroid Build Coastguard Worker 
23*890232f2SAndroid Build Coastguard Worker /// FlatBuffersUtils hosts some utility functions that might be useful
24*890232f2SAndroid Build Coastguard Worker public enum FlatBuffersUtils {
25*890232f2SAndroid Build Coastguard Worker 
26*890232f2SAndroid Build Coastguard Worker   /// Gets the size of the prefix
27*890232f2SAndroid Build Coastguard Worker   /// - Parameter bb: Flatbuffer object
getSizePrefixnull28*890232f2SAndroid Build Coastguard Worker   public static func getSizePrefix(bb: ByteBuffer) -> Int32 {
29*890232f2SAndroid Build Coastguard Worker     bb.read(def: Int32.self, position: bb.reader)
30*890232f2SAndroid Build Coastguard Worker   }
31*890232f2SAndroid Build Coastguard Worker 
32*890232f2SAndroid Build Coastguard Worker   /// Removes the prefix by duplicating the Flatbuffer this call is expensive since its
33*890232f2SAndroid Build Coastguard Worker   /// creates a new buffer use `readPrefixedSizeCheckedRoot` instead
34*890232f2SAndroid Build Coastguard Worker   /// unless a completely new buffer is required
35*890232f2SAndroid Build Coastguard Worker   /// - Parameter bb: Flatbuffer object
36*890232f2SAndroid Build Coastguard Worker   ///
37*890232f2SAndroid Build Coastguard Worker   ///
removeSizePrefixnull38*890232f2SAndroid Build Coastguard Worker   public static func removeSizePrefix(bb: ByteBuffer) -> ByteBuffer {
39*890232f2SAndroid Build Coastguard Worker     bb.duplicate(removing: MemoryLayout<Int32>.size)
40*890232f2SAndroid Build Coastguard Worker   }
41*890232f2SAndroid Build Coastguard Worker }
42