From 316279f4b4f0f89f876a2e279bb334887c40486b Mon Sep 17 00:00:00 2001 From: frosch Date: Wed, 30 Apr 2025 12:40:07 +0200 Subject: [PATCH] Codechange: Use more std::string_view. --- src/base_media_func.h | 21 +++------------------ src/music.cpp | 15 +++++++++++---- src/music/midifile.cpp | 20 +++++++------------- src/script/script_scanner.cpp | 4 ++-- src/settings.cpp | 23 ++++++++++++++--------- src/strgen/strgen_base.cpp | 4 ++-- 6 files changed, 39 insertions(+), 48 deletions(-) diff --git a/src/base_media_func.h b/src/base_media_func.h index 1384a7476c..aafd4e954a 100644 --- a/src/base_media_func.h +++ b/src/base_media_func.h @@ -143,24 +143,9 @@ bool BaseSet::FillSetDetails(const IniFile &ini, const std::string &path, con this->LogError(full_filename, fmt::format("md5s.{} field missing", filename)); return false; } - const char *c = item->value->c_str(); - for (size_t i = 0; i < file->hash.size() * 2; i++, c++) { - uint j; - if ('0' <= *c && *c <= '9') { - j = *c - '0'; - } else if ('a' <= *c && *c <= 'f') { - j = *c - 'a' + 10; - } else if ('A' <= *c && *c <= 'F') { - j = *c - 'A' + 10; - } else { - this->LogError(full_filename, fmt::format("md5s.{} is malformed: {}", filename, *item->value)); - return false; - } - if (i % 2 == 0) { - file->hash[i / 2] = j << 4; - } else { - file->hash[i / 2] |= j; - } + if (!ConvertHexToBytes(*item->value, file->hash)) { + this->LogError(full_filename, fmt::format("md5s.{} is malformed: {}", filename, *item->value)); + return false; } /* Then find the warning message when the file's missing */ diff --git a/src/music.cpp b/src/music.cpp index 586b17029e..b49c6db252 100644 --- a/src/music.cpp +++ b/src/music.cpp @@ -150,17 +150,24 @@ bool MusicSet::FillSetDetails(const IniFile &ini, const std::string &path, const this->songinfo[i].filetype = MTT_STANDARDMIDI; } - const char *trimmed_filename = filename.c_str(); + std::string_view trimmed_filename{filename}; /* As we possibly add a path to the filename and we compare * on the filename with the path as in the .obm, we need to * keep stripping path elements until we find a match. */ - for (; trimmed_filename != nullptr; trimmed_filename = strchr(trimmed_filename, PATHSEPCHAR)) { + while (!trimmed_filename.empty()) { /* Remove possible double path separator characters from * the beginning, so we don't start reading e.g. root. */ - while (*trimmed_filename == PATHSEPCHAR) trimmed_filename++; + while (trimmed_filename.starts_with(PATHSEPCHAR)) trimmed_filename.remove_prefix(1); item = names != nullptr ? names->GetItem(trimmed_filename) : nullptr; if (item != nullptr && item->value.has_value() && !item->value->empty()) break; + + auto next = trimmed_filename.find(PATHSEPCHAR); + if (next == std::string_view::npos) { + trimmed_filename = {}; + } else { + trimmed_filename.remove_prefix(next); + } } if (this->songinfo[i].filetype == MTT_STANDARDMIDI) { @@ -180,7 +187,7 @@ bool MusicSet::FillSetDetails(const IniFile &ini, const std::string &path, const this->songinfo[i].tracknr = tracknr++; } - item = trimmed_filename != nullptr && timingtrim != nullptr ? timingtrim->GetItem(trimmed_filename) : nullptr; + item = !trimmed_filename.empty() && timingtrim != nullptr ? timingtrim->GetItem(trimmed_filename) : nullptr; if (item != nullptr && item->value.has_value() && !item->value->empty()) { StringConsumer consumer{*item->value}; auto start = consumer.TryReadIntegerBase(10); diff --git a/src/music/midifile.cpp b/src/music/midifile.cpp index 9985c96e85..2ec38161fe 100644 --- a/src/music/midifile.cpp +++ b/src/music/midifile.cpp @@ -1043,25 +1043,19 @@ std::string MidiFile::GetSMFFile(const MusicSongInfo &song) if (song.filetype != MTT_MPSMIDI) return std::string(); - char basename[MAX_PATH]; + std::string tempdirname = FioGetDirectory(Searchpath::SP_AUTODOWNLOAD_DIR, Subdirectory::BASESET_DIR); { - const char *fnstart = strrchr(song.filename.c_str(), PATHSEPCHAR); - if (fnstart == nullptr) { - fnstart = song.filename.c_str(); - } else { - fnstart++; - } + std::string_view basename{song.filename}; + auto fnstart = basename.rfind(PATHSEPCHAR); + if (fnstart != std::string_view::npos) basename.remove_prefix(fnstart + 1); /* Remove all '.' characters from filename */ - char *wp = basename; - for (const char *rp = fnstart; *rp != '\0'; rp++) { - if (*rp != '.') *wp++ = *rp; + tempdirname.reserve(tempdirname.size() + basename.size()); + for (auto c : basename) { + if (c != '.') tempdirname.append(1, c); } - *wp++ = '\0'; } - std::string tempdirname = FioGetDirectory(Searchpath::SP_AUTODOWNLOAD_DIR, Subdirectory::BASESET_DIR); - tempdirname += basename; AppendPathSeparator(tempdirname); FioCreateDirectory(tempdirname); diff --git a/src/script/script_scanner.cpp b/src/script/script_scanner.cpp index 65a8120a67..0aea257240 100644 --- a/src/script/script_scanner.cpp +++ b/src/script/script_scanner.cpp @@ -215,8 +215,8 @@ static bool IsSameScript(const ContentInfo &ci, bool md5sum, ScriptInfo *info, S if (tar.second.tar_filename != iter->first) continue; /* Check the extension. */ - const char *ext = strrchr(tar.first.c_str(), '.'); - if (ext == nullptr || !StrEqualsIgnoreCase(ext, ".nut")) continue; + auto ext = tar.first.rfind('.'); + if (ext == std::string_view::npos || !StrEqualsIgnoreCase(tar.first.substr(ext), ".nut")) continue; checksum.AddFile(tar.first, 0, tar_filename); } diff --git a/src/settings.cpp b/src/settings.cpp index b050667d42..d93548fd8e 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -240,15 +240,15 @@ static std::optional LookupManyOfMany(const std::vector & /** * Parse a string into a vector of uint32s. - * @param p the string to be parsed. Each element in the list is separated by a comma or a space character + * @param str the string to be parsed. Each element in the list is separated by a comma or a space character * @return std::optional with a vector of parsed integers. The optional is empty upon an error. */ -static std::optional> ParseIntList(const char *p) +static std::optional> ParseIntList(std::string_view str) { bool comma = false; // do we accept comma? std::vector result; - StringConsumer consumer{std::string_view{p}}; + StringConsumer consumer{str}; for (;;) { consumer.SkipUntilCharNotIn(StringConsumer::WHITESPACE_NO_NEWLINE); if (!consumer.AnyBytesLeft()) break; @@ -278,15 +278,15 @@ static std::optional> ParseIntList(const char *p) * @param type the type of elements the array holds (eg INT8, UINT16, etc.) * @return return true on success and false on error */ -static bool LoadIntList(const char *str, void *array, int nelems, VarType type) +static bool LoadIntList(std::optional str, void *array, int nelems, VarType type) { size_t elem_size = SlVarSize(type); - if (str == nullptr) { + if (!str.has_value()) { memset(array, 0, nelems * elem_size); return true; } - auto opt_items = ParseIntList(str); + auto opt_items = ParseIntList(*str); if (!opt_items.has_value() || opt_items->size() != (size_t)nelems) return false; char *p = static_cast(array); @@ -672,7 +672,12 @@ void StringSettingDesc::ParseValue(const IniItem *item, void *object) const void ListSettingDesc::ParseValue(const IniItem *item, void *object) const { - const char *str = (item == nullptr) ? this->def : item->value.has_value() ? item->value->c_str() : nullptr; + std::optional str; + if (item != nullptr) { + str = item->value; + } else if (this->def != nullptr) { + str = this->def; + } void *ptr = GetVariableAddress(object, this->save); if (!LoadIntList(str, ptr, this->save.length, GetVarMemType(this->save.conv))) { _settings_error_list.emplace_back( @@ -1032,7 +1037,7 @@ static void GraphicsSetLoadConfig(IniFile &ini) } if (const IniItem *item = group->GetItem("extra_params"); item != nullptr && item->value) { - auto params = ParseIntList(item->value->c_str()); + auto params = ParseIntList(*item->value); if (params.has_value()) { BaseGraphics::ini_data.extra_params = params.value(); } else { @@ -1099,7 +1104,7 @@ static GRFConfigList GRFLoadConfig(const IniFile &ini, std::string_view grpname, /* Parse parameters */ if (item.value.has_value() && !item.value->empty()) { - auto params = ParseIntList(item.value->c_str()); + auto params = ParseIntList(*item.value); if (params.has_value()) { c->SetParams(params.value()); } else { diff --git a/src/strgen/strgen_base.cpp b/src/strgen/strgen_base.cpp index dddb258378..1313edad8b 100644 --- a/src/strgen/strgen_base.cpp +++ b/src/strgen/strgen_base.cpp @@ -23,7 +23,7 @@ StrgenState _strgen; static bool _translated; ///< Whether the current language is not the master language -static const char *_cur_ident; +static std::string_view _cur_ident; static ParsedCommandStruct _cur_pcs; static size_t _cur_argidx; @@ -759,7 +759,7 @@ void LanguageWriter::WriteLang(const StringData &data) std::string output; StringBuilder builder(output); - _cur_ident = ls->name.c_str(); + _cur_ident = ls->name; _strgen.cur_line = ls->line; /* Produce a message if a string doesn't have a translation. */