Ignore empty entities loaded from database.

This commit is contained in:
levlam 2022-07-31 00:34:30 +03:00
parent 3047262889
commit 2c9e1767dd
4 changed files with 12 additions and 4 deletions

View File

@ -1137,6 +1137,7 @@ static void parse_caption(FormattedText &caption, ParserT &parser) {
parse(caption.text, parser); parse(caption.text, parser);
if (parser.version() >= static_cast<int32>(Version::AddCaptionEntities)) { if (parser.version() >= static_cast<int32>(Version::AddCaptionEntities)) {
parse(caption.entities, parser); parse(caption.entities, parser);
remove_empty_entities(caption.entities);
} else { } else {
if (!check_utf8(caption.text)) { if (!check_utf8(caption.text)) {
caption.text.clear(); caption.text.clear();

View File

@ -1405,6 +1405,10 @@ vector<std::pair<Slice, int32>> find_media_timestamps(Slice str) {
return result; return result;
} }
void remove_empty_entities(vector<MessageEntity> &entities) {
td::remove_if(entities, [](const auto &entity) { return entity.length == 0; });
}
static int32 text_length(Slice text) { static int32 text_length(Slice text) {
return narrow_cast<int32>(utf8_utf16_length(text)); return narrow_cast<int32>(utf8_utf16_length(text));
} }
@ -2566,7 +2570,7 @@ static FormattedText parse_markdown_v3_without_pre(Slice text, vector<MessageEnt
CHECK(entity.offset + entity.length <= utf16_offset); CHECK(entity.offset + entity.length <= utf16_offset);
} }
td::remove_if(entities, [](const auto &entity) { return entity.length == 0; }); remove_empty_entities(entities);
sort_entities(entities); sort_entities(entities);
return {std::move(new_text), std::move(entities)}; return {std::move(new_text), std::move(entities)};
@ -3859,7 +3863,7 @@ static std::pair<size_t, int32> remove_invalid_entities(const string &text, vect
int32 utf16_offset = 0; int32 utf16_offset = 0;
int32 last_non_whitespace_utf16_offset = -1; int32 last_non_whitespace_utf16_offset = -1;
td::remove_if(entities, [](const auto &entity) { return entity.length == 0; }); remove_empty_entities(entities);
for (size_t pos = 0; pos <= text.size(); pos++) { for (size_t pos = 0; pos <= text.size(); pos++) {
while (!nested_entities_stack.empty()) { while (!nested_entities_stack.empty()) {
@ -3924,7 +3928,7 @@ static std::pair<size_t, int32> remove_invalid_entities(const string &text, vect
CHECK(nested_entities_stack.empty()); CHECK(nested_entities_stack.empty());
CHECK(current_entity == entities.size()); CHECK(current_entity == entities.size());
td::remove_if(entities, [](const auto &entity) { return entity.length == 0; }); remove_empty_entities(entities);
return {last_non_whitespace_pos, last_non_whitespace_utf16_offset}; return {last_non_whitespace_pos, last_non_whitespace_utf16_offset};
} }
@ -4114,7 +4118,7 @@ Status fix_formatted_text(string &text, vector<MessageEntity> &entities, bool al
return Status::Error(400, PSLICE() << "Receive an entity with incorrect length " << entity.length); return Status::Error(400, PSLICE() << "Receive an entity with incorrect length " << entity.length);
} }
} }
td::remove_if(entities, [](const MessageEntity &entity) { return entity.length == 0; }); remove_empty_entities(entities);
fix_entities(entities); fix_entities(entities);

View File

@ -168,6 +168,8 @@ bool is_email_address(Slice str);
vector<std::pair<Slice, bool>> find_urls(Slice str); // slice + is_email_address vector<std::pair<Slice, bool>> find_urls(Slice str); // slice + is_email_address
vector<std::pair<Slice, int32>> find_media_timestamps(Slice str); // slice + media_timestamp vector<std::pair<Slice, int32>> find_media_timestamps(Slice str); // slice + media_timestamp
void remove_empty_entities(vector<MessageEntity> &entities);
string get_first_url(Slice text, const vector<MessageEntity> &entities); string get_first_url(Slice text, const vector<MessageEntity> &entities);
Result<vector<MessageEntity>> parse_markdown(string &text); Result<vector<MessageEntity>> parse_markdown(string &text);

View File

@ -62,6 +62,7 @@ template <class ParserT>
void FormattedText::parse(ParserT &parser) { void FormattedText::parse(ParserT &parser) {
td::parse(text, parser); td::parse(text, parser);
td::parse(entities, parser); td::parse(entities, parser);
remove_empty_entities(entities);
} }
} // namespace td } // namespace td