From d0ab10cc3894052da74a6e2a82e03f83c5c3556a Mon Sep 17 00:00:00 2001 From: hunbernd Date: Sun, 5 Feb 2017 19:13:40 +0100 Subject: [PATCH] Fix slow emoticons by using generic regex rules where possible. --- retroshare-gui/src/util/HandleRichText.cpp | 79 +++++++++++++++------- retroshare-gui/src/util/HandleRichText.h | 3 + 2 files changed, 56 insertions(+), 26 deletions(-) diff --git a/retroshare-gui/src/util/HandleRichText.cpp b/retroshare-gui/src/util/HandleRichText.cpp index 865bce7e0..f8299687a 100644 --- a/retroshare-gui/src/util/HandleRichText.cpp +++ b/retroshare-gui/src/util/HandleRichText.cpp @@ -168,6 +168,13 @@ RsHtml::RsHtml() void RsHtml::initEmoticons(const QHash, QHash > >& hash) { + //add rules for standard emoticons + QString genericpattern; + genericpattern += "(?:^|\\s)(:\\w{1,40}:)(?:$|\\s)|"; //generic rule for :emoji_name: + genericpattern += "(?:^|\\s)(\\(\\w{1,40}\\))(?:$|\\s)"; //generic rule for (emoji_name) + QRegExp genericrx(genericpattern); + genericrx.setMinimal(true); + QString newRE; for(QHash, QHash > >::const_iterator groupit = hash.begin(); groupit != hash.end(); ++groupit) { QHash group = groupit.value().second; @@ -177,38 +184,42 @@ void RsHtml::initEmoticons(const QHash, QHash(embedInfos); + + while((index = rx.indexIn(text, pos)) != -1) { + if(embedImg.smileys.contains(rx.cap(0).trimmed())) + return index; + else + ++pos; + } + return -1; +} + /** * Parses a DOM tree and replaces text by HTML tags. * The tree is traversed depth-first, but only through children of Element type @@ -376,13 +403,13 @@ void RsHtml::embedHtml(QTextDocument *textDocument, QDomDocument& doc, QDomEleme if(myRE.pattern().length() == 0) // we'll get stuck with an empty regexp return; - if(myRE.indexIn(tempText) == -1) + int nextPos = 0; + if((nextPos = indexInWithValidation(myRE, tempText, embedInfos)) == -1) continue; // there is at least one link inside, we start replacing int currentPos = 0; - int nextPos = 0; - while((nextPos = myRE.indexIn(tempText, currentPos)) != -1) { + do { // if nextPos == 0 it means the text begins by a link if(nextPos > 0) { QDomText textPart = doc.createTextNode(tempText.mid(currentPos, nextPos - currentPos)); @@ -439,7 +466,7 @@ void RsHtml::embedHtml(QTextDocument *textDocument, QDomDocument& doc, QDomEleme index++; currentPos = nextPos + myRE.matchedLength(); - } + } while((nextPos = indexInWithValidation(myRE, tempText, embedInfos, currentPos)) != -1); // text after the last link, only if there's one, don't touch the index // otherwise decrement the index because we're going to remove node diff --git a/retroshare-gui/src/util/HandleRichText.h b/retroshare-gui/src/util/HandleRichText.h index 2c1ab791a..54036b53c 100644 --- a/retroshare-gui/src/util/HandleRichText.h +++ b/retroshare-gui/src/util/HandleRichText.h @@ -86,6 +86,9 @@ protected: virtual bool canReplaceAnchor(QDomDocument &doc, QDomElement &element, const RetroShareLink &link); virtual void anchorTextForImg(QDomDocument &doc, QDomElement &element, const RetroShareLink &link, QString &text); virtual void anchorStylesheetForImg(QDomDocument &doc, QDomElement &element, const RetroShareLink &link, QString &styleSheet); + +private: + int indexInWithValidation(QRegExp &rx, const QString &text, EmbedInHtml &embedInfos, int pos = 0); }; #endif // HANDLE_RICH_TEXT_H_