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).
This commit is contained in:
brad2014 2018-09-18 19:44:39 -07:00 committed by Jonathan White
parent d9fcdd2920
commit 32456e1b24
2 changed files with 24 additions and 16 deletions

View File

@ -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;
}

View File

@ -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<Database> m_db;
QUuid m_uuid;
GroupData m_data;