From 629865e29bee209d1e8f92e8b8680ae75e754294 Mon Sep 17 00:00:00 2001 From: lizzie Date: Wed, 27 Aug 2025 06:45:02 +0000 Subject: [PATCH] [common, fs] Use std::string_view instead of std::string&; inline functions that are used rarely Signed-off-by: lizzie --- src/common/fs/path_util.cpp | 20 ----------- src/common/fs/path_util.h | 12 +++++-- src/common/heap_tracker.cpp | 3 +- src/common/string_util.cpp | 43 ++++------------------- src/common/string_util.h | 50 ++++++++++++++++++--------- src/core/file_sys/xts_archive.cpp | 4 +-- src/core/loader/loader.cpp | 16 ++++----- src/frontend_common/content_manager.h | 2 +- 8 files changed, 61 insertions(+), 89 deletions(-) diff --git a/src/common/fs/path_util.cpp b/src/common/fs/path_util.cpp index e032360961..fa1403225e 100644 --- a/src/common/fs/path_util.cpp +++ b/src/common/fs/path_util.cpp @@ -517,24 +517,4 @@ std::string_view GetPathWithoutTop(std::string_view path) { return path.substr(std::min(name_bck_index, name_fwd_index) + 1); } -std::string_view GetFilename(std::string_view path) { - const auto name_index = path.find_last_of("\\/"); - - if (name_index == std::string_view::npos) { - return {}; - } - - return path.substr(name_index + 1); -} - -std::string_view GetExtensionFromFilename(std::string_view name) { - const std::size_t index = name.rfind('.'); - - if (index == std::string_view::npos) { - return {}; - } - - return name.substr(index + 1); -} - } // namespace Common::FS diff --git a/src/common/fs/path_util.h b/src/common/fs/path_util.h index c1f478690a..b34efc472f 100644 --- a/src/common/fs/path_util.h +++ b/src/common/fs/path_util.h @@ -352,9 +352,17 @@ enum class DirectorySeparator { [[nodiscard]] std::string_view GetPathWithoutTop(std::string_view path); // Gets the filename of the path -[[nodiscard]] std::string_view GetFilename(std::string_view path); +[[nodiscard]] inline std::string_view GetFilename(const std::string_view path) noexcept { + if (auto const name_index = path.find_last_of("\\/"); name_index != std::string_view::npos) + return path.substr(name_index + 1); + return {}; +} // Gets the extension of the filename -[[nodiscard]] std::string_view GetExtensionFromFilename(std::string_view name); +[[nodiscard]] inline std::string_view GetExtensionFromFilename(const std::string_view name) noexcept { + if (auto const index = name.rfind('.'); index != std::string_view::npos) + return name.substr(index + 1); + return {}; +} } // namespace Common::FS diff --git a/src/common/heap_tracker.cpp b/src/common/heap_tracker.cpp index d509f2644c..c147c279bd 100644 --- a/src/common/heap_tracker.cpp +++ b/src/common/heap_tracker.cpp @@ -4,10 +4,9 @@ // SPDX-License-Identifier: GPL-2.0-or-later #include -#include - #include "common/heap_tracker.h" #include "common/logging/log.h" +#include "common/assert.h" namespace Common { diff --git a/src/common/string_util.cpp b/src/common/string_util.cpp index 1909aced54..78b4694374 100644 --- a/src/common/string_util.cpp +++ b/src/common/string_util.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include "common/string_util.h" @@ -21,51 +22,21 @@ namespace Common { /// Make a string lowercase -std::string ToLower(std::string str) { +std::string ToLower(const std::string_view sv) { + std::string str{sv}; std::transform(str.begin(), str.end(), str.begin(), - [](unsigned char c) { return static_cast(std::tolower(c)); }); + [](auto const c) { return char(std::tolower(c)); }); return str; } /// Make a string uppercase -std::string ToUpper(std::string str) { +std::string ToUpper(const std::string_view sv) { + std::string str{sv}; std::transform(str.begin(), str.end(), str.begin(), - [](unsigned char c) { return static_cast(std::toupper(c)); }); + [](auto const c) { return char(std::toupper(c)); }); return str; } -std::string StringFromBuffer(std::span data) { - return std::string(data.begin(), std::find(data.begin(), data.end(), '\0')); -} - -std::string StringFromBuffer(std::span data) { - return std::string(data.begin(), std::find(data.begin(), data.end(), '\0')); -} - -// Turns " hej " into "hej". Also handles tabs. -std::string StripSpaces(const std::string& str) { - const std::size_t s = str.find_first_not_of(" \t\r\n"); - - if (str.npos != s) - return str.substr(s, str.find_last_not_of(" \t\r\n") - s + 1); - else - return ""; -} - -// "\"hello\"" is turned to "hello" -// This one assumes that the string has already been space stripped in both -// ends, as done by StripSpaces above, for example. -std::string StripQuotes(const std::string& s) { - if (s.size() && '\"' == s[0] && '\"' == *s.rbegin()) - return s.substr(1, s.size() - 2); - else - return s; -} - -std::string StringFromBool(bool value) { - return value ? "True" : "False"; -} - bool SplitPath(const std::string& full_path, std::string* _pPath, std::string* _pFilename, std::string* _pExtension) { if (full_path.empty()) diff --git a/src/common/string_util.h b/src/common/string_util.h index 53d0549ca7..997ff873a2 100644 --- a/src/common/string_util.h +++ b/src/common/string_util.h @@ -13,18 +13,38 @@ namespace Common { /// Make a string lowercase -[[nodiscard]] std::string ToLower(std::string str); +[[nodiscard]] std::string ToLower(const std::string_view sv); /// Make a string uppercase -[[nodiscard]] std::string ToUpper(std::string str); +[[nodiscard]] std::string ToUpper(const std::string_view sv); -[[nodiscard]] std::string StringFromBuffer(std::span data); -[[nodiscard]] std::string StringFromBuffer(std::span data); +[[nodiscard]] inline std::string StringFromBuffer(std::span data) noexcept { + return std::string(data.begin(), std::find(data.begin(), data.end(), '\0')); +} +[[nodiscard]] inline std::string StringFromBuffer(std::span data) noexcept { + return std::string(data.begin(), std::find(data.begin(), data.end(), '\0')); +} -[[nodiscard]] std::string StripSpaces(const std::string& s); -[[nodiscard]] std::string StripQuotes(const std::string& s); +/// Turns " hej " into "hej". Also handles tabs. +[[nodiscard]] inline std::string StripSpaces(const std::string_view str) noexcept { + const std::size_t s = str.find_first_not_of(" \t\r\n"); + if (str.npos != s) + return std::string{str.substr(s, str.find_last_not_of(" \t\r\n") - s + 1)}; + return {}; +} -[[nodiscard]] std::string StringFromBool(bool value); +/// "\"hello\"" is turned to "hello" +/// This one assumes that the string has already been space stripped in both +/// ends, as done by StripSpaces above, for example. +[[nodiscard]] inline std::string StripQuotes(const std::string_view s) noexcept { + if (s.size() && '\"' == s[0] && '\"' == *s.rbegin()) + return std::string{s.substr(1, s.size() - 2)}; + return std::string{s}; +} + +[[nodiscard]] inline std::string StringFromBool(bool value) noexcept { + return value ? "True" : "False"; +} [[nodiscard]] std::string TabsToSpaces(int tab_size, std::string in); @@ -54,7 +74,7 @@ bool SplitPath(const std::string& full_path, std::string* _pPath, std::string* _ * `other` for equality. */ template -[[nodiscard]] bool ComparePartialString(InIt begin, InIt end, const char* other) { +[[nodiscard]] inline bool ComparePartialString(InIt begin, InIt end, const char* other) noexcept { for (; begin != end && *other != '\0'; ++begin, ++other) { if (*begin != *other) { return false; @@ -64,18 +84,14 @@ template return (begin == end) == (*other == '\0'); } -/** - * Creates a std::string from a fixed-size NUL-terminated char buffer. If the buffer isn't - * NUL-terminated then the string ends at max_len characters. - */ +/// Creates a std::string from a fixed-size NUL-terminated char buffer. If the buffer isn't +/// NUL-terminated then the string ends at max_len characters. [[nodiscard]] std::string StringFromFixedZeroTerminatedBuffer(std::string_view buffer, std::size_t max_len); -/** - * Creates a UTF-16 std::u16string from a fixed-size NUL-terminated char buffer. If the buffer isn't - * null-terminated, then the string ends at the greatest multiple of two less then or equal to - * max_len_bytes. - */ +/// Creates a UTF-16 std::u16string from a fixed-size NUL-terminated char buffer. If the buffer isn't +/// null-terminated, then the string ends at the greatest multiple of two less then or equal to +/// max_len_bytes. [[nodiscard]] std::u16string UTF16StringFromFixedZeroTerminatedBuffer(std::u16string_view buffer, std::size_t max_len); diff --git a/src/core/file_sys/xts_archive.cpp b/src/core/file_sys/xts_archive.cpp index 6692211e1d..be0b792e17 100644 --- a/src/core/file_sys/xts_archive.cpp +++ b/src/core/file_sys/xts_archive.cpp @@ -56,8 +56,8 @@ NAX::NAX(VirtualFile file_) return; } - const std::string two_dir = Common::ToUpper(match[1]); - const std::string nca_id = Common::ToLower(match[2]); + const std::string two_dir = Common::ToUpper(std::string{match[1]}); + const std::string nca_id = Common::ToLower(std::string{match[2]}); status = Parse(fmt::format("/registered/{}/{}.nca", two_dir, nca_id)); } diff --git a/src/core/loader/loader.cpp b/src/core/loader/loader.cpp index 6aabdc75e1..2b2f419417 100644 --- a/src/core/loader/loader.cpp +++ b/src/core/loader/loader.cpp @@ -61,25 +61,23 @@ FileType IdentifyFile(FileSys::VirtualFile file) { FileType GuessFromFilename(const std::string& name) { if (name == "main") return FileType::DeconstructedRomDirectory; - if (name == "00") + else if (name == "00") return FileType::NCA; - const std::string extension = + auto const extension = Common::ToLower(std::string(Common::FS::GetExtensionFromFilename(name))); - if (extension == "nro") return FileType::NRO; - if (extension == "nso") + else if (extension == "nso") return FileType::NSO; - if (extension == "nca") + else if (extension == "nca") return FileType::NCA; - if (extension == "xci") + else if (extension == "xci") return FileType::XCI; - if (extension == "nsp") + else if (extension == "nsp") return FileType::NSP; - if (extension == "kip") + else if (extension == "kip") return FileType::KIP; - return FileType::Unknown; } diff --git a/src/frontend_common/content_manager.h b/src/frontend_common/content_manager.h index c4e97a47b8..9732497f4c 100644 --- a/src/frontend_common/content_manager.h +++ b/src/frontend_common/content_manager.h @@ -160,7 +160,7 @@ inline InstallResult InstallNSP(Core::System& system, FileSys::VfsFilesystem& vf std::shared_ptr nsp; FileSys::VirtualFile file = vfs.OpenFile(filename, FileSys::OpenMode::Read); - if (boost::to_lower_copy(file->GetName()).ends_with(std::string("nsp"))) { + if (boost::to_lower_copy(file->GetName()).ends_with("nsp")) { nsp = std::make_shared(file); if (nsp->IsExtractedType()) { return InstallResult::Failure;