From 5c5d8f2465be70cbc256ec59913ac0a3c7c958be Mon Sep 17 00:00:00 2001
From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com>
Date: Sat, 23 Apr 2022 20:46:10 +0200
Subject: [PATCH 1/2] qt, test: Add tests for searching in `AddressBookPage`
 dialog

---
 src/qt/test/addressbooktests.cpp | 52 ++++++++++++++++++++++++++++++--
 1 file changed, 49 insertions(+), 3 deletions(-)

diff --git a/src/qt/test/addressbooktests.cpp b/src/qt/test/addressbooktests.cpp
index a60e0647e33..9fc633405ae 100644
--- a/src/qt/test/addressbooktests.cpp
+++ b/src/qt/test/addressbooktests.cpp
@@ -24,6 +24,7 @@
 #include <chrono>
 
 #include <QApplication>
+#include <QLineEdit>
 #include <QMessageBox>
 #include <QTableView>
 #include <QTimer>
@@ -102,11 +103,13 @@ void TestAddAddressesToSendBook(interfaces::Node& node)
     QString s_label("already here (s)");
 
     // Define a new address (which should add to the address book successfully).
-    QString new_address;
+    QString new_address_a;
+    QString new_address_b;
 
     std::tie(r_key_dest, preexisting_r_address) = build_address();
     std::tie(s_key_dest, preexisting_s_address) = build_address();
-    std::tie(std::ignore, new_address) = build_address();
+    std::tie(std::ignore, new_address_a) = build_address();
+    std::tie(std::ignore, new_address_b) = build_address();
 
     {
         LOCK(wallet->cs_wallet);
@@ -159,9 +162,52 @@ void TestAddAddressesToSendBook(interfaces::Node& node)
     // Submit a new address which should add successfully - we expect the
     // warning message to be blank.
     EditAddressAndSubmit(
-        &editAddressDialog, QString("new"), new_address, QString(""));
+        &editAddressDialog, QString("io - new A"), new_address_a, QString(""));
     check_addbook_size(3);
     QCOMPARE(table_view->model()->rowCount(), 2);
+
+    EditAddressAndSubmit(
+        &editAddressDialog, QString("io - new B"), new_address_b, QString(""));
+    check_addbook_size(4);
+    QCOMPARE(table_view->model()->rowCount(), 3);
+
+    auto search_line = address_book.findChild<QLineEdit*>("searchLineEdit");
+
+    search_line->setText(r_label);
+    QCOMPARE(table_view->model()->rowCount(), 0);
+
+    search_line->setText(s_label);
+    QCOMPARE(table_view->model()->rowCount(), 1);
+
+    search_line->setText("io");
+    QCOMPARE(table_view->model()->rowCount(), 2);
+
+    // Check wilcard "?".
+    search_line->setText("io?new");
+    QCOMPARE(table_view->model()->rowCount(), 0);
+    search_line->setText("io???new");
+    QCOMPARE(table_view->model()->rowCount(), 2);
+
+    // Check wilcard "*".
+    search_line->setText("io*new");
+    QCOMPARE(table_view->model()->rowCount(), 2);
+    search_line->setText("*");
+    QCOMPARE(table_view->model()->rowCount(), 3);
+
+    search_line->setText(preexisting_r_address);
+    QCOMPARE(table_view->model()->rowCount(), 0);
+
+    search_line->setText(preexisting_s_address);
+    QCOMPARE(table_view->model()->rowCount(), 1);
+
+    search_line->setText(new_address_a);
+    QCOMPARE(table_view->model()->rowCount(), 1);
+
+    search_line->setText(new_address_b);
+    QCOMPARE(table_view->model()->rowCount(), 1);
+
+    search_line->setText("");
+    QCOMPARE(table_view->model()->rowCount(), 3);
 }
 
 } // namespace

From e280087946184b37c8a1eb345fae30ebb07f3384 Mon Sep 17 00:00:00 2001
From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com>
Date: Sat, 21 May 2022 16:07:23 +0200
Subject: [PATCH 2/2] qt: Use `QRegularExpression` in
 `AddressBookSortFilterProxyModel` class
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Co-authored-by: João Barbosa <joao.paulo.barbosa@gmail.com>
---
 src/qt/addressbookpage.cpp | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/src/qt/addressbookpage.cpp b/src/qt/addressbookpage.cpp
index d59a4345f38..a82bd5f73eb 100644
--- a/src/qt/addressbookpage.cpp
+++ b/src/qt/addressbookpage.cpp
@@ -19,6 +19,11 @@
 #include <QMenu>
 #include <QMessageBox>
 #include <QSortFilterProxyModel>
+#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
+#include <QRegularExpression>
+#else
+#include <QRegExp>
+#endif
 
 class AddressBookSortFilterProxyModel final : public QSortFilterProxyModel
 {
@@ -46,12 +51,13 @@ class AddressBookSortFilterProxyModel final : public QSortFilterProxyModel
 
         auto address = model->index(row, AddressTableModel::Address, parent);
 
-        if (filterRegExp().indexIn(model->data(address).toString()) < 0 &&
-            filterRegExp().indexIn(model->data(label).toString()) < 0) {
-            return false;
-        }
-
-        return true;
+#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
+        const auto pattern = filterRegularExpression();
+#else
+        const auto pattern = filterRegExp();
+#endif
+        return (model->data(address).toString().contains(pattern) ||
+                model->data(label).toString().contains(pattern));
     }
 };
 
