From 32456e1b2425f034a646b4e2e47f1e53f2769c10 Mon Sep 17 00:00:00 2001 From: brad2014 Date: Tue, 18 Sep 2018 19:44:39 -0700 Subject: [PATCH] Fix typo in Group::findGroupByPath found by sonarcloud (#2233) * Group::findGroupByPath now limited to search from root (matching current usage) * Factors out Group::findGroupByPathRecursion (on prenormalized strings). --- src/core/Group.cpp | 36 +++++++++++++++++++++--------------- src/core/Group.h | 4 +++- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/core/Group.cpp b/src/core/Group.cpp index a1a1dcd4b..f5338533b 100644 --- a/src/core/Group.cpp +++ b/src/core/Group.cpp @@ -570,30 +570,36 @@ Entry* Group::findEntryByPath(QString entryPath, QString basePath) return nullptr; } -Group* Group::findGroupByPath(QString groupPath, QString basePath) +Group* Group::findGroupByPath(QString groupPath) { - Q_ASSERT(!groupPath.isNull()); - QStringList possiblePaths; - possiblePaths << groupPath; - if (!groupPath.startsWith("/")) { - possiblePaths << QString("/" + groupPath); - } - if (!groupPath.endsWith("/")) { - possiblePaths << QString(groupPath + "/"); - } - if (!groupPath.endsWith("/") && !groupPath.endsWith("/")) { - possiblePaths << QString("/" + groupPath + "/"); - } + // normalize the groupPath by adding missing front and rear slashes. once. + QString normalizedGroupPath; - if (possiblePaths.contains(basePath)) { + if (groupPath == "") { + normalizedGroupPath = QString("/"); // root group + } else { + normalizedGroupPath = ((groupPath.startsWith("/"))? "" : "/") + + groupPath + + ((groupPath.endsWith("/") )? "" : "/"); + } + return findGroupByPathRecursion(normalizedGroupPath, "/"); +} + +Group* Group::findGroupByPathRecursion(QString groupPath, QString basePath) +{ + // paths must be normalized + Q_ASSERT(groupPath.startsWith("/") && groupPath.endsWith("/")); + Q_ASSERT(basePath.startsWith("/") && basePath.endsWith("/")); + + if (groupPath == basePath) { return this; } for (Group* innerGroup : children()) { QString innerBasePath = basePath + innerGroup->name() + "/"; - Group* group = innerGroup->findGroupByPath(groupPath, innerBasePath); + Group* group = innerGroup->findGroupByPathRecursion(groupPath, innerBasePath); if (group != nullptr) { return group; } diff --git a/src/core/Group.h b/src/core/Group.h index 576780555..35619d938 100644 --- a/src/core/Group.h +++ b/src/core/Group.h @@ -109,7 +109,7 @@ public: Entry* findEntry(QString entryId); Entry* findEntryByUuid(const QUuid& uuid); Entry* findEntryByPath(QString entryPath, QString basePath = QString("")); - Group* findGroupByPath(QString groupPath, QString basePath = QString("/")); + Group* findGroupByPath(QString groupPath); QStringList locate(QString locateTerm, QString currentPath = QString("/")); Entry* addEntryWithPath(QString entryPath); void setUuid(const QUuid& uuid); @@ -195,6 +195,8 @@ private: void cleanupParent(); void recCreateDelObjects(); + Group* findGroupByPathRecursion(QString groupPath, QString basePath); + QPointer m_db; QUuid m_uuid; GroupData m_data;