From d34ac1e396a4d218ac28fa5efe3f235693f0ec54 Mon Sep 17 00:00:00 2001 From: noavarice Date: Wed, 22 Mar 2017 13:45:49 +0300 Subject: [PATCH 1/2] test: added case for multiple URL's in one message --- test/chatlog/textformatter_test.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/chatlog/textformatter_test.cpp b/test/chatlog/textformatter_test.cpp index e94de4d6a..181590099 100644 --- a/test/chatlog/textformatter_test.cpp +++ b/test/chatlog/textformatter_test.cpp @@ -96,6 +96,24 @@ static const StringToString urlCases { {QStringLiteral("https://url.com/some`url/some`more`url/"), QStringLiteral("" "https://url.com/some`url/some`more`url/")}, + // Test case from issue #4275 + {QStringLiteral("http://www.metacritic.com/game/pc/mass-effect-andromeda\n" + "http://www.metacritic.com/game/playstation-4/mass-effect-andromeda\n" + "http://www.metacritic.com/game/xbox-one/mass-effect-andromeda"), + QStringLiteral("" + "http://www.metacritic.com/game/pc/mass-effect-andromeda\n" + "http://www.metacritic.com/game/playstation-4/mass-effect-andromeda\n" + "" + "http://www.metacritic.com/game/xbox-one/mass-effect-andromeda")}, + {QStringLiteral("http://site.com/part1/part2 " + "http://site.com/part3 " + "and one more time " + "www.site.com/part1/part2"), + QStringLiteral("http://site.com/part1/part2 " + "http://site.com/part3 " + "and one more time " + "www.site.com/part1/part2")}, }; /** From 08208e9aa515270a190ff85a1ba704c8a130c5cd Mon Sep 17 00:00:00 2001 From: noavarice Date: Wed, 22 Mar 2017 14:11:49 +0300 Subject: [PATCH 2/2] fix: fixed wrong formatting for multiple URL's in one message Fix #4275 I did not consider that replacing one substring with another will point to shifting position of next URL found with regexp. That's the behavior of Qt's "QRegularExpression" class - it takes a string into constructor and seems to make its copy inside so changing source string does not affect this regex object --- src/chatlog/textformatter.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/chatlog/textformatter.cpp b/src/chatlog/textformatter.cpp index 3d7662d71..4da44ef7f 100644 --- a/src/chatlog/textformatter.cpp +++ b/src/chatlog/textformatter.cpp @@ -150,14 +150,17 @@ static bool isTagIntersection(const QString& str) */ static void processUrl(QString& str, std::function func) { + int startLength = str.length(); + int offset = 0; for (QRegularExpression exp : urlPatterns) { QRegularExpressionMatchIterator iter = exp.globalMatch(str); while (iter.hasNext()) { QRegularExpressionMatch match = iter.next(); - int startPos = match.capturedStart(); + int startPos = match.capturedStart() + offset; int length = match.capturedLength(); QString url = str.mid(startPos, length); str.replace(startPos, length, func(url)); + offset = str.length() - startLength; } } }