From e3858e81dcddf10c89529d684b451f244cb365de Mon Sep 17 00:00:00 2001 From: Rubidium Date: Sun, 20 Apr 2025 23:08:19 +0200 Subject: [PATCH] Codechange: use std::string_view over const std::string& for finding files --- src/fileio.cpp | 12 ++++++------ src/fileio_func.h | 6 +++--- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/fileio.cpp b/src/fileio.cpp index 3d05b2a3c5..14106c61c0 100644 --- a/src/fileio.cpp +++ b/src/fileio.cpp @@ -118,7 +118,7 @@ static void FillValidSearchPaths(bool only_local_path) * @param subdir the subdirectory to look in * @return true if and only if the file can be opened */ -bool FioCheckFileExists(const std::string &filename, Subdirectory subdir) +bool FioCheckFileExists(std::string_view filename, Subdirectory subdir) { auto f = FioFOpenFile(filename, "rb", subdir); return f.has_value(); @@ -141,7 +141,7 @@ bool FileExists(const std::string &filename) * @param filename Filename to look for. * @return String containing the path if the path was found, else an empty string. */ -std::string FioFindFullPath(Subdirectory subdir, const std::string &filename) +std::string FioFindFullPath(Subdirectory subdir, std::string_view filename) { assert(subdir < NUM_SUBDIRS); @@ -180,7 +180,7 @@ std::string FioFindDirectory(Subdirectory subdir) return _personal_dir; } -static std::optional FioFOpenFileSp(const std::string &filename, const char *mode, Searchpath sp, Subdirectory subdir, size_t *filesize) +static std::optional FioFOpenFileSp(std::string_view filename, const char *mode, Searchpath sp, Subdirectory subdir, size_t *filesize) { #if defined(_WIN32) /* fopen is implemented as a define with ellipses for @@ -195,7 +195,7 @@ static std::optional FioFOpenFileSp(const std::string &filename, con if (subdir == NO_DIRECTORY) { buf = filename; } else { - buf = _searchpaths[sp] + _subdirs[subdir] + filename; + buf = fmt::format("{}{}{}", _searchpaths[sp], _subdirs[subdir], filename); } auto f = FileHandle::Open(buf, mode); @@ -239,7 +239,7 @@ static std::optional FioFOpenFileTar(const TarFileListEntry &entry, * @param subdir Subdirectory to open. * @return File handle of the opened file, or \c nullptr if the file is not available. */ -std::optional FioFOpenFile(const std::string &filename, const char *mode, Subdirectory subdir, size_t *filesize) +std::optional FioFOpenFile(std::string_view filename, const char *mode, Subdirectory subdir, size_t *filesize) { std::optional f = std::nullopt; assert(subdir < NUM_SUBDIRS || subdir == NO_DIRECTORY); @@ -252,7 +252,7 @@ std::optional FioFOpenFile(const std::string &filename, const char * /* We can only use .tar in case of data-dir, and read-mode */ if (!f.has_value() && mode[0] == 'r' && subdir != NO_DIRECTORY) { /* Filenames in tars are always forced to be lowercase */ - std::string resolved_name = filename; + std::string resolved_name{filename}; strtolower(resolved_name); /* Resolve ".." */ diff --git a/src/fileio_func.h b/src/fileio_func.h index 2b65924a33..34a2b2f1a2 100644 --- a/src/fileio_func.h +++ b/src/fileio_func.h @@ -13,9 +13,9 @@ #include "core/enum_type.hpp" #include "fileio_type.h" -std::optional FioFOpenFile(const std::string &filename, const char *mode, Subdirectory subdir, size_t *filesize = nullptr); -bool FioCheckFileExists(const std::string &filename, Subdirectory subdir); -std::string FioFindFullPath(Subdirectory subdir, const std::string &filename); +std::optional FioFOpenFile(std::string_view filename, const char *mode, Subdirectory subdir, size_t *filesize = nullptr); +bool FioCheckFileExists(std::string_view filename, Subdirectory subdir); +std::string FioFindFullPath(Subdirectory subdir, std::string_view filename); std::string FioGetDirectory(Searchpath sp, Subdirectory subdir); std::string FioFindDirectory(Subdirectory subdir); void FioCreateDirectory(const std::string &name);