mirror of https://github.com/OpenTTD/OpenTTD
Codechange: Use more std::string_view.
parent
9cf36dac39
commit
316279f4b4
|
@ -143,25 +143,10 @@ bool BaseSet<T>::FillSetDetails(const IniFile &ini, const std::string &path, con
|
||||||
this->LogError(full_filename, fmt::format("md5s.{} field missing", filename));
|
this->LogError(full_filename, fmt::format("md5s.{} field missing", filename));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
const char *c = item->value->c_str();
|
if (!ConvertHexToBytes(*item->value, file->hash)) {
|
||||||
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));
|
this->LogError(full_filename, fmt::format("md5s.{} is malformed: {}", filename, *item->value));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (i % 2 == 0) {
|
|
||||||
file->hash[i / 2] = j << 4;
|
|
||||||
} else {
|
|
||||||
file->hash[i / 2] |= j;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Then find the warning message when the file's missing */
|
/* Then find the warning message when the file's missing */
|
||||||
item = origin != nullptr ? origin->GetItem(filename) : nullptr;
|
item = origin != nullptr ? origin->GetItem(filename) : nullptr;
|
||||||
|
|
|
@ -150,17 +150,24 @@ bool MusicSet::FillSetDetails(const IniFile &ini, const std::string &path, const
|
||||||
this->songinfo[i].filetype = MTT_STANDARDMIDI;
|
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
|
/* 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
|
* on the filename with the path as in the .obm, we need to
|
||||||
* keep stripping path elements until we find a match. */
|
* 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
|
/* Remove possible double path separator characters from
|
||||||
* the beginning, so we don't start reading e.g. root. */
|
* 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;
|
item = names != nullptr ? names->GetItem(trimmed_filename) : nullptr;
|
||||||
if (item != nullptr && item->value.has_value() && !item->value->empty()) break;
|
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) {
|
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++;
|
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()) {
|
if (item != nullptr && item->value.has_value() && !item->value->empty()) {
|
||||||
StringConsumer consumer{*item->value};
|
StringConsumer consumer{*item->value};
|
||||||
auto start = consumer.TryReadIntegerBase<uint>(10);
|
auto start = consumer.TryReadIntegerBase<uint>(10);
|
||||||
|
|
|
@ -1043,25 +1043,19 @@ std::string MidiFile::GetSMFFile(const MusicSongInfo &song)
|
||||||
|
|
||||||
if (song.filetype != MTT_MPSMIDI) return std::string();
|
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);
|
std::string_view basename{song.filename};
|
||||||
if (fnstart == nullptr) {
|
auto fnstart = basename.rfind(PATHSEPCHAR);
|
||||||
fnstart = song.filename.c_str();
|
if (fnstart != std::string_view::npos) basename.remove_prefix(fnstart + 1);
|
||||||
} else {
|
|
||||||
fnstart++;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Remove all '.' characters from filename */
|
/* Remove all '.' characters from filename */
|
||||||
char *wp = basename;
|
tempdirname.reserve(tempdirname.size() + basename.size());
|
||||||
for (const char *rp = fnstart; *rp != '\0'; rp++) {
|
for (auto c : basename) {
|
||||||
if (*rp != '.') *wp++ = *rp;
|
if (c != '.') tempdirname.append(1, c);
|
||||||
}
|
}
|
||||||
*wp++ = '\0';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string tempdirname = FioGetDirectory(Searchpath::SP_AUTODOWNLOAD_DIR, Subdirectory::BASESET_DIR);
|
|
||||||
tempdirname += basename;
|
|
||||||
AppendPathSeparator(tempdirname);
|
AppendPathSeparator(tempdirname);
|
||||||
FioCreateDirectory(tempdirname);
|
FioCreateDirectory(tempdirname);
|
||||||
|
|
||||||
|
|
|
@ -215,8 +215,8 @@ static bool IsSameScript(const ContentInfo &ci, bool md5sum, ScriptInfo *info, S
|
||||||
if (tar.second.tar_filename != iter->first) continue;
|
if (tar.second.tar_filename != iter->first) continue;
|
||||||
|
|
||||||
/* Check the extension. */
|
/* Check the extension. */
|
||||||
const char *ext = strrchr(tar.first.c_str(), '.');
|
auto ext = tar.first.rfind('.');
|
||||||
if (ext == nullptr || !StrEqualsIgnoreCase(ext, ".nut")) continue;
|
if (ext == std::string_view::npos || !StrEqualsIgnoreCase(tar.first.substr(ext), ".nut")) continue;
|
||||||
|
|
||||||
checksum.AddFile(tar.first, 0, tar_filename);
|
checksum.AddFile(tar.first, 0, tar_filename);
|
||||||
}
|
}
|
||||||
|
|
|
@ -240,15 +240,15 @@ static std::optional<uint32_t> LookupManyOfMany(const std::vector<std::string> &
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse a string into a vector of uint32s.
|
* 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.
|
* @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?
|
bool comma = false; // do we accept comma?
|
||||||
std::vector<uint32_t> result;
|
std::vector<uint32_t> result;
|
||||||
|
|
||||||
StringConsumer consumer{std::string_view{p}};
|
StringConsumer consumer{str};
|
||||||
for (;;) {
|
for (;;) {
|
||||||
consumer.SkipUntilCharNotIn(StringConsumer::WHITESPACE_NO_NEWLINE);
|
consumer.SkipUntilCharNotIn(StringConsumer::WHITESPACE_NO_NEWLINE);
|
||||||
if (!consumer.AnyBytesLeft()) break;
|
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.)
|
* @param type the type of elements the array holds (eg INT8, UINT16, etc.)
|
||||||
* @return return true on success and false on error
|
* @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);
|
size_t elem_size = SlVarSize(type);
|
||||||
if (str == nullptr) {
|
if (!str.has_value()) {
|
||||||
memset(array, 0, nelems * elem_size);
|
memset(array, 0, nelems * elem_size);
|
||||||
return true;
|
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;
|
if (!opt_items.has_value() || opt_items->size() != (size_t)nelems) return false;
|
||||||
|
|
||||||
char *p = static_cast<char *>(array);
|
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
|
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);
|
void *ptr = GetVariableAddress(object, this->save);
|
||||||
if (!LoadIntList(str, ptr, this->save.length, GetVarMemType(this->save.conv))) {
|
if (!LoadIntList(str, ptr, this->save.length, GetVarMemType(this->save.conv))) {
|
||||||
_settings_error_list.emplace_back(
|
_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) {
|
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()) {
|
if (params.has_value()) {
|
||||||
BaseGraphics::ini_data.extra_params = params.value();
|
BaseGraphics::ini_data.extra_params = params.value();
|
||||||
} else {
|
} else {
|
||||||
|
@ -1099,7 +1104,7 @@ static GRFConfigList GRFLoadConfig(const IniFile &ini, std::string_view grpname,
|
||||||
|
|
||||||
/* Parse parameters */
|
/* Parse parameters */
|
||||||
if (item.value.has_value() && !item.value->empty()) {
|
if (item.value.has_value() && !item.value->empty()) {
|
||||||
auto params = ParseIntList(item.value->c_str());
|
auto params = ParseIntList(*item.value);
|
||||||
if (params.has_value()) {
|
if (params.has_value()) {
|
||||||
c->SetParams(params.value());
|
c->SetParams(params.value());
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -23,7 +23,7 @@
|
||||||
|
|
||||||
StrgenState _strgen;
|
StrgenState _strgen;
|
||||||
static bool _translated; ///< Whether the current language is not the master language
|
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 ParsedCommandStruct _cur_pcs;
|
||||||
static size_t _cur_argidx;
|
static size_t _cur_argidx;
|
||||||
|
|
||||||
|
@ -759,7 +759,7 @@ void LanguageWriter::WriteLang(const StringData &data)
|
||||||
|
|
||||||
std::string output;
|
std::string output;
|
||||||
StringBuilder builder(output);
|
StringBuilder builder(output);
|
||||||
_cur_ident = ls->name.c_str();
|
_cur_ident = ls->name;
|
||||||
_strgen.cur_line = ls->line;
|
_strgen.cur_line = ls->line;
|
||||||
|
|
||||||
/* Produce a message if a string doesn't have a translation. */
|
/* Produce a message if a string doesn't have a translation. */
|
||||||
|
|
Loading…
Reference in New Issue