From 839b9372f8ca6b067e63cce9eceb17f3da9778ea Mon Sep 17 00:00:00 2001 From: Dennis Oberst Date: Thu, 27 Mar 2025 19:07:08 +0100 Subject: [PATCH] tools [utils]: implement file operation with std::filesystem Coverity noticed inefficiencies of unneeded copies. Fix this by using std::filesystem for these operations. Coverity-Id: 479416 Pick-to: 6.9 6.8 Change-Id: I44666f4ca791763c13152c4861e78a122b8cfc50 Reviewed-by: Alexey Edelev --- src/tools/qtprotoccommon/utils.cpp | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/src/tools/qtprotoccommon/utils.cpp b/src/tools/qtprotoccommon/utils.cpp index afb4d832..83d221c7 100644 --- a/src/tools/qtprotoccommon/utils.cpp +++ b/src/tools/qtprotoccommon/utils.cpp @@ -9,6 +9,7 @@ #include #include #include +#include namespace { const std::string_view asciiSpacing = " \t\n\r\f\v"; @@ -80,20 +81,13 @@ void asciiToUpper(std::string &str) std::string removeFileSuffix(std::string_view fileName) { - std::string result(fileName); - size_t dot = result.rfind('.'), slash = result.rfind('/'); - if (dot != std::string::npos && (slash == std::string::npos || dot > slash)) - result.resize(dot); - return result; + std::filesystem::path path(fileName); + return (path.parent_path() / path.stem()).string(); } std::string extractFileBasename(std::string_view fileName) { - std::string result(fileName); - size_t dot = result.rfind('.'), slash = result.rfind('/'); - if (dot != std::string::npos && (slash == std::string::npos || dot > slash)) - result.resize(dot); - return slash != std::string::npos ? result.substr(slash + 1) : result; + return std::filesystem::path(fileName).stem().string(); } std::string toValidIdentifier(std::string_view name)