mirror of
https://github.com/google/flatbuffers.git
synced 2026-07-03 10:22:27 +00:00
[Feature] Checks for Nullable strings (#6050)
* Allows null strings in createString method c# * Adds nullable strings to JS and swift * Changes js checks * Fixes typo
This commit is contained in:
@@ -252,6 +252,7 @@ flatbuffers.Builder.prototype.clear = function() {
|
|||||||
this.vtables = [];
|
this.vtables = [];
|
||||||
this.vector_num_elems = 0;
|
this.vector_num_elems = 0;
|
||||||
this.force_defaults = false;
|
this.force_defaults = false;
|
||||||
|
this.string_maps = null;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -773,6 +774,28 @@ flatbuffers.Builder.prototype.endVector = function() {
|
|||||||
};
|
};
|
||||||
/// @endcond
|
/// @endcond
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encode the string `s` in the buffer using UTF-8. If the string passed has
|
||||||
|
* already been seen, we return the offset of the already written string
|
||||||
|
*
|
||||||
|
* @param {string|Uint8Array} s The string to encode
|
||||||
|
* @return {flatbuffers.Offset} The offset in the buffer where the encoded string starts
|
||||||
|
*/
|
||||||
|
flatbuffers.Builder.prototype.createSharedString = function(s) {
|
||||||
|
if (!s) { return 0 }
|
||||||
|
|
||||||
|
if (!this.string_maps) {
|
||||||
|
this.string_maps = new Map();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.string_maps.has(s)) {
|
||||||
|
return this.string_maps.get(s)
|
||||||
|
}
|
||||||
|
let offset = this.createString(s)
|
||||||
|
this.string_maps.set(s, offset)
|
||||||
|
return offset
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Encode the string `s` in the buffer using UTF-8. If a Uint8Array is passed
|
* Encode the string `s` in the buffer using UTF-8. If a Uint8Array is passed
|
||||||
* instead of a string, it is assumed to contain valid UTF-8 encoded data.
|
* instead of a string, it is assumed to contain valid UTF-8 encoded data.
|
||||||
@@ -781,6 +804,7 @@ flatbuffers.Builder.prototype.endVector = function() {
|
|||||||
* @return {flatbuffers.Offset} The offset in the buffer where the encoded string starts
|
* @return {flatbuffers.Offset} The offset in the buffer where the encoded string starts
|
||||||
*/
|
*/
|
||||||
flatbuffers.Builder.prototype.createString = function(s) {
|
flatbuffers.Builder.prototype.createString = function(s) {
|
||||||
|
if (!s) { return 0 }
|
||||||
if (s instanceof Uint8Array) {
|
if (s instanceof Uint8Array) {
|
||||||
var utf8 = s;
|
var utf8 = s;
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -554,6 +554,10 @@ namespace FlatBuffers
|
|||||||
/// </returns>
|
/// </returns>
|
||||||
public StringOffset CreateString(string s)
|
public StringOffset CreateString(string s)
|
||||||
{
|
{
|
||||||
|
if (s == null)
|
||||||
|
{
|
||||||
|
return new StringOffset(0);
|
||||||
|
}
|
||||||
NotNested();
|
NotNested();
|
||||||
AddByte(0);
|
AddByte(0);
|
||||||
var utf8StringLen = Encoding.UTF8.GetByteCount(s);
|
var utf8StringLen = Encoding.UTF8.GetByteCount(s);
|
||||||
@@ -594,6 +598,11 @@ namespace FlatBuffers
|
|||||||
/// </returns>
|
/// </returns>
|
||||||
public StringOffset CreateSharedString(string s)
|
public StringOffset CreateSharedString(string s)
|
||||||
{
|
{
|
||||||
|
if (s == null)
|
||||||
|
{
|
||||||
|
return new StringOffset(0);
|
||||||
|
}
|
||||||
|
|
||||||
if (_sharedStringMap == null)
|
if (_sharedStringMap == null)
|
||||||
{
|
{
|
||||||
_sharedStringMap = new Dictionary<string, StringOffset>();
|
_sharedStringMap = new Dictionary<string, StringOffset>();
|
||||||
|
|||||||
@@ -385,7 +385,8 @@ public struct FlatBufferBuilder {
|
|||||||
/// Insets a string into the buffer using UTF8
|
/// Insets a string into the buffer using UTF8
|
||||||
/// - Parameter str: String to be serialized
|
/// - Parameter str: String to be serialized
|
||||||
/// - returns: The strings offset in the buffer
|
/// - returns: The strings offset in the buffer
|
||||||
mutating public func create(string str: String) -> Offset<String> {
|
mutating public func create(string str: String?) -> Offset<String> {
|
||||||
|
guard let str = str else { return Offset() }
|
||||||
let len = str.utf8.count
|
let len = str.utf8.count
|
||||||
notNested()
|
notNested()
|
||||||
preAlign(len: len &+ 1, type: UOffset.self)
|
preAlign(len: len &+ 1, type: UOffset.self)
|
||||||
@@ -395,20 +396,13 @@ public struct FlatBufferBuilder {
|
|||||||
return Offset(offset: _bb.size)
|
return Offset(offset: _bb.size)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Insets a string into the buffer using UTF8
|
|
||||||
/// - Parameter str: String to be serialized
|
|
||||||
/// - returns: The strings offset in the buffer
|
|
||||||
mutating public func create(string str: String?) -> Offset<String> {
|
|
||||||
guard let str = str else { return Offset() }
|
|
||||||
return create(string: str)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Inserts a shared string to the buffer
|
/// Inserts a shared string to the buffer
|
||||||
///
|
///
|
||||||
/// The function checks the stringOffsetmap if it's seen a similar string before
|
/// The function checks the stringOffsetmap if it's seen a similar string before
|
||||||
/// - Parameter str: String to be serialized
|
/// - Parameter str: String to be serialized
|
||||||
/// - returns: The strings offset in the buffer
|
/// - returns: The strings offset in the buffer
|
||||||
mutating public func createShared(string str: String) -> Offset<String> {
|
mutating public func createShared(string str: String?) -> Offset<String> {
|
||||||
|
guard let str = str else { return Offset() }
|
||||||
if let offset = stringOffsetMap[str] {
|
if let offset = stringOffsetMap[str] {
|
||||||
return offset
|
return offset
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -60,6 +60,12 @@ final class FlatBuffersTests: XCTestCase {
|
|||||||
XCTAssertEqual(c.name, country)
|
XCTAssertEqual(c.name, country)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func testWriteNullableStrings() {
|
||||||
|
var b = FlatBufferBuilder()
|
||||||
|
XCTAssertTrue(b.create(string: nil).isEmpty)
|
||||||
|
XCTAssertTrue(b.createShared(string: nil).isEmpty)
|
||||||
|
}
|
||||||
|
|
||||||
func testWriteOptionalValues() {
|
func testWriteOptionalValues() {
|
||||||
var b = FlatBufferBuilder()
|
var b = FlatBufferBuilder()
|
||||||
let root = optional_scalars_ScalarStuff.createScalarStuff(&b,
|
let root = optional_scalars_ScalarStuff.createScalarStuff(&b,
|
||||||
@@ -74,7 +80,6 @@ final class FlatBuffersTests: XCTestCase {
|
|||||||
XCTAssertEqual(scalarTable.defaultI8, 42)
|
XCTAssertEqual(scalarTable.defaultI8, 42)
|
||||||
XCTAssertEqual(scalarTable.justU8, 100)
|
XCTAssertEqual(scalarTable.justU8, 100)
|
||||||
XCTAssertEqual(scalarTable.maybeU8, 10)
|
XCTAssertEqual(scalarTable.maybeU8, 10)
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -350,5 +350,14 @@ namespace FlatBuffers.Test
|
|||||||
var endOffset = fbb.Offset;
|
var endOffset = fbb.Offset;
|
||||||
Assert.AreEqual(endOffset, storedOffset);
|
Assert.AreEqual(endOffset, storedOffset);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[FlatBuffersTestMethod]
|
||||||
|
public void FlatBufferBuilder_Add_null_String()
|
||||||
|
{
|
||||||
|
var fbb = new FlatBufferBuilder(16);
|
||||||
|
string s = null;
|
||||||
|
Assert.AreEqual(fbb.CreateSharedString(s).Value, 0);
|
||||||
|
Assert.AreEqual(fbb.CreateString(s).Value, 0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,6 +50,8 @@ function main() {
|
|||||||
test64bit();
|
test64bit();
|
||||||
testUnicode();
|
testUnicode();
|
||||||
fuzzTest1();
|
fuzzTest1();
|
||||||
|
testNullStrings();
|
||||||
|
testSharedStrings();
|
||||||
|
|
||||||
console.log('FlatBuffers test: completed successfully');
|
console.log('FlatBuffers test: completed successfully');
|
||||||
}
|
}
|
||||||
@@ -437,4 +439,19 @@ function fuzzTest1() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function testSharedStrings() {
|
||||||
|
var shared_string = "Hello world";
|
||||||
|
var builder = new flatbuffers.Builder();
|
||||||
|
let mainOffset = builder.createSharedString(shared_string);
|
||||||
|
assert.strictEqual(builder.createSharedString(shared_string), mainOffset);
|
||||||
|
}
|
||||||
|
|
||||||
|
function testNullStrings() {
|
||||||
|
var builder = new flatbuffers.Builder();
|
||||||
|
assert.strictEqual(builder.createString(null), 0);
|
||||||
|
assert.strictEqual(builder.createSharedString(null), 0);
|
||||||
|
assert.strictEqual(builder.createString(undefined), 0);
|
||||||
|
assert.strictEqual(builder.createSharedString(undefined), 0);
|
||||||
|
}
|
||||||
|
|
||||||
main();
|
main();
|
||||||
|
|||||||
Reference in New Issue
Block a user