1
0
mirror of https://github.com/OpenTTD/OpenTTD synced 2025-12-10 06:52:05 +01:00

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 GitHub
parent a19f6c02e5
commit 06b830dc07
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. */
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("");
}

View File

@@ -507,7 +507,7 @@ std::tuple<FiosType, std::string> FiosGetHeightmapListCallback(SaveLoadOperation
for (Searchpath sp : _valid_searchpaths) {
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;
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()) {
/* 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. */
for (const auto &tar : _tar_filelist[GAME_DIR]) {
for (const auto &[name, entry] : _tar_filelist[GAME_DIR]) {
/* 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. */
if (tar.first.size() <= ldir.size() || tar.first.compare(0, ldir.size(), ldir) != 0) continue;
if (tar.first.compare(tar.first.size() - 4, 4, ".txt") != 0) continue;
if (!name.starts_with(ldir)) continue;
if (!name.ends_with(".txt")) continue;
scanner.AddFile(tar.first, 0, tar_filename);
scanner.AddFile(name, 0, tar_filename);
}
} else {
/* Scan filesystem */

View File

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

View File

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

View File

@@ -875,14 +875,14 @@ static void CheckClientAndServerName()
{
static const std::string fallback_client_name = "Unnamed Client";
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);
_settings_client.network.client_name = fallback_client_name;
}
static const std::string fallback_server_name = "Unnamed Server";
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);
_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);
if (_settings_client.network.admin_password.empty() ||
_settings_client.network.admin_password.compare(password) != 0) {
if (_settings_client.network.admin_password.empty() || _settings_client.network.admin_password != password) {
/* Password is invalid */
return this->SendError(NETWORK_ERROR_WRONG_PASSWORD);
}

View File

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

View File

@@ -1451,7 +1451,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_RCON(Packet &p)
/* We are allowed, nothing more to validate. */
} else if (_settings_client.network.rcon_password.empty()) {
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);
return NETWORK_RECV_STATUS_OKAY;
}
@@ -1648,7 +1648,7 @@ bool NetworkServerChangeClientName(ClientID client_id, const std::string &new_na
{
/* Check if the name's already in use */
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);

View File

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

View File

@@ -111,7 +111,7 @@ struct AIPLChunkHandler : ChunkHandler {
* latest version of the AI instead. */
config->Change(_ai_saveload_name, -1, false);
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, "Configuration switched to Random AI.");
}
@@ -136,7 +136,7 @@ struct AIPLChunkHandler : ChunkHandler {
* latest version of the AI instead. */
config->Change(_ai_saveload_name, -1, false);
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, "A random other AI will be loaded in its place.");
} else {

View File

@@ -80,7 +80,7 @@ struct GSDTChunkHandler : ChunkHandler {
* latest version of the GameScript instead. */
config->Change(_game_saveload_name, -1, false);
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, "This game will continue to run without GameScript.");
} else {

View File

@@ -2861,7 +2861,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;
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) {
auto complevel = full_name.substr(separator + 1);

View File

@@ -1547,7 +1547,7 @@ StringList GetGRFPresetList()
ConfigIniFile ini(_config_file);
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));
}
}

View File

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