Codechange: Prefer string equality instead of comparison. (#14727)

This commit is contained in:
Peter Nelson
2025-10-24 20:30:03 +01:00
committed by dP
parent 596e347d14
commit de9984f379
15 changed files with 37 additions and 44 deletions

View File

@@ -79,7 +79,7 @@ bool BaseSet<T>::FillSetDetails(const IniFile &ini, const std::string &path, con
/* Add the translations of the descriptions too. */ /* Add the translations of the descriptions too. */
for (const IniItem &titem : metadata->items) { for (const IniItem &titem : metadata->items) {
if (titem.name.compare(0, 12, "description.") != 0) continue; if (!titem.name.starts_with("description.")) continue;
this->description[titem.name.substr(12)] = titem.value.value_or(""); this->description[titem.name.substr(12)] = titem.value.value_or("");
} }

View File

@@ -507,7 +507,7 @@ std::tuple<FiosType, std::string> FiosGetHeightmapListCallback(SaveLoadOperation
for (Searchpath sp : _valid_searchpaths) { for (Searchpath sp : _valid_searchpaths) {
std::string buf = FioGetDirectory(sp, HEIGHTMAP_DIR); std::string buf = FioGetDirectory(sp, HEIGHTMAP_DIR);
if (buf.compare(0, buf.size(), it->second.tar_filename, 0, buf.size()) == 0) { if (it->second.tar_filename.starts_with(buf)) {
match = true; match = true;
break; break;
} }

View File

@@ -225,15 +225,15 @@ static std::shared_ptr<GameStrings> LoadTranslations()
if (!tar_filename.empty() && (iter = _tar_list[GAME_DIR].find(tar_filename)) != _tar_list[GAME_DIR].end()) { if (!tar_filename.empty() && (iter = _tar_list[GAME_DIR].find(tar_filename)) != _tar_list[GAME_DIR].end()) {
/* The main script is in a tar file, so find all files that /* The main script is in a tar file, so find all files that
* are in the same tar and add them to the langfile scanner. */ * are in the same tar and add them to the langfile scanner. */
for (const auto &tar : _tar_filelist[GAME_DIR]) { for (const auto &[name, entry] : _tar_filelist[GAME_DIR]) {
/* Not in the same tar. */ /* Not in the same tar. */
if (tar.second.tar_filename != iter->first) continue; if (entry.tar_filename != iter->first) continue;
/* Check the path and extension. */ /* Check the path and extension. */
if (tar.first.size() <= ldir.size() || tar.first.compare(0, ldir.size(), ldir) != 0) continue; if (!name.starts_with(ldir)) continue;
if (tar.first.compare(tar.first.size() - 4, 4, ".txt") != 0) continue; if (!name.ends_with(".txt")) continue;
scanner.AddFile(tar.first, 0, tar_filename); scanner.AddFile(name, 0, tar_filename);
} }
} else { } else {
/* Scan filesystem */ /* Scan filesystem */

View File

@@ -173,8 +173,7 @@ IniGroup &IniLoadFile::CreateGroup(std::string_view name)
*/ */
void IniLoadFile::RemoveGroup(std::string_view name) void IniLoadFile::RemoveGroup(std::string_view name)
{ {
size_t len = name.length(); this->groups.remove_if([&name](const IniGroup &group) { return group.name.starts_with(name); });
this->groups.remove_if([&name, &len](const IniGroup &group) { return group.name.compare(0, len, name) == 0; });
} }
/** /**

View File

@@ -67,10 +67,8 @@ struct LanguagePackHeader {
*/ */
uint8_t GetGenderIndex(std::string_view gender_str) const uint8_t GetGenderIndex(std::string_view gender_str) const
{ {
for (uint8_t i = 0; i < MAX_NUM_GENDERS; i++) { auto it = std::ranges::find(this->genders, gender_str);
if (gender_str.compare(this->genders[i]) == 0) return i; return static_cast<uint8_t>(std::distance(std::begin(this->genders), it));
}
return MAX_NUM_GENDERS;
} }
/** /**
@@ -80,10 +78,8 @@ struct LanguagePackHeader {
*/ */
uint8_t GetCaseIndex(std::string_view case_str) const uint8_t GetCaseIndex(std::string_view case_str) const
{ {
for (uint8_t i = 0; i < MAX_NUM_CASES; i++) { auto it = std::ranges::find(this->cases, case_str);
if (case_str.compare(this->cases[i]) == 0) return i; return static_cast<uint8_t>(std::distance(std::begin(this->cases), it));
}
return MAX_NUM_CASES;
} }
}; };
/** Make sure the size is right. */ /** Make sure the size is right. */

View File

@@ -875,14 +875,14 @@ static void CheckClientAndServerName()
{ {
static const std::string fallback_client_name = "Unnamed Client"; static const std::string fallback_client_name = "Unnamed Client";
StrTrimInPlace(_settings_client.network.client_name); StrTrimInPlace(_settings_client.network.client_name);
if (_settings_client.network.client_name.empty() || _settings_client.network.client_name.compare(fallback_client_name) == 0) { if (_settings_client.network.client_name.empty() || _settings_client.network.client_name == fallback_client_name) {
Debug(net, 1, "No \"client_name\" has been set, using \"{}\" instead. Please set this now using the \"name <new name>\" command", fallback_client_name); Debug(net, 1, "No \"client_name\" has been set, using \"{}\" instead. Please set this now using the \"name <new name>\" command", fallback_client_name);
_settings_client.network.client_name = fallback_client_name; _settings_client.network.client_name = fallback_client_name;
} }
static const std::string fallback_server_name = "Unnamed Server"; static const std::string fallback_server_name = "Unnamed Server";
StrTrimInPlace(_settings_client.network.server_name); StrTrimInPlace(_settings_client.network.server_name);
if (_settings_client.network.server_name.empty() || _settings_client.network.server_name.compare(fallback_server_name) == 0) { if (_settings_client.network.server_name.empty() || _settings_client.network.server_name == fallback_server_name) {
Debug(net, 1, "No \"server_name\" has been set, using \"{}\" instead. Please set this now using the \"server_name <new name>\" command", fallback_server_name); Debug(net, 1, "No \"server_name\" has been set, using \"{}\" instead. Please set this now using the \"server_name <new name>\" command", fallback_server_name);
_settings_client.network.server_name = fallback_server_name; _settings_client.network.server_name = fallback_server_name;
} }

View File

@@ -633,8 +633,7 @@ NetworkRecvStatus ServerNetworkAdminSocketHandler::Receive_ADMIN_JOIN(Packet &p)
std::string password = p.Recv_string(NETWORK_PASSWORD_LENGTH); std::string password = p.Recv_string(NETWORK_PASSWORD_LENGTH);
if (_settings_client.network.admin_password.empty() || if (_settings_client.network.admin_password.empty() || _settings_client.network.admin_password != password) {
_settings_client.network.admin_password.compare(password) != 0) {
/* Password is invalid */ /* Password is invalid */
return this->SendError(NETWORK_ERROR_WRONG_PASSWORD); return this->SendError(NETWORK_ERROR_WRONG_PASSWORD);
} }

View File

@@ -538,7 +538,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_CLIENT_INFO(Pac
ci = NetworkClientInfo::GetByClientID(client_id); ci = NetworkClientInfo::GetByClientID(client_id);
if (ci != nullptr) { if (ci != nullptr) {
if (playas == ci->client_playas && name.compare(ci->client_name) != 0) { if (playas == ci->client_playas && name != ci->client_name) {
/* Client name changed, display the change */ /* Client name changed, display the change */
NetworkTextMessage(NETWORK_ACTION_NAME_CHANGE, CC_DEFAULT, false, ci->client_name, name); NetworkTextMessage(NETWORK_ACTION_NAME_CHANGE, CC_DEFAULT, false, ci->client_name, name);
} else if (playas != ci->client_playas) { } else if (playas != ci->client_playas) {
@@ -1297,17 +1297,17 @@ void NetworkUpdateClientName(const std::string &client_name)
if (ci == nullptr) return; if (ci == nullptr) return;
/* Don't change the name if it is the same as the old name */ /* Don't change the name if it is the same as the old name */
if (client_name.compare(ci->client_name) != 0) { if (client_name == ci->client_name) return;
if (!_network_server) {
MyClient::SendSetName(client_name); if (!_network_server) {
} else { MyClient::SendSetName(client_name);
/* Copy to a temporary buffer so no #n gets added after our name in the settings when there are duplicate names. */ } else {
std::string temporary_name = client_name; /* Copy to a temporary buffer so no #n gets added after our name in the settings when there are duplicate names. */
if (NetworkMakeClientNameUnique(temporary_name)) { std::string temporary_name = client_name;
NetworkTextMessage(NETWORK_ACTION_NAME_CHANGE, CC_DEFAULT, false, ci->client_name, temporary_name); if (NetworkMakeClientNameUnique(temporary_name)) {
ci->client_name = std::move(temporary_name); NetworkTextMessage(NETWORK_ACTION_NAME_CHANGE, CC_DEFAULT, false, ci->client_name, temporary_name);
NetworkUpdateClientInfo(CLIENT_ID_SERVER); ci->client_name = std::move(temporary_name);
} NetworkUpdateClientInfo(CLIENT_ID_SERVER);
} }
} }
} }

View File

@@ -1460,7 +1460,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_RCON(Packet &p)
/* We are allowed, nothing more to validate. */ /* We are allowed, nothing more to validate. */
} else if (_settings_client.network.rcon_password.empty()) { } else if (_settings_client.network.rcon_password.empty()) {
return NETWORK_RECV_STATUS_OKAY; return NETWORK_RECV_STATUS_OKAY;
} else if (_settings_client.network.rcon_password.compare(password) != 0) { } else if (_settings_client.network.rcon_password != password) {
Debug(net, 1, "[rcon] Wrong password from client-id {}", this->client_id); Debug(net, 1, "[rcon] Wrong password from client-id {}", this->client_id);
return NETWORK_RECV_STATUS_OKAY; return NETWORK_RECV_STATUS_OKAY;
} }
@@ -1657,7 +1657,7 @@ bool NetworkServerChangeClientName(ClientID client_id, const std::string &new_na
{ {
/* Check if the name's already in use */ /* Check if the name's already in use */
for (NetworkClientInfo *ci : NetworkClientInfo::Iterate()) { for (NetworkClientInfo *ci : NetworkClientInfo::Iterate()) {
if (ci->client_name.compare(new_name) == 0) return false; if (ci->client_name == new_name) return false;
} }
NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(client_id); NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(client_id);

View File

@@ -38,9 +38,8 @@ struct EFCParam {
bool Add(const std::wstring_view &font) bool Add(const std::wstring_view &font)
{ {
for (const auto &entry : this->fonts) { auto it = std::ranges::find(this->fonts, font);
if (font.compare(entry) == 0) return false; if (it != std::end(this->fonts)) return false;
}
this->fonts.emplace_back(font); this->fonts.emplace_back(font);

View File

@@ -111,7 +111,7 @@ struct AIPLChunkHandler : ChunkHandler {
* latest version of the AI instead. */ * latest version of the AI instead. */
config->Change(_ai_saveload_name, -1, false); config->Change(_ai_saveload_name, -1, false);
if (!config->HasScript()) { if (!config->HasScript()) {
if (_ai_saveload_name.compare("%_dummy") != 0) { if (_ai_saveload_name != "%_dummy") {
Debug(script, 0, "The savegame has an AI by the name '{}', version {} which is no longer available.", _ai_saveload_name, _ai_saveload_version); Debug(script, 0, "The savegame has an AI by the name '{}', version {} which is no longer available.", _ai_saveload_name, _ai_saveload_version);
Debug(script, 0, "Configuration switched to Random AI."); Debug(script, 0, "Configuration switched to Random AI.");
} }
@@ -136,7 +136,7 @@ struct AIPLChunkHandler : ChunkHandler {
* latest version of the AI instead. */ * latest version of the AI instead. */
config->Change(_ai_saveload_name, -1, false); config->Change(_ai_saveload_name, -1, false);
if (!config->HasScript()) { if (!config->HasScript()) {
if (_ai_saveload_name.compare("%_dummy") != 0) { if (_ai_saveload_name != "%_dummy") {
Debug(script, 0, "The savegame has an AI by the name '{}', version {} which is no longer available.", _ai_saveload_name, _ai_saveload_version); Debug(script, 0, "The savegame has an AI by the name '{}', version {} which is no longer available.", _ai_saveload_name, _ai_saveload_version);
Debug(script, 0, "A random other AI will be loaded in its place."); Debug(script, 0, "A random other AI will be loaded in its place.");
} else { } else {

View File

@@ -80,7 +80,7 @@ struct GSDTChunkHandler : ChunkHandler {
* latest version of the GameScript instead. */ * latest version of the GameScript instead. */
config->Change(_game_saveload_name, -1, false); config->Change(_game_saveload_name, -1, false);
if (!config->HasScript()) { if (!config->HasScript()) {
if (_game_saveload_name.compare("%_dummy") != 0) { if (_game_saveload_name != "%_dummy") {
Debug(script, 0, "The savegame has an GameScript by the name '{}', version {} which is no longer available.", _game_saveload_name, _game_saveload_version); Debug(script, 0, "The savegame has an GameScript by the name '{}', version {} which is no longer available.", _game_saveload_name, _game_saveload_version);
Debug(script, 0, "This game will continue to run without GameScript."); Debug(script, 0, "This game will continue to run without GameScript.");
} else { } else {

View File

@@ -3073,7 +3073,7 @@ static std::pair<const SaveLoadFormat &, uint8_t> GetSavegameFormat(std::string_
std::string_view name = has_comp_level ? full_name.substr(0, separator) : full_name; std::string_view name = has_comp_level ? full_name.substr(0, separator) : full_name;
for (const auto &slf : _saveload_formats) { for (const auto &slf : _saveload_formats) {
if (slf.init_write != nullptr && name.compare(slf.name) == 0) { if (slf.init_write != nullptr && name == slf.name) {
if (has_comp_level) { if (has_comp_level) {
auto complevel = full_name.substr(separator + 1); auto complevel = full_name.substr(separator + 1);

View File

@@ -1548,7 +1548,7 @@ StringList GetGRFPresetList()
ConfigIniFile ini(_config_file); ConfigIniFile ini(_config_file);
for (const IniGroup &group : ini.groups) { for (const IniGroup &group : ini.groups) {
if (group.name.compare(0, 7, "preset-") == 0) { if (group.name.starts_with("preset-")) {
list.push_back(group.name.substr(7)); list.push_back(group.name.substr(7));
} }
} }

View File

@@ -574,7 +574,7 @@ static void MaxVehiclesChanged(int32_t)
*/ */
static bool ReplaceAsteriskWithEmptyPassword(std::string &newval) static bool ReplaceAsteriskWithEmptyPassword(std::string &newval)
{ {
if (newval.compare("*") == 0) newval.clear(); if (newval == "*") newval.clear();
return true; return true;
} }