Optimize remove_emoji_modifiers usages.

This commit is contained in:
levlam 2021-09-20 13:45:49 +03:00
parent 057cee8674
commit 53f9b0438e
8 changed files with 38 additions and 31 deletions

View File

@ -20,7 +20,7 @@ bool DialogAction::is_valid_emoji(string &emoji) {
if (!clean_input_string(emoji)) {
return false;
}
emoji = remove_emoji_modifiers(emoji);
remove_emoji_modifiers_in_place(emoji);
if (emoji.empty()) {
return false;
}

View File

@ -401,8 +401,9 @@ void DialogFilter::init_icon_names() {
"Mask", "Party", "Sport", "Study", "Trade", "Travel", "Work"};
CHECK(emojis.size() == icon_names.size());
for (size_t i = 0; i < emojis.size(); i++) {
emoji_to_icon_name_[remove_emoji_modifiers(emojis[i])] = icon_names[i];
icon_name_to_emoji_[icon_names[i]] = remove_emoji_modifiers(emojis[i]);
remove_emoji_modifiers_in_place(emojis[i]);
emoji_to_icon_name_[emojis[i]] = icon_names[i];
icon_name_to_emoji_[icon_names[i]] = emojis[i];
}
return true;
}();

View File

@ -656,9 +656,8 @@ class MessageDice final : public MessageContent {
static constexpr const char *DEFAULT_EMOJI = "🎲";
MessageDice() = default;
MessageDice(string emoji, int32 dice_value)
: emoji(emoji.empty() ? string(DEFAULT_EMOJI) : remove_emoji_modifiers(std::move(emoji)))
, dice_value(dice_value) {
MessageDice(const string &emoji, int32 dice_value)
: emoji(emoji.empty() ? string(DEFAULT_EMOJI) : remove_emoji_modifiers(emoji).str()), dice_value(dice_value) {
}
MessageContentType get_type() const final {
@ -1398,9 +1397,8 @@ static void parse(unique_ptr<MessageContent> &content, ParserT &parser) {
case MessageContentType::Dice: {
auto m = make_unique<MessageDice>();
if (parser.version() >= static_cast<int32>(Version::AddDiceEmoji)) {
string emoji;
parse(emoji, parser);
m->emoji = remove_emoji_modifiers(std::move(emoji));
parse(m->emoji, parser);
remove_emoji_modifiers_in_place(m->emoji);
} else {
m->emoji = MessageDice::DEFAULT_EMOJI;
}
@ -1677,7 +1675,7 @@ static Result<InputMessageContent> create_input_message_content(
if (!clean_input_string(input_dice->emoji_)) {
return Status::Error(400, "Dice emoji must be encoded in UTF-8");
}
content = td::make_unique<MessageDice>(std::move(input_dice->emoji_), 0);
content = td::make_unique<MessageDice>(input_dice->emoji_, 0);
clear_draft = input_dice->clear_draft_;
break;
}

View File

@ -1405,9 +1405,8 @@ void StickersManager::on_load_special_sticker_set(const SpecialStickerSetType &t
auto pending_get_requests = std::move(pending_get_animated_emoji_click_stickers_);
reset_to_empty(pending_get_animated_emoji_click_stickers_);
for (auto &pending_request : pending_get_requests) {
choose_animated_emoji_click_sticker(sticker_set, std::move(pending_request.message_text_),
pending_request.full_message_id_, pending_request.start_time_,
std::move(pending_request.promise_));
choose_animated_emoji_click_sticker(sticker_set, pending_request.message_text_, pending_request.full_message_id_,
pending_request.start_time_, std::move(pending_request.promise_));
}
auto pending_click_requests = std::move(pending_on_animated_emoji_message_clicked_);
reset_to_empty(pending_on_animated_emoji_message_clicked_);
@ -2787,7 +2786,7 @@ StickerSetId StickersManager::on_get_messages_sticker_set(StickerSetId sticker_s
stickers.push_back(it->second);
s->sticker_emojis_map_[it->second].push_back(pack->emoticon_);
}
auto &sticker_ids = s->emoji_stickers_map_[remove_emoji_modifiers(pack->emoticon_)];
auto &sticker_ids = s->emoji_stickers_map_[remove_emoji_modifiers(pack->emoticon_).str()];
for (auto sticker_id : stickers) {
if (!td::contains(sticker_ids, sticker_id)) {
sticker_ids.push_back(sticker_id);
@ -2988,7 +2987,7 @@ vector<FileId> StickersManager::get_stickers(string emoji, int32 limit, bool for
return {};
}
emoji = remove_emoji_modifiers(emoji);
remove_emoji_modifiers_in_place(emoji);
if (!emoji.empty()) {
if (!are_recent_stickers_loaded_[0]) {
load_recent_stickers(false, std::move(promise));
@ -3192,7 +3191,7 @@ vector<FileId> StickersManager::search_stickers(string emoji, int32 limit, Promi
return {};
}
emoji = remove_emoji_modifiers(emoji);
remove_emoji_modifiers_in_place(emoji);
if (emoji.empty()) {
promise.set_value(Unit());
return {};
@ -4067,7 +4066,7 @@ int StickersManager::get_emoji_number(Slice emoji) {
return emoji[0] - '0';
}
vector<FileId> StickersManager::get_animated_emoji_stickers(const StickerSet *sticker_set, const string &emoji) const {
vector<FileId> StickersManager::get_animated_emoji_stickers(const StickerSet *sticker_set, Slice emoji) const {
vector<FileId> result;
for (auto sticker_id : sticker_set->sticker_ids) {
auto s = get_sticker(sticker_id);
@ -4079,13 +4078,13 @@ vector<FileId> StickersManager::get_animated_emoji_stickers(const StickerSet *st
if (result.empty()) {
const static vector<string> heart_emojis{"💛", "💙", "💚", "💜", "🧡", "🖤", "🤎", "🤍"};
if (td::contains(heart_emojis, emoji)) {
return get_animated_emoji_stickers(sticker_set, "");
return get_animated_emoji_stickers(sticker_set, Slice(""));
}
}
return result;
}
void StickersManager::choose_animated_emoji_click_sticker(const StickerSet *sticker_set, string message_text,
void StickersManager::choose_animated_emoji_click_sticker(const StickerSet *sticker_set, Slice message_text,
FullMessageId full_message_id, double start_time,
Promise<td_api::object_ptr<td_api::sticker>> &&promise) {
CHECK(sticker_set->was_loaded);
@ -4119,8 +4118,11 @@ void StickersManager::choose_animated_emoji_click_sticker(const StickerSet *stic
if (last_clicked_animated_emoji_full_message_id_ != full_message_id) {
flush_pending_animated_emoji_clicks();
} else if (last_clicked_animated_emoji_ != message_text) {
last_clicked_animated_emoji_full_message_id_ = full_message_id;
}
if (last_clicked_animated_emoji_ != message_text) {
pending_animated_emoji_clicks_.clear();
last_clicked_animated_emoji_ = message_text.str();
}
if (!pending_animated_emoji_clicks_.empty() && found_stickers.size() >= 2) {
@ -4135,8 +4137,6 @@ void StickersManager::choose_animated_emoji_click_sticker(const StickerSet *stic
CHECK(!found_stickers.empty());
auto result = found_stickers[Random::fast(0, narrow_cast<int>(found_stickers.size()) - 1)];
last_clicked_animated_emoji_ = std::move(message_text);
last_clicked_animated_emoji_full_message_id_ = full_message_id;
pending_animated_emoji_clicks_.emplace_back(result.first, start_time);
if (pending_animated_emoji_clicks_.size() == 5) {
flush_pending_animated_emoji_clicks();
@ -4319,7 +4319,7 @@ Status StickersManager::on_animated_emoji_message_clicked(const string &emoji, F
return Status::OK();
}
void StickersManager::schedule_update_animated_emoji_clicked(const StickerSet *sticker_set, const string &emoji,
void StickersManager::schedule_update_animated_emoji_clicked(const StickerSet *sticker_set, Slice emoji,
FullMessageId full_message_id,
vector<std::pair<int, double>> clicks) {
if (clicks.empty()) {

View File

@ -596,9 +596,9 @@ class StickersManager final : public Actor {
static int get_emoji_number(Slice emoji);
vector<FileId> get_animated_emoji_stickers(const StickerSet *sticker_set, const string &emoji) const;
vector<FileId> get_animated_emoji_stickers(const StickerSet *sticker_set, Slice emoji) const;
void choose_animated_emoji_click_sticker(const StickerSet *sticker_set, string message_text,
void choose_animated_emoji_click_sticker(const StickerSet *sticker_set, Slice message_text,
FullMessageId full_message_id, double start_time,
Promise<td_api::object_ptr<td_api::sticker>> &&promise);
@ -609,8 +609,8 @@ class StickersManager final : public Actor {
void flush_pending_animated_emoji_clicks();
void schedule_update_animated_emoji_clicked(const StickerSet *sticker_set, const string &emoji,
FullMessageId full_message_id, vector<std::pair<int, double>> clicks);
void schedule_update_animated_emoji_clicked(const StickerSet *sticker_set, Slice emoji, FullMessageId full_message_id,
vector<std::pair<int, double>> clicks);
void send_update_animated_emoji_clicked(FullMessageId full_message_id, FileId sticker_id);

View File

@ -306,7 +306,7 @@ void StickersManager::parse_sticker_set(StickerSet *sticker_set, ParserT &parser
vector<string> emojis;
parse(emojis, parser);
for (auto &emoji : emojis) {
auto &sticker_ids = sticker_set->emoji_stickers_map_[remove_emoji_modifiers(emoji)];
auto &sticker_ids = sticker_set->emoji_stickers_map_[remove_emoji_modifiers(emoji).str()];
if (sticker_ids.empty() || sticker_ids.back() != sticker_id) {
sticker_ids.push_back(sticker_id);
}

View File

@ -307,7 +307,7 @@ string get_emoji_fingerprint(uint64 num) {
return emojis[static_cast<size_t>((num & 0x7FFFFFFFFFFFFFFF) % emojis.size())].str();
}
string remove_emoji_modifiers(string emoji) {
Slice remove_emoji_modifiers(Slice emoji) {
static const Slice modifiers[] = {u8"\uFE0E" /* variation selector-15 */,
u8"\uFE0F" /* variation selector-16 */,
u8"\u200D\u2640" /* zero width joiner + female sign */,
@ -322,7 +322,7 @@ string remove_emoji_modifiers(string emoji) {
found = false;
for (auto &modifier : modifiers) {
if (ends_with(emoji, modifier) && emoji.size() > modifier.size()) {
emoji.resize(emoji.size() - modifier.size());
emoji.remove_suffix(modifier.size());
found = true;
}
}
@ -330,4 +330,8 @@ string remove_emoji_modifiers(string emoji) {
return emoji;
}
void remove_emoji_modifiers_in_place(string &emoji) {
emoji.resize(remove_emoji_modifiers(emoji).size());
}
} // namespace td

View File

@ -7,6 +7,7 @@
#pragma once
#include "td/utils/common.h"
#include "td/utils/Slice.h"
namespace td {
@ -35,6 +36,9 @@ int64 get_vector_hash(const vector<uint64> &numbers) TD_WARN_UNUSED_RESULT;
string get_emoji_fingerprint(uint64 num);
// removes all emoji modifiers
string remove_emoji_modifiers(string emoji);
Slice remove_emoji_modifiers(Slice emoji);
// removes all emoji modifiers in place
void remove_emoji_modifiers_in_place(string &emoji);
} // namespace td