Don't remove emoji selectors in search_stickers.

This commit is contained in:
levlam 2023-01-23 22:10:23 +03:00
parent 833c7c30da
commit 587a882270
4 changed files with 16 additions and 14 deletions

View File

@ -4750,7 +4750,7 @@ void StickersManager::search_stickers(StickerType sticker_type, string emoji, in
limit = MAX_FOUND_STICKERS; limit = MAX_FOUND_STICKERS;
} }
remove_emoji_modifiers_in_place(emoji); remove_emoji_modifiers_in_place(emoji, false);
if (emoji.empty() || sticker_type == StickerType::Mask) { if (emoji.empty() || sticker_type == StickerType::Mask) {
return promise.set_value(get_stickers_object({})); return promise.set_value(get_stickers_object({}));
} }

View File

@ -327,13 +327,13 @@ Slice remove_fitzpatrick_modifier(Slice emoji) {
return emoji; return emoji;
} }
string remove_emoji_modifiers(Slice emoji) { string remove_emoji_modifiers(Slice emoji, bool remove_selectors) {
string result = emoji.str(); string result = emoji.str();
remove_emoji_modifiers_in_place(result); remove_emoji_modifiers_in_place(result, remove_selectors);
return result; 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 */, static const Slice modifiers[] = {u8"\uFE0F" /* variation selector-16 */,
u8"\u200D\u2640" /* zero width joiner + female sign */, u8"\u200D\u2640" /* zero width joiner + female sign */,
u8"\u200D\u2642" /* zero width joiner + male 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"\U0001F3FD" /* emoji modifier fitzpatrick type-4 */,
u8"\U0001F3FE" /* emoji modifier fitzpatrick type-5 */, u8"\U0001F3FE" /* emoji modifier fitzpatrick type-5 */,
u8"\U0001F3FF" /* emoji modifier fitzpatrick type-6 */}; u8"\U0001F3FF" /* emoji modifier fitzpatrick type-6 */};
const size_t start_index = remove_selectors ? 0 : 1;
size_t j = 0; size_t j = 0;
for (size_t i = 0; i < emoji.size();) { for (size_t i = 0; i < emoji.size();) {
bool is_found = false; bool is_found = false;
for (auto &modifier : modifiers) { for (size_t k = start_index; k < sizeof(modifiers) / sizeof(*modifiers); k++) {
auto length = modifier.size(); auto length = modifiers[k].size();
if (i + length <= emoji.size() && Slice(&emoji[i], length) == modifier) { if (i + length <= emoji.size() && Slice(&emoji[i], length) == modifiers[k]) {
// skip modifier // skip the modifier
i += length; i += length;
is_found = true; is_found = true;
break; break;

View File

@ -21,10 +21,10 @@ int get_fitzpatrick_modifier(Slice emoji);
Slice remove_fitzpatrick_modifier(Slice emoji); Slice remove_fitzpatrick_modifier(Slice emoji);
// removes all emoji modifiers from the string // 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 // 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 // removes all emoji selectors from the string if it is an emoji
string remove_emoji_selectors(Slice emoji); string remove_emoji_selectors(Slice emoji);

View File

@ -70,17 +70,18 @@ TEST(Emoji, get_fitzpatrick_modifier) {
test_get_fitzpatrick_modifier("πŸ§‘β€πŸŽ„", 0); test_get_fitzpatrick_modifier("πŸ§‘β€πŸŽ„", 0);
} }
static void test_remove_emoji_modifiers(td::string emoji, const td::string &result) { 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)); ASSERT_STREQ(result, td::remove_emoji_modifiers(emoji, remove_selectors));
td::remove_emoji_modifiers_in_place(emoji); td::remove_emoji_modifiers_in_place(emoji, remove_selectors);
ASSERT_STREQ(result, emoji); 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(Emoji, remove_emoji_modifiers) {
test_remove_emoji_modifiers("", ""); test_remove_emoji_modifiers("", "");
test_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("πŸ‘‹πŸΌ", "πŸ‘‹"); test_remove_emoji_modifiers("πŸ‘‹πŸΌ", "πŸ‘‹");
test_remove_emoji_modifiers("πŸ‘‹πŸ½", "πŸ‘‹"); test_remove_emoji_modifiers("πŸ‘‹πŸ½", "πŸ‘‹");