From 5b741a8eb6aa821ffdf45128d27fca6dd25698f3 Mon Sep 17 00:00:00 2001 From: pajlada Date: Sun, 26 Nov 2023 19:17:58 +0100 Subject: [PATCH] refactor: Remove Emoji's EmojiMap with a vector (#4980) --- CHANGELOG.md | 1 + benchmarks/src/Emojis.cpp | 17 +++++++-- .../completion/sources/EmoteSource.cpp | 8 +++-- src/providers/emoji/Emojis.cpp | 15 ++++---- src/providers/emoji/Emojis.hpp | 12 +++---- src/widgets/dialogs/EmotePopup.cpp | 36 ++++++++++--------- tests/src/Emojis.cpp | 9 ++++- 7 files changed, 59 insertions(+), 39 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0590c9ceb..3d0c2a6c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -61,6 +61,7 @@ - Dev: Replace `boost::optional` with `std::optional`. (#4877) - Dev: Improve performance of selecting text. (#4889, #4911) - Dev: Removed direct dependency on Qt 5 compatibility module. (#4906) +- Dev: Refactor `Emoji`'s EmojiMap into a vector. (#4980) - Dev: Refactor `DebugCount` and add copy button to debug popup. (#4921) - Dev: Changed lifetime of context menus. (#4924) - Dev: Refactor `ChannelView`, removing a bunch of clang-tidy warnings. (#4926) diff --git a/benchmarks/src/Emojis.cpp b/benchmarks/src/Emojis.cpp index 830a2941a..10b82ef43 100644 --- a/benchmarks/src/Emojis.cpp +++ b/benchmarks/src/Emojis.cpp @@ -68,9 +68,20 @@ static void BM_EmojiParsing(benchmark::State &state) }; const auto &emojiMap = emojis.getEmojis(); - std::shared_ptr penguin; - emojiMap.tryGet("1F427", penguin); - auto penguinEmoji = penguin->emote; + auto getEmoji = [&](auto code) { + std::shared_ptr emoji; + for (const auto &e : emojis.getEmojis()) + { + if (e->unifiedCode == code) + { + emoji = e; + break; + } + } + return emoji->emote; + }; + auto penguinEmoji = getEmoji("1F427"); + assert(penguinEmoji.get() != nullptr); std::vector tests{ { diff --git a/src/controllers/completion/sources/EmoteSource.cpp b/src/controllers/completion/sources/EmoteSource.cpp index 95d2f32fb..d5c71f8a7 100644 --- a/src/controllers/completion/sources/EmoteSource.cpp +++ b/src/controllers/completion/sources/EmoteSource.cpp @@ -27,9 +27,11 @@ namespace { } } - void addEmojis(std::vector &out, const EmojiMap &map) + void addEmojis(std::vector &out, + const std::vector &map) { - map.each([&](const QString &, const std::shared_ptr &emoji) { + for (const auto &emoji : map) + { for (auto &&shortCode : emoji->shortCodes) { out.push_back( @@ -40,7 +42,7 @@ namespace { .providerName = "Emoji", .isEmoji = true}); } - }); + }; } } // namespace diff --git a/src/providers/emoji/Emojis.cpp b/src/providers/emoji/Emojis.cpp index 57107ddc1..c99e31d47 100644 --- a/src/providers/emoji/Emojis.cpp +++ b/src/providers/emoji/Emojis.cpp @@ -196,7 +196,7 @@ void Emojis::loadEmojis() this->emojiFirstByte_[emojiData->value.at(0)].append(emojiData); - this->emojis.insert(emojiData->unifiedCode, emojiData); + this->emojis.push_back(emojiData); if (unparsedEmoji.HasMember("skin_variations")) { @@ -218,8 +218,7 @@ void Emojis::loadEmojis() this->emojiFirstByte_[variationEmojiData->value.at(0)].append( variationEmojiData); - this->emojis.insert(variationEmojiData->unifiedCode, - variationEmojiData); + this->emojis.push_back(variationEmojiData); } } } @@ -244,10 +243,8 @@ void Emojis::sortEmojis() void Emojis::loadEmojiSet() { getSettings()->emojiSet.connect([this](const auto &emojiSet) { - this->emojis.each([=](const auto &name, - std::shared_ptr &emoji) { - (void)name; - + for (const auto &emoji : this->emojis) + { QString emojiSetToUse = emojiSet; // clang-format off static std::map emojiSets = { @@ -289,7 +286,7 @@ void Emojis::loadEmojiSet() emoji->emote = std::make_shared(Emote{ EmoteName{emoji->value}, ImageSet{Image::fromUrl({url}, 0.35)}, Tooltip{":" + emoji->shortCodes[0] + ":
Emoji"}, Url{}}); - }); + } }); } @@ -430,7 +427,7 @@ QString Emojis::replaceShortCodes(const QString &text) const return ret; } -const EmojiMap &Emojis::getEmojis() const +const std::vector &Emojis::getEmojis() const { return this->emojis; } diff --git a/src/providers/emoji/Emojis.hpp b/src/providers/emoji/Emojis.hpp index deb585e3d..d6d783fc5 100644 --- a/src/providers/emoji/Emojis.hpp +++ b/src/providers/emoji/Emojis.hpp @@ -1,12 +1,11 @@ #pragma once -#include "util/ConcurrentMap.hpp" - #include #include #include #include +#include #include #include @@ -37,7 +36,7 @@ struct EmojiData { EmotePtr emote; }; -using EmojiMap = ConcurrentMap>; +using EmojiPtr = std::shared_ptr; class IEmojis { @@ -46,7 +45,7 @@ public: virtual std::vector> parse( const QString &text) const = 0; - virtual const EmojiMap &getEmojis() const = 0; + virtual const std::vector &getEmojis() const = 0; virtual const std::vector &getShortCodes() const = 0; virtual QString replaceShortCodes(const QString &text) const = 0; }; @@ -59,11 +58,10 @@ public: std::vector> parse( const QString &text) const override; - EmojiMap emojis; std::vector shortCodes; QString replaceShortCodes(const QString &text) const override; - const EmojiMap &getEmojis() const override; + const std::vector &getEmojis() const override; const std::vector &getShortCodes() const override; private: @@ -71,6 +69,8 @@ private: void sortEmojis(); void loadEmojiSet(); + std::vector emojis; + /// Emojis QRegularExpression findShortCodesRegex_{":([-+\\w]+):"}; diff --git a/src/widgets/dialogs/EmotePopup.cpp b/src/widgets/dialogs/EmotePopup.cpp index 985562409..97251a287 100644 --- a/src/widgets/dialogs/EmotePopup.cpp +++ b/src/widgets/dialogs/EmotePopup.cpp @@ -72,15 +72,14 @@ auto makeEmoteMessage(const EmoteMap &map, const MessageElementFlag &emoteFlag) return builder.release(); } -auto makeEmojiMessage(EmojiMap &emojiMap) +auto makeEmojiMessage(const std::vector &emojiMap) { MessageBuilder builder; builder->flags.set(MessageFlag::Centered); builder->flags.set(MessageFlag::DisableCompactEmotes); - emojiMap.each([&builder](const auto &key, const auto &value) { - (void)key; // unused - + for (const auto &value : emojiMap) + { builder .emplace( value->emote, @@ -88,7 +87,7 @@ auto makeEmojiMessage(EmojiMap &emojiMap) MessageElementFlag::EmojiAll}) ->setLink( Link(Link::Type::InsertText, ":" + value->shortCodes[0] + ":")); - }); + } return builder.release(); } @@ -165,7 +164,7 @@ void addEmotes(Channel &channel, const EmoteMap &map, const QString &title, channel.addMessage(makeEmoteMessage(map, emoteFlag)); } -void loadEmojis(ChannelView &view, EmojiMap &emojiMap) +void loadEmojis(ChannelView &view, const std::vector &emojiMap) { ChannelPtr emojiChannel(new Channel("", Channel::Type::None)); emojiChannel->addMessage(makeEmojiMessage(emojiMap)); @@ -173,7 +172,8 @@ void loadEmojis(ChannelView &view, EmojiMap &emojiMap) view.setChannel(emojiChannel); } -void loadEmojis(Channel &channel, EmojiMap &emojiMap, const QString &title) +void loadEmojis(Channel &channel, const std::vector &emojiMap, + const QString &title) { channel.addMessage(makeTitleMessage(title)); channel.addMessage(makeEmojiMessage(emojiMap)); @@ -269,7 +269,8 @@ EmotePopup::EmotePopup(QWidget *parent) this->globalEmotesView_ = makeView("Global"); this->viewEmojis_ = makeView("Emojis"); - loadEmojis(*this->viewEmojis_, getApp()->emotes->emojis.emojis); + loadEmojis(*this->viewEmojis_, + getApp()->getEmotes()->getEmojis()->getEmojis()); this->addShortcuts(); this->signalHolder_.managedConnect(getApp()->hotkeys->onItemsUpdated, [this]() { @@ -546,17 +547,18 @@ void EmotePopup::filterEmotes(const QString &searchText) this->filterTwitchEmotes(searchChannel, searchText); } - EmojiMap filteredEmojis{}; + std::vector filteredEmojis{}; int emojiCount = 0; - getApp()->emotes->emojis.emojis.each( - [&, searchText](const auto &name, std::shared_ptr &emoji) { - if (emoji->shortCodes[0].contains(searchText, Qt::CaseInsensitive)) - { - filteredEmojis.insert(name, emoji); - emojiCount++; - } - }); + const auto &emojis = getIApp()->getEmotes()->getEmojis()->getEmojis(); + for (const auto &emoji : emojis) + { + if (emoji->shortCodes[0].contains(searchText, Qt::CaseInsensitive)) + { + filteredEmojis.push_back(emoji); + emojiCount++; + } + } // emojis if (emojiCount > 0) { diff --git a/tests/src/Emojis.cpp b/tests/src/Emojis.cpp index f984538f8..0f6cf6762 100644 --- a/tests/src/Emojis.cpp +++ b/tests/src/Emojis.cpp @@ -70,7 +70,14 @@ TEST(Emojis, Parse) auto getEmoji = [&](auto code) { std::shared_ptr emoji; - emojis.getEmojis().tryGet(code, emoji); + for (const auto &e : emojis.getEmojis()) + { + if (e->unifiedCode == code) + { + emoji = e; + break; + } + } return emoji->emote; };