1
0
Fork 0

Codechange: make start-ai console command parsing work with std::string_view

pull/14105/head
Rubidium 2025-04-25 00:05:16 +02:00 committed by rubidium42
parent ccbf7f4a46
commit 08ce16018b
3 changed files with 10 additions and 12 deletions

View File

@ -1504,17 +1504,15 @@ static bool ConStartAI([[maybe_unused]] uint8_t argc, [[maybe_unused]] char *arg
* try again with the assumption everything right of the dot is * try again with the assumption everything right of the dot is
* the version the user wants to load. */ * the version the user wants to load. */
if (!config->HasScript()) { if (!config->HasScript()) {
const char *e = strrchr(argv[1], '.'); StringConsumer consumer{std::string_view{argv[1]}};
if (e != nullptr) { auto name = consumer.ReadUntilChar('.', StringConsumer::SKIP_ONE_SEPARATOR);
size_t name_length = e - argv[1]; if (consumer.AnyBytesLeft()) {
e++; auto version = consumer.TryReadIntegerBase<uint32_t>(10);
auto version = ParseInteger(e);
if (!version.has_value()) { if (!version.has_value()) {
IConsolePrint(CC_ERROR, "The version is not a valid number."); IConsolePrint(CC_ERROR, "The version is not a valid number.");
return true; return true;
} }
config->Change(std::string(argv[1], name_length), *version, true); config->Change(name, *version, true);
} }
} }

View File

@ -20,10 +20,10 @@
#include "../safeguards.h" #include "../safeguards.h"
void ScriptConfig::Change(std::optional<std::string> name, int version, bool force_exact_match) void ScriptConfig::Change(std::optional<std::string_view> name, int version, bool force_exact_match)
{ {
if (name.has_value()) { if (name.has_value()) {
this->name = std::move(name.value()); this->name = name.value();
this->info = this->FindInfo(this->name, version, force_exact_match); this->info = this->FindInfo(this->name, version, force_exact_match);
} else { } else {
this->info = nullptr; this->info = nullptr;
@ -140,7 +140,7 @@ int ScriptConfig::GetVersion() const
return this->version; return this->version;
} }
void ScriptConfig::StringToSettings(const std::string &value) void ScriptConfig::StringToSettings(std::string_view value)
{ {
std::string_view to_process = value; std::string_view to_process = value;
for (;;) { for (;;) {

View File

@ -78,7 +78,7 @@ public:
* @param force_exact_match If true try to find the exact same version * @param force_exact_match If true try to find the exact same version
* as specified. If false any compatible version is ok. * as specified. If false any compatible version is ok.
*/ */
void Change(std::optional<std::string> name, int version = -1, bool force_exact_match = false); void Change(std::optional<std::string_view> name, int version = -1, bool force_exact_match = false);
/** /**
* Get the ScriptInfo linked to this ScriptConfig. * Get the ScriptInfo linked to this ScriptConfig.
@ -154,7 +154,7 @@ public:
* Convert a string which is stored in the config file or savegames to * Convert a string which is stored in the config file or savegames to
* custom settings of this Script. * custom settings of this Script.
*/ */
void StringToSettings(const std::string &value); void StringToSettings(std::string_view value);
/** /**
* Convert the custom settings to a string that can be stored in the config * Convert the custom settings to a string that can be stored in the config