diff options
author | vkill <vkill.net@gmail.com> | 2020-01-17 04:08:59 +0800 |
---|---|---|
committer | Wouter van Oortmerssen <aardappel@gmail.com> | 2020-01-16 12:08:59 -0800 |
commit | c4b2b0a25ddf3a6a22515953ec72c0595aff6d88 (patch) | |
tree | 74278df03a3a2e83318576d0c75045ea3f3442be | |
parent | a4b2884e4ed6116335d534af8f58a84678b74a17 (diff) | |
download | flatbuffers-c4b2b0a25ddf3a6a22515953ec72c0595aff6d88.tar.gz flatbuffers-c4b2b0a25ddf3a6a22515953ec72c0595aff6d88.tar.bz2 flatbuffers-c4b2b0a25ddf3a6a22515953ec72c0595aff6d88.zip |
[Swift] Support create long string (#5709)
* [Swift] Support create long string
* [Swift] Move the test case to correct dir
-rw-r--r-- | swift/Sources/FlatBuffers/ByteBuffer.swift | 14 | ||||
-rw-r--r-- | tests/FlatBuffers.Test.Swift/Tests/FlatBuffers.Test.SwiftTests/FlatBuffersTests.swift | 2 |
2 files changed, 9 insertions, 7 deletions
diff --git a/swift/Sources/FlatBuffers/ByteBuffer.swift b/swift/Sources/FlatBuffers/ByteBuffer.swift index 654b265c..b596ce46 100644 --- a/swift/Sources/FlatBuffers/ByteBuffer.swift +++ b/swift/Sources/FlatBuffers/ByteBuffer.swift @@ -69,7 +69,7 @@ public final class ByteBuffer { /// Fills the buffer with padding by adding to the writersize /// - Parameter padding: Amount of padding between two to be serialized objects func fill(padding: UInt32) { - ensureSpace(size: UInt8(padding)) + ensureSpace(size: padding) _writerSize += (MemoryLayout<UInt8>.size * Int(padding)) } @@ -77,7 +77,7 @@ public final class ByteBuffer { /// - Parameter elements: An array of Scalars func push<T: Scalar>(elements: [T]) { let size = elements.count * MemoryLayout<T>.size - ensureSpace(size: UInt8(size)) + ensureSpace(size: UInt32(size)) elements.lazy.reversed().forEach { (s) in push(value: s, len: MemoryLayout.size(ofValue: s)) } @@ -88,7 +88,7 @@ public final class ByteBuffer { /// - value: Pointer to the object in memory /// - size: Size of Value being written to the buffer func push(struct value: UnsafeMutableRawPointer, size: Int) { - ensureSpace(size: UInt8(size)) + ensureSpace(size: UInt32(size)) _memory.advanced(by: writerIndex - size).copyMemory(from: value, byteCount: size) defer { value.deallocate() } _writerSize += size @@ -99,7 +99,7 @@ public final class ByteBuffer { /// - value: Object that will be written to the buffer /// - len: Offset to subtract from the WriterIndex func push<T: Scalar>(value: T, len: Int) { - ensureSpace(size: UInt8(len)) + ensureSpace(size: UInt32(len)) var v = value.convertedEndian memcpy(_memory.advanced(by: writerIndex - len), &v, len) _writerSize += len @@ -109,7 +109,7 @@ public final class ByteBuffer { /// - Parameter str: String that will be added to the buffer /// - Parameter len: length of the string func push(string str: String, len: Int) { - ensureSpace(size: UInt8(len)) + ensureSpace(size: UInt32(len)) if str.utf8.withContiguousStorageIfAvailable({ self.push(bytes: $0, len: len) }) != nil { } else { let utf8View = str.utf8 @@ -149,7 +149,7 @@ public final class ByteBuffer { /// Makes sure that buffer has enouch space for each of the objects that will be written into it /// - Parameter size: size of object @discardableResult - func ensureSpace(size: UInt8) -> UInt8 { + func ensureSpace(size: UInt32) -> UInt32 { if Int(size) + _writerSize > _capacity { reallocate(size) } assert(size < FlatBufferMaxSize, "Buffer can't grow beyond 2 Gigabytes") return size @@ -157,7 +157,7 @@ public final class ByteBuffer { /// Reallocates the buffer incase the object to be written doesnt fit in the current buffer /// - Parameter size: Size of the current object - fileprivate func reallocate(_ size: UInt8) { + fileprivate func reallocate(_ size: UInt32) { let currentWritingIndex = writerIndex while _capacity <= _writerSize + Int(size) { _capacity = _capacity << 1 diff --git a/tests/FlatBuffers.Test.Swift/Tests/FlatBuffers.Test.SwiftTests/FlatBuffersTests.swift b/tests/FlatBuffers.Test.Swift/Tests/FlatBuffers.Test.SwiftTests/FlatBuffersTests.swift index e8e737e2..ffd1f455 100644 --- a/tests/FlatBuffers.Test.Swift/Tests/FlatBuffers.Test.SwiftTests/FlatBuffersTests.swift +++ b/tests/FlatBuffers.Test.Swift/Tests/FlatBuffers.Test.SwiftTests/FlatBuffersTests.swift @@ -22,6 +22,8 @@ final class FlatBuffersTests: XCTestCase { b.clear() XCTAssertEqual(b.create(string: helloWorld).o, 20) XCTAssertEqual(b.create(string: country).o, 32) + b.clear() + XCTAssertEqual(b.create(string: String(repeating: "a", count: 257)).o, 264) } func testStartTable() { |