mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-02 04:04:19 +00:00
fix CScript string.compare (#8547)
* 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 * run generate_code.py ---------
This commit is contained in:
@@ -650,12 +650,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
|
||||
@@ -1585,7 +1595,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";
|
||||
|
||||
@@ -521,7 +521,7 @@ public struct Monster : IFlatbufferObject
|
||||
public static VectorOffset CreateSortedVectorOfMonster(FlatBufferBuilder builder, Offset<Monster>[] offsets) {
|
||||
Array.Sort(offsets,
|
||||
(Offset<Monster> o1, Offset<Monster> 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 struct Monster : IFlatbufferObject
|
||||
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) {
|
||||
|
||||
@@ -292,7 +292,7 @@ public struct Collide : IFlatbufferObject
|
||||
public static VectorOffset CreateSortedVectorOfCollide(FlatBufferBuilder builder, Offset<Collide>[] offsets) {
|
||||
Array.Sort(offsets,
|
||||
(Offset<Collide> o1, Offset<Collide> 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 struct Collide : IFlatbufferObject
|
||||
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) {
|
||||
|
||||
Reference in New Issue
Block a user