From 587a882270595d236ddbe450bbcb6ead4fc2340d Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 23 Jan 2023 22:10:23 +0300 Subject: [PATCH] Don't remove emoji selectors in search_stickers. --- td/telegram/StickersManager.cpp | 2 +- tdutils/td/utils/emoji.cpp | 15 ++++++++------- tdutils/td/utils/emoji.h | 4 ++-- tdutils/test/emoji.cpp | 9 +++++---- 4 files changed, 16 insertions(+), 14 deletions(-) diff --git a/td/telegram/StickersManager.cpp b/td/telegram/StickersManager.cpp index 5934ff2b4..d004afd84 100644 --- a/td/telegram/StickersManager.cpp +++ b/td/telegram/StickersManager.cpp @@ -4750,7 +4750,7 @@ void StickersManager::search_stickers(StickerType sticker_type, string emoji, in limit = MAX_FOUND_STICKERS; } - remove_emoji_modifiers_in_place(emoji); + remove_emoji_modifiers_in_place(emoji, false); if (emoji.empty() || sticker_type == StickerType::Mask) { return promise.set_value(get_stickers_object({})); } diff --git a/tdutils/td/utils/emoji.cpp b/tdutils/td/utils/emoji.cpp index 56b8e1dad..d6f8f65cd 100644 --- a/tdutils/td/utils/emoji.cpp +++ b/tdutils/td/utils/emoji.cpp @@ -327,13 +327,13 @@ Slice remove_fitzpatrick_modifier(Slice emoji) { return emoji; } -string remove_emoji_modifiers(Slice emoji) { +string remove_emoji_modifiers(Slice emoji, bool remove_selectors) { string result = emoji.str(); - remove_emoji_modifiers_in_place(result); + remove_emoji_modifiers_in_place(result, remove_selectors); return result; } -void remove_emoji_modifiers_in_place(string &emoji) { +void remove_emoji_modifiers_in_place(string &emoji, bool remove_selectors) { static const Slice modifiers[] = {u8"\uFE0F" /* variation selector-16 */, u8"\u200D\u2640" /* zero width joiner + female sign */, u8"\u200D\u2642" /* zero width joiner + male sign */, @@ -342,13 +342,14 @@ void remove_emoji_modifiers_in_place(string &emoji) { u8"\U0001F3FD" /* emoji modifier fitzpatrick type-4 */, u8"\U0001F3FE" /* emoji modifier fitzpatrick type-5 */, u8"\U0001F3FF" /* emoji modifier fitzpatrick type-6 */}; + const size_t start_index = remove_selectors ? 0 : 1; size_t j = 0; for (size_t i = 0; i < emoji.size();) { bool is_found = false; - for (auto &modifier : modifiers) { - auto length = modifier.size(); - if (i + length <= emoji.size() && Slice(&emoji[i], length) == modifier) { - // skip modifier + for (size_t k = start_index; k < sizeof(modifiers) / sizeof(*modifiers); k++) { + auto length = modifiers[k].size(); + if (i + length <= emoji.size() && Slice(&emoji[i], length) == modifiers[k]) { + // skip the modifier i += length; is_found = true; break; diff --git a/tdutils/td/utils/emoji.h b/tdutils/td/utils/emoji.h index 8a89c3bba..29bf1dba5 100644 --- a/tdutils/td/utils/emoji.h +++ b/tdutils/td/utils/emoji.h @@ -21,10 +21,10 @@ int get_fitzpatrick_modifier(Slice emoji); Slice remove_fitzpatrick_modifier(Slice emoji); // removes all emoji modifiers from the string -string remove_emoji_modifiers(Slice emoji); +string remove_emoji_modifiers(Slice emoji, bool remove_selectors = true); // removes all emoji modifiers from the string in-place -void remove_emoji_modifiers_in_place(string &emoji); +void remove_emoji_modifiers_in_place(string &emoji, bool remove_selectors = true); // removes all emoji selectors from the string if it is an emoji string remove_emoji_selectors(Slice emoji); diff --git a/tdutils/test/emoji.cpp b/tdutils/test/emoji.cpp index aec011ac1..de4dabde4 100644 --- a/tdutils/test/emoji.cpp +++ b/tdutils/test/emoji.cpp @@ -70,17 +70,18 @@ TEST(Emoji, get_fitzpatrick_modifier) { test_get_fitzpatrick_modifier("🧑‍🎄", 0); } -static void test_remove_emoji_modifiers(td::string emoji, const td::string &result) { - ASSERT_STREQ(result, td::remove_emoji_modifiers(emoji)); - td::remove_emoji_modifiers_in_place(emoji); +static void test_remove_emoji_modifiers(td::string emoji, const td::string &result, bool remove_selectors = true) { + ASSERT_STREQ(result, td::remove_emoji_modifiers(emoji, remove_selectors)); + td::remove_emoji_modifiers_in_place(emoji, remove_selectors); ASSERT_STREQ(result, emoji); - ASSERT_STREQ(emoji, td::remove_emoji_modifiers(emoji)); + ASSERT_STREQ(emoji, td::remove_emoji_modifiers(emoji, remove_selectors)); } TEST(Emoji, remove_emoji_modifiers) { test_remove_emoji_modifiers("", ""); test_remove_emoji_modifiers("👩🏼‍❤‍💋‍👩🏻", "👩‍❤‍💋‍👩"); test_remove_emoji_modifiers("👩🏼‍❤️‍💋‍👩🏻", "👩‍❤‍💋‍👩"); + test_remove_emoji_modifiers("👩🏼‍❤️‍💋‍👩🏻", "👩‍❤️‍💋‍👩", false); test_remove_emoji_modifiers("👋🏻", "👋"); test_remove_emoji_modifiers("👋🏼", "👋"); test_remove_emoji_modifiers("👋🏽", "👋");