mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-27 19:58:06 +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 {
|
||||
@@ -206,8 +209,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
|
||||
@@ -225,29 +228,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
|
||||
self.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
|
||||
self.capacity = count
|
||||
}
|
||||
#endif
|
||||
|
||||
/// Constructor that creates a Flatbuffer from unsafe memory region without copying
|
||||
@@ -259,8 +262,8 @@ public struct ByteBuffer {
|
||||
@inline(__always)
|
||||
public init(
|
||||
assumingMemoryBound memory: UnsafeMutableRawPointer,
|
||||
capacity: Int)
|
||||
{
|
||||
capacity: Int
|
||||
) {
|
||||
_storage = Storage(
|
||||
blob: .pointer(memory),
|
||||
capacity: capacity)
|
||||
@@ -277,8 +280,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
|
||||
@@ -329,8 +332,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")
|
||||
@@ -352,8 +355,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")
|
||||
@@ -363,46 +366,46 @@ public struct ByteBuffer {
|
||||
}
|
||||
|
||||
#if !os(WASI)
|
||||
/// Reads a string from the buffer and encodes it to a swift string
|
||||
/// - Parameters:
|
||||
/// - index: index of the string in the buffer
|
||||
/// - count: length of the string
|
||||
/// - type: Encoding of the string
|
||||
@inline(__always)
|
||||
public func readString(
|
||||
at index: Int,
|
||||
count: Int,
|
||||
type: String.Encoding = .utf8) -> String?
|
||||
{
|
||||
assert(
|
||||
index + count <= capacity,
|
||||
"Reading out of bounds is illegal")
|
||||
return _storage.readWithUnsafeRawPointer(position: index) {
|
||||
let buf = UnsafeBufferPointer(
|
||||
start: $0.bindMemory(to: UInt8.self, capacity: count),
|
||||
count: count)
|
||||
return String(
|
||||
bytes: buf,
|
||||
encoding: type)
|
||||
/// Reads a string from the buffer and encodes it to a swift string
|
||||
/// - Parameters:
|
||||
/// - index: index of the string in the buffer
|
||||
/// - count: length of the string
|
||||
/// - type: Encoding of the string
|
||||
@inline(__always)
|
||||
public func readString(
|
||||
at index: Int,
|
||||
count: Int,
|
||||
type: String.Encoding = .utf8
|
||||
) -> String? {
|
||||
assert(
|
||||
index + count <= capacity,
|
||||
"Reading out of bounds is illegal")
|
||||
return _storage.readWithUnsafeRawPointer(position: index) {
|
||||
let buf = UnsafeBufferPointer(
|
||||
start: $0.bindMemory(to: UInt8.self, capacity: count),
|
||||
count: count)
|
||||
return String(
|
||||
bytes: buf,
|
||||
encoding: type)
|
||||
}
|
||||
}
|
||||
}
|
||||
#else
|
||||
/// Reads a string from the buffer and encodes it to a swift string
|
||||
/// - Parameters:
|
||||
/// - index: index of the string in the buffer
|
||||
/// - count: length of the string
|
||||
@inline(__always)
|
||||
public func readString(
|
||||
at index: Int,
|
||||
count: Int) -> String?
|
||||
{
|
||||
assert(
|
||||
index + count <= capacity,
|
||||
"Reading out of bounds is illegal")
|
||||
return _storage.readWithUnsafeRawPointer(position: index) {
|
||||
String(cString: $0.bindMemory(to: UInt8.self, capacity: count))
|
||||
/// Reads a string from the buffer and encodes it to a swift string
|
||||
/// - Parameters:
|
||||
/// - index: index of the string in the buffer
|
||||
/// - count: length of the string
|
||||
@inline(__always)
|
||||
public func readString(
|
||||
at index: Int,
|
||||
count: Int
|
||||
) -> String? {
|
||||
assert(
|
||||
index + count <= capacity,
|
||||
"Reading out of bounds is illegal")
|
||||
return _storage.readWithUnsafeRawPointer(position: index) {
|
||||
String(cString: $0.bindMemory(to: UInt8.self, capacity: count))
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/// Creates a new Flatbuffer object that's duplicated from the current one
|
||||
@@ -434,8 +437,8 @@ public struct ByteBuffer {
|
||||
@inline(__always)
|
||||
public func withUnsafeBytes<T>(
|
||||
body: (UnsafeRawBufferPointer) throws
|
||||
-> T) rethrows -> T
|
||||
{
|
||||
-> T
|
||||
) rethrows -> T {
|
||||
try _storage.withUnsafeBytes(body)
|
||||
}
|
||||
|
||||
@@ -443,8 +446,8 @@ public struct ByteBuffer {
|
||||
@inline(__always)
|
||||
func withUnsafeMutableRawPointer<T>(
|
||||
body: (UnsafeMutableRawPointer) throws
|
||||
-> T) rethrows -> T
|
||||
{
|
||||
-> T
|
||||
) rethrows -> T {
|
||||
try _storage.withUnsafeRawPointer(body)
|
||||
}
|
||||
|
||||
@@ -452,8 +455,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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user