From b31e65033c796d5dc7e557b3753c14f67be718f8 Mon Sep 17 00:00:00 2001 From: Tony Mountifield Date: Tue, 16 Nov 2021 22:19:15 +0000 Subject: [PATCH 1/4] Migrate from QRegExp to QRegularExpression for Qt6 compatibility --- src/chatdlg.cpp | 4 ++-- src/util.cpp | 21 ++++++++++++--------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/chatdlg.cpp b/src/chatdlg.cpp index b6968a863b..3c0b3cb388 100644 --- a/src/chatdlg.cpp +++ b/src/chatdlg.cpp @@ -114,11 +114,11 @@ void CChatDlg::AddChatText ( QString strChatText ) // analyze strChatText to check if hyperlink (limit ourselves to http(s)://) but do not // replace the hyperlinks if any HTML code for a hyperlink was found (the user has done the HTML // coding hisself and we should not mess with that) - if ( !strChatText.contains ( QRegExp ( "href\\s*=|src\\s*=" ) ) ) + if ( !strChatText.contains ( QRegularExpression ( "href\\s*=|src\\s*=" ) ) ) { // searches for all occurrences of http(s) and cuts until a space (\S matches any non-white-space // character and the + means that matches the previous element one or more times.) - strChatText.replace ( QRegExp ( "(https?://\\S+)" ), "\\1" ); + strChatText.replace ( QRegularExpression ( "(https?://\\S+)" ), "\\1" ); } // add new text in chat window diff --git a/src/util.cpp b/src/util.cpp index cdacd3cb42..ed5dc6a84c 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -704,25 +704,28 @@ bool NetworkUtil::ParseNetworkAddress ( QString strAddress, CHostAddress& HostAd // hostname:port // (where addr4or6 is a literal IPv4 or IPv6 address, and addr4 is a literal IPv4 address - bool bLiteralAddr = false; - QRegExp rx1 ( "^\\[([^]]*)\\](?::(\\d+))?$" ); // [addr4or6] or [addr4or6]:port - QRegExp rx2 ( "^([^:]*)(?::(\\d+))?$" ); // addr4 or addr4:port or host or host:port + bool bLiteralAddr = false; + QRegularExpression rx1 ( "^\\[([^]]*)\\](?::(\\d+))?$" ); // [addr4or6] or [addr4or6]:port + QRegularExpression rx2 ( "^([^:]*)(?::(\\d+))?$" ); // addr4 or addr4:port or host or host:port QString strPort; + QRegularExpressionMatch rx1match = rx1.match ( strAddress ); + QRegularExpressionMatch rx2match = rx2.match ( strAddress ); + // parse input address with rx1 and rx2 in turn, capturing address/host and port - if ( rx1.indexIn ( strAddress ) == 0 ) + if ( rx1match.capturedStart() == 0 ) { // literal address within [] - strAddress = rx1.cap ( 1 ); - strPort = rx1.cap ( 2 ); + strAddress = rx1match.captured ( 1 ); + strPort = rx1match.captured ( 2 ); bLiteralAddr = true; // don't allow hostname within [] } - else if ( rx2.indexIn ( strAddress ) == 0 ) + else if ( rx2match.capturedStart() == 0 ) { // hostname or IPv4 address - strAddress = rx2.cap ( 1 ); - strPort = rx2.cap ( 2 ); + strAddress = rx2match.captured ( 1 ); + strPort = rx2match.captured ( 2 ); } else { From 8df207c105aa4f7c90c0e8fae901958e1f7f713c Mon Sep 17 00:00:00 2001 From: Tony Mountifield Date: Wed, 17 Nov 2021 14:48:56 +0000 Subject: [PATCH 2/4] Exclude certain terminating characters from matched hyperlink --- src/chatdlg.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/chatdlg.cpp b/src/chatdlg.cpp index 3c0b3cb388..4230a4d13d 100644 --- a/src/chatdlg.cpp +++ b/src/chatdlg.cpp @@ -118,7 +118,9 @@ void CChatDlg::AddChatText ( QString strChatText ) { // searches for all occurrences of http(s) and cuts until a space (\S matches any non-white-space // character and the + means that matches the previous element one or more times.) - strChatText.replace ( QRegularExpression ( "(https?://\\S+)" ), "\\1" ); + // The (??\\[\\]{}])(??\\[\\]{}]))" ), + "\\1" ); } // add new text in chat window From 845fb9a20e28aa9f529647c2e62979d71dc1c356 Mon Sep 17 00:00:00 2001 From: Tony Mountifield Date: Fri, 26 Nov 2021 10:55:16 +0000 Subject: [PATCH 3/4] Add explanatory comment to describe the regex --- src/chatdlg.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/chatdlg.cpp b/src/chatdlg.cpp index 4230a4d13d..80b75093e3 100644 --- a/src/chatdlg.cpp +++ b/src/chatdlg.cpp @@ -118,7 +118,14 @@ void CChatDlg::AddChatText ( QString strChatText ) { // searches for all occurrences of http(s) and cuts until a space (\S matches any non-white-space // character and the + means that matches the previous element one or more times.) - // The (??\\[\\]{}]) is a negative look-behind assertion that disallows the match + // from ending with one of the characters !"'()+,.:;<=>?[]{} + // - (??\\[\\]{}]) is a negative look-behind assertion that disallows the match + // from ending with a ? followed by one of the characters !"'()+,.:;<=>?[]{} + // These last two parts must be separate, as a look-behind assertion must be fixed length. strChatText.replace ( QRegularExpression ( "(https?://\\S+(??\\[\\]{}])(??\\[\\]{}]))" ), "\\1" ); } From 23d778b008166873dae3f301abecb692d6e4e04f Mon Sep 17 00:00:00 2001 From: Tony Mountifield Date: Tue, 30 Nov 2021 20:55:29 +0000 Subject: [PATCH 4/4] Clarify regex with #define for common part Co-authored-by: Noam Postavsky --- src/chatdlg.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/chatdlg.cpp b/src/chatdlg.cpp index 80b75093e3..bc04c00672 100644 --- a/src/chatdlg.cpp +++ b/src/chatdlg.cpp @@ -126,7 +126,8 @@ void CChatDlg::AddChatText ( QString strChatText ) // - (??\\[\\]{}]) is a negative look-behind assertion that disallows the match // from ending with a ? followed by one of the characters !"'()+,.:;<=>?[]{} // These last two parts must be separate, as a look-behind assertion must be fixed length. - strChatText.replace ( QRegularExpression ( "(https?://\\S+(??\\[\\]{}])(??\\[\\]{}]))" ), +#define PUNCT_NOEND_URL "[!\"'()+,.:;<=>?\\[\\]{}]" + strChatText.replace ( QRegularExpression ( "(https?://\\S+(?\\1" ); }