From 532567a4757dac1243e3b8d9c13ad25547f24f80 Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 10 Mar 2020 17:22:57 +0300 Subject: [PATCH] Resort entities if needed after rtrim. GitOrigin-RevId: f3124b2dc72e1b7858dde90133c5313025103603 --- td/telegram/MessageEntity.cpp | 5 +++++ test/message_entities.cpp | 3 +++ 2 files changed, 8 insertions(+) diff --git a/td/telegram/MessageEntity.cpp b/td/telegram/MessageEntity.cpp index 702364c5..4ed129a4 100644 --- a/td/telegram/MessageEntity.cpp +++ b/td/telegram/MessageEntity.cpp @@ -3609,12 +3609,17 @@ Status fix_formatted_text(string &text, vector &entities, bool al entities.back().type == MessageEntity::Type::MentionName); entities.pop_back(); } + bool need_sort = false; for (auto &entity : entities) { if (entity.offset + entity.length > last_non_whitespace_utf16_offset + 1) { entity.length = last_non_whitespace_utf16_offset + 1 - entity.offset; + need_sort = true; CHECK(entity.length > 0); } } + if (need_sort) { + std::sort(entities.begin(), entities.end()); + } // ltrim size_t first_non_whitespaces_pos = 0; diff --git a/test/message_entities.cpp b/test/message_entities.cpp index f55508e8..9527ac28 100644 --- a/test/message_entities.cpp +++ b/test/message_entities.cpp @@ -793,6 +793,9 @@ TEST(MessageEntities, fix_formatted_text) { {td::MessageEntity::Type::Italic, 0, 1}, {td::MessageEntity::Type::Bold, 2, 1}, {td::MessageEntity::Type::Italic, 2, 1}}); + check_fix_formatted_text("a ", {{td::MessageEntity::Type::Italic, 0, 2}, {td::MessageEntity::Type::Bold, 0, 1}}, "a", + {{td::MessageEntity::Type::Bold, 0, 1}, {td::MessageEntity::Type::Italic, 0, 1}}, false, + false, false, false); check_fix_formatted_text("abc", {{td::MessageEntity::Type::Italic, 1, 1}, {td::MessageEntity::Type::Italic, 0, 1}}, "abc", {{td::MessageEntity::Type::Italic, 0, 2}}); check_fix_formatted_text("abc", {{td::MessageEntity::Type::Italic, 1, 1}, {td::MessageEntity::Type::Italic, 1, 1}},