From b164dc699bedd09483691a6ed4133aacf4e8d4a3 Mon Sep 17 00:00:00 2001 From: xiangchen02 <18351967270@163.com> Date: Mon, 3 Mar 2025 20:50:44 +0800 Subject: [PATCH 1/2] fix CScript string.compare Because the default compare sorting rules of c# string are different from those of cpp, the binary file exported by flatc -b cannot use LookupByKey --- src/idl_gen_csharp.cpp | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/src/idl_gen_csharp.cpp b/src/idl_gen_csharp.cpp index 9988523f32f..0b644c2abc9 100644 --- a/src/idl_gen_csharp.cpp +++ b/src/idl_gen_csharp.cpp @@ -620,12 +620,22 @@ class CSharpGenerator : public BaseGenerator { std::string GenKeyGetter(flatbuffers::StructDef &struct_def, flatbuffers::FieldDef *key_field) const { // Get the getter for the key of the struct. - return GenGetterForLookupByKey(struct_def, key_field, "builder.DataBuffer", - "builder.DataBuffer.Length - o1.Value") + - ".CompareTo(" + - GenGetterForLookupByKey(struct_def, key_field, "builder.DataBuffer", - "builder.DataBuffer.Length - o2.Value") + - ")"; + if (IsString(key_field->value.type)) { + return "string.CompareOrdinal(" + + GenGetterForLookupByKey(struct_def, key_field, "builder.DataBuffer", + "builder.DataBuffer.Length - o1.Value") + + ", " + + GenGetterForLookupByKey(struct_def, key_field, "builder.DataBuffer", + "builder.DataBuffer.Length - o2.Value") + + ")"; + } else { + return GenGetterForLookupByKey(struct_def, key_field, "builder.DataBuffer", + "builder.DataBuffer.Length - o1.Value") + + ".CompareTo(" + + GenGetterForLookupByKey(struct_def, key_field, "builder.DataBuffer", + "builder.DataBuffer.Length - o2.Value") + + ")"; + } } // Get the value of a table verification function start @@ -1539,7 +1549,11 @@ class CSharpGenerator : public BaseGenerator { "(start + middle), bb);\n"; code += " obj_.__assign(tableOffset, bb);\n"; - code += " int comp = obj_." + name + ".CompareTo(key);\n"; + if (IsString(key_field->value.type)) { + code += " int comp = string.CompareOrdinal(obj_." + name + ", key);\n"; + } else { + code += " int comp = obj_." + name + ".CompareTo(key);\n"; + } code += " if (comp > 0) {\n"; code += " span = middle;\n"; code += " } else if (comp < 0) {\n"; From 03df77399149985c1a95dcb6b8727a1ac919a6d0 Mon Sep 17 00:00:00 2001 From: xiangchen02 <18351967270@163.com> Date: Mon, 3 Mar 2025 21:02:18 +0800 Subject: [PATCH 2/2] run generate_code.py --- tests/MyGame/Example/Monster.cs | 4 ++-- tests/union_value_collsion/union_value_collision_generated.cs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/MyGame/Example/Monster.cs b/tests/MyGame/Example/Monster.cs index 17327798208..46abfd09401 100644 --- a/tests/MyGame/Example/Monster.cs +++ b/tests/MyGame/Example/Monster.cs @@ -521,7 +521,7 @@ public struct Monster : IFlatbufferObject public static VectorOffset CreateSortedVectorOfMonster(FlatBufferBuilder builder, Offset[] offsets) { Array.Sort(offsets, (Offset o1, Offset o2) => - new Monster().__assign(builder.DataBuffer.Length - o1.Value, builder.DataBuffer).Name.CompareTo(new Monster().__assign(builder.DataBuffer.Length - o2.Value, builder.DataBuffer).Name)); + string.CompareOrdinal(new Monster().__assign(builder.DataBuffer.Length - o1.Value, builder.DataBuffer).Name, new Monster().__assign(builder.DataBuffer.Length - o2.Value, builder.DataBuffer).Name)); return builder.CreateVectorOfTables(offsets); } @@ -533,7 +533,7 @@ public static VectorOffset CreateSortedVectorOfMonster(FlatBufferBuilder builder int middle = span / 2; int tableOffset = Table.__indirect(vectorLocation + 4 * (start + middle), bb); obj_.__assign(tableOffset, bb); - int comp = obj_.Name.CompareTo(key); + int comp = string.CompareOrdinal(obj_.Name, key); if (comp > 0) { span = middle; } else if (comp < 0) { diff --git a/tests/union_value_collsion/union_value_collision_generated.cs b/tests/union_value_collsion/union_value_collision_generated.cs index f4686997f31..927183637a8 100644 --- a/tests/union_value_collsion/union_value_collision_generated.cs +++ b/tests/union_value_collsion/union_value_collision_generated.cs @@ -292,7 +292,7 @@ public struct Collide : IFlatbufferObject public static VectorOffset CreateSortedVectorOfCollide(FlatBufferBuilder builder, Offset[] offsets) { Array.Sort(offsets, (Offset o1, Offset o2) => - new Collide().__assign(builder.DataBuffer.Length - o1.Value, builder.DataBuffer).Collide_.CompareTo(new Collide().__assign(builder.DataBuffer.Length - o2.Value, builder.DataBuffer).Collide_)); + string.CompareOrdinal(new Collide().__assign(builder.DataBuffer.Length - o1.Value, builder.DataBuffer).Collide_, new Collide().__assign(builder.DataBuffer.Length - o2.Value, builder.DataBuffer).Collide_)); return builder.CreateVectorOfTables(offsets); } @@ -304,7 +304,7 @@ public static VectorOffset CreateSortedVectorOfCollide(FlatBufferBuilder builder int middle = span / 2; int tableOffset = Table.__indirect(vectorLocation + 4 * (start + middle), bb); obj_.__assign(tableOffset, bb); - int comp = obj_.Collide_.CompareTo(key); + int comp = string.CompareOrdinal(obj_.Collide_, key); if (comp > 0) { span = middle; } else if (comp < 0) {