From 917ef03e9721ffeb7a16dc4c0759fdc1b0d1301f Mon Sep 17 00:00:00 2001 From: Rubidium Date: Mon, 21 Apr 2025 22:28:57 +0200 Subject: [PATCH] Codechange: use std::string_view in IConsole settings API --- src/console_cmds.cpp | 2 +- src/settings.cpp | 12 ++++++------ src/settings_func.h | 8 ++++---- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp index 1a7e1d6908..80df076665 100644 --- a/src/console_cmds.cpp +++ b/src/console_cmds.cpp @@ -2329,7 +2329,7 @@ static bool ConListSettings([[maybe_unused]] uint8_t argc, [[maybe_unused]] char if (argc > 2) return false; - IConsoleListSettings((argc == 2) ? argv[1] : nullptr); + IConsoleListSettings((argc == 2) ? argv[1] : std::string_view{}); return true; } diff --git a/src/settings.cpp b/src/settings.cpp index 5a1db8cf26..051b8620d6 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -1909,7 +1909,7 @@ void StringSettingDesc::ChangeValue(const void *object, std::string &&newval) co /* Those 2 functions need to be here, else we have to make some stuff non-static * and besides, it is also better to keep stuff like this at the same place */ -void IConsoleSetSetting(const char *name, const char *value, bool force_newgame) +void IConsoleSetSetting(std::string_view name, std::string_view value, bool force_newgame) { const SettingDesc *sd = GetSettingFromName(name); /* Company settings are not in "list_settings", so don't try to modify them. */ @@ -1941,7 +1941,7 @@ void IConsoleSetSetting(const char *name, const char *value, bool force_newgame) } } -void IConsoleSetSetting(const char *name, int value) +void IConsoleSetSetting(std::string_view name, int value) { const SettingDesc *sd = GetSettingFromName(name); assert(sd != nullptr); @@ -1953,7 +1953,7 @@ void IConsoleSetSetting(const char *name, int value) * @param name Name of the setting to output its value * @param force_newgame force the newgame settings */ -void IConsoleGetSetting(const char *name, bool force_newgame) +void IConsoleGetSetting(std::string_view name, bool force_newgame) { const SettingDesc *sd = GetSettingFromName(name); /* Company settings are not in "list_settings", so don't try to read them. */ @@ -1976,12 +1976,12 @@ void IConsoleGetSetting(const char *name, bool force_newgame) } } -static void IConsoleListSettingsTable(const SettingTable &table, const char *prefilter) +static void IConsoleListSettingsTable(const SettingTable &table, std::string_view prefilter) { for (auto &desc : table) { const SettingDesc *sd = GetSettingDesc(desc); if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue; - if (prefilter != nullptr && sd->GetName().find(prefilter) == std::string::npos) continue; + if (!prefilter.empty() && sd->GetName().find(prefilter) == std::string::npos) continue; IConsolePrint(CC_DEFAULT, "{} = {}", sd->GetName(), sd->FormatValue(&GetGameSettings())); } } @@ -1991,7 +1991,7 @@ static void IConsoleListSettingsTable(const SettingTable &table, const char *pre * * @param prefilter If not \c nullptr, only list settings with names that begin with \a prefilter prefix */ -void IConsoleListSettings(const char *prefilter) +void IConsoleListSettings(std::string_view prefilter) { IConsolePrint(CC_HELP, "All settings with their current value:"); diff --git a/src/settings_func.h b/src/settings_func.h index 034a37ef12..d08190d0f8 100644 --- a/src/settings_func.h +++ b/src/settings_func.h @@ -17,10 +17,10 @@ struct IniFile; struct WindowDesc; -void IConsoleSetSetting(const char *name, const char *value, bool force_newgame = false); -void IConsoleSetSetting(const char *name, int32_t value); -void IConsoleGetSetting(const char *name, bool force_newgame = false); -void IConsoleListSettings(const char *prefilter); +void IConsoleSetSetting(std::string_view name, std::string_view value, bool force_newgame = false); +void IConsoleSetSetting(std::string_view name, int32_t value); +void IConsoleGetSetting(std::string_view name, bool force_newgame = false); +void IConsoleListSettings(std::string_view prefilter); void LoadFromConfig(bool minimal = false); void SaveToConfig();