From aacc4bcb4a487871daae6717f77605aaba444341 Mon Sep 17 00:00:00 2001 From: Marc Deop Date: Sat, 2 Mar 2019 12:26:57 +0100 Subject: style: apply .clang-format to all .cpp and .h files --- lib/util.cpp | 76 ++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 40 insertions(+), 36 deletions(-) (limited to 'lib/util.cpp') diff --git a/lib/util.cpp b/lib/util.cpp index d042aa34..a7c745d4 100644 --- a/lib/util.cpp +++ b/lib/util.cpp @@ -13,20 +13,19 @@ * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include "util.h" +#include #include #include -#include #include -static const auto RegExpOptions = - QRegularExpression::CaseInsensitiveOption - | QRegularExpression::OptimizeOnFirstUsageOption - | QRegularExpression::UseUnicodePropertiesOption; +static const auto RegExpOptions = QRegularExpression::CaseInsensitiveOption + | QRegularExpression::OptimizeOnFirstUsageOption + | QRegularExpression::UseUnicodePropertiesOption; // Converts all that looks like a URL into HTML links static void linkifyUrls(QString& htmlEscapedText) @@ -39,34 +38,37 @@ static void linkifyUrls(QString& htmlEscapedText) // Note: outer parentheses are a part of C++ raw string delimiters, not of // the regex (see http://en.cppreference.com/w/cpp/language/string_literal). // Note2: yet another pair of outer parentheses are \1 in the replacement. - static const QRegularExpression FullUrlRegExp(QStringLiteral( - R"(((www\.(?!\.)|(https?|ftp|magnet)://)(&(?![lg]t;)|[^&\s<>'"])+(&(?![lg]t;)|[^&!,.\s<>'"\]):])))" - ), RegExpOptions); + static const QRegularExpression FullUrlRegExp( + QStringLiteral( + R"(((www\.(?!\.)|(https?|ftp|magnet)://)(&(?![lg]t;)|[^&\s<>'"])+(&(?![lg]t;)|[^&!,.\s<>'"\]):])))"), + RegExpOptions); // email address: // [word chars, dots or dashes]@[word chars, dots or dashes].[word chars] - static const QRegularExpression EmailAddressRegExp(QStringLiteral( - R"((mailto:)?(\b(\w|\.|-)+@(\w|\.|-)+\.\w+\b))" - ), RegExpOptions); + static const QRegularExpression EmailAddressRegExp( + QStringLiteral(R"((mailto:)?(\b(\w|\.|-)+@(\w|\.|-)+\.\w+\b))"), + RegExpOptions); // An interim liberal implementation of // https://matrix.org/docs/spec/appendices.html#identifier-grammar - static const QRegularExpression MxIdRegExp(QStringLiteral( - R"((^|[^<>/])([!#@][-a-z0-9_=/.]{1,252}:[-.a-z0-9]+))" - ), RegExpOptions); + static const QRegularExpression MxIdRegExp( + QStringLiteral( + R"((^|[^<>/])([!#@][-a-z0-9_=/.]{1,252}:[-.a-z0-9]+))"), + RegExpOptions); // NOTE: htmlEscapedText is already HTML-escaped! No literal <,>,& htmlEscapedText.replace(EmailAddressRegExp, - QStringLiteral(R"(\1\2)")); + QStringLiteral(R"(\1\2)")); htmlEscapedText.replace(FullUrlRegExp, - QStringLiteral(R"(\1)")); - htmlEscapedText.replace(MxIdRegExp, - QStringLiteral(R"(\1\2)")); + QStringLiteral(R"(\1)")); + htmlEscapedText.replace( + MxIdRegExp, + QStringLiteral(R"(\1\2)")); } QString QMatrixClient::prettyPrint(const QString& plainText) { - auto pt = QStringLiteral("") + - plainText.toHtmlEscaped() + QStringLiteral(""); + auto pt = QStringLiteral("") + + plainText.toHtmlEscaped() + QStringLiteral(""); pt.replace('\n', QStringLiteral("
")); linkifyUrls(pt); @@ -76,7 +78,7 @@ QString QMatrixClient::prettyPrint(const QString& plainText) QString QMatrixClient::cacheLocation(const QString& dirName) { const QString cachePath = - QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + QStandardPaths::writableLocation(QStandardPaths::CacheLocation) % '/' % dirName % '/'; QDir dir; if (!dir.exists(cachePath)) @@ -104,17 +106,24 @@ void f2(int, QString); static_assert(std::is_same, QString>::value, "Test fn_arg_t<>"); -struct S { int mf(); }; +struct S { + int mf(); +}; static_assert(is_callable_v, "Test member function"); -static_assert(returns(), "Test returns<> with member function"); +static_assert(returns(), + "Test returns<> with member function"); -struct Fo { int operator()(); }; +struct Fo { + int operator()(); +}; static_assert(is_callable_v, "Test is_callable<> with function object"); static_assert(function_traits::arg_number == 0, "Test function object"); static_assert(std::is_same, int>::value, "Test return type of function object"); -struct Fo1 { void operator()(int); }; +struct Fo1 { + void operator()(int); +}; static_assert(function_traits::arg_number == 1, "Test function object 1"); static_assert(is_callable_v, "Test is_callable<> with function object 1"); static_assert(std::is_same, int>(), @@ -127,14 +136,10 @@ static_assert(std::is_same, int>::value, "Test fn_return_t<> with lambda"); #endif -template -struct fn_object -{ +template struct fn_object { static int smf(double) { return 0; } }; -template <> -struct fn_object -{ +template <> struct fn_object { void operator()(QString); }; static_assert(is_callable_v>, "Test function object"); @@ -142,13 +147,12 @@ static_assert(returns>(), "Test returns<> with function object"); static_assert(!is_callable_v>, "Test non-function object"); // FIXME: These two don't work -//static_assert(is_callable_v::smf)>, +// static_assert(is_callable_v::smf)>, // "Test static member function"); -//static_assert(returns::smf)>(), +// static_assert(returns::smf)>(), // "Test returns<> with static member function"); -template -QString ft(T&&); +template QString ft(T&&); static_assert(std::is_same)>, QString&&>(), "Test function templates"); -- cgit v1.2.3 From 21e5138f6cf1e96d3cac702e2ada2a0148a3ec92 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Sun, 24 Mar 2019 18:51:08 +0900 Subject: linkifyUrls(): fix linkification of emails containing "www." Closes #303. --- lib/util.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'lib/util.cpp') diff --git a/lib/util.cpp b/lib/util.cpp index d042aa34..e1f312ee 100644 --- a/lib/util.cpp +++ b/lib/util.cpp @@ -38,14 +38,14 @@ static void linkifyUrls(QString& htmlEscapedText) // comma or dot // Note: outer parentheses are a part of C++ raw string delimiters, not of // the regex (see http://en.cppreference.com/w/cpp/language/string_literal). - // Note2: yet another pair of outer parentheses are \1 in the replacement. + // Note2: the next-outer parentheses are \N in the replacement. static const QRegularExpression FullUrlRegExp(QStringLiteral( - R"(((www\.(?!\.)|(https?|ftp|magnet)://)(&(?![lg]t;)|[^&\s<>'"])+(&(?![lg]t;)|[^&!,.\s<>'"\]):])))" + R"(\b((www\.(?!\.)(?!(\w|\.|-)+@)|(https?|ftp|magnet)://)(&(?![lg]t;)|[^&\s<>'"])+(&(?![lg]t;)|[^&!,.\s<>'"\]):])))" ), RegExpOptions); // email address: // [word chars, dots or dashes]@[word chars, dots or dashes].[word chars] static const QRegularExpression EmailAddressRegExp(QStringLiteral( - R"((mailto:)?(\b(\w|\.|-)+@(\w|\.|-)+\.\w+\b))" + R"(\b(mailto:)?((\w|\.|-)+@(\w|\.|-)+\.\w+\b))" ), RegExpOptions); // An interim liberal implementation of // https://matrix.org/docs/spec/appendices.html#identifier-grammar @@ -53,7 +53,7 @@ static void linkifyUrls(QString& htmlEscapedText) R"((^|[^<>/])([!#@][-a-z0-9_=/.]{1,252}:[-.a-z0-9]+))" ), RegExpOptions); - // NOTE: htmlEscapedText is already HTML-escaped! No literal <,>,& + // NOTE: htmlEscapedText is already HTML-escaped! No literal <,>,&," htmlEscapedText.replace(EmailAddressRegExp, QStringLiteral(R"(\1\2)")); -- cgit v1.2.3 From e855085835909549aa866ed968e24902eb378b5a Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Sun, 17 Mar 2019 09:03:34 +0900 Subject: RoomMemberEvent: sanitize user display names MemberEventContent::displayName() will strip away Unicode text direction override characters. Direct access to JSON can still provide "raw" data. --- lib/util.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'lib/util.cpp') diff --git a/lib/util.cpp b/lib/util.cpp index e1f312ee..8d16cfc8 100644 --- a/lib/util.cpp +++ b/lib/util.cpp @@ -63,10 +63,18 @@ static void linkifyUrls(QString& htmlEscapedText) QStringLiteral(R"(\1\2)")); } +QString QMatrixClient::sanitized(const QString& plainText) +{ + auto text = plainText; + text.remove(QChar(0x202e)); + text.remove(QChar(0x202d)); + return text; +} + QString QMatrixClient::prettyPrint(const QString& plainText) { auto pt = QStringLiteral("") + - plainText.toHtmlEscaped() + QStringLiteral(""); + plainText.toHtmlEscaped() + QStringLiteral(""); pt.replace('\n', QStringLiteral("
")); linkifyUrls(pt); -- cgit v1.2.3 From adcea5868d45610be0539af3e1cfc15f8495815c Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Sun, 24 Mar 2019 19:09:48 +0900 Subject: Expose linkifyUrls() into library API for future use --- lib/util.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'lib/util.cpp') diff --git a/lib/util.cpp b/lib/util.cpp index 8d16cfc8..fe6286f3 100644 --- a/lib/util.cpp +++ b/lib/util.cpp @@ -29,16 +29,17 @@ static const auto RegExpOptions = | QRegularExpression::UseUnicodePropertiesOption; // Converts all that looks like a URL into HTML links -static void linkifyUrls(QString& htmlEscapedText) +void QMatrixClient::linkifyUrls(QString& htmlEscapedText) { + // Note: outer parentheses are a part of C++ raw string delimiters, not of + // the regex (see http://en.cppreference.com/w/cpp/language/string_literal). + // Note2: the next-outer parentheses are \N in the replacement. + + // generic url: // regexp is originally taken from Konsole (https://github.com/KDE/konsole) - // full url: // protocolname:// or www. followed by anything other than whitespaces, // <, >, ' or ", and ends before whitespaces, <, >, ', ", ], !, ), :, // comma or dot - // Note: outer parentheses are a part of C++ raw string delimiters, not of - // the regex (see http://en.cppreference.com/w/cpp/language/string_literal). - // Note2: the next-outer parentheses are \N in the replacement. static const QRegularExpression FullUrlRegExp(QStringLiteral( R"(\b((www\.(?!\.)(?!(\w|\.|-)+@)|(https?|ftp|magnet)://)(&(?![lg]t;)|[^&\s<>'"])+(&(?![lg]t;)|[^&!,.\s<>'"\]):])))" ), RegExpOptions); -- cgit v1.2.3 From 432e7fd7107d8260e0016a1adcd8d94263dc1044 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Thu, 4 Apr 2019 21:27:38 +0900 Subject: Clean up on clang-tidy/clazy analysis --- lib/util.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/util.cpp') diff --git a/lib/util.cpp b/lib/util.cpp index fe6286f3..c3e21c8e 100644 --- a/lib/util.cpp +++ b/lib/util.cpp @@ -157,7 +157,7 @@ static_assert(!is_callable_v>, "Test non-function object"); // "Test returns<> with static member function"); template -QString ft(T&&); +QString ft(T&&) { return {}; } static_assert(std::is_same)>, QString&&>(), "Test function templates"); -- cgit v1.2.3 From 346adee1810109f4b7b14298e55d29a44c076a66 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Mon, 13 May 2019 20:41:04 +0900 Subject: prettyPrint(): First linkify, than add more tags Otherwise the linkification gets confused by HTML tags being already there and doesn't linkify what has to be linkified if that occurs at the beginning of the message. --- lib/util.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'lib/util.cpp') diff --git a/lib/util.cpp b/lib/util.cpp index c3e21c8e..0248e521 100644 --- a/lib/util.cpp +++ b/lib/util.cpp @@ -74,12 +74,11 @@ QString QMatrixClient::sanitized(const QString& plainText) QString QMatrixClient::prettyPrint(const QString& plainText) { - auto pt = QStringLiteral("") + - plainText.toHtmlEscaped() + QStringLiteral(""); - pt.replace('\n', QStringLiteral("
")); - + auto pt = plainText.toHtmlEscaped(); linkifyUrls(pt); - return pt; + pt.replace('\n', QStringLiteral("
")); + return QStringLiteral("") + pt + + QStringLiteral(""); } QString QMatrixClient::cacheLocation(const QString& dirName) -- cgit v1.2.3 From 3c253ed025246a34d849d14aac6feaee672d7e63 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Mon, 13 May 2019 20:42:50 +0900 Subject: linkifyUrls(): be more conservative in parsing serverparts Closes #321. --- lib/util.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/util.cpp') diff --git a/lib/util.cpp b/lib/util.cpp index 0248e521..883db2ea 100644 --- a/lib/util.cpp +++ b/lib/util.cpp @@ -51,7 +51,7 @@ void QMatrixClient::linkifyUrls(QString& htmlEscapedText) // An interim liberal implementation of // https://matrix.org/docs/spec/appendices.html#identifier-grammar static const QRegularExpression MxIdRegExp(QStringLiteral( - R"((^|[^<>/])([!#@][-a-z0-9_=/.]{1,252}:[-.a-z0-9]+))" + R"((^|[^<>/])([!#@][-a-z0-9_=/.]{1,252}:(?:\w|\.|-)+\.\w+(?::\d{1,5})?))" ), RegExpOptions); // NOTE: htmlEscapedText is already HTML-escaped! No literal <,>,&," -- cgit v1.2.3 From 1a034626bcbe064ebe0ada8cdfe1a47f2d82e477 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Mon, 13 May 2019 21:43:54 +0900 Subject: sanitized(): add object replacement character (0xfffc) to the blacklist --- lib/util.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'lib/util.cpp') diff --git a/lib/util.cpp b/lib/util.cpp index 883db2ea..4e17d2f9 100644 --- a/lib/util.cpp +++ b/lib/util.cpp @@ -57,18 +57,19 @@ void QMatrixClient::linkifyUrls(QString& htmlEscapedText) // NOTE: htmlEscapedText is already HTML-escaped! No literal <,>,&," htmlEscapedText.replace(EmailAddressRegExp, - QStringLiteral(R"(\1\2)")); + QStringLiteral(R"(\1\2)")); htmlEscapedText.replace(FullUrlRegExp, - QStringLiteral(R"(\1)")); + QStringLiteral(R"(\1)")); htmlEscapedText.replace(MxIdRegExp, - QStringLiteral(R"(\1\2)")); + QStringLiteral(R"(\1\2)")); } QString QMatrixClient::sanitized(const QString& plainText) { auto text = plainText; - text.remove(QChar(0x202e)); - text.remove(QChar(0x202d)); + text.remove(QChar(0x202e)); // RLO + text.remove(QChar(0x202d)); // LRO + text.remove(QChar(0xfffc)); // Object replacement character return text; } -- cgit v1.2.3 From 48ffee5fa78f44bd00d76772ad79ae4eeb6c8dc4 Mon Sep 17 00:00:00 2001 From: Alexey Andreyev Date: Wed, 15 May 2019 16:36:02 +0300 Subject: Move out the logic of the hue calculation to utils --- lib/util.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'lib/util.cpp') diff --git a/lib/util.cpp b/lib/util.cpp index 4e17d2f9..01d4e77b 100644 --- a/lib/util.cpp +++ b/lib/util.cpp @@ -23,6 +23,10 @@ #include #include +#include +#include +#include + static const auto RegExpOptions = QRegularExpression::CaseInsensitiveOption | QRegularExpression::OptimizeOnFirstUsageOption @@ -93,6 +97,21 @@ QString QMatrixClient::cacheLocation(const QString& dirName) return cachePath; } +qreal QMatrixClient::stringToHueF(const QString &string) +{ + Q_ASSERT(!string.isEmpty()); + QByteArray hash = QCryptographicHash::hash(string.toUtf8(), + QCryptographicHash::Sha1); + QDataStream dataStream(qToLittleEndian(hash).left(2)); + dataStream.setByteOrder(QDataStream::LittleEndian); + quint16 hashValue; + dataStream >> hashValue; + const auto hueF = + qreal(hashValue)/std::numeric_limits::max(); + Q_ASSERT((0 <= hueF) && (hueF <= 1)); + return hueF; +} + // Tests for function_traits<> #ifdef Q_CC_CLANG -- cgit v1.2.3 From ddc5a60184972e1449191ce77561b875a145a665 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Tue, 2 Jul 2019 08:53:25 +0900 Subject: linkifyUrls: support matrix: scheme and relative URLs --- lib/util.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/util.cpp') diff --git a/lib/util.cpp b/lib/util.cpp index 01d4e77b..88cba959 100644 --- a/lib/util.cpp +++ b/lib/util.cpp @@ -45,7 +45,7 @@ void QMatrixClient::linkifyUrls(QString& htmlEscapedText) // <, >, ' or ", and ends before whitespaces, <, >, ', ", ], !, ), :, // comma or dot static const QRegularExpression FullUrlRegExp(QStringLiteral( - R"(\b((www\.(?!\.)(?!(\w|\.|-)+@)|(https?|ftp|magnet)://)(&(?![lg]t;)|[^&\s<>'"])+(&(?![lg]t;)|[^&!,.\s<>'"\]):])))" + R"(\b((www\.(?!\.)(?!(\w|\.|-)+@)|(https?|ftp|magnet|matrix):(//)?)(&(?![lg]t;)|[^&\s<>'"])+(&(?![lg]t;)|[^&!,.\s<>'"\]):])))" ), RegExpOptions); // email address: // [word chars, dots or dashes]@[word chars, dots or dashes].[word chars] -- cgit v1.2.3 From 651478c1681ba6f93e22c20328a048dbbc263ffe Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Mon, 8 Jul 2019 21:31:40 +0900 Subject: Move serverPart() to the public API Also: Connection::resolveServer() now only accepts MXIDs, not domains. --- lib/util.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'lib/util.cpp') diff --git a/lib/util.cpp b/lib/util.cpp index 88cba959..b9639843 100644 --- a/lib/util.cpp +++ b/lib/util.cpp @@ -112,6 +112,20 @@ qreal QMatrixClient::stringToHueF(const QString &string) return hueF; } +static const auto ServerPartRegEx = QStringLiteral( + "(\\[[^]]+\\]|[^:@]+)" // Either IPv6 address or hostname/IPv4 address + "(?::(\\d{1,5}))?" // Optional port +); + +QString QMatrixClient::serverPart(const QString& mxId) +{ + static QString re = "^[@!#$+].+?:(" // Localpart and colon + % ServerPartRegEx % ")$"; + static QRegularExpression parser(re, + QRegularExpression::UseUnicodePropertiesOption); // Because Asian digits + return parser.match(mxId).captured(1); +} + // Tests for function_traits<> #ifdef Q_CC_CLANG -- cgit v1.2.3 From c05ade838f0fce81f2bbe80a3295618a8a26ff52 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Fri, 2 Aug 2019 19:59:40 +0900 Subject: Apply the new brace wrapping to source files --- lib/util.cpp | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) (limited to 'lib/util.cpp') diff --git a/lib/util.cpp b/lib/util.cpp index 9e0807c6..1919e811 100644 --- a/lib/util.cpp +++ b/lib/util.cpp @@ -124,7 +124,8 @@ QString QMatrixClient::serverPart(const QString& mxId) % ServerPartRegEx % ")$"; static QRegularExpression parser( re, - QRegularExpression::UseUnicodePropertiesOption); // Because Asian digits + QRegularExpression::UseUnicodePropertiesOption); // Because Asian + // digits return parser.match(mxId).captured(1); } @@ -148,16 +149,14 @@ void f2(int, QString); static_assert(std::is_same, QString>::value, "Test fn_arg_t<>"); -struct S -{ +struct S { int mf(); }; static_assert(is_callable_v, "Test member function"); static_assert(returns(), "Test returns<> with member function"); -struct Fo -{ +struct Fo { int operator()(); }; static_assert(is_callable_v, "Test is_callable<> with function object"); @@ -165,8 +164,7 @@ static_assert(function_traits::arg_number == 0, "Test function object"); static_assert(std::is_same, int>::value, "Test return type of function object"); -struct Fo1 -{ +struct Fo1 { void operator()(int); }; static_assert(function_traits::arg_number == 1, "Test function object 1"); @@ -182,13 +180,11 @@ static_assert(std::is_same, int>::value, #endif template -struct fn_object -{ +struct fn_object { static int smf(double) { return 0; } }; template <> -struct fn_object -{ +struct fn_object { void operator()(QString); }; static_assert(is_callable_v>, "Test function object"); -- cgit v1.2.3 From 27ca32a1e5a56e09b9cc1d94224d2831004dcf3d Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Sun, 7 Jul 2019 19:32:34 +0900 Subject: Namespace: QMatrixClient -> Quotient (with back comp alias) --- lib/util.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'lib/util.cpp') diff --git a/lib/util.cpp b/lib/util.cpp index 1919e811..be9656f8 100644 --- a/lib/util.cpp +++ b/lib/util.cpp @@ -32,7 +32,7 @@ static const auto RegExpOptions = | QRegularExpression::UseUnicodePropertiesOption; // Converts all that looks like a URL into HTML links -void QMatrixClient::linkifyUrls(QString& htmlEscapedText) +void Quotient::linkifyUrls(QString& htmlEscapedText) { // Note: outer parentheses are a part of C++ raw string delimiters, not of // the regex (see http://en.cppreference.com/w/cpp/language/string_literal). @@ -70,7 +70,7 @@ void QMatrixClient::linkifyUrls(QString& htmlEscapedText) QStringLiteral(R"(\1\2)")); } -QString QMatrixClient::sanitized(const QString& plainText) +QString Quotient::sanitized(const QString& plainText) { auto text = plainText; text.remove(QChar(0x202e)); // RLO @@ -79,7 +79,7 @@ QString QMatrixClient::sanitized(const QString& plainText) return text; } -QString QMatrixClient::prettyPrint(const QString& plainText) +QString Quotient::prettyPrint(const QString& plainText) { auto pt = plainText.toHtmlEscaped(); linkifyUrls(pt); @@ -88,7 +88,7 @@ QString QMatrixClient::prettyPrint(const QString& plainText) + QStringLiteral(""); } -QString QMatrixClient::cacheLocation(const QString& dirName) +QString Quotient::cacheLocation(const QString& dirName) { const QString cachePath = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) % '/' @@ -99,7 +99,7 @@ QString QMatrixClient::cacheLocation(const QString& dirName) return cachePath; } -qreal QMatrixClient::stringToHueF(const QString& string) +qreal Quotient::stringToHueF(const QString& string) { Q_ASSERT(!string.isEmpty()); QByteArray hash = QCryptographicHash::hash(string.toUtf8(), @@ -118,7 +118,7 @@ static const auto ServerPartRegEx = QStringLiteral( "(?::(\\d{1,5}))?" // Optional port ); -QString QMatrixClient::serverPart(const QString& mxId) +QString Quotient::serverPart(const QString& mxId) { static QString re = "^[@!#$+].+?:(" // Localpart and colon % ServerPartRegEx % ")$"; @@ -135,7 +135,7 @@ QString QMatrixClient::serverPart(const QString& mxId) # pragma clang diagnostic push # pragma ide diagnostic ignored "OCSimplifyInspection" #endif -using namespace QMatrixClient; +using namespace Quotient; int f(); static_assert(std::is_same, int>::value, -- cgit v1.2.3 From 62674f731b0e46d8a413e26ca1c9da27fd9aae9a Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Mon, 5 Aug 2019 10:31:56 +0900 Subject: stringToHueF: pick a safer name for the variable std::string is still a thing, after all. --- lib/util.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'lib/util.cpp') diff --git a/lib/util.cpp b/lib/util.cpp index be9656f8..cc18d9ab 100644 --- a/lib/util.cpp +++ b/lib/util.cpp @@ -99,10 +99,10 @@ QString Quotient::cacheLocation(const QString& dirName) return cachePath; } -qreal Quotient::stringToHueF(const QString& string) +qreal Quotient::stringToHueF(const QString& s) { - Q_ASSERT(!string.isEmpty()); - QByteArray hash = QCryptographicHash::hash(string.toUtf8(), + Q_ASSERT(!s.isEmpty()); + QByteArray hash = QCryptographicHash::hash(s.toUtf8(), QCryptographicHash::Sha1); QDataStream dataStream(qToLittleEndian(hash).left(2)); dataStream.setByteOrder(QDataStream::LittleEndian); -- cgit v1.2.3 From 534a15d05e0b6e1b44b6387003e1475150e848e8 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Sun, 29 Sep 2019 18:01:45 +0900 Subject: function_traits: drop unused pieces is_callable won't ever be needed because std::is_invokable is here; arg_number and returns() didn't find its users; and function_type has been just broken all along for member functions. --- lib/util.cpp | 38 ++------------------------------------ 1 file changed, 2 insertions(+), 36 deletions(-) (limited to 'lib/util.cpp') diff --git a/lib/util.cpp b/lib/util.cpp index cc18d9ab..041a8aba 100644 --- a/lib/util.cpp +++ b/lib/util.cpp @@ -141,62 +141,28 @@ int f(); static_assert(std::is_same, int>::value, "Test fn_return_t<>"); -void f1(int); -static_assert(function_traits::arg_number == 1, - "Test fn_arg_number"); - -void f2(int, QString); -static_assert(std::is_same, QString>::value, +void f1(int, QString); +static_assert(std::is_same, QString>::value, "Test fn_arg_t<>"); -struct S { - int mf(); -}; -static_assert(is_callable_v, "Test member function"); -static_assert(returns(), - "Test returns<> with member function"); - struct Fo { int operator()(); }; -static_assert(is_callable_v, "Test is_callable<> with function object"); -static_assert(function_traits::arg_number == 0, "Test function object"); static_assert(std::is_same, int>::value, "Test return type of function object"); struct Fo1 { void operator()(int); }; -static_assert(function_traits::arg_number == 1, "Test function object 1"); -static_assert(is_callable_v, "Test is_callable<> with function object 1"); static_assert(std::is_same, int>(), "Test fn_arg_t defaulting to first argument"); #if (!defined(_MSC_VER) || _MSC_VER >= 1910) static auto l = [] { return 1; }; -static_assert(is_callable_v, "Test is_callable_v<> with lambda"); static_assert(std::is_same, int>::value, "Test fn_return_t<> with lambda"); #endif -template -struct fn_object { - static int smf(double) { return 0; } -}; -template <> -struct fn_object { - void operator()(QString); -}; -static_assert(is_callable_v>, "Test function object"); -static_assert(returns>(), - "Test returns<> with function object"); -static_assert(!is_callable_v>, "Test non-function object"); -// FIXME: These two don't work -// static_assert(is_callable_v::smf)>, -// "Test static member function"); -// static_assert(returns::smf)>(), -// "Test returns<> with static member function"); - template QString ft(T&&) { -- cgit v1.2.3 From 4be8a673b4ad2f1caa0e34b53ed3b12e750cf569 Mon Sep 17 00:00:00 2001 From: Alexey Andreyev Date: Sat, 30 Nov 2019 03:13:23 +0300 Subject: Fix room highlighting for names with hashtag Fixes #359 --- lib/util.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/util.cpp') diff --git a/lib/util.cpp b/lib/util.cpp index 041a8aba..9f4ac85f 100644 --- a/lib/util.cpp +++ b/lib/util.cpp @@ -56,7 +56,7 @@ void Quotient::linkifyUrls(QString& htmlEscapedText) // https://matrix.org/docs/spec/appendices.html#identifier-grammar static const QRegularExpression MxIdRegExp( QStringLiteral( - R"((^|[^<>/])([!#@][-a-z0-9_=/.]{1,252}:(?:\w|\.|-)+\.\w+(?::\d{1,5})?))"), + R"((^|[^<>/])([!#@][-a-z0-9_=#/.]{1,252}:(?:\w|\.|-)+\.\w+(?::\d{1,5})?))"), RegExpOptions); // NOTE: htmlEscapedText is already HTML-escaped! No literal <,>,&," -- cgit v1.2.3 From 1af2dffb70862a59801a73dacedc695bb062977a Mon Sep 17 00:00:00 2001 From: Andres Salomon Date: Fri, 7 Feb 2020 00:04:36 -0500 Subject: Fix build on big-endian systems On little-endian systems, this call to qToLittleEndian(hash) disappears completely. On big-endian systems, it turns into qbswap(hash), and causes a build error. qbswap() isn't defined for QByteArrays, because QByteArray isn't an array containing multi-byte elements. Since each element is a single byte, machine endianness isn't a factor. (If we really wanted to swap the bytes, we'd need to reverse every 4 bytes of the array.) This just drops the call to QToLittleEndian completely. The lines after it converts part of the hash to a QDataStream, which DOES have to worry about endianness, but that code is also specifically calling QDataStream::setByteOrder to specify little-endian. --- lib/util.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/util.cpp') diff --git a/lib/util.cpp b/lib/util.cpp index 9f4ac85f..4cbebfe2 100644 --- a/lib/util.cpp +++ b/lib/util.cpp @@ -104,7 +104,7 @@ qreal Quotient::stringToHueF(const QString& s) Q_ASSERT(!s.isEmpty()); QByteArray hash = QCryptographicHash::hash(s.toUtf8(), QCryptographicHash::Sha1); - QDataStream dataStream(qToLittleEndian(hash).left(2)); + QDataStream dataStream(hash.left(2)); dataStream.setByteOrder(QDataStream::LittleEndian); quint16 hashValue; dataStream >> hashValue; -- cgit v1.2.3 From 2795c514f283fb274c8e70e81b04be3d02703fe7 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Fri, 29 May 2020 14:31:57 +0200 Subject: util.cpp: drop OptimizeOnFirstUsage option on newer Qt Qt 5.12+ always optimise QRegularExpression on first usage. --- lib/util.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'lib/util.cpp') diff --git a/lib/util.cpp b/lib/util.cpp index 4cbebfe2..61661de8 100644 --- a/lib/util.cpp +++ b/lib/util.cpp @@ -28,7 +28,9 @@ static const auto RegExpOptions = QRegularExpression::CaseInsensitiveOption - | QRegularExpression::OptimizeOnFirstUsageOption +#if QT_VERSION < QT_VERSION_CHECK(5, 12, 0) + | QRegularExpression::OptimizeOnFirstUsageOption // Default since 5.12 +#endif | QRegularExpression::UseUnicodePropertiesOption; // Converts all that looks like a URL into HTML links -- cgit v1.2.3 From bc105c2fef5334d0654071f72e248a0892a9b20b Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Sun, 23 Aug 2020 10:28:59 +0200 Subject: More cleanup; drop Qt bearer management on Qt 5.15+ Qt 5.15 deprecates bearer management. --- lib/util.cpp | 30 +++++++++--------------------- 1 file changed, 9 insertions(+), 21 deletions(-) (limited to 'lib/util.cpp') diff --git a/lib/util.cpp b/lib/util.cpp index 61661de8..797f6d69 100644 --- a/lib/util.cpp +++ b/lib/util.cpp @@ -126,32 +126,30 @@ QString Quotient::serverPart(const QString& mxId) % ServerPartRegEx % ")$"; static QRegularExpression parser( re, - QRegularExpression::UseUnicodePropertiesOption); // Because Asian - // digits + QRegularExpression::UseUnicodePropertiesOption); // Because Asian digits return parser.match(mxId).captured(1); } // Tests for function_traits<> -#ifdef Q_CC_CLANG -# pragma clang diagnostic push -# pragma ide diagnostic ignored "OCSimplifyInspection" -#endif using namespace Quotient; -int f(); -static_assert(std::is_same, int>::value, +int f_(); +static_assert(std::is_same, int>::value, "Test fn_return_t<>"); -void f1(int, QString); -static_assert(std::is_same, QString>::value, +void f1_(int, QString); +static_assert(std::is_same, QString>::value, "Test fn_arg_t<>"); struct Fo { int operator()(); + static constexpr auto l = [] { return 0.0f; }; }; static_assert(std::is_same, int>::value, "Test return type of function object"); +static_assert(std::is_same, float>::value, + "Test return type of lambda"); struct Fo1 { void operator()(int); @@ -159,20 +157,10 @@ struct Fo1 { static_assert(std::is_same, int>(), "Test fn_arg_t defaulting to first argument"); -#if (!defined(_MSC_VER) || _MSC_VER >= 1910) -static auto l = [] { return 1; }; -static_assert(std::is_same, int>::value, - "Test fn_return_t<> with lambda"); -#endif - template -QString ft(T&&) +static QString ft(T&&) { return {}; } static_assert(std::is_same)>, QString&&>(), "Test function templates"); - -#ifdef Q_CC_CLANG -# pragma clang diagnostic pop -#endif -- cgit v1.2.3 From 3c85f049389dec3b0ee6406f0be2cfaf0089f1fe Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Fri, 11 Sep 2020 06:50:45 +0200 Subject: More stringent serverpart checks in user ids May lead to new crashes due to nullptr returned from Connection::user() on more utterly invalid content from the wire that the library still doesn't properly invalidate. This has long been quite a good case for exceptions, or another error-handling framework: Connection::user() can return nullptr either when out of memory or when the id is invalid or empty, and other places are likely to treat invalid ids in different ways but probably just hope that memory exhaustion "never happens", or try to handle it in a quite different way than an empty or invalid id. Something to think of in 0.7. --- lib/util.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/util.cpp') diff --git a/lib/util.cpp b/lib/util.cpp index 797f6d69..023645c1 100644 --- a/lib/util.cpp +++ b/lib/util.cpp @@ -116,13 +116,13 @@ qreal Quotient::stringToHueF(const QString& s) } static const auto ServerPartRegEx = QStringLiteral( - "(\\[[^]]+\\]|[^:@]+)" // Either IPv6 address or hostname/IPv4 address + "(\\[[^][:blank:]]+\\]|[-[:alnum:].]+)" // Either IPv6 address or hostname/IPv4 address "(?::(\\d{1,5}))?" // Optional port ); QString Quotient::serverPart(const QString& mxId) { - static QString re = "^[@!#$+].+?:(" // Localpart and colon + static QString re = "^[@!#$+].*?:(" // Localpart and colon % ServerPartRegEx % ")$"; static QRegularExpression parser( re, -- cgit v1.2.3 From 0e87640560343c15b0a218796509d2d94e1a5c77 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Fri, 11 Sep 2020 07:10:32 +0200 Subject: util.cpp: assert validity of regular expressions --- lib/util.cpp | 3 +++ 1 file changed, 3 insertions(+) (limited to 'lib/util.cpp') diff --git a/lib/util.cpp b/lib/util.cpp index 023645c1..ffb36068 100644 --- a/lib/util.cpp +++ b/lib/util.cpp @@ -60,6 +60,8 @@ void Quotient::linkifyUrls(QString& htmlEscapedText) QStringLiteral( R"((^|[^<>/])([!#@][-a-z0-9_=#/.]{1,252}:(?:\w|\.|-)+\.\w+(?::\d{1,5})?))"), RegExpOptions); + Q_ASSERT(FullUrlRegExp.isValid() && EmailAddressRegExp.isValid() + && MxIdRegExp.isValid()); // NOTE: htmlEscapedText is already HTML-escaped! No literal <,>,&," @@ -127,6 +129,7 @@ QString Quotient::serverPart(const QString& mxId) static QRegularExpression parser( re, QRegularExpression::UseUnicodePropertiesOption); // Because Asian digits + Q_ASSERT(parser.isValid()); return parser.match(mxId).captured(1); } -- cgit v1.2.3 From 0a2acd750a4155969092be674ed3dd9a71b2354f Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Thu, 24 Dec 2020 18:20:04 +0100 Subject: Fix clang-tidy/clazy warnings --- lib/util.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'lib/util.cpp') diff --git a/lib/util.cpp b/lib/util.cpp index ffb36068..875d7522 100644 --- a/lib/util.cpp +++ b/lib/util.cpp @@ -161,9 +161,6 @@ static_assert(std::is_same, int>(), "Test fn_arg_t defaulting to first argument"); template -static QString ft(T&&) -{ - return {}; -} +static QString ft(T&&); static_assert(std::is_same)>, QString&&>(), "Test function templates"); -- cgit v1.2.3