From 2d3ce59b8b214f7e706d0ef3b6e073f2c45e4d11 Mon Sep 17 00:00:00 2001 From: hemirt Date: Sun, 7 Oct 2018 13:18:45 +0200 Subject: [PATCH] Fix emote replacement to render emotes correctly (#768) * fix emote replacement rendering from inside the caught string * \b -> \\b, while -> if i actually wanted the regex identifier \b and you need to escape the escape character \ an error in judgement made me use while, as if the api was meant to be "consume"-like interface that boost regex uses Fixes #26 --- src/providers/twitch/TwitchMessageBuilder.cpp | 34 +++++++++++-------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/src/providers/twitch/TwitchMessageBuilder.cpp b/src/providers/twitch/TwitchMessageBuilder.cpp index 35e7b1503..c69dc10b1 100644 --- a/src/providers/twitch/TwitchMessageBuilder.cpp +++ b/src/providers/twitch/TwitchMessageBuilder.cpp @@ -260,13 +260,16 @@ MessagePtr TwitchMessageBuilder::build() log("v nullptr {}", std::get<2>(tup).string); continue; } - int index = 0; - QString emote = " " + std::get<2>(tup).string + " "; - while ((index = midExtendedRef.indexOf(emote, index)) != - -1) { - std::get<0>(tup) = from + index + 1; - index += emote.size() - 1; - twitchEmotes.push_back(tup); + QRegularExpression emoteregex( + "\\b" + std::get<2>(tup).string + "\\b", + QRegularExpression::UseUnicodePropertiesOption); + auto match = emoteregex.match(midExtendedRef); + if (match.hasMatch()) { + int last = match.lastCapturedIndex(); + for (int i = 0; i <= last; ++i) { + std::get<0>(tup) = from + match.capturedStart(); + twitchEmotes.push_back(std::move(tup)); + } } } @@ -314,13 +317,16 @@ MessagePtr TwitchMessageBuilder::build() log("v nullptr {}", std::get<2>(tup).string); continue; } - int index = 0; - QString emote = " " + std::get<2>(tup).string + " "; - while ((index = midExtendedRef.indexOf(emote, index)) != - -1) { - std::get<0>(tup) = from + index + 1; - index += emote.size() - 1; - twitchEmotes.push_back(tup); + QRegularExpression emoteregex( + "\\b" + std::get<2>(tup).string + "\\b", + QRegularExpression::UseUnicodePropertiesOption); + auto match = emoteregex.match(midExtendedRef); + if (match.hasMatch()) { + int last = match.lastCapturedIndex(); + for (int i = 0; i <= last; ++i) { + std::get<0>(tup) = from + match.capturedStart(); + twitchEmotes.push_back(std::move(tup)); + } } }