Skip to content

Instantly share code, notes, and snippets.

@danlark1
Last active April 3, 2022 13:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danlark1/6f834905bd68496fb3b749ebe27422d9 to your computer and use it in GitHub Desktop.
Save danlark1/6f834905bd68496fb3b749ebe27422d9 to your computer and use it in GitHub Desktop.
proto_sort
struct FieldNumberSorter {
bool operator()(const FieldDescriptor* left,
const FieldDescriptor* right) const {
// Sorting by tag order.
return left->number() < right->number();
}
};
void Reflection::ListFieldsMayFailOnStripped(
const Message& message, bool should_fail,
std::vector<const FieldDescriptor*>* output) const {
// Traverse all fields in their order of declaration.
for (int i = 0; i <= last_non_weak_field_index; i++) {
const FieldDescriptor* field = descriptor_->field(i);
if (FieldSize(message, field) > 0) {
output->push_back(field);
}
}
// Sort by their tag number
std::sort(output->begin(), output->end(), FieldNumberSorter());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment