Commit e7cf7ebb authored by Nicolas Fella's avatar Nicolas Fella
Browse files

Avoid another unnecessary string split

We are only interested in the last element and not the whole split list
so doing a full split is inefficient.

Instead use lastIndexOf and mid.

We need to special-case when the string ends with a / since otherwise we
would end up with an empty string
parent a986f15b
......@@ -27,6 +27,8 @@
#include <QMimeDatabase>
#include "util.h"
namespace Kerfuffle {
Archive::Entry::Entry(QObject *parent, const QString &fullPath, const QString &rootNode)
: QObject(parent)
......@@ -110,12 +112,9 @@ void Archive::Entry::setParent(Archive::Entry *parent)
void Archive::Entry::setFullPath(const QString &fullPath)
{
m_fullPath = fullPath;
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
const QStringList pieces = m_fullPath.split(QLatin1Char('/'), QString::SkipEmptyParts);
#else
const QStringList pieces = m_fullPath.split(QLatin1Char('/'), Qt::SkipEmptyParts);
#endif
m_name = pieces.isEmpty() ? QString() : pieces.last();
m_name = Kerfuffle::Util::lastPathSegment(m_fullPath);
}
QString Archive::Entry::fullPath(PathFormat format) const
......
/*
* ark -- archiver for the KDE project
*
* Copyright (C) 2021 Nicolas Fella <nicolas.fella@gmx.de>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES ( INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION ) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* ( INCLUDING NEGLIGENCE OR OTHERWISE ) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef KERFUFFLE_UTILS_H
#define KERFUFFLE_UTILS_H
namespace Kerfuffle {
namespace Util {
// Get the name segment from a path
// e.g. /foo/bar/bla -> bla
// /foo/bar/ -> bar
QString lastPathSegment(const QString &path)
{
if (path.endsWith(QLatin1Char('/'))) {
const int index = path.lastIndexOf(QLatin1Char('/'), -2);
return path.mid(index + 1).chopped(1);
} else {
const int index = path.lastIndexOf(QLatin1Char('/'));
return path.mid(index + 1);
}
}
}
}
#endif
......@@ -25,6 +25,7 @@
#include "archivemodel.h"
#include "ark_debug.h"
#include "jobs.h"
#include "util.h"
#include <KIO/Global>
#include <KLocalizedString>
......@@ -544,12 +545,7 @@ void ArchiveModel::newEntry(Archive::Entry *receivedEntry, InsertBehaviour behav
Archive::Entry *parent = parentFor(receivedEntry, behaviour);
// Create an Archive::Entry.
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
const QStringList path = entryFileName.split(QLatin1Char('/'), QString::SkipEmptyParts);
#else
const QStringList path = entryFileName.split(QLatin1Char('/'), Qt::SkipEmptyParts);
#endif
Archive::Entry *entry = parent->find(path.last());
Archive::Entry *entry = parent->find(Kerfuffle::Util::lastPathSegment(entryFileName));
if (entry) {
entry->copyMetaData(receivedEntry);
entry->setProperty("fullPath", entryFileName);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment