IgnorePhrase replacement

also removes twitch emotes info about the matched and changed parts and
shifts positions of other emotes from emote infos to the corresponding new
position
This commit is contained in:
hemirt 2018-07-08 21:03:13 +02:00
parent d3b6e294ed
commit 1834342f74
2 changed files with 59 additions and 4 deletions

View file

@ -24,7 +24,7 @@ public:
IgnorePhrase(const QString &pattern, bool isRegex, bool isReplace, const QString &replace) IgnorePhrase(const QString &pattern, bool isRegex, bool isReplace, const QString &replace)
: pattern_(pattern) : pattern_(pattern)
, isRegex_(isRegex) , isRegex_(isRegex)
, regex_(isRegex_ ? pattern : "\\b" + QRegularExpression::escape(pattern) + "\\b", , regex_(!isRegex ? pattern : QRegularExpression::escape(pattern),
QRegularExpression::CaseInsensitiveOption | QRegularExpression::CaseInsensitiveOption |
QRegularExpression::UseUnicodePropertiesOption) QRegularExpression::UseUnicodePropertiesOption)
, isReplace_(isReplace) , isReplace_(isReplace)
@ -51,6 +51,11 @@ public:
return this->isValid() && this->regex_.match(subject).hasMatch(); return this->isValid() && this->regex_.match(subject).hasMatch();
} }
const QRegularExpression &getRegex() const
{
return this->regex_;
}
bool isReplace() const bool isReplace() const
{ {
return this->isReplace_; return this->isReplace_;

View file

@ -166,6 +166,58 @@ MessagePtr TwitchMessageBuilder::build()
[](const auto &a, const auto &b) { return a.first < b.first; }); [](const auto &a, const auto &b) { return a.first < b.first; });
} }
const auto &phrases = app->ignores->phrases.getVector();
auto removeEmotesInRange = [&twitchEmotes](int pos, int len) mutable {
twitchEmotes.erase(
std::remove_if(twitchEmotes.begin(), twitchEmotes.end(),
[&pos, &len](const auto &item) {
return ((item.first >= pos) && item.first < (pos + len));
}),
twitchEmotes.end());
};
auto shiftIndicesAfter = [&twitchEmotes](int pos, int by) mutable {
auto it = std::find_if(twitchEmotes.begin(), twitchEmotes.end(),
[&pos](const auto &item) { return item.first >= pos; });
while (it != twitchEmotes.end()) {
it->first += by;
++it;
}
};
for (const auto &phrase : phrases) {
if (!phrase.isReplace() || !phrase.isValid()) {
continue;
}
if (phrase.isRegex()) {
const auto &regex = phrase.getRegex();
QRegularExpressionMatch match;
int from = 0;
while ((from = this->originalMessage_.indexOf(regex, from, &match)) != -1) {
int len = match.capturedLength();
removeEmotesInRange(from, len);
auto mid = this->originalMessage_.mid(from, len);
mid.replace(regex, phrase.getReplace());
this->originalMessage_.replace(from, len, mid);
int midsize = mid.size();
from += midsize;
shiftIndicesAfter(from, midsize - len);
}
} else {
const auto &pattern = phrase.getPattern();
int from = 0;
while ((from = this->originalMessage_.indexOf(pattern, from)) != -1) {
int len = pattern.size();
removeEmotesInRange(from, len);
const auto &replace = phrase.getReplace();
this->originalMessage_.replace(from, len, replace);
int replacesize = replace.size();
from += replacesize;
shiftIndicesAfter(from, replacesize - len);
}
}
}
auto currentTwitchEmote = twitchEmotes.begin(); auto currentTwitchEmote = twitchEmotes.begin();
/*for (const auto &phrase : app->ignores->phrases.getVector()) { /*for (const auto &phrase : app->ignores->phrases.getVector()) {
@ -181,9 +233,7 @@ MessagePtr TwitchMessageBuilder::build()
long int i = 0; long int i = 0;
for (int current = 0; current < splits.size(); ++current) { for (const auto &split : splits) {
const QString &split = splits[current];
Log("Splits {} {} {}", current, split, i);
MessageColor textColor = MessageColor textColor =
this->action_ ? MessageColor(this->usernameColor_) : MessageColor(MessageColor::Text); this->action_ ? MessageColor(this->usernameColor_) : MessageColor(MessageColor::Text);