Add fast path in remove_invalid_entities.

This commit is contained in:
levlam 2021-08-18 21:37:31 +03:00
parent ca9dd55141
commit 5c2441021e
2 changed files with 19 additions and 0 deletions

View File

@ -3657,6 +3657,19 @@ static Result<string> clean_input_string_with_entities(const string &text, vecto
// entities must be sorted by offset and length, but not necessary by type
// returns {last_non_whitespace_pos, last_non_whitespace_utf16_offset}
static std::pair<size_t, int32> remove_invalid_entities(const string &text, vector<MessageEntity> &entities) {
if (entities.empty()) {
// fast path
for (size_t pos = 0; pos < text.size(); pos++) {
auto back_pos = text.size() - pos - 1;
auto c = text[back_pos];
if (c != '\n' && c != ' ') {
return {back_pos, 0 /*unused*/};
}
}
return {text.size(), -1};
}
// check_is_sorted(entities);
vector<MessageEntity *> nested_entities_stack;
size_t current_entity = 0;

View File

@ -733,6 +733,12 @@ TEST(MessageEntities, fix_formatted_text) {
check_fix_formatted_text(str, {}, false, false, false, false);
check_fix_formatted_text(str, {}, false, false, false, true);
check_fix_formatted_text(" aba\n ", {}, " aba\n ", {}, true, true, true, true);
check_fix_formatted_text(" aba\n ", {}, "aba", {}, true, true, true, false);
check_fix_formatted_text(" \n ", {}, "", {}, true, true, true, true);
check_fix_formatted_text(" \n ", {}, "", {}, true, true, true, false);
check_fix_formatted_text(" \n ", {}, false, true, true, false);
str += "a \r\n ";
fixed_str += "a \n ";