From c9acf8e80e2327da7350046805cd6ef4b712bc73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sun, 31 Aug 2025 15:43:57 +0200 Subject: [PATCH] More std::string_view --- Common/File/AndroidStorage.cpp | 6 +++--- Common/UI/View.h | 2 +- Core/ELF/ParamSFO.cpp | 30 +++++++++++++++++------------- Core/ELF/ParamSFO.h | 6 +++--- Core/Util/GameDB.cpp | 2 +- 5 files changed, 25 insertions(+), 21 deletions(-) diff --git a/Common/File/AndroidStorage.cpp b/Common/File/AndroidStorage.cpp index 46d6f9576a..c2345dcedb 100644 --- a/Common/File/AndroidStorage.cpp +++ b/Common/File/AndroidStorage.cpp @@ -163,14 +163,14 @@ static bool ParseFileInfo(std::string_view line, File::FileInfo *fileInfo) { std::vector parts; SplitString(line, '|', parts); if (parts.size() != 4) { - ERROR_LOG(Log::FileSystem, "Bad format (1): %s", line.c_str()); + ERROR_LOG(Log::FileSystem, "Bad format (1): %.*s", (int)line.size(), line.data()); return false; } fileInfo->name = parts[2]; fileInfo->isDirectory = parts[0][0] == 'D'; fileInfo->exists = true; if (1 != sscanf(parts[1].c_str(), "%" PRIu64, &fileInfo->size)) { - ERROR_LOG(Log::FileSystem, "Bad format (2): %s", line.c_str()); + ERROR_LOG(Log::FileSystem, "Bad format (2): %.*s", (int)line.size(), line.data()); return false; } fileInfo->isWritable = true; // TODO: Should be passed as part of the string. @@ -180,7 +180,7 @@ static bool ParseFileInfo(std::string_view line, File::FileInfo *fileInfo) { uint64_t lastModifiedMs = 0; if (1 != sscanf(parts[3].c_str(), "%" PRIu64, &lastModifiedMs)) { - ERROR_LOG(Log::FileSystem, "Bad format (3): %s", line.c_str()); + ERROR_LOG(Log::FileSystem, "Bad format (3): %.*s", (int)line.size(), line.data()); return false; } diff --git a/Common/UI/View.h b/Common/UI/View.h index dcdc5dce1c..23c22ff505 100644 --- a/Common/UI/View.h +++ b/Common/UI/View.h @@ -722,7 +722,7 @@ public: class Choice : public ClickableItem { public: Choice(std::string_view text, LayoutParams *layoutParams = nullptr) - : Choice(text, std::string(), false, layoutParams) {} + : Choice(text, "", false, layoutParams) { } Choice(std::string_view text, ImageID image, LayoutParams *layoutParams = nullptr) : ClickableItem(layoutParams), text_(text), image_(image) {} Choice(std::string_view text, std::string_view smallText, bool selected = false, LayoutParams *layoutParams = nullptr) diff --git a/Core/ELF/ParamSFO.cpp b/Core/ELF/ParamSFO.cpp index 33eacb6ba6..8db59097be 100644 --- a/Core/ELF/ParamSFO.cpp +++ b/Core/ELF/ParamSFO.cpp @@ -44,21 +44,25 @@ struct IndexTable u32_le data_table_offset; /* Offset of the param_data from start of data_table */ }; -void ParamSFOData::SetValue(const std::string &key, unsigned int value, int max_size) { - values[key].type = VT_INT; - values[key].i_value = value; - values[key].max_size = max_size; -} -void ParamSFOData::SetValue(const std::string &key, const std::string &value, int max_size) { - values[key].type = VT_UTF8; - values[key].s_value = value; - values[key].max_size = max_size; +void ParamSFOData::SetValue(std::string_view key, unsigned int value, int max_size) { + auto [it, inserted] = values.try_emplace(std::string(key)); // The string construction only happens if inserted is true. + it->second.type = VT_INT; + it->second.i_value = value; + it->second.max_size = max_size; } -void ParamSFOData::SetValue(const std::string &key, const u8 *value, unsigned int size, int max_size) { - values[key].type = VT_UTF8_SPE; - values[key].SetData(value, size); - values[key].max_size = max_size; +void ParamSFOData::SetValue(std::string_view key, std::string_view value, int max_size) { + auto [it, inserted] = values.try_emplace(std::string(key)); + it->second.type = VT_UTF8; + it->second.s_value = value; + it->second.max_size = max_size; +} + +void ParamSFOData::SetValue(std::string_view key, const u8 *value, unsigned int size, int max_size) { + auto [it, inserted] = values.try_emplace(std::string(key)); + it->second.type = VT_UTF8_SPE; + it->second.SetData(value, size); + it->second.max_size = max_size; } int ParamSFOData::GetValueInt(std::string_view key) const { diff --git a/Core/ELF/ParamSFO.h b/Core/ELF/ParamSFO.h index a737afdbfe..4dc2534a78 100644 --- a/Core/ELF/ParamSFO.h +++ b/Core/ELF/ParamSFO.h @@ -28,9 +28,9 @@ class Path; class ParamSFOData { public: - void SetValue(const std::string &key, unsigned int value, int max_size); - void SetValue(const std::string &key, const std::string &value, int max_size); - void SetValue(const std::string &key, const u8 *value, unsigned int size, int max_size); + void SetValue(std::string_view key, unsigned int value, int max_size); + void SetValue(std::string_view key, std::string_view value, int max_size); + void SetValue(std::string_view key, const u8 *value, unsigned int size, int max_size); int GetValueInt(std::string_view key) const; std::string GetValueString(std::string_view key) const; // Common keys: "TITLE", "DISC_VERSION" diff --git a/Core/Util/GameDB.cpp b/Core/Util/GameDB.cpp index 4d42d175e4..59fd2ee786 100644 --- a/Core/Util/GameDB.cpp +++ b/Core/Util/GameDB.cpp @@ -98,7 +98,7 @@ void GameDB::LoadIfNeeded() { SplitCSVLine(lineString, items); if (items.size() != columns_.size()) { // Bad line - ERROR_LOG(Log::System, "Bad line in CSV file: %s", std::string(lineString).c_str()); + ERROR_LOG(Log::System, "Bad line in CSV file: %.*s", (int)lineString.size(), lineString.data()); continue; }