1
0
Fork 0

Codechange: rewrite script string-to-settings conversion to C++

pull/10801/head
Rubidium 2023-05-06 12:44:47 +02:00 committed by rubidium42
parent 552d2f71a2
commit 48825e1a8e
4 changed files with 20 additions and 23 deletions

View File

@ -14,6 +14,7 @@
#include "api/script_object.hpp" #include "api/script_object.hpp"
#include "../textfile_gui.h" #include "../textfile_gui.h"
#include "../string_func.h" #include "../string_func.h"
#include <charconv>
#include "../safeguards.h" #include "../safeguards.h"
@ -102,7 +103,7 @@ int ScriptConfig::GetSetting(const std::string &name) const
return (*it).second; return (*it).second;
} }
void ScriptConfig::SetSetting(const std::string &name, int value) void ScriptConfig::SetSetting(const std::string_view name, int value)
{ {
/* You can only set Script specific settings if an Script is selected. */ /* You can only set Script specific settings if an Script is selected. */
if (this->info == nullptr) return; if (this->info == nullptr) return;
@ -112,7 +113,7 @@ void ScriptConfig::SetSetting(const std::string &name, int value)
value = Clamp(value, config_item->min_value, config_item->max_value); value = Clamp(value, config_item->min_value, config_item->max_value);
this->settings[name] = value; this->settings[std::string{name}] = value;
} }
void ScriptConfig::ResetSettings() void ScriptConfig::ResetSettings()
@ -170,28 +171,24 @@ int ScriptConfig::GetVersion() const
void ScriptConfig::StringToSettings(const std::string &value) void ScriptConfig::StringToSettings(const std::string &value)
{ {
char *value_copy = stredup(value.c_str()); std::string_view to_process = value;
char *s = value_copy; for (;;) {
while (s != nullptr) {
/* Analyze the string ('name=value,name=value\0') */ /* Analyze the string ('name=value,name=value\0') */
char *item_name = s; size_t pos = to_process.find_first_of('=');
s = strchr(s, '='); if (pos == std::string_view::npos) return;
if (s == nullptr) break;
if (*s == '\0') break;
*s = '\0';
s++;
char *item_value = s; std::string_view item_name = to_process.substr(0, pos);
s = strchr(s, ',');
if (s != nullptr) {
*s = '\0';
s++;
}
this->SetSetting(item_name, atoi(item_value)); to_process.remove_prefix(pos + 1);
pos = to_process.find_first_of(',');
int item_value = 0;
std::from_chars(to_process.data(), to_process.data() + std::min(pos, to_process.size()), item_value);
this->SetSetting(item_name, item_value);
if (pos == std::string_view::npos) return;
to_process.remove_prefix(pos + 1);
} }
free(value_copy);
} }
std::string ScriptConfig::SettingsToString() const std::string ScriptConfig::SettingsToString() const

View File

@ -127,7 +127,7 @@ public:
/** /**
* Set the value of a setting for this config. * Set the value of a setting for this config.
*/ */
void SetSetting(const std::string &name, int value); void SetSetting(const std::string_view name, int value);
/** /**
* Reset all settings to their default value. * Reset all settings to their default value.

View File

@ -254,7 +254,7 @@ const ScriptConfigItemList *ScriptInfo::GetConfigList() const
return &this->config_list; return &this->config_list;
} }
const ScriptConfigItem *ScriptInfo::GetConfigItem(const std::string &name) const const ScriptConfigItem *ScriptInfo::GetConfigItem(const std::string_view name) const
{ {
for (const auto &item : this->config_list) { for (const auto &item : this->config_list) {
if (item.name == name) return &item; if (item.name == name) return &item;

View File

@ -122,7 +122,7 @@ public:
/** /**
* Get the description of a certain Script config option. * Get the description of a certain Script config option.
*/ */
const ScriptConfigItem *GetConfigItem(const std::string &name) const; const ScriptConfigItem *GetConfigItem(const std::string_view name) const;
/** /**
* Set a setting. * Set a setting.