Utils: Add FilePath::{suffix,fileName}View functions

Can save a few cycles in some cases.

Change-Id: I0da3ad9ae2127e20f8e90d68f924e87661028071
Reviewed-by: Alessandro Portale <alessandro.portale@qt.io>
This commit is contained in:
hjk
2022-11-23 12:53:04 +01:00
parent 46cfdbf285
commit a94f37c8b0
2 changed files with 19 additions and 7 deletions

View File

@@ -248,10 +248,15 @@ QString FilePath::nativePath() const
return data;
}
QString FilePath::fileName() const
QStringView FilePath::fileNameView() const
{
const QStringView fp = pathView();
return fp.mid(fp.lastIndexOf('/') + 1).toString();
return fp.mid(fp.lastIndexOf('/') + 1);
}
QString FilePath::fileName() const
{
return fileNameView().toString();
}
QString FilePath::fileNameWithPathComponents(int pathComponents) const
@@ -313,17 +318,22 @@ QString FilePath::completeBaseName() const
/// (but not including) the last '.'. In case of ".ui.qml" it will
/// be treated as one suffix.
QString FilePath::suffix() const
QStringView FilePath::suffixView() const
{
const QString &name = fileName();
if (name.endsWith(".ui.qml"))
return "ui.qml";
const QStringView name = fileNameView();
if (name.endsWith(u".ui.qml"))
return u"ui.qml";
const int index = name.lastIndexOf('.');
if (index >= 0)
return name.mid(index + 1);
return {};
}
QString FilePath::suffix() const
{
return suffixView().toString();
}
/// \returns the complete suffix (extension) of the file.
///
/// The complete suffix consists of all characters in the file after
@@ -572,7 +582,7 @@ static FilePaths appendExeExtensions(const Environment &env, const FilePath &exe
if (executable.osType() == OsTypeWindows) {
// Check all the executable extensions on windows:
// PATHEXT is only used if the executable has no extension
if (executable.suffix().isEmpty()) {
if (executable.suffixView().isEmpty()) {
const QStringList extensions = env.expandedValueForKey("PATHEXT").split(';');
for (const QString &ext : extensions)

View File

@@ -81,11 +81,13 @@ public:
void setParts(const QStringView scheme, const QStringView host, const QStringView path);
QString fileName() const;
QStringView fileNameView() const;
QString fileNameWithPathComponents(int pathComponents) const;
QString baseName() const;
QString completeBaseName() const;
QString suffix() const;
QStringView suffixView() const;
QString completeSuffix() const;
[[nodiscard]] FilePath pathAppended(const QString &str) const;