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:
whiteye
2025-12-01 11:06:41 +08:00
committed by GitHub
parent 7dd38fa23a
commit 97d26ab4ae
3 changed files with 25 additions and 11 deletions

View File

@@ -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";