diff options
| author | lizzie <lizzie@eden-emu.dev> | 2025-08-27 22:21:04 +0200 |
|---|---|---|
| committer | crueter <crueter@eden-emu.dev> | 2025-08-27 22:21:04 +0200 |
| commit | 09e77fa146f6630c8a01f59166a572dd58a30d2e (patch) | |
| tree | c70cd6e7e95bf822e0ec0ece6061cf94d29eb59e /src/common | |
| parent | dae0d7bec678253bd5ee5d10690bec5ffbfab024 (diff) | |
[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: https://git.eden-emu.dev/eden-emu/eden/pulls/330
Reviewed-by: crueter <crueter@eden-emu.dev>
Co-authored-by: lizzie <lizzie@eden-emu.dev>
Co-committed-by: lizzie <lizzie@eden-emu.dev>
Diffstat (limited to 'src/common')
| -rw-r--r-- | src/common/fs/path_util.cpp | 20 | ||||
| -rw-r--r-- | src/common/fs/path_util.h | 12 | ||||
| -rw-r--r-- | src/common/heap_tracker.cpp | 3 | ||||
| -rw-r--r-- | src/common/string_util.cpp | 46 | ||||
| -rw-r--r-- | src/common/string_util.h | 53 |
5 files changed, 57 insertions, 77 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 <fstream> -#include <vector> - #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..7bcbe737b6 100644 --- a/src/common/string_util.cpp +++ b/src/common/string_util.cpp @@ -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: 2014 Citra Emulator Project // SPDX-License-Identifier: GPL-2.0-or-later @@ -7,6 +10,7 @@ #include <codecvt> #include <locale> #include <sstream> +#include <string_view> #include "common/string_util.h" @@ -21,51 +25,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<char>(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<char>(std::toupper(c)); }); + [](auto const c) { return char(std::toupper(c)); }); 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, std::string* _pExtension) { if (full_path.empty()) diff --git a/src/common/string_util.h b/src/common/string_util.h index 53d0549ca7..8ed87cdadc 100644 --- a/src/common/string_util.h +++ b/src/common/string_util.h @@ -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: 2014 Citra Emulator Project // SPDX-License-Identifier: GPL-2.0-or-later @@ -13,18 +16,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<const u8> data); -[[nodiscard]] std::string StringFromBuffer(std::span<const char> data); +[[nodiscard]] inline std::string StringFromBuffer(std::span<const u8> data) noexcept { + 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); -[[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 +77,7 @@ bool SplitPath(const std::string& full_path, std::string* _pPath, std::string* _ * `other` for equality. */ 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) { if (*begin != *other) { return false; @@ -64,18 +87,14 @@ template <typename InIt> 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); |
