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 <alexey.edelev@qt.io>
This commit is contained in:
Dennis Oberst 2025-03-27 19:07:08 +01:00
parent 4710cacf13
commit 839b9372f8
1 changed files with 4 additions and 10 deletions

View File

@ -9,6 +9,7 @@
#include <cctype> #include <cctype>
#include <cassert> #include <cassert>
#include <regex> #include <regex>
#include <filesystem>
namespace { namespace {
const std::string_view asciiSpacing = " \t\n\r\f\v"; 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 removeFileSuffix(std::string_view fileName)
{ {
std::string result(fileName); std::filesystem::path path(fileName);
size_t dot = result.rfind('.'), slash = result.rfind('/'); return (path.parent_path() / path.stem()).string();
if (dot != std::string::npos && (slash == std::string::npos || dot > slash))
result.resize(dot);
return result;
} }
std::string extractFileBasename(std::string_view fileName) std::string extractFileBasename(std::string_view fileName)
{ {
std::string result(fileName); return std::filesystem::path(fileName).stem().string();
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;
} }
std::string toValidIdentifier(std::string_view name) std::string toValidIdentifier(std::string_view name)