From 398444e67635c158a20301d1a32a1c45bfdd4056 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sun, 22 Jul 2018 01:23:29 -0400 Subject: file_util, vfs: Use std::string_view where applicable Avoids unnecessary construction of std::string instances where applicable. --- src/core/file_sys/vfs_real.cpp | 57 +++++++++++++++++++++++++++++------------- 1 file changed, 39 insertions(+), 18 deletions(-) (limited to 'src/core/file_sys/vfs_real.cpp') diff --git a/src/core/file_sys/vfs_real.cpp b/src/core/file_sys/vfs_real.cpp index 27fd464ae..095fec77e 100644 --- a/src/core/file_sys/vfs_real.cpp +++ b/src/core/file_sys/vfs_real.cpp @@ -72,12 +72,15 @@ size_t RealVfsFile::Write(const u8* data, size_t length, size_t offset) { return backing.WriteBytes(data, length); } -bool RealVfsFile::Rename(const std::string& name) { - const auto out = FileUtil::Rename(GetName(), name); - path = parent_path + DIR_SEP + name; +bool RealVfsFile::Rename(std::string_view name) { + std::string name_str(name.begin(), name.end()); + const auto out = FileUtil::Rename(GetName(), name_str); + + path = (parent_path + DIR_SEP).append(name); path_components = parent_components; - path_components.push_back(name); + path_components.push_back(std::move(name_str)); backing = FileUtil::IOFile(path, PermissionsToCharArray(perms).c_str()); + return out; } @@ -135,36 +138,54 @@ std::shared_ptr RealVfsDirectory::GetParentDirectory() const { return std::make_shared(parent_path, perms); } -std::shared_ptr RealVfsDirectory::CreateSubdirectory(const std::string& name) { - if (!FileUtil::CreateDir(path + DIR_SEP + name)) +std::shared_ptr RealVfsDirectory::CreateSubdirectory(std::string_view name) { + const std::string subdir_path = (path + DIR_SEP).append(name); + + if (!FileUtil::CreateDir(subdir_path)) { return nullptr; - subdirectories.emplace_back(std::make_shared(path + DIR_SEP + name, perms)); + } + + subdirectories.emplace_back(std::make_shared(subdir_path, perms)); return subdirectories.back(); } -std::shared_ptr RealVfsDirectory::CreateFile(const std::string& name) { - if (!FileUtil::CreateEmptyFile(path + DIR_SEP + name)) +std::shared_ptr RealVfsDirectory::CreateFile(std::string_view name) { + const std::string file_path = (path + DIR_SEP).append(name); + + if (!FileUtil::CreateEmptyFile(file_path)) { return nullptr; - files.emplace_back(std::make_shared(path + DIR_SEP + name, perms)); + } + + files.emplace_back(std::make_shared(file_path, perms)); return files.back(); } -bool RealVfsDirectory::DeleteSubdirectory(const std::string& name) { - return FileUtil::DeleteDirRecursively(path + DIR_SEP + name); +bool RealVfsDirectory::DeleteSubdirectory(std::string_view name) { + const std::string subdir_path = (path + DIR_SEP).append(name); + + return FileUtil::DeleteDirRecursively(subdir_path); } -bool RealVfsDirectory::DeleteFile(const std::string& name) { - auto file = GetFile(name); - if (file == nullptr) +bool RealVfsDirectory::DeleteFile(std::string_view name) { + const auto file = GetFile(name); + + if (file == nullptr) { return false; + } + files.erase(std::find(files.begin(), files.end(), file)); + auto real_file = std::static_pointer_cast(file); real_file->Close(); - return FileUtil::Delete(path + DIR_SEP + name); + + const std::string file_path = (path + DIR_SEP).append(name); + return FileUtil::Delete(file_path); } -bool RealVfsDirectory::Rename(const std::string& name) { - return FileUtil::Rename(path, parent_path + DIR_SEP + name); +bool RealVfsDirectory::Rename(std::string_view name) { + const std::string new_name = (parent_path + DIR_SEP).append(name); + + return FileUtil::Rename(path, new_name); } bool RealVfsDirectory::ReplaceFileWithSubdirectory(VirtualFile file, VirtualDir dir) { -- cgit v1.2.3