1
0
Fork 0

Codechange: Remove usages of stoi and stol. (#14196)

pull/14197/head
frosch 2025-05-03 17:46:30 +02:00 committed by GitHub
parent b6afd8d2b6
commit 9ac9798d7f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 59 additions and 48 deletions

View File

@ -2132,9 +2132,9 @@ static bool ConNetworkAuthorizedKey(std::span<std::string_view> argv)
authorized_key = argv[3]; authorized_key = argv[3];
if (StrStartsWithIgnoreCase(authorized_key, "client:")) { if (StrStartsWithIgnoreCase(authorized_key, "client:")) {
std::string id_string(authorized_key.substr(7)); auto value = ParseInteger<uint32_t>(authorized_key.substr(7));
authorized_key = NetworkGetPublicKeyOfClient(static_cast<ClientID>(std::stoi(id_string))); if (value.has_value()) authorized_key = NetworkGetPublicKeyOfClient(static_cast<ClientID>(*value));
if (authorized_key.empty()) { if (!value.has_value() || authorized_key.empty()) {
IConsolePrint(CC_ERROR, "You must enter a valid client id; see 'clients'."); IConsolePrint(CC_ERROR, "You must enter a valid client id; see 'clients'.");
return false; return false;
} }
@ -2154,8 +2154,8 @@ static bool ConNetworkAuthorizedKey(std::span<std::string_view> argv)
} }
if (StrStartsWithIgnoreCase(type, "company:")) { if (StrStartsWithIgnoreCase(type, "company:")) {
std::string id_string(type.substr(8)); auto value = ParseInteger<uint32_t>(type.substr(8));
Company *c = Company::GetIfValid(std::stoi(id_string) - 1); Company *c = value.has_value() ? Company::GetIfValid(*value - 1) : nullptr;
if (c == nullptr) { if (c == nullptr) {
IConsolePrint(CC_ERROR, "You must enter a valid company id; see 'companies'."); IConsolePrint(CC_ERROR, "You must enter a valid company id; see 'companies'.");
return false; return false;

View File

@ -27,6 +27,7 @@
#include "game/game_gui.hpp" #include "game/game_gui.hpp"
#include "gfx_func.h" #include "gfx_func.h"
#include "core/geometry_func.hpp" #include "core/geometry_func.hpp"
#include "core/string_consumer.hpp"
#include "language.h" #include "language.h"
#include "rev.h" #include "rev.h"
#include "highscore.h" #include "highscore.h"
@ -125,45 +126,46 @@ struct SelectGameWindow : public Window {
for (const Sign *sign : Sign::Iterate()) { for (const Sign *sign : Sign::Iterate()) {
std::smatch match; std::smatch match;
if (std::regex_search(sign->name, match, re)) { if (!std::regex_search(sign->name, match, re)) continue;
IntroGameViewportCommand vc;
/* Sequence index from the first matching group. */
vc.command_index = std::stoi(match[1].str());
/* Sign coordinates for positioning. */
vc.position = RemapCoords(sign->x, sign->y, sign->z);
/* Delay from the third matching group. */
vc.delay = std::stoi(match[3].str()) * 1000; // milliseconds
/* Parse flags from second matching group. */ IntroGameViewportCommand vc;
enum IdType : uint8_t { /* Sequence index from the first matching group. */
ID_NONE, ID_VEHICLE if (auto value = ParseInteger<int>(match[1].str()); value.has_value()) {
} id_type = ID_NONE; vc.command_index = *value;
for (char c : match[2].str()) { } else {
if (isdigit(c)) { continue;
if (id_type == ID_VEHICLE) {
vc.vehicle = static_cast<VehicleID>(vc.vehicle.base() * 10 + (c - '0'));
}
} else {
id_type = ID_NONE;
switch (toupper(c)) {
case '-': vc.zoom_adjust = +1; break;
case '+': vc.zoom_adjust = -1; break;
case 'T': vc.align_v = IntroGameViewportCommand::TOP; break;
case 'M': vc.align_v = IntroGameViewportCommand::MIDDLE; break;
case 'B': vc.align_v = IntroGameViewportCommand::BOTTOM; break;
case 'L': vc.align_h = IntroGameViewportCommand::LEFT; break;
case 'C': vc.align_h = IntroGameViewportCommand::CENTRE; break;
case 'R': vc.align_h = IntroGameViewportCommand::RIGHT; break;
case 'P': vc.pan_to_next = true; break;
case 'V': id_type = ID_VEHICLE; vc.vehicle = VehicleID::Begin(); break;
}
}
}
/* Successfully parsed, store. */
intro_viewport_commands.push_back(vc);
signs_to_delete.push_back(sign->index);
} }
/* Sign coordinates for positioning. */
vc.position = RemapCoords(sign->x, sign->y, sign->z);
/* Delay from the third matching group. */
if (auto value = ParseInteger<uint>(match[3].str()); value.has_value()) {
vc.delay = *value * 1000; // milliseconds
} else {
continue;
}
/* Parse flags from second matching group. */
auto flags = match[2].str();
StringConsumer consumer{flags};
while (consumer.AnyBytesLeft()) {
auto c = consumer.ReadUtf8();
switch (toupper(c)) {
case '-': vc.zoom_adjust = +1; break;
case '+': vc.zoom_adjust = -1; break;
case 'T': vc.align_v = IntroGameViewportCommand::TOP; break;
case 'M': vc.align_v = IntroGameViewportCommand::MIDDLE; break;
case 'B': vc.align_v = IntroGameViewportCommand::BOTTOM; break;
case 'L': vc.align_h = IntroGameViewportCommand::LEFT; break;
case 'C': vc.align_h = IntroGameViewportCommand::CENTRE; break;
case 'R': vc.align_h = IntroGameViewportCommand::RIGHT; break;
case 'P': vc.pan_to_next = true; break;
case 'V': vc.vehicle = static_cast<VehicleID>(consumer.ReadIntegerBase<uint32_t>(10, VehicleID::Invalid().base())); break;
}
}
/* Successfully parsed, store. */
intro_viewport_commands.push_back(vc);
signs_to_delete.push_back(sign->index);
} }
/* Sort the commands by sequence index. */ /* Sort the commands by sequence index. */

View File

@ -59,6 +59,13 @@
#define strtoll SAFEGUARD_DO_NOT_USE_THIS_METHOD #define strtoll SAFEGUARD_DO_NOT_USE_THIS_METHOD
#define strtoul SAFEGUARD_DO_NOT_USE_THIS_METHOD #define strtoul SAFEGUARD_DO_NOT_USE_THIS_METHOD
#define strtoull SAFEGUARD_DO_NOT_USE_THIS_METHOD #define strtoull SAFEGUARD_DO_NOT_USE_THIS_METHOD
#define stoi SAFEGUARD_DO_NOT_USE_THIS_METHOD
#define stol SAFEGUARD_DO_NOT_USE_THIS_METHOD
#define stoll SAFEGUARD_DO_NOT_USE_THIS_METHOD
#define stoul SAFEGUARD_DO_NOT_USE_THIS_METHOD
#define stoull SAFEGUARD_DO_NOT_USE_THIS_METHOD
#define stoimax SAFEGUARD_DO_NOT_USE_THIS_METHOD
#define stoumax SAFEGUARD_DO_NOT_USE_THIS_METHOD
/* Use fmt::print instead. */ /* Use fmt::print instead. */
#define printf SAFEGUARD_DO_NOT_USE_THIS_METHOD #define printf SAFEGUARD_DO_NOT_USE_THIS_METHOD

View File

@ -2863,18 +2863,17 @@ static std::pair<const SaveLoadFormat &, uint8_t> GetSavegameFormat(const std::s
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.compare(slf.name) == 0) {
if (has_comp_level) { if (has_comp_level) {
const std::string complevel(full_name, separator + 1); auto complevel = std::string_view(full_name).substr(separator + 1);
/* Get the level and determine whether all went fine. */ /* Get the level and determine whether all went fine. */
size_t processed; auto level = ParseInteger<uint8_t>(complevel);
long level = std::stol(complevel, &processed, 10); if (!level.has_value() || *level != Clamp(*level, slf.min_compression, slf.max_compression)) {
if (processed == 0 || level != Clamp(level, slf.min_compression, slf.max_compression)) {
ShowErrorMessage( ShowErrorMessage(
GetEncodedString(STR_CONFIG_ERROR), GetEncodedString(STR_CONFIG_ERROR),
GetEncodedString(STR_CONFIG_ERROR_INVALID_SAVEGAME_COMPRESSION_LEVEL, complevel), GetEncodedString(STR_CONFIG_ERROR_INVALID_SAVEGAME_COMPRESSION_LEVEL, complevel),
WL_CRITICAL); WL_CRITICAL);
} else { } else {
return {slf, ClampTo<uint8_t>(level)}; return {slf, *level};
} }
} }
return {slf, slf.default_compression}; return {slf, slf.default_compression};

View File

@ -14,6 +14,7 @@
#include "script_text.hpp" #include "script_text.hpp"
#include "script_log.hpp" #include "script_log.hpp"
#include "../script_fatalerror.hpp" #include "../script_fatalerror.hpp"
#include "../../core/string_consumer.hpp"
#include "../../table/control_codes.h" #include "../../table/control_codes.h"
#include "table/strings.h" #include "table/strings.h"
@ -141,7 +142,9 @@ SQInteger ScriptText::_set(HSQUIRRELVM vm)
std::string str = StrMakeValid(view); std::string str = StrMakeValid(view);
if (!str.starts_with("param_") || str.size() > 8) return SQ_ERROR; if (!str.starts_with("param_") || str.size() > 8) return SQ_ERROR;
k = stoi(str.substr(6)); auto key = ParseInteger<int32_t>(str.substr(6));
if (!key.has_value()) return SQ_ERROR;
k = *key;
} else if (sq_gettype(vm, 2) == OT_INTEGER) { } else if (sq_gettype(vm, 2) == OT_INTEGER) {
SQInteger key; SQInteger key;
sq_getinteger(vm, 2, &key); sq_getinteger(vm, 2, &key);