From f20ef8c0c5080dc3c8796958aa0150549ea41d93 Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 23 Sep 2019 22:57:02 +0300 Subject: [PATCH] Better MessageEntity comparator. GitOrigin-RevId: 7522f05688c5ff4beee86d4218953dfb5f4a2821 --- td/telegram/MessageEntity.cpp | 5 +++++ td/telegram/MessageEntity.h | 18 ++++++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/td/telegram/MessageEntity.cpp b/td/telegram/MessageEntity.cpp index cf852322..3ccdf389 100644 --- a/td/telegram/MessageEntity.cpp +++ b/td/telegram/MessageEntity.cpp @@ -23,6 +23,11 @@ namespace td { +int MessageEntity::get_type_priority(Type type) { + static const int types[] = {5, 5, 5, 5, 5, 9, 9, 2, 1, 1, 5, 5, 5, 5, 9, 9, 0}; + return types[static_cast(type)]; +} + StringBuilder &operator<<(StringBuilder &string_builder, const MessageEntity &message_entity) { bool has_argument = false; string_builder << '['; diff --git a/td/telegram/MessageEntity.h b/td/telegram/MessageEntity.h index 73b2f155..b9f20b6d 100644 --- a/td/telegram/MessageEntity.h +++ b/td/telegram/MessageEntity.h @@ -19,7 +19,6 @@ #include "td/telegram/td_api.h" #include "td/telegram/telegram_api.h" -#include #include #include @@ -28,8 +27,6 @@ namespace td { class ContactsManager; class MessageEntity { - tl_object_ptr get_text_entity_type_object() const; - public: enum class Type : int32 { Mention, @@ -73,7 +70,15 @@ class MessageEntity { } bool operator<(const MessageEntity &other) const { - return std::tie(offset, length, type) < std::tie(other.offset, other.length, other.type); + if (offset != other.offset) { + return offset < other.offset; + } + if (length != other.length) { + return length > other.length; + } + auto priority = get_type_priority(type); + auto other_priority = get_type_priority(other.type); + return priority < other_priority; } bool operator!=(const MessageEntity &rhs) const { @@ -85,6 +90,11 @@ class MessageEntity { template void parse(ParserT &parser); + + private: + tl_object_ptr get_text_entity_type_object() const; + + static int get_type_priority(Type type); }; StringBuilder &operator<<(StringBuilder &string_builder, const MessageEntity &message_entity);