1
0
Fork 0

Codechange: Use more std::string_view.

pull/14172/head
frosch 2025-04-30 12:40:07 +02:00 committed by frosch
parent 9cf36dac39
commit 316279f4b4
6 changed files with 39 additions and 48 deletions

View File

@ -143,24 +143,9 @@ bool BaseSet<T>::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 */

View File

@ -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<uint>(10);

View File

@ -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);

View File

@ -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);
}

View File

@ -240,15 +240,15 @@ static std::optional<uint32_t> LookupManyOfMany(const std::vector<std::string> &
/**
* 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<std::vector<uint32_t>> ParseIntList(const char *p)
static std::optional<std::vector<uint32_t>> ParseIntList(std::string_view str)
{
bool comma = false; // do we accept comma?
std::vector<uint32_t> 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<std::vector<uint32_t>> 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<std::string_view> 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<char *>(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<std::string_view> 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 {

View File

@ -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. */