From 29ceaf0a84cb208e25a0f92d8633f7e45cb7c388 Mon Sep 17 00:00:00 2001 From: Rubidium Date: Sun, 27 Apr 2025 17:17:05 +0200 Subject: [PATCH] Codechange: use std::string_view over const char * --- src/base_media_base.h | 6 +++--- src/base_media_func.h | 8 ++++---- src/fios.cpp | 10 +++++----- src/fios.h | 2 +- src/gfxinit.cpp | 2 +- src/music.cpp | 2 +- src/network/core/tcp_content.cpp | 6 +++--- src/script/script_scanner.cpp | 6 +++--- src/script/script_scanner.hpp | 2 +- src/sound.cpp | 2 +- src/textfile_gui.cpp | 6 +++--- src/textfile_gui.h | 2 +- 12 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/base_media_base.h b/src/base_media_base.h index d390bf3dbd..423057acea 100644 --- a/src/base_media_base.h +++ b/src/base_media_base.h @@ -176,7 +176,7 @@ protected: * Get the extension that is used to identify this set. * @return the extension */ - static const char *GetExtension(); + static std::string_view GetExtension(); public: /** * Determine the graphics pack that has to be used. @@ -219,9 +219,9 @@ public: * @param ci The content info to compare it to. * @param md5sum Should the MD5 checksum be tested as well? * @param s The list with sets. - * @return The filename of the first file of the base set, or \c nullptr if there is no match. + * @return The filename of the first file of the base set, or \c std::nullopt if there is no match. */ template -const char *TryGetBaseSetFile(const ContentInfo &ci, bool md5sum, const Tbase_set *s); +std::optional TryGetBaseSetFile(const ContentInfo &ci, bool md5sum, const Tbase_set *s); #endif /* BASE_MEDIA_BASE_H */ diff --git a/src/base_media_func.h b/src/base_media_func.h index 2e0d644ff9..880c242aa9 100644 --- a/src/base_media_func.h +++ b/src/base_media_func.h @@ -319,21 +319,21 @@ template #include "network/core/tcp_content_type.h" -template const char *TryGetBaseSetFile(const ContentInfo &ci, bool md5sum, const Tbase_set *s) +template std::optional TryGetBaseSetFile(const ContentInfo &ci, bool md5sum, const Tbase_set *s) { for (; s != nullptr; s = s->next) { if (s->GetNumMissing() != 0) continue; if (s->shortname != ci.unique_id) continue; - if (!md5sum) return s->files[0].filename.c_str(); + if (!md5sum) return s->files[0].filename; MD5Hash md5; for (const auto &file : s->files) { md5 ^= file.hash; } - if (md5 == ci.md5sum) return s->files[0].filename.c_str(); + if (md5 == ci.md5sum) return s->files[0].filename; } - return nullptr; + return std::nullopt; } template diff --git a/src/fios.cpp b/src/fios.cpp index 4e74c2e246..2011e695c2 100644 --- a/src/fios.cpp +++ b/src/fios.cpp @@ -672,20 +672,20 @@ static ScenarioScanner _scanner; * Find a given scenario based on its unique ID. * @param ci The content info to compare it to. * @param md5sum Whether to look at the md5sum or the id. - * @return The filename of the file, else \c nullptr. + * @return The filename of the file, else \c std::nullopt. */ -const char *FindScenario(const ContentInfo &ci, bool md5sum) +std::optional FindScenario(const ContentInfo &ci, bool md5sum) { _scanner.Scan(false); for (ScenarioIdentifier &id : _scanner) { if (md5sum ? (id.md5sum == ci.md5sum) : (id.scenid == ci.unique_id)) { - return id.filename.c_str(); + return id.filename; } } - return nullptr; + return std::nullopt; } /** @@ -696,7 +696,7 @@ const char *FindScenario(const ContentInfo &ci, bool md5sum) */ bool HasScenario(const ContentInfo &ci, bool md5sum) { - return (FindScenario(ci, md5sum) != nullptr); + return FindScenario(ci, md5sum).has_value(); } /** diff --git a/src/fios.h b/src/fios.h index f73915f698..c4a72266a6 100644 --- a/src/fios.h +++ b/src/fios.h @@ -121,7 +121,7 @@ std::tuple FiosGetScenarioListCallback(SaveLoadOperation std::tuple FiosGetHeightmapListCallback(SaveLoadOperation fop, const std::string &file, const std::string_view ext); void ScanScenarios(); -const char *FindScenario(const ContentInfo &ci, bool md5sum); +std::optional FindScenario(const ContentInfo &ci, bool md5sum); /** * A savegame name automatically numbered. diff --git a/src/gfxinit.cpp b/src/gfxinit.cpp index 3626bc6e39..5cf7a9fada 100644 --- a/src/gfxinit.cpp +++ b/src/gfxinit.cpp @@ -506,7 +506,7 @@ template <> } template <> -/* static */ const char *BaseMedia::GetExtension() +/* static */ std::string_view BaseMedia::GetExtension() { return ".obg"; // OpenTTD Base Graphics } diff --git a/src/music.cpp b/src/music.cpp index a8bf2ee1ac..558ddfb03d 100644 --- a/src/music.cpp +++ b/src/music.cpp @@ -83,7 +83,7 @@ template <> } template <> -/* static */ const char *BaseMedia::GetExtension() +/* static */ std::string_view BaseMedia::GetExtension() { return ".obm"; // OpenTTD Base Music } diff --git a/src/network/core/tcp_content.cpp b/src/network/core/tcp_content.cpp index ea55a30542..48013bafd9 100644 --- a/src/network/core/tcp_content.cpp +++ b/src/network/core/tcp_content.cpp @@ -59,7 +59,7 @@ bool ContentInfo::IsValid() const std::optional ContentInfo::GetTextfile(TextfileType type) const { if (this->state == INVALID) return std::nullopt; - const char *tmp; + std::optional tmp; switch (this->type) { default: NOT_REACHED(); case CONTENT_TYPE_AI: @@ -93,8 +93,8 @@ std::optional ContentInfo::GetTextfile(TextfileType type) const tmp = FindScenario(*this, true); break; } - if (tmp == nullptr) return std::nullopt; - return ::GetTextfile(type, GetContentInfoSubDir(this->type), tmp); + if (!tmp.has_value()) return std::nullopt; + return ::GetTextfile(type, GetContentInfoSubDir(this->type), *tmp); } /** diff --git a/src/script/script_scanner.cpp b/src/script/script_scanner.cpp index 00b612fd2c..c21f152084 100644 --- a/src/script/script_scanner.cpp +++ b/src/script/script_scanner.cpp @@ -240,10 +240,10 @@ bool ScriptScanner::HasScript(const ContentInfo &ci, bool md5sum) return false; } -const char *ScriptScanner::FindMainScript(const ContentInfo &ci, bool md5sum) +std::optional ScriptScanner::FindMainScript(const ContentInfo &ci, bool md5sum) { for (const auto &item : this->info_list) { - if (IsSameScript(ci, md5sum, item.second, this->GetDirectory())) return item.second->GetMainScript().c_str(); + if (IsSameScript(ci, md5sum, item.second, this->GetDirectory())) return item.second->GetMainScript(); } - return nullptr; + return std::nullopt; } diff --git a/src/script/script_scanner.hpp b/src/script/script_scanner.hpp index 7f5b5c3f5a..e457815abf 100644 --- a/src/script/script_scanner.hpp +++ b/src/script/script_scanner.hpp @@ -74,7 +74,7 @@ public: * @param md5sum Whether to check the MD5 checksum. * @return A filename of a file of the content, else \c nullptr. */ - const char *FindMainScript(const ContentInfo &ci, bool md5sum); + std::optional FindMainScript(const ContentInfo &ci, bool md5sum); bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename) override; diff --git a/src/sound.cpp b/src/sound.cpp index 7f72c64959..a079c43e47 100644 --- a/src/sound.cpp +++ b/src/sound.cpp @@ -257,7 +257,7 @@ template <> } template <> -/* static */ const char *BaseMedia::GetExtension() +/* static */ std::string_view BaseMedia::GetExtension() { return ".obs"; // OpenTTD Base Sounds } diff --git a/src/textfile_gui.cpp b/src/textfile_gui.cpp index d03edf5ca8..fdd3d4cd3e 100644 --- a/src/textfile_gui.cpp +++ b/src/textfile_gui.cpp @@ -842,9 +842,9 @@ void TextfileWindow::LoadText(std::string_view buf) * @param filename The filename of the content to look for. * @return The path to the textfile, \c nullptr otherwise. */ -std::optional GetTextfile(TextfileType type, Subdirectory dir, const std::string &filename) +std::optional GetTextfile(TextfileType type, Subdirectory dir, std::string_view filename) { - static const char * const prefixes[] = { + static const std::string_view prefixes[] = { "readme", "changelog", "license", @@ -861,7 +861,7 @@ std::optional GetTextfile(TextfileType type, Subdirectory dir, cons auto slash = filename.find_last_of(PATHSEPCHAR); if (slash == std::string::npos) return std::nullopt; - std::string_view base_path(filename.data(), slash + 1); + std::string_view base_path = filename.substr(0, slash + 1); static const std::initializer_list extensions{ "txt", diff --git a/src/textfile_gui.h b/src/textfile_gui.h index 4c6df055e3..5e66611691 100644 --- a/src/textfile_gui.h +++ b/src/textfile_gui.h @@ -15,7 +15,7 @@ #include "textfile_type.h" #include "window_gui.h" -std::optional GetTextfile(TextfileType type, Subdirectory dir, const std::string &filename); +std::optional GetTextfile(TextfileType type, Subdirectory dir, std::string_view filename); /** Window for displaying a textfile */ struct TextfileWindow : public Window, MissingGlyphSearcher {