refactor: Remove Emoji's EmojiMap with a vector (#4980)

This commit is contained in:
pajlada 2023-11-26 19:17:58 +01:00 committed by GitHub
parent 9f9739836f
commit 5b741a8eb6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 59 additions and 39 deletions

View file

@ -61,6 +61,7 @@
- Dev: Replace `boost::optional` with `std::optional`. (#4877) - Dev: Replace `boost::optional` with `std::optional`. (#4877)
- Dev: Improve performance of selecting text. (#4889, #4911) - Dev: Improve performance of selecting text. (#4889, #4911)
- Dev: Removed direct dependency on Qt 5 compatibility module. (#4906) - 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: Refactor `DebugCount` and add copy button to debug popup. (#4921)
- Dev: Changed lifetime of context menus. (#4924) - Dev: Changed lifetime of context menus. (#4924)
- Dev: Refactor `ChannelView`, removing a bunch of clang-tidy warnings. (#4926) - Dev: Refactor `ChannelView`, removing a bunch of clang-tidy warnings. (#4926)

View file

@ -68,9 +68,20 @@ static void BM_EmojiParsing(benchmark::State &state)
}; };
const auto &emojiMap = emojis.getEmojis(); const auto &emojiMap = emojis.getEmojis();
std::shared_ptr<EmojiData> penguin; auto getEmoji = [&](auto code) {
emojiMap.tryGet("1F427", penguin); std::shared_ptr<EmojiData> emoji;
auto penguinEmoji = penguin->emote; 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<TestCase> tests{ std::vector<TestCase> tests{
{ {

View file

@ -27,9 +27,11 @@ namespace {
} }
} }
void addEmojis(std::vector<EmoteItem> &out, const EmojiMap &map) void addEmojis(std::vector<EmoteItem> &out,
const std::vector<EmojiPtr> &map)
{ {
map.each([&](const QString &, const std::shared_ptr<EmojiData> &emoji) { for (const auto &emoji : map)
{
for (auto &&shortCode : emoji->shortCodes) for (auto &&shortCode : emoji->shortCodes)
{ {
out.push_back( out.push_back(
@ -40,7 +42,7 @@ namespace {
.providerName = "Emoji", .providerName = "Emoji",
.isEmoji = true}); .isEmoji = true});
} }
}); };
} }
} // namespace } // namespace

View file

@ -196,7 +196,7 @@ void Emojis::loadEmojis()
this->emojiFirstByte_[emojiData->value.at(0)].append(emojiData); this->emojiFirstByte_[emojiData->value.at(0)].append(emojiData);
this->emojis.insert(emojiData->unifiedCode, emojiData); this->emojis.push_back(emojiData);
if (unparsedEmoji.HasMember("skin_variations")) if (unparsedEmoji.HasMember("skin_variations"))
{ {
@ -218,8 +218,7 @@ void Emojis::loadEmojis()
this->emojiFirstByte_[variationEmojiData->value.at(0)].append( this->emojiFirstByte_[variationEmojiData->value.at(0)].append(
variationEmojiData); variationEmojiData);
this->emojis.insert(variationEmojiData->unifiedCode, this->emojis.push_back(variationEmojiData);
variationEmojiData);
} }
} }
} }
@ -244,10 +243,8 @@ void Emojis::sortEmojis()
void Emojis::loadEmojiSet() void Emojis::loadEmojiSet()
{ {
getSettings()->emojiSet.connect([this](const auto &emojiSet) { getSettings()->emojiSet.connect([this](const auto &emojiSet) {
this->emojis.each([=](const auto &name, for (const auto &emoji : this->emojis)
std::shared_ptr<EmojiData> &emoji) { {
(void)name;
QString emojiSetToUse = emojiSet; QString emojiSetToUse = emojiSet;
// clang-format off // clang-format off
static std::map<QString, QString> emojiSets = { static std::map<QString, QString> emojiSets = {
@ -289,7 +286,7 @@ void Emojis::loadEmojiSet()
emoji->emote = std::make_shared<Emote>(Emote{ emoji->emote = std::make_shared<Emote>(Emote{
EmoteName{emoji->value}, ImageSet{Image::fromUrl({url}, 0.35)}, EmoteName{emoji->value}, ImageSet{Image::fromUrl({url}, 0.35)},
Tooltip{":" + emoji->shortCodes[0] + ":<br/>Emoji"}, Url{}}); Tooltip{":" + emoji->shortCodes[0] + ":<br/>Emoji"}, Url{}});
}); }
}); });
} }
@ -430,7 +427,7 @@ QString Emojis::replaceShortCodes(const QString &text) const
return ret; return ret;
} }
const EmojiMap &Emojis::getEmojis() const const std::vector<EmojiPtr> &Emojis::getEmojis() const
{ {
return this->emojis; return this->emojis;
} }

View file

@ -1,12 +1,11 @@
#pragma once #pragma once
#include "util/ConcurrentMap.hpp"
#include <boost/variant.hpp> #include <boost/variant.hpp>
#include <QMap> #include <QMap>
#include <QRegularExpression> #include <QRegularExpression>
#include <QVector> #include <QVector>
#include <memory>
#include <set> #include <set>
#include <vector> #include <vector>
@ -37,7 +36,7 @@ struct EmojiData {
EmotePtr emote; EmotePtr emote;
}; };
using EmojiMap = ConcurrentMap<QString, std::shared_ptr<EmojiData>>; using EmojiPtr = std::shared_ptr<EmojiData>;
class IEmojis class IEmojis
{ {
@ -46,7 +45,7 @@ public:
virtual std::vector<boost::variant<EmotePtr, QString>> parse( virtual std::vector<boost::variant<EmotePtr, QString>> parse(
const QString &text) const = 0; const QString &text) const = 0;
virtual const EmojiMap &getEmojis() const = 0; virtual const std::vector<EmojiPtr> &getEmojis() const = 0;
virtual const std::vector<QString> &getShortCodes() const = 0; virtual const std::vector<QString> &getShortCodes() const = 0;
virtual QString replaceShortCodes(const QString &text) const = 0; virtual QString replaceShortCodes(const QString &text) const = 0;
}; };
@ -59,11 +58,10 @@ public:
std::vector<boost::variant<EmotePtr, QString>> parse( std::vector<boost::variant<EmotePtr, QString>> parse(
const QString &text) const override; const QString &text) const override;
EmojiMap emojis;
std::vector<QString> shortCodes; std::vector<QString> shortCodes;
QString replaceShortCodes(const QString &text) const override; QString replaceShortCodes(const QString &text) const override;
const EmojiMap &getEmojis() const override; const std::vector<EmojiPtr> &getEmojis() const override;
const std::vector<QString> &getShortCodes() const override; const std::vector<QString> &getShortCodes() const override;
private: private:
@ -71,6 +69,8 @@ private:
void sortEmojis(); void sortEmojis();
void loadEmojiSet(); void loadEmojiSet();
std::vector<EmojiPtr> emojis;
/// Emojis /// Emojis
QRegularExpression findShortCodesRegex_{":([-+\\w]+):"}; QRegularExpression findShortCodesRegex_{":([-+\\w]+):"};

View file

@ -72,15 +72,14 @@ auto makeEmoteMessage(const EmoteMap &map, const MessageElementFlag &emoteFlag)
return builder.release(); return builder.release();
} }
auto makeEmojiMessage(EmojiMap &emojiMap) auto makeEmojiMessage(const std::vector<EmojiPtr> &emojiMap)
{ {
MessageBuilder builder; MessageBuilder builder;
builder->flags.set(MessageFlag::Centered); builder->flags.set(MessageFlag::Centered);
builder->flags.set(MessageFlag::DisableCompactEmotes); builder->flags.set(MessageFlag::DisableCompactEmotes);
emojiMap.each([&builder](const auto &key, const auto &value) { for (const auto &value : emojiMap)
(void)key; // unused {
builder builder
.emplace<EmoteElement>( .emplace<EmoteElement>(
value->emote, value->emote,
@ -88,7 +87,7 @@ auto makeEmojiMessage(EmojiMap &emojiMap)
MessageElementFlag::EmojiAll}) MessageElementFlag::EmojiAll})
->setLink( ->setLink(
Link(Link::Type::InsertText, ":" + value->shortCodes[0] + ":")); Link(Link::Type::InsertText, ":" + value->shortCodes[0] + ":"));
}); }
return builder.release(); return builder.release();
} }
@ -165,7 +164,7 @@ void addEmotes(Channel &channel, const EmoteMap &map, const QString &title,
channel.addMessage(makeEmoteMessage(map, emoteFlag)); channel.addMessage(makeEmoteMessage(map, emoteFlag));
} }
void loadEmojis(ChannelView &view, EmojiMap &emojiMap) void loadEmojis(ChannelView &view, const std::vector<EmojiPtr> &emojiMap)
{ {
ChannelPtr emojiChannel(new Channel("", Channel::Type::None)); ChannelPtr emojiChannel(new Channel("", Channel::Type::None));
emojiChannel->addMessage(makeEmojiMessage(emojiMap)); emojiChannel->addMessage(makeEmojiMessage(emojiMap));
@ -173,7 +172,8 @@ void loadEmojis(ChannelView &view, EmojiMap &emojiMap)
view.setChannel(emojiChannel); view.setChannel(emojiChannel);
} }
void loadEmojis(Channel &channel, EmojiMap &emojiMap, const QString &title) void loadEmojis(Channel &channel, const std::vector<EmojiPtr> &emojiMap,
const QString &title)
{ {
channel.addMessage(makeTitleMessage(title)); channel.addMessage(makeTitleMessage(title));
channel.addMessage(makeEmojiMessage(emojiMap)); channel.addMessage(makeEmojiMessage(emojiMap));
@ -269,7 +269,8 @@ EmotePopup::EmotePopup(QWidget *parent)
this->globalEmotesView_ = makeView("Global"); this->globalEmotesView_ = makeView("Global");
this->viewEmojis_ = makeView("Emojis"); this->viewEmojis_ = makeView("Emojis");
loadEmojis(*this->viewEmojis_, getApp()->emotes->emojis.emojis); loadEmojis(*this->viewEmojis_,
getApp()->getEmotes()->getEmojis()->getEmojis());
this->addShortcuts(); this->addShortcuts();
this->signalHolder_.managedConnect(getApp()->hotkeys->onItemsUpdated, this->signalHolder_.managedConnect(getApp()->hotkeys->onItemsUpdated,
[this]() { [this]() {
@ -546,17 +547,18 @@ void EmotePopup::filterEmotes(const QString &searchText)
this->filterTwitchEmotes(searchChannel, searchText); this->filterTwitchEmotes(searchChannel, searchText);
} }
EmojiMap filteredEmojis{}; std::vector<EmojiPtr> filteredEmojis{};
int emojiCount = 0; int emojiCount = 0;
getApp()->emotes->emojis.emojis.each( const auto &emojis = getIApp()->getEmotes()->getEmojis()->getEmojis();
[&, searchText](const auto &name, std::shared_ptr<EmojiData> &emoji) { for (const auto &emoji : emojis)
if (emoji->shortCodes[0].contains(searchText, Qt::CaseInsensitive)) {
{ if (emoji->shortCodes[0].contains(searchText, Qt::CaseInsensitive))
filteredEmojis.insert(name, emoji); {
emojiCount++; filteredEmojis.push_back(emoji);
} emojiCount++;
}); }
}
// emojis // emojis
if (emojiCount > 0) if (emojiCount > 0)
{ {

View file

@ -70,7 +70,14 @@ TEST(Emojis, Parse)
auto getEmoji = [&](auto code) { auto getEmoji = [&](auto code) {
std::shared_ptr<EmojiData> emoji; std::shared_ptr<EmojiData> emoji;
emojis.getEmojis().tryGet(code, emoji); for (const auto &e : emojis.getEmojis())
{
if (e->unifiedCode == code)
{
emoji = e;
break;
}
}
return emoji->emote; return emoji->emote;
}; };