Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions src/idl_gen_csharp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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";
Expand Down
4 changes: 2 additions & 2 deletions tests/MyGame/Example/Monster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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) {
Expand Down
Loading