[common, fs] Use std::string_view instead of std::string&; inline functions that are used rarely (#330)

Signed-off-by: lizzie <lizzie@eden-emu.dev>
Co-authored-by: crueter <crueter@eden-emu.dev>
Reviewed-on: #330
Reviewed-by: crueter <crueter@eden-emu.dev>
Co-authored-by: lizzie <lizzie@eden-emu.dev>
Co-committed-by: lizzie <lizzie@eden-emu.dev>
This commit is contained in:
lizzie 2025-08-27 22:21:04 +02:00 committed by crueter
parent dae0d7bec6
commit 09e77fa146
Signed by: crueter
GPG key ID: 425ACD2D4830EBC6
8 changed files with 76 additions and 89 deletions

View file

@ -517,24 +517,4 @@ std::string_view GetPathWithoutTop(std::string_view path) {
return path.substr(std::min(name_bck_index, name_fwd_index) + 1); 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 } // namespace Common::FS

View file

@ -352,9 +352,17 @@ enum class DirectorySeparator {
[[nodiscard]] std::string_view GetPathWithoutTop(std::string_view path); [[nodiscard]] std::string_view GetPathWithoutTop(std::string_view path);
// Gets the filename of the 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 // 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 } // namespace Common::FS

View file

@ -4,10 +4,9 @@
// SPDX-License-Identifier: GPL-2.0-or-later // SPDX-License-Identifier: GPL-2.0-or-later
#include <fstream> #include <fstream>
#include <vector>
#include "common/heap_tracker.h" #include "common/heap_tracker.h"
#include "common/logging/log.h" #include "common/logging/log.h"
#include "common/assert.h"
namespace Common { namespace Common {

View file

@ -1,3 +1,6 @@
// SPDX-FileCopyrightText: Copyright 2025 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: 2013 Dolphin Emulator Project // SPDX-FileCopyrightText: 2013 Dolphin Emulator Project
// SPDX-FileCopyrightText: 2014 Citra Emulator Project // SPDX-FileCopyrightText: 2014 Citra Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later // SPDX-License-Identifier: GPL-2.0-or-later
@ -7,6 +10,7 @@
#include <codecvt> #include <codecvt>
#include <locale> #include <locale>
#include <sstream> #include <sstream>
#include <string_view>
#include "common/string_util.h" #include "common/string_util.h"
@ -21,51 +25,21 @@
namespace Common { namespace Common {
/// Make a string lowercase /// 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(), std::transform(str.begin(), str.end(), str.begin(),
[](unsigned char c) { return static_cast<char>(std::tolower(c)); }); [](auto const c) { return char(std::tolower(c)); });
return str; return str;
} }
/// Make a string uppercase /// 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(), std::transform(str.begin(), str.end(), str.begin(),
[](unsigned char c) { return static_cast<char>(std::toupper(c)); }); [](auto const c) { return char(std::toupper(c)); });
return str; return str;
} }
std::string StringFromBuffer(std::span<const u8> data) {
return std::string(data.begin(), std::find(data.begin(), data.end(), '\0'));
}
std::string StringFromBuffer(std::span<const char> 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, bool SplitPath(const std::string& full_path, std::string* _pPath, std::string* _pFilename,
std::string* _pExtension) { std::string* _pExtension) {
if (full_path.empty()) if (full_path.empty())

View file

@ -1,3 +1,6 @@
// SPDX-FileCopyrightText: Copyright 2025 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: 2013 Dolphin Emulator Project // SPDX-FileCopyrightText: 2013 Dolphin Emulator Project
// SPDX-FileCopyrightText: 2014 Citra Emulator Project // SPDX-FileCopyrightText: 2014 Citra Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later // SPDX-License-Identifier: GPL-2.0-or-later
@ -13,18 +16,38 @@
namespace Common { namespace Common {
/// Make a string lowercase /// Make a string lowercase
[[nodiscard]] std::string ToLower(std::string str); [[nodiscard]] std::string ToLower(const std::string_view sv);
/// Make a string uppercase /// 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<const u8> data); [[nodiscard]] inline std::string StringFromBuffer(std::span<const u8> data) noexcept {
[[nodiscard]] std::string StringFromBuffer(std::span<const char> data); return std::string(data.begin(), std::find(data.begin(), data.end(), '\0'));
}
[[nodiscard]] inline std::string StringFromBuffer(std::span<const char> data) noexcept {
return std::string(data.begin(), std::find(data.begin(), data.end(), '\0'));
}
[[nodiscard]] std::string StripSpaces(const std::string& s); /// Turns " hej " into "hej". Also handles tabs.
[[nodiscard]] std::string StripQuotes(const std::string& s); [[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); [[nodiscard]] std::string TabsToSpaces(int tab_size, std::string in);
@ -54,7 +77,7 @@ bool SplitPath(const std::string& full_path, std::string* _pPath, std::string* _
* `other` for equality. * `other` for equality.
*/ */
template <typename InIt> template <typename InIt>
[[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) { for (; begin != end && *other != '\0'; ++begin, ++other) {
if (*begin != *other) { if (*begin != *other) {
return false; return false;
@ -64,18 +87,14 @@ template <typename InIt>
return (begin == end) == (*other == '\0'); return (begin == end) == (*other == '\0');
} }
/** /// Creates a std::string from a fixed-size NUL-terminated char buffer. If the buffer isn't
* 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.
* NUL-terminated then the string ends at max_len characters.
*/
[[nodiscard]] std::string StringFromFixedZeroTerminatedBuffer(std::string_view buffer, [[nodiscard]] std::string StringFromFixedZeroTerminatedBuffer(std::string_view buffer,
std::size_t max_len); std::size_t max_len);
/** /// Creates a UTF-16 std::u16string from a fixed-size NUL-terminated char buffer. If the buffer isn't
* 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
* null-terminated, then the string ends at the greatest multiple of two less then or equal to /// max_len_bytes.
* max_len_bytes.
*/
[[nodiscard]] std::u16string UTF16StringFromFixedZeroTerminatedBuffer(std::u16string_view buffer, [[nodiscard]] std::u16string UTF16StringFromFixedZeroTerminatedBuffer(std::u16string_view buffer,
std::size_t max_len); std::size_t max_len);

View file

@ -1,3 +1,6 @@
// SPDX-FileCopyrightText: Copyright 2025 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later // SPDX-License-Identifier: GPL-2.0-or-later
@ -56,8 +59,8 @@ NAX::NAX(VirtualFile file_)
return; return;
} }
const std::string two_dir = Common::ToUpper(match[1]); const std::string two_dir = Common::ToUpper(std::string{match[1]});
const std::string nca_id = Common::ToLower(match[2]); const std::string nca_id = Common::ToLower(std::string{match[2]});
status = Parse(fmt::format("/registered/{}/{}.nca", two_dir, nca_id)); status = Parse(fmt::format("/registered/{}/{}.nca", two_dir, nca_id));
} }

View file

@ -1,3 +1,6 @@
// SPDX-FileCopyrightText: Copyright 2025 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later // SPDX-License-Identifier: GPL-2.0-or-later
@ -61,25 +64,23 @@ FileType IdentifyFile(FileSys::VirtualFile file) {
FileType GuessFromFilename(const std::string& name) { FileType GuessFromFilename(const std::string& name) {
if (name == "main") if (name == "main")
return FileType::DeconstructedRomDirectory; return FileType::DeconstructedRomDirectory;
if (name == "00") else if (name == "00")
return FileType::NCA; return FileType::NCA;
const std::string extension = auto const extension =
Common::ToLower(std::string(Common::FS::GetExtensionFromFilename(name))); Common::ToLower(std::string(Common::FS::GetExtensionFromFilename(name)));
if (extension == "nro") if (extension == "nro")
return FileType::NRO; return FileType::NRO;
if (extension == "nso") else if (extension == "nso")
return FileType::NSO; return FileType::NSO;
if (extension == "nca") else if (extension == "nca")
return FileType::NCA; return FileType::NCA;
if (extension == "xci") else if (extension == "xci")
return FileType::XCI; return FileType::XCI;
if (extension == "nsp") else if (extension == "nsp")
return FileType::NSP; return FileType::NSP;
if (extension == "kip") else if (extension == "kip")
return FileType::KIP; return FileType::KIP;
return FileType::Unknown; return FileType::Unknown;
} }

View file

@ -1,3 +1,6 @@
// SPDX-FileCopyrightText: Copyright 2025 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: 2024 yuzu Emulator Project // SPDX-FileCopyrightText: 2024 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later // SPDX-License-Identifier: GPL-2.0-or-later
@ -160,7 +163,7 @@ inline InstallResult InstallNSP(Core::System& system, FileSys::VfsFilesystem& vf
std::shared_ptr<FileSys::NSP> nsp; std::shared_ptr<FileSys::NSP> nsp;
FileSys::VirtualFile file = vfs.OpenFile(filename, FileSys::OpenMode::Read); 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<FileSys::NSP>(file); nsp = std::make_shared<FileSys::NSP>(file);
if (nsp->IsExtractedType()) { if (nsp->IsExtractedType()) {
return InstallResult::Failure; return InstallResult::Failure;