From bf9a526d63854f1ebb5bf102d54cc170f29ce370 Mon Sep 17 00:00:00 2001 From: Rolf Eike Beer Date: Fri, 22 Mar 2019 21:25:59 +0100 Subject: [PATCH] slightly optimize ConnectionListModel -avoid object copies -use some more bullet proof C++11 constructs -avoid using a QRegExp, simple string matches are faster Signed-off-by: Rolf Eike Beer --- core/connectionlistmodel.cpp | 10 ++++++---- core/connectionlistmodel.h | 10 +++++----- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/core/connectionlistmodel.cpp b/core/connectionlistmodel.cpp index 8cd270347..0e4d69828 100644 --- a/core/connectionlistmodel.cpp +++ b/core/connectionlistmodel.cpp @@ -21,7 +21,7 @@ int ConnectionListModel::rowCount(const QModelIndex&) const return m_addresses.count(); } -void ConnectionListModel::addAddress(const QString address) +void ConnectionListModel::addAddress(const QString &address) { if (!m_addresses.contains(address)) { beginInsertRows(QModelIndex(), rowCount(), rowCount()); @@ -37,8 +37,10 @@ void ConnectionListModel::removeAllAddresses() endRemoveRows(); } -int ConnectionListModel::indexOf(QString address) +int ConnectionListModel::indexOf(const QString &address) const { - const QRegExp re(".*" + address + ".*", Qt::CaseInsensitive); - return m_addresses.indexOf(re); + for (int i = 0; i < m_addresses.count(); i++) + if (m_addresses.at(i).contains(address, Qt::CaseInsensitive)) + return i; + return -1; } diff --git a/core/connectionlistmodel.h b/core/connectionlistmodel.h index d6ffe831c..d05b753c7 100644 --- a/core/connectionlistmodel.h +++ b/core/connectionlistmodel.h @@ -6,12 +6,12 @@ class ConnectionListModel : public QAbstractListModel { Q_OBJECT public: - ConnectionListModel(QObject *parent = 0); - QVariant data(const QModelIndex &index, int role) const; - int rowCount(const QModelIndex &parent = QModelIndex()) const; - void addAddress(const QString address); + ConnectionListModel(QObject *parent = nullptr); + QVariant data(const QModelIndex &index, int role) const override; + int rowCount(const QModelIndex &parent = QModelIndex()) const override; + void addAddress(const QString &address); void removeAllAddresses(); - int indexOf(QString address); + int indexOf(const QString &address) const; private: QStringList m_addresses; };