From d2e43d78fc811ec2eb28c06d1ddde6802f13ea43 Mon Sep 17 00:00:00 2001 From: hemirt Date: Sat, 17 Nov 2018 11:00:44 +0100 Subject: [PATCH] fixes twitch emotes position changes the position in unicodepoints to position in char16 array (qstring) --- src/providers/twitch/TwitchMessageBuilder.cpp | 39 +++++++------------ src/providers/twitch/TwitchMessageBuilder.hpp | 5 +-- 2 files changed, 16 insertions(+), 28 deletions(-) diff --git a/src/providers/twitch/TwitchMessageBuilder.cpp b/src/providers/twitch/TwitchMessageBuilder.cpp index 38595c70f..f7e192308 100644 --- a/src/providers/twitch/TwitchMessageBuilder.cpp +++ b/src/providers/twitch/TwitchMessageBuilder.cpp @@ -174,11 +174,18 @@ MessagePtr TwitchMessageBuilder::build() if (iterator != this->tags.end()) { QStringList emoteString = iterator.value().toString().split('/'); - + std::vector correctPositions; + for (int i = 0; i < this->originalMessage_.size(); ++i) { + if (!this->originalMessage_.at(i).isLowSurrogate()) { + correctPositions.push_back(i); + } + } for (QString emote : emoteString) { - this->appendTwitchEmote(emote, twitchEmotes); + this->appendTwitchEmote(emote, twitchEmotes, correctPositions); } + + } auto app = getApp(); const auto &phrases = app->ignores->phrases.getVector(); @@ -186,15 +193,6 @@ MessagePtr TwitchMessageBuilder::build() [&message = this->originalMessage_](int pos, int len, std::vector> &twitchEmotes) mutable { - int emotePos = 0; - for(int i = 0; i < pos; ++i) { - ++emotePos; - if (message.at(i).isLowSurrogate()) { - --emotePos; - } - } - pos = emotePos; - auto it = std::partition(twitchEmotes.begin(), twitchEmotes.end(), [pos, len](const auto &item) { @@ -461,17 +459,7 @@ void TwitchMessageBuilder::addWords( variant); } - for (int j = 0; j < word.size(); j++) - { - i++; - - if (word.at(j).isHighSurrogate()) - { - j++; - } - } - - i++; + i += word.size() + 1; } } @@ -944,7 +932,8 @@ void TwitchMessageBuilder::parseHighlights(bool isPastMsg) void TwitchMessageBuilder::appendTwitchEmote( const QString &emote, - std::vector> &vec) + std::vector> &vec, + std::vector & correctPositions) { auto app = getApp(); if (!emote.contains(':')) @@ -972,8 +961,8 @@ void TwitchMessageBuilder::appendTwitchEmote( return; } - auto start = coords.at(0).toInt(); - auto end = coords.at(1).toInt(); + auto start = correctPositions[coords.at(0).toInt()]; + auto end = correctPositions[coords.at(1).toInt()]; if (start >= end || start < 0 || end > this->originalMessage_.length()) { diff --git a/src/providers/twitch/TwitchMessageBuilder.hpp b/src/providers/twitch/TwitchMessageBuilder.hpp index e46bf8d4e..5aa72b204 100644 --- a/src/providers/twitch/TwitchMessageBuilder.hpp +++ b/src/providers/twitch/TwitchMessageBuilder.hpp @@ -55,9 +55,8 @@ private: void appendUsername(); void parseHighlights(bool isPastMsg); - void appendTwitchEmote( - const QString &emote, - std::vector> &vec); + void appendTwitchEmote(const QString &emote, + std::vector> &vec, std::vector &correctPositions); Outcome tryAppendEmote(const EmoteName &name); void addWords(